rsa-encrypt-test.c 1.53 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 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
#include "knuth-lfib.h"

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

  /* FIXME: How is this spelled? */
  const uint8_t *msg = "Squemish ossifrage";
  unsigned msg_length;

  uint8_t *decrypted;
  unsigned decrypted_length;
  
  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);
  msg_length = strlen(msg);

  if (verbose)
    fprintf(stderr, "msg: `%s', length = %d\n", msg, msg_length);
  
  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 44
    {
      /* In which GMP version was gmp_fprintf introduced? */
      fprintf(stderr, "encrypted: ");
      mpz_out_str(stderr, 10, gibberish);
    }
Niels Möller's avatar
Niels Möller committed
45
  
46
  decrypted = xalloc(msg_length + 1);
Niels Möller's avatar
Niels Möller committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  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));

  decrypted_length = key.size;
  ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
  ASSERT(decrypted_length == msg_length);
  ASSERT(MEMEQ(msg_length, msg, decrypted));
  
  rsa_private_key_clear(&key);
  rsa_public_key_clear(&pub);
  mpz_clear(gibberish);
64
  free(decrypted);
Niels Möller's avatar
Niels Möller committed
65 66 67
  SUCCESS();
}