Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / testsuite / rsa-encrypt-test.c
blobc7b616c92f2e15b86c08e3c0e8ecef5ce7c0f010
1 #include "testutils.h"
3 #include "rsa.h"
4 #include "knuth-lfib.h"
6 void
7 test_main(void)
9 struct rsa_public_key pub;
10 struct rsa_private_key key;
11 struct knuth_lfib_ctx lfib;
13 /* FIXME: How is this spelled? */
14 const uint8_t *msg = "Squemish ossifrage";
15 unsigned msg_length;
17 uint8_t *decrypted;
18 unsigned decrypted_length;
19 uint8_t after;
21 mpz_t gibberish;
23 rsa_private_key_init(&key);
24 rsa_public_key_init(&pub);
25 mpz_init(gibberish);
27 knuth_lfib_init(&lfib, 17);
29 test_rsa_set_key_1(&pub, &key);
30 msg_length = strlen(msg);
32 if (verbose)
33 fprintf(stderr, "msg: `%s', length = %d\n", msg, msg_length);
35 ASSERT(rsa_encrypt(&pub,
36 &lfib, (nettle_random_func *) knuth_lfib_random,
37 msg_length, msg,
38 gibberish));
40 if (verbose)
42 /* In which GMP version was gmp_fprintf introduced? */
43 fprintf(stderr, "encrypted: ");
44 mpz_out_str(stderr, 10, gibberish);
47 decrypted = xalloc(msg_length + 1);
49 knuth_lfib_random (&lfib, msg_length + 1, decrypted);
50 after = decrypted[msg_length];
52 decrypted_length = msg_length - 1;
53 ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
55 decrypted_length = msg_length;
56 ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
57 ASSERT(decrypted_length == msg_length);
58 ASSERT(MEMEQ(msg_length, msg, decrypted));
59 ASSERT(decrypted[msg_length] == after);
61 knuth_lfib_random (&lfib, msg_length + 1, decrypted);
62 after = decrypted[msg_length];
64 decrypted_length = key.size;
65 ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
66 ASSERT(decrypted_length == msg_length);
67 ASSERT(MEMEQ(msg_length, msg, decrypted));
68 ASSERT(decrypted[msg_length] == after);
70 knuth_lfib_random (&lfib, msg_length + 1, decrypted);
71 after = decrypted[msg_length];
73 decrypted_length = msg_length;
74 ASSERT(rsa_decrypt_tr(&pub, &key,
75 &lfib, (nettle_random_func *) knuth_lfib_random,
76 &decrypted_length, decrypted, gibberish));
77 ASSERT(decrypted_length == msg_length);
78 ASSERT(MEMEQ(msg_length, msg, decrypted));
79 ASSERT(decrypted[msg_length] == after);
81 rsa_private_key_clear(&key);
82 rsa_public_key_clear(&pub);
83 mpz_clear(gibberish);
84 free(decrypted);