dd
Fork of CyaSSL by
Revision 2:ca372ff4b2fe, committed 2015-02-20
- Comitter:
- saranieves92
- Date:
- Fri Feb 20 18:37:29 2015 +0000
- Parent:
- 1:3a6d417fac10
- Commit message:
- intento de rsa
Changed in this revision
ctc_rsa.h | Show annotated file Show diff for this revision Revisions of this file |
rsa.c | Show annotated file Show diff for this revision Revisions of this file |
diff -r 3a6d417fac10 -r ca372ff4b2fe ctc_rsa.h --- a/ctc_rsa.h Fri Feb 20 17:44:36 2015 +0000 +++ b/ctc_rsa.h Fri Feb 20 18:37:29 2015 +0000 @@ -61,9 +61,8 @@ int RsaEncryptSize(RsaKey* key); -#ifdef CYASSL_KEY_GEN - int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng); -#endif +int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng); + #ifdef __cplusplus
diff -r 3a6d417fac10 -r ca372ff4b2fe rsa.c --- a/rsa.c Fri Feb 20 17:44:36 2015 +0000 +++ b/rsa.c Fri Feb 20 18:37:29 2015 +0000 @@ -45,7 +45,110 @@ RSA_MIN_PAD_SZ = 11 /* seperator + 0 + pad value + 8 pads */ }; +int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng) +{ + mp_int p, q, tmp1, tmp2, tmp3; + int err; + if (key == NULL || rng == NULL) + return -1; + + if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE) + return -1; + + if (e < 3 || (e & 1) == 0) + return -1; + + if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) + return err; + + err = mp_set_int(&tmp3, e); + + /* make p */ + if (err == MP_OKAY) { + do { + err = rand_prime(&p, size/16, rng, key->heap); /* size in bytes/2 */ + + if (err == MP_OKAY) + err = mp_sub_d(&p, 1, &tmp1); /* tmp1 = p-1 */ + + if (err == MP_OKAY) + err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(p-1, e) */ + } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes p-1 */ + } + + /* make q */ + if (err == MP_OKAY) { + do { + err = rand_prime(&q, size/16, rng, key->heap); /* size in bytes/2 */ + + if (err == MP_OKAY) + err = mp_sub_d(&q, 1, &tmp1); /* tmp1 = q-1 */ + + if (err == MP_OKAY) + err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(q-1, e) */ + } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes q-1 */ + } + + if (err == MP_OKAY) + err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL); + + if (err == MP_OKAY) + err = mp_init_multi(&key->dP, &key->dP, &key->u, NULL, NULL, NULL); + + if (err == MP_OKAY) + err = mp_sub_d(&p, 1, &tmp2); /* tmp2 = p-1 */ + + if (err == MP_OKAY) + err = mp_lcm(&tmp1, &tmp2, &tmp1); /* tmp1 = lcm(p-1, q-1),last loop */ + + /* make key */ + if (err == MP_OKAY) + err = mp_set_int(&key->e, e); /* key->e = e */ + + if (err == MP_OKAY) /* key->d = 1/e mod lcm(p-1, q-1) */ + err = mp_invmod(&key->e, &tmp1, &key->d); + + if (err == MP_OKAY) + err = mp_mul(&p, &q, &key->n); /* key->n = pq */ + + if (err == MP_OKAY) + err = mp_sub_d(&p, 1, &tmp1); + + if (err == MP_OKAY) + err = mp_sub_d(&q, 1, &tmp2); + + if (err == MP_OKAY) + err = mp_mod(&key->d, &tmp1, &key->dP); + + if (err == MP_OKAY) + err = mp_mod(&key->d, &tmp2, &key->dQ); + + if (err == MP_OKAY) + err = mp_invmod(&q, &p, &key->u); + + if (err == MP_OKAY) + err = mp_copy(&p, &key->p); + + if (err == MP_OKAY) + err = mp_copy(&q, &key->q); + + if (err == MP_OKAY) + key->type = RSA_PRIVATE; + + mp_clear(&tmp3); + mp_clear(&tmp2); + mp_clear(&tmp1); + mp_clear(&q); + mp_clear(&p); + + if (err != MP_OKAY) { + FreeRsaKey(key); + return err; + } + + return 0; +} void InitRsaKey(RsaKey* key, void* heap) { key->type = -1; /* haven't decdied yet */