-
Notifications
You must be signed in to change notification settings - Fork 14
/
ecdh.go
223 lines (209 loc) · 6.42 KB
/
ecdh.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//go:build linux && !android
// +build linux,!android
package openssl
// #include "goopenssl.h"
import "C"
import (
"errors"
"runtime"
)
type PublicKeyECDH struct {
_pkey C.GO_EVP_PKEY_PTR
bytes []byte
// priv is only set when PublicKeyECDH is derived from a private key,
// in which case priv's finalizer is responsible for freeing _pkey.
// This ensures priv is not finalized while the public key is alive,
// which could cause use-after-free and double-free behavior.
//
// We could avoid this altogether if using EVP_PKEY_up_ref
// when instantiating a derived public key, unfortunately
// it is not available on OpenSSL 1.0.2.
priv *PrivateKeyECDH
}
func (k *PublicKeyECDH) finalize() {
if k.priv == nil {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
}
type PrivateKeyECDH struct {
_pkey C.GO_EVP_PKEY_PTR
}
func (k *PrivateKeyECDH) finalize() {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
func NewPublicKeyECDH(curve string, bytes []byte) (*PublicKeyECDH, error) {
if len(bytes) < 1 {
return nil, errors.New("NewPublicKeyECDH: missing key")
}
nid, err := curveNID(curve)
if err != nil {
return nil, err
}
key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
if key == nil {
return nil, newOpenSSLError("EC_KEY_new_by_curve_name")
}
var k *PublicKeyECDH
defer func() {
if k == nil {
C.go_openssl_EC_KEY_free(key)
}
}()
if vMajor == 1 && vMinor == 0 {
// EC_KEY_oct2key does not exist on OpenSSL 1.0.2,
// we have to simulate it.
group := C.go_openssl_EC_KEY_get0_group(key)
pt := C.go_openssl_EC_POINT_new(group)
if pt == nil {
return nil, newOpenSSLError("EC_POINT_new")
}
defer C.go_openssl_EC_POINT_free(pt)
if C.go_openssl_EC_POINT_oct2point(group, pt, base(bytes), C.size_t(len(bytes)), nil) != 1 {
return nil, errors.New("point not on curve")
}
if C.go_openssl_EC_KEY_set_public_key(key, pt) != 1 {
return nil, newOpenSSLError("EC_KEY_set_public_key")
}
} else {
if C.go_openssl_EC_KEY_oct2key(key, base(bytes), C.size_t(len(bytes)), nil) != 1 {
return nil, newOpenSSLError("EC_KEY_oct2key")
}
}
pkey, err := newEVPPKEY(key)
if err != nil {
return nil, err
}
k = &PublicKeyECDH{pkey, append([]byte(nil), bytes...), nil}
runtime.SetFinalizer(k, (*PublicKeyECDH).finalize)
return k, nil
}
func (k *PublicKeyECDH) Bytes() []byte { return k.bytes }
func NewPrivateKeyECDH(curve string, bytes []byte) (*PrivateKeyECDH, error) {
nid, err := curveNID(curve)
if err != nil {
return nil, err
}
b := bytesToBN(bytes)
if b == nil {
return nil, newOpenSSLError("BN_bin2bn")
}
defer C.go_openssl_BN_free(b)
key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
if key == nil {
return nil, newOpenSSLError("EC_KEY_new_by_curve_name")
}
var pkey C.GO_EVP_PKEY_PTR
defer func() {
if pkey == nil {
C.go_openssl_EC_KEY_free(key)
}
}()
if C.go_openssl_EC_KEY_set_private_key(key, b) != 1 {
return nil, newOpenSSLError("EC_KEY_set_private_key")
}
pkey, err = newEVPPKEY(key)
if err != nil {
return nil, err
}
k := &PrivateKeyECDH{pkey}
runtime.SetFinalizer(k, (*PrivateKeyECDH).finalize)
return k, nil
}
func (k *PrivateKeyECDH) PublicKey() (*PublicKeyECDH, error) {
defer runtime.KeepAlive(k)
key := C.go_openssl_EVP_PKEY_get1_EC_KEY(k._pkey)
if key == nil {
return nil, newOpenSSLError("EVP_PKEY_get1_EC_KEY")
}
defer C.go_openssl_EC_KEY_free(key)
group := C.go_openssl_EC_KEY_get0_group(key)
if group == nil {
return nil, newOpenSSLError("EC_KEY_get0_group")
}
pt := C.go_openssl_EC_KEY_get0_public_key(key)
if pt == nil {
// The public key will be nil if k has been generated using
// NewPrivateKeyECDH instead of GenerateKeyECDH.
//
// OpenSSL does not expose any method to generate the public
// key from the private key [1], so we have to calculate it here
// https://github.com/openssl/openssl/issues/18437#issuecomment-1144717206
pt = C.go_openssl_EC_POINT_new(group)
if pt == nil {
return nil, newOpenSSLError("EC_POINT_new")
}
defer C.go_openssl_EC_POINT_free(pt)
kbig := C.go_openssl_EC_KEY_get0_private_key(key)
if C.go_openssl_EC_POINT_mul(group, pt, kbig, nil, nil, nil) == 0 {
return nil, newOpenSSLError("EC_POINT_mul")
}
}
n := C.go_openssl_EC_POINT_point2oct(group, pt, C.GO_POINT_CONVERSION_UNCOMPRESSED, nil, 0, nil)
if n == 0 {
return nil, newOpenSSLError("EC_POINT_point2oct")
}
bytes := make([]byte, n)
n = C.go_openssl_EC_POINT_point2oct(group, pt, C.GO_POINT_CONVERSION_UNCOMPRESSED, base(bytes), C.size_t(len(bytes)), nil)
if int(n) != len(bytes) {
return nil, newOpenSSLError("EC_POINT_point2oct")
}
pub := &PublicKeyECDH{k._pkey, bytes, k}
// Note: Same as in NewPublicKeyECDH regarding finalizer and KeepAlive.
runtime.SetFinalizer(pub, (*PublicKeyECDH).finalize)
return pub, nil
}
func ECDH(priv *PrivateKeyECDH, pub *PublicKeyECDH) ([]byte, error) {
defer runtime.KeepAlive(priv)
defer runtime.KeepAlive(pub)
ctx := C.go_openssl_EVP_PKEY_CTX_new(priv._pkey, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_derive_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive_init")
}
if C.go_openssl_EVP_PKEY_derive_set_peer(ctx, pub._pkey) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive_set_peer")
}
var outLen C.size_t
if C.go_openssl_EVP_PKEY_derive(ctx, nil, &outLen) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive_init")
}
out := make([]byte, outLen)
if C.go_openssl_EVP_PKEY_derive(ctx, base(out), &outLen) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive_init")
}
return out, nil
}
func GenerateKeyECDH(curve string) (*PrivateKeyECDH, []byte, error) {
pkey, err := generateEVPPKey(C.GO_EVP_PKEY_EC, 0, curve)
if err != nil {
return nil, nil, err
}
var k *PrivateKeyECDH
defer func() {
if k == nil {
C.go_openssl_EVP_PKEY_free(pkey)
}
}()
key := C.go_openssl_EVP_PKEY_get1_EC_KEY(pkey)
if key == nil {
return nil, nil, newOpenSSLError("EVP_PKEY_get1_EC_KEY")
}
defer C.go_openssl_EC_KEY_free(key)
b := C.go_openssl_EC_KEY_get0_private_key(key)
if b == nil {
return nil, nil, newOpenSSLError("EC_KEY_get0_private_key")
}
bits := C.go_openssl_EVP_PKEY_get_bits(pkey)
out := make([]byte, (bits+7)/8)
if C.go_openssl_BN_bn2binpad(b, base(out), C.int(len(out))) == 0 {
return nil, nil, newOpenSSLError("BN_bn2binpad")
}
k = &PrivateKeyECDH{pkey}
runtime.SetFinalizer(k, (*PrivateKeyECDH).finalize)
return k, out, nil
}