Skip to content

Commit 9f3d11c

Browse files
author
gefeili
committed
refactor around digests
1 parent e7fef7d commit 9f3d11c

File tree

11 files changed

+96
-105
lines changed

11 files changed

+96
-105
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ private Friend()
1515
}
1616
}
1717

18-
1918
AsconPermutationFriend.AsconPermutation p;
2019
protected int ASCON_PB_ROUNDS = 12;
2120

@@ -73,13 +72,26 @@ protected void squeeze(byte[] output, int outOff, int len)
7372

7473
protected int hash(byte[] output, int outOff, int outLen)
7574
{
76-
if (DigestSize + outOff > output.length)
77-
{
78-
throw new OutputLengthException("output buffer is too short");
79-
}
75+
ensureSufficientOutputBuffer(output, outOff, outLen);
8076
padAndAbsorb();
8177
/* squeeze full output blocks */
8278
squeeze(output, outOff, outLen);
8379
return outLen;
8480
}
81+
82+
protected void ensureSufficientOutputBuffer(byte[] output, int outOff, int len)
83+
{
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+
}
96+
}
8597
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public AsconCXof128(byte[] s)
3636
public AsconCXof128(byte[] s, int off, int len)
3737
{
3838
algorithmName = "Ascon-CXOF128";
39-
if ((off + len) > s.length)
40-
{
41-
throw new DataLengthException("input buffer too short");
42-
}
39+
ensureSufficientInputBuffer(s, off, len);
4340
if (len > 256)
4441
{
4542
throw new DataLengthException("customized string is too long");
@@ -56,20 +53,14 @@ public AsconCXof128(byte[] s, int off, int len)
5653
@Override
5754
public void update(byte in)
5855
{
59-
if (m_squeezing)
60-
{
61-
throw new IllegalArgumentException("attempt to absorb while squeezing");
62-
}
56+
ensureNoAbsorbWhileSqueezing(m_squeezing);
6357
super.update(in);
6458
}
6559

6660
@Override
6761
public void update(byte[] input, int inOff, int len)
6862
{
69-
if (m_squeezing)
70-
{
71-
throw new IllegalArgumentException("attempt to absorb while squeezing");
72-
}
63+
ensureNoAbsorbWhileSqueezing(m_squeezing);
7364
super.update(input, inOff, len);
7465
}
7566

@@ -107,10 +98,7 @@ protected void padAndAbsorb()
10798
@Override
10899
public int doOutput(byte[] output, int outOff, int outLen)
109100
{
110-
if (DigestSize + outOff > output.length)
111-
{
112-
throw new OutputLengthException("output buffer is too short");
113-
}
101+
ensureSufficientOutputBuffer(output, outOff, outLen);
114102
padAndAbsorb();
115103
/* squeeze full output blocks */
116104
squeeze(output, outOff, outLen);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@ public AsconXof(AsconXof.AsconParameters parameters)
4949
@Override
5050
public void update(byte in)
5151
{
52-
if (m_squeezing)
53-
{
54-
throw new IllegalArgumentException("attempt to absorb while squeezing");
55-
}
52+
ensureNoAbsorbWhileSqueezing(m_squeezing);
5653
super.update(in);
5754
}
5855

5956
@Override
6057
public void update(byte[] input, int inOff, int len)
6158
{
62-
if (m_squeezing)
63-
{
64-
throw new IllegalArgumentException("attempt to absorb while squeezing");
65-
}
59+
ensureNoAbsorbWhileSqueezing(m_squeezing);
6660
super.update(input, inOff, len);
6761
}
6862

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,14 @@ protected void padAndAbsorb()
6060
@Override
6161
public void update(byte in)
6262
{
63-
if (m_squeezing)
64-
{
65-
throw new IllegalArgumentException("attempt to absorb while squeezing");
66-
}
63+
ensureNoAbsorbWhileSqueezing(m_squeezing);
6764
super.update(in);
6865
}
6966

7067
@Override
7168
public void update(byte[] input, int inOff, int len)
7269
{
73-
if (m_squeezing)
74-
{
75-
throw new IllegalArgumentException("attempt to absorb while squeezing");
76-
}
70+
ensureNoAbsorbWhileSqueezing(m_squeezing);
7771
super.update(input, inOff, len);
7872
}
7973

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ public void update(byte in)
124124
@Override
125125
public void update(byte[] input, int inOff, int len)
126126
{
127-
if ((inOff + len) > input.length)
128-
{
129-
throw new DataLengthException("input buffer too short");
130-
}
127+
ensureSufficientInputBuffer(input, inOff, len);
131128
int available = BlockSize - m_bufPos;
132129
if (processor.isLengthWithinAvailableSpace(len, available))
133130
{
@@ -155,10 +152,7 @@ public void update(byte[] input, int inOff, int len)
155152
@Override
156153
public int doFinal(byte[] output, int outOff)
157154
{
158-
if (DigestSize + outOff > output.length)
159-
{
160-
throw new OutputLengthException("output buffer is too short");
161-
}
155+
ensureSufficientOutputBuffer(output, outOff);
162156
finish(output, outOff);
163157
reset();
164158
return DigestSize;
@@ -170,6 +164,22 @@ public void reset()
170164
m_bufPos = 0;
171165
}
172166

167+
protected void ensureSufficientInputBuffer(byte[] input, int inOff, int len)
168+
{
169+
if (inOff + len > input.length)
170+
{
171+
throw new DataLengthException("input buffer too short");
172+
}
173+
}
174+
175+
protected void ensureSufficientOutputBuffer(byte[] output, int outOff)
176+
{
177+
if (DigestSize + outOff > output.length)
178+
{
179+
throw new OutputLengthException("output buffer is too short");
180+
}
181+
}
182+
173183
protected abstract void processBytes(byte[] input, int inOff);
174184

175185
protected abstract void finish(byte[] output, int outOff);

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

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

33
import org.bouncycastle.crypto.engines.AsconPermutationFriend;
4-
import org.bouncycastle.util.Longs;
54
import org.bouncycastle.util.Pack;
65

76
/**
@@ -44,12 +43,6 @@ public ISAPDigest()
4443
reset();
4544
}
4645

47-
protected long U64BIG(long x)
48-
{
49-
return ((Longs.rotateRight(x, 8) & (0xFF000000FF000000L)) | (Longs.rotateRight(x, 24) & (0x00FF000000FF0000L)) |
50-
(Longs.rotateRight(x, 40) & (0x0000FF000000FF00L)) | (Longs.rotateRight(x, 56) & (0x000000FF000000FFL)));
51-
}
52-
5346
@Override
5447
protected void processBytes(byte[] input, int inOff)
5548
{
@@ -68,14 +61,12 @@ protected void finish(byte[] output, int outOff)
6861
p.x0 ^= (m_buf[--m_bufPos] & 0xFFL) << ((7 - m_bufPos) << 3);
6962
}
7063
// squeeze
71-
long[] out64 = new long[4];
7264
for (int i = 0; i < 4; ++i)
7365
{
7466
p.p(12);
75-
out64[i] = U64BIG(p.x0);
67+
Pack.longToBigEndian(p.x0, output, outOff);
68+
outOff += 8;
7669
}
77-
/* squeeze final output block */
78-
Pack.longToLittleEndian(out64, output, outOff);
7970
}
8071

8172
@Override

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ private Friend()
2525

2626
private final byte[] state;
2727
private final byte[][] state_2d;
28-
private static final int STATE_INBYTES = 32;
28+
private static final int SQUEEZE_RATE_INBYTES = 16;
2929
private static final int D = 8;
3030
private int blockCount;
3131

3232
public PhotonBeetleDigest()
3333
{
3434
super(ProcessingBufferType.Buffered, 4);
35-
state = new byte[STATE_INBYTES];
36-
state_2d = new byte[D][D];
3735
DigestSize = 32;
36+
state = new byte[DigestSize];
37+
state_2d = new byte[D][D];
3838
algorithmName = "Photon-Beetle Hash";
3939
blockCount = 0;
4040
}
@@ -60,17 +60,17 @@ protected void finish(byte[] output, int outOff)
6060
int LAST_THREE_BITS_OFFSET = 5;
6161
if (m_bufPos == 0 && blockCount == 0)
6262
{
63-
state[STATE_INBYTES - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
63+
state[DigestSize - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
6464
}
6565
else if (blockCount < 4)
6666
{
6767
System.arraycopy(m_buf, 0, state, blockCount << 2, m_bufPos);
6868
state[(blockCount << 2) + m_bufPos] ^= 0x01; // ozs
69-
state[STATE_INBYTES - 1] ^= (byte)1 << LAST_THREE_BITS_OFFSET;
69+
state[DigestSize - 1] ^= (byte)1 << LAST_THREE_BITS_OFFSET;
7070
}
7171
else if (blockCount == 4 && m_bufPos == 0)
7272
{
73-
state[STATE_INBYTES - 1] ^= (byte)2 << LAST_THREE_BITS_OFFSET;
73+
state[DigestSize - 1] ^= (byte)2 << LAST_THREE_BITS_OFFSET;
7474
}
7575
else
7676
{
@@ -80,13 +80,12 @@ else if (blockCount == 4 && m_bufPos == 0)
8080
{
8181
state[m_bufPos] ^= 0x01; // ozs
8282
}
83-
state[STATE_INBYTES - 1] ^= (m_bufPos % BlockSize == 0 ? (byte)1 : (byte)2) << LAST_THREE_BITS_OFFSET;
83+
state[DigestSize - 1] ^= (m_bufPos % BlockSize == 0 ? (byte)1 : (byte)2) << LAST_THREE_BITS_OFFSET;
8484
}
8585
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
86-
int SQUEEZE_RATE_INBYTES = 16;
8786
System.arraycopy(state, 0, output, outOff, SQUEEZE_RATE_INBYTES);
8887
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
89-
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, DigestSize - SQUEEZE_RATE_INBYTES);
88+
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, SQUEEZE_RATE_INBYTES);
9089
}
9190

9291
@Override
@@ -96,4 +95,4 @@ public void reset()
9695
Arrays.fill(state, (byte)0);
9796
blockCount = 0;
9897
}
99-
}
98+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ public class SparkleDigest
1616
public static class Friend
1717
{
1818
private static final Friend INSTANCE = new Friend();
19-
private Friend() {}
19+
20+
private Friend()
21+
{
22+
}
2023
}
2124

2225
public enum SparkleParameters
2326
{
2427
ESCH256,
2528
ESCH384
2629
}
30+
2731
private static final int RATE_WORDS = 4;
2832
private final int[] state;
2933
private final int SPARKLE_STEPS_SLIM;
@@ -71,10 +75,7 @@ protected void finish(byte[] output, int outOff)
7175

7276
// padding
7377
m_buf[m_bufPos] = (byte)0x80;
74-
while(++m_bufPos < BlockSize)
75-
{
76-
m_buf[m_bufPos] = 0x00;
77-
}
78+
Arrays.fill(m_buf, m_bufPos, BlockSize, (byte)0);
7879
}
7980
else
8081
{
@@ -108,9 +109,9 @@ public void reset()
108109

109110
private void processBlock(byte[] buf, int off, int steps)
110111
{
111-
int t0 = Pack.littleEndianToInt(buf, off );
112-
int t1 = Pack.littleEndianToInt(buf, off + 4);
113-
int t2 = Pack.littleEndianToInt(buf, off + 8);
112+
int t0 = Pack.littleEndianToInt(buf, off);
113+
int t1 = Pack.littleEndianToInt(buf, off + 4);
114+
int t2 = Pack.littleEndianToInt(buf, off + 8);
114115
int t3 = Pack.littleEndianToInt(buf, off + 12);
115116

116117
// addition of a buffer block to the state
@@ -138,4 +139,4 @@ private static int ELL(int x)
138139
{
139140
return Integers.rotateRight(x, 16) ^ (x & 0xFFFF);
140141
}
141-
}
142+
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private Friend()
2626
private int phase;
2727
private static final int mode = 1; // set as ModeHash
2828
private static final int PhaseUp = 2;
29+
private static final int PhaseDown = 1;
2930
private static final int TAGLEN = 16;
3031
private int Cd;
3132

@@ -43,9 +44,10 @@ protected void processBytes(byte[] input, int inOff)
4344
{
4445
if (phase != PhaseUp)
4546
{
46-
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, null, 0, 0, 0);
47+
XoodyakEngine.up(Friend.INSTANCE, mode, state, 0);
4748
}
48-
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, input, inOff, BlockSize, Cd);
49+
XoodyakEngine.down(Friend.INSTANCE, mode, state, input, inOff, BlockSize, Cd);
50+
phase = PhaseDown;
4951
Cd = 0;
5052
}
5153

