-
Notifications
You must be signed in to change notification settings - Fork 12
/
evp.go
519 lines (495 loc) · 16.2 KB
/
evp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"crypto"
"errors"
"hash"
"strconv"
"sync"
"unsafe"
)
// cacheMD is a cache of crypto.Hash to GO_EVP_MD_PTR.
var cacheMD sync.Map
// hashFuncHash calls fn() and returns its result.
// If fn() panics, the panic is recovered and returned as an error.
// This is used to avoid aborting the program when calling
// an unsupported hash function. It is the caller's responsibility
// to check the returned value.
func hashFuncHash(fn func() hash.Hash) (h hash.Hash, err error) {
defer func() {
r := recover()
if r == nil {
return
}
h = nil
switch e := r.(type) {
case error:
err = e
case string:
err = errors.New(e)
default:
err = errors.New("unsupported panic")
}
}()
return fn(), nil
}
// hashToMD converts a hash.Hash implementation from this package to a GO_EVP_MD_PTR.
func hashToMD(h hash.Hash) C.GO_EVP_MD_PTR {
var ch crypto.Hash
switch h.(type) {
case *sha1Hash, *sha1Marshal:
ch = crypto.SHA1
case *sha224Hash, *sha224Marshal:
ch = crypto.SHA224
case *sha256Hash, *sha256Marshal:
ch = crypto.SHA256
case *sha384Hash, *sha384Marshal:
ch = crypto.SHA384
case *sha512Hash, *sha512Marshal:
ch = crypto.SHA512
case *sha3_224Hash:
ch = crypto.SHA3_224
case *sha3_256Hash:
ch = crypto.SHA3_256
case *sha3_384Hash:
ch = crypto.SHA3_384
case *sha3_512Hash:
ch = crypto.SHA3_512
}
if ch != 0 {
return cryptoHashToMD(ch)
}
return nil
}
// hashFuncToMD converts a hash.Hash function to a GO_EVP_MD_PTR.
// See [hashFuncHash] for details on error handling.
func hashFuncToMD(fn func() hash.Hash) (C.GO_EVP_MD_PTR, error) {
h, err := hashFuncHash(fn)
if err != nil {
return nil, err
}
md := hashToMD(h)
if md == nil {
return nil, errors.New("unsupported hash function")
}
return md, nil
}
// cryptoHashToMD converts a crypto.Hash to a GO_EVP_MD_PTR.
func cryptoHashToMD(ch crypto.Hash) (md C.GO_EVP_MD_PTR) {
if v, ok := cacheMD.Load(ch); ok {
return v.(C.GO_EVP_MD_PTR)
}
defer func() {
if md != nil {
switch vMajor {
case 1:
// On OpenSSL 1 EVP_MD objects can be not-nil even
// when they are not supported. We need to pass the md
// to a EVP_MD_CTX to really know if they can be used.
ctx := C.go_openssl_EVP_MD_CTX_new()
if C.go_openssl_EVP_DigestInit_ex(ctx, md, nil) != 1 {
md = nil
}
C.go_openssl_EVP_MD_CTX_free(ctx)
case 3:
// On OpenSSL 3, directly operating on a EVP_MD object
// not created by EVP_MD_fetch has negative performance
// implications, as digest operations will have
// to fetch it on every call. Better to just fetch it once here.
md = C.go_openssl_EVP_MD_fetch(nil, C.go_openssl_EVP_MD_get0_name(md), nil)
default:
panic(errUnsupportedVersion())
}
}
cacheMD.Store(ch, md)
}()
// SupportsHash returns false for MD5SHA1 because we don't
// provide a hash.Hash implementation for it. Yet, it can
// still be used when signing/verifying with an RSA key.
if ch == crypto.MD5SHA1 {
if vMajor == 1 && vMinor == 0 {
return C.go_openssl_EVP_md5_sha1_backport()
} else {
return C.go_openssl_EVP_md5_sha1()
}
}
switch ch {
case crypto.MD4:
return C.go_openssl_EVP_md4()
case crypto.MD5:
return C.go_openssl_EVP_md5()
case crypto.SHA1:
return C.go_openssl_EVP_sha1()
case crypto.SHA224:
return C.go_openssl_EVP_sha224()
case crypto.SHA256:
return C.go_openssl_EVP_sha256()
case crypto.SHA384:
return C.go_openssl_EVP_sha384()
case crypto.SHA512:
return C.go_openssl_EVP_sha512()
case crypto.SHA3_224:
if versionAtOrAbove(1, 1, 1) {
return C.go_openssl_EVP_sha3_224()
}
case crypto.SHA3_256:
if versionAtOrAbove(1, 1, 1) {
return C.go_openssl_EVP_sha3_256()
}
case crypto.SHA3_384:
if versionAtOrAbove(1, 1, 1) {
return C.go_openssl_EVP_sha3_384()
}
case crypto.SHA3_512:
if versionAtOrAbove(1, 1, 1) {
return C.go_openssl_EVP_sha3_512()
}
}
return nil
}
func generateEVPPKey(id C.int, bits int, curve string) (C.GO_EVP_PKEY_PTR, error) {
if bits != 0 && curve != "" {
return nil, fail("incorrect generateEVPPKey parameters")
}
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(id, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_id failed")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_keygen_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen_init failed")
}
if bits != 0 {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, id, -1, C.GO_EVP_PKEY_CTRL_RSA_KEYGEN_BITS, C.int(bits), nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
if curve != "" {
nid, err := curveNID(curve)
if err != nil {
return nil, err
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, id, -1, C.GO_EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
var pkey C.GO_EVP_PKEY_PTR
if C.go_openssl_EVP_PKEY_keygen(ctx, &pkey) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen failed")
}
return pkey, nil
}
type withKeyFunc func(func(C.GO_EVP_PKEY_PTR) C.int) C.int
type initFunc func(C.GO_EVP_PKEY_CTX_PTR) error
type cryptFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, *C.size_t, *C.uchar, C.size_t) error
type verifyFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, C.size_t, *C.uchar, C.size_t) error
func setupEVP(withKey withKeyFunc, padding C.int,
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc) (_ C.GO_EVP_PKEY_CTX_PTR, err error) {
var ctx C.GO_EVP_PKEY_CTX_PTR
withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
return 1
})
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
}
defer func() {
if err != nil {
if ctx != nil {
C.go_openssl_EVP_PKEY_CTX_free(ctx)
ctx = nil
}
}
}()
if err := init(ctx); err != nil {
return nil, err
}
if padding == 0 {
return ctx, nil
}
// Each padding type has its own requirements in terms of when to apply the padding,
// so it can't be just set at this point.
setPadding := func() error {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_PADDING, padding, nil) != 1 {
return newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
return nil
}
switch padding {
case C.GO_RSA_PKCS1_OAEP_PADDING:
md := hashToMD(h)
if md == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
var mgfMD C.GO_EVP_MD_PTR
if mgfHash != nil {
// mgfHash is optional, but if it is set it must match a supported hash function.
mgfMD = hashToMD(mgfHash)
if mgfMD == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
}
// setPadding must happen before setting EVP_PKEY_CTRL_RSA_OAEP_MD.
if err := setPadding(); err != nil {
return nil, err
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_OAEP_MD, 0, unsafe.Pointer(md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
if mgfHash != nil {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_MGF1_MD, 0, unsafe.Pointer(mgfMD)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
// ctx takes ownership of label, so malloc a copy for OpenSSL to free.
// OpenSSL does not take ownership of the label if the length is zero,
// so better avoid the allocation.
var clabel *C.uchar
if len(label) > 0 {
clabel = (*C.uchar)(cryptoMalloc(len(label)))
copy((*[1 << 30]byte)(unsafe.Pointer(clabel))[:len(label)], label)
var err error
if vMajor == 3 {
ret := C.go_openssl_EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, unsafe.Pointer(clabel), C.int(len(label)))
if ret != 1 {
err = newOpenSSLError("EVP_PKEY_CTX_set0_rsa_oaep_label failed")
}
} else {
ret := C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_OAEP_LABEL, C.int(len(label)), unsafe.Pointer(clabel))
if ret != 1 {
err = newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
if err != nil {
cryptoFree(unsafe.Pointer(clabel))
return nil, err
}
}
case C.GO_RSA_PKCS1_PSS_PADDING:
md := cryptoHashToMD(ch)
if md == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_MD, 0, unsafe.Pointer(md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
// setPadding must happen after setting EVP_PKEY_CTRL_MD.
if err := setPadding(); err != nil {
return nil, err
}
if saltLen != 0 {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltLen, nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
case C.GO_RSA_PKCS1_PADDING:
if ch != 0 {
// We support unhashed messages.
md := cryptoHashToMD(ch)
if md == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, -1, -1, C.GO_EVP_PKEY_CTRL_MD, 0, unsafe.Pointer(md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
if err := setPadding(); err != nil {
return nil, err
}
}
default:
if err := setPadding(); err != nil {
return nil, err
}
}
return ctx, nil
}
func cryptEVP(withKey withKeyFunc, padding C.int,
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc, crypt cryptFunc, in []byte) ([]byte, error) {
ctx, err := setupEVP(withKey, padding, h, mgfHash, label, saltLen, ch, init)
if err != nil {
return nil, err
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
pkeySize := withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_PKEY_get_size(pkey)
})
outLen := C.size_t(pkeySize)
out := make([]byte, pkeySize)
if err := crypt(ctx, base(out), &outLen, base(in), C.size_t(len(in))); err != nil {
return nil, err
}
// The size returned by EVP_PKEY_get_size() is only preliminary and not exact,
// so the final contents of the out buffer may be smaller.
return out[:outLen], nil
}
func verifyEVP(withKey withKeyFunc, padding C.int,
h hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc, verify verifyFunc,
sig, in []byte) error {
ctx, err := setupEVP(withKey, padding, h, nil, label, saltLen, ch, init)
if err != nil {
return err
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
return verify(ctx, base(sig), C.size_t(len(sig)), base(in), C.size_t(len(in)))
}
func evpEncrypt(withKey withKeyFunc, padding C.int, h, mgfHash hash.Hash, label, msg []byte) ([]byte, error) {
encryptInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_encrypt_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_encrypt_init failed")
}
return nil
}
encrypt := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_encrypt(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_encrypt failed")
}
return nil
}
return cryptEVP(withKey, padding, h, mgfHash, label, 0, 0, encryptInit, encrypt, msg)
}
func evpDecrypt(withKey withKeyFunc, padding C.int, h, mgfHash hash.Hash, label, msg []byte) ([]byte, error) {
decryptInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_decrypt_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_decrypt_init failed")
}
return nil
}
decrypt := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_decrypt(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_decrypt failed")
}
return nil
}
return cryptEVP(withKey, padding, h, mgfHash, label, 0, 0, decryptInit, decrypt, msg)
}
func evpSign(withKey withKeyFunc, padding C.int, saltLen C.int, h crypto.Hash, hashed []byte) ([]byte, error) {
signtInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_sign_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_sign_init failed")
}
return nil
}
sign := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_sign(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_sign failed")
}
return nil
}
return cryptEVP(withKey, padding, nil, nil, nil, saltLen, h, signtInit, sign, hashed)
}
func evpVerify(withKey withKeyFunc, padding C.int, saltLen C.int, h crypto.Hash, sig, hashed []byte) error {
verifyInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_verify_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_verify_init failed")
}
return nil
}
verify := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_verify(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_verify failed")
}
return nil
}
return verifyEVP(withKey, padding, nil, nil, saltLen, h, verifyInit, verify, sig, hashed)
}
func evpHashSign(withKey withKeyFunc, h crypto.Hash, msg []byte) ([]byte, error) {
md := cryptoHashToMD(h)
if md == nil {
return nil, errors.New("unsupported hash function: " + strconv.Itoa(int(h)))
}
var out []byte
var outLen C.size_t
ctx := C.go_openssl_EVP_MD_CTX_new()
if ctx == nil {
return nil, newOpenSSLError("EVP_MD_CTX_new failed")
}
defer C.go_openssl_EVP_MD_CTX_free(ctx)
if withKey(func(key C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_DigestSignInit(ctx, nil, md, nil, key)
}) != 1 {
return nil, newOpenSSLError("EVP_DigestSignInit failed")
}
if C.go_openssl_EVP_DigestUpdate(ctx, unsafe.Pointer(base(msg)), C.size_t(len(msg))) != 1 {
return nil, newOpenSSLError("EVP_DigestUpdate failed")
}
// Obtain the signature length
if C.go_openssl_EVP_DigestSignFinal(ctx, nil, &outLen) != 1 {
return nil, newOpenSSLError("EVP_DigestSignFinal failed")
}
out = make([]byte, outLen)
// Obtain the signature
if C.go_openssl_EVP_DigestSignFinal(ctx, base(out), &outLen) != 1 {
return nil, newOpenSSLError("EVP_DigestSignFinal failed")
}
return out[:outLen], nil
}
func evpHashVerify(withKey withKeyFunc, h crypto.Hash, msg, sig []byte) error {
md := cryptoHashToMD(h)
if md == nil {
return errors.New("unsupported hash function: " + strconv.Itoa(int(h)))
}
ctx := C.go_openssl_EVP_MD_CTX_new()
if ctx == nil {
return newOpenSSLError("EVP_MD_CTX_new failed")
}
defer C.go_openssl_EVP_MD_CTX_free(ctx)
if withKey(func(key C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_DigestVerifyInit(ctx, nil, md, nil, key)
}) != 1 {
return newOpenSSLError("EVP_DigestVerifyInit failed")
}
if C.go_openssl_EVP_DigestUpdate(ctx, unsafe.Pointer(base(msg)), C.size_t(len(msg))) != 1 {
return newOpenSSLError("EVP_DigestUpdate failed")
}
if C.go_openssl_EVP_DigestVerifyFinal(ctx, base(sig), C.size_t(len(sig))) != 1 {
return newOpenSSLError("EVP_DigestVerifyFinal failed")
}
return nil
}
func newEVPPKEY(key C.GO_EC_KEY_PTR) (C.GO_EVP_PKEY_PTR, error) {
pkey := C.go_openssl_EVP_PKEY_new()
if pkey == nil {
return nil, newOpenSSLError("EVP_PKEY_new failed")
}
if C.go_openssl_EVP_PKEY_assign(pkey, C.GO_EVP_PKEY_EC, unsafe.Pointer(key)) != 1 {
C.go_openssl_EVP_PKEY_free(pkey)
return nil, newOpenSSLError("EVP_PKEY_assign failed")
}
return pkey, nil
}
// getECKey returns the EC_KEY from pkey.
// If pkey does not contain an EC_KEY it panics.
// The returned key should not be freed.
func getECKey(pkey C.GO_EVP_PKEY_PTR) (key C.GO_EC_KEY_PTR) {
if vMajor == 1 && vMinor == 0 {
if key0 := C.go_openssl_EVP_PKEY_get0(pkey); key0 != nil {
key = C.GO_EC_KEY_PTR(key0)
}
} else {
key = C.go_openssl_EVP_PKEY_get0_EC_KEY(pkey)
}
if key == nil {
panic("pkey does not contain an EC_KEY")
}
return key
}
func newEvpFromParams(id C.int, selection C.int, params C.GO_OSSL_PARAM_PTR) (C.GO_EVP_PKEY_PTR, error) {
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(id, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_id")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_fromdata_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_fromdata_init")
}
var pkey C.GO_EVP_PKEY_PTR
if C.go_openssl_EVP_PKEY_fromdata(ctx, &pkey, selection, params) != 1 {
return nil, newOpenSSLError("EVP_PKEY_fromdata")
}
return pkey, nil
}