Skip to content

Commit 3eb62a0

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents fb70a4a + 610587a commit 3eb62a0

9 files changed

Lines changed: 568 additions & 231 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/digests/LongDigest.java‎

Lines changed: 134 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class LongDigest
2424

2525
protected long H1, H2, H3, H4, H5, H6, H7, H8;
2626

27-
private long[] W = new long[80];
27+
private long[] W = new long[16];
2828
private int wOff;
2929

3030
/**
@@ -271,68 +271,104 @@ protected void processBlock()
271271
{
272272
adjustByteCounts();
273273

274-
//
275-
// expand 16 word block into 80 word blocks.
276-
//
277-
for (int t = 16; t <= 79; t++)
278-
{
279-
W[t] = Sigma1(W[t - 2]) + W[t - 7] + Sigma0(W[t - 15]) + W[t - 16];
280-
}
274+
long[] W = this.W;
281275

282-
//
283-
// set up working variables.
284-
//
285-
long a = H1;
286-
long b = H2;
287-
long c = H3;
288-
long d = H4;
289-
long e = H5;
290-
long f = H6;
291-
long g = H7;
292-
long h = H8;
293-
294-
int t = 0;
295-
for(int i = 0; i < 10; i ++)
276+
// The message schedule lives in 16 rotating locals instead of a 64/80-word array: no bounds
277+
// checks or memory traffic for the expansion, and the array only ever holds the 16 input words.
278+
long w00 = W[0], w01 = W[1], w02 = W[2], w03 = W[3];
279+
long w04 = W[4], w05 = W[5], w06 = W[6], w07 = W[7];
280+
long w08 = W[8], w09 = W[9], w10 = W[10], w11 = W[11];
281+
long w12 = W[12], w13 = W[13], w14 = W[14], w15 = W[15];
282+
283+
long a = H1, b = H2, c = H3, d = H4, e = H5, f = H6, g = H7, h = H8;
284+
285+
for (int t = 0; ; t += 16)
296286
{
297-
// t = 8 * i
298-
h += Sum1(e) + Ch(e, f, g) + K[t] + W[t++];
299-
d += h;
300-
h += Sum0(a) + Maj(a, b, c);
301-
302-
// t = 8 * i + 1
303-
g += Sum1(d) + Ch(d, e, f) + K[t] + W[t++];
304-
c += g;
305-
g += Sum0(h) + Maj(h, a, b);
306-
307-
// t = 8 * i + 2
308-
f += Sum1(c) + Ch(c, d, e) + K[t] + W[t++];
309-
b += f;
310-
f += Sum0(g) + Maj(g, h, a);
311-
312-
// t = 8 * i + 3
313-
e += Sum1(b) + Ch(b, c, d) + K[t] + W[t++];
314-
a += e;
315-
e += Sum0(f) + Maj(f, g, h);
316-
317-
// t = 8 * i + 4
318-
d += Sum1(a) + Ch(a, b, c) + K[t] + W[t++];
319-
h += d;
320-
d += Sum0(e) + Maj(e, f, g);
321-
322-
// t = 8 * i + 5
323-
c += Sum1(h) + Ch(h, a, b) + K[t] + W[t++];
324-
g += c;
325-
c += Sum0(d) + Maj(d, e, f);
326-
327-
// t = 8 * i + 6
328-
b += Sum1(g) + Ch(g, h, a) + K[t] + W[t++];
329-
f += b;
330-
b += Sum0(c) + Maj(c, d, e);
331-
332-
// t = 8 * i + 7
333-
a += Sum1(f) + Ch(f, g, h) + K[t] + W[t++];
334-
e += a;
335-
a += Sum0(b) + Maj(b, c, d);
287+
h += Sum1(e) + Ch(e, f, g) + K[t] + w00;
288+
d += h;
289+
h += Sum0(a) + Maj(a, b, c);
290+
291+
g += Sum1(d) + Ch(d, e, f) + K[t + 1] + w01;
292+
c += g;
293+
g += Sum0(h) + Maj(h, a, b);
294+
295+
f += Sum1(c) + Ch(c, d, e) + K[t + 2] + w02;
296+
b += f;
297+
f += Sum0(g) + Maj(g, h, a);
298+
299+
e += Sum1(b) + Ch(b, c, d) + K[t + 3] + w03;
300+
a += e;
301+
e += Sum0(f) + Maj(f, g, h);
302+
303+
d += Sum1(a) + Ch(a, b, c) + K[t + 4] + w04;
304+
h += d;
305+
d += Sum0(e) + Maj(e, f, g);
306+
307+
c += Sum1(h) + Ch(h, a, b) + K[t + 5] + w05;
308+
g += c;
309+
c += Sum0(d) + Maj(d, e, f);
310+
311+
b += Sum1(g) + Ch(g, h, a) + K[t + 6] + w06;
312+
f += b;
313+
b += Sum0(c) + Maj(c, d, e);
314+
315+
a += Sum1(f) + Ch(f, g, h) + K[t + 7] + w07;
316+
e += a;
317+
a += Sum0(b) + Maj(b, c, d);
318+
319+
h += Sum1(e) + Ch(e, f, g) + K[t + 8] + w08;
320+
d += h;
321+
h += Sum0(a) + Maj(a, b, c);
322+
323+
g += Sum1(d) + Ch(d, e, f) + K[t + 9] + w09;
324+
c += g;
325+
g += Sum0(h) + Maj(h, a, b);
326+
327+
f += Sum1(c) + Ch(c, d, e) + K[t + 10] + w10;
328+
b += f;
329+
f += Sum0(g) + Maj(g, h, a);
330+
331+
e += Sum1(b) + Ch(b, c, d) + K[t + 11] + w11;
332+
a += e;
333+
e += Sum0(f) + Maj(f, g, h);
334+
335+
d += Sum1(a) + Ch(a, b, c) + K[t + 12] + w12;
336+
h += d;
337+
d += Sum0(e) + Maj(e, f, g);
338+
339+
c += Sum1(h) + Ch(h, a, b) + K[t + 13] + w13;
340+
g += c;
341+
c += Sum0(d) + Maj(d, e, f);
342+
343+
b += Sum1(g) + Ch(g, h, a) + K[t + 14] + w14;
344+
f += b;
345+
b += Sum0(c) + Maj(c, d, e);
346+
347+
a += Sum1(f) + Ch(f, g, h) + K[t + 15] + w15;
348+
e += a;
349+
a += Sum0(b) + Maj(b, c, d);
350+
351+
if (t == 64)
352+
break;
353+
354+
// W[t+16+j] = sigma1(W[t+14+j]) + W[t+9+j] + sigma0(W[t+1+j]) + W[t+j]; wrapped indices refer to
355+
// words already updated in this pass, which is exactly the required schedule.
356+
w00 += Sigma1(w14) + w09 + Sigma0(w01);
357+
w01 += Sigma1(w15) + w10 + Sigma0(w02);
358+
w02 += Sigma1(w00) + w11 + Sigma0(w03);
359+
w03 += Sigma1(w01) + w12 + Sigma0(w04);
360+
w04 += Sigma1(w02) + w13 + Sigma0(w05);
361+
w05 += Sigma1(w03) + w14 + Sigma0(w06);
362+
w06 += Sigma1(w04) + w15 + Sigma0(w07);
363+
w07 += Sigma1(w05) + w00 + Sigma0(w08);
364+
w08 += Sigma1(w06) + w01 + Sigma0(w09);
365+
w09 += Sigma1(w07) + w02 + Sigma0(w10);
366+
w10 += Sigma1(w08) + w03 + Sigma0(w11);
367+
w11 += Sigma1(w09) + w04 + Sigma0(w12);
368+
w12 += Sigma1(w10) + w05 + Sigma0(w13);
369+
w13 += Sigma1(w11) + w06 + Sigma0(w14);
370+
w14 += Sigma1(w12) + w07 + Sigma0(w15);
371+
w15 += Sigma1(w13) + w08 + Sigma0(w00);
336372
}
337373

338374
H1 += a;
@@ -354,72 +390,63 @@ protected void processBlock()
354390
}
355391
}
356392

357-
/* SHA-384 and SHA-512 functions (as for SHA-256 but for longs) */
358-
private long Ch(
359-
long x,
360-
long y,
361-
long z)
393+
private static long Ch(long x, long y, long z)
362394
{
363-
return ((x & y) ^ ((~x) & z));
395+
//return (x & y) ^ (z & ~x);
396+
return z ^ (x & (y ^ z));
364397
}
365398

