Skip to content

Commit 0713ee7

Browse files
author
gefeili
committed
Refactor in Engines
1 parent f12f73b commit 0713ee7

File tree

4 files changed

+41
-65
lines changed

4 files changed

+41
-65
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/GiftCofbEngine.java

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,32 @@ private void giftb128(byte[] P, byte[] K, byte[] C)
116116
C[15] = (byte)(S[3]);
117117
}
118118

119-
private void xor_topbar_block(byte[] d, byte[] s1, byte[] s2)
119+
private void double_half_block(byte[] s)
120120
{
121-
Bytes.xor(8, s1, s2, d);
122-
System.arraycopy(s1, 8, d, 8, 8);
121+
int mask = ((s[0] & 0xFF) >>> 7) * 27;
122+
/*x^{64} + x^4 + x^3 + x + 1*/
123+
for (int i = 0; i < 7; i++)
124+
{
125+
s[i] = (byte)(((s[i] & 0xFF) << 1) | ((s[i + 1] & 0xFF) >>> 7));
126+
}
127+
s[7] = (byte)(((s[7] & 0xFF) << 1) ^ mask);
123128
}
124129

125-
private void double_half_block(byte[] d, byte[] s)
130+
private void triple_half_block(byte[] s)
126131
{
127-
int i;
128132
byte[] tmp = new byte[8];
129133
/*x^{64} + x^4 + x^3 + x + 1*/
130-
for (i = 0; i < 7; i++)
134+
for (int i = 0; i < 7; i++)
131135
{
132136
tmp[i] = (byte)(((s[i] & 0xFF) << 1) | ((s[i + 1] & 0xFF) >>> 7));
133137
}
134138
tmp[7] = (byte)(((s[7] & 0xFF) << 1) ^ (((s[0] & 0xFF) >>> 7) * 27));
135-
System.arraycopy(tmp, 0, d, 0, 8);
136-
}
137-
138-
private void triple_half_block(byte[] d, byte[] s)
139-
{
140-
byte[] tmp = new byte[8];
141-
double_half_block(tmp, s);
142-
Bytes.xor(8, s, tmp, d);
139+
Bytes.xorTo(8, tmp, s);
143140
}
144141

145142
private void pho1(byte[] d, byte[] Y, byte[] M, int mOff, int no_of_bytes)
146143
{
147144
byte[] tmpM = new byte[16];
148-
//padding(tmpM, M, mOff, no_of_bytes);
149145
byte[] tmp = new byte[16];
150146
if (no_of_bytes == 0)
151147
{
@@ -160,11 +156,10 @@ else if (no_of_bytes < 16)
160156
{
161157
System.arraycopy(M, mOff, tmpM, 0, no_of_bytes);
162158
}
163-
int i;
164159
//G(Y, Y);
165160
/*Y[1],Y[2] -> Y[2],Y[1]<<<1*/
166161
System.arraycopy(Y, 8, tmp, 0, 8);
167-
for (i = 0; i < 7; i++)
162+
for (int i = 0; i < 7; i++)
168163
{
169164
tmp[i + 8] = (byte)((Y[i] & 0xFF) << 1 | (Y[i + 1] & 0xFF) >>> 7);
170165
}
@@ -173,25 +168,13 @@ else if (no_of_bytes < 16)
173168
Bytes.xor(16, Y, tmpM, d);
174169
}
175170

176-
private void pho(byte[] Y, byte[] M, int mOff, byte[] X, byte[] C, int cOff, int no_of_bytes)
177-
{
178-
Bytes.xor(no_of_bytes, Y, M, mOff, C, cOff);
179-
pho1(X, Y, M, mOff, no_of_bytes);
180-
}
181-
182-
private void phoprime(byte[] Y, byte[] C, int cOff, byte[] X, byte[] M, int mOff, int no_of_bytes)
183-
{
184-
Bytes.xor(no_of_bytes, Y, C, cOff, M, mOff);
185-
pho1(X, Y, M, mOff, no_of_bytes);
186-
}
187-
188171
@Override
189172
protected void processBufferAAD(byte[] in, int inOff)
190173
{
191174
pho1(input, Y, in, inOff, 16);
192175
/* offset = 2*offset */
193-
double_half_block(offset, offset);
194-
xor_topbar_block(input, input, offset);
176+
double_half_block(offset);
177+
Bytes.xorTo(8, offset, input);
195178
/* Y[i] = E(X[i]) */
196179
giftb128(input, k, Y);
197180
}
@@ -203,20 +186,20 @@ protected void processFinalAAD()
203186
/* last byte[] */
204187
/* full byte[]: offset = 3*offset */
205188
/* partial byte[]: offset = 3^2*offset */
206-
triple_half_block(offset, offset);
189+
triple_half_block(offset);
207190
if (((m_aadPos & 15) != 0) || m_state == State.DecInit || m_state == State.EncInit)
208191
{
209-
triple_half_block(offset, offset);
192+
triple_half_block(offset);
210193
}
211194
if (len == 0)
212195
{
213196
/* empty M: offset = 3^2*offset */
214-
triple_half_block(offset, offset);
215-
triple_half_block(offset, offset);
197+
triple_half_block(offset);
198+
triple_half_block(offset);
216199
}
217200
/* X[i] = (pad(A[i]) + G(Y[i-1])) + offset */
218201
pho1(input, Y, m_aad, 0, m_aadPos);
219-
xor_topbar_block(input, input, offset);
202+
Bytes.xorTo(8, offset, input);
220203
/* Y[a] = E(X[a]) */
221204
giftb128(input, k, Y);
222205
}
@@ -255,50 +238,49 @@ protected void init(byte[] key, byte[] iv)
255238
reset(false);
256239
}
257240

