Skip to content

Commit dfa92b8

Browse files
committed
Merge branch '1958-aead-parameters' into 'main'
1958 aead parameters See merge request root/bc-java!64
2 parents e404498 + ac52173 commit dfa92b8

20 files changed

+1806
-1795
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 KEY_SIZE;
18+
protected int IV_SIZE;
19+
protected int MAC_SIZE;
20+
protected byte[] initialAssociatedText;
21+
protected byte[] mac;
22+
23+
@Override
24+
public String getAlgorithmName()
25+
{
26+
return algorithmName;
27+
}
28+
29+
public int getKeyBytesSize()
30+
{
31+
return KEY_SIZE;
32+
}
33+
34+
public int getIVBytesSize()
35+
{
36+
return IV_SIZE;
37+
}
38+
39+
public byte[] getMac()
40+
{
41+
return mac;
42+
}
43+
44+
public void reset()
45+
{
46+
reset(true);
47+
}
48+
49+
public int processByte(byte in, byte[] out, int outOff)
50+
throws DataLengthException
51+
{
52+
return processBytes(new byte[]{in}, 0, 1, out, outOff);
53+
}
54+
55+
public void init(boolean forEncryption, CipherParameters params)
56+
{
57+
this.forEncryption = forEncryption;
58+
KeyParameter key;
59+
byte[] npub;
60+
byte[] k;
61+
62+
if (params instanceof AEADParameters)
63+
{
64+
AEADParameters aeadParameters = (AEADParameters)params;
65+
key = aeadParameters.getKey();
66+
npub = aeadParameters.getNonce();
67+
initialAssociatedText = aeadParameters.getAssociatedText();
68+
69+
int macSizeBits = aeadParameters.getMacSize();
70+
if (macSizeBits != MAC_SIZE * 8)
71+
{
72+
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
73+
}
74+
}
75+
else if (params instanceof ParametersWithIV)
76+
{
77+
ParametersWithIV withIV = (ParametersWithIV)params;
78+
key = (KeyParameter)withIV.getParameters();
79+
npub = withIV.getIV();
80+
initialAssociatedText = null;
81+
}
82+
else
83+
{
84+
throw new IllegalArgumentException("invalid parameters passed to " + algorithmName);
85+
}
86+
87+
if (key == null)
88+
{
89+
throw new IllegalArgumentException(algorithmName + " Init parameters must include a key");
90+
}
91+
if (npub == null || npub.length != IV_SIZE)
92+
{
93+
throw new IllegalArgumentException(algorithmName + " requires exactly " + IV_SIZE + " bytes of IV");
94+
}
95+
96+
k = key.getKey();
97+
if (k.length != KEY_SIZE)
98+
{
99+
throw new IllegalArgumentException(algorithmName + " key must be " + KEY_SIZE + " bytes long");
100+
}
101+
102+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
103+
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
104+
105+
init(k, npub);
106+
if (initialAssociatedText != null)
107+
{
108+
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
109+
}
110+
}
111+
112+
protected abstract void init(byte[] key, byte[] iv);
113+
114+
protected void reset(boolean clearMac)
115+
{
116+
if (clearMac)
117+
{
118+
mac = null;
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)