| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_exp2.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||||||||||||||
| 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||||||||||||||
| 3 | * | - | ||||||||||||||||||||||||
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use | - | ||||||||||||||||||||||||
| 5 | * this file except in compliance with the License. You can obtain a copy | - | ||||||||||||||||||||||||
| 6 | * in the file LICENSE in the source distribution or at | - | ||||||||||||||||||||||||
| 7 | * https://www.openssl.org/source/license.html | - | ||||||||||||||||||||||||
| 8 | */ | - | ||||||||||||||||||||||||
| 9 | - | |||||||||||||||||||||||||
| 10 | #include <stdio.h> | - | ||||||||||||||||||||||||
| 11 | #include "internal/cryptlib.h" | - | ||||||||||||||||||||||||
| 12 | #include "bn_lcl.h" | - | ||||||||||||||||||||||||
| 13 | - | |||||||||||||||||||||||||
| 14 | #define TABLE_SIZE 32 | - | ||||||||||||||||||||||||
| 15 | - | |||||||||||||||||||||||||
| 16 | int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1, | - | ||||||||||||||||||||||||
| 17 | const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, | - | ||||||||||||||||||||||||
| 18 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | - | ||||||||||||||||||||||||
| 19 | { | - | ||||||||||||||||||||||||
| 20 | int i, j, bits, b, bits1, bits2, ret = | - | ||||||||||||||||||||||||
| 21 | 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2; | - | ||||||||||||||||||||||||
| 22 | int r_is_one = 1; | - | ||||||||||||||||||||||||
| 23 | BIGNUM *d, *r; | - | ||||||||||||||||||||||||
| 24 | const BIGNUM *a_mod_m; | - | ||||||||||||||||||||||||
| 25 | /* Tables of variables obtained from 'ctx' */ | - | ||||||||||||||||||||||||
| 26 | BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE]; | - | ||||||||||||||||||||||||
| 27 | BN_MONT_CTX *mont = NULL; | - | ||||||||||||||||||||||||
| 28 | - | |||||||||||||||||||||||||
| 29 | bn_check_top(a1); | - | ||||||||||||||||||||||||
| 30 | bn_check_top(p1); | - | ||||||||||||||||||||||||
| 31 | bn_check_top(a2); | - | ||||||||||||||||||||||||
| 32 | bn_check_top(p2); | - | ||||||||||||||||||||||||
| 33 | bn_check_top(m); | - | ||||||||||||||||||||||||
| 34 | - | |||||||||||||||||||||||||
| 35 | if (!(m->d[0] & 1)) {
| 0-283 | ||||||||||||||||||||||||
| 36 | BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS); | - | ||||||||||||||||||||||||
| 37 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
| 38 | } | - | ||||||||||||||||||||||||
| 39 | bits1 = BN_num_bits(p1); | - | ||||||||||||||||||||||||
| 40 | bits2 = BN_num_bits(p2); | - | ||||||||||||||||||||||||
| 41 | if ((bits1 == 0) && (bits2 == 0)) {
| 0-283 | ||||||||||||||||||||||||
| 42 | ret = BN_one(rr); | - | ||||||||||||||||||||||||
| 43 | return ret; never executed: return ret; | 0 | ||||||||||||||||||||||||
| 44 | } | - | ||||||||||||||||||||||||
| 45 | - | |||||||||||||||||||||||||
| 46 | bits = (bits1 > bits2) ? bits1 : bits2;
| 88-195 | ||||||||||||||||||||||||
| 47 | - | |||||||||||||||||||||||||
| 48 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||
| 49 | d = BN_CTX_get(ctx); | - | ||||||||||||||||||||||||
| 50 | r = BN_CTX_get(ctx); | - | ||||||||||||||||||||||||
| 51 | val1[0] = BN_CTX_get(ctx); | - | ||||||||||||||||||||||||
| 52 | val2[0] = BN_CTX_get(ctx); | - | ||||||||||||||||||||||||
| 53 | if (val2[0] == NULL)
| 0-283 | ||||||||||||||||||||||||
| 54 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 55 | - | |||||||||||||||||||||||||
| 56 | if (in_mont != NULL)
| 0-283 | ||||||||||||||||||||||||
| 57 | mont = in_mont; executed 283 times by 1 test: mont = in_mont;Executed by:
| 283 | ||||||||||||||||||||||||
| 58 | else { | - | ||||||||||||||||||||||||
| 59 | if ((mont = BN_MONT_CTX_new()) == NULL)
| 0 | ||||||||||||||||||||||||
| 60 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 61 | if (!BN_MONT_CTX_set(mont, m, ctx))
| 0 | ||||||||||||||||||||||||
| 62 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 63 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 64 | - | |||||||||||||||||||||||||
| 65 | window1 = BN_window_bits_for_exponent_size(bits1);
| 0-283 | ||||||||||||||||||||||||
| 66 | window2 = BN_window_bits_for_exponent_size(bits2);
| 0-283 | ||||||||||||||||||||||||
| 67 | - | |||||||||||||||||||||||||
| 68 | /* | - | ||||||||||||||||||||||||
| 69 | * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1) | - | ||||||||||||||||||||||||
| 70 | */ | - | ||||||||||||||||||||||||
| 71 | if (a1->neg || BN_ucmp(a1, m) >= 0) {
| 0-283 | ||||||||||||||||||||||||
| 72 | if (!BN_mod(val1[0], a1, m, ctx))
| 0-120 | ||||||||||||||||||||||||
| 73 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 74 | a_mod_m = val1[0]; | - | ||||||||||||||||||||||||
| 75 | } else executed 120 times by 1 test: end of blockExecuted by:
| 120 | ||||||||||||||||||||||||
| 76 | a_mod_m = a1; executed 163 times by 1 test: a_mod_m = a1;Executed by:
| 163 | ||||||||||||||||||||||||
| 77 | if (BN_is_zero(a_mod_m)) {
| 1-282 | ||||||||||||||||||||||||
| 78 | BN_zero(rr); | - | ||||||||||||||||||||||||
| 79 | ret = 1; | - | ||||||||||||||||||||||||
| 80 | goto err; executed 1 time by 1 test: goto err;Executed by:
| 1 | ||||||||||||||||||||||||
| 81 | } | - | ||||||||||||||||||||||||
| 82 | - | |||||||||||||||||||||||||
| 83 | if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
| 0-282 | ||||||||||||||||||||||||
| 84 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 85 | if (window1 > 1) {
| 0-282 | ||||||||||||||||||||||||
| 86 | if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
| 0-282 | ||||||||||||||||||||||||
| 87 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 88 | - | |||||||||||||||||||||||||
| 89 | j = 1 << (window1 - 1); | - | ||||||||||||||||||||||||
| 90 | for (i = 1; i < j; i++) {
| 282-2494 | ||||||||||||||||||||||||
| 91 | if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||
| 0-2494 | ||||||||||||||||||||||||
| 92 | !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
| 0-2494 | ||||||||||||||||||||||||
| 93 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 94 | } executed 2494 times by 1 test: end of blockExecuted by:
| 2494 | ||||||||||||||||||||||||
| 95 | } executed 282 times by 1 test: end of blockExecuted by:
| 282 | ||||||||||||||||||||||||
| 96 | - | |||||||||||||||||||||||||
| 97 | /* | - | ||||||||||||||||||||||||
| 98 | * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1) | - | ||||||||||||||||||||||||
| 99 | */ | - | ||||||||||||||||||||||||
| 100 | if (a2->neg || BN_ucmp(a2, m) >= 0) {
| 1-208 | ||||||||||||||||||||||||
| 101 | if (!BN_mod(val2[0], a2, m, ctx))
| 0-75 | ||||||||||||||||||||||||
| 102 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 103 | a_mod_m = val2[0]; | - | ||||||||||||||||||||||||
| 104 | } else executed 75 times by 1 test: end of blockExecuted by:
| 75 | ||||||||||||||||||||||||
| 105 | a_mod_m = a2; executed 207 times by 1 test: a_mod_m = a2;Executed by:
| 207 | ||||||||||||||||||||||||
| 106 | if (BN_is_zero(a_mod_m)) {
| 2-280 | ||||||||||||||||||||||||
| 107 | BN_zero(rr); | - | ||||||||||||||||||||||||
| 108 | ret = 1; | - | ||||||||||||||||||||||||
| 109 | goto err; executed 2 times by 1 test: goto err;Executed by:
| 2 | ||||||||||||||||||||||||
| 110 | } | - | ||||||||||||||||||||||||
| 111 | if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
| 0-280 | ||||||||||||||||||||||||
| 112 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 113 | if (window2 > 1) {
| 4-276 | ||||||||||||||||||||||||
| 114 | if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
| 0-276 | ||||||||||||||||||||||||
| 115 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 116 | - | |||||||||||||||||||||||||
| 117 | j = 1 << (window2 - 1); | - | ||||||||||||||||||||||||
| 118 | for (i = 1; i < j; i++) {
| 276-2448 | ||||||||||||||||||||||||
| 119 | if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||
| 0-2448 | ||||||||||||||||||||||||
| 120 | !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
| 0-2448 | ||||||||||||||||||||||||
| 121 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 122 | } executed 2448 times by 1 test: end of blockExecuted by:
| 2448 | ||||||||||||||||||||||||
| 123 | } executed 276 times by 1 test: end of blockExecuted by:
| 276 | ||||||||||||||||||||||||
| 124 | - | |||||||||||||||||||||||||
| 125 | /* Now compute the power product, using independent windows. */ | - | ||||||||||||||||||||||||
| 126 | r_is_one = 1; | - | ||||||||||||||||||||||||
| 127 | wvalue1 = 0; /* The 'value' of the first window */ | - | ||||||||||||||||||||||||
| 128 | wvalue2 = 0; /* The 'value' of the second window */ | - | ||||||||||||||||||||||||
| 129 | wpos1 = 0; /* If wvalue1 > 0, the bottom bit of the | - | ||||||||||||||||||||||||
| 130 | * first window */ | - | ||||||||||||||||||||||||
| 131 | wpos2 = 0; /* If wvalue2 > 0, the bottom bit of the | - | ||||||||||||||||||||||||
| 132 | * second window */ | - | ||||||||||||||||||||||||
| 133 | - | |||||||||||||||||||||||||
| 134 | if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
| 0-280 | ||||||||||||||||||||||||
| 135 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 136 | for (b = bits - 1; b >= 0; b--) {
| 280-63046 | ||||||||||||||||||||||||
| 137 | if (!r_is_one) {
| 769-62277 | ||||||||||||||||||||||||
| 138 | if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
| 0-62277 | ||||||||||||||||||||||||
| 139 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 140 | } executed 62277 times by 1 test: end of blockExecuted by:
| 62277 | ||||||||||||||||||||||||
| 141 | - | |||||||||||||||||||||||||
| 142 | if (!wvalue1)
| 27678-35368 | ||||||||||||||||||||||||
| 143 | if (BN_is_bit_set(p1, b)) {
| 12021-23347 | ||||||||||||||||||||||||
| 144 | /* | - | ||||||||||||||||||||||||
| 145 | * consider bits b-window1+1 .. b for this window | - | ||||||||||||||||||||||||
| 146 | */ | - | ||||||||||||||||||||||||
| 147 | i = b - window1 + 1; | - | ||||||||||||||||||||||||
| 148 | while (!BN_is_bit_set(p1, i)) /* works for i<0 */
| 11171-12021 | ||||||||||||||||||||||||
| 149 | i++; executed 11171 times by 1 test: i++;Executed by:
| 11171 | ||||||||||||||||||||||||
| 150 | wpos1 = i; | - | ||||||||||||||||||||||||
| 151 | wvalue1 = 1; | - | ||||||||||||||||||||||||
| 152 | for (i = b - 1; i >= wpos1; i--) {
| 12021-27678 | ||||||||||||||||||||||||
| 153 | wvalue1 <<= 1; | - | ||||||||||||||||||||||||
| 154 | if (BN_is_bit_set(p1, i))
| 8434-19244 | ||||||||||||||||||||||||
| 155 | wvalue1++; executed 19244 times by 1 test: wvalue1++;Executed by:
| 19244 | ||||||||||||||||||||||||
| 156 | } executed 27678 times by 1 test: end of blockExecuted by:
| 27678 | ||||||||||||||||||||||||
| 157 | } executed 12021 times by 1 test: end of blockExecuted by:
| 12021 | ||||||||||||||||||||||||
| 158 | - | |||||||||||||||||||||||||
| 159 | if (!wvalue2)
| 27208-35838 | ||||||||||||||||||||||||
| 160 | if (BN_is_bit_set(p2, b)) {
| 11811-24027 | ||||||||||||||||||||||||
| 161 | /* | - | ||||||||||||||||||||||||
| 162 | * consider bits b-window2+1 .. b for this window | - | ||||||||||||||||||||||||
| 163 | */ | - | ||||||||||||||||||||||||
| 164 | i = b - window2 + 1; | - | ||||||||||||||||||||||||
| 165 | while (!BN_is_bit_set(p2, i))
| 10992-11811 | ||||||||||||||||||||||||
| 166 | i++; executed 10992 times by 1 test: i++;Executed by:
| 10992 | ||||||||||||||||||||||||
| 167 | wpos2 = i; | - | ||||||||||||||||||||||||
| 168 | wvalue2 = 1; | - | ||||||||||||||||||||||||
| 169 | for (i = b - 1; i >= wpos2; i--) {
| 11811-27208 | ||||||||||||||||||||||||
| 170 | wvalue2 <<= 1; | - | ||||||||||||||||||||||||
| 171 | if (BN_is_bit_set(p2, i))
| 8612-18596 | ||||||||||||||||||||||||
| 172 | wvalue2++; executed 18596 times by 1 test: wvalue2++;Executed by:
| 18596 | ||||||||||||||||||||||||
| 173 | } executed 27208 times by 1 test: end of blockExecuted by:
| 27208 | ||||||||||||||||||||||||
| 174 | } executed 11811 times by 1 test: end of blockExecuted by:
| 11811 | ||||||||||||||||||||||||
| 175 | - | |||||||||||||||||||||||||
| 176 | if (wvalue1 && b == wpos1) {
| 12021-39699 | ||||||||||||||||||||||||
| 177 | /* wvalue1 is odd and < 2^window1 */ | - | ||||||||||||||||||||||||
| 178 | if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
| 0-12021 | ||||||||||||||||||||||||
| 179 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 180 | wvalue1 = 0; | - | ||||||||||||||||||||||||
| 181 | r_is_one = 0; | - | ||||||||||||||||||||||||
| 182 | } executed 12021 times by 1 test: end of blockExecuted by:
| 12021 | ||||||||||||||||||||||||
| 183 | - | |||||||||||||||||||||||||
| 184 | if (wvalue2 && b == wpos2) {
| 11811-39019 | ||||||||||||||||||||||||
| 185 | /* wvalue2 is odd and < 2^window2 */ | - | ||||||||||||||||||||||||
| 186 | if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
| 0-11811 | ||||||||||||||||||||||||
| 187 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 188 | wvalue2 = 0; | - | ||||||||||||||||||||||||
| 189 | r_is_one = 0; | - | ||||||||||||||||||||||||
| 190 | } executed 11811 times by 1 test: end of blockExecuted by:
| 11811 | ||||||||||||||||||||||||
| 191 | } executed 63046 times by 1 test: end of blockExecuted by:
| 63046 | ||||||||||||||||||||||||
| 192 | if (!BN_from_montgomery(rr, r, mont, ctx))
| 0-280 | ||||||||||||||||||||||||
| 193 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 194 | ret = 1; | - | ||||||||||||||||||||||||
| 195 | err: code before this statement executed 280 times by 1 test: err:Executed by:
| 280 | ||||||||||||||||||||||||
| 196 | if (in_mont == NULL)
| 0-283 | ||||||||||||||||||||||||
| 197 | BN_MONT_CTX_free(mont); never executed: BN_MONT_CTX_free(mont); | 0 | ||||||||||||||||||||||||
| 198 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||
| 199 | bn_check_top(rr); | - | ||||||||||||||||||||||||
| 200 | return ret; executed 283 times by 1 test: return ret;Executed by:
| 283 | ||||||||||||||||||||||||
| 201 | } | - | ||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |