Skip to content

Commit dd36184

Browse files
committed
reduce diffs
1 parent 0bf146c commit dd36184

File tree

8 files changed

+101
-122
lines changed

8 files changed

+101
-122
lines changed

evp.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ const (
7474
)
7575

7676
type hashAlgorithm struct {
77-
md ossl.EVP_MD_PTR
78-
ch crypto.Hash
79-
size int
80-
blockSize int
81-
provider provider
77+
md ossl.EVP_MD_PTR
78+
ch crypto.Hash
79+
size int
80+
blockSize int
81+
provider provider
82+
marshallable bool
83+
magic string
84+
marshalledSize int
8285
}
8386

8487
// loadHash converts a crypto.Hash to a EVP_MD.
@@ -95,25 +98,41 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
9598
hash.md = ossl.EVP_md4()
9699
case crypto.MD5:
97100
hash.md = ossl.EVP_md5()
101+
hash.magic = magicMD5
102+
hash.marshalledSize = marshaledSizeMD5
98103
case crypto.MD5SHA1:
99104
hash.md = ossl.EVP_md5_sha1()
100105
case crypto.SHA1:
101106
hash.md = ossl.EVP_sha1()
107+
hash.magic = magic1
108+
hash.marshalledSize = marshaledSize1
102109
case crypto.SHA224:
103110
hash.md = ossl.EVP_sha224()
111+
hash.magic = magic224
112+
hash.marshalledSize = marshaledSize256
104113
case crypto.SHA256:
105114
hash.md = ossl.EVP_sha256()
115+
hash.magic = magic256
116+
hash.marshalledSize = marshaledSize256
106117
case crypto.SHA384:
107118
hash.md = ossl.EVP_sha384()
119+
hash.magic = magic384
120+
hash.marshalledSize = marshaledSize512
108121
case crypto.SHA512:
109122
hash.md = ossl.EVP_sha512()
123+
hash.magic = magic512
124+
hash.marshalledSize = marshaledSize512
110125
case crypto.SHA512_224:
111126
if versionAtOrAbove(1, 1, 1) {
112127
hash.md = ossl.EVP_sha512_224()
128+
hash.magic = magic512_224
129+
hash.marshalledSize = marshaledSize512
113130
}
114131
case crypto.SHA512_256:
115132
if versionAtOrAbove(1, 1, 1) {
116133
hash.md = ossl.EVP_sha512_256()
134+
hash.magic = magic512_256
135+
hash.marshalledSize = marshaledSize512
117136
}
118137
case crypto.SHA3_224:
119138
if versionAtOrAbove(1, 1, 1) {
@@ -151,6 +170,11 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
151170
hash.md = md
152171
}
153172
}
173+
if hash.magic != "" {
174+
if hash.marshalledSize == 0 {
175+
panic("marshalledSize must be set for " + hash.magic)
176+
}
177+
}
154178

155179
switch vMajor {
156180
case 1:
@@ -161,10 +185,13 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
161185
switch C.GoString((*C.char)(unsafe.Pointer(cname))) {
162186
case "default":
163187
hash.provider = providerOSSLDefault
188+
hash.marshallable = hash.magic != ""
164189
case "fips":
165190
hash.provider = providerOSSLFIPS
191+
hash.marshallable = hash.magic != ""
166192
case "symcryptprovider":
167193
hash.provider = providerSymCrypt
194+
hash.marshallable = hash.magic != "" && isSymCryptHashStateSerializable(hash.md)
168195
}
169196
}
170197
default:

hash.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ const (
2525
magic512_256 = "sha\x06"
2626
magic512 = "sha\x07"
2727

28-
marshaledSizeMD5 = len(magicMD5) + 4*4 + 64 + 8
29-
marshaledSize1 = len(magic1) + 5*4 + 64 + 8
30-
marshaledSize256 = len(magic256) + 8*4 + 64 + 8
31-
marshaledSize512 = len(magic512) + 8*8 + 128 + 8
28+
marshaledSizeMD5 = len(magicMD5) + 4*4 + 64 + 8 // from crypto/md5
29+
marshaledSize1 = len(magic1) + 5*4 + 64 + 8 // from crypto/sha1
30+
marshaledSize256 = len(magic256) + 8*4 + 64 + 8 // from crypto/sha256
31+
marshaledSize512 = len(magic512) + 8*8 + 128 + 8 // from crypto/sha512
3232
)
3333

