dsa_lib.c revision 194206
153469Sobrien/* crypto/dsa/dsa_lib.c */ 2126432Sache/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 353469Sobrien * All rights reserved. 453469Sobrien * 553469Sobrien * This package is an SSL implementation written 653469Sobrien * by Eric Young (eay@cryptsoft.com). 753469Sobrien * The implementation was written so as to conform with Netscapes SSL. 853469Sobrien * 953469Sobrien * This library is free for commercial and non-commercial use as long as 1053469Sobrien * the following conditions are aheared to. The following conditions 1153469Sobrien * apply to all code found in this distribution, be it the RC4, RSA, 1253469Sobrien * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1353469Sobrien * included with this distribution is covered by the same copyright terms 1453469Sobrien * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1553469Sobrien * 1653469Sobrien * Copyright remains Eric Young's, and as such any Copyright notices in 1753469Sobrien * the code are not to be removed. 1853469Sobrien * If this package is used in a product, Eric Young should be given attribution 1953469Sobrien * as the author of the parts of the library used. 2053469Sobrien * This can be in the form of a textual message at program startup or 2153469Sobrien * in documentation (online or textual) provided with the package. 2253469Sobrien * 2353469Sobrien * Redistribution and use in source and binary forms, with or without 2453469Sobrien * modification, are permitted provided that the following conditions 2553469Sobrien * are met: 2653469Sobrien * 1. Redistributions of source code must retain the copyright 2753469Sobrien * notice, this list of conditions and the following disclaimer. 2853469Sobrien * 2. Redistributions in binary form must reproduce the above copyright 2953469Sobrien * notice, this list of conditions and the following disclaimer in the 3053469Sobrien * documentation and/or other materials provided with the distribution. 3153469Sobrien * 3. All advertising materials mentioning features or use of this software 3253469Sobrien * must display the following acknowledgement: 3353469Sobrien * "This product includes cryptographic software written by 3453469Sobrien * Eric Young (eay@cryptsoft.com)" 3553469Sobrien * The word 'cryptographic' can be left out if the rouines from the library 3653469Sobrien * being used are not cryptographic related :-). 3753469Sobrien * 4. If you include any Windows specific code (or a derivative thereof) from 3853469Sobrien * the apps directory (application code) you must include an acknowledgement: 3955360Sobrien * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055360Sobrien * 4153469Sobrien * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4253469Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4353469Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4453469Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4553469Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4653469Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4753469Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4853469Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4953469Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5053469Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5153469Sobrien * SUCH DAMAGE. 5253469Sobrien * 5353469Sobrien * The licence and distribution terms for any publically available version or 5453469Sobrien * derivative of this code cannot be changed. i.e. this code cannot simply be 5553469Sobrien * copied and put under another distribution licence 56131554Stjr * [including the GNU Public Licence.] 57131554Stjr */ 58131554Stjr 59131554Stjr/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ 6053469Sobrien 6153469Sobrien#include <stdio.h> 6253469Sobrien#include "cryptlib.h" 6353469Sobrien#include <openssl/bn.h> 6453469Sobrien#include <openssl/dsa.h> 6553469Sobrien#include <openssl/asn1.h> 6653469Sobrien#ifndef OPENSSL_NO_ENGINE 6753469Sobrien#include <openssl/engine.h> 6853469Sobrien#endif 6953469Sobrien#ifndef OPENSSL_NO_DH 7053469Sobrien#include <openssl/dh.h> 7153469Sobrien#endif 7253469Sobrien 7353469Sobrienconst char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT; 7453469Sobrien 7553469Sobrienstatic const DSA_METHOD *default_DSA_method = NULL; 7653469Sobrien 7753469Sobrienvoid DSA_set_default_method(const DSA_METHOD *meth) 7853469Sobrien { 7953469Sobrien#ifdef OPENSSL_FIPS 8053469Sobrien if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD)) 81131554Stjr { 82131554Stjr DSAerr(DSA_F_DSA_SET_DEFAULT_METHOD, DSA_R_NON_FIPS_METHOD); 83131554Stjr return; 8453469Sobrien } 8553469Sobrien#endif 8653469Sobrien 87131554Stjr default_DSA_method = meth; 8853469Sobrien } 89131554Stjr 9053469Sobrienconst DSA_METHOD *DSA_get_default_method(void) 9153469Sobrien { 9253469Sobrien if(!default_DSA_method) 9353469Sobrien default_DSA_method = DSA_OpenSSL(); 9453469Sobrien return default_DSA_method; 9553469Sobrien } 9653469Sobrien 9753469SobrienDSA *DSA_new(void) 9853469Sobrien { 9953469Sobrien return DSA_new_method(NULL); 10053469Sobrien } 10153469Sobrien 10253469Sobrienint DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 10355360Sobrien { 10453469Sobrien /* NB: The caller is specifically setting a method, so it's not up to us 10553469Sobrien * to deal with which ENGINE it comes from. */ 10653469Sobrien const DSA_METHOD *mtmp; 10753469Sobrien#ifdef OPENSSL_FIPS 10855360Sobrien if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD)) 10955360Sobrien { 11055360Sobrien DSAerr(DSA_F_DSA_SET_METHOD, DSA_R_NON_FIPS_METHOD); 11153469Sobrien return 0; 11253469Sobrien } 11353469Sobrien#endif 11453469Sobrien mtmp = dsa->meth; 11555360Sobrien if (mtmp->finish) mtmp->finish(dsa); 11653469Sobrien#ifndef OPENSSL_NO_ENGINE 11753469Sobrien if (dsa->engine) 11853469Sobrien { 11953469Sobrien ENGINE_finish(dsa->engine); 12053469Sobrien dsa->engine = NULL; 12153469Sobrien } 12253469Sobrien#endif 12353469Sobrien dsa->meth = meth; 12453469Sobrien if (meth->init) meth->init(dsa); 12553469Sobrien return 1; 12653469Sobrien } 12753469Sobrien 12853469SobrienDSA *DSA_new_method(ENGINE *engine) 12956230Sru { 13056230Sru DSA *ret; 13156230Sru 13256230Sru ret=(DSA *)OPENSSL_malloc(sizeof(DSA)); 13356230Sru if (ret == NULL) 13456230Sru { 13556230Sru DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 13656230Sru return(NULL); 13756230Sru } 13853469Sobrien ret->meth = DSA_get_default_method(); 13956230Sru#ifndef OPENSSL_NO_ENGINE 14053469Sobrien if (engine) 14153469Sobrien { 14256230Sru if (!ENGINE_init(engine)) 14353469Sobrien { 14453469Sobrien DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 14553469Sobrien OPENSSL_free(ret); 14653469Sobrien return NULL; 14753469Sobrien } 14853469Sobrien ret->engine = engine; 14953469Sobrien } 15053469Sobrien else 15153469Sobrien ret->engine = ENGINE_get_default_DSA(); 15253469Sobrien if(ret->engine) 15353469Sobrien { 15453469Sobrien ret->meth = ENGINE_get_DSA(ret->engine); 15553469Sobrien if(!ret->meth) 15653469Sobrien { 15755360Sobrien DSAerr(DSA_F_DSA_NEW_METHOD, 15853469Sobrien ERR_R_ENGINE_LIB); 15953469Sobrien ENGINE_finish(ret->engine); 16053469Sobrien OPENSSL_free(ret); 16153469Sobrien return NULL; 16255360Sobrien } 16355360Sobrien } 16455360Sobrien#endif 16553469Sobrien#ifdef OPENSSL_FIPS 16653469Sobrien if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD)) 16753469Sobrien { 16853469Sobrien DSAerr(DSA_F_DSA_NEW_METHOD, DSA_R_NON_FIPS_METHOD); 16953469Sobrien#ifndef OPENSSL_NO_ENGINE 17053469Sobrien if (ret->engine) 17153469Sobrien ENGINE_finish(ret->engine); 17253469Sobrien#endif 17353469Sobrien OPENSSL_free(ret); 17453469Sobrien return NULL; 17553469Sobrien } 17653469Sobrien#endif 17753469Sobrien 17853469Sobrien ret->pad=0; 17953469Sobrien ret->version=0; 18053469Sobrien ret->write_params=1; 18153469Sobrien ret->p=NULL; 18253469Sobrien ret->q=NULL; 18353469Sobrien ret->g=NULL; 18453469Sobrien 18553469Sobrien ret->pub_key=NULL; 18653469Sobrien ret->priv_key=NULL; 18753469Sobrien 18853469Sobrien ret->kinv=NULL; 18953469Sobrien ret->r=NULL; 19053469Sobrien ret->method_mont_p=NULL; 19153469Sobrien 19253469Sobrien ret->references=1; 19353469Sobrien ret->flags=ret->meth->flags; 19453469Sobrien CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 19553469Sobrien if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 19653469Sobrien { 19753469Sobrien#ifndef OPENSSL_NO_ENGINE 19853469Sobrien if (ret->engine) 19953469Sobrien ENGINE_finish(ret->engine); 20053469Sobrien#endif 20153469Sobrien CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 20253469Sobrien OPENSSL_free(ret); 20353469Sobrien ret=NULL; 20453469Sobrien } 20553469Sobrien 20653469Sobrien return(ret); 207 } 208 209void DSA_free(DSA *r) 210 { 211 int i; 212 213 if (r == NULL) return; 214 215 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); 216#ifdef REF_PRINT 217 REF_PRINT("DSA",r); 218#endif 219 if (i > 0) return; 220#ifdef REF_CHECK 221 if (i < 0) 222 { 223 fprintf(stderr,"DSA_free, bad reference count\n"); 224 abort(); 225 } 226#endif 227 228 if(r->meth->finish) 229 r->meth->finish(r); 230#ifndef OPENSSL_NO_ENGINE 231 if(r->engine) 232 ENGINE_finish(r->engine); 233#endif 234 235 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 236 237 if (r->p != NULL) BN_clear_free(r->p); 238 if (r->q != NULL) BN_clear_free(r->q); 239 if (r->g != NULL) BN_clear_free(r->g); 240 if (r->pub_key != NULL) BN_clear_free(r->pub_key); 241 if (r->priv_key != NULL) BN_clear_free(r->priv_key); 242 if (r->kinv != NULL) BN_clear_free(r->kinv); 243 if (r->r != NULL) BN_clear_free(r->r); 244 OPENSSL_free(r); 245 } 246 247int DSA_up_ref(DSA *r) 248 { 249 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 250#ifdef REF_PRINT 251 REF_PRINT("DSA",r); 252#endif 253#ifdef REF_CHECK 254 if (i < 2) 255 { 256 fprintf(stderr, "DSA_up_ref, bad reference count\n"); 257 abort(); 258 } 259#endif 260 return ((i > 1) ? 1 : 0); 261 } 262 263int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 264 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 265 { 266 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 267 new_func, dup_func, free_func); 268 } 269 270int DSA_set_ex_data(DSA *d, int idx, void *arg) 271 { 272 return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); 273 } 274 275void *DSA_get_ex_data(DSA *d, int idx) 276 { 277 return(CRYPTO_get_ex_data(&d->ex_data,idx)); 278 } 279 280#ifndef OPENSSL_NO_DH 281DH *DSA_dup_DH(const DSA *r) 282 { 283 /* DSA has p, q, g, optional pub_key, optional priv_key. 284 * DH has p, optional length, g, optional pub_key, optional priv_key. 285 */ 286 287 DH *ret = NULL; 288 289 if (r == NULL) 290 goto err; 291 ret = DH_new(); 292 if (ret == NULL) 293 goto err; 294 if (r->p != NULL) 295 if ((ret->p = BN_dup(r->p)) == NULL) 296 goto err; 297 if (r->q != NULL) 298 ret->length = BN_num_bits(r->q); 299 if (r->g != NULL) 300 if ((ret->g = BN_dup(r->g)) == NULL) 301 goto err; 302 if (r->pub_key != NULL) 303 if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) 304 goto err; 305 if (r->priv_key != NULL) 306 if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) 307 goto err; 308 309 return ret; 310 311 err: 312 if (ret != NULL) 313 DH_free(ret); 314 return NULL; 315 } 316#endif 317