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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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 67296465Sdelphij# include <openssl/engine.h> 68111147Snectar#endif 69160814Ssimon#ifndef OPENSSL_NO_DH 70296465Sdelphij# include <openssl/dh.h> 71160814Ssimon#endif 7255714Skris 73296465Sdelphijconst 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) 78296465Sdelphij{ 79194206Ssimon#ifdef OPENSSL_FIPS 80296465Sdelphij if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD)) { 81296465Sdelphij DSAerr(DSA_F_DSA_SET_DEFAULT_METHOD, DSA_R_NON_FIPS_METHOD); 82296465Sdelphij return; 83296465Sdelphij } 84194206Ssimon#endif 8559191Skris 86296465Sdelphij default_DSA_method = meth; 87296465Sdelphij} 88296465Sdelphij 89109998Smarkmconst DSA_METHOD *DSA_get_default_method(void) 90296465Sdelphij{ 91296465Sdelphij if (!default_DSA_method) 92296465Sdelphij default_DSA_method = DSA_OpenSSL(); 93296465Sdelphij return default_DSA_method; 94296465Sdelphij} 9559191Skris 9655714SkrisDSA *DSA_new(void) 97296465Sdelphij{ 98296465Sdelphij return DSA_new_method(NULL); 99296465Sdelphij} 10059191Skris 101109998Smarkmint DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 102296465Sdelphij{ 103296465Sdelphij /* 104296465Sdelphij * NB: The caller is specifically setting a method, so it's not up to us 105296465Sdelphij * to deal with which ENGINE it comes from. 106296465Sdelphij */ 107296465Sdelphij const DSA_METHOD *mtmp; 108194206Ssimon#ifdef OPENSSL_FIPS 109296465Sdelphij if (FIPS_mode() && !(meth->flags & DSA_FLAG_FIPS_METHOD)) { 110296465Sdelphij DSAerr(DSA_F_DSA_SET_METHOD, DSA_R_NON_FIPS_METHOD); 111296465Sdelphij return 0; 112296465Sdelphij } 113194206Ssimon#endif 114296465Sdelphij mtmp = dsa->meth; 115296465Sdelphij if (mtmp->finish) 116296465Sdelphij mtmp->finish(dsa); 117111147Snectar#ifndef OPENSSL_NO_ENGINE 118296465Sdelphij if (dsa->engine) { 119296465Sdelphij ENGINE_finish(dsa->engine); 120296465Sdelphij dsa->engine = NULL; 121296465Sdelphij } 122111147Snectar#endif 123296465Sdelphij dsa->meth = meth; 124296465Sdelphij if (meth->init) 125296465Sdelphij meth->init(dsa); 126296465Sdelphij return 1; 127296465Sdelphij} 12859191Skris 129109998SmarkmDSA *DSA_new_method(ENGINE *engine) 130296465Sdelphij{ 131296465Sdelphij DSA *ret; 13255714Skris 133296465Sdelphij ret = (DSA *)OPENSSL_malloc(sizeof(DSA)); 134296465Sdelphij if (ret == NULL) { 135296465Sdelphij DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); 136296465Sdelphij return (NULL); 137296465Sdelphij } 138296465Sdelphij ret->meth = DSA_get_default_method(); 139111147Snectar#ifndef OPENSSL_NO_ENGINE 140296465Sdelphij if (engine) { 141296465Sdelphij if (!ENGINE_init(engine)) { 142296465Sdelphij DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 143296465Sdelphij OPENSSL_free(ret); 144296465Sdelphij return NULL; 145296465Sdelphij } 146296465Sdelphij ret->engine = engine; 147296465Sdelphij } else 148296465Sdelphij ret->engine = ENGINE_get_default_DSA(); 149296465Sdelphij if (ret->engine) { 150296465Sdelphij ret->meth = ENGINE_get_DSA(ret->engine); 151296465Sdelphij if (!ret->meth) { 152296465Sdelphij DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 153296465Sdelphij ENGINE_finish(ret->engine); 154296465Sdelphij OPENSSL_free(ret); 155296465Sdelphij return NULL; 156296465Sdelphij } 157296465Sdelphij } 158111147Snectar#endif 159194206Ssimon#ifdef OPENSSL_FIPS 160296465Sdelphij if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD)) { 161296465Sdelphij DSAerr(DSA_F_DSA_NEW_METHOD, DSA_R_NON_FIPS_METHOD); 162296465Sdelphij# ifndef OPENSSL_NO_ENGINE 163296465Sdelphij if (ret->engine) 164296465Sdelphij ENGINE_finish(ret->engine); 165296465Sdelphij# endif 166296465Sdelphij OPENSSL_free(ret); 167296465Sdelphij return NULL; 168296465Sdelphij } 169194206Ssimon#endif 170109998Smarkm 171296465Sdelphij ret->pad = 0; 172296465Sdelphij ret->version = 0; 173296465Sdelphij ret->write_params = 1; 174296465Sdelphij ret->p = NULL; 175296465Sdelphij ret->q = NULL; 176296465Sdelphij ret->g = NULL; 17755714Skris 178296465Sdelphij ret->pub_key = NULL; 179296465Sdelphij ret->priv_key = NULL; 18055714Skris 181296465Sdelphij ret->kinv = NULL; 182296465Sdelphij ret->r = NULL; 183296465Sdelphij ret->method_mont_p = NULL; 18455714Skris 185296465Sdelphij ret->references = 1; 186296465Sdelphij ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; 187296465Sdelphij CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 188296465Sdelphij if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { 189111147Snectar#ifndef OPENSSL_NO_ENGINE 190296465Sdelphij if (ret->engine) 191296465Sdelphij ENGINE_finish(ret->engine); 192111147Snectar#endif 193296465Sdelphij CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 194296465Sdelphij OPENSSL_free(ret); 195296465Sdelphij ret = NULL; 196296465Sdelphij } 19755714Skris 198296465Sdelphij return (ret); 199296465Sdelphij} 200296465Sdelphij 20155714Skrisvoid DSA_free(DSA *r) 202296465Sdelphij{ 203296465Sdelphij int i; 20455714Skris 205296465Sdelphij if (r == NULL) 206296465Sdelphij return; 20755714Skris 208296465Sdelphij i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA); 20955714Skris#ifdef REF_PRINT 210296465Sdelphij REF_PRINT("DSA", r); 21155714Skris#endif 212296465Sdelphij if (i > 0) 213296465Sdelphij return; 21455714Skris#ifdef REF_CHECK 215296465Sdelphij if (i < 0) { 216296465Sdelphij fprintf(stderr, "DSA_free, bad reference count\n"); 217296465Sdelphij abort(); 218296465Sdelphij } 21955714Skris#endif 22055714Skris 221296465Sdelphij if (r->meth->finish) 222296465Sdelphij r->meth->finish(r); 223111147Snectar#ifndef OPENSSL_NO_ENGINE 224296465Sdelphij if (r->engine) 225296465Sdelphij ENGINE_finish(r->engine); 226111147Snectar#endif 22776866Skris 228296465Sdelphij CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 22959191Skris 230296465Sdelphij if (r->p != NULL) 231296465Sdelphij BN_clear_free(r->p); 232296465Sdelphij if (r->q != NULL) 233296465Sdelphij BN_clear_free(r->q); 234296465Sdelphij if (r->g != NULL) 235296465Sdelphij BN_clear_free(r->g); 236296465Sdelphij if (r->pub_key != NULL) 237296465Sdelphij BN_clear_free(r->pub_key); 238296465Sdelphij if (r->priv_key != NULL) 239296465Sdelphij BN_clear_free(r->priv_key); 240296465Sdelphij if (r->kinv != NULL) 241296465Sdelphij BN_clear_free(r->kinv); 242296465Sdelphij if (r->r != NULL) 243296465Sdelphij BN_clear_free(r->r); 244296465Sdelphij OPENSSL_free(r); 245296465Sdelphij} 24655714Skris 247109998Smarkmint DSA_up_ref(DSA *r) 248296465Sdelphij{ 249296465Sdelphij int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 250109998Smarkm#ifdef REF_PRINT 251296465Sdelphij REF_PRINT("DSA", r); 252109998Smarkm#endif 253109998Smarkm#ifdef REF_CHECK 254296465Sdelphij if (i < 2) { 255296465Sdelphij fprintf(stderr, "DSA_up_ref, bad reference count\n"); 256296465Sdelphij abort(); 257296465Sdelphij } 258109998Smarkm#endif 259296465Sdelphij return ((i > 1) ? 1 : 0); 260296465Sdelphij} 261109998Smarkm 26259191Skrisint DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 263296465Sdelphij CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 264296465Sdelphij{ 265296465Sdelphij return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 266296465Sdelphij new_func, dup_func, free_func); 267296465Sdelphij} 26859191Skris 26959191Skrisint DSA_set_ex_data(DSA *d, int idx, void *arg) 270296465Sdelphij{ 271296465Sdelphij return (CRYPTO_set_ex_data(&d->ex_data, idx, arg)); 272296465Sdelphij} 27359191Skris 27459191Skrisvoid *DSA_get_ex_data(DSA *d, int idx) 275296465Sdelphij{ 276296465Sdelphij return (CRYPTO_get_ex_data(&d->ex_data, idx)); 277296465Sdelphij} 27859191Skris 279109998Smarkm#ifndef OPENSSL_NO_DH 280109998SmarkmDH *DSA_dup_DH(const DSA *r) 281296465Sdelphij{ 282296465Sdelphij /* 283296465Sdelphij * DSA has p, q, g, optional pub_key, optional priv_key. DH has p, 284296465Sdelphij * optional length, g, optional pub_key, optional priv_key. 285296465Sdelphij */ 28655714Skris 287296465Sdelphij DH *ret = NULL; 28855714Skris 289296465Sdelphij if (r == NULL) 290296465Sdelphij goto err; 291296465Sdelphij ret = DH_new(); 292296465Sdelphij if (ret == NULL) 293296465Sdelphij goto err; 294296465Sdelphij if (r->p != NULL) 295296465Sdelphij if ((ret->p = BN_dup(r->p)) == NULL) 296296465Sdelphij goto err; 297296465Sdelphij if (r->q != NULL) 298296465Sdelphij ret->length = BN_num_bits(r->q); 299296465Sdelphij if (r->g != NULL) 300296465Sdelphij if ((ret->g = BN_dup(r->g)) == NULL) 301296465Sdelphij goto err; 302296465Sdelphij if (r->pub_key != NULL) 303296465Sdelphij if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) 304296465Sdelphij goto err; 305296465Sdelphij if (r->priv_key != NULL) 306296465Sdelphij if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) 307296465Sdelphij goto err; 30855714Skris 309296465Sdelphij return ret; 31055714Skris 31155714Skris err: 312296465Sdelphij if (ret != NULL) 313296465Sdelphij DH_free(ret); 314296465Sdelphij return NULL; 315296465Sdelphij} 31655714Skris#endif 317