Skip to content

Commit d670265

Browse files
committed
Return the bare signature from the MAYO, SNOVA, QR-UOV, SQIsign and AIMer signers rather than the NIST crypto_sign signed-message envelope, and require verification to be given exactly the parameter set's signature length, so that a signature no longer carries a copy of the signed data into every certificate and CMS structure built on it and trailing bytes are no longer ignored, with the KAT tests rebuilding the envelope as FalconTest already does, relates to github #2403.
1 parent 90d1f8a commit d670265

17 files changed

Lines changed: 258 additions & 81 deletions

File tree

‎core/src/main/java/org/bouncycastle/pqc/crypto/aimer/AIMerEngine.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ void run_phase_2_and_3(AIMerSignature sign, long[][][][] alpha_v_shares, MultChk
725725
/**
726726
* Generate an AIMER signature
727727
*
728-
* @param sig Output signature (will be filled)
728+
* @param sig Output signature (will be filled), exactly getSignatureBytes() long
729729
* @param m Message to sign
730730
* @param mlen Message length
731731
* @param sk Secret key
@@ -791,8 +791,7 @@ public int crypto_sign_signature(byte[] sig, byte[] m, int mlen, byte[] sk, AIMe
791791
//////////////////////////////////////////////////////
792792
// Phase 5: Opening the views of the MPC protocols. //
793793
//////////////////////////////////////////////////////
794-
System.arraycopy(m, 0, sig, 0, m.length);
795-
pos = m.length;
794+
pos = 0;
796795
System.arraycopy(sign.salt, 0, sig, pos, sign.salt.length);
797796
pos += sign.salt.length;
798797

‎core/src/main/java/org/bouncycastle/pqc/crypto/aimer/AIMerSigner.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void init(boolean forSigning, CipherParameters param)
6464
@Override
6565
public byte[] generateSignature(byte[] message)
6666
{
67-
byte[] sig = new byte[params.getSignatureBytes() + message.length];
67+
byte[] sig = new byte[params.getSignatureBytes()];
6868
AIMerEngine engine = new AIMerEngine(params);
6969
int result = engine.crypto_sign_signature(sig, message, message.length, privKey.getEncoded(), params, random);
7070
if (result == 0)
@@ -77,14 +77,15 @@ public byte[] generateSignature(byte[] message)
7777
@Override
7878
public boolean verifySignature(byte[] message, byte[] signature)
7979
{
80-
// the envelope is message || signature: any other length would be indexed past its end or leave a trailing tail unread.
81-
if (signature.length != message.length + params.getSignatureBytes())
80+
// An AIMer signature is exactly getSignatureBytes() long: a shorter buffer
81+
// would be indexed past its end, and accepting a longer one would make the
82+
// encoding non-unique - trailing bytes could be added to a valid signature
83+
// and it would still verify.
84+
if (signature.length != params.getSignatureBytes())
8285
{
8386
return false;
8487
}
85-
byte[] sig = new byte[params.getSignatureBytes()];
8688
AIMerEngine engine = new AIMerEngine(params);
87-
System.arraycopy(signature, message.length, sig, 0, params.getSignatureBytes());
88-
return engine.crypto_sign_verify(sig, sig.length, message, message.length, pubKey.getEncoded(), params) == 0;
89+
return engine.crypto_sign_verify(signature, signature.length, message, message.length, pubKey.getEncoded(), params) == 0;
8990
}
9091
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/mayo/MayoSigner.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void init(boolean forSigning, CipherParameters param)
8181
* Follows the signature generation process outlined in the MAYO specification document.
8282
*
8383
* @param message The message to be signed
84-
* @return The signature bytes concatenated with the original message
84+
* @return The signature bytes, {@link MayoParameters#getSigBytes()} of them
8585
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 8 and 10</a>
8686
*/
8787
@Override
@@ -256,7 +256,7 @@ public byte[] generateSignature(byte[] message)
256256
GF16.encode(s, sig, nk);
257257
System.arraycopy(salt, 0, sig, sig.length - saltBytes, saltBytes);
258258

259-
return Arrays.concatenate(sig, message);
259+
return sig;
260260
}
261261
finally
262262
{
@@ -287,12 +287,12 @@ public byte[] generateSignature(byte[] message)
287287
@Override
288288
public boolean verifySignature(byte[] message, byte[] signature)
289289
{
290-
// Reject a buffer too short to contain a signature before indexing it:
291-
// generateSignature returns the signature optionally followed by the
292-
// message (the signed-message envelope), and verify reads only the
293-
// leading getSigBytes() bytes (the encoded solution and the salt); a
294-
// shorter buffer would throw ArrayIndexOutOfBoundsException.
295-
if (signature.length < params.getSigBytes())
290+
// A MAYO signature is exactly getSigBytes() long (the encoded solution and
291+
// the salt), so require that: a shorter buffer would be indexed past its
292+
// end, and accepting a longer one would make the encoding non-unique -
293+
// trailing bytes could be added to a valid signature and it would still
294+
// verify.
295+
if (signature.length != params.getSigBytes())
296296
{
297297
return false;
298298
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/qruov/QRUOVSigner.java‎

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import org.bouncycastle.crypto.CryptoServicesRegistrar;
77
import org.bouncycastle.crypto.params.ParametersWithRandom;
88
import org.bouncycastle.pqc.crypto.MessageSigner;
9-
import org.bouncycastle.util.Arrays;
109

1110
/**
12-
* QR-UOV signature implementation. Signing outputs the canonical
13-
* {@code signature || message} envelope per the reference NIST KAT format;
14-
* verification accepts a signature whose tail can carry the message.
11+
* QR-UOV signature implementation. Signing outputs the bare signature,
12+
* {@link QRUOVParameters#getSignatureBytes()} of them, and verification
13+
* requires exactly that. The reference NIST KAT files record the
14+
* {@code signature || message} "sm" envelope instead; that is a property of
15+
* the KAT harness rather than of the signature, and the test rebuilds it.
1516
*/
1617
public class QRUOVSigner
1718
implements MessageSigner
@@ -79,17 +80,16 @@ public byte[] generateSignature(byte[] message)
7980
byte[] sigBytes = new byte[params.getSignatureBytes()];
8081
engine.storeSignature(sigR, sigS, sigBytes);
8182

82-
// The KAT envelope is signature || message
83-
return Arrays.concatenate(sigBytes, message);
83+
return sigBytes;
8484
}
8585

8686
public boolean verifySignature(byte[] message, byte[] signature)
8787
{
88-
// Reject a signature shorter than one parameter-set signature: the
89-
// signature || message envelope must carry at least the signature, and
90-
// a shorter buffer would throw ArrayIndexOutOfBoundsException in the
91-
// arraycopy below.
92-
if (signature.length < params.getSignatureBytes())
88+
// A QR-UOV signature is exactly getSignatureBytes() long, so require
89+
// that: a shorter buffer would be indexed past its end, and accepting a
90+
// longer one would make the encoding non-unique - trailing bytes could
91+
// be added to a valid signature and it would still verify.
92+
if (signature.length != params.getSignatureBytes())
9393
{
9494
return false;
9595
}
@@ -110,22 +110,9 @@ public boolean verifySignature(byte[] message, byte[] signature)
110110
byte[][][][] P3 = new byte[m][M][L][M];
111111
engine.restoreP3(pkBytes, pb, P3);
112112

113-
// Only the first signatureBytes of `signature` are the actual sig.
114-
int sigBytes = params.getSignatureBytes();
115-
byte[] sigOnly;
116-
if (signature.length == sigBytes)
117-
{
118-
sigOnly = signature;
119-
}
120-
else
121-
{
122-
sigOnly = new byte[sigBytes];
123-
System.arraycopy(signature, 0, sigOnly, 0, sigBytes);
124-
}
125-
126113
byte[] sigR = new byte[saltLen];
127114
byte[][] sigS = new byte[N][L];
128-
engine.restoreSignature(sigOnly, sigR, sigS);
115+
engine.restoreSignature(signature, sigR, sigS);
129116

130117
return engine.verify(seedPk, P3, message, sigR, sigS);
131118
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/snova/SnovaSigner.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ public byte[] generateSignature(byte[] message)
101101
}
102102
signDigestCore(signature, hash, salt, keyElements.map1.aAlpha, keyElements.map1.bAlpha, keyElements.map1.qAlpha1, keyElements.map1.qAlpha2,
103103
keyElements.T12, keyElements.map2.f11, keyElements.map2.f12, keyElements.map2.f21, publicKeySeed, ptPrivateKeySeed);
104-
return Arrays.concatenate(signature, message);
104+
return signature;
105105
}
106106

107107
@Override
108108
public boolean verifySignature(byte[] message, byte[] signature)
109109
{
110-
// Reject a buffer too short to contain a signature before indexing it:
111-
// generateSignature returns the signature optionally followed by the
112-
// message (the signed-message envelope), and verifySignatureCore reads
113-
// only the leading signature bytes; a shorter buffer would throw
114-
// ArrayIndexOutOfBoundsException.
115-
if (signature.length < ((params.getN() * params.getLsq() + 1) >>> 1) + params.getSaltLength())
110+
// A Snova signature is exactly the encoded solution plus the salt, so
111+
// require that length: a shorter buffer would be indexed past its end,
112+
// and accepting a longer one would make the encoding non-unique -
113+
// trailing bytes could be added to a valid signature and it would still
114+
// verify.
115+
if (signature.length != ((params.getN() * params.getLsq() + 1) >>> 1) + params.getSaltLength())
116116
{
117117
return false;
118118
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/sqisign/SQIsignSigner.java‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ private byte[] signLvl1(byte[] message)
106106
{
107107
throw new IllegalStateException("SQIsign sign: protocols_sign failed");
108108
}
109-
return org.bouncycastle.util.Arrays.concatenate(
110-
SQIsignEncodeLvl1.signatureToBytes(sig), message);
109+
return SQIsignEncodeLvl1.signatureToBytes(sig);
111110
}
112111

113112
private byte[] signLvl3(byte[] message)
@@ -122,8 +121,7 @@ private byte[] signLvl3(byte[] message)
122121
{
123122
throw new IllegalStateException("SQIsign sign: protocols_sign failed");
124123
}
125-
return org.bouncycastle.util.Arrays.concatenate(
126-
SQIsignEncodeLvl3.signatureToBytes(sig), message);
124+
return SQIsignEncodeLvl3.signatureToBytes(sig);
127125
}
128126

129127
private byte[] signLvl5(byte[] message)
@@ -138,8 +136,7 @@ private byte[] signLvl5(byte[] message)
138136
{
139137
throw new IllegalStateException("SQIsign sign: protocols_sign failed");
140138
}
141-
return org.bouncycastle.util.Arrays.concatenate(
142-
SQIsignEncodeLvl5.signatureToBytes(sig), message);
139+
return SQIsignEncodeLvl5.signatureToBytes(sig);
143140
}
144141

