diff --git a/testsuite/rsa-test.c b/testsuite/rsa-test.c index 3062fd991a6f0e6f4ed90f4e22d69a45208d3e98..40161738020bcfe6980f188920b5c95f71483f67 100644 --- a/testsuite/rsa-test.c +++ b/testsuite/rsa-test.c @@ -1,125 +1,5 @@ #include "testutils.h" -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#include <stdio.h> - -#if HAVE_LIBGMP -# include "rsa.h" -#endif - -#define SIGN(key, hash, msg, signature) do { \ - hash##_update(&hash, LDATA(msg)); \ - rsa_##hash##_sign(key, &hash, signature); \ -} while(0) - -#define VERIFY(key, hash, msg, signature) ( \ - hash##_update(&hash, LDATA(msg)), \ - rsa_##hash##_verify(key, &hash, signature) \ -) - -#if HAVE_LIBGMP - -/* Missing in current gmp */ -static void -mpz_togglebit (mpz_t x, unsigned long int bit) -{ - if (mpz_tstbit(x, bit)) - mpz_clrbit(x, bit); - else - mpz_setbit(x, bit); -} - -#endif /* HAVE_LIBGMP */ - -static void -test_rsa_md5(struct rsa_public_key *pub, - struct rsa_private_key *key, - mpz_t expected) -{ - struct md5_ctx md5; - mpz_t signature; - - md5_init(&md5); - mpz_init(signature); - - SIGN(key, md5, "The magic words are squeamish ossifrage", signature); - - if (verbose) - { - fprintf(stderr, "rsa-md5 signature: "); - mpz_out_str(stderr, 16, signature); - fprintf(stderr, "\n"); - } - - if (mpz_cmp(signature, expected)) - FAIL(); - - /* Try bad data */ - if (VERIFY(pub, md5, - "The magick words are squeamish ossifrage", signature)) - FAIL(); - - /* Try correct data */ - if (!VERIFY(pub, md5, - "The magic words are squeamish ossifrage", signature)) - FAIL(); - - /* Try bad signature */ - mpz_togglebit(signature, 17); - - if (VERIFY(pub, md5, - "The magic words are squeamish ossifrage", signature)) - FAIL(); - - mpz_clear(signature); -} - -static void -test_rsa_sha1(struct rsa_public_key *pub, - struct rsa_private_key *key, - mpz_t expected) -{ - struct sha1_ctx sha1; - mpz_t signature; - - sha1_init(&sha1); - mpz_init(signature); - - SIGN(key, sha1, "The magic words are squeamish ossifrage", signature); - - if (verbose) - { - fprintf(stderr, "rsa-sha1 signature: "); - mpz_out_str(stderr, 16, signature); - fprintf(stderr, "\n"); - } - - if (mpz_cmp(signature, expected)) - FAIL(); - - /* Try bad data */ - if (VERIFY(pub, sha1, - "The magick words are squeamish ossifrage", signature)) - FAIL(); - - /* Try correct data */ - if (!VERIFY(pub, sha1, - "The magic words are squeamish ossifrage", signature)) - FAIL(); - - /* Try bad signature */ - mpz_togglebit(signature, 17); - - if (VERIFY(pub, sha1, - "The magic words are squeamish ossifrage", signature)) - FAIL(); - - mpz_clear(signature); -} - int test_main(void) { diff --git a/testsuite/testutils.c b/testsuite/testutils.c index 41ae577137242f14e64520b5b41ccdc5f199b9c6..b6e4a785e7c1f321c196eeea4c32c1bf56653a40 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -1,5 +1,6 @@ /* testutils.c */ + #include "testutils.h" #include "cbc.h" @@ -220,3 +221,112 @@ test_hash(const struct nettle_hash *hash, if (buffer[hash->digest_size - 1]) FAIL(); } + +#if HAVE_LIBGMP +#define SIGN(key, hash, msg, signature) do { \ + hash##_update(&hash, LDATA(msg)); \ + rsa_##hash##_sign(key, &hash, signature); \ +} while(0) + +#define VERIFY(key, hash, msg, signature) ( \ + hash##_update(&hash, LDATA(msg)), \ + rsa_##hash##_verify(key, &hash, signature) \ +) + + +/* Missing in current gmp */ +static void +mpz_togglebit (mpz_t x, unsigned long int bit) +{ + if (mpz_tstbit(x, bit)) + mpz_clrbit(x, bit); + else + mpz_setbit(x, bit); +} + +void +test_rsa_md5(struct rsa_public_key *pub, + struct rsa_private_key *key, + mpz_t expected) +{ + struct md5_ctx md5; + mpz_t signature; + + md5_init(&md5); + mpz_init(signature); + + SIGN(key, md5, "The magic words are squeamish ossifrage", signature); + + if (verbose) + { + fprintf(stderr, "rsa-md5 signature: "); + mpz_out_str(stderr, 16, signature); + fprintf(stderr, "\n"); + } + + if (mpz_cmp(signature, expected)) + FAIL(); + + /* Try bad data */ + if (VERIFY(pub, md5, + "The magick words are squeamish ossifrage", signature)) + FAIL(); + + /* Try correct data */ + if (!VERIFY(pub, md5, + "The magic words are squeamish ossifrage", signature)) + FAIL(); + + /* Try bad signature */ + mpz_togglebit(signature, 17); + + if (VERIFY(pub, md5, + "The magic words are squeamish ossifrage", signature)) + FAIL(); + + mpz_clear(signature); +} + +void +test_rsa_sha1(struct rsa_public_key *pub, + struct rsa_private_key *key, + mpz_t expected) +{ + struct sha1_ctx sha1; + mpz_t signature; + + sha1_init(&sha1); + mpz_init(signature); + + SIGN(key, sha1, "The magic words are squeamish ossifrage", signature); + + if (verbose) + { + fprintf(stderr, "rsa-sha1 signature: "); + mpz_out_str(stderr, 16, signature); + fprintf(stderr, "\n"); + } + + if (mpz_cmp(signature, expected)) + FAIL(); + + /* Try bad data */ + if (VERIFY(pub, sha1, + "The magick words are squeamish ossifrage", signature)) + FAIL(); + + /* Try correct data */ + if (!VERIFY(pub, sha1, + "The magic words are squeamish ossifrage", signature)) + FAIL(); + + /* Try bad signature */ + mpz_togglebit(signature, 17); + + if (VERIFY(pub, sha1, + "The magic words are squeamish ossifrage", signature)) + FAIL(); + + mpz_clear(signature); +} +#endif /* HAVE_LIBGMP */ diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 759811521a4f2db3a18d260b5b66581d63a0f8d5..3c2756feb54c7e4629b19a7cfb0b24619b35b105 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -1,10 +1,19 @@ #ifndef NETTLE_TESTUTILS_H_INCLUDED #define NETTLE_TESTUTILS_H_INCLUDED +#if HAVE_CONFIG_H +# include "config.h" +#endif + #include <inttypes.h> #include <string.h> #include <stdlib.h> +#include <stdio.h> + +#if HAVE_LIBGMP +# include "rsa.h" +#endif #include "nettle-meta.h" @@ -49,6 +58,18 @@ test_hash(const struct nettle_hash *hash, const uint8_t *data, const uint8_t *digest); +#if HAVE_LIBGMP +void +test_rsa_md5(struct rsa_public_key *pub, + struct rsa_private_key *key, + mpz_t expected); + +void +test_rsa_sha1(struct rsa_public_key *pub, + struct rsa_private_key *key, + mpz_t expected); +#endif /* HAVE_LIBGMP */ + #define H2(d, s) decode_hex((d), (s)) #define H(x) decode_hex_dup(x) #define HL(x) decode_hex_length(x), decode_hex_dup(x)