Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/sm2/sm2_sign.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | int sm2_compute_z_digest(uint8_t *out, | - | ||||||||||||
6 | const EVP_MD *digest, | - | ||||||||||||
7 | const uint8_t *id, | - | ||||||||||||
8 | const size_t id_len, | - | ||||||||||||
9 | const EC_KEY *key) | - | ||||||||||||
10 | { | - | ||||||||||||
11 | int rc = 0; | - | ||||||||||||
12 | const EC_GROUP *group = EC_KEY_get0_group(key); | - | ||||||||||||
13 | BN_CTX *ctx = | - | ||||||||||||
14 | ((void *)0) | - | ||||||||||||
15 | ; | - | ||||||||||||
16 | EVP_MD_CTX *hash = | - | ||||||||||||
17 | ((void *)0) | - | ||||||||||||
18 | ; | - | ||||||||||||
19 | BIGNUM *p = | - | ||||||||||||
20 | ((void *)0) | - | ||||||||||||
21 | ; | - | ||||||||||||
22 | BIGNUM *a = | - | ||||||||||||
23 | ((void *)0) | - | ||||||||||||
24 | ; | - | ||||||||||||
25 | BIGNUM *b = | - | ||||||||||||
26 | ((void *)0) | - | ||||||||||||
27 | ; | - | ||||||||||||
28 | BIGNUM *xG = | - | ||||||||||||
29 | ((void *)0) | - | ||||||||||||
30 | ; | - | ||||||||||||
31 | BIGNUM *yG = | - | ||||||||||||
32 | ((void *)0) | - | ||||||||||||
33 | ; | - | ||||||||||||
34 | BIGNUM *xA = | - | ||||||||||||
35 | ((void *)0) | - | ||||||||||||
36 | ; | - | ||||||||||||
37 | BIGNUM *yA = | - | ||||||||||||
38 | ((void *)0) | - | ||||||||||||
39 | ; | - | ||||||||||||
40 | int p_bytes = 0; | - | ||||||||||||
41 | uint8_t *buf = | - | ||||||||||||
42 | ((void *)0) | - | ||||||||||||
43 | ; | - | ||||||||||||
44 | uint16_t entl = 0; | - | ||||||||||||
45 | uint8_t e_byte = 0; | - | ||||||||||||
46 | - | |||||||||||||
47 | hash = EVP_MD_CTX_new(); | - | ||||||||||||
48 | ctx = BN_CTX_new(); | - | ||||||||||||
49 | if (hash ==
| 0-5 | ||||||||||||
50 | ((void *)0)
| 0-5 | ||||||||||||
51 | || ctx ==
| 0-5 | ||||||||||||
52 | ((void *)0)
| 0-5 | ||||||||||||
53 | ) { | - | ||||||||||||
54 | ERR_put_error(53,(113),((1|64)),__FILE__,47); | - | ||||||||||||
55 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
56 | } | - | ||||||||||||
57 | - | |||||||||||||
58 | p = BN_CTX_get(ctx); | - | ||||||||||||
59 | a = BN_CTX_get(ctx); | - | ||||||||||||
60 | b = BN_CTX_get(ctx); | - | ||||||||||||
61 | xG = BN_CTX_get(ctx); | - | ||||||||||||
62 | yG = BN_CTX_get(ctx); | - | ||||||||||||
63 | xA = BN_CTX_get(ctx); | - | ||||||||||||
64 | yA = BN_CTX_get(ctx); | - | ||||||||||||
65 | - | |||||||||||||
66 | if (yA ==
| 0-5 | ||||||||||||
67 | ((void *)0)
| 0-5 | ||||||||||||
68 | ) { | - | ||||||||||||
69 | ERR_put_error(53,(113),((1|64)),__FILE__,60); | - | ||||||||||||
70 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
71 | } | - | ||||||||||||
72 | - | |||||||||||||
73 | if (!EVP_DigestInit(hash, digest)
| 0-5 | ||||||||||||
74 | ERR_put_error(53,(113),(6),__FILE__,65); | - | ||||||||||||
75 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
76 | } | - | ||||||||||||
77 | - | |||||||||||||
78 | - | |||||||||||||
79 | - | |||||||||||||
80 | if (id_len >= (
| 0-5 | ||||||||||||
81 | (65535)
| 0-5 | ||||||||||||
82 | / 8)
| 0-5 | ||||||||||||
83 | - | |||||||||||||
84 | ERR_put_error(53,(113),(111),__FILE__,73); | - | ||||||||||||
85 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | entl = (uint16_t)(8 * id_len); | - | ||||||||||||
89 | - | |||||||||||||
90 | e_byte = entl >> 8; | - | ||||||||||||
91 | if (!EVP_DigestUpdate(hash, &e_byte, 1)
| 0-5 | ||||||||||||
92 | ERR_put_error(53,(113),(6),__FILE__,81); | - | ||||||||||||
93 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
94 | } | - | ||||||||||||
95 | e_byte = entl & 0xFF; | - | ||||||||||||
96 | if (!EVP_DigestUpdate(hash, &e_byte, 1)
| 0-5 | ||||||||||||
97 | ERR_put_error(53,(113),(6),__FILE__,86); | - | ||||||||||||
98 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
99 | } | - | ||||||||||||
100 | - | |||||||||||||
101 | if (id_len > 0
| 0-5 | ||||||||||||
102 | ERR_put_error(53,(113),(6),__FILE__,91); | - | ||||||||||||
103 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
104 | } | - | ||||||||||||
105 | - | |||||||||||||
106 | if (!EC_GROUP_get_curve(group, p, a, b, ctx)
| 0-5 | ||||||||||||
107 | ERR_put_error(53,(113),(16),__FILE__,96); | - | ||||||||||||
108 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
109 | } | - | ||||||||||||
110 | - | |||||||||||||
111 | p_bytes = ((BN_num_bits(p)+7)/8); | - | ||||||||||||
112 | buf = CRYPTO_zalloc(p_bytes, __FILE__, 101); | - | ||||||||||||
113 | if (buf ==
| 0-5 | ||||||||||||
114 | ((void *)0)
| 0-5 | ||||||||||||
115 | ) { | - | ||||||||||||
116 | ERR_put_error(53,(113),((1|64)),__FILE__,103); | - | ||||||||||||
117 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
118 | } | - | ||||||||||||
119 | - | |||||||||||||
120 | if (BN_bn2binpad(a, buf, p_bytes) < 0
| 0-5 | ||||||||||||
121 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
122 | || BN_bn2binpad(b, buf, p_bytes) < 0
| 0-5 | ||||||||||||
123 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
124 | || !EC_POINT_get_affine_coordinates(group,
| 0-5 | ||||||||||||
125 | EC_GROUP_get0_generator(group),
| 0-5 | ||||||||||||
126 | xG, yG, ctx)
| 0-5 | ||||||||||||
127 | || BN_bn2binpad(xG, buf, p_bytes) < 0
| 0-5 | ||||||||||||
128 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
129 | || BN_bn2binpad(yG, buf, p_bytes) < 0
| 0-5 | ||||||||||||
130 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
131 | || !EC_POINT_get_affine_coordinates(group,
| 0-5 | ||||||||||||
132 | EC_KEY_get0_public_key(key),
| 0-5 | ||||||||||||
133 | xA, yA, ctx)
| 0-5 | ||||||||||||
134 | || BN_bn2binpad(xA, buf, p_bytes) < 0
| 0-5 | ||||||||||||
135 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
136 | || BN_bn2binpad(yA, buf, p_bytes) < 0
| 0-5 | ||||||||||||
137 | || !EVP_DigestUpdate(hash, buf, p_bytes)
| 0-5 | ||||||||||||
138 | || !EVP_DigestFinal(hash, out,
| 0-5 | ||||||||||||
139 | ((void *)0)
| 0-5 | ||||||||||||
140 | )
| 0-5 | ||||||||||||
141 | ERR_put_error(53,(113),((4|64)),__FILE__,126); | - | ||||||||||||
142 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
143 | } | - | ||||||||||||
144 | - | |||||||||||||
145 | rc = 1; | - | ||||||||||||
146 | - | |||||||||||||
147 | done: code before this statement executed 5 times by 2 tests: done: Executed by:
| 5 | ||||||||||||
148 | CRYPTO_free(buf, __FILE__, 133); | - | ||||||||||||
149 | BN_CTX_free(ctx); | - | ||||||||||||
150 | EVP_MD_CTX_free(hash); | - | ||||||||||||
151 | return executed 5 times by 2 tests: rc;return rc; Executed by:
executed 5 times by 2 tests: return rc; Executed by:
| 5 | ||||||||||||
152 | } | - | ||||||||||||
153 | - | |||||||||||||
154 | static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest, | - | ||||||||||||
155 | const EC_KEY *key, | - | ||||||||||||
156 | const uint8_t *id, | - | ||||||||||||
157 | const size_t id_len, | - | ||||||||||||
158 | const uint8_t *msg, size_t msg_len) | - | ||||||||||||
159 | { | - | ||||||||||||
160 | EVP_MD_CTX *hash = EVP_MD_CTX_new(); | - | ||||||||||||
161 | const int md_size = EVP_MD_size(digest); | - | ||||||||||||
162 | uint8_t *z = | - | ||||||||||||
163 | ((void *)0) | - | ||||||||||||
164 | ; | - | ||||||||||||
165 | BIGNUM *e = | - | ||||||||||||
166 | ((void *)0) | - | ||||||||||||
167 | ; | - | ||||||||||||
168 | - | |||||||||||||
169 | if (md_size < 0
| 0-2 | ||||||||||||
170 | ERR_put_error(53,(100),(102),__FILE__,151); | - | ||||||||||||
171 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
172 | } | - | ||||||||||||
173 | - | |||||||||||||
174 | z = CRYPTO_zalloc(md_size, __FILE__, 155); | - | ||||||||||||
175 | if (hash ==
| 0-2 | ||||||||||||
176 | ((void *)0)
| 0-2 | ||||||||||||
177 | || z ==
| 0-2 | ||||||||||||
178 | ((void *)0)
| 0-2 | ||||||||||||
179 | ) { | - | ||||||||||||
180 | ERR_put_error(53,(100),((1|64)),__FILE__,157); | - | ||||||||||||
181 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
182 | } | - | ||||||||||||
183 | - | |||||||||||||
184 | if (!sm2_compute_z_digest(z, digest, id, id_len, key)
| 0-2 | ||||||||||||
185 | - | |||||||||||||
186 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
187 | } | - | ||||||||||||
188 | - | |||||||||||||
189 | if (!EVP_DigestInit(hash, digest)
| 0-2 | ||||||||||||
190 | || !EVP_DigestUpdate(hash, z, md_size)
| 0-2 | ||||||||||||
191 | || !EVP_DigestUpdate(hash, msg, msg_len)
| 0-2 | ||||||||||||
192 | - | |||||||||||||
193 | || !EVP_DigestFinal(hash, z,
| 0-2 | ||||||||||||
194 | ((void *)0)
| 0-2 | ||||||||||||
195 | )
| 0-2 | ||||||||||||
196 | ERR_put_error(53,(100),(6),__FILE__,171); | - | ||||||||||||
197 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
198 | } | - | ||||||||||||
199 | - | |||||||||||||
200 | e = BN_bin2bn(z, md_size, | - | ||||||||||||
201 | ((void *)0) | - | ||||||||||||
202 | ); | - | ||||||||||||
203 | if (e ==
| 0-2 | ||||||||||||
204 | ((void *)0)
| 0-2 | ||||||||||||
205 | ) | - | ||||||||||||
206 | ERR_put_error(53,(100),((4|64)),__FILE__,177); never executed: ERR_put_error(53,(100),((4|64)),__FILE__,177); | 0 | ||||||||||||
207 | - | |||||||||||||
208 | done: code before this statement executed 2 times by 1 test: done: Executed by:
| 2 | ||||||||||||
209 | CRYPTO_free(z, __FILE__, 180); | - | ||||||||||||
210 | EVP_MD_CTX_free(hash); | - | ||||||||||||
211 | return executed 2 times by 1 test: e;return e; Executed by:
executed 2 times by 1 test: return e; Executed by:
| 2 | ||||||||||||
212 | } | - | ||||||||||||
213 | - | |||||||||||||
214 | static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e) | - | ||||||||||||
215 | { | - | ||||||||||||
216 | const BIGNUM *dA = EC_KEY_get0_private_key(key); | - | ||||||||||||
217 | const EC_GROUP *group = EC_KEY_get0_group(key); | - | ||||||||||||
218 | const BIGNUM *order = EC_GROUP_get0_order(group); | - | ||||||||||||
219 | ECDSA_SIG *sig = | - | ||||||||||||
220 | ((void *)0) | - | ||||||||||||
221 | ; | - | ||||||||||||
222 | EC_POINT *kG = | - | ||||||||||||
223 | ((void *)0) | - | ||||||||||||
224 | ; | - | ||||||||||||
225 | BN_CTX *ctx = | - | ||||||||||||
226 | ((void *)0) | - | ||||||||||||
227 | ; | - | ||||||||||||
228 | BIGNUM *k = | - | ||||||||||||
229 | ((void *)0) | - | ||||||||||||
230 | ; | - | ||||||||||||
231 | BIGNUM *rk = | - | ||||||||||||
232 | ((void *)0) | - | ||||||||||||
233 | ; | - | ||||||||||||
234 | BIGNUM *r = | - | ||||||||||||
235 | ((void *)0) | - | ||||||||||||
236 | ; | - | ||||||||||||
237 | BIGNUM *s = | - | ||||||||||||
238 | ((void *)0) | - | ||||||||||||
239 | ; | - | ||||||||||||
240 | BIGNUM *x1 = | - | ||||||||||||
241 | ((void *)0) | - | ||||||||||||
242 | ; | - | ||||||||||||
243 | BIGNUM *tmp = | - | ||||||||||||
244 | ((void *)0) | - | ||||||||||||
245 | ; | - | ||||||||||||
246 | - | |||||||||||||
247 | kG = EC_POINT_new(group); | - | ||||||||||||
248 | ctx = BN_CTX_new(); | - | ||||||||||||
249 | if (kG ==
| 0-3 | ||||||||||||
250 | ((void *)0)
| 0-3 | ||||||||||||
251 | || ctx ==
| 0-3 | ||||||||||||
252 | ((void *)0)
| 0-3 | ||||||||||||
253 | ) { | - | ||||||||||||
254 | ERR_put_error(53,(106),((1|64)),__FILE__,203); | - | ||||||||||||
255 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
256 | } | - | ||||||||||||
257 | - | |||||||||||||
258 | BN_CTX_start(ctx); | - | ||||||||||||
259 | k = BN_CTX_get(ctx); | - | ||||||||||||
260 | rk = BN_CTX_get(ctx); | - | ||||||||||||
261 | x1 = BN_CTX_get(ctx); | - | ||||||||||||
262 | tmp = BN_CTX_get(ctx); | - | ||||||||||||
263 | if (tmp ==
| 0-3 | ||||||||||||
264 | ((void *)0)
| 0-3 | ||||||||||||
265 | ) { | - | ||||||||||||
266 | ERR_put_error(53,(106),((1|64)),__FILE__,213); | - | ||||||||||||
267 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
268 | } | - | ||||||||||||
269 | - | |||||||||||||
270 | - | |||||||||||||
271 | - | |||||||||||||
272 | - | |||||||||||||
273 | - | |||||||||||||
274 | r = BN_new(); | - | ||||||||||||
275 | s = BN_new(); | - | ||||||||||||
276 | - | |||||||||||||
277 | if (r ==
| 0-3 | ||||||||||||
278 | ((void *)0)
| 0-3 | ||||||||||||
279 | || s ==
| 0-3 | ||||||||||||
280 | ((void *)0)
| 0-3 | ||||||||||||
281 | ) { | - | ||||||||||||
282 | ERR_put_error(53,(106),((1|64)),__FILE__,225); | - | ||||||||||||
283 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
284 | } | - | ||||||||||||
285 | - | |||||||||||||
286 | for (;;) { | - | ||||||||||||
287 | if (!BN_priv_rand_range(k, order)
| 0-3 | ||||||||||||
288 | ERR_put_error(53,(106),((4|64)),__FILE__,231); | - | ||||||||||||
289 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
290 | } | - | ||||||||||||
291 | - | |||||||||||||
292 | if (!EC_POINT_mul(group, kG, k,
| 0-3 | ||||||||||||
293 | ((void *)0)
| 0-3 | ||||||||||||
294 | ,
| 0-3 | ||||||||||||
295 | ((void *)0)
| 0-3 | ||||||||||||
296 | , ctx)
| 0-3 | ||||||||||||
297 | || !EC_POINT_get_affine_coordinates(group, kG, x1,
| 0-3 | ||||||||||||
298 | ((void *)0)
| 0-3 | ||||||||||||
299 | ,
| 0-3 | ||||||||||||
300 | ctx)
| 0-3 | ||||||||||||
301 | || !BN_mod_add(r, e, x1, order, ctx)
| 0-3 | ||||||||||||
302 | ERR_put_error(53,(106),((4|64)),__FILE__,239); | - | ||||||||||||
303 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
304 | } | - | ||||||||||||
305 | - | |||||||||||||
306 | - | |||||||||||||
307 | if (BN_is_zero(r)
| 0-3 | ||||||||||||
308 | continue; never executed: continue; | 0 | ||||||||||||
309 | - | |||||||||||||
310 | if (!BN_add(rk, r, k)
| 0-3 | ||||||||||||
311 | ERR_put_error(53,(106),((4|64)),__FILE__,248); | - | ||||||||||||
312 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
313 | } | - | ||||||||||||
314 | - | |||||||||||||
315 | if (BN_cmp(rk, order) == 0
| 0-3 | ||||||||||||
316 | continue; never executed: continue; | 0 | ||||||||||||
317 | - | |||||||||||||
318 | if (!BN_add(s, dA, BN_value_one())
| 0-3 | ||||||||||||
319 | || !ec_group_do_inverse_ord(group, s, s, ctx)
| 0-3 | ||||||||||||
320 | || !BN_mod_mul(tmp, dA, r, order, ctx)
| 0-3 | ||||||||||||
321 | || !BN_sub(tmp, k, tmp)
| 0-3 | ||||||||||||
322 | || !BN_mod_mul(s, s, tmp, order, ctx)
| 0-3 | ||||||||||||
323 | ERR_put_error(53,(106),(3),__FILE__,260); | - | ||||||||||||
324 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
325 | } | - | ||||||||||||
326 | - | |||||||||||||
327 | sig = ECDSA_SIG_new(); | - | ||||||||||||
328 | if (sig ==
| 0-3 | ||||||||||||
329 | ((void *)0)
| 0-3 | ||||||||||||
330 | ) { | - | ||||||||||||
331 | ERR_put_error(53,(106),((1|64)),__FILE__,266); | - | ||||||||||||
332 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
333 | } | - | ||||||||||||
334 | - | |||||||||||||
335 | - | |||||||||||||
336 | ECDSA_SIG_set0(sig, r, s); | - | ||||||||||||
337 | break; executed 3 times by 2 tests: break; Executed by:
| 3 | ||||||||||||
338 | } | - | ||||||||||||
339 | - | |||||||||||||
340 | done: code before this statement executed 3 times by 2 tests: done: Executed by:
| 3 | ||||||||||||
341 | if (sig ==
| 0-3 | ||||||||||||
342 | ((void *)0)
| 0-3 | ||||||||||||
343 | ) { | - | ||||||||||||
344 | BN_free(r); | - | ||||||||||||
345 | BN_free(s); | - | ||||||||||||
346 | } never executed: end of block | 0 | ||||||||||||
347 | - | |||||||||||||
348 | BN_CTX_free(ctx); | - | ||||||||||||
349 | EC_POINT_free(kG); | - | ||||||||||||
350 | return executed 3 times by 2 tests: sig;return sig; Executed by:
executed 3 times by 2 tests: return sig; Executed by:
| 3 | ||||||||||||
351 | } | - | ||||||||||||
352 | - | |||||||||||||
353 | static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig, | - | ||||||||||||
354 | const BIGNUM *e) | - | ||||||||||||
355 | { | - | ||||||||||||
356 | int ret = 0; | - | ||||||||||||
357 | const EC_GROUP *group = EC_KEY_get0_group(key); | - | ||||||||||||
358 | const BIGNUM *order = EC_GROUP_get0_order(group); | - | ||||||||||||
359 | BN_CTX *ctx = | - | ||||||||||||
360 | ((void *)0) | - | ||||||||||||
361 | ; | - | ||||||||||||
362 | EC_POINT *pt = | - | ||||||||||||
363 | ((void *)0) | - | ||||||||||||
364 | ; | - | ||||||||||||
365 | BIGNUM *t = | - | ||||||||||||
366 | ((void *)0) | - | ||||||||||||
367 | ; | - | ||||||||||||
368 | BIGNUM *x1 = | - | ||||||||||||
369 | ((void *)0) | - | ||||||||||||
370 | ; | - | ||||||||||||
371 | const BIGNUM *r = | - | ||||||||||||
372 | ((void *)0) | - | ||||||||||||
373 | ; | - | ||||||||||||
374 | const BIGNUM *s = | - | ||||||||||||
375 | ((void *)0) | - | ||||||||||||
376 | ; | - | ||||||||||||
377 | - | |||||||||||||
378 | ctx = BN_CTX_new(); | - | ||||||||||||
379 | pt = EC_POINT_new(group); | - | ||||||||||||
380 | if (ctx ==
| 0-6 | ||||||||||||
381 | ((void *)0)
| 0-6 | ||||||||||||
382 | || pt ==
| 0-6 | ||||||||||||
383 | ((void *)0)
| 0-6 | ||||||||||||
384 | ) { | - | ||||||||||||
385 | ERR_put_error(53,(107),((1|64)),__FILE__,302); | - | ||||||||||||
386 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
387 | } | - | ||||||||||||
388 | - | |||||||||||||
389 | BN_CTX_start(ctx); | - | ||||||||||||
390 | t = BN_CTX_get(ctx); | - | ||||||||||||
391 | x1 = BN_CTX_get(ctx); | - | ||||||||||||
392 | if (x1 ==
| 0-6 | ||||||||||||
393 | ((void *)0)
| 0-6 | ||||||||||||
394 | ) { | - | ||||||||||||
395 | ERR_put_error(53,(107),((1|64)),__FILE__,310); | - | ||||||||||||
396 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
397 | } | - | ||||||||||||
398 | ECDSA_SIG_get0(sig, &r, &s); | - | ||||||||||||
399 | - | |||||||||||||
400 | if (BN_cmp(r, BN_value_one()) < 0
| 0-6 | ||||||||||||
401 | || BN_cmp(s, BN_value_one()) < 0
| 0-6 | ||||||||||||
402 | || BN_cmp(order, r) <= 0
| 0-6 | ||||||||||||
403 | || BN_cmp(order, s) <= 0
| 0-6 | ||||||||||||
404 | ERR_put_error(53,(107),(101),__FILE__,330); | - | ||||||||||||
405 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
406 | } | - | ||||||||||||
407 | - | |||||||||||||
408 | if (!BN_mod_add(t, r, s, order, ctx)
| 0-6 | ||||||||||||
409 | ERR_put_error(53,(107),(3),__FILE__,335); | - | ||||||||||||
410 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
411 | } | - | ||||||||||||
412 | - | |||||||||||||
413 | if (BN_is_zero(t)
| 0-6 | ||||||||||||
414 | ERR_put_error(53,(107),(101),__FILE__,340); | - | ||||||||||||
415 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
416 | } | - | ||||||||||||
417 | - | |||||||||||||
418 | if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
| 0-6 | ||||||||||||
419 | || !EC_POINT_get_affine_coordinates(group, pt, x1,
| 0-6 | ||||||||||||
420 | ((void *)0)
| 0-6 | ||||||||||||
421 | , ctx)
| 0-6 | ||||||||||||
422 | ERR_put_error(53,(107),(16),__FILE__,346); | - | ||||||||||||
423 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
424 | } | - | ||||||||||||
425 | - | |||||||||||||
426 | if (!BN_mod_add(t, e, x1, order, ctx)
| 0-6 | ||||||||||||
427 | ERR_put_error(53,(107),(3),__FILE__,351); | - | ||||||||||||
428 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
429 | } | - | ||||||||||||
430 | - | |||||||||||||
431 | if (BN_cmp(r, t) == 0
| 0-6 | ||||||||||||
432 | ret = 1; executed 6 times by 2 tests: ret = 1; Executed by:
| 6 | ||||||||||||
433 | - | |||||||||||||
434 | done: code before this statement executed 6 times by 2 tests: done: Executed by:
| 6 | ||||||||||||
435 | EC_POINT_free(pt); | - | ||||||||||||
436 | BN_CTX_free(ctx); | - | ||||||||||||
437 | return executed 6 times by 2 tests: ret;return ret; Executed by:
executed 6 times by 2 tests: return ret; Executed by:
| 6 | ||||||||||||
438 | } | - | ||||||||||||
439 | - | |||||||||||||
440 | ECDSA_SIG *sm2_do_sign(const EC_KEY *key, | - | ||||||||||||
441 | const EVP_MD *digest, | - | ||||||||||||
442 | const uint8_t *id, | - | ||||||||||||
443 | const size_t id_len, | - | ||||||||||||
444 | const uint8_t *msg, size_t msg_len) | - | ||||||||||||
445 | { | - | ||||||||||||
446 | BIGNUM *e = | - | ||||||||||||
447 | ((void *)0) | - | ||||||||||||
448 | ; | - | ||||||||||||
449 | ECDSA_SIG *sig = | - | ||||||||||||
450 | ((void *)0) | - | ||||||||||||
451 | ; | - | ||||||||||||
452 | - | |||||||||||||
453 | e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len); | - | ||||||||||||
454 | if (e ==
| 0-1 | ||||||||||||
455 | ((void *)0)
| 0-1 | ||||||||||||
456 | ) { | - | ||||||||||||
457 | - | |||||||||||||
458 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
459 | } | - | ||||||||||||
460 | - | |||||||||||||
461 | sig = sm2_sig_gen(key, e); | - | ||||||||||||
462 | - | |||||||||||||
463 | done: code before this statement executed 1 time by 1 test: done: Executed by:
| 1 | ||||||||||||
464 | BN_free(e); | - | ||||||||||||
465 | return executed 1 time by 1 test: sig;return sig; Executed by:
executed 1 time by 1 test: return sig; Executed by:
| 1 | ||||||||||||
466 | } | - | ||||||||||||
467 | - | |||||||||||||
468 | int sm2_do_verify(const EC_KEY *key, | - | ||||||||||||
469 | const EVP_MD *digest, | - | ||||||||||||
470 | const ECDSA_SIG *sig, | - | ||||||||||||
471 | const uint8_t *id, | - | ||||||||||||
472 | const size_t id_len, | - | ||||||||||||
473 | const uint8_t *msg, size_t msg_len) | - | ||||||||||||
474 | { | - | ||||||||||||
475 | BIGNUM *e = | - | ||||||||||||
476 | ((void *)0) | - | ||||||||||||
477 | ; | - | ||||||||||||
478 | int ret = 0; | - | ||||||||||||
479 | - | |||||||||||||
480 | e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len); | - | ||||||||||||
481 | if (e ==
| 0-1 | ||||||||||||
482 | ((void *)0)
| 0-1 | ||||||||||||
483 | ) { | - | ||||||||||||
484 | - | |||||||||||||
485 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
486 | } | - | ||||||||||||
487 | - | |||||||||||||
488 | ret = sm2_sig_verify(key, sig, e); | - | ||||||||||||
489 | - | |||||||||||||
490 | done: code before this statement executed 1 time by 1 test: done: Executed by:
| 1 | ||||||||||||
491 | BN_free(e); | - | ||||||||||||
492 | return executed 1 time by 1 test: ret;return ret; Executed by:
executed 1 time by 1 test: return ret; Executed by:
| 1 | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | int sm2_sign(const unsigned char *dgst, int dgstlen, | - | ||||||||||||
496 | unsigned char *sig, unsigned int *siglen, EC_KEY *eckey) | - | ||||||||||||
497 | { | - | ||||||||||||
498 | BIGNUM *e = | - | ||||||||||||
499 | ((void *)0) | - | ||||||||||||
500 | ; | - | ||||||||||||
501 | ECDSA_SIG *s = | - | ||||||||||||
502 | ((void *)0) | - | ||||||||||||
503 | ; | - | ||||||||||||
504 | int sigleni; | - | ||||||||||||
505 | int ret = -1; | - | ||||||||||||
506 | - | |||||||||||||
507 | e = BN_bin2bn(dgst, dgstlen, | - | ||||||||||||
508 | ((void *)0) | - | ||||||||||||
509 | ); | - | ||||||||||||
510 | if (e ==
| 0-2 | ||||||||||||
511 | ((void *)0)
| 0-2 | ||||||||||||
512 | ) { | - | ||||||||||||
513 | ERR_put_error(53,(105),(3),__FILE__,419); | - | ||||||||||||
514 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
515 | } | - | ||||||||||||
516 | - | |||||||||||||
517 | s = sm2_sig_gen(eckey, e); | - | ||||||||||||
518 | - | |||||||||||||
519 | sigleni = i2d_ECDSA_SIG(s, &sig); | - | ||||||||||||
520 | if (sigleni < 0
| 0-2 | ||||||||||||
521 | ERR_put_error(53,(105),((4|64)),__FILE__,427); | - | ||||||||||||
522 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
523 | } | - | ||||||||||||
524 | *siglen = (unsigned int)sigleni; | - | ||||||||||||
525 | - | |||||||||||||
526 | ret = 1; | - | ||||||||||||
527 | - | |||||||||||||
528 | done: code before this statement executed 2 times by 1 test: done: Executed by:
| 2 | ||||||||||||
529 | ECDSA_SIG_free(s); | - | ||||||||||||
530 | BN_free(e); | - | ||||||||||||
531 | return executed 2 times by 1 test: ret;return ret; Executed by:
executed 2 times by 1 test: return ret; Executed by:
| 2 | ||||||||||||
532 | } | - | ||||||||||||
533 | - | |||||||||||||
534 | int sm2_verify(const unsigned char *dgst, int dgstlen, | - | ||||||||||||
535 | const unsigned char *sig, int sig_len, EC_KEY *eckey) | - | ||||||||||||
536 | { | - | ||||||||||||
537 | ECDSA_SIG *s = | - | ||||||||||||
538 | ((void *)0) | - | ||||||||||||
539 | ; | - | ||||||||||||
540 | BIGNUM *e = | - | ||||||||||||
541 | ((void *)0) | - | ||||||||||||
542 | ; | - | ||||||||||||
543 | const unsigned char *p = sig; | - | ||||||||||||
544 | unsigned char *der = | - | ||||||||||||
545 | ((void *)0) | - | ||||||||||||
546 | ; | - | ||||||||||||
547 | int derlen = -1; | - | ||||||||||||
548 | int ret = -1; | - | ||||||||||||
549 | - | |||||||||||||
550 | s = ECDSA_SIG_new(); | - | ||||||||||||
551 | if (s ==
| 0-5 | ||||||||||||
552 | ((void *)0)
| 0-5 | ||||||||||||
553 | ) { | - | ||||||||||||
554 | ERR_put_error(53,(108),((1|64)),__FILE__,452); | - | ||||||||||||
555 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
556 | } | - | ||||||||||||
557 | if (d2i_ECDSA_SIG(&s, &p, sig_len) ==
| 0-5 | ||||||||||||
558 | ((void *)0)
| 0-5 | ||||||||||||
559 | ) { | - | ||||||||||||
560 | ERR_put_error(53,(108),(104),__FILE__,456); | - | ||||||||||||
561 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
562 | } | - | ||||||||||||
563 | - | |||||||||||||
564 | derlen = i2d_ECDSA_SIG(s, &der); | - | ||||||||||||
565 | if (derlen != sig_len
| 0-5 | ||||||||||||
566 | ERR_put_error(53,(108),(104),__FILE__,462); | - | ||||||||||||
567 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
568 | } | - | ||||||||||||
569 | - | |||||||||||||
570 | e = BN_bin2bn(dgst, dgstlen, | - | ||||||||||||
571 | ((void *)0) | - | ||||||||||||
572 | ); | - | ||||||||||||
573 | if (e ==
| 0-5 | ||||||||||||
574 | ((void *)0)
| 0-5 | ||||||||||||
575 | ) { | - | ||||||||||||
576 | ERR_put_error(53,(108),(3),__FILE__,468); | - | ||||||||||||
577 | goto never executed: done;goto done; never executed: goto done; | 0 | ||||||||||||
578 | } | - | ||||||||||||
579 | - | |||||||||||||
580 | ret = sm2_sig_verify(eckey, s, e); | - | ||||||||||||
581 | - | |||||||||||||
582 | done: code before this statement executed 5 times by 1 test: done: Executed by:
| 5 | ||||||||||||
583 | CRYPTO_free(der, __FILE__, 475); | - | ||||||||||||
584 | BN_free(e); | - | ||||||||||||
585 | ECDSA_SIG_free(s); | - | ||||||||||||
586 | return executed 5 times by 1 test: ret;return ret; Executed by:
executed 5 times by 1 test: return ret; Executed by:
| 5 | ||||||||||||
587 | } | - | ||||||||||||
Switch to Source code | Preprocessed file |