sl@0: /* crypto/dsa/dsatest.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: /* Until the key-gen callbacks are modified to use newer prototypes, we allow sl@0: * deprecated functions for openssl-internal code */ sl@0: #ifndef SYMBIAN sl@0: #ifdef OPENSSL_NO_DEPRECATED sl@0: #undef OPENSSL_NO_DEPRECATED sl@0: #endif sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifndef SYMBIAN sl@0: #include "../e_os.h" sl@0: #else sl@0: #include "e_os.h" sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef SYMBIAN sl@0: #ifdef stdin sl@0: #undef stdin sl@0: #endif sl@0: #ifdef stdout sl@0: #undef stdout sl@0: #endif sl@0: #ifdef stderr sl@0: #undef stderr sl@0: #endif sl@0: sl@0: #define stdin fp_stdin sl@0: #define stdout fp_stdout sl@0: #define stderr fp_stderr sl@0: sl@0: extern FILE *fp_stdout; sl@0: extern FILE *fp_stderr; sl@0: #endif sl@0: #ifdef OPENSSL_NO_DSA sl@0: int main(int argc, char *argv[]) sl@0: { sl@0: fprintf(stdout,"No DSA support\n"); sl@0: return(0); sl@0: } sl@0: #else sl@0: #include sl@0: sl@0: #ifdef OPENSSL_SYS_WIN16 sl@0: #define MS_CALLBACK _far _loadds sl@0: #else sl@0: #define MS_CALLBACK sl@0: #endif sl@0: sl@0: static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg); sl@0: sl@0: /* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to sl@0: * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */ sl@0: static unsigned char seed[20]={ sl@0: 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40, sl@0: 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3, sl@0: }; sl@0: sl@0: static unsigned char out_p[]={ sl@0: 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa, sl@0: 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb, sl@0: 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7, sl@0: 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5, sl@0: 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf, sl@0: 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac, sl@0: 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2, sl@0: 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91, sl@0: }; sl@0: sl@0: static unsigned char out_q[]={ sl@0: 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee, sl@0: 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e, sl@0: 0xda,0xce,0x91,0x5f, sl@0: }; sl@0: sl@0: static unsigned char out_g[]={ sl@0: 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13, sl@0: 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00, sl@0: 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb, sl@0: 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e, sl@0: 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf, sl@0: 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c, sl@0: 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c, sl@0: 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02, sl@0: }; sl@0: sl@0: static const unsigned char str1[]="12345678901234567890"; sl@0: sl@0: static const char rnd_seed[] = "string to make the random number generator think it has entropy"; sl@0: sl@0: static BIO *bio_err=NULL; sl@0: #ifndef SYMBIAN sl@0: int main(int argc, char **argv) sl@0: #else sl@0: int dsa_main(int argc, char **argv) sl@0: #endif sl@0: { sl@0: BN_GENCB cb; sl@0: DSA *dsa=NULL; sl@0: int counter,ret=0,i,j; sl@0: unsigned char buf[256]; sl@0: unsigned long h; sl@0: unsigned char sig[256]; sl@0: unsigned int siglen; sl@0: sl@0: if (bio_err == NULL) sl@0: bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); sl@0: if( bio_err == NULL&& errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: /*CRYPTO_malloc_debug_init(); sl@0: if( bio_err == NULL&& errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); sl@0: if( bio_err == NULL&& errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); sl@0: if( bio_err == NULL&& errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: */ sl@0: ERR_load_crypto_strings(); sl@0: RAND_seed(rnd_seed, sizeof rnd_seed); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: BIO_printf(bio_err,"test generation of DSA parameters\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: BN_GENCB_set(&cb, dsa_cb, bio_err); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: if(((dsa = DSA_new()) == NULL)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: } sl@0: if(!DSA_generate_parameters_ex(dsa, 512, sl@0: seed, 20, &counter, &h, &cb)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: if (dsa != NULL) sl@0: { sl@0: DSA_free(dsa); sl@0: sl@0: } sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: BIO_printf(bio_err,"seed\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: for (i=0; i<20; i+=4) sl@0: { sl@0: BIO_printf(bio_err,"%02X%02X%02X%02X ", sl@0: seed[i],seed[i+1],seed[i+2],seed[i+3]); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: } sl@0: BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: if (dsa == NULL) goto end; sl@0: DSA_print(bio_err,dsa,0); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: if (counter != 105) sl@0: { sl@0: BIO_printf(bio_err,"counter should be 105\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: if (h != 2) sl@0: { sl@0: BIO_printf(bio_err,"h should be 2\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: sl@0: i=BN_bn2bin(dsa->q,buf); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: j=sizeof(out_q); sl@0: if ((i != j) || (memcmp(buf,out_q,i) != 0)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: BIO_printf(bio_err,"q value is wrong\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: sl@0: i=BN_bn2bin(dsa->p,buf); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: j=sizeof(out_p); sl@0: if ((i != j) || (memcmp(buf,out_p,i) != 0)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: BIO_printf(bio_err,"p value is wrong\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: sl@0: i=BN_bn2bin(dsa->g,buf); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: j=sizeof(out_g); sl@0: if ((i != j) || (memcmp(buf,out_g,i) != 0)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: BIO_printf(bio_err,"g value is wrong\n"); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: goto end; sl@0: } sl@0: sl@0: dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME; sl@0: DSA_generate_key(dsa); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: DSA_sign(0, str1, 20, sig, &siglen, dsa); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) sl@0: ret=1; sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME; sl@0: DSA_generate_key(dsa); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: DSA_sign(0, str1, 20, sig, &siglen, dsa); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) sl@0: ret=1; sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: end: sl@0: if (!ret) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: } sl@0: if (dsa != NULL) sl@0: { sl@0: DSA_free(dsa); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: } sl@0: sl@0: CRYPTO_mem_leaks(bio_err); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: if (bio_err != NULL) sl@0: { sl@0: BIO_free(bio_err); sl@0: bio_err = NULL; sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: } sl@0: sl@0: #ifdef SYMBIAN sl@0: CRYPTO_cleanup_all_ex_data(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: ERR_remove_state(0); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: ERR_free_strings(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: #ifdef OPENSSL_SYS_NETWARE sl@0: if (!ret) fprintf(stdout,"ERROR\n"); sl@0: #endif sl@0: fprintf(stdout,"Test Case Passed!\n"); sl@0: fprintf(stderr,"Test Case Passed!\n"); sl@0: //EXIT(!ret); sl@0: return(0); sl@0: } sl@0: sl@0: static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg) sl@0: { sl@0: char c='*'; sl@0: static int ok=0,num=0; sl@0: sl@0: if (p == 0) { c='.'; num++; }; sl@0: if (p == 1) c='+'; sl@0: if (p == 2) { c='*'; ok++; } sl@0: if (p == 3) c='\n'; sl@0: BIO_write(arg->arg,&c,1); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: (void)BIO_flush(arg->arg); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: if (!ok && (p == 0) && (num > 1)) sl@0: { sl@0: BIO_printf((BIO *)arg,"error in dsatest\n"); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: #endif