145142
public boolean verifySignature(byte[] message, byte[] signature)
@@ -165,16 +162,14 @@ public boolean verifySignature(byte[] message, byte[] signature)
165162

166163
private boolean verifyLvl1(byte[] message, byte[] signature)
167164
{
168-
if (signature == null || signature.length < SQIsignEncodeLvl1.SIGNATURE_BYTES)
165+
if (signature == null || signature.length != SQIsignEncodeLvl1.SIGNATURE_BYTES)
169166
{
170167
return false;
171168
}
172169
SQIsignPublicKeyData pk = SQIsignEncodeLvl1.publicKeyFromBytes(pubKey.getPublicKey());
173170
SQIsignSignatureLvl1 sig;
174171
try
175172
{
176-
// The first SIGNATURE_BYTES are the signature; any trailing bytes
177-
// (e.g. the appended message in NIST "sm" format) are ignored.
178173
sig = SQIsignEncodeLvl1.signatureFromBytes(signature);
179174
}
180175
catch (IllegalArgumentException e)
@@ -193,7 +188,7 @@ private boolean verifyLvl1(byte[] message, byte[] signature)
193188

194189
private boolean verifyLvl3(byte[] message, byte[] signature)
195190
{
196-
if (signature == null || signature.length < SQIsignEncodeLvl3.SIGNATURE_BYTES)
191+
if (signature == null || signature.length != SQIsignEncodeLvl3.SIGNATURE_BYTES)
197192
{
198193
return false;
199194
}
@@ -219,7 +214,7 @@ private boolean verifyLvl3(byte[] message, byte[] signature)
219214

220215
private boolean verifyLvl5(byte[] message, byte[] signature)
221216
{
222-
if (signature == null || signature.length < SQIsignEncodeLvl5.SIGNATURE_BYTES)
217+
if (signature == null || signature.length != SQIsignEncodeLvl5.SIGNATURE_BYTES)
223218
{
224219
return false;
225220
}

‎core/src/test/java/org/bouncycastle/pqc/crypto/mayo/MayoRetryTest.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ public void testRetryProducesValidSignature()
7676
RetryForcingSigner signer = new RetryForcingSigner();
7777
signer.init(true, new ParametersWithRandom(
7878
(MayoPrivateKeyParameters)kp.getPrivate(), random));
79-
byte[] sigPlusMsg = signer.generateSignature(message);
79+
byte[] sig = signer.generateSignature(message);
8080

81-
// generateSignature returns sig || message; verifySignature expects the
82-
// signature portion only.
83-
int sigBytes = parameters.getSigBytes();
84-
byte[] sig = new byte[sigBytes];
85-
System.arraycopy(sigPlusMsg, 0, sig, 0, sigBytes);
81+
assertEquals("signature length for " + parameters,
82+
parameters.getSigBytes(), sig.length);
8683

8784
MayoSigner verifier = new MayoSigner();
8885
verifier.init(false, (MayoPublicKeyParameters)kp.getPublic());

‎core/src/test/java/org/bouncycastle/pqc/crypto/test/AIMerTest.java‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,31 @@ public MessageSigner getMessageSigner()
9898
{
9999
return new AIMerSigner();
100100
}
101+
102+
// the KAT files record the NIST crypto_sign "sm" envelope, which
103+
// AIMer's reference harness lays out as message || signature;
104+
// the signer returns the bare signature.
105+
@Override
106+
public byte[] toVectorSignature(byte[] signature, byte[] message)
107+
{
108+
return Arrays.concatenate(message, signature);
109+
}
101110
});
102111
long end = System.currentTimeMillis();
103112
System.out.println("time cost: " + (end - start) + "\n");
104113
}
105114

