Skip to content

Commit 27bb74c

Browse files
author
gefeili
committed
Merge branch 'main' into 1958-aead-parameters
2 parents 20da394 + eb646df commit 27bb74c

File tree

7 files changed

+298
-89
lines changed

7 files changed

+298
-89
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/mayo/MayoParameters.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ public class MayoParameters
1818
40, // r_bytes
1919
120159, // P1_bytes
2020
24336, // P2_bytes
21-
// P3_bytes
2221
24, // csk_bytes
2322
1420, // cpk_bytes
2423
454, // sig_bytes
2524
new int[]{8, 1, 1, 0}, // F_TAIL_78
26-
new byte[]{8, 1, 1, 0}, // f_tail_arr
2725
24, // salt_bytes
2826
32, // digest_bytes
29-
// pk_seed_bytes
3027
24 // sk_seed_bytes
3128
);
3229

@@ -39,22 +36,18 @@ public class MayoParameters
3936
81 - 17, // v = 64
4037
4 * 17 + 1, // A_cols = 4 * 17 + 1 = 69
4138
4, // k
42-
// q
4339
32, // m_bytes
4440
544, // O_bytes
4541
32, // v_bytes
4642
34, // r_bytes
4743
66560, // P1_bytes
4844
34816, // P2_bytes
49-
// P3_bytes
5045
24, // csk_bytes
5146
4912, // cpk_bytes
5247
186, // sig_bytes
5348
new int[]{8, 0, 2, 8}, //F_TAIL_64
54-
new byte[]{8, 0, 2, 8}, // f_tail_arr
5549
24, // salt_bytes
5650
32, // digest_bytes
57-
// pk_seed_bytes
5851
24 // sk_seed_bytes
5952
);
6053

@@ -67,22 +60,18 @@ public class MayoParameters
6760
118 - 10, // v = 108
6861
11 * 10 + 1, // A_cols = 11 * 10 + 1 = 111
6962
11, // k
70-
// q
7163
54, // m_bytes
7264
540, // O_bytes
7365
54, // v_bytes
7466
55, // r_bytes
7567
317844, // P1_bytes
7668
58320, // P2_bytes
77-
// P3_bytes
7869
32, // csk_bytes
7970
2986, // cpk_bytes
8071
681, // sig_bytes
8172
new int[]{8, 0, 1, 7}, //F_TAIL_108
82-
new byte[]{8, 0, 1, 7}, // f_tail_arr
8373
32, // salt_bytes
8474
48, // digest_bytes
85-
// pk_seed_bytes
8675
32 // sk_seed_bytes
8776
);
8877

@@ -95,22 +84,18 @@ public class MayoParameters
9584
154 - 12, // v = 142
9685
12 * 12 + 1, // A_cols = 12 * 12 + 1 = 145
9786
12, // k
98-
// q
9987
71, // m_bytes
10088
852, // O_bytes
10189
71, // v_bytes
10290
72, // r_bytes
10391
720863, // P1_bytes
10492
120984, // P2_bytes
105-
// P3_bytes
10693
40, // csk_bytes
10794
5554, // cpk_bytes
10895
964, // sig_bytes
10996
new int[]{4, 0, 8, 1}, //F_TAIL_142
110-
new byte[]{4, 0, 8, 1}, // f_tail_arr
11197
40, // salt_bytes
11298
64, // digest_bytes
113-
// pk_seed_bytes
11499
40 // sk_seed_bytes
115100
);
116101

@@ -133,15 +118,14 @@ public class MayoParameters
133118
private final int cpkBytes;
134119
private final int sigBytes;
135120
private final int[] fTail;
136-
private final byte[] fTailArr;
137121
private final int saltBytes;
138122
private final int digestBytes;
139123
private static final int pkSeedBytes = 16;
140124
private final int skSeedBytes;
141125

