fips_ecdsa_selftest.c 1.3 KB
Newer Older
D
Dr. Stephen Henson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/* fips/ecdsa/fips_ecdsa_selftest.c */

#define OPENSSL_FIPSAPI

#include <string.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/fips.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/bn.h>

#ifdef OPENSSL_FIPS

static const unsigned char str1[]="12345678901234567890";

static int corrupt_ecdsa = 0;

void FIPS_corrupt_ecdsa()
    {
    corrupt_ecdsa = 1;
    }

int FIPS_selftest_ecdsa()
    {
    EC_KEY *ec=NULL;
    int ret = 0;
    EVP_MD_CTX mctx;
    ECDSA_SIG *esig = NULL;

    FIPS_md_ctx_init(&mctx);

    ec = EC_KEY_new_by_curve_name(NID_secp384r1);

    if(ec == NULL)
	goto err;

    EC_KEY_generate_key(ec);

    if (!FIPS_digestinit(&mctx, EVP_sha512()))
	goto err;
    if (!FIPS_digestupdate(&mctx, str1, 20))
	goto err;
    esig = FIPS_ecdsa_sign_ctx(ec, &mctx);
    if (!esig)
	goto err;

    if (corrupt_ecdsa)
	BN_add_word(esig->r, 1);

    if (!FIPS_digestinit(&mctx, EVP_sha512()))
	goto err;
    if (!FIPS_digestupdate(&mctx, str1, 20))
	goto err;
    if (FIPS_ecdsa_verify_ctx(ec, &mctx, esig) != 1)
	goto err;

    ret = 1;

    err:
    FIPS_md_ctx_cleanup(&mctx);
    if (ec)
	EC_KEY_free(ec);
    if (esig)
	FIPS_ecdsa_sig_free(esig);
    if (ret == 0)
	    FIPSerr(FIPS_F_FIPS_SELFTEST_ECDSA,FIPS_R_SELFTEST_FAILED);
    return ret;
    }
#endif