Integer type name change.
[silc.git] / lib / silccrypt / pkcs1.c
index 2ba6f3efbaf5a62ca753f28a4dc7a661a45d213b..6e5a6a4a560b181774b0ab82364fb5e5503458dd 100644 (file)
    Hence, the encoding is always in PKCS #1 version 1.5 format.
 
    Any questions and comments regarding this modified version should be
-   sent to priikone@poseidon.pspt.fi.
+   sent to priikone@silcnet.org.
 
    References: ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc,
                ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1.asc,
               and RFC 2437.
 
-   Copyright notice: All code, including the SILC PKCS API code that is
-   not part of the Mozilla code, falls under the same license (MPL or GPL)
-   found attached to this file, below.
+   Copyright notice: All code in this file, including the SILC PKCS API
+   code that is not part of the Mozilla code, falls under the same license
+   (MPL or GPL) found attached to this file, below.
 */
 
 /*
@@ -77,6 +77,7 @@
  */
 
 #include "silcincludes.h"
+#include "rsa_internal.h"
 #include "rsa.h"
 
 #define RSA_BLOCK_MIN_PAD_LEN          8
@@ -104,8 +105,8 @@ typedef enum {
  * the rules defined in PKCS #1.
  */
 static unsigned char *
-RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType,
-                  unsigned char *data, unsigned int data_len)
+RSA_FormatOneBlock(SilcUInt32 modulusLen, RSA_BlockType blockType,
+                  unsigned char *data, SilcUInt32 data_len)
 {
     unsigned char *block;
     unsigned char *bp;
@@ -163,7 +164,7 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType,
        for (i = 0; i < padLen; i++) {
            /* Pad with non-zero random data. */
            do {
-               silc_rng_global_get_byte(bp + i);
+             bp[i] = silc_rng_global_get_byte();
            } while (bp[i] == RSA_BLOCK_AFTER_PAD_OCTET);
        }
        bp += padLen;
@@ -180,10 +181,10 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType,
 }
 
 static int
