1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
15 Export RSA PKCS keys, Tom St Denis
21 This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1]
22 @param out [out] Destination of the packet
23 @param outlen [in/out] The max size and resulting size of the packet
24 @param type The type of exported key (PK_PRIVATE or PK_PUBLIC)
25 @param key The RSA key to export
26 @return CRYPT_OK if successful
28 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key)
31 LTC_ARGCHK(out != NULL);
32 LTC_ARGCHK(outlen != NULL);
33 LTC_ARGCHK(key != NULL);
36 if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
37 return CRYPT_PK_INVALID_TYPE;
40 if (type == PK_PRIVATE) {
43 Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p
45 return der_encode_sequence_multi(out, outlen,
46 LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
47 LTC_ASN1_INTEGER, 1UL, key->N,
48 LTC_ASN1_INTEGER, 1UL, key->e,
49 LTC_ASN1_INTEGER, 1UL, key->d,
50 LTC_ASN1_INTEGER, 1UL, key->p,
51 LTC_ASN1_INTEGER, 1UL, key->q,
52 LTC_ASN1_INTEGER, 1UL, key->dP,
53 LTC_ASN1_INTEGER, 1UL, key->dQ,
54 LTC_ASN1_INTEGER, 1UL, key->qP,
55 LTC_ASN1_EOL, 0UL, NULL);
58 return der_encode_sequence_multi(out, outlen,
59 LTC_ASN1_INTEGER, 1UL, key->N,
60 LTC_ASN1_INTEGER, 1UL, key->e,
61 LTC_ASN1_EOL, 0UL, NULL);
67 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_export.c,v $ */
68 /* $Revision: 1.15 $ */
69 /* $Date: 2006/03/31 14:15:35 $ */