123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- #include "internal/cryptlib.h"
- #include "bn_local.h"
- static ossl_inline
- BIGNUM *bn_mod_inverse_no_branch(BIGNUM *in,
- const BIGNUM *a, const BIGNUM *n,
- BN_CTX *ctx, int *pnoinv)
- {
- BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
- BIGNUM *ret = NULL;
- int sign;
- bn_check_top(a);
- bn_check_top(n);
- BN_CTX_start(ctx);
- A = BN_CTX_get(ctx);
- B = BN_CTX_get(ctx);
- X = BN_CTX_get(ctx);
- D = BN_CTX_get(ctx);
- M = BN_CTX_get(ctx);
- Y = BN_CTX_get(ctx);
- T = BN_CTX_get(ctx);
- if (T == NULL)
- goto err;
- if (in == NULL)
- R = BN_new();
- else
- R = in;
- if (R == NULL)
- goto err;
- BN_one(X);
- BN_zero(Y);
- if (BN_copy(B, a) == NULL)
- goto err;
- if (BN_copy(A, n) == NULL)
- goto err;
- A->neg = 0;
- if (B->neg || (BN_ucmp(B, A) >= 0)) {
-
- {
- BIGNUM local_B;
- bn_init(&local_B);
- BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
- if (!BN_nnmod(B, &local_B, A, ctx))
- goto err;
-
- }
- }
- sign = -1;
-
- while (!BN_is_zero(B)) {
- BIGNUM *tmp;
-
-
- {
- BIGNUM local_A;
- bn_init(&local_A);
- BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
-
- if (!BN_div(D, M, &local_A, B, ctx))
- goto err;
-
- }
-
- tmp = A;
-
- A = B;
- B = M;
-
-
- if (!BN_mul(tmp, D, X, ctx))
- goto err;
- if (!BN_add(tmp, tmp, Y))
- goto err;
- M = Y;
- Y = X;
- X = tmp;
- sign = -sign;
- }
-
- if (sign < 0) {
- if (!BN_sub(Y, n, Y))
- goto err;
- }
-
- if (BN_is_one(A)) {
-
- if (!Y->neg && BN_ucmp(Y, n) < 0) {
- if (!BN_copy(R, Y))
- goto err;
- } else {
- if (!BN_nnmod(R, Y, n, ctx))
- goto err;
- }
- } else {
- *pnoinv = 1;
-
- goto err;
- }
- ret = R;
- *pnoinv = 0;
- err:
- if ((ret == NULL) && (in == NULL))
- BN_free(R);
- BN_CTX_end(ctx);
- bn_check_top(ret);
- return ret;
- }
- BIGNUM *int_bn_mod_inverse(BIGNUM *in,
- const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
- int *pnoinv)
- {
- BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
- BIGNUM *ret = NULL;
- int sign;
-
- if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
- *pnoinv = 1;
- return NULL;
- }
- *pnoinv = 0;
- if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
- || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
- return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv);
- }
- bn_check_top(a);
- bn_check_top(n);
- BN_CTX_start(ctx);
- A = BN_CTX_get(ctx);
- B = BN_CTX_get(ctx);
- X = BN_CTX_get(ctx);
- D = BN_CTX_get(ctx);
- M = BN_CTX_get(ctx);
- Y = BN_CTX_get(ctx);
- T = BN_CTX_get(ctx);
- if (T == NULL)
- goto err;
- if (in == NULL)
- R = BN_new();
- else
- R = in;
- if (R == NULL)
- goto err;
- BN_one(X);
- BN_zero(Y);
- if (BN_copy(B, a) == NULL)
- goto err;
- if (BN_copy(A, n) == NULL)
- goto err;
- A->neg = 0;
- if (B->neg || (BN_ucmp(B, A) >= 0)) {
- if (!BN_nnmod(B, B, A, ctx))
- goto err;
- }
- sign = -1;
-
- if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {
-
- int shift;
- while (!BN_is_zero(B)) {
-
-
- shift = 0;
- while (!BN_is_bit_set(B, shift)) {
- shift++;
- if (BN_is_odd(X)) {
- if (!BN_uadd(X, X, n))
- goto err;
- }
-
- if (!BN_rshift1(X, X))
- goto err;
- }
- if (shift > 0) {
- if (!BN_rshift(B, B, shift))
- goto err;
- }
-
- shift = 0;
- while (!BN_is_bit_set(A, shift)) {
- shift++;
- if (BN_is_odd(Y)) {
- if (!BN_uadd(Y, Y, n))
- goto err;
- }
-
- if (!BN_rshift1(Y, Y))
- goto err;
- }
- if (shift > 0) {
- if (!BN_rshift(A, A, shift))
- goto err;
- }
-
- if (BN_ucmp(B, A) >= 0) {
-
- if (!BN_uadd(X, X, Y))
- goto err;
-
- if (!BN_usub(B, B, A))
- goto err;
- } else {
-
- if (!BN_uadd(Y, Y, X))
- goto err;
-
- if (!BN_usub(A, A, B))
- goto err;
- }
- }
- } else {
-
- while (!BN_is_zero(B)) {
- BIGNUM *tmp;
-
-
- if (BN_num_bits(A) == BN_num_bits(B)) {
- if (!BN_one(D))
- goto err;
- if (!BN_sub(M, A, B))
- goto err;
- } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
-
- if (!BN_lshift1(T, B))
- goto err;
- if (BN_ucmp(A, T) < 0) {
-
- if (!BN_one(D))
- goto err;
- if (!BN_sub(M, A, B))
- goto err;
- } else {
-
- if (!BN_sub(M, A, T))
- goto err;
- if (!BN_add(D, T, B))
- goto err;
- if (BN_ucmp(A, D) < 0) {
-
- if (!BN_set_word(D, 2))
- goto err;
-
- } else {
-
- if (!BN_set_word(D, 3))
- goto err;
-
- if (!BN_sub(M, M, B))
- goto err;
- }
- }
- } else {
- if (!BN_div(D, M, A, B, ctx))
- goto err;
- }
-
- tmp = A;
-
- A = B;
- B = M;
-
-
-
- if (BN_is_one(D)) {
- if (!BN_add(tmp, X, Y))
- goto err;
- } else {
- if (BN_is_word(D, 2)) {
- if (!BN_lshift1(tmp, X))
- goto err;
- } else if (BN_is_word(D, 4)) {
- if (!BN_lshift(tmp, X, 2))
- goto err;
- } else if (D->top == 1) {
- if (!BN_copy(tmp, X))
- goto err;
- if (!BN_mul_word(tmp, D->d[0]))
- goto err;
- } else {
- if (!BN_mul(tmp, D, X, ctx))
- goto err;
- }
- if (!BN_add(tmp, tmp, Y))
- goto err;
- }
- M = Y;
- Y = X;
- X = tmp;
- sign = -sign;
- }
- }
-
- if (sign < 0) {
- if (!BN_sub(Y, n, Y))
- goto err;
- }
-
- if (BN_is_one(A)) {
-
- if (!Y->neg && BN_ucmp(Y, n) < 0) {
- if (!BN_copy(R, Y))
- goto err;
- } else {
- if (!BN_nnmod(R, Y, n, ctx))
- goto err;
- }
- } else {
- *pnoinv = 1;
- goto err;
- }
- ret = R;
- err:
- if ((ret == NULL) && (in == NULL))
- BN_free(R);
- BN_CTX_end(ctx);
- bn_check_top(ret);
- return ret;
- }
- BIGNUM *BN_mod_inverse(BIGNUM *in,
- const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
- {
- BN_CTX *new_ctx = NULL;
- BIGNUM *rv;
- int noinv = 0;
- if (ctx == NULL) {
- ctx = new_ctx = BN_CTX_new();
- if (ctx == NULL) {
- BNerr(BN_F_BN_MOD_INVERSE, ERR_R_MALLOC_FAILURE);
- return NULL;
- }
- }
- rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
- if (noinv)
- BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);
- BN_CTX_free(new_ctx);
- return rv;
- }
- int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
- {
- BIGNUM *g, *temp = NULL;
- BN_ULONG mask = 0;
- int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0;
-
- if (BN_is_zero(in_b)) {
- ret = BN_copy(r, in_a) != NULL;
- r->neg = 0;
- return ret;
- }
- if (BN_is_zero(in_a)) {
- ret = BN_copy(r, in_b) != NULL;
- r->neg = 0;
- return ret;
- }
- bn_check_top(in_a);
- bn_check_top(in_b);
- BN_CTX_start(ctx);
- temp = BN_CTX_get(ctx);
- g = BN_CTX_get(ctx);
-
- if (g == NULL
- || !BN_lshift1(g, in_b)
- || !BN_lshift1(r, in_a))
- goto err;
-
- for (i = 0; i < r->dmax && i < g->dmax; i++) {
- mask = ~(r->d[i] | g->d[i]);
- for (j = 0; j < BN_BITS2; j++) {
- bit &= mask;
- shifts += bit;
- mask >>= 1;
- }
- }
-
- if (!BN_rshift(r, r, shifts)
- || !BN_rshift(g, g, shifts))
- goto err;
-
- top = 1 + ((r->top >= g->top) ? r->top : g->top);
- if (bn_wexpand(r, top) == NULL
- || bn_wexpand(g, top) == NULL
- || bn_wexpand(temp, top) == NULL)
- goto err;
-
- BN_consttime_swap((~r->d[0]) & 1, r, g, top);
-
- rlen = BN_num_bits(r);
- glen = BN_num_bits(g);
- m = 4 + 3 * ((rlen >= glen) ? rlen : glen);
- for (i = 0; i < m; i++) {
-
- cond = (-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1
-
- & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1)));
- delta = (-cond & -delta) | ((cond - 1) & delta);
- r->neg ^= cond;
-
- BN_consttime_swap(cond, r, g, top);
-
- delta++;
- if (!BN_add(temp, g, r))
- goto err;
- BN_consttime_swap(g->d[0] & 1
-
- & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1))),
- g, temp, top);
- if (!BN_rshift1(g, g))
- goto err;
- }
-
- r->neg = 0;
-
- if (!BN_lshift(r, r, shifts)
- || !BN_rshift1(r, r))
- goto err;
- ret = 1;
- err:
- BN_CTX_end(ctx);
- bn_check_top(r);
- return ret;
- }
|