-
Notifications
You must be signed in to change notification settings - Fork 13
/
dsa.go
324 lines (295 loc) · 8.8 KB
/
dsa.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"runtime"
"unsafe"
)
// SupportsDSA returns true if the OpenSSL library supports DSA.
func SupportsDSA() bool {
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(C.GO_EVP_PKEY_DSA, nil)
if ctx == nil {
return false
}
C.go_openssl_EVP_PKEY_CTX_free(ctx)
return true
}
// DSAParameters contains the DSA parameters.
type DSAParameters struct {
P, Q, G BigInt
}
// PrivateKeyDSA represents a DSA private key.
type PrivateKeyDSA struct {
DSAParameters
X, Y BigInt
// _pkey MUST NOT be accessed directly. Instead, use the withKey method.
_pkey C.GO_EVP_PKEY_PTR
}
func (k *PrivateKeyDSA) finalize() {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
func (k *PrivateKeyDSA) withKey(f func(C.GO_EVP_PKEY_PTR) C.int) C.int {
defer runtime.KeepAlive(k)
return f(k._pkey)
}
// PublicKeyDSA represents a DSA public key.
type PublicKeyDSA struct {
DSAParameters
Y BigInt
// _pkey MUST NOT be accessed directly. Instead, use the withKey method.
_pkey C.GO_EVP_PKEY_PTR
}
func (k *PublicKeyDSA) finalize() {
C.go_openssl_EVP_PKEY_free(k._pkey)
}
func (k *PublicKeyDSA) withKey(f func(C.GO_EVP_PKEY_PTR) C.int) C.int {
defer runtime.KeepAlive(k)
return f(k._pkey)
}
// GenerateDSAParameters generates a set of DSA parameters.
func GenerateDSAParameters(l, n int) (DSAParameters, error) {
// The DSA parameters are generated by creating a new DSA key and
// extracting the domain parameters from it.
// Generate a new DSA key context and set the known parameters.
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(C.GO_EVP_PKEY_DSA, nil)
if ctx == nil {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_CTX_new_id failed")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_paramgen_init(ctx) != 1 {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_paramgen_init failed")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_DSA, -1, C.GO_EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, C.int(l), nil) != 1 {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_DSA, -1, C.GO_EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, C.int(n), nil) != 1 {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
var pkey C.GO_EVP_PKEY_PTR
if C.go_openssl_EVP_PKEY_paramgen(ctx, &pkey) != 1 {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_paramgen failed")
}
defer C.go_openssl_EVP_PKEY_free(pkey)
// Extract the domain parameters from the generated key.
var p, q, g C.GO_BIGNUM_PTR
switch vMajor {
case 1:
dsa := getDSA(pkey)
if vMinor == 0 {
C.go_openssl_DSA_get0_pqg_backport(dsa, &p, &q, &g)
} else {
C.go_openssl_DSA_get0_pqg(dsa, &p, &q, &g)
}
case 3:
defer func() {
C.go_openssl_BN_free(p)
C.go_openssl_BN_free(q)
C.go_openssl_BN_free(g)
}()
if C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_FFC_P, &p) != 1 ||
C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_FFC_Q, &q) != 1 ||
C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_FFC_G, &g) != 1 {
return DSAParameters{}, newOpenSSLError("EVP_PKEY_get_bn_param")
}
default:
panic(errUnsupportedVersion())
}
return DSAParameters{
P: bnToBig(p),
Q: bnToBig(q),
G: bnToBig(g),
}, nil
}
// NewPrivateKeyDSA creates a new DSA private key from the given parameters.
func NewPrivateKeyDSA(params DSAParameters, x, y BigInt) (*PrivateKeyDSA, error) {
if x == nil || y == nil {
panic("x and y must not be nil")
}
pkey, err := newDSA(params, x, y)
if err != nil {
return nil, err
}
k := &PrivateKeyDSA{params, x, y, pkey}
runtime.SetFinalizer(k, (*PrivateKeyDSA).finalize)
return k, nil
}
// NewPublicKeyDSA creates a new DSA public key from the given parameters.
func NewPublicKeyDSA(params DSAParameters, y BigInt) (*PublicKeyDSA, error) {
if y == nil {
panic("y must not be nil")
}
pkey, err := newDSA(params, nil, y)
if err != nil {
return nil, err
}
k := &PublicKeyDSA{params, y, pkey}
runtime.SetFinalizer(k, (*PublicKeyDSA).finalize)
return k, nil
}
// GenerateKeyDSA generates a new private DSA key using the given parameters.
func GenerateKeyDSA(params DSAParameters) (*PrivateKeyDSA, error) {
pkey, err := newDSA(params, nil, nil)
if err != nil {
return nil, err
}
var x, y C.GO_BIGNUM_PTR
switch vMajor {
case 1:
dsa := getDSA(pkey)
if vMinor == 0 {
C.go_openssl_DSA_get0_key_backport(dsa, &y, &x)
} else {
C.go_openssl_DSA_get0_key(dsa, &y, &x)
}
case 3:
defer func() {
C.go_openssl_BN_clear_free(x)
C.go_openssl_BN_free(y)
}()
if C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_PUB_KEY, &y) != 1 ||
C.go_openssl_EVP_PKEY_get_bn_param(pkey, _OSSL_PKEY_PARAM_PRIV_KEY, &x) != 1 {
return nil, newOpenSSLError("EVP_PKEY_get_bn_param")
}
default:
panic(errUnsupportedVersion())
}
k := &PrivateKeyDSA{params, bnToBig(x), bnToBig(y), pkey}
runtime.SetFinalizer(k, (*PrivateKeyDSA).finalize)
return k, nil
}
// SignDSA signs a hash (which should be the result of hashing a larger message).
func SignDSA(priv *PrivateKeyDSA, hash []byte) ([]byte, error) {
return evpSign(priv.withKey, 0, 0, 0, hash)
}
// VerifyDSA verifiessig using the public key, pub.
func VerifyDSA(pub *PublicKeyDSA, hash []byte, sig []byte) bool {
return evpVerify(pub.withKey, 0, 0, 0, sig, hash) == nil
}
func newDSA(params DSAParameters, x, y BigInt) (C.GO_EVP_PKEY_PTR, error) {
switch vMajor {
case 1:
return newDSA1(params, x, y)
case 3:
return newDSA3(params, x, y)
default:
panic(errUnsupportedVersion())
}
}
func newDSA1(params DSAParameters, x, y BigInt) (pkey C.GO_EVP_PKEY_PTR, err error) {
checkMajorVersion(1)
dsa := C.go_openssl_DSA_new()
if dsa == nil {
return nil, newOpenSSLError("DSA_new failed")
}
defer func() {
if pkey == nil {
C.go_openssl_DSA_free(dsa)
}
}()
p, q, g := bigToBN(params.P), bigToBN(params.Q), bigToBN(params.G)
var ret C.int
if vMinor == 0 {
ret = C.go_openssl_DSA_set0_pqg_backport(dsa, p, q, g)
} else {
ret = C.go_openssl_DSA_set0_pqg(dsa, p, q, g)
}
if ret != 1 {
C.go_openssl_BN_free(p)
C.go_openssl_BN_free(q)
C.go_openssl_BN_free(g)
return nil, newOpenSSLError("DSA_set0_pqg failed")
}
if y != nil {
pub, priv := bigToBN(y), bigToBN(x)
if vMinor == 0 {
ret = C.go_openssl_DSA_set0_key_backport(dsa, pub, priv)
} else {
ret = C.go_openssl_DSA_set0_key(dsa, pub, priv)
}
if ret != 1 {
C.go_openssl_BN_free(pub)
C.go_openssl_BN_clear_free(priv)
return nil, newOpenSSLError("DSA_set0_key failed")
}
} else {
if C.go_openssl_DSA_generate_key(dsa) != 1 {
return nil, newOpenSSLError("DSA_generate_key failed")
}
}
pkey = C.go_openssl_EVP_PKEY_new()
if pkey == nil {
return nil, newOpenSSLError("EVP_PKEY_new failed")
}
if C.go_openssl_EVP_PKEY_assign(pkey, C.GO_EVP_PKEY_DSA, unsafe.Pointer(dsa)) != 1 {
C.go_openssl_EVP_PKEY_free(pkey)
return nil, newOpenSSLError("EVP_PKEY_assign failed")
}
return pkey, nil
}
func newDSA3(params DSAParameters, x, y BigInt) (C.GO_EVP_PKEY_PTR, error) {
checkMajorVersion(3)
bld, err := newParamBuilder()
if err != nil {
return nil, err
}
defer bld.finalize()
bld.addBigInt(_OSSL_PKEY_PARAM_FFC_P, params.P, false)
bld.addBigInt(_OSSL_PKEY_PARAM_FFC_Q, params.Q, false)
bld.addBigInt(_OSSL_PKEY_PARAM_FFC_G, params.G, false)
selection := C.int(C.GO_EVP_PKEY_KEYPAIR)
if y != nil {
bld.addBigInt(_OSSL_PKEY_PARAM_PUB_KEY, y, false)
if x == nil {
selection = C.int(C.GO_EVP_PKEY_PUBLIC_KEY)
}
}
if x != nil {
bld.addBigInt(_OSSL_PKEY_PARAM_PRIV_KEY, x, true)
}
bldparams, err := bld.build()
if err != nil {
return nil, err
}
defer C.go_openssl_OSSL_PARAM_free(bldparams)
pkey, err := newEvpFromParams(C.GO_EVP_PKEY_DSA, selection, bldparams)
if err != nil {
return nil, err
}
if y != nil {
return pkey, nil
}
// pkey doesn't contain the public component, but the crypto/dsa package
// expects it to be always there. Generate a new key using pkey as domain
// parameters placeholder.
defer C.go_openssl_EVP_PKEY_free(pkey)
ctx := C.go_openssl_EVP_PKEY_CTX_new_from_pkey(nil, pkey, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_from_pkey")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_keygen_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen_init")
}
var gkey C.GO_EVP_PKEY_PTR
if C.go_openssl_EVP_PKEY_keygen(ctx, &gkey) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen")
}
return gkey, nil
}
// getDSA returns the DSA from pkey.
// If pkey does not contain an DSA it panics.
// The returned key should not be freed.
func getDSA(pkey C.GO_EVP_PKEY_PTR) (key C.GO_DSA_PTR) {
if vMajor == 1 && vMinor == 0 {
if key0 := C.go_openssl_EVP_PKEY_get0(pkey); key0 != nil {
key = C.GO_DSA_PTR(key0)
}
} else {
key = C.go_openssl_EVP_PKEY_get0_DSA(pkey)
}
if key == nil {
panic("pkey does not contain an DSA")
}
return key
}