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 |
|
16 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) |
|
17 { |
|
18 unsigned long y, z; |
|
19 int err; |
|
20 |
|
21 _ARGCHK(out != NULL); |
|
22 _ARGCHK(outlen != NULL); |
|
23 _ARGCHK(key != NULL); |
|
24 |
|
25 /* can we store the static header? */ |
|
26 if (*outlen < (PACKET_SIZE + 1)) { |
|
27 return CRYPT_BUFFER_OVERFLOW; |
|
28 } |
|
29 |
|
30 /* type valid? */ |
|
31 if (!(key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) && |
|
32 (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED)) { |
|
33 return CRYPT_PK_INVALID_TYPE; |
|
34 } |
|
35 |
|
36 /* start at offset y=PACKET_SIZE */ |
|
37 y = PACKET_SIZE; |
|
38 |
|
39 /* output key type */ |
|
40 out[y++] = type; |
|
41 |
|
42 /* output modulus */ |
|
43 OUTPUT_BIGNUM(&key->N, out, y, z); |
|
44 |
|
45 /* output public key */ |
|
46 OUTPUT_BIGNUM(&key->e, out, y, z); |
|
47 |
|
48 if (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED) { |
|
49 OUTPUT_BIGNUM(&key->d, out, y, z); |
|
50 } |
|
51 |
|
52 if (type == PK_PRIVATE_OPTIMIZED) { |
|
53 OUTPUT_BIGNUM(&key->dQ, out, y, z); |
|
54 OUTPUT_BIGNUM(&key->dP, out, y, z); |
|
55 OUTPUT_BIGNUM(&key->pQ, out, y, z); |
|
56 OUTPUT_BIGNUM(&key->qP, out, y, z); |
|
57 OUTPUT_BIGNUM(&key->p, out, y, z); |
|
58 OUTPUT_BIGNUM(&key->q, out, y, z); |
|
59 } |
|
60 |
|
61 /* store packet header */ |
|
62 packet_store_header(out, PACKET_SECT_RSA, PACKET_SUB_KEY); |
|
63 |
|
64 /* copy to the user buffer */ |
|
65 *outlen = y; |
|
66 |
|
67 /* clear stack and return */ |
|
68 return CRYPT_OK; |
|
69 } |
|
70 |
|
71 #endif /* MRSA */ |
|
72 |