From 544153a8ffa6d68385712ab0a7c6315399346909 Mon Sep 17 00:00:00 2001 From: Benedict Date: Tue, 29 Nov 2016 22:24:26 +0100 Subject: completed challenge 40, set 5 --- lib/lib5.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 198 insertions(+), 9 deletions(-) (limited to 'lib/lib5.c') diff --git a/lib/lib5.c b/lib/lib5.c index 2f45c29..722cc46 100644 --- a/lib/lib5.c +++ b/lib/lib5.c @@ -331,10 +331,10 @@ int rsa_generate_key_bignum(struct rsa_key_bignum *public, struct rsa_key_bignum // well should check here for error but asusme infinte memory here BIGNUM *q = BN_new(); - if (!BN_generate_prime_ex(p, 256, 1, NULL, NULL, NULL) || - !BN_generate_prime_ex(q, 256, 1, NULL, NULL, NULL)) - die("error generating prime"); - + BN_generate_prime_ex(p, 256, 1, NULL, NULL, NULL); + do { + BN_generate_prime_ex(q, 256, 1, NULL, NULL, NULL); + } while (!BN_cmp(p, q)); BIGNUM *n = BN_new(); if(!BN_mul(n,p,q,ctx)) @@ -358,14 +358,17 @@ int rsa_generate_key_bignum(struct rsa_key_bignum *public, struct rsa_key_bignum BIGNUM *e = BN_new(); BN_set_word(e, 3); - //BIGNUM *d = BN_mod_inverse(NULL, e, et, ctx); BIGNUM *d = BN_new(); modular_multiplicative_inverse_bignum_my(d, e, et); - public->exponent = e; - public->modulo = n; - private->exponent = d; - private->modulo = n; + public->exponent = BN_new(); + public->modulo = BN_new(); + private->exponent = BN_new(); + private->modulo = BN_new(); + public->exponent = BN_dup(e); + BN_copy(public->modulo, n); + BN_copy(private->exponent, d); + BN_copy(private->modulo, n); } @@ -375,3 +378,189 @@ int free_rsa_key_bignum(struct rsa_key_bignum *t) BN_free(t->modulo); } +/** + * computes the nth root of number. + * Note that the computed root is always an integer + * does not work good for numbers which are not divisible by n :-( + **/ +int nth_root_bignum(BIGNUM *res, BIGNUM *number, BIGNUM *n) +{ + BIGNUM *n_1 = BN_new(); + BIGNUM *r = BN_new(); + BIGNUM *d = BN_new(); + BIGNUM *zero = BN_new(); + BN_zero(zero); + BN_set_word(r, 1); + BN_set_word(d, 1); + BN_sub(n_1, n, d); + + do { + BN_exp(res, r, n_1, ctx); + BN_div(res, NULL, number, res, ctx); + BN_sub(res, res, r); + BN_div(d, NULL, res, n, ctx); + BN_add(r, r, d); + } while (BN_cmp(d, zero)); + + BN_copy(res, r); + BN_free(zero); + BN_free(r); + BN_free(d); + BN_free(n_1); +} +int rsa_broadcast_cube(BIGNUM *res, BIGNUM **a, BIGNUM **n) +{ + BIGNUM *tmp = BN_new(); + BIGNUM *N= BN_new(); + BIGNUM *N_ni = BN_new(); + BIGNUM *sum = BN_new(); + BIGNUM *n_3 = BN_new(); + + BN_set_word(n_3, 3); + BN_one(N); + BN_zero(sum); + int i; + + for(i=0;i<3;i++) + BN_mul(N, N, n[i], ctx); + + for(i=0;i<3;i++) { + BN_div(N_ni, NULL, N, n[i], ctx); + BN_mod_inverse(tmp, N_ni, n[i], ctx); + modular_multiplicative_inverse_bignum_my(tmp, N_ni, n[i]); + BN_mul(tmp, tmp, N_ni, ctx); + BN_mul(tmp, tmp, a[i], ctx); + BN_add(sum, sum, tmp); + } + + BN_nnmod(sum, sum, N, ctx); + nth_root_bignum(res, sum, n_3); +} + +int chinese_remainder_theorem_bignum(BIGNUM *solution, BIGNUM *sol_no_mod, BIGNUM **a, BIGNUM **n, int len) +{ + int i,j; + + for(i=0;i NTH_ROOT_PRECISION || d < -NTH_ROOT_PRECISION); + return r; +} + -- cgit v1.2.3-70-g09d2