@@ -56,13 +58,16 @@ protected void finish(byte[] output, int outOff)
5658
{
5759
if (phase != PhaseUp)
5860
{
59-
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, null, 0, 0, 0);
61+
XoodyakEngine.up(Friend.INSTANCE, mode, state, 0);
6062
}
61-
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, m_buf, 0, m_bufPos, Cd);
63+
XoodyakEngine.down(Friend.INSTANCE, mode, state, m_buf, 0, m_bufPos, Cd);
6264
}
63-
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, output, outOff, TAGLEN, 0x40);
64-
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, null, 0, 0, 0);
65-
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, output, outOff + TAGLEN, TAGLEN, 0);
65+
XoodyakEngine.up(Friend.INSTANCE, mode, state, 0x40);
66+
System.arraycopy(state, 0, output, outOff, TAGLEN);
67+
XoodyakEngine.down(Friend.INSTANCE, mode, state, null, 0, 0, 0);
68+
XoodyakEngine.up(Friend.INSTANCE, mode, state, 0);
69+
System.arraycopy(state, 0, output, outOff + TAGLEN, TAGLEN);
70+
phase = PhaseDown;
6671
}
6772

6873
@Override
@@ -73,4 +78,4 @@ public void reset()
7378
phase = PhaseUp;
7479
Cd = 0x03;
7580
}
76-
}
81+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public void permutation(byte[] state)
149149
private class Dumbo
150150
extends Spongent
151151
{
152-
153152
public Dumbo()
154153
{
155154
super(160, 20, 80, (byte)0x75);
@@ -166,7 +165,6 @@ public void lfsr_step()
166165
private class Jumbo
167166
extends Spongent
168167
{
169-
170168
public Jumbo()
171169
{
172170
super(176, 22, 90, (byte)0x45);

0 commit comments

Comments
 (0)