Skip to content

Commit 8cc2bc3

Browse files
committed
Merge branch '1958-aead-parameters' into 'main'
Refactor Lightweight cryptography schemes See merge request root/bc-java!66
2 parents eb646df + 27bb74c commit 8cc2bc3

39 files changed

+4070
-2458
lines changed
Lines changed: 45 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,31 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import org.bouncycastle.crypto.DataLengthException;
4-
import org.bouncycastle.crypto.ExtendedDigest;
53
import org.bouncycastle.crypto.OutputLengthException;
6-
import org.bouncycastle.util.Arrays;
7-
import org.bouncycastle.util.Longs;
4+
import org.bouncycastle.crypto.engines.AsconPermutationFriend;
85

96
abstract class AsconBaseDigest
10-
implements ExtendedDigest
7+
extends BufferBaseDigest
118
{
12-
protected long x0;
13-
protected long x1;
14-
protected long x2;
15-
protected long x3;
16-
protected long x4;
17-
protected final int CRYPTO_BYTES = 32;
18-
protected final int ASCON_HASH_RATE = 8;
19-
protected int ASCON_PB_ROUNDS = 12;
20-
protected final byte[] m_buf = new byte[ASCON_HASH_RATE];
21-
protected int m_bufPos = 0;
22-
23-
24-
private void round(long C)
9+
public static class Friend
2510
{
26-
long t0 = x0 ^ x1 ^ x2 ^ x3 ^ C ^ (x1 & (x0 ^ x2 ^ x4 ^ C));
27-
long t1 = x0 ^ x2 ^ x3 ^ x4 ^ C ^ ((x1 ^ x2 ^ C) & (x1 ^ x3));
28-
long t2 = x1 ^ x2 ^ x4 ^ C ^ (x3 & x4);
29-
long t3 = x0 ^ x1 ^ x2 ^ C ^ ((~x0) & (x3 ^ x4));
30-
long t4 = x1 ^ x3 ^ x4 ^ ((x0 ^ x4) & x1);
31-
x0 = t0 ^ Longs.rotateRight(t0, 19) ^ Longs.rotateRight(t0, 28);
32-
x1 = t1 ^ Longs.rotateRight(t1, 39) ^ Longs.rotateRight(t1, 61);
33-
x2 = ~(t2 ^ Longs.rotateRight(t2, 1) ^ Longs.rotateRight(t2, 6));
34-
x3 = t3 ^ Longs.rotateRight(t3, 10) ^ Longs.rotateRight(t3, 17);
35-
x4 = t4 ^ Longs.rotateRight(t4, 7) ^ Longs.rotateRight(t4, 41);
36-
}
11+
private static final Friend INSTANCE = new Friend();
3712

38-
protected void p(int nr)
39-
{
40-
if (nr == 12)
41-
{
42-
round(0xf0L);
43-
round(0xe1L);
44-
round(0xd2L);
45-
round(0xc3L);
46-
}
47-
if (nr >= 8)
13+
private Friend()
4814
{
49-
round(0xb4L);
50-
round(0xa5L);
5115
}
52-
round(0x96L);
53-
round(0x87L);
54-
round(0x78L);
55-
round(0x69L);
56-
round(0x5aL);
57-
round(0x4bL);
5816
}
5917

18+
AsconPermutationFriend.AsconPermutation p;
19+
protected int ASCON_PB_ROUNDS = 12;
20+
21+
protected AsconBaseDigest()
22+
{
23+
super(ProcessingBufferType.Immediate, 8);
24+
p = AsconPermutationFriend.getAsconPermutation(ISAPDigest.Friend.getFriend(Friend.INSTANCE));
25+
DigestSize = 32;
26+
}
27+
28+
6029
protected abstract long pad(int i);
6130

6231
protected abstract long loadBytes(final byte[] bytes, int inOff);
@@ -67,106 +36,62 @@ protected void p(int nr)
6736

6837
protected abstract void setBytes(long w, byte[] bytes, int inOff, int n);
6938

70-
@Override
71-
public int getDigestSize()
72-
{
73-
return CRYPTO_BYTES;
74-
}
75-
76-
@Override
77-
public int getByteLength()
39+
protected void processBytes(byte[] input, int inOff)
7840
{
79-
return ASCON_HASH_RATE;
41+
p.x0 ^= loadBytes(input, inOff);
42+
p.p(ASCON_PB_ROUNDS);
8043
}
8144

82-
@Override
83-
public void update(byte in)
45+
protected void finish(byte[] output, int outOff)
8446
{
85-
m_buf[m_bufPos] = in;
86-
if (++m_bufPos == ASCON_HASH_RATE)
87-
{
88-
x0 ^= loadBytes(m_buf, 0);
89-
p(ASCON_PB_ROUNDS);
90-
m_bufPos = 0;
91-
}
92-
}
93-
94-
@Override
95-
public void update(byte[] input, int inOff, int len)
96-
{
97-
if ((inOff + len) > input.length)
98-
{
99-
throw new DataLengthException("input buffer too short");
100-
}
101-
int available = 8 - m_bufPos;
102-
if (len < available)
103-
{
104-
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
105-
m_bufPos += len;
106-
return;
107-
}
108-
int inPos = 0;
109-
if (m_bufPos > 0)
110-
{
111-
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
112-
inPos += available;
113-
x0 ^= loadBytes(m_buf, 0);
114-
p(ASCON_PB_ROUNDS);
115-
}
116-
int remaining;
117-
while ((remaining = len - inPos) >= 8)
118-
{
119-
x0 ^= loadBytes(input, inOff + inPos);
120-
p(ASCON_PB_ROUNDS);
121-
inPos += 8;
122-
}
123-
System.arraycopy(input, inOff + inPos, m_buf, 0, remaining);
124-
m_bufPos = remaining;
125-
}
126-
127-
@Override
128-
public int doFinal(byte[] output, int outOff)
129-
{
130-
return hash(output, outOff, CRYPTO_BYTES);
47+
padAndAbsorb();
48+
/* squeeze full output blocks */
49+
squeeze(output, outOff, DigestSize);
13150
}
13251

13352
protected void padAndAbsorb()
13453
{
135-
x0 ^= loadBytes(m_buf, 0, m_bufPos);
136-
x0 ^= pad(m_bufPos);
137-
p(12);
54+
p.x0 ^= loadBytes(m_buf, 0, m_bufPos) ^ pad(m_bufPos);
55+
p.p(12);
13856
}
13957

14058
protected void squeeze(byte[] output, int outOff, int len)
14159
{
14260
/* squeeze full output blocks */
143-
while (len > ASCON_HASH_RATE)
61+
while (len > BlockSize)
14462
{
145-
setBytes(x0, output, outOff);
146-
p(ASCON_PB_ROUNDS);
147-
outOff += ASCON_HASH_RATE;
148-
len -= ASCON_HASH_RATE;
63+
setBytes(p.x0, output, outOff);
64+
p.p(ASCON_PB_ROUNDS);
65+
outOff += BlockSize;
66+
len -= BlockSize;
14967
}
15068
/* squeeze final output block */
151-
setBytes(x0, output, outOff, len);
69+
setBytes(p.x0, output, outOff, len);
15270
reset();
15371
}
15472

15573
protected int hash(byte[] output, int outOff, int outLen)
15674
{
157-
if (CRYPTO_BYTES + outOff > output.length)
158-
{
159-
throw new OutputLengthException("output buffer is too short");
160-
}
75+
ensureSufficientOutputBuffer(output, outOff, outLen);
16176
padAndAbsorb();
16277
/* squeeze full output blocks */
16378
squeeze(output, outOff, outLen);
16479
return outLen;
16580
}
16681

167-
public void reset()
82+
protected void ensureSufficientOutputBuffer(byte[] output, int outOff, int len)
16883
{
169-
Arrays.clear(m_buf);
170-
m_bufPos = 0;
84+
if (outOff + len > output.length)
85+
{
86+
throw new OutputLengthException("output buffer is too short");
87+
}
88+
}
89+
90+
protected void ensureNoAbsorbWhileSqueezing(boolean m_squeezing)
91+
{
92+
if (m_squeezing)
93+
{
94+
throw new IllegalArgumentException("attempt to absorb while squeezing");
95+
}
17196
}
17297
}

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,32 @@ public AsconCXof128(byte[] s)
3535

