3
|
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 /* RSA Code by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef MRSA |
|
16 |
15
|
17 int rsa_exptmod(const unsigned char *in, unsigned long inlen, |
|
18 unsigned char *out, unsigned long *outlen, int which, |
|
19 prng_state *prng, int prng_idx, |
3
|
20 rsa_key *key) |
|
21 { |
15
|
22 mp_int tmp, tmpa, tmpb; |
3
|
23 unsigned long x; |
15
|
24 int err; |
3
|
25 |
|
26 _ARGCHK(in != NULL); |
|
27 _ARGCHK(out != NULL); |
|
28 _ARGCHK(outlen != NULL); |
|
29 _ARGCHK(key != NULL); |
15
|
30 |
|
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
32 return err; |
|
33 } |
3
|
34 |
|
35 if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) { |
|
36 return CRYPT_PK_NOT_PRIVATE; |
|
37 } |
|
38 |
|
39 /* must be a private or public operation */ |
|
40 if (which != PK_PRIVATE && which != PK_PUBLIC) { |
|
41 return CRYPT_PK_INVALID_TYPE; |
|
42 } |
|
43 |
|
44 /* init and copy into tmp */ |
|
45 if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { goto error; } |
|
46 if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; } |
|
47 |
|
48 /* sanity check on the input */ |
|
49 if (mp_cmp(&key->N, &tmp) == MP_LT) { |
|
50 err = CRYPT_PK_INVALID_SIZE; |
|
51 goto done; |
|
52 } |
|
53 |
|
54 /* are we using the private exponent and is the key optimized? */ |
|
55 if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) { |
|
56 /* tmpa = tmp^dP mod p */ |
15
|
57 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; } |
3
|
58 |
|
59 /* tmpb = tmp^dQ mod q */ |
15
|
60 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; } |
3
|
61 |
|
62 /* tmp = tmpa*qP + tmpb*pQ mod N */ |
|
63 if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; } |
|
64 if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; } |
|
65 if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; } |
|
66 } else { |
|
67 /* exptmod it */ |
15
|
68 if (which == PK_PRIVATE) { |
|
69 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->d, &key->N, &tmp)) != MP_OKAY) { goto error; } |
|
70 } else { |
|
71 if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; } |
|
72 } |
3
|
73 } |
|
74 |
|
75 /* read it back */ |
15
|
76 x = (unsigned long)mp_unsigned_bin_size(&key->N); |
3
|
77 if (x > *outlen) { |
|
78 err = CRYPT_BUFFER_OVERFLOW; |
|
79 goto done; |
|
80 } |
|
81 *outlen = x; |
|
82 |
|
83 /* convert it */ |
15
|
84 zeromem(out, x); |
|
85 if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; } |
3
|
86 |
|
87 /* clean up and return */ |
|
88 err = CRYPT_OK; |
|
89 goto done; |
|
90 error: |
|
91 err = mpi_to_ltc_error(err); |
|
92 done: |
|
93 mp_clear_multi(&tmp, &tmpa, &tmpb, NULL); |
|
94 return err; |
|
95 } |
|
96 |
|
97 #endif |