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