3636
public AsconCXof128(byte[] s, int off, int len)
3737
{
38-
if ((off + len) > s.length)
39-
{
40-
throw new DataLengthException("input buffer too short");
41-
}
38+
algorithmName = "Ascon-CXOF128";
39+
ensureSufficientInputBuffer(s, off, len);
4240
if (len > 256)
4341
{
4442
throw new DataLengthException("customized string is too long");
4543
}
4644
initState(s, off, len);
4745
// NOTE: Cache the initialized state
48-
z0 = x0;
49-
z1 = x1;
50-
z2 = x2;
51-
z3 = x3;
52-
z4 = x4;
46+
z0 = p.x0;
47+
z1 = p.x1;
48+
z2 = p.x2;
49+
z3 = p.x3;
50+
z4 = p.x4;
5351
}
5452

5553
@Override
5654
public void update(byte in)
5755
{
58-
if (m_squeezing)
59-
{
60-
throw new IllegalArgumentException("attempt to absorb while squeezing");
61-
}
56+
ensureNoAbsorbWhileSqueezing(m_squeezing);
6257
super.update(in);
6358
}
6459

6560
@Override
6661
public void update(byte[] input, int inOff, int len)
6762
{
68-
if (m_squeezing)
69-
{
70-
throw new IllegalArgumentException("attempt to absorb while squeezing");
71-
}
63+
ensureNoAbsorbWhileSqueezing(m_squeezing);
7264
super.update(input, inOff, len);
7365
}
7466

