Skip to content

Commit 5b2e973

Browse files
author
gefeili
committed
refactor around engines
1 parent 9f3d11c commit 5b2e973

File tree

9 files changed

+61
-56
lines changed

9 files changed

+61
-56
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected void processBytes(byte[] input, int inOff)
4848
}
4949
else
5050
{
51-
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
51+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
5252
Bytes.xorTo(BlockSize, input, inOff, state);
5353
}
5454
blockCount++;
@@ -74,17 +74,17 @@ else if (blockCount == 4 && m_bufPos == 0)
7474
}
7575
else
7676
{
77-
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
77+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
7878
Bytes.xorTo(m_bufPos, m_buf, state);
7979
if (m_bufPos < BlockSize)
8080
{
8181
state[m_bufPos] ^= 0x01; // ozs
8282
}
8383
state[DigestSize - 1] ^= (m_bufPos % BlockSize == 0 ? (byte)1 : (byte)2) << LAST_THREE_BITS_OFFSET;
8484
}
85-
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
85+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
8686
System.arraycopy(state, 0, output, outOff, SQUEEZE_RATE_INBYTES);
87-
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
87+
PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, state_2d, state);
8888
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, SQUEEZE_RATE_INBYTES);
8989
}
9090

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -768,24 +768,7 @@ protected boolean checkData(boolean isDoFinal)
768768
}
769769
}
770770

771-
protected void finishAAD(State nextState, boolean isDoFinal)
772-
{
773-
// State indicates whether we ever received AAD
774-
switch (m_state)
775-
{
776-
case DecAad:
777-
case EncAad:
778-
{
779-
processFinalAAD();
780-
break;
781-
}
782-
default:
783-
break;
784-
}
785-
786-
m_aadPos = 0;
787-
m_state = nextState;
788-
}
771+
protected abstract void finishAAD(State nextState, boolean isDoFinal);
789772

