xref: /openbsd-src/regress/lib/libcrypto/dsa/dsatest.c (revision a1db19540bb0b11f304d500473fd51b6d11c5dc4)
1*a1db1954Stb /*	$OpenBSD: dsatest.c,v 1.11 2024/02/29 20:04:43 tb Exp $	*/
23c6bd008Smiod /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33c6bd008Smiod  * All rights reserved.
43c6bd008Smiod  *
53c6bd008Smiod  * This package is an SSL implementation written
63c6bd008Smiod  * by Eric Young (eay@cryptsoft.com).
73c6bd008Smiod  * The implementation was written so as to conform with Netscapes SSL.
83c6bd008Smiod  *
93c6bd008Smiod  * This library is free for commercial and non-commercial use as long as
103c6bd008Smiod  * the following conditions are aheared to.  The following conditions
113c6bd008Smiod  * apply to all code found in this distribution, be it the RC4, RSA,
123c6bd008Smiod  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
133c6bd008Smiod  * included with this distribution is covered by the same copyright terms
143c6bd008Smiod  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
153c6bd008Smiod  *
163c6bd008Smiod  * Copyright remains Eric Young's, and as such any Copyright notices in
173c6bd008Smiod  * the code are not to be removed.
183c6bd008Smiod  * If this package is used in a product, Eric Young should be given attribution
193c6bd008Smiod  * as the author of the parts of the library used.
203c6bd008Smiod  * This can be in the form of a textual message at program startup or
213c6bd008Smiod  * in documentation (online or textual) provided with the package.
223c6bd008Smiod  *
233c6bd008Smiod  * Redistribution and use in source and binary forms, with or without
243c6bd008Smiod  * modification, are permitted provided that the following conditions
253c6bd008Smiod  * are met:
263c6bd008Smiod  * 1. Redistributions of source code must retain the copyright
273c6bd008Smiod  *    notice, this list of conditions and the following disclaimer.
283c6bd008Smiod  * 2. Redistributions in binary form must reproduce the above copyright
293c6bd008Smiod  *    notice, this list of conditions and the following disclaimer in the
303c6bd008Smiod  *    documentation and/or other materials provided with the distribution.
313c6bd008Smiod  * 3. All advertising materials mentioning features or use of this software
323c6bd008Smiod  *    must display the following acknowledgement:
333c6bd008Smiod  *    "This product includes cryptographic software written by
343c6bd008Smiod  *     Eric Young (eay@cryptsoft.com)"
353c6bd008Smiod  *    The word 'cryptographic' can be left out if the rouines from the library
363c6bd008Smiod  *    being used are not cryptographic related :-).
373c6bd008Smiod  * 4. If you include any Windows specific code (or a derivative thereof) from
383c6bd008Smiod  *    the apps directory (application code) you must include an acknowledgement:
393c6bd008Smiod  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
403c6bd008Smiod  *
413c6bd008Smiod  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
423c6bd008Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
433c6bd008Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
443c6bd008Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
453c6bd008Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
463c6bd008Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
473c6bd008Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
483c6bd008Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
493c6bd008Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
503c6bd008Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
513c6bd008Smiod  * SUCH DAMAGE.
523c6bd008Smiod  *
533c6bd008Smiod  * The licence and distribution terms for any publically available version or
543c6bd008Smiod  * derivative of this code cannot be changed.  i.e. this code cannot simply be
553c6bd008Smiod  * copied and put under another distribution licence
563c6bd008Smiod  * [including the GNU Public Licence.]
573c6bd008Smiod  */
583c6bd008Smiod 
597b57ed75Stb #include <err.h>
603c6bd008Smiod #include <stdio.h>
613c6bd008Smiod #include <stdlib.h>
623c6bd008Smiod #include <string.h>
633c6bd008Smiod #include <sys/types.h>
643c6bd008Smiod #include <sys/stat.h>
653c6bd008Smiod 
663c6bd008Smiod #include <openssl/crypto.h>
673c6bd008Smiod #include <openssl/bio.h>
683c6bd008Smiod #include <openssl/err.h>
693c6bd008Smiod #include <openssl/bn.h>
703c6bd008Smiod 
713c6bd008Smiod #include <openssl/dsa.h>
723c6bd008Smiod 
733c6bd008Smiod static int dsa_cb(int p, int n, BN_GENCB *arg);
743c6bd008Smiod 
753c6bd008Smiod /* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
763c6bd008Smiod  * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
773c6bd008Smiod static unsigned char seed[20] = {
783c6bd008Smiod 	0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, 0xb6, 0x21, 0x1b, 0x40,
793c6bd008Smiod 	0x62, 0xba, 0x32, 0x24, 0xe0, 0x42, 0x7d, 0xd3,
803c6bd008Smiod };
813c6bd008Smiod 
823c6bd008Smiod static unsigned char out_p[] = {
833c6bd008Smiod 	0x8d, 0xf2, 0xa4, 0x94, 0x49, 0x22, 0x76, 0xaa,
843c6bd008Smiod 	0x3d, 0x25, 0x75, 0x9b, 0xb0, 0x68, 0x69, 0xcb,
853c6bd008Smiod 	0xea, 0xc0, 0xd8, 0x3a, 0xfb, 0x8d, 0x0c, 0xf7,
863c6bd008Smiod 	0xcb, 0xb8, 0x32, 0x4f, 0x0d, 0x78, 0x82, 0xe5,
873c6bd008Smiod 	0xd0, 0x76, 0x2f, 0xc5, 0xb7, 0x21, 0x0e, 0xaf,
883c6bd008Smiod 	0xc2, 0xe9, 0xad, 0xac, 0x32, 0xab, 0x7a, 0xac,
893c6bd008Smiod 	0x49, 0x69, 0x3d, 0xfb, 0xf8, 0x37, 0x24, 0xc2,
903c6bd008Smiod 	0xec, 0x07, 0x36, 0xee, 0x31, 0xc8, 0x02, 0x91,
913c6bd008Smiod };
923c6bd008Smiod 
933c6bd008Smiod static unsigned char out_q[] = {
943c6bd008Smiod 	0xc7, 0x73, 0x21, 0x8c, 0x73, 0x7e, 0xc8, 0xee,
953c6bd008Smiod 	0x99, 0x3b, 0x4f, 0x2d, 0xed, 0x30, 0xf4, 0x8e,
963c6bd008Smiod 	0xda, 0xce, 0x91, 0x5f,
973c6bd008Smiod };
983c6bd008Smiod 
993c6bd008Smiod static unsigned char out_g[] = {
1003c6bd008Smiod 	0x62, 0x6d, 0x02, 0x78, 0x39, 0xea, 0x0a, 0x13,
1013c6bd008Smiod 	0x41, 0x31, 0x63, 0xa5, 0x5b, 0x4c, 0xb5, 0x00,
1023c6bd008Smiod 	0x29, 0x9d, 0x55, 0x22, 0x95, 0x6c, 0xef, 0xcb,
1033c6bd008Smiod 	0x3b, 0xff, 0x10, 0xf3, 0x99, 0xce, 0x2c, 0x2e,
1043c6bd008Smiod 	0x71, 0xcb, 0x9d, 0xe5, 0xfa, 0x24, 0xba, 0xbf,
1053c6bd008Smiod 	0x58, 0xe5, 0xb7, 0x95, 0x21, 0x92, 0x5c, 0x9c,
1063c6bd008Smiod 	0xc4, 0x2e, 0x9f, 0x6f, 0x46, 0x4b, 0x08, 0x8c,
1073c6bd008Smiod 	0xc5, 0x72, 0xaf, 0x53, 0xe6, 0xd7, 0x88, 0x02,
1083c6bd008Smiod };
1093c6bd008Smiod 
1103c6bd008Smiod static const unsigned char str1[] = "12345678901234567890";
1113c6bd008Smiod 
1123c6bd008Smiod static BIO *bio_err = NULL;
1133c6bd008Smiod 
1147b57ed75Stb int
main(int argc,char ** argv)1157b57ed75Stb main(int argc, char **argv)
1163c6bd008Smiod {
1177b57ed75Stb 	BN_GENCB *cb;
1183c6bd008Smiod 	DSA *dsa = NULL;
1197b57ed75Stb 	int counter, i, j;
1203c6bd008Smiod 	unsigned char buf[256];
1213c6bd008Smiod 	unsigned long h;
1223c6bd008Smiod 	unsigned char sig[256];
1233c6bd008Smiod 	unsigned int siglen;
1247b57ed75Stb 	int ret = 0;
1253c6bd008Smiod 
1263c6bd008Smiod 	if (bio_err == NULL)
1273c6bd008Smiod 		bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1283c6bd008Smiod 
1293c6bd008Smiod 	ERR_load_crypto_strings();
1303c6bd008Smiod 
1313c6bd008Smiod 	BIO_printf(bio_err, "test generation of DSA parameters\n");
1323c6bd008Smiod 
1337b57ed75Stb 	if ((cb = BN_GENCB_new()) == NULL)
1347b57ed75Stb 		goto end;
1357b57ed75Stb 
1367b57ed75Stb 	BN_GENCB_set(cb, dsa_cb, bio_err);
1377b57ed75Stb 	if ((dsa = DSA_new()) == NULL)
1387b57ed75Stb 		goto end;
1397b57ed75Stb 
140c75241f9Stb 	if (DSA_get0_engine(dsa) != NULL) {
141c75241f9Stb 		BIO_printf(bio_err, "ENGINE was not NULL\n");
142c75241f9Stb 		goto end;
143c75241f9Stb 	}
144c75241f9Stb 
1457b57ed75Stb 	if (!DSA_generate_parameters_ex(dsa, 512, seed, 20, &counter, &h, cb))
1463c6bd008Smiod 		goto end;
1473c6bd008Smiod 
1483c6bd008Smiod 	BIO_printf(bio_err, "seed\n");
1497b57ed75Stb 	for (i = 0; i < 20; i += 4) {
1503c6bd008Smiod 		BIO_printf(bio_err, "%02X%02X%02X%02X ",
1513c6bd008Smiod 			seed[i], seed[i + 1], seed[i + 2], seed[i + 3]);
1523c6bd008Smiod 	}
1533c6bd008Smiod 	BIO_printf(bio_err, "\ncounter=%d h=%ld\n", counter, h);
1543c6bd008Smiod 
1553c6bd008Smiod 	DSA_print(bio_err, dsa, 0);
1567b57ed75Stb 	if (counter != 105) {
1573c6bd008Smiod 		BIO_printf(bio_err, "counter should be 105\n");
1583c6bd008Smiod 		goto end;
1593c6bd008Smiod 	}
1607b57ed75Stb 	if (h != 2) {
1613c6bd008Smiod 		BIO_printf(bio_err, "h should be 2\n");
1623c6bd008Smiod 		goto end;
1633c6bd008Smiod 	}
1643c6bd008Smiod 
165386a8f7dStb 	i = BN_bn2bin(DSA_get0_q(dsa), buf);
1663c6bd008Smiod 	j = sizeof(out_q);
1677b57ed75Stb 	if ((i != j) || (memcmp(buf, out_q, i) != 0)) {
1683c6bd008Smiod 		BIO_printf(bio_err, "q value is wrong\n");
1693c6bd008Smiod 		goto end;
1703c6bd008Smiod 	}
1713c6bd008Smiod 
172386a8f7dStb 	i = BN_bn2bin(DSA_get0_p(dsa), buf);
1733c6bd008Smiod 	j = sizeof(out_p);
1747b57ed75Stb 	if ((i != j) || (memcmp(buf, out_p, i) != 0)) {
1753c6bd008Smiod 		BIO_printf(bio_err, "p value is wrong\n");
1763c6bd008Smiod 		goto end;
1773c6bd008Smiod 	}
1783c6bd008Smiod 
179386a8f7dStb 	i = BN_bn2bin(DSA_get0_g(dsa), buf);
1803c6bd008Smiod 	j = sizeof(out_g);
1817b57ed75Stb 	if ((i != j) || (memcmp(buf, out_g, i) != 0)) {
1823c6bd008Smiod 		BIO_printf(bio_err, "g value is wrong\n");
1833c6bd008Smiod 		goto end;
1843c6bd008Smiod 	}
1853c6bd008Smiod 
1863c6bd008Smiod 	DSA_generate_key(dsa);
1873c6bd008Smiod 	DSA_sign(0, str1, 20, sig, &siglen, dsa);
1883c6bd008Smiod 	if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
1893c6bd008Smiod 		ret = 1;
1903c6bd008Smiod 
1913c6bd008Smiod end:
1927b57ed75Stb 	BN_GENCB_free(cb);
1933c6bd008Smiod 	if (!ret)
1943c6bd008Smiod 		ERR_print_errors(bio_err);
1957b57ed75Stb 	DSA_free(dsa);
1963c6bd008Smiod 	CRYPTO_cleanup_all_ex_data();
1973c6bd008Smiod 	ERR_remove_thread_state(NULL);
1983c6bd008Smiod 	ERR_free_strings();
1993c6bd008Smiod 	BIO_free(bio_err);
2003c6bd008Smiod 	bio_err = NULL;
2017b57ed75Stb 
2027b57ed75Stb 	return !ret;
2033c6bd008Smiod }
2043c6bd008Smiod 
2057b57ed75Stb static int
dsa_cb(int p,int n,BN_GENCB * arg)2067b57ed75Stb dsa_cb(int p, int n, BN_GENCB *arg)
2073c6bd008Smiod {
2083c6bd008Smiod 	char c = '*';
2093c6bd008Smiod 	static int ok = 0, num = 0;
2103c6bd008Smiod 
2117b57ed75Stb 	if (p == 0) {
2127b57ed75Stb 		c = '.';
2137b57ed75Stb 		num++;
2147b57ed75Stb 	}
2157b57ed75Stb 	if (p == 1)
2167b57ed75Stb 		c = '+';
2177b57ed75Stb 	if (p == 2) {
2187b57ed75Stb 		c = '*';
2197b57ed75Stb 		ok++;
2207b57ed75Stb 	}
2213c6bd008Smiod 
2227b57ed75Stb 	if (p == 3)
2237b57ed75Stb 		c = '\n';
2247b57ed75Stb 	BIO_write(BN_GENCB_get_arg(arg), &c, 1);
2257b57ed75Stb 	(void)BIO_flush(BN_GENCB_get_arg(arg));
2267b57ed75Stb 
2277b57ed75Stb 	if (!ok && (p == 0) && (num > 1)) {
2283c6bd008Smiod 		BIO_printf((BIO *)arg, "error in dsatest\n");
2293c6bd008Smiod 		return 0;
2303c6bd008Smiod 	}
2313c6bd008Smiod 	return 1;
2323c6bd008Smiod }
233