blob: c58095628b282c2c0a3193d23428eeff7951a372 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 *
57 * The DSS routines are based on patches supplied by
58 * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
59
60#include <openssl/dsa.h>
61
Adam Langley2b2d66d2015-01-30 17:08:37 -080062#include <string.h>
63
Adam Langley95c29f32014-06-20 12:00:00 -070064#include <openssl/asn1.h>
Adam Langleybed8ce72014-09-05 17:04:51 -070065#include <openssl/dh.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066#include <openssl/engine.h>
67#include <openssl/err.h>
68#include <openssl/ex_data.h>
69#include <openssl/mem.h>
Brian Smith054e6822015-03-27 21:12:01 -100070#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070071
72#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070073#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070074
Adam Langley2b2d66d2015-01-30 17:08:37 -080075
Adam Langley95c29f32014-06-20 12:00:00 -070076extern const DSA_METHOD DSA_default_method;
77
78DSA *DSA_new(void) { return DSA_new_method(NULL); }
79
80DSA *DSA_new_method(const ENGINE *engine) {
81 DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA));
82 if (dsa == NULL) {
83 OPENSSL_PUT_ERROR(DSA, DSA_new_method, ERR_R_MALLOC_FAILURE);
84 return NULL;
85 }
86
87 memset(dsa, 0, sizeof(DSA));
88
89 if (engine) {
90 dsa->meth = ENGINE_get_DSA_method(engine);
91 }
92
93 if (dsa->meth == NULL) {
94 dsa->meth = (DSA_METHOD*) &DSA_default_method;
95 }
96 METHOD_ref(dsa->meth);
97
98 dsa->write_params = 1;
99 dsa->references = 1;
100
Adam Langley683d7bd2015-04-13 11:04:14 -0700101 CRYPTO_MUTEX_init(&dsa->method_mont_p_lock);
102
Adam Langley95c29f32014-06-20 12:00:00 -0700103 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data)) {
104 METHOD_unref(dsa->meth);
105 OPENSSL_free(dsa);
106 return NULL;
107 }
108
109 if (dsa->meth->init && !dsa->meth->init(dsa)) {
110 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
111 METHOD_unref(dsa->meth);
112 OPENSSL_free(dsa);
113 return NULL;
114 }
115
116 return dsa;
117}
118
119void DSA_free(DSA *dsa) {
120 if (dsa == NULL) {
121 return;
122 }
123
124 if (CRYPTO_add(&dsa->references, -1, CRYPTO_LOCK_DSA) > 0) {
125 return;
126 }
127
128 if (dsa->meth->finish) {
129 dsa->meth->finish(dsa);
130 }
131 METHOD_unref(dsa->meth);
132
133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
134
David Benjaminc9a202f2015-02-11 01:16:26 -0500135 if (dsa->p != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700136 BN_clear_free(dsa->p);
David Benjaminc9a202f2015-02-11 01:16:26 -0500137 }
138 if (dsa->q != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700139 BN_clear_free(dsa->q);
David Benjaminc9a202f2015-02-11 01:16:26 -0500140 }
141 if (dsa->g != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700142 BN_clear_free(dsa->g);
David Benjaminc9a202f2015-02-11 01:16:26 -0500143 }
144 if (dsa->pub_key != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700145 BN_clear_free(dsa->pub_key);
David Benjaminc9a202f2015-02-11 01:16:26 -0500146 }
147 if (dsa->priv_key != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700148 BN_clear_free(dsa->priv_key);
David Benjaminc9a202f2015-02-11 01:16:26 -0500149 }
150 if (dsa->kinv != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700151 BN_clear_free(dsa->kinv);
David Benjaminc9a202f2015-02-11 01:16:26 -0500152 }
153 if (dsa->r != NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700154 BN_clear_free(dsa->r);
David Benjaminc9a202f2015-02-11 01:16:26 -0500155 }
Adam Langley683d7bd2015-04-13 11:04:14 -0700156 CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700157 OPENSSL_free(dsa);
158}
159
160int DSA_up_ref(DSA *dsa) {
161 CRYPTO_add(&dsa->references, 1, CRYPTO_LOCK_DSA);
162 return 1;
163}
164
165int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
166 size_t seed_len, int *out_counter,
167 unsigned long *out_h, BN_GENCB *cb) {
168 if (dsa->meth->generate_parameters) {
169 return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
170 out_counter, out_h, cb);
171 }
172 return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
173 out_counter, out_h, cb);
174}
175
176int DSA_generate_key(DSA *dsa) {
177 if (dsa->meth->keygen) {
178 return dsa->meth->keygen(dsa);
179 }
180 return DSA_default_method.keygen(dsa);
181}
182
183DSA_SIG *DSA_SIG_new(void) {
184 DSA_SIG *sig;
185 sig = OPENSSL_malloc(sizeof(DSA_SIG));
186 if (!sig) {
187 return NULL;
188 }
189 sig->r = NULL;
190 sig->s = NULL;
191 return sig;
192}
193
194void DSA_SIG_free(DSA_SIG *sig) {
195 if (!sig) {
196 return;
197 }
198
199 if (sig->r) {
200 BN_free(sig->r);
201 }
202 if (sig->s) {
203 BN_free(sig->s);
204 }
205 OPENSSL_free(sig);
206}
207
208DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
209 if (dsa->meth->sign) {
210 return dsa->meth->sign(digest, digest_len, dsa);
211 }
212 return DSA_default_method.sign(digest, digest_len, dsa);
213}
214
215int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
216 const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500217 int valid;
218 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700219 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700220 }
David Benjamin95e18c52015-01-10 23:37:17 -0500221 return valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700222}
223
224int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
225 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
226 if (dsa->meth->verify) {
227 return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
228 }
229
230 return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
231}
232
233int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
234 uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
235 DSA_SIG *s;
236
237 s = DSA_do_sign(digest, digest_len, dsa);
238 if (s == NULL) {
239 *out_siglen = 0;
240 return 0;
241 }
242
243 *out_siglen = i2d_DSA_SIG(s, &out_sig);
244 DSA_SIG_free(s);
245 return 1;
246}
247
248int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
249 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500250 int valid;
251 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
252 return -1;
253 }
254 return valid;
255}
256
257int DSA_check_signature(int *out_valid, const uint8_t *digest,
258 size_t digest_len, const uint8_t *sig, size_t sig_len,
259 const DSA *dsa) {
Adam Langley95c29f32014-06-20 12:00:00 -0700260 DSA_SIG *s = NULL;
David Benjamin95e18c52015-01-10 23:37:17 -0500261 int ret = 0;
Adam Langleyca9a5382015-01-08 12:26:55 -0800262 uint8_t *der = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700263
264 s = DSA_SIG_new();
265 if (s == NULL) {
266 goto err;
267 }
268
Adam Langleyca9a5382015-01-08 12:26:55 -0800269 const uint8_t *sigp = sig;
270 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
271 goto err;
272 }
273
274 /* Ensure that the signature uses DER and doesn't have trailing garbage. */
275 int der_len = i2d_DSA_SIG(s, &der);
276 if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700277 goto err;
278 }
279
David Benjamin95e18c52015-01-10 23:37:17 -0500280 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700281
282err:
Adam Langleyca9a5382015-01-08 12:26:55 -0800283 if (der != NULL) {
284 OPENSSL_free(der);
285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286 if (s) {
287 DSA_SIG_free(s);
288 }
289 return ret;
290}
291
Adam Langley95c29f32014-06-20 12:00:00 -0700292int DSA_size(const DSA *dsa) {
293 int ret, i;
294 ASN1_INTEGER bs;
295 unsigned char buf[4]; /* 4 bytes looks really small.
296 However, i2d_ASN1_INTEGER() will not look
297 beyond the first byte, as long as the second
298 parameter is NULL. */
299
300 i = BN_num_bits(dsa->q);
301 bs.length = (i + 7) / 8;
302 bs.data = buf;
303 bs.type = V_ASN1_INTEGER;
304 /* If the top bit is set the asn1 encoding is 1 larger. */
305 buf[0] = 0xff;
306
307 i = i2d_ASN1_INTEGER(&bs, NULL);
308 i += i; /* r and s */
309 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
310 return ret;
311}
312
313int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
314 BIGNUM **out_r) {
315 if (dsa->meth->sign_setup) {
Adam Langleyd4b4f082014-06-20 12:00:00 -0700316 return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700317 }
318
Adam Langleyd4b4f082014-06-20 12:00:00 -0700319 return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700320}
321
322int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
323 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
324 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, new_func,
325 dup_func, free_func);
326}
327
328int DSA_set_ex_data(DSA *d, int idx, void *arg) {
329 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
330}
331
332void *DSA_get_ex_data(const DSA *d, int idx) {
333 return CRYPTO_get_ex_data(&d->ex_data, idx);
334}
Adam Langleybed8ce72014-09-05 17:04:51 -0700335
336DH *DSA_dup_DH(const DSA *r) {
337 DH *ret = NULL;
338
339 if (r == NULL) {
340 goto err;
341 }
342 ret = DH_new();
343 if (ret == NULL) {
344 goto err;
345 }
346 if (r->q != NULL) {
347 ret->priv_length = BN_num_bits(r->q);
348 if ((ret->q = BN_dup(r->q)) == NULL) {
349 goto err;
350 }
351 }
352 if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
353 (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
354 (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
355 (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
356 goto err;
357 }
358
359 return ret;
360
361err:
362 if (ret != NULL) {
363 DH_free(ret);
364 }
365 return NULL;
366}