790773
protected void bufferReset()
791774
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public AsconEngine(AsconParameters asconParameters)
6161
throw new IllegalArgumentException("invalid parameter setting for ASCON AEAD");
6262
}
6363
nr = (BlockSize == 8) ? 6 : 8;
64-
m_bufferSizeDecrypt = BlockSize + MAC_SIZE;
6564
AADBufferSize = BlockSize;
6665
dsep = 1L;
6766
setInnerMembers(asconParameters == AsconParameters.ascon128a ? ProcessingBufferType.Immediate : ProcessingBufferType.ImmediateLargeMac, AADOperatorType.Default, DataOperatorType.Default);

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private void processBuffer(byte[] input, int inOff, byte[] output, int outOff, S
312312
lfsr_step();
313313

314314
// Compute ciphertext block
315-
computerCipherBlock(input, inOff, BlockSize, output, outOff);
315+
computeCipherBlock(input, inOff, BlockSize, output, outOff);
316316

317317
if (nb_its > 0)
318318
{
@@ -337,7 +337,7 @@ protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int
337337
System.arraycopy(input, inOff, previous_outputMessage, 0, BlockSize);
338338
}
339339

340-
private void computerCipherBlock(byte[] input, int inOff, int blockSize, byte[] output, int outOff)
340+
private void computeCipherBlock(byte[] input, int inOff, int blockSize, byte[] output, int outOff)
341341
{
342342
System.arraycopy(npub, 0, buffer, 0, IV_SIZE);
343343
Arrays.fill(buffer, IV_SIZE, BlockSize, (byte)0);
@@ -440,6 +440,24 @@ public int getOutputSize(int len)
440440
return Math.max(0, len + m_bufPos - MAC_SIZE);
441441
}
442442

443+
protected void finishAAD(State nextState, boolean isDoFinal)
444+
{
445+
// State indicates whether we ever received AAD
446+
switch (m_state)
447+
{
448+
case DecAad:
449+
case EncAad:
450+
{
451+
processFinalAAD();
452+
break;
453+
}
454+
default:
455+
break;
456+
}
457+
458+
m_aadPos = 0;
459+
m_state = nextState;
460+
}
443461

444462
@Override
445463
protected void processFinalAAD()
@@ -575,7 +593,7 @@ private void processBytes(byte[] m, byte[] output, int outOff, int nb_it, int nb
575593
if (i < nblocks_m)
576594
{
577595
// Compute ciphertext block
578-
computerCipherBlock(m, rv, r_size, output, outOff);
596+
computeCipherBlock(m, rv, r_size, output, outOff);
579597
if (forEncryption)
580598
{
581599
System.arraycopy(buffer, 0, outputMessage, 0, r_size);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ public void init()
277277
Pack.littleEndianToShort(k, 0, k16, 0, k16.length);
278278
iv16 = new short[npub.length >> 1];
279279
Pack.littleEndianToShort(npub, 0, iv16, 0, iv16.length);
280-
//reset();
281280
}
282281

283282
public void reset()
@@ -369,8 +368,7 @@ public void processEncBlock(byte[] input, int inOff, byte[] output, int outOff)
369368
public void processEncFinalBlock(byte[] output, int outOff)
370369
{
371370
// Squeeze full or partial lane and stop
372-
int len = m_bufPos;
373-
for (int i = 0; i < len; ++i)
371+
for (int i = 0; i < m_bufPos; ++i)
374372
{
375373
output[outOff++] = (byte)((SX[i >> 1] >>> ((i & 1) << 3)) ^ m_buf[i]);
376374
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ public enum PhotonBeetleParameters
5454

5555
public PhotonBeetleEngine(PhotonBeetleParameters pbp)
5656
{
57-
KEY_SIZE = 16;
58-
IV_SIZE = 16;
59-
MAC_SIZE = 16;
57+
KEY_SIZE = IV_SIZE = MAC_SIZE = 16;
6058
int CAPACITY_INBITS = 0, RATE_INBITS = 0;
6159
switch (pbp)
6260
{
@@ -94,7 +92,7 @@ protected void init(byte[] key, byte[] iv)
9492

9593
protected void processBufferAAD(byte[] input, int inOff)
9694
{
97-
PhotonPermutation(state_2d, state);
95+
photonPermutation(state_2d, state);
9896
Bytes.xorTo(BlockSize, input, inOff, state);
9997
}
10098

@@ -128,7 +126,7 @@ public void processFinalAAD()
128126
{
129127
if (m_aadPos != 0)
130128
{
131-
PhotonPermutation(state_2d, state);
129+
photonPermutation(state_2d, state);
132130
Bytes.xorTo(m_aadPos, m_aad, state);
133131
if (m_aadPos < BlockSize)
134132
{
@@ -142,14 +140,14 @@ public void processFinalAAD()
142140

143141
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
144142
{
145-
PhotonPermutation(state_2d, state);
143+
photonPermutation(state_2d, state);
146144
rhoohr(output, outOff, input, inOff, BlockSize);
147145
Bytes.xorTo(BlockSize, input, inOff, state);
148146
}
149147

150148
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
151149
{
152-
PhotonPermutation(state_2d, state);
150+
photonPermutation(state_2d, state);
153151
rhoohr(output, outOff, input, inOff, BlockSize);
154152
Bytes.xorTo(BlockSize, output, outOff, state);
155153
}
@@ -170,7 +168,7 @@ protected void processFinalBlock(byte[] output, int outOff)
170168
{
171169
if (bufferLen != 0)
172170
{
173-
PhotonPermutation(state_2d, state);
171+
photonPermutation(state_2d, state);
174172
rhoohr(output, outOff, m_buf, 0, bufferLen);
175173
if (forEncryption)
176174
{
@@ -191,7 +189,7 @@ protected void processFinalBlock(byte[] output, int outOff)
191189
{
192190
state[STATE_INBYTES - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
193191
}
194-
PhotonPermutation(state_2d, state);
192+
photonPermutation(state_2d, state);
195193
System.arraycopy(state, 0, mac, 0, MAC_SIZE);
196194
}
197195

@@ -205,7 +203,7 @@ protected void reset(boolean clearMac)
205203
super.reset(clearMac);
206204
}
207205

208-
private static void PhotonPermutation(byte[][] state_2d, byte[] state)
206+
private static void photonPermutation(byte[][] state_2d, byte[] state)
209207
{
210208
int i, j, k;
211209
int dq = 3;
@@ -312,13 +310,13 @@ private void rhoohr(byte[] ciphertext, int outOff, byte[] plaintext, int inOff,
312310
}
313311
}
314312

315-
public static void PhotonPermutation(PhotonBeetleDigest.Friend friend, byte[][] state_2d, byte[] state)
313+
public static void photonPermutation(PhotonBeetleDigest.Friend friend, byte[][] state_2d, byte[] state)
316314
{
317315
if (null == friend)
318316
{
319317
throw new NullPointerException("This method is only for use by PhotonBeetleDigest");
320318
}
321319

322-
PhotonPermutation(state_2d, state);
320+
photonPermutation(state_2d, state);
323321
}
324322
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ private class RomulusM
122122
{
123123
private final byte[] mac_s = new byte[16];
124124
private final byte[] mac_CNT = new byte[7];
125-
126125
private final byte[] s = new byte[16];
127-
private final byte[] CNT = new byte[7];
128126
int offset;
129127
boolean twist = true;
130128

@@ -844,12 +842,7 @@ void nonce_encryption(byte[] N, byte[] CNT, byte[] s, byte[] k, byte D)
844842
private void reset_lfsr_gf56(byte[] CNT)
845843
{
846844
CNT[0] = 0x01;
847-
CNT[1] = 0x00;
848-
CNT[2] = 0x00;
849-
CNT[3] = 0x00;
850-
CNT[4] = 0x00;
851-
CNT[5] = 0x00;
852-
CNT[6] = 0x00;
845+
Arrays.fill(CNT, 1, 7, (byte) 0);
853846
}
854847

855848
public static void hirose_128_128_256(RomulusDigest.Friend friend, byte[] h, byte[] g, byte[] m, int mOff)

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ protected void init(byte[] key, byte[] iv)
126126
reset();
127127
}
128128

129+
protected void finishAAD(State nextState, boolean isDoFinal)
130+
{
131+
// State indicates whether we ever received AAD
132+
switch (m_state)
133+
{
134+
case DecAad:
135+
case EncAad:
136+
{
137+
processFinalAAD();
138+
break;
139+
}
140+
default:
141+
break;
142+
}
143+
144+
m_aadPos = 0;
145+
m_state = nextState;
146+
}
147+
129148
@Override
130149
protected void processFinalBlock(byte[] output, int outOff)
131150
{
@@ -209,7 +228,6 @@ protected void processBufferAAD(byte[] buffer, int bufOff)
209228

210229
protected void processBufferDecrypt(byte[] buffer, int bufOff, byte[] output, int outOff)
211230
{
212-
213231
for (int i = 0; i < RATE_WORDS / 2; ++i)
214232
{
215233
int j = i + (RATE_WORDS / 2);
@@ -234,8 +252,6 @@ protected void processBufferDecrypt(byte[] buffer, int bufOff, byte[] output, in
234252

235253
protected void processBufferEncrypt(byte[] buffer, int bufOff, byte[] output, int outOff)
236254
{
237-
// assert bufOff <= buffer.length - RATE_BYTES;
238-
239255
for (int i = 0; i < RATE_WORDS / 2; ++i)
240256
{
241257
int j = i + (RATE_WORDS / 2);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ protected void reset(boolean clearMac)
179179
private void AbsorbAny(byte[] X, int Xoff, int XLen, int Cd)
180180
{
181181
int splitLen;
182+
if (phase != PhaseUp)
183+
{
184+
up(mode, state, 0);
185+
}
182186
do
183187
{
184-
if (phase != PhaseUp)
185-
{
186-
up(mode, state, 0);
187-
}
188188
splitLen = Math.min(XLen, AADBufferSize);
189189
down(mode, state, X, Xoff, splitLen, Cd);
190190
phase = PhaseDown;

0 commit comments

Comments
 (0)