X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fdsatest.c;h=23991fa096008b451c581d85f7da31b4d8c7951e;hp=1f4b8379c4d7712fb0027ea2aab8f5fc37c38663;hb=1f483a69bce11c940309edc437eee6e32294d5f2;hpb=0f81f5f78c06bd69a96deb66cd30e50a67310ef2 diff --git a/test/dsatest.c b/test/dsatest.c index 1f4b8379c4..23991fa096 100644 --- a/test/dsatest.c +++ b/test/dsatest.c @@ -1,59 +1,10 @@ -/* crypto/dsa/dsatest.c */ -/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) - * All rights reserved. - * - * This package is an SSL implementation written - * by Eric Young (eay@cryptsoft.com). - * The implementation was written so as to conform with Netscapes SSL. - * - * This library is free for commercial and non-commercial use as long as - * the following conditions are aheared to. The following conditions - * apply to all code found in this distribution, be it the RC4, RSA, - * lhash, DES, etc., code; not just the SSL code. The SSL documentation - * included with this distribution is covered by the same copyright terms - * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * - * Copyright remains Eric Young's, and as such any Copyright notices in - * the code are not to be removed. - * If this package is used in a product, Eric Young should be given attribution - * as the author of the parts of the library used. - * This can be in the form of a textual message at program startup or - * in documentation (online or textual) provided with the package. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * "This product includes cryptographic software written by - * Eric Young (eay@cryptsoft.com)" - * The word 'cryptographic' can be left out if the rouines from the library - * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from - * the apps directory (application code) you must include an acknowledgement: - * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * - * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. +/* + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. * - * The licence and distribution terms for any publically available version or - * derivative of this code cannot be changed. i.e. this code cannot simply be - * copied and put under another distribution licence - * [including the GNU Public Licence.] + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html */ #include @@ -62,23 +13,15 @@ #include #include -#include "../e_os.h" - #include #include -#include -#include #include +#include -#ifdef OPENSSL_NO_DSA -int main(int argc, char *argv[]) -{ - printf("No DSA support\n"); - return (0); -} -#else -# include +#include "testutil.h" +#include "internal/nelem.h" +#ifndef OPENSSL_NO_DSA static int dsa_cb(int p, int n, BN_GENCB *arg); /* @@ -120,12 +63,7 @@ static unsigned char out_g[] = { static const unsigned char str1[] = "12345678901234567890"; -static const char rnd_seed[] = - "string to make the random number generator think it has entropy"; - -static BIO *bio_err = NULL; - -int main(int argc, char **argv) +static int dsa_test(void) { BN_GENCB *cb; DSA *dsa = NULL; @@ -134,122 +72,70 @@ int main(int argc, char **argv) unsigned long h; unsigned char sig[256]; unsigned int siglen; + const BIGNUM *p = NULL, *q = NULL, *g = NULL; - if (bio_err == NULL) - bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); - - CRYPTO_malloc_debug_init(); - CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); - CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); - - ERR_load_crypto_strings(); - RAND_seed(rnd_seed, sizeof rnd_seed); - - BIO_printf(bio_err, "test generation of DSA parameters\n"); - - cb = BN_GENCB_new(); - if (!cb) + if (!TEST_ptr(cb = BN_GENCB_new())) goto end; - BN_GENCB_set(cb, dsa_cb, bio_err); - if (((dsa = DSA_new()) == NULL) || !DSA_generate_parameters_ex(dsa, 512, - seed, 20, - &counter, - &h, cb)) + BN_GENCB_set(cb, dsa_cb, NULL); + if (!TEST_ptr(dsa = DSA_new()) + || !TEST_true(DSA_generate_parameters_ex(dsa, 512, seed, 20, + &counter, &h, cb))) goto end; - BIO_printf(bio_err, "seed\n"); - for (i = 0; i < 20; i += 4) { - BIO_printf(bio_err, "%02X%02X%02X%02X ", - seed[i], seed[i + 1], seed[i + 2], seed[i + 3]); - } - BIO_printf(bio_err, "\ncounter=%d h=%ld\n", counter, h); - - DSA_print(bio_err, dsa, 0); - if (counter != 105) { - BIO_printf(bio_err, "counter should be 105\n"); + if (!TEST_int_eq(counter, 105)) goto end; - } - if (h != 2) { - BIO_printf(bio_err, "h should be 2\n"); + if (!TEST_int_eq(h, 2)) goto end; - } - i = BN_bn2bin(dsa->q, buf); + DSA_get0_pqg(dsa, &p, &q, &g); + i = BN_bn2bin(q, buf); j = sizeof(out_q); - if ((i != j) || (memcmp(buf, out_q, i) != 0)) { - BIO_printf(bio_err, "q value is wrong\n"); + if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_q, i)) goto end; - } - i = BN_bn2bin(dsa->p, buf); + i = BN_bn2bin(p, buf); j = sizeof(out_p); - if ((i != j) || (memcmp(buf, out_p, i) != 0)) { - BIO_printf(bio_err, "p value is wrong\n"); + if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_p, i)) goto end; - } - i = BN_bn2bin(dsa->g, buf); + i = BN_bn2bin(g, buf); j = sizeof(out_g); - if ((i != j) || (memcmp(buf, out_g, i) != 0)) { - BIO_printf(bio_err, "g value is wrong\n"); + if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_g, i)) goto end; - } - dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME; DSA_generate_key(dsa); DSA_sign(0, str1, 20, sig, &siglen, dsa); - if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) - ret = 1; - - dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME; - DSA_generate_key(dsa); - DSA_sign(0, str1, 20, sig, &siglen, dsa); - if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) + if (TEST_true(DSA_verify(0, str1, 20, sig, siglen, dsa))) ret = 1; end: - if (!ret) - ERR_print_errors(bio_err); DSA_free(dsa); BN_GENCB_free(cb); - CRYPTO_cleanup_all_ex_data(); - ERR_remove_thread_state(NULL); - ERR_free_strings(); - CRYPTO_mem_leaks(bio_err); - BIO_free(bio_err); - bio_err = NULL; -# ifdef OPENSSL_SYS_NETWARE - if (!ret) - printf("ERROR\n"); -# endif - EXIT(!ret); + return ret; } static int dsa_cb(int p, int n, BN_GENCB *arg) { - char c = '*'; static int ok = 0, num = 0; - if (p == 0) { - c = '.'; + if (p == 0) num++; - }; - if (p == 1) - c = '+'; - if (p == 2) { - c = '*'; + if (p == 2) ok++; - } - if (p == 3) - c = '\n'; - BIO_write(BN_GENCB_get_arg(arg), &c, 1); - (void)BIO_flush(BN_GENCB_get_arg(arg)); if (!ok && (p == 0) && (num > 1)) { - BIO_printf((BIO *)arg, "error in dsatest\n"); + TEST_error("dsa_cb error"); return 0; } return 1; } +#endif /* OPENSSL_NO_DSA */ + +int setup_tests(void) +{ +#ifndef OPENSSL_NO_DSA + ADD_TEST(dsa_test); #endif + return 1; +}