Skip to content

Commit b095911

Browse files
committed
use raw parameters
1 parent 84820f0 commit b095911

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

params.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,44 @@ package openssl
55
// #include "goopenssl.h"
66
import "C"
77
import (
8-
"math"
98
"unsafe"
109
)
1110

11+
const _OSSL_PARAM_UNMODIFIED uint = uint(^uintptr(0))
12+
1213
// _OSSL_PARAM is a structure to pass or request object parameters.
1314
// https://docs.openssl.org/3.0/man3/OSSL_PARAM/.
1415
type _OSSL_PARAM struct {
1516
Key *C.char
1617
DataType uint32
1718
Data unsafe.Pointer
18-
DataSize int
19-
ReturnSize int
19+
DataSize uint
20+
ReturnSize uint
2021
}
2122

2223
func ossl_param_construct(key *C.char, dataType uint32, data unsafe.Pointer, dataSize int) _OSSL_PARAM {
2324
return _OSSL_PARAM{
2425
Key: key,
2526
DataType: dataType,
2627
Data: data,
27-
DataSize: dataSize,
28-
ReturnSize: math.MaxInt - 1,
28+
DataSize: uint(dataSize),
29+
ReturnSize: _OSSL_PARAM_UNMODIFIED,
2930
}
3031
}
3132

3233
func _OSSL_PARAM_construct_octet_string(key *C.char, data unsafe.Pointer, dataSize int) _OSSL_PARAM {
3334
return ossl_param_construct(key, C.GO_OSSL_PARAM_OCTET_STRING, data, dataSize)
3435
}
3536

37+
func _OSSL_PARAM_construct_int32(key *C.char, data *int32) _OSSL_PARAM {
38+
return ossl_param_construct(key, C.GO_OSSL_PARAM_INTEGER, unsafe.Pointer(data), 4)
39+
}
40+
3641
func _OSSL_PARAM_construct_end() _OSSL_PARAM {
3742
return _OSSL_PARAM{}
3843
}
44+
45+
func _OSSL_PARAM_modified(param *_OSSL_PARAM) bool {
46+
// If ReturnSize is not set, the parameter has not been modified.
47+
return param != nil && param.ReturnSize != _OSSL_PARAM_UNMODIFIED
48+
}

providersymcrypt.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func symCryptUnmarshalBinary(d []byte, chain, buffer []byte) _UINT64 {
106106
return newUINT64(length)
107107
}
108108

