comparison rsa_exptmod.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
12 /* RSA Code by Tom St Denis */ 12 /* RSA Code by Tom St Denis */
13 #include "mycrypt.h" 13 #include "mycrypt.h"
14 14
15 #ifdef MRSA 15 #ifdef MRSA
16 16
17 /* compute an RSA modular exponentiation */
17 int rsa_exptmod(const unsigned char *in, unsigned long inlen, 18 int rsa_exptmod(const unsigned char *in, unsigned long inlen,
18 unsigned char *out, unsigned long *outlen, int which, 19 unsigned char *out, unsigned long *outlen, int which,
19 prng_state *prng, int prng_idx, 20 prng_state *prng, int prng_idx,
20 rsa_key *key) 21 rsa_key *key)
21 { 22 {
26 _ARGCHK(in != NULL); 27 _ARGCHK(in != NULL);
27 _ARGCHK(out != NULL); 28 _ARGCHK(out != NULL);
28 _ARGCHK(outlen != NULL); 29 _ARGCHK(outlen != NULL);
29 _ARGCHK(key != NULL); 30 _ARGCHK(key != NULL);
30 31
32 /* valid prng? */
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { 33 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
32 return err; 34 return err;
33 } 35 }
34 36
35 if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) { 37 /* is the key of the right type for the operation? */
38 if (which == PK_PRIVATE && (key->type != PK_PRIVATE)) {
36 return CRYPT_PK_NOT_PRIVATE; 39 return CRYPT_PK_NOT_PRIVATE;
37 } 40 }
38 41
39 /* must be a private or public operation */ 42 /* must be a private or public operation */
40 if (which != PK_PRIVATE && which != PK_PUBLIC) { 43 if (which != PK_PRIVATE && which != PK_PUBLIC) {
41 return CRYPT_PK_INVALID_TYPE; 44 return CRYPT_PK_INVALID_TYPE;
42 } 45 }
43 46
44 /* init and copy into tmp */ 47 /* init and copy into tmp */
45 if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { goto error; } 48 if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); }
46 if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; } 49 if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
47 50
48 /* sanity check on the input */ 51 /* sanity check on the input */
49 if (mp_cmp(&key->N, &tmp) == MP_LT) { 52 if (mp_cmp(&key->N, &tmp) == MP_LT) {
50 err = CRYPT_PK_INVALID_SIZE; 53 err = CRYPT_PK_INVALID_SIZE;
51 goto done; 54 goto done;
52 } 55 }
53 56
54 /* are we using the private exponent and is the key optimized? */ 57 /* are we using the private exponent and is the key optimized? */
55 if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) { 58 if (which == PK_PRIVATE) {
56 /* tmpa = tmp^dP mod p */ 59 /* tmpa = tmp^dP mod p */
57 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; } 60 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; }
58 61
59 /* tmpb = tmp^dQ mod q */ 62 /* tmpb = tmp^dQ mod q */
60 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; } 63 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; }
61 64
62 /* tmp = tmpa*qP + tmpb*pQ mod N */ 65 /* tmp = (tmpa - tmpb) * qInv (mod p) */
63 if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; } 66 if ((err = mp_sub(&tmpa, &tmpb, &tmp)) != MP_OKAY) { goto error; }
64 if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; } 67 if ((err = mp_mulmod(&tmp, &key->qP, &key->p, &tmp)) != MP_OKAY) { goto error; }
65 if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; } 68
69 /* tmp = tmpb + q * tmp */
70 if ((err = mp_mul(&tmp, &key->q, &tmp)) != MP_OKAY) { goto error; }
71 if ((err = mp_add(&tmp, &tmpb, &tmp)) != MP_OKAY) { goto error; }
66 } else { 72 } else {
67 /* exptmod it */ 73 /* exptmod it */
68 if (which == PK_PRIVATE) { 74 if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
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 }
73 } 75 }
74 76
75 /* read it back */ 77 /* read it back */
76 x = (unsigned long)mp_unsigned_bin_size(&key->N); 78 x = (unsigned long)mp_unsigned_bin_size(&key->N);
77 if (x > *outlen) { 79 if (x > *outlen) {