258-
259241
@Override
260242
protected void processFinalBlock(byte[] output, int outOff)
261243
{
262-
int inOff = 0;
263244
int len = dataOperator.getLen() - (forEncryption ? 0 : MAC_SIZE);
264245
if (len != 0)
265246
{
266247
/* full block: offset = 3*offset */
267248
/* empty data / partial block: offset = 3^2*offset */
268-
triple_half_block(offset, offset);
249+
triple_half_block(offset);
269250
if ((len & 15) != 0)
270251
{
271-
triple_half_block(offset, offset);
252+
triple_half_block(offset);
272253
}
273254
/* last block */
274255
/* C[m] = Y[m+a-1] + M[m]*/
275256
/* X[a+m] = M[m] + G(Y[m+a-1]) + offset */
257+
Bytes.xor(m_bufPos, Y, m_buf, 0, output, outOff);
276258
if (forEncryption)
277259
{
278-
pho(Y, m_buf, inOff, input, output, outOff, m_bufPos);
260+
pho1(input, Y, m_buf, 0, m_bufPos);
279261
}
280262
else
281263
{
282-
phoprime(Y, m_buf, inOff, input, output, outOff, m_bufPos);
264+
pho1(input, Y, output, outOff, m_bufPos);
283265
}
284-
xor_topbar_block(input, input, offset);
266+
Bytes.xorTo(8, offset, input);
285267
/* T = E(X[m+a]) */
286268
giftb128(input, k, Y);
287269
}
288270
System.arraycopy(Y, 0, mac, 0, BlockSize);
289271
}
290272

