Skip to content

Commit 728acd7

Browse files
committed
Java 4 updates
1 parent 833d50f commit 728acd7

File tree

28 files changed

+3180
-268
lines changed

28 files changed

+3180
-268
lines changed

core/src/main/java/org/bouncycastle/crypto/threshold/ShamirSplitSecret.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public byte[] getSecret()
7979
}
8080

8181
tmp = 1;
82-
for (byte p : products)
82+
for (int prdI = 0; prdI != products.length; prdI++)
8383
{
84-
tmp = poly.gfMul(tmp & 0xff, p & 0xff);
84+
tmp = poly.gfMul(tmp & 0xff, products[prdI] & 0xff);
8585
}
8686
r[i] = tmp;
8787
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package org.bouncycastle.crypto.engines;
2+
3+
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;
9+
import org.bouncycastle.util.Pack;
10+
11+
/**
12+
* Ascon-AEAD128 was introduced as part of the NIST Lightweight Cryptography
13+
* competition and described in the NIST Special Publication SP 800-232 (Initial
14+
* Public Draft).
15+
* For additional details, see:
16+
* <ul>
17+
* <li><a href="https://csrc.nist.gov/pubs/sp/800/232/ipd">NIST SP 800-232 (Initial Public Draft)</a></li>
18+
* <li><a href="https://github.com/ascon/ascon-c">Reference, highly optimized, masked C and
19+
* ASM implementations of Ascon (NIST SP 800-232)</a></li>
20+
* </ul>
21+
*
22+
* @version 1.3
23+
*/
24+
public class AsconAEAD128
25+
extends AsconBaseEngine
26+
{
27+
public AsconAEAD128()
28+
{
29+
CRYPTO_KEYBYTES = 16;
30+
CRYPTO_ABYTES = 16;
31+
ASCON_AEAD_RATE = 16;
32+
ASCON_IV = 0x00001000808c0001L;
33+
algorithmName = "Ascon-AEAD128";
34+
nr = 8;
35+
m_bufferSizeDecrypt = ASCON_AEAD_RATE + CRYPTO_ABYTES;
36+
m_buf = new byte[m_bufferSizeDecrypt];
37+
dsep = -9223372036854775808L; //0x80L << 56
38+
}
39+
40+
protected long pad(int i)
41+
{
42+
return 0x01L << (i << 3);
43+
}
44+
45+
@Override
46+
protected long loadBytes(byte[] in, int inOff)
47+
{
48+
return Pack.littleEndianToLong(in, inOff);
49+
}
50+
51+
@Override
52+
protected void setBytes(long n, byte[] bs, int off)
53+
{
54+
Pack.longToLittleEndian(n, bs, off);
55+
}
56+
57+
protected void ascon_aeadinit()
58+
{
59+
/* initialize */
60+
x0 = ASCON_IV;
61+
x1 = K0;
62+
x2 = K1;
63+
x3 = N0;
64+
x4 = N1;
65+
p(12);
66+
x3 ^= K0;
67+
x4 ^= K1;
68+
}
69+
70+
protected void processFinalAadBlock()
71+
{
72+
if (m_bufPos >= 8) // ASCON_AEAD_RATE == 16 is implied
73+
{
74+
x0 ^= Pack.littleEndianToLong(m_buf, 0);
75+
x1 ^= Pack.littleEndianToLong(m_buf, 8) ^ pad(m_bufPos);
76+
}
77+
else
78+
{
79+
x0 ^= Pack.littleEndianToLong(m_buf, 0) ^ pad(m_bufPos);
80+
}
81+
}
82+
83+
protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int outOff)
84+
{
85+
if (inLen >= 8) // ASCON_AEAD_RATE == 16 is implied
86+
{
87+
long c0 = Pack.littleEndianToLong(input, 0);
88+
inLen -= 8;
89+
long c1 = Pack.littleEndianToLong(input, 8, inLen);
90+
Pack.longToLittleEndian(x0 ^ c0, output, outOff);
91+
Pack.longToLittleEndian(x1 ^ c1, output, outOff + 8, inLen);
92+
x0 = c0;
93+
x1 &= -(1L << (inLen << 3));
94+
x1 |= c1;
95+
x1 ^= pad(inLen);
96+
}
97+
else
98+
{
99+
if (inLen != 0)
100+
{
101+
long c0 = Pack.littleEndianToLong(input, 0, inLen);
102+
Pack.longToLittleEndian(x0 ^ c0, output, outOff, inLen);
103+
x0 &= -(1L << (inLen << 3));
104+
x0 |= c0;
105+
}
106+
x0 ^= pad(inLen);
107+
}
108+
finishData(DecFinal);
109+
}
110+
111+
protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int outOff)
112+
{
113+
if (inLen >= 8) // ASCON_AEAD_RATE == 16 is implied
114+
{
115+
x0 ^= Pack.littleEndianToLong(input, 0);
116+
inLen -= 8;
117+
x1 ^= Pack.littleEndianToLong(input, 8, inLen);
118+
Pack.longToLittleEndian(x0, output, outOff);
119+
Pack.longToLittleEndian(x1, output, outOff + 8);
120+
x1 ^= pad(inLen);
121+
}
122+
else
123+
{
124+
if (inLen != 0)
125+
{
126+
x0 ^= Pack.littleEndianToLong(input, 0, inLen);
127+
Pack.longToLittleEndian(x0, output, outOff, inLen);
128+
}
129+
x0 ^= pad(inLen);
130+
}
131+
finishData(EncFinal);
132+
}
133+
134+
private void finishData(State nextState)
135+
{
136+
x2 ^= K0;
137+
x3 ^= K1;
138+
p(12);
139+
x3 ^= K0;
140+
x4 ^= K1;
141+
m_state = nextState;
142+
}
143+
144+
public void init(boolean forEncryption, CipherParameters params)
145+
throws IllegalArgumentException
146+
{
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+
}
188+
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);
195+
196+
m_state = forEncryption ? EncInit : DecInit;
197+
198+
reset(true);
199+
}
200+
201+
public String getAlgorithmVersion()
202+
{
203+
return "v1.3";
204+
}
205+
}
206+

0 commit comments

Comments
 (0)