@@ -24,81 +24,87 @@ import (
24
24
// and applying a noescape along the way.
25
25
// This is all to preserve compatibility with the allocation behavior of the non-openssl implementations.
26
26
27
- func shaX (ch crypto.Hash , p []byte , sum []byte ) bool {
27
+ func hashOneShot (ch crypto.Hash , p []byte , sum []byte ) bool {
28
28
return C .go_openssl_EVP_Digest (unsafe .Pointer (& * addr (p )), C .size_t (len (p )), (* C .uchar )(unsafe .Pointer (& * addr (sum ))), nil , cryptoHashToMD (ch ), nil ) != 0
29
29
}
30
30
31
+ func MD4 (p []byte ) (sum [16 ]byte ) {
32
+ if ! hashOneShot (crypto .MD4 , p , sum [:]) {
33
+ panic ("openssl: MD4 failed" )
34
+ }
35
+ return
36
+ }
37
+
38
+ func MD5 (p []byte ) (sum [16 ]byte ) {
39
+ if ! hashOneShot (crypto .MD5 , p , sum [:]) {
40
+ panic ("openssl: MD5 failed" )
41
+ }
42
+ return
43
+ }
44
+
31
45
func SHA1 (p []byte ) (sum [20 ]byte ) {
32
- if ! shaX (crypto .SHA1 , p , sum [:]) {
46
+ if ! hashOneShot (crypto .SHA1 , p , sum [:]) {
33
47
panic ("openssl: SHA1 failed" )
34
48
}
35
49
return
36
50
}
37
51
38
52
func SHA224 (p []byte ) (sum [28 ]byte ) {
39
- if ! shaX (crypto .SHA224 , p , sum [:]) {
53
+ if ! hashOneShot (crypto .SHA224 , p , sum [:]) {
40
54
panic ("openssl: SHA224 failed" )
41
55
}
42
56
return
43
57
}
44
58
45
59
func SHA256 (p []byte ) (sum [32 ]byte ) {
46
- if ! shaX (crypto .SHA256 , p , sum [:]) {
60
+ if ! hashOneShot (crypto .SHA256 , p , sum [:]) {
47
61
panic ("openssl: SHA256 failed" )
48
62
}
49
63
return
50
64
}
51
65
52
66
func SHA384 (p []byte ) (sum [48 ]byte ) {
53
- if ! shaX (crypto .SHA384 , p , sum [:]) {
67
+ if ! hashOneShot (crypto .SHA384 , p , sum [:]) {
54
68
panic ("openssl: SHA384 failed" )
55
69
}
56
70
return
57
71
}
58
72
59
73
func SHA512 (p []byte ) (sum [64 ]byte ) {
60
- if ! shaX (crypto .SHA512 , p , sum [:]) {
74
+ if ! hashOneShot (crypto .SHA512 , p , sum [:]) {
61
75
panic ("openssl: SHA512 failed" )
62
76
}
63
77
return
64
78
}
65
79
66
80
// SupportsHash returns true if a hash.Hash implementation is supported for h.
67
81
func SupportsHash (h crypto.Hash ) bool {
68
- switch h {
69
- case crypto .SHA1 , crypto .SHA224 , crypto .SHA256 , crypto .SHA384 , crypto .SHA512 :
70
- return true
71
- case crypto .SHA3_224 , crypto .SHA3_256 , crypto .SHA3_384 , crypto .SHA3_512 :
72
- return vMajor > 1 ||
73
- (vMajor >= 1 && vMinor > 1 ) ||
74
- (vMajor >= 1 && vMinor >= 1 && vPatch >= 1 )
75
- }
76
- return false
82
+ return cryptoHashToMD (h ) != nil
77
83
}
78
84
79
85
func SHA3_224 (p []byte ) (sum [28 ]byte ) {
80
- if ! shaX (crypto .SHA3_224 , p , sum [:]) {
86
+ if ! hashOneShot (crypto .SHA3_224 , p , sum [:]) {
81
87
panic ("openssl: SHA3_224 failed" )
82
88
}
83
89
return
84
90
}
85
91
86
92
func SHA3_256 (p []byte ) (sum [32 ]byte ) {
87
- if ! shaX (crypto .SHA3_256 , p , sum [:]) {
93
+ if ! hashOneShot (crypto .SHA3_256 , p , sum [:]) {
88
94
panic ("openssl: SHA3_256 failed" )
89
95
}
90
96
return
91
97
}
92
98
93
99
func SHA3_384 (p []byte ) (sum [48 ]byte ) {
94
- if ! shaX (crypto .SHA3_384 , p , sum [:]) {
100
+ if ! hashOneShot (crypto .SHA3_384 , p , sum [:]) {
95
101
panic ("openssl: SHA3_384 failed" )
96
102
}
97
103
return
98
104
}
99
105
100
106
func SHA3_512 (p []byte ) (sum [64 ]byte ) {
101
- if ! shaX (crypto .SHA3_512 , p , sum [:]) {
107
+ if ! hashOneShot (crypto .SHA3_512 , p , sum [:]) {
102
108
panic ("openssl: SHA3_512 failed" )
103
109
}
104
110
return
@@ -183,17 +189,17 @@ func (h *evpHash) BlockSize() int {
183
189
}
184
190
185
191
func (h * evpHash ) sum (out []byte ) {
186
- if C .go_sha_sum (h .ctx , h .ctx2 , base (out )) != 1 {
187
- panic (newOpenSSLError ("go_sha_sum " ))
192
+ if C .go_hash_sum (h .ctx , h .ctx2 , base (out )) != 1 {
193
+ panic (newOpenSSLError ("go_hash_sum " ))
188
194
}
189
195
runtime .KeepAlive (h )
190
196
}
191
197
192
- // shaState returns a pointer to the internal sha structure.
198
+ // hashState returns a pointer to the internal hash structure.
193
199
//
194
200
// The EVP_MD_CTX memory layout has changed in OpenSSL 3
195
201
// and the property holding the internal structure is no longer md_data but algctx.
196
- func (h * evpHash ) shaState () unsafe.Pointer {
202
+ func (h * evpHash ) hashState () unsafe.Pointer {
197
203
switch vMajor {
198
204
case 1 :
199
205
// https://github.com/openssl/openssl/blob/0418e993c717a6863f206feaa40673a261de7395/crypto/evp/evp_local.h#L12.
@@ -217,6 +223,97 @@ func (h *evpHash) shaState() unsafe.Pointer {
217
223
}
218
224
}
219
225
226
+ // NewMD4 returns a new MD4 hash.
227
+ // The returned hash doesn't implement encoding.BinaryMarshaler and
228
+ // encoding.BinaryUnmarshaler.
229
+ func NewMD4 () hash.Hash {
230
+ return & md4Hash {
231
+ evpHash : newEvpHash (crypto .MD4 , 16 , 64 ),
232
+ }
233
+ }
234
+
235
+ type md4Hash struct {
236
+ * evpHash
237
+ out [16 ]byte
238
+ }
239
+
240
+ func (h * md4Hash ) Sum (in []byte ) []byte {
241
+ h .sum (h .out [:])
242
+ return append (in , h .out [:]... )
243
+ }
244
+
245
+ // NewMD5 returns a new MD5 hash.
246
+ func NewMD5 () hash.Hash {
247
+ return & md5Hash {
248
+ evpHash : newEvpHash (crypto .MD5 , 16 , 64 ),
249
+ }
250
+ }
251
+
252
+ // md5State layout is taken from
253
+ // https://github.com/openssl/openssl/blob/0418e993c717a6863f206feaa40673a261de7395/include/openssl/md5.h#L33.
254
+ type md5State struct {
255
+ h [4 ]uint32
256
+ nl , nh uint32
257
+ x [64 ]byte
258
+ nx uint32
259
+ }
260
+
261
+ type md5Hash struct {
262
+ * evpHash
263
+ out [16 ]byte
264
+ }
265
+
266
+ func (h * md5Hash ) Sum (in []byte ) []byte {
267
+ h .sum (h .out [:])
268
+ return append (in , h .out [:]... )
269
+ }
270
+
271
+ const (
272
+ md5Magic = "md5\x01 "
273
+ md5MarshaledSize = len (md5Magic ) + 4 * 4 + 64 + 8
274
+ )
275
+
276
+ func (h * md5Hash ) MarshalBinary () ([]byte , error ) {
277
+ d := (* md5State )(h .hashState ())
278
+ if d == nil {
279
+ return nil , errors .New ("crypto/md5: can't retrieve hash state" )
280
+ }
281
+ b := make ([]byte , 0 , md5MarshaledSize )
282
+ b = append (b , md5Magic ... )
283
+ b = appendUint32 (b , d .h [0 ])
284
+ b = appendUint32 (b , d .h [1 ])
285
+ b = appendUint32 (b , d .h [2 ])
286
+ b = appendUint32 (b , d .h [3 ])
287
+ b = append (b , d .x [:d .nx ]... )
288
+ b = b [:len (b )+ len (d .x )- int (d .nx )] // already zero
289
+ b = appendUint64 (b , uint64 (d .nl )>> 3 | uint64 (d .nh )<< 29 )
290
+ return b , nil
291
+ }
292
+
293
+ func (h * md5Hash ) UnmarshalBinary (b []byte ) error {
294
+ if len (b ) < len (md5Magic ) || string (b [:len (md5Magic )]) != md5Magic {
295
+ return errors .New ("crypto/md5: invalid hash state identifier" )
296
+ }
297
+ if len (b ) != md5MarshaledSize {
298
+ return errors .New ("crypto/md5: invalid hash state size" )
299
+ }
300
+ d := (* md5State )(h .hashState ())
301
+ if d == nil {
302
+ return errors .New ("crypto/md5: can't retrieve hash state" )
303
+ }
304
+ b = b [len (md5Magic ):]
305
+ b , d .h [0 ] = consumeUint32 (b )
306
+ b , d .h [1 ] = consumeUint32 (b )
307
+ b , d .h [2 ] = consumeUint32 (b )
308
+ b , d .h [3 ] = consumeUint32 (b )
309
+ b = b [copy (d .x [:], b ):]
310
+ _ , n := consumeUint64 (b )
311
+ d .nl = uint32 (n << 3 )
312
+ d .nh = uint32 (n >> 29 )
313
+ d .nx = uint32 (n ) % 64
314
+ return nil
315
+ }
316
+
220
317
// NewSHA1 returns a new SHA1 hash.
221
318
func NewSHA1 () hash.Hash {
222
319
return & sha1Hash {
@@ -249,7 +346,7 @@ const (
249
346
)
250
347
251
348
func (h * sha1Hash ) MarshalBinary () ([]byte , error ) {
252
- d := (* sha1State )(h .shaState ())
349
+ d := (* sha1State )(h .hashState ())
253
350
if d == nil {
254
351
return nil , errors .New ("crypto/sha1: can't retrieve hash state" )
255
352
}
@@ -273,7 +370,7 @@ func (h *sha1Hash) UnmarshalBinary(b []byte) error {
273
370
if len (b ) != sha1MarshaledSize {
274
371
return errors .New ("crypto/sha1: invalid hash state size" )
275
372
}
276
- d := (* sha1State )(h .shaState ())
373
+ d := (* sha1State )(h .hashState ())
277
374
if d == nil {
278
375
return errors .New ("crypto/sha1: can't retrieve hash state" )
279
376
}
@@ -341,7 +438,7 @@ type sha256State struct {
341
438
}
342
439
343
440
func (h * sha224Hash ) MarshalBinary () ([]byte , error ) {
344
- d := (* sha256State )(h .shaState ())
441
+ d := (* sha256State )(h .hashState ())
345
442
if d == nil {
346
443
return nil , errors .New ("crypto/sha256: can't retrieve hash state" )
347
444
}
@@ -362,7 +459,7 @@ func (h *sha224Hash) MarshalBinary() ([]byte, error) {
362
459
}
363
460
364
461
func (h * sha256Hash ) MarshalBinary () ([]byte , error ) {
365
- d := (* sha256State )(h .shaState ())
462
+ d := (* sha256State )(h .hashState ())
366
463
if d == nil {
367
464
return nil , errors .New ("crypto/sha256: can't retrieve hash state" )
368
465
}
@@ -389,7 +486,7 @@ func (h *sha224Hash) UnmarshalBinary(b []byte) error {
389
486
if len (b ) != marshaledSize256 {
390
487
return errors .New ("crypto/sha256: invalid hash state size" )
391
488
}
392
- d := (* sha256State )(h .shaState ())
489
+ d := (* sha256State )(h .hashState ())
393
490
if d == nil {
394
491
return errors .New ("crypto/sha256: can't retrieve hash state" )
395
492
}
@@ -417,7 +514,7 @@ func (h *sha256Hash) UnmarshalBinary(b []byte) error {
417
514
if len (b ) != marshaledSize256 {
418
515
return errors .New ("crypto/sha256: invalid hash state size" )
419
516
}
420
- d := (* sha256State )(h .shaState ())
517
+ d := (* sha256State )(h .hashState ())
421
518
if d == nil {
422
519
return errors .New ("crypto/sha256: can't retrieve hash state" )
423
520
}
@@ -490,7 +587,7 @@ const (
490
587
)
491
588
492
589
func (h * sha384Hash ) MarshalBinary () ([]byte , error ) {
493
- d := (* sha512State )(h .shaState ())
590
+ d := (* sha512State )(h .hashState ())
494
591
if d == nil {
495
592
return nil , errors .New ("crypto/sha512: can't retrieve hash state" )
496
593
}
@@ -511,7 +608,7 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
511
608
}
512
609
513
610
func (h * sha512Hash ) MarshalBinary () ([]byte , error ) {
514
- d := (* sha512State )(h .shaState ())
611
+ d := (* sha512State )(h .hashState ())
515
612
if d == nil {
516
613
return nil , errors .New ("crypto/sha512: can't retrieve hash state" )
517
614
}
@@ -541,7 +638,7 @@ func (h *sha384Hash) UnmarshalBinary(b []byte) error {
541
638
if len (b ) != marshaledSize512 {
542
639
return errors .New ("crypto/sha512: invalid hash state size" )
543
640
}
544
- d := (* sha512State )(h .shaState ())
641
+ d := (* sha512State )(h .hashState ())
545
642
if d == nil {
546
643
return errors .New ("crypto/sha512: can't retrieve hash state" )
547
644
}
@@ -572,7 +669,7 @@ func (h *sha512Hash) UnmarshalBinary(b []byte) error {
572
669
if len (b ) != marshaledSize512 {
573
670
return errors .New ("crypto/sha512: invalid hash state size" )
574
671
}
575
- d := (* sha512State )(h .shaState ())
672
+ d := (* sha512State )(h .hashState ())
576
673
if d == nil {
577
674
return errors .New ("crypto/sha512: can't retrieve hash state" )
578
675
}
0 commit comments