366-
private long Maj(
367-
long x,
368-
long y,
369-
long z)
399+
private static long Maj(long x, long y, long z)
370400
{
371-
return ((x & y) ^ (x & z) ^ (y & z));
401+
//return ((x & y) ^ (x & z) ^ (y & z));
402+
return (x & y) | (z & (x ^ y));
372403
}
373404

374-
private long Sum0(
375-
long x)
405+
private static long Sum0(long x)
376406
{
377-
return ((x << 36)|(x >>> 28)) ^ ((x << 30)|(x >>> 34)) ^ ((x << 25)|(x >>> 39));
407+
return (x << 36 | x >>> 28) ^ (x << 30 | x >>> 34) ^ (x << 25 | x >>> 39);
378408
}
379409

380-
private long Sum1(
381-
long x)
410+
private static long Sum1(long x)
382411
{
383-
return ((x << 50)|(x >>> 14)) ^ ((x << 46)|(x >>> 18)) ^ ((x << 23)|(x >>> 41));
412+
return (x << 50 | x >>> 14) ^ (x << 46 | x >>> 18) ^ (x << 23 | x >>> 41);
384413
}
385414

386-
private long Sigma0(
387-
long x)
415+
private static long Sigma0(long x)
388416
{
389-
return ((x << 63)|(x >>> 1)) ^ ((x << 56)|(x >>> 8)) ^ (x >>> 7);
417+
return (x << 63 | x >>> 1) ^ (x << 56 | x >>> 8) ^ (x >>> 7);
390418
}
391419

