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