dsa_lib.c revision 160814
155714Skris/* crypto/dsa/dsa_lib.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ 6055714Skris 6155714Skris#include <stdio.h> 6255714Skris#include "cryptlib.h" 6355714Skris#include <openssl/bn.h> 6455714Skris#include <openssl/dsa.h> 6555714Skris#include <openssl/asn1.h> 66111147Snectar#ifndef OPENSSL_NO_ENGINE 67109998Smarkm#include <openssl/engine.h> 68111147Snectar#endif 69160814Ssimon#ifndef OPENSSL_NO_DH 70160814Ssimon#include <openssl/dh.h> 71160814Ssimon#endif 7255714Skris 7355714Skrisconst char *DSA_version="DSA" OPENSSL_VERSION_PTEXT; 7455714Skris 75109998Smarkmstatic const DSA_METHOD *default_DSA_method = NULL; 7659191Skris 77109998Smarkmvoid DSA_set_default_method(const DSA_METHOD *meth) 78109998Smarkm { 7959191Skris default_DSA_method = meth; 80109998Smarkm } 8159191Skris 82109998Smarkmconst DSA_METHOD *DSA_get_default_method(void) 83109998Smarkm { 84109998Smarkm if(!default_DSA_method) 85109998Smarkm default_DSA_method = DSA_OpenSSL(); 8659191Skris return default_DSA_method; 87109998Smarkm } 8859191Skris 8955714SkrisDSA *DSA_new(void) 90109998Smarkm { 9159191Skris return DSA_new_method(NULL); 92109998Smarkm } 9359191Skris 94109998Smarkmint DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 95109998Smarkm { 96109998Smarkm /* NB: The caller is specifically setting a method, so it's not up to us 97109998Smarkm * to deal with which ENGINE it comes from. */ 98109998Smarkm const DSA_METHOD *mtmp; 9959191Skris mtmp = dsa->meth; 10059191Skris if (mtmp->finish) mtmp->finish(dsa); 101111147Snectar#ifndef OPENSSL_NO_ENGINE 102109998Smarkm if (dsa->engine) 103109998Smarkm { 104109998Smarkm ENGINE_finish(dsa->engine); 105109998Smarkm dsa->engine = NULL; 106109998Smarkm } 107111147Snectar#endif 10859191Skris dsa->meth = meth; 10959191Skris if (meth->init) meth->init(dsa); 110109998Smarkm return 1; 111109998Smarkm } 11259191Skris 113109998SmarkmDSA *DSA_new_method(ENGINE *engine) 11455714Skris { 11555714Skris DSA *ret; 11655714Skris 11768651Skris ret=(DSA *)OPENSSL_malloc(sizeof(DSA)); 11855714Skris if (ret == NULL) 11955714Skris { 120109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 12155714Skris return(NULL); 12255714Skris } 123109998Smarkm ret->meth = DSA_get_default_method(); 124111147Snectar#ifndef OPENSSL_NO_ENGINE 125109998Smarkm if (engine) 126109998Smarkm { 127109998Smarkm if (!ENGINE_init(engine)) 128109998Smarkm { 129109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 130109998Smarkm OPENSSL_free(ret); 131109998Smarkm return NULL; 132109998Smarkm } 133109998Smarkm ret->engine = engine; 134109998Smarkm } 135109998Smarkm else 136109998Smarkm ret->engine = ENGINE_get_default_DSA(); 137109998Smarkm if(ret->engine) 138109998Smarkm { 139109998Smarkm ret->meth = ENGINE_get_DSA(ret->engine); 140109998Smarkm if(!ret->meth) 141109998Smarkm { 142109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD, 143109998Smarkm ERR_R_ENGINE_LIB); 144109998Smarkm ENGINE_finish(ret->engine); 145109998Smarkm OPENSSL_free(ret); 146109998Smarkm return NULL; 147109998Smarkm } 148109998Smarkm } 149111147Snectar#endif 150109998Smarkm 15155714Skris ret->pad=0; 15255714Skris ret->version=0; 15355714Skris ret->write_params=1; 15455714Skris ret->p=NULL; 15555714Skris ret->q=NULL; 15655714Skris ret->g=NULL; 15755714Skris 15855714Skris ret->pub_key=NULL; 15955714Skris ret->priv_key=NULL; 16055714Skris 16155714Skris ret->kinv=NULL; 16255714Skris ret->r=NULL; 16355714Skris ret->method_mont_p=NULL; 16455714Skris 16555714Skris ret->references=1; 16659191Skris ret->flags=ret->meth->flags; 167109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 16859191Skris if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 16959191Skris { 170111147Snectar#ifndef OPENSSL_NO_ENGINE 171109998Smarkm if (ret->engine) 172109998Smarkm ENGINE_finish(ret->engine); 173111147Snectar#endif 174109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 17568651Skris OPENSSL_free(ret); 17659191Skris ret=NULL; 17759191Skris } 17859191Skris 17955714Skris return(ret); 18055714Skris } 18155714Skris 18255714Skrisvoid DSA_free(DSA *r) 18355714Skris { 18455714Skris int i; 18555714Skris 18655714Skris if (r == NULL) return; 18755714Skris 18855714Skris i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); 18955714Skris#ifdef REF_PRINT 19055714Skris REF_PRINT("DSA",r); 19155714Skris#endif 19255714Skris if (i > 0) return; 19355714Skris#ifdef REF_CHECK 19455714Skris if (i < 0) 19555714Skris { 19655714Skris fprintf(stderr,"DSA_free, bad reference count\n"); 19755714Skris abort(); 19855714Skris } 19955714Skris#endif 20055714Skris 201109998Smarkm if(r->meth->finish) 202109998Smarkm r->meth->finish(r); 203111147Snectar#ifndef OPENSSL_NO_ENGINE 204109998Smarkm if(r->engine) 205109998Smarkm ENGINE_finish(r->engine); 206111147Snectar#endif 20776866Skris 208109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 20959191Skris 21055714Skris if (r->p != NULL) BN_clear_free(r->p); 21155714Skris if (r->q != NULL) BN_clear_free(r->q); 21255714Skris if (r->g != NULL) BN_clear_free(r->g); 21355714Skris if (r->pub_key != NULL) BN_clear_free(r->pub_key); 21455714Skris if (r->priv_key != NULL) BN_clear_free(r->priv_key); 21555714Skris if (r->kinv != NULL) BN_clear_free(r->kinv); 21655714Skris if (r->r != NULL) BN_clear_free(r->r); 21768651Skris OPENSSL_free(r); 21855714Skris } 21955714Skris 220109998Smarkmint DSA_up_ref(DSA *r) 22155714Skris { 222109998Smarkm int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 223109998Smarkm#ifdef REF_PRINT 224109998Smarkm REF_PRINT("DSA",r); 225109998Smarkm#endif 226109998Smarkm#ifdef REF_CHECK 227109998Smarkm if (i < 2) 228109998Smarkm { 229109998Smarkm fprintf(stderr, "DSA_up_ref, bad reference count\n"); 230109998Smarkm abort(); 231109998Smarkm } 232109998Smarkm#endif 233109998Smarkm return ((i > 1) ? 1 : 0); 234109998Smarkm } 235109998Smarkm 236109998Smarkmint DSA_size(const DSA *r) 237109998Smarkm { 23855714Skris int ret,i; 23955714Skris ASN1_INTEGER bs; 240109998Smarkm unsigned char buf[4]; /* 4 bytes looks really small. 241109998Smarkm However, i2d_ASN1_INTEGER() will not look 242109998Smarkm beyond the first byte, as long as the second 243109998Smarkm parameter is NULL. */ 24455714Skris 24555714Skris i=BN_num_bits(r->q); 24655714Skris bs.length=(i+7)/8; 24755714Skris bs.data=buf; 24855714Skris bs.type=V_ASN1_INTEGER; 24955714Skris /* If the top bit is set the asn1 encoding is 1 larger. */ 25055714Skris buf[0]=0xff; 25155714Skris 25255714Skris i=i2d_ASN1_INTEGER(&bs,NULL); 25355714Skris i+=i; /* r and s */ 25455714Skris ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); 25555714Skris return(ret); 25655714Skris } 25755714Skris 25859191Skrisint DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 25959191Skris CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 26059191Skris { 261109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 262109998Smarkm new_func, dup_func, free_func); 26359191Skris } 26459191Skris 26559191Skrisint DSA_set_ex_data(DSA *d, int idx, void *arg) 26659191Skris { 26759191Skris return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); 26859191Skris } 26959191Skris 27059191Skrisvoid *DSA_get_ex_data(DSA *d, int idx) 27159191Skris { 27259191Skris return(CRYPTO_get_ex_data(&d->ex_data,idx)); 27359191Skris } 27459191Skris 275109998Smarkm#ifndef OPENSSL_NO_DH 276109998SmarkmDH *DSA_dup_DH(const DSA *r) 27755714Skris { 27855714Skris /* DSA has p, q, g, optional pub_key, optional priv_key. 27955714Skris * DH has p, optional length, g, optional pub_key, optional priv_key. 28055714Skris */ 28155714Skris 28255714Skris DH *ret = NULL; 28355714Skris 28455714Skris if (r == NULL) 28555714Skris goto err; 28655714Skris ret = DH_new(); 28755714Skris if (ret == NULL) 28855714Skris goto err; 28955714Skris if (r->p != NULL) 29055714Skris if ((ret->p = BN_dup(r->p)) == NULL) 29155714Skris goto err; 29255714Skris if (r->q != NULL) 29355714Skris ret->length = BN_num_bits(r->q); 29455714Skris if (r->g != NULL) 29555714Skris if ((ret->g = BN_dup(r->g)) == NULL) 29655714Skris goto err; 29755714Skris if (r->pub_key != NULL) 29855714Skris if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) 29955714Skris goto err; 30055714Skris if (r->priv_key != NULL) 30155714Skris if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) 30255714Skris goto err; 30355714Skris 30455714Skris return ret; 30555714Skris 30655714Skris err: 30755714Skris if (ret != NULL) 30855714Skris DH_free(ret); 30955714Skris return NULL; 31055714Skris } 31155714Skris#endif 312