142126
private MayoParameters(String name, int n, int m, int mVecLimbs, int o, int v, int ACols, int k,
143127
int mBytes, int OBytes, int vBytes, int rBytes, int P1Bytes, int P2Bytes,
144-
int cskBytes, int cpkBytes, int sigBytes, int[] fTail, byte[] fTailArr,
128+
int cskBytes, int cpkBytes, int sigBytes, int[] fTail,
145129
int saltBytes, int digestBytes, int skSeedBytes)
146130
{
147131
this.name = name;
@@ -162,7 +146,6 @@ private MayoParameters(String name, int n, int m, int mVecLimbs, int o, int v, i
162146
this.cpkBytes = cpkBytes;
163147
this.sigBytes = sigBytes;
164148
this.fTail = fTail;
165-
this.fTailArr = fTailArr;
166149
this.saltBytes = saltBytes;
167150
this.digestBytes = digestBytes;
168151
this.skSeedBytes = skSeedBytes;
@@ -258,11 +241,6 @@ public int[] getFTail()
258241
return fTail;
259242
}
260243

261-
public byte[] getFTailArr()
262-
{
263-
return fTailArr;
264-
}
265-
266244
public int getSaltBytes()
267245
{
268246
return saltBytes;
@@ -288,7 +266,7 @@ public int getSkSeedBytes()
288266
*/
289267
public int getP1Limbs()
290268
{
291-
return ((v * (v + 1)) / 2) * mVecLimbs;
269+
return ((v * (v + 1)) >> 1) * mVecLimbs;
292270
}
293271

294272
/**
@@ -304,7 +282,7 @@ public int getP2Limbs()
304282
*/
305283
public int getP3Limbs()
306284
{
307-
return ((o * (o + 1)) / 2) * mVecLimbs;
285+
return ((o * (o + 1)) >> 1) * mVecLimbs;
308286
}
309287
}
310288

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ public byte[] generateSignature(byte[] message)
131131
// Generate S = seed_pk || (additional bytes), using SHAKE256.
132132
// Output length is param_pk_seed_bytes + param_O_bytes.
133133
shake.update(seed_sk, 0, seed_sk.length);
134-
shake.doFinal(seed_pk, 0, pk_seed_bytes + oBytes);
134+
shake.doFinal(seed_pk, 0, totalS);
135135

136136
// Decode the portion of S after the first param_pk_seed_bytes into O.
137137
// (In C, this is: decode(S + param_pk_seed_bytes, O, param_v * param_o))
138-
Utils.decode(seed_pk, pk_seed_bytes, O, 0, v * o);
138+
Utils.decode(seed_pk, pk_seed_bytes, O, 0, O.length);
139139

140140
// Expand P1 and P2 into the long array P using seed_pk.
141141
Utils.expandP1P2(params, P, seed_pk);
@@ -434,7 +434,7 @@ void computeA(long[] Mtmp, byte[] AOut)
434434
final int m = params.getM();
435435
final int mVecLimbs = params.getMVecLimbs();
436436
final int ACols = params.getACols();
437-
final byte[] fTailArr = params.getFTailArr();
437+
final int[] fTailArr = params.getFTail();
438438

439439
int bitsToShift = 0;
440440
int wordsToShift = 0;
@@ -514,7 +514,7 @@ void computeA(long[] Mtmp, byte[] AOut)
514514
byte[] tab = new byte[F_TAIL_LEN << 2];
515515
for (int i = 0, idx = 0; i < F_TAIL_LEN; i++)
516516
{
517-
byte ft = fTailArr[i];
517+
int ft = fTailArr[i];
518518
tab[idx++] = (byte)GF16Utils.mulF(ft, 1);
519519
tab[idx++] = (byte)GF16Utils.mulF(ft, 2);
520520
tab[idx++] = (byte)GF16Utils.mulF(ft, 4);
@@ -798,13 +798,8 @@ void ef(byte[] A, int nrows, int ncols)
798798
for (int i = 0, irowLen = 0; i < nrows; i++, irowLen += rowLen)
799799
{
800800
Pack.longToLittleEndian(packedA, irowLen, len_4, bytes, 0);
801-
int j = 0;
802-
for (; j < ncols >> 1; j++)
803-
{
804-
A[outIndex++] = (byte)(bytes[j] & 0x0F); // Lower nibble
805-
A[outIndex++] = (byte)((bytes[j] >> 4) & 0x0F); // Upper nibble
806-
}
807-
A[outIndex++] = (byte)(bytes[j] & 0x0F);
801+
Utils.decode(bytes, 0, A, outIndex, ncols);
802+
outIndex += ncols;
808803
}
809804
}
810805

pkix/src/main/java/org/bouncycastle/cms/CMSSignedDataStreamGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ else if (tagged.getTagNo() == 3)
372372
return new ASN1Integer(1);
373373
}
374374

