-
Notifications
You must be signed in to change notification settings - Fork 12
/
ecdsa.go
211 lines (188 loc) · 5.88 KB
/
ecdsa.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
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"crypto"
"errors"
"runtime"
)
type PrivateKeyECDSA struct {
// _pkey MUST NOT be accessed directly. Instead, use the withKey method.
_pkey C.GO_EVP_PKEY_PTR
}
func (k *PrivateKeyECDSA) finalize() {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
func (k *PrivateKeyECDSA) withKey(f func(C.GO_EVP_PKEY_PTR) C.int) C.int {
defer runtime.KeepAlive(k)
return f(k._pkey)
}
type PublicKeyECDSA struct {
// _pkey MUST NOT be accessed directly. Instead, use the withKey method.
_pkey C.GO_EVP_PKEY_PTR
}
func (k *PublicKeyECDSA) finalize() {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
func (k *PublicKeyECDSA) withKey(f func(C.GO_EVP_PKEY_PTR) C.int) C.int {
defer runtime.KeepAlive(k)
return f(k._pkey)
}
var errUnknownCurve = errors.New("openssl: unknown elliptic curve")
func NewPublicKeyECDSA(curve string, x, y BigInt) (*PublicKeyECDSA, error) {
pkey, err := newECDSAKey(curve, x, y, nil)
if err != nil {
return nil, err
}
k := &PublicKeyECDSA{_pkey: pkey}
runtime.SetFinalizer(k, (*PublicKeyECDSA).finalize)
return k, nil
}
func NewPrivateKeyECDSA(curve string, x, y, d BigInt) (*PrivateKeyECDSA, error) {
pkey, err := newECDSAKey(curve, x, y, d)
if err != nil {
return nil, err
}
k := &PrivateKeyECDSA{_pkey: pkey}
runtime.SetFinalizer(k, (*PrivateKeyECDSA).finalize)
return k, nil
}
func GenerateKeyECDSA(curve string) (x, y, d BigInt, err error) {
// Generate the private key.
pkey, err := generateEVPPKey(C.GO_EVP_PKEY_EC, 0, curve)
if err != nil {
return nil, nil, nil, err
}
defer C.go_openssl_EVP_PKEY_free(pkey)
var bx, by, bd C.GO_BIGNUM_PTR
defer func() {
C.go_openssl_BN_free(bx)
C.go_openssl_BN_free(by)
}()
switch vMajor {
case 1:
// Retrieve the internal EC_KEY, which holds the X, Y, and D coordinates.
key := getECKey(pkey)
group := C.go_openssl_EC_KEY_get0_group(key)
pt := C.go_openssl_EC_KEY_get0_public_key(key)
// Allocate two big numbers to store the X and Y coordinates.
bx, by = C.go_openssl_BN_new(), C.go_openssl_BN_new()
if bx == nil || by == nil {
return nil, nil, nil, newOpenSSLError("BN_new failed")
}
// Get X and Y.
if C.go_openssl_EC_POINT_get_affine_coordinates_GFp(group, pt, bx, by, nil) == 0 {
return nil, nil, nil, newOpenSSLError("EC_POINT_get_affine_coordinates_GFp failed")
}
// Get Z. We don't need to free it, get0 does not increase the reference count.
bd = C.go_openssl_EC_KEY_get0_private_key(key)
case 3:
if C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_EC_PUB_X, &bx) != 1 ||
C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_EC_PUB_Y, &by) != 1 ||
C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_PRIV_KEY, &bd) != 1 {
return nil, nil, nil, newOpenSSLError("EVP_PKEY_get_bn_param")
}
defer C.go_openssl_BN_clear_free(bd)
default:
panic(errUnsupportedVersion())
}
// Get D.
return bnToBig(bx), bnToBig(by), bnToBig(bd), nil
}
func SignMarshalECDSA(priv *PrivateKeyECDSA, hash []byte) ([]byte, error) {
return evpSign(priv.withKey, 0, 0, 0, hash)
}
func HashSignECDSA(priv *PrivateKeyECDSA, h crypto.Hash, msg []byte) ([]byte, error) {
return evpHashSign(priv.withKey, h, msg)
}
func VerifyECDSA(pub *PublicKeyECDSA, hash []byte, sig []byte) bool {
return evpVerify(pub.withKey, 0, 0, 0, sig, hash) == nil
}
func HashVerifyECDSA(pub *PublicKeyECDSA, h crypto.Hash, msg, sig []byte) bool {
return evpHashVerify(pub.withKey, h, msg, sig) == nil
}
func newECDSAKey(curve string, x, y, d BigInt) (C.GO_EVP_PKEY_PTR, error) {
nid, err := curveNID(curve)
if err != nil {
return nil, err
}
var bx, by, bd C.GO_BIGNUM_PTR
defer func() {
C.go_openssl_BN_free(bx)
C.go_openssl_BN_free(by)
C.go_openssl_BN_clear_free(bd)
}()
bx = bigToBN(x)
by = bigToBN(y)
bd = bigToBN(d)
if bx == nil || by == nil || (d != nil && bd == nil) {
return nil, newOpenSSLError("BN_lebin2bn failed")
}
switch vMajor {
case 1:
return newECDSAKey1(nid, bx, by, bd)
case 3:
return newECDSAKey3(nid, bx, by, bd)
default:
panic(errUnsupportedVersion())
}
}
func newECDSAKey1(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (pkey C.GO_EVP_PKEY_PTR, err error) {
checkMajorVersion(1)
key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
if key == nil {
return nil, newOpenSSLError("EC_KEY_new_by_curve_name failed")
}
defer func() {
if pkey == nil {
defer C.go_openssl_EC_KEY_free(key)
}
}()
if C.go_openssl_EC_KEY_set_public_key_affine_coordinates(key, bx, by) != 1 {
return nil, newOpenSSLError("EC_KEY_set_public_key_affine_coordinates failed")
}
if bd != nil && C.go_openssl_EC_KEY_set_private_key(key, bd) != 1 {
return nil, newOpenSSLError("EC_KEY_set_private_key failed")
}
return newEVPPKEY(key)
}
func newECDSAKey3(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (C.GO_EVP_PKEY_PTR, error) {
checkMajorVersion(3)
// Create the encoded public key public key from bx and by.
pubBytes, err := generateAndEncodeEcPublicKey(nid, func(group C.GO_EC_GROUP_PTR) (C.GO_EC_POINT_PTR, error) {
pt := C.go_openssl_EC_POINT_new(group)
if pt == nil {
return nil, newOpenSSLError("EC_POINT_new")
}
if C.go_openssl_EC_POINT_set_affine_coordinates(group, pt, bx, by, nil) != 1 {
C.go_openssl_EC_POINT_free(pt)
return nil, newOpenSSLError("EC_POINT_set_affine_coordinates")
}
return pt, nil
})
if err != nil {
return nil, err
}
// Construct the parameters.
bld, err := newParamBuilder()
if err != nil {
return nil, err
}
defer bld.finalize()
bld.addUTF8String(_OSSL_PKEY_PARAM_GROUP_NAME, C.go_openssl_OBJ_nid2sn(nid), 0)
bld.addOctetString(_OSSL_PKEY_PARAM_PUB_KEY, pubBytes)
var selection C.int
if bd != nil {
bld.addBN(_OSSL_PKEY_PARAM_PRIV_KEY, bd)
selection = C.GO_EVP_PKEY_KEYPAIR
} else {
selection = C.GO_EVP_PKEY_PUBLIC_KEY
}
params, err := bld.build()
if err != nil {
return nil, err
}
defer C.go_openssl_OSSL_PARAM_free(params)
return newEvpFromParams(C.GO_EVP_PKEY_EC, selection, params)
}