291-
292273
@Override
293274
protected void processBufferEncrypt(byte[] inputM, int inOff, byte[] output, int outOff)
294275
{
295276
/* Process M */
296277
/* full byte[]s */
297-
double_half_block(offset, offset);
278+
double_half_block(offset);
298279
/* C[i] = Y[i+a-1] + M[i]*/
299280
/* X[i] = M[i] + G(Y[i+a-1]) + offset */
300-
pho(Y, inputM, inOff, input, output, outOff, BlockSize);
301-
xor_topbar_block(input, input, offset);
281+
Bytes.xor(BlockSize, Y, inputM, inOff, output, outOff);
282+
pho1(input, Y, inputM, inOff, BlockSize);
283+
Bytes.xorTo(8, offset, input);
302284
/* Y[i] = E(X[i+a]) */
303285
giftb128(input, k, Y);
304286
}
@@ -308,11 +290,12 @@ protected void processBufferDecrypt(byte[] inputM, int inOff, byte[] output, int
308290
{
309291
/* Process M */
310292
/* full byte[]s */
311-
double_half_block(offset, offset);
293+
double_half_block(offset);
312294
/* C[i] = Y[i+a-1] + M[i]*/
313295
/* X[i] = M[i] + G(Y[i+a-1]) + offset */
314-
phoprime(Y, inputM, inOff, input, output, outOff, BlockSize);
315-
xor_topbar_block(input, input, offset);
296+
Bytes.xor(BlockSize, Y, inputM, inOff, output, outOff);
297+
pho1(input, Y, output, outOff, BlockSize);
298+
Bytes.xorTo(8, offset, input);
316299
/* Y[i] = E(X[i+a]) */
317300
giftb128(input, k, Y);
318301
}

core/src/main/java/org/bouncycastle/crypto/engines/RomulusEngine.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private class RomulusM
123123
private final byte[] mac_s = new byte[16];
124124
private final byte[] mac_CNT = new byte[7];
125125
private final byte[] s = new byte[16];
126-
int offset;
127-
boolean twist = true;
126+
private int offset;
127+
private boolean twist = true;
128128

129129
public RomulusM()
130130
{
@@ -339,7 +339,6 @@ private class RomulusN
339339
public RomulusN()
340340
{
341341
s = new byte[AD_BLK_LEN_HALF];
342-
twist = true;
343342
}
344343

345344
@Override
@@ -825,7 +824,6 @@ void block_cipher(byte[] s, byte[] K, byte[] T, int tOff, byte[] CNT, byte D)
825824
// Combines the secret key, counter and domain bits to form the full 384-bit tweakey
826825
System.arraycopy(CNT, 0, KT, 0, 7);
827826
KT[7] = D;
828-
Arrays.fill(KT, 8, 16, (byte)0x00);
829827
System.arraycopy(T, tOff, KT, 16, 16);
830828
System.arraycopy(K, 0, KT, 32, 16);
831829
skinny_128_384_plus_enc(s, KT);

core/src/main/java/org/bouncycastle/crypto/engines/SparkleEngine.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.crypto.engines;
22

33
import org.bouncycastle.crypto.digests.SparkleDigest;
4+
import org.bouncycastle.util.Arrays;
45
import org.bouncycastle.util.Bytes;
56
import org.bouncycastle.util.Integers;
67
import org.bouncycastle.util.Pack;
@@ -282,11 +283,8 @@ protected void processFinalAAD()
282283
state[STATE_WORDS - 1] ^= _A0;
283284

284285
// padding
285-
m_aad[m_aadPos] = (byte)0x80;
286-
while (++m_aadPos < BlockSize)
287-
{
288-
m_aad[m_aadPos] = 0x00;
289-
}
286+
m_aad[m_aadPos++] = (byte)0x80;
287+
Arrays.fill(m_aad, m_aadPos, BlockSize, (byte)0);
290288
}
291289
else
292290
{

core/src/main/java/org/bouncycastle/crypto/engines/XoodyakEngine.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ protected void finishAAD(State nextState, boolean isDoFinal)
8888

8989
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
9090
{
91-
int Cu = encrypted ? 0 : 0x80;
92-
up(mode, state, Cu); /* Up without extract */
91+
up(mode, state, encrypted ? 0 : 0x80); /* Up without extract */
9392
/* Extract from Up and Add */
9493
Bytes.xor(BlockSize, state, input, inOff, output, outOff);
9594
down(mode, state, input, inOff, BlockSize, 0x00);
@@ -99,8 +98,7 @@ protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int
9998

10099
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
101100
{
102-
int Cu = encrypted ? 0 : 0x80;
103-
up(mode, state, Cu); /* Up without extract */
101+
up(mode, state, encrypted ? 0 : 0x80); /* Up without extract */
104102
/* Extract from Up and Add */
105103
Bytes.xor(BlockSize, state, input, inOff, output, outOff);
106104
down(mode, state, output, outOff, BlockSize, 0x00);
@@ -111,10 +109,9 @@ protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int
111109
@Override
112110
protected void processFinalBlock(byte[] output, int outOff)
113111
{
114-
int Cu = encrypted ? 0 : 0x80;
115112
if (m_bufPos != 0 || !encrypted)
116113
{
117-
up(mode, state, Cu); /* Up without extract */
114+
up(mode, state, encrypted ? 0 : 0x80); /* Up without extract */
118115
/* Extract from Up and Add */
119116
Bytes.xor(m_bufPos, state, m_buf, 0, output, outOff);
120117
if (forEncryption)

0 commit comments

Comments
 (0)