Skip to content

Commit 476db85

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

File tree

5 files changed

+24
-49
lines changed

5 files changed

+24
-49
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/PhotonBeetleDigest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ private Friend()
2424
}
2525

2626
private final byte[] state;
27-
private final byte[][] state_2d;
2827
private static final int SQUEEZE_RATE_INBYTES = 16;
2928
private static final int D = 8;
3029
private int blockCount;
@@ -34,7 +33,6 @@ public PhotonBeetleDigest()
3433
super(ProcessingBufferType.Buffered, 4);
3534
DigestSize = 32;
3635
state = new byte[DigestSize];
37-
state_2d = new byte[D][D];
3836
algorithmName = "Photon-Beetle Hash";
3937
blockCount = 0;
4038
}
@@ -48,7 +46,7 @@ protected void processBytes(byte[] input, int inOff)
4846
}
4947
else
5048
{
51-
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
49+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state);
5250
Bytes.xorTo(BlockSize, input, inOff, state);
5351
}
5452
blockCount++;
@@ -74,17 +72,17 @@ else if (blockCount == 4 && m_bufPos == 0)
7472
}
7573
else
7674
{
77-
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
75+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state);
7876
Bytes.xorTo(m_bufPos, m_buf, state);
7977
if (m_bufPos < BlockSize)
8078
{
8179
state[m_bufPos] ^= 0x01; // ozs
8280
}
8381
state[DigestSize - 1] ^= (m_bufPos % BlockSize == 0 ? (byte)1 : (byte)2) << LAST_THREE_BITS_OFFSET;
8482
}
85-
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
83+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state);
8684
System.arraycopy(state, 0, output, outOff, SQUEEZE_RATE_INBYTES);
87-
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
85+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state);
8886
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, SQUEEZE_RATE_INBYTES);
8987
}
9088

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void KeccakP200Round(byte[] state, int indexRound)
260260

261261
private byte ROL8(byte a, int offset)
262262
{
263-
return (byte)((offset != 0) ? (((a & 0xFF) << offset) ^ ((a & 0xFF) >>> (8 - offset))) : a);
263+
return (byte)(((a & 0xff) << offset) | ((a & 0xff) >> (8 - offset)));
264264
}
265265

266266
private int index(int x, int y)
@@ -274,7 +274,6 @@ private byte rotl(byte b)
274274
return (byte)(((b & 0xFF) << 1) | ((b & 0xFF) >>> 7));
275275
}
276276

