OpenCoverage

drbg_ctr.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/rand/drbg_ctr.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 2011-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 <stdlib.h>-
11#include <string.h>-
12#include <openssl/crypto.h>-
13#include <openssl/err.h>-
14#include <openssl/rand.h>-
15#include "internal/thread_once.h"-
16#include "internal/thread_once.h"-
17#include "rand_lcl.h"-
18/*-
19 * Implementation of NIST SP 800-90A CTR DRBG.-
20 */-
21-
22static void inc_128(RAND_DRBG_CTR *ctr)-
23{-
24 int i;-
25 unsigned char c;-
26 unsigned char *p = &ctr->V[15];-
27-
28 for (i = 0; i < 16; i++, p--) {
i < 16Description
TRUEevaluated 17777205 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEnever evaluated
0-17777205
29 c = *p;-
30 c++;-
31 *p = c;-
32 if (c != 0) {
c != 0Description
TRUEevaluated 17923679 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 77099 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
77099-17923679
33 /* If we didn't wrap around, we're done. */-
34 break;
executed 17729011 times by 2 tests: break;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
17729011
35 }-
36 }
executed 77097 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
77097
37}
executed 17675448 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
17675448
38-
39static void ctr_XOR(RAND_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)-
40{-
41 size_t i, n;-
42-
43 if (in == NULL || inlen == 0)
in == ((void *)0)Description
TRUEevaluated 7218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2100727 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
inlen == 0Description
TRUEevaluated 3600 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2113562 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
3600-2113562
44 return;
executed 10818 times by 1 test: return;
Executed by:
  • libcrypto.so.1.1
10818
45-
46 /*-
47 * Any zero padding will have no effect on the result as we-
48 * are XORing. So just process however much input we have.-
49 */-
50 n = inlen < ctr->keylen ? inlen : ctr->keylen;
inlen < ctr->keylenDescription
TRUEnever evaluated
FALSEevaluated 2107945 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2107945
51 for (i = 0; i < n; i++)
i < nDescription
TRUEevaluated 65699309 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2133729 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
2133729-65699309
52 ctr->K[i] ^= in[i];
executed 65704331 times by 2 tests: ctr->K[i] ^= in[i];
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
65704331
53 if (inlen <= ctr->keylen)
inlen <= ctr->keylenDescription
TRUEnever evaluated
FALSEevaluated 2120534 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2120534
54 return;
never executed: return;
0
55-
56 n = inlen - ctr->keylen;-
57 if (n > 16) {
n > 16Description
TRUEnever evaluated
FALSEevaluated 2116629 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2116629
58 /* Should never happen */-
59 n = 16;-
60 }
never executed: end of block
0
61 for (i = 0; i < n; i++)
i < nDescription
TRUEevaluated 33413748 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2131999 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
2131999-33413748
62 ctr->V[i] ^= in[i + ctr->keylen];
executed 33447824 times by 2 tests: ctr->V[i] ^= in[i + ctr->keylen];
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
33447824
63}
executed 2119266 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2119266
64-
65/*-
66 * Process a complete block using BCC algorithm of SP 800-90A 10.3.3-
67 */-
68__owur static int ctr_BCC_block(RAND_DRBG_CTR *ctr, unsigned char *out,-
69 const unsigned char *in)-
70{-
71 int i, outlen = AES_BLOCK_SIZE;-
72-
73 for (i = 0; i < 16; i++)
i < 16Description
TRUEevaluated 144141478 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 9487527 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
9487527-144141478
74 out[i] ^= in[i];
executed 144315469 times by 2 tests: out[i] ^= in[i];
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
144315469
75-
76 if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, AES_BLOCK_SIZE)
!EVP_CipherUpd...tlen, out, 16)Description
TRUEnever evaluated
FALSEevaluated 9107744 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-9107744
77 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 9277088 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-9277088
78 return 0;
never executed: return 0;
0
79 return 1;
executed 9242579 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
9242579
80}-
81-
82-
83/*-
84 * Handle several BCC operations for as much data as we need for K and X-
85 */-
86__owur static int ctr_BCC_blocks(RAND_DRBG_CTR *ctr, const unsigned char *in)-
87{-
88 if (!ctr_BCC_block(ctr, ctr->KX, in)
!ctr_BCC_block..., ctr->KX, in)Description
TRUEnever evaluated
FALSEevaluated 2126716 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2126716
89 || !ctr_BCC_block(ctr, ctr->KX + 16, in))
!ctr_BCC_block...->KX + 16, in)Description
TRUEnever evaluated
FALSEevaluated 2126487 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2126487
90 return 0;
never executed: return 0;
0
91 if (ctr->keylen != 16 && !ctr_BCC_block(ctr, ctr->KX + 32, in))
ctr->keylen != 16Description
TRUEevaluated 2125097 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 5779 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
!ctr_BCC_block...->KX + 32, in)Description
TRUEnever evaluated
FALSEevaluated 2122961 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2125097
92 return 0;
never executed: return 0;
0
93 return 1;
executed 2128112 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2128112
94}-
95-
96/*-
97 * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:-
98 * see 10.3.1 stage 7.-
99 */-
100__owur static int ctr_BCC_init(RAND_DRBG_CTR *ctr)-
101{-
102 memset(ctr->KX, 0, 48);-
103 memset(ctr->bltmp, 0, 16);-
104 if (!ctr_BCC_block(ctr, ctr->KX, ctr->bltmp))
!ctr_BCC_block...X, ctr->bltmp)Description
TRUEnever evaluated
FALSEevaluated 1048909 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1048909
105 return 0;
never executed: return 0;
0
106 ctr->bltmp[3] = 1;-
107 if (!ctr_BCC_block(ctr, ctr->KX + 16, ctr->bltmp))
!ctr_BCC_block...6, ctr->bltmp)Description
TRUEnever evaluated
FALSEevaluated 1054781 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1054781
108 return 0;
never executed: return 0;
0
109 if (ctr->keylen != 16) {
ctr->keylen != 16Description
TRUEevaluated 1055022 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2406 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
2406-1055022
110 ctr->bltmp[3] = 2;-
111 if (!ctr_BCC_block(ctr, ctr->KX + 32, ctr->bltmp))
!ctr_BCC_block...2, ctr->bltmp)Description
TRUEnever evaluated
FALSEevaluated 1052786 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1052786
112 return 0;
never executed: return 0;
0
113 }
executed 1052231 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1052231
114 return 1;
executed 1056216 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1056216
115}-
116-
117/*-
118 * Process several blocks into BCC algorithm, some possibly partial-
119 */-
120__owur static int ctr_BCC_update(RAND_DRBG_CTR *ctr,-
121 const unsigned char *in, size_t inlen)-
122{-
123 if (in == NULL || inlen == 0)
in == ((void *)0)Description
TRUEevaluated 2109284 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2116530 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
inlen == 0Description
TRUEevaluated 3600 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2127705 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
3600-2127705
124 return 1;
executed 2114178 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2114178
125-
126 /* If we have partial block handle it first */-
127 if (ctr->bltmp_pos) {
ctr->bltmp_posDescription
TRUEevaluated 2121858 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 4757 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
4757-2121858
128 size_t left = 16 - ctr->bltmp_pos;-
129-
130 /* If we now have a complete block process it */-
131 if (inlen >= left) {
inlen >= leftDescription
TRUEevaluated 1068528 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 1062805 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
1062805-1068528
132 memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);-
133 if (!ctr_BCC_blocks(ctr, ctr->bltmp))
!ctr_BCC_block...r, ctr->bltmp)Description
TRUEnever evaluated
FALSEevaluated 1061982 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1061982
134 return 0;
never executed: return 0;
0
135 ctr->bltmp_pos = 0;-
136 inlen -= left;-
137 in += left;-
138 }
executed 1060802 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1060802
139 }
executed 2120769 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2120769
140-
141 /* Process zero or more complete blocks */-
142 for (; inlen >= 16; in += 16, inlen -= 16) {
inlen >= 16Description
TRUEevaluated 14057 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2122941 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
14057-2122941
143 if (!ctr_BCC_blocks(ctr, in))
!ctr_BCC_blocks(ctr, in)Description
TRUEnever evaluated
FALSEevaluated 14057 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-14057
144 return 0;
never executed: return 0;
0
145 }
executed 14057 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
14057
146-
147 /* Copy any remaining partial block to the temporary buffer */-
148 if (inlen > 0) {
inlen > 0Description
TRUEevaluated 2116703 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 4757 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
4757-2116703
149 memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);-
150 ctr->bltmp_pos += inlen;-
151 }
executed 2118794 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2118794
152 return 1;
executed 2128649 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2128649
153}-
154-
155__owur static int ctr_BCC_final(RAND_DRBG_CTR *ctr)-
156{-
157 if (ctr->bltmp_pos) {
ctr->bltmp_posDescription
TRUEevaluated 1065727 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEnever evaluated
0-1065727
158 memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);-
159 if (!ctr_BCC_blocks(ctr, ctr->bltmp))
!ctr_BCC_block...r, ctr->bltmp)Description
TRUEnever evaluated
FALSEevaluated 1058357 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1058357
160 return 0;
never executed: return 0;
0
161 }
executed 1058171 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1058171
162 return 1;
executed 1060204 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1060204
163}-
164-
165__owur static int ctr_df(RAND_DRBG_CTR *ctr,-
166 const unsigned char *in1, size_t in1len,-
167 const unsigned char *in2, size_t in2len,-
168 const unsigned char *in3, size_t in3len)-
169{-
170 static unsigned char c80 = 0x80;-
171 size_t inlen;-
172 unsigned char *p = ctr->bltmp;-
173 int outlen = AES_BLOCK_SIZE;-
174-
175 if (!ctr_BCC_init(ctr))
!ctr_BCC_init(ctr)Description
TRUEnever evaluated
FALSEevaluated 1054484 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1054484
176 return 0;
never executed: return 0;
0
177 if (in1 == NULL)
in1 == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1056829 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1056829
178 in1len = 0;
never executed: in1len = 0;
0
179 if (in2 == NULL)
in2 == ((void *)0)Description
TRUEevaluated 1058043 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2947 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
2947-1058043
180 in2len = 0;
executed 1057171 times by 2 tests: in2len = 0;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1057171
181 if (in3 == NULL)
in3 == ((void *)0)Description
TRUEevaluated 1055418 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 6171 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
6171-1055418
182 in3len = 0;
executed 1057536 times by 2 tests: in3len = 0;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1057536
183 inlen = in1len + in2len + in3len;-
184 /* Initialise L||N in temporary block */-
185 *p++ = (inlen >> 24) & 0xff;-
186 *p++ = (inlen >> 16) & 0xff;-
187 *p++ = (inlen >> 8) & 0xff;-
188 *p++ = inlen & 0xff;-
189-
190 /* NB keylen is at most 32 bytes */-
191 *p++ = 0;-
192 *p++ = 0;-
193 *p++ = 0;-
194 *p = (unsigned char)((ctr->keylen + 16) & 0xff);-
195 ctr->bltmp_pos = 8;-
196 if (!ctr_BCC_update(ctr, in1, in1len)
!ctr_BCC_updat..., in1, in1len)Description
TRUEnever evaluated
FALSEevaluated 1060519 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1060519
197 || !ctr_BCC_update(ctr, in2, in2len)
!ctr_BCC_updat..., in2, in2len)Description
TRUEnever evaluated
FALSEevaluated 1061657 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1061657
198 || !ctr_BCC_update(ctr, in3, in3len)
!ctr_BCC_updat..., in3, in3len)Description
TRUEnever evaluated
FALSEevaluated 1061592 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1061592
199 || !ctr_BCC_update(ctr, &c80, 1)
!ctr_BCC_update(ctr, &c80, 1)Description
TRUEnever evaluated
FALSEevaluated 1067026 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1067026
200 || !ctr_BCC_final(ctr))
!ctr_BCC_final(ctr)Description
TRUEnever evaluated
FALSEevaluated 1059890 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1059890
201 return 0;
never executed: return 0;
0
202 /* Set up key K */-
203 if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->KX, NULL, 1))
!EVP_CipherIni...void *)0) , 1)Description
TRUEnever evaluated
FALSEevaluated 1039622 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1039622
204 return 0;
never executed: return 0;
0
205 /* X follows key K */-
206 if (!EVP_CipherUpdate(ctr->ctx, ctr->KX, &outlen, ctr->KX + ctr->keylen,
!EVP_CipherUpd...r->keylen, 16)Description
TRUEnever evaluated
FALSEevaluated 1033080 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1033080
207 AES_BLOCK_SIZE)
!EVP_CipherUpd...r->keylen, 16)Description
TRUEnever evaluated
FALSEevaluated 1033080 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1033080
208 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 1032116 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1032116
209 return 0;
never executed: return 0;
0
210 if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 16, &outlen, ctr->KX,
!EVP_CipherUpd..., ctr->KX, 16)Description
TRUEnever evaluated
FALSEevaluated 1036967 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036967
211 AES_BLOCK_SIZE)
!EVP_CipherUpd..., ctr->KX, 16)Description
TRUEnever evaluated
FALSEevaluated 1036967 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036967
212 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 1053122 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1053122
213 return 0;
never executed: return 0;
0
214 if (ctr->keylen != 16)
ctr->keylen != 16Description
TRUEevaluated 1048801 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2406 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
2406-1048801
215 if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 32, &outlen, ctr->KX + 16,
!EVP_CipherUpd...->KX + 16, 16)Description
TRUEnever evaluated
FALSEevaluated 1036021 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036021
216 AES_BLOCK_SIZE)
!EVP_CipherUpd...->KX + 16, 16)Description
TRUEnever evaluated
FALSEevaluated 1036021 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036021
217 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 1036464 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036464
218 return 0;
never executed: return 0;
0
219 return 1;
executed 1037211 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1037211
220}-
221-
222/*-
223 * NB the no-df Update in SP800-90A specifies a constant input length-
224 * of seedlen, however other uses of this algorithm pad the input with-
225 * zeroes if necessary and have up to two parameters XORed together,-
226 * so we handle both cases in this function instead.-
227 */-
228__owur static int ctr_update(RAND_DRBG *drbg,-
229 const unsigned char *in1, size_t in1len,-
230 const unsigned char *in2, size_t in2len,-
231 const unsigned char *nonce, size_t noncelen)-
232{-
233 RAND_DRBG_CTR *ctr = &drbg->data.ctr;-
234 int outlen = AES_BLOCK_SIZE;-
235-
236 /* correct key is already set up. */-
237 inc_128(ctr);-
238 if (!EVP_CipherUpdate(ctr->ctx, ctr->K, &outlen, ctr->V, AES_BLOCK_SIZE)
!EVP_CipherUpd...n, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 2077957 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2077957
239 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 2090106 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2090106
240 return 0;
never executed: return 0;
0
241-
242 /* If keylen longer than 128 bits need extra encrypt */-
243 if (ctr->keylen != 16) {
ctr->keylen != 16Description
TRUEevaluated 2084114 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 6738 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6738-2084114
244 inc_128(ctr);-
245 if (!EVP_CipherUpdate(ctr->ctx, ctr->K+16, &outlen, ctr->V,
!EVP_CipherUpd...n, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 2083800 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2083800
246 AES_BLOCK_SIZE)
!EVP_CipherUpd...n, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 2083800 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2083800
247 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 2085401 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2085401
248 return 0;
never executed: return 0;
0
249 }
executed 2077559 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2077559
250 inc_128(ctr);-
251 if (!EVP_CipherUpdate(ctr->ctx, ctr->V, &outlen, ctr->V, AES_BLOCK_SIZE)
!EVP_CipherUpd...n, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 2091943 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2091943
252 || outlen != AES_BLOCK_SIZE)
outlen != 16Description
TRUEnever evaluated
FALSEevaluated 2109462 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2109462
253 return 0;
never executed: return 0;
0
254-
255 /* If 192 bit key part of V is on end of K */-
256 if (ctr->keylen == 24) {
ctr->keylen == 24Description
TRUEevaluated 6738 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2099521 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
6738-2099521
257 memcpy(ctr->V + 8, ctr->V, 8);-
258 memcpy(ctr->V, ctr->K + 24, 8);-
259 }
executed 6738 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
6738
260-
261 if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
(drbg->flags & 0x1) == 0Description
TRUEevaluated 2094452 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 10107 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
10107-2094452
262 /* If no input reuse existing derived value */-
263 if (in1 != NULL || nonce != NULL || in2 != NULL)
in1 != ((void *)0)Description
TRUEevaluated 1051507 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 1052792 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
nonce != ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1053558 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
in2 != ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1059308 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1059308
264 if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
!ctr_df(ctr, i..., in2, in2len)Description
TRUEnever evaluated
FALSEevaluated 1036562 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1036562
265 return 0;
never executed: return 0;
0
266 /* If this a reuse input in1len != 0 */-
267 if (in1len)
in1lenDescription
TRUEevaluated 2104731 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 3984 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
3984-2104731
268 ctr_XOR(ctr, ctr->KX, drbg->seedlen);
executed 2094558 times by 2 tests: ctr_XOR(ctr, ctr->KX, drbg->seedlen);
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2094558
269 } else {
executed 2112796 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2112796
270 ctr_XOR(ctr, in1, in1len);-
271 ctr_XOR(ctr, in2, in2len);-
272 }
executed 10107 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
10107
273-
274 if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->K, NULL, 1))
!EVP_CipherIni...void *)0) , 1)Description
TRUEnever evaluated
FALSEevaluated 2082205 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-2082205
275 return 0;
never executed: return 0;
0
276 return 1;
executed 2079305 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
2079305
277}-
278-
279__owur static int drbg_ctr_instantiate(RAND_DRBG *drbg,-
280 const unsigned char *entropy, size_t entropylen,-
281 const unsigned char *nonce, size_t noncelen,-
282 const unsigned char *pers, size_t perslen)-
283{-
284 RAND_DRBG_CTR *ctr = &drbg->data.ctr;-
285-
286 if (entropy == NULL)
entropy == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 6125 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-6125
287 return 0;
never executed: return 0;
0
288-
289 memset(ctr->K, 0, sizeof(ctr->K));-
290 memset(ctr->V, 0, sizeof(ctr->V));-
291 if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->K, NULL, 1))
!EVP_CipherIni...void *)0) , 1)Description
TRUEnever evaluated
FALSEevaluated 6125 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-6125
292 return 0;
never executed: return 0;
0
293 if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
!ctr_update(dr...nce, noncelen)Description
TRUEnever evaluated
FALSEevaluated 6125 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-6125
294 return 0;
never executed: return 0;
0
295 return 1;
executed 6125 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
6125
296}-
297-
298__owur static int drbg_ctr_reseed(RAND_DRBG *drbg,-
299 const unsigned char *entropy, size_t entropylen,-
300 const unsigned char *adin, size_t adinlen)-
301{-
302 if (entropy == NULL)
entropy == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 4400 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-4400
303 return 0;
never executed: return 0;
0
304 if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
!ctr_update(dr...void *)0) , 0)Description
TRUEnever evaluated
FALSEevaluated 4400 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-4400
305 return 0;
never executed: return 0;
0
306 return 1;
executed 4400 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
4400
307}-
308-
309__owur static int drbg_ctr_generate(RAND_DRBG *drbg,-
310 unsigned char *out, size_t outlen,-
311 const unsigned char *adin, size_t adinlen)-
312{-
313 RAND_DRBG_CTR *ctr = &drbg->data.ctr;-
314-
315 if (adin != NULL && adinlen != 0) {
adin != ((void *)0)Description
TRUEevaluated 1043409 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 3990 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
adinlen != 0Description
TRUEevaluated 1044062 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 2880 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
2880-1044062
316 if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
!ctr_update(dr...void *)0) , 0)Description
TRUEnever evaluated
FALSEevaluated 1034495 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1034495
317 return 0;
never executed: return 0;
0
318 /* This means we reuse derived value */-
319 if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
(drbg->flags & 0x1) == 0Description
TRUEevaluated 1035196 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 1443 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1443-1035196
320 adin = NULL;-
321 adinlen = 1;-
322 }
executed 1033532 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1033532
323 } else {
executed 1039031 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1039031
324 adinlen = 0;-
325 }
executed 6870 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
6870
326-
327 for ( ; ; ) {-
328 int outl = AES_BLOCK_SIZE;-
329-
330 inc_128(ctr);-
331 if (outlen < 16) {
outlen < 16Description
TRUEevaluated 230486 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 11753918 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
230486-11753918
332 /* Use K as temp space as it will be updated */-
333 if (!EVP_CipherUpdate(ctr->ctx, ctr->K, &outl, ctr->V,
!EVP_CipherUpd...l, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 230486 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-230486
334 AES_BLOCK_SIZE)
!EVP_CipherUpd...l, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 230486 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-230486
335 || outl != AES_BLOCK_SIZE)
outl != 16Description
TRUEnever evaluated
FALSEevaluated 230486 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-230486
336 return 0;
never executed: return 0;
0
337 memcpy(out, ctr->K, outlen);-
338 break;
executed 230486 times by 2 tests: break;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
230486
339 }-
340 if (!EVP_CipherUpdate(ctr->ctx, out, &outl, ctr->V, AES_BLOCK_SIZE)
!EVP_CipherUpd...l, ctr->V, 16)Description
TRUEnever evaluated
FALSEevaluated 11465781 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-11465781
341 || outl != AES_BLOCK_SIZE)
outl != 16Description
TRUEnever evaluated
FALSEevaluated 11885959 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-11885959
342 return 0;
never executed: return 0;
0
343 out += 16;-
344 outlen -= 16;-
345 if (outlen == 0)
outlen == 0Description
TRUEevaluated 827529 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 11119617 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
827529-11119617
346 break;
executed 827491 times by 2 tests: break;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
827491
347 }
executed 11085862 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
11085862
348-
349 if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
!ctr_update(dr...void *)0) , 0)Description
TRUEnever evaluated
FALSEevaluated 1038266 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-1038266
350 return 0;
never executed: return 0;
0
351 return 1;
executed 1036700 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
1036700
352}-
353-
354static int drbg_ctr_uninstantiate(RAND_DRBG *drbg)-
355{-
356 EVP_CIPHER_CTX_free(drbg->data.ctr.ctx);-
357 EVP_CIPHER_CTX_free(drbg->data.ctr.ctx_df);-
358 OPENSSL_cleanse(&drbg->data.ctr, sizeof(drbg->data.ctr));-
359 return 1;
executed 14785 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
14785
360}-
361-
362static RAND_DRBG_METHOD drbg_ctr_meth = {-
363 drbg_ctr_instantiate,-
364 drbg_ctr_reseed,-
365 drbg_ctr_generate,-
366 drbg_ctr_uninstantiate-
367};-
368-
369int drbg_ctr_init(RAND_DRBG *drbg)-
370{-
371 RAND_DRBG_CTR *ctr = &drbg->data.ctr;-
372 size_t keylen;-
373-
374 switch (drbg->type) {-
375 default:
never executed: default:
0
376 /* This can't happen, but silence the compiler warning. */-
377 return 0;
never executed: return 0;
0
378 case NID_aes_128_ctr:
executed 4332 times by 1 test: case 904:
Executed by:
  • libcrypto.so.1.1
4332
379 keylen = 16;-
380 ctr->cipher = EVP_aes_128_ecb();-
381 break;
executed 4332 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
4332
382 case NID_aes_192_ctr:
executed 4332 times by 1 test: case 905:
Executed by:
  • libcrypto.so.1.1
4332
383 keylen = 24;-
384 ctr->cipher = EVP_aes_192_ecb();-
385 break;
executed 4332 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
4332
386 case NID_aes_256_ctr:
executed 6133 times by 2 tests: case 906:
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
6133
387 keylen = 32;-
388 ctr->cipher = EVP_aes_256_ecb();-
389 break;
executed 6133 times by 2 tests: break;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
6133
390 }-
391-
392 drbg->meth = &drbg_ctr_meth;-
393-
394 ctr->keylen = keylen;-
395 if (ctr->ctx == NULL)
ctr->ctx == ((void *)0)Description
TRUEevaluated 14785 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12-14785
396 ctr->ctx = EVP_CIPHER_CTX_new();
executed 14785 times by 2 tests: ctr->ctx = EVP_CIPHER_CTX_new();
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
14785
397 if (ctr->ctx == NULL)
ctr->ctx == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 14797 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-14797
398 return 0;
never executed: return 0;
0
399 drbg->strength = keylen * 8;-
400 drbg->seedlen = keylen + 16;-
401-
402 if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
(drbg->flags & 0x1) == 0Description
TRUEevaluated 8299 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 6498 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6498-8299
403 /* df initialisation */-
404 static const unsigned char df_key[32] = {-
405 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,-
406 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,-
407 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,-
408 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f-
409 };-
410-
411 if (ctr->ctx_df == NULL)
ctr->ctx_df == ((void *)0)Description
TRUEevaluated 8293 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6-8293
412 ctr->ctx_df = EVP_CIPHER_CTX_new();
executed 8293 times by 2 tests: ctr->ctx_df = EVP_CIPHER_CTX_new();
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
8293
413 if (ctr->ctx_df == NULL)
ctr->ctx_df == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 8299 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-8299
414 return 0;
never executed: return 0;
0
415 /* Set key schedule for df_key */-
416 if (!EVP_CipherInit_ex(ctr->ctx_df, ctr->cipher, NULL, df_key, NULL, 1))
!EVP_CipherIni...void *)0) , 1)Description
TRUEnever evaluated
FALSEevaluated 8299 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-8299
417 return 0;
never executed: return 0;
0
418-
419 drbg->min_entropylen = ctr->keylen;-
420 drbg->max_entropylen = DRBG_MINMAX_FACTOR * drbg->min_entropylen;-
421 drbg->min_noncelen = drbg->min_entropylen / 2;-
422 drbg->max_noncelen = DRBG_MINMAX_FACTOR * drbg->min_noncelen;-
423 drbg->max_perslen = DRBG_MAX_LENGTH;-
424 drbg->max_adinlen = DRBG_MAX_LENGTH;-
425 } else {
executed 8299 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
8299
426 drbg->min_entropylen = drbg->seedlen;-
427 drbg->max_entropylen = drbg->seedlen;-
428 /* Nonce not used */-
429 drbg->min_noncelen = 0;-
430 drbg->max_noncelen = 0;-
431 drbg->max_perslen = drbg->seedlen;-
432 drbg->max_adinlen = drbg->seedlen;-
433 }
executed 6498 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
6498
434-
435 drbg->max_request = 1 << 16;-
436-
437 return 1;
executed 14797 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
14797
438}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2