Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | | - |
41 | | - |
42 | | - |
43 | | - |
44 | | - |
45 | | - |
46 | | - |
47 | | - |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
52 | | - |
53 | | - |
54 | | - |
55 | | - |
56 | | - |
57 | | - |
58 | #include <openssl/err.h> | - |
59 | | - |
60 | #include "bn_lcl.h" | - |
61 | | - |
62 | BIGNUM * | - |
63 | BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | - |
64 | | - |
65 | | - |
66 | | - |
67 | | - |
68 | | - |
69 | | - |
70 | { | - |
71 | BIGNUM *ret = in; | - |
72 | int err = 1; | - |
73 | int r; | - |
74 | BIGNUM *A, *b, *q, *t, *x, *y; | - |
75 | int e, i, j; | - |
76 | | - |
77 | if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {TRUE | evaluated 87 times by 2 tests | FALSE | never evaluated |
TRUE | evaluated 82 times by 2 tests | FALSE | evaluated 5 times by 1 test |
TRUE | evaluated 37 times by 2 tests | FALSE | evaluated 45 times by 2 tests |
TRUE | never evaluated | FALSE | evaluated 37 times by 2 tests |
TRUE | never evaluated | FALSE | evaluated 82 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
| 0-87 |
78 | if (BN_abs_is_word(p, 2)) {TRUE | evaluated 5 times by 1 test | FALSE | never evaluated |
TRUE | evaluated 5 times by 1 test | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0-5 |
79 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 5 times by 1 test |
| 0-5 |
80 | ret = BN_new(); never executed: ret = BN_new(); | 0 |
81 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 5 times by 1 test |
| 0-5 |
82 | goto end; never executed: goto end; | 0 |
83 | if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {TRUE | never evaluated | FALSE | evaluated 5 times by 1 test |
| 0-5 |
84 | if (ret != in)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
85 | BN_free(ret); never executed: BN_free(ret); | 0 |
86 | return NULL; never executed: return ((void *)0) ; | 0 |
87 | } | - |
88 | bn_check_top(ret); | - |
89 | return ret;executed 5 times by 1 test: return ret; | 5 |
90 | } | - |
91 | | - |
92 | BNerror(BN_R_P_IS_NOT_PRIME); | - |
93 | return (NULL); never executed: return ( ((void *)0) ); | 0 |
94 | } | - |
95 | | - |
96 | if (BN_is_zero(a) || BN_is_one(a)) {TRUE | evaluated 6 times by 1 test | FALSE | evaluated 76 times by 2 tests |
TRUE | evaluated 31 times by 2 tests | FALSE | evaluated 45 times by 2 tests |
TRUE | evaluated 2 times by 1 test | FALSE | evaluated 29 times by 2 tests |
TRUE | never evaluated | FALSE | evaluated 74 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | evaluated 1 time by 1 test | FALSE | evaluated 1 time by 1 test |
| 0-76 |
97 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 7 times by 1 test |
| 0-7 |
98 | ret = BN_new(); never executed: ret = BN_new(); | 0 |
99 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 7 times by 1 test |
| 0-7 |
100 | goto end; never executed: goto end; | 0 |
101 | if (!BN_set_word(ret, BN_is_one(a))) {TRUE | never evaluated | FALSE | evaluated 7 times by 1 test |
| 0-7 |
102 | if (ret != in)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
103 | BN_free(ret); never executed: BN_free(ret); | 0 |
104 | return NULL; never executed: return ((void *)0) ; | 0 |
105 | } | - |
106 | bn_check_top(ret); | - |
107 | return ret;executed 7 times by 1 test: return ret; | 7 |
108 | } | - |
109 | | - |
110 | BN_CTX_start(ctx); | - |
111 | if ((A = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
112 | goto end; never executed: goto end; | 0 |
113 | if ((b = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
114 | goto end; never executed: goto end; | 0 |
115 | if ((q = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
116 | goto end; never executed: goto end; | 0 |
117 | if ((t = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
118 | goto end; never executed: goto end; | 0 |
119 | if ((x = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
120 | goto end; never executed: goto end; | 0 |
121 | if ((y = BN_CTX_get(ctx)) == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
122 | goto end; never executed: goto end; | 0 |
123 | | - |
124 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
125 | ret = BN_new(); never executed: ret = BN_new(); | 0 |
126 | if (ret == NULL)TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
127 | goto end; never executed: goto end; | 0 |
128 | | - |
129 | | - |
130 | if (!BN_nnmod(A, a, p, ctx))TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
131 | goto end; never executed: goto end; | 0 |
132 | | - |
133 | | - |
134 | e = 1; | - |
135 | while (!BN_is_bit_set(p, e))TRUE | evaluated 153 times by 2 tests | FALSE | evaluated 75 times by 2 tests |
| 75-153 |
136 | e++;executed 153 times by 2 tests: e++; | 153 |
137 | | - |
138 | | - |
139 | if (e == 1) {TRUE | evaluated 41 times by 2 tests | FALSE | evaluated 34 times by 2 tests |
| 34-41 |
140 | | - |
141 | | - |
142 | | - |
143 | | - |
144 | | - |
145 | | - |
146 | | - |
147 | if (!BN_rshift(q, p, 2))TRUE | never evaluated | FALSE | evaluated 41 times by 2 tests |
| 0-41 |
148 | goto end; never executed: goto end; | 0 |
149 | q->neg = 0; | - |
150 | if (!BN_add_word(q, 1))TRUE | never evaluated | FALSE | evaluated 41 times by 2 tests |
| 0-41 |
151 | goto end; never executed: goto end; | 0 |
152 | if (!BN_mod_exp_ct(ret, A, q, p, ctx))TRUE | never evaluated | FALSE | evaluated 41 times by 2 tests |
| 0-41 |
153 | goto end; never executed: goto end; | 0 |
154 | err = 0; | - |
155 | goto vrfy;executed 41 times by 2 tests: goto vrfy; | 41 |
156 | } | - |
157 | | - |
158 | if (e == 2) {TRUE | evaluated 18 times by 1 test | FALSE | evaluated 16 times by 2 tests |
| 16-18 |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | | - |
165 | | - |
166 | | - |
167 | | - |
168 | | - |
169 | | - |
170 | | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | | - |
181 | | - |
182 | | - |
183 | | - |
184 | | - |
185 | | - |
186 | if (!BN_mod_lshift1_quick(t, A, p))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
187 | goto end; never executed: goto end; | 0 |
188 | | - |
189 | | - |
190 | if (!BN_rshift(q, p, 3))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
191 | goto end; never executed: goto end; | 0 |
192 | q->neg = 0; | - |
193 | if (!BN_mod_exp_ct(b, t, q, p, ctx))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
194 | goto end; never executed: goto end; | 0 |
195 | | - |
196 | | - |
197 | if (!BN_mod_sqr(y, b, p, ctx))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
198 | goto end; never executed: goto end; | 0 |
199 | | - |
200 | | - |
201 | if (!BN_mod_mul(t, t, y, p, ctx))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
202 | goto end; never executed: goto end; | 0 |
203 | if (!BN_sub_word(t, 1))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
204 | goto end; never executed: goto end; | 0 |
205 | | - |
206 | | - |
207 | if (!BN_mod_mul(x, A, b, p, ctx))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
208 | goto end; never executed: goto end; | 0 |
209 | if (!BN_mod_mul(x, x, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
210 | goto end; never executed: goto end; | 0 |
211 | | - |
212 | if (!BN_copy(ret, x))TRUE | never evaluated | FALSE | evaluated 18 times by 1 test |
| 0-18 |
213 | goto end; never executed: goto end; | 0 |
214 | err = 0; | - |
215 | goto vrfy;executed 18 times by 1 test: goto vrfy; | 18 |
216 | } | - |
217 | | - |
218 | | - |
219 | | - |
220 | if (!BN_copy(q, p)) goto end; never executed: goto end; TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
| 0-16 |
221 | q->neg = 0; | - |
222 | i = 2; | - |
223 | do { | - |
224 | | - |
225 | | - |
226 | | - |
227 | if (i < 22) {TRUE | evaluated 40 times by 2 tests | FALSE | never evaluated |
| 0-40 |
228 | if (!BN_set_word(y, i))TRUE | never evaluated | FALSE | evaluated 40 times by 2 tests |
| 0-40 |
229 | goto end; never executed: goto end; | 0 |
230 | } else {executed 40 times by 2 tests: end of block | 40 |
231 | if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
232 | goto end; never executed: goto end; | 0 |
233 | if (BN_ucmp(y, p) >= 0) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
234 | if (p->neg) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
235 | if (!BN_add(y, y, p))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
236 | goto end; never executed: goto end; | 0 |
237 | } else { never executed: end of block | 0 |
238 | if (!BN_sub(y, y, p))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
239 | goto end; never executed: goto end; | 0 |
240 | } never executed: end of block | 0 |
241 | } | - |
242 | | - |
243 | if (BN_is_zero(y))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
244 | if (!BN_set_word(y, i))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
245 | goto end; never executed: goto end; | 0 |
246 | } never executed: end of block | 0 |
247 | | - |
248 | r = BN_kronecker(y, q, ctx); | - |
249 | if (r < -1)TRUE | never evaluated | FALSE | evaluated 40 times by 2 tests |
| 0-40 |
250 | goto end; never executed: goto end; | 0 |
251 | if (r == 0) {TRUE | never evaluated | FALSE | evaluated 40 times by 2 tests |
| 0-40 |
252 | | - |
253 | BNerror(BN_R_P_IS_NOT_PRIME); | - |
254 | goto end; never executed: goto end; | 0 |
255 | } | - |
256 | }executed 40 times by 2 tests: end of block | 40 |
257 | while (r == 1 && ++i < 82);TRUE | evaluated 24 times by 2 tests | FALSE | evaluated 16 times by 2 tests |
TRUE | evaluated 24 times by 2 tests | FALSE | never evaluated |
| 0-24 |
258 | | - |
259 | if (r != -1) {TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
| 0-16 |
260 | | - |
261 | | - |
262 | | - |
263 | | - |
264 | | - |
265 | BNerror(BN_R_TOO_MANY_ITERATIONS); | - |
266 | goto end; never executed: goto end; | 0 |
267 | } | - |
268 | | - |
269 | | - |
270 | if (!BN_rshift(q, q, e))TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
| 0-16 |
271 | goto end; never executed: goto end; | 0 |
272 | | - |
273 | | - |
274 | | - |
275 | if (!BN_mod_exp_ct(y, y, q, p, ctx))TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
| 0-16 |
276 | goto end; never executed: goto end; | 0 |
277 | if (BN_is_one(y)) {TRUE | evaluated 5 times by 1 test | FALSE | evaluated 11 times by 2 tests |
TRUE | never evaluated | FALSE | evaluated 5 times by 1 test |
TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0-16 |
278 | BNerror(BN_R_P_IS_NOT_PRIME); | - |
279 | goto end; never executed: goto end; | 0 |
280 | } | - |
281 | | - |
282 | | - |
283 | | - |
284 | | - |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | | - |
298 | | - |
299 | | - |
300 | | - |
301 | if (!BN_rshift1(t, q))TRUE | never evaluated | FALSE | evaluated 16 times by 2 tests |
| 0-16 |
302 | goto end; never executed: goto end; | 0 |
303 | | - |
304 | | - |
305 | if (BN_is_zero(t)) TRUE | evaluated 5 times by 1 test | FALSE | evaluated 11 times by 2 tests |
| 5-11 |
306 | { | - |
307 | if (!BN_nnmod(t, A, p, ctx))TRUE | never evaluated | FALSE | evaluated 5 times by 1 test |
| 0-5 |
308 | goto end; never executed: goto end; | 0 |
309 | if (BN_is_zero(t)) {TRUE | evaluated 1 time by 1 test | FALSE | evaluated 4 times by 1 test |
| 1-4 |
310 | | - |
311 | BN_zero(ret); | - |
312 | err = 0; | - |
313 | goto end;executed 1 time by 1 test: goto end; | 1 |
314 | } else if (!BN_one(x))TRUE | never evaluated | FALSE | evaluated 4 times by 1 test |
| 0-4 |
315 | goto end; never executed: goto end; | 0 |
316 | } else {executed 4 times by 1 test: end of block | 4 |
317 | if (!BN_mod_exp_ct(x, A, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 11 times by 2 tests |
| 0-11 |
318 | goto end; never executed: goto end; | 0 |
319 | if (BN_is_zero(x)) {TRUE | never evaluated | FALSE | evaluated 11 times by 2 tests |
| 0-11 |
320 | | - |
321 | BN_zero(ret); | - |
322 | err = 0; | - |
323 | goto end; never executed: goto end; | 0 |
324 | } | - |
325 | }executed 11 times by 2 tests: end of block | 11 |
326 | | - |
327 | | - |
328 | if (!BN_mod_sqr(b, x, p, ctx))TRUE | never evaluated | FALSE | evaluated 15 times by 2 tests |
| 0-15 |
329 | goto end; never executed: goto end; | 0 |
330 | if (!BN_mod_mul(b, b, A, p, ctx))TRUE | never evaluated | FALSE | evaluated 15 times by 2 tests |
| 0-15 |
331 | goto end; never executed: goto end; | 0 |
332 | | - |
333 | | - |
334 | if (!BN_mod_mul(x, x, A, p, ctx))TRUE | never evaluated | FALSE | evaluated 15 times by 2 tests |
| 0-15 |
335 | goto end; never executed: goto end; | 0 |
336 | | - |
337 | while (1) { | - |
338 | | - |
339 | | - |
340 | | - |
341 | | - |
342 | | - |
343 | | - |
344 | | - |
345 | | - |
346 | | - |
347 | if (BN_is_one(b)) {TRUE | evaluated 21 times by 2 tests | FALSE | evaluated 51 times by 2 tests |
TRUE | evaluated 15 times by 2 tests | FALSE | evaluated 6 times by 1 test |
TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | evaluated 15 times by 2 tests | FALSE | never evaluated |
| 0-57 |
348 | if (!BN_copy(ret, x))TRUE | never evaluated | FALSE | evaluated 15 times by 2 tests |
| 0-15 |
349 | goto end; never executed: goto end; | 0 |
350 | err = 0; | - |
351 | goto vrfy;executed 15 times by 2 tests: goto vrfy; | 15 |
352 | } | - |
353 | | - |
354 | | - |
355 | | - |
356 | i = 1; | - |
357 | if (!BN_mod_sqr(t, b, p, ctx))TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
| 0-57 |
358 | goto end; never executed: goto end; | 0 |
359 | while (!BN_is_one(t)) {TRUE | evaluated 66 times by 2 tests | FALSE | evaluated 1780 times by 2 tests |
TRUE | evaluated 57 times by 2 tests | FALSE | evaluated 9 times by 1 test |
TRUE | never evaluated | FALSE | evaluated 1789 times by 2 tests |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | evaluated 57 times by 2 tests | FALSE | never evaluated |
| 0-1789 |
360 | i++; | - |
361 | if (i == e) {TRUE | never evaluated | FALSE | evaluated 1789 times by 2 tests |
| 0-1789 |
362 | BNerror(BN_R_NOT_A_SQUARE); | - |
363 | goto end; never executed: goto end; | 0 |
364 | } | - |
365 | if (!BN_mod_mul(t, t, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 1789 times by 2 tests |
| 0-1789 |
366 | goto end; never executed: goto end; | 0 |
367 | }executed 1789 times by 2 tests: end of block | 1789 |
368 | | - |
369 | | - |
370 | | - |
371 | if (!BN_copy(t, y))TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
| 0-57 |
372 | goto end; never executed: goto end; | 0 |
373 | for (j = e - i - 1; j > 0; j--) {TRUE | evaluated 64 times by 2 tests | FALSE | evaluated 57 times by 2 tests |
| 57-64 |
374 | if (!BN_mod_sqr(t, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 64 times by 2 tests |
| 0-64 |
375 | goto end; never executed: goto end; | 0 |
376 | }executed 64 times by 2 tests: end of block | 64 |
377 | if (!BN_mod_mul(y, t, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
| 0-57 |
378 | goto end; never executed: goto end; | 0 |
379 | if (!BN_mod_mul(x, x, t, p, ctx))TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
| 0-57 |
380 | goto end; never executed: goto end; | 0 |
381 | if (!BN_mod_mul(b, b, y, p, ctx))TRUE | never evaluated | FALSE | evaluated 57 times by 2 tests |
| 0-57 |
382 | goto end; never executed: goto end; | 0 |
383 | e = i; | - |
384 | }executed 57 times by 2 tests: end of block | 57 |
385 | | - |
386 | vrfy: code before this statement never executed: vrfy: | 0 |
387 | if (!err) {TRUE | evaluated 74 times by 2 tests | FALSE | never evaluated |
| 0-74 |
388 | | - |
389 | | - |
390 | | - |
391 | if (!BN_mod_sqr(x, ret, p, ctx))TRUE | never evaluated | FALSE | evaluated 74 times by 2 tests |
| 0-74 |
392 | err = 1; never executed: err = 1; | 0 |
393 | | - |
394 | if (!err && 0 != BN_cmp(x, A)) {TRUE | evaluated 74 times by 2 tests | FALSE | never evaluated |
TRUE | never evaluated | FALSE | evaluated 74 times by 2 tests |
| 0-74 |
395 | BNerror(BN_R_NOT_A_SQUARE); | - |
396 | err = 1; | - |
397 | } never executed: end of block | 0 |
398 | }executed 74 times by 2 tests: end of block | 74 |
399 | | - |
400 | end:code before this statement executed 74 times by 2 tests: end: | 74 |
401 | if (err) {TRUE | never evaluated | FALSE | evaluated 75 times by 2 tests |
| 0-75 |
402 | if (ret != NULL && ret != in) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
403 | BN_clear_free(ret); | - |
404 | } never executed: end of block | 0 |
405 | ret = NULL; | - |
406 | } never executed: end of block | 0 |
407 | BN_CTX_end(ctx); | - |
408 | bn_check_top(ret); | - |
409 | return ret;executed 75 times by 2 tests: return ret; | 75 |
410 | } | - |
| | |