Skip to content

Commit 32ba114

Browse files
committed
refactoring of context into ParametersWithContext
1 parent d2df9f2 commit 32ba114

File tree

7 files changed

+116
-92
lines changed

7 files changed

+116
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.bouncycastle.crypto.params;
2+
3+
import org.bouncycastle.crypto.CipherParameters;
4+
import org.bouncycastle.util.Arrays;
5+
6+
public class ParametersWithContext
7+
implements CipherParameters
8+
{
9+
private CipherParameters parameters;
10+
private byte[] context;
11+
12+
public ParametersWithContext(
13+
CipherParameters parameters,
14+
byte[] context)
15+
{
16+
this.parameters = parameters;
17+
this.context = Arrays.clone(context);
18+
}
19+
20+
public byte[] getContext()
21+
{
22+
return Arrays.clone(context);
23+
}
24+
25+
public CipherParameters getParameters()
26+
{
27+
return parameters;
28+
}
29+
}

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/HashMLDSASigner.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
import org.bouncycastle.crypto.Signer;
1313
import org.bouncycastle.crypto.digests.SHA512Digest;
1414
import org.bouncycastle.crypto.digests.SHAKEDigest;
15+
import org.bouncycastle.crypto.params.ParametersWithContext;
1516
import org.bouncycastle.crypto.params.ParametersWithRandom;
1617
import org.bouncycastle.pqc.crypto.DigestUtils;
1718
import org.bouncycastle.util.Arrays;
1819

