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