Skip to content

Commit a89da1e

Browse files
author
gefeili
committed
move getMac to AEADBaseEngine, fix the bug related to Grain128AEADEngine
1 parent 455e44c commit a89da1e

File tree

8 files changed

+33
-111
lines changed

8 files changed

+33
-111
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ abstract class AEADBaseEngine
1818
protected int CRYPTO_NPUBBYTES;
1919
protected int CRYPTO_ABYTES;
2020
protected byte[] initialAssociatedText;
21+
protected byte[] mac;
2122

2223
@Override
2324
public String getAlgorithmName()
@@ -35,6 +36,10 @@ public int getIVBytesSize()
3536
return CRYPTO_NPUBBYTES;
3637
}
3738

39+
public byte[] getMac()
40+
{
41+
return mac;
42+
}
3843

3944
public int processByte(byte in, byte[] out, int outOff)
4045
throws DataLengthException

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bouncycastle.crypto.DataLengthException;
44
import org.bouncycastle.crypto.InvalidCipherTextException;
55
import org.bouncycastle.crypto.OutputLengthException;
6-
import org.bouncycastle.crypto.modes.AEADCipher;
76
import org.bouncycastle.util.Arrays;
87
import org.bouncycastle.util.Longs;
98

@@ -25,7 +24,6 @@ protected enum State
2524

2625

2726
protected State m_state = State.Uninitialized;
28-
protected byte[] mac;
2927
protected int nr;
3028
protected int ASCON_AEAD_RATE;
3129
protected long K0;
@@ -382,11 +380,6 @@ public int doFinal(byte[] outBytes, int outOff)
382380
return resultLength;
383381
}
384382

