Skip to content

Commit d2df9f2

Browse files
committed
refactoring of context setting
1 parent 42b5928 commit d2df9f2

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class HashSLHDSASigner
2525
{
2626
private SLHDSAPrivateKeyParameters privKey;
2727
private SLHDSAPublicKeyParameters pubKey;
28-
28+
private byte[] ctx;
2929
private SecureRandom random;
3030
private Digest digest;
3131
private byte[] digestOidEncoding;
@@ -48,12 +48,26 @@ public void init(boolean forSigning, CipherParameters param)
4848
privKey = (SLHDSAPrivateKeyParameters)param;
4949
}
5050

51+
ctx = privKey.getContext();
52+
53+
if (ctx.length > 255)
54+
{
55+
throw new IllegalArgumentException("context too long");
56+
}
57+
5158
initDigest(privKey);
5259
}
5360
else
5461
{
5562
pubKey = (SLHDSAPublicKeyParameters)param;
5663

64+
ctx = pubKey.getContext();
65+
66+
if (ctx.length > 255)
67+
{
68+
throw new IllegalArgumentException("context too long");
69+
}
70+
5771
initDigest(pubKey);
5872
}
5973

@@ -93,12 +107,6 @@ public byte[] generateSignature() throws CryptoException, DataLengthException
93107
SLHDSAEngine engine = privKey.getParameters().getEngine();
94108

95109
engine.init(privKey.pk.seed);
96-
byte[] ctx = privKey.getContext();
97-
98-
if (ctx.length > 255)
99-
{
100-
throw new RuntimeException("Context too long");
101-
}
102110

103111
byte[] hash = new byte[digest.getDigestSize()];
104112
digest.doFinal(hash, 0);
@@ -118,13 +126,6 @@ public byte[] generateSignature() throws CryptoException, DataLengthException
118126
@Override
119127
public boolean verifySignature(byte[] signature)
120128
{
121-
byte[] ctx = pubKey.getContext();
122-
123-
if (ctx.length > 255)
124-
{
125-
throw new RuntimeException("Context too long");
126-
}
127-
128129
byte[] hash = new byte[digest.getDigestSize()];
129130
digest.doFinal(hash, 0);
130131

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SLHDSASigner
2222
{
2323
private SLHDSAPrivateKeyParameters privKey;
2424
private SLHDSAPublicKeyParameters pubKey;
25-
25+
private byte[] ctx;
2626
private SecureRandom random;
2727

2828
/**
@@ -48,11 +48,26 @@ public void init(boolean forSigning, CipherParameters param)
4848
privKey = (SLHDSAPrivateKeyParameters)param;
4949
}
5050

51+
ctx = privKey.getContext();
52+
53+
if (ctx.length > 255)
54+
{
55+
throw new IllegalArgumentException("context too long");
56+
}
57+
5158
isPreHash = privKey.parameters.isPreHash();
5259
}
5360
else
5461
{
5562
pubKey = (SLHDSAPublicKeyParameters)param;
63+
64+
ctx = pubKey.getContext();
65+
66+
if (ctx.length > 255)
67+
{
68+
throw new IllegalArgumentException("context too long");
69+
}
70+
5671
isPreHash = pubKey.parameters.isPreHash();
5772
}
5873

@@ -67,12 +82,6 @@ public byte[] generateSignature(byte[] message)
6782
SLHDSAEngine engine = privKey.getParameters().getEngine();
6883

6984
engine.init(privKey.pk.seed);
70-
byte[] ctx = privKey.getContext();
71-
72-
if (ctx.length > 255)
73-
{
74-
throw new RuntimeException("Context too long");
75-
}
7685

7786
byte[] ds_message = new byte[1 + 1 + ctx.length + message.length];
7887
ds_message[0] = 0;
@@ -88,12 +97,6 @@ public byte[] generateSignature(byte[] message)
8897
// Equivalent to slh_verify_internal from specs
8998
public boolean verifySignature(byte[] message, byte[] signature)
9099
{
91-
byte[] ctx = pubKey.getContext();
92-
if (ctx.length > 255)
93-
{
94-
throw new RuntimeException("Context too long");
95-
}
96-
97100
byte[] ds_message = new byte[1 + 1 + ctx.length + message.length];
98101
ds_message[0] = 0;
99102
ds_message[1] = (byte)ctx.length;
@@ -102,6 +105,7 @@ public boolean verifySignature(byte[] message, byte[] signature)
102105

103106
return internalVerifySignature(ds_message, signature);
104107
}
108+
105109
public boolean internalVerifySignature(byte[] message, byte[] signature)
106110
{
107111
//# Input: Message M, signature SIG, public key PK

0 commit comments

Comments
 (0)