rsa-encrypt-test.c 3.21 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
#include "testutils.h"

3
#include "rsa.h"
Niels Möller's avatar
Niels Möller committed
4 5
#include "knuth-lfib.h"

6
void
Niels Möller's avatar
Niels Möller committed
7 8 9 10 11 12 13
test_main(void)
{
  struct rsa_public_key pub;
  struct rsa_private_key key;
  struct knuth_lfib_ctx lfib;

  /* FIXME: How is this spelled? */
14 15
  const unsigned char msg[] = "Squemish ossifrage";
  size_t msg_length = LLENGTH(msg);
Niels Möller's avatar
Niels Möller committed
16 17

  uint8_t *decrypted;
18
  size_t decrypted_length;
19 20
  uint8_t after;

Niels Möller's avatar
Niels Möller committed
21 22 23 24 25 26 27 28 29 30 31
  mpz_t gibberish;

  rsa_private_key_init(&key);
  rsa_public_key_init(&pub);
  mpz_init(gibberish);

  knuth_lfib_init(&lfib, 17);
  
  test_rsa_set_key_1(&pub, &key);

  if (verbose)
32
    fprintf(stderr, "msg: `%s', length = %d\n", msg, (int) msg_length);
Niels Möller's avatar
Niels Möller committed
33 34
  
  ASSERT(rsa_encrypt(&pub,
35
		     &lfib, (nettle_random_func *) knuth_lfib_random,
Niels Möller's avatar
Niels Möller committed
36 37 38 39
		     msg_length, msg,
		     gibberish));

  if (verbose)
40 41 42 43
    {
      fprintf(stderr, "encrypted: ");
      mpz_out_str(stderr, 10, gibberish);
    }
Niels Möller's avatar
Niels Möller committed
44
  
45
  decrypted = xalloc(msg_length + 1);
Niels Möller's avatar
Niels Möller committed
46

47 48 49
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
  after = decrypted[msg_length];
  
Niels Möller's avatar
Niels Möller committed
50 51 52 53 54 55 56
  decrypted_length = msg_length - 1;
  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));

  decrypted_length = msg_length;
  ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
  ASSERT(decrypted_length == msg_length);
  ASSERT(MEMEQ(msg_length, msg, decrypted));
57 58 59 60
  ASSERT(decrypted[msg_length] == after);

  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
  after = decrypted[msg_length];
Niels Möller's avatar
Niels Möller committed
61 62 63 64 65

  decrypted_length = key.size;
  ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
  ASSERT(decrypted_length == msg_length);
  ASSERT(MEMEQ(msg_length, msg, decrypted));
66
  ASSERT(decrypted[msg_length] == after);
Niels Möller's avatar
Niels Möller committed
67
  
68 69 70 71 72 73 74 75 76 77 78
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
  after = decrypted[msg_length];

  decrypted_length = msg_length;
  ASSERT(rsa_decrypt_tr(&pub, &key,
			&lfib, (nettle_random_func *) knuth_lfib_random,
			&decrypted_length, decrypted, gibberish));
  ASSERT(decrypted_length == msg_length);
  ASSERT(MEMEQ(msg_length, msg, decrypted));
  ASSERT(decrypted[msg_length] == after);

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  /* test side channel resistant variant */
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
  after = decrypted[msg_length];
  decrypted_length = msg_length;

  ASSERT(rsa_sec_decrypt(&pub, &key,
                         &lfib, (nettle_random_func *) knuth_lfib_random,
                         decrypted_length, decrypted, gibberish));
  ASSERT(MEMEQ(msg_length, msg, decrypted));
  ASSERT(decrypted[msg_length] == after);

  /* test invalid length to rsa_sec_decrypt */
  knuth_lfib_random (&lfib, msg_length + 1, decrypted);
  decrypted_length = msg_length - 1;
  after = decrypted[decrypted_length] = 'X';
  decrypted[0] = 'A';

  ASSERT(!rsa_sec_decrypt(&pub, &key,
                          &lfib, (nettle_random_func *) knuth_lfib_random,
                          decrypted_length, decrypted, gibberish));
  ASSERT(decrypted[decrypted_length] == after);
  ASSERT(decrypted[0] == 'A');


103 104 105 106 107 108 109
  /* Test invalid key. */
  mpz_add_ui (key.q, key.q, 2);
  decrypted_length = key.size;
  ASSERT(!rsa_decrypt_tr(&pub, &key,
			 &lfib, (nettle_random_func *) knuth_lfib_random,
			 &decrypted_length, decrypted, gibberish));

Niels Möller's avatar
Niels Möller committed
110 111 112
  rsa_private_key_clear(&key);
  rsa_public_key_clear(&pub);
  mpz_clear(gibberish);
113
  free(decrypted);
Niels Möller's avatar
Niels Möller committed
114 115
}