385-
public byte[] getMac()
386-
{
387-
return mac;
388-
}
389-
390383
public int getUpdateOutputSize(int len)
391384
{
392385
int total = Math.max(0, len);

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
import java.util.Arrays;
55

66
import org.bouncycastle.crypto.CipherParameters;
7-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
87
import org.bouncycastle.crypto.DataLengthException;
98
import org.bouncycastle.crypto.InvalidCipherTextException;
109
import org.bouncycastle.crypto.OutputLengthException;
11-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
12-
import org.bouncycastle.crypto.modes.AEADCipher;
13-
import org.bouncycastle.crypto.params.KeyParameter;
14-
import org.bouncycastle.crypto.params.ParametersWithIV;
1510

1611
/**
1712
* Elephant AEAD v2, based on the current round 3 submission, https://www.esat.kuleuven.be/cosic/elephant/
@@ -46,7 +41,6 @@ private enum State
4641
private int nSBox;
4742
private final int nRounds;
4843
private byte lfsrIV;
49-
private byte[] tag;
5044
private byte[] npub;
5145
private byte[] expanded_key;
5246
private boolean initialised;
@@ -380,14 +374,14 @@ public int doFinal(byte[] output, int outOff)
380374
int nblocks_ad = 1 + (CRYPTO_NPUBBYTES + adlen) / BLOCK_SIZE;
381375
int nb_it = Math.max(nblocks_c + 1, nblocks_ad - 1);
382376
outOff += processBytes(inputMessage, output, outOff, nb_it, nblocks_m, nblocks_c, mlen, nblocks_ad, true);
383-
tag = new byte[CRYPTO_ABYTES];
377+
mac = new byte[CRYPTO_ABYTES];
384378
xor_block(tag_buffer, expanded_key, 0, BLOCK_SIZE);
385379
permutation(tag_buffer);
386380
xor_block(tag_buffer, expanded_key, 0, BLOCK_SIZE);
387381
if (forEncryption)
388382
{
389-
System.arraycopy(tag_buffer, 0, tag, 0, CRYPTO_ABYTES);
390-
System.arraycopy(tag, 0, output, outOff, tag.length);
383+
System.arraycopy(tag_buffer, 0, mac, 0, CRYPTO_ABYTES);
384+
System.arraycopy(mac, 0, output, outOff, mac.length);
391385
rv += CRYPTO_ABYTES;
392386
}
393387
else
@@ -405,12 +399,6 @@ public int doFinal(byte[] output, int outOff)
405399
return rv;
406400
}
407401

408-
@Override
409-
public byte[] getMac()
410-
{
411-
return tag;
412-
}
413-
414402
@Override
415403
public int getUpdateOutputSize(int len)
416404
{
@@ -480,7 +468,7 @@ private void reset(boolean clearMac)
480468
{
481469
if (clearMac)
482470
{
483-
tag = null;
471+
mac = null;
484472
}
485473
aadData.reset();
486474
Arrays.fill(tag_buffer, (byte)0);

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public class Grain128AEADEngine
3434
private boolean aadFinished = false;
3535
private final ErasableOutputStream aadData = new ErasableOutputStream();
3636

37-
private byte[] mac;
38-
3937
public Grain128AEADEngine()
4038
{
4139
algorithmName = "Grain-128AEAD";
@@ -44,11 +42,6 @@ public Grain128AEADEngine()
4442
CRYPTO_ABYTES = 8;
4543
}
4644

47-
public String getAlgorithmName()
48-
{
49-
return "Grain-128AEAD";
50-
}
51-
5245
/**
5346
* Initialize a Grain-128AEAD cipher.
5447
*
@@ -63,7 +56,7 @@ public void init(boolean forEncryption, CipherParameters params)
6356
* Grain encryption and decryption is completely symmetrical, so the
6457
* 'forEncryption' is irrelevant.
6558
*/
66-
byte[][] keyiv = initialize(true, params);
59+
byte[][] keyiv = initialize(forEncryption, params);
6760

6861

6962
/*
@@ -397,12 +390,6 @@ private void authShift(int val)
397390
authSr[1] = (authSr[1] >>> 1) | (val << 31);
398391
}
399392

400-
public int processByte(byte input, byte[] output, int outOff)
401-
throws DataLengthException
402-
{
403-
return processBytes(new byte[]{input}, 0, 1, output, outOff);
404-
}
405-
406393
public int doFinal(byte[] out, int outOff)
407394
throws IllegalStateException, InvalidCipherTextException
408395
{
@@ -423,11 +410,6 @@ public int doFinal(byte[] out, int outOff)
423410
return mac.length;
424411
}
425412

426-
public byte[] getMac()
427-
{
428-
return mac;
429-
}
430-
431413
public int getUpdateOutputSize(int len)
432414
{
433415
return len;

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import java.io.ByteArrayOutputStream;
44

55
import org.bouncycastle.crypto.CipherParameters;
6-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
76
import org.bouncycastle.crypto.DataLengthException;
87
import org.bouncycastle.crypto.InvalidCipherTextException;
98
import org.bouncycastle.crypto.OutputLengthException;
10-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
11-
import org.bouncycastle.crypto.modes.AEADCipher;
12-
import org.bouncycastle.crypto.params.KeyParameter;
13-
import org.bouncycastle.crypto.params.ParametersWithIV;
149
import org.bouncycastle.util.Pack;
1510

1611
/**
@@ -59,11 +54,8 @@ public ISAPEngine(IsapType isapType)
5954
private boolean initialised;
6055
final int ISAP_STATE_SZ = 40;
6156
private byte[] k;
62-
private byte[] c;
63-
private byte[] ad;
6457
private byte[] npub;
65-
private byte[] mac;
66-
private ByteArrayOutputStream aadData = new ByteArrayOutputStream();
58+
private final ByteArrayOutputStream aadData = new ByteArrayOutputStream();
6759
private final ByteArrayOutputStream message = new ByteArrayOutputStream();
6860
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
6961
private int ISAP_rH;
@@ -856,6 +848,8 @@ public int doFinal(byte[] output, int outOff)
856848
throw new IllegalArgumentException("Need call init function before encryption/decryption");
857849
}
858850
int len;
851+
byte[] c;
852+
byte[] ad;
859853
if (forEncryption)
860854
{
861855
byte[] enc_input = message.toByteArray();
@@ -898,12 +892,6 @@ public int doFinal(byte[] output, int outOff)
898892
return len;
899893
}
900894

901-
@Override
902-
public byte[] getMac()
903-
{
904-
return mac;
905-
}
906-
907895
@Override
908896
public int getUpdateOutputSize(int len)
909897
{

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import java.io.ByteArrayOutputStream;
44

55
import org.bouncycastle.crypto.CipherParameters;
6-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
76
import org.bouncycastle.crypto.DataLengthException;
87
import org.bouncycastle.crypto.InvalidCipherTextException;
98
import org.bouncycastle.crypto.OutputLengthException;
10-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
11-
import org.bouncycastle.crypto.modes.AEADCipher;
12-
import org.bouncycastle.crypto.params.KeyParameter;
13-
import org.bouncycastle.crypto.params.ParametersWithIV;
149

1510
/**
1611
* Photon-Beetle, https://www.isical.ac.in/~lightweight/beetle/
@@ -34,7 +29,6 @@ public enum PhotonBeetleParameters
3429
private byte[] N;
3530
private byte[] state;
3631
private byte[][] state_2d;
37-
private byte[] T;
3832
private boolean initialised;
3933
private final ByteArrayOutputStream aadData = new ByteArrayOutputStream();
4034
private final ByteArrayOutputStream message = new ByteArrayOutputStream();
@@ -97,12 +91,12 @@ public PhotonBeetleEngine(PhotonBeetleParameters pbp)
9791
public void init(boolean forEncryption, CipherParameters params)
9892
throws IllegalArgumentException
9993
{
100-
byte[][] keyiv =initialize(forEncryption, params);
94+
byte[][] keyiv = initialize(forEncryption, params);
10195
K = keyiv[0];
10296
N = keyiv[1];
10397
state = new byte[STATE_INBYTES];
10498
state_2d = new byte[D][D];
105-
T = new byte[TAG_INBYTES];
99+
mac = new byte[TAG_INBYTES];
106100
initialised = true;
107101
reset(false);
108102
}
@@ -200,18 +194,18 @@ public int doFinal(byte[] output, int outOff)
200194
state[STATE_INBYTES - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
201195
}
202196
PHOTON_Permutation();
203-
T = new byte[TAG_INBYTES];
204-
System.arraycopy(state, 0, T, 0, TAG_INBYTES);
197+
mac = new byte[TAG_INBYTES];
198+
System.arraycopy(state, 0, mac, 0, TAG_INBYTES);
205199
if (forEncryption)
206200
{
207-
System.arraycopy(T, 0, output, outOff, TAG_INBYTES);
201+
System.arraycopy(mac, 0, output, outOff, TAG_INBYTES);
208202
len += TAG_INBYTES;
209203
}
210204
else
211205
{
212206
for (i = 0; i < TAG_INBYTES; ++i)
213207
{
214-
if (T[i] != input[len + i])
208+
if (mac[i] != input[len + i])
215209
{
216210
throw new IllegalArgumentException("Mac does not match");
217211
}
@@ -221,12 +215,6 @@ public int doFinal(byte[] output, int outOff)
221215
return len;
222216
}
223217

224-
@Override
225-
public byte[] getMac()
226-
{
227-
return T;
228-
}
229-
230218
@Override
231219
public int getUpdateOutputSize(int len)
232220
{
@@ -255,7 +243,7 @@ private void reset(boolean clearMac)
255243
{
256244
if (clearMac)
257245
{
258-
T = null;
246+
mac = null;
259247
}
260248
input_empty = true;
261249
aadData.reset();

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

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

33
import org.bouncycastle.crypto.CipherParameters;
4-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
54
import org.bouncycastle.crypto.DataLengthException;
65
import org.bouncycastle.crypto.InvalidCipherTextException;
76
import org.bouncycastle.crypto.OutputLengthException;
8-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
97
import org.bouncycastle.crypto.digests.SparkleDigest;
10-
import org.bouncycastle.crypto.params.AEADParameters;
11-
import org.bouncycastle.crypto.params.KeyParameter;
12-
import org.bouncycastle.crypto.params.ParametersWithIV;
138
import org.bouncycastle.util.Arrays;
149
import org.bouncycastle.util.Integers;
1510
import org.bouncycastle.util.Pack;
@@ -49,7 +44,6 @@ private enum State
4944
private final int[] state;
5045
private final int[] k;
5146
private final int[] npub;
52-
private byte[] tag;
5347
private boolean encrypted;
5448
private State m_state = State.Uninitialized;
5549

@@ -383,15 +377,15 @@ public int doFinal(byte[] out, int outOff)
383377
{
384378
state[RATE_WORDS + i] ^= k[i];
385379
}
386-
tag = new byte[CRYPTO_ABYTES];
387-
Pack.intToLittleEndian(state, RATE_WORDS, TAG_WORDS, tag, 0);
380+
mac = new byte[CRYPTO_ABYTES];
381+
Pack.intToLittleEndian(state, RATE_WORDS, TAG_WORDS, mac, 0);
388382
if (forEncryption)
389383
{
390-
System.arraycopy(tag, 0, out, outOff, CRYPTO_ABYTES);
384+
System.arraycopy(mac, 0, out, outOff, CRYPTO_ABYTES);
391385
}
392386
else
393387
{
394-
if (!Arrays.constantTimeAreEqual(CRYPTO_ABYTES, tag, 0, m_buf, m_bufPos))
388+
if (!Arrays.constantTimeAreEqual(CRYPTO_ABYTES, mac, 0, m_buf, m_bufPos))
395389
{
396390
throw new InvalidCipherTextException(algorithmName + " mac does not match");
397391
}
@@ -400,11 +394,6 @@ public int doFinal(byte[] out, int outOff)
400394
return resultLength;
401395
}
402396

403-
public byte[] getMac()
404-
{
405-
return tag;
406-
}
407-
408397
public int getUpdateOutputSize(int len)
409398
{
410399
// The -1 is to account for the lazy processing of a full buffer
@@ -639,7 +628,7 @@ private void reset(boolean clearMac)
639628
{
640629
if (clearMac)
641630
{
642-
tag = null;
631+
mac = null;
643632
}
644633

645634
Arrays.clear(m_buf);

0 commit comments

Comments
 (0)