rsa-keygen-test.c 2.12 KB
Newer Older
1 2 3 4 5
#include "testutils.h"

#include "knuth-lfib.h"

static void
6
progress(void *ctx UNUSED, int c)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
{
  fputc(c, stderr);
}

int
test_main(void)
{
  struct rsa_public_key pub;
  struct rsa_private_key key;
  
  struct knuth_lfib_ctx lfib;

  mpz_t expected;
  
  mpz_init(expected);
  
23 24
  rsa_private_key_init(&key);
  rsa_public_key_init(&pub);
25 26 27 28 29

  /* Generate a 1024 bit key with random e */
  knuth_lfib_init(&lfib, 13);

  if (!rsa_generate_keypair(&pub, &key,
30
			    &lfib, (nettle_random_func *) knuth_lfib_random,
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
			    NULL, verbose ? progress : NULL,
			    1024, 50))
    FAIL();

  test_rsa_key(&pub, &key);
  
  mpz_set_str(expected,
	      "34db1d465b94b12f" "bc1c024d2c6385ff" "a52a6aeb1754a58b"
	      "b9f0ace0186cfd45" "3963e33440b88696" "513b50956ff463c6"
	      "c369830dbe9f0605" "68c796cfe29ab35e" "722af1d3f5835610"
	      "4fb7bb44d6f319d9" "1a1fcc789ab79e82" "98bac0d68187f05a"
	      "1d0c1fcc324d1e0e" "69a4653de09c7c5b" "2278b3658b95b104"
	      "bafcfe2b5f9f88e3", 16);

  test_rsa_md5(&pub, &key, expected);

  /* Generate a 2000 bit key with fixed e */
  knuth_lfib_init(&lfib, 17);

  mpz_set_ui(pub.e, 17);
  if (!rsa_generate_keypair(&pub, &key,
52
			    &lfib, (nettle_random_func *) knuth_lfib_random,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
			    NULL, verbose ? progress : NULL,
			    2000, 0))
    FAIL();

  test_rsa_key(&pub, &key);

  mpz_set_str(expected,
	      "a311f80570e2c7b3" "d4888aae13c4c29b" "aa0139339809e581"
	      "98722ca37c4a95ea" "0c94aae3b712f2a1" "ea0784a7ddea5127"
	      "ca5dd218b96a9dac" "f607a90dfcb9458e" "cebdd0c25d84d0b9"
	      "3c74ac49d678f25a" "4a4493092c2e79d2" "d3b399ec589643a6"
	      "244e7b5c202fda3a" "9de1b5224d95ddb2" "a381b7cd061a85cb"
	      "dc65af62213da9d0" "636439ee50642694" "11137ef8c2e8887f"
	      "55e795e0b0ac6eb2" "b8576973a0d8ebda" "a8ee1e4ba23e4338"
	      "15121310036920d0" "906844742e6ed25f" "b2bb3fe9caadf7ae"
	      "fcd84d53730cf570" "1c8666c60238cfd4" "2fd1b6346a7d06fa"
	      "44df010cfc7a3bca" "8cadd0cd9b68fa8b" "41204cbf8fdb6775"
	      "f92107ef036bf139" "99bf", 16);

  test_rsa_sha1(&pub, &key, expected);
  
74 75
  rsa_private_key_clear(&key);
  rsa_public_key_clear(&pub);
76 77 78 79
  mpz_clear(expected);

  SUCCESS();
}