OpenCoverage

bn_recp.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_recp.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1995-2018 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 "internal/cryptlib.h"-
11#include "bn_lcl.h"-
12-
13void BN_RECP_CTX_init(BN_RECP_CTX *recp)-
14{-
15 memset(recp, 0, sizeof(*recp));-
16 bn_init(&(recp->N));-
17 bn_init(&(recp->Nr));-
18}
executed 577 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
577
19-
20BN_RECP_CTX *BN_RECP_CTX_new(void)-
21{-
22 BN_RECP_CTX *ret;-
23-
24 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
(ret = CRYPTO_...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1
25 BNerr(BN_F_BN_RECP_CTX_NEW, ERR_R_MALLOC_FAILURE);-
26 return NULL;
never executed: return ((void *)0) ;
0
27 }-
28-
29 bn_init(&(ret->N));-
30 bn_init(&(ret->Nr));-
31 ret->flags = BN_FLG_MALLOCED;-
32 return ret;
executed 1 time by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
1
33}-
34-
35void BN_RECP_CTX_free(BN_RECP_CTX *recp)-
36{-
37 if (recp == NULL)
recp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 578 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-578
38 return;
never executed: return;
0
39 BN_free(&recp->N);-
40 BN_free(&recp->Nr);-
41 if (recp->flags & BN_FLG_MALLOCED)
recp->flags & 0x01Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 577 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-577
42 OPENSSL_free(recp);
executed 1 time by 1 test: CRYPTO_free(recp, __FILE__, 42);
Executed by:
  • libcrypto.so.1.1
1
43}
executed 578 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
578
44-
45int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)-
46{-
47 if (!BN_copy(&(recp->N), d))
!BN_copy(&(recp->N), d)Description
TRUEnever evaluated
FALSEevaluated 727 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-727
48 return 0;
never executed: return 0;
0
49 BN_zero(&(recp->Nr));-
50 recp->num_bits = BN_num_bits(d);-
51 recp->shift = 0;-
52 return 1;
executed 727 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
727
53}-
54-
55int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,-
56 BN_RECP_CTX *recp, BN_CTX *ctx)-
57{-
58 int ret = 0;-
59 BIGNUM *a;-
60 const BIGNUM *ca;-
61-
62 BN_CTX_start(ctx);-
63 if ((a = BN_CTX_get(ctx)) == NULL)
(a = BN_CTX_ge...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 172764 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-172764
64 goto err;
never executed: goto err;
0
65 if (y != NULL) {
y != ((void *)0)Description
TRUEevaluated 172764 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-172764
66 if (x == y) {
x == yDescription
TRUEevaluated 143423 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 29341 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
29341-143423
67 if (!BN_sqr(a, x, ctx))
!BN_sqr(a, x, ctx)Description
TRUEnever evaluated
FALSEevaluated 143423 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-143423
68 goto err;
never executed: goto err;
0
69 } else {
executed 143423 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
143423
70 if (!BN_mul(a, x, y, ctx))
!BN_mul(a, x, y, ctx)Description
TRUEnever evaluated
FALSEevaluated 29341 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-29341
71 goto err;
never executed: goto err;
0
72 }
executed 29341 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
29341
73 ca = a;-
74 } else
executed 172764 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
172764
75 ca = x; /* Just do the mod */
never executed: ca = x;
0
76-
77 ret = BN_div_recp(NULL, r, ca, recp, ctx);-
78 err:
code before this statement executed 172764 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
172764
79 BN_CTX_end(ctx);-
80 bn_check_top(r);-
81 return ret;
executed 172764 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
172764
82}-
83-
84int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,-
85 BN_RECP_CTX *recp, BN_CTX *ctx)-
86{-
87 int i, j, ret = 0;-
88 BIGNUM *a, *b, *d, *r;-
89-
90 BN_CTX_start(ctx);-
91 d = (dv != NULL) ? dv : BN_CTX_get(ctx);
(dv != ((void *)0) )Description
TRUEevaluated 150 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 172764 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
150-172764
92 r = (rem != NULL) ? rem : BN_CTX_get(ctx);
(rem != ((void *)0) )Description
TRUEevaluated 172914 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-172914
93 a = BN_CTX_get(ctx);-
94 b = BN_CTX_get(ctx);-
95 if (b == NULL)
b == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 172914 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-172914
96 goto err;
never executed: goto err;
0
97-
98 if (BN_ucmp(m, &(recp->N)) < 0) {
BN_ucmp(m, &(recp->N)) < 0Description
TRUEevaluated 9160 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
9160-163754
99 BN_zero(d);-
100 if (!BN_copy(r, m)) {
!BN_copy(r, m)Description
TRUEnever evaluated
FALSEevaluated 9160 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-9160
101 BN_CTX_end(ctx);-
102 return 0;
never executed: return 0;
0
103 }-
104 BN_CTX_end(ctx);-
105 return 1;
executed 9160 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
9160
106 }-
107-
108 /*-
109 * We want the remainder Given input of ABCDEF / ab we need multiply-
110 * ABCDEF by 3 digests of the reciprocal of ab-
111 */-
112-
113 /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */-
114 i = BN_num_bits(m);-
115 j = recp->num_bits << 1;-
116 if (j > i)
j > iDescription
TRUEevaluated 151545 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12209 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12209-151545
117 i = j;
executed 151545 times by 1 test: i = j;
Executed by:
  • libcrypto.so.1.1
151545
118-
119 /* Nr := round(2^i / N) */-
120 if (i != recp->shift)
i != recp->shiftDescription
TRUEevaluated 717 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 163037 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
717-163037
121 recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
executed 717 times by 1 test: recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
Executed by:
  • libcrypto.so.1.1
717
122 /* BN_reciprocal could have returned -1 for an error */-
123 if (recp->shift == -1)
recp->shift == -1Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
124 goto err;
never executed: goto err;
0
125-
126 /*--
127 * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|-
128 * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|-
129 * <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|-
130 * = |m/N|-
131 */-
132 if (!BN_rshift(a, m, recp->num_bits))
!BN_rshift(a, ...ecp->num_bits)Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
133 goto err;
never executed: goto err;
0
134 if (!BN_mul(b, a, &(recp->Nr), ctx))
!BN_mul(b, a, ...ecp->Nr), ctx)Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
135 goto err;
never executed: goto err;
0
136 if (!BN_rshift(d, b, i - recp->num_bits))
!BN_rshift(d, ...ecp->num_bits)Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
137 goto err;
never executed: goto err;
0
138 d->neg = 0;-
139-
140 if (!BN_mul(b, &(recp->N), d, ctx))
!BN_mul(b, &(recp->N), d, ctx)Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
141 goto err;
never executed: goto err;
0
142 if (!BN_usub(r, m, b))
!BN_usub(r, m, b)Description
TRUEnever evaluated
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-163754
143 goto err;
never executed: goto err;
0
144 r->neg = 0;-
145-
146 j = 0;-
147 while (BN_ucmp(r, &(recp->N)) >= 0) {
BN_ucmp(r, &(recp->N)) >= 0Description
TRUEevaluated 122656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 163754 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
122656-163754
148 if (j++ > 2) {
j++ > 2Description
TRUEnever evaluated
FALSEevaluated 122656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-122656
149 BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);-
150 goto err;
never executed: goto err;
0
151 }-
152 if (!BN_usub(r, r, &(recp->N)))
!BN_usub(r, r, &(recp->N))Description
TRUEnever evaluated
FALSEevaluated 122656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-122656
153 goto err;
never executed: goto err;
0
154 if (!BN_add_word(d, 1))
!BN_add_word(d, 1)Description
TRUEnever evaluated
FALSEevaluated 122656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-122656
155 goto err;
never executed: goto err;
0
156 }
executed 122656 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
122656
157-
158 r->neg = BN_is_zero(r) ? 0 : m->neg;
BN_is_zero(r)Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 163745 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
9-163745
159 d->neg = m->neg ^ recp->N.neg;-
160 ret = 1;-
161 err:
code before this statement executed 163754 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
163754
162 BN_CTX_end(ctx);-
163 bn_check_top(dv);-
164 bn_check_top(rem);-
165 return ret;
executed 163754 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
163754
166}-
167-
168/*-
169 * len is the expected size of the result We actually calculate with an extra-
170 * word of precision, so we can do faster division if the remainder is not-
171 * required.-
172 */-
173/* r := 2^len / m */-
174int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)-
175{-
176 int ret = -1;-
177 BIGNUM *t;-
178-
179 BN_CTX_start(ctx);-
180 if ((t = BN_CTX_get(ctx)) == NULL)
(t = BN_CTX_ge...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 717 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-717
181 goto err;
never executed: goto err;
0
182-
183 if (!BN_set_bit(t, len))
!BN_set_bit(t, len)Description
TRUEnever evaluated
FALSEevaluated 717 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-717
184 goto err;
never executed: goto err;
0
185-
186 if (!BN_div(r, NULL, t, m, ctx))
!BN_div(r, ((v...) , t, m, ctx)Description
TRUEnever evaluated
FALSEevaluated 717 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-717
187 goto err;
never executed: goto err;
0
188-
189 ret = len;-
190 err:
code before this statement executed 717 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
717
191 bn_check_top(r);-
192 BN_CTX_end(ctx);-
193 return ret;
executed 717 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
717
194}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2