Skip to content

Commit 8954c62

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 35349ab + 8b4326f commit 8954c62

File tree

7 files changed

+131
-64
lines changed

7 files changed

+131
-64
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/AsconCXof128.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,26 @@ public void reset()
8181

8282
private void initState(byte[] z, int zOff, int zLen)
8383
{
84-
p.set(7445901275803737603L, 4886737088792722364L, -1616759365661982283L, 3076320316797452470L, -8124743304765850554L);
85-
p.x0 ^= ((long)zLen) << 3;
86-
p.p(12);
87-
update(z, zOff, zLen);
88-
padAndAbsorb();
84+
// p.set(0x0000080000cc0004L, 0L, 0L, 0L, 0L);
85+
// p.p(12);
86+
87+
if (zLen == 0)
88+
{
89+
// p.p(12);
90+
// padAndAbsorb();
91+
92+
p.set(0x500cccc894e3c9e8L, 0x5bed06f28f71248dL, 0x3b03a0f930afd512L, 0x112ef093aa5c698bL, 0x00c8356340a347f0L);
93+
}
94+
else
95+
{
96+
p.set(0x675527c2a0e8de03L, 0x43d12d7dc0377bbcL, 0xe9901dec426e81b5L, 0x2ab14907720780b6L, 0x8f3f1d02d432bc46L);
97+
98+
p.x0 ^= ((long)zLen) << 3;
99+
p.p(12);
100+
update(z, zOff, zLen);
101+
padAndAbsorb();
102+
}
103+
89104
super.reset();
90105
}
91106
}
92-

core/src/main/java/org/bouncycastle/crypto/digests/AsconXofBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void ensureNoAbsorbWhileSqueezing(boolean m_squeezing)
100100
{
101101
if (m_squeezing)
102102
{
103-
throw new IllegalArgumentException("attempt to absorb while squeezing");
103+
throw new IllegalStateException("attempt to absorb while squeezing");
104104
}
105105
}
106106
}

core/src/main/java/org/bouncycastle/crypto/engines/Grain128AEADEngine.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,43 +239,49 @@ protected void processBufferAAD(byte[] input, int inOff)
239239
@Override
240240
protected void processFinalAAD()
241241
{
242+
// Encode(ad length) denotes the message length encoded in the DER format.
243+
242244
int len = aadOperator.getLen();
243245
byte[] input = ((StreamAADOperator)aadOperator).getBytes();
244-
byte[] ader;
245246

246-
//encodeDer
247+
// Need up to 5 bytes for the DER length as an 'int'
248+
byte[] ader = new byte[5];
249+
250+
int pos;
247251
if (len < 128)
248252
{
249-
ader = new byte[1];
250-
ader[0] = (byte)len;
253+
pos = ader.length - 1;
254+
ader[pos] = (byte)len;
251255
}
252256
else
253257
{
254-
// aderlen is the highest bit position divided by 8
255-
int aderlen = len_length(len);
256-
ader = new byte[1 + aderlen];
257-
ader[0] = (byte)(0x80 | aderlen);
258-
int tmp = len;
259-
for (int i = 1; i < ader.length; ++i)
258+
pos = ader.length;
259+
260+
int dl = len;
261+
do
260262
{
261-
ader[i] = (byte)tmp;
262-
tmp >>>= 8;
263+
ader[--pos] = (byte)dl;
264+
dl >>>= 8;
263265
}
266+
while (dl != 0);
267+
268+
int count = ader.length - pos;
269+
ader[--pos] = (byte)(0x80 | count);
264270
}
265271

266-
absorbAadData(ader, ader.length);
267-
absorbAadData(input, len);
272+
absorbAadData(ader, pos, ader.length - pos);
273+
absorbAadData(input, 0, len);
268274
}
269275

270-
private void absorbAadData(byte[] ader, int len)
276+
private void absorbAadData(byte[] buf, int off, int len)
271277
{
272278
for (int i = 0; i < len; ++i)
273279
{
274-
byte ader_i = ader[i];
280+
byte b = buf[off + i];
275281
for (int j = 0; j < 8; ++j)
276282
{
277283
shift();
278-
updateInternalState((ader_i >> j) & 1);
284+
updateInternalState((b >> j) & 1);
279285
}
280286
}
281287
}
@@ -319,21 +325,4 @@ protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int
319325
output[outOff + i] = cc;
320326
}
321327
}
322-
323-
private static int len_length(int v)
324-
{
325-
if ((v & 0xff) == v)
326-
{
327-
return 1;
328-
}
329-
if ((v & 0xffff) == v)
330-
{
331-
return 2;
332-
}
333-
if ((v & 0xffffff) == v)
334-
{
335-
return 3;
336-
}
337-
return 4;
338-
}
339328
}

core/src/test/java/org/bouncycastle/crypto/test/AsconTest.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ public void performTest()
7070
testExceptionsEngine_ascon80pq();
7171

7272
testExceptionsXof_AsconXof128();
73-
testExceptionsXof_AsconCxof128();
73+
testExceptionsXof_AsconCXof128();
7474
testExceptionsXof_AsconXof();
7575
testExceptionsXof_AsconXofA();
7676

77+
testOutputXof_AsconXof128();
78+
testOutputXof_AsconCXof128();
79+
testOutputXof_AsconXof();
80+
testOutputXof_AsconXofA();
81+
7782
testParametersDigest_AsconHash256();
7883
testParametersDigest_AsconHash();
7984
testParametersDigest_AsconHashA();
@@ -84,7 +89,7 @@ public void performTest()
8489
testParametersEngine_ascon80pq();
8590

8691
testParametersXof_AsconXof128();
87-
testParametersXof_AsconCxof128();
92+
testParametersXof_AsconCXof128();
8893
testParametersXof_AsconXof();
8994
testParametersXof_AsconXofA();
9095

@@ -331,7 +336,7 @@ public ExtendedDigest createDigest()
331336
});
332337
}
333338

334-
public void testExceptionsXof_AsconCxof128()
339+
public void testExceptionsXof_AsconCXof128()
335340
throws Exception
336341
{
337342
implTestExceptionsXof(new CreateDigest()
@@ -344,6 +349,26 @@ public ExtendedDigest createDigest()
344349
});
345350
}
346351

352+
public void testOutputXof_AsconXof()
353+
{
354+
implTestOutputXof(new AsconXof(AsconXof.AsconParameters.AsconXof));
355+
}
356+
357+
public void testOutputXof_AsconXofA()
358+
{
359+
implTestOutputXof(new AsconXof(AsconXof.AsconParameters.AsconXofA));
360+
}
361+
362+
public void testOutputXof_AsconXof128()
363+
{
364+
implTestOutputXof(new AsconXof128());
365+
}
366+
367+
public void testOutputXof_AsconCXof128()
368+
{
369+
implTestOutputXof(new AsconCXof128());
370+
}
371+
347372
public void testParametersDigest_AsconHash()
348373
throws Exception
349374
{
@@ -460,7 +485,7 @@ public ExtendedDigest createDigest()
460485
}, 32);
461486
}
462487

463-
public void testParametersXof_AsconCxof128()
488+
public void testParametersXof_AsconCXof128()
464489
throws Exception
465490
{
466491
implTestParametersDigest(new CreateDigest()
@@ -1005,6 +1030,35 @@ private void implTestExceptionsXof(CreateDigest operator)
10051030
}
10061031
}
10071032

