updates.
[silc.git] / lib / silccrypt / rsa.c
index df7932985705698b18c9720af40ddfbca56f9c3e..a4479334ddf35701a36af6a51fe8a5302bdeaae4 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
  *
- * Copyright (C) 1997 - 2000 Pekka Riikonen
+ * Copyright (C) 1997 - 2001 Pekka Riikonen
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@
  *     Public key exponent:
  *     e   relatively prime to (p-1) * (q-1)
  *     Private key exponent:
- *     d = e ^ -1 mod ((p-1) * (q-1))
+ *     d = e ^ -1 mod lcm(((p-1) * (q-1)))
  *
  *     Encryption:
  *     c = m ^ e mod n
  */
 /* $Id$ */
 
+/*
+   ChangeLog
+
+   o Mon Feb 12 11:20:32 EET 2001  Pekka
+
+     Changed RSA private exponent generation to what PKCS #1 suggests.  We
+     try to find the smallest possible d by doing modinv(e, lcm(phi)) instead
+     of modinv(e, phi).  Note: this is not security fix but optimization.
+
+   o Tue Feb 20 13:58:58 EET 2001  Pekka
+
+     Set key->bits in rsa_generate_key.  It is the modulus length in bits.
+     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.
+
+*/
+
 #include "silcincludes.h"
 #include "rsa.h"
 
