Mercurial > dropbear
comparison rsa_export.c @ 143:5d99163f7e32 libtomcrypt-orig
import of libtomcrypt 0.99
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 19 Dec 2004 11:34:45 +0000 |
parents | 6362d3854bb4 |
children |
comparison
equal
deleted
inserted
replaced
15:6362d3854bb4 | 143:5d99163f7e32 |
---|---|
11 | 11 |
12 #include "mycrypt.h" | 12 #include "mycrypt.h" |
13 | 13 |
14 #ifdef MRSA | 14 #ifdef MRSA |
15 | 15 |
16 /* This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] */ | |
16 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) | 17 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) |
17 { | 18 { |
18 unsigned long y, z; | |
19 int err; | 19 int err; |
20 | 20 |
21 _ARGCHK(out != NULL); | 21 _ARGCHK(out != NULL); |
22 _ARGCHK(outlen != NULL); | 22 _ARGCHK(outlen != NULL); |
23 _ARGCHK(key != 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 | 24 |
30 /* type valid? */ | 25 /* type valid? */ |
31 if (!(key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) && | 26 if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) { |
32 (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED)) { | |
33 return CRYPT_PK_INVALID_TYPE; | 27 return CRYPT_PK_INVALID_TYPE; |
34 } | 28 } |
29 | |
30 if (type == PK_PRIVATE) { | |
31 /* private key */ | |
32 mp_int zero; | |
35 | 33 |
36 /* start at offset y=PACKET_SIZE */ | 34 /* first INTEGER == 0 to signify two-prime RSA */ |
37 y = PACKET_SIZE; | 35 if ((err = mp_init(&zero)) != MP_OKAY) { |
38 | 36 return mpi_to_ltc_error(err); |
39 /* output key type */ | 37 } |
40 out[y++] = type; | 38 |
41 | 39 /* output is |
42 /* output modulus */ | 40 Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p |
43 OUTPUT_BIGNUM(&key->N, out, y, z); | 41 */ |
44 | 42 err = der_put_multi_integer(out, outlen, &zero, &key->N, &key->e, |
45 /* output public key */ | 43 &key->d, &key->p, &key->q, &key->dP, |
46 OUTPUT_BIGNUM(&key->e, out, y, z); | 44 &key->dQ, &key->qP, NULL); |
47 | 45 |
48 if (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED) { | 46 /* clear zero and return */ |
49 OUTPUT_BIGNUM(&key->d, out, y, z); | 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); | |
50 } | 52 } |
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 } | 53 } |
70 | 54 |
71 #endif /* MRSA */ | 55 #endif /* MRSA */ |
72 | 56 |