3434
// maxHashSize is the size of SHA52 and SHA3_512, the largest hashes we support.
@@ -385,70 +385,48 @@ func (h *evpHash) Clone() hash.Hash {
385385
var errHashNotMarshallable = errors.New("openssl: hash state is not marshallable")
386386

387387
func (d *evpHash) MarshalBinary() ([]byte, error) {
388-
buf := make([]byte, 0, marshaledSize512) // stack allocate the buffer by setting the max size we support
388+
if !d.alg.marshallable {
389+
return nil, errHashNotMarshallable
390+
}
391+
buf := make([]byte, 0, d.alg.marshalledSize)
389392
return d.AppendBinary(buf)
390393
}
391394

392395
func (d *evpHash) AppendBinary(buf []byte) ([]byte, error) {
393396
defer runtime.KeepAlive(d)
394-
d.init()
395-
magic, _ := cryptoHashEncodingInfo(d.alg.ch)
396-
if magic == "" {
397+
if !d.alg.marshallable {
397398
return nil, errHashNotMarshallable
398399
}
400+
d.init()
399401
switch d.alg.provider {
400402
case providerOSSLDefault, providerOSSLFIPS:
401-
return osslHashAppendBinary(d.ctx, d.alg.ch, magic, buf)
403+
return osslHashAppendBinary(d.ctx, d.alg.ch, d.alg.magic, buf)
402404
case providerSymCrypt:
403-
return symCryptHashAppendBinary(d.ctx, d.alg.ch, magic, buf)
405+
return symCryptHashAppendBinary(d.ctx, d.alg.ch, d.alg.magic, buf)
404406
default:
405-
return nil, errHashNotMarshallable
407+
panic("openssl: unknown hash provider" + strconv.Itoa(int(d.alg.provider)))
406408
}
407409
}
408410

409411
func (d *evpHash) UnmarshalBinary(b []byte) error {
410412
defer runtime.KeepAlive(d)
411413
d.init()
412-
magic, size := cryptoHashEncodingInfo(d.alg.ch)
413-
if magic == "" {
414+
if !d.alg.marshallable {
414415
return errHashNotMarshallable
415416
}
416-
if len(b) < len(magic) || string(b[:len(magic)]) != string(magic[:]) {
417+
if len(b) < len(d.alg.magic) || string(b[:len(d.alg.magic)]) != d.alg.magic {
417418
return errors.New("openssl: invalid hash state identifier")
418419
}
419-
if len(b) != size {
420+
if len(b) != d.alg.marshalledSize {
420421
return errors.New("openssl: invalid hash state size")
421422
}
422423
switch d.alg.provider {
423424
case providerOSSLDefault, providerOSSLFIPS:
424-
return osslHashUnmarshalBinary(d.ctx, d.alg.ch, magic, b)
425+
return osslHashUnmarshalBinary(d.ctx, d.alg.ch, d.alg.magic, b)
425426
case providerSymCrypt:
426-
return symCryptHashUnmarshalBinary(d.ctx, d.alg.ch, magic, b)
427-
default:
428-
return errHashNotMarshallable
429-
}
430-
}
431-
432-
func cryptoHashEncodingInfo(ch crypto.Hash) (magic string, size int) {
433-
switch ch {
434-
case crypto.MD5:
435-
return magicMD5, marshaledSizeMD5
436-
case crypto.SHA1:
437-
return magic1, marshaledSize1
438-
case crypto.SHA224:
439-
return magic224, marshaledSize256
440-
case crypto.SHA256:
441-
return magic256, marshaledSize256
442-
case crypto.SHA384:
443-
return magic384, marshaledSize512
444-
case crypto.SHA512_224:
445-
return magic512_224, marshaledSize512
446-
case crypto.SHA512_256:
447-
return magic512_256, marshaledSize512
448-
case crypto.SHA512:
449-
return magic512, marshaledSize512
427+
return symCryptHashUnmarshalBinary(d.ctx, d.alg.ch, d.alg.magic, b)
450428
default:
451-
return "", 0
429+
panic("openssl: unknown hash provider" + strconv.Itoa(int(d.alg.provider)))
452430
}
453431
}
454432

internal/ossl/ossl.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ go_hash_sum(const _EVP_MD_CTX_PTR ctx, _EVP_MD_CTX_PTR ctx2, unsigned char *out,
2323
*/
2424
import "C"
2525
import (
26-
"math"
2726
"unsafe"
2827
)
2928

@@ -42,30 +41,41 @@ func HashSum(ctx1, ctx2 EVP_MD_CTX_PTR, out []byte) error {
4241
return nil
4342
}
4443

44+
const _OSSL_PARAM_UNMODIFIED uint = uint(^uintptr(0))
45+
4546
// OSSL_PARAM is a structure to pass or request object parameters.
4647
// https://docs.openssl.org/3.0/man3/OSSL_PARAM/.
4748
type OSSL_PARAM struct {
4849
Key *byte
4950
DataType uint32
5051
Data unsafe.Pointer
51-
DataSize int
52-
ReturnSize int
52+
DataSize uint
53+
ReturnSize uint
5354
}
5455

5556
func ossl_param_construct(key *byte, dataType uint32, data unsafe.Pointer, dataSize int) OSSL_PARAM {
5657
return OSSL_PARAM{
5758
Key: key,
5859
DataType: dataType,
5960
Data: data,
60-
DataSize: dataSize,
61-
ReturnSize: math.MaxInt - 1,
61+
DataSize: uint(dataSize),
62+
ReturnSize: _OSSL_PARAM_UNMODIFIED,
6263
}
6364
}
6465

6566
func OSSL_PARAM_construct_octet_string(key *byte, data unsafe.Pointer, dataSize int) OSSL_PARAM {
6667
return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, data, dataSize)
6768
}
6869

70+
func OSSL_PARAM_construct_int32(key *byte, data *int32) OSSL_PARAM {
71+
return ossl_param_construct(key, OSSL_PARAM_INTEGER, unsafe.Pointer(data), 4)
72+
}
73+
6974
func OSSL_PARAM_construct_end() OSSL_PARAM {
7075
return OSSL_PARAM{}
7176
}
77+
78+
func OSSL_PARAM_modified(param *OSSL_PARAM) bool {
79+
// If ReturnSize is not set, the parameter has not been modified.
80+
return param != nil && param.ReturnSize != _OSSL_PARAM_UNMODIFIED
81+
}

internal/ossl/shims.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// #include <openssl/core_names.h>
2424
// #include <openssl/provider.h>
2525
// #include <openssl/param_build.h>
26+
// #include <openssl/params.h>
2627
// #endif
2728
// #if OPENSSL_VERSION_NUMBER < 0x10100000L
2829
// #include <openssl/bn.h>
@@ -87,6 +88,7 @@ enum {
8788
_EVP_PKEY_CTRL_DSA_PARAMGEN_BITS = 0x1001,
8889
_EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS = 0x1002,
8990

91+
_OSSL_PARAM_INTEGER = 1,
9092
_OSSL_PARAM_OCTET_STRING = 5,
9193
};
9294

internal/ossl/zossl.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ossl/zossl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum {
8383
_EVP_PKEY_CTRL_RSA_OAEP_LABEL = 0x100A,
8484
_EVP_PKEY_CTRL_DSA_PARAMGEN_BITS = 0x1001,
8585
_EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS = 0x1002,
86+
_OSSL_PARAM_INTEGER = 1,
8687
_OSSL_PARAM_OCTET_STRING = 5,
8788
};
8889

params.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@ type paramBuilder struct {
3737

3838
// newParamBuilder creates a new paramBuilder.
3939
func newParamBuilder() (*paramBuilder, error) {
40-
return newParamBuilderN(8) // the maximum known number of BIGNUMs to free are 8 for RSA
41-
}
42-
43-
func newParamBuilderN(n int) (*paramBuilder, error) {
4440
bld, err := ossl.OSSL_PARAM_BLD_new()
4541
if err != nil {
4642
return nil, err
4743
}
4844
pb := &paramBuilder{
4945
bld: bld,
50-
bnToFree: make([]bnParam, 0, n),
46+
bnToFree: make([]bnParam, 0, 8), // the maximum known number of BIGNUMs to free are 8 for RSA
5147
}
5248
runtime.SetFinalizer(pb, (*paramBuilder).finalize)
5349
return pb, nil

0 commit comments

Comments
 (0)