Skip to content

Commit 6869b09

Browse files
author
gefeili
committed
Refactor ElephantEngine xorTo
1 parent a0dd6f0 commit 6869b09

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,9 @@ private void computerCipherBlock(byte[] input, int inOff, int blockSize, byte[]
343343
{
344344
System.arraycopy(npub, 0, buffer, 0, IV_SIZE);
345345
Arrays.fill(buffer, IV_SIZE, BlockSize, (byte)0);
346-
Bytes.xorTo(BlockSize, current_mask, buffer);
347-
Bytes.xorTo(BlockSize, next_mask, buffer);
346+
xorTo(BlockSize, current_mask, next_mask, buffer);
348347
instance.permutation(buffer);
349-
Bytes.xorTo(BlockSize, current_mask, buffer);
350-
Bytes.xorTo(BlockSize, next_mask, buffer);
348+
xorTo(BlockSize, current_mask, next_mask, buffer);
351349

352350
Bytes.xorTo(blockSize, input, inOff, buffer);
353351
System.arraycopy(buffer, 0, output, outOff, blockSize);
@@ -372,11 +370,9 @@ private void absorbAAD()
372370

373371
private void absorbCiphertext()
374372
{
375-
Bytes.xorTo(BlockSize, previous_mask, buffer);
376-
Bytes.xorTo(BlockSize, next_mask, buffer);
373+
xorTo(BlockSize, previous_mask, next_mask, buffer);
377374
instance.permutation(buffer);
378-
Bytes.xorTo(BlockSize, previous_mask, buffer);
379-
Bytes.xorTo(BlockSize, next_mask, buffer);
375+
xorTo(BlockSize, previous_mask, next_mask, buffer);
380376
Bytes.xorTo(BlockSize, buffer, tag_buffer);
381377
}
382378

@@ -601,7 +597,7 @@ private void processBytes(byte[] m, byte[] output, int outOff, int nb_it, int nb
601597
// If clen is divisible by BLOCK_SIZE, add an additional padding block
602598
if (block_offset == mlen)
603599
{
604-
Arrays.fill(buffer, 0, BlockSize, (byte)0);
600+
Arrays.fill(buffer, 1, BlockSize, (byte)0);
605601
buffer[0] = 0x01;
606602
}
607603
else
@@ -637,4 +633,12 @@ private void processBytes(byte[] m, byte[] output, int outOff, int nb_it, int nb
637633
}
638634
nb_its = i;
639635
}
636+
637+
public static void xorTo(int len, byte[] x, byte[] y, byte[] z)
638+
{
639+
for (int i = 0; i < len; ++i)
640+
{
641+
z[i] ^= x[i] ^ y[i];
642+
}
643+
}
640644
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GiftCofbEngine
1717
private byte[] input;
1818
private byte[] offset;
1919
/*Round constants*/
20-
private final byte[] GIFT_RC = {
20+
private static final byte[] GIFT_RC = {
2121
(byte)0x01, (byte)0x03, (byte)0x07, (byte)0x0F, (byte)0x1F, (byte)0x3E, (byte)0x3D, (byte)0x3B, (byte)0x37, (byte)0x2F,
2222
(byte)0x1E, (byte)0x3C, (byte)0x39, (byte)0x33, (byte)0x27, (byte)0x0E, (byte)0x1D, (byte)0x3A, (byte)0x35, (byte)0x2B,
2323
(byte)0x16, (byte)0x2C, (byte)0x18, (byte)0x30, (byte)0x21, (byte)0x02, (byte)0x05, (byte)0x0B, (byte)0x17, (byte)0x2E,
@@ -28,7 +28,7 @@ public GiftCofbEngine()
2828
{
2929
AADBufferSize = BlockSize = MAC_SIZE = IV_SIZE = KEY_SIZE = 16;
3030
algorithmName = "GIFT-COFB AEAD";
31-
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Counter, DataOperatorType.Counter);
31+
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Default, DataOperatorType.Counter);
3232
}
3333

3434
private int rowperm(int S, int B0_pos, int B1_pos, int B2_pos, int B3_pos)
@@ -205,8 +205,7 @@ protected void processFinalAAD()
205205
/* full byte[]: offset = 3*offset */
206206
/* partial byte[]: offset = 3^2*offset */
207207
triple_half_block(offset, offset);
208-
int aadLen = aadOperator.getLen();
209-
if (((aadLen & 15) != 0) || m_state == State.DecInit || m_state == State.EncInit)
208+
if (((m_aadPos & 15) != 0) || m_state == State.DecInit || m_state == State.EncInit)
210209
{
211210
triple_half_block(offset, offset);
212211
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ public void processBufferDecrypt(byte[] input, int inOff, byte[] output, int out
442442
@Override
443443
public void reset()
444444
{
445-
Arrays.clear(CNT);
446445
Arrays.clear(s);
447446
reset_lfsr_gf56(CNT);
448447
twist = true;
@@ -622,15 +621,14 @@ public void processBufferDecrypt(byte[] input, int inOff, byte[] output, int out
622621
@Override
623622
public void reset()
624623
{
625-
Arrays.clear(Z);
626624
Arrays.clear(h);
627625
Arrays.clear(g);
628626
Arrays.clear(LR);
629627
Arrays.clear(T);
630628
Arrays.clear(S);
631629
Arrays.clear(CNT_Z);
632630
reset_lfsr_gf56(CNT);
633-
System.arraycopy(npub, 0, Z, 0, 16);
631+
System.arraycopy(npub, 0, Z, 0, IV_SIZE);
634632
block_cipher(Z, k, T, 0, CNT_Z, (byte)66);
635633
reset_lfsr_gf56(CNT_Z);
636634
}

0 commit comments

Comments
 (0)