OpenCoverage

bn_exp2.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_exp2.c
Source codeSwitch to Preprocessed file
LineSourceCount
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-
16int 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)) {
!(m->d[0] & 1)Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.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)) {
(bits1 == 0)Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits2 == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0-283
42 ret = BN_one(rr);-
43 return ret;
never executed: return ret;
0
44 }-
45-
46 bits = (bits1 > bits2) ? bits1 : bits2;
(bits1 > bits2)Description
TRUEevaluated 88 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 195 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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)
val2[0] == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-283
54 goto err;
never executed: goto err;
0
55-
56 if (in_mont != NULL)
in_mont != ((void *)0)Description
TRUEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-283
57 mont = in_mont;
executed 283 times by 1 test: mont = in_mont;
Executed by:
  • libcrypto.so.1.1
283
58 else {-
59 if ((mont = BN_MONT_CTX_new()) == NULL)
(mont = BN_MON...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
60 goto err;
never executed: goto err;
0
61 if (!BN_MONT_CTX_set(mont, m, ctx))
!BN_MONT_CTX_set(mont, m, ctx)Description
TRUEnever evaluated
FALSEnever evaluated
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);
(bits1) > 671Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits1) > 239Description
TRUEevaluated 65 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits1) > 79Description
TRUEevaluated 218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
(bits1) > 23Description
TRUEnever evaluated
FALSEnever evaluated
0-283
66 window2 = BN_window_bits_for_exponent_size(bits2);
(bits2) > 671Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits2) > 239Description
TRUEevaluated 65 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits2) > 79Description
TRUEevaluated 213 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 5 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(bits2) > 23Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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) {
a1->negDescription
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
BN_ucmp(a1, m) >= 0Description
TRUEevaluated 120 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 163 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-283
72 if (!BN_mod(val1[0], a1, m, ctx))
!BN_div( ((voi...a1),(m),(ctx))Description
TRUEnever evaluated
FALSEevaluated 120 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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 block
Executed by:
  • libcrypto.so.1.1
120
76 a_mod_m = a1;
executed 163 times by 1 test: a_mod_m = a1;
Executed by:
  • libcrypto.so.1.1
163
77 if (BN_is_zero(a_mod_m)) {
BN_is_zero(a_mod_m)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-282
78 BN_zero(rr);-
79 ret = 1;-
80 goto err;
executed 1 time by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
1
81 }-
82-
83 if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
!BN_to_montgom..._m, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-282
84 goto err;
never executed: goto err;
0
85 if (window1 > 1) {
window1 > 1Description
TRUEevaluated 282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-282
86 if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
!BN_mod_mul_mo...0], mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-282
87 goto err;
never executed: goto err;
0
88-
89 j = 1 << (window1 - 1);-
90 for (i = 1; i < j; i++) {
i < jDescription
TRUEevaluated 2494 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
282-2494
91 if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||
((val1[i] = BN... ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 2494 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-2494
92 !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
!BN_mod_mul_mo... d, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 2494 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-2494
93 goto err;
never executed: goto err;
0
94 }
executed 2494 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
2494
95 }
executed 282 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
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) {
a2->negDescription
TRUEevaluated 74 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 208 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
BN_ucmp(a2, m) >= 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-208
101 if (!BN_mod(val2[0], a2, m, ctx))
!BN_div( ((voi...a2),(m),(ctx))Description
TRUEnever evaluated
FALSEevaluated 75 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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 block
Executed by:
  • libcrypto.so.1.1
75
105 a_mod_m = a2;
executed 207 times by 1 test: a_mod_m = a2;
Executed by:
  • libcrypto.so.1.1
207
106 if (BN_is_zero(a_mod_m)) {
BN_is_zero(a_mod_m)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 280 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
2-280
107 BN_zero(rr);-
108 ret = 1;-
109 goto err;
executed 2 times by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
2
110 }-
111 if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
!BN_to_montgom..._m, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 280 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-280
112 goto err;
never executed: goto err;
0
113 if (window2 > 1) {
window2 > 1Description
TRUEevaluated 276 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
4-276
114 if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
!BN_mod_mul_mo...0], mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 276 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-276
115 goto err;
never executed: goto err;
0
116-
117 j = 1 << (window2 - 1);-
118 for (i = 1; i < j; i++) {
i < jDescription
TRUEevaluated 2448 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 276 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
276-2448
119 if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||
((val2[i] = BN... ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 2448 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-2448
120 !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
!BN_mod_mul_mo... d, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 2448 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-2448
121 goto err;
never executed: goto err;
0
122 }
executed 2448 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
2448
123 }
executed 276 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
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))
!BN_to_montgom...(), mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 280 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-280
135 goto err;
never executed: goto err;
0
136 for (b = bits - 1; b >= 0; b--) {
b >= 0Description
TRUEevaluated 63046 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 280 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
280-63046
137 if (!r_is_one) {
!r_is_oneDescription
TRUEevaluated 62277 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 769 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
769-62277
138 if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
!BN_mod_mul_mo... r, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 62277 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-62277
139 goto err;
never executed: goto err;
0
140 }
executed 62277 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
62277
141-
142 if (!wvalue1)
!wvalue1Description
TRUEevaluated 35368 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 27678 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
27678-35368
143 if (BN_is_bit_set(p1, b)) {
BN_is_bit_set(p1, b)Description
TRUEevaluated 12021 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 23347 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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 */
!BN_is_bit_set(p1, i)Description
TRUEevaluated 11171 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12021 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
11171-12021
149 i++;
executed 11171 times by 1 test: i++;
Executed by:
  • libcrypto.so.1.1
11171
150 wpos1 = i;-
151 wvalue1 = 1;-
152 for (i = b - 1; i >= wpos1; i--) {
i >= wpos1Description
TRUEevaluated 27678 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12021 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12021-27678
153 wvalue1 <<= 1;-
154 if (BN_is_bit_set(p1, i))
BN_is_bit_set(p1, i)Description
TRUEevaluated 19244 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 8434 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
8434-19244
155 wvalue1++;
executed 19244 times by 1 test: wvalue1++;
Executed by:
  • libcrypto.so.1.1
19244
156 }
executed 27678 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
27678
157 }
executed 12021 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
12021
158-
159 if (!wvalue2)
!wvalue2Description
TRUEevaluated 35838 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 27208 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
27208-35838
160 if (BN_is_bit_set(p2, b)) {
BN_is_bit_set(p2, b)Description
TRUEevaluated 11811 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 24027 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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))
!BN_is_bit_set(p2, i)Description
TRUEevaluated 10992 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 11811 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
10992-11811
166 i++;
executed 10992 times by 1 test: i++;
Executed by:
  • libcrypto.so.1.1
10992
167 wpos2 = i;-
168 wvalue2 = 1;-
169 for (i = b - 1; i >= wpos2; i--) {
i >= wpos2Description
TRUEevaluated 27208 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 11811 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
11811-27208
170 wvalue2 <<= 1;-
171 if (BN_is_bit_set(p2, i))
BN_is_bit_set(p2, i)Description
TRUEevaluated 18596 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 8612 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
8612-18596
172 wvalue2++;
executed 18596 times by 1 test: wvalue2++;
Executed by:
  • libcrypto.so.1.1
18596
173 }
executed 27208 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
27208
174 }
executed 11811 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
11811
175-
176 if (wvalue1 && b == wpos1) {
wvalue1Description
TRUEevaluated 39699 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 23347 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
b == wpos1Description
TRUEevaluated 12021 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 27678 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12021-39699
177 /* wvalue1 is odd and < 2^window1 */-
178 if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
!BN_mod_mul_mo...1], mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 12021 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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 block
Executed by:
  • libcrypto.so.1.1
12021
183-
184 if (wvalue2 && b == wpos2) {
wvalue2Description
TRUEevaluated 39019 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 24027 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
b == wpos2Description
TRUEevaluated 11811 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 27208 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
11811-39019
185 /* wvalue2 is odd and < 2^window2 */-
186 if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
!BN_mod_mul_mo...1], mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 11811 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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 block
Executed by:
  • libcrypto.so.1.1
11811
191 }
executed 63046 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
63046
192 if (!BN_from_montgomery(rr, r, mont, ctx))
!BN_from_montg... r, mont, ctx)Description
TRUEnever evaluated
FALSEevaluated 280 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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:
  • libcrypto.so.1.1
280
196 if (in_mont == NULL)
in_mont == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 283 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
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:
  • libcrypto.so.1.1
283
201}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2