Fixed MPI implementation of silc_mp_sizeinbase to return size
[silc.git] / lib / silccrypt / rsa.c
index 47158aab0a4700fc79a31101a154bc5391229b2b..202ccf3ffd16759079a849d6bc9e3803d3a3dfa8 100644 (file)
      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"
@@ -121,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;
 }
@@ -227,7 +232,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
 
   if (key->pub_set) {
     silc_mp_uninit(&key->e);
-    silc_mp_uninit(&key->e);
+    silc_mp_uninit(&key->n);
     key->pub_set = FALSE;
   }
 
@@ -236,7 +241,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
 
   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;
@@ -246,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;
@@ -322,7 +327,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
   key->prv_set = TRUE;
   key->pub_set = TRUE;
 
-  return TRUE;
+  return key->bits;
 }
 
 SILC_PKCS_API_CONTEXT_LEN(rsa)
@@ -516,7 +521,7 @@ void rsa_generate_keys(RsaKey *key, SilcUInt32 bits,
   /* 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. */
@@ -526,7 +531,7 @@ void rsa_generate_keys(RsaKey *key, SilcUInt32 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);