@@ -15,69 +15,79 @@ import (
15
15
)
16
16
17
17
type PublicKeyECDH struct {
18
+ p * publicKeyECDH
19
+ }
20
+
21
+ type publicKeyECDH struct {
18
22
curve string
19
23
key * C.GO_EC_POINT
20
24
group * C.GO_EC_GROUP
21
25
bytes []byte
22
26
}
23
27
24
- func (k * PublicKeyECDH ) finalize () {
28
+ func (k * publicKeyECDH ) finalize () {
25
29
C ._goboringcrypto_EC_POINT_free (k .key )
26
30
}
27
31
28
32
type PrivateKeyECDH struct {
33
+ p * privateKeyECDH
34
+ }
35
+
36
+ type privateKeyECDH struct {
29
37
curve string
30
38
key * C.GO_EC_KEY
31
39
}
32
40
33
- func (k * PrivateKeyECDH ) finalize () {
41
+ func (k * PrivateKeyECDH ) Valid () bool { return k .p != nil }
42
+
43
+ func (k * privateKeyECDH ) finalize () {
34
44
C ._goboringcrypto_EC_KEY_free (k .key )
35
45
}
36
46
37
- func NewPublicKeyECDH (curve string , bytes []byte ) (* PublicKeyECDH , error ) {
47
+ func NewPublicKeyECDH (curve string , bytes []byte ) (PublicKeyECDH , error ) {
38
48
if len (bytes ) < 1 {
39
- return nil , errors .New ("NewPublicKeyECDH: missing key" )
49
+ return PublicKeyECDH {} , errors .New ("NewPublicKeyECDH: missing key" )
40
50
}
41
51
42
52
nid , err := curveNID (curve )
43
53
if err != nil {
44
- return nil , err
54
+ return PublicKeyECDH {} , err
45
55
}
46
56
47
57
group := C ._goboringcrypto_EC_GROUP_new_by_curve_name (nid )
48
58
if group == nil {
49
- return nil , fail ("EC_GROUP_new_by_curve_name" )
59
+ return PublicKeyECDH {} , fail ("EC_GROUP_new_by_curve_name" )
50
60
}
51
61
defer C ._goboringcrypto_EC_GROUP_free (group )
52
62
key := C ._goboringcrypto_EC_POINT_new (group )
53
63
if key == nil {
54
- return nil , fail ("EC_POINT_new" )
64
+ return PublicKeyECDH {} , fail ("EC_POINT_new" )
55
65
}
56
66
ok := C ._goboringcrypto_EC_POINT_oct2point (group , key , (* C .uint8_t )(unsafe .Pointer (& bytes [0 ])), C .size_t (len (bytes )), nil ) != 0
57
67
if ! ok {
58
68
C ._goboringcrypto_EC_POINT_free (key )
59
- return nil , errors .New ("point not on curve" )
69
+ return PublicKeyECDH {} , errors .New ("point not on curve" )
60
70
}
61
71
62
- k := & PublicKeyECDH {curve , key , group , append ([]byte (nil ), bytes ... )}
72
+ k := & publicKeyECDH {curve , key , group , append ([]byte (nil ), bytes ... )}
63
73
// Note: Because of the finalizer, any time k.key is passed to cgo,
64
74
// that call must be followed by a call to runtime.KeepAlive(k),
65
75
// to make sure k is not collected (and finalized) before the cgo
66
76
// call returns.
67
- runtime .SetFinalizer (k , (* PublicKeyECDH ).finalize )
68
- return k , nil
77
+ runtime .SetFinalizer (k , (* publicKeyECDH ).finalize )
78
+ return PublicKeyECDH { p : k } , nil
69
79
}
70
80
71
- func (k * PublicKeyECDH ) Bytes () []byte { return k .bytes }
81
+ func (k * PublicKeyECDH ) Bytes () []byte { return k .p . bytes }
72
82
73
- func NewPrivateKeyECDH (curve string , bytes []byte ) (* PrivateKeyECDH , error ) {
83
+ func NewPrivateKeyECDH (curve string , bytes []byte ) (PrivateKeyECDH , error ) {
74
84
nid , err := curveNID (curve )
75
85
if err != nil {
76
- return nil , err
86
+ return PrivateKeyECDH {} , err
77
87
}
78
88
key := C ._goboringcrypto_EC_KEY_new_by_curve_name (nid )
79
89
if key == nil {
80
- return nil , fail ("EC_KEY_new_by_curve_name" )
90
+ return PrivateKeyECDH {} , fail ("EC_KEY_new_by_curve_name" )
81
91
}
82
92
b := bytesToBN (bytes )
83
93
ok := b != nil && C ._goboringcrypto_EC_KEY_set_private_key (key , b ) != 0
@@ -86,42 +96,42 @@ func NewPrivateKeyECDH(curve string, bytes []byte) (*PrivateKeyECDH, error) {
86
96
}
87
97
if ! ok {
88
98
C ._goboringcrypto_EC_KEY_free (key )
89
- return nil , fail ("EC_KEY_set_private_key" )
99
+ return PrivateKeyECDH {} , fail ("EC_KEY_set_private_key" )
90
100
}
91
- k := & PrivateKeyECDH {curve , key }
101
+ k := & privateKeyECDH {curve , key }
92
102
// Note: Same as in NewPublicKeyECDH regarding finalizer and KeepAlive.
93
- runtime .SetFinalizer (k , (* PrivateKeyECDH ).finalize )
94
- return k , nil
103
+ runtime .SetFinalizer (k , (* privateKeyECDH ).finalize )
104
+ return PrivateKeyECDH { p : k } , nil
95
105
}
96
106
97
- func (k * PrivateKeyECDH ) PublicKey () (* PublicKeyECDH , error ) {
107
+ func (k * PrivateKeyECDH ) PublicKey () (PublicKeyECDH , error ) {
98
108
defer runtime .KeepAlive (k )
99
109
100
- group := C ._goboringcrypto_EC_KEY_get0_group (k .key )
110
+ group := C ._goboringcrypto_EC_KEY_get0_group (k .p . key )
101
111
if group == nil {
102
- return nil , fail ("EC_KEY_get0_group" )
112
+ return PublicKeyECDH {} , fail ("EC_KEY_get0_group" )
103
113
}
104
- kbig := C ._goboringcrypto_EC_KEY_get0_private_key (k .key )
114
+ kbig := C ._goboringcrypto_EC_KEY_get0_private_key (k .p . key )
105
115
if kbig == nil {
106
- return nil , fail ("EC_KEY_get0_private_key" )
116
+ return PublicKeyECDH {} , fail ("EC_KEY_get0_private_key" )
107
117
}
108
118
pt := C ._goboringcrypto_EC_POINT_new (group )
109
119
if pt == nil {
110
- return nil , fail ("EC_POINT_new" )
120
+ return PublicKeyECDH {} , fail ("EC_POINT_new" )
111
121
}
112
122
if C ._goboringcrypto_EC_POINT_mul (group , pt , kbig , nil , nil , nil ) == 0 {
113
123
C ._goboringcrypto_EC_POINT_free (pt )
114
- return nil , fail ("EC_POINT_mul" )
124
+ return PublicKeyECDH {} , fail ("EC_POINT_mul" )
115
125
}
116
- bytes , err := pointBytesECDH (k .curve , group , pt )
126
+ bytes , err := pointBytesECDH (k .p . curve , group , pt )
117
127
if err != nil {
118
128
C ._goboringcrypto_EC_POINT_free (pt )
119
- return nil , err
129
+ return PublicKeyECDH {} , err
120
130
}
121
- pub := & PublicKeyECDH { k .curve , pt , group , bytes }
131
+ pub := & publicKeyECDH { k . p .curve , pt , group , bytes }
122
132
// Note: Same as in NewPublicKeyECDH regarding finalizer and KeepAlive.
123
- runtime .SetFinalizer (pub , (* PublicKeyECDH ).finalize )
124
- return pub , nil
133
+ runtime .SetFinalizer (pub , (* publicKeyECDH ).finalize )
134
+ return PublicKeyECDH { p : pub } , nil
125
135
}
126
136
127
137
func pointBytesECDH (curve string , group * C.GO_EC_GROUP , pt * C.GO_EC_POINT ) ([]byte , error ) {
@@ -133,12 +143,12 @@ func pointBytesECDH(curve string, group *C.GO_EC_GROUP, pt *C.GO_EC_POINT) ([]by
133
143
return out , nil
134
144
}
135
145
136
- func ECDH (priv * PrivateKeyECDH , pub * PublicKeyECDH ) ([]byte , error ) {
137
- group := C ._goboringcrypto_EC_KEY_get0_group (priv .key )
146
+ func ECDH (priv PrivateKeyECDH , pub PublicKeyECDH ) ([]byte , error ) {
147
+ group := C ._goboringcrypto_EC_KEY_get0_group (priv .p . key )
138
148
if group == nil {
139
149
return nil , fail ("EC_KEY_get0_group" )
140
150
}
141
- privBig := C ._goboringcrypto_EC_KEY_get0_private_key (priv .key )
151
+ privBig := C ._goboringcrypto_EC_KEY_get0_private_key (priv .p . key )
142
152
if privBig == nil {
143
153
return nil , fail ("EC_KEY_get0_private_key" )
144
154
}
@@ -147,10 +157,10 @@ func ECDH(priv *PrivateKeyECDH, pub *PublicKeyECDH) ([]byte, error) {
147
157
return nil , fail ("EC_POINT_new" )
148
158
}
149
159
defer C ._goboringcrypto_EC_POINT_free (pt )
150
- if C ._goboringcrypto_EC_POINT_mul (group , pt , nil , pub .key , privBig , nil ) == 0 {
160
+ if C ._goboringcrypto_EC_POINT_mul (group , pt , nil , pub .p . key , privBig , nil ) == 0 {
151
161
return nil , fail ("EC_POINT_mul" )
152
162
}
153
- out , err := xCoordBytesECDH (priv .curve , group , pt )
163
+ out , err := xCoordBytesECDH (priv .p . curve , group , pt )
154
164
if err != nil {
155
165
return nil , err
156
166
}
@@ -187,38 +197,38 @@ func curveSize(curve string) int {
187
197
}
188
198
}
189
199
190
- func GenerateKeyECDH (curve string ) (* PrivateKeyECDH , []byte , error ) {
200
+ func GenerateKeyECDH (curve string ) (PrivateKeyECDH , []byte , error ) {
191
201
nid , err := curveNID (curve )
192
202
if err != nil {
193
- return nil , nil , err
203
+ return PrivateKeyECDH {} , nil , err
194
204
}
195
205
key := C ._goboringcrypto_EC_KEY_new_by_curve_name (nid )
196
206
if key == nil {
197
- return nil , nil , fail ("EC_KEY_new_by_curve_name" )
207
+ return PrivateKeyECDH {} , nil , fail ("EC_KEY_new_by_curve_name" )
198
208
}
199
209
if C ._goboringcrypto_EC_KEY_generate_key_fips (key ) == 0 {
200
210
C ._goboringcrypto_EC_KEY_free (key )
201
- return nil , nil , fail ("EC_KEY_generate_key_fips" )
211
+ return PrivateKeyECDH {} , nil , fail ("EC_KEY_generate_key_fips" )
202
212
}
203
213
204
214
group := C ._goboringcrypto_EC_KEY_get0_group (key )
205
215
if group == nil {
206
216
C ._goboringcrypto_EC_KEY_free (key )
207
- return nil , nil , fail ("EC_KEY_get0_group" )
217
+ return PrivateKeyECDH {} , nil , fail ("EC_KEY_get0_group" )
208
218
}
209
219
b := C ._goboringcrypto_EC_KEY_get0_private_key (key )
210
220
if b == nil {
211
221
C ._goboringcrypto_EC_KEY_free (key )
212
- return nil , nil , fail ("EC_KEY_get0_private_key" )
222
+ return PrivateKeyECDH {} , nil , fail ("EC_KEY_get0_private_key" )
213
223
}
214
224
bytes , err := bigBytesECDH (curve , b )
215
225
if err != nil {
216
226
C ._goboringcrypto_EC_KEY_free (key )
217
- return nil , nil , err
227
+ return PrivateKeyECDH {} , nil , err
218
228
}
219
229
220
- k := & PrivateKeyECDH {curve , key }
230
+ k := & privateKeyECDH {curve , key }
221
231
// Note: Same as in NewPublicKeyECDH regarding finalizer and KeepAlive.
222
- runtime .SetFinalizer (k , (* PrivateKeyECDH ).finalize )
223
- return k , bytes , nil
232
+ runtime .SetFinalizer (k , (* privateKeyECDH ).finalize )
233
+ return PrivateKeyECDH { p : k } , bytes , nil
224
234
}
0 commit comments