Skip to content

Commit 455e44c

Browse files
author
gefeili
committed
TODO: Add tests to cover AEADParameters
1 parent c707052 commit 455e44c

File tree

13 files changed

+253
-612
lines changed

13 files changed

+253
-612
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.bouncycastle.crypto.engines;
2+
3+
import org.bouncycastle.crypto.CipherParameters;
4+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
5+
import org.bouncycastle.crypto.DataLengthException;
6+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
7+
import org.bouncycastle.crypto.modes.AEADCipher;
8+
import org.bouncycastle.crypto.params.AEADParameters;
9+
import org.bouncycastle.crypto.params.KeyParameter;
10+
import org.bouncycastle.crypto.params.ParametersWithIV;
11+
12+
abstract class AEADBaseEngine
13+
implements AEADCipher
14+
{
15+
protected boolean forEncryption;
16+
protected String algorithmName;
17+
protected int CRYPTO_KEYBYTES;
18+
protected int CRYPTO_NPUBBYTES;
19+
protected int CRYPTO_ABYTES;
20+
protected byte[] initialAssociatedText;
21+
22+
@Override
23+
public String getAlgorithmName()
24+
{
25+
return algorithmName;
26+
}
27+
28+
public int getKeyBytesSize()
29+
{
30+
return CRYPTO_KEYBYTES;
31+
}
32+
33+
public int getIVBytesSize()
34+
{
35+
return CRYPTO_NPUBBYTES;
36+
}
37+
38+
39+
public int processByte(byte in, byte[] out, int outOff)
40+
throws DataLengthException
41+
{
42+
return processBytes(new byte[]{ in }, 0, 1, out, outOff);
43+
}
44+
45+
protected byte[][] initialize(boolean forEncryption, CipherParameters params)
46+
{
47+
this.forEncryption = forEncryption;
48+
KeyParameter key;
49+
byte[] npub;
50+
byte[] k;
51+
52+
if (params instanceof AEADParameters)
53+
{
54+
AEADParameters aeadParameters = (AEADParameters)params;
55+
key = aeadParameters.getKey();
56+
npub = aeadParameters.getNonce();
57+
initialAssociatedText = aeadParameters.getAssociatedText();
58+
59+
int macSizeBits = aeadParameters.getMacSize();
60+
if (macSizeBits != CRYPTO_ABYTES * 8)
61+
{
62+
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
63+
}
64+
}
65+
else if (params instanceof ParametersWithIV)
66+
{
67+
ParametersWithIV withIV = (ParametersWithIV)params;
68+
key = (KeyParameter)withIV.getParameters();
69+
npub = withIV.getIV();
70+
initialAssociatedText = null;
71+
}
72+
else
73+
{
74+
throw new IllegalArgumentException("invalid parameters passed to " + algorithmName);
75+
}
76+
77+
if (key == null)
78+
{
79+
throw new IllegalArgumentException(algorithmName + " Init parameters must include a key");
80+
}
81+
if (npub == null || npub.length != CRYPTO_NPUBBYTES)
82+
{
83+
throw new IllegalArgumentException(algorithmName + " requires exactly " + CRYPTO_NPUBBYTES + " bytes of IV");
84+
}
85+
86+
k = key.getKey();
87+
if (k.length != CRYPTO_KEYBYTES)
88+
{
89+
throw new IllegalArgumentException(algorithmName + " key must be " + CRYPTO_KEYBYTES + " bytes long");
90+
}
91+
92+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
93+
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
94+
return new byte[][]{k, npub};
95+
}
96+
}

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

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

33
import org.bouncycastle.crypto.CipherParameters;
4-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
5-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
6-
import org.bouncycastle.crypto.params.AEADParameters;
7-
import org.bouncycastle.crypto.params.KeyParameter;
8-
import org.bouncycastle.crypto.params.ParametersWithIV;
94
import org.bouncycastle.util.Pack;
105