392-
private long Sigma1(
393-
long x)
420+
private static long Sigma1(long x)
394421
{
395-
return ((x << 45)|(x >>> 19)) ^ ((x << 3)|(x >>> 61)) ^ (x >>> 6);
422+
return (x << 45 | x >>> 19) ^ (x << 3 | x >>> 61) ^ (x >>> 6);
396423
}
397424

398-
/* SHA-384 and SHA-512 Constants
399-
* (represent the first 64 bits of the fractional parts of the
400-
* cube roots of the first sixty-four prime numbers)
425+
/**
426+
* SHA-512 Constants (represent the first 64 bits of the fractional parts of the cube roots of the first sixty-four
427+
* prime numbers)
401428
*/
402429
static final long K[] = {
403-
0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
404-
0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
405-
0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
406-
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L,
407-
0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
408-
0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
409-
0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
410-
0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L,
411-
0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
412-
0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL,
413-
0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
414-
0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
415-
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
416-
0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
417-
0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
418-
0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
419-
0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
420-
0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
421-
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
422-
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L
430+
0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
431+
0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
432+
0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
433+
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L,
434+
0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
435+
0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
436+
0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
437+
0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L,
438+
0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
439+
0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL,
440+
0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
441+
0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
442+
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
443+
0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
444+
0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
445+
0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
446+
0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
447+
0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
448+
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
449+
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L
423450
};
424451

425452
protected abstract CryptoServiceProperties cryptoServiceProperties();

0 commit comments

Comments
 (0)