Skip to content

Commit 79cb32c

Browse files
committed
make NewHashX functions inlinable
1 parent 25e7f1b commit 79cb32c

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

hash.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ func SHA3_512(p []byte) (sum [64]byte) {
111111
return
112112
}
113113

114-
var isMarshallableMap sync.Map
114+
var isMarshallableCache sync.Map
115115

116116
// isHashMarshallable returns true if the memory layout of cb
117117
// is known by this library and can therefore be marshalled.
118118
func isHashMarshallable(ch crypto.Hash) bool {
119119
if vMajor == 1 {
120120
return true
121121
}
122-
if v, ok := isMarshallableMap.Load(ch); ok {
122+
if v, ok := isMarshallableCache.Load(ch); ok {
123123
return v.(bool)
124124
}
125125
md := cryptoHashToMD(ch)
@@ -138,7 +138,7 @@ func isHashMarshallable(ch crypto.Hash) bool {
138138
// We only know the memory layout of the built-in providers.
139139
// See evpHash.hashState for more details.
140140
marshallable := name == "default" || name == "fips"
141-
isMarshallableMap.Store(ch, marshallable)
141+
isMarshallableCache.Store(ch, marshallable)
142142
return marshallable
143143
}
144144

@@ -148,12 +148,13 @@ type evpHash struct {
148148
// ctx2 is used in evpHash.sum to avoid changing
149149
// the state of ctx. Having it here allows reusing the
150150
// 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
154155
}
155156

156-
func newEvpHash(ch crypto.Hash, size, blockSize int) *evpHash {
157+
func newEvpHash(ch crypto.Hash) *evpHash {
157158
md := cryptoHashToMD(ch)
158159
if md == nil {
159160
panic("openssl: unsupported hash function: " + strconv.Itoa(int(ch)))
@@ -164,11 +165,13 @@ func newEvpHash(ch crypto.Hash, size, blockSize int) *evpHash {
164165
panic(newOpenSSLError("EVP_DigestInit_ex"))
165166
}
166167
ctx2 := C.go_openssl_EVP_MD_CTX_new()
168+
blockSize := int(C.go_openssl_EVP_MD_get_block_size(md))
167169
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),
172175
}
173176
runtime.SetFinalizer(h, (*evpHash).finalize)
174177
return h
@@ -232,6 +235,9 @@ func (h *evpHash) sum(out []byte) {
232235
// The EVP_MD_CTX memory layout has changed in OpenSSL 3
233236
// and the property holding the internal structure is no longer md_data but algctx.
234237
func (h *evpHash) hashState() unsafe.Pointer {
238+
if !h.marshallable {
239+
panic("openssl: hash state is not marshallable")
240+
}
235241
switch vMajor {
236242
case 1:
237243
// https://github.com/openssl/openssl/blob/0418e993c717a6863f206feaa40673a261de7395/crypto/evp/evp_local.h#L12.
@@ -260,7 +266,7 @@ func (h *evpHash) hashState() unsafe.Pointer {
260266
// encoding.BinaryUnmarshaler.
261267
func NewMD4() hash.Hash {
262268
return &md4Hash{
263-
evpHash: newEvpHash(crypto.MD4, 16, 64),
269+
evpHash: newEvpHash(crypto.MD4),
264270
}
265271
}
266272

@@ -276,8 +282,8 @@ func (h *md4Hash) Sum(in []byte) []byte {
276282

277283
// NewMD5 returns a new MD5 hash.
278284
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 {
281287
return &md5Marshal{h}
282288
}
283289
return &h
@@ -354,8 +360,8 @@ func (h *md5Marshal) UnmarshalBinary(b []byte) error {
354360

355361
// NewSHA1 returns a new SHA1 hash.
356362
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 {
359365
return &sha1Marshal{h}
360366
}
361367
return &h
@@ -434,8 +440,8 @@ func (h *sha1Marshal) UnmarshalBinary(b []byte) error {
434440

435441
// NewSHA224 returns a new SHA224 hash.
436442
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 {
439445
return &sha224Marshal{h}
440446
}
441447
return &h
@@ -453,8 +459,8 @@ func (h *sha224Hash) Sum(in []byte) []byte {
453459

454460
// NewSHA256 returns a new SHA256 hash.
455461
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 {
458464
return &sha256Marshal{h}
459465
}
460466
return &h
@@ -593,8 +599,8 @@ func (h *sha256Marshal) UnmarshalBinary(b []byte) error {
593599

594600
// NewSHA384 returns a new SHA384 hash.
595601
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 {
598604
return &sha384Marshal{h}
599605
}
600606
return &h
@@ -612,8 +618,8 @@ func (h *sha384Hash) Sum(in []byte) []byte {
612618

613619
// NewSHA512 returns a new SHA512 hash.
614620
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 {
617623
return &sha512Marshal{h}
618624
}
619625
return &h
@@ -761,7 +767,7 @@ func (h *sha512Marshal) UnmarshalBinary(b []byte) error {
761767
// NewSHA3_224 returns a new SHA3-224 hash.
762768
func NewSHA3_224() hash.Hash {
763769
return &sha3_224Hash{
764-
evpHash: newEvpHash(crypto.SHA3_224, 224/8, 64),
770+
evpHash: newEvpHash(crypto.SHA3_224),
765771
}
766772
}
767773

@@ -778,7 +784,7 @@ func (h *sha3_224Hash) Sum(in []byte) []byte {
778784
// NewSHA3_256 returns a new SHA3-256 hash.
779785
func NewSHA3_256() hash.Hash {
780786
return &sha3_256Hash{
781-
evpHash: newEvpHash(crypto.SHA3_256, 256/8, 64),
787+
evpHash: newEvpHash(crypto.SHA3_256),
782788
}
783789
}
784790

@@ -795,7 +801,7 @@ func (h *sha3_256Hash) Sum(in []byte) []byte {
795801
// NewSHA3_384 returns a new SHA3-384 hash.
796802
func NewSHA3_384() hash.Hash {
797803
return &sha3_384Hash{
798-
evpHash: newEvpHash(crypto.SHA3_384, 384/8, 128),
804+
evpHash: newEvpHash(crypto.SHA3_384),
799805
}
800806
}
801807

@@ -812,7 +818,7 @@ func (h *sha3_384Hash) Sum(in []byte) []byte {
812818
// NewSHA3_512 returns a new SHA3-512 hash.
813819
func NewSHA3_512() hash.Hash {
814820
return &sha3_512Hash{
815-
evpHash: newEvpHash(crypto.SHA3_512, 512/8, 128),
821+
evpHash: newEvpHash(crypto.SHA3_512),
816822
}
817823
}
818824

shims.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ DEFINEFUNC_3_0(GO_EVP_MD_PTR, EVP_MD_fetch, (GO_OSSL_LIB_CTX_PTR ctx, const char
195195
DEFINEFUNC_3_0(void, EVP_MD_free, (GO_EVP_MD_PTR md), (md)) \
196196
DEFINEFUNC_3_0(const char *, EVP_MD_get0_name, (const GO_EVP_MD_PTR md), (md)) \
197197
DEFINEFUNC_3_0(const GO_OSSL_PROVIDER_PTR, EVP_MD_get0_provider, (const GO_EVP_MD_PTR md), (md)) \
198+
DEFINEFUNC(int, EVP_MD_get_block_size, (const GO_EVP_MD_PTR md), (md)) \
198199
DEFINEFUNC(int, RAND_bytes, (unsigned char *arg0, int arg1), (arg0, arg1)) \
199200
DEFINEFUNC_RENAMED_1_1(GO_EVP_MD_CTX_PTR, EVP_MD_CTX_new, EVP_MD_CTX_create, (void), ()) \
200201
DEFINEFUNC_RENAMED_1_1(void, EVP_MD_CTX_free, EVP_MD_CTX_destroy, (GO_EVP_MD_CTX_PTR ctx), (ctx)) \

0 commit comments

Comments
 (0)