tomcrypt/dh.c

492 lines
14 KiB
C
Raw Normal View History

2003-03-02 19:59:24 -05:00
#include "mycrypt.h"
#ifdef MDH
2003-03-02 20:01:00 -05:00
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
2003-03-02 19:59:24 -05:00
static const struct {
int size;
char *name, *base, *prime;
} sets[] = {
#ifdef DH768
{
96,
"DH-768",
2003-09-07 21:06:11 -04:00
"4",
"F///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"//////m3wvV"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH1024
{
128,
"DH-1024",
2003-09-07 21:06:11 -04:00
"4",
"F///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////m3C47"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH1280
{
160,
"DH-1280",
2003-09-07 21:06:11 -04:00
"4",
"F///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"//////////////////////////////m4kSN"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH1536
{
192,
"DH-1536",
2003-09-07 21:06:11 -04:00
"4",
"F///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////m5uqd"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH1792
{
224,
"DH-1792",
2003-09-07 21:06:11 -04:00
"4",
"F///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"//////////////////////////////////////////////////////mT/sd"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH2048
{
256,
"DH-2048",
2003-09-07 21:06:11 -04:00
"4",
"3///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"/////////////////////////////////////////m8MPh"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH2560
{
320,
"DH-2560",
2003-09-07 21:06:11 -04:00
"4",
"3///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"/////mKFpF"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH3072
{
384,
"DH-3072",
2003-09-07 21:06:11 -04:00
"4",
"3///////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"/////////////////////////////m32nN"
2003-03-02 19:59:24 -05:00
},
#endif
#ifdef DH4096
{
512,
2003-06-01 14:55:11 -04:00
"DH-4096",
2003-09-07 21:06:11 -04:00
"4",
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"////////////////////////////////////////////////////////////"
"/////////////////////m8pOF"
2003-03-02 19:59:24 -05:00
},
#endif
2003-06-01 14:55:11 -04:00
{
2003-03-02 19:59:24 -05:00
0,
NULL,
NULL,
NULL
}
};
static int is_valid_idx(int n)
{
int x;
for (x = 0; sets[x].size; x++);
if ((n < 0) || (n >= x)) {
return 0;
}
return 1;
}
int dh_test(void)
{
mp_int p, g, tmp;
int x, res, primality;
2003-12-24 13:59:57 -05:00
if ((res = mp_init_multi(&p, &g, &tmp, NULL)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
2003-03-02 20:02:42 -05:00
for (x = 0; sets[x].size != 0; x++) {
2003-03-02 19:59:24 -05:00
#if 0
printf("dh_test():testing size %d-bits\n", sets[x].size * 8);
#endif
2003-12-24 13:59:57 -05:00
if ((res = mp_read_radix(&g,(char *)sets[x].base, 64)) != MP_OKAY) { goto error; }
if ((res = mp_read_radix(&p,(char *)sets[x].prime, 64)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* ensure p is prime */
2003-12-24 13:59:57 -05:00
if ((res = is_prime(&p, &primality)) != CRYPT_OK) { goto done; }
2003-06-01 14:55:11 -04:00
if (primality == 0) {
2003-03-02 19:59:24 -05:00
res = CRYPT_FAIL_TESTVECTOR;
goto done;
}
2003-12-24 13:59:57 -05:00
if ((res = mp_sub_d(&p, 1, &tmp)) != MP_OKAY) { goto error; }
if ((res = mp_div_2(&tmp, &tmp)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* ensure (p-1)/2 is prime */
2003-12-24 13:59:57 -05:00
if ((res = is_prime(&tmp, &primality)) != CRYPT_OK) { goto done; }
2003-03-02 19:59:24 -05:00
if (primality == 0) {
res = CRYPT_FAIL_TESTVECTOR;
goto done;
}
/* now see if g^((p-1)/2) mod p is in fact 1 */
2003-12-24 13:59:57 -05:00
if ((res = mp_exptmod(&g, &tmp, &p, &tmp)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
if (mp_cmp_d(&tmp, 1)) {
res = CRYPT_FAIL_TESTVECTOR;
goto done;
}
}
res = CRYPT_OK;
goto done;
error:
2003-12-24 13:59:57 -05:00
res = mpi_to_ltc_error(res);
2003-03-02 19:59:24 -05:00
done:
mp_clear_multi(&tmp, &g, &p, NULL);
return res;
}
void dh_sizes(int *low, int *high)
{
int x;
_ARGCHK(low != NULL);
_ARGCHK(high != NULL);
*low = INT_MAX;
*high = 0;
2003-03-02 20:02:42 -05:00
for (x = 0; sets[x].size != 0; x++) {
2003-03-02 19:59:24 -05:00
if (*low > sets[x].size) *low = sets[x].size;
if (*high < sets[x].size) *high = sets[x].size;
}
}
int dh_get_size(dh_key *key)
{
_ARGCHK(key != NULL);
2003-03-12 21:12:16 -05:00
if (is_valid_idx(key->idx) == 1) {
2003-03-02 19:59:24 -05:00
return sets[key->idx].size;
2003-03-12 21:12:16 -05:00
} else {
2003-03-02 19:59:24 -05:00
return INT_MAX; /* large value that would cause dh_make_key() to fail */
2003-03-12 21:12:16 -05:00
}
2003-03-02 19:59:24 -05:00
}
2003-06-01 14:55:11 -04:00
2003-03-02 19:59:24 -05:00
int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key)
{
2003-03-02 20:02:42 -05:00
unsigned char buf[512];
2003-03-02 19:59:24 -05:00
unsigned long x;
mp_int p, g;
2003-03-02 20:02:42 -05:00
int res, err;
2003-03-02 19:59:24 -05:00
_ARGCHK(key != NULL);
/* good prng? */
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
}
/* find key size */
2003-03-02 20:02:42 -05:00
for (x = 0; (keysize > sets[x].size) && (sets[x].size != 0); x++);
2003-03-02 19:59:24 -05:00
#ifdef FAST_PK
keysize = MIN(sets[x].size, 32);
2003-06-01 14:55:11 -04:00
#else
2003-03-02 19:59:24 -05:00
keysize = sets[x].size;
#endif
if (sets[x].size == 0) {
return CRYPT_INVALID_KEYSIZE;
}
key->idx = x;
/* make up random string */
2003-03-02 20:02:42 -05:00
if (prng_descriptor[wprng].read(buf, keysize, prng) != (unsigned long)keysize) {
2003-03-02 19:59:24 -05:00
return CRYPT_ERROR_READPRNG;
}
/* init parameters */
2003-12-24 13:59:57 -05:00
if ((res = mp_init_multi(&g, &p, &key->x, &key->y, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(res);
2003-03-02 19:59:24 -05:00
}
2003-12-24 13:59:57 -05:00
if ((res = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; }
if ((res = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* load the x value */
2003-12-24 13:59:57 -05:00
if ((res = mp_read_unsigned_bin(&key->x, buf, keysize)) != MP_OKAY) { goto error; }
if ((res = mp_exptmod(&g, &key->x, &p, &key->y)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
key->type = PK_PRIVATE;
2003-06-01 14:55:11 -04:00
2003-12-24 13:59:57 -05:00
if ((res = mp_shrink(&key->x)) != MP_OKAY) { goto error; }
if ((res = mp_shrink(&key->y)) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* free up ram */
res = CRYPT_OK;
goto done2;
2003-03-02 20:02:10 -05:00
error:
2003-12-24 13:59:57 -05:00
res = mpi_to_ltc_error(res);
2003-03-02 19:59:24 -05:00
mp_clear_multi(&key->x, &key->y, NULL);
done2:
mp_clear_multi(&p, &g, NULL);
zeromem(buf, sizeof(buf));
return res;
}
void dh_free(dh_key *key)
{
_ARGCHK(key != NULL);
mp_clear_multi(&key->x, &key->y, NULL);
}
#define OUTPUT_BIGNUM(num, buf2, y, z) \
{ \
2003-03-02 20:02:42 -05:00
z = (unsigned long)mp_unsigned_bin_size(num); \
2003-03-02 19:59:24 -05:00
STORE32L(z, buf2+y); \
y += 4; \
2003-12-24 13:59:57 -05:00
if ((err = mp_to_unsigned_bin(num, buf2+y)) != MP_OKAY) { return mpi_to_ltc_error(err); } \
2003-03-02 19:59:24 -05:00
y += z; \
}
#define INPUT_BIGNUM(num, in, x, y) \
{ \
/* load value */ \
2003-03-02 20:01:40 -05:00
if (y + 4 > inlen) { \
2003-03-02 20:02:42 -05:00
err = CRYPT_INVALID_PACKET; \
2003-03-02 20:01:40 -05:00
goto error; \
} \
2003-03-02 19:59:24 -05:00
LOAD32L(x, in+y); \
y += 4; \
\
/* sanity check... */ \
2003-03-02 20:01:40 -05:00
if (x+y > inlen) { \
2003-03-02 20:02:42 -05:00
err = CRYPT_INVALID_PACKET; \
2003-03-02 19:59:24 -05:00
goto error; \
} \
\
/* load it */ \
2003-12-24 13:59:57 -05:00
if ((err = mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x)) != MP_OKAY) {\
err = mpi_to_ltc_error(err); \
2003-03-02 19:59:24 -05:00
goto error; \
} \
y += x; \
2003-12-24 13:59:57 -05:00
if ((err = mp_shrink(num)) != MP_OKAY) { \
err = mpi_to_ltc_error(err); \
2003-03-02 20:02:10 -05:00
goto error; \
} \
2003-03-02 19:59:24 -05:00
}
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key)
{
unsigned char buf2[1536];
unsigned long y, z;
2003-12-24 13:59:57 -05:00
int err;
2003-03-02 19:59:24 -05:00
_ARGCHK(out != NULL);
_ARGCHK(outlen != NULL);
_ARGCHK(key != NULL);
if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
return CRYPT_PK_NOT_PRIVATE;
}
/* header */
y = PACKET_SIZE;
/* header */
buf2[y++] = type;
2003-03-02 20:02:42 -05:00
buf2[y++] = (unsigned char)(sets[key->idx].size / 8);
2003-03-02 19:59:24 -05:00
/* export y */
OUTPUT_BIGNUM(&key->y, buf2, y, z);
2003-06-01 14:55:11 -04:00
if (type == PK_PRIVATE) {
2003-03-02 19:59:24 -05:00
/* export x */
OUTPUT_BIGNUM(&key->x, buf2, y, z);
}
2003-06-01 14:55:11 -04:00
2003-03-02 19:59:24 -05:00
/* check for overflow */
if (*outlen < y) {
#ifdef CLEAN_STACK
zeromem(buf2, sizeof(buf2));
#endif
return CRYPT_BUFFER_OVERFLOW;
}
/* store header */
2003-03-02 20:02:10 -05:00
packet_store_header(buf2, PACKET_SECT_DH, PACKET_SUB_KEY);
2003-03-02 19:59:24 -05:00
/* output it */
*outlen = y;
2003-03-02 20:02:42 -05:00
memcpy(out, buf2, (size_t)y);
2003-03-02 19:59:24 -05:00
/* clear mem */
2003-06-01 14:55:11 -04:00
#ifdef CLEAN_STACK
2003-03-02 19:59:24 -05:00
zeromem(buf2, sizeof(buf2));
2003-06-01 14:55:11 -04:00
#endif
2003-03-02 19:59:24 -05:00
return CRYPT_OK;
}
2003-03-02 20:01:40 -05:00
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key)
2003-03-02 19:59:24 -05:00
{
2003-03-02 20:01:40 -05:00
unsigned long x, y, s;
2003-03-02 20:02:42 -05:00
int err;
2003-03-02 19:59:24 -05:00
_ARGCHK(in != NULL);
_ARGCHK(key != NULL);
2003-06-01 14:55:11 -04:00
/* make sure valid length */
if (2+PACKET_SIZE > inlen) {
return CRYPT_INVALID_PACKET;
}
2003-03-02 19:59:24 -05:00
/* check type byte */
2003-03-02 20:02:42 -05:00
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_KEY)) != CRYPT_OK) {
return err;
2003-03-02 19:59:24 -05:00
}
/* init */
2003-12-24 13:59:57 -05:00
if ((err = mp_init_multi(&key->x, &key->y, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err);
2003-03-02 19:59:24 -05:00
}
2003-06-01 14:55:11 -04:00
/* advance past packet header */
2003-03-02 19:59:24 -05:00
y = PACKET_SIZE;
2003-06-01 14:55:11 -04:00
/* key type, e.g. private, public */
2003-03-02 20:02:42 -05:00
key->type = (int)in[y++];
2003-06-01 14:55:11 -04:00
/* key size in bytes */
2003-03-02 20:02:42 -05:00
s = (unsigned long)in[y++] * 8;
2003-06-01 14:55:11 -04:00
2003-03-02 20:02:42 -05:00
for (x = 0; (s > (unsigned long)sets[x].size) && (sets[x].size != 0); x++);
2003-03-02 20:01:00 -05:00
if (sets[x].size == 0) {
2003-03-02 20:02:42 -05:00
err = CRYPT_INVALID_KEYSIZE;
2003-03-02 20:01:00 -05:00
goto error;
}
2003-03-02 20:02:42 -05:00
key->idx = (int)x;
2003-03-02 19:59:24 -05:00
/* type check both values */
if ((key->type != PK_PUBLIC) && (key->type != PK_PRIVATE)) {
2003-03-02 20:02:42 -05:00
err = CRYPT_PK_TYPE_MISMATCH;
2003-03-02 19:59:24 -05:00
goto error;
}
/* is the key idx valid? */
2003-03-02 20:02:42 -05:00
if (is_valid_idx(key->idx) != 1) {
err = CRYPT_PK_TYPE_MISMATCH;
2003-03-02 19:59:24 -05:00
goto error;
}
/* load public value g^x mod p*/
INPUT_BIGNUM(&key->y, in, x, y);
if (key->type == PK_PRIVATE) {
INPUT_BIGNUM(&key->x, in, x, y);
}
2003-03-02 20:02:10 -05:00
/* eliminate private key if public */
if (key->type == PK_PUBLIC) {
mp_clear(&key->x);
2003-06-01 14:55:11 -04:00
}
2003-03-02 20:02:10 -05:00
2003-03-02 19:59:24 -05:00
return CRYPT_OK;
error:
mp_clear_multi(&key->y, &key->x, NULL);
2003-03-02 20:02:42 -05:00
return err;
2003-03-02 19:59:24 -05:00
}
2003-06-01 14:55:11 -04:00
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
2003-03-02 19:59:24 -05:00
unsigned char *out, unsigned long *outlen)
{
mp_int tmp, p;
unsigned long x;
int res;
_ARGCHK(private_key != NULL);
_ARGCHK(public_key != NULL);
_ARGCHK(out != NULL);
_ARGCHK(outlen != NULL);
/* types valid? */
if (private_key->type != PK_PRIVATE) {
return CRYPT_PK_NOT_PRIVATE;
}
/* same idx? */
if (private_key->idx != public_key->idx) {
return CRYPT_PK_TYPE_MISMATCH;
}
/* compute y^x mod p */
2003-06-01 14:55:11 -04:00
if (mp_init_multi(&tmp, &p, NULL) != MP_OKAY) {
2003-03-02 19:59:24 -05:00
return CRYPT_MEM;
}
2003-07-10 22:09:41 -04:00
if (mp_read_radix(&p, (char *)sets[private_key->idx].prime, 64) != MP_OKAY) { goto error; }
2003-07-16 13:43:06 -04:00
if (mp_exptmod(&public_key->y, &private_key->x, &p, &tmp) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
/* enough space for output? */
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 (*outlen < x) {
res = CRYPT_BUFFER_OVERFLOW;
goto done;
}
2003-07-16 13:43:06 -04:00
if (mp_to_unsigned_bin(&tmp, out) != MP_OKAY) { goto error; }
2003-03-02 19:59:24 -05:00
*outlen = x;
res = CRYPT_OK;
goto done;
error:
res = CRYPT_MEM;
done:
mp_clear_multi(&p, &tmp, NULL);
return res;
}
#include "dh_sys.c"
#endif