Added silc_ prefix to RSA routines.
Author: Pekka Riikonen <priikone@silcnet.org>
- 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
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;
/* 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);
/* 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;
Author: Pekka Riikonen <priikone@silcnet.org>
- 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
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 */
{
SilcUInt32 i, len;
unsigned char *data = NULL;
- SilcBool ret;
+ int ret;
SILC_LOG_DEBUG(("Parsing SILC public key file"));
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;
SILC_LOG_DEBUG(("Parsing SILC public key"));
if (!ret_public_key)
- return FALSE;
+ return 0;
silc_buffer_set(&buf, key, key_len);
}
/* 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);
*ret_public_key = silc_pubkey;
- return TRUE;
+ return key_len;
err:
silc_free(pkcs_name);
silc_free(silc_pubkey);
if (asn1)
silc_asn1_free(asn1);
- return FALSE;
+ return 0;
}
/* Exports public key as SILC protocol style public key file */
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"));
silc_free(data);
- return ret;
+ return ret ? TRUE : FALSE;
}
/* Private key version */
/* 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;
SILC_LOG_DEBUG(("Parsing SILC private key"));
if (!ret_private_key)
- return FALSE;
+ return 0;
silc_buffer_set(&buf, key, key_len);
}
/* 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 */
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,
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,
*
* DESCRIPTION
*
- * Public key cryptosystem types. These are defined by the SILC
- * Key Exchange protocol.
+ * Supported public key cryptosystem types.
*
* SOURCE
*/
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);
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);
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,
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,
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)
#include "silc.h"
#include "rsa.h"
+#include "silcpkcs1_i.h"
/************************** PKCS #1 message format ***************************/
}
/* 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);
/* 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));
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 */
/* 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;
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));
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 */
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);
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);
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);
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);
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);