@@ -56,7 +73,7 @@
 
 SILC_PKCS_API_INIT(rsa)
 {
-  unsigned int prime_bits = keylen / 2;
+  uint32 prime_bits = keylen / 2;
   SilcInt p, q;
 
   printf("Generating RSA Public and Private keys, might take a while...\n");
@@ -111,11 +128,11 @@ SILC_PKCS_API_GET_PUBLIC_KEY(rsa)
 {
   RsaKey *key = (RsaKey *)context;
   unsigned char *e, *n, *ret;
-  unsigned int e_len, n_len;
+  uint32 e_len, n_len;
   unsigned char tmp[4];
 
-  e = silc_mp_mp2bin(&key->e, &e_len);
-  n = silc_mp_mp2bin(&key->n, &n_len);
+  e = silc_mp_mp2bin(&key->e, 0, &e_len);
+  n = silc_mp_mp2bin(&key->n, key->bits / 8, &n_len);
 
   *ret_len = e_len + 4 + n_len + 4;
   ret = silc_calloc(*ret_len, sizeof(unsigned char));
@@ -150,12 +167,12 @@ SILC_PKCS_API_GET_PRIVATE_KEY(rsa)
 {
   RsaKey *key = (RsaKey *)context;
   unsigned char *e, *n, *d, *ret;
-  unsigned int e_len, n_len, d_len;
+  uint32 e_len, n_len, d_len;
   unsigned char tmp[4];
 
-  e = silc_mp_mp2bin(&key->e, &e_len);
-  n = silc_mp_mp2bin(&key->n, &n_len);
-  d = silc_mp_mp2bin(&key->d, &d_len);
+  e = silc_mp_mp2bin(&key->e, 0, &e_len);
+  n = silc_mp_mp2bin(&key->n, key->bits / 8, &n_len);
+  d = silc_mp_mp2bin(&key->d, 0, &d_len);
 
   *ret_len = e_len + 4 + n_len + 4 + d_len + 4;
   ret = silc_calloc(*ret_len, sizeof(unsigned char));
@@ -197,7 +214,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
 {
   RsaKey *key = (RsaKey *)context;
   unsigned char tmp[4];
-  unsigned int e_len, n_len;
+  uint32 e_len, n_len;
 
   silc_mp_init(&key->e);
   silc_mp_init(&key->n);
@@ -207,7 +224,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
   if (e_len > key_len) {
     silc_mp_clear(&key->e);
     silc_mp_clear(&key->n);
-    return FALSE;
+    return 0;
   }
 
   silc_mp_bin2mp(key_data + 4, e_len, &key->e);
@@ -217,12 +234,14 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
   if (e_len + n_len > key_len) {
     silc_mp_clear(&key->e);
     silc_mp_clear(&key->n);
-    return FALSE;
+    return 0;
   }
 
   silc_mp_bin2mp(key_data + 4 + e_len + 4, n_len, &key->n);
 
-  return TRUE;
+  key->bits = n_len * 8;
+
+  return key->bits;
 }
 
 /* Set private key. This derives the public key from the private
@@ -233,7 +252,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
 {
   RsaKey *key = (RsaKey *)context;
   unsigned char tmp[4];
-  unsigned int e_len, n_len, d_len;
+  uint32 e_len, n_len, d_len;
 
   silc_mp_init(&key->e);
   silc_mp_init(&key->n);
@@ -269,6 +288,8 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
 
   silc_mp_bin2mp(key_data + 4 + e_len + 4 + n_len + 4, d_len, &key->d);
 
+  key->bits = n_len * 8;
+
   return TRUE;
 }
 
@@ -277,40 +298,6 @@ SILC_PKCS_API_CONTEXT_LEN(rsa)
   return sizeof(RsaKey);
 }
 
-SILC_PKCS_API_DATA_CONTEXT_LEN(rsa)
-{
-  return sizeof(RsaDataContext);
-}
-
-SILC_PKCS_API_SET_ARG(rsa)
-{
-  RsaDataContext *data_ctx = (RsaDataContext *)data_context;
-
-  switch(argnum) {
-  case 1:
-    data_ctx->src = val;
-    return TRUE;
-    break;
-  case 2:
-    data_ctx->dst = val;
-    return TRUE;
-    break;
-  case 3:
-    data_ctx->exp = val;
-    return TRUE;
-    break;
-  case 4:
-    data_ctx->mod = val;
-    return TRUE;
-    break;
-  default:
-    return FALSE;
-    break;
-  }
-
-  return FALSE;
-}
-
 SILC_PKCS_API_ENCRYPT(rsa)
 {
   RsaKey *key = (RsaKey *)context;
@@ -330,7 +317,7 @@ SILC_PKCS_API_ENCRYPT(rsa)
   /* Encrypt */
   rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->e, &key->n);
   
-  tmplen = (1024 + 7) / 8;
+  tmplen = (key->bits + 7) / 8;
 
   /* Format the MP int back into data */
   for (i = tmplen; i > 0; i--) {
@@ -364,7 +351,7 @@ SILC_PKCS_API_DECRYPT(rsa)
   /* Decrypt */
   rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->d, &key->n);
 
-  tmplen = (1024 + 7) / 8;
+  tmplen = (key->bits + 7) / 8;
 
   /* Format the MP int back into data */
   for (i = tmplen; i > 0; i--) {
@@ -398,7 +385,7 @@ SILC_PKCS_API_SIGN(rsa)
   /* Sign */
   rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->d, &key->n);
 
-  tmplen = (1024 + 7) / 8;
+  tmplen = (key->bits + 7) / 8;
 
   /* Format the MP int back into data */
   for (i = tmplen; i > 0; i--) {
@@ -456,11 +443,11 @@ 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, unsigned int bits, 
+void rsa_generate_keys(RsaKey *key, uint32 bits, 
                       SilcInt *p, SilcInt *q)
 {
   SilcInt phi, hlp;
-  SilcInt dq;
+  SilcInt div, lcm;
   SilcInt pm1, qm1;
   
   /* Initialize variables */
@@ -471,10 +458,14 @@ void rsa_generate_keys(RsaKey *key, unsigned int bits,
   silc_mp_init(&key->d);
   silc_mp_init(&phi);
   silc_mp_init(&hlp);
-  silc_mp_init(&dq);
+  silc_mp_init(&div);
+  silc_mp_init(&lcm);
   silc_mp_init(&pm1);
   silc_mp_init(&qm1);
 
+  /* Set modulus length */
+  key->bits = bits;
+
   /* Set the primes */
   silc_mp_set(&key->p, p);
   silc_mp_set(&key->q, q);
@@ -500,14 +491,15 @@ void rsa_generate_keys(RsaKey *key, unsigned int bits,
     goto retry_e;
   }
   
-  /* Find d, the private exponent. First we do phi / 2, to get it a 
-     bit smaller */
-  silc_mp_div_ui(&dq, &phi, 2);
-  silc_mp_modinv(&key->d, &key->e, &dq);
+  /* Find d, the private exponent. */
+  silc_mp_gcd(&div, &pm1, &qm1);
+  silc_mp_fdiv_q(&lcm, &phi, &div);
+  silc_mp_modinv(&key->d, &key->e, &lcm);
   
   silc_mp_clear(&phi);
   silc_mp_clear(&hlp);
-  silc_mp_clear(&dq);
+  silc_mp_clear(&div);
+  silc_mp_clear(&lcm);
   silc_mp_clear(&pm1);
   silc_mp_clear(&qm1);
 }