Mercurial > dropbear
comparison libtomcrypt/src/pk/rsa/rsa_export.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children | e9dba7abd939 |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 /** | 11 /** |
14 @file rsa_export.c | 12 @file rsa_export.c |
15 Export RSA LTC_PKCS keys, Tom St Denis | 13 Export RSA PKCS keys, Tom St Denis |
16 */ | 14 */ |
17 | 15 |
18 #ifdef LTC_MRSA | 16 #ifdef LTC_MRSA |
19 | 17 |
20 /** | 18 /** |
21 This will export either an RSAPublicKey or RSAPrivateKey [defined in LTC_PKCS #1 v2.1] | 19 This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] |
22 @param out [out] Destination of the packet | 20 @param out [out] Destination of the packet |
23 @param outlen [in/out] The max size and resulting size of the packet | 21 @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) | 22 @param type The type of exported key (PK_PRIVATE or PK_PUBLIC) |
25 @param key The RSA key to export | 23 @param key The RSA key to export |
26 @return CRYPT_OK if successful | 24 @return CRYPT_OK if successful |
27 */ | 25 */ |
28 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) | 26 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) |
29 { | 27 { |
30 unsigned long zero=0; | 28 unsigned long zero=0; |
29 int err; | |
31 LTC_ARGCHK(out != NULL); | 30 LTC_ARGCHK(out != NULL); |
32 LTC_ARGCHK(outlen != NULL); | 31 LTC_ARGCHK(outlen != NULL); |
33 LTC_ARGCHK(key != NULL); | 32 LTC_ARGCHK(key != NULL); |
34 | 33 |
35 /* type valid? */ | 34 /* type valid? */ |
37 return CRYPT_PK_INVALID_TYPE; | 36 return CRYPT_PK_INVALID_TYPE; |
38 } | 37 } |
39 | 38 |
40 if (type == PK_PRIVATE) { | 39 if (type == PK_PRIVATE) { |
41 /* private key */ | 40 /* private key */ |
42 /* output is | 41 /* output is |
43 Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p | 42 Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p |
44 */ | 43 */ |
45 return der_encode_sequence_multi(out, outlen, | 44 return der_encode_sequence_multi(out, outlen, |
46 LTC_ASN1_SHORT_INTEGER, 1UL, &zero, | 45 LTC_ASN1_SHORT_INTEGER, 1UL, &zero, |
47 LTC_ASN1_INTEGER, 1UL, key->N, | 46 LTC_ASN1_INTEGER, 1UL, key->N, |
48 LTC_ASN1_INTEGER, 1UL, key->e, | 47 LTC_ASN1_INTEGER, 1UL, key->e, |
49 LTC_ASN1_INTEGER, 1UL, key->d, | 48 LTC_ASN1_INTEGER, 1UL, key->d, |
50 LTC_ASN1_INTEGER, 1UL, key->p, | 49 LTC_ASN1_INTEGER, 1UL, key->p, |
51 LTC_ASN1_INTEGER, 1UL, key->q, | 50 LTC_ASN1_INTEGER, 1UL, key->q, |
52 LTC_ASN1_INTEGER, 1UL, key->dP, | 51 LTC_ASN1_INTEGER, 1UL, key->dP, |
53 LTC_ASN1_INTEGER, 1UL, key->dQ, | 52 LTC_ASN1_INTEGER, 1UL, key->dQ, |
54 LTC_ASN1_INTEGER, 1UL, key->qP, | 53 LTC_ASN1_INTEGER, 1UL, key->qP, |
55 LTC_ASN1_EOL, 0UL, NULL); | 54 LTC_ASN1_EOL, 0UL, NULL); |
56 } else { | 55 } else { |
57 /* public key */ | 56 /* public key */ |
58 return der_encode_sequence_multi(out, outlen, | 57 unsigned long tmplen, *ptmplen; |
59 LTC_ASN1_INTEGER, 1UL, key->N, | 58 unsigned char* tmp = NULL; |
60 LTC_ASN1_INTEGER, 1UL, key->e, | 59 |
60 if (type & PK_STD) { | |
61 tmplen = (mp_count_bits(key->N)/8)*2+8; | |
62 tmp = XMALLOC(tmplen); | |
63 ptmplen = &tmplen; | |
64 if (tmp == NULL) { | |
65 return CRYPT_MEM; | |
66 } | |
67 } | |
68 else { | |
69 tmp = out; | |
70 ptmplen = outlen; | |
71 } | |
72 | |
73 err = der_encode_sequence_multi(tmp, ptmplen, | |
74 LTC_ASN1_INTEGER, 1UL, key->N, | |
75 LTC_ASN1_INTEGER, 1UL, key->e, | |
61 LTC_ASN1_EOL, 0UL, NULL); | 76 LTC_ASN1_EOL, 0UL, NULL); |
77 | |
78 if ((err != CRYPT_OK) || !(type & PK_STD)) { | |
79 goto finish; | |
80 } | |
81 | |
82 err = der_encode_subject_public_key_info(out, outlen, | |
83 PKA_RSA, tmp, tmplen, LTC_ASN1_NULL, NULL, 0); | |
84 | |
85 finish: | |
86 if (tmp != out) | |
87 XFREE(tmp); | |
88 return err; | |
89 | |
62 } | 90 } |
63 } | 91 } |
64 | 92 |
65 #endif /* LTC_MRSA */ | 93 #endif /* LTC_MRSA */ |
66 | 94 |
67 /* $Source$ */ | 95 /* ref: $Format:%D$ */ |
68 /* $Revision$ */ | 96 /* git commit: $Format:%H$ */ |
69 /* $Date$ */ | 97 /* commit time: $Format:%ai$ */ |