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_import(const unsigned char *in, unsigned long inlen, rsa_key *key) |
|
17 { |
|
18 unsigned long x, y; |
|
19 int err; |
|
20 |
|
21 _ARGCHK(in != NULL); |
|
22 _ARGCHK(key != NULL); |
|
23 |
|
24 /* check length */ |
|
25 if (inlen < (1+PACKET_SIZE)) { |
|
26 return CRYPT_INVALID_PACKET; |
|
27 } |
|
28 |
|
29 /* test packet header */ |
|
30 if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_RSA, PACKET_SUB_KEY)) != CRYPT_OK) { |
|
31 return err; |
|
32 } |
|
33 |
|
34 /* init key */ |
|
35 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, |
|
36 &key->pQ, &key->p, &key->q, NULL)) != MP_OKAY) { |
|
37 return mpi_to_ltc_error(err); |
|
38 } |
|
39 |
|
40 /* get key type */ |
|
41 y = PACKET_SIZE; |
|
42 key->type = (int)in[y++]; |
|
43 |
|
44 /* load the modulus */ |
|
45 INPUT_BIGNUM(&key->N, in, x, y, inlen); |
|
46 |
|
47 /* load public exponent */ |
|
48 INPUT_BIGNUM(&key->e, in, x, y, inlen); |
|
49 |
|
50 /* get private exponent */ |
|
51 if (key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) { |
|
52 INPUT_BIGNUM(&key->d, in, x, y, inlen); |
|
53 } |
|
54 |
|
55 /* get CRT private data if required */ |
|
56 if (key->type == PK_PRIVATE_OPTIMIZED) { |
|
57 INPUT_BIGNUM(&key->dQ, in, x, y, inlen); |
|
58 INPUT_BIGNUM(&key->dP, in, x, y, inlen); |
|
59 INPUT_BIGNUM(&key->pQ, in, x, y, inlen); |
|
60 INPUT_BIGNUM(&key->qP, in, x, y, inlen); |
|
61 INPUT_BIGNUM(&key->p, in, x, y, inlen); |
|
62 INPUT_BIGNUM(&key->q, in, x, y, inlen); |
|
63 } |
|
64 |
|
65 /* free up ram not required */ |
|
66 if (key->type != PK_PRIVATE_OPTIMIZED) { |
|
67 mp_clear_multi(&key->dQ, &key->dP, &key->pQ, &key->qP, &key->p, &key->q, NULL); |
|
68 } |
|
69 if (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED) { |
|
70 mp_clear(&key->d); |
|
71 } |
|
72 |
|
73 return CRYPT_OK; |
|
74 error: |
|
75 mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP, |
|
76 &key->pQ, &key->qP, &key->p, &key->q, NULL); |
|
77 return err; |
|
78 } |
|
79 |
|
80 #endif /* MRSA */ |
|
81 |