forked from spacemonkeygo/openssl
/
hmac.go
81 lines (68 loc) · 1.93 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
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build cgo
package openssl
/*
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "openssl/evp.h"
#include "openssl/hmac.h"
*/
import "C"
import (
"runtime"
"unsafe"
)
type HMAC struct {
ctx C.HMAC_CTX
engine *Engine
md *C.EVP_MD
}
func NewHMAC(key []byte, digestAlgorithm EVP_MD) (*HMAC, error) {
return NewHMACWithEngine(key, digestAlgorithm, nil)
}
func NewHMACWithEngine(key []byte, digestAlgorithm EVP_MD, e *Engine) (*HMAC, error) {
var md *C.EVP_MD = getDigestFunction(digestAlgorithm)
hmac := &HMAC{engine: e, md: md}
C.HMAC_CTX_init(&hmac.ctx)
C.HMAC_Init_ex(&hmac.ctx,
unsafe.Pointer(&key[0]),
C.int(len(key)),
md,
nil)
runtime.SetFinalizer(hmac, func(hmac *HMAC) { hmac.Close() })
return hmac, nil
}
func (h *HMAC) Close() {
C.HMAC_CTX_cleanup(&h.ctx)
}
func (s *HMAC) Write(data []byte) (n int, err error) {
if len(data) == 0 {
return 0, nil
}
C.HMAC_Update(&s.ctx, (*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data)))
return len(data), nil
}
func (h *HMAC) Reset() error {
C.HMAC_Init_ex(&h.ctx, nil, 0, nil, nil)
return nil
}
func (h *HMAC) Final() (result []byte, err error) {
mdLength := C.EVP_MD_size(h.md)
result = make([]byte, mdLength)
C.HMAC_Final(&h.ctx, (*C.uchar)(unsafe.Pointer(&result[0])), (*C.uint)(unsafe.Pointer(&mdLength)))
return result, h.Reset()
}