Fixed MPI implementation of silc_mp_sizeinbase to return size
[silc.git] / lib / silccrypt / rsa.c
index 66aea06a875c825b232e6f1af6c0bec6f99af2ba..202ccf3ffd16759079a849d6bc9e3803d3a3dfa8 100644 (file)
      The `tmplen' in encrypt, decrypt, sign and verify PKCS API functions
      is now calculated by (key->bits + 7) / 8.  It is the length of one block.
 
+   o Sat Mar 16 18:27:19 EET 2002  Pekka
+
+     Use the SilcRng sent as argument to SILC_PKCS_API_INIT in prime 
+     generation.
+
+   o Sat Sep 26 19:59:48 EEST 2002  Pekka
+
+     Fixed double free in public key setting.  Use a bit larger e as
+     starting point in key generation.
+
 */
 
 #include "silcincludes.h"
@@ -75,7 +85,7 @@
 
 SILC_PKCS_API_INIT(rsa)
 {
-  uint32 prime_bits = keylen / 2;
+  SilcUInt32 prime_bits = keylen / 2;
   SilcMPInt p, q;
   bool found = FALSE;
 
@@ -87,10 +97,10 @@ SILC_PKCS_API_INIT(rsa)
   /* Find p and q */
   while (!found) {
     printf("Finding p: ");
-    silc_math_gen_prime(&p, prime_bits, TRUE);
+    silc_math_gen_prime(&p, prime_bits, TRUE, rng);
     
     printf("\nFinding q: ");
-    silc_math_gen_prime(&q, prime_bits, TRUE);
+    silc_math_gen_prime(&q, prime_bits, TRUE, rng);
 
     if ((silc_mp_cmp(&p, &q)) == 0)
       printf("\nFound equal primes, not good, retrying...\n");
@@ -116,7 +126,7 @@ SILC_PKCS_API_INIT(rsa)
   silc_mp_uninit(&p);
   silc_mp_uninit(&q);
   
-  printf("\nKeys generated succesfully.\n");
+  printf("\nKeys generated successfully.\n");
 
   return TRUE;
 }
@@ -132,7 +142,7 @@ SILC_PKCS_API_GET_PUBLIC_KEY(rsa)
 {
   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);
@@ -171,7 +181,7 @@ SILC_PKCS_API_GET_PRIVATE_KEY(rsa)
 {
   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);
@@ -218,14 +228,20 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
 {
   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->n);
+    key->pub_set = FALSE;
+  }
 
   silc_mp_init(&key->e);
   silc_mp_init(&key->n);
 
   memcpy(tmp, key_data, 4);
   SILC_GET32_MSB(e_len, tmp);
-  if (e_len > key_len) {
+  if (!e_len || e_len > key_len) {
     silc_mp_uninit(&key->e);
     silc_mp_uninit(&key->n);
     return 0;
@@ -235,7 +251,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
   
   memcpy(tmp, key_data + 4 + e_len, 4);
   SILC_GET32_MSB(n_len, tmp);
-  if (e_len + n_len > key_len) {
+  if (!n_len || e_len + n_len > key_len) {
     silc_mp_uninit(&key->e);
     silc_mp_uninit(&key->n);
     return 0;
@@ -244,6 +260,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
   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;
 }
@@ -256,7 +273,18 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
 {
   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);
@@ -267,6 +295,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
   if (e_len > key_len) {
     silc_mp_uninit(&key->e);
     silc_mp_uninit(&key->n);
+    silc_mp_uninit(&key->d);
     return FALSE;
   }
 
@@ -277,6 +306,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
   if (e_len + n_len > key_len) {
     silc_mp_uninit(&key->e);
     silc_mp_uninit(&key->n);
+    silc_mp_uninit(&key->d);
     return FALSE;
   }
 
@@ -287,14 +317,17 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
   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;
+  return key->bits;
 }
 
 SILC_PKCS_API_CONTEXT_LEN(rsa)
@@ -456,7 +489,7 @@ SILC_PKCS_API_VERIFY(rsa)
    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;
@@ -464,8 +497,6 @@ void rsa_generate_keys(RsaKey *key, uint32 bits,
   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);
@@ -479,22 +510,18 @@ void rsa_generate_keys(RsaKey *key, uint32 bits,
   /* 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
      for all keys. Also, to make encryption faster we use small 
      number. */
-  silc_mp_set_ui(&key->e, 127);
+  silc_mp_set_ui(&key->e, 65533);
  retry_e:
   /* See if e is relatively prime to phi. gcd == greates common divisor,
      if gcd equals 1 they are relatively prime. */
@@ -504,7 +531,7 @@ void rsa_generate_keys(RsaKey *key, uint32 bits,
     goto retry_e;
   }
   
-  /* Find d, the private exponent. */
+  /* Find d, the private exponent, e ^ -1 mod lcm(phi). */
   silc_mp_gcd(&div, &pm1, &qm1);
   silc_mp_div(&lcm, &phi, &div);
   silc_mp_modinv(&key->d, &key->e, &lcm);
@@ -522,11 +549,12 @@ void rsa_generate_keys(RsaKey *key, uint32 bits,
 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,