375-
private boolean checkForVersion3(List signerInfos, List signerInfoGens)
375+
private static boolean checkForVersion3(List signerInfos, List signerInfoGens)
376376
{
377377
for (Iterator it = signerInfos.iterator(); it.hasNext();)
378378
{
379-
SignerInfo s = SignerInfo.getInstance(((SignerInformation)it.next()).toASN1Structure());
379+
SignerInfo s = ((SignerInformation)it.next()).toASN1Structure();
380380

381-
if (s.getVersion().intValueExact() == 3)
381+
if (s.getVersion().hasValue(3))
382382
{
383383
return true;
384384
}

pkix/src/main/java/org/bouncycastle/cms/KEMRecipientInformation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class KEMRecipientInformation
2727
{
2828
ASN1OctetString octs = ASN1OctetString.getInstance(r.getId());
2929

30-
rid = new KEMRecipientId(octs.getOctets()); // TODO: should be KEM
30+
rid = new KEMRecipientId(octs.getOctets());
3131
}
3232
else
3333
{
3434
IssuerAndSerialNumber iAnds = IssuerAndSerialNumber.getInstance(r.getId());
3535

36-
rid = new KEMRecipientId(iAnds.getName(), iAnds.getSerialNumber().getValue()); // TODO:
36+
rid = new KEMRecipientId(iAnds.getName(), iAnds.getSerialNumber().getValue());
3737
}
3838
}
3939

pkix/src/test/java/org/bouncycastle/cms/test/CMSTestUtil.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public class CMSTestUtil
6161
public static KeyPairGenerator ecDsaKpg;
6262
public static KeyPairGenerator ed25519Kpg;
6363
public static KeyPairGenerator ed448Kpg;
64-
public static KeyPairGenerator mlKemKpg;
64+
public static KeyPairGenerator mlDsa44Kpg;
65+
public static KeyPairGenerator mlDsa65Kpg;
66+
public static KeyPairGenerator mlDsa87Kpg;
67+
public static KeyPairGenerator mlKem512Kpg;
68+
public static KeyPairGenerator mlKem768Kpg;
69+
public static KeyPairGenerator mlKem1024Kpg;
6570
public static KeyPairGenerator ntruKpg;
6671
public static KeyGenerator aes192kg;
6772
public static KeyGenerator desede128kg;
@@ -168,7 +173,14 @@ public class CMSTestUtil
168173
ed448Kpg = KeyPairGenerator.getInstance("Ed448", "BC");
169174

170175
ntruKpg = KeyPairGenerator.getInstance(BCObjectIdentifiers.ntruhps2048509.getId(), "BC");
171-
mlKemKpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
176+
177+
mlDsa44Kpg = KeyPairGenerator.getInstance("ML-DSA-44", "BC");
178+
mlDsa65Kpg = KeyPairGenerator.getInstance("ML-DSA-65", "BC");
179+
mlDsa87Kpg = KeyPairGenerator.getInstance("ML-DSA-87", "BC");
180+
181+
mlKem512Kpg = KeyPairGenerator.getInstance("ML-KEM-512", "BC");
182+
mlKem768Kpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
183+
mlKem1024Kpg = KeyPairGenerator.getInstance("ML-KEM-1024", "BC");
172184

173185
aes192kg = KeyGenerator.getInstance("AES", "BC");
174186
aes192kg.init(192, rand);
@@ -281,9 +293,34 @@ public static KeyPair makeNtruKeyPair()
281293
return ntruKpg.generateKeyPair();
282294
}
283295

284-
public static KeyPair makeMLKemKeyPair()
296+
public static KeyPair makeMLKem512KeyPair()
297+
{
298+
return mlKem512Kpg.generateKeyPair();
299+
}
300+
301+
public static KeyPair makeMLKem768KeyPair()
302+
{
303+
return mlKem768Kpg.generateKeyPair();
304+
}
305+
306+
public static KeyPair makeMLKem1024KeyPair()
307+
{
308+
return mlKem1024Kpg.generateKeyPair();
309+
}
310+
311+
public static KeyPair makeMLDsa44KeyPair()
285312
{
286-
return mlKemKpg.generateKeyPair();
313+
return mlDsa44Kpg.generateKeyPair();
314+
}
315+
316+
public static KeyPair makeMLDsa65KeyPair()
317+
{
318+
return mlDsa65Kpg.generateKeyPair();
319+
}
320+
321+
public static KeyPair makeMLDsa87KeyPair()
322+
{
323+
return mlDsa87Kpg.generateKeyPair();
287324
}
288325

289326
public static SecretKey makeDesede128Key()
@@ -504,6 +541,10 @@ public static X509Certificate makeOaepCertificate(KeyPair subKP, String _subDN,
504541

505542
private static JcaContentSignerBuilder makeContentSignerBuilder(PublicKey issPub)
506543
{
544+
/*
545+
* NOTE: Current ALL test certificates are issued under a SHA1withRSA root, so this list is mostly
546+
* redundant (and also incomplete in that it doesn't handle EdDSA or ML-DSA issuers).
547+
*/
507548
JcaContentSignerBuilder contentSignerBuilder;
508549
if (issPub instanceof RSAPublicKey)
509550
{
@@ -521,10 +562,14 @@ else if (issPub.getAlgorithm().equals("ECGOST3410"))
521562
{
522563
contentSignerBuilder = new JcaContentSignerBuilder("GOST3411withECGOST3410");
523564
}
524-
else
565+
else if (issPub.getAlgorithm().equals("GOST3410"))
525566
{
526567
contentSignerBuilder = new JcaContentSignerBuilder("GOST3411WithGOST3410");
527568
}
569+
else
570+
{
571+
throw new UnsupportedOperationException("Algorithm handlers incomplete");
572+
}
528573

529574
contentSignerBuilder.setProvider(BouncyCastleProvider.PROVIDER_NAME);
530575

0 commit comments

Comments
 (0)