10Sstevel@tonic-gate /* crypto/dsa/dsatest.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
59*2139Sjp161948 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
60*2139Sjp161948 * deprecated functions for openssl-internal code */
61*2139Sjp161948 #ifdef OPENSSL_NO_DEPRECATED
62*2139Sjp161948 #undef OPENSSL_NO_DEPRECATED
63*2139Sjp161948 #endif
64*2139Sjp161948
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include <stdlib.h>
670Sstevel@tonic-gate #include <string.h>
680Sstevel@tonic-gate #include <sys/types.h>
690Sstevel@tonic-gate #include <sys/stat.h>
700Sstevel@tonic-gate
710Sstevel@tonic-gate #include "../e_os.h"
720Sstevel@tonic-gate
730Sstevel@tonic-gate #include <openssl/crypto.h>
740Sstevel@tonic-gate #include <openssl/rand.h>
750Sstevel@tonic-gate #include <openssl/bio.h>
760Sstevel@tonic-gate #include <openssl/err.h>
77*2139Sjp161948 #include <openssl/bn.h>
780Sstevel@tonic-gate
790Sstevel@tonic-gate #ifdef OPENSSL_NO_DSA
main(int argc,char * argv[])800Sstevel@tonic-gate int main(int argc, char *argv[])
810Sstevel@tonic-gate {
820Sstevel@tonic-gate printf("No DSA support\n");
830Sstevel@tonic-gate return(0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate #include <openssl/dsa.h>
870Sstevel@tonic-gate
880Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN16
890Sstevel@tonic-gate #define MS_CALLBACK _far _loadds
900Sstevel@tonic-gate #else
910Sstevel@tonic-gate #define MS_CALLBACK
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate
94*2139Sjp161948 static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg);
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
970Sstevel@tonic-gate * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
980Sstevel@tonic-gate static unsigned char seed[20]={
990Sstevel@tonic-gate 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
1000Sstevel@tonic-gate 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate static unsigned char out_p[]={
1040Sstevel@tonic-gate 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
1050Sstevel@tonic-gate 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
1060Sstevel@tonic-gate 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
1070Sstevel@tonic-gate 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
1080Sstevel@tonic-gate 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
1090Sstevel@tonic-gate 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
1100Sstevel@tonic-gate 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
1110Sstevel@tonic-gate 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91,
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static unsigned char out_q[]={
1150Sstevel@tonic-gate 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
1160Sstevel@tonic-gate 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
1170Sstevel@tonic-gate 0xda,0xce,0x91,0x5f,
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate static unsigned char out_g[]={
1210Sstevel@tonic-gate 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
1220Sstevel@tonic-gate 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
1230Sstevel@tonic-gate 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
1240Sstevel@tonic-gate 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
1250Sstevel@tonic-gate 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
1260Sstevel@tonic-gate 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
1270Sstevel@tonic-gate 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
1280Sstevel@tonic-gate 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02,
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static const unsigned char str1[]="12345678901234567890";
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static BIO *bio_err=NULL;
1360Sstevel@tonic-gate
main(int argc,char ** argv)1370Sstevel@tonic-gate int main(int argc, char **argv)
1380Sstevel@tonic-gate {
139*2139Sjp161948 BN_GENCB cb;
1400Sstevel@tonic-gate DSA *dsa=NULL;
1410Sstevel@tonic-gate int counter,ret=0,i,j;
1420Sstevel@tonic-gate unsigned char buf[256];
1430Sstevel@tonic-gate unsigned long h;
1440Sstevel@tonic-gate unsigned char sig[256];
1450Sstevel@tonic-gate unsigned int siglen;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (bio_err == NULL)
1480Sstevel@tonic-gate bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate CRYPTO_malloc_debug_init();
1510Sstevel@tonic-gate CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
1520Sstevel@tonic-gate CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate ERR_load_crypto_strings();
1550Sstevel@tonic-gate RAND_seed(rnd_seed, sizeof rnd_seed);
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate BIO_printf(bio_err,"test generation of DSA parameters\n");
1580Sstevel@tonic-gate
159*2139Sjp161948 BN_GENCB_set(&cb, dsa_cb, bio_err);
160*2139Sjp161948 if(((dsa = DSA_new()) == NULL) || !DSA_generate_parameters_ex(dsa, 512,
161*2139Sjp161948 seed, 20, &counter, &h, &cb))
162*2139Sjp161948 goto end;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate BIO_printf(bio_err,"seed\n");
1650Sstevel@tonic-gate for (i=0; i<20; i+=4)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate BIO_printf(bio_err,"%02X%02X%02X%02X ",
1680Sstevel@tonic-gate seed[i],seed[i+1],seed[i+2],seed[i+3]);
1690Sstevel@tonic-gate }
170*2139Sjp161948 BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate if (dsa == NULL) goto end;
1730Sstevel@tonic-gate DSA_print(bio_err,dsa,0);
1740Sstevel@tonic-gate if (counter != 105)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate BIO_printf(bio_err,"counter should be 105\n");
1770Sstevel@tonic-gate goto end;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate if (h != 2)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate BIO_printf(bio_err,"h should be 2\n");
1820Sstevel@tonic-gate goto end;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate i=BN_bn2bin(dsa->q,buf);
1860Sstevel@tonic-gate j=sizeof(out_q);
1870Sstevel@tonic-gate if ((i != j) || (memcmp(buf,out_q,i) != 0))
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate BIO_printf(bio_err,"q value is wrong\n");
1900Sstevel@tonic-gate goto end;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate i=BN_bn2bin(dsa->p,buf);
1940Sstevel@tonic-gate j=sizeof(out_p);
1950Sstevel@tonic-gate if ((i != j) || (memcmp(buf,out_p,i) != 0))
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate BIO_printf(bio_err,"p value is wrong\n");
1980Sstevel@tonic-gate goto end;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate i=BN_bn2bin(dsa->g,buf);
2020Sstevel@tonic-gate j=sizeof(out_g);
2030Sstevel@tonic-gate if ((i != j) || (memcmp(buf,out_g,i) != 0))
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate BIO_printf(bio_err,"g value is wrong\n");
2060Sstevel@tonic-gate goto end;
2070Sstevel@tonic-gate }
208*2139Sjp161948
209*2139Sjp161948 dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME;
2100Sstevel@tonic-gate DSA_generate_key(dsa);
2110Sstevel@tonic-gate DSA_sign(0, str1, 20, sig, &siglen, dsa);
2120Sstevel@tonic-gate if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
2130Sstevel@tonic-gate ret=1;
214*2139Sjp161948
215*2139Sjp161948 dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME;
216*2139Sjp161948 DSA_generate_key(dsa);
217*2139Sjp161948 DSA_sign(0, str1, 20, sig, &siglen, dsa);
218*2139Sjp161948 if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
219*2139Sjp161948 ret=1;
220*2139Sjp161948
2210Sstevel@tonic-gate end:
2220Sstevel@tonic-gate if (!ret)
2230Sstevel@tonic-gate ERR_print_errors(bio_err);
2240Sstevel@tonic-gate if (dsa != NULL) DSA_free(dsa);
2250Sstevel@tonic-gate CRYPTO_cleanup_all_ex_data();
2260Sstevel@tonic-gate ERR_remove_state(0);
2270Sstevel@tonic-gate ERR_free_strings();
2280Sstevel@tonic-gate CRYPTO_mem_leaks(bio_err);
2290Sstevel@tonic-gate if (bio_err != NULL)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate BIO_free(bio_err);
2320Sstevel@tonic-gate bio_err = NULL;
2330Sstevel@tonic-gate }
234*2139Sjp161948 #ifdef OPENSSL_SYS_NETWARE
235*2139Sjp161948 if (!ret) printf("ERROR\n");
236*2139Sjp161948 #endif
2370Sstevel@tonic-gate EXIT(!ret);
2380Sstevel@tonic-gate return(0);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
dsa_cb(int p,int n,BN_GENCB * arg)241*2139Sjp161948 static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate char c='*';
2440Sstevel@tonic-gate static int ok=0,num=0;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if (p == 0) { c='.'; num++; };
2470Sstevel@tonic-gate if (p == 1) c='+';
2480Sstevel@tonic-gate if (p == 2) { c='*'; ok++; }
2490Sstevel@tonic-gate if (p == 3) c='\n';
250*2139Sjp161948 BIO_write(arg->arg,&c,1);
251*2139Sjp161948 (void)BIO_flush(arg->arg);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if (!ok && (p == 0) && (num > 1))
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate BIO_printf((BIO *)arg,"error in dsatest\n");
256*2139Sjp161948 return 0;
2570Sstevel@tonic-gate }
258*2139Sjp161948 return 1;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate #endif
261