Integer type name change.
[silc.git] / lib / silccrypt / rsa.c
index 2efd68bee9f88c7a9f9b1ef834f488fbf09004af..b0b7bf50cde27c2fe68c108360dfa511df3e0503 100644 (file)
@@ -35,8 +35,9 @@
  *     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 
@@ -63,6 +64,7 @@
 */
 
 #include "silcincludes.h"
+#include "rsa_internal.h"
 #include "rsa.h"
 
 /*
@@ -73,8 +75,9 @@
 
 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");
 
@@ -82,16 +85,17 @@ SILC_PKCS_API_INIT(rsa)
   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 */
@@ -128,7 +132,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);
@@ -167,7 +171,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);
@@ -214,7 +218,13 @@ 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->e);
+    key->pub_set = FALSE;
+  }
 
   silc_mp_init(&key->e);
   silc_mp_init(&key->n);
@@ -240,6 +250,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;
 }
@@ -252,7 +263,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);
@@ -263,6 +285,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;
   }
 
@@ -273,6 +296,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;
   }
 
@@ -283,12 +307,15 @@ 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;
 }
@@ -452,7 +479,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;
@@ -460,8 +487,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);
@@ -475,16 +500,12 @@ 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
@@ -518,11 +539,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,