blob: 8c66ddf290b4685eb9e32910a0645dc18609a8a1 [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
62#include <openssl/asn1.h>
63#include <openssl/engine.h>
64#include <openssl/err.h>
65#include <openssl/ex_data.h>
66#include <openssl/mem.h>
67
68#include "internal.h"
69
70extern const DSA_METHOD DSA_default_method;
71
72DSA *DSA_new(void) { return DSA_new_method(NULL); }
73
74DSA *DSA_new_method(const ENGINE *engine) {
75 DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA));
76 if (dsa == NULL) {
77 OPENSSL_PUT_ERROR(DSA, DSA_new_method, ERR_R_MALLOC_FAILURE);
78 return NULL;
79 }
80
81 memset(dsa, 0, sizeof(DSA));
82
83 if (engine) {
84 dsa->meth = ENGINE_get_DSA_method(engine);
85 }
86
87 if (dsa->meth == NULL) {
88 dsa->meth = (DSA_METHOD*) &DSA_default_method;
89 }
90 METHOD_ref(dsa->meth);
91
92 dsa->write_params = 1;
93 dsa->references = 1;
94
95 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data)) {
96 METHOD_unref(dsa->meth);
97 OPENSSL_free(dsa);
98 return NULL;
99 }
100
101 if (dsa->meth->init && !dsa->meth->init(dsa)) {
102 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
103 METHOD_unref(dsa->meth);
104 OPENSSL_free(dsa);
105 return NULL;
106 }
107
108 return dsa;
109}
110
111void DSA_free(DSA *dsa) {
112 if (dsa == NULL) {
113 return;
114 }
115
116 if (CRYPTO_add(&dsa->references, -1, CRYPTO_LOCK_DSA) > 0) {
117 return;
118 }
119
120 if (dsa->meth->finish) {
121 dsa->meth->finish(dsa);
122 }
123 METHOD_unref(dsa->meth);
124
125 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data);
126
127 if (dsa->p != NULL)
128 BN_clear_free(dsa->p);
129 if (dsa->q != NULL)
130 BN_clear_free(dsa->q);
131 if (dsa->g != NULL)
132 BN_clear_free(dsa->g);
133 if (dsa->pub_key != NULL)
134 BN_clear_free(dsa->pub_key);
135 if (dsa->priv_key != NULL)
136 BN_clear_free(dsa->priv_key);
137 if (dsa->kinv != NULL)
138 BN_clear_free(dsa->kinv);
139 if (dsa->r != NULL)
140 BN_clear_free(dsa->r);
141 OPENSSL_free(dsa);
142}
143
144int DSA_up_ref(DSA *dsa) {
145 CRYPTO_add(&dsa->references, 1, CRYPTO_LOCK_DSA);
146 return 1;
147}
148
149int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
150 size_t seed_len, int *out_counter,
151 unsigned long *out_h, BN_GENCB *cb) {
152 if (dsa->meth->generate_parameters) {
153 return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
154 out_counter, out_h, cb);
155 }
156 return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
157 out_counter, out_h, cb);
158}
159
160int DSA_generate_key(DSA *dsa) {
161 if (dsa->meth->keygen) {
162 return dsa->meth->keygen(dsa);
163 }
164 return DSA_default_method.keygen(dsa);
165}
166
167DSA_SIG *DSA_SIG_new(void) {
168 DSA_SIG *sig;
169 sig = OPENSSL_malloc(sizeof(DSA_SIG));
170 if (!sig) {
171 return NULL;
172 }
173 sig->r = NULL;
174 sig->s = NULL;
175 return sig;
176}
177
178void DSA_SIG_free(DSA_SIG *sig) {
179 if (!sig) {
180 return;
181 }
182
183 if (sig->r) {
184 BN_free(sig->r);
185 }
186 if (sig->s) {
187 BN_free(sig->s);
188 }
189 OPENSSL_free(sig);
190}
191
192DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
193 if (dsa->meth->sign) {
194 return dsa->meth->sign(digest, digest_len, dsa);
195 }
196 return DSA_default_method.sign(digest, digest_len, dsa);
197}
198
199int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
200 const DSA *dsa) {
201 int valid, ret;
202
203 if (dsa->meth->verify) {
204 ret = dsa->meth->verify(&valid, digest, digest_len, sig, dsa);
205 } else {
206 ret = DSA_default_method.verify(&valid, digest, digest_len, sig, dsa);
207 }
208
209 if (!ret) {
210 return -1;
211 } else if (!valid) {
212 return 0;
213 }
214 return 1;
215}
216
217int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
218 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
219 if (dsa->meth->verify) {
220 return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
221 }
222
223 return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
224}
225
226int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
227 uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
228 DSA_SIG *s;
229
230 s = DSA_do_sign(digest, digest_len, dsa);
231 if (s == NULL) {
232 *out_siglen = 0;
233 return 0;
234 }
235
236 *out_siglen = i2d_DSA_SIG(s, &out_sig);
237 DSA_SIG_free(s);
238 return 1;
239}
240
241int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
242 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
243 DSA_SIG *s = NULL;
244 int ret = -1, valid;
245
246 s = DSA_SIG_new();
247 if (s == NULL) {
248 goto err;
249 }
250
251 if (d2i_DSA_SIG(&s, &sig, sig_len) == NULL) {
252 goto err;
253 }
254
255 if (!DSA_do_check_signature(&valid, digest, digest_len, s, dsa)) {
256 goto err;
257 }
258
259 ret = valid;
260
261err:
262 if (s) {
263 DSA_SIG_free(s);
264 }
265 return ret;
266}
267
268int DSA_check_signature(int *out_valid, const uint8_t *digest,
269 size_t digest_len, const uint8_t *sig, size_t sig_len,
270 const DSA *dsa) {
271 DSA_SIG *s = NULL;
272 int ret = 0;
273
274 s = DSA_SIG_new();
275 if (s == NULL) {
276 goto err;
277 }
278
279 if (d2i_DSA_SIG(&s, &sig, sig_len) == NULL) {
280 goto err;
281 }
282
283 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
284
285err:
286 if (s) {
287 DSA_SIG_free(s);
288 }
289 return ret;
290}
291
292int 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}