123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- #include <math.h>
- #include <stdlib.h>
- #include <openssl/x509.h>
- #include <openssl/rsa.h>
- #include <openssl/bn.h>
- #include "questions.h"
- #include "qwiener.h"
- cf_t* cf_new(void)
- {
- cf_t *f;
- f = (cf_t *) malloc(sizeof(cf_t));
- size_t i;
- for (i=0; i!=3; i++) {
- f->fs[i].h = BN_new();
- f->fs[i].k = BN_new();
- }
- f->a = BN_new();
- f->x.h = BN_new();
- f->x.k = BN_new();
- f->ctx = BN_CTX_new();
- return f;
- }
- void cf_free(cf_t* f)
- {
- size_t i;
- for (i=0; i!=3; i++) {
- BN_free(f->fs[i].h);
- BN_free(f->fs[i].k);
- }
- BN_free(f->a);
- BN_free(f->x.h);
- BN_free(f->x.k);
- free(f);
- }
- cf_t* cf_init(cf_t* f, BIGNUM* num, BIGNUM* den)
- {
- if (!f) f = cf_new();
- BN_zero(f->fs[0].h);
- BN_one(f->fs[0].k);
- BN_one(f->fs[1].h);
- BN_zero(f->fs[1].k);
- f->i = 2;
- if (!BN_copy(f->x.h, num)) return NULL;
- if (!BN_copy(f->x.k, den)) return NULL;
- return f;
- }
- bigfraction_t* cf_next(cf_t *f)
- {
- bigfraction_t *ith_fs = &f->fs[f->i];
- BIGNUM* rem = BN_new();
- if (BN_is_zero(f->x.h)) return NULL;
- BN_div(f->a, rem, f->x.h, f->x.k, f->ctx);
-
- BN_mul(f->fs[f->i].h , f->a, f->fs[(f->i-1+3) % 3].h, f->ctx);
- BN_uadd(f->fs[f->i].h, f->fs[f->i].h, f->fs[(f->i-2+3) % 3].h);
-
- BN_mul(f->fs[f->i].k , f->a, f->fs[(f->i-1+3) % 3].k, f->ctx);
- BN_uadd(f->fs[f->i].k, f->fs[f->i].k, f->fs[(f->i-2+3) % 3].k);
- f->i = (f->i + 1) % 3;
-
- BN_copy(f->x.h, f->x.k);
- BN_copy(f->x.k, rem);
- return ith_fs;
- }
- int BN_sqrtmod(BIGNUM* dv, BIGNUM* rem, BIGNUM* a, BN_CTX* ctx)
- {
- BIGNUM *shift;
- BIGNUM *adj;
- shift = BN_new();
- adj = BN_new();
- BN_zero(dv);
- BN_copy(rem, a);
-
-
- for (bn_wexpand(shift, a->top+1), shift->top=a->top, shift->d[shift->top-1] = 1;
- BN_ucmp(shift, rem) != 1;
-
- BN_lshift1(shift, shift), BN_lshift1(shift, shift));
- while (!BN_is_one(shift)) {
-
- BN_rshift1(shift, shift);
- BN_rshift1(shift, shift);
- BN_uadd(adj, dv, shift);
- BN_rshift1(dv, dv);
- if (BN_ucmp(rem, adj) != -1) {
- BN_uadd(dv, dv, shift);
- BN_usub(rem, rem, adj);
- }
- }
- BN_free(shift);
- BN_free(adj);
- return BN_is_zero(rem);
- }
- int wiener_question_setup(void) { return 0; }
- int wiener_question_teardown(void) { return 0; }
- int wiener_question_test(X509* cert) { return 1; }
- int wiener_question_ask(X509* cert)
- {
- RSA *rsa;
-
- BIGNUM *n, *e, *d, *phi;
- BIGNUM *p, *q;
-
- cf_t* cf;
- bigfraction_t *it;
- size_t i;
- BIGNUM *t, *tmp, *rem;
-
- BIGNUM *b2, *delta;
- BN_CTX *ctx;
- int bits;
- rsa = X509_get_pubkey(cert)->pkey.rsa;
- phi = BN_new();
- tmp = BN_new();
- rem = BN_new();
- n = rsa->n;
- e = rsa->e;
- b2 = BN_new();
- delta = BN_new();
-
- bits = BN_num_bits(n);
- cf = cf_init(NULL, e, n);
- ctx = cf->ctx;
- for (i=0, it = cf_next(cf);
-
- i!=bits && it;
- i++, it = cf_next(cf)) {
- t = it->h;
- d = it->k;
-
- BN_mul(phi, e, d, cf->ctx);
- BN_usub(tmp, phi, BN_value_one());
- BN_div(phi, rem, tmp, t, cf->ctx);
- if (!BN_is_zero(rem)) continue;
-
- if (BN_is_odd(phi) && BN_cmp(n, phi) == 1) continue;
-
- BN_usub(b2, n, phi);
- BN_uadd(b2, b2, BN_value_one());
- BN_rshift(b2, b2, 1);
- if (BN_is_zero(b2)) continue;
-
- BN_sqr(tmp, b2, ctx);
- BN_usub(delta, tmp, n);
- if (!BN_sqrtmod(tmp, rem, delta, ctx)) continue;
-
- p = BN_new();
- q = BN_new();
- BN_usub(p, b2, tmp);
- BN_uadd(q, b2, tmp);
-
- break;
- }
- cf_free(cf);
- BN_free(rem);
- BN_free(tmp);
- BN_free(b2);
- BN_free(delta);
- BN_free(phi);
- return i;
- }
- qa_question_t WienerQuestion = {
- .name = "Wiener",
- .setup = wiener_question_setup,
- .teardown = wiener_question_teardown,
- .test = wiener_question_test,
- .ask = wiener_question_ask
- };
|