277-
278277
// State should be BLOCK_SIZE bytes long
279278
// Note: input may be equal to output
280279
private void lfsr_step()

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,15 @@ private void isap_rk(AsconPermutationFriend.AsconPermutation p, long iv64, byte[
167167

168168
public void processEncBlock(byte[] input, int inOff, byte[] output, int outOff)
169169
{
170-
long m64 = Pack.littleEndianToLong(input, inOff);
171-
long c64 = U64BIG(p.x0) ^ m64;
170+
Pack.longToBigEndian(Pack.bigEndianToLong(input, inOff) ^ p.x0, output, outOff);
172171
PX1(p);
173-
Pack.longToLittleEndian(c64, output, outOff);
174172
}
175173

176174
public void processEncFinalBlock(byte[] output, int outOff)
177175
{
178176
/* Encrypt final m block */
179177
byte[] xo = Pack.longToLittleEndian(p.x0);
180-
int mlen = m_bufPos;
181-
Bytes.xor(mlen, xo, BlockSize - mlen, m_buf, 0, output, outOff);
178+
Bytes.xor(m_bufPos, xo, BlockSize - m_bufPos, m_buf, 0, output, outOff);
182179
}
183180

184181
public void reset()
@@ -197,12 +194,6 @@ private int getLongSize(int x)
197194
{
198195
return ((x + 7) >>> 3);
199196
}
200-
201-
protected long U64BIG(long x)
202-
{
203-
return ((Longs.rotateRight(x, 8) & (0xFF000000FF000000L)) | (Longs.rotateRight(x, 24) & (0x00FF000000FF0000L)) |
204-
(Longs.rotateRight(x, 40) & (0x0000FF000000FF00L)) | (Longs.rotateRight(x, 56) & (0x000000FF000000FFL)));
205-
}
206197
}
207198

208199
private class ISAPAEAD_A_128A
@@ -250,7 +241,7 @@ protected void PX2(AsconPermutationFriend.AsconPermutation p)
250241
private abstract class ISAPAEAD_K
251242
implements ISAP_AEAD
252243
{
253-
final int ISAP_STATE_SZ_CRYPTO_NPUBBYTES = ISAP_STATE_SZ - IV_SIZE;
244+
protected final int ISAP_STATE_SZ_CRYPTO_NPUBBYTES = ISAP_STATE_SZ - IV_SIZE;
254245
protected short[] ISAP_IV1_16;
255246
protected short[] ISAP_IV2_16;
256247
protected short[] ISAP_IV3_16;

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public enum PhotonBeetleParameters
2424
private byte[] K;
2525
private byte[] N;
2626
private byte[] state;
27-
private byte[][] state_2d;
2827
private final int RATE_INBYTES_HALF;
2928
private final int STATE_INBYTES;
3029
private final int LAST_THREE_BITS_OFFSET;
@@ -84,15 +83,14 @@ protected void init(byte[] key, byte[] iv)
8483
K = key;
8584
N = iv;
8685
state = new byte[STATE_INBYTES];
87-
state_2d = new byte[D][D];
8886
m_state = forEncryption ? State.EncInit : State.DecInit;
8987
reset(false);
9088
}
9189

9290

9391
protected void processBufferAAD(byte[] input, int inOff)
9492
{
95-
photonPermutation(state_2d, state);
93+
photonPermutation(state);
9694
Bytes.xorTo(BlockSize, input, inOff, state);
9795
}
9896

@@ -126,7 +124,7 @@ public void processFinalAAD()
126124
{
127125
if (m_aadPos != 0)
128126
{
129-
photonPermutation(state_2d, state);
127+
photonPermutation(state);
130128
Bytes.xorTo(m_aadPos, m_aad, state);
131129
if (m_aadPos < BlockSize)
132130
{
@@ -140,14 +138,12 @@ public void processFinalAAD()
140138

141139
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
142140
{
143-
photonPermutation(state_2d, state);
144141
rhoohr(output, outOff, input, inOff, BlockSize);
145142
Bytes.xorTo(BlockSize, input, inOff, state);
146143
}
147144

148145
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
149146
{
150-
photonPermutation(state_2d, state);
151147
rhoohr(output, outOff, input, inOff, BlockSize);
152148
Bytes.xorTo(BlockSize, output, outOff, state);
153149
}
@@ -168,7 +164,6 @@ protected void processFinalBlock(byte[] output, int outOff)
168164
{
169165
if (bufferLen != 0)
170166
{
171-
photonPermutation(state_2d, state);
172167
rhoohr(output, outOff, m_buf, 0, bufferLen);
173168
if (forEncryption)
174169
{
@@ -185,11 +180,11 @@ protected void processFinalBlock(byte[] output, int outOff)
185180
}
186181
state[STATE_INBYTES - 1] ^= c1 << LAST_THREE_BITS_OFFSET;
187182
}
188-
if (input_empty)
183+
else if (input_empty)
189184
{
190185
state[STATE_INBYTES - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
191186
}
192-
photonPermutation(state_2d, state);
187+
photonPermutation(state);
193188
System.arraycopy(state, 0, mac, 0, MAC_SIZE);
194189
}
195190

@@ -203,12 +198,13 @@ protected void reset(boolean clearMac)
203198
super.reset(clearMac);
204199
}
205200

206-
private static void photonPermutation(byte[][] state_2d, byte[] state)
201+
private static void photonPermutation(byte[] state)
207202
{
208203
int i, j, k;
209204
int dq = 3;
210205
int dr = 7;
211206
int DSquare = 64;
207+
byte[][] state_2d = new byte[D][D];
212208
for (i = 0; i < DSquare; i++)
213209
{
214210
state_2d[i >>> dq][i & dr] = (byte)(((state[i >> 1] & 0xFF) >>> (4 * (i & 1))) & 0xf);
@@ -292,31 +288,26 @@ private byte select(boolean condition1, boolean condition2, byte option3, byte o
292288

293289
private void rhoohr(byte[] ciphertext, int outOff, byte[] plaintext, int inOff, int DBlen_inbytes)
294290
{
295-
byte[] OuterState_part1_ROTR1 = state_2d[0];
291+
photonPermutation(state);
292+
byte[] OuterState_part1_ROTR1 = new byte[D];
296293
int i, loop_end = Math.min(DBlen_inbytes, RATE_INBYTES_HALF);
297294
for (i = 0; i < RATE_INBYTES_HALF - 1; i++)
298295
{
299296
OuterState_part1_ROTR1[i] = (byte)(((state[i] & 0xFF) >>> 1) | ((state[(i + 1)] & 1) << 7));
300297
}
301298
OuterState_part1_ROTR1[RATE_INBYTES_HALF - 1] = (byte)(((state[i] & 0xFF) >>> 1) | ((state[0] & 1) << 7));
302-
i = 0;
303-
while (i < loop_end)
304-
{
305-
ciphertext[i + outOff] = (byte)(state[i + RATE_INBYTES_HALF] ^ plaintext[i++ + inOff]);
306-
}
307-
while (i < DBlen_inbytes)
308-
{
309-
ciphertext[i + outOff] = (byte)(OuterState_part1_ROTR1[i - RATE_INBYTES_HALF] ^ plaintext[i++ + inOff]);
310-
}
299+
Bytes.xor(loop_end, state, RATE_INBYTES_HALF, plaintext, inOff, ciphertext, outOff);
300+
Bytes.xor(DBlen_inbytes - loop_end, OuterState_part1_ROTR1, loop_end - RATE_INBYTES_HALF, plaintext,
301+
inOff + loop_end, ciphertext, outOff + loop_end);
311302
}
312303

313-
public static void photonPermutation(PhotonBeetleDigest.Friend friend, byte[][] state_2d, byte[] state)
304+
public static void photonPermutation(PhotonBeetleDigest.Friend friend, byte[] state)
314305
{
315306
if (null == friend)
316307
{
317308
throw new NullPointerException("This method is only for use by PhotonBeetleDigest");
318309
}
319310

320-
photonPermutation(state_2d, state);
311+
photonPermutation(state);
321312
}
322-
}
313+
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,12 @@ int ad_encryption(byte[] A, int AOff, byte[] s, byte[] k, int adlen, byte[] CNT,
245245
byte[] T = new byte[16];
246246
byte[] mp = new byte[16];
247247
int n = 16;
248-
int i, len8;
248+
int len8;
249249
len8 = Math.min(adlen, n);
250250
adlen -= len8;
251251
// Rho(S,A) pads an A block and XORs it to the internal state.
252252
pad(A, AOff, mp, n, len8);
253-
for (i = 0; i < n; i++)
254-
{
255-
s[i] = (byte)(s[i] ^ mp[i]);
256-
}
253+
Bytes.xorTo(n, mp, s);
257254
offset = AOff += len8;
258255
lfsr_gf56(CNT);
259256
if (adlen != 0)
@@ -793,7 +790,6 @@ void rho(byte[] m, int mOff, byte[] c, int cOff, byte[] s, int len8)
793790
}
794791
}
795792
}
796-
797793
}
798794

799795
// Applies CNT'=2 * CNT (mod GF(2^56)), where GF(2^56) is defined using the irreducible polynomial

0 commit comments

Comments
 (0)