106115
/**
107-
* verifySignature() is handed the message || signature envelope
108-
* generateSignature() produces, so it has to require exactly that length:
109-
* a shorter buffer used to be indexed past its end, and a longer one had
110-
* its trailing bytes ignored, so a valid signature with data appended
111-
* still verified.
116+
* verifySignature() takes the bare signature, so it has to require exactly
117+
* getSignatureBytes(): a shorter buffer used to be indexed past its end, and
118+
* a longer one had its trailing bytes ignored, so a valid signature with data
119+
* appended still verified.
112120
*
113-
* The first KAT vector of each parameter set supplies a public key and a
114-
* valid envelope, which keeps this off the key generation and signing
115-
* paths - a valid envelope is what makes the appended-data cases bite.
121+
* The first KAT vector of each parameter set supplies a public key and the
122+
* message || signature envelope the reference harness records, which keeps
123+
* this off the key generation and signing paths - a valid signature is what
124+
* makes the appended-data cases bite. It also pins the envelope itself:
125+
* handing verify the whole thing must now fail.
116126
*/
117127
public void testWrongLengthSignatureRejected()
118128
throws Exception
@@ -125,22 +135,25 @@ public void testWrongLengthSignatureRejected()
125135
HashMap vector = readFirstVector(files[fileIndex]);
126136
byte[] message = Hex.decode((String)vector.get("msg"));
127137
byte[] pk = Hex.decode((String)vector.get("pk"));
128-
byte[] signature = Hex.decode((String)vector.get("sm"));
138+
byte[] sm = Hex.decode((String)vector.get("sm"));
129139

