Added SILC Thread Queue API
[silc.git] / lib / silcacc / silcacc_pkcs.c
index ee2c5a2275874e7888b2b72c63faa05e083f6bb7..d1b85e327d296d36430947fbf5cfff1313a3e200 100644 (file)
@@ -21,6 +21,8 @@
 
 /************************** Types and definitions ***************************/
 
+#define SILC_ACC_KEY_MAGIC 0xfde09137
+
 SILC_PKCS_GET_ALGORITHM(silc_acc_pkcs_get_algorithm);
 SILC_PKCS_IMPORT_PUBLIC_KEY_FILE(silc_acc_pkcs_import_public_key_file);
 SILC_PKCS_IMPORT_PUBLIC_KEY(silc_acc_pkcs_import_public_key);
@@ -43,6 +45,7 @@ SILC_PKCS_VERIFY(silc_acc_pkcs_verify);
 
 /* Accelerator public key */
 typedef struct {
+  SilcUInt32 magic;
   int pkcs_index;              /* Accelerator PKCS index */
   SilcAccelerator acc;         /* The accelerator */
   void *context;               /* Accelerator context */
@@ -51,6 +54,7 @@ typedef struct {
 
 /* Accelerator private key */
 typedef struct {
+  SilcUInt32 magic;
   int pkcs_index;              /* Accelerator PKCS index */
   SilcAccelerator acc;         /* The accelerator */
   void *context;               /* Accelerator context */
@@ -145,43 +149,70 @@ SILC_PKCS_PUBLIC_KEY_COPY(silc_acc_pkcs_public_key_copy)
 
 SILC_PKCS_PUBLIC_KEY_COMPARE(silc_acc_pkcs_public_key_compare)
 {
-  /* XXX */
-  return FALSE;
+  SilcAcceleratorPublicKey pub;
+
+  pub = key2;
+  if (pub->magic == SILC_ACC_KEY_MAGIC)
+    key2 = pub->accelerated->public_key;
+
+  pub = key1;
+
+  return pub->accelerated->pkcs->
+    public_key_compare(pub->accelerated->pkcs,
+                      pub->accelerated->public_key, key2);
 }
 
 SILC_PKCS_IMPORT_PRIVATE_KEY_FILE(silc_acc_pkcs_import_private_key_file)
 {
-  return 0;
+  /* Not implemented */
+  return FALSE;
 }
 
 SILC_PKCS_IMPORT_PRIVATE_KEY(silc_acc_pkcs_import_private_key)
 {
-  return 0;
+  /* Not implemented */
+  return FALSE;
 }
 
 SILC_PKCS_EXPORT_PRIVATE_KEY_FILE(silc_acc_pkcs_export_private_key_file)
 {
-  return 0;
+  SilcAcceleratorPrivateKey prv = private_key;
+  return prv->accelerated->pkcs->
+    export_private_key_file(prv->accelerated->pkcs, stack,
+                           prv->accelerated->private_key, passphrase,
+                           passphrase_len, encoding, rng, ret_len);
 }
 
 SILC_PKCS_EXPORT_PRIVATE_KEY(silc_acc_pkcs_export_private_key)
 {
-  return 0;
+  SilcAcceleratorPrivateKey prv = private_key;
+  return prv->accelerated->pkcs->
+    export_private_key(prv->accelerated->pkcs, stack,
+                      prv->accelerated->private_key, ret_len);
 }
 
 SILC_PKCS_PRIVATE_KEY_BITLEN(silc_acc_pkcs_private_key_bitlen)
 {
-  return 0;
+  SilcAcceleratorPrivateKey prv = private_key;
+  return prv->accelerated->pkcs->
+    private_key_bitlen(prv->accelerated->pkcs,
+                      prv->accelerated->private_key);
 }
 
+/* Accelerator routines follow */
+
 SILC_PKCS_PUBLIC_KEY_FREE(silc_acc_pkcs_public_key_free)
 {
-
+  SilcAcceleratorPublicKey pub = public_key;
+  pub->acc->pkcs[pub->pkcs_index].
+    public_key_free(&pub->acc->pkcs[pub->pkcs_index], pub->context);
 }
 
 SILC_PKCS_PRIVATE_KEY_FREE(silc_acc_pkcs_private_key_free)
 {
-
+  SilcAcceleratorPrivateKey prv = private_key;
+  prv->acc->pkcs[prv->pkcs_index].
+    private_key_free(&prv->acc->pkcs[prv->pkcs_index], prv->context);
 }
 
 SILC_PKCS_ENCRYPT(silc_acc_pkcs_encrypt)
@@ -211,7 +242,7 @@ SILC_PKCS_SIGN(silc_acc_pkcs_sign)
   /* Accelerate */
   return prv->acc->pkcs[prv->pkcs_index].sign(
                       &prv->acc->pkcs[prv->pkcs_index], prv->context, src,
-                      src_len, compute_hash, hash, sign_cb, context);
+                      src_len, compute_hash, hash, rng, sign_cb, context);
 }
 
 SILC_PKCS_VERIFY(silc_acc_pkcs_verify)
@@ -221,7 +252,7 @@ SILC_PKCS_VERIFY(silc_acc_pkcs_verify)
   /* Accelerate */
   return pub->acc->pkcs[pub->pkcs_index].verify(
                       &pub->acc->pkcs[pub->pkcs_index], pub->context,
-                      signature, signature_len, data, data_len, hash,
+                      signature, signature_len, data, data_len, hash, rng,
                       verify_cb, context);
 }
 
