OpenCoverage

bn_rand.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_rand.c
Switch to Source codePreprocessed file
LineSourceCount
1-
2typedef enum bnrand_flag_e {-
3 NORMAL, TESTING, PRIVATE-
4} BNRAND_FLAG;-
5-
6static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)-
7{-
8 unsigned char *buf = -
9 ((void *)0)-
10 ;-
11 int b, ret = 0, bit, bytes, mask;-
12-
13 if (bits == 0
bits == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 137416 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
2-137416
14 if (top != -1
top != -1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
|| bottom != 0
bottom != 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0-2
15 goto
executed 2 times by 1 test: goto toosmall;
Executed by:
  • libcrypto.so.1.1
toosmall;
executed 2 times by 1 test: goto toosmall;
Executed by:
  • libcrypto.so.1.1
2
16 (BN_set_word((rnd),0));-
17 return
never executed: return 1;
1;
never executed: return 1;
0
18 }-
19 if (bits < 0
bits < 0Description
TRUEnever evaluated
FALSEevaluated 137416 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
|| (bits == 1
bits == 1Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 137413 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
&& top > 0
top > 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
))
0-137416
20 goto
executed 1 time by 1 test: goto toosmall;
Executed by:
  • libcrypto.so.1.1
toosmall;
executed 1 time by 1 test: goto toosmall;
Executed by:
  • libcrypto.so.1.1
1
21-
22 bytes = (bits + 7) / 8;-
23 bit = (bits - 1) % 8;-
24 mask = 0xff << (bit + 1);-
25-
26 buf = CRYPTO_malloc(bytes, __FILE__, 39);-
27 if (buf ==
buf == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 137415 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-137415
28 ((void *)0)
buf == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 137415 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-137415
29 ) {-
30 ERR_put_error(3,(127),((1|64)),__FILE__,41);-
31 goto
never executed: goto err;
err;
never executed: goto err;
0
32 }-
33-
34-
35 b = flag == NORMAL
flag == NORMALDescription
TRUEevaluated 734 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 136681 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);
734-136681
36 if (b <= 0
b <= 0Description
TRUEnever evaluated
FALSEevaluated 137415 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-137415
37 goto
never executed: goto err;
err;
never executed: goto err;
0
38-
39 if (flag == TESTING
flag == TESTINGDescription
TRUEevaluated 1807 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 135608 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
1807-135608
40-
41-
42-
43 int i;-
44 unsigned char c;-
45-
46 for (i = 0; i < bytes
i < bytesDescription
TRUEevaluated 148234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1807 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
; i++) {
1807-148234
47 if (RAND_bytes(&c, 1) <= 0
RAND_bytes(&c, 1) <= 0Description
TRUEnever evaluated
FALSEevaluated 148234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-148234
48 goto
never executed: goto err;
err;
never executed: goto err;
0
49 if (c >= 128
c >= 128Description
TRUEevaluated 74246 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 73988 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
&& i > 0
i > 0Description
TRUEevaluated 73382 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 864 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
864-74246
50 buf[i] = buf[i - 1];
executed 73382 times by 1 test: buf[i] = buf[i - 1];
Executed by:
  • libcrypto.so.1.1
73382
51 else if (c < 42
c < 42Description
TRUEevaluated 24287 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 50565 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
24287-50565
52 buf[i] = 0;
executed 24287 times by 1 test: buf[i] = 0;
Executed by:
  • libcrypto.so.1.1
24287
53 else if (c < 84
c < 84Description
TRUEevaluated 24281 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 26284 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
24281-26284
54 buf[i] = 255;
executed 24281 times by 1 test: buf[i] = 255;
Executed by:
  • libcrypto.so.1.1
24281
55 }
executed 148234 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
148234
56 }
executed 1807 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1807
57-
58 if (top >= 0
top >= 0Description
TRUEevaluated 49342 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 88073 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
49342-88073
59 if (top
topDescription
TRUEevaluated 3385 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 45957 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
3385-45957
60 if (bit == 0
bit == 0Description
TRUEnever evaluated
FALSEevaluated 3385 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-3385
61 buf[0] = 1;-
62 buf[1] |= 0x80;-
63 }
never executed: end of block
else {
0
64 buf[0] |= (3 << (bit - 1));-
65 }
executed 3385 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
3385
66 } else {-
67 buf[0] |= (1 << bit);-
68 }
executed 45957 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
45957
69 }-
70 buf[0] &= ~mask;-
71 if (bottom
bottomDescription
TRUEevaluated 3598 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 133817 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
3598-133817
72 buf[bytes - 1] |= 1;
executed 3598 times by 1 test: buf[bytes - 1] |= 1;
Executed by:
  • libcrypto.so.1.1
3598
73 if (!BN_bin2bn(buf, bytes, rnd)
!BN_bin2bn(buf, bytes, rnd)Description
TRUEnever evaluated
FALSEevaluated 137415 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-137415
74 goto
never executed: goto err;
err;
never executed: goto err;
0
75 ret = 1;-
76 err:
code before this statement executed 137415 times by 2 tests: err:
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
137415
77 CRYPTO_clear_free(buf, bytes, __FILE__, 88);-
78 ;-
79 return
executed 137415 times by 2 tests: return ret;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
ret;
executed 137415 times by 2 tests: return ret;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
137415
80-
81toosmall:-
82 ERR_put_error(3,(127),(118),__FILE__,93);-
83 return
executed 3 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
0;
executed 3 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
3
84}-
85-
86int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)-
87{-
88 return
executed 737 times by 1 test: return bnrand(NORMAL, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
bnrand(NORMAL, rnd, bits, top, bottom);
executed 737 times by 1 test: return bnrand(NORMAL, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
737
89}-
90-
91int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)-
92{-
93 return
executed 1807 times by 1 test: return bnrand(TESTING, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
bnrand(TESTING, rnd, bits, top, bottom);
executed 1807 times by 1 test: return bnrand(TESTING, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
1807
94}-
95-
96int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)-
97{-
98 return
executed 124517 times by 1 test: return bnrand(PRIVATE, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
bnrand(PRIVATE, rnd, bits, top, bottom);
executed 124517 times by 1 test: return bnrand(PRIVATE, rnd, bits, top, bottom);
Executed by:
  • libcrypto.so.1.1
124517
99}-
100-
101-
102static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)-
103{-
104 int n;-
105 int count = 100;-
106-
107 if (range->neg
range->negDescription
TRUEnever evaluated
FALSEevaluated 9242 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
|| BN_is_zero(range)
BN_is_zero(range)Description
TRUEnever evaluated
FALSEevaluated 9242 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
0-9242
108 ERR_put_error(3,(138),(115),__FILE__,119);-
109 return
never executed: return 0;
0;
never executed: return 0;
0
110 }-
111-
112 n = BN_num_bits(range);-
113-
114-
115-
116 if (n == 1
n == 1Description
TRUEnever evaluated
FALSEevaluated 9242 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-9242
117 (
never executed: (BN_set_word((r),0));
BN_set_word((r),0));
never executed: (BN_set_word((r),0));
0
118 else if (!BN_is_bit_set(range, n - 2)
!BN_is_bit_set(range, n - 2)Description
TRUEevaluated 591 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 8651 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
&& !BN_is_bit_set(range, n - 3)
!BN_is_bit_set(range, n - 3)Description
TRUEevaluated 248 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 343 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
248-8651
119-
120-
121-
122-
123 do {-
124 if (!bnrand(flag, r, n + 1, -1, 0)
!bnrand(flag, r, n + 1, -1, 0)Description
TRUEnever evaluated
FALSEevaluated 320 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-320
125 return
never executed: return 0;
0;
never executed: return 0;
0
126-
127-
128-
129-
130-
131-
132-
133 if (BN_cmp(r, range) >= 0
BN_cmp(r, range) >= 0Description
TRUEevaluated 237 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 83 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
83-237
134 if (!BN_sub(r, r, range)
!BN_sub(r, r, range)Description
TRUEnever evaluated
FALSEevaluated 237 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-237
135 return
never executed: return 0;
0;
never executed: return 0;
0
136 if (BN_cmp(r, range) >= 0
BN_cmp(r, range) >= 0Description
TRUEevaluated 140 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 97 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
97-140
137 if (!BN_sub(r, r, range)
!BN_sub(r, r, range)Description
TRUEnever evaluated
FALSEevaluated 140 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
)
0-140
138 return
never executed: return 0;
0;
never executed: return 0;
0
139 }
executed 237 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
237
140-
141 if (!--count
!--countDescription
TRUEnever evaluated
FALSEevaluated 320 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
) {
0-320
142 ERR_put_error(3,(138),(113),__FILE__,153);-
143 return
never executed: return 0;
0;
never executed: return 0;
0
144 }-
145-
146 }
executed 320 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
320
147 while (BN_cmp(r, range) >= 0
BN_cmp(r, range) >= 0Description
TRUEevaluated 72 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 248 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
);
72-248
148 }
executed 248 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
else {
248
149 do {-
150-
151 if (!bnrand(flag, r, n, -1, 0)
!bnrand(flag, r, n, -1, 0)Description
TRUEnever evaluated
FALSEevaluated 10037 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-10037
152 return
never executed: return 0;
0;
never executed: return 0;
0
153-
154 if (!--count
!--countDescription
TRUEnever evaluated
FALSEevaluated 10037 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-10037
155 ERR_put_error(3,(138),(113),__FILE__,166);-
156 return
never executed: return 0;
0;
never executed: return 0;
0
157 }-
158 }
executed 10037 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
10037
159 while (BN_cmp(r, range) >= 0
BN_cmp(r, range) >= 0Description
TRUEevaluated 1043 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 8994 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
);
1043-8994
160 }
executed 8994 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
8994
161-
162 ;-
163 return
executed 9242 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1;
executed 9242 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
9242
164}-
165-
166int BN_rand_range(BIGNUM *r, const BIGNUM *range)-
167{-
168 return
never executed: return bnrand_range(NORMAL, r, range);
bnrand_range(NORMAL, r, range);
never executed: return bnrand_range(NORMAL, r, range);
0
169}-
170-
171int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)-
172{-
173 return
executed 9242 times by 2 tests: return bnrand_range(PRIVATE, r, range);
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
bnrand_range(PRIVATE, r, range);
executed 9242 times by 2 tests: return bnrand_range(PRIVATE, r, range);
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
9242
174}-
175-
176int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)-
177{-
178 return
never executed: return BN_rand(rnd, bits, top, bottom);
BN_rand(rnd, bits, top, bottom);
never executed: return BN_rand(rnd, bits, top, bottom);
0
179}-
180-
181int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)-
182{-
183 return
never executed: return BN_rand_range(r, range);
BN_rand_range(r, range);
never executed: return BN_rand_range(r, range);
0
184}-
185int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,-
186 const BIGNUM *priv, const unsigned char *message,-
187 size_t message_len, BN_CTX *ctx)-
188{-
189 SHA512_CTX sha;-
190-
191-
192-
193-
194 unsigned char random_bytes[64];-
195 unsigned char digest[64];-
196 unsigned done, todo;-
197-
198 const unsigned num_k_bytes = ((BN_num_bits(range)+7)/8) + 8;-
199 unsigned char private_bytes[96];-
200 unsigned char *k_bytes;-
201 int ret = 0;-
202-
203 k_bytes = CRYPTO_malloc(num_k_bytes, __FILE__, 223);-
204 if (k_bytes ==
k_bytes == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-427
205 ((void *)0)
k_bytes == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-427
206 )-
207 goto
never executed: goto err;
err;
never executed: goto err;
0
208-
209-
210 todo = sizeof(priv->d[0]) * priv->top;-
211 if (todo > sizeof(private_bytes)
todo > sizeof(private_bytes)Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-427
212-
213-
214-
215-
216-
217 ERR_put_error(3,(140),(117),__FILE__,235);-
218 goto
never executed: goto err;
err;
never executed: goto err;
0
219 }-
220 memcpy(private_bytes, priv->d, todo);-
221 memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);-
222-
223 for (done = 0; done < num_k_bytes
done < num_k_bytesDescription
TRUEevaluated 432 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
;) {
427-432
224 if (RAND_priv_bytes(random_bytes, sizeof(random_bytes)) != 1
RAND_priv_byte...m_bytes)) != 1Description
TRUEnever evaluated
FALSEevaluated 432 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-432
225 goto
never executed: goto err;
err;
never executed: goto err;
0
226 SHA512_Init(&sha);-
227 SHA512_Update(&sha, &done, sizeof(done));-
228 SHA512_Update(&sha, private_bytes, sizeof(private_bytes));-
229 SHA512_Update(&sha, message, message_len);-
230 SHA512_Update(&sha, random_bytes, sizeof(random_bytes));-
231 SHA512_Final(digest, &sha);-
232-
233 todo = num_k_bytes - done;-
234 if (todo > 64
todo > 64Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
5-427
235 todo = 64;
executed 5 times by 1 test: todo = 64;
Executed by:
  • libcrypto.so.1.1
5
236 memcpy(k_bytes + done, digest, todo);-
237 done += todo;-
238 }
executed 432 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
432
239-
240 if (!BN_bin2bn(k_bytes, num_k_bytes, out)
!BN_bin2bn(k_b..._k_bytes, out)Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-427
241 goto
never executed: goto err;
err;
never executed: goto err;
0
242 if (BN_div(
BN_div( ((void...e),(ctx)) != 1Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-427
243 ((void *)0)
BN_div( ((void...e),(ctx)) != 1Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-427
244 ,(out),(out),(range),(ctx)) != 1
BN_div( ((void...e),(ctx)) != 1Description
TRUEnever evaluated
FALSEevaluated 427 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-427
245 goto
never executed: goto err;
err;
never executed: goto err;
0
246 ret = 1;-
247-
248 err:
code before this statement executed 427 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
427
249 CRYPTO_free(k_bytes, __FILE__, 265);-
250 OPENSSL_cleanse(private_bytes, sizeof(private_bytes));-
251 return
executed 427 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
ret;
executed 427 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
427
252}-
Switch to Source codePreprocessed file

Generated by Squish Coco 4.2.2