116
/**
@@ -27,6 +22,7 @@ public class AsconAEAD128
2722
public AsconAEAD128()
2823
{
2924
CRYPTO_KEYBYTES = 16;
25+
CRYPTO_NPUBBYTES = 16;
3026
CRYPTO_ABYTES = 16;
3127
ASCON_AEAD_RATE = 16;
3228
ASCON_IV = 0x00001000808c0001L;
@@ -144,54 +140,12 @@ private void finishData(State nextState)
144140
public void init(boolean forEncryption, CipherParameters params)
145141
throws IllegalArgumentException
146142
{
147-
KeyParameter key;
148-
byte[] npub;
149-
if (params instanceof AEADParameters)
150-
{
151-
AEADParameters aeadParameters = (AEADParameters)params;
152-
key = aeadParameters.getKey();
153-
npub = aeadParameters.getNonce();
154-
initialAssociatedText = aeadParameters.getAssociatedText();
155-
156-
int macSizeBits = aeadParameters.getMacSize();
157-
if (macSizeBits != CRYPTO_ABYTES * 8)
158-
{
159-
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
160-
}
161-
}
162-
else if (params instanceof ParametersWithIV)
163-
{
164-
ParametersWithIV withIV = (ParametersWithIV)params;
165-
key = (KeyParameter)withIV.getParameters();
166-
npub = withIV.getIV();
167-
initialAssociatedText = null;
168-
}
169-
else
170-
{
171-
throw new IllegalArgumentException("invalid parameters passed to Ascon");
172-
}
173-
174-
if (key == null)
175-
{
176-
throw new IllegalArgumentException("Ascon Init parameters must include a key");
177-
}
178-
if (npub == null || npub.length != CRYPTO_ABYTES)
179-
{
180-
throw new IllegalArgumentException("Ascon-AEAD-128 requires exactly " + CRYPTO_ABYTES + " bytes of IV");
181-
}
182-
183-
byte[] k = key.getKey();
184-
if (k.length != CRYPTO_KEYBYTES)
185-
{
186-
throw new IllegalArgumentException("Ascon-AEAD-128 key must be " + CRYPTO_KEYBYTES + " bytes long");
187-
}
143+
byte[][] keyiv = initialize(forEncryption, params);
188144

189-
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
190-
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
191-
K0 = Pack.littleEndianToLong(k, 0);
192-
K1 = Pack.littleEndianToLong(k, 8);
193-
N0 = Pack.littleEndianToLong(npub, 0);
194-
N1 = Pack.littleEndianToLong(npub, 8);
145+
K0 = Pack.littleEndianToLong(keyiv[0], 0);
146+
K1 = Pack.littleEndianToLong(keyiv[0], 8);
147+
N0 = Pack.littleEndianToLong(keyiv[1], 0);
148+
N1 = Pack.littleEndianToLong(keyiv[1], 8);
195149

196150
m_state = forEncryption ? State.EncInit : State.DecInit;
197151

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.bouncycastle.util.Longs;
99

1010
abstract class AsconBaseEngine
11-
implements AEADCipher
11+
extends AEADBaseEngine
1212
{
1313
protected enum State
1414
{
@@ -25,11 +25,7 @@ protected enum State
2525

2626

2727
protected State m_state = State.Uninitialized;
28-
protected String algorithmName;
2928
protected byte[] mac;
30-
protected byte[] initialAssociatedText;
31-
protected int CRYPTO_KEYBYTES;
32-
protected int CRYPTO_ABYTES;
3329
protected int nr;
3430
protected int ASCON_AEAD_RATE;
3531
protected long K0;
@@ -254,12 +250,6 @@ public void processAADBytes(byte[] inBytes, int inOff, int len)
254250
m_bufPos = len;
255251
}
256252

257-
public int processByte(byte in, byte[] out, int outOff)
258-
throws DataLengthException
259-
{
260-
return processBytes(new byte[]{in}, 0, 1, out, outOff);
261-
}
262-
263253
public int processBytes(byte[] inBytes, int inOff, int len, byte[] outBytes, int outOff)
264254
throws DataLengthException
265255
{
@@ -479,22 +469,5 @@ protected void reset(boolean clearMac)
479469
}
480470
}
481471

482-
public int getKeyBytesSize()
483-
{
484-
return CRYPTO_KEYBYTES;
485-
}
486-
487-
public int getIVBytesSize()
488-
{
489-
return CRYPTO_ABYTES;
490-
}
491-
492-
493-
public String getAlgorithmName()
494-
{
495-
return algorithmName;
496-
}
497-
498472
public abstract String getAlgorithmVersion();
499-
500473
}

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

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

33
import org.bouncycastle.crypto.CipherParameters;
4-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
5-
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
6-
import org.bouncycastle.crypto.params.AEADParameters;
7-
import org.bouncycastle.crypto.params.KeyParameter;
8-
import org.bouncycastle.crypto.params.ParametersWithIV;
94
import org.bouncycastle.util.Pack;
105

116
/**
@@ -36,31 +31,29 @@ public enum AsconParameters
3631
}
3732

3833
private final AsconParameters asconParameters;
39-
4034
private long K2;
4135

4236
public AsconEngine(AsconParameters asconParameters)
4337
{
4438
this.asconParameters = asconParameters;
39+
CRYPTO_NPUBBYTES = 16;
40+
CRYPTO_ABYTES = 16;
4541
switch (asconParameters)
4642
{
4743
case ascon80pq:
4844
CRYPTO_KEYBYTES = 20;
49-
CRYPTO_ABYTES = 16;
5045
ASCON_AEAD_RATE = 8;
5146
ASCON_IV = 0xa0400c0600000000L;
5247
algorithmName = "Ascon-80pq AEAD";
5348
break;
5449
case ascon128a:
5550
CRYPTO_KEYBYTES = 16;
56-
CRYPTO_ABYTES = 16;
5751
ASCON_AEAD_RATE = 16;
5852
ASCON_IV = 0x80800c0800000000L;
5953
algorithmName = "Ascon-128a AEAD";
6054
break;
6155
case ascon128:
6256
CRYPTO_KEYBYTES = 16;
63-
CRYPTO_ABYTES = 16;
6457
ASCON_AEAD_RATE = 8;
6558
ASCON_IV = 0x80400c0600000000L;
6659
algorithmName = "Ascon-128 AEAD";
@@ -219,62 +212,20 @@ private void finishData(State nextState)
219212
public void init(boolean forEncryption, CipherParameters params)
220213
throws IllegalArgumentException
221214
{
222-
KeyParameter key;
223-
byte[] npub;
224-
if (params instanceof AEADParameters)
225-
{
226-
AEADParameters aeadParameters = (AEADParameters)params;
227-
key = aeadParameters.getKey();
228-
npub = aeadParameters.getNonce();
229-
initialAssociatedText = aeadParameters.getAssociatedText();
230-
231-
int macSizeBits = aeadParameters.getMacSize();
232-
if (macSizeBits != CRYPTO_ABYTES * 8)
233-
{
234-
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
235-
}
236-
}
237-
else if (params instanceof ParametersWithIV)
238-
{
239-
ParametersWithIV withIV = (ParametersWithIV)params;
240-
key = (KeyParameter)withIV.getParameters();
241-
npub = withIV.getIV();
242-
initialAssociatedText = null;
243-
}
244-
else
245-
{
246-
throw new IllegalArgumentException("invalid parameters passed to Ascon");
247-
}
248-
249-
if (key == null)
250-
{
251-
throw new IllegalArgumentException("Ascon Init parameters must include a key");
252-
}
253-
if (npub == null || npub.length != CRYPTO_ABYTES)
254-
{
255-
throw new IllegalArgumentException(asconParameters + " requires exactly " + CRYPTO_ABYTES + " bytes of IV");
256-
}
257-
258-
byte[] k = key.getKey();
259-
if (k.length != CRYPTO_KEYBYTES)
260-
{
261-
throw new IllegalArgumentException(asconParameters + " key must be " + CRYPTO_KEYBYTES + " bytes long");
262-
}
215+
byte[][] keyiv = initialize(forEncryption, params);
263216

264-
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
265-
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
266-
N0 = Pack.bigEndianToLong(npub, 0);
267-
N1 = Pack.bigEndianToLong(npub, 8);
217+
N0 = Pack.bigEndianToLong(keyiv[1], 0);
218+
N1 = Pack.bigEndianToLong(keyiv[1], 8);
268219
if (CRYPTO_KEYBYTES == 16)
269220
{
270-
K1 = Pack.bigEndianToLong(k, 0);
271-
K2 = Pack.bigEndianToLong(k, 8);
221+
K1 = Pack.bigEndianToLong(keyiv[0], 0);
222+
K2 = Pack.bigEndianToLong(keyiv[0], 8);
272223
}
273224
else if (CRYPTO_KEYBYTES == 20)
274225
{
275-
K0 = Pack.bigEndianToInt(k, 0);
276-
K1 = Pack.bigEndianToLong(k, 4);
277-
K2 = Pack.bigEndianToLong(k, 12);
226+
K0 = Pack.bigEndianToInt(keyiv[0], 0);
227+
K1 = Pack.bigEndianToLong(keyiv[0], 4);
228+
K2 = Pack.bigEndianToLong(keyiv[0], 12);
278229
}
279230
else
280231
{

0 commit comments

Comments
 (0)