Skip to content

Commit 0b86bcd

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master'
2 parents 4b505a6 + d51e11e commit 0b86bcd

File tree

6 files changed

+112
-137
lines changed

6 files changed

+112
-137
lines changed

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.bouncycastle.crypto.CryptoServicesRegistrar;
2828
import org.bouncycastle.crypto.ExtendedDigest;
2929
import org.bouncycastle.util.Arrays;
30+
import org.bouncycastle.util.Integers;
3031
import org.bouncycastle.util.Pack;
3132

3233
/**
@@ -158,7 +159,7 @@ public Blake2sDigest(Blake2sDigest digest)
158159
this.keyLength = digest.keyLength;
159160
this.key = Arrays.clone(digest.key);
160161
this.digestLength = digest.digestLength;
161-
this.internalState = Arrays.clone(internalState);
162+
this.internalState = Arrays.clone(digest.internalState);
162163
this.chainValue = Arrays.clone(digest.chainValue);
163164
this.t0 = digest.t0;
164165
this.t1 = digest.t1;
@@ -285,41 +286,36 @@ private void init(byte[] salt, byte[] personalization, byte[] key)
285286

286287
if (key != null && key.length > 0)
287288
{
288-
if (key.length > 32)
289+
keyLength = key.length;
290+
if (keyLength > 32)
289291
{
290-
throw new IllegalArgumentException(
291-
"Keys > 32 bytes are not supported");
292+
throw new IllegalArgumentException("Keys > 32 bytes are not supported");
292293
}
293-
this.key = new byte[key.length];
294-
System.arraycopy(key, 0, this.key, 0, key.length);
295-
296-
keyLength = key.length;
297-
System.arraycopy(key, 0, buffer, 0, key.length);
294+
this.key = new byte[keyLength];
295+
System.arraycopy(key, 0, this.key, 0, keyLength);
296+
System.arraycopy(key, 0, buffer, 0, keyLength);
298297
bufferPos = BLOCK_LENGTH_BYTES; // zero padding
299298
}
300299

301300
if (chainValue == null)
302301
{
303302
chainValue = new int[8];
304303

305-
chainValue[0] = blake2s_IV[0]
306-
^ (digestLength | (keyLength << 8) | ((fanout << 16) | (depth << 24)));
304+
chainValue[0] = blake2s_IV[0] ^ (digestLength | (keyLength << 8) | ((fanout << 16) | (depth << 24)));
307305
chainValue[1] = blake2s_IV[1] ^ leafLength;
308306

309307
int nofHi = (int)(nodeOffset >> 32);
310308
int nofLo = (int)nodeOffset;
311309
chainValue[2] = blake2s_IV[2] ^ nofLo;
312-
chainValue[3] = blake2s_IV[3] ^ (nofHi |
313-
(nodeDepth << 16) | (innerHashLength << 24));
310+
chainValue[3] = blake2s_IV[3] ^ (nofHi | (nodeDepth << 16) | (innerHashLength << 24));
314311

315312
chainValue[4] = blake2s_IV[4];
316313
chainValue[5] = blake2s_IV[5];
317314
if (salt != null)
318315
{
319316
if (salt.length != 8)
320317
{
321-
throw new IllegalArgumentException(
322-
"Salt length must be exactly 8 bytes");
318+
throw new IllegalArgumentException("Salt length must be exactly 8 bytes");
323319
}
324320
this.salt = new byte[8];
325321
System.arraycopy(salt, 0, this.salt, 0, salt.length);
@@ -334,12 +330,10 @@ private void init(byte[] salt, byte[] personalization, byte[] key)
334330
{
335331
if (personalization.length != 8)
336332
{
337-
throw new IllegalArgumentException(
338-
"Personalization length must be exactly 8 bytes");
333+
throw new IllegalArgumentException("Personalization length must be exactly 8 bytes");
339334
}
340335
this.personalization = new byte[8];
341-
System.arraycopy(personalization, 0, this.personalization, 0,
342-
personalization.length);
336+
System.arraycopy(personalization, 0, this.personalization, 0, personalization.length);
343337

344338
chainValue[6] ^= Pack.littleEndianToInt(personalization, 0);
345339
chainValue[7] ^= Pack.littleEndianToInt(personalization, 4);
@@ -542,18 +536,13 @@ private void compress(byte[] message, int messagePos)
542536
private void G(int m1, int m2, int posA, int posB, int posC, int posD)
543537
{
544538
internalState[posA] = internalState[posA] + internalState[posB] + m1;
545-
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 16);
539+
internalState[posD] = Integers.rotateRight(internalState[posD] ^ internalState[posA], 16);
546540
internalState[posC] = internalState[posC] + internalState[posD];
547-
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 12);
541+
internalState[posB] = Integers.rotateRight(internalState[posB] ^ internalState[posC], 12);
548542
internalState[posA] = internalState[posA] + internalState[posB] + m2;
549-
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 8);
543+
internalState[posD] = Integers.rotateRight(internalState[posD] ^ internalState[posA], 8);
550544
internalState[posC] = internalState[posC] + internalState[posD];
551-
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 7);
552-
}
553-
554-
private int rotr32(int x, int rot)
555-
{
556-
return x >>> rot | (x << (32 - rot));
545+
internalState[posB] = Integers.rotateRight(internalState[posB] ^ internalState[posC], 7);
557546
}
558547

559548
/**

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import org.bouncycastle.crypto.CryptoServicePurpose;
12-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
1312
import org.bouncycastle.crypto.Xof;
1413
import org.bouncycastle.util.Arrays;
1514

@@ -87,7 +86,7 @@ public class Blake2xsDigest
8786
*/
8887
public Blake2xsDigest()
8988
{
90-
this(Blake2xsDigest.UNKNOWN_DIGEST_LENGTH, CryptoServicePurpose.ANY); //TODO: change this?
89+
this(UNKNOWN_DIGEST_LENGTH, CryptoServicePurpose.ANY); //TODO: change this?
9190
}
9291