@@ -103,26 +95,16 @@ protected void padAndAbsorb()
10395
super.padAndAbsorb();
10496
}
10597

106-
@Override
107-
public String getAlgorithmName()
108-
{
109-
return "Ascon-CXOF128";
110-
}
111-
11298
@Override
11399
public int doOutput(byte[] output, int outOff, int outLen)
114100
{
115-
if (CRYPTO_BYTES + outOff > output.length)
116-
{
117-
throw new OutputLengthException("output buffer is too short");
118-
}
101+
ensureSufficientOutputBuffer(output, outOff, outLen);
119102
padAndAbsorb();
120103
/* squeeze full output blocks */
121104
squeeze(output, outOff, outLen);
122105
return outLen;
123106
}
124107

125-
126108
@Override
127109
public int doFinal(byte[] output, int outOff, int outLen)
128110
{
@@ -137,23 +119,15 @@ public void reset()
137119
super.reset();
138120
m_squeezing = false;
139121
/* initialize */
140-
x0 = z0;
141-
x1 = z1;
142-
x2 = z2;
143-
x3 = z3;
144-
x4 = z4;
122+
p.set(z0, z1, z2, z3, z4);
145123
}
146124

147125
private void initState(byte[] z, int zOff, int zLen)
148126
{
149-
x0 = 7445901275803737603L;
150-
x1 = 4886737088792722364L;
151-
x2 = -1616759365661982283L;
152-
x3 = 3076320316797452470L;
153-
x4 = -8124743304765850554L;
127+
p.set(7445901275803737603L, 4886737088792722364L, -1616759365661982283L, 3076320316797452470L, -8124743304765850554L);
154128
long bitLength = ((long)zLen) << 3;
155129
Pack.longToLittleEndian(bitLength, m_buf, 0);
156-
p(12);
130+
p.p(12);
157131
update(z, zOff, zLen);
158132
padAndAbsorb();
159133
m_squeezing = false;

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public AsconDigest(AsconParameters parameters)
4141
reset();
4242
}
4343

44-
private final String algorithmName;
45-
4644
protected long pad(int i)
4745
{
4846
return 0x80L << (56 - (i << 3));
@@ -68,12 +66,6 @@ protected void setBytes(long w, byte[] bytes, int inOff, int n)
6866
Pack.longToBigEndian(w, bytes, inOff, n);
6967
}
7068

71-
@Override
72-
public String getAlgorithmName()
73-
{
74-
return algorithmName;
75-
}
76-
7769
@Override
7870
public void reset()
7971
{
@@ -82,18 +74,10 @@ public void reset()
8274
switch (asconParameters)
8375
{
8476
case AsconHashA:
85-
x0 = 92044056785660070L;
86-
x1 = 8326807761760157607L;
87-
x2 = 3371194088139667532L;
88-
x3 = -2956994353054992515L;
89-
x4 = -6828509670848688761L;
77+
p.set(92044056785660070L, 8326807761760157607L, 3371194088139667532L, -2956994353054992515L, -6828509670848688761L);
9078
break;
9179
case AsconHash:
92-
x0 = -1255492011513352131L;
93-
x1 = -8380609354527731710L;
94-
x2 = -5437372128236807582L;
95-
x3 = 4834782570098516968L;
96-
x4 = 3787428097924915520L;
80+
p.set(-1255492011513352131L, -8380609354527731710L, -5437372128236807582L, 4834782570098516968L, 3787428097924915520L);
9781
break;
9882
}
9983
}

0 commit comments

Comments
 (0)