comparison src/pk/rsa/rsa_import.c @ 280:59400faa4b44 libtomcrypt-orig libtomcrypt-1.05

Re-import libtomcrypt 1.05 for cleaner propagating. From crypt-1.05.tar.bz2, SHA1 of 88250202bb51570dc64f7e8f1c943cda9479258f
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 12:58:00 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
-1:000000000000 280:59400faa4b44
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 #include "tomcrypt.h"
12
13 /**
14 @file rsa_import.c
15 Import a PKCS RSA key, Tom St Denis
16 */
17
18 #ifdef MRSA
19
20 /**
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
26 */
27 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key)
28 {
29 int err;
30 mp_int zero;
31
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(key != NULL);
34
35 /* init key */
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);
39 }
40
41 if ((err = der_decode_sequence_multi(in, inlen,
42 LTC_ASN1_INTEGER, 1UL, &key->N,
43 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
44 goto LBL_ERR;
45 }
46
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) {
60 goto LBL_ERR;
61 }
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;
66 goto LBL_ERR;
67 } else {
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) {
73 goto LBL_ERR;
74 }
75
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;
79 }
80 return CRYPT_OK;
81 LBL_ERR:
82 mp_clear_multi(&zero, &key->d, &key->e, &key->N, &key->dQ, &key->dP,
83 &key->qP, &key->p, &key->q, NULL);
84 return err;
85 }
86
87 #endif /* MRSA */
88
89
90 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_import.c,v $ */
91 /* $Revision: 1.10 $ */
92 /* $Date: 2005/06/03 18:48:28 $ */