Added SILC Server library.
[silc.git] / lib / silccrypt / rsa.c
index 7f9beb17d727d02cd835bbfeb7e90474b75c908b..18399a5a393a0c49707d0803dad3eb402b27dab8 100644 (file)
@@ -1,51 +1,51 @@
 /*
- * rsa.c       RSA Public and Private key generation functions,
- *             RSA encrypt and decrypt functions.
- *
- * Author: Pekka Riikonen <priikone@silcnet.org>
- *
- * Copyright (C) 1997 - 2003 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
- * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
- *
* Created: Sat Mar  1 13:26:45 1997 pekka
- *
* RSA public key cryptographic algorithm used in this distribution is:
- *
*     Key generation:
*     p, q            primes
*     p != q
*     n = p * q       modulus
- *
*     Public key exponent:
*     e   relatively prime to (p-1) * (q-1)
*     Private key exponent:
*     d = e ^ -1 mod lcm(((p-1) * (q-1)))
- *
*     Encryption:
*     c = m ^ e mod n
*     Decryption:
*     m = c ^ d mod n
- *
* Supports CRT (Chinese Remainder Theorem) for private key operations.
- *
* 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
* everything else too about cryptography.
- *
- */
+
+  rsa.c        RSA Public and Private key generation functions,
+               RSA encrypt and decrypt functions.
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 1997 - 2005 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
 the Free Software Foundation; version 2 of the License.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  Created: Sat Mar  1 13:26:45 1997 pekka
+
+  RSA public key cryptographic algorithm used in this distribution is:
+
      Key generation:
      p, q            primes
      p != q
      n = p * q       modulus
+
      Public key exponent:
      e   relatively prime to (p-1) * (q-1)
      Private key exponent:
      d = e ^ -1 mod lcm(((p-1) * (q-1)))
+
      Encryption:
      c = m ^ e mod n
      Decryption:
      m = c ^ d mod n
+
+  Supports CRT (Chinese Remainder Theorem) for private key operations.
+
+  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
+  everything else too about cryptography.
+
+*/
 /* $Id$ */
 
 /*
@@ -75,7 +75,7 @@
 
 */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "rsa_internal.h"
 #include "rsa.h"
 
@@ -89,7 +89,7 @@ SILC_PKCS_API_INIT(rsa)
 {
   SilcUInt32 prime_bits = keylen / 2;
   SilcMPInt p, q;
-  bool found = FALSE;
+  SilcBool found = FALSE;
 
   if (keylen < 768 || keylen > 16384)
     return FALSE;
@@ -382,7 +382,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
   silc_buffer_pull(&k, len);
 
   /* Get optimized d for CRT, if present. */
-  if (k.len > 4) {
+  if (silc_buffer_len(&k) > 4) {
     key->crt = TRUE;
     silc_mp_init(&key->dP);
     silc_mp_init(&key->dQ);
@@ -767,7 +767,7 @@ SILC_PKCS_API_VERIFY(pkcs1)
    to compute the modulus n has to be generated before calling this. They
    are then sent as argument for the function. */
 
-bool rsa_generate_keys(RsaKey *key, SilcUInt32 bits,
+SilcBool rsa_generate_keys(RsaKey *key, SilcUInt32 bits,
                       SilcMPInt *p, SilcMPInt *q)
 {
   SilcMPInt phi, hlp;
@@ -843,7 +843,7 @@ bool rsa_generate_keys(RsaKey *key, SilcUInt32 bits,
 
 /* Clears whole key structure. */
 
-bool rsa_clear_keys(RsaKey *key)
+SilcBool rsa_clear_keys(RsaKey *key)
 {
   key->bits = 0;
   if (key->pub_set) {
@@ -865,7 +865,7 @@ bool rsa_clear_keys(RsaKey *key)
 
 /* RSA public key operation */
 
-bool rsa_public_operation(RsaKey *key, SilcMPInt *src, SilcMPInt *dst)
+SilcBool rsa_public_operation(RsaKey *key, SilcMPInt *src, SilcMPInt *dst)
 {
   /* dst = src ^ e mod n */
   silc_mp_pow_mod(dst, src, &key->e, &key->n);
@@ -874,7 +874,7 @@ bool rsa_public_operation(RsaKey *key, SilcMPInt *src, SilcMPInt *dst)
 
 /* RSA private key operation */
 
-bool rsa_private_operation(RsaKey *key, SilcMPInt *src, SilcMPInt *dst)
+SilcBool rsa_private_operation(RsaKey *key, SilcMPInt *src, SilcMPInt *dst)
 {
   if (!key->crt) {
     /* dst = src ^ d mod n */