Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * DRBG: Deterministic Random Bits Generator |
| 3 | * Based on NIST Recommended DRBG from NIST SP800-90A with the following |
| 4 | * properties: |
| 5 | * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores |
| 6 | * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores |
| 7 | * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores |
| 8 | * * with and without prediction resistance |
| 9 | * |
| 10 | * Copyright Stephan Mueller <smueller@chronox.de>, 2014 |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, and the entire permission notice in its entirety, |
| 17 | * including the disclaimer of warranties. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in the |
| 20 | * documentation and/or other materials provided with the distribution. |
| 21 | * 3. The name of the author may not be used to endorse or promote |
| 22 | * products derived from this software without specific prior |
| 23 | * written permission. |
| 24 | * |
| 25 | * ALTERNATIVELY, this product may be distributed under the terms of |
| 26 | * the GNU General Public License, in which case the provisions of the GPL are |
| 27 | * required INSTEAD OF the above restrictions. (This clause is |
| 28 | * necessary due to a potential bad interaction between the GPL and |
| 29 | * the restrictions contained in a BSD-style copyright.) |
| 30 | * |
| 31 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 32 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 33 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 34 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 35 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 36 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 37 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 38 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 39 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 41 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 42 | * DAMAGE. |
| 43 | * |
| 44 | * DRBG Usage |
| 45 | * ========== |
| 46 | * The SP 800-90A DRBG allows the user to specify a personalization string |
| 47 | * for initialization as well as an additional information string for each |
| 48 | * random number request. The following code fragments show how a caller |
| 49 | * uses the kernel crypto API to use the full functionality of the DRBG. |
| 50 | * |
| 51 | * Usage without any additional data |
| 52 | * --------------------------------- |
| 53 | * struct crypto_rng *drng; |
| 54 | * int err; |
| 55 | * char data[DATALEN]; |
| 56 | * |
| 57 | * drng = crypto_alloc_rng(drng_name, 0, 0); |
| 58 | * err = crypto_rng_get_bytes(drng, &data, DATALEN); |
| 59 | * crypto_free_rng(drng); |
| 60 | * |
| 61 | * |
| 62 | * Usage with personalization string during initialization |
| 63 | * ------------------------------------------------------- |
| 64 | * struct crypto_rng *drng; |
| 65 | * int err; |
| 66 | * char data[DATALEN]; |
| 67 | * struct drbg_string pers; |
| 68 | * char personalization[11] = "some-string"; |
| 69 | * |
| 70 | * drbg_string_fill(&pers, personalization, strlen(personalization)); |
| 71 | * drng = crypto_alloc_rng(drng_name, 0, 0); |
| 72 | * // The reset completely re-initializes the DRBG with the provided |
| 73 | * // personalization string |
| 74 | * err = crypto_rng_reset(drng, &personalization, strlen(personalization)); |
| 75 | * err = crypto_rng_get_bytes(drng, &data, DATALEN); |
| 76 | * crypto_free_rng(drng); |
| 77 | * |
| 78 | * |
| 79 | * Usage with additional information string during random number request |
| 80 | * --------------------------------------------------------------------- |
| 81 | * struct crypto_rng *drng; |
| 82 | * int err; |
| 83 | * char data[DATALEN]; |
| 84 | * char addtl_string[11] = "some-string"; |
| 85 | * string drbg_string addtl; |
| 86 | * |
| 87 | * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string)); |
| 88 | * drng = crypto_alloc_rng(drng_name, 0, 0); |
| 89 | * // The following call is a wrapper to crypto_rng_get_bytes() and returns |
| 90 | * // the same error codes. |
| 91 | * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl); |
| 92 | * crypto_free_rng(drng); |
| 93 | * |
| 94 | * |
| 95 | * Usage with personalization and additional information strings |
| 96 | * ------------------------------------------------------------- |
| 97 | * Just mix both scenarios above. |
| 98 | */ |
| 99 | |
| 100 | #include <crypto/drbg.h> |
| 101 | |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 102 | /*************************************************************** |
| 103 | * Backend cipher definitions available to DRBG |
| 104 | ***************************************************************/ |
| 105 | |
| 106 | /* |
| 107 | * The order of the DRBG definitions here matter: every DRBG is registered |
| 108 | * as stdrng. Each DRBG receives an increasing cra_priority values the later |
| 109 | * they are defined in this array (see drbg_fill_array). |
| 110 | * |
| 111 | * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and |
| 112 | * the SHA256 / AES 256 over other ciphers. Thus, the favored |
| 113 | * DRBGs are the latest entries in this array. |
| 114 | */ |
| 115 | static const struct drbg_core drbg_cores[] = { |
| 116 | #ifdef CONFIG_CRYPTO_DRBG_CTR |
| 117 | { |
| 118 | .flags = DRBG_CTR | DRBG_STRENGTH128, |
| 119 | .statelen = 32, /* 256 bits as defined in 10.2.1 */ |
| 120 | .max_addtllen = 35, |
| 121 | .max_bits = 19, |
| 122 | .max_req = 48, |
| 123 | .blocklen_bytes = 16, |
| 124 | .cra_name = "ctr_aes128", |
| 125 | .backend_cra_name = "ecb(aes)", |
| 126 | }, { |
| 127 | .flags = DRBG_CTR | DRBG_STRENGTH192, |
| 128 | .statelen = 40, /* 320 bits as defined in 10.2.1 */ |
| 129 | .max_addtllen = 35, |
| 130 | .max_bits = 19, |
| 131 | .max_req = 48, |
| 132 | .blocklen_bytes = 16, |
| 133 | .cra_name = "ctr_aes192", |
| 134 | .backend_cra_name = "ecb(aes)", |
| 135 | }, { |
| 136 | .flags = DRBG_CTR | DRBG_STRENGTH256, |
| 137 | .statelen = 48, /* 384 bits as defined in 10.2.1 */ |
| 138 | .max_addtllen = 35, |
| 139 | .max_bits = 19, |
| 140 | .max_req = 48, |
| 141 | .blocklen_bytes = 16, |
| 142 | .cra_name = "ctr_aes256", |
| 143 | .backend_cra_name = "ecb(aes)", |
| 144 | }, |
| 145 | #endif /* CONFIG_CRYPTO_DRBG_CTR */ |
| 146 | #ifdef CONFIG_CRYPTO_DRBG_HASH |
| 147 | { |
| 148 | .flags = DRBG_HASH | DRBG_STRENGTH128, |
| 149 | .statelen = 55, /* 440 bits */ |
| 150 | .max_addtllen = 35, |
| 151 | .max_bits = 19, |
| 152 | .max_req = 48, |
| 153 | .blocklen_bytes = 20, |
| 154 | .cra_name = "sha1", |
| 155 | .backend_cra_name = "sha1", |
| 156 | }, { |
| 157 | .flags = DRBG_HASH | DRBG_STRENGTH256, |
| 158 | .statelen = 111, /* 888 bits */ |
| 159 | .max_addtllen = 35, |
| 160 | .max_bits = 19, |
| 161 | .max_req = 48, |
| 162 | .blocklen_bytes = 48, |
| 163 | .cra_name = "sha384", |
| 164 | .backend_cra_name = "sha384", |
| 165 | }, { |
| 166 | .flags = DRBG_HASH | DRBG_STRENGTH256, |
| 167 | .statelen = 111, /* 888 bits */ |
| 168 | .max_addtllen = 35, |
| 169 | .max_bits = 19, |
| 170 | .max_req = 48, |
| 171 | .blocklen_bytes = 64, |
| 172 | .cra_name = "sha512", |
| 173 | .backend_cra_name = "sha512", |
| 174 | }, { |
| 175 | .flags = DRBG_HASH | DRBG_STRENGTH256, |
| 176 | .statelen = 55, /* 440 bits */ |
| 177 | .max_addtllen = 35, |
| 178 | .max_bits = 19, |
| 179 | .max_req = 48, |
| 180 | .blocklen_bytes = 32, |
| 181 | .cra_name = "sha256", |
| 182 | .backend_cra_name = "sha256", |
| 183 | }, |
| 184 | #endif /* CONFIG_CRYPTO_DRBG_HASH */ |
| 185 | #ifdef CONFIG_CRYPTO_DRBG_HMAC |
| 186 | { |
Stephan Mueller | 5b635e2 | 2014-07-06 02:26:37 +0200 | [diff] [blame] | 187 | .flags = DRBG_HMAC | DRBG_STRENGTH128, |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 188 | .statelen = 20, /* block length of cipher */ |
| 189 | .max_addtllen = 35, |
| 190 | .max_bits = 19, |
| 191 | .max_req = 48, |
| 192 | .blocklen_bytes = 20, |
| 193 | .cra_name = "hmac_sha1", |
| 194 | .backend_cra_name = "hmac(sha1)", |
| 195 | }, { |
| 196 | .flags = DRBG_HMAC | DRBG_STRENGTH256, |
| 197 | .statelen = 48, /* block length of cipher */ |
| 198 | .max_addtllen = 35, |
| 199 | .max_bits = 19, |
| 200 | .max_req = 48, |
| 201 | .blocklen_bytes = 48, |
| 202 | .cra_name = "hmac_sha384", |
| 203 | .backend_cra_name = "hmac(sha384)", |
| 204 | }, { |
| 205 | .flags = DRBG_HMAC | DRBG_STRENGTH256, |
| 206 | .statelen = 64, /* block length of cipher */ |
| 207 | .max_addtllen = 35, |
| 208 | .max_bits = 19, |
| 209 | .max_req = 48, |
| 210 | .blocklen_bytes = 64, |
| 211 | .cra_name = "hmac_sha512", |
| 212 | .backend_cra_name = "hmac(sha512)", |
| 213 | }, { |
| 214 | .flags = DRBG_HMAC | DRBG_STRENGTH256, |
| 215 | .statelen = 32, /* block length of cipher */ |
| 216 | .max_addtllen = 35, |
| 217 | .max_bits = 19, |
| 218 | .max_req = 48, |
| 219 | .blocklen_bytes = 32, |
| 220 | .cra_name = "hmac_sha256", |
| 221 | .backend_cra_name = "hmac(sha256)", |
| 222 | }, |
| 223 | #endif /* CONFIG_CRYPTO_DRBG_HMAC */ |
| 224 | }; |
| 225 | |
| 226 | /****************************************************************** |
| 227 | * Generic helper functions |
| 228 | ******************************************************************/ |
| 229 | |
| 230 | /* |
| 231 | * Return strength of DRBG according to SP800-90A section 8.4 |
| 232 | * |
| 233 | * @flags DRBG flags reference |
| 234 | * |
| 235 | * Return: normalized strength in *bytes* value or 32 as default |
| 236 | * to counter programming errors |
| 237 | */ |
| 238 | static inline unsigned short drbg_sec_strength(drbg_flag_t flags) |
| 239 | { |
| 240 | switch (flags & DRBG_STRENGTH_MASK) { |
| 241 | case DRBG_STRENGTH128: |
| 242 | return 16; |
| 243 | case DRBG_STRENGTH192: |
| 244 | return 24; |
| 245 | case DRBG_STRENGTH256: |
| 246 | return 32; |
| 247 | default: |
| 248 | return 32; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * FIPS 140-2 continuous self test |
| 254 | * The test is performed on the result of one round of the output |
| 255 | * function. Thus, the function implicitly knows the size of the |
| 256 | * buffer. |
| 257 | * |
| 258 | * The FIPS test can be called in an endless loop until it returns |
| 259 | * true. Although the code looks like a potential for a deadlock, it |
| 260 | * is not the case, because returning a false cannot mathematically |
| 261 | * occur (except once when a reseed took place and the updated state |
| 262 | * would is now set up such that the generation of new value returns |
| 263 | * an identical one -- this is most unlikely and would happen only once). |
| 264 | * Thus, if this function repeatedly returns false and thus would cause |
| 265 | * a deadlock, the integrity of the entire kernel is lost. |
| 266 | * |
| 267 | * @drbg DRBG handle |
| 268 | * @buf output buffer of random data to be checked |
| 269 | * |
| 270 | * return: |
| 271 | * true on success |
| 272 | * false on error |
| 273 | */ |
| 274 | static bool drbg_fips_continuous_test(struct drbg_state *drbg, |
| 275 | const unsigned char *buf) |
| 276 | { |
| 277 | #ifdef CONFIG_CRYPTO_FIPS |
| 278 | int ret = 0; |
| 279 | /* skip test if we test the overall system */ |
| 280 | if (drbg->test_data) |
| 281 | return true; |
| 282 | /* only perform test in FIPS mode */ |
| 283 | if (0 == fips_enabled) |
| 284 | return true; |
| 285 | if (!drbg->fips_primed) { |
| 286 | /* Priming of FIPS test */ |
| 287 | memcpy(drbg->prev, buf, drbg_blocklen(drbg)); |
| 288 | drbg->fips_primed = true; |
| 289 | /* return false due to priming, i.e. another round is needed */ |
| 290 | return false; |
| 291 | } |
| 292 | ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg)); |
| 293 | memcpy(drbg->prev, buf, drbg_blocklen(drbg)); |
| 294 | /* the test shall pass when the two compared values are not equal */ |
| 295 | return ret != 0; |
| 296 | #else |
| 297 | return true; |
| 298 | #endif /* CONFIG_CRYPTO_FIPS */ |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Convert an integer into a byte representation of this integer. |
| 303 | * The byte representation is big-endian |
| 304 | * |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 305 | * @val value to be converted |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 306 | * @buf buffer holding the converted integer -- caller must ensure that |
| 307 | * buffer size is at least 32 bit |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 308 | */ |
| 309 | #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR)) |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 310 | static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 311 | { |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 312 | struct s { |
| 313 | __u32 conv; |
| 314 | }; |
| 315 | struct s *conversion = (struct s *) buf; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 316 | |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 317 | conversion->conv = cpu_to_be32(val); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Increment buffer |
| 322 | * |
| 323 | * @dst buffer to increment |
| 324 | * @add value to add |
| 325 | */ |
| 326 | static inline void drbg_add_buf(unsigned char *dst, size_t dstlen, |
| 327 | const unsigned char *add, size_t addlen) |
| 328 | { |
| 329 | /* implied: dstlen > addlen */ |
| 330 | unsigned char *dstptr; |
| 331 | const unsigned char *addptr; |
| 332 | unsigned int remainder = 0; |
| 333 | size_t len = addlen; |
| 334 | |
| 335 | dstptr = dst + (dstlen-1); |
| 336 | addptr = add + (addlen-1); |
| 337 | while (len) { |
| 338 | remainder += *dstptr + *addptr; |
| 339 | *dstptr = remainder & 0xff; |
| 340 | remainder >>= 8; |
| 341 | len--; dstptr--; addptr--; |
| 342 | } |
| 343 | len = dstlen - addlen; |
| 344 | while (len && remainder > 0) { |
| 345 | remainder = *dstptr + 1; |
| 346 | *dstptr = remainder & 0xff; |
| 347 | remainder >>= 8; |
| 348 | len--; dstptr--; |
| 349 | } |
| 350 | } |
| 351 | #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */ |
| 352 | |
| 353 | /****************************************************************** |
| 354 | * CTR DRBG callback functions |
| 355 | ******************************************************************/ |
| 356 | |
| 357 | #ifdef CONFIG_CRYPTO_DRBG_CTR |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 358 | #define CRYPTO_DRBG_CTR_STRING "CTR " |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 359 | static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key, |
| 360 | unsigned char *outval, const struct drbg_string *in); |
| 361 | static int drbg_init_sym_kernel(struct drbg_state *drbg); |
| 362 | static int drbg_fini_sym_kernel(struct drbg_state *drbg); |
| 363 | |
| 364 | /* BCC function for CTR DRBG as defined in 10.4.3 */ |
| 365 | static int drbg_ctr_bcc(struct drbg_state *drbg, |
| 366 | unsigned char *out, const unsigned char *key, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 367 | struct list_head *in) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 368 | { |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 369 | int ret = 0; |
| 370 | struct drbg_string *curr = NULL; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 371 | struct drbg_string data; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 372 | short cnt = 0; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 373 | |
| 374 | drbg_string_fill(&data, out, drbg_blocklen(drbg)); |
| 375 | |
| 376 | /* 10.4.3 step 1 */ |
| 377 | memset(out, 0, drbg_blocklen(drbg)); |
| 378 | |
| 379 | /* 10.4.3 step 2 / 4 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 380 | list_for_each_entry(curr, in, list) { |
| 381 | const unsigned char *pos = curr->buf; |
| 382 | size_t len = curr->len; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 383 | /* 10.4.3 step 4.1 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 384 | while (len) { |
| 385 | /* 10.4.3 step 4.2 */ |
| 386 | if (drbg_blocklen(drbg) == cnt) { |
| 387 | cnt = 0; |
| 388 | ret = drbg_kcapi_sym(drbg, key, out, &data); |
| 389 | if (ret) |
| 390 | return ret; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 391 | } |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 392 | out[cnt] ^= *pos; |
| 393 | pos++; |
| 394 | cnt++; |
| 395 | len--; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 396 | } |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 397 | } |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 398 | /* 10.4.3 step 4.2 for last block */ |
| 399 | if (cnt) |
| 400 | ret = drbg_kcapi_sym(drbg, key, out, &data); |
| 401 | |
| 402 | return ret; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
| 406 | * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df |
| 407 | * (and drbg_ctr_bcc, but this function does not need any temporary buffers), |
| 408 | * the scratchpad is used as follows: |
| 409 | * drbg_ctr_update: |
| 410 | * temp |
| 411 | * start: drbg->scratchpad |
| 412 | * length: drbg_statelen(drbg) + drbg_blocklen(drbg) |
| 413 | * note: the cipher writing into this variable works |
| 414 | * blocklen-wise. Now, when the statelen is not a multiple |
| 415 | * of blocklen, the generateion loop below "spills over" |
| 416 | * by at most blocklen. Thus, we need to give sufficient |
| 417 | * memory. |
| 418 | * df_data |
| 419 | * start: drbg->scratchpad + |
| 420 | * drbg_statelen(drbg) + drbg_blocklen(drbg) |
| 421 | * length: drbg_statelen(drbg) |
| 422 | * |
| 423 | * drbg_ctr_df: |
| 424 | * pad |
| 425 | * start: df_data + drbg_statelen(drbg) |
| 426 | * length: drbg_blocklen(drbg) |
| 427 | * iv |
| 428 | * start: pad + drbg_blocklen(drbg) |
| 429 | * length: drbg_blocklen(drbg) |
| 430 | * temp |
| 431 | * start: iv + drbg_blocklen(drbg) |
Stephan Mueller | 8fecaad | 2014-07-01 17:08:48 +0200 | [diff] [blame] | 432 | * length: drbg_satelen(drbg) + drbg_blocklen(drbg) |
| 433 | * note: temp is the buffer that the BCC function operates |
| 434 | * on. BCC operates blockwise. drbg_statelen(drbg) |
| 435 | * is sufficient when the DRBG state length is a multiple |
| 436 | * of the block size. For AES192 (and maybe other ciphers) |
| 437 | * this is not correct and the length for temp is |
| 438 | * insufficient (yes, that also means for such ciphers, |
| 439 | * the final output of all BCC rounds are truncated). |
| 440 | * Therefore, add drbg_blocklen(drbg) to cover all |
| 441 | * possibilities. |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 442 | */ |
| 443 | |
| 444 | /* Derivation Function for CTR DRBG as defined in 10.4.2 */ |
| 445 | static int drbg_ctr_df(struct drbg_state *drbg, |
| 446 | unsigned char *df_data, size_t bytes_to_return, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 447 | struct list_head *seedlist) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 448 | { |
| 449 | int ret = -EFAULT; |
| 450 | unsigned char L_N[8]; |
| 451 | /* S3 is input */ |
| 452 | struct drbg_string S1, S2, S4, cipherin; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 453 | LIST_HEAD(bcc_list); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 454 | unsigned char *pad = df_data + drbg_statelen(drbg); |
| 455 | unsigned char *iv = pad + drbg_blocklen(drbg); |
| 456 | unsigned char *temp = iv + drbg_blocklen(drbg); |
| 457 | size_t padlen = 0; |
| 458 | unsigned int templen = 0; |
| 459 | /* 10.4.2 step 7 */ |
| 460 | unsigned int i = 0; |
| 461 | /* 10.4.2 step 8 */ |
| 462 | const unsigned char *K = (unsigned char *) |
| 463 | "\x00\x01\x02\x03\x04\x05\x06\x07" |
| 464 | "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 465 | "\x10\x11\x12\x13\x14\x15\x16\x17" |
| 466 | "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; |
| 467 | unsigned char *X; |
| 468 | size_t generated_len = 0; |
| 469 | size_t inputlen = 0; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 470 | struct drbg_string *seed = NULL; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 471 | |
| 472 | memset(pad, 0, drbg_blocklen(drbg)); |
| 473 | memset(iv, 0, drbg_blocklen(drbg)); |
| 474 | memset(temp, 0, drbg_statelen(drbg)); |
| 475 | |
| 476 | /* 10.4.2 step 1 is implicit as we work byte-wise */ |
| 477 | |
| 478 | /* 10.4.2 step 2 */ |
| 479 | if ((512/8) < bytes_to_return) |
| 480 | return -EINVAL; |
| 481 | |
| 482 | /* 10.4.2 step 2 -- calculate the entire length of all input data */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 483 | list_for_each_entry(seed, seedlist, list) |
| 484 | inputlen += seed->len; |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 485 | drbg_cpu_to_be32(inputlen, &L_N[0]); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 486 | |
| 487 | /* 10.4.2 step 3 */ |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 488 | drbg_cpu_to_be32(bytes_to_return, &L_N[4]); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 489 | |
| 490 | /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */ |
| 491 | padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg)); |
| 492 | /* wrap the padlen appropriately */ |
| 493 | if (padlen) |
| 494 | padlen = drbg_blocklen(drbg) - padlen; |
| 495 | /* |
| 496 | * pad / padlen contains the 0x80 byte and the following zero bytes. |
| 497 | * As the calculated padlen value only covers the number of zero |
| 498 | * bytes, this value has to be incremented by one for the 0x80 byte. |
| 499 | */ |
| 500 | padlen++; |
| 501 | pad[0] = 0x80; |
| 502 | |
| 503 | /* 10.4.2 step 4 -- first fill the linked list and then order it */ |
| 504 | drbg_string_fill(&S1, iv, drbg_blocklen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 505 | list_add_tail(&S1.list, &bcc_list); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 506 | drbg_string_fill(&S2, L_N, sizeof(L_N)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 507 | list_add_tail(&S2.list, &bcc_list); |
| 508 | list_splice_tail(seedlist, &bcc_list); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 509 | drbg_string_fill(&S4, pad, padlen); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 510 | list_add_tail(&S4.list, &bcc_list); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 511 | |
| 512 | /* 10.4.2 step 9 */ |
| 513 | while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) { |
| 514 | /* |
| 515 | * 10.4.2 step 9.1 - the padding is implicit as the buffer |
| 516 | * holds zeros after allocation -- even the increment of i |
| 517 | * is irrelevant as the increment remains within length of i |
| 518 | */ |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 519 | drbg_cpu_to_be32(i, iv); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 520 | /* 10.4.2 step 9.2 -- BCC and concatenation with temp */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 521 | ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 522 | if (ret) |
| 523 | goto out; |
| 524 | /* 10.4.2 step 9.3 */ |
| 525 | i++; |
| 526 | templen += drbg_blocklen(drbg); |
| 527 | } |
| 528 | |
| 529 | /* 10.4.2 step 11 */ |
| 530 | X = temp + (drbg_keylen(drbg)); |
| 531 | drbg_string_fill(&cipherin, X, drbg_blocklen(drbg)); |
| 532 | |
| 533 | /* 10.4.2 step 12: overwriting of outval is implemented in next step */ |
| 534 | |
| 535 | /* 10.4.2 step 13 */ |
| 536 | while (generated_len < bytes_to_return) { |
| 537 | short blocklen = 0; |
| 538 | /* |
| 539 | * 10.4.2 step 13.1: the truncation of the key length is |
| 540 | * implicit as the key is only drbg_blocklen in size based on |
| 541 | * the implementation of the cipher function callback |
| 542 | */ |
| 543 | ret = drbg_kcapi_sym(drbg, temp, X, &cipherin); |
| 544 | if (ret) |
| 545 | goto out; |
| 546 | blocklen = (drbg_blocklen(drbg) < |
| 547 | (bytes_to_return - generated_len)) ? |
| 548 | drbg_blocklen(drbg) : |
| 549 | (bytes_to_return - generated_len); |
| 550 | /* 10.4.2 step 13.2 and 14 */ |
| 551 | memcpy(df_data + generated_len, X, blocklen); |
| 552 | generated_len += blocklen; |
| 553 | } |
| 554 | |
| 555 | ret = 0; |
| 556 | |
| 557 | out: |
| 558 | memset(iv, 0, drbg_blocklen(drbg)); |
| 559 | memset(temp, 0, drbg_statelen(drbg)); |
| 560 | memset(pad, 0, drbg_blocklen(drbg)); |
| 561 | return ret; |
| 562 | } |
| 563 | |
Stephan Mueller | 72e7c25 | 2014-07-06 02:24:35 +0200 | [diff] [blame] | 564 | /* |
| 565 | * update function of CTR DRBG as defined in 10.2.1.2 |
| 566 | * |
| 567 | * The reseed variable has an enhanced meaning compared to the update |
| 568 | * functions of the other DRBGs as follows: |
| 569 | * 0 => initial seed from initialization |
| 570 | * 1 => reseed via drbg_seed |
| 571 | * 2 => first invocation from drbg_ctr_update when addtl is present. In |
| 572 | * this case, the df_data scratchpad is not deleted so that it is |
| 573 | * available for another calls to prevent calling the DF function |
| 574 | * again. |
| 575 | * 3 => second invocation from drbg_ctr_update. When the update function |
| 576 | * was called with addtl, the df_data memory already contains the |
| 577 | * DFed addtl information and we do not need to call DF again. |
| 578 | */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 579 | static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed, |
| 580 | int reseed) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 581 | { |
| 582 | int ret = -EFAULT; |
| 583 | /* 10.2.1.2 step 1 */ |
| 584 | unsigned char *temp = drbg->scratchpad; |
| 585 | unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) + |
| 586 | drbg_blocklen(drbg); |
| 587 | unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */ |
| 588 | unsigned int len = 0; |
| 589 | struct drbg_string cipherin; |
| 590 | unsigned char prefix = DRBG_PREFIX1; |
| 591 | |
| 592 | memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg)); |
Stephan Mueller | 72e7c25 | 2014-07-06 02:24:35 +0200 | [diff] [blame] | 593 | if (3 > reseed) |
| 594 | memset(df_data, 0, drbg_statelen(drbg)); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 595 | |
| 596 | /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 597 | if (seed) { |
| 598 | ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 599 | if (ret) |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg)); |
| 604 | /* |
| 605 | * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation |
| 606 | * zeroizes all memory during initialization |
| 607 | */ |
| 608 | while (len < (drbg_statelen(drbg))) { |
| 609 | /* 10.2.1.2 step 2.1 */ |
| 610 | drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1); |
| 611 | /* |
| 612 | * 10.2.1.2 step 2.2 */ |
| 613 | ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin); |
| 614 | if (ret) |
| 615 | goto out; |
| 616 | /* 10.2.1.2 step 2.3 and 3 */ |
| 617 | len += drbg_blocklen(drbg); |
| 618 | } |
| 619 | |
| 620 | /* 10.2.1.2 step 4 */ |
| 621 | temp_p = temp; |
| 622 | df_data_p = df_data; |
| 623 | for (len = 0; len < drbg_statelen(drbg); len++) { |
| 624 | *temp_p ^= *df_data_p; |
| 625 | df_data_p++; temp_p++; |
| 626 | } |
| 627 | |
| 628 | /* 10.2.1.2 step 5 */ |
| 629 | memcpy(drbg->C, temp, drbg_keylen(drbg)); |
| 630 | /* 10.2.1.2 step 6 */ |
| 631 | memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg)); |
| 632 | ret = 0; |
| 633 | |
| 634 | out: |
| 635 | memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg)); |
Stephan Mueller | 72e7c25 | 2014-07-06 02:24:35 +0200 | [diff] [blame] | 636 | if (2 != reseed) |
| 637 | memset(df_data, 0, drbg_statelen(drbg)); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * scratchpad use: drbg_ctr_update is called independently from |
| 643 | * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused |
| 644 | */ |
| 645 | /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */ |
| 646 | static int drbg_ctr_generate(struct drbg_state *drbg, |
| 647 | unsigned char *buf, unsigned int buflen, |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 648 | struct list_head *addtl) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 649 | { |
| 650 | int len = 0; |
| 651 | int ret = 0; |
| 652 | struct drbg_string data; |
| 653 | unsigned char prefix = DRBG_PREFIX1; |
| 654 | |
| 655 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 656 | |
| 657 | /* 10.2.1.5.2 step 2 */ |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 658 | if (addtl && !list_empty(addtl)) { |
| 659 | ret = drbg_ctr_update(drbg, addtl, 2); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 660 | if (ret) |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* 10.2.1.5.2 step 4.1 */ |
| 665 | drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1); |
| 666 | drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg)); |
| 667 | while (len < buflen) { |
| 668 | int outlen = 0; |
| 669 | /* 10.2.1.5.2 step 4.2 */ |
| 670 | ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data); |
| 671 | if (ret) { |
| 672 | len = ret; |
| 673 | goto out; |
| 674 | } |
| 675 | outlen = (drbg_blocklen(drbg) < (buflen - len)) ? |
| 676 | drbg_blocklen(drbg) : (buflen - len); |
| 677 | if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) { |
| 678 | /* 10.2.1.5.2 step 6 */ |
| 679 | drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1); |
| 680 | continue; |
| 681 | } |
| 682 | /* 10.2.1.5.2 step 4.3 */ |
| 683 | memcpy(buf + len, drbg->scratchpad, outlen); |
| 684 | len += outlen; |
| 685 | /* 10.2.1.5.2 step 6 */ |
| 686 | if (len < buflen) |
| 687 | drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1); |
| 688 | } |
| 689 | |
Stephan Mueller | 72e7c25 | 2014-07-06 02:24:35 +0200 | [diff] [blame] | 690 | /* 10.2.1.5.2 step 6 */ |
| 691 | ret = drbg_ctr_update(drbg, NULL, 3); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 692 | if (ret) |
| 693 | len = ret; |
| 694 | |
| 695 | out: |
| 696 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 697 | return len; |
| 698 | } |
| 699 | |
| 700 | static struct drbg_state_ops drbg_ctr_ops = { |
| 701 | .update = drbg_ctr_update, |
| 702 | .generate = drbg_ctr_generate, |
| 703 | .crypto_init = drbg_init_sym_kernel, |
| 704 | .crypto_fini = drbg_fini_sym_kernel, |
| 705 | }; |
| 706 | #endif /* CONFIG_CRYPTO_DRBG_CTR */ |
| 707 | |
| 708 | /****************************************************************** |
| 709 | * HMAC DRBG callback functions |
| 710 | ******************************************************************/ |
| 711 | |
| 712 | #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC) |
| 713 | static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 714 | unsigned char *outval, const struct list_head *in); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 715 | static int drbg_init_hash_kernel(struct drbg_state *drbg); |
| 716 | static int drbg_fini_hash_kernel(struct drbg_state *drbg); |
| 717 | #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */ |
| 718 | |
| 719 | #ifdef CONFIG_CRYPTO_DRBG_HMAC |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 720 | #define CRYPTO_DRBG_HMAC_STRING "HMAC " |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 721 | /* update function of HMAC DRBG as defined in 10.1.2.2 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 722 | static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed, |
| 723 | int reseed) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 724 | { |
| 725 | int ret = -EFAULT; |
| 726 | int i = 0; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 727 | struct drbg_string seed1, seed2, vdata; |
| 728 | LIST_HEAD(seedlist); |
| 729 | LIST_HEAD(vdatalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 730 | |
Stephan Mueller | f072f0e | 2014-08-17 17:38:58 +0200 | [diff] [blame^] | 731 | if (!reseed) |
| 732 | /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */ |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 733 | memset(drbg->V, 1, drbg_statelen(drbg)); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 734 | |
| 735 | drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 736 | list_add_tail(&seed1.list, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 737 | /* buffer of seed2 will be filled in for loop below with one byte */ |
| 738 | drbg_string_fill(&seed2, NULL, 1); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 739 | list_add_tail(&seed2.list, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 740 | /* input data of seed is allowed to be NULL at this point */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 741 | if (seed) |
| 742 | list_splice_tail(seed, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 743 | |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 744 | drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg)); |
| 745 | list_add_tail(&vdata.list, &vdatalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 746 | for (i = 2; 0 < i; i--) { |
| 747 | /* first round uses 0x0, second 0x1 */ |
| 748 | unsigned char prefix = DRBG_PREFIX0; |
| 749 | if (1 == i) |
| 750 | prefix = DRBG_PREFIX1; |
| 751 | /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */ |
| 752 | seed2.buf = &prefix; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 753 | ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 754 | if (ret) |
| 755 | return ret; |
| 756 | |
| 757 | /* 10.1.2.2 step 2 and 5 -- HMAC for V */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 758 | ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 759 | if (ret) |
| 760 | return ret; |
| 761 | |
| 762 | /* 10.1.2.2 step 3 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 763 | if (!seed) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /* generate function of HMAC DRBG as defined in 10.1.2.5 */ |
| 771 | static int drbg_hmac_generate(struct drbg_state *drbg, |
| 772 | unsigned char *buf, |
| 773 | unsigned int buflen, |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 774 | struct list_head *addtl) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 775 | { |
| 776 | int len = 0; |
| 777 | int ret = 0; |
| 778 | struct drbg_string data; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 779 | LIST_HEAD(datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 780 | |
| 781 | /* 10.1.2.5 step 2 */ |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 782 | if (addtl && !list_empty(addtl)) { |
| 783 | ret = drbg_hmac_update(drbg, addtl, 1); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 784 | if (ret) |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | drbg_string_fill(&data, drbg->V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 789 | list_add_tail(&data.list, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 790 | while (len < buflen) { |
| 791 | unsigned int outlen = 0; |
| 792 | /* 10.1.2.5 step 4.1 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 793 | ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 794 | if (ret) |
| 795 | return ret; |
| 796 | outlen = (drbg_blocklen(drbg) < (buflen - len)) ? |
| 797 | drbg_blocklen(drbg) : (buflen - len); |
| 798 | if (!drbg_fips_continuous_test(drbg, drbg->V)) |
| 799 | continue; |
| 800 | |
| 801 | /* 10.1.2.5 step 4.2 */ |
| 802 | memcpy(buf + len, drbg->V, outlen); |
| 803 | len += outlen; |
| 804 | } |
| 805 | |
| 806 | /* 10.1.2.5 step 6 */ |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 807 | if (addtl && !list_empty(addtl)) |
| 808 | ret = drbg_hmac_update(drbg, addtl, 1); |
| 809 | else |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 810 | ret = drbg_hmac_update(drbg, NULL, 1); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 811 | if (ret) |
| 812 | return ret; |
| 813 | |
| 814 | return len; |
| 815 | } |
| 816 | |
| 817 | static struct drbg_state_ops drbg_hmac_ops = { |
| 818 | .update = drbg_hmac_update, |
| 819 | .generate = drbg_hmac_generate, |
| 820 | .crypto_init = drbg_init_hash_kernel, |
| 821 | .crypto_fini = drbg_fini_hash_kernel, |
| 822 | |
| 823 | }; |
| 824 | #endif /* CONFIG_CRYPTO_DRBG_HMAC */ |
| 825 | |
| 826 | /****************************************************************** |
| 827 | * Hash DRBG callback functions |
| 828 | ******************************************************************/ |
| 829 | |
| 830 | #ifdef CONFIG_CRYPTO_DRBG_HASH |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 831 | #define CRYPTO_DRBG_HASH_STRING "HASH " |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 832 | /* |
| 833 | * scratchpad usage: as drbg_hash_update and drbg_hash_df are used |
| 834 | * interlinked, the scratchpad is used as follows: |
| 835 | * drbg_hash_update |
| 836 | * start: drbg->scratchpad |
| 837 | * length: drbg_statelen(drbg) |
| 838 | * drbg_hash_df: |
| 839 | * start: drbg->scratchpad + drbg_statelen(drbg) |
| 840 | * length: drbg_blocklen(drbg) |
| 841 | * |
| 842 | * drbg_hash_process_addtl uses the scratchpad, but fully completes |
| 843 | * before either of the functions mentioned before are invoked. Therefore, |
| 844 | * drbg_hash_process_addtl does not need to be specifically considered. |
| 845 | */ |
| 846 | |
| 847 | /* Derivation Function for Hash DRBG as defined in 10.4.1 */ |
| 848 | static int drbg_hash_df(struct drbg_state *drbg, |
| 849 | unsigned char *outval, size_t outlen, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 850 | struct list_head *entropylist) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 851 | { |
| 852 | int ret = 0; |
| 853 | size_t len = 0; |
| 854 | unsigned char input[5]; |
| 855 | unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 856 | struct drbg_string data; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 857 | |
| 858 | memset(tmp, 0, drbg_blocklen(drbg)); |
| 859 | |
| 860 | /* 10.4.1 step 3 */ |
| 861 | input[0] = 1; |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 862 | drbg_cpu_to_be32((outlen * 8), &input[1]); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 863 | |
| 864 | /* 10.4.1 step 4.1 -- concatenation of data for input into hash */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 865 | drbg_string_fill(&data, input, 5); |
| 866 | list_add(&data.list, entropylist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 867 | |
| 868 | /* 10.4.1 step 4 */ |
| 869 | while (len < outlen) { |
| 870 | short blocklen = 0; |
| 871 | /* 10.4.1 step 4.1 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 872 | ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 873 | if (ret) |
| 874 | goto out; |
| 875 | /* 10.4.1 step 4.2 */ |
| 876 | input[0]++; |
| 877 | blocklen = (drbg_blocklen(drbg) < (outlen - len)) ? |
| 878 | drbg_blocklen(drbg) : (outlen - len); |
| 879 | memcpy(outval + len, tmp, blocklen); |
| 880 | len += blocklen; |
| 881 | } |
| 882 | |
| 883 | out: |
| 884 | memset(tmp, 0, drbg_blocklen(drbg)); |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 889 | static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed, |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 890 | int reseed) |
| 891 | { |
| 892 | int ret = 0; |
| 893 | struct drbg_string data1, data2; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 894 | LIST_HEAD(datalist); |
| 895 | LIST_HEAD(datalist2); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 896 | unsigned char *V = drbg->scratchpad; |
| 897 | unsigned char prefix = DRBG_PREFIX1; |
| 898 | |
| 899 | memset(drbg->scratchpad, 0, drbg_statelen(drbg)); |
| 900 | if (!seed) |
| 901 | return -EINVAL; |
| 902 | |
| 903 | if (reseed) { |
| 904 | /* 10.1.1.3 step 1 */ |
| 905 | memcpy(V, drbg->V, drbg_statelen(drbg)); |
| 906 | drbg_string_fill(&data1, &prefix, 1); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 907 | list_add_tail(&data1.list, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 908 | drbg_string_fill(&data2, V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 909 | list_add_tail(&data2.list, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 910 | } |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 911 | list_splice_tail(seed, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 912 | |
| 913 | /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 914 | ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 915 | if (ret) |
| 916 | goto out; |
| 917 | |
| 918 | /* 10.1.1.2 / 10.1.1.3 step 4 */ |
| 919 | prefix = DRBG_PREFIX0; |
| 920 | drbg_string_fill(&data1, &prefix, 1); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 921 | list_add_tail(&data1.list, &datalist2); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 922 | drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 923 | list_add_tail(&data2.list, &datalist2); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 924 | /* 10.1.1.2 / 10.1.1.3 step 4 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 925 | ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 926 | |
| 927 | out: |
| 928 | memset(drbg->scratchpad, 0, drbg_statelen(drbg)); |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | /* processing of additional information string for Hash DRBG */ |
| 933 | static int drbg_hash_process_addtl(struct drbg_state *drbg, |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 934 | struct list_head *addtl) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 935 | { |
| 936 | int ret = 0; |
| 937 | struct drbg_string data1, data2; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 938 | LIST_HEAD(datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 939 | unsigned char prefix = DRBG_PREFIX2; |
| 940 | |
| 941 | /* this is value w as per documentation */ |
| 942 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 943 | |
| 944 | /* 10.1.1.4 step 2 */ |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 945 | if (!addtl || list_empty(addtl)) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 946 | return 0; |
| 947 | |
| 948 | /* 10.1.1.4 step 2a */ |
| 949 | drbg_string_fill(&data1, &prefix, 1); |
| 950 | drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 951 | list_add_tail(&data1.list, &datalist); |
| 952 | list_add_tail(&data2.list, &datalist); |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 953 | list_splice_tail(addtl, &datalist); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 954 | ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 955 | if (ret) |
| 956 | goto out; |
| 957 | |
| 958 | /* 10.1.1.4 step 2b */ |
| 959 | drbg_add_buf(drbg->V, drbg_statelen(drbg), |
| 960 | drbg->scratchpad, drbg_blocklen(drbg)); |
| 961 | |
| 962 | out: |
| 963 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | /* Hashgen defined in 10.1.1.4 */ |
| 968 | static int drbg_hash_hashgen(struct drbg_state *drbg, |
| 969 | unsigned char *buf, |
| 970 | unsigned int buflen) |
| 971 | { |
| 972 | int len = 0; |
| 973 | int ret = 0; |
| 974 | unsigned char *src = drbg->scratchpad; |
| 975 | unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg); |
| 976 | struct drbg_string data; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 977 | LIST_HEAD(datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 978 | unsigned char prefix = DRBG_PREFIX1; |
| 979 | |
| 980 | memset(src, 0, drbg_statelen(drbg)); |
| 981 | memset(dst, 0, drbg_blocklen(drbg)); |
| 982 | |
| 983 | /* 10.1.1.4 step hashgen 2 */ |
| 984 | memcpy(src, drbg->V, drbg_statelen(drbg)); |
| 985 | |
| 986 | drbg_string_fill(&data, src, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 987 | list_add_tail(&data.list, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 988 | while (len < buflen) { |
| 989 | unsigned int outlen = 0; |
| 990 | /* 10.1.1.4 step hashgen 4.1 */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 991 | ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 992 | if (ret) { |
| 993 | len = ret; |
| 994 | goto out; |
| 995 | } |
| 996 | outlen = (drbg_blocklen(drbg) < (buflen - len)) ? |
| 997 | drbg_blocklen(drbg) : (buflen - len); |
| 998 | if (!drbg_fips_continuous_test(drbg, dst)) { |
| 999 | drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1); |
| 1000 | continue; |
| 1001 | } |
| 1002 | /* 10.1.1.4 step hashgen 4.2 */ |
| 1003 | memcpy(buf + len, dst, outlen); |
| 1004 | len += outlen; |
| 1005 | /* 10.1.1.4 hashgen step 4.3 */ |
| 1006 | if (len < buflen) |
| 1007 | drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1); |
| 1008 | } |
| 1009 | |
| 1010 | out: |
| 1011 | memset(drbg->scratchpad, 0, |
| 1012 | (drbg_statelen(drbg) + drbg_blocklen(drbg))); |
| 1013 | return len; |
| 1014 | } |
| 1015 | |
| 1016 | /* generate function for Hash DRBG as defined in 10.1.1.4 */ |
| 1017 | static int drbg_hash_generate(struct drbg_state *drbg, |
| 1018 | unsigned char *buf, unsigned int buflen, |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 1019 | struct list_head *addtl) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1020 | { |
| 1021 | int len = 0; |
| 1022 | int ret = 0; |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 1023 | union { |
| 1024 | unsigned char req[8]; |
| 1025 | __u64 req_int; |
| 1026 | } u; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1027 | unsigned char prefix = DRBG_PREFIX3; |
| 1028 | struct drbg_string data1, data2; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1029 | LIST_HEAD(datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1030 | |
| 1031 | /* 10.1.1.4 step 2 */ |
| 1032 | ret = drbg_hash_process_addtl(drbg, addtl); |
| 1033 | if (ret) |
| 1034 | return ret; |
| 1035 | /* 10.1.1.4 step 3 */ |
| 1036 | len = drbg_hash_hashgen(drbg, buf, buflen); |
| 1037 | |
| 1038 | /* this is the value H as documented in 10.1.1.4 */ |
| 1039 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 1040 | /* 10.1.1.4 step 4 */ |
| 1041 | drbg_string_fill(&data1, &prefix, 1); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1042 | list_add_tail(&data1.list, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1043 | drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1044 | list_add_tail(&data2.list, &datalist); |
| 1045 | ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1046 | if (ret) { |
| 1047 | len = ret; |
| 1048 | goto out; |
| 1049 | } |
| 1050 | |
| 1051 | /* 10.1.1.4 step 5 */ |
| 1052 | drbg_add_buf(drbg->V, drbg_statelen(drbg), |
| 1053 | drbg->scratchpad, drbg_blocklen(drbg)); |
| 1054 | drbg_add_buf(drbg->V, drbg_statelen(drbg), |
| 1055 | drbg->C, drbg_statelen(drbg)); |
Stephan Mueller | 72f3e00 | 2014-08-17 17:37:34 +0200 | [diff] [blame] | 1056 | u.req_int = cpu_to_be64(drbg->reseed_ctr); |
| 1057 | drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1058 | |
| 1059 | out: |
| 1060 | memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); |
| 1061 | return len; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * scratchpad usage: as update and generate are used isolated, both |
| 1066 | * can use the scratchpad |
| 1067 | */ |
| 1068 | static struct drbg_state_ops drbg_hash_ops = { |
| 1069 | .update = drbg_hash_update, |
| 1070 | .generate = drbg_hash_generate, |
| 1071 | .crypto_init = drbg_init_hash_kernel, |
| 1072 | .crypto_fini = drbg_fini_hash_kernel, |
| 1073 | }; |
| 1074 | #endif /* CONFIG_CRYPTO_DRBG_HASH */ |
| 1075 | |
| 1076 | /****************************************************************** |
| 1077 | * Functions common for DRBG implementations |
| 1078 | ******************************************************************/ |
| 1079 | |
| 1080 | /* |
| 1081 | * Seeding or reseeding of the DRBG |
| 1082 | * |
| 1083 | * @drbg: DRBG state struct |
| 1084 | * @pers: personalization / additional information buffer |
| 1085 | * @reseed: 0 for initial seed process, 1 for reseeding |
| 1086 | * |
| 1087 | * return: |
| 1088 | * 0 on success |
| 1089 | * error value otherwise |
| 1090 | */ |
| 1091 | static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, |
| 1092 | bool reseed) |
| 1093 | { |
| 1094 | int ret = 0; |
| 1095 | unsigned char *entropy = NULL; |
| 1096 | size_t entropylen = 0; |
| 1097 | struct drbg_string data1; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1098 | LIST_HEAD(seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1099 | |
| 1100 | /* 9.1 / 9.2 / 9.3.1 step 3 */ |
| 1101 | if (pers && pers->len > (drbg_max_addtl(drbg))) { |
Stephan Mueller | a908957 | 2014-07-06 02:24:03 +0200 | [diff] [blame] | 1102 | pr_devel("DRBG: personalization string too long %zu\n", |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1103 | pers->len); |
| 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | |
| 1107 | if (drbg->test_data && drbg->test_data->testentropy) { |
| 1108 | drbg_string_fill(&data1, drbg->test_data->testentropy->buf, |
| 1109 | drbg->test_data->testentropy->len); |
| 1110 | pr_devel("DRBG: using test entropy\n"); |
| 1111 | } else { |
| 1112 | /* |
| 1113 | * Gather entropy equal to the security strength of the DRBG. |
| 1114 | * With a derivation function, a nonce is required in addition |
| 1115 | * to the entropy. A nonce must be at least 1/2 of the security |
| 1116 | * strength of the DRBG in size. Thus, entropy * nonce is 3/2 |
| 1117 | * of the strength. The consideration of a nonce is only |
| 1118 | * applicable during initial seeding. |
| 1119 | */ |
| 1120 | entropylen = drbg_sec_strength(drbg->core->flags); |
| 1121 | if (!entropylen) |
| 1122 | return -EFAULT; |
| 1123 | if (!reseed) |
| 1124 | entropylen = ((entropylen + 1) / 2) * 3; |
| 1125 | pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n", |
| 1126 | entropylen); |
| 1127 | entropy = kzalloc(entropylen, GFP_KERNEL); |
| 1128 | if (!entropy) |
| 1129 | return -ENOMEM; |
| 1130 | get_random_bytes(entropy, entropylen); |
| 1131 | drbg_string_fill(&data1, entropy, entropylen); |
| 1132 | } |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1133 | list_add_tail(&data1.list, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * concatenation of entropy with personalization str / addtl input) |
| 1137 | * the variable pers is directly handed in by the caller, so check its |
| 1138 | * contents whether it is appropriate |
| 1139 | */ |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1140 | if (pers && pers->buf && 0 < pers->len) { |
| 1141 | list_add_tail(&pers->list, &seedlist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1142 | pr_devel("DRBG: using personalization string\n"); |
| 1143 | } |
| 1144 | |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1145 | ret = drbg->d_ops->update(drbg, &seedlist, reseed); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1146 | if (ret) |
| 1147 | goto out; |
| 1148 | |
| 1149 | drbg->seeded = true; |
| 1150 | /* 10.1.1.2 / 10.1.1.3 step 5 */ |
| 1151 | drbg->reseed_ctr = 1; |
| 1152 | |
| 1153 | out: |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1154 | kzfree(entropy); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | /* Free all substructures in a DRBG state without the DRBG state structure */ |
| 1159 | static inline void drbg_dealloc_state(struct drbg_state *drbg) |
| 1160 | { |
| 1161 | if (!drbg) |
| 1162 | return; |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1163 | kzfree(drbg->V); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1164 | drbg->V = NULL; |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1165 | kzfree(drbg->C); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1166 | drbg->C = NULL; |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1167 | kzfree(drbg->scratchpad); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1168 | drbg->scratchpad = NULL; |
| 1169 | drbg->reseed_ctr = 0; |
| 1170 | #ifdef CONFIG_CRYPTO_FIPS |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1171 | kzfree(drbg->prev); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1172 | drbg->prev = NULL; |
| 1173 | drbg->fips_primed = false; |
| 1174 | #endif |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Allocate all sub-structures for a DRBG state. |
| 1179 | * The DRBG state structure must already be allocated. |
| 1180 | */ |
| 1181 | static inline int drbg_alloc_state(struct drbg_state *drbg) |
| 1182 | { |
| 1183 | int ret = -ENOMEM; |
| 1184 | unsigned int sb_size = 0; |
| 1185 | |
| 1186 | if (!drbg) |
| 1187 | return -EINVAL; |
| 1188 | |
| 1189 | drbg->V = kzalloc(drbg_statelen(drbg), GFP_KERNEL); |
| 1190 | if (!drbg->V) |
| 1191 | goto err; |
| 1192 | drbg->C = kzalloc(drbg_statelen(drbg), GFP_KERNEL); |
| 1193 | if (!drbg->C) |
| 1194 | goto err; |
| 1195 | #ifdef CONFIG_CRYPTO_FIPS |
| 1196 | drbg->prev = kzalloc(drbg_blocklen(drbg), GFP_KERNEL); |
| 1197 | if (!drbg->prev) |
| 1198 | goto err; |
| 1199 | drbg->fips_primed = false; |
| 1200 | #endif |
| 1201 | /* scratchpad is only generated for CTR and Hash */ |
| 1202 | if (drbg->core->flags & DRBG_HMAC) |
| 1203 | sb_size = 0; |
| 1204 | else if (drbg->core->flags & DRBG_CTR) |
| 1205 | sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */ |
| 1206 | drbg_statelen(drbg) + /* df_data */ |
| 1207 | drbg_blocklen(drbg) + /* pad */ |
| 1208 | drbg_blocklen(drbg) + /* iv */ |
Stephan Mueller | 8fecaad | 2014-07-01 17:08:48 +0200 | [diff] [blame] | 1209 | drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */ |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1210 | else |
| 1211 | sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg); |
| 1212 | |
| 1213 | if (0 < sb_size) { |
| 1214 | drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL); |
| 1215 | if (!drbg->scratchpad) |
| 1216 | goto err; |
| 1217 | } |
| 1218 | spin_lock_init(&drbg->drbg_lock); |
| 1219 | return 0; |
| 1220 | |
| 1221 | err: |
| 1222 | drbg_dealloc_state(drbg); |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Strategy to avoid holding long term locks: generate a shadow copy of DRBG |
| 1228 | * and perform all operations on this shadow copy. After finishing, restore |
| 1229 | * the updated state of the shadow copy into original drbg state. This way, |
| 1230 | * only the read and write operations of the original drbg state must be |
| 1231 | * locked |
| 1232 | */ |
| 1233 | static inline void drbg_copy_drbg(struct drbg_state *src, |
| 1234 | struct drbg_state *dst) |
| 1235 | { |
| 1236 | if (!src || !dst) |
| 1237 | return; |
| 1238 | memcpy(dst->V, src->V, drbg_statelen(src)); |
| 1239 | memcpy(dst->C, src->C, drbg_statelen(src)); |
| 1240 | dst->reseed_ctr = src->reseed_ctr; |
| 1241 | dst->seeded = src->seeded; |
| 1242 | dst->pr = src->pr; |
| 1243 | #ifdef CONFIG_CRYPTO_FIPS |
| 1244 | dst->fips_primed = src->fips_primed; |
| 1245 | memcpy(dst->prev, src->prev, drbg_blocklen(src)); |
| 1246 | #endif |
| 1247 | /* |
| 1248 | * Not copied: |
| 1249 | * scratchpad is initialized drbg_alloc_state; |
| 1250 | * priv_data is initialized with call to crypto_init; |
| 1251 | * d_ops and core are set outside, as these parameters are const; |
| 1252 | * test_data is set outside to prevent it being copied back. |
| 1253 | */ |
| 1254 | } |
| 1255 | |
| 1256 | static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow) |
| 1257 | { |
| 1258 | int ret = -ENOMEM; |
| 1259 | struct drbg_state *tmp = NULL; |
| 1260 | |
| 1261 | if (!drbg || !drbg->core || !drbg->V || !drbg->C) { |
| 1262 | pr_devel("DRBG: attempt to generate shadow copy for " |
| 1263 | "uninitialized DRBG state rejected\n"); |
| 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | /* HMAC does not have a scratchpad */ |
| 1267 | if (!(drbg->core->flags & DRBG_HMAC) && NULL == drbg->scratchpad) |
| 1268 | return -EINVAL; |
| 1269 | |
| 1270 | tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL); |
| 1271 | if (!tmp) |
| 1272 | return -ENOMEM; |
| 1273 | |
| 1274 | /* read-only data as they are defined as const, no lock needed */ |
| 1275 | tmp->core = drbg->core; |
| 1276 | tmp->d_ops = drbg->d_ops; |
| 1277 | |
| 1278 | ret = drbg_alloc_state(tmp); |
| 1279 | if (ret) |
| 1280 | goto err; |
| 1281 | |
| 1282 | spin_lock_bh(&drbg->drbg_lock); |
| 1283 | drbg_copy_drbg(drbg, tmp); |
| 1284 | /* only make a link to the test buffer, as we only read that data */ |
| 1285 | tmp->test_data = drbg->test_data; |
| 1286 | spin_unlock_bh(&drbg->drbg_lock); |
| 1287 | *shadow = tmp; |
| 1288 | return 0; |
| 1289 | |
| 1290 | err: |
Stephan Mueller | 46f64f6 | 2014-08-17 17:37:59 +0200 | [diff] [blame] | 1291 | kzfree(tmp); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | static void drbg_restore_shadow(struct drbg_state *drbg, |
| 1296 | struct drbg_state **shadow) |
| 1297 | { |
| 1298 | struct drbg_state *tmp = *shadow; |
| 1299 | |
| 1300 | spin_lock_bh(&drbg->drbg_lock); |
| 1301 | drbg_copy_drbg(tmp, drbg); |
| 1302 | spin_unlock_bh(&drbg->drbg_lock); |
| 1303 | drbg_dealloc_state(tmp); |
| 1304 | kzfree(tmp); |
| 1305 | *shadow = NULL; |
| 1306 | } |
| 1307 | |
| 1308 | /************************************************************************* |
| 1309 | * DRBG interface functions |
| 1310 | *************************************************************************/ |
| 1311 | |
| 1312 | /* |
| 1313 | * DRBG generate function as required by SP800-90A - this function |
| 1314 | * generates random numbers |
| 1315 | * |
| 1316 | * @drbg DRBG state handle |
| 1317 | * @buf Buffer where to store the random numbers -- the buffer must already |
| 1318 | * be pre-allocated by caller |
| 1319 | * @buflen Length of output buffer - this value defines the number of random |
| 1320 | * bytes pulled from DRBG |
| 1321 | * @addtl Additional input that is mixed into state, may be NULL -- note |
| 1322 | * the entropy is pulled by the DRBG internally unconditionally |
| 1323 | * as defined in SP800-90A. The additional input is mixed into |
| 1324 | * the state in addition to the pulled entropy. |
| 1325 | * |
| 1326 | * return: generated number of bytes |
| 1327 | */ |
| 1328 | static int drbg_generate(struct drbg_state *drbg, |
| 1329 | unsigned char *buf, unsigned int buflen, |
| 1330 | struct drbg_string *addtl) |
| 1331 | { |
| 1332 | int len = 0; |
| 1333 | struct drbg_state *shadow = NULL; |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 1334 | LIST_HEAD(addtllist); |
| 1335 | struct drbg_string timestamp; |
| 1336 | union { |
| 1337 | cycles_t cycles; |
| 1338 | unsigned char char_cycles[sizeof(cycles_t)]; |
| 1339 | } now; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1340 | |
| 1341 | if (0 == buflen || !buf) { |
| 1342 | pr_devel("DRBG: no output buffer provided\n"); |
| 1343 | return -EINVAL; |
| 1344 | } |
| 1345 | if (addtl && NULL == addtl->buf && 0 < addtl->len) { |
| 1346 | pr_devel("DRBG: wrong format of additional information\n"); |
| 1347 | return -EINVAL; |
| 1348 | } |
| 1349 | |
| 1350 | len = drbg_make_shadow(drbg, &shadow); |
| 1351 | if (len) { |
| 1352 | pr_devel("DRBG: shadow copy cannot be generated\n"); |
| 1353 | return len; |
| 1354 | } |
| 1355 | |
| 1356 | /* 9.3.1 step 2 */ |
| 1357 | len = -EINVAL; |
| 1358 | if (buflen > (drbg_max_request_bytes(shadow))) { |
| 1359 | pr_devel("DRBG: requested random numbers too large %u\n", |
| 1360 | buflen); |
| 1361 | goto err; |
| 1362 | } |
| 1363 | |
| 1364 | /* 9.3.1 step 3 is implicit with the chosen DRBG */ |
| 1365 | |
| 1366 | /* 9.3.1 step 4 */ |
| 1367 | if (addtl && addtl->len > (drbg_max_addtl(shadow))) { |
| 1368 | pr_devel("DRBG: additional information string too long %zu\n", |
| 1369 | addtl->len); |
| 1370 | goto err; |
| 1371 | } |
| 1372 | /* 9.3.1 step 5 is implicit with the chosen DRBG */ |
| 1373 | |
| 1374 | /* |
| 1375 | * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented |
| 1376 | * here. The spec is a bit convoluted here, we make it simpler. |
| 1377 | */ |
| 1378 | if ((drbg_max_requests(shadow)) < shadow->reseed_ctr) |
| 1379 | shadow->seeded = false; |
| 1380 | |
| 1381 | /* allocate cipher handle */ |
Stephan Mueller | 45943a5 | 2014-08-17 17:38:29 +0200 | [diff] [blame] | 1382 | len = shadow->d_ops->crypto_init(shadow); |
| 1383 | if (len) |
| 1384 | goto err; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1385 | |
| 1386 | if (shadow->pr || !shadow->seeded) { |
| 1387 | pr_devel("DRBG: reseeding before generation (prediction " |
| 1388 | "resistance: %s, state %s)\n", |
| 1389 | drbg->pr ? "true" : "false", |
| 1390 | drbg->seeded ? "seeded" : "unseeded"); |
| 1391 | /* 9.3.1 steps 7.1 through 7.3 */ |
| 1392 | len = drbg_seed(shadow, addtl, true); |
| 1393 | if (len) |
| 1394 | goto err; |
| 1395 | /* 9.3.1 step 7.4 */ |
| 1396 | addtl = NULL; |
| 1397 | } |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* |
| 1400 | * Mix the time stamp into the DRBG state if the DRBG is not in |
| 1401 | * test mode. If there are two callers invoking the DRBG at the same |
| 1402 | * time, i.e. before the first caller merges its shadow state back, |
| 1403 | * both callers would obtain the same random number stream without |
| 1404 | * changing the state here. |
| 1405 | */ |
| 1406 | if (!drbg->test_data) { |
| 1407 | now.cycles = random_get_entropy(); |
| 1408 | drbg_string_fill(×tamp, now.char_cycles, sizeof(cycles_t)); |
| 1409 | list_add_tail(×tamp.list, &addtllist); |
| 1410 | } |
| 1411 | if (addtl && 0 < addtl->len) |
| 1412 | list_add_tail(&addtl->list, &addtllist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1413 | /* 9.3.1 step 8 and 10 */ |
Stephan Mueller | 27e4de2 | 2014-07-06 02:25:36 +0200 | [diff] [blame] | 1414 | len = shadow->d_ops->generate(shadow, buf, buflen, &addtllist); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1415 | |
| 1416 | /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */ |
| 1417 | shadow->reseed_ctr++; |
| 1418 | if (0 >= len) |
| 1419 | goto err; |
| 1420 | |
| 1421 | /* |
| 1422 | * Section 11.3.3 requires to re-perform self tests after some |
| 1423 | * generated random numbers. The chosen value after which self |
| 1424 | * test is performed is arbitrary, but it should be reasonable. |
| 1425 | * However, we do not perform the self tests because of the following |
| 1426 | * reasons: it is mathematically impossible that the initial self tests |
| 1427 | * were successfully and the following are not. If the initial would |
| 1428 | * pass and the following would not, the kernel integrity is violated. |
| 1429 | * In this case, the entire kernel operation is questionable and it |
| 1430 | * is unlikely that the integrity violation only affects the |
| 1431 | * correct operation of the DRBG. |
| 1432 | * |
| 1433 | * Albeit the following code is commented out, it is provided in |
| 1434 | * case somebody has a need to implement the test of 11.3.3. |
| 1435 | */ |
| 1436 | #if 0 |
| 1437 | if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) { |
| 1438 | int err = 0; |
| 1439 | pr_devel("DRBG: start to perform self test\n"); |
| 1440 | if (drbg->core->flags & DRBG_HMAC) |
| 1441 | err = alg_test("drbg_pr_hmac_sha256", |
| 1442 | "drbg_pr_hmac_sha256", 0, 0); |
| 1443 | else if (drbg->core->flags & DRBG_CTR) |
| 1444 | err = alg_test("drbg_pr_ctr_aes128", |
| 1445 | "drbg_pr_ctr_aes128", 0, 0); |
| 1446 | else |
| 1447 | err = alg_test("drbg_pr_sha256", |
| 1448 | "drbg_pr_sha256", 0, 0); |
| 1449 | if (err) { |
| 1450 | pr_err("DRBG: periodical self test failed\n"); |
| 1451 | /* |
| 1452 | * uninstantiate implies that from now on, only errors |
| 1453 | * are returned when reusing this DRBG cipher handle |
| 1454 | */ |
| 1455 | drbg_uninstantiate(drbg); |
| 1456 | drbg_dealloc_state(shadow); |
| 1457 | kzfree(shadow); |
| 1458 | return 0; |
| 1459 | } else { |
| 1460 | pr_devel("DRBG: self test successful\n"); |
| 1461 | } |
| 1462 | } |
| 1463 | #endif |
| 1464 | |
| 1465 | err: |
Stephan Mueller | 45943a5 | 2014-08-17 17:38:29 +0200 | [diff] [blame] | 1466 | shadow->d_ops->crypto_fini(shadow); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1467 | drbg_restore_shadow(drbg, &shadow); |
| 1468 | return len; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * Wrapper around drbg_generate which can pull arbitrary long strings |
| 1473 | * from the DRBG without hitting the maximum request limitation. |
| 1474 | * |
| 1475 | * Parameters: see drbg_generate |
| 1476 | * Return codes: see drbg_generate -- if one drbg_generate request fails, |
| 1477 | * the entire drbg_generate_long request fails |
| 1478 | */ |
| 1479 | static int drbg_generate_long(struct drbg_state *drbg, |
| 1480 | unsigned char *buf, unsigned int buflen, |
| 1481 | struct drbg_string *addtl) |
| 1482 | { |
| 1483 | int len = 0; |
| 1484 | unsigned int slice = 0; |
| 1485 | do { |
| 1486 | int tmplen = 0; |
| 1487 | unsigned int chunk = 0; |
| 1488 | slice = ((buflen - len) / drbg_max_request_bytes(drbg)); |
| 1489 | chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len); |
| 1490 | tmplen = drbg_generate(drbg, buf + len, chunk, addtl); |
| 1491 | if (0 >= tmplen) |
| 1492 | return tmplen; |
| 1493 | len += tmplen; |
Stephan Mueller | ce5481d | 2014-07-31 21:47:33 +0200 | [diff] [blame] | 1494 | } while (slice > 0 && (len < buflen)); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1495 | return len; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * DRBG instantiation function as required by SP800-90A - this function |
| 1500 | * sets up the DRBG handle, performs the initial seeding and all sanity |
| 1501 | * checks required by SP800-90A |
| 1502 | * |
| 1503 | * @drbg memory of state -- if NULL, new memory is allocated |
| 1504 | * @pers Personalization string that is mixed into state, may be NULL -- note |
| 1505 | * the entropy is pulled by the DRBG internally unconditionally |
| 1506 | * as defined in SP800-90A. The additional input is mixed into |
| 1507 | * the state in addition to the pulled entropy. |
| 1508 | * @coreref reference to core |
| 1509 | * @pr prediction resistance enabled |
| 1510 | * |
| 1511 | * return |
| 1512 | * 0 on success |
| 1513 | * error value otherwise |
| 1514 | */ |
| 1515 | static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, |
| 1516 | int coreref, bool pr) |
| 1517 | { |
| 1518 | int ret = -ENOMEM; |
| 1519 | |
| 1520 | pr_devel("DRBG: Initializing DRBG core %d with prediction resistance " |
| 1521 | "%s\n", coreref, pr ? "enabled" : "disabled"); |
| 1522 | drbg->core = &drbg_cores[coreref]; |
| 1523 | drbg->pr = pr; |
| 1524 | drbg->seeded = false; |
| 1525 | switch (drbg->core->flags & DRBG_TYPE_MASK) { |
| 1526 | #ifdef CONFIG_CRYPTO_DRBG_HMAC |
| 1527 | case DRBG_HMAC: |
| 1528 | drbg->d_ops = &drbg_hmac_ops; |
| 1529 | break; |
| 1530 | #endif /* CONFIG_CRYPTO_DRBG_HMAC */ |
| 1531 | #ifdef CONFIG_CRYPTO_DRBG_HASH |
| 1532 | case DRBG_HASH: |
| 1533 | drbg->d_ops = &drbg_hash_ops; |
| 1534 | break; |
| 1535 | #endif /* CONFIG_CRYPTO_DRBG_HASH */ |
| 1536 | #ifdef CONFIG_CRYPTO_DRBG_CTR |
| 1537 | case DRBG_CTR: |
| 1538 | drbg->d_ops = &drbg_ctr_ops; |
| 1539 | break; |
| 1540 | #endif /* CONFIG_CRYPTO_DRBG_CTR */ |
| 1541 | default: |
| 1542 | return -EOPNOTSUPP; |
| 1543 | } |
| 1544 | |
| 1545 | /* 9.1 step 1 is implicit with the selected DRBG type */ |
| 1546 | |
| 1547 | /* |
| 1548 | * 9.1 step 2 is implicit as caller can select prediction resistance |
| 1549 | * and the flag is copied into drbg->flags -- |
| 1550 | * all DRBG types support prediction resistance |
| 1551 | */ |
| 1552 | |
| 1553 | /* 9.1 step 4 is implicit in drbg_sec_strength */ |
| 1554 | |
| 1555 | ret = drbg_alloc_state(drbg); |
| 1556 | if (ret) |
| 1557 | return ret; |
| 1558 | |
| 1559 | ret = -EFAULT; |
Stephan Mueller | 45943a5 | 2014-08-17 17:38:29 +0200 | [diff] [blame] | 1560 | if (drbg->d_ops->crypto_init(drbg)) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1561 | goto err; |
| 1562 | ret = drbg_seed(drbg, pers, false); |
Stephan Mueller | 45943a5 | 2014-08-17 17:38:29 +0200 | [diff] [blame] | 1563 | drbg->d_ops->crypto_fini(drbg); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1564 | if (ret) |
| 1565 | goto err; |
| 1566 | |
| 1567 | return 0; |
| 1568 | |
| 1569 | err: |
| 1570 | drbg_dealloc_state(drbg); |
| 1571 | return ret; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * DRBG uninstantiate function as required by SP800-90A - this function |
| 1576 | * frees all buffers and the DRBG handle |
| 1577 | * |
| 1578 | * @drbg DRBG state handle |
| 1579 | * |
| 1580 | * return |
| 1581 | * 0 on success |
| 1582 | */ |
| 1583 | static int drbg_uninstantiate(struct drbg_state *drbg) |
| 1584 | { |
| 1585 | spin_lock_bh(&drbg->drbg_lock); |
| 1586 | drbg_dealloc_state(drbg); |
| 1587 | /* no scrubbing of test_data -- this shall survive an uninstantiate */ |
| 1588 | spin_unlock_bh(&drbg->drbg_lock); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * Helper function for setting the test data in the DRBG |
| 1594 | * |
| 1595 | * @drbg DRBG state handle |
| 1596 | * @test_data test data to sets |
| 1597 | */ |
| 1598 | static inline void drbg_set_testdata(struct drbg_state *drbg, |
| 1599 | struct drbg_test_data *test_data) |
| 1600 | { |
| 1601 | if (!test_data || !test_data->testentropy) |
| 1602 | return; |
| 1603 | spin_lock_bh(&drbg->drbg_lock); |
| 1604 | drbg->test_data = test_data; |
| 1605 | spin_unlock_bh(&drbg->drbg_lock); |
| 1606 | } |
| 1607 | |
| 1608 | /*************************************************************** |
| 1609 | * Kernel crypto API cipher invocations requested by DRBG |
| 1610 | ***************************************************************/ |
| 1611 | |
| 1612 | #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC) |
| 1613 | struct sdesc { |
| 1614 | struct shash_desc shash; |
| 1615 | char ctx[]; |
| 1616 | }; |
| 1617 | |
| 1618 | static int drbg_init_hash_kernel(struct drbg_state *drbg) |
| 1619 | { |
| 1620 | struct sdesc *sdesc; |
| 1621 | struct crypto_shash *tfm; |
| 1622 | |
| 1623 | tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0); |
| 1624 | if (IS_ERR(tfm)) { |
| 1625 | pr_info("DRBG: could not allocate digest TFM handle\n"); |
| 1626 | return PTR_ERR(tfm); |
| 1627 | } |
| 1628 | BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm)); |
| 1629 | sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm), |
| 1630 | GFP_KERNEL); |
| 1631 | if (!sdesc) { |
| 1632 | crypto_free_shash(tfm); |
| 1633 | return -ENOMEM; |
| 1634 | } |
| 1635 | |
| 1636 | sdesc->shash.tfm = tfm; |
| 1637 | sdesc->shash.flags = 0; |
| 1638 | drbg->priv_data = sdesc; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static int drbg_fini_hash_kernel(struct drbg_state *drbg) |
| 1643 | { |
| 1644 | struct sdesc *sdesc = (struct sdesc *)drbg->priv_data; |
| 1645 | if (sdesc) { |
| 1646 | crypto_free_shash(sdesc->shash.tfm); |
| 1647 | kzfree(sdesc); |
| 1648 | } |
| 1649 | drbg->priv_data = NULL; |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1654 | unsigned char *outval, const struct list_head *in) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1655 | { |
| 1656 | struct sdesc *sdesc = (struct sdesc *)drbg->priv_data; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1657 | struct drbg_string *input = NULL; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1658 | |
| 1659 | if (key) |
| 1660 | crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg)); |
| 1661 | crypto_shash_init(&sdesc->shash); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1662 | list_for_each_entry(input, in, list) |
| 1663 | crypto_shash_update(&sdesc->shash, input->buf, input->len); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1664 | return crypto_shash_final(&sdesc->shash, outval); |
| 1665 | } |
| 1666 | #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */ |
| 1667 | |
| 1668 | #ifdef CONFIG_CRYPTO_DRBG_CTR |
| 1669 | static int drbg_init_sym_kernel(struct drbg_state *drbg) |
| 1670 | { |
| 1671 | int ret = 0; |
| 1672 | struct crypto_blkcipher *tfm; |
| 1673 | |
| 1674 | tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0); |
| 1675 | if (IS_ERR(tfm)) { |
| 1676 | pr_info("DRBG: could not allocate cipher TFM handle\n"); |
| 1677 | return PTR_ERR(tfm); |
| 1678 | } |
| 1679 | BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm)); |
| 1680 | drbg->priv_data = tfm; |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
| 1684 | static int drbg_fini_sym_kernel(struct drbg_state *drbg) |
| 1685 | { |
| 1686 | struct crypto_blkcipher *tfm = |
| 1687 | (struct crypto_blkcipher *)drbg->priv_data; |
| 1688 | if (tfm) |
| 1689 | crypto_free_blkcipher(tfm); |
| 1690 | drbg->priv_data = NULL; |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key, |
| 1695 | unsigned char *outval, const struct drbg_string *in) |
| 1696 | { |
| 1697 | int ret = 0; |
| 1698 | struct scatterlist sg_in, sg_out; |
| 1699 | struct blkcipher_desc desc; |
| 1700 | struct crypto_blkcipher *tfm = |
| 1701 | (struct crypto_blkcipher *)drbg->priv_data; |
| 1702 | |
| 1703 | desc.tfm = tfm; |
| 1704 | desc.flags = 0; |
| 1705 | crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg))); |
| 1706 | /* there is only component in *in */ |
| 1707 | sg_init_one(&sg_in, in->buf, in->len); |
| 1708 | sg_init_one(&sg_out, outval, drbg_blocklen(drbg)); |
| 1709 | ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len); |
| 1710 | |
| 1711 | return ret; |
| 1712 | } |
| 1713 | #endif /* CONFIG_CRYPTO_DRBG_CTR */ |
| 1714 | |
| 1715 | /*************************************************************** |
| 1716 | * Kernel crypto API interface to register DRBG |
| 1717 | ***************************************************************/ |
| 1718 | |
| 1719 | /* |
| 1720 | * Look up the DRBG flags by given kernel crypto API cra_name |
| 1721 | * The code uses the drbg_cores definition to do this |
| 1722 | * |
| 1723 | * @cra_name kernel crypto API cra_name |
| 1724 | * @coreref reference to integer which is filled with the pointer to |
| 1725 | * the applicable core |
| 1726 | * @pr reference for setting prediction resistance |
| 1727 | * |
| 1728 | * return: flags |
| 1729 | */ |
| 1730 | static inline void drbg_convert_tfm_core(const char *cra_driver_name, |
| 1731 | int *coreref, bool *pr) |
| 1732 | { |
| 1733 | int i = 0; |
| 1734 | size_t start = 0; |
| 1735 | int len = 0; |
| 1736 | |
| 1737 | *pr = true; |
| 1738 | /* disassemble the names */ |
| 1739 | if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) { |
| 1740 | start = 10; |
| 1741 | *pr = false; |
| 1742 | } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) { |
| 1743 | start = 8; |
| 1744 | } else { |
| 1745 | return; |
| 1746 | } |
| 1747 | |
| 1748 | /* remove the first part */ |
| 1749 | len = strlen(cra_driver_name) - start; |
| 1750 | for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) { |
| 1751 | if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name, |
| 1752 | len)) { |
| 1753 | *coreref = i; |
| 1754 | return; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | static int drbg_kcapi_init(struct crypto_tfm *tfm) |
| 1760 | { |
| 1761 | struct drbg_state *drbg = crypto_tfm_ctx(tfm); |
| 1762 | bool pr = false; |
| 1763 | int coreref = 0; |
| 1764 | |
Stephan Mueller | 4f15071 | 2014-07-06 02:25:04 +0200 | [diff] [blame] | 1765 | drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1766 | /* |
| 1767 | * when personalization string is needed, the caller must call reset |
| 1768 | * and provide the personalization string as seed information |
| 1769 | */ |
| 1770 | return drbg_instantiate(drbg, NULL, coreref, pr); |
| 1771 | } |
| 1772 | |
| 1773 | static void drbg_kcapi_cleanup(struct crypto_tfm *tfm) |
| 1774 | { |
| 1775 | drbg_uninstantiate(crypto_tfm_ctx(tfm)); |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * Generate random numbers invoked by the kernel crypto API: |
| 1780 | * The API of the kernel crypto API is extended as follows: |
| 1781 | * |
| 1782 | * If dlen is larger than zero, rdata is interpreted as the output buffer |
| 1783 | * where random data is to be stored. |
| 1784 | * |
| 1785 | * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen |
| 1786 | * which holds the additional information string that is used for the |
| 1787 | * DRBG generation process. The output buffer that is to be used to store |
| 1788 | * data is also pointed to by struct drbg_gen. |
| 1789 | */ |
| 1790 | static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata, |
| 1791 | unsigned int dlen) |
| 1792 | { |
| 1793 | struct drbg_state *drbg = crypto_rng_ctx(tfm); |
| 1794 | if (0 < dlen) { |
| 1795 | return drbg_generate_long(drbg, rdata, dlen, NULL); |
| 1796 | } else { |
| 1797 | struct drbg_gen *data = (struct drbg_gen *)rdata; |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1798 | struct drbg_string addtl; |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1799 | /* catch NULL pointer */ |
| 1800 | if (!data) |
| 1801 | return 0; |
| 1802 | drbg_set_testdata(drbg, data->test_data); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1803 | /* linked list variable is now local to allow modification */ |
| 1804 | drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1805 | return drbg_generate_long(drbg, data->outbuf, data->outlen, |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1806 | &addtl); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * Reset the DRBG invoked by the kernel crypto API |
| 1812 | * The reset implies a full re-initialization of the DRBG. Similar to the |
| 1813 | * generate function of drbg_kcapi_random, this function extends the |
| 1814 | * kernel crypto API interface with struct drbg_gen |
| 1815 | */ |
| 1816 | static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen) |
| 1817 | { |
| 1818 | struct drbg_state *drbg = crypto_rng_ctx(tfm); |
| 1819 | struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm); |
| 1820 | bool pr = false; |
| 1821 | struct drbg_string seed_string; |
| 1822 | int coreref = 0; |
| 1823 | |
| 1824 | drbg_uninstantiate(drbg); |
| 1825 | drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref, |
| 1826 | &pr); |
| 1827 | if (0 < slen) { |
| 1828 | drbg_string_fill(&seed_string, seed, slen); |
| 1829 | return drbg_instantiate(drbg, &seed_string, coreref, pr); |
| 1830 | } else { |
| 1831 | struct drbg_gen *data = (struct drbg_gen *)seed; |
| 1832 | /* allow invocation of API call with NULL, 0 */ |
| 1833 | if (!data) |
| 1834 | return drbg_instantiate(drbg, NULL, coreref, pr); |
| 1835 | drbg_set_testdata(drbg, data->test_data); |
Stephan Mueller | 8c98716 | 2014-06-28 21:58:24 +0200 | [diff] [blame] | 1836 | /* linked list variable is now local to allow modification */ |
| 1837 | drbg_string_fill(&seed_string, data->addtl->buf, |
| 1838 | data->addtl->len); |
| 1839 | return drbg_instantiate(drbg, &seed_string, coreref, pr); |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | /*************************************************************** |
| 1844 | * Kernel module: code to load the module |
| 1845 | ***************************************************************/ |
| 1846 | |
| 1847 | /* |
| 1848 | * Tests as defined in 11.3.2 in addition to the cipher tests: testing |
| 1849 | * of the error handling. |
| 1850 | * |
| 1851 | * Note: testing of failing seed source as defined in 11.3.2 is not applicable |
| 1852 | * as seed source of get_random_bytes does not fail. |
| 1853 | * |
| 1854 | * Note 2: There is no sensible way of testing the reseed counter |
| 1855 | * enforcement, so skip it. |
| 1856 | */ |
| 1857 | static inline int __init drbg_healthcheck_sanity(void) |
| 1858 | { |
| 1859 | #ifdef CONFIG_CRYPTO_FIPS |
| 1860 | int len = 0; |
| 1861 | #define OUTBUFLEN 16 |
| 1862 | unsigned char buf[OUTBUFLEN]; |
| 1863 | struct drbg_state *drbg = NULL; |
| 1864 | int ret = -EFAULT; |
| 1865 | int rc = -EFAULT; |
| 1866 | bool pr = false; |
| 1867 | int coreref = 0; |
| 1868 | struct drbg_string addtl; |
| 1869 | size_t max_addtllen, max_request_bytes; |
| 1870 | |
| 1871 | /* only perform test in FIPS mode */ |
| 1872 | if (!fips_enabled) |
| 1873 | return 0; |
| 1874 | |
| 1875 | #ifdef CONFIG_CRYPTO_DRBG_CTR |
| 1876 | drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr); |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 1877 | #elif defined CONFIG_CRYPTO_DRBG_HASH |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1878 | drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr); |
| 1879 | #else |
| 1880 | drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr); |
| 1881 | #endif |
| 1882 | |
| 1883 | drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL); |
| 1884 | if (!drbg) |
| 1885 | return -ENOMEM; |
| 1886 | |
| 1887 | /* |
| 1888 | * if the following tests fail, it is likely that there is a buffer |
| 1889 | * overflow as buf is much smaller than the requested or provided |
| 1890 | * string lengths -- in case the error handling does not succeed |
| 1891 | * we may get an OOPS. And we want to get an OOPS as this is a |
| 1892 | * grave bug. |
| 1893 | */ |
| 1894 | |
| 1895 | /* get a valid instance of DRBG for following tests */ |
| 1896 | ret = drbg_instantiate(drbg, NULL, coreref, pr); |
| 1897 | if (ret) { |
| 1898 | rc = ret; |
| 1899 | goto outbuf; |
| 1900 | } |
| 1901 | max_addtllen = drbg_max_addtl(drbg); |
| 1902 | max_request_bytes = drbg_max_request_bytes(drbg); |
| 1903 | drbg_string_fill(&addtl, buf, max_addtllen + 1); |
| 1904 | /* overflow addtllen with additonal info string */ |
| 1905 | len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl); |
| 1906 | BUG_ON(0 < len); |
| 1907 | /* overflow max_bits */ |
| 1908 | len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL); |
| 1909 | BUG_ON(0 < len); |
| 1910 | drbg_uninstantiate(drbg); |
| 1911 | |
| 1912 | /* overflow max addtllen with personalization string */ |
| 1913 | ret = drbg_instantiate(drbg, &addtl, coreref, pr); |
| 1914 | BUG_ON(0 == ret); |
| 1915 | /* test uninstantated DRBG */ |
| 1916 | len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL); |
| 1917 | BUG_ON(0 < len); |
| 1918 | /* all tests passed */ |
| 1919 | rc = 0; |
| 1920 | |
| 1921 | pr_devel("DRBG: Sanity tests for failure code paths successfully " |
| 1922 | "completed\n"); |
| 1923 | |
| 1924 | drbg_uninstantiate(drbg); |
| 1925 | outbuf: |
| 1926 | kzfree(drbg); |
| 1927 | return rc; |
| 1928 | #else /* CONFIG_CRYPTO_FIPS */ |
| 1929 | return 0; |
| 1930 | #endif /* CONFIG_CRYPTO_FIPS */ |
| 1931 | } |
| 1932 | |
| 1933 | static struct crypto_alg drbg_algs[22]; |
| 1934 | |
| 1935 | /* |
| 1936 | * Fill the array drbg_algs used to register the different DRBGs |
| 1937 | * with the kernel crypto API. To fill the array, the information |
| 1938 | * from drbg_cores[] is used. |
| 1939 | */ |
| 1940 | static inline void __init drbg_fill_array(struct crypto_alg *alg, |
| 1941 | const struct drbg_core *core, int pr) |
| 1942 | { |
| 1943 | int pos = 0; |
| 1944 | static int priority = 100; |
| 1945 | |
| 1946 | memset(alg, 0, sizeof(struct crypto_alg)); |
| 1947 | memcpy(alg->cra_name, "stdrng", 6); |
| 1948 | if (pr) { |
| 1949 | memcpy(alg->cra_driver_name, "drbg_pr_", 8); |
| 1950 | pos = 8; |
| 1951 | } else { |
| 1952 | memcpy(alg->cra_driver_name, "drbg_nopr_", 10); |
| 1953 | pos = 10; |
| 1954 | } |
| 1955 | memcpy(alg->cra_driver_name + pos, core->cra_name, |
| 1956 | strlen(core->cra_name)); |
| 1957 | |
| 1958 | alg->cra_priority = priority; |
| 1959 | priority++; |
| 1960 | /* |
| 1961 | * If FIPS mode enabled, the selected DRBG shall have the |
| 1962 | * highest cra_priority over other stdrng instances to ensure |
| 1963 | * it is selected. |
| 1964 | */ |
| 1965 | if (fips_enabled) |
| 1966 | alg->cra_priority += 200; |
| 1967 | |
| 1968 | alg->cra_flags = CRYPTO_ALG_TYPE_RNG; |
| 1969 | alg->cra_ctxsize = sizeof(struct drbg_state); |
| 1970 | alg->cra_type = &crypto_rng_type; |
| 1971 | alg->cra_module = THIS_MODULE; |
| 1972 | alg->cra_init = drbg_kcapi_init; |
| 1973 | alg->cra_exit = drbg_kcapi_cleanup; |
| 1974 | alg->cra_u.rng.rng_make_random = drbg_kcapi_random; |
| 1975 | alg->cra_u.rng.rng_reset = drbg_kcapi_reset; |
| 1976 | alg->cra_u.rng.seedsize = 0; |
| 1977 | } |
| 1978 | |
| 1979 | static int __init drbg_init(void) |
| 1980 | { |
| 1981 | unsigned int i = 0; /* pointer to drbg_algs */ |
| 1982 | unsigned int j = 0; /* pointer to drbg_cores */ |
| 1983 | int ret = -EFAULT; |
| 1984 | |
| 1985 | ret = drbg_healthcheck_sanity(); |
| 1986 | if (ret) |
| 1987 | return ret; |
| 1988 | |
| 1989 | if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) { |
| 1990 | pr_info("DRBG: Cannot register all DRBG types" |
Stephan Mueller | a908957 | 2014-07-06 02:24:03 +0200 | [diff] [blame] | 1991 | "(slots needed: %zu, slots available: %zu)\n", |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 1992 | ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs)); |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * each DRBG definition can be used with PR and without PR, thus |
| 1998 | * we instantiate each DRBG in drbg_cores[] twice. |
| 1999 | * |
| 2000 | * As the order of placing them into the drbg_algs array matters |
| 2001 | * (the later DRBGs receive a higher cra_priority) we register the |
| 2002 | * prediction resistance DRBGs first as the should not be too |
| 2003 | * interesting. |
| 2004 | */ |
| 2005 | for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++) |
| 2006 | drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1); |
| 2007 | for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++) |
| 2008 | drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0); |
| 2009 | return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2)); |
| 2010 | } |
| 2011 | |
Fengguang Wu | 96956ae | 2014-07-10 16:52:04 +0800 | [diff] [blame] | 2012 | static void __exit drbg_exit(void) |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 2013 | { |
| 2014 | crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2)); |
| 2015 | } |
| 2016 | |
| 2017 | module_init(drbg_init); |
| 2018 | module_exit(drbg_exit); |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 2019 | #ifndef CRYPTO_DRBG_HASH_STRING |
| 2020 | #define CRYPTO_DRBG_HASH_STRING "" |
| 2021 | #endif |
| 2022 | #ifndef CRYPTO_DRBG_HMAC_STRING |
| 2023 | #define CRYPTO_DRBG_HMAC_STRING "" |
| 2024 | #endif |
| 2025 | #ifndef CRYPTO_DRBG_CTR_STRING |
| 2026 | #define CRYPTO_DRBG_CTR_STRING "" |
| 2027 | #endif |
Stephan Mueller | 541af94 | 2014-05-31 15:44:17 +0200 | [diff] [blame] | 2028 | MODULE_LICENSE("GPL"); |
| 2029 | MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>"); |
Stephan Mueller | e25e47e | 2014-07-06 02:23:03 +0200 | [diff] [blame] | 2030 | MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) " |
| 2031 | "using following cores: " |
| 2032 | CRYPTO_DRBG_HASH_STRING |
| 2033 | CRYPTO_DRBG_HMAC_STRING |
| 2034 | CRYPTO_DRBG_CTR_STRING); |