1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
15
Import a PKCS RSA key, Tom St Denis
21
Import an RSAPublicKey or RSAPrivateKey [two-prime only, only support >= 1024-bit keys, defined in PKCS #1 v2.1]
22
@param in The packet to import from
23
@param inlen It's length (octets)
24
@param key [out] Destination for newly imported key
25
@return CRYPT_OK if successful, upon error allocated memory is freed
27
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key)
32
LTC_ARGCHK(in != NULL);
33
LTC_ARGCHK(key != NULL);
36
if ((err = mp_init_multi(&zero, &key->e, &key->d, &key->N, &key->dQ,
37
&key->dP, &key->qP, &key->p, &key->q, NULL)) != MP_OKAY) {
38
return mpi_to_ltc_error(err);
41
if ((err = der_decode_sequence_multi(in, inlen,
42
LTC_ASN1_INTEGER, 1UL, &key->N,
43
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
47
if (mp_cmp_d(&key->N, 0) == MP_EQ) {
48
/* it's a private key */
49
if ((err = der_decode_sequence_multi(in, inlen,
50
LTC_ASN1_INTEGER, 1UL, &zero,
51
LTC_ASN1_INTEGER, 1UL, &key->N,
52
LTC_ASN1_INTEGER, 1UL, &key->e,
53
LTC_ASN1_INTEGER, 1UL, &key->d,
54
LTC_ASN1_INTEGER, 1UL, &key->p,
55
LTC_ASN1_INTEGER, 1UL, &key->q,
56
LTC_ASN1_INTEGER, 1UL, &key->dP,
57
LTC_ASN1_INTEGER, 1UL, &key->dQ,
58
LTC_ASN1_INTEGER, 1UL, &key->qP,
59
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
62
key->type = PK_PRIVATE;
63
} else if (mp_cmp_d(&key->N, 1) == MP_EQ) {
64
/* we don't support multi-prime RSA */
65
err = CRYPT_PK_INVALID_TYPE;
68
/* it's a public key and we lack e */
69
if ((err = der_decode_sequence_multi(in, inlen,
70
LTC_ASN1_INTEGER, 1UL, &key->N,
71
LTC_ASN1_INTEGER, 1UL, &key->e,
72
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
76
/* free up some ram */
77
mp_clear_multi(&key->p, &key->q, &key->qP, &key->dP, &key->dQ, NULL);
78
key->type = PK_PUBLIC;
82
mp_clear_multi(&zero, &key->d, &key->e, &key->N, &key->dQ, &key->dP,
83
&key->qP, &key->p, &key->q, NULL);
90
/* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_import.c,v $ */
91
/* $Revision: 1.10 $ */
92
/* $Date: 2005/06/03 18:48:28 $ */