OpenCoverage

bn_word.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_word.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1995-2016 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-
13BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)-
14{-
15#ifndef BN_LLONG-
16 BN_ULONG ret = 0;-
17#else-
18 BN_ULLONG ret = 0;-
19#endif-
20 int i;-
21-
22 if (w == 0)
w == 0Description
TRUEnever evaluated
FALSEevaluated 7022607 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-7022607
23 return (BN_ULONG)-1;
never executed: return (unsigned long)-1;
0
24-
25#ifndef BN_LLONG-
26 /*-
27 * If |w| is too long and we don't have BN_ULLONG then we need to fall-
28 * back to using BN_div_word-
29 */-
30 if (w > ((BN_ULONG)1 << BN_BITS4)) {
w > ((unsigned long)1 << 32)Description
TRUEevaluated 103 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 7022504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
103-7022504
31 BIGNUM *tmp = BN_dup(a);-
32 if (tmp == NULL)
tmp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 103 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-103
33 return (BN_ULONG)-1;
never executed: return (unsigned long)-1;
0
34-
35 ret = BN_div_word(tmp, w);-
36 BN_free(tmp);-
37-
38 return ret;
executed 103 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
103
39 }-
40#endif-
41-
42 bn_check_top(a);-
43 w &= BN_MASK2;-
44 for (i = a->top - 1; i >= 0; i--) {
i >= 0Description
TRUEevaluated 141244951 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 7022504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
7022504-141244951
45#ifndef BN_LLONG-
46 /*-
47 * We can assume here that | w <= ((BN_ULONG)1 << BN_BITS4) | and so-
48 * | ret < ((BN_ULONG)1 << BN_BITS4) | and therefore the shifts here are-
49 * safe and will not overflow-
50 */-
51 ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;-
52 ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;-
53#else-
54 ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) %-
55 (BN_ULLONG) w);-
56#endif-
57 }
executed 141244951 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
141244951
58 return (BN_ULONG)ret;
executed 7022504 times by 1 test: return (unsigned long)ret;
Executed by:
  • libcrypto.so.1.1
