X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=test%2Fdsatest.c;h=23991fa096008b451c581d85f7da31b4d8c7951e;hb=4460ad90af0338abe31286f29b36baf2e41abf19;hp=c64a91138ca62bf9767276e9023836504bd268dc;hpb=440e5d805f449d662520313b33fd90aeee86980b;p=oweals%2Fopenssl.git diff --git a/test/dsatest.c b/test/dsatest.c index c64a91138c..23991fa096 100644 --- a/test/dsatest.c +++ b/test/dsatest.c @@ -1,7 +1,7 @@ /* - * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. * - * Licensed under the OpenSSL license (the "License"). You may not use + * 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 @@ -13,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); /* @@ -71,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; @@ -85,119 +72,70 @@ int main(int argc, char **argv) unsigned long h; unsigned char sig[256]; unsigned int siglen; - BIGNUM *p = NULL, *q = NULL, *g = NULL; - - if (bio_err == NULL) - bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); - - CRYPTO_set_mem_debug(1); - CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); + const BIGNUM *p = NULL, *q = NULL, *g = NULL; - 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; - } 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(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(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_set_flags(dsa, 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_clear_flags(dsa, 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); - -#ifndef OPENSSL_NO_CRYPTO_MDEBUG - if (CRYPTO_mem_leaks(bio_err) <= 0) - ret = 0; -#endif - BIO_free(bio_err); - bio_err = NULL; - 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(BN_GENCB_get_arg(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; +}