Skip to content

Commit c9efa52

Browse files
committed
Noekeon improvements
- check key size in Init - perf. opts.
1 parent 4f2f534 commit c9efa52

File tree

1 file changed

+58
-54
lines changed

1 file changed

+58
-54
lines changed

crypto/src/crypto/engines/NoekeonEngine.cs

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,39 @@ public virtual void Init(bool forEncryption, ICipherParameters parameters)
6060
throw new ArgumentException("Invalid parameters passed to Noekeon init - "
6161
+ Platform.GetTypeName(parameters), "parameters");
6262

63-
this._forEncryption = forEncryption;
64-
this._initialised = true;
65-
6663
KeyParameter p = (KeyParameter) parameters;
64+
byte[] key = p.GetKey();
65+
if (key.Length != 16)
66+
throw new ArgumentException("Key length not 128 bits.");
6767

68-
Pack.BE_To_UInt32(p.GetKey(), 0, k, 0, 4);
68+
Pack.BE_To_UInt32(key, 0, k, 0, 4);
6969

7070
if (!forEncryption)
7171
{
7272
// theta(k, new uint[]{ 0x00, 0x00, 0x00, 0x00 });
7373
{
7474
uint a0 = k[0], a1 = k[1], a2 = k[2], a3 = k[3];
7575

76-
uint t = a0 ^ a2;
77-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
78-
a1 ^= t;
79-
a3 ^= t;
76+
uint t02 = a0 ^ a2;
77+
t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
78+
79+
uint t13 = a1 ^ a3;
80+
t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
8081

81-
t = a1 ^ a3;
82-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
83-
a0 ^= t;
84-
a2 ^= t;
82+
a0 ^= t13;
83+
a1 ^= t02;
84+
a2 ^= t13;
85+
a3 ^= t02;
8586

86-
k[0] = a0; k[1] = a1; k[2] = a2; k[3] = a3;
87+
k[0] = a0; k[1] = a1; k[2] = a2; k[3] = a3;
8788
}
8889
}
89-
}
9090

91-
public virtual int ProcessBlock(
91+
this._forEncryption = forEncryption;
92+
this._initialised = true;
93+
}
94+
95+
public virtual int ProcessBlock(
9296
byte[] input,
9397
int inOff,
9498
byte[] output,
@@ -119,27 +123,27 @@ private int EncryptBlock(byte[] input, int inOff, byte[] output, int outOff)
119123
uint k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
120124

121125
int round = 0;
122-
uint t;
123126
for (;;)
124127
{
125128
a0 ^= RoundConstants[round];
126129

127130
// theta(a, k);
128131
{
129-
t = a0 ^ a2;
130-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
131-
a1 ^= t;
132-
a3 ^= t;
132+
uint t02 = a0 ^ a2;
133+
t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
133134

134135
a0 ^= k0;
135136
a1 ^= k1;
136137
a2 ^= k2;
137138
a3 ^= k3;
138139

139-
t = a1 ^ a3;
140-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
141-
a0 ^= t;
142-
a2 ^= t;
140+
uint t13 = a1 ^ a3;
141+
t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
142+
143+
a0 ^= t13;
144+
a1 ^= t02;
145+
a2 ^= t13;
146+
a3 ^= t02;
143147
}
144148

145149
if (++round > Size)
@@ -154,14 +158,14 @@ private int EncryptBlock(byte[] input, int inOff, byte[] output, int outOff)
154158

155159
// gamma(a);
156160
{
157-
a1 ^= ~a3 & ~a2;
158-
a0 ^= a2 & a1;
161+
uint t = a3;
162+
a1 ^= a3 | a2;
163+
a3 = a0 ^ (a2 & ~a1);
159164

160-
t = a3; a3 = a0; a0 = t;
161-
a2 ^= a0 ^ a1 ^ a3;
165+
a2 = t ^ ~a1 ^ a2 ^ a3;
162166

163-
a1 ^= ~a3 & ~a2;
164-
a0 ^= a2 & a1;
167+
a1 ^= a3 | a2;
168+
a0 = t ^ (a2 & a1);
165169
}
166170

167171
// pi2(a);
@@ -190,28 +194,28 @@ private int DecryptBlock(byte[] input, int inOff, byte[] output, int outOff)
190194
uint k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
191195

192196
int round = Size;
193-
uint t;
194197
for (;;)
195198
{
196199
// theta(a, k);
197200
{
198-
t = a0 ^ a2;
199-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
200-
a1 ^= t;
201-
a3 ^= t;
201+
uint t02 = a0 ^ a2;
202+
t02 ^= Integers.RotateLeft(t02, 8) ^ Integers.RotateLeft(t02, 24);
202203

203-
a0 ^= k0;
204-
a1 ^= k1;
205-
a2 ^= k2;
206-
a3 ^= k3;
204+
a0 ^= k0;
205+
a1 ^= k1;
206+
a2 ^= k2;
207+
a3 ^= k3;
207208

208-
t = a1 ^ a3;
209-
t ^= Integers.RotateLeft(t, 8) ^ Integers.RotateLeft(t, 24);
210-
a0 ^= t;
211-
a2 ^= t;
212-
}
209+
uint t13 = a1 ^ a3;
210+
t13 ^= Integers.RotateLeft(t13, 8) ^ Integers.RotateLeft(t13, 24);
213211

214-
a0 ^= RoundConstants[round];
212+
a0 ^= t13;
213+
a1 ^= t02;
214+
a2 ^= t13;
215+
a3 ^= t02;
216+
}
217+
218+
a0 ^= RoundConstants[round];
215219

216220
if (--round < 0)
217221
break;
@@ -225,18 +229,18 @@ private int DecryptBlock(byte[] input, int inOff, byte[] output, int outOff)
225229

226230
// gamma(a);
227231
{
228-
a1 ^= ~a3 & ~a2;
229-
a0 ^= a2 & a1;
232+
uint t = a3;
233+
a1 ^= a3 | a2;
234+
a3 = a0 ^ (a2 & ~a1);
230235

231-
t = a3; a3 = a0; a0 = t;
232-
a2 ^= a0 ^ a1 ^ a3;
236+
a2 = t ^ ~a1 ^ a2 ^ a3;
233237

234-
a1 ^= ~a3 & ~a2;
235-
a0 ^= a2 & a1;
236-
}
238+
a1 ^= a3 | a2;
239+
a0 = t ^ (a2 & a1);
240+
}
237241

238-
// pi2(a);
239-
{
242+
// pi2(a);
243+
{
240244
a1 = Integers.RotateLeft(a1, 31);
241245
a2 = Integers.RotateLeft(a2, 27);
242246
a3 = Integers.RotateLeft(a3, 30);

0 commit comments

Comments
 (0)