-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAscon.cs
More file actions
172 lines (137 loc) · 4.58 KB
/
Copy pathAscon.cs
File metadata and controls
172 lines (137 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.IO;
namespace HashFunctions
{
internal class Ascon
{
public Ascon()
{
}
public string CreateFromFile(String fileName, int pa, int pb) // pa=12, pb=12
{
ulong[] state = new ulong[5];
FileStream fileStream = File.OpenRead(fileName);
state = Initialization(state, pa);
state = Absorbing(state, fileStream, pb);
string H = Squeezing(state, pa, pb);
string result = H.ToLower();
fileStream.Close();
return result;
}
private ulong[] Initialization(ulong[] state, int pa)
{
state[0] = 0x00400c0000000100; // Initialisation vector
Permutation(state, pa);
return state;
}
private ulong[] Absorbing(ulong[] state, FileStream m, int p)
{
int rByte = 8;
int mLastBlockLength = (int)m.Length % rByte;
long mBlockCount = m.Length / rByte;
byte[] mBlockBytes = new byte[rByte];
BufferedStream bufferedStream = new BufferedStream(m);
if (mBlockCount == 0)
{
bufferedStream.Read(mBlockBytes, 0, rByte);
state[0] ^= Tools.BytesToUlong(Padding(mBlockBytes, mLastBlockLength));
}
else
{
for (int i = 0; i < mBlockCount; i++)
{
bufferedStream.Read(mBlockBytes, 0, rByte);
state[0] ^= Tools.BytesToUlong(mBlockBytes);
state = Permutation(state, 12);
}
bufferedStream.Read(mBlockBytes, 0, rByte);
state[0] ^= Tools.BytesToUlong(Padding(mBlockBytes, mLastBlockLength));
}
return state;
}
private byte[] Padding(byte[] mBlock, int mLastBlockLength)
{
byte[] tempBlock = new byte[8];
for (int i = 0; i < mLastBlockLength; i++)
tempBlock[i] = mBlock[i];
tempBlock[mLastBlockLength] = 128;
return tempBlock;
}
private unsafe ulong[] Permutation(ulong[] state, int p)
{
for (int i = 0; i < p; i++)
{
state = AdditionOfConstants(state, i, p);
state = SubstitutionLayer(state);
state = LinearDiffusionLayer(state);
}
return state; ;
}
private ulong[] AdditionOfConstants(ulong[] state, int i, int p)
{
ulong[] cr = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b };
state[2] ^= cr[i + 12 - p];
return state;
}
private ulong[] SubstitutionLayer(ulong[] x)
{
ulong[] t = new ulong[5];
x[0] ^= x[4];
x[4] ^= x[3];
x[2] ^= x[1];
t[0] = x[0];
t[1] = x[1];
t[2] = x[2];
t[3] = x[3];
t[4] = x[4];
t[0] = ~t[0];
t[1] = ~t[1];
t[2] = ~t[2];
t[3] = ~t[3];
t[4] = ~t[4];
t[0] &= x[1];
t[1] &= x[2];
t[2] &= x[3];
t[3] &= x[4];
t[4] &= x[0];
x[0] ^= t[1];
x[1] ^= t[2];
x[2] ^= t[3];
x[3] ^= t[4];
x[4] ^= t[0];
x[1] ^= x[0];
x[0] ^= x[4];
x[3] ^= x[2];
x[2] = ~x[2];
return x;
}
private ulong[] LinearDiffusionLayer(ulong[] state)
{
ulong temp1 = new ulong();
ulong temp2 = new ulong();
int[,] bits = { { 19, 28 }, { 61, 39 }, { 1, 6 }, { 10, 17 }, { 7, 41 } };
for (int x = 0; x < state.Length; x++)
{
temp1 = RotateRight(state[x], bits[x, 0]);
temp2 = RotateRight(state[x], bits[x, 1]);
state[x] ^= temp1 ^ temp2;
}
return state;
}
private ulong RotateRight(ulong value, int bits)
{
return (value >> bits | value << (64 - bits));
}
private string Squeezing(ulong[] state, int pa, int pb)
{
string[] h = new string[4];
state = Permutation(state, pa);
for (int i = 0; i < 4; i++)
{
h[i] = state[0].ToString("X").PadLeft(16, '0');
state = Permutation(state, pb);
}
return h[0] + h[1] + h[2] + h[3];
}
}
}