updates.
[silc.git] / lib / silccrypt / silcpkcs.c
index 74870ec8a33f0c4ee4361190d4e521efa99841dc..a5ebf6be46a697f26026bce93ec490b30c8f7ad2 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
 #include "silcincludes.h"
 
 #include "rsa.h"
+#include "pkcs1.h"
 
 /* List of all PKCS's in SILC. PKCS's don't support SIM's thus
    only static declarations are possible. XXX: I hope this to change
    real soon. */
 SilcPKCSObject silc_pkcs_list[] =
 {
+  /* RSA with PKCS #1 (Uses directly routines from Raw RSA operations) */
   { "rsa", &silc_rsa_data_context, 
+    silc_rsa_init, silc_rsa_clear_keys, silc_rsa_get_public_key,
+    silc_rsa_get_private_key, silc_rsa_set_public_key,
+    silc_rsa_set_private_key, silc_rsa_context_len,
+    silc_rsa_data_context_len, silc_rsa_set_arg,
+    silc_pkcs1_encrypt, silc_pkcs1_decrypt,
+    silc_pkcs1_sign, silc_pkcs1_verify },
+
+  /* Raw RSA operations */
+  { "rsa-raw", &silc_rsa_data_context, 
     silc_rsa_init, silc_rsa_clear_keys, silc_rsa_get_public_key,
     silc_rsa_get_private_key, silc_rsa_set_public_key,
     silc_rsa_set_private_key, silc_rsa_context_len,
@@ -85,6 +96,9 @@ int silc_pkcs_is_supported(const unsigned char *name)
 {
   int i;
 
+  if (!name)
+    return FALSE;
+
   for (i = 0; silc_pkcs_list[i].name; i++) {
     if (!strcmp(silc_pkcs_list[i].name, name))
       return TRUE;
@@ -169,6 +183,86 @@ int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
   return pkcs->pkcs->set_private_key(pkcs->context, prv, prv_len);
 }
 
+/* Encrypts */
+
+int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, unsigned int src_len,
+                     unsigned char *dst, unsigned int *dst_len)
+{
+  return pkcs->pkcs->encrypt(pkcs->context, src, src_len, dst, dst_len);
+}
+
+/* Decrypts */
+
+int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, unsigned int src_len,
+                     unsigned char *dst, unsigned int *dst_len)
+{
+  return pkcs->pkcs->decrypt(pkcs->context, src, src_len, dst, dst_len);
+}
+
+/* Generates signature */
+
+int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, unsigned int src_len,
+                  unsigned char *dst, unsigned int *dst_len)
+{
+  return pkcs->pkcs->sign(pkcs->context, src, src_len, dst, dst_len);
+}
+
+/* Verifies signature */
+
+int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature, 
+                    unsigned int signature_len, unsigned char *data, 
+                    unsigned int data_len)
+{
+  return pkcs->pkcs->verify(pkcs->context, signature, signature_len, 
+                           data, data_len);
+}
+
+/* Generates signature with hash. The hash is signed. */
+
+int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
+                            unsigned char *src, unsigned int src_len,
+                            unsigned char *dst, unsigned int *dst_len)
+{
+  unsigned char hashr[32];
+  unsigned int hash_len;
+  int ret;
+
+  silc_hash_make(hash, src, src_len, hashr);
+  hash_len = hash->hash->hash_len;
+
+  SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
+
+  ret = pkcs->pkcs->sign(pkcs->context, hashr, hash_len, dst, dst_len);
+  memset(hashr, 0, sizeof(hashr));
+
+  return ret;
+}
+
+/* Verifies signature with hash. The `data' is hashed and verified against
+   the `signature'. */
+
+int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash, 
+                              unsigned char *signature, 
+                              unsigned int signature_len, 
+                              unsigned char *data, 
+                              unsigned int data_len)
+{
+  unsigned char hashr[32];
+  unsigned int hash_len;
+  int ret;
+
+  silc_hash_make(hash, data, data_len, hashr);
+  hash_len = hash->hash->hash_len;
+
+  SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
+
+  ret = pkcs->pkcs->verify(pkcs->context, signature, signature_len, 
+                          hashr, hash_len);
+  memset(hashr, 0, sizeof(hashr));
+
+  return ret;
+}
+
 /* Encodes and returns SILC public key identifier. If some of the 
    arguments is NULL those are not encoded into the identifier string.
    Protocol says that at least username and host must be provided. */