Skip to content

Commit 082f2c5

Browse files
committed
Initial implementation of HW accelerated slhdsa sha256 on Intel.
No native limit tests implemented.
1 parent 6e9485c commit 082f2c5

12 files changed

+1186
-11
lines changed

core/src/main/java/org/bouncycastle/crypto/DefaultNativeServices.java

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ static Set<String> getNativeFeatureSet()
215215
{
216216
set.add(SHAKE);
217217
}
218+
219+
if (NativeFeatures.hasSlhDSASha256()) {
220+
set.add(SLHDSA_SHA256);
221+
}
218222
}
219223

220224
if (set.isEmpty())

core/src/main/java/org/bouncycastle/crypto/NativeFeatures.java

+19
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ static boolean hasSHAKE()
400400
}
401401
}
402402

403+
static boolean hasSlhDSASha256()
404+
{
405+
try
406+
{
407+
return nativeSlhDSASha256();
408+
}
409+
catch (UnsatisfiedLinkError ule)
410+
{
411+
if (LOG.isLoggable(Level.FINE))
412+
{
413+
LOG.log(Level.FINE, "native shake exception: " + ule.getMessage(), ule);
414+
}
415+
return false;
416+
}
417+
}
418+
419+
403420
private static native boolean nativeSHAKE();
404421

405422
private static native boolean nativeSHA3();
@@ -415,4 +432,6 @@ static boolean hasSHAKE()
415432
private static native boolean nativeMulAcc();
416433

417434
private static native boolean nativeRSA();
435+
436+
private static native boolean nativeSlhDSASha256();
418437
}

core/src/main/java/org/bouncycastle/crypto/NativeServices.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public interface NativeServices
3636
String SHAKE = "SHAKE";
3737
String MULACC = "MULACC";
3838

39+
String SLHDSA_SHA256 = "SLHDSA_SHA256";
40+
3941
String NONE = "NONE";
4042

4143
String getStatusMessage();

core/src/main/java/org/bouncycastle/pqc/crypto/slhdsa/SLHDSAParameters.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public int getN()
138138

139139
public SLHDSAEngine get()
140140
{
141-
return new SLHDSASha2Engine(n, w, d, a, k, h);
141+
return SLHDSASha2Engine.newInstance(n, w, d, a, k, h);
142142
}
143143
}
144144

core/src/main/java/org/bouncycastle/pqc/crypto/slhdsa/SLHDSASha2Engine.java

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.bouncycastle.pqc.crypto.slhdsa;
22

3+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
34
import org.bouncycastle.crypto.Digest;
5+
import org.bouncycastle.crypto.NativeServices;
46
import org.bouncycastle.crypto.digests.SHA256Digest;
57
import org.bouncycastle.crypto.digests.SHA512Digest;
68
import org.bouncycastle.crypto.generators.MGF1BytesGenerator;
@@ -13,7 +15,7 @@
1315
import org.bouncycastle.util.Pack;
1416

1517
class SLHDSASha2Engine
16-
extends SLHDSAEngine
18+
extends SLHDSAEngine
1719
{
1820
private final HMac treeHMac;
1921
private final MGF1BytesGenerator mgf1;
@@ -27,7 +29,21 @@ class SLHDSASha2Engine
2729
private Memoable msgMemo;
2830
private Memoable sha256Memo;
2931

30-
public SLHDSASha2Engine(int n, int w, int d, int a, int k, int h)
32+
33+
public static SLHDSAEngine newInstance(int n, int w, int d, int a, int k, int h)
34+
{
35+
if (CryptoServicesRegistrar.hasEnabledService(NativeServices.SLHDSA_SHA256))
36+
{
37+
if (n == 16)
38+
{
39+
return new SLHDSASha2NativeEngine(n, w, d, a, k, h);
40+
}
41+
}
42+
return new SLHDSASha2Engine(n, w, d, a, k, h);
43+
}
44+
45+
46+
private SLHDSASha2Engine(int n, int w, int d, int a, int k, int h)
3147
{
3248
super(n, w, d, a, k, h);
3349
if (n == 16)
@@ -55,13 +71,13 @@ void init(byte[] pkSeed)
5571

5672
msgDigest.update(pkSeed, 0, pkSeed.length);
5773
msgDigest.update(padding, 0, bl - N); // toByte(0, 64 - n)
58-
msgMemo = ((Memoable)msgDigest).copy();
74+
msgMemo = ((Memoable) msgDigest).copy();
5975

6076
msgDigest.reset();
6177

6278
sha256.update(pkSeed, 0, pkSeed.length);
6379
sha256.update(padding, 0, 64 - pkSeed.length); // toByte(0, 64 - n)
64-
sha256Memo = ((Memoable)sha256).copy();
80+
sha256Memo = ((Memoable) sha256).copy();
6581

6682
sha256.reset();
6783
}
@@ -70,7 +86,7 @@ public byte[] F(byte[] pkSeed, ADRS adrs, byte[] m1)
7086
{
7187
byte[] compressedADRS = compressedADRS(adrs);
7288

73-
((Memoable)sha256).reset(sha256Memo);
89+
((Memoable) sha256).reset(sha256Memo);
7490

7591
sha256.update(compressedADRS, 0, compressedADRS.length);
7692
sha256.update(m1, 0, m1.length);
@@ -83,7 +99,7 @@ public byte[] H(byte[] pkSeed, ADRS adrs, byte[] m1, byte[] m2)
8399
{
84100
byte[] compressedADRS = compressedADRS(adrs);
85101

86-
((Memoable)msgDigest).reset(msgMemo);
102+
((Memoable) msgDigest).reset(msgMemo);
87103

88104
msgDigest.update(compressedADRS, 0, compressedADRS.length);
89105

@@ -138,7 +154,7 @@ public byte[] T_l(byte[] pkSeed, ADRS adrs, byte[] m)
138154
{
139155
byte[] compressedADRS = compressedADRS(adrs);
140156

141-
((Memoable)msgDigest).reset(msgMemo);
157+
((Memoable) msgDigest).reset(msgMemo);
142158

143159
msgDigest.update(compressedADRS, 0, compressedADRS.length);
144160
msgDigest.update(m, 0, m.length);
@@ -151,7 +167,7 @@ byte[] PRF(byte[] pkSeed, byte[] skSeed, ADRS adrs)
151167
{
152168
int n = skSeed.length;
153169

154-
((Memoable)sha256).reset(sha256Memo);
170+
((Memoable) sha256).reset(sha256Memo);
155171

156172
byte[] compressedADRS = compressedADRS(adrs);
157173

0 commit comments

Comments
 (0)