tomcrypt/dh_sys.c

452 lines
12 KiB
C
Raw Normal View History

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
2004-05-12 16:42:16 -04:00
* guarantee it works.
2004-01-25 12:40:34 -05:00
*
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
*/
2003-03-02 19:59:24 -05:00
int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
unsigned char *out, unsigned long *len,
prng_state *prng, int wprng, int hash,
dh_key *key)
{
2004-06-19 22:41:49 -04:00
unsigned char *pub_expt, *dh_shared, *skey;
2003-03-02 19:59:24 -05:00
dh_key pubkey;
unsigned long x, y, z, hashsize, pubkeysize;
2003-03-02 20:02:42 -05:00
int err;
2003-03-02 19:59:24 -05:00
_ARGCHK(inkey != NULL);
2004-02-20 15:03:32 -05:00
_ARGCHK(out != NULL);
_ARGCHK(len != NULL);
_ARGCHK(key != NULL);
2003-03-02 19:59:24 -05:00
/* check that wprng/hash are not invalid */
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
}
2003-03-02 20:02:42 -05:00
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
return err;
2003-03-02 19:59:24 -05:00
}
if (keylen > hash_descriptor[hash].hashsize) {
2003-12-24 13:59:57 -05:00
return CRYPT_INVALID_HASH;
2003-03-02 19:59:24 -05:00
}
2004-06-19 22:41:49 -04:00
/* allocate memory */
pub_expt = XMALLOC(DH_BUF_SIZE);
dh_shared = XMALLOC(DH_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (pub_expt == NULL || dh_shared == NULL || skey == NULL) {
if (pub_expt != NULL) {
XFREE(pub_expt);
}
if (dh_shared != NULL) {
XFREE(dh_shared);
}
if (skey != NULL) {
XFREE(skey);
}
return CRYPT_MEM;
}
2003-03-02 19:59:24 -05:00
/* make a random key and export the public copy */
2003-03-02 20:02:42 -05:00
if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
2004-06-19 22:41:49 -04:00
pubkeysize = DH_BUF_SIZE;
2003-03-02 20:02:42 -05:00
if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
2003-03-02 19:59:24 -05:00
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* now check if the out buffer is big enough */
2004-05-12 16:42:16 -04:00
if (*len < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + keylen)) {
2003-03-02 19:59:24 -05:00
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* make random key */
hashsize = hash_descriptor[hash].hashsize;
2004-06-19 22:41:49 -04:00
x = DH_BUF_SIZE;
2003-03-02 20:02:42 -05:00
if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) {
2003-03-02 19:59:24 -05:00
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
z = MAXBLOCKSIZE;
2003-03-02 20:02:42 -05:00
if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
2004-02-20 15:03:32 -05:00
/* store header */
packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_ENC_KEY);
2003-03-02 19:59:24 -05:00
/* output header */
y = PACKET_SIZE;
/* size of hash name and the name itself */
out[y++] = hash_descriptor[hash].ID;
/* length of DH pubkey and the key itself */
STORE32L(pubkeysize, out+y);
y += 4;
for (x = 0; x < pubkeysize; x++, y++) {
out[y] = pub_expt[x];
}
/* Store the encrypted key */
STORE32L(keylen, out+y);
y += 4;
for (x = 0; x < keylen; x++, y++) {
out[y] = skey[x] ^ inkey[x];
}
2004-02-20 15:03:32 -05:00
*len = y;
2003-03-02 19:59:24 -05:00
2004-06-19 22:41:49 -04:00
err = CRYPT_OK;
__ERR:
2003-03-02 19:59:24 -05:00
#ifdef CLEAN_STACK
/* clean up */
2004-06-19 22:41:49 -04:00
zeromem(pub_expt, DH_BUF_SIZE);
zeromem(dh_shared, DH_BUF_SIZE);
zeromem(skey, MAXBLOCKSIZE);
2003-03-02 19:59:24 -05:00
#endif
2004-06-19 22:41:49 -04:00
XFREE(skey);
XFREE(dh_shared);
XFREE(pub_expt);
2003-03-02 19:59:24 -05:00
2004-06-19 22:41:49 -04:00
return err;
2003-03-02 19:59:24 -05:00
}
2003-03-02 20:02:10 -05:00
int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen,
dh_key *key)
2003-03-02 19:59:24 -05:00
{
2004-06-19 22:41:49 -04:00
unsigned char *shared_secret, *skey;
2003-03-02 20:02:42 -05:00
unsigned long x, y, z,hashsize, keysize;
2004-02-20 15:03:32 -05:00
int hash, err;
2003-03-02 19:59:24 -05:00
dh_key pubkey;
2004-02-20 15:03:32 -05:00
_ARGCHK(in != NULL);
2003-03-02 19:59:24 -05:00
_ARGCHK(outkey != NULL);
_ARGCHK(keylen != NULL);
2004-02-20 15:03:32 -05:00
_ARGCHK(key != NULL);
2003-03-02 19:59:24 -05:00
/* right key type? */
if (key->type != PK_PRIVATE) {
return CRYPT_PK_NOT_PRIVATE;
}
2004-06-19 22:41:49 -04:00
/* allocate ram */
shared_secret = XMALLOC(DH_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (shared_secret == NULL || skey == NULL) {
if (shared_secret != NULL) {
XFREE(shared_secret);
}
if (skey != NULL) {
XFREE(skey);
}
return CRYPT_MEM;
}
2003-03-02 20:02:10 -05:00
/* check if initial header should fit */
if (inlen < PACKET_SIZE+1+4+4) {
2004-06-19 22:41:49 -04:00
err = CRYPT_INVALID_PACKET;
goto __ERR;
2003-03-02 20:02:10 -05:00
} else {
inlen -= PACKET_SIZE+1+4+4;
}
2003-03-02 19:59:24 -05:00
/* is header correct? */
2003-03-02 20:02:42 -05:00
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_ENC_KEY)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* now lets get the hash name */
y = PACKET_SIZE;
hash = find_hash_id(in[y++]);
if (hash == -1) {
2004-06-19 22:41:49 -04:00
err = CRYPT_INVALID_HASH;
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* common values */
hashsize = hash_descriptor[hash].hashsize;
/* get public key */
LOAD32L(x, in+y);
2003-03-02 20:02:10 -05:00
/* now check if the imported key will fit */
if (inlen < x) {
2004-06-19 22:41:49 -04:00
err = CRYPT_INVALID_PACKET;
goto __ERR;
2003-03-02 20:02:10 -05:00
} else {
inlen -= x;
}
2003-03-02 19:59:24 -05:00
y += 4;
2003-03-02 20:02:42 -05:00
if ((err = dh_import(in+y, x, &pubkey)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
y += x;
/* make shared key */
2004-06-19 22:41:49 -04:00
x = DH_BUF_SIZE;
2003-03-02 20:02:42 -05:00
if ((err = dh_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) {
2003-03-02 19:59:24 -05:00
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
dh_free(&pubkey);
2004-06-19 22:41:49 -04:00
z = MAXBLOCKSIZE;
2003-03-02 20:02:42 -05:00
if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* load in the encrypted key */
LOAD32L(keysize, in+y);
2003-03-02 20:02:10 -05:00
/* will the outkey fit as part of the input */
if (inlen < keysize) {
2004-06-19 22:41:49 -04:00
err = CRYPT_INVALID_PACKET;
goto __ERR;
2003-03-02 20:02:10 -05:00
} else {
inlen -= keysize;
}
2003-03-02 19:59:24 -05:00
if (keysize > *keylen) {
2004-02-20 15:03:32 -05:00
err = CRYPT_BUFFER_OVERFLOW;
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
y += 4;
*keylen = keysize;
for (x = 0; x < keysize; x++, y++) {
outkey[x] = skey[x] ^ in[y];
}
2004-02-20 15:03:32 -05:00
err = CRYPT_OK;
2004-06-19 22:41:49 -04:00
__ERR:
2003-03-02 19:59:24 -05:00
#ifdef CLEAN_STACK
2004-06-19 22:41:49 -04:00
zeromem(shared_secret, DH_BUF_SIZE);
zeromem(skey, MAXBLOCKSIZE);
2003-03-02 19:59:24 -05:00
#endif
2004-06-19 22:41:49 -04:00
XFREE(skey);
XFREE(shared_secret);
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
/* perform an ElGamal Signature of a hash
*
* The math works as follows. x is the private key, M is the message to sign
1. pick a random k
2. compute a = g^k mod p
3. compute b = (M - xa)/k mod p
4. Send (a,b)
Now to verify with y=g^x mod p, a and b
1. compute y^a * a^b = g^(xa) * g^(k*(M-xa)/k)
= g^(xa + (M - xa))
= g^M [all mod p]
2. Compare against g^M mod p [based on input hash].
3. If result of #2 == result of #1 then signature valid
*/
2003-03-02 19:59:24 -05:00
int dh_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, dh_key *key)
{
mp_int a, b, k, m, g, p, p1, tmp;
2004-06-19 22:41:49 -04:00
unsigned char *buf;
2003-03-02 19:59:24 -05:00
unsigned long x, y;
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
/* check parameters */
if (key->type != PK_PRIVATE) {
return CRYPT_PK_NOT_PRIVATE;
}
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
}
2003-03-02 20:01:00 -05:00
/* is the IDX valid ? */
2003-03-02 20:02:42 -05:00
if (is_valid_idx(key->idx) != 1) {
2003-03-02 20:01:00 -05:00
return CRYPT_PK_INVALID_TYPE;
}
2004-06-19 22:41:49 -04:00
/* allocate ram for buf */
buf = XMALLOC(520);
2003-03-02 19:59:24 -05:00
/* make up a random value k,
* since the order of the group is prime
* we need not check if gcd(k, r) is 1
*/
2003-03-02 20:02:42 -05:00
if (prng_descriptor[wprng].read(buf, sets[key->idx].size, prng) !=
(unsigned long)(sets[key->idx].size)) {
2004-06-19 22:41:49 -04:00
err = CRYPT_ERROR_READPRNG;
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* init bignums */
2003-12-24 13:59:57 -05:00
if ((err = mp_init_multi(&a, &b, &k, &m, &p, &g, &p1, &tmp, NULL)) != MP_OKAY) {
2004-06-19 22:41:49 -04:00
err = mpi_to_ltc_error(err);
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* load k and m */
2003-12-24 13:59:57 -05:00
if ((err = mp_read_unsigned_bin(&m, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; }
2003-03-02 20:02:42 -05:00
#ifdef FAST_PK
2003-12-24 13:59:57 -05:00
if ((err = mp_read_unsigned_bin(&k, buf, MIN(32,sets[key->idx].size))) != MP_OKAY) { goto error; }
2003-03-02 20:02:42 -05:00
#else
2003-12-24 13:59:57 -05:00
if ((err = mp_read_unsigned_bin(&k, buf, sets[key->idx].size)) != MP_OKAY) { goto error; }
2003-03-02 20:02:42 -05:00
#endif
2003-03-02 19:59:24 -05:00
/* load g, p and p1 */
2003-12-24 13:59:57 -05:00
if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; }
if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; }
if ((err = mp_sub_d(&p, 1, &p1)) != MP_OKAY) { goto error; }
if ((err = mp_div_2(&p1, &p1)) != MP_OKAY) { goto error; } /* p1 = (p-1)/2 */
2003-03-02 19:59:24 -05:00
/* now get a = g^k mod p */
2003-12-24 13:59:57 -05:00
if ((err = mp_exptmod(&g, &k, &p, &a)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* now find M = xa + kb mod p1 or just b = (M - xa)/k mod p1 */
2003-12-24 13:59:57 -05:00
if ((err = mp_invmod(&k, &p1, &k)) != MP_OKAY) { goto error; } /* k = 1/k mod p1 */
if ((err = mp_mulmod(&a, &key->x, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = xa */
if ((err = mp_submod(&m, &tmp, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = M - xa */
if ((err = mp_mulmod(&k, &tmp, &p1, &b)) != MP_OKAY) { goto error; } /* b = (M - xa)/k */
2004-02-20 15:03:32 -05:00
/* check for overflow */
if ((unsigned long)(PACKET_SIZE + 4 + 4 + mp_unsigned_bin_size(&a) + mp_unsigned_bin_size(&b)) > *outlen) {
err = CRYPT_BUFFER_OVERFLOW;
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-02-20 15:03:32 -05:00
}
2003-03-02 19:59:24 -05:00
/* store header */
y = PACKET_SIZE;
/* now store them both (a,b) */
2003-03-02 20:02:42 -05:00
x = (unsigned long)mp_unsigned_bin_size(&a);
2004-02-20 15:03:32 -05:00
STORE32L(x, out+y); y += 4;
if ((err = mp_to_unsigned_bin(&a, out+y)) != MP_OKAY) { goto error; }
2003-12-24 13:59:57 -05:00
y += x;
2003-03-02 19:59:24 -05:00
2003-03-02 20:02:42 -05:00
x = (unsigned long)mp_unsigned_bin_size(&b);
2004-02-20 15:03:32 -05:00
STORE32L(x, out+y); y += 4;
if ((err = mp_to_unsigned_bin(&b, out+y)) != MP_OKAY) { goto error; }
2003-12-24 13:59:57 -05:00
y += x;
2003-03-02 19:59:24 -05:00
/* check if size too big */
if (*outlen < y) {
2004-02-20 15:03:32 -05:00
err = CRYPT_BUFFER_OVERFLOW;
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
}
/* store header */
2004-02-20 15:03:32 -05:00
packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_SIGNED);
2003-03-02 19:59:24 -05:00
*outlen = y;
2004-02-20 15:03:32 -05:00
err = CRYPT_OK;
2004-06-19 22:41:49 -04:00
goto __ERR;
2003-03-02 19:59:24 -05:00
error:
2004-02-20 15:03:32 -05:00
err = mpi_to_ltc_error(err);
2004-06-19 22:41:49 -04:00
__ERR:
2003-03-02 19:59:24 -05:00
mp_clear_multi(&tmp, &p1, &g, &p, &m, &k, &b, &a, NULL);
2004-06-19 22:41:49 -04:00
XFREE(buf);
2004-02-20 15:03:32 -05:00
return err;
2003-03-02 19:59:24 -05:00
}
2004-05-12 16:42:16 -04:00
/* verify the signature in sig of the given hash */
2003-03-02 20:02:10 -05:00
int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long hashlen,
int *stat, dh_key *key)
2003-03-02 19:59:24 -05:00
{
mp_int a, b, p, g, m, tmp;
unsigned long x, y;
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(sig != NULL);
2003-03-02 19:59:24 -05:00
_ARGCHK(hash != NULL);
_ARGCHK(stat != NULL);
2004-02-20 15:03:32 -05:00
_ARGCHK(key != NULL);
2003-03-02 19:59:24 -05:00
/* default to invalid */
*stat = 0;
2003-03-02 20:02:10 -05:00
/* check initial input length */
if (siglen < PACKET_SIZE+4+4) {
return CRYPT_INVALID_PACKET;
2004-05-12 16:42:16 -04:00
}
2003-03-02 20:02:10 -05:00
2003-03-02 19:59:24 -05:00
/* header ok? */
2003-03-02 20:02:42 -05:00
if ((err = packet_valid_header((unsigned char *)sig, PACKET_SECT_DH, PACKET_SUB_SIGNED)) != CRYPT_OK) {
return err;
2003-03-02 19:59:24 -05:00
}
2003-03-02 20:02:10 -05:00
2003-03-02 19:59:24 -05:00
/* get hash out of packet */
y = PACKET_SIZE;
/* init all bignums */
2003-12-24 13:59:57 -05:00
if ((err = mp_init_multi(&a, &p, &b, &g, &m, &tmp, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err);
2003-03-02 19:59:24 -05:00
}
/* load a and b */
2004-05-12 16:42:16 -04:00
INPUT_BIGNUM(&a, sig, x, y, siglen);
INPUT_BIGNUM(&b, sig, x, y, siglen);
2003-03-02 19:59:24 -05:00
/* load p and g */
2004-05-12 16:42:16 -04:00
if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error1; }
if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error1; }
2003-03-02 19:59:24 -05:00
/* load m */
2004-05-12 16:42:16 -04:00
if ((err = mp_read_unsigned_bin(&m, (unsigned char *)hash, hashlen)) != MP_OKAY) { goto error1; }
2003-03-02 19:59:24 -05:00
/* find g^m mod p */
2004-05-12 16:42:16 -04:00
if ((err = mp_exptmod(&g, &m, &p, &m)) != MP_OKAY) { goto error1; } /* m = g^m mod p */
2003-03-02 19:59:24 -05:00
/* find y^a * a^b */
2004-05-12 16:42:16 -04:00
if ((err = mp_exptmod(&key->y, &a, &p, &tmp)) != MP_OKAY) { goto error1; } /* tmp = y^a mod p */
if ((err = mp_exptmod(&a, &b, &p, &a)) != MP_OKAY) { goto error1; } /* a = a^b mod p */
if ((err = mp_mulmod(&a, &tmp, &p, &a)) != MP_OKAY) { goto error1; } /* a = y^a * a^b mod p */
2003-03-02 19:59:24 -05:00
/* y^a * a^b == g^m ??? */
if (mp_cmp(&a, &m) == 0) {
*stat = 1;
}
/* clean up */
2004-02-20 15:03:32 -05:00
err = CRYPT_OK;
2003-03-02 19:59:24 -05:00
goto done;
2004-05-12 16:42:16 -04:00
error1:
2004-02-20 15:03:32 -05:00
err = mpi_to_ltc_error(err);
2004-05-12 16:42:16 -04:00
error:
2003-03-02 19:59:24 -05:00
done:
mp_clear_multi(&tmp, &m, &g, &p, &b, &a, NULL);
2004-02-20 15:03:32 -05:00
return err;
2003-03-02 19:59:24 -05:00
}