Added preliminary Symbian support.
[silc.git] / lib / silccrypt / silcpkcs1.c
index 5820861d9fd36774b421dbf704d87b9728162588..e3c6b68f38f75e305728f57c0d0fed4f7c1dd494 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2006 Pekka Riikonen
+  Copyright (C) 2003 - 2007 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
@@ -74,14 +74,15 @@ SilcBool silc_pkcs1_encode(SilcPkcs1BlockType bt,
 
   case SILC_PKCS1_BT_PUB:
     /* Encryption */
+    if (!rng) {
+      SILC_LOG_ERROR(("Cannot encrypt: random number generator not provided"));
+      return FALSE;
+    }
 
     /* It is guaranteed this routine does not return zero byte. */
-    if (rng)
-      for (i = 2; i < padlen; i++)
-       dest_data[i] = silc_rng_get_byte_fast(rng);
-    else
-      for (i = 2; i < padlen; i++)
-       dest_data[i] = silc_rng_global_get_byte_fast();
+    for (i = 2; i < padlen; i++)
+      dest_data[i] = silc_rng_get_byte_fast(rng);
+
     break;
   }
 
@@ -354,6 +355,7 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key,
   SilcAsn1 asn1;
   SilcBufferStruct alg_key;
   RsaPrivateKey *privkey;
+  SilcUInt32 ver;
 
   if (!ret_private_key)
     return FALSE;
@@ -372,7 +374,7 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key,
   if (!silc_asn1_decode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                         SILC_ASN1_INT(NULL),
+                         SILC_ASN1_SHORT_INT(&ver),
                          SILC_ASN1_INT(&privkey->n),
                          SILC_ASN1_INT(&privkey->e),
                          SILC_ASN1_INT(&privkey->d),
@@ -384,6 +386,9 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key,
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
 
+  if (ver != 0)
+    goto err;
+
   /* Set key length */
   privkey->bits = silc_mp_sizeinbase(&privkey->n, 2);
 
@@ -404,7 +409,6 @@ unsigned char *silc_pkcs1_export_private_key(void *private_key,
   RsaPrivateKey *key = private_key;
   SilcAsn1 asn1;
   SilcBufferStruct alg_key;
-  SilcMPInt version;
   unsigned char *ret;
 
   asn1 = silc_asn1_alloc();
@@ -412,13 +416,11 @@ unsigned char *silc_pkcs1_export_private_key(void *private_key,
     return FALSE;
 
   /* Encode to PKCS #1 private key */
-  silc_mp_init(&version);
-  silc_mp_set_ui(&version, 0);
   memset(&alg_key, 0, sizeof(alg_key));
   if (!silc_asn1_encode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                         SILC_ASN1_INT(&version),
+                         SILC_ASN1_SHORT_INT(0),
                          SILC_ASN1_INT(&key->n),
                          SILC_ASN1_INT(&key->e),
                          SILC_ASN1_INT(&key->d),
@@ -429,7 +431,6 @@ unsigned char *silc_pkcs1_export_private_key(void *private_key,
                          SILC_ASN1_INT(&key->qP),
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
-  silc_mp_uninit(&version);
 
   ret = silc_buffer_steal(&alg_key, ret_len);
   silc_asn1_free(asn1);
@@ -473,7 +474,8 @@ SilcBool silc_pkcs1_encrypt(void *public_key,
                            SilcUInt32 src_len,
                            unsigned char *dst,
                            SilcUInt32 dst_size,
-                           SilcUInt32 *ret_dst_len)
+                           SilcUInt32 *ret_dst_len,
+                           SilcRng rng)
 {
   RsaPublicKey *key = public_key;
   SilcMPInt mp_tmp;
@@ -488,7 +490,7 @@ SilcBool silc_pkcs1_encrypt(void *public_key,
 
   /* Pad data */
   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
-                        padded, len, NULL))
+                        padded, len, rng))
     return FALSE;
 
   silc_mp_init(&mp_tmp);
@@ -679,10 +681,13 @@ SilcBool silc_pkcs1_verify_no_oid(void *public_key,
   if (hash) {
     silc_hash_make(hash, data, data_len, hashr);
     data = hashr;
+    data_len = silc_hash_len(hash);
   }
 
   /* Compare */
-  if (memcmp(data, unpadded, len))
+  if (len != data_len)
+    ret = FALSE;
+  else if (memcmp(data, unpadded, len))
     ret = FALSE;
 
   memset(verify, 0, verify_len);