Skip to content

Commit fcccf29

Browse files
committed
Fix Haraka digest API compliance
1 parent 55d5c4b commit fcccf29

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/sphincsplus/HarakaS256Digest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ public String getAlgorithmName()
2222
return "HarakaS-256";
2323
}
2424

25-
@Override
2625
public int getDigestSize()
2726
{
2827
return 32;
2928
}
3029

3130
public void update(byte in)
3231
{
33-
if (off + 1 > 32)
32+
if (off > 32 - 1)
3433
{
3534
throw new IllegalArgumentException("total input cannot be more than 32 bytes");
3635
}
@@ -40,7 +39,7 @@ public void update(byte in)
4039

4140
public void update(byte[] in, int inOff, int len)
4241
{
43-
if (off + len > 32)
42+
if (off > 32 - len)
4443
{
4544
throw new IllegalArgumentException("total input cannot be more than 32 bytes");
4645
}
@@ -51,9 +50,11 @@ public void update(byte[] in, int inOff, int len)
5150

5251
public int doFinal(byte[] output, int outOff)
5352
{
54-
byte[] s = new byte[64];
53+
// TODO Check received all 32 bytes of input?
54+
55+
byte[] s = new byte[32];
5556
haraka256Perm(s);
56-
System.arraycopy(s, 0, output, outOff, output.length - outOff);
57+
xor(s, 0, buffer, 0, output, outOff, 32);
5758

5859
reset();
5960

core/src/main/java/org/bouncycastle/pqc/crypto/sphincsplus/HarakaS512Digest.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ public String getAlgorithmName()
2222
return "HarakaS-512";
2323
}
2424

25-
@Override
2625
public int getDigestSize()
2726
{
28-
return 64;
27+
return 32;
2928
}
3029

3130
public void update(byte in)
3231
{
33-
if (off + 1 > 64)
32+
if (off > 64 - 1)
3433
{
3534
throw new IllegalArgumentException("total input cannot be more than 64 bytes");
3635
}
@@ -39,27 +38,23 @@ public void update(byte in)
3938

4039
public void update(byte[] in, int inOff, int len)
4140
{
42-
if (off + len > 64)
41+
if (off > 64 - len)
4342
{
4443
throw new IllegalArgumentException("total input cannot be more than 64 bytes");
4544
}
4645
System.arraycopy(in, inOff, buffer, off, len);
4746
off += len;
4847
}
4948

50-
5149
public int doFinal(byte[] out, int outOff)
5250
{
51+
// TODO Check received all 64 bytes of input?
52+
5353
byte[] s = new byte[64];
5454
haraka512Perm(s);
55-
for (int i = 0; i < 64; ++i)
56-
{
57-
s[i] ^= buffer[i];
58-
}
59-
System.arraycopy(s, 8, out, outOff, 8);
60-
System.arraycopy(s, 24, out, outOff + 8, 8);
61-
System.arraycopy(s, 32, out, outOff + 16, 8);
62-
System.arraycopy(s, 48, out, outOff + 24, 8);
55+
xor(s, 8, buffer, 8, out, outOff , 8);
56+
xor(s, 24, buffer, 24, out, outOff + 8, 16);
57+
xor(s, 48, buffer, 48, out, outOff + 24, 8);
6358

6459
reset();
6560

core/src/main/java/org/bouncycastle/pqc/crypto/sphincsplus/HarakaSBase.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,6 @@ protected void haraka256Perm(byte[] output)
162162
brEnc32Le(output, q[i << 1], i << 2);
163163
brEnc32Le(output, q[(i << 1) + 1], (i << 2) + 16);
164164
}
165-
166-
for (i = 0; i < 32; i++)
167-
{
168-
output[i] ^= buffer[i];
169-
}
170165
}
171166

172167
private void brEnc32Le(byte[] dst, int x, int startPos)
@@ -787,4 +782,12 @@ private void brAesCt64InterleaveOut(int[] w, long[] q, int pos)
787782
w[pos + 2] = (int)(x2 | (x2 >>> 16));
788783
w[pos + 3] = (int)(x3 | (x3 >>> 16));
789784
}
785+
786+
protected static void xor(byte[] x, int xOff, byte[] y, int yOff, byte[] z, int zOff, int zLen)
787+
{
788+
for (int i = 0; i < zLen; i++)
789+
{
790+
z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]);
791+
}
792+
}
790793
}

core/src/main/java/org/bouncycastle/pqc/crypto/sphincsplus/SPHINCSPlusEngine.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,25 @@ void init(byte[] pkSeed)
534534

535535
public byte[] F(byte[] pkSeed, ADRS adrs, byte[] m1)
536536
{
537-
byte[] rv = new byte[64];
537+
byte[] hash = new byte[32];
538538
harakaS512Digest.update(adrs.value, 0, adrs.value.length);
539539
if (robust)
540540
{
541-
byte[] mask = new byte[m1.length];
542541
harakaS256Digest.update(adrs.value, 0, adrs.value.length);
543-
harakaS256Digest.doFinal(mask, 0);
542+
harakaS256Digest.doFinal(hash, 0);
544543
for (int i = 0; i < m1.length; ++i)
545544
{
546-
mask[i] ^= m1[i];
545+
hash[i] ^= m1[i];
547546
}
548-
harakaS512Digest.update(mask, 0, mask.length);
547+
harakaS512Digest.update(hash, 0, m1.length);
549548
}
550549
else
551550
{
552551
harakaS512Digest.update(m1, 0, m1.length);
553552
}
554-
harakaS512Digest.doFinal(rv, 0);
555-
return Arrays.copyOf(rv, N);
553+
// NOTE The digest implementation implicitly pads the input with zeros up to 64 length
554+
harakaS512Digest.doFinal(hash, 0);
555+
return Arrays.copyOf(hash, N);
556556
}
557557

558558
public byte[] H(byte[] pkSeed, ADRS adrs, byte[] m1, byte[] m2)

0 commit comments

Comments
 (0)