annotate src/pk/rsa/rsa_import.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.org
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include "tomcrypt.h"
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 @file rsa_import.c
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 Import a PKCS RSA key, Tom St Denis
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #ifdef MRSA
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 Import an RSAPublicKey or RSAPrivateKey [two-prime only, defined in PKCS #1 v2.1]
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param in The packet to import from
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param inlen It's length (octets)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param key [out] Destination for newly imported key
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @return CRYPT_OK if successful, upon error allocated memory is freed
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 unsigned long x, y;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 int err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 LTC_ARGCHK(in != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 LTC_ARGCHK(key != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 /* init key */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 &key->p, &key->q, NULL)) != MP_OKAY) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return mpi_to_ltc_error(err);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 /* check the header */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 if (inlen < 4) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 return CRYPT_INVALID_PACKET;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* should be 0x30 0x8{1|2} LL LL */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if ((in[0] != 0x30) || ((in[1] != 0x81) && (in[1] != 0x82))) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 return CRYPT_INVALID_PACKET;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /* ok all the ASN.1 params are fine so far, let's move up */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 x = ((unsigned long)in[2]);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 y = 0;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 if ((in[1] & ~0x80) == 2) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 x = (x << 8) + ((unsigned long)in[3]) + 1;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 in += 1;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 y = 1;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 in += 3; /* advance input */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 x += 3; /* size of packet according to header */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 y += 3; /* used input */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 if (x != inlen) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 return CRYPT_INVALID_PACKET;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 /* decrement inlen by the header size */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 inlen -= y;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 /* read first number, it's either N or 0 [0 == private key] */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 x = inlen;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 if ((err = der_get_multi_integer(in, &x, &key->N, NULL)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 /* advance */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 inlen -= x;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 in += x;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if (mp_cmp_d(&key->N, 0) == MP_EQ) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 /* it's a private key */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if ((err = der_get_multi_integer(in, &inlen, &key->N, &key->e,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 &key->d, &key->p, &key->q, &key->dP,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 &key->dQ, &key->qP, NULL)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 key->type = PK_PRIVATE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 } else if (mp_cmp_d(&key->N, 1) == MP_EQ) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 /* we don't support multi-prime RSA */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 err = CRYPT_PK_INVALID_TYPE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 } else {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 /* it's a public key and we lack e */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 if ((err = der_get_multi_integer(in, &inlen, &key->e, NULL)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 /* free up some ram */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 mp_clear_multi(&key->p, &key->q, &key->qP, &key->dP, &key->dQ, NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 key->type = PK_PUBLIC;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 return CRYPT_OK;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 LBL_ERR:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 &key->qP, &key->p, &key->q, NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 #endif /* MRSA */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112