enable ltm, add key blinding, add remove tfm

This commit is contained in:
Love Hornquist Astrand
2010-08-15 15:06:58 -07:00
parent 44dfbeb596
commit 763a72b73a
143 changed files with 482 additions and 21199 deletions

View File

@@ -44,14 +44,30 @@
#include "tommath.h"
static int
random_num(mp_int *num, size_t len)
{
unsigned char *p;
len = (len + 7) / 8;
p = malloc(len);
if (p == NULL)
return 1;
if (RAND_bytes(p, len) != 1) {
free(p);
return 1;
}
mp_read_unsigned_bin(num, p, len);
free(p);
return 0;
}
static void
BN2mpz(mp_int *s, const BIGNUM *bn)
{
size_t len;
void *p;
mp_init(s);
len = BN_num_bytes(bn);
p = malloc(len);
BN_bn2bin(bn, p);
@@ -59,6 +75,34 @@ BN2mpz(mp_int *s, const BIGNUM *bn)
free(p);
}
static void
setup_blind(mp_int *n, mp_int *b, mp_int *bi)
{
random_num(b, mp_count_bits(n));
mp_mod(b, n, b);
mp_invmod(b, n, bi);
}
static void
blind(mp_int *in, mp_int *b, mp_int *e, mp_int *n)
{
mp_int t1;
mp_init(&t1);
/* in' = (in * b^e) mod n */
mp_exptmod(b, e, n, &t1);
mp_mul(&t1, in, in);
mp_mod(in, n, in);
mp_clear(&t1);
}
static void
unblind(mp_int *out, mp_int *bi, mp_int *n)
{
/* out' = (out * 1/b) mod n */
mp_mul(out, bi, out);
mp_mod(out, n, out);
}
static int
ltm_rsa_private_calculate(mp_int * in, mp_int * p, mp_int * q,
mp_int * dmp1, mp_int * dmq1, mp_int * iqmp,
@@ -87,7 +131,7 @@ ltm_rsa_private_calculate(mp_int * in, mp_int * p, mp_int * q,
mp_mul(&u, q, &u);
mp_add(&u, &vq, out);
mp_zero_multi(&vp, &vq, &u, NULL);
mp_clear_multi(&vp, &vq, &u, NULL);
return 0;
}
@@ -108,6 +152,8 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
if (padding != RSA_PKCS1_PADDING)
return -1;
mp_init_multi(&n, &e, &enc, &dec, NULL);
size = RSA_size(rsa);
if (size < RSA_PKCS1_PADDING_SIZE || size - RSA_PKCS1_PADDING_SIZE < flen)
@@ -118,7 +164,7 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
p = p0 = malloc(size - 1);
if (p0 == NULL) {
mp_zero_multi(&e, &n, NULL);
mp_clear_multi(&e, &n, NULL);
return -3;
}
@@ -126,7 +172,7 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
*p++ = 2;
if (RAND_bytes(p, padlen) != 1) {
mp_zero_multi(&e, &n, NULL);
mp_clear_multi(&e, &n, NULL);
free(p0);
return -4;
}
@@ -147,7 +193,7 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
res = mp_exptmod(&dec, &e, &n, &enc);
mp_zero_multi(&dec, &e, &n, NULL);
mp_clear_multi(&dec, &e, &n, NULL);
if (res != 0)
return -4;
@@ -159,7 +205,7 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
mp_to_unsigned_bin(&enc, to);
size = ssize;
}
mp_zero(&enc);
mp_clear(&enc);
return size;
}
@@ -179,28 +225,29 @@ ltm_rsa_public_decrypt(int flen, const unsigned char* from,
if (flen > RSA_size(rsa))
return -2;
mp_init_multi(&e, &n, &s, &us, NULL);
BN2mpz(&n, rsa->n);
BN2mpz(&e, rsa->e);
#if 0
/* Check that the exponent is larger then 3 */
if (mp_int_compare_value(&e, 3) <= 0) {
mp_zero_multi(&e, &n, NULL);
mp_clear_multi(&e, &n, NULL);
return -3;
}
#endif
mp_init_multi(&s, &us, NULL);
mp_read_unsigned_bin(&s, rk_UNCONST(from), flen);
if (mp_cmp(&s, &n) >= 0) {
mp_zero_multi(&e, &n, NULL);
mp_clear_multi(&e, &n, NULL);
return -4;
}
res = mp_exptmod(&s, &e, &n, &us);
mp_zero_multi(&s, &e, &n, NULL);
mp_clear_multi(&s, &e, &n, NULL);
if (res != 0)
return -5;
@@ -211,7 +258,7 @@ ltm_rsa_public_decrypt(int flen, const unsigned char* from,
assert(size <= RSA_size(rsa));
mp_to_unsigned_bin(&us, p);
mp_zero(&us);
mp_clear(&us);
/* head zero was skipped by mp_to_unsigned_bin */
if (*p == 0)
@@ -239,10 +286,15 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
int res;
int size;
mp_int in, out, n, e;
mp_int bi, b;
int blinding = (rsa->flags & RSA_FLAG_NO_BLINDING) == 0;
int do_unblind = 0;
if (padding != RSA_PKCS1_PADDING)
return -1;
mp_init_multi(&e, &n, &in, &out, &b, &bi, NULL);
size = RSA_size(rsa);
if (size < RSA_PKCS1_PADDING_SIZE || size - RSA_PKCS1_PADDING_SIZE < flen)
@@ -261,7 +313,6 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
BN2mpz(&n, rsa->n);
BN2mpz(&e, rsa->e);
mp_init_multi(&in, &out, NULL);
mp_read_unsigned_bin(&in, p0, size);
free(p0);
@@ -270,9 +321,17 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
goto out;
}
if (blinding) {
setup_blind(&n, &b, &bi);
blind(&in, &b, &e, &n);
do_unblind = 1;
}
if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
mp_int p, q, dmp1, dmq1, iqmp;
mp_init_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
BN2mpz(&p, rsa->p);
BN2mpz(&q, rsa->q);
BN2mpz(&dmp1, rsa->dmp1);
@@ -281,7 +340,7 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
res = ltm_rsa_private_calculate(&in, &p, &q, &dmp1, &dmq1, &iqmp, &out);
mp_zero_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
mp_clear_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
if (res != 0) {
size = -4;
@@ -292,13 +351,16 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
BN2mpz(&d, rsa->d);
res = mp_exptmod(&in, &d, &n, &out);
mp_zero(&d);
mp_clear(&d);
if (res != 0) {
size = -5;
goto out;
}
}
if (do_unblind)
unblind(&out, &bi, &n);
if (size > 0) {
size_t ssize;
ssize = mp_unsigned_bin_size(&out);
@@ -308,7 +370,7 @@ ltm_rsa_private_encrypt(int flen, const unsigned char* from,
}
out:
mp_zero_multi(&e, &n, &in, &out, NULL);
mp_clear_multi(&e, &n, &in, &out, &b, &bi, NULL);
return size;
}
@@ -320,7 +382,9 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
unsigned char *ptr;
int res;
size_t size;
mp_int in, out, n, e;
mp_int in, out, n, e, b, bi;
int blinding = (rsa->flags & RSA_FLAG_NO_BLINDING) == 0;
int do_unblind = 0;
if (padding != RSA_PKCS1_PADDING)
return -1;
@@ -329,7 +393,7 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
if (flen > size)
return -2;
mp_init_multi(&in, &out, NULL);
mp_init_multi(&in, &n, &e, &out, &bi, &b, NULL);
BN2mpz(&n, rsa->n);
BN2mpz(&e, rsa->e);
@@ -341,9 +405,17 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
goto out;
}
if (blinding) {
setup_blind(&n, &b, &bi);
blind(&in, &b, &e, &n);
do_unblind = 1;
}
if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
mp_int p, q, dmp1, dmq1, iqmp;
mp_init_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
BN2mpz(&p, rsa->p);
BN2mpz(&q, rsa->q);
BN2mpz(&dmp1, rsa->dmp1);
@@ -352,7 +424,7 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
res = ltm_rsa_private_calculate(&in, &p, &q, &dmp1, &dmq1, &iqmp, &out);
mp_zero_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
mp_clear_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);
if (res != 0) {
size = -3;
@@ -367,13 +439,16 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
BN2mpz(&d, rsa->d);
res = mp_exptmod(&in, &d, &n, &out);
mp_zero(&d);
mp_clear(&d);
if (res != 0) {
size = -5;
goto out;
}
}
if (do_unblind)
unblind(&out, &bi, &n);
ptr = to;
{
size_t ssize;
@@ -399,7 +474,7 @@ ltm_rsa_private_decrypt(int flen, const unsigned char* from,
memmove(to, ptr, size);
out:
mp_zero_multi(&e, &n, &in, &out, NULL);
mp_clear_multi(&e, &n, &in, &out, NULL);
return size;
}
@@ -423,24 +498,6 @@ mpz2BN(mp_int *s)
return bn;
}
static int
random_num(mp_int *num, size_t len)
{
unsigned char *p;
len = (len + 7) / 8;
p = malloc(len);
if (p == NULL)
return 1;
if (RAND_bytes(p, len) != 1) {
free(p);
return 1;
}
mp_read_unsigned_bin(num, p, len);
free(p);
return 0;
}
#define CHECK(f, v) if ((f) != (v)) { goto out; }
static int
@@ -526,7 +583,7 @@ ltm_rsa_generate_key(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
ret = 1;
out:
mp_zero_multi(&el, &p, &q, &n, &d, &dmp1,
mp_clear_multi(&el, &p, &q, &n, &d, &dmp1,
&dmq1, &iqmp, &t1, &t2, &t3, NULL);
return ret;