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