Added SILC MAC API, removed the SILC HMAC API.
[crypto.git] / lib / silccrypt / silcpk.c
index ad03a023610ff65356e1ba9b099a2dc470cfee91..708fb49ab9b37e79c969835ec6c5418cba5564ab 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2007 Pekka Riikonen
+  Copyright (C) 1997 - 2008 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
@@ -17,7 +17,7 @@
 
 */
 
-#include "silc.h"
+#include "silccrypto.h"
 #include "silcpk_i.h"
 
 /****************************** Key generation *******************************/
@@ -126,7 +126,7 @@ SilcBool silc_pkcs_silc_decode_identifier(const char *identifier,
   int len;
 
   /* Protocol says that at least UN and HN must be provided as identifier */
-  if (!strstr(identifier, "UN=") && !strstr(identifier, "HN=")) {
+  if (!strstr(identifier, "UN=") || !strstr(identifier, "HN=")) {
     SILC_LOG_DEBUG(("The public does not have the required UN= and HN= "
                    "identifiers"));
     return FALSE;
@@ -207,9 +207,11 @@ char *silc_pkcs_silc_encode_identifier(SilcStack stack,
   SilcBufferStruct buf;
   char *identifier;
 
-  if (!username || !host)
+  if (!username || !host) {
+    SILC_LOG_ERROR(("Public key identifier is missing UN and/or HN"));
     return NULL;
-  if (strlen(username) < 3 || strlen(host) < 3)
+  }
+  if (strlen(username) < 1 || strlen(host) < 1)
     return NULL;
 
   memset(&buf, 0, sizeof(buf));
@@ -264,6 +266,7 @@ char *silc_pkcs_silc_encode_identifier(SilcStack stack,
   if (version) {
     if (strlen(version) > 1 || !isdigit(version[0])) {
       silc_buffer_spurge(stack, &buf);
+      SILC_LOG_ERROR(("Public key identifier has invalid version (V)"));
       return NULL;
     }
     silc_buffer_sformat(stack, &buf,
@@ -324,12 +327,12 @@ SILC_PKCS_IMPORT_PUBLIC_KEY_FILE(silc_pkcs_silc_import_public_key_file)
   /* Check start of file and remove header from the data. */
   len = strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN);
   if (filedata_len < len + strlen(SILC_PKCS_PUBLIC_KEYFILE_END)) {
-    SILC_LOG_ERROR(("Malformed SILC public key header"));
+    SILC_LOG_DEBUG(("Malformed SILC public key header"));
     return FALSE;
   }
   for (i = 0; i < len; i++) {
     if (*filedata != SILC_PKCS_PUBLIC_KEYFILE_BEGIN[i]) {
-      SILC_LOG_ERROR(("Malformed SILC public key header"));
+      SILC_LOG_DEBUG(("Malformed SILC public key header"));
       return FALSE;
     }
     filedata++;
@@ -349,7 +352,7 @@ SILC_PKCS_IMPORT_PUBLIC_KEY_FILE(silc_pkcs_silc_import_public_key_file)
     break;
   }
 
-  ret = silc_pkcs_silc_import_public_key(pkcs, filedata, filedata_len,
+  ret = silc_pkcs_silc_import_public_key(pkcs, NULL, filedata, filedata_len,
                                         ret_public_key, ret_alg);
   silc_free(data);
 
@@ -360,7 +363,6 @@ SILC_PKCS_IMPORT_PUBLIC_KEY_FILE(silc_pkcs_silc_import_public_key_file)
 
 SILC_PKCS_IMPORT_PUBLIC_KEY(silc_pkcs_silc_import_public_key)
 {
-  const SilcPKCSAlgorithm *alg;
   SilcBufferStruct buf, alg_key;
   SilcSILCPublicKey silc_pubkey = NULL;
   SilcAsn1 asn1 = NULL;
@@ -846,12 +848,12 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
   /* Check start of file and remove header from the data. */
   len = strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN);
   if (filedata_len < len + strlen(SILC_PKCS_PRIVATE_KEYFILE_END)) {
-    SILC_LOG_ERROR(("Malformed SILC private key header"));
+    SILC_LOG_DEBUG(("Malformed SILC private key header"));
     return FALSE;
   }
   for (i = 0; i < len; i++) {
     if (*filedata != SILC_PKCS_PRIVATE_KEYFILE_BEGIN[i]) {
-      SILC_LOG_ERROR(("Malformed SILC private key header"));
+      SILC_LOG_DEBUG(("Malformed SILC private key header"));
       return FALSE;
     }
     filedata++;
@@ -901,7 +903,7 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
   }
 
   /* Allocate HMAC */
-  if (!silc_hmac_alloc("hmac-sha1-96", NULL, &sha1hmac)) {
+  if (!silc_mac_alloc("hmac-sha1-96", &sha1hmac)) {
     SILC_LOG_ERROR(("Could not allocate SHA1 HMAC, probably not registered"));
     silc_hash_free(sha1);
     silc_cipher_free(aes);
@@ -924,15 +926,15 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
   silc_cipher_set_key(aes, keymat, 256, FALSE);
 
   /* First, verify the MAC of the private key data */
-  mac_len = silc_hmac_len(sha1hmac);
-  silc_hmac_init_with_key(sha1hmac, keymat, 16);
-  silc_hmac_update(sha1hmac, filedata, len - mac_len);
-  silc_hmac_final(sha1hmac, tmp, NULL);
+  mac_len = silc_mac_len(sha1hmac);
+  silc_mac_init_with_key(sha1hmac, keymat, 16);
+  silc_mac_update(sha1hmac, filedata, len - mac_len);
+  silc_mac_final(sha1hmac, tmp, NULL);
   if (memcmp(tmp, filedata + (len - mac_len), mac_len)) {
     SILC_LOG_DEBUG(("Integrity check for private key failed"));
     memset(keymat, 0, sizeof(keymat));
     memset(tmp, 0, sizeof(tmp));
-    silc_hmac_free(sha1hmac);
+    silc_mac_free(sha1hmac);
     silc_hash_free(sha1);
     silc_cipher_free(aes);
     return FALSE;
@@ -947,7 +949,7 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
     SILC_LOG_DEBUG(("Bad private key length in buffer!"));
     memset(keymat, 0, sizeof(keymat));
     memset(tmp, 0, sizeof(tmp));
-    silc_hmac_free(sha1hmac);
+    silc_mac_free(sha1hmac);
     silc_hash_free(sha1);
     silc_cipher_free(aes);
     return FALSE;
@@ -958,13 +960,13 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
   /* Cleanup */
   memset(keymat, 0, sizeof(keymat));
   memset(tmp, 0, sizeof(tmp));
-  silc_hmac_free(sha1hmac);
+  silc_mac_free(sha1hmac);
   silc_hash_free(sha1);
   silc_cipher_free(aes);
 
   /* Import the private key */
-  ret = silc_pkcs_silc_import_private_key(pkcs, filedata, len, ret_private_key,
-                                         ret_alg);
+  ret = silc_pkcs_silc_import_private_key(pkcs, NULL, NULL, 0, filedata,
+                                         len, ret_private_key, ret_alg);
 
   silc_free(data);
 
@@ -980,7 +982,6 @@ SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_import_private_key_file)
 SILC_PKCS_IMPORT_PRIVATE_KEY(silc_pkcs_silc_import_private_key)
 {
   SilcBufferStruct buf;
-  const SilcPKCSAlgorithm *alg;
   SilcBufferStruct alg_key;
   SilcSILCPrivateKey silc_privkey = NULL;
   SilcAsn1 asn1 = NULL;
@@ -1321,7 +1322,7 @@ SILC_PKCS_EXPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_export_private_key_file)
   }
 
   /* Allocate HMAC */
-  if (!silc_hmac_alloc("hmac-sha1-96", NULL, &sha1hmac)) {
+  if (!silc_mac_alloc("hmac-sha1-96", &sha1hmac)) {
     SILC_LOG_ERROR(("Could not allocate SHA1 HMAC, probably not registered"));
     silc_sfree(stack, key);
     silc_hash_free(sha1);
@@ -1348,12 +1349,12 @@ SILC_PKCS_EXPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_export_private_key_file)
      block size of the cipher. */
 
   /* Allocate buffer for encryption */
-  len = silc_hmac_len(sha1hmac);
+  len = silc_mac_len(sha1hmac);
   padlen = 16 + (16 - ((key_len + 4) % blocklen));
   enc = silc_buffer_salloc_size(stack, 4 + 4 + key_len + padlen + len);
   if (!enc) {
     silc_sfree(stack, key);
-    silc_hmac_free(sha1hmac);
+    silc_mac_free(sha1hmac);
     silc_hash_free(sha1);
     silc_cipher_free(aes);
     return FALSE;
@@ -1384,16 +1385,16 @@ SILC_PKCS_EXPORT_PRIVATE_KEY_FILE(silc_pkcs_silc_export_private_key_file)
   /* Compute HMAC over the encrypted data and append the MAC to data.
      The key is the first digest of the original key material. */
   key_len = silc_buffer_len(enc) - len;
-  silc_hmac_init_with_key(sha1hmac, keymat, 16);
-  silc_hmac_update(sha1hmac, enc->data, key_len);
+  silc_mac_init_with_key(sha1hmac, keymat, 16);
+  silc_mac_update(sha1hmac, enc->data, key_len);
   silc_buffer_pull(enc, key_len);
-  silc_hmac_final(sha1hmac, enc->data, NULL);
+  silc_mac_final(sha1hmac, enc->data, NULL);
   silc_buffer_push(enc, key_len);
 
   /* Cleanup */
   memset(keymat, 0, sizeof(keymat));
   memset(tmp, 0, sizeof(tmp));
-  silc_hmac_free(sha1hmac);
+  silc_mac_free(sha1hmac);
   silc_hash_free(sha1);
   silc_cipher_free(aes);
 
@@ -1636,7 +1637,7 @@ SILC_PKCS_SIGN(silc_pkcs_silc_sign)
   return silc_privkey->pkcs->sign(silc_privkey->pkcs,
                                  silc_privkey->private_key,
                                  src, src_len,
-                                 compute_hash, hash,
+                                 compute_hash, hash, rng,
                                  sign_cb, context);
 }
 
@@ -1654,6 +1655,6 @@ SILC_PKCS_VERIFY(silc_pkcs_silc_verify)
   return silc_pubkey->pkcs->verify(silc_pubkey->pkcs,
                                   silc_pubkey->public_key,
                                   signature, signature_len,
-                                  data, data_len, hash,
+                                  data, data_len, hash != NULL, hash, rng,
                                   verify_cb, context);
 }