Skip to content

Commit 6391634

Browse files
author
gefeili
committed
Refactor on Engines around xor
1 parent 8c39ea1 commit 6391634

File tree

8 files changed

+76
-98
lines changed

8 files changed

+76
-98
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Arrays;
44

5+
import org.bouncycastle.util.Bytes;
6+
57
/**
68
* Elephant AEAD v2, based on the current round 3 submission, https://www.esat.kuleuven.be/cosic/elephant/
79
* Reference C implementation: https://github.com/TimBeyne/Elephant
@@ -283,14 +285,6 @@ private void lfsr_step()
283285
System.arraycopy(current_mask, 1, next_mask, 0, BlockSize - 1);
284286
}
285287

286-
private void xor_block(byte[] state, byte[] block, int bOff, int size)
287-
{
288-
for (int i = 0; i < size; ++i)
289-
{
290-
state[i] ^= block[i + bOff];
291-
}
292-
}
293-
294288
@Override
295289
protected void init(byte[] k, byte[] iv)
296290
throws IllegalArgumentException
@@ -349,13 +343,13 @@ private void computerCipherBlock(byte[] input, int inOff, int blockSize, byte[]
349343
{
350344
System.arraycopy(npub, 0, buffer, 0, IV_SIZE);
351345
Arrays.fill(buffer, IV_SIZE, BlockSize, (byte)0);
352-
xor_block(buffer, current_mask, 0, BlockSize);
353-
xor_block(buffer, next_mask, 0, BlockSize);
346+
Bytes.xorTo(BlockSize, current_mask, buffer);
347+
Bytes.xorTo(BlockSize, next_mask, buffer);
354348
instance.permutation(buffer);
355-
xor_block(buffer, current_mask, 0, BlockSize);
356-
xor_block(buffer, next_mask, 0, BlockSize);
349+
Bytes.xorTo(BlockSize, current_mask, buffer);
350+
Bytes.xorTo(BlockSize, next_mask, buffer);
357351

358-
xor_block(buffer, input, inOff, blockSize);
352+
Bytes.xorTo(blockSize, input, inOff, buffer);
359353
System.arraycopy(buffer, 0, output, outOff, blockSize);
360354
}
361355

@@ -370,20 +364,20 @@ private void swapMasks()
370364
private void absorbAAD()
371365
{
372366
processAADBytes(buffer);
373-
xor_block(buffer, next_mask, 0, BlockSize);
367+
Bytes.xorTo(BlockSize, next_mask, buffer);
374368
instance.permutation(buffer);
375-
xor_block(buffer, next_mask, 0, BlockSize);
376-
xor_block(tag_buffer, buffer, 0, BlockSize);
369+
Bytes.xorTo(BlockSize, next_mask, buffer);
370+
Bytes.xorTo(BlockSize, buffer, tag_buffer);
377371
}
378372

379373
private void absorbCiphertext()
380374
{
381-
xor_block(buffer, previous_mask, 0, BlockSize);
382-
xor_block(buffer, next_mask, 0, BlockSize);
375+
Bytes.xorTo(BlockSize, previous_mask, buffer);
376+
Bytes.xorTo(BlockSize, next_mask, buffer);
383377
instance.permutation(buffer);
384-
xor_block(buffer, previous_mask, 0, BlockSize);
385-
xor_block(buffer, next_mask, 0, BlockSize);
386-
xor_block(tag_buffer, buffer, 0, BlockSize);
378+
Bytes.xorTo(BlockSize, previous_mask, buffer);
379+
Bytes.xorTo(BlockSize, next_mask, buffer);
380+
Bytes.xorTo(BlockSize, buffer, tag_buffer);
387381
}
388382

389383
protected void processFinalBlock(byte[] output, int outOff)
@@ -396,9 +390,9 @@ protected void processFinalBlock(byte[] output, int outOff)
396390
int nb_it = Math.max(nblocks_c + 1, nblocks_ad - 1);
397391
processBytes(m_buf, output, outOff, nb_it, nblocks_m, nblocks_c, mlen, nblocks_ad);
398392
mac = new byte[MAC_SIZE];
399-
xor_block(tag_buffer, expanded_key, 0, BlockSize);
393+
Bytes.xorTo(BlockSize, expanded_key, tag_buffer);
400394
instance.permutation(tag_buffer);
401-
xor_block(tag_buffer, expanded_key, 0, BlockSize);
395+
Bytes.xorTo(BlockSize, expanded_key, tag_buffer);
402396
System.arraycopy(tag_buffer, 0, mac, 0, MAC_SIZE);
403397
}
404398

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

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

3+
import org.bouncycastle.util.Bytes;
4+
35
/**
46
* GIFT-COFB v1.1, based on the current round 3 submission, https://www.isical.ac.in/~lightweight/COFB/
57
* Reference C implementation: https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-submissions/elephant.zip
@@ -114,20 +116,9 @@ private void giftb128(byte[] P, byte[] K, byte[] C)
114116
C[15] = (byte)(S[3]);
115117
}
116118

117-
private void xor_block(byte[] d, int dOff, byte[] s1, byte[] s2, int s2Off, int no_of_bytes)
118-
{
119-
for (int i = 0; i < no_of_bytes; i++)
120-
{
121-
d[i + dOff] = (byte)(s1[i] ^ s2[i + s2Off]);
122-
}
123-
}
124-
125119
private void xor_topbar_block(byte[] d, byte[] s1, byte[] s2)
126120
{
127-
for (int i = 0; i < 8; i++)
128-
{
129-
d[i] = (byte)(s1[i] ^ s2[i]);
130-
}
121+
Bytes.xor(8, s1, s2, d);
131122
System.arraycopy(s1, 8, d, 8, 8);
132123
}
133124

@@ -148,10 +139,7 @@ private void triple_half_block(byte[] d, byte[] s)
148139
{
149140
byte[] tmp = new byte[8];
150141
double_half_block(tmp, s);
151-
for (int i = 0; i < 8; i++)
152-
{
153-
d[i] = (byte)(s[i] ^ tmp[i]);
154-
}
142+
Bytes.xor(8, s, tmp, d);
155143
}
156144

157145
private void pho1(byte[] d, byte[] Y, byte[] M, int mOff, int no_of_bytes)
@@ -182,18 +170,19 @@ else if (no_of_bytes < 16)
182170
}
183171
tmp[15] = (byte)((Y[7] & 0xFF) << 1 | (Y[0] & 0xFF) >>> 7);
184172
System.arraycopy(tmp, 0, Y, 0, 16);
185-
xor_block(d, 0, Y, tmpM, 0, 16);
173+
Bytes.xor(16, Y, tmpM, d);
186174
}
187175

188176
private void pho(byte[] Y, byte[] M, int mOff, byte[] X, byte[] C, int cOff, int no_of_bytes)
189177
{
190-
xor_block(C, cOff, Y, M, mOff, no_of_bytes);
178+
Bytes.xor(no_of_bytes, Y, M, mOff, C, cOff);
191179
pho1(X, Y, M, mOff, no_of_bytes);
192180
}
193181

194182
private void phoprime(byte[] Y, byte[] C, int cOff, byte[] X, byte[] M, int mOff, int no_of_bytes)
195183
{
196-
xor_block(M, mOff, Y, C, cOff, no_of_bytes);
184+
Bytes.xor(no_of_bytes, Y, C, cOff, M, mOff);
185+
//xor_block(M, mOff, Y, C, cOff, no_of_bytes);
197186
pho1(X, Y, M, mOff, no_of_bytes);
198187
}
199188

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ public ISAPAEAD_K()
338338
public void init()
339339
{
340340
k16 = new short[k.length >> 1];
341-
byteToShort(k, k16, k16.length);
341+
Pack.littleEndianToShort(k, 0, k16, 0, k16.length);
342342
iv16 = new short[npub.length >> 1];
343-
byteToShort(npub, iv16, iv16.length);
343+
Pack.littleEndianToShort(npub, 0, iv16, 0, iv16.length);
344344
//reset();
345345
}
346346

@@ -428,11 +428,11 @@ public void processMACFinal(byte[] input, int inOff, int len, byte[] tag)
428428
SX[len >> 1] ^= 0x80 << ((len & 1) << 3);
429429
PermuteRoundsHX(SX, E, C);
430430
// Derive K*
431-
shortToByte(SX, tag);
431+
Pack.shortToLittleEndian(SX, 0, 8, tag, 0);
432432
isap_rk(ISAP_IV2_16, tag, KEY_SIZE, SX, KEY_SIZE, C);
433433
// Squeeze tag
434434
PermuteRoundsHX(SX, E, C);
435-
shortToByte(SX, tag);
435+
Pack.shortToLittleEndian(SX, 0, 8, tag, 0);
436436
}
437437

438438
public void processEncBlock(byte[] input, int inOff, byte[] output, int outOff)
@@ -462,22 +462,6 @@ private void byteToShortXor(byte[] input, int inOff, short[] output, int outLen)
462462
}
463463
}
464464

465-
private void byteToShort(byte[] input, short[] output, int outLen)
466-
{
467-
for (int i = 0; i < outLen; ++i)
468-
{
469-
output[i] = Pack.littleEndianToShort(input, (i << 1));
470-
}
471-
}
472-
473-
private void shortToByte(short[] input, byte[] output)
474-
{
475-
for (int i = 0; i < 8; ++i)
476-
{
477-
Pack.shortToLittleEndian(input[i], output, (i << 1));
478-
}
479-
}
480-
481465
protected void rounds12X(short[] SX, short[] E, short[] C)
482466
{
483467
prepareThetaX(SX, C);

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

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

33
import org.bouncycastle.util.Arrays;
4+
import org.bouncycastle.util.Bytes;
45

56
/**
67
* Romulus v1.3, based on the current round 3 submission, https://romulusae.github.io/romulus/
@@ -274,10 +275,7 @@ public void processBufferAAD(byte[] input, int inOff)
274275
{
275276
if (twist)
276277
{
277-
for (int i = 0; i < 16; i++)
278-
{
279-
mac_s[i] = (byte)(mac_s[i] ^ input[inOff + i]);
280-
}
278+
Bytes.xorTo(MAC_SIZE, input, inOff, mac_s);
281279
}
282280
else
283281
{
@@ -301,10 +299,7 @@ else if (m_aadPos != 0)
301299
m_aad[BlockSize - 1] = (byte)(m_aadPos & 0x0f);
302300
if (twist)
303301
{
304-
for (int i = 0; i < BlockSize; i++)
305-
{
306-
mac_s[i] = (byte)(mac_s[i] ^ m_aad[i]);
307-
}
302+
Bytes.xorTo(BlockSize, m_aad, mac_s);
308303
}
309304
else
310305
{
@@ -374,10 +369,7 @@ public void processBufferAAD(byte[] input, int inOff)
374369
{
375370
if (twist)
376371
{
377-
for (int i = 0; i < AD_BLK_LEN_HALF; i++)
378-
{
379-
s[i] = (byte)(s[i] ^ input[inOff + i]);
380-
}
372+
Bytes.xorTo(AD_BLK_LEN_HALF, input, inOff, s);
381373
}
382374
else
383375
{
@@ -397,10 +389,7 @@ public void processFinalAAD()
397389
pad(m_aad, 0, mp, AD_BLK_LEN_HALF, len8);
398390
if (twist)
399391
{
400-
for (int i = 0; i < AD_BLK_LEN_HALF; i++)
401-
{
402-
s[i] = (byte)(s[i] ^ mp[i]);
403-
}
392+
Bytes.xorTo(AD_BLK_LEN_HALF, mp, s);
404393
}
405394
else
406395
{
@@ -483,10 +472,7 @@ public void processFinalBlock(byte[] output, int outOff)
483472
int len8 = Math.min(m_bufPos, 16);
484473
System.arraycopy(npub, 0, S, 0, 16);
485474
block_cipher(S, Z, T, 0, CNT, (byte)64);
486-
for (int i = 0; i < len8; i++)
487-
{
488-
output[i + outOff] = (byte)((m_buf[i]) ^ S[i]);
489-
}
475+
Bytes.xor(len8, m_buf, S, output, outOff);
490476
System.arraycopy(npub, 0, S, 0, 16);
491477

492478
lfsr_gf56(CNT);
@@ -587,10 +573,7 @@ public void processBufferEncrypt(byte[] input, int inOff, byte[] output, int out
587573
{
588574
System.arraycopy(npub, 0, S, 0, 16);
589575
block_cipher(S, Z, T, 0, CNT, (byte)64);
590-
for (int i = 0; i < AD_BLK_LEN_HALF; i++)
591-
{
592-
output[i + outOff] = (byte)((input[i + inOff]) ^ S[i]);
593-
}
576+
Bytes.xor(AD_BLK_LEN_HALF, S, input, inOff, output, outOff);
594577
System.arraycopy(npub, 0, S, 0, 16);
595578
block_cipher(S, Z, T, 0, CNT, (byte)65);
596579
System.arraycopy(S, 0, Z, 0, 16);
@@ -615,10 +598,7 @@ public void processBufferDecrypt(byte[] input, int inOff, byte[] output, int out
615598
{
616599
System.arraycopy(npub, 0, S, 0, 16);
617600
block_cipher(S, Z, T, 0, CNT, (byte)64);
618-
for (int i = 0; i < AD_BLK_LEN_HALF; i++)
619-
{
620-
output[i + outOff] = (byte)((input[i + inOff]) ^ S[i]);
621-
}
601+
Bytes.xor(AD_BLK_LEN_HALF, S, input, inOff, output, outOff);
622602
System.arraycopy(npub, 0, S, 0, 16);
623603
block_cipher(S, Z, T, 0, CNT, (byte)65);
624604
System.arraycopy(S, 0, Z, 0, 16);

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

Lines changed: 1 addition & 0 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.Bytes;
45
import org.bouncycastle.util.Integers;
56
import org.bouncycastle.util.Pack;
67

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

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

33
import org.bouncycastle.util.Arrays;
4+
import org.bouncycastle.util.Bytes;
45
import org.bouncycastle.util.Integers;
56
import org.bouncycastle.util.Pack;
67

@@ -95,10 +96,8 @@ private void encrypt(byte[] input, int inOff, int len, byte[] output, int outOff
9596
System.arraycopy(input, inOff, P, 0, splitLen);
9697
Up(null, 0, Cu); /* Up without extract */
9798
/* Extract from Up and Add */
98-
for (int i = 0; i < splitLen; i++)
99-
{
100-
output[outOff + i] = (byte)(input[inOff++] ^ state[i]);
101-
}
99+
Bytes.xor(splitLen, state, input, inOff, output, outOff);
100+
inOff += splitLen;
102101
Down(P, 0, splitLen, 0x00);
103102
Cu = 0x00;
104103
outOff += splitLen;
@@ -116,10 +115,8 @@ private void decrypt(byte[] input, int inOff, int len, byte[] output, int outOff
116115
splitLen = Math.min(len, BlockSize); /* use Rkout instead of Rsqueeze, this function is only called in keyed mode */
117116
Up(null, 0, Cu); /* Up without extract */
118117
/* Extract from Up and Add */
119-
for (int i = 0; i < splitLen; i++)
120-
{
121-
output[outOff + i] = (byte)(input[inOff++] ^ state[i]);
122-
}
118+
Bytes.xor(splitLen, state, input, inOff, output, outOff);
119+
inOff += splitLen;
123120
Down(output, outOff, splitLen, 0x00);
124121
Cu = 0x00;
125122
outOff += splitLen;

core/src/main/java/org/bouncycastle/util/Bytes.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ public static void xor(int len, byte[] x, int xOff, byte[] y, int yOff, byte[] z
2424
}
2525
}
2626

