From: Pekka Riikonen Date: Sun, 7 Jan 2007 15:48:16 +0000 (+0000) Subject: PKCS import routines now return the bytes imported. X-Git-Tag: silc.client.1.1.beta1~67 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=a5f75b246a2bbcf74e3a68eec3a9064eb93b83d0 PKCS import routines now return the bytes imported. Added silc_ prefix to RSA routines. --- diff --git a/lib/silccrypt/rsa.c b/lib/silccrypt/rsa.c index e58bca15..96fbf1e3 100644 --- a/lib/silccrypt/rsa.c +++ b/lib/silccrypt/rsa.c @@ -5,7 +5,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2006 Pekka Riikonen + Copyright (C) 1997 - 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 @@ -81,8 +81,8 @@ to compute the modulus n has to be generated before calling this. They are then sent as argument for the function. */ -SilcBool rsa_generate_keys(SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q, - void **ret_public_key, void **ret_private_key) +SilcBool silc_rsa_generate_keys(SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q, + void **ret_public_key, void **ret_private_key) { RsaPublicKey *pubkey; RsaPrivateKey *privkey; @@ -167,8 +167,8 @@ SilcBool rsa_generate_keys(SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q, /* RSA public key operation */ -SilcBool rsa_public_operation(RsaPublicKey *key, SilcMPInt *src, - SilcMPInt *dst) +SilcBool silc_rsa_public_operation(RsaPublicKey *key, SilcMPInt *src, + SilcMPInt *dst) { /* dst = src ^ e mod n */ silc_mp_pow_mod(dst, src, &key->e, &key->n); @@ -177,8 +177,8 @@ SilcBool rsa_public_operation(RsaPublicKey *key, SilcMPInt *src, /* RSA private key operation */ -SilcBool rsa_private_operation(RsaPrivateKey *key, SilcMPInt *src, - SilcMPInt *dst) +SilcBool silc_rsa_private_operation(RsaPrivateKey *key, SilcMPInt *src, + SilcMPInt *dst) { SilcMPInt tmp; diff --git a/lib/silccrypt/rsa.h b/lib/silccrypt/rsa.h index 0b05728e..81247b05 100644 --- a/lib/silccrypt/rsa.h +++ b/lib/silccrypt/rsa.h @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2006 Pekka Riikonen + Copyright (C) 1997 - 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 @@ -37,14 +37,14 @@ typedef struct { SilcMPInt q; /* CRT, q */ SilcMPInt dP; /* CRT, d mod p - 1 */ SilcMPInt dQ; /* CRT, d mod q - 1 */ - SilcMPInt qP; /* CRT, q ^ -1 mod p */ + SilcMPInt qP; /* CRT, q ^ -1 mod p (aka u, aka qInv) */ } RsaPrivateKey; -SilcBool rsa_generate_keys(SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q, - void **ret_public_key, void **ret_private_key); -SilcBool rsa_public_operation(RsaPublicKey *key, SilcMPInt *src, - SilcMPInt *dst); -SilcBool rsa_private_operation(RsaPrivateKey *key, SilcMPInt *src, - SilcMPInt *dst); +SilcBool silc_rsa_generate_keys(SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q, + void **ret_public_key, void **ret_private_key); +SilcBool silc_rsa_public_operation(RsaPublicKey *key, SilcMPInt *src, + SilcMPInt *dst); +SilcBool silc_rsa_private_operation(RsaPrivateKey *key, SilcMPInt *src, + SilcMPInt *dst); #endif /* RSA_H */ diff --git a/lib/silccrypt/silcpk.c b/lib/silccrypt/silcpk.c index cb9ef422..128f46f7 100644 --- a/lib/silccrypt/silcpk.c +++ b/lib/silccrypt/silcpk.c @@ -303,7 +303,7 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata, { SilcUInt32 i, len; unsigned char *data = NULL; - SilcBool ret; + int ret; SILC_LOG_DEBUG(("Parsing SILC public key file")); @@ -338,14 +338,14 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata, ret_public_key); silc_free(data); - return ret; + return ret ? TRUE : FALSE; } /* Imports SILC protocol style public key */ -SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key) +int silc_pkcs_silc_import_public_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key) { const SilcPKCSAlgorithm *pkcs; SilcBufferStruct buf, alg_key; @@ -359,7 +359,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, SILC_LOG_DEBUG(("Parsing SILC public key")); if (!ret_public_key) - return FALSE; + return 0; silc_buffer_set(&buf, key, key_len); @@ -471,10 +471,9 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, } /* Import PKCS algorithm public key */ - if (pkcs->import_public_key) - if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key), - &silc_pubkey->public_key)) - goto err; + if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key), + &silc_pubkey->public_key)) + goto err; silc_free(pkcs_name); silc_free(ident); @@ -482,7 +481,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, *ret_public_key = silc_pubkey; - return TRUE; + return key_len; err: silc_free(pkcs_name); @@ -490,7 +489,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, silc_free(silc_pubkey); if (asn1) silc_asn1_free(asn1); - return FALSE; + return 0; } /* Exports public key as SILC protocol style public key file */ @@ -785,7 +784,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata, SilcUInt32 blocklen; unsigned char tmp[32], keymat[64], *data = NULL; SilcUInt32 i, len, magic, mac_len; - SilcBool ret; + int ret; SILC_LOG_DEBUG(("Parsing SILC private key file")); @@ -909,7 +908,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata, silc_free(data); - return ret; + return ret ? TRUE : FALSE; } /* Private key version */ @@ -917,9 +916,9 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata, /* Imports SILC implementation style private key */ -SilcBool silc_pkcs_silc_import_private_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key) +int silc_pkcs_silc_import_private_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key) { SilcBufferStruct buf; const SilcPKCSAlgorithm *pkcs; @@ -934,7 +933,7 @@ SilcBool silc_pkcs_silc_import_private_key(unsigned char *key, SILC_LOG_DEBUG(("Parsing SILC private key")); if (!ret_private_key) - return FALSE; + return 0; silc_buffer_set(&buf, key, key_len); @@ -1185,24 +1184,23 @@ SilcBool silc_pkcs_silc_import_private_key(unsigned char *key, } /* Import PKCS algorithm private key */ - if (pkcs->import_private_key) - if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key), - &silc_privkey->private_key)) - goto err; + if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key), + &silc_privkey->private_key)) + goto err; silc_free(pkcs_name); silc_asn1_free(asn1); *ret_private_key = silc_privkey; - return TRUE; + return key_len; err: silc_free(pkcs_name); silc_free(silc_privkey); if (asn1) silc_asn1_free(asn1); - return FALSE; + return 0; } /* Exports private key as SILC implementation style private key file */ diff --git a/lib/silccrypt/silcpk_i.h b/lib/silccrypt/silcpk_i.h index 06fe473a..9f6aa76e 100644 --- a/lib/silccrypt/silcpk_i.h +++ b/lib/silccrypt/silcpk_i.h @@ -31,9 +31,9 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata, SilcUInt32 filedata_len, SilcPKCSFileEncoding encoding, void **ret_public_key); -SilcBool silc_pkcs_silc_import_public_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key); +int silc_pkcs_silc_import_public_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key); unsigned char * silc_pkcs_silc_export_public_key_file(void *public_key, SilcPKCSFileEncoding encoding, @@ -50,9 +50,9 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata, SilcUInt32 passphrase_len, SilcPKCSFileEncoding encoding, void **ret_private_key); -SilcBool silc_pkcs_silc_import_private_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key); +int silc_pkcs_silc_import_private_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key); unsigned char * silc_pkcs_silc_export_private_key_file(void *private_key, const char *passphrase, diff --git a/lib/silccrypt/silcpkcs.h b/lib/silccrypt/silcpkcs.h index 78812b0f..dab35b30 100644 --- a/lib/silccrypt/silcpkcs.h +++ b/lib/silccrypt/silcpkcs.h @@ -42,8 +42,7 @@ typedef struct SilcPKCSObjectStruct SilcPKCSObject; * * DESCRIPTION * - * Public key cryptosystem types. These are defined by the SILC - * Key Exchange protocol. + * Supported public key cryptosystem types. * * SOURCE */ @@ -129,10 +128,10 @@ typedef struct { void **ret_public_key, void **ret_private_key); - /* Public key routines */ - SilcBool (*import_public_key)(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key); + /* Public key routines. */ + int (*import_public_key)(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key); unsigned char *(*export_public_key)(void *public_key, SilcUInt32 *ret_len); SilcUInt32 (*public_key_bitlen)(void *public_key); @@ -141,9 +140,9 @@ typedef struct { void (*public_key_free)(void *public_key); /* Private key routines */ - SilcBool (*import_private_key)(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key); + int (*import_private_key)(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key); unsigned char *(*export_private_key)(void *private_key, SilcUInt32 *ret_len); SilcUInt32 (*private_key_bitlen)(void *public_key); @@ -196,10 +195,11 @@ struct SilcPKCSObjectStruct { SilcPKCSFileEncoding encoding, void **ret_public_key); - /* Imports from public key binary data */ - SilcBool (*import_public_key)(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key); + /* Imports from public key binary data. Returns the amount of bytes + imported from `key' or 0 on error. */ + int (*import_public_key)(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key); /* Exports public key to file */ unsigned char *(*export_public_key_file)(void *public_key, @@ -232,10 +232,11 @@ struct SilcPKCSObjectStruct { SilcPKCSFileEncoding encoding, void **ret_private_key); - /* Imports from private key binary data */ - SilcBool (*import_private_key)(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key); + /* Imports from private key binary data. Returns the amount of bytes + imported from `key' or 0 on error. */ + int (*import_private_key)(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key); /* Exports private key to file */ unsigned char *(*export_private_key_file)(void *private_key, @@ -286,8 +287,8 @@ struct SilcPKCSObjectStruct { SilcHash hash); }; -/* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister - to unregister all PKCS at once. */ +/* Marks for all PKCS in. This can be used in silc_pkcs_unregister to + unregister all PKCS at once. */ #define SILC_ALL_PKCS ((SilcPKCSObject *)1) #define SILC_ALL_PKCS_ALG ((SilcPKCSAlgorithm *)1) diff --git a/lib/silccrypt/silcpkcs1.c b/lib/silccrypt/silcpkcs1.c index e3c6b68f..6fd603ed 100644 --- a/lib/silccrypt/silcpkcs1.c +++ b/lib/silccrypt/silcpkcs1.c @@ -20,6 +20,7 @@ #include "silc.h" #include "rsa.h" +#include "silcpkcs1_i.h" /************************** PKCS #1 message format ***************************/ @@ -204,7 +205,7 @@ SilcBool silc_pkcs1_generate_key(SilcUInt32 keylen, } /* Generate the actual keys */ - if (!rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key)) + if (!silc_rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key)) return FALSE; silc_mp_uninit(&p); @@ -215,20 +216,20 @@ SilcBool silc_pkcs1_generate_key(SilcUInt32 keylen, /* Import PKCS #1 compliant public key */ -SilcBool silc_pkcs1_import_public_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key) +int silc_pkcs1_import_public_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key) { SilcAsn1 asn1 = NULL; SilcBufferStruct alg_key; RsaPublicKey *pubkey; if (!ret_public_key) - return FALSE; + return 0; asn1 = silc_asn1_alloc(); if (!asn1) - return FALSE; + return 0; /* Allocate RSA public key */ *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey)); @@ -250,11 +251,12 @@ SilcBool silc_pkcs1_import_public_key(unsigned char *key, silc_asn1_free(asn1); - return TRUE; + return key_len; err: + silc_free(pubkey); silc_asn1_free(asn1); - return FALSE; + return 0; } /* Export PKCS #1 compliant public key */ @@ -348,9 +350,9 @@ void silc_pkcs1_public_key_free(void *public_key) /* Import PKCS #1 compliant private key */ -SilcBool silc_pkcs1_import_private_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key) +int silc_pkcs1_import_private_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key) { SilcAsn1 asn1; SilcBufferStruct alg_key; @@ -358,11 +360,11 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key, SilcUInt32 ver; if (!ret_private_key) - return FALSE; + return 0; asn1 = silc_asn1_alloc(); if (!asn1) - return FALSE; + return 0; /* Allocate RSA private key */ *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey)); @@ -394,11 +396,12 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key, silc_asn1_free(asn1); - return TRUE; + return key_len; err: + silc_free(privkey); silc_asn1_free(asn1); - return FALSE; + return 0; } /* Export PKCS #1 compliant private key */ @@ -500,7 +503,7 @@ SilcBool silc_pkcs1_encrypt(void *public_key, silc_mp_bin2mp(padded, len, &mp_tmp); /* Encrypt */ - rsa_public_operation(key, &mp_tmp, &mp_dst); + silc_rsa_public_operation(key, &mp_tmp, &mp_dst); /* MP to data */ silc_mp_mp2bin_noalloc(&mp_dst, dst, len); @@ -536,7 +539,7 @@ SilcBool silc_pkcs1_decrypt(void *private_key, silc_mp_bin2mp(src, src_len, &mp_tmp); /* Decrypt */ - rsa_private_operation(key, &mp_tmp, &mp_dst); + silc_rsa_private_operation(key, &mp_tmp, &mp_dst); /* MP to data */ padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len); @@ -624,7 +627,7 @@ SilcBool silc_pkcs1_sign_no_oid(void *private_key, silc_mp_bin2mp(padded, len, &mp_tmp); /* Sign */ - rsa_private_operation(key, &mp_tmp, &mp_dst); + silc_rsa_private_operation(key, &mp_tmp, &mp_dst); /* MP to data */ silc_mp_mp2bin_noalloc(&mp_dst, signature, len); @@ -662,7 +665,7 @@ SilcBool silc_pkcs1_verify_no_oid(void *public_key, silc_mp_bin2mp(signature, signature_len, &mp_tmp2); /* Verify */ - rsa_public_operation(key, &mp_tmp2, &mp_dst); + silc_rsa_public_operation(key, &mp_tmp2, &mp_dst); /* MP to data */ verify = silc_mp_mp2bin(&mp_dst, len, &verify_len); diff --git a/lib/silccrypt/silcpkcs1_i.h b/lib/silccrypt/silcpkcs1_i.h index 40899e06..d23b65b8 100644 --- a/lib/silccrypt/silcpkcs1_i.h +++ b/lib/silccrypt/silcpkcs1_i.h @@ -24,18 +24,18 @@ SilcBool silc_pkcs1_generate_key(SilcUInt32 keylen, SilcRng rng, void **ret_public_key, void **ret_private_key); -SilcBool silc_pkcs1_import_public_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_public_key); +int silc_pkcs1_import_public_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_public_key); unsigned char *silc_pkcs1_export_public_key(void *public_key, SilcUInt32 *ret_len); SilcUInt32 silc_pkcs1_public_key_bitlen(void *public_key); void *silc_pkcs1_public_key_copy(void *public_key); SilcBool silc_pkcs1_public_key_compare(void *key1, void *key2); void silc_pkcs1_public_key_free(void *public_key); -SilcBool silc_pkcs1_import_private_key(unsigned char *key, - SilcUInt32 key_len, - void **ret_private_key); +int silc_pkcs1_import_private_key(unsigned char *key, + SilcUInt32 key_len, + void **ret_private_key); unsigned char *silc_pkcs1_export_private_key(void *private_key, SilcUInt32 *ret_len); SilcUInt32 silc_pkcs1_private_key_bitlen(void *private_key);