-RSA_FormatBlock(unsigned char **result, unsigned int *result_len,
-               unsigned int modulusLen,
+RSA_FormatBlock(unsigned char **result, SilcUInt32 *result_len,
+               SilcUInt32 modulusLen,
                RSA_BlockType blockType, unsigned char *data,
-               unsigned int data_len)
+               SilcUInt32 data_len)
 {
     /*
      * XXX For now assume that the data length fits in a single
@@ -237,14 +238,14 @@ RSA_FormatBlock(unsigned char **result, unsigned int *result_len,
  */
 unsigned char *
 RSA_DecodeOneBlock(unsigned char *data,
-                  unsigned int modulusLen,
-                  unsigned int expectedLen,
+                  SilcUInt32 modulusLen,
+                  SilcUInt32 expectedLen,
                   RSA_BlockType bt,
-                  unsigned int *pResultLen)
+                  SilcUInt32 *pResultLen)
 {
     RSA_BlockType blockType;
     unsigned char *dp, *res;
-    unsigned int i, len = 0;
+    SilcUInt32 i, len = 0;
 
     dp = data;
     if (dp[0] != RSA_BLOCK_FIRST_OCTET) {
@@ -313,18 +314,20 @@ RSA_DecodeOneBlock(unsigned char *data,
 SILC_PKCS_API_ENCRYPT(pkcs1)
 {
   RsaKey *key = (RsaKey *)context;
-  SilcInt mp_tmp;
-  SilcInt mp_dst;
+  SilcMPInt mp_tmp;
+  SilcMPInt mp_dst;
   unsigned char *padded;
-  unsigned int padded_len, len = key->bits / 8;
+  SilcUInt32 padded_len, len = key->bits / 8;
 
   /* Pad data */
   if (!RSA_FormatBlock(&padded, &padded_len, len,
                       RSA_BlockPublic, src, src_len))
     return FALSE;
 
-  silc_mp_init_set_ui(&mp_tmp, 0);
-  silc_mp_init_set_ui(&mp_dst, 0);
+  silc_mp_init(&mp_tmp);
+  silc_mp_init(&mp_dst);
+  silc_mp_set_ui(&mp_tmp, 0);
+  silc_mp_set_ui(&mp_dst, 0);
 
   /* Data to MP */
   silc_mp_bin2mp(padded, padded_len, &mp_tmp);
@@ -338,8 +341,8 @@ SILC_PKCS_API_ENCRYPT(pkcs1)
 
   memset(padded, 0, padded_len);
   silc_free(padded);
-  silc_mp_clear(&mp_tmp);
-  silc_mp_clear(&mp_dst);
+  silc_mp_uninit(&mp_tmp);
+  silc_mp_uninit(&mp_dst);
 
   return TRUE;
 }
@@ -347,13 +350,15 @@ SILC_PKCS_API_ENCRYPT(pkcs1)
 SILC_PKCS_API_DECRYPT(pkcs1)
 {
   RsaKey *key = (RsaKey *)context;
-  SilcInt mp_tmp;
-  SilcInt mp_dst;
+  SilcMPInt mp_tmp;
+  SilcMPInt mp_dst;
   unsigned char *padded, *unpadded;
-  unsigned int padded_len;
+  SilcUInt32 padded_len;
 
-  silc_mp_init_set_ui(&mp_tmp, 0);
-  silc_mp_init_set_ui(&mp_dst, 0);
+  silc_mp_init(&mp_tmp);
+  silc_mp_init(&mp_dst);
+  silc_mp_set_ui(&mp_tmp, 0);
+  silc_mp_set_ui(&mp_dst, 0);
 
   /* Data to MP */
   silc_mp_bin2mp(src, src_len, &mp_tmp);
@@ -370,8 +375,8 @@ SILC_PKCS_API_DECRYPT(pkcs1)
   if (!unpadded) {
     memset(padded, 0, padded_len);
     silc_free(padded);
-    silc_mp_clear(&mp_tmp);
-    silc_mp_clear(&mp_dst);
+    silc_mp_uninit(&mp_tmp);
+    silc_mp_uninit(&mp_dst);
     return FALSE;
   }
 
@@ -383,8 +388,8 @@ SILC_PKCS_API_DECRYPT(pkcs1)
   memset(unpadded, 0, padded_len);
   silc_free(padded);
   silc_free(unpadded);
-  silc_mp_clear(&mp_tmp);
-  silc_mp_clear(&mp_dst);
+  silc_mp_uninit(&mp_tmp);
+  silc_mp_uninit(&mp_dst);
 
   return TRUE;
 }
@@ -392,19 +397,21 @@ SILC_PKCS_API_DECRYPT(pkcs1)
 SILC_PKCS_API_SIGN(pkcs1)
 {
   RsaKey *key = (RsaKey *)context;
-  SilcInt mp_tmp;
-  SilcInt mp_dst;
+  SilcMPInt mp_tmp;
+  SilcMPInt mp_dst;
   unsigned char *padded;
-  unsigned int padded_len;
-  unsigned int len = key->bits / 8;
+  SilcUInt32 padded_len;
+  SilcUInt32 len = key->bits / 8;
 
   /* Pad data */
   if (!RSA_FormatBlock(&padded, &padded_len, len, RSA_BlockPrivate, 
                       src, src_len))
     return FALSE;
 
-  silc_mp_init_set_ui(&mp_tmp, 0);
-  silc_mp_init_set_ui(&mp_dst, 0);
+  silc_mp_init(&mp_tmp);
+  silc_mp_init(&mp_dst);
+  silc_mp_set_ui(&mp_tmp, 0);
+  silc_mp_set_ui(&mp_dst, 0);
 
   /* Data to MP */
   silc_mp_bin2mp(padded, len, &mp_tmp);
@@ -418,8 +425,8 @@ SILC_PKCS_API_SIGN(pkcs1)
 
   memset(padded, 0, padded_len);
   silc_free(padded);
-  silc_mp_clear(&mp_tmp);
-  silc_mp_clear(&mp_dst);
+  silc_mp_uninit(&mp_tmp);
+  silc_mp_uninit(&mp_dst);
 
   return TRUE;
 }
@@ -428,13 +435,15 @@ SILC_PKCS_API_VERIFY(pkcs1)
 {
   RsaKey *key = (RsaKey *)context;
   int ret = TRUE;
-  SilcInt mp_tmp2;
-  SilcInt mp_dst;
+  SilcMPInt mp_tmp2;
+  SilcMPInt mp_dst;
   unsigned char *verify, *unpadded;
-  unsigned int verify_len, len = key->bits / 8;
+  SilcUInt32 verify_len, len = key->bits / 8;
 
-  silc_mp_init_set_ui(&mp_tmp2, 0);
-  silc_mp_init_set_ui(&mp_dst, 0);
+  silc_mp_init(&mp_tmp2);
+  silc_mp_init(&mp_dst);
+  silc_mp_set_ui(&mp_tmp2, 0);
+  silc_mp_set_ui(&mp_dst, 0);
 
   /* Format the signature into MP int */
   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
@@ -451,8 +460,8 @@ SILC_PKCS_API_VERIFY(pkcs1)
   if (!unpadded) {
     memset(verify, 0, verify_len);
     silc_free(verify);
-    silc_mp_clear(&mp_tmp2);
-    silc_mp_clear(&mp_dst);
+    silc_mp_uninit(&mp_tmp2);
+    silc_mp_uninit(&mp_dst);
     return FALSE;
   }
 
@@ -464,8 +473,8 @@ SILC_PKCS_API_VERIFY(pkcs1)
   memset(unpadded, 0, verify_len);
   silc_free(verify);
   silc_free(unpadded);
-  silc_mp_clear(&mp_tmp2);
-  silc_mp_clear(&mp_dst);
+  silc_mp_uninit(&mp_tmp2);
+  silc_mp_uninit(&mp_dst);
 
   return ret;
 }