-
Notifications
You must be signed in to change notification settings - Fork 13
/
hmac.go
274 lines (247 loc) · 6.69 KB
/
hmac.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
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"hash"
"runtime"
"sync"
"unsafe"
)
// NewHMAC returns a new HMAC using OpenSSL.
// The function h must return a hash implemented by
// OpenSSL (for example, h could be openssl.NewSHA256).
// If h is not recognized, NewHMAC returns nil.
func NewHMAC(fh func() hash.Hash, key []byte) hash.Hash {
h, _ := hashFuncHash(fh)
md := hashToMD(h)
if md == nil {
return nil
}
if len(key) == 0 {
// This is supported in OpenSSL/Standard lib and as such
// we must support it here. When using HMAC with a null key
// HMAC_Init will try and reuse the key from the ctx. This is
// not the behavior previously implemented, so as a workaround
// we pass an "empty" key.
key = make([]byte, C.GO_EVP_MAX_MD_SIZE)
}
hmac := &opensslHMAC{
size: h.Size(),
blockSize: h.BlockSize(),
}
switch vMajor {
case 1:
ctx := newHMAC1(key, md)
if ctx.ctx == nil {
return nil
}
hmac.ctx1 = ctx
case 3:
ctx := newHMAC3(key, md)
if ctx.ctx == nil {
return nil
}
hmac.ctx3 = ctx
default:
panic(errUnsupportedVersion())
}
runtime.SetFinalizer(hmac, (*opensslHMAC).finalize)
return hmac
}
// hmacCtx3 is used for OpenSSL 1.
type hmacCtx1 struct {
ctx C.GO_HMAC_CTX_PTR
}
// hmacCtx3 is used for OpenSSL 3.
type hmacCtx3 struct {
ctx C.GO_EVP_MAC_CTX_PTR
key []byte // only set for OpenSSL 3.0.0, 3.0.1, and 3.0.2.
}
type opensslHMAC struct {
ctx1 hmacCtx1
ctx3 hmacCtx3
size int
blockSize int
sum []byte
}
func newHMAC1(key []byte, md C.GO_EVP_MD_PTR) hmacCtx1 {
ctx := hmacCtxNew()
if ctx == nil {
panic("openssl: EVP_MAC_CTX_new failed")
}
if C.go_openssl_HMAC_Init_ex(ctx, unsafe.Pointer(&key[0]), C.int(len(key)), md, nil) == 0 {
panic(newOpenSSLError("HMAC_Init_ex failed"))
}
return hmacCtx1{ctx}
}
var hmacDigestsSupported sync.Map
var fetchHMAC3 = sync.OnceValue(func() C.GO_EVP_MAC_PTR {
name := C.CString("HMAC")
mac := C.go_openssl_EVP_MAC_fetch(nil, name, nil)
C.free(unsafe.Pointer(name))
if mac == nil {
panic("openssl: HMAC not supported")
}
return mac
})
func buildHMAC3Params(digest *C.char) (C.GO_OSSL_PARAM_PTR, error) {
bld, err := newParamBuilder()
if err != nil {
return nil, err
}
defer bld.finalize()
bld.addUTF8String(_OSSL_MAC_PARAM_DIGEST, digest, 0)
return bld.build()
}
func isHMAC3DigestSupported(digest string) bool {
if v, ok := hmacDigestsSupported.Load(digest); ok {
return v.(bool)
}
ctx := C.go_openssl_EVP_MAC_CTX_new(fetchHMAC3())
if ctx == nil {
panic(newOpenSSLError("EVP_MAC_CTX_new"))
}
defer C.go_openssl_EVP_MAC_CTX_free(ctx)
cdigest := C.CString(digest)
defer C.free(unsafe.Pointer(cdigest))
params, err := buildHMAC3Params(cdigest)
if err != nil {
panic(err)
}
defer C.go_openssl_OSSL_PARAM_free(params)
supported := C.go_openssl_EVP_MAC_CTX_set_params(ctx, params) != 0
hmacDigestsSupported.Store(digest, supported)
return supported
}
func newHMAC3(key []byte, md C.GO_EVP_MD_PTR) hmacCtx3 {
digest := C.go_openssl_EVP_MD_get0_name(md)
if !isHMAC3DigestSupported(C.GoString(digest)) {
// The digest is not supported by the HMAC provider.
// Don't panic here so the Go standard library to
// fall back to the Go implementation.
// See https://github.com/golang-fips/openssl/issues/153.
return hmacCtx3{}
}
params, err := buildHMAC3Params(digest)
if err != nil {
panic(err)
}
defer C.go_openssl_OSSL_PARAM_free(params)
ctx := C.go_openssl_EVP_MAC_CTX_new(fetchHMAC3())
if ctx == nil {
panic(newOpenSSLError("EVP_MAC_CTX_new"))
}
if C.go_openssl_EVP_MAC_init(ctx, base(key), C.size_t(len(key)), params) == 0 {
C.go_openssl_EVP_MAC_CTX_free(ctx)
panic(newOpenSSLError("EVP_MAC_init"))
}
var hkey []byte
if vMinor == 0 && vPatch <= 2 {
// EVP_MAC_init only resets the ctx internal state if a key is passed
// when using OpenSSL 3.0.0, 3.0.1, and 3.0.2. Save a copy of the key
// in the context so Reset can use it later. New OpenSSL versions
// do not have this issue so it isn't necessary to save the key.
// See https://github.com/openssl/openssl/issues/17811.
hkey = make([]byte, len(key))
copy(hkey, key)
}
return hmacCtx3{ctx, hkey}
}
func (h *opensslHMAC) Reset() {
switch vMajor {
case 1:
if C.go_openssl_HMAC_Init_ex(h.ctx1.ctx, nil, 0, nil, nil) == 0 {
panic(newOpenSSLError("HMAC_Init_ex failed"))
}
case 3:
if C.go_openssl_EVP_MAC_init(h.ctx3.ctx, base(h.ctx3.key), C.size_t(len(h.ctx3.key)), nil) == 0 {
panic(newOpenSSLError("EVP_MAC_init failed"))
}
default:
panic(errUnsupportedVersion())
}
runtime.KeepAlive(h) // Next line will keep h alive too; just making doubly sure.
h.sum = nil
}
func (h *opensslHMAC) finalize() {
switch vMajor {
case 1:
hmacCtxFree(h.ctx1.ctx)
case 3:
C.go_openssl_EVP_MAC_CTX_free(h.ctx3.ctx)
default:
panic(errUnsupportedVersion())
}
}
func (h *opensslHMAC) Write(p []byte) (int, error) {
if len(p) > 0 {
switch vMajor {
case 1:
C.go_openssl_HMAC_Update(h.ctx1.ctx, base(p), C.size_t(len(p)))
case 3:
C.go_openssl_EVP_MAC_update(h.ctx3.ctx, base(p), C.size_t(len(p)))
default:
panic(errUnsupportedVersion())
}
}
runtime.KeepAlive(h)
return len(p), nil
}
func (h *opensslHMAC) Size() int {
return h.size
}
func (h *opensslHMAC) BlockSize() int {
return h.blockSize
}
func (h *opensslHMAC) Sum(in []byte) []byte {
if h.sum == nil {
size := h.Size()
h.sum = make([]byte, size)
}
// Make copy of context because Go hash.Hash mandates
// that Sum has no effect on the underlying stream.
// In particular it is OK to Sum, then Write more, then Sum again,
// and the second Sum acts as if the first didn't happen.
switch vMajor {
case 1:
ctx2 := hmacCtxNew()
if ctx2 == nil {
panic("openssl: HMAC_CTX_new failed")
}
defer hmacCtxFree(ctx2)
if C.go_openssl_HMAC_CTX_copy(ctx2, h.ctx1.ctx) == 0 {
panic("openssl: HMAC_CTX_copy failed")
}
C.go_openssl_HMAC_Final(ctx2, base(h.sum), nil)
case 3:
ctx2 := C.go_openssl_EVP_MAC_CTX_dup(h.ctx3.ctx)
if ctx2 == nil {
panic("openssl: EVP_MAC_CTX_dup failed")
}
defer C.go_openssl_EVP_MAC_CTX_free(ctx2)
C.go_openssl_EVP_MAC_final(ctx2, base(h.sum), nil, C.size_t(len(h.sum)))
default:
panic(errUnsupportedVersion())
}
return append(in, h.sum...)
}
func hmacCtxNew() C.GO_HMAC_CTX_PTR {
if vMajor == 1 && vMinor == 0 {
// 0x120 is the sizeof value when building against OpenSSL 1.0.2 on Ubuntu 16.04.
ctx := (C.GO_HMAC_CTX_PTR)(C.malloc(0x120))
if ctx != nil {
C.go_openssl_HMAC_CTX_init(ctx)
}
return ctx
}
return C.go_openssl_HMAC_CTX_new()
}
func hmacCtxFree(ctx C.GO_HMAC_CTX_PTR) {
if vMajor == 1 && vMinor == 0 {
C.go_openssl_HMAC_CTX_cleanup(ctx)
C.free(unsafe.Pointer(ctx))
return
}
C.go_openssl_HMAC_CTX_free(ctx)
}