@@ -111,15 +111,15 @@ func SHA3_512(p []byte) (sum [64]byte) {
111
111
return
112
112
}
113
113
114
- var isMarshallableMap sync.Map
114
+ var isMarshallableCache sync.Map
115
115
116
116
// isHashMarshallable returns true if the memory layout of cb
117
117
// is known by this library and can therefore be marshalled.
118
118
func isHashMarshallable (ch crypto.Hash ) bool {
119
119
if vMajor == 1 {
120
120
return true
121
121
}
122
- if v , ok := isMarshallableMap .Load (ch ); ok {
122
+ if v , ok := isMarshallableCache .Load (ch ); ok {
123
123
return v .(bool )
124
124
}
125
125
md := cryptoHashToMD (ch )
@@ -138,7 +138,7 @@ func isHashMarshallable(ch crypto.Hash) bool {
138
138
// We only know the memory layout of the built-in providers.
139
139
// See evpHash.hashState for more details.
140
140
marshallable := name == "default" || name == "fips"
141
- isMarshallableMap .Store (ch , marshallable )
141
+ isMarshallableCache .Store (ch , marshallable )
142
142
return marshallable
143
143
}
144
144
@@ -148,12 +148,13 @@ type evpHash struct {
148
148
// ctx2 is used in evpHash.sum to avoid changing
149
149
// the state of ctx. Having it here allows reusing the
150
150
// same allocated object multiple times.
151
- ctx2 C.GO_EVP_MD_CTX_PTR
152
- size int
153
- blockSize int
151
+ ctx2 C.GO_EVP_MD_CTX_PTR
152
+ size int
153
+ blockSize int
154
+ marshallable bool
154
155
}
155
156
156
- func newEvpHash (ch crypto.Hash , size , blockSize int ) * evpHash {
157
+ func newEvpHash (ch crypto.Hash ) * evpHash {
157
158
md := cryptoHashToMD (ch )
158
159
if md == nil {
159
160
panic ("openssl: unsupported hash function: " + strconv .Itoa (int (ch )))
@@ -164,11 +165,13 @@ func newEvpHash(ch crypto.Hash, size, blockSize int) *evpHash {
164
165
panic (newOpenSSLError ("EVP_DigestInit_ex" ))
165
166
}
166
167
ctx2 := C .go_openssl_EVP_MD_CTX_new ()
168
+ blockSize := int (C .go_openssl_EVP_MD_get_block_size (md ))
167
169
h := & evpHash {
168
- ctx : ctx ,
169
- ctx2 : ctx2 ,
170
- size : size ,
171
- blockSize : blockSize ,
170
+ ctx : ctx ,
171
+ ctx2 : ctx2 ,
172
+ size : ch .Size (),
173
+ blockSize : blockSize ,
174
+ marshallable : isHashMarshallable (ch ),
172
175
}
173
176
runtime .SetFinalizer (h , (* evpHash ).finalize )
174
177
return h
@@ -232,6 +235,9 @@ func (h *evpHash) sum(out []byte) {
232
235
// The EVP_MD_CTX memory layout has changed in OpenSSL 3
233
236
// and the property holding the internal structure is no longer md_data but algctx.
234
237
func (h * evpHash ) hashState () unsafe.Pointer {
238
+ if ! h .marshallable {
239
+ panic ("openssl: hash state is not marshallable" )
240
+ }
235
241
switch vMajor {
236
242
case 1 :
237
243
// https://github.com/openssl/openssl/blob/0418e993c717a6863f206feaa40673a261de7395/crypto/evp/evp_local.h#L12.
@@ -260,7 +266,7 @@ func (h *evpHash) hashState() unsafe.Pointer {
260
266
// encoding.BinaryUnmarshaler.
261
267
func NewMD4 () hash.Hash {
262
268
return & md4Hash {
263
- evpHash : newEvpHash (crypto .MD4 , 16 , 64 ),
269
+ evpHash : newEvpHash (crypto .MD4 ),
264
270
}
265
271
}
266
272
@@ -276,8 +282,8 @@ func (h *md4Hash) Sum(in []byte) []byte {
276
282
277
283
// NewMD5 returns a new MD5 hash.
278
284
func NewMD5 () hash.Hash {
279
- h := md5Hash {evpHash : newEvpHash (crypto .MD5 , 16 , 64 )}
280
- if isHashMarshallable ( crypto . MD5 ) {
285
+ h := md5Hash {evpHash : newEvpHash (crypto .MD5 )}
286
+ if h . marshallable {
281
287
return & md5Marshal {h }
282
288
}
283
289
return & h
@@ -354,8 +360,8 @@ func (h *md5Marshal) UnmarshalBinary(b []byte) error {
354
360
355
361
// NewSHA1 returns a new SHA1 hash.
356
362
func NewSHA1 () hash.Hash {
357
- h := sha1Hash {evpHash : newEvpHash (crypto .SHA1 , 20 , 64 )}
358
- if isHashMarshallable ( crypto . SHA1 ) {
363
+ h := sha1Hash {evpHash : newEvpHash (crypto .SHA1 )}
364
+ if h . marshallable {
359
365
return & sha1Marshal {h }
360
366
}
361
367
return & h
@@ -434,8 +440,8 @@ func (h *sha1Marshal) UnmarshalBinary(b []byte) error {
434
440
435
441
// NewSHA224 returns a new SHA224 hash.
436
442
func NewSHA224 () hash.Hash {
437
- h := sha224Hash {evpHash : newEvpHash (crypto .SHA224 , 224 / 8 , 64 )}
438
- if isHashMarshallable ( crypto . SHA224 ) {
443
+ h := sha224Hash {evpHash : newEvpHash (crypto .SHA224 )}
444
+ if h . marshallable {
439
445
return & sha224Marshal {h }
440
446
}
441
447
return & h
@@ -453,8 +459,8 @@ func (h *sha224Hash) Sum(in []byte) []byte {
453
459
454
460
// NewSHA256 returns a new SHA256 hash.
455
461
func NewSHA256 () hash.Hash {
456
- h := sha256Hash {evpHash : newEvpHash (crypto .SHA256 , 256 / 8 , 64 )}
457
- if isHashMarshallable ( crypto . SHA256 ) {
462
+ h := sha256Hash {evpHash : newEvpHash (crypto .SHA256 )}
463
+ if h . marshallable {
458
464
return & sha256Marshal {h }
459
465
}
460
466
return & h
@@ -593,8 +599,8 @@ func (h *sha256Marshal) UnmarshalBinary(b []byte) error {
593
599
594
600
// NewSHA384 returns a new SHA384 hash.
595
601
func NewSHA384 () hash.Hash {
596
- h := sha384Hash {evpHash : newEvpHash (crypto .SHA384 , 384 / 8 , 128 )}
597
- if isHashMarshallable ( crypto . SHA384 ) {
602
+ h := sha384Hash {evpHash : newEvpHash (crypto .SHA384 )}
603
+ if h . marshallable {
598
604
return & sha384Marshal {h }
599
605
}
600
606
return & h
@@ -612,8 +618,8 @@ func (h *sha384Hash) Sum(in []byte) []byte {
612
618
613
619
// NewSHA512 returns a new SHA512 hash.
614
620
func NewSHA512 () hash.Hash {
615
- h := sha512Hash {evpHash : newEvpHash (crypto .SHA512 , 512 / 8 , 128 )}
616
- if isHashMarshallable ( crypto . SHA512 ) {
621
+ h := sha512Hash {evpHash : newEvpHash (crypto .SHA512 )}
622
+ if h . marshallable {
617
623
return & sha512Marshal {h }
618
624
}
619
625
return & h
@@ -761,7 +767,7 @@ func (h *sha512Marshal) UnmarshalBinary(b []byte) error {
761
767
// NewSHA3_224 returns a new SHA3-224 hash.
762
768
func NewSHA3_224 () hash.Hash {
763
769
return & sha3_224Hash {
764
- evpHash : newEvpHash (crypto .SHA3_224 , 224 / 8 , 64 ),
770
+ evpHash : newEvpHash (crypto .SHA3_224 ),
765
771
}
766
772
}
767
773
@@ -778,7 +784,7 @@ func (h *sha3_224Hash) Sum(in []byte) []byte {
778
784
// NewSHA3_256 returns a new SHA3-256 hash.
779
785
func NewSHA3_256 () hash.Hash {
780
786
return & sha3_256Hash {
781
- evpHash : newEvpHash (crypto .SHA3_256 , 256 / 8 , 64 ),
787
+ evpHash : newEvpHash (crypto .SHA3_256 ),
782
788
}
783
789
}
784
790
@@ -795,7 +801,7 @@ func (h *sha3_256Hash) Sum(in []byte) []byte {
795
801
// NewSHA3_384 returns a new SHA3-384 hash.
796
802
func NewSHA3_384 () hash.Hash {
797
803
return & sha3_384Hash {
798
- evpHash : newEvpHash (crypto .SHA3_384 , 384 / 8 , 128 ),
804
+ evpHash : newEvpHash (crypto .SHA3_384 ),
799
805
}
800
806
}
801
807
@@ -812,7 +818,7 @@ func (h *sha3_384Hash) Sum(in []byte) []byte {
812
818
// NewSHA3_512 returns a new SHA3-512 hash.
813
819
func NewSHA3_512 () hash.Hash {
814
820
return & sha3_512Hash {
815
- evpHash : newEvpHash (crypto .SHA3_512 , 512 / 8 , 128 ),
821
+ evpHash : newEvpHash (crypto .SHA3_512 ),
816
822
}
817
823
}
818
824
0 commit comments