Skip to content

Commit e5dac47

Browse files
author
Vitaly Isaev
committed
UCS-7462 | feat: check if hash function is supported by the current version of OpenSSL
1 parent 0a6e8fa commit e5dac47

File tree

3 files changed

+125
-31
lines changed

3 files changed

+125
-31
lines changed

digest_computer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *DigestComputer) Close() {
4141
}
4242

4343
func (s *DigestComputer) Reset() error {
44-
if 1 != C.X_EVP_DigestInit_ex(s.ctx, s.evpMD.fp(), engineRef(s.engine)) {
44+
if 1 != C.X_EVP_DigestInit_ex(s.ctx, s.evpMD.c(), engineRef(s.engine)) {
4545
return fmt.Errorf("openssl: %v: cannot init evpMD ctx", s.evpMD.String())
4646
}
4747
return nil

evp_md.go

+124-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package openssl
22

3+
// #include "openssl/opensslv.h"
34
// #include "shim.h"
45
import "C"
56

7+
// EVP_MD represents hash function implemented by OpenSSL
68
type EVP_MD int
79

810
const (
@@ -32,8 +34,10 @@ const (
3234
EVP_SHAKE128
3335
EVP_SHAKE256
3436
EVP_SM3
37+
EVP_WHIRLPOOL
3538
)
3639

40+
// Size returns the size of the digest
3741
func (evp EVP_MD) Size() int {
3842
var bits int
3943
switch evp {
@@ -83,6 +87,7 @@ func (evp EVP_MD) Size() int {
8387
return bits / 8
8488
}
8589

90+
// Size returns hash function block size in bytes
8691
func (evp EVP_MD) BlockSize() int {
8792
var bits int
8893
switch evp {
@@ -181,7 +186,125 @@ func (evp EVP_MD) String() string {
181186
}
182187
}
183188

184-
func (evp EVP_MD) fp() (evpMD *C.EVP_MD) {
189+
190+
/*
191+
OpenSSL compatibility table:
192+
193+
1.1.1 -> 0x1010100fL
194+
1.1.0 -> 0x1010000fL
195+
196+
Digest 1.0.2 1.1.0 1.1.1
197+
BLAKE2B512 - + +
198+
BLAKE2S256 - + +
199+
GOST - + +
200+
MD2 - + +
201+
MD4 + + +
202+
MD5 + + +
203+
RIPEMD160 + + +
204+
SHA1 + + +
205+
SHA224 + + +
206+
SHA256 + + +
207+
SHA384 + + +
208+
SHA512 + + +
209+
SHA512-224 - - +
210+
SHA512-256 - - +
211+
SHA3-224 - - +
212+
SHA3-256 - - +
213+
SHA3-384 - - +
214+
SHA3-512 - - +
215+
SHAKE128 - - +
216+
SHAKE256 - - +
217+
SM3 - - +
218+
WHIRLPOOL + + +
219+
*/
220+
221+
var hashFunctionsOpenSSLv111 = map[EVP_MD]bool{
222+
EVP_BLAKE2B_512: true,
223+
EVP_BLAKE2S_256: true,
224+
EVP_GOST: true,
225+
EVP_MD2: true,
226+
EVP_MD4: true,
227+
EVP_MD5: true,
228+
EVP_RIPEMD160: true,
229+
EVP_SHA1: true,
230+
EVP_SHA224: true,
231+
EVP_SHA256: true,
232+
EVP_SHA384: true,
233+
EVP_SHA512: true,
234+
EVP_SHA512_224: true,
235+
EVP_SHA512_256: true,
236+
EVP_SHA3_224: true,
237+
EVP_SHA3_256: true,
238+
EVP_SHA3_384: true,
239+
EVP_SHA3_512: true,
240+
EVP_SHAKE128: true,
241+
EVP_SHAKE256: true,
242+
EVP_SM3: true,
243+
EVP_WHIRLPOOL: true,
244+
}
245+
246+
var hashFunctionsOpenSSLv110 = map[EVP_MD]bool{
247+
EVP_BLAKE2B_512: true,
248+
EVP_BLAKE2S_256: true,
249+
EVP_GOST: true,
250+
EVP_MD2: true,
251+
EVP_MD4: true,
252+
EVP_MD5: true,
253+
EVP_RIPEMD160: true,
254+
EVP_SHA1: true,
255+
EVP_SHA224: true,
256+
EVP_SHA256: true,
257+
EVP_SHA384: true,
258+
EVP_SHA512: true,
259+
EVP_SHA512_224: false,
260+
EVP_SHA512_256: false,
261+
EVP_SHA3_224: false,
262+
EVP_SHA3_256: false,
263+
EVP_SHA3_384: false,
264+
EVP_SHA3_512: false,
265+
EVP_SHAKE128: false,
266+
EVP_SHAKE256: false,
267+
EVP_SM3: false,
268+
EVP_WHIRLPOOL: true,
269+
}
270+
271+
var hashFunctionsOpenSSLv102 = map[EVP_MD]bool{
272+
EVP_BLAKE2B_512: false,
273+
EVP_BLAKE2S_256: false,
274+
EVP_GOST: false,
275+
EVP_MD2: false,
276+
EVP_MD4: true,
277+
EVP_MD5: true,
278+
EVP_RIPEMD160: true,
279+
EVP_SHA1: true,
280+
EVP_SHA224: true,
281+
EVP_SHA256: true,
282+
EVP_SHA384: true,
283+
EVP_SHA512: true,
284+
EVP_SHA512_224: false,
285+
EVP_SHA512_256: false,
286+
EVP_SHA3_224: false,
287+
EVP_SHA3_256: false,
288+
EVP_SHA3_384: false,
289+
EVP_SHA3_512: false,
290+
EVP_SHAKE128: false,
291+
EVP_SHAKE256: false,
292+
EVP_SM3: false,
293+
EVP_WHIRLPOOL: true,
294+
}
295+
296+
// Supported checks if this hash function is supported by the installed version of OpenSSL
297+
func (evp EVP_MD) Supported() bool {
298+
if C.OPENSSL_VERSION_NUMBER >= 0x1010100f {
299+
return hashFunctionsOpenSSLv111[evp]
300+
} else if C.OPENSSL_VERSION_NUMBER >= 0x1010000f {
301+
return hashFunctionsOpenSSLv110[evp]
302+
}
303+
return hashFunctionsOpenSSLv102[evp]
304+
}
305+
306+
// c returns pointer to the struct that is used during digest initialization
307+
func (evp EVP_MD) c() (evpMD *C.EVP_MD) {
185308
switch evp {
186309
case EVP_BLAKE2B_512:
187310
evpMD = C.X_EVP_blake2b512()

shim.c

-29
Original file line numberDiff line numberDiff line change
@@ -598,35 +598,6 @@ const EVP_MD *X_EVP_md_null() {
598598
return EVP_md_null();
599599
}
600600

601-
/*
602-
1.1.1 -> 0x1010100fL
603-
1.1.0 -> 0x1010000fL
604-
605-
Digest 1.0.2 1.1.0 1.1.1
606-
BLAKE2B512 - + +
607-
BLAKE2S256 - + +
608-
GOST - + +
609-
MD2 - + +
610-
MD4 + + +
611-
MD5 + + +
612-
RIPEMD160 + + +
613-
SHA1 + + +
614-
SHA224 + + +
615-
SHA256 + + +
616-
SHA384 + + +
617-
SHA512 + + +
618-
SHA512-224 - - +
619-
SHA512-256 - - +
620-
SHA3-224 - - +
621-
SHA3-256 - - +
622-
SHA3-384 - - +
623-
SHA3-512 - - +
624-
SHAKE128 - - +
625-
SHAKE256 - - +
626-
SM3 - - +
627-
WHIRLPOOL + + +
628-
*/
629-
630601
const EVP_MD *X_EVP_blake2b512() {
631602
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
632603
return EVP_blake2b512();

0 commit comments

Comments
 (0)