X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcacc%2Fsilcacc_pkcs.c;h=cc666880811b635db25652f8c69b5a2da000a639;hb=80dc2a39c614ea1376a2e19ebe2460af11c9afee;hp=ee2c5a2275874e7888b2b72c63faa05e083f6bb7;hpb=1218ffebfc22091283bbd73886c8e7cd734e856f;p=crypto.git diff --git a/lib/silcacc/silcacc_pkcs.c b/lib/silcacc/silcacc_pkcs.c index ee2c5a22..cc666880 100644 --- a/lib/silcacc/silcacc_pkcs.c +++ b/lib/silcacc/silcacc_pkcs.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2007 Pekka Riikonen + Copyright (C) 2007 - 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,10 +17,12 @@ */ -#include "silc.h" +#include "silccrypto.h" /************************** 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,14 +324,15 @@ 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)) { - SILC_LOG_ERROR(("Error accelerating public key with accelerator '%s'", + if (!acc->pkcs[i].import_public_key(&acc->pkcs[i], public_key, 0, + &acc_pubkey->context)) { + SILC_LOG_DEBUG(("Error accelerating public key with accelerator '%s'", acc->name)); silc_free(acc_pubkey); silc_free(pubkey->pkcs); @@ -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,15 @@ 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)) { + /* Accelerate the public key. Returns accelerator context. The + import_public_key operation is used to accelerate the key. */ + 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);