dsa_lib.c revision 111147
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 6955714Skris 7055714Skrisconst char *DSA_version="DSA" OPENSSL_VERSION_PTEXT; 7155714Skris 72109998Smarkmstatic const DSA_METHOD *default_DSA_method = NULL; 7359191Skris 74109998Smarkmvoid DSA_set_default_method(const DSA_METHOD *meth) 75109998Smarkm { 7659191Skris default_DSA_method = meth; 77109998Smarkm } 7859191Skris 79109998Smarkmconst DSA_METHOD *DSA_get_default_method(void) 80109998Smarkm { 81109998Smarkm if(!default_DSA_method) 82109998Smarkm default_DSA_method = DSA_OpenSSL(); 8359191Skris return default_DSA_method; 84109998Smarkm } 8559191Skris 8655714SkrisDSA *DSA_new(void) 87109998Smarkm { 8859191Skris return DSA_new_method(NULL); 89109998Smarkm } 9059191Skris 91109998Smarkmint DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 92109998Smarkm { 93109998Smarkm /* NB: The caller is specifically setting a method, so it's not up to us 94109998Smarkm * to deal with which ENGINE it comes from. */ 95109998Smarkm const DSA_METHOD *mtmp; 9659191Skris mtmp = dsa->meth; 9759191Skris if (mtmp->finish) mtmp->finish(dsa); 98111147Snectar#ifndef OPENSSL_NO_ENGINE 99109998Smarkm if (dsa->engine) 100109998Smarkm { 101109998Smarkm ENGINE_finish(dsa->engine); 102109998Smarkm dsa->engine = NULL; 103109998Smarkm } 104111147Snectar#endif 10559191Skris dsa->meth = meth; 10659191Skris if (meth->init) meth->init(dsa); 107109998Smarkm return 1; 108109998Smarkm } 10959191Skris 110109998SmarkmDSA *DSA_new_method(ENGINE *engine) 11155714Skris { 11255714Skris DSA *ret; 11355714Skris 11468651Skris ret=(DSA *)OPENSSL_malloc(sizeof(DSA)); 11555714Skris if (ret == NULL) 11655714Skris { 117109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 11855714Skris return(NULL); 11955714Skris } 120109998Smarkm ret->meth = DSA_get_default_method(); 121111147Snectar#ifndef OPENSSL_NO_ENGINE 122109998Smarkm if (engine) 123109998Smarkm { 124109998Smarkm if (!ENGINE_init(engine)) 125109998Smarkm { 126109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 127109998Smarkm OPENSSL_free(ret); 128109998Smarkm return NULL; 129109998Smarkm } 130109998Smarkm ret->engine = engine; 131109998Smarkm } 132109998Smarkm else 133109998Smarkm ret->engine = ENGINE_get_default_DSA(); 134109998Smarkm if(ret->engine) 135109998Smarkm { 136109998Smarkm ret->meth = ENGINE_get_DSA(ret->engine); 137109998Smarkm if(!ret->meth) 138109998Smarkm { 139109998Smarkm DSAerr(DSA_F_DSA_NEW_METHOD, 140109998Smarkm ERR_R_ENGINE_LIB); 141109998Smarkm ENGINE_finish(ret->engine); 142109998Smarkm OPENSSL_free(ret); 143109998Smarkm return NULL; 144109998Smarkm } 145109998Smarkm } 146111147Snectar#endif 147109998Smarkm 14855714Skris ret->pad=0; 14955714Skris ret->version=0; 15055714Skris ret->write_params=1; 15155714Skris ret->p=NULL; 15255714Skris ret->q=NULL; 15355714Skris ret->g=NULL; 15455714Skris 15555714Skris ret->pub_key=NULL; 15655714Skris ret->priv_key=NULL; 15755714Skris 15855714Skris ret->kinv=NULL; 15955714Skris ret->r=NULL; 16055714Skris ret->method_mont_p=NULL; 16155714Skris 16255714Skris ret->references=1; 16359191Skris ret->flags=ret->meth->flags; 164109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 16559191Skris if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 16659191Skris { 167111147Snectar#ifndef OPENSSL_NO_ENGINE 168109998Smarkm if (ret->engine) 169109998Smarkm ENGINE_finish(ret->engine); 170111147Snectar#endif 171109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 17268651Skris OPENSSL_free(ret); 17359191Skris ret=NULL; 17459191Skris } 17559191Skris 17655714Skris return(ret); 17755714Skris } 17855714Skris 17955714Skrisvoid DSA_free(DSA *r) 18055714Skris { 18155714Skris int i; 18255714Skris 18355714Skris if (r == NULL) return; 18455714Skris 18555714Skris i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); 18655714Skris#ifdef REF_PRINT 18755714Skris REF_PRINT("DSA",r); 18855714Skris#endif 18955714Skris if (i > 0) return; 19055714Skris#ifdef REF_CHECK 19155714Skris if (i < 0) 19255714Skris { 19355714Skris fprintf(stderr,"DSA_free, bad reference count\n"); 19455714Skris abort(); 19555714Skris } 19655714Skris#endif 19755714Skris 198109998Smarkm if(r->meth->finish) 199109998Smarkm r->meth->finish(r); 200111147Snectar#ifndef OPENSSL_NO_ENGINE 201109998Smarkm if(r->engine) 202109998Smarkm ENGINE_finish(r->engine); 203111147Snectar#endif 20476866Skris 205109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 20659191Skris 20755714Skris if (r->p != NULL) BN_clear_free(r->p); 20855714Skris if (r->q != NULL) BN_clear_free(r->q); 20955714Skris if (r->g != NULL) BN_clear_free(r->g); 21055714Skris if (r->pub_key != NULL) BN_clear_free(r->pub_key); 21155714Skris if (r->priv_key != NULL) BN_clear_free(r->priv_key); 21255714Skris if (r->kinv != NULL) BN_clear_free(r->kinv); 21355714Skris if (r->r != NULL) BN_clear_free(r->r); 21468651Skris OPENSSL_free(r); 21555714Skris } 21655714Skris 217109998Smarkmint DSA_up_ref(DSA *r) 21855714Skris { 219109998Smarkm int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 220109998Smarkm#ifdef REF_PRINT 221109998Smarkm REF_PRINT("DSA",r); 222109998Smarkm#endif 223109998Smarkm#ifdef REF_CHECK 224109998Smarkm if (i < 2) 225109998Smarkm { 226109998Smarkm fprintf(stderr, "DSA_up_ref, bad reference count\n"); 227109998Smarkm abort(); 228109998Smarkm } 229109998Smarkm#endif 230109998Smarkm return ((i > 1) ? 1 : 0); 231109998Smarkm } 232109998Smarkm 233109998Smarkmint DSA_size(const DSA *r) 234109998Smarkm { 23555714Skris int ret,i; 23655714Skris ASN1_INTEGER bs; 237109998Smarkm unsigned char buf[4]; /* 4 bytes looks really small. 238109998Smarkm However, i2d_ASN1_INTEGER() will not look 239109998Smarkm beyond the first byte, as long as the second 240109998Smarkm parameter is NULL. */ 24155714Skris 24255714Skris i=BN_num_bits(r->q); 24355714Skris bs.length=(i+7)/8; 24455714Skris bs.data=buf; 24555714Skris bs.type=V_ASN1_INTEGER; 24655714Skris /* If the top bit is set the asn1 encoding is 1 larger. */ 24755714Skris buf[0]=0xff; 24855714Skris 24955714Skris i=i2d_ASN1_INTEGER(&bs,NULL); 25055714Skris i+=i; /* r and s */ 25155714Skris ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); 25255714Skris return(ret); 25355714Skris } 25455714Skris 25559191Skrisint DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 25659191Skris CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 25759191Skris { 258109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 259109998Smarkm new_func, dup_func, free_func); 26059191Skris } 26159191Skris 26259191Skrisint DSA_set_ex_data(DSA *d, int idx, void *arg) 26359191Skris { 26459191Skris return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); 26559191Skris } 26659191Skris 26759191Skrisvoid *DSA_get_ex_data(DSA *d, int idx) 26859191Skris { 26959191Skris return(CRYPTO_get_ex_data(&d->ex_data,idx)); 27059191Skris } 27159191Skris 272109998Smarkm#ifndef OPENSSL_NO_DH 273109998SmarkmDH *DSA_dup_DH(const DSA *r) 27455714Skris { 27555714Skris /* DSA has p, q, g, optional pub_key, optional priv_key. 27655714Skris * DH has p, optional length, g, optional pub_key, optional priv_key. 27755714Skris */ 27855714Skris 27955714Skris DH *ret = NULL; 28055714Skris 28155714Skris if (r == NULL) 28255714Skris goto err; 28355714Skris ret = DH_new(); 28455714Skris if (ret == NULL) 28555714Skris goto err; 28655714Skris if (r->p != NULL) 28755714Skris if ((ret->p = BN_dup(r->p)) == NULL) 28855714Skris goto err; 28955714Skris if (r->q != NULL) 29055714Skris ret->length = BN_num_bits(r->q); 29155714Skris if (r->g != NULL) 29255714Skris if ((ret->g = BN_dup(r->g)) == NULL) 29355714Skris goto err; 29455714Skris if (r->pub_key != NULL) 29555714Skris if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) 29655714Skris goto err; 29755714Skris if (r->priv_key != NULL) 29855714Skris if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) 29955714Skris goto err; 30055714Skris 30155714Skris return ret; 30255714Skris 30355714Skris err: 30455714Skris if (ret != NULL) 30555714Skris DH_free(ret); 30655714Skris return NULL; 30755714Skris } 30855714Skris#endif 309