1920
public class HashMLDSASigner
2021
implements Signer
2122
{
23+
private static final byte[] EMPTY_CONTEXT = new byte[0];
24+
2225
private MLDSAPrivateKeyParameters privKey;
2326
private MLDSAPublicKeyParameters pubKey;
2427

@@ -34,6 +37,23 @@ public HashMLDSASigner()
3437

3538
public void init(boolean forSigning, CipherParameters param)
3639
{
40+
byte[] ctx;
41+
42+
if (param instanceof ParametersWithContext)
43+
{
44+
ctx = ((ParametersWithContext)param).getContext();
45+
param = ((ParametersWithContext)param).getParameters();
46+
47+
if (ctx.length > 255)
48+
{
49+
throw new IllegalArgumentException("context too long");
50+
}
51+
}
52+
else
53+
{
54+
ctx = EMPTY_CONTEXT;
55+
}
56+
3757
if (forSigning)
3858
{
3959
if (param instanceof ParametersWithRandom)
@@ -49,12 +69,6 @@ public void init(boolean forSigning, CipherParameters param)
4969

5070
engine = privKey.getParameters().getEngine(this.random);
5171

52-
byte[] ctx = privKey.getContext();
53-
if (ctx.length > 255)
54-
{
55-
throw new IllegalArgumentException("context too long");
56-
}
57-
5872
engine.initSign(privKey.tr, true, ctx);
5973

6074
initDigest(privKey);
@@ -64,13 +78,7 @@ public void init(boolean forSigning, CipherParameters param)
6478
pubKey = (MLDSAPublicKeyParameters)param;
6579

6680
engine = pubKey.getParameters().getEngine(this.random);
67-
68-
byte[] ctx = pubKey.getContext();
69-
if (ctx.length > 255)
70-
{
71-
throw new IllegalArgumentException("context too long");
72-
}
73-
81+
7482
engine.initVerify(pubKey.rho, pubKey.t1, true, ctx);
7583

7684
initDigest(pubKey);

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/MLDSAKeyParameters.java

-15
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,16 @@ public class MLDSAKeyParameters
77
{
88
private final MLDSAParameters params;
99

10-
private final byte[] context;
11-
12-
public MLDSAKeyParameters(boolean isPrivate, MLDSAParameters params, byte[] context)
13-
{
14-
super(isPrivate);
15-
this.params = params;
16-
this.context = context;
17-
}
18-
1910
public MLDSAKeyParameters(
2011
boolean isPrivate,
2112
MLDSAParameters params)
2213
{
2314
super(isPrivate);
2415
this.params = params;
25-
this.context = new byte[0];
2616
}
2717

2818
public MLDSAParameters getParameters()
2919
{
3020
return params;
3121
}
32-
33-
public byte[] getContext()
34-
{
35-
return context.clone();
36-
}
3722
}

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/MLDSASigner.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import org.bouncycastle.crypto.DataLengthException;
88
import org.bouncycastle.crypto.Signer;
99
import org.bouncycastle.crypto.digests.SHAKEDigest;
10+
import org.bouncycastle.crypto.params.ParametersWithContext;
1011
import org.bouncycastle.crypto.params.ParametersWithRandom;
1112

1213
public class MLDSASigner
1314
implements Signer
1415
{
16+
private static final byte[] EMPTY_CONTEXT = new byte[0];
17+
1518
private MLDSAPrivateKeyParameters privKey;
1619
private MLDSAPublicKeyParameters pubKey;
1720

@@ -27,6 +30,23 @@ public MLDSASigner()
2730
public void init(boolean forSigning, CipherParameters param)
2831
{
2932
boolean isPreHash;
33+
byte[] ctx;
34+
35+
if (param instanceof ParametersWithContext)
36+
{
37+
ctx = ((ParametersWithContext)param).getContext();
38+
param = ((ParametersWithContext)param).getParameters();
39+
40+
if (ctx.length > 255)
41+
{
42+
throw new IllegalArgumentException("context too long");
43+
}
44+
}
45+
else
46+
{
47+
ctx = EMPTY_CONTEXT;
48+
}
49+
3050

3151
if (forSigning)
3252
{
@@ -43,12 +63,6 @@ public void init(boolean forSigning, CipherParameters param)
4363

4464
engine = privKey.getParameters().getEngine(this.random);
4565

46-
byte[] ctx = privKey.getContext();
47-
if (ctx.length > 255)
48-
{
49-
throw new IllegalArgumentException("context too long");
50-
}
51-
5266
engine.initSign(privKey.tr, false, ctx);
5367

5468
msgDigest = engine.getShake256Digest();
@@ -61,12 +75,6 @@ public void init(boolean forSigning, CipherParameters param)
6175

6276
engine = pubKey.getParameters().getEngine(random);
6377

64-
byte[] ctx = pubKey.getContext();
65-
if (ctx.length > 255)
66-
{
67-
throw new IllegalArgumentException("context too long");
68-
}
69-
7078
engine.initVerify(pubKey.rho, pubKey.t1, false, ctx);
7179

7280
msgDigest = engine.getShake256Digest();
@@ -111,21 +119,21 @@ public boolean verifySignature(byte[] signature)
111119
boolean isTrue = engine.verifyInternal(signature, signature.length, msgDigest, pubKey.rho, pubKey.t1);
112120

113121
reset();
114-
122+
115123
return isTrue;
116124
}
117125

118126
public void reset()
119127
{
120128
msgDigest = engine.getShake256Digest();
121129
}
122-
130+
123131
protected byte[] internalGenerateSignature(byte[] message, byte[] random)
124132
{
125133
MLDSAEngine engine = privKey.getParameters().getEngine(this.random);
126134

127135
engine.initSign(privKey.tr, false, null);
128-
136+
129137
return engine.signInternal(message, message.length, privKey.rho, privKey.k, privKey.t0, privKey.s1, privKey.s2, random);
130138
}
131139

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.bouncycastle.crypto.digests.SHA256Digest;
1414
import org.bouncycastle.crypto.digests.SHA512Digest;
1515
import org.bouncycastle.crypto.digests.SHAKEDigest;
16+
import org.bouncycastle.crypto.params.ParametersWithContext;
1617
import org.bouncycastle.crypto.params.ParametersWithRandom;
1718
import org.bouncycastle.pqc.crypto.DigestUtils;
1819
import org.bouncycastle.util.Arrays;
@@ -23,6 +24,8 @@
2324
public class HashSLHDSASigner
2425
implements Signer
2526
{
27+
private static final byte[] EMPTY_CONTEXT = new byte[0];
28+
2629
private SLHDSAPrivateKeyParameters privKey;
2730
private SLHDSAPublicKeyParameters pubKey;
2831
private byte[] ctx;
@@ -36,6 +39,21 @@ public HashSLHDSASigner()
3639

3740
public void init(boolean forSigning, CipherParameters param)
3841
{
42+
if (param instanceof ParametersWithContext)
43+
{
44+
ctx = ((ParametersWithContext)param).getContext();
45+
param = ((ParametersWithContext)param).getParameters();
46+
47+
if (ctx.length > 255)
48+
{
49+
throw new IllegalArgumentException("context too long");
50+
}
51+
}
52+
else
53+
{
54+
ctx = EMPTY_CONTEXT;
55+
}
56+
3957
if (forSigning)
4058
{
4159
if (param instanceof ParametersWithRandom)
@@ -48,26 +66,12 @@ public void init(boolean forSigning, CipherParameters param)
4866
privKey = (SLHDSAPrivateKeyParameters)param;
4967
}
5068

51-
ctx = privKey.getContext();
52-
53-
if (ctx.length > 255)
54-
{
55-
throw new IllegalArgumentException("context too long");
56-
}
57-
5869
initDigest(privKey);
5970
}
6071
else
6172
{
6273
pubKey = (SLHDSAPublicKeyParameters)param;
63-
64-
ctx = pubKey.getContext();
65-
66-
if (ctx.length > 255)
67-
{
68-
throw new IllegalArgumentException("context too long");
69-
}
70-
74+
7175
initDigest(pubKey);
7276
}
7377

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

-14
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,15 @@ public class SLHDSAKeyParameters
66
extends AsymmetricKeyParameter
77
{
88
final SLHDSAParameters parameters;
9-
final byte[] context;
10-
11-
protected SLHDSAKeyParameters(boolean isPrivate, SLHDSAParameters parameters, byte[] context)
12-
{
13-
super(isPrivate);
14-
this.parameters = parameters;
15-
this.context = context;
16-
}
179

1810
protected SLHDSAKeyParameters(boolean isPrivate, SLHDSAParameters parameters)
1911
{
2012
super(isPrivate);
2113
this.parameters = parameters;
22-
this.context = new byte[0];
2314
}
2415

2516
public SLHDSAParameters getParameters()
2617
{
2718
return parameters;
2819
}
29-
30-
public byte[] getContext()
31-
{
32-
return context.clone();
33-
}
3420
}

0 commit comments

Comments
 (0)