Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/rand/drbg_ctr.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | - | |||||||||||||||||||
22 | static 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--) {
| 0-17777205 | ||||||||||||||||||
29 | c = *p; | - | ||||||||||||||||||
30 | c++; | - | ||||||||||||||||||
31 | *p = c; | - | ||||||||||||||||||
32 | if (c != 0) {
| 77099-17923679 | ||||||||||||||||||
33 | /* If we didn't wrap around, we're done. */ | - | ||||||||||||||||||
34 | break; executed 17729011 times by 2 tests: break; Executed by:
| 17729011 | ||||||||||||||||||
35 | } | - | ||||||||||||||||||
36 | } executed 77097 times by 2 tests: end of block Executed by:
| 77097 | ||||||||||||||||||
37 | } executed 17675448 times by 2 tests: end of block Executed by:
| 17675448 | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | static 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)
| 3600-2113562 | ||||||||||||||||||
44 | return; executed 10818 times by 1 test: return; Executed by:
| 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;
| 0-2107945 | ||||||||||||||||||
51 | for (i = 0; i < n; i++)
| 2133729-65699309 | ||||||||||||||||||
52 | ctr->K[i] ^= in[i]; executed 65704331 times by 2 tests: ctr->K[i] ^= in[i]; Executed by:
| 65704331 | ||||||||||||||||||
53 | if (inlen <= ctr->keylen)
| 0-2120534 | ||||||||||||||||||
54 | return; never executed: return; | 0 | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | n = inlen - ctr->keylen; | - | ||||||||||||||||||
57 | if (n > 16) {
| 0-2116629 | ||||||||||||||||||
58 | /* Should never happen */ | - | ||||||||||||||||||
59 | n = 16; | - | ||||||||||||||||||
60 | } never executed: end of block | 0 | ||||||||||||||||||
61 | for (i = 0; i < n; i++)
| 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:
| 33447824 | ||||||||||||||||||
63 | } executed 2119266 times by 2 tests: end of block Executed by:
| 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++)
| 9487527-144141478 | ||||||||||||||||||
74 | out[i] ^= in[i]; executed 144315469 times by 2 tests: out[i] ^= in[i]; Executed by:
| 144315469 | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, AES_BLOCK_SIZE)
| 0-9107744 | ||||||||||||||||||
77 | || outlen != AES_BLOCK_SIZE)
| 0-9277088 | ||||||||||||||||||
78 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
79 | return 1; executed 9242579 times by 2 tests: return 1; Executed by:
| 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)
| 0-2126716 | ||||||||||||||||||
89 | || !ctr_BCC_block(ctr, ctr->KX + 16, in))
| 0-2126487 | ||||||||||||||||||
90 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
91 | if (ctr->keylen != 16 && !ctr_BCC_block(ctr, ctr->KX + 32, in))
| 0-2125097 | ||||||||||||||||||
92 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
93 | return 1; executed 2128112 times by 2 tests: return 1; Executed by:
| 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))
| 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))
| 0-1054781 | ||||||||||||||||||
108 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
109 | if (ctr->keylen != 16) {
| 2406-1055022 | ||||||||||||||||||
110 | ctr->bltmp[3] = 2; | - | ||||||||||||||||||
111 | if (!ctr_BCC_block(ctr, ctr->KX + 32, ctr->bltmp))
| 0-1052786 | ||||||||||||||||||
112 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
113 | } executed 1052231 times by 2 tests: end of block Executed by:
| 1052231 | ||||||||||||||||||
114 | return 1; executed 1056216 times by 2 tests: return 1; Executed by:
| 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)
| 3600-2127705 | ||||||||||||||||||
124 | return 1; executed 2114178 times by 2 tests: return 1; Executed by:
| 2114178 | ||||||||||||||||||
125 | - | |||||||||||||||||||
126 | /* If we have partial block handle it first */ | - | ||||||||||||||||||
127 | if (ctr->bltmp_pos) {
| 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) {
| 1062805-1068528 | ||||||||||||||||||
132 | memcpy(ctr->bltmp + ctr->bltmp_pos, in, left); | - | ||||||||||||||||||
133 | if (!ctr_BCC_blocks(ctr, ctr->bltmp))
| 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:
| 1060802 | ||||||||||||||||||
139 | } executed 2120769 times by 2 tests: end of block Executed by:
| 2120769 | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | /* Process zero or more complete blocks */ | - | ||||||||||||||||||
142 | for (; inlen >= 16; in += 16, inlen -= 16) {
| 14057-2122941 | ||||||||||||||||||
143 | if (!ctr_BCC_blocks(ctr, in))
| 0-14057 | ||||||||||||||||||
144 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
145 | } executed 14057 times by 2 tests: end of block Executed by:
| 14057 | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | /* Copy any remaining partial block to the temporary buffer */ | - | ||||||||||||||||||
148 | if (inlen > 0) {
| 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:
| 2118794 | ||||||||||||||||||
152 | return 1; executed 2128649 times by 2 tests: return 1; Executed by:
| 2128649 | ||||||||||||||||||
153 | } | - | ||||||||||||||||||
154 | - | |||||||||||||||||||
155 | __owur static int ctr_BCC_final(RAND_DRBG_CTR *ctr) | - | ||||||||||||||||||
156 | { | - | ||||||||||||||||||
157 | if (ctr->bltmp_pos) {
| 0-1065727 | ||||||||||||||||||
158 | memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos); | - | ||||||||||||||||||
159 | if (!ctr_BCC_blocks(ctr, ctr->bltmp))
| 0-1058357 | ||||||||||||||||||
160 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
161 | } executed 1058171 times by 2 tests: end of block Executed by:
| 1058171 | ||||||||||||||||||
162 | return 1; executed 1060204 times by 2 tests: return 1; Executed by:
| 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))
| 0-1054484 | ||||||||||||||||||
176 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
177 | if (in1 == NULL)
| 0-1056829 | ||||||||||||||||||
178 | in1len = 0; never executed: in1len = 0; | 0 | ||||||||||||||||||
179 | if (in2 == NULL)
| 2947-1058043 | ||||||||||||||||||
180 | in2len = 0; executed 1057171 times by 2 tests: in2len = 0; Executed by:
| 1057171 | ||||||||||||||||||
181 | if (in3 == NULL)
| 6171-1055418 | ||||||||||||||||||
182 | in3len = 0; executed 1057536 times by 2 tests: in3len = 0; Executed by:
| 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)
| 0-1060519 | ||||||||||||||||||
197 | || !ctr_BCC_update(ctr, in2, in2len)
| 0-1061657 | ||||||||||||||||||
198 | || !ctr_BCC_update(ctr, in3, in3len)
| 0-1061592 | ||||||||||||||||||
199 | || !ctr_BCC_update(ctr, &c80, 1)
| 0-1067026 | ||||||||||||||||||
200 | || !ctr_BCC_final(ctr))
| 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))
| 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,
| 0-1033080 | ||||||||||||||||||
207 | AES_BLOCK_SIZE)
| 0-1033080 | ||||||||||||||||||
208 | || outlen != AES_BLOCK_SIZE)
| 0-1032116 | ||||||||||||||||||
209 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
210 | if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 16, &outlen, ctr->KX,
| 0-1036967 | ||||||||||||||||||
211 | AES_BLOCK_SIZE)
| 0-1036967 | ||||||||||||||||||
212 | || outlen != AES_BLOCK_SIZE)
| 0-1053122 | ||||||||||||||||||
213 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
214 | if (ctr->keylen != 16)
| 2406-1048801 | ||||||||||||||||||
215 | if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 32, &outlen, ctr->KX + 16,
| 0-1036021 | ||||||||||||||||||
216 | AES_BLOCK_SIZE)
| 0-1036021 | ||||||||||||||||||
217 | || outlen != AES_BLOCK_SIZE)
| 0-1036464 | ||||||||||||||||||
218 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
219 | return 1; executed 1037211 times by 2 tests: return 1; Executed by:
| 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)
| 0-2077957 | ||||||||||||||||||
239 | || outlen != AES_BLOCK_SIZE)
| 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) {
| 6738-2084114 | ||||||||||||||||||
244 | inc_128(ctr); | - | ||||||||||||||||||
245 | if (!EVP_CipherUpdate(ctr->ctx, ctr->K+16, &outlen, ctr->V,
| 0-2083800 | ||||||||||||||||||
246 | AES_BLOCK_SIZE)
| 0-2083800 | ||||||||||||||||||
247 | || outlen != AES_BLOCK_SIZE)
| 0-2085401 | ||||||||||||||||||
248 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
249 | } executed 2077559 times by 2 tests: end of block Executed by:
| 2077559 | ||||||||||||||||||
250 | inc_128(ctr); | - | ||||||||||||||||||
251 | if (!EVP_CipherUpdate(ctr->ctx, ctr->V, &outlen, ctr->V, AES_BLOCK_SIZE)
| 0-2091943 | ||||||||||||||||||
252 | || outlen != AES_BLOCK_SIZE)
| 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) {
| 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:
| 6738 | ||||||||||||||||||
260 | - | |||||||||||||||||||
261 | if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
| 10107-2094452 | ||||||||||||||||||
262 | /* If no input reuse existing derived value */ | - | ||||||||||||||||||
263 | if (in1 != NULL || nonce != NULL || in2 != NULL)
| 0-1059308 | ||||||||||||||||||
264 | if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
| 0-1036562 | ||||||||||||||||||
265 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
266 | /* If this a reuse input in1len != 0 */ | - | ||||||||||||||||||
267 | if (in1len)
| 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:
| 2094558 | ||||||||||||||||||
269 | } else { executed 2112796 times by 2 tests: end of block Executed by:
| 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:
| 10107 | ||||||||||||||||||
273 | - | |||||||||||||||||||
274 | if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->K, NULL, 1))
| 0-2082205 | ||||||||||||||||||
275 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
276 | return 1; executed 2079305 times by 2 tests: return 1; Executed by:
| 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)
| 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))
| 0-6125 | ||||||||||||||||||
292 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
293 | if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
| 0-6125 | ||||||||||||||||||
294 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
295 | return 1; executed 6125 times by 2 tests: return 1; Executed by:
| 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)
| 0-4400 | ||||||||||||||||||
303 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
304 | if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
| 0-4400 | ||||||||||||||||||
305 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
306 | return 1; executed 4400 times by 1 test: return 1; Executed by:
| 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) {
| 2880-1044062 | ||||||||||||||||||
316 | if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
| 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) {
| 1443-1035196 | ||||||||||||||||||
320 | adin = NULL; | - | ||||||||||||||||||
321 | adinlen = 1; | - | ||||||||||||||||||
322 | } executed 1033532 times by 2 tests: end of block Executed by:
| 1033532 | ||||||||||||||||||
323 | } else { executed 1039031 times by 2 tests: end of block Executed by:
| 1039031 | ||||||||||||||||||
324 | adinlen = 0; | - | ||||||||||||||||||
325 | } executed 6870 times by 2 tests: end of block Executed by:
| 6870 | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | for ( ; ; ) { | - | ||||||||||||||||||
328 | int outl = AES_BLOCK_SIZE; | - | ||||||||||||||||||
329 | - | |||||||||||||||||||
330 | inc_128(ctr); | - | ||||||||||||||||||
331 | if (outlen < 16) {
| 230486-11753918 | ||||||||||||||||||
332 | /* Use K as temp space as it will be updated */ | - | ||||||||||||||||||
333 | if (!EVP_CipherUpdate(ctr->ctx, ctr->K, &outl, ctr->V,
| 0-230486 | ||||||||||||||||||
334 | AES_BLOCK_SIZE)
| 0-230486 | ||||||||||||||||||
335 | || outl != AES_BLOCK_SIZE)
| 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:
| 230486 | ||||||||||||||||||
339 | } | - | ||||||||||||||||||
340 | if (!EVP_CipherUpdate(ctr->ctx, out, &outl, ctr->V, AES_BLOCK_SIZE)
| 0-11465781 | ||||||||||||||||||
341 | || outl != AES_BLOCK_SIZE)
| 0-11885959 | ||||||||||||||||||
342 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
343 | out += 16; | - | ||||||||||||||||||
344 | outlen -= 16; | - | ||||||||||||||||||
345 | if (outlen == 0)
| 827529-11119617 | ||||||||||||||||||
346 | break; executed 827491 times by 2 tests: break; Executed by:
| 827491 | ||||||||||||||||||
347 | } executed 11085862 times by 2 tests: end of block Executed by:
| 11085862 | ||||||||||||||||||
348 | - | |||||||||||||||||||
349 | if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
| 0-1038266 | ||||||||||||||||||
350 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
351 | return 1; executed 1036700 times by 2 tests: return 1; Executed by:
| 1036700 | ||||||||||||||||||
352 | } | - | ||||||||||||||||||
353 | - | |||||||||||||||||||
354 | static 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:
| 14785 | ||||||||||||||||||
360 | } | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | static RAND_DRBG_METHOD drbg_ctr_meth = { | - | ||||||||||||||||||
363 | drbg_ctr_instantiate, | - | ||||||||||||||||||
364 | drbg_ctr_reseed, | - | ||||||||||||||||||
365 | drbg_ctr_generate, | - | ||||||||||||||||||
366 | drbg_ctr_uninstantiate | - | ||||||||||||||||||
367 | }; | - | ||||||||||||||||||
368 | - | |||||||||||||||||||
369 | int 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:
| 4332 | ||||||||||||||||||
379 | keylen = 16; | - | ||||||||||||||||||
380 | ctr->cipher = EVP_aes_128_ecb(); | - | ||||||||||||||||||
381 | break; executed 4332 times by 1 test: break; Executed by:
| 4332 | ||||||||||||||||||
382 | case NID_aes_192_ctr: executed 4332 times by 1 test: case 905: Executed by:
| 4332 | ||||||||||||||||||
383 | keylen = 24; | - | ||||||||||||||||||
384 | ctr->cipher = EVP_aes_192_ecb(); | - | ||||||||||||||||||
385 | break; executed 4332 times by 1 test: break; Executed by:
| 4332 | ||||||||||||||||||
386 | case NID_aes_256_ctr: executed 6133 times by 2 tests: case 906: Executed by:
| 6133 | ||||||||||||||||||
387 | keylen = 32; | - | ||||||||||||||||||
388 | ctr->cipher = EVP_aes_256_ecb(); | - | ||||||||||||||||||
389 | break; executed 6133 times by 2 tests: break; Executed by:
| 6133 | ||||||||||||||||||
390 | } | - | ||||||||||||||||||
391 | - | |||||||||||||||||||
392 | drbg->meth = &drbg_ctr_meth; | - | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | ctr->keylen = keylen; | - | ||||||||||||||||||
395 | if (ctr->ctx == NULL)
| 12-14785 | ||||||||||||||||||
396 | ctr->ctx = EVP_CIPHER_CTX_new(); executed 14785 times by 2 tests: ctr->ctx = EVP_CIPHER_CTX_new(); Executed by:
| 14785 | ||||||||||||||||||
397 | if (ctr->ctx == NULL)
| 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) {
| 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)
| 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:
| 8293 | ||||||||||||||||||
413 | if (ctr->ctx_df == NULL)
| 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))
| 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:
| 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:
| 6498 | ||||||||||||||||||
434 | - | |||||||||||||||||||
435 | drbg->max_request = 1 << 16; | - | ||||||||||||||||||
436 | - | |||||||||||||||||||
437 | return 1; executed 14797 times by 2 tests: return 1; Executed by:
| 14797 | ||||||||||||||||||
438 | } | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |