X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Frsa.c;h=b0b7bf50cde27c2fe68c108360dfa511df3e0503;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=79d88cd2d7b0fd2227e0fa7711da9ea1244a4496;hpb=ee9ad49e68cd69759ca643579c2f0de0747c4f61;p=silc.git diff --git a/lib/silccrypt/rsa.c b/lib/silccrypt/rsa.c index 79d88cd2..b0b7bf50 100644 --- a/lib/silccrypt/rsa.c +++ b/lib/silccrypt/rsa.c @@ -75,8 +75,9 @@ SILC_PKCS_API_INIT(rsa) { - uint32 prime_bits = keylen / 2; + SilcUInt32 prime_bits = keylen / 2; SilcMPInt p, q; + bool found = FALSE; printf("Generating RSA Public and Private keys, might take a while...\n"); @@ -84,16 +85,17 @@ SILC_PKCS_API_INIT(rsa) silc_mp_init(&q); /* Find p and q */ - retry_primes: - printf("Finding p: "); - silc_math_gen_prime(&p, prime_bits, TRUE); - - printf("\nFinding q: "); - silc_math_gen_prime(&q, prime_bits, TRUE); - - if ((silc_mp_cmp(&p, &q)) == 0) { - printf("\nFound equal primes, not good, retrying...\n"); - goto retry_primes; + while (!found) { + printf("Finding p: "); + silc_math_gen_prime(&p, prime_bits, TRUE); + + printf("\nFinding q: "); + silc_math_gen_prime(&q, prime_bits, TRUE); + + if ((silc_mp_cmp(&p, &q)) == 0) + printf("\nFound equal primes, not good, retrying...\n"); + else + found = TRUE; } /* If p is smaller than q, switch them */ @@ -130,7 +132,7 @@ SILC_PKCS_API_GET_PUBLIC_KEY(rsa) { RsaKey *key = (RsaKey *)context; unsigned char *e, *n, *ret; - uint32 e_len, n_len; + SilcUInt32 e_len, n_len; unsigned char tmp[4]; e = silc_mp_mp2bin(&key->e, 0, &e_len); @@ -169,7 +171,7 @@ SILC_PKCS_API_GET_PRIVATE_KEY(rsa) { RsaKey *key = (RsaKey *)context; unsigned char *e, *n, *d, *ret; - uint32 e_len, n_len, d_len; + SilcUInt32 e_len, n_len, d_len; unsigned char tmp[4]; e = silc_mp_mp2bin(&key->e, 0, &e_len); @@ -216,7 +218,13 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa) { RsaKey *key = (RsaKey *)context; unsigned char tmp[4]; - uint32 e_len, n_len; + SilcUInt32 e_len, n_len; + + if (key->pub_set) { + silc_mp_uninit(&key->e); + silc_mp_uninit(&key->e); + key->pub_set = FALSE; + } silc_mp_init(&key->e); silc_mp_init(&key->n); @@ -242,6 +250,7 @@ SILC_PKCS_API_SET_PUBLIC_KEY(rsa) silc_mp_bin2mp(key_data + 4 + e_len + 4, n_len, &key->n); key->bits = n_len * 8; + key->pub_set = TRUE; return key->bits; } @@ -254,7 +263,18 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa) { RsaKey *key = (RsaKey *)context; unsigned char tmp[4]; - uint32 e_len, n_len, d_len; + SilcUInt32 e_len, n_len, d_len; + + if (key->prv_set) { + silc_mp_uninit(&key->d); + key->prv_set = FALSE; + } + + if (key->pub_set) { + silc_mp_uninit(&key->e); + silc_mp_uninit(&key->n); + key->pub_set = FALSE; + } silc_mp_init(&key->e); silc_mp_init(&key->n); @@ -265,6 +285,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa) if (e_len > key_len) { silc_mp_uninit(&key->e); silc_mp_uninit(&key->n); + silc_mp_uninit(&key->d); return FALSE; } @@ -275,6 +296,7 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa) if (e_len + n_len > key_len) { silc_mp_uninit(&key->e); silc_mp_uninit(&key->n); + silc_mp_uninit(&key->d); return FALSE; } @@ -285,12 +307,15 @@ SILC_PKCS_API_SET_PRIVATE_KEY(rsa) if (e_len + n_len + d_len > key_len) { silc_mp_uninit(&key->e); silc_mp_uninit(&key->n); + silc_mp_uninit(&key->d); return FALSE; } silc_mp_bin2mp(key_data + 4 + e_len + 4 + n_len + 4, d_len, &key->d); key->bits = n_len * 8; + key->prv_set = TRUE; + key->pub_set = TRUE; return TRUE; } @@ -454,7 +479,7 @@ SILC_PKCS_API_VERIFY(rsa) to compute the modulus n has to be generated before calling this. They are then sent as argument for the function. */ -void rsa_generate_keys(RsaKey *key, uint32 bits, +void rsa_generate_keys(RsaKey *key, SilcUInt32 bits, SilcMPInt *p, SilcMPInt *q) { SilcMPInt phi, hlp; @@ -462,8 +487,6 @@ void rsa_generate_keys(RsaKey *key, uint32 bits, SilcMPInt pm1, qm1; /* Initialize variables */ - silc_mp_init(&key->p); - silc_mp_init(&key->q); silc_mp_init(&key->n); silc_mp_init(&key->e); silc_mp_init(&key->d); @@ -477,16 +500,12 @@ void rsa_generate_keys(RsaKey *key, uint32 bits, /* Set modulus length */ key->bits = bits; - /* Set the primes */ - silc_mp_set(&key->p, p); - silc_mp_set(&key->q, q); - /* Compute modulus, n = p * q */ - silc_mp_mul(&key->n, &key->p, &key->q); + silc_mp_mul(&key->n, p, q); /* phi = (p - 1) * (q - 1) */ - silc_mp_sub_ui(&pm1, &key->p, 1); - silc_mp_sub_ui(&qm1, &key->q, 1); + silc_mp_sub_ui(&pm1, p, 1); + silc_mp_sub_ui(&qm1, q, 1); silc_mp_mul(&phi, &pm1, &qm1); /* Set e, the public exponent. We try to use same public exponent @@ -520,11 +539,12 @@ void rsa_generate_keys(RsaKey *key, uint32 bits, void rsa_clear_keys(RsaKey *key) { key->bits = 0; - silc_mp_uninit(&key->p); - silc_mp_uninit(&key->q); - silc_mp_uninit(&key->n); - silc_mp_uninit(&key->e); - silc_mp_uninit(&key->d); + if (key->pub_set) { + silc_mp_uninit(&key->n); + silc_mp_uninit(&key->e); + } + if (key->prv_set) + silc_mp_uninit(&key->d); } /* RSA encrypt/decrypt function. cm = ciphertext or plaintext,