27+
public static void xor(int len, byte[] x, byte[] y, byte[] z, int zOff)
28+
{
29+
for (int i = 0; i < len; ++i)
30+
{
31+
z[zOff++] = (byte)(x[i] ^ y[i]);
32+
}
33+
}
34+
35+
public static void xor(int len, byte[] x, byte[] y, int yOff, byte[] z, int zOff)
36+
{
37+
for (int i = 0; i < len; ++i)
38+
{
39+
z[zOff++] = (byte)(x[i] ^ y[yOff++]);
40+
}
41+
}
42+
2743
public static void xorTo(int len, byte[] x, byte[] z)
2844
{
2945
for (int i = 0; i < len; ++i)

core/src/main/java/org/bouncycastle/util/Pack.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public static short littleEndianToShort(byte[] bs, int off)
175175
return (short)n;
176176
}
177177

178+
public static void littleEndianToShort(byte[] bs, int bOff, short[] ns, int nOff, int count)
179+
{
180+
for (int i = 0; i < count; ++i)
181+
{
182+
ns[nOff + i] = littleEndianToShort(bs, bOff);
183+
bOff += 2;
184+
}
185+
}
186+
178187
public static int littleEndianToInt(byte[] bs, int off)
179188
{
180189
int n = bs[off] & 0xff;
@@ -245,6 +254,14 @@ public static void shortToLittleEndian(short n, byte[] bs, int off)
245254
bs[++off] = (byte)(n >>> 8);
246255
}
247256

257+
public static void shortToLittleEndian(short[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
258+
{
259+
for (int i = 0; i < nsLen; ++i)
260+
{
261+
shortToLittleEndian(ns[nsOff + i], bs, bsOff);
262+
bsOff += 2;
263+
}
264+
}
248265

249266
public static byte[] shortToBigEndian(short n)
250267
{

0 commit comments

Comments
 (0)