130140
AIMerSigner verifier = new AIMerSigner();
131141
verifier.init(false, new AIMerPublicKeyParameters(parameters, pk));
132142

133-
assertEquals(name, message.length + parameters.getSignatureBytes(), signature.length);
143+
assertEquals(name, message.length + parameters.getSignatureBytes(), sm.length);
144+
byte[] signature = Arrays.copyOfRange(sm, message.length, sm.length);
134145
assertTrue(name, verifier.verifySignature(message, signature));
135146

136-
// a short envelope must be rejected rather than indexed past its end
147+
// a short signature must be rejected rather than indexed past its end
137148
assertFalse(name, verifier.verifySignature(message, new byte[0]));
138-
assertFalse(name, verifier.verifySignature(message, Arrays.copyOf(signature, message.length)));
139149
assertFalse(name, verifier.verifySignature(message, Arrays.copyOf(signature, signature.length - 1)));
140150

141151
// trailing data must not be silently ignored
142152
assertFalse(name, verifier.verifySignature(message, Arrays.append(signature, (byte)0)));
143153
assertFalse(name, verifier.verifySignature(message, Arrays.concatenate(signature, new byte[16])));
154+
155+
// and the KAT envelope is not itself a signature
156+
assertFalse(name, verifier.verifySignature(message, sm));
144157
}
145158
}
146159

‎core/src/test/java/org/bouncycastle/pqc/crypto/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static Test suite()
5858
suite.addTestSuite(AIMerTest.class);
5959
suite.addTestSuite(PublicKeyLengthValidationTest.class);
6060
suite.addTestSuite(PqcMalformedInputTest.class);
61+
suite.addTestSuite(PqcSignatureEncodingTest.class);
6162
suite.addTestSuite(PqcUnmappedAlgorithmOidTest.class);
6263
suite.addTestSuite(SmaugTTest.class);
6364

‎core/src/test/java/org/bouncycastle/pqc/crypto/test/MayoTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.bouncycastle.pqc.crypto.mayo.MayoPrivateKeyParameters;
1414
import org.bouncycastle.pqc.crypto.mayo.MayoPublicKeyParameters;
1515
import org.bouncycastle.pqc.crypto.mayo.MayoSigner;
16+
import org.bouncycastle.util.Arrays;
1617

1718
public class MayoTest
1819
extends TestCase
@@ -86,6 +87,14 @@ public MessageSigner getMessageSigner()
8687
{
8788
return new MayoSigner();
8889
}
90+
91+
// the KAT files record the NIST crypto_sign "sm" envelope,
92+
// signature || message; the signer returns the bare signature.
93+
@Override
94+
public byte[] toVectorSignature(byte[] signature, byte[] message)
95+
{
96+
return Arrays.concatenate(signature, message);
97+
}
8998
});
9099
long end = System.currentTimeMillis();
91100
System.out.println("time cost: " + (end - start) +"\n");

0 commit comments

Comments
 (0)