Skip to content

Commit 3e434fb

Browse files
committed
reviewed SM3 digest
1 parent 48cb393 commit 3e434fb

File tree

4 files changed

+94
-76
lines changed

4 files changed

+94
-76
lines changed

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

+86-69
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Implementation of Chinese SM3 digest as described at
88
* http://tools.ietf.org/html/draft-shen-sm3-hash-00
99
* and at .... ( Chinese PDF )
10-
* <p>
10+
* <p/>
1111
* The specification says "process a bit stream",
1212
* but this is written to process bytes in blocks of 4,
1313
* meaning this will process 32-bit word groups.
@@ -19,28 +19,31 @@ public class SM3Digest
1919
extends GeneralDigest
2020
{
2121
private static final int DIGEST_LENGTH = 32; // bytes
22-
private static final int BLOCK_SIZE = 64/4; // of 32 bit ints (16 ints)
22+
private static final int BLOCK_SIZE = 64 / 4; // of 32 bit ints (16 ints)
2323

24-
private int[] V = new int[DIGEST_LENGTH/4]; // in 32 bit ints (8 ints)
24+
private int[] V = new int[DIGEST_LENGTH / 4]; // in 32 bit ints (8 ints)
2525
private int[] inwords = new int[BLOCK_SIZE];
26-
private int xOff;
26+
private int xOff;
2727

2828
// Work-bufs used within processBlock()
29-
private int[] W = new int[68];
29+
private int[] W = new int[68];
3030
private int[] W1 = new int[64];
3131

32-
3332
// Round constant T for processBlock() which is 32 bit integer rolled left up to (63 MOD 32) bit positions.
3433
private static final int[] T = new int[64];
35-
static {
36-
for (int i = 0; i < 16; ++i) {
34+
35+
static
36+
{
37+
for (int i = 0; i < 16; ++i)
38+
{
3739
int t = 0x79CC4519;
38-
T[i] = (t << i) | (t >>> (32-i));
40+
T[i] = (t << i) | (t >>> (32 - i));
3941
}
40-
for (int i = 16; i < 64; ++i) {
42+
for (int i = 16; i < 64; ++i)
43+
{
4144
int n = i % 32;
4245
int t = 0x7A879D8A;
43-
T[i] = (t << n) | (t >>> (32-n));
46+
T[i] = (t << n) | (t >>> (32 - n));
4447
}
4548
}
4649

@@ -66,7 +69,7 @@ public SM3Digest(SM3Digest t)
6669

6770
private void copyIn(SM3Digest t)
6871
{
69-
System.arraycopy(t.V, 0, this.V, 0, this.V.length);
72+
System.arraycopy(t.V, 0, this.V, 0, this.V.length);
7073
System.arraycopy(t.inwords, 0, this.inwords, 0, this.inwords.length);
7174
xOff = t.xOff;
7275
}
@@ -82,7 +85,6 @@ public int getDigestSize()
8285
}
8386

8487

85-
8688
public Memoable copy()
8789
{
8890
return new SM3Digest(this);
@@ -100,24 +102,25 @@ public void reset(Memoable other)
100102
/**
101103
* reset the chaining variables
102104
*/
103-
public void reset() {
105+
public void reset()
106+
{
104107
super.reset();
105108

106-
this.V[0] = 0x7380166F;
107-
this.V[1] = 0x4914B2B9;
108-
this.V[2] = 0x172442D7;
109-
this.V[3] = 0xDA8A0600;
110-
this.V[4] = 0xA96F30BC;
111-
this.V[5] = 0x163138AA;
112-
this.V[6] = 0xE38DEE4D;
113-
this.V[7] = 0xB0FB0E4E;
109+
this.V[0] = 0x7380166F;
110+
this.V[1] = 0x4914B2B9;
111+
this.V[2] = 0x172442D7;
112+
this.V[3] = 0xDA8A0600;
113+
this.V[4] = 0xA96F30BC;
114+
this.V[5] = 0x163138AA;
115+
this.V[6] = 0xE38DEE4D;
116+
this.V[7] = 0xB0FB0E4E;
114117

115-
this.xOff = 0;
118+
this.xOff = 0;
116119
}
117120

118121

119-
public int doFinal( byte[] out,
120-
int outOff )
122+
public int doFinal(byte[] out,
123+
int outOff)
121124
{
122125
finish();
123126

@@ -136,35 +139,38 @@ public int doFinal( byte[] out,
136139
}
137140

138141

139-
protected void processWord( byte[] in,
140-
int inOff )
142+
protected void processWord(byte[] in,
143+
int inOff)
141144
{
142145
// Note: Inlined for performance
143146
// this.inwords[xOff] = Pack.bigEndianToInt(in, inOff);
144-
int n = (((in[ inOff] & 0xff) << 24) |
145-
((in[++inOff] & 0xff) << 16) |
146-
((in[++inOff] & 0xff) << 8) |
147-
((in[++inOff] & 0xff) ));
147+
int n = (((in[inOff] & 0xff) << 24) |
148+
((in[++inOff] & 0xff) << 16) |
149+
((in[++inOff] & 0xff) << 8) |
150+
((in[++inOff] & 0xff)));
148151

149152
this.inwords[this.xOff] = n;
150153
++this.xOff;
151154

152-
if (this.xOff >= 16) {
155+
if (this.xOff >= 16)
156+
{
153157
processBlock();
154158
}
155159
}
156-
157-
protected void processLength( long bitLength )
160+
161+
protected void processLength(long bitLength)
158162
{
159-
if (this.xOff > (BLOCK_SIZE-2)) {
163+
if (this.xOff > (BLOCK_SIZE - 2))
164+
{
160165
// xOff == 15 --> can't fit the 64 bit length field at tail..
161166
this.inwords[this.xOff] = 0; // fill with zero
162167
++this.xOff;
163168

164169
processBlock();
165170
}
166171
// Fill with zero words, until reach 2nd to last slot
167-
while (this.xOff < (BLOCK_SIZE-2)) {
172+
while (this.xOff < (BLOCK_SIZE - 2))
173+
{
168174
this.inwords[this.xOff] = 0;
169175
++this.xOff;
170176
}
@@ -212,49 +218,58 @@ protected void processLength( long bitLength )
212218
213219
*/
214220

215-
private int P0(final int x) {
216-
final int r9 = ((x << 9) | (x >>> (32- 9)));
217-
final int r17 = ((x << 17) | (x >>> (32-17)));
221+
private int P0(final int x)
222+
{
223+
final int r9 = ((x << 9) | (x >>> (32 - 9)));
224+
final int r17 = ((x << 17) | (x >>> (32 - 17)));
218225
return (x ^ r9 ^ r17);
219226
}
220227

221-
private int P1(final int x) {
222-
final int r15 = ((x << 15) | (x >>> (32-15)));
223-
final int r23 = ((x << 23) | (x >>> (32-23)));
228+
private int P1(final int x)
229+
{
230+
final int r15 = ((x << 15) | (x >>> (32 - 15)));
231+
final int r23 = ((x << 23) | (x >>> (32 - 23)));
224232
return (x ^ r15 ^ r23);
225233
}
226234

227-
private int FF0(final int x, final int y, final int z) {
235+
private int FF0(final int x, final int y, final int z)
236+
{
228237
return (x ^ y ^ z);
229238
}
230239

231-
private int FF1(final int x, final int y, final int z) {
240+
private int FF1(final int x, final int y, final int z)
241+
{
232242
return ((x & y) | (x & z) | (y & z));
233243
}
234244

235-
private int GG0(final int x, final int y, final int z) {
245+
private int GG0(final int x, final int y, final int z)
246+
{
236247
return (x ^ y ^ z);
237248
}
238249

239-
private int GG1(final int x, final int y, final int z) {
250+
private int GG1(final int x, final int y, final int z)
251+
{
240252
return ((x & y) | ((~x) & z));
241253
}
242254

243255

244256
protected void processBlock()
245257
{
246-
for (int j = 0; j < 16; ++j) {
258+
for (int j = 0; j < 16; ++j)
259+
{
247260
this.W[j] = this.inwords[j];
248261
}
249-
for (int j = 16; j < 68; ++j) {
250-
int wj3 = this.W[j-3];
251-
int r15 = ((wj3 << 15) | (wj3 >>> (32-15)));
252-
int wj13 = this.W[j-13];
253-
int r7 = ((wj13 << 7) | (wj13 >>> (32-7)));
254-
this.W[j] = P1( this.W[j-16] ^ this.W[j-9] ^ r15 ) ^ r7 ^ this.W[j-6];
262+
for (int j = 16; j < 68; ++j)
263+
{
264+
int wj3 = this.W[j - 3];
265+
int r15 = ((wj3 << 15) | (wj3 >>> (32 - 15)));
266+
int wj13 = this.W[j - 13];
267+
int r7 = ((wj13 << 7) | (wj13 >>> (32 - 7)));
268+
this.W[j] = P1(this.W[j - 16] ^ this.W[j - 9] ^ r15) ^ r7 ^ this.W[j - 6];
255269
}
256-
for (int j = 0; j < 64; ++j) {
257-
this.W1[j] = this.W[j] ^ this.W[j+4];
270+
for (int j = 0; j < 64; ++j)
271+
{
272+
this.W1[j] = this.W[j] ^ this.W[j + 4];
258273
}
259274

260275
int A = this.V[0];
@@ -267,37 +282,39 @@ protected void processBlock()
267282
int H = this.V[7];
268283

269284

270-
for (int j = 0; j < 16; ++j) {
271-
int a12 = ((A << 12) | (A >>> (32-12)));
285+
for (int j = 0; j < 16; ++j)
286+
{
287+
int a12 = ((A << 12) | (A >>> (32 - 12)));
272288
int s1_ = a12 + E + T[j];
273-
int SS1 = ((s1_ << 7) | (s1_ >>> (32-7)));
289+
int SS1 = ((s1_ << 7) | (s1_ >>> (32 - 7)));
274290
int SS2 = SS1 ^ a12;
275-
int TT1 = FF0(A,B,C) + D + SS2 + this.W1[j];
276-
int TT2 = GG0(E,F,G) + H + SS1 + this.W [j];
291+
int TT1 = FF0(A, B, C) + D + SS2 + this.W1[j];
292+
int TT2 = GG0(E, F, G) + H + SS1 + this.W[j];
277293
D = C;
278-
C = ((B << 9) | (B >>> (32-9)));
294+
C = ((B << 9) | (B >>> (32 - 9)));
279295
B = A;
280296
A = TT1;
281297
H = G;
282-
G = ((F << 19) | (F >>> (32-19)));
298+
G = ((F << 19) | (F >>> (32 - 19)));
283299
F = E;
284300
E = P0(TT2);
285301
}
286302

287303
// Different FF,GG functions on rounds 16..63
288-
for (int j = 16; j < 64; ++j) {
289-
int a12 = ((A << 12) | (A >>> (32-12)));
304+
for (int j = 16; j < 64; ++j)
305+
{
306+
int a12 = ((A << 12) | (A >>> (32 - 12)));
290307
int s1_ = a12 + E + T[j];
291-
int SS1 = ((s1_ << 7) | (s1_ >>> (32-7)));
308+
int SS1 = ((s1_ << 7) | (s1_ >>> (32 - 7)));
292309
int SS2 = SS1 ^ a12;
293-
int TT1 = FF1(A,B,C) + D + SS2 + this.W1[j];
294-
int TT2 = GG1(E,F,G) + H + SS1 + this.W [j];
310+
int TT1 = FF1(A, B, C) + D + SS2 + this.W1[j];
311+
int TT2 = GG1(E, F, G) + H + SS1 + this.W[j];
295312
D = C;
296-
C = ((B << 9) | (B >>> (32-9)));
313+
C = ((B << 9) | (B >>> (32 - 9)));
297314
B = A;
298315
A = TT1;
299316
H = G;
300-
G = ((F << 19) | (F >>> (32-19)));
317+
G = ((F << 19) | (F >>> (32 - 19)));
301318
F = E;
302319
E = P0(TT2);
303320
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SM3DigestTest
1818
"a",
1919
"abcdefghijklmnopqrstuvwxyz",
2020
};
21-
21+
2222
private static String[] digests = {
2323
// Standard test vectors
2424
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
@@ -28,7 +28,7 @@ public class SM3DigestTest
2828
"623476ac18f65a2909e43c7fec61b49c7e764a91a18ccb82f1917a29c86c5e88",
2929
"b80fe97a4da24afc277564f66a359ef440462ad28dcc6d63adb24d5c20a61595",
3030
};
31-
31+
3232
final static String sixtyFourKdigest = "97049bdc8f0736bc7300eafa9980aeb9cf00f24f7ec3a8f1f8884954d7655c1d";
3333
final static String million_a_digest = "c8aaf89429554029e231941a2acc0ad61ff2a5acd8fadd25847a3a732b3b02c3";
3434

@@ -41,7 +41,7 @@ public class SM3DigestTest
4141
public void performTest()
4242
{
4343
super.performTest();
44-
44+
4545
sixtyFourKTest(sixtyFourKdigest);
4646
millionATest(million_a_digest);
4747
}
@@ -50,8 +50,8 @@ protected Digest cloneDigest(Digest digest)
5050
{
5151
return new SM3Digest((SM3Digest)digest);
5252
}
53-
54-
public static void main( String[] args)
53+
54+
public static void main(String[] args)
5555
{
5656
runTest(new SM3DigestTest());
5757
}

prov/src/main/java/org/bouncycastle/jce/provider/BouncyCastleProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public final class BouncyCastleProvider extends Provider
9696
private static final String DIGEST_PACKAGE = "org.bouncycastle.jcajce.provider.digest.";
9797
private static final String[] DIGESTS =
9898
{
99-
"GOST3411", "MD2", "MD4", "MD5", "SHA1", "RIPEMD128", "RIPEMD160", "RIPEMD256", "RIPEMD320", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "Skein", "Tiger", "Whirlpool"
99+
"GOST3411", "MD2", "MD4", "MD5", "SHA1", "RIPEMD128", "RIPEMD160", "RIPEMD256", "RIPEMD320", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "Skein", "SM3", "Tiger", "Whirlpool"
100100
};
101101

102102
/*

prov/src/test/java/org/bouncycastle/jce/provider/test/DigestTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class DigestTest
3030
{ "RIPEMD320", "de4c01b3054f8930a79d09ae738e92301e5a17085beffdc1b8d116713e74f82fa942d64cdbc4682d" },
3131
{ "Tiger", "2AAB1484E8C158F2BFB8C5FF41B57A525129131C957B5F93" },
3232
{ "GOST3411", "b285056dbf18d7392d7677369524dd14747459ed8143997e163b2986f92fd42c" },
33-
{ "WHIRLPOOL", "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5" }
33+
{ "WHIRLPOOL", "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5" },
34+
{ "SM3", "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0" },
3435
};
3536

3637
public String getName()

0 commit comments

Comments
 (0)