9392
/**
@@ -125,7 +124,7 @@ public Blake2xsDigest(int digestBytes, byte[] key)
125124
*/
126125
public Blake2xsDigest(int digestBytes, byte[] key, byte[] salt, byte[] personalization, CryptoServicePurpose purpose)
127126
{
128-
if (digestBytes < 1 || digestBytes > Blake2xsDigest.UNKNOWN_DIGEST_LENGTH)
127+
if (digestBytes < 1 || digestBytes > UNKNOWN_DIGEST_LENGTH)
129128
{
130129
throw new IllegalArgumentException(
131130
"BLAKE2xs digest length must be between 1 and 2^16-1");
@@ -134,7 +133,7 @@ public Blake2xsDigest(int digestBytes, byte[] key, byte[] salt, byte[] personali
134133
digestLength = digestBytes;
135134
nodeOffset = computeNodeOffset();
136135
this.purpose = purpose;
137-
hash = new Blake2sDigest(Blake2xsDigest.DIGEST_LENGTH, key, salt, personalization, nodeOffset, purpose);
136+
hash = new Blake2sDigest(DIGEST_LENGTH, key, salt, personalization, nodeOffset, purpose);
138137
}
139138

140139
public Blake2xsDigest(Blake2xsDigest digest)
@@ -189,7 +188,7 @@ public int getByteLength()
189188
*/
190189
public long getUnknownMaxLength()
191190
{
192-
return Blake2xsDigest.MAX_NUMBER_BLOCKS * Blake2xsDigest.DIGEST_LENGTH;
191+
return MAX_NUMBER_BLOCKS * DIGEST_LENGTH;
193192
}
194193

195194
/**
@@ -223,7 +222,7 @@ public void reset()
223222
hash.reset();
224223

225224
h0 = null;
226-
bufPos = Blake2xsDigest.DIGEST_LENGTH;
225+
bufPos = DIGEST_LENGTH;
227226
digestPos = 0;
228227
blockPos = 0;
229228
nodeOffset = computeNodeOffset();
@@ -238,7 +237,7 @@ public void reset()
238237
*/
239238
public int doFinal(byte[] out, int outOffset)
240239
{
241-
return doFinal(out, outOffset, out.length);
240+
return doFinal(out, outOffset, digestLength);
242241
}
243242

244243
/**
@@ -275,7 +274,7 @@ public int doOutput(byte[] out, int outOff, int outLen)
275274
hash.doFinal(h0, 0);
276275
}
277276

278-
if (digestLength != Blake2xsDigest.UNKNOWN_DIGEST_LENGTH)
277+
if (digestLength != UNKNOWN_DIGEST_LENGTH)
279278
{
280279
if (digestPos + outLen > digestLength)
281280
{
@@ -291,9 +290,9 @@ else if (blockPos << 5 >= getUnknownMaxLength())
291290

292291
for (int i = 0; i < outLen; i++)
293292
{
294-
if (bufPos >= Blake2xsDigest.DIGEST_LENGTH)
293+
if (bufPos >= DIGEST_LENGTH)
295294
{
296-
Blake2sDigest h = new Blake2sDigest(computeStepLength(), Blake2xsDigest.DIGEST_LENGTH, nodeOffset);
295+
Blake2sDigest h = new Blake2sDigest(computeStepLength(), DIGEST_LENGTH, nodeOffset);
297296
h.update(h0, 0, h0.length);
298297

299298
Arrays.fill(buf, (byte)0);
@@ -302,7 +301,7 @@ else if (blockPos << 5 >= getUnknownMaxLength())
302301
nodeOffset++;
303302
blockPos++;
304303
}
305-
out[i] = buf[bufPos];
304+
out[outOff + i] = buf[bufPos];
306305
bufPos++;
307306
digestPos++;
308307
}
@@ -314,12 +313,12 @@ else if (blockPos << 5 >= getUnknownMaxLength())
314313
// always the maximum.
315314
private int computeStepLength()
316315
{
317-
if (digestLength == Blake2xsDigest.UNKNOWN_DIGEST_LENGTH)
316+
if (digestLength == UNKNOWN_DIGEST_LENGTH)
318317
{
319-
return Blake2xsDigest.DIGEST_LENGTH;
318+
return DIGEST_LENGTH;
320319
}
321320

322-
return Math.min(Blake2xsDigest.DIGEST_LENGTH, digestLength - digestPos);
321+
return Math.min(DIGEST_LENGTH, digestLength - digestPos);
323322
}
324323

325324
private long computeNodeOffset()

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.bouncycastle.crypto.test;
22

3+
import java.util.Arrays;
4+
import java.util.Random;
5+
36
import org.bouncycastle.crypto.digests.Blake2xsDigest;
47
import org.bouncycastle.util.encoders.Hex;
58
import org.bouncycastle.util.test.SimpleTest;
@@ -2579,38 +2582,44 @@ public String getName()
25792582

25802583
private void testBlake2xsTestVectors()
25812584
{
2585+
Random random = new Random();
2586+
25822587
for (int i = 0; i != Blake2xsDigestTest.xofTestVectors.length; i++)
25832588
{
25842589
String[] vector = Blake2xsDigestTest.xofTestVectors[i];
25852590
byte[] input = Hex.decode(vector[0]);
25862591
byte[] key = Hex.decode(vector[1]);
2592+
byte[] expected = Hex.decode(vector[2]);
25872593

2588-
Blake2xsDigest h = new Blake2xsDigest(vector[2].length() / 2, key);
2594+
int digestSize = expected.length;
2595+
Blake2xsDigest h = new Blake2xsDigest(digestSize, key);
25892596
h.update(input, 0, input.length);
25902597

2591-
byte[] out = new byte[vector[2].length() / 2];
2592-
h.doFinal(out, 0);
2593-
if (!areEqual(out, Hex.decode(vector[2])))
2598+
byte[] out = new byte[16 + digestSize];
2599+
int outOff = 1 + random.nextInt(16);
2600+
h.doFinal(out, outOff);
2601+
if (!areEqual(out, outOff, outOff + digestSize, expected, 0, digestSize))
25942602
{
2595-
fail("BLAKE2xs mismatch on test vector ", vector[2], Hex.toHexString(out));
2603+
fail("BLAKE2xs mismatch on test vector ", vector[2], Hex.toHexString(out, outOff, digestSize));
25962604
}
25972605

2598-
out = new byte[vector[2].length() / 2];
2606+
Arrays.fill(out, (byte)0);
2607+
outOff = 1 + random.nextInt(16);
2608+
25992609
h.update(input, 0, input.length);
26002610
Blake2xsDigest clone = new Blake2xsDigest(h);
26012611

2602-
h.doOutput(out, 0, out.length);
2603-
if (!areEqual(out, Hex.decode(vector[2])))
2612+
h.doOutput(out, outOff, digestSize);
2613+
if (!areEqual(out, outOff, outOff + digestSize, expected, 0, digestSize))
26042614
{
2605-
fail("BLAKE2xs mismatch on test vector after a reset", vector[2], Hex.toHexString(out));
2615+
fail("BLAKE2xs mismatch on test vector after a reset", vector[2], Hex.toHexString(out, outOff, digestSize));
26062616
}
26072617

2608-
byte[] outClone = new byte[out.length];
2618+
byte[] outClone = new byte[digestSize];
26092619
clone.doFinal(outClone, 0, outClone.length);
2610-
if (!areEqual(out, outClone))
2620+
if (!areEqual(outClone, expected))
26112621
{
2612-
fail("BLAKE2xs mismatch on test vector against a clone",
2613-
vector[2], Hex.toHexString(outClone));
2622+
fail("BLAKE2xs mismatch on test vector against a clone", vector[2], Hex.toHexString(outClone));
26142623
}
26152624
}
26162625
}

tls/src/test/java/org/bouncycastle/tls/test/MockRawKeysTlsClient.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.security.SecureRandom;
55
import java.util.Vector;
66

7-
import junit.framework.TestCase;
87
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
98
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
109
import org.bouncycastle.tls.Certificate;
@@ -23,16 +22,16 @@
2322
import org.bouncycastle.tls.TlsServerCertificate;
2423
import org.bouncycastle.tls.TlsUtils;
2524
import org.bouncycastle.tls.crypto.TlsCertificate;
26-
import org.bouncycastle.tls.crypto.TlsCrypto;
2725
import org.bouncycastle.tls.crypto.TlsCryptoParameters;
2826
import org.bouncycastle.tls.crypto.impl.bc.BcDefaultTlsCredentialedSigner;
2927
import org.bouncycastle.tls.crypto.impl.bc.BcTlsCrypto;
3028
import org.bouncycastle.tls.crypto.impl.bc.BcTlsRawKeyCertificate;
3129

30+
import junit.framework.TestCase;
31+
3232
class MockRawKeysTlsClient
3333
extends DefaultTlsClient
3434
{
35-
3635
private short serverCertType;
3736
private short clientCertType;
3837
private short[] offerServerCertTypes;
@@ -46,6 +45,7 @@ class MockRawKeysTlsClient
4645
throws Exception
4746
{
4847
super(new BcTlsCrypto(new SecureRandom()));
48+
4949
this.serverCertType = serverCertType;
5050
this.clientCertType = clientCertType;
5151
this.offerServerCertTypes = offerServerCertTypes;
@@ -61,9 +61,9 @@ protected ProtocolVersion[] getSupportedVersions()
6161

6262
protected int[] getSupportedCipherSuites()
6363
{
64-
return ProtocolVersion.TLSv13.equals(tlsVersion) ?
65-
new int[] {CipherSuite.TLS_AES_128_GCM_SHA256} :
66-
new int[] {CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
64+
return TlsUtils.isTLSv13(tlsVersion)
65+
? new int[]{ CipherSuite.TLS_AES_128_GCM_SHA256 }
66+
: new int[]{ CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 };
6767
}
6868

6969
protected short[] getAllowedClientCertificateTypes()
@@ -113,19 +113,13 @@ public TlsCredentials getClientCredentials(CertificateRequest certificateRequest
113113
SignatureAlgorithm.ed25519, "x509-client-ed25519.pem", "x509-client-key-ed25519.pem");
114114
break;
115115
case CertificateType.RawPublicKey:
116-
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate(
117-
(BcTlsCrypto)getCrypto(),
118-
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
119-
Certificate cert = new Certificate(
120-
CertificateType.RawPublicKey,
121-
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
122-
new CertificateEntry[] {new CertificateEntry(rawKeyCert, null)});
123-
credentials = new BcDefaultTlsCredentialedSigner(
124-
new TlsCryptoParameters(context),
125-
(BcTlsCrypto)getCrypto(),
126-
privateKey,
127-
cert,
128-
SignatureAndHashAlgorithm.ed25519);
116+
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate((BcTlsCrypto)getCrypto(),
117+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
118+
Certificate cert = new Certificate(CertificateType.RawPublicKey,
119+
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
120+
new CertificateEntry[]{ new CertificateEntry(rawKeyCert, null) });
121+
credentials = new BcDefaultTlsCredentialedSigner(new TlsCryptoParameters(context),
122+
(BcTlsCrypto)getCrypto(), privateKey, cert, SignatureAndHashAlgorithm.ed25519);
129123
break;
130124
default:
131125
throw new IllegalArgumentException("Only supports X509 and raw keys");
@@ -136,9 +130,4 @@ public TlsCredentials getClientCredentials(CertificateRequest certificateRequest
136130
}
137131
};
138132
}
139-
140-
public TlsCrypto getCrypto()
141-
{
142-
return super.getCrypto();
143-
}
144133
}

0 commit comments

Comments
 (0)