2004-01-25 12:40:34 -05:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* gurantee it works.
|
|
|
|
*
|
|
|
|
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* RSA Code by Tom St Denis */
|
2003-03-02 19:59:24 -05:00
|
|
|
#include "mycrypt.h"
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
/* Min and Max RSA key sizes (in bits) */
|
|
|
|
#define MIN_RSA_SIZE 1024
|
|
|
|
#define MAX_RSA_SIZE 4096
|
|
|
|
|
|
|
|
/* Stack required for temps (plus padding) */
|
|
|
|
#define RSA_STACK (8 + (MAX_RSA_SIZE/8))
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
#ifdef MRSA
|
|
|
|
|
|
|
|
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key)
|
|
|
|
{
|
|
|
|
mp_int p, q, tmp1, tmp2, tmp3;
|
2004-02-20 15:03:32 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((size < (MIN_RSA_SIZE/8)) || (size > (MAX_RSA_SIZE/8))) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((e < 3) || ((e & 1) == 0)) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_INVALID_ARG;
|
|
|
|
}
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) {
|
|
|
|
return mpi_to_ltc_error(err);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make primes p and q (optimization provided by Wayne Scott) */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_set_int(&tmp3, e)) != MP_OKAY) { goto error; } /* tmp3 = e */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* make prime "p" */
|
|
|
|
do {
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = rand_prime(&p, size/2, prng, wprng)) != CRYPT_OK) { goto done; }
|
|
|
|
if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = p-1 */
|
|
|
|
if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(p-1, e) */
|
|
|
|
} while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides p-1 */
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* make prime "q" */
|
|
|
|
do {
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = rand_prime(&q, size/2, prng, wprng)) != CRYPT_OK) { goto done; }
|
|
|
|
if ((err = mp_sub_d(&q, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = q-1 */
|
|
|
|
if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(q-1, e) */
|
|
|
|
} while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides q-1 */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* tmp1 = lcm(p-1, q-1) */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_sub_d(&p, 1, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = p-1 */
|
|
|
|
/* tmp1 = q-1 (previous do/while loop) */
|
|
|
|
if ((err = mp_lcm(&tmp1, &tmp2, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = lcm(p-1, q-1) */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* make key */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP,
|
|
|
|
&key->qP, &key->pQ, &key->p, &key->q, NULL)) != MP_OKAY) {
|
2003-03-02 19:59:24 -05:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_set_int(&key->e, e)) != MP_OKAY) { goto error2; } /* key->e = e */
|
|
|
|
if ((err = mp_invmod(&key->e, &tmp1, &key->d)) != MP_OKAY) { goto error2; } /* key->d = 1/e mod lcm(p-1,q-1) */
|
|
|
|
if ((err = mp_mul(&p, &q, &key->N)) != MP_OKAY) { goto error2; } /* key->N = pq */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* optimize for CRT now */
|
|
|
|
/* find d mod q-1 and d mod p-1 */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error2; } /* tmp1 = q-1 */
|
|
|
|
if ((err = mp_sub_d(&q, 1, &tmp2)) != MP_OKAY) { goto error2; } /* tmp2 = p-1 */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_mod(&key->d, &tmp1, &key->dP)) != MP_OKAY) { goto error2; } /* dP = d mod p-1 */
|
|
|
|
if ((err = mp_mod(&key->d, &tmp2, &key->dQ)) != MP_OKAY) { goto error2; } /* dQ = d mod q-1 */
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_invmod(&q, &p, &key->qP)) != MP_OKAY) { goto error2; } /* qP = 1/q mod p */
|
|
|
|
if ((err = mp_mulmod(&key->qP, &q, &key->N, &key->qP)) != MP_OKAY) { goto error2; } /* qP = q * (1/q mod p) mod N */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_invmod(&p, &q, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = 1/p mod q */
|
|
|
|
if ((err = mp_mulmod(&key->pQ, &p, &key->N, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = p * (1/p mod q) mod N */
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_copy(&p, &key->p)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_copy(&q, &key->q)) != MP_OKAY) { goto error2; }
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 20:02:10 -05:00
|
|
|
/* shrink ram required */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_shrink(&key->e)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->d)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->N)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->dQ)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->dP)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->qP)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->pQ)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error2; }
|
|
|
|
if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error2; }
|
|
|
|
|
|
|
|
err = CRYPT_OK;
|
2003-03-02 19:59:24 -05:00
|
|
|
key->type = PK_PRIVATE_OPTIMIZED;
|
|
|
|
goto done;
|
|
|
|
error2:
|
2003-06-01 14:55:11 -04:00
|
|
|
mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,
|
2003-03-02 19:59:24 -05:00
|
|
|
&key->qP, &key->pQ, &key->p, &key->q, NULL);
|
|
|
|
error:
|
2004-02-20 15:03:32 -05:00
|
|
|
err = mpi_to_ltc_error(err);
|
2003-03-02 19:59:24 -05:00
|
|
|
done:
|
|
|
|
mp_clear_multi(&tmp3, &tmp2, &tmp1, &p, &q, NULL);
|
2004-02-20 15:03:32 -05:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void rsa_free(rsa_key *key)
|
|
|
|
{
|
|
|
|
_ARGCHK(key != NULL);
|
2003-06-01 14:55:11 -04:00
|
|
|
mp_clear_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP,
|
2003-03-02 19:59:24 -05:00
|
|
|
&key->qP, &key->pQ, &key->p, &key->q, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rsa_exptmod(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen, int which,
|
|
|
|
rsa_key *key)
|
|
|
|
{
|
|
|
|
mp_int tmp, tmpa, tmpb;
|
|
|
|
unsigned long x;
|
2004-02-20 15:03:32 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(key != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) {
|
|
|
|
return CRYPT_PK_NOT_PRIVATE;
|
|
|
|
}
|
|
|
|
|
2003-12-24 13:59:57 -05:00
|
|
|
/* must be a private or public operation */
|
|
|
|
if (which != PK_PRIVATE && which != PK_PUBLIC) {
|
|
|
|
return CRYPT_PK_INVALID_TYPE;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* init and copy into tmp */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { goto error; }
|
|
|
|
if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* sanity check on the input */
|
|
|
|
if (mp_cmp(&key->N, &tmp) == MP_LT) {
|
2004-02-20 15:03:32 -05:00
|
|
|
err = CRYPT_PK_INVALID_SIZE;
|
2003-03-02 19:59:24 -05:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* are we using the private exponent and is the key optimized? */
|
|
|
|
if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) {
|
|
|
|
/* tmpa = tmp^dP mod p */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_exptmod(&tmp, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* tmpb = tmp^dQ mod q */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* tmp = tmpa*qP + tmpb*pQ mod N */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; }
|
|
|
|
if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; }
|
|
|
|
if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
} else {
|
|
|
|
/* exptmod it */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_exptmod(&tmp, which==PK_PRIVATE?&key->d:&key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read it back */
|
2003-03-02 20:02:42 -05:00
|
|
|
x = (unsigned long)mp_unsigned_bin_size(&tmp);
|
2003-03-02 19:59:24 -05:00
|
|
|
if (x > *outlen) {
|
2004-02-20 15:03:32 -05:00
|
|
|
err = CRYPT_BUFFER_OVERFLOW;
|
2003-03-02 19:59:24 -05:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*outlen = x;
|
|
|
|
|
|
|
|
/* convert it */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_to_unsigned_bin(&tmp, out)) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* clean up and return */
|
2004-02-20 15:03:32 -05:00
|
|
|
err = CRYPT_OK;
|
2003-03-02 19:59:24 -05:00
|
|
|
goto done;
|
|
|
|
error:
|
2004-02-20 15:03:32 -05:00
|
|
|
err = mpi_to_ltc_error(err);
|
2003-03-02 19:59:24 -05:00
|
|
|
done:
|
|
|
|
mp_clear_multi(&tmp, &tmpa, &tmpb, NULL);
|
2004-02-20 15:03:32 -05:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
int rsa_signpad(const unsigned char *in, unsigned long inlen,
|
2003-03-02 19:59:24 -05:00
|
|
|
unsigned char *out, unsigned long *outlen)
|
|
|
|
{
|
|
|
|
unsigned long x, y;
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
|
|
|
|
if (*outlen < (3 * inlen)) {
|
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check inlen */
|
2003-06-01 14:55:11 -04:00
|
|
|
if (inlen > 512) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_PK_INVALID_SIZE;
|
|
|
|
}
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
for (y = x = 0; x < inlen; x++)
|
2003-03-02 20:02:42 -05:00
|
|
|
out[y++] = (unsigned char)0xFF;
|
2003-03-02 19:59:24 -05:00
|
|
|
for (x = 0; x < inlen; x++)
|
|
|
|
out[y++] = in[x];
|
|
|
|
for (x = 0; x < inlen; x++)
|
2003-03-02 20:02:42 -05:00
|
|
|
out[y++] = (unsigned char)0xFF;
|
2003-03-02 19:59:24 -05:00
|
|
|
*outlen = 3 * inlen;
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
int rsa_pad(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
2003-03-02 19:59:24 -05:00
|
|
|
int wprng, prng_state *prng)
|
|
|
|
{
|
2004-02-20 15:03:32 -05:00
|
|
|
unsigned char buf[3*(MAX_RSA_SIZE/8)];
|
2003-03-02 19:59:24 -05:00
|
|
|
unsigned long x;
|
2003-03-02 20:02:42 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
|
|
|
|
/* is output big enough? */
|
2003-06-01 14:55:11 -04:00
|
|
|
if (*outlen < (3 * inlen)) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get random padding required */
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
2003-06-01 14:55:11 -04:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check inlen */
|
2004-02-20 15:03:32 -05:00
|
|
|
if (inlen > (MAX_RSA_SIZE/8)) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_PK_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prng_descriptor[wprng].read(buf, inlen*2-2, prng) != (inlen*2 - 2)) {
|
|
|
|
return CRYPT_ERROR_READPRNG;
|
|
|
|
}
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
/* pad it like a sandwhich
|
2003-03-02 19:59:24 -05:00
|
|
|
*
|
|
|
|
* Looks like 0xFF R1 M R2 0xFF
|
2003-06-01 14:55:11 -04:00
|
|
|
*
|
|
|
|
* Where R1/R2 are random and exactly equal to the length of M minus one byte.
|
2003-03-02 19:59:24 -05:00
|
|
|
*/
|
2003-06-01 14:55:11 -04:00
|
|
|
for (x = 0; x < inlen-1; x++) {
|
|
|
|
out[x+1] = buf[x];
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < inlen; x++) {
|
|
|
|
out[x+inlen] = in[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < inlen-1; x++) {
|
|
|
|
out[x+inlen+inlen] = buf[x+inlen-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* last and first bytes are 0xFF */
|
2003-03-02 20:02:42 -05:00
|
|
|
out[0] = out[inlen+inlen+inlen-1] = (unsigned char)0xFF;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* clear up and return */
|
|
|
|
#ifdef CLEAN_STACK
|
|
|
|
zeromem(buf, sizeof(buf));
|
|
|
|
#endif
|
|
|
|
*outlen = inlen*3;
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
int rsa_signdepad(const unsigned char *in, unsigned long inlen,
|
2004-02-20 15:03:32 -05:00
|
|
|
unsigned char *out, unsigned long *outlen)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
if (*outlen < inlen/3) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check padding bytes */
|
|
|
|
for (x = 0; x < inlen/3; x++) {
|
2003-03-02 20:02:42 -05:00
|
|
|
if (in[x] != (unsigned char)0xFF || in[x+(inlen/3)+(inlen/3)] != (unsigned char)0xFF) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_INVALID_PACKET;
|
|
|
|
}
|
|
|
|
}
|
2003-12-24 13:59:57 -05:00
|
|
|
for (x = 0; x < inlen/3; x++) {
|
2003-03-02 19:59:24 -05:00
|
|
|
out[x] = in[x+(inlen/3)];
|
2003-12-24 13:59:57 -05:00
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
*outlen = inlen/3;
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
int rsa_depad(const unsigned char *in, unsigned long inlen,
|
2003-03-02 19:59:24 -05:00
|
|
|
unsigned char *out, unsigned long *outlen)
|
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
if (*outlen < inlen/3) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
2003-12-24 13:59:57 -05:00
|
|
|
for (x = 0; x < inlen/3; x++) {
|
2003-03-02 19:59:24 -05:00
|
|
|
out[x] = in[x+(inlen/3)];
|
2003-12-24 13:59:57 -05:00
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
*outlen = inlen/3;
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key)
|
|
|
|
{
|
2004-01-25 12:40:34 -05:00
|
|
|
unsigned long y, z;
|
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(out != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(outlen != NULL);
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
|
|
|
/* can we store the static header? */
|
|
|
|
if (*outlen < (PACKET_SIZE + 1)) {
|
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* type valid? */
|
2003-06-01 14:55:11 -04:00
|
|
|
if (!(key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) &&
|
|
|
|
(type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED)) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_PK_INVALID_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start at offset y=PACKET_SIZE */
|
|
|
|
y = PACKET_SIZE;
|
|
|
|
|
|
|
|
/* output key type */
|
2004-02-20 15:03:32 -05:00
|
|
|
out[y++] = type;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* output modulus */
|
2004-02-20 15:03:32 -05:00
|
|
|
OUTPUT_BIGNUM(&key->N, out, y, z);
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* output public key */
|
2004-02-20 15:03:32 -05:00
|
|
|
OUTPUT_BIGNUM(&key->e, out, y, z);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
if (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED) {
|
2004-02-20 15:03:32 -05:00
|
|
|
OUTPUT_BIGNUM(&key->d, out, y, z);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == PK_PRIVATE_OPTIMIZED) {
|
2004-02-20 15:03:32 -05:00
|
|
|
OUTPUT_BIGNUM(&key->dQ, out, y, z);
|
|
|
|
OUTPUT_BIGNUM(&key->dP, out, y, z);
|
|
|
|
OUTPUT_BIGNUM(&key->pQ, out, y, z);
|
|
|
|
OUTPUT_BIGNUM(&key->qP, out, y, z);
|
|
|
|
OUTPUT_BIGNUM(&key->p, out, y, z);
|
|
|
|
OUTPUT_BIGNUM(&key->q, out, y, z);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* store packet header */
|
2004-02-20 15:03:32 -05:00
|
|
|
packet_store_header(out, PACKET_SECT_RSA, PACKET_SUB_KEY);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* copy to the user buffer */
|
|
|
|
*outlen = y;
|
|
|
|
|
|
|
|
/* clear stack and return */
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:01:40 -05:00
|
|
|
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
|
|
|
unsigned long x, y;
|
2003-03-02 20:02:42 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-02-20 15:03:32 -05:00
|
|
|
_ARGCHK(in != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
/* check length */
|
2004-02-20 15:03:32 -05:00
|
|
|
if (inlen < (1+PACKET_SIZE)) {
|
2003-03-02 20:01:40 -05:00
|
|
|
return CRYPT_INVALID_PACKET;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2003-06-01 14:55:11 -04:00
|
|
|
/* test packet header */
|
|
|
|
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_RSA, PACKET_SUB_KEY)) != CRYPT_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* init key */
|
2004-02-20 15:03:32 -05:00
|
|
|
if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP,
|
|
|
|
&key->pQ, &key->p, &key->q, NULL)) != MP_OKAY) {
|
|
|
|
return mpi_to_ltc_error(err);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get key type */
|
|
|
|
y = PACKET_SIZE;
|
2003-03-02 20:02:42 -05:00
|
|
|
key->type = (int)in[y++];
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* load the modulus */
|
|
|
|
INPUT_BIGNUM(&key->N, in, x, y);
|
|
|
|
|
|
|
|
/* load public exponent */
|
|
|
|
INPUT_BIGNUM(&key->e, in, x, y);
|
|
|
|
|
|
|
|
/* get private exponent */
|
|
|
|
if (key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) {
|
|
|
|
INPUT_BIGNUM(&key->d, in, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get CRT private data if required */
|
|
|
|
if (key->type == PK_PRIVATE_OPTIMIZED) {
|
|
|
|
INPUT_BIGNUM(&key->dQ, in, x, y);
|
|
|
|
INPUT_BIGNUM(&key->dP, in, x, y);
|
|
|
|
INPUT_BIGNUM(&key->pQ, in, x, y);
|
|
|
|
INPUT_BIGNUM(&key->qP, in, x, y);
|
|
|
|
INPUT_BIGNUM(&key->p, in, x, y);
|
|
|
|
INPUT_BIGNUM(&key->q, in, x, y);
|
|
|
|
}
|
2003-06-01 14:55:11 -04:00
|
|
|
|
2003-03-02 20:02:10 -05:00
|
|
|
/* free up ram not required */
|
|
|
|
if (key->type != PK_PRIVATE_OPTIMIZED) {
|
|
|
|
mp_clear_multi(&key->dQ, &key->dP, &key->pQ, &key->qP, &key->p, &key->q, NULL);
|
|
|
|
}
|
|
|
|
if (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED) {
|
|
|
|
mp_clear(&key->d);
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
return CRYPT_OK;
|
2004-01-25 12:40:34 -05:00
|
|
|
error:
|
2003-06-01 14:55:11 -04:00
|
|
|
mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,
|
2003-03-02 19:59:24 -05:00
|
|
|
&key->pQ, &key->qP, &key->p, &key->q, NULL);
|
2003-03-02 20:02:42 -05:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "rsa_sys.c"
|
|
|
|
|
|
|
|
#endif /* RSA */
|
|
|
|
|
|
|
|
|