@@ -249,6 +280,11 @@ SilcPublicKey silc_acc_public_key(SilcAccelerator acc,
     return NULL;
   }
 
+  if (silc_acc_get_public_key(NULL, public_key)) {
+    SILC_LOG_DEBUG(("Pubilc key %p is already accelerated", public_key));
+    return NULL;
+  }
+
   /* Check that accelerator supports this public key algorithm */
   alg = silc_pkcs_get_algorithm(public_key);
   if (!alg)
@@ -263,7 +299,7 @@ SilcPublicKey silc_acc_public_key(SilcAccelerator acc,
   }
   if (alg) {
     SILC_LOG_DEBUG(("Accelerator %s does not support %s/%s acceleration",
-                   alg->name, alg->scheme));
+                   acc->name, alg->name, alg->scheme));
     return NULL;
   }
 
@@ -279,6 +315,7 @@ SilcPublicKey silc_acc_public_key(SilcAccelerator acc,
   }
   *pubkey->pkcs = silc_acc_pkcs;
   pubkey->pkcs->type = silc_pkcs_get_type(public_key);
+  pubkey->alg = silc_pkcs_get_algorithm(public_key);
 
   /* Allocate accelerator public key */
   acc_pubkey = silc_calloc(1, sizeof(*acc_pubkey));
@@ -287,13 +324,14 @@ SilcPublicKey silc_acc_public_key(SilcAccelerator acc,
     silc_free(pubkey);
     return NULL;
   }
+  acc_pubkey->magic = SILC_ACC_KEY_MAGIC;
   acc_pubkey->accelerated = public_key;
   acc_pubkey->acc = acc;
   acc_pubkey->pkcs_index = i;
 
   /* Accelerate the public key.  Returns accelerator context. */
-  if (!acc->pkcs->import_public_key(&acc->pkcs[i], public_key, 0,
-                                   &acc_pubkey->context)) {
+  if (!acc->pkcs[i].import_public_key(&acc->pkcs[i], public_key, 0,
+                                     &acc_pubkey->context)) {
     SILC_LOG_ERROR(("Error accelerating public key with accelerator '%s'",
                    acc->name));
     silc_free(acc_pubkey);
@@ -330,6 +368,11 @@ SilcPrivateKey silc_acc_private_key(SilcAccelerator acc,
     return NULL;
   }
 
+  if (silc_acc_get_private_key(NULL, private_key)) {
+    SILC_LOG_DEBUG(("Private key %p is already accelerated", private_key));
+    return NULL;
+  }
+
   /* Check that accelerator supports this private key algorithm */
   alg = silc_pkcs_get_algorithm(private_key);
   if (!alg)
@@ -344,7 +387,7 @@ SilcPrivateKey silc_acc_private_key(SilcAccelerator acc,
   }
   if (alg) {
     SILC_LOG_DEBUG(("Accelerator %s does not support %s/%s acceleration",
-                   alg->name, alg->scheme));
+                   acc->name, alg->name, alg->scheme));
     return NULL;
   }
 
@@ -360,6 +403,7 @@ SilcPrivateKey silc_acc_private_key(SilcAccelerator acc,
   }
   *privkey->pkcs = silc_acc_pkcs;
   privkey->pkcs->type = silc_pkcs_get_type(private_key);
+  privkey->alg = silc_pkcs_get_algorithm(private_key);
 
   /* Allocate accelerator public key */
   acc_privkey = silc_calloc(1, sizeof(*acc_privkey));
@@ -368,13 +412,14 @@ SilcPrivateKey silc_acc_private_key(SilcAccelerator acc,
     silc_free(privkey);
     return NULL;
   }
+  acc_privkey->magic = SILC_ACC_KEY_MAGIC;
   acc_privkey->accelerated = private_key;
   acc_privkey->acc = acc;
   acc_privkey->pkcs_index = i;
 
   /* Accelerate the public key.  Returns accelerator context. */
-  if (!acc->pkcs->import_private_key(&acc->pkcs[i], private_key, 0,
-                                    &acc_privkey->context)) {
+  if (!acc->pkcs[i].import_private_key(&acc->pkcs[i], private_key, 0,
+                                      &acc_privkey->context)) {
     SILC_LOG_ERROR(("Error accelerating private key with accelerator '%s'",
                    acc->name));
     silc_free(acc_privkey);