11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 51cb0ef41Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at 71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html 81cb0ef41Sopenharmony_ci */ 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include "internal/cryptlib.h" 111cb0ef41Sopenharmony_ci#include "bn_local.h" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci/* 141cb0ef41Sopenharmony_ci * bn_mod_inverse_no_branch is a special version of BN_mod_inverse. It does 151cb0ef41Sopenharmony_ci * not contain branches that may leak sensitive information. 161cb0ef41Sopenharmony_ci * 171cb0ef41Sopenharmony_ci * This is a static function, we ensure all callers in this file pass valid 181cb0ef41Sopenharmony_ci * arguments: all passed pointers here are non-NULL. 191cb0ef41Sopenharmony_ci */ 201cb0ef41Sopenharmony_cistatic ossl_inline 211cb0ef41Sopenharmony_ciBIGNUM *bn_mod_inverse_no_branch(BIGNUM *in, 221cb0ef41Sopenharmony_ci const BIGNUM *a, const BIGNUM *n, 231cb0ef41Sopenharmony_ci BN_CTX *ctx, int *pnoinv) 241cb0ef41Sopenharmony_ci{ 251cb0ef41Sopenharmony_ci BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; 261cb0ef41Sopenharmony_ci BIGNUM *ret = NULL; 271cb0ef41Sopenharmony_ci int sign; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci bn_check_top(a); 301cb0ef41Sopenharmony_ci bn_check_top(n); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci BN_CTX_start(ctx); 331cb0ef41Sopenharmony_ci A = BN_CTX_get(ctx); 341cb0ef41Sopenharmony_ci B = BN_CTX_get(ctx); 351cb0ef41Sopenharmony_ci X = BN_CTX_get(ctx); 361cb0ef41Sopenharmony_ci D = BN_CTX_get(ctx); 371cb0ef41Sopenharmony_ci M = BN_CTX_get(ctx); 381cb0ef41Sopenharmony_ci Y = BN_CTX_get(ctx); 391cb0ef41Sopenharmony_ci T = BN_CTX_get(ctx); 401cb0ef41Sopenharmony_ci if (T == NULL) 411cb0ef41Sopenharmony_ci goto err; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci if (in == NULL) 441cb0ef41Sopenharmony_ci R = BN_new(); 451cb0ef41Sopenharmony_ci else 461cb0ef41Sopenharmony_ci R = in; 471cb0ef41Sopenharmony_ci if (R == NULL) 481cb0ef41Sopenharmony_ci goto err; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci if (!BN_one(X)) 511cb0ef41Sopenharmony_ci goto err; 521cb0ef41Sopenharmony_ci BN_zero(Y); 531cb0ef41Sopenharmony_ci if (BN_copy(B, a) == NULL) 541cb0ef41Sopenharmony_ci goto err; 551cb0ef41Sopenharmony_ci if (BN_copy(A, n) == NULL) 561cb0ef41Sopenharmony_ci goto err; 571cb0ef41Sopenharmony_ci A->neg = 0; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci if (B->neg || (BN_ucmp(B, A) >= 0)) { 601cb0ef41Sopenharmony_ci /* 611cb0ef41Sopenharmony_ci * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, 621cb0ef41Sopenharmony_ci * BN_div_no_branch will be called eventually. 631cb0ef41Sopenharmony_ci */ 641cb0ef41Sopenharmony_ci { 651cb0ef41Sopenharmony_ci BIGNUM local_B; 661cb0ef41Sopenharmony_ci bn_init(&local_B); 671cb0ef41Sopenharmony_ci BN_with_flags(&local_B, B, BN_FLG_CONSTTIME); 681cb0ef41Sopenharmony_ci if (!BN_nnmod(B, &local_B, A, ctx)) 691cb0ef41Sopenharmony_ci goto err; 701cb0ef41Sopenharmony_ci /* Ensure local_B goes out of scope before any further use of B */ 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci sign = -1; 741cb0ef41Sopenharmony_ci /*- 751cb0ef41Sopenharmony_ci * From B = a mod |n|, A = |n| it follows that 761cb0ef41Sopenharmony_ci * 771cb0ef41Sopenharmony_ci * 0 <= B < A, 781cb0ef41Sopenharmony_ci * -sign*X*a == B (mod |n|), 791cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|). 801cb0ef41Sopenharmony_ci */ 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci while (!BN_is_zero(B)) { 831cb0ef41Sopenharmony_ci BIGNUM *tmp; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci /*- 861cb0ef41Sopenharmony_ci * 0 < B < A, 871cb0ef41Sopenharmony_ci * (*) -sign*X*a == B (mod |n|), 881cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|) 891cb0ef41Sopenharmony_ci */ 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci /* 921cb0ef41Sopenharmony_ci * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, 931cb0ef41Sopenharmony_ci * BN_div_no_branch will be called eventually. 941cb0ef41Sopenharmony_ci */ 951cb0ef41Sopenharmony_ci { 961cb0ef41Sopenharmony_ci BIGNUM local_A; 971cb0ef41Sopenharmony_ci bn_init(&local_A); 981cb0ef41Sopenharmony_ci BN_with_flags(&local_A, A, BN_FLG_CONSTTIME); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci /* (D, M) := (A/B, A%B) ... */ 1011cb0ef41Sopenharmony_ci if (!BN_div(D, M, &local_A, B, ctx)) 1021cb0ef41Sopenharmony_ci goto err; 1031cb0ef41Sopenharmony_ci /* Ensure local_A goes out of scope before any further use of A */ 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci /*- 1071cb0ef41Sopenharmony_ci * Now 1081cb0ef41Sopenharmony_ci * A = D*B + M; 1091cb0ef41Sopenharmony_ci * thus we have 1101cb0ef41Sopenharmony_ci * (**) sign*Y*a == D*B + M (mod |n|). 1111cb0ef41Sopenharmony_ci */ 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci tmp = A; /* keep the BIGNUM object, the value does not 1141cb0ef41Sopenharmony_ci * matter */ 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci /* (A, B) := (B, A mod B) ... */ 1171cb0ef41Sopenharmony_ci A = B; 1181cb0ef41Sopenharmony_ci B = M; 1191cb0ef41Sopenharmony_ci /* ... so we have 0 <= B < A again */ 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci /*- 1221cb0ef41Sopenharmony_ci * Since the former M is now B and the former B is now A, 1231cb0ef41Sopenharmony_ci * (**) translates into 1241cb0ef41Sopenharmony_ci * sign*Y*a == D*A + B (mod |n|), 1251cb0ef41Sopenharmony_ci * i.e. 1261cb0ef41Sopenharmony_ci * sign*Y*a - D*A == B (mod |n|). 1271cb0ef41Sopenharmony_ci * Similarly, (*) translates into 1281cb0ef41Sopenharmony_ci * -sign*X*a == A (mod |n|). 1291cb0ef41Sopenharmony_ci * 1301cb0ef41Sopenharmony_ci * Thus, 1311cb0ef41Sopenharmony_ci * sign*Y*a + D*sign*X*a == B (mod |n|), 1321cb0ef41Sopenharmony_ci * i.e. 1331cb0ef41Sopenharmony_ci * sign*(Y + D*X)*a == B (mod |n|). 1341cb0ef41Sopenharmony_ci * 1351cb0ef41Sopenharmony_ci * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at 1361cb0ef41Sopenharmony_ci * -sign*X*a == B (mod |n|), 1371cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|). 1381cb0ef41Sopenharmony_ci * Note that X and Y stay non-negative all the time. 1391cb0ef41Sopenharmony_ci */ 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci if (!BN_mul(tmp, D, X, ctx)) 1421cb0ef41Sopenharmony_ci goto err; 1431cb0ef41Sopenharmony_ci if (!BN_add(tmp, tmp, Y)) 1441cb0ef41Sopenharmony_ci goto err; 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci M = Y; /* keep the BIGNUM object, the value does not 1471cb0ef41Sopenharmony_ci * matter */ 1481cb0ef41Sopenharmony_ci Y = X; 1491cb0ef41Sopenharmony_ci X = tmp; 1501cb0ef41Sopenharmony_ci sign = -sign; 1511cb0ef41Sopenharmony_ci } 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci /*- 1541cb0ef41Sopenharmony_ci * The while loop (Euclid's algorithm) ends when 1551cb0ef41Sopenharmony_ci * A == gcd(a,n); 1561cb0ef41Sopenharmony_ci * we have 1571cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|), 1581cb0ef41Sopenharmony_ci * where Y is non-negative. 1591cb0ef41Sopenharmony_ci */ 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci if (sign < 0) { 1621cb0ef41Sopenharmony_ci if (!BN_sub(Y, n, Y)) 1631cb0ef41Sopenharmony_ci goto err; 1641cb0ef41Sopenharmony_ci } 1651cb0ef41Sopenharmony_ci /* Now Y*a == A (mod |n|). */ 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci if (BN_is_one(A)) { 1681cb0ef41Sopenharmony_ci /* Y*a == 1 (mod |n|) */ 1691cb0ef41Sopenharmony_ci if (!Y->neg && BN_ucmp(Y, n) < 0) { 1701cb0ef41Sopenharmony_ci if (!BN_copy(R, Y)) 1711cb0ef41Sopenharmony_ci goto err; 1721cb0ef41Sopenharmony_ci } else { 1731cb0ef41Sopenharmony_ci if (!BN_nnmod(R, Y, n, ctx)) 1741cb0ef41Sopenharmony_ci goto err; 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci } else { 1771cb0ef41Sopenharmony_ci *pnoinv = 1; 1781cb0ef41Sopenharmony_ci /* caller sets the BN_R_NO_INVERSE error */ 1791cb0ef41Sopenharmony_ci goto err; 1801cb0ef41Sopenharmony_ci } 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci ret = R; 1831cb0ef41Sopenharmony_ci *pnoinv = 0; 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci err: 1861cb0ef41Sopenharmony_ci if ((ret == NULL) && (in == NULL)) 1871cb0ef41Sopenharmony_ci BN_free(R); 1881cb0ef41Sopenharmony_ci BN_CTX_end(ctx); 1891cb0ef41Sopenharmony_ci bn_check_top(ret); 1901cb0ef41Sopenharmony_ci return ret; 1911cb0ef41Sopenharmony_ci} 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci/* 1941cb0ef41Sopenharmony_ci * This is an internal function, we assume all callers pass valid arguments: 1951cb0ef41Sopenharmony_ci * all pointers passed here are assumed non-NULL. 1961cb0ef41Sopenharmony_ci */ 1971cb0ef41Sopenharmony_ciBIGNUM *int_bn_mod_inverse(BIGNUM *in, 1981cb0ef41Sopenharmony_ci const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, 1991cb0ef41Sopenharmony_ci int *pnoinv) 2001cb0ef41Sopenharmony_ci{ 2011cb0ef41Sopenharmony_ci BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; 2021cb0ef41Sopenharmony_ci BIGNUM *ret = NULL; 2031cb0ef41Sopenharmony_ci int sign; 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci /* This is invalid input so we don't worry about constant time here */ 2061cb0ef41Sopenharmony_ci if (BN_abs_is_word(n, 1) || BN_is_zero(n)) { 2071cb0ef41Sopenharmony_ci *pnoinv = 1; 2081cb0ef41Sopenharmony_ci return NULL; 2091cb0ef41Sopenharmony_ci } 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci *pnoinv = 0; 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) 2141cb0ef41Sopenharmony_ci || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) { 2151cb0ef41Sopenharmony_ci return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv); 2161cb0ef41Sopenharmony_ci } 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci bn_check_top(a); 2191cb0ef41Sopenharmony_ci bn_check_top(n); 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci BN_CTX_start(ctx); 2221cb0ef41Sopenharmony_ci A = BN_CTX_get(ctx); 2231cb0ef41Sopenharmony_ci B = BN_CTX_get(ctx); 2241cb0ef41Sopenharmony_ci X = BN_CTX_get(ctx); 2251cb0ef41Sopenharmony_ci D = BN_CTX_get(ctx); 2261cb0ef41Sopenharmony_ci M = BN_CTX_get(ctx); 2271cb0ef41Sopenharmony_ci Y = BN_CTX_get(ctx); 2281cb0ef41Sopenharmony_ci T = BN_CTX_get(ctx); 2291cb0ef41Sopenharmony_ci if (T == NULL) 2301cb0ef41Sopenharmony_ci goto err; 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci if (in == NULL) 2331cb0ef41Sopenharmony_ci R = BN_new(); 2341cb0ef41Sopenharmony_ci else 2351cb0ef41Sopenharmony_ci R = in; 2361cb0ef41Sopenharmony_ci if (R == NULL) 2371cb0ef41Sopenharmony_ci goto err; 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci if (!BN_one(X)) 2401cb0ef41Sopenharmony_ci goto err; 2411cb0ef41Sopenharmony_ci BN_zero(Y); 2421cb0ef41Sopenharmony_ci if (BN_copy(B, a) == NULL) 2431cb0ef41Sopenharmony_ci goto err; 2441cb0ef41Sopenharmony_ci if (BN_copy(A, n) == NULL) 2451cb0ef41Sopenharmony_ci goto err; 2461cb0ef41Sopenharmony_ci A->neg = 0; 2471cb0ef41Sopenharmony_ci if (B->neg || (BN_ucmp(B, A) >= 0)) { 2481cb0ef41Sopenharmony_ci if (!BN_nnmod(B, B, A, ctx)) 2491cb0ef41Sopenharmony_ci goto err; 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci sign = -1; 2521cb0ef41Sopenharmony_ci /*- 2531cb0ef41Sopenharmony_ci * From B = a mod |n|, A = |n| it follows that 2541cb0ef41Sopenharmony_ci * 2551cb0ef41Sopenharmony_ci * 0 <= B < A, 2561cb0ef41Sopenharmony_ci * -sign*X*a == B (mod |n|), 2571cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|). 2581cb0ef41Sopenharmony_ci */ 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) { 2611cb0ef41Sopenharmony_ci /* 2621cb0ef41Sopenharmony_ci * Binary inversion algorithm; requires odd modulus. This is faster 2631cb0ef41Sopenharmony_ci * than the general algorithm if the modulus is sufficiently small 2641cb0ef41Sopenharmony_ci * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit 2651cb0ef41Sopenharmony_ci * systems) 2661cb0ef41Sopenharmony_ci */ 2671cb0ef41Sopenharmony_ci int shift; 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci while (!BN_is_zero(B)) { 2701cb0ef41Sopenharmony_ci /*- 2711cb0ef41Sopenharmony_ci * 0 < B < |n|, 2721cb0ef41Sopenharmony_ci * 0 < A <= |n|, 2731cb0ef41Sopenharmony_ci * (1) -sign*X*a == B (mod |n|), 2741cb0ef41Sopenharmony_ci * (2) sign*Y*a == A (mod |n|) 2751cb0ef41Sopenharmony_ci */ 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci /* 2781cb0ef41Sopenharmony_ci * Now divide B by the maximum possible power of two in the 2791cb0ef41Sopenharmony_ci * integers, and divide X by the same value mod |n|. When we're 2801cb0ef41Sopenharmony_ci * done, (1) still holds. 2811cb0ef41Sopenharmony_ci */ 2821cb0ef41Sopenharmony_ci shift = 0; 2831cb0ef41Sopenharmony_ci while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */ 2841cb0ef41Sopenharmony_ci shift++; 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci if (BN_is_odd(X)) { 2871cb0ef41Sopenharmony_ci if (!BN_uadd(X, X, n)) 2881cb0ef41Sopenharmony_ci goto err; 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci /* 2911cb0ef41Sopenharmony_ci * now X is even, so we can easily divide it by two 2921cb0ef41Sopenharmony_ci */ 2931cb0ef41Sopenharmony_ci if (!BN_rshift1(X, X)) 2941cb0ef41Sopenharmony_ci goto err; 2951cb0ef41Sopenharmony_ci } 2961cb0ef41Sopenharmony_ci if (shift > 0) { 2971cb0ef41Sopenharmony_ci if (!BN_rshift(B, B, shift)) 2981cb0ef41Sopenharmony_ci goto err; 2991cb0ef41Sopenharmony_ci } 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci /* 3021cb0ef41Sopenharmony_ci * Same for A and Y. Afterwards, (2) still holds. 3031cb0ef41Sopenharmony_ci */ 3041cb0ef41Sopenharmony_ci shift = 0; 3051cb0ef41Sopenharmony_ci while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */ 3061cb0ef41Sopenharmony_ci shift++; 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci if (BN_is_odd(Y)) { 3091cb0ef41Sopenharmony_ci if (!BN_uadd(Y, Y, n)) 3101cb0ef41Sopenharmony_ci goto err; 3111cb0ef41Sopenharmony_ci } 3121cb0ef41Sopenharmony_ci /* now Y is even */ 3131cb0ef41Sopenharmony_ci if (!BN_rshift1(Y, Y)) 3141cb0ef41Sopenharmony_ci goto err; 3151cb0ef41Sopenharmony_ci } 3161cb0ef41Sopenharmony_ci if (shift > 0) { 3171cb0ef41Sopenharmony_ci if (!BN_rshift(A, A, shift)) 3181cb0ef41Sopenharmony_ci goto err; 3191cb0ef41Sopenharmony_ci } 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci /*- 3221cb0ef41Sopenharmony_ci * We still have (1) and (2). 3231cb0ef41Sopenharmony_ci * Both A and B are odd. 3241cb0ef41Sopenharmony_ci * The following computations ensure that 3251cb0ef41Sopenharmony_ci * 3261cb0ef41Sopenharmony_ci * 0 <= B < |n|, 3271cb0ef41Sopenharmony_ci * 0 < A < |n|, 3281cb0ef41Sopenharmony_ci * (1) -sign*X*a == B (mod |n|), 3291cb0ef41Sopenharmony_ci * (2) sign*Y*a == A (mod |n|), 3301cb0ef41Sopenharmony_ci * 3311cb0ef41Sopenharmony_ci * and that either A or B is even in the next iteration. 3321cb0ef41Sopenharmony_ci */ 3331cb0ef41Sopenharmony_ci if (BN_ucmp(B, A) >= 0) { 3341cb0ef41Sopenharmony_ci /* -sign*(X + Y)*a == B - A (mod |n|) */ 3351cb0ef41Sopenharmony_ci if (!BN_uadd(X, X, Y)) 3361cb0ef41Sopenharmony_ci goto err; 3371cb0ef41Sopenharmony_ci /* 3381cb0ef41Sopenharmony_ci * NB: we could use BN_mod_add_quick(X, X, Y, n), but that 3391cb0ef41Sopenharmony_ci * actually makes the algorithm slower 3401cb0ef41Sopenharmony_ci */ 3411cb0ef41Sopenharmony_ci if (!BN_usub(B, B, A)) 3421cb0ef41Sopenharmony_ci goto err; 3431cb0ef41Sopenharmony_ci } else { 3441cb0ef41Sopenharmony_ci /* sign*(X + Y)*a == A - B (mod |n|) */ 3451cb0ef41Sopenharmony_ci if (!BN_uadd(Y, Y, X)) 3461cb0ef41Sopenharmony_ci goto err; 3471cb0ef41Sopenharmony_ci /* 3481cb0ef41Sopenharmony_ci * as above, BN_mod_add_quick(Y, Y, X, n) would slow things down 3491cb0ef41Sopenharmony_ci */ 3501cb0ef41Sopenharmony_ci if (!BN_usub(A, A, B)) 3511cb0ef41Sopenharmony_ci goto err; 3521cb0ef41Sopenharmony_ci } 3531cb0ef41Sopenharmony_ci } 3541cb0ef41Sopenharmony_ci } else { 3551cb0ef41Sopenharmony_ci /* general inversion algorithm */ 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci while (!BN_is_zero(B)) { 3581cb0ef41Sopenharmony_ci BIGNUM *tmp; 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci /*- 3611cb0ef41Sopenharmony_ci * 0 < B < A, 3621cb0ef41Sopenharmony_ci * (*) -sign*X*a == B (mod |n|), 3631cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|) 3641cb0ef41Sopenharmony_ci */ 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ci /* (D, M) := (A/B, A%B) ... */ 3671cb0ef41Sopenharmony_ci if (BN_num_bits(A) == BN_num_bits(B)) { 3681cb0ef41Sopenharmony_ci if (!BN_one(D)) 3691cb0ef41Sopenharmony_ci goto err; 3701cb0ef41Sopenharmony_ci if (!BN_sub(M, A, B)) 3711cb0ef41Sopenharmony_ci goto err; 3721cb0ef41Sopenharmony_ci } else if (BN_num_bits(A) == BN_num_bits(B) + 1) { 3731cb0ef41Sopenharmony_ci /* A/B is 1, 2, or 3 */ 3741cb0ef41Sopenharmony_ci if (!BN_lshift1(T, B)) 3751cb0ef41Sopenharmony_ci goto err; 3761cb0ef41Sopenharmony_ci if (BN_ucmp(A, T) < 0) { 3771cb0ef41Sopenharmony_ci /* A < 2*B, so D=1 */ 3781cb0ef41Sopenharmony_ci if (!BN_one(D)) 3791cb0ef41Sopenharmony_ci goto err; 3801cb0ef41Sopenharmony_ci if (!BN_sub(M, A, B)) 3811cb0ef41Sopenharmony_ci goto err; 3821cb0ef41Sopenharmony_ci } else { 3831cb0ef41Sopenharmony_ci /* A >= 2*B, so D=2 or D=3 */ 3841cb0ef41Sopenharmony_ci if (!BN_sub(M, A, T)) 3851cb0ef41Sopenharmony_ci goto err; 3861cb0ef41Sopenharmony_ci if (!BN_add(D, T, B)) 3871cb0ef41Sopenharmony_ci goto err; /* use D (:= 3*B) as temp */ 3881cb0ef41Sopenharmony_ci if (BN_ucmp(A, D) < 0) { 3891cb0ef41Sopenharmony_ci /* A < 3*B, so D=2 */ 3901cb0ef41Sopenharmony_ci if (!BN_set_word(D, 2)) 3911cb0ef41Sopenharmony_ci goto err; 3921cb0ef41Sopenharmony_ci /* 3931cb0ef41Sopenharmony_ci * M (= A - 2*B) already has the correct value 3941cb0ef41Sopenharmony_ci */ 3951cb0ef41Sopenharmony_ci } else { 3961cb0ef41Sopenharmony_ci /* only D=3 remains */ 3971cb0ef41Sopenharmony_ci if (!BN_set_word(D, 3)) 3981cb0ef41Sopenharmony_ci goto err; 3991cb0ef41Sopenharmony_ci /* 4001cb0ef41Sopenharmony_ci * currently M = A - 2*B, but we need M = A - 3*B 4011cb0ef41Sopenharmony_ci */ 4021cb0ef41Sopenharmony_ci if (!BN_sub(M, M, B)) 4031cb0ef41Sopenharmony_ci goto err; 4041cb0ef41Sopenharmony_ci } 4051cb0ef41Sopenharmony_ci } 4061cb0ef41Sopenharmony_ci } else { 4071cb0ef41Sopenharmony_ci if (!BN_div(D, M, A, B, ctx)) 4081cb0ef41Sopenharmony_ci goto err; 4091cb0ef41Sopenharmony_ci } 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci /*- 4121cb0ef41Sopenharmony_ci * Now 4131cb0ef41Sopenharmony_ci * A = D*B + M; 4141cb0ef41Sopenharmony_ci * thus we have 4151cb0ef41Sopenharmony_ci * (**) sign*Y*a == D*B + M (mod |n|). 4161cb0ef41Sopenharmony_ci */ 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci tmp = A; /* keep the BIGNUM object, the value does not matter */ 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci /* (A, B) := (B, A mod B) ... */ 4211cb0ef41Sopenharmony_ci A = B; 4221cb0ef41Sopenharmony_ci B = M; 4231cb0ef41Sopenharmony_ci /* ... so we have 0 <= B < A again */ 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci /*- 4261cb0ef41Sopenharmony_ci * Since the former M is now B and the former B is now A, 4271cb0ef41Sopenharmony_ci * (**) translates into 4281cb0ef41Sopenharmony_ci * sign*Y*a == D*A + B (mod |n|), 4291cb0ef41Sopenharmony_ci * i.e. 4301cb0ef41Sopenharmony_ci * sign*Y*a - D*A == B (mod |n|). 4311cb0ef41Sopenharmony_ci * Similarly, (*) translates into 4321cb0ef41Sopenharmony_ci * -sign*X*a == A (mod |n|). 4331cb0ef41Sopenharmony_ci * 4341cb0ef41Sopenharmony_ci * Thus, 4351cb0ef41Sopenharmony_ci * sign*Y*a + D*sign*X*a == B (mod |n|), 4361cb0ef41Sopenharmony_ci * i.e. 4371cb0ef41Sopenharmony_ci * sign*(Y + D*X)*a == B (mod |n|). 4381cb0ef41Sopenharmony_ci * 4391cb0ef41Sopenharmony_ci * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at 4401cb0ef41Sopenharmony_ci * -sign*X*a == B (mod |n|), 4411cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|). 4421cb0ef41Sopenharmony_ci * Note that X and Y stay non-negative all the time. 4431cb0ef41Sopenharmony_ci */ 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_ci /* 4461cb0ef41Sopenharmony_ci * most of the time D is very small, so we can optimize tmp := D*X+Y 4471cb0ef41Sopenharmony_ci */ 4481cb0ef41Sopenharmony_ci if (BN_is_one(D)) { 4491cb0ef41Sopenharmony_ci if (!BN_add(tmp, X, Y)) 4501cb0ef41Sopenharmony_ci goto err; 4511cb0ef41Sopenharmony_ci } else { 4521cb0ef41Sopenharmony_ci if (BN_is_word(D, 2)) { 4531cb0ef41Sopenharmony_ci if (!BN_lshift1(tmp, X)) 4541cb0ef41Sopenharmony_ci goto err; 4551cb0ef41Sopenharmony_ci } else if (BN_is_word(D, 4)) { 4561cb0ef41Sopenharmony_ci if (!BN_lshift(tmp, X, 2)) 4571cb0ef41Sopenharmony_ci goto err; 4581cb0ef41Sopenharmony_ci } else if (D->top == 1) { 4591cb0ef41Sopenharmony_ci if (!BN_copy(tmp, X)) 4601cb0ef41Sopenharmony_ci goto err; 4611cb0ef41Sopenharmony_ci if (!BN_mul_word(tmp, D->d[0])) 4621cb0ef41Sopenharmony_ci goto err; 4631cb0ef41Sopenharmony_ci } else { 4641cb0ef41Sopenharmony_ci if (!BN_mul(tmp, D, X, ctx)) 4651cb0ef41Sopenharmony_ci goto err; 4661cb0ef41Sopenharmony_ci } 4671cb0ef41Sopenharmony_ci if (!BN_add(tmp, tmp, Y)) 4681cb0ef41Sopenharmony_ci goto err; 4691cb0ef41Sopenharmony_ci } 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ci M = Y; /* keep the BIGNUM object, the value does not matter */ 4721cb0ef41Sopenharmony_ci Y = X; 4731cb0ef41Sopenharmony_ci X = tmp; 4741cb0ef41Sopenharmony_ci sign = -sign; 4751cb0ef41Sopenharmony_ci } 4761cb0ef41Sopenharmony_ci } 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci /*- 4791cb0ef41Sopenharmony_ci * The while loop (Euclid's algorithm) ends when 4801cb0ef41Sopenharmony_ci * A == gcd(a,n); 4811cb0ef41Sopenharmony_ci * we have 4821cb0ef41Sopenharmony_ci * sign*Y*a == A (mod |n|), 4831cb0ef41Sopenharmony_ci * where Y is non-negative. 4841cb0ef41Sopenharmony_ci */ 4851cb0ef41Sopenharmony_ci 4861cb0ef41Sopenharmony_ci if (sign < 0) { 4871cb0ef41Sopenharmony_ci if (!BN_sub(Y, n, Y)) 4881cb0ef41Sopenharmony_ci goto err; 4891cb0ef41Sopenharmony_ci } 4901cb0ef41Sopenharmony_ci /* Now Y*a == A (mod |n|). */ 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci if (BN_is_one(A)) { 4931cb0ef41Sopenharmony_ci /* Y*a == 1 (mod |n|) */ 4941cb0ef41Sopenharmony_ci if (!Y->neg && BN_ucmp(Y, n) < 0) { 4951cb0ef41Sopenharmony_ci if (!BN_copy(R, Y)) 4961cb0ef41Sopenharmony_ci goto err; 4971cb0ef41Sopenharmony_ci } else { 4981cb0ef41Sopenharmony_ci if (!BN_nnmod(R, Y, n, ctx)) 4991cb0ef41Sopenharmony_ci goto err; 5001cb0ef41Sopenharmony_ci } 5011cb0ef41Sopenharmony_ci } else { 5021cb0ef41Sopenharmony_ci *pnoinv = 1; 5031cb0ef41Sopenharmony_ci goto err; 5041cb0ef41Sopenharmony_ci } 5051cb0ef41Sopenharmony_ci ret = R; 5061cb0ef41Sopenharmony_ci err: 5071cb0ef41Sopenharmony_ci if ((ret == NULL) && (in == NULL)) 5081cb0ef41Sopenharmony_ci BN_free(R); 5091cb0ef41Sopenharmony_ci BN_CTX_end(ctx); 5101cb0ef41Sopenharmony_ci bn_check_top(ret); 5111cb0ef41Sopenharmony_ci return ret; 5121cb0ef41Sopenharmony_ci} 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci/* solves ax == 1 (mod n) */ 5151cb0ef41Sopenharmony_ciBIGNUM *BN_mod_inverse(BIGNUM *in, 5161cb0ef41Sopenharmony_ci const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) 5171cb0ef41Sopenharmony_ci{ 5181cb0ef41Sopenharmony_ci BN_CTX *new_ctx = NULL; 5191cb0ef41Sopenharmony_ci BIGNUM *rv; 5201cb0ef41Sopenharmony_ci int noinv = 0; 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci if (ctx == NULL) { 5231cb0ef41Sopenharmony_ci ctx = new_ctx = BN_CTX_new_ex(NULL); 5241cb0ef41Sopenharmony_ci if (ctx == NULL) { 5251cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); 5261cb0ef41Sopenharmony_ci return NULL; 5271cb0ef41Sopenharmony_ci } 5281cb0ef41Sopenharmony_ci } 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci rv = int_bn_mod_inverse(in, a, n, ctx, &noinv); 5311cb0ef41Sopenharmony_ci if (noinv) 5321cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_BN, BN_R_NO_INVERSE); 5331cb0ef41Sopenharmony_ci BN_CTX_free(new_ctx); 5341cb0ef41Sopenharmony_ci return rv; 5351cb0ef41Sopenharmony_ci} 5361cb0ef41Sopenharmony_ci 5371cb0ef41Sopenharmony_ci/*- 5381cb0ef41Sopenharmony_ci * This function is based on the constant-time GCD work by Bernstein and Yang: 5391cb0ef41Sopenharmony_ci * https://eprint.iacr.org/2019/266 5401cb0ef41Sopenharmony_ci * Generalized fast GCD function to allow even inputs. 5411cb0ef41Sopenharmony_ci * The algorithm first finds the shared powers of 2 between 5421cb0ef41Sopenharmony_ci * the inputs, and removes them, reducing at least one of the 5431cb0ef41Sopenharmony_ci * inputs to an odd value. Then it proceeds to calculate the GCD. 5441cb0ef41Sopenharmony_ci * Before returning the resulting GCD, we take care of adding 5451cb0ef41Sopenharmony_ci * back the powers of two removed at the beginning. 5461cb0ef41Sopenharmony_ci * Note 1: we assume the bit length of both inputs is public information, 5471cb0ef41Sopenharmony_ci * since access to top potentially leaks this information. 5481cb0ef41Sopenharmony_ci */ 5491cb0ef41Sopenharmony_ciint BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) 5501cb0ef41Sopenharmony_ci{ 5511cb0ef41Sopenharmony_ci BIGNUM *g, *temp = NULL; 5521cb0ef41Sopenharmony_ci BN_ULONG mask = 0; 5531cb0ef41Sopenharmony_ci int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0; 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ci /* Note 2: zero input corner cases are not constant-time since they are 5561cb0ef41Sopenharmony_ci * handled immediately. An attacker can run an attack under this 5571cb0ef41Sopenharmony_ci * assumption without the need of side-channel information. */ 5581cb0ef41Sopenharmony_ci if (BN_is_zero(in_b)) { 5591cb0ef41Sopenharmony_ci ret = BN_copy(r, in_a) != NULL; 5601cb0ef41Sopenharmony_ci r->neg = 0; 5611cb0ef41Sopenharmony_ci return ret; 5621cb0ef41Sopenharmony_ci } 5631cb0ef41Sopenharmony_ci if (BN_is_zero(in_a)) { 5641cb0ef41Sopenharmony_ci ret = BN_copy(r, in_b) != NULL; 5651cb0ef41Sopenharmony_ci r->neg = 0; 5661cb0ef41Sopenharmony_ci return ret; 5671cb0ef41Sopenharmony_ci } 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci bn_check_top(in_a); 5701cb0ef41Sopenharmony_ci bn_check_top(in_b); 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci BN_CTX_start(ctx); 5731cb0ef41Sopenharmony_ci temp = BN_CTX_get(ctx); 5741cb0ef41Sopenharmony_ci g = BN_CTX_get(ctx); 5751cb0ef41Sopenharmony_ci 5761cb0ef41Sopenharmony_ci /* make r != 0, g != 0 even, so BN_rshift is not a potential nop */ 5771cb0ef41Sopenharmony_ci if (g == NULL 5781cb0ef41Sopenharmony_ci || !BN_lshift1(g, in_b) 5791cb0ef41Sopenharmony_ci || !BN_lshift1(r, in_a)) 5801cb0ef41Sopenharmony_ci goto err; 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_ci /* find shared powers of two, i.e. "shifts" >= 1 */ 5831cb0ef41Sopenharmony_ci for (i = 0; i < r->dmax && i < g->dmax; i++) { 5841cb0ef41Sopenharmony_ci mask = ~(r->d[i] | g->d[i]); 5851cb0ef41Sopenharmony_ci for (j = 0; j < BN_BITS2; j++) { 5861cb0ef41Sopenharmony_ci bit &= mask; 5871cb0ef41Sopenharmony_ci shifts += bit; 5881cb0ef41Sopenharmony_ci mask >>= 1; 5891cb0ef41Sopenharmony_ci } 5901cb0ef41Sopenharmony_ci } 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci /* subtract shared powers of two; shifts >= 1 */ 5931cb0ef41Sopenharmony_ci if (!BN_rshift(r, r, shifts) 5941cb0ef41Sopenharmony_ci || !BN_rshift(g, g, shifts)) 5951cb0ef41Sopenharmony_ci goto err; 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci /* expand to biggest nword, with room for a possible extra word */ 5981cb0ef41Sopenharmony_ci top = 1 + ((r->top >= g->top) ? r->top : g->top); 5991cb0ef41Sopenharmony_ci if (bn_wexpand(r, top) == NULL 6001cb0ef41Sopenharmony_ci || bn_wexpand(g, top) == NULL 6011cb0ef41Sopenharmony_ci || bn_wexpand(temp, top) == NULL) 6021cb0ef41Sopenharmony_ci goto err; 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci /* re arrange inputs s.t. r is odd */ 6051cb0ef41Sopenharmony_ci BN_consttime_swap((~r->d[0]) & 1, r, g, top); 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ci /* compute the number of iterations */ 6081cb0ef41Sopenharmony_ci rlen = BN_num_bits(r); 6091cb0ef41Sopenharmony_ci glen = BN_num_bits(g); 6101cb0ef41Sopenharmony_ci m = 4 + 3 * ((rlen >= glen) ? rlen : glen); 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci for (i = 0; i < m; i++) { 6131cb0ef41Sopenharmony_ci /* conditionally flip signs if delta is positive and g is odd */ 6141cb0ef41Sopenharmony_ci cond = ((unsigned int)-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1 6151cb0ef41Sopenharmony_ci /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ 6161cb0ef41Sopenharmony_ci & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))); 6171cb0ef41Sopenharmony_ci delta = (-cond & -delta) | ((cond - 1) & delta); 6181cb0ef41Sopenharmony_ci r->neg ^= cond; 6191cb0ef41Sopenharmony_ci /* swap */ 6201cb0ef41Sopenharmony_ci BN_consttime_swap(cond, r, g, top); 6211cb0ef41Sopenharmony_ci 6221cb0ef41Sopenharmony_ci /* elimination step */ 6231cb0ef41Sopenharmony_ci delta++; 6241cb0ef41Sopenharmony_ci if (!BN_add(temp, g, r)) 6251cb0ef41Sopenharmony_ci goto err; 6261cb0ef41Sopenharmony_ci BN_consttime_swap(g->d[0] & 1 /* g is odd */ 6271cb0ef41Sopenharmony_ci /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ 6281cb0ef41Sopenharmony_ci & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))), 6291cb0ef41Sopenharmony_ci g, temp, top); 6301cb0ef41Sopenharmony_ci if (!BN_rshift1(g, g)) 6311cb0ef41Sopenharmony_ci goto err; 6321cb0ef41Sopenharmony_ci } 6331cb0ef41Sopenharmony_ci 6341cb0ef41Sopenharmony_ci /* remove possible negative sign */ 6351cb0ef41Sopenharmony_ci r->neg = 0; 6361cb0ef41Sopenharmony_ci /* add powers of 2 removed, then correct the artificial shift */ 6371cb0ef41Sopenharmony_ci if (!BN_lshift(r, r, shifts) 6381cb0ef41Sopenharmony_ci || !BN_rshift1(r, r)) 6391cb0ef41Sopenharmony_ci goto err; 6401cb0ef41Sopenharmony_ci 6411cb0ef41Sopenharmony_ci ret = 1; 6421cb0ef41Sopenharmony_ci 6431cb0ef41Sopenharmony_ci err: 6441cb0ef41Sopenharmony_ci BN_CTX_end(ctx); 6451cb0ef41Sopenharmony_ci bn_check_top(r); 6461cb0ef41Sopenharmony_ci return ret; 6471cb0ef41Sopenharmony_ci} 648