7022504
59}-
60-
61BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)-
62{-
63 BN_ULONG ret = 0;-
64 int i, j;-
65-
66 bn_check_top(a);-
67 w &= BN_MASK2;-
68-
69 if (!w)
!wDescription
TRUEnever evaluated
FALSEevaluated 35819 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-35819
70 /* actually this an error (division by zero) */-
71 return (BN_ULONG)-1;
never executed: return (unsigned long)-1;
0
72 if (a->top == 0)
a->top == 0Description
TRUEnever evaluated
FALSEevaluated 35819 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-35819
73 return 0;
never executed: return 0;
0
74-
75 /* normalize input (so bn_div_words doesn't complain) */-
76 j = BN_BITS2 - BN_num_bits_word(w);-
77 w <<= j;-
78 if (!BN_lshift(a, a, j))
!BN_lshift(a, a, j)Description
TRUEnever evaluated
FALSEevaluated 35819 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-35819
79 return (BN_ULONG)-1;
never executed: return (unsigned long)-1;
0
80-
81 for (i = a->top - 1; i >= 0; i--) {
i >= 0Description
TRUEevaluated 95669 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 35819 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
35819-95669
82 BN_ULONG l, d;-
83-
84 l = a->d[i];-
85 d = bn_div_words(ret, l, w);-
86 ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;-
87 a->d[i] = d;-
88 }
executed 95669 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
95669
89 if ((a->top > 0) && (a->d[a->top - 1] == 0))
(a->top > 0)Description
TRUEevaluated 35819 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
(a->d[a->top - 1] == 0)Description
TRUEevaluated 34752 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1067 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-35819
90 a->top--;
executed 34752 times by 1 test: a->top--;
Executed by:
  • libcrypto.so.1.1
34752
91 ret >>= j;-
92 if (!a->top)
!a->topDescription
TRUEevaluated 17143 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 18676 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
17143-18676
93 a->neg = 0; /* don't allow negative zero */
executed 17143 times by 1 test: a->neg = 0;
Executed by:
  • libcrypto.so.1.1
17143
94 bn_check_top(a);-
95 return ret;
executed 35819 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
35819
96}-
97-
98int BN_add_word(BIGNUM *a, BN_ULONG w)-
99{-
100 BN_ULONG l;-
101 int i;-
102-
103 bn_check_top(a);-
104 w &= BN_MASK2;-
105-
106 /* degenerate case: w is zero */-
107 if (!w)
!wDescription
TRUEevaluated 48980 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 208788 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
48980-208788
108 return 1;
executed 48980 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
48980
109 /* degenerate case: a is zero */-
110 if (BN_is_zero(a))
BN_is_zero(a)Description
TRUEevaluated 1305 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207483 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
1305-207483
111 return BN_set_word(a, w);
executed 1305 times by 1 test: return BN_set_word(a, w);
Executed by:
  • libcrypto.so.1.1
1305
112 /* handle 'a' when negative */-
113 if (a->neg) {
a->negDescription
TRUEnever evaluated
FALSEevaluated 207483 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-207483
114 a->neg = 0;-
115 i = BN_sub_word(a, w);-
116 if (!BN_is_zero(a))
!BN_is_zero(a)Description
TRUEnever evaluated
FALSEnever evaluated
0
117 a->neg = !(a->neg);
never executed: a->neg = !(a->neg);
0
118 return i;
never executed: return i;
0
119 }-
120 for (i = 0; w != 0 && i < a->top; i++) {
w != 0Description
TRUEevaluated 217503 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 207214 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
i < a->topDescription
TRUEevaluated 217234 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 269 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
269-217503
121 a->d[i] = l = (a->d[i] + w) & BN_MASK2;-
122 w = (w > l) ? 1 : 0;
(w > l)Description
TRUEevaluated 10020 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207214 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
10020-207214
123 }
executed 217234 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
217234
124 if (w && i == a->top) {
wDescription
TRUEevaluated 269 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207214 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
i == a->topDescription
TRUEevaluated 269 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-207214
125 if (bn_wexpand(a, a->top + 1) == NULL)
bn_wexpand(a, ...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 269 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-269
126 return 0;
never executed: return 0;
0
127 a->top++;-
128 a->d[i] = w;-
129 }
executed 269 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
269
130 bn_check_top(a);-
131 return 1;
executed 207483 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
207483
132}-
133-
134int BN_sub_word(BIGNUM *a, BN_ULONG w)-
135{-
136 int i;-
137-
138 bn_check_top(a);-
139 w &= BN_MASK2;-
140-
141 /* degenerate case: w is zero */-
142 if (!w)
!wDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 88354 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
3-88354
143 return 1;
executed 3 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
3
144 /* degenerate case: a is zero */-
145 if (BN_is_zero(a)) {
BN_is_zero(a)Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 88348 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
6-88348
146 i = BN_set_word(a, w);-
147 if (i != 0)
i != 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-6
148 BN_set_negative(a, 1);
executed 6 times by 1 test: BN_set_negative(a, 1);
Executed by:
  • libcrypto.so.1.1
6
149 return i;
executed 6 times by 1 test: return i;
Executed by:
  • libcrypto.so.1.1
6
150 }-
151 /* handle 'a' when negative */-
152 if (a->neg) {
a->negDescription
TRUEnever evaluated
FALSEevaluated 88348 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-88348
153 a->neg = 0;-
154 i = BN_add_word(a, w);-
155 a->neg = 1;-
156 return i;
never executed: return i;
0
157 }-
158-
159 if ((a->top == 1) && (a->d[0] < w)) {
(a->top == 1)Description
TRUEevaluated 501 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 87847 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
(a->d[0] < w)Description
TRUEnever evaluated
FALSEevaluated 501 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-87847
160 a->d[0] = w - a->d[0];-
161 a->neg = 1;-
162 return 1;
never executed: return 1;
0
163 }-
164 i = 0;-
165 for (;;) {-
166 if (a->d[i] >= w) {
a->d[i] >= wDescription
TRUEevaluated 88348 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 80712 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
80712-88348
167 a->d[i] -= w;-
168 break;
executed 88348 times by 2 tests: break;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
88348
169 } else {-
170 a->d[i] = (a->d[i] - w) & BN_MASK2;-
171 i++;-
172 w = 1;-
173 }
executed 80712 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
80712
174 }-
175 if ((a->d[i] == 0) && (i == (a->top - 1)))
(a->d[i] == 0)Description
TRUEevaluated 24803 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 63545 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
(i == (a->top - 1))Description
TRUEevaluated 23812 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 991 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
991-63545
176 a->top--;
executed 23812 times by 1 test: a->top--;
Executed by:
  • libcrypto.so.1.1
23812
177 bn_check_top(a);-
178 return 1;
executed 88348 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
88348
179}-
180-
181int BN_mul_word(BIGNUM *a, BN_ULONG w)-
182{-
183 BN_ULONG ll;-
184-
185 bn_check_top(a);-
186 w &= BN_MASK2;-
187 if (a->top) {
a->topDescription
TRUEevaluated 142222 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 590 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
590-142222
188 if (w == 0)
w == 0Description
TRUEnever evaluated
FALSEevaluated 142222 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-142222
189 BN_zero(a);
never executed: (BN_set_word((a),0));
0
190 else {-
191 ll = bn_mul_words(a->d, a->d, a->top, w);-
192 if (ll) {
llDescription
TRUEevaluated 131406 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 10816 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
10816-131406
193 if (bn_wexpand(a, a->top + 1) == NULL)
bn_wexpand(a, ...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 131406 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-131406
194 return 0;
never executed: return 0;
0
195 a->d[a->top++] = ll;-
196 }
executed 131406 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
131406
197 }
executed 142222 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
142222
198 }-
199 bn_check_top(a);-
200 return 1;
executed 142812 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
142812
201}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2