1033+
private void implTestOutputXof(Xof ascon)
1034+
{
1035+
Random random = new Random();
1036+
1037+
byte[] expected = new byte[64];
1038+
ascon.doFinal(expected, 0, expected.length);
1039+
1040+
byte[] output = new byte[64];
1041+
for (int i = 0; i < 64; ++i)
1042+
{
1043+
random.nextBytes(output);
1044+
1045+
int pos = 0;
1046+
while (pos <= output.length - 16)
1047+
{
1048+
int len = random.nextInt(17);
1049+
ascon.doOutput(output, pos, len);
1050+
pos += len;
1051+
}
1052+
1053+
ascon.doFinal(output, pos, output.length - pos);
1054+
1055+
if (!areEqual(expected, output))
1056+
{
1057+
fail("");
1058+
}
1059+
}
1060+
}
1061+
10081062
private void implTestParametersDigest(CreateDigest operator, int digestSize)
10091063
{
10101064
ExtendedDigest ascon = operator.createDigest();

core/src/test/java/org/bouncycastle/crypto/test/Grain128AEADTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,8 @@ static void isEqualTo(
228228
}
229229
}
230230

231-
// public static void main(String[] args)
232-
// {
233-
// runTest(new AsconTest());
234-
// runTest(new ElephantTest());
235-
// runTest(new GiftCofbTest());
236-
// runTest(new Grain128AEADTest());
237-
// runTest(new ISAPTest());
238-
// runTest(new PhotonBeetleTest());
239-
// runTest(new RomulusTest());
240-
// runTest(new SparkleTest());
241-
// runTest(new XoodyakTest());
242-
// }
231+
public static void main(String[] args)
232+
{
233+
runTest(new Grain128AEADTest());
234+
}
243235
}
244-

pkix/src/test/java/org/bouncycastle/cert/test/SampleCredentials.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ private static SampleCredentials load(String algorithm, String path, String name
5252
Reader reader = new InputStreamReader(input);
5353

5454
PemReader pemReader = new PemReader(reader);
55-
PemObject pemPub = expectPemObject(pemReader, "PRIVATE KEY");
56-
PemObject pemPriv = expectPemObject(pemReader, "PUBLIC KEY");
55+
PemObject pemPriv = expectPemObject(pemReader, "PRIVATE KEY");
56+
PemObject pemPub = expectPemObject(pemReader, "PUBLIC KEY");
5757
PemObject pemCert = expectPemObject(pemReader, "CERTIFICATE");
5858
pemReader.close();
5959

6060
KeyFactory kf = KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
6161
CertificateFactory cf = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME);
6262

63-
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(pemPub.getContent()));
64-
PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(pemPriv.getContent()));
63+
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(pemPriv.getContent()));
64+
PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(pemPub .getContent()));
6565
KeyPair keyPair = new KeyPair(publicKey, privateKey);
6666

6767
X509Certificate certificate = (X509Certificate)cf.generateCertificate(

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,14 +3522,33 @@ private static void implTestVerifySignedData(byte[] signedData, SampleCredential
35223522
{
35233523
CMSSignedData sd = new CMSSignedData(signedData);
35243524

3525-
assertTrue(sd.verifySignatures(new SignerInformationVerifierProvider()
3525+
// Verify using the certificate from the supplied credentials
3526+
SignerInformationVerifierProvider verifierProvider = new SignerInformationVerifierProvider()
35263527
{
35273528
public SignerInformationVerifier get(SignerId signerId)
35283529
throws OperatorCreationException
35293530
{
35303531
return new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(credentials.getCertificate());
35313532
}
3532-
}));
3533+
};
3534+
3535+
// External signer verification
3536+
{
3537+
SignerInformationStore signers = sd.getSignerInfos();
3538+
3539+
Iterator it = signers.getSigners().iterator();
3540+
while (it.hasNext())
3541+
{
3542+
SignerInformation signer = (SignerInformation)it.next();
3543+
3544+
SignerInformationVerifier verifier = verifierProvider.get(signer.getSID());
3545+
3546+
assertTrue(signer.verify(verifier));
3547+
}
3548+
}
3549+
3550+
// Built-in signer verification
3551+
assertTrue(sd.verifySignatures(verifierProvider));
35333552
}
35343553

35353554
private static class TestCMSSignatureAlgorithmNameGenerator

0 commit comments

Comments
 (0)