4 #include "silcincludes.h"
6 #include "rsa_internal.h"
8 void testi(SilcRng rng, void *context)
13 MP_INT tnum; /* number we'll encrypt */
14 MP_INT test; /* en/decrypted result of tnum */
15 RsaKey *key = (RsaKey *)context;
18 numbuf = (char *)malloc((bits / 3) + 1);
24 fprintf(stderr, "\nTesting encryption and decryption ... ");
26 for(i = 0; i < bytes; i++)
27 sprintf(numbuf + 2 * i, "%02x", silc_rng_get_byte(rng));
29 mpz_set_str(&tnum, numbuf, 16);
32 memset(numbuf, 0, bits / 3);
35 /* make tnum smaller than n */
36 mpz_div_ui(&tnum, &tnum, 10);
38 rsa_en_de_crypt(&test, &tnum, &key->e, &key->n);
40 rsa_en_de_crypt(&test, &test, &key->d, &key->n);
41 /* see if decrypted result is same than the original one is */
42 if (mpz_cmp(&test, &tnum) != 0) {
43 fprintf(stderr, "Error in encryption and decryption!\n");
50 fprintf(stderr, "Keys are Ok.\n");
57 unsigned char *pk, *prv;
58 unsigned int pk_len, prv_len;
59 unsigned char *src, *dst, *new;
60 unsigned int src_len, dst_len, new_len;
63 silc_pkcs_alloc("rsa", &pkcs);
65 rng = silc_rng_alloc();
67 silc_math_primegen_init();
69 pkcs->pkcs->init(pkcs->context, 1024, rng);
71 pk = silc_pkcs_get_public_key(pkcs, &pk_len);
72 prv = silc_pkcs_get_public_key(pkcs, &prv_len);
74 src = "PEKKA RIIKONEN";
76 dst = silc_calloc(200, sizeof(unsigned char));
77 pkcs->pkcs->encrypt(pkcs->context, src, src_len, dst, &dst_len);
79 SILC_LOG_HEXDUMP(("src"), src, src_len);
80 SILC_LOG_HEXDUMP(("dst"), dst, dst_len);
82 new = silc_calloc(200, sizeof(unsigned char));
83 pkcs->pkcs->decrypt(pkcs->context, dst, dst_len, new, &new_len);
85 SILC_LOG_HEXDUMP(("new"), new, new_len);
87 testi(rng, pkcs->context);