* Decryption:
* m = c ^ d mod n
*
- * This code is based on SSH's (Secure Shell), PGP's (Pretty Good Privacy)
- * and RSAREF Toolkit's RSA source codes. They all were a big help for me.
+ * The SSH's (Secure Shell), PGP's (Pretty Good Privacy) and RSAREF
+ * Toolkit were used as reference when coding this implementation. They
+ * all were a big help for me.
*
* I also suggest reading Bruce Schneier's; Applied Cryptography, Second
* Edition, John Wiley & Sons, Inc. 1996. This book deals about RSA and
*/
#include "silcincludes.h"
+#include "rsa_internal.h"
#include "rsa.h"
/*
SILC_PKCS_API_INIT(rsa)
{
- uint32 prime_bits = keylen / 2;
+ SilcUInt32 prime_bits = keylen / 2;
SilcMPInt p, q;
+ bool found = FALSE;
printf("Generating RSA Public and Private keys, might take a while...\n");
silc_mp_init(&q);
/* Find p and q */
- retry_primes:
- printf("Finding p: ");
- silc_math_gen_prime(&p, prime_bits, TRUE);
-
- printf("\nFinding q: ");
- silc_math_gen_prime(&q, prime_bits, TRUE);
-
- if ((silc_mp_cmp(&p, &q)) == 0) {
- printf("\nFound equal primes, not good, retrying...\n");
- goto retry_primes;
+ while (!found) {
+ printf("Finding p: ");
+ silc_math_gen_prime(&p, prime_bits, TRUE);
+
+ printf("\nFinding q: ");
+ silc_math_gen_prime(&q, prime_bits, TRUE);
+
+ if ((silc_mp_cmp(&p, &q)) == 0)
+ printf("\nFound equal primes, not good, retrying...\n");
+ else
+ found = TRUE;
}
/* If p is smaller than q, switch them */
{
RsaKey *key = (RsaKey *)context;
unsigned char *e, *n, *ret;
- uint32 e_len, n_len;
+ SilcUInt32 e_len, n_len;
unsigned char tmp[4];
e = silc_mp_mp2bin(&key->e, 0, &e_len);
{
RsaKey *key = (RsaKey *)context;
unsigned char *e, *n, *d, *ret;
- uint32 e_len, n_len, d_len;
+ SilcUInt32 e_len, n_len, d_len;
unsigned char tmp[4];
e = silc_mp_mp2bin(&key->e, 0, &e_len);
{
RsaKey *key = (RsaKey *)context;
unsigned char tmp[4];
- uint32 e_len, n_len;
+ SilcUInt32 e_len, n_len;
+
+ if (key->pub_set) {
+ silc_mp_uninit(&key->e);
+ silc_mp_uninit(&key->e);
+ key->pub_set = FALSE;
+ }
silc_mp_init(&key->e);
silc_mp_init(&key->n);
silc_mp_bin2mp(key_data + 4 + e_len + 4, n_len, &key->n);
key->bits = n_len * 8;
+ key->pub_set = TRUE;
return key->bits;
}
{
RsaKey *key = (RsaKey *)context;
unsigned char tmp[4];
- uint32 e_len, n_len, d_len;
+ SilcUInt32 e_len, n_len, d_len;
+
+ if (key->prv_set) {
+ silc_mp_uninit(&key->d);
+ key->prv_set = FALSE;
+ }
+
+ if (key->pub_set) {
+ silc_mp_uninit(&key->e);
+ silc_mp_uninit(&key->n);
+ key->pub_set = FALSE;
+ }
silc_mp_init(&key->e);
silc_mp_init(&key->n);
if (e_len > key_len) {
silc_mp_uninit(&key->e);
silc_mp_uninit(&key->n);
+ silc_mp_uninit(&key->d);
return FALSE;
}
if (e_len + n_len > key_len) {
silc_mp_uninit(&key->e);
silc_mp_uninit(&key->n);
+ silc_mp_uninit(&key->d);
return FALSE;
}
if (e_len + n_len + d_len > key_len) {
silc_mp_uninit(&key->e);
silc_mp_uninit(&key->n);
+ silc_mp_uninit(&key->d);
return FALSE;
}
silc_mp_bin2mp(key_data + 4 + e_len + 4 + n_len + 4, d_len, &key->d);
key->bits = n_len * 8;
+ key->prv_set = TRUE;
+ key->pub_set = TRUE;
return TRUE;
}
to compute the modulus n has to be generated before calling this. They
are then sent as argument for the function. */
-void rsa_generate_keys(RsaKey *key, uint32 bits,
+void rsa_generate_keys(RsaKey *key, SilcUInt32 bits,
SilcMPInt *p, SilcMPInt *q)
{
SilcMPInt phi, hlp;
SilcMPInt pm1, qm1;
/* Initialize variables */
- silc_mp_init(&key->p);
- silc_mp_init(&key->q);
silc_mp_init(&key->n);
silc_mp_init(&key->e);
silc_mp_init(&key->d);
/* Set modulus length */
key->bits = bits;
- /* Set the primes */
- silc_mp_set(&key->p, p);
- silc_mp_set(&key->q, q);
-
/* Compute modulus, n = p * q */
- silc_mp_mul(&key->n, &key->p, &key->q);
+ silc_mp_mul(&key->n, p, q);
/* phi = (p - 1) * (q - 1) */
- silc_mp_sub_ui(&pm1, &key->p, 1);
- silc_mp_sub_ui(&qm1, &key->q, 1);
+ silc_mp_sub_ui(&pm1, p, 1);
+ silc_mp_sub_ui(&qm1, q, 1);
silc_mp_mul(&phi, &pm1, &qm1);
/* Set e, the public exponent. We try to use same public exponent
void rsa_clear_keys(RsaKey *key)
{
key->bits = 0;
- silc_mp_uninit(&key->p);
- silc_mp_uninit(&key->q);
- silc_mp_uninit(&key->n);
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->d);
+ if (key->pub_set) {
+ silc_mp_uninit(&key->n);
+ silc_mp_uninit(&key->e);
+ }
+ if (key->prv_set)
+ silc_mp_uninit(&key->d);
}
/* RSA encrypt/decrypt function. cm = ciphertext or plaintext,