109-
// swapEndianessInt32 swaps the endianness of the given byte slice
109+
// swapEndianessUint32 swaps the endianness of the given byte slice
110110
// in place. It assumes the slice is a backup of a 32-bit integer array.
111-
func swapEndianessInt32(d []uint8) {
111+
func swapEndianessUint32(d []uint8) {
112112
for i := 0; i < len(d); i += 4 {
113113
d[i], d[i+3] = d[i+3], d[i]
114114
d[i+1], d[i+2] = d[i+2], d[i+1]
@@ -128,13 +128,13 @@ type _SYMCRYPT_MD5_STATE_EXPORT_BLOB struct {
128128
func (b *_SYMCRYPT_MD5_STATE_EXPORT_BLOB) appendBinary(d []byte) ([]byte, error) {
129129
// b.chain is little endian, but Go expects big endian,
130130
// we need to swap the bytes.
131-
swapEndianessInt32(b.chain[:])
131+
swapEndianessUint32(b.chain[:])
132132
return symCryptAppendBinary(d, b.chain[:], b.buffer[:], b.length), nil
133133
}
134134

135135
func (b *_SYMCRYPT_MD5_STATE_EXPORT_BLOB) unmarshalBinary(d []byte) {
136136
b.length = symCryptUnmarshalBinary(d, b.chain[:], b.buffer[:])
137-
swapEndianessInt32(b.chain[:])
137+
swapEndianessUint32(b.chain[:])
138138
}
139139

140140
type _SYMCRYPT_SHA1_STATE_EXPORT_BLOB struct {
@@ -208,6 +208,9 @@ func symCryptHashAppendBinary(ctx C.GO_EVP_MD_CTX_PTR, ch crypto.Hash, magic str
208208
if C.go_openssl_EVP_MD_CTX_get_params(ctx, (C.GO_OSSL_PARAM_PTR)(unsafe.Pointer(&params[0]))) != 1 {
209209
return nil, newOpenSSLError("EVP_MD_CTX_get_params")
210210
}
211+
if !_OSSL_PARAM_modified(&params[0]) {
212+
return nil, errors.New("EVP_MD_CTX_get_params did not retrieve the state")
213+
}
211214

212215
header := (*_SYMCRYPT_BLOB_HEADER)(unsafe.Pointer(&state[0]))
213216
if header.magic != _SYMCRYPT_BLOB_MAGIC {
@@ -275,21 +278,17 @@ func symCryptHashUnmarshalBinary(ctx C.GO_EVP_MD_CTX_PTR, ch crypto.Hash, magic
275278
default:
276279
panic("unsupported hash " + ch.String())
277280
}
278-
bld := C.go_openssl_OSSL_PARAM_BLD_new()
279-
if bld == nil {
280-
return newOpenSSLError("OSSL_PARAM_BLD_new")
281-
}
282-
defer C.go_openssl_OSSL_PARAM_BLD_free(bld)
283-
cbytes := C.CBytes(unsafe.Slice((*byte)(blobPtr), hdr.size))
284-
defer C.free(cbytes)
285-
C.go_openssl_OSSL_PARAM_BLD_push_octet_string(bld, _SCOSSL_DIGEST_PARAM_STATE, cbytes, C.size_t(hdr.size))
286-
C.go_openssl_OSSL_PARAM_BLD_push_int32(bld, _SCOSSL_DIGEST_PARAM_RECOMPUTE_CHECKSUM, 1)
287-
params := C.go_openssl_OSSL_PARAM_BLD_to_param(bld)
288-
if params == nil {
289-
return newOpenSSLError("OSSL_PARAM_BLD_to_param")
281+
var checksum int32 = 1
282+
cbytesBlob := C.CBytes(unsafe.Slice((*byte)(blobPtr), hdr.size))
283+
defer C.free(cbytesBlob)
284+
cbytesCheksum := C.CBytes(unsafe.Slice((*byte)(unsafe.Pointer(&checksum)), 4))
285+
defer C.free(cbytesCheksum)
286+
params := [3]_OSSL_PARAM{
287+
_OSSL_PARAM_construct_octet_string(_SCOSSL_DIGEST_PARAM_STATE, cbytesBlob, int(hdr.size)),
288+
_OSSL_PARAM_construct_int32(_SCOSSL_DIGEST_PARAM_RECOMPUTE_CHECKSUM, (*int32)(cbytesCheksum)),
289+
_OSSL_PARAM_construct_end(),
290290
}
291-
defer C.go_openssl_OSSL_PARAM_free(params)
292-
if C.go_openssl_EVP_MD_CTX_set_params(ctx, params) == 0 {
291+
if C.go_openssl_EVP_MD_CTX_set_params(ctx, (C.GO_OSSL_PARAM_PTR)(unsafe.Pointer(&params[0]))) != 1 {
293292
return newOpenSSLError("EVP_MD_CTX_set_params")
294293
}
295294
return nil

shims.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ enum {
2626
GO_EVP_MAX_MD_SIZE = 64,
2727

2828
GO_EVP_PKEY_PUBLIC_KEY = 0x86,
29-
GO_EVP_PKEY_KEYPAIR = 0x87,
29+
GO_EVP_PKEY_KEYPAIR = 0x87
30+
};
3031

32+
// #if OPENSSL_VERSION_NUMBER >= 0x30000000L
33+
// #include <openssl/params.h>
34+
// #endif
35+
enum {
36+
GO_OSSL_PARAM_INTEGER = 1,
3137
GO_OSSL_PARAM_OCTET_STRING = 5
3238
};
3339

0 commit comments

Comments
 (0)