15
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef MRSA |
|
15 |
143
|
16 /* This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] */ |
15
|
17 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) |
|
18 { |
|
19 int err; |
|
20 |
|
21 _ARGCHK(out != NULL); |
|
22 _ARGCHK(outlen != NULL); |
|
23 _ARGCHK(key != NULL); |
|
24 |
|
25 /* type valid? */ |
143
|
26 if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) { |
15
|
27 return CRYPT_PK_INVALID_TYPE; |
|
28 } |
143
|
29 |
|
30 if (type == PK_PRIVATE) { |
|
31 /* private key */ |
|
32 mp_int zero; |
15
|
33 |
143
|
34 /* first INTEGER == 0 to signify two-prime RSA */ |
|
35 if ((err = mp_init(&zero)) != MP_OKAY) { |
|
36 return mpi_to_ltc_error(err); |
|
37 } |
|
38 |
|
39 /* output is |
|
40 Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p |
|
41 */ |
|
42 err = der_put_multi_integer(out, outlen, &zero, &key->N, &key->e, |
|
43 &key->d, &key->p, &key->q, &key->dP, |
|
44 &key->dQ, &key->qP, NULL); |
|
45 |
|
46 /* clear zero and return */ |
|
47 mp_clear(&zero); |
|
48 return err; |
|
49 } else { |
|
50 /* public key */ |
|
51 return der_put_multi_integer(out, outlen, &key->N, &key->e, NULL); |
15
|
52 } |
|
53 } |
|
54 |
|
55 #endif /* MRSA */ |
|
56 |