Mercurial > dropbear
comparison rsa_exptmod.c @ 15:6362d3854bb4 libtomcrypt-orig
0.96 release of LibTomCrypt
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 15 Jun 2004 14:07:21 +0000 |
parents | 7faae8f46238 |
children | 5d99163f7e32 |
comparison
equal
deleted
inserted
replaced
3:7faae8f46238 | 15:6362d3854bb4 |
---|---|
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 int rsa_exptmod(const unsigned char *in, unsigned long inlen, | 17 int rsa_exptmod(const unsigned char *in, unsigned long inlen, |
18 unsigned char *out, unsigned long *outlen, int which, | 18 unsigned char *out, unsigned long *outlen, int which, |
19 prng_state *prng, int prng_idx, | |
19 rsa_key *key) | 20 rsa_key *key) |
20 { | 21 { |
21 mp_int tmp, tmpa, tmpb; | 22 mp_int tmp, tmpa, tmpb; |
22 unsigned long x; | 23 unsigned long x; |
23 int err; | 24 int err; |
24 | 25 |
25 _ARGCHK(in != NULL); | 26 _ARGCHK(in != NULL); |
26 _ARGCHK(out != NULL); | 27 _ARGCHK(out != NULL); |
27 _ARGCHK(outlen != NULL); | 28 _ARGCHK(outlen != NULL); |
28 _ARGCHK(key != NULL); | 29 _ARGCHK(key != NULL); |
30 | |
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { | |
32 return err; | |
33 } | |
29 | 34 |
30 if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) { | 35 if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) { |
31 return CRYPT_PK_NOT_PRIVATE; | 36 return CRYPT_PK_NOT_PRIVATE; |
32 } | 37 } |
33 | 38 |
47 } | 52 } |
48 | 53 |
49 /* are we using the private exponent and is the key optimized? */ | 54 /* are we using the private exponent and is the key optimized? */ |
50 if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) { | 55 if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) { |
51 /* tmpa = tmp^dP mod p */ | 56 /* tmpa = tmp^dP mod p */ |
52 if ((err = mp_exptmod(&tmp, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; } | 57 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; } |
53 | 58 |
54 /* tmpb = tmp^dQ mod q */ | 59 /* tmpb = tmp^dQ mod q */ |
55 if ((err = mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; } | 60 if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; } |
56 | 61 |
57 /* tmp = tmpa*qP + tmpb*pQ mod N */ | 62 /* tmp = tmpa*qP + tmpb*pQ mod N */ |
58 if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; } | 63 if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; } |
59 if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; } | 64 if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; } |
60 if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; } | 65 if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; } |
61 } else { | 66 } else { |
62 /* exptmod it */ | 67 /* exptmod it */ |
63 if ((err = mp_exptmod(&tmp, which==PK_PRIVATE?&key->d:&key->e, &key->N, &tmp)) != MP_OKAY) { goto error; } | 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 } | |
64 } | 73 } |
65 | 74 |
66 /* read it back */ | 75 /* read it back */ |
67 x = (unsigned long)mp_unsigned_bin_size(&tmp); | 76 x = (unsigned long)mp_unsigned_bin_size(&key->N); |
68 if (x > *outlen) { | 77 if (x > *outlen) { |
69 err = CRYPT_BUFFER_OVERFLOW; | 78 err = CRYPT_BUFFER_OVERFLOW; |
70 goto done; | 79 goto done; |
71 } | 80 } |
72 *outlen = x; | 81 *outlen = x; |
73 | 82 |
74 /* convert it */ | 83 /* convert it */ |
75 if ((err = mp_to_unsigned_bin(&tmp, out)) != MP_OKAY) { goto error; } | 84 zeromem(out, x); |
85 if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; } | |
76 | 86 |
77 /* clean up and return */ | 87 /* clean up and return */ |
78 err = CRYPT_OK; | 88 err = CRYPT_OK; |
79 goto done; | 89 goto done; |
80 error: | 90 error: |