7
7
* Implementation of Chinese SM3 digest as described at
8
8
* http://tools.ietf.org/html/draft-shen-sm3-hash-00
9
9
* and at .... ( Chinese PDF )
10
- * <p>
10
+ * <p/ >
11
11
* The specification says "process a bit stream",
12
12
* but this is written to process bytes in blocks of 4,
13
13
* meaning this will process 32-bit word groups.
@@ -19,28 +19,31 @@ public class SM3Digest
19
19
extends GeneralDigest
20
20
{
21
21
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)
23
23
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)
25
25
private int [] inwords = new int [BLOCK_SIZE ];
26
- private int xOff ;
26
+ private int xOff ;
27
27
28
28
// Work-bufs used within processBlock()
29
- private int [] W = new int [68 ];
29
+ private int [] W = new int [68 ];
30
30
private int [] W1 = new int [64 ];
31
31
32
-
33
32
// Round constant T for processBlock() which is 32 bit integer rolled left up to (63 MOD 32) bit positions.
34
33
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
+ {
37
39
int t = 0x79CC4519 ;
38
- T [i ] = (t << i ) | (t >>> (32 - i ));
40
+ T [i ] = (t << i ) | (t >>> (32 - i ));
39
41
}
40
- for (int i = 16 ; i < 64 ; ++i ) {
42
+ for (int i = 16 ; i < 64 ; ++i )
43
+ {
41
44
int n = i % 32 ;
42
45
int t = 0x7A879D8A ;
43
- T [i ] = (t << n ) | (t >>> (32 - n ));
46
+ T [i ] = (t << n ) | (t >>> (32 - n ));
44
47
}
45
48
}
46
49
@@ -66,7 +69,7 @@ public SM3Digest(SM3Digest t)
66
69
67
70
private void copyIn (SM3Digest t )
68
71
{
69
- System .arraycopy (t .V , 0 , this .V , 0 , this .V .length );
72
+ System .arraycopy (t .V , 0 , this .V , 0 , this .V .length );
70
73
System .arraycopy (t .inwords , 0 , this .inwords , 0 , this .inwords .length );
71
74
xOff = t .xOff ;
72
75
}
@@ -82,7 +85,6 @@ public int getDigestSize()
82
85
}
83
86
84
87
85
-
86
88
public Memoable copy ()
87
89
{
88
90
return new SM3Digest (this );
@@ -100,24 +102,25 @@ public void reset(Memoable other)
100
102
/**
101
103
* reset the chaining variables
102
104
*/
103
- public void reset () {
105
+ public void reset ()
106
+ {
104
107
super .reset ();
105
108
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 ;
114
117
115
- this .xOff = 0 ;
118
+ this .xOff = 0 ;
116
119
}
117
120
118
121
119
- public int doFinal ( byte [] out ,
120
- int outOff )
122
+ public int doFinal (byte [] out ,
123
+ int outOff )
121
124
{
122
125
finish ();
123
126
@@ -136,35 +139,38 @@ public int doFinal( byte[] out,
136
139
}
137
140
138
141
139
- protected void processWord ( byte [] in ,
140
- int inOff )
142
+ protected void processWord (byte [] in ,
143
+ int inOff )
141
144
{
142
145
// Note: Inlined for performance
143
146
// 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 )));
148
151
149
152
this .inwords [this .xOff ] = n ;
150
153
++this .xOff ;
151
154
152
- if (this .xOff >= 16 ) {
155
+ if (this .xOff >= 16 )
156
+ {
153
157
processBlock ();
154
158
}
155
159
}
156
-
157
- protected void processLength ( long bitLength )
160
+
161
+ protected void processLength (long bitLength )
158
162
{
159
- if (this .xOff > (BLOCK_SIZE -2 )) {
163
+ if (this .xOff > (BLOCK_SIZE - 2 ))
164
+ {
160
165
// xOff == 15 --> can't fit the 64 bit length field at tail..
161
166
this .inwords [this .xOff ] = 0 ; // fill with zero
162
167
++this .xOff ;
163
168
164
169
processBlock ();
165
170
}
166
171
// 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
+ {
168
174
this .inwords [this .xOff ] = 0 ;
169
175
++this .xOff ;
170
176
}
@@ -212,49 +218,58 @@ protected void processLength( long bitLength )
212
218
213
219
*/
214
220
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 )));
218
225
return (x ^ r9 ^ r17 );
219
226
}
220
227
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 )));
224
232
return (x ^ r15 ^ r23 );
225
233
}
226
234
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
+ {
228
237
return (x ^ y ^ z );
229
238
}
230
239
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
+ {
232
242
return ((x & y ) | (x & z ) | (y & z ));
233
243
}
234
244
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
+ {
236
247
return (x ^ y ^ z );
237
248
}
238
249
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
+ {
240
252
return ((x & y ) | ((~x ) & z ));
241
253
}
242
254
243
255
244
256
protected void processBlock ()
245
257
{
246
- for (int j = 0 ; j < 16 ; ++j ) {
258
+ for (int j = 0 ; j < 16 ; ++j )
259
+ {
247
260
this .W [j ] = this .inwords [j ];
248
261
}
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 ];
255
269
}
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 ];
258
273
}
259
274
260
275
int A = this .V [0 ];
@@ -267,37 +282,39 @@ protected void processBlock()
267
282
int H = this .V [7 ];
268
283
269
284
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 )));
272
288
int s1_ = a12 + E + T [j ];
273
- int SS1 = ((s1_ << 7 ) | (s1_ >>> (32 - 7 )));
289
+ int SS1 = ((s1_ << 7 ) | (s1_ >>> (32 - 7 )));
274
290
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 ];
277
293
D = C ;
278
- C = ((B << 9 ) | (B >>> (32 - 9 )));
294
+ C = ((B << 9 ) | (B >>> (32 - 9 )));
279
295
B = A ;
280
296
A = TT1 ;
281
297
H = G ;
282
- G = ((F << 19 ) | (F >>> (32 - 19 )));
298
+ G = ((F << 19 ) | (F >>> (32 - 19 )));
283
299
F = E ;
284
300
E = P0 (TT2 );
285
301
}
286
302
287
303
// 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 )));
290
307
int s1_ = a12 + E + T [j ];
291
- int SS1 = ((s1_ << 7 ) | (s1_ >>> (32 - 7 )));
308
+ int SS1 = ((s1_ << 7 ) | (s1_ >>> (32 - 7 )));
292
309
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 ];
295
312
D = C ;
296
- C = ((B << 9 ) | (B >>> (32 - 9 )));
313
+ C = ((B << 9 ) | (B >>> (32 - 9 )));
297
314
B = A ;
298
315
A = TT1 ;
299
316
H = G ;
300
- G = ((F << 19 ) | (F >>> (32 - 19 )));
317
+ G = ((F << 19 ) | (F >>> (32 - 19 )));
301
318
F = E ;
302
319
E = P0 (TT2 );
303
320
}
0 commit comments