1541af946SStephan Mueller /* 2541af946SStephan Mueller * DRBG: Deterministic Random Bits Generator 3541af946SStephan Mueller * Based on NIST Recommended DRBG from NIST SP800-90A with the following 4541af946SStephan Mueller * properties: 5541af946SStephan Mueller * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores 6541af946SStephan Mueller * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores 7541af946SStephan Mueller * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores 8541af946SStephan Mueller * * with and without prediction resistance 9541af946SStephan Mueller * 10541af946SStephan Mueller * Copyright Stephan Mueller <smueller@chronox.de>, 2014 11541af946SStephan Mueller * 12541af946SStephan Mueller * Redistribution and use in source and binary forms, with or without 13541af946SStephan Mueller * modification, are permitted provided that the following conditions 14541af946SStephan Mueller * are met: 15541af946SStephan Mueller * 1. Redistributions of source code must retain the above copyright 16541af946SStephan Mueller * notice, and the entire permission notice in its entirety, 17541af946SStephan Mueller * including the disclaimer of warranties. 18541af946SStephan Mueller * 2. Redistributions in binary form must reproduce the above copyright 19541af946SStephan Mueller * notice, this list of conditions and the following disclaimer in the 20541af946SStephan Mueller * documentation and/or other materials provided with the distribution. 21541af946SStephan Mueller * 3. The name of the author may not be used to endorse or promote 22541af946SStephan Mueller * products derived from this software without specific prior 23541af946SStephan Mueller * written permission. 24541af946SStephan Mueller * 25541af946SStephan Mueller * ALTERNATIVELY, this product may be distributed under the terms of 26541af946SStephan Mueller * the GNU General Public License, in which case the provisions of the GPL are 27541af946SStephan Mueller * required INSTEAD OF the above restrictions. (This clause is 28541af946SStephan Mueller * necessary due to a potential bad interaction between the GPL and 29541af946SStephan Mueller * the restrictions contained in a BSD-style copyright.) 30541af946SStephan Mueller * 31541af946SStephan Mueller * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 32541af946SStephan Mueller * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 33541af946SStephan Mueller * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF 34541af946SStephan Mueller * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 35541af946SStephan Mueller * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36541af946SStephan Mueller * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 37541af946SStephan Mueller * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 38541af946SStephan Mueller * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 39541af946SStephan Mueller * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 40541af946SStephan Mueller * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 41541af946SStephan Mueller * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH 42541af946SStephan Mueller * DAMAGE. 43541af946SStephan Mueller * 44541af946SStephan Mueller * DRBG Usage 45541af946SStephan Mueller * ========== 46541af946SStephan Mueller * The SP 800-90A DRBG allows the user to specify a personalization string 47541af946SStephan Mueller * for initialization as well as an additional information string for each 48541af946SStephan Mueller * random number request. The following code fragments show how a caller 49541af946SStephan Mueller * uses the kernel crypto API to use the full functionality of the DRBG. 50541af946SStephan Mueller * 51541af946SStephan Mueller * Usage without any additional data 52541af946SStephan Mueller * --------------------------------- 53541af946SStephan Mueller * struct crypto_rng *drng; 54541af946SStephan Mueller * int err; 55541af946SStephan Mueller * char data[DATALEN]; 56541af946SStephan Mueller * 57541af946SStephan Mueller * drng = crypto_alloc_rng(drng_name, 0, 0); 58541af946SStephan Mueller * err = crypto_rng_get_bytes(drng, &data, DATALEN); 59541af946SStephan Mueller * crypto_free_rng(drng); 60541af946SStephan Mueller * 61541af946SStephan Mueller * 62541af946SStephan Mueller * Usage with personalization string during initialization 63541af946SStephan Mueller * ------------------------------------------------------- 64541af946SStephan Mueller * struct crypto_rng *drng; 65541af946SStephan Mueller * int err; 66541af946SStephan Mueller * char data[DATALEN]; 67541af946SStephan Mueller * struct drbg_string pers; 68541af946SStephan Mueller * char personalization[11] = "some-string"; 69541af946SStephan Mueller * 70541af946SStephan Mueller * drbg_string_fill(&pers, personalization, strlen(personalization)); 71541af946SStephan Mueller * drng = crypto_alloc_rng(drng_name, 0, 0); 72541af946SStephan Mueller * // The reset completely re-initializes the DRBG with the provided 73541af946SStephan Mueller * // personalization string 74541af946SStephan Mueller * err = crypto_rng_reset(drng, &personalization, strlen(personalization)); 75541af946SStephan Mueller * err = crypto_rng_get_bytes(drng, &data, DATALEN); 76541af946SStephan Mueller * crypto_free_rng(drng); 77541af946SStephan Mueller * 78541af946SStephan Mueller * 79541af946SStephan Mueller * Usage with additional information string during random number request 80541af946SStephan Mueller * --------------------------------------------------------------------- 81541af946SStephan Mueller * struct crypto_rng *drng; 82541af946SStephan Mueller * int err; 83541af946SStephan Mueller * char data[DATALEN]; 84541af946SStephan Mueller * char addtl_string[11] = "some-string"; 85541af946SStephan Mueller * string drbg_string addtl; 86541af946SStephan Mueller * 87541af946SStephan Mueller * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string)); 88541af946SStephan Mueller * drng = crypto_alloc_rng(drng_name, 0, 0); 89541af946SStephan Mueller * // The following call is a wrapper to crypto_rng_get_bytes() and returns 90541af946SStephan Mueller * // the same error codes. 91541af946SStephan Mueller * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl); 92541af946SStephan Mueller * crypto_free_rng(drng); 93541af946SStephan Mueller * 94541af946SStephan Mueller * 95541af946SStephan Mueller * Usage with personalization and additional information strings 96541af946SStephan Mueller * ------------------------------------------------------------- 97541af946SStephan Mueller * Just mix both scenarios above. 98541af946SStephan Mueller */ 99541af946SStephan Mueller 100541af946SStephan Mueller #include <crypto/drbg.h> 1010eb76ba2SArd Biesheuvel #include <crypto/internal/cipher.h> 10257225e67SStephan Mueller #include <linux/kernel.h> 1038ea5ee00SNicolai Stange #include <linux/jiffies.h> 104541af946SStephan Mueller 105541af946SStephan Mueller /*************************************************************** 106541af946SStephan Mueller * Backend cipher definitions available to DRBG 107541af946SStephan Mueller ***************************************************************/ 108541af946SStephan Mueller 109541af946SStephan Mueller /* 110541af946SStephan Mueller * The order of the DRBG definitions here matter: every DRBG is registered 111541af946SStephan Mueller * as stdrng. Each DRBG receives an increasing cra_priority values the later 112541af946SStephan Mueller * they are defined in this array (see drbg_fill_array). 113541af946SStephan Mueller * 114541af946SStephan Mueller * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and 115541af946SStephan Mueller * the SHA256 / AES 256 over other ciphers. Thus, the favored 116541af946SStephan Mueller * DRBGs are the latest entries in this array. 117541af946SStephan Mueller */ 118541af946SStephan Mueller static const struct drbg_core drbg_cores[] = { 119541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_CTR 120541af946SStephan Mueller { 121541af946SStephan Mueller .flags = DRBG_CTR | DRBG_STRENGTH128, 122541af946SStephan Mueller .statelen = 32, /* 256 bits as defined in 10.2.1 */ 123541af946SStephan Mueller .blocklen_bytes = 16, 124541af946SStephan Mueller .cra_name = "ctr_aes128", 12504bcbfcfSStephan Mueller .backend_cra_name = "aes", 126541af946SStephan Mueller }, { 127541af946SStephan Mueller .flags = DRBG_CTR | DRBG_STRENGTH192, 128541af946SStephan Mueller .statelen = 40, /* 320 bits as defined in 10.2.1 */ 129541af946SStephan Mueller .blocklen_bytes = 16, 130541af946SStephan Mueller .cra_name = "ctr_aes192", 13104bcbfcfSStephan Mueller .backend_cra_name = "aes", 132541af946SStephan Mueller }, { 133541af946SStephan Mueller .flags = DRBG_CTR | DRBG_STRENGTH256, 134541af946SStephan Mueller .statelen = 48, /* 384 bits as defined in 10.2.1 */ 135541af946SStephan Mueller .blocklen_bytes = 16, 136541af946SStephan Mueller .cra_name = "ctr_aes256", 13704bcbfcfSStephan Mueller .backend_cra_name = "aes", 138541af946SStephan Mueller }, 139541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_CTR */ 140541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_HASH 141541af946SStephan Mueller { 142541af946SStephan Mueller .flags = DRBG_HASH | DRBG_STRENGTH128, 143541af946SStephan Mueller .statelen = 55, /* 440 bits */ 144541af946SStephan Mueller .blocklen_bytes = 20, 145541af946SStephan Mueller .cra_name = "sha1", 146541af946SStephan Mueller .backend_cra_name = "sha1", 147541af946SStephan Mueller }, { 148541af946SStephan Mueller .flags = DRBG_HASH | DRBG_STRENGTH256, 149541af946SStephan Mueller .statelen = 111, /* 888 bits */ 150541af946SStephan Mueller .blocklen_bytes = 48, 151541af946SStephan Mueller .cra_name = "sha384", 152541af946SStephan Mueller .backend_cra_name = "sha384", 153541af946SStephan Mueller }, { 154541af946SStephan Mueller .flags = DRBG_HASH | DRBG_STRENGTH256, 155541af946SStephan Mueller .statelen = 111, /* 888 bits */ 156541af946SStephan Mueller .blocklen_bytes = 64, 157541af946SStephan Mueller .cra_name = "sha512", 158541af946SStephan Mueller .backend_cra_name = "sha512", 159541af946SStephan Mueller }, { 160541af946SStephan Mueller .flags = DRBG_HASH | DRBG_STRENGTH256, 161541af946SStephan Mueller .statelen = 55, /* 440 bits */ 162541af946SStephan Mueller .blocklen_bytes = 32, 163541af946SStephan Mueller .cra_name = "sha256", 164541af946SStephan Mueller .backend_cra_name = "sha256", 165541af946SStephan Mueller }, 166541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_HASH */ 167541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_HMAC 168541af946SStephan Mueller { 1695b635e28SStephan Mueller .flags = DRBG_HMAC | DRBG_STRENGTH128, 170541af946SStephan Mueller .statelen = 20, /* block length of cipher */ 171541af946SStephan Mueller .blocklen_bytes = 20, 172541af946SStephan Mueller .cra_name = "hmac_sha1", 173541af946SStephan Mueller .backend_cra_name = "hmac(sha1)", 174541af946SStephan Mueller }, { 175541af946SStephan Mueller .flags = DRBG_HMAC | DRBG_STRENGTH256, 176541af946SStephan Mueller .statelen = 48, /* block length of cipher */ 177541af946SStephan Mueller .blocklen_bytes = 48, 178541af946SStephan Mueller .cra_name = "hmac_sha384", 179541af946SStephan Mueller .backend_cra_name = "hmac(sha384)", 180541af946SStephan Mueller }, { 181541af946SStephan Mueller .flags = DRBG_HMAC | DRBG_STRENGTH256, 182541af946SStephan Mueller .statelen = 32, /* block length of cipher */ 183541af946SStephan Mueller .blocklen_bytes = 32, 184541af946SStephan Mueller .cra_name = "hmac_sha256", 185541af946SStephan Mueller .backend_cra_name = "hmac(sha256)", 1869b7b9468SStephan Müller }, { 1879b7b9468SStephan Müller .flags = DRBG_HMAC | DRBG_STRENGTH256, 1889b7b9468SStephan Müller .statelen = 64, /* block length of cipher */ 1899b7b9468SStephan Müller .blocklen_bytes = 64, 1909b7b9468SStephan Müller .cra_name = "hmac_sha512", 1919b7b9468SStephan Müller .backend_cra_name = "hmac(sha512)", 192541af946SStephan Mueller }, 193541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_HMAC */ 194541af946SStephan Mueller }; 195541af946SStephan Mueller 19657225e67SStephan Mueller static int drbg_uninstantiate(struct drbg_state *drbg); 19757225e67SStephan Mueller 198541af946SStephan Mueller /****************************************************************** 199541af946SStephan Mueller * Generic helper functions 200541af946SStephan Mueller ******************************************************************/ 201541af946SStephan Mueller 202541af946SStephan Mueller /* 203541af946SStephan Mueller * Return strength of DRBG according to SP800-90A section 8.4 204541af946SStephan Mueller * 205541af946SStephan Mueller * @flags DRBG flags reference 206541af946SStephan Mueller * 207541af946SStephan Mueller * Return: normalized strength in *bytes* value or 32 as default 208541af946SStephan Mueller * to counter programming errors 209541af946SStephan Mueller */ 210541af946SStephan Mueller static inline unsigned short drbg_sec_strength(drbg_flag_t flags) 211541af946SStephan Mueller { 212541af946SStephan Mueller switch (flags & DRBG_STRENGTH_MASK) { 213541af946SStephan Mueller case DRBG_STRENGTH128: 214541af946SStephan Mueller return 16; 215541af946SStephan Mueller case DRBG_STRENGTH192: 216541af946SStephan Mueller return 24; 217541af946SStephan Mueller case DRBG_STRENGTH256: 218541af946SStephan Mueller return 32; 219541af946SStephan Mueller default: 220541af946SStephan Mueller return 32; 221541af946SStephan Mueller } 222541af946SStephan Mueller } 223541af946SStephan Mueller 224541af946SStephan Mueller /* 225db07cd26SStephan Mueller * FIPS 140-2 continuous self test for the noise source 226db07cd26SStephan Mueller * The test is performed on the noise source input data. Thus, the function 227db07cd26SStephan Mueller * implicitly knows the size of the buffer to be equal to the security 228db07cd26SStephan Mueller * strength. 229db07cd26SStephan Mueller * 230db07cd26SStephan Mueller * Note, this function disregards the nonce trailing the entropy data during 231db07cd26SStephan Mueller * initial seeding. 232db07cd26SStephan Mueller * 233db07cd26SStephan Mueller * drbg->drbg_mutex must have been taken. 234db07cd26SStephan Mueller * 235db07cd26SStephan Mueller * @drbg DRBG handle 236db07cd26SStephan Mueller * @entropy buffer of seed data to be checked 237db07cd26SStephan Mueller * 238db07cd26SStephan Mueller * return: 239db07cd26SStephan Mueller * 0 on success 240db07cd26SStephan Mueller * -EAGAIN on when the CTRNG is not yet primed 241db07cd26SStephan Mueller * < 0 on error 242db07cd26SStephan Mueller */ 243db07cd26SStephan Mueller static int drbg_fips_continuous_test(struct drbg_state *drbg, 244db07cd26SStephan Mueller const unsigned char *entropy) 245db07cd26SStephan Mueller { 246db07cd26SStephan Mueller unsigned short entropylen = drbg_sec_strength(drbg->core->flags); 247db07cd26SStephan Mueller int ret = 0; 248db07cd26SStephan Mueller 249db07cd26SStephan Mueller if (!IS_ENABLED(CONFIG_CRYPTO_FIPS)) 250db07cd26SStephan Mueller return 0; 251db07cd26SStephan Mueller 252db07cd26SStephan Mueller /* skip test if we test the overall system */ 253db07cd26SStephan Mueller if (list_empty(&drbg->test_data.list)) 254db07cd26SStephan Mueller return 0; 255db07cd26SStephan Mueller /* only perform test in FIPS mode */ 256db07cd26SStephan Mueller if (!fips_enabled) 257db07cd26SStephan Mueller return 0; 258db07cd26SStephan Mueller 259db07cd26SStephan Mueller if (!drbg->fips_primed) { 260db07cd26SStephan Mueller /* Priming of FIPS test */ 261db07cd26SStephan Mueller memcpy(drbg->prev, entropy, entropylen); 262db07cd26SStephan Mueller drbg->fips_primed = true; 263db07cd26SStephan Mueller /* priming: another round is needed */ 264db07cd26SStephan Mueller return -EAGAIN; 265db07cd26SStephan Mueller } 266db07cd26SStephan Mueller ret = memcmp(drbg->prev, entropy, entropylen); 267db07cd26SStephan Mueller if (!ret) 268db07cd26SStephan Mueller panic("DRBG continuous self test failed\n"); 269db07cd26SStephan Mueller memcpy(drbg->prev, entropy, entropylen); 270db07cd26SStephan Mueller 271db07cd26SStephan Mueller /* the test shall pass when the two values are not equal */ 272db07cd26SStephan Mueller return 0; 273db07cd26SStephan Mueller } 274db07cd26SStephan Mueller 275db07cd26SStephan Mueller /* 276541af946SStephan Mueller * Convert an integer into a byte representation of this integer. 277541af946SStephan Mueller * The byte representation is big-endian 278541af946SStephan Mueller * 279541af946SStephan Mueller * @val value to be converted 28072f3e00dSStephan Mueller * @buf buffer holding the converted integer -- caller must ensure that 28172f3e00dSStephan Mueller * buffer size is at least 32 bit 282541af946SStephan Mueller */ 283541af946SStephan Mueller #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR)) 28472f3e00dSStephan Mueller static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf) 285541af946SStephan Mueller { 28672f3e00dSStephan Mueller struct s { 2877c8ae03fSStephan Mueller __be32 conv; 28872f3e00dSStephan Mueller }; 28972f3e00dSStephan Mueller struct s *conversion = (struct s *) buf; 290541af946SStephan Mueller 29172f3e00dSStephan Mueller conversion->conv = cpu_to_be32(val); 292541af946SStephan Mueller } 293541af946SStephan Mueller #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */ 294541af946SStephan Mueller 295541af946SStephan Mueller /****************************************************************** 296541af946SStephan Mueller * CTR DRBG callback functions 297541af946SStephan Mueller ******************************************************************/ 298541af946SStephan Mueller 299541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_CTR 300e25e47ecSStephan Mueller #define CRYPTO_DRBG_CTR_STRING "CTR " 3010653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256"); 3020653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256"); 3030653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192"); 3040653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192"); 3050653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128"); 3060653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128"); 30762b62b6eSStephan Mueller 308ed494d4fSStephan Mueller static void drbg_kcapi_symsetkey(struct drbg_state *drbg, 309ed494d4fSStephan Mueller const unsigned char *key); 310ed494d4fSStephan Mueller static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval, 311ed494d4fSStephan Mueller const struct drbg_string *in); 312541af946SStephan Mueller static int drbg_init_sym_kernel(struct drbg_state *drbg); 313541af946SStephan Mueller static int drbg_fini_sym_kernel(struct drbg_state *drbg); 314a07203fbSStephan Mueller static int drbg_kcapi_sym_ctr(struct drbg_state *drbg, 315a07203fbSStephan Mueller u8 *inbuf, u32 inbuflen, 316a07203fbSStephan Mueller u8 *outbuf, u32 outlen); 31743490e80SStephan Müller #define DRBG_OUTSCRATCHLEN 256 318541af946SStephan Mueller 319541af946SStephan Mueller /* BCC function for CTR DRBG as defined in 10.4.3 */ 320541af946SStephan Mueller static int drbg_ctr_bcc(struct drbg_state *drbg, 321541af946SStephan Mueller unsigned char *out, const unsigned char *key, 3228c987166SStephan Mueller struct list_head *in) 323541af946SStephan Mueller { 3248c987166SStephan Mueller int ret = 0; 3258c987166SStephan Mueller struct drbg_string *curr = NULL; 326541af946SStephan Mueller struct drbg_string data; 3278c987166SStephan Mueller short cnt = 0; 328541af946SStephan Mueller 329541af946SStephan Mueller drbg_string_fill(&data, out, drbg_blocklen(drbg)); 330541af946SStephan Mueller 331541af946SStephan Mueller /* 10.4.3 step 2 / 4 */ 332ed494d4fSStephan Mueller drbg_kcapi_symsetkey(drbg, key); 3338c987166SStephan Mueller list_for_each_entry(curr, in, list) { 3348c987166SStephan Mueller const unsigned char *pos = curr->buf; 3358c987166SStephan Mueller size_t len = curr->len; 336541af946SStephan Mueller /* 10.4.3 step 4.1 */ 3378c987166SStephan Mueller while (len) { 338541af946SStephan Mueller /* 10.4.3 step 4.2 */ 3398c987166SStephan Mueller if (drbg_blocklen(drbg) == cnt) { 3408c987166SStephan Mueller cnt = 0; 341ed494d4fSStephan Mueller ret = drbg_kcapi_sym(drbg, out, &data); 342541af946SStephan Mueller if (ret) 343541af946SStephan Mueller return ret; 344541af946SStephan Mueller } 3458c987166SStephan Mueller out[cnt] ^= *pos; 3468c987166SStephan Mueller pos++; 3478c987166SStephan Mueller cnt++; 3488c987166SStephan Mueller len--; 3498c987166SStephan Mueller } 3508c987166SStephan Mueller } 3518c987166SStephan Mueller /* 10.4.3 step 4.2 for last block */ 3528c987166SStephan Mueller if (cnt) 353ed494d4fSStephan Mueller ret = drbg_kcapi_sym(drbg, out, &data); 3548c987166SStephan Mueller 3558c987166SStephan Mueller return ret; 356541af946SStephan Mueller } 357541af946SStephan Mueller 358541af946SStephan Mueller /* 359541af946SStephan Mueller * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df 360541af946SStephan Mueller * (and drbg_ctr_bcc, but this function does not need any temporary buffers), 361541af946SStephan Mueller * the scratchpad is used as follows: 362541af946SStephan Mueller * drbg_ctr_update: 363541af946SStephan Mueller * temp 364541af946SStephan Mueller * start: drbg->scratchpad 365541af946SStephan Mueller * length: drbg_statelen(drbg) + drbg_blocklen(drbg) 366541af946SStephan Mueller * note: the cipher writing into this variable works 367541af946SStephan Mueller * blocklen-wise. Now, when the statelen is not a multiple 368541af946SStephan Mueller * of blocklen, the generateion loop below "spills over" 369541af946SStephan Mueller * by at most blocklen. Thus, we need to give sufficient 370541af946SStephan Mueller * memory. 371541af946SStephan Mueller * df_data 372541af946SStephan Mueller * start: drbg->scratchpad + 373541af946SStephan Mueller * drbg_statelen(drbg) + drbg_blocklen(drbg) 374541af946SStephan Mueller * length: drbg_statelen(drbg) 375541af946SStephan Mueller * 376541af946SStephan Mueller * drbg_ctr_df: 377541af946SStephan Mueller * pad 378541af946SStephan Mueller * start: df_data + drbg_statelen(drbg) 379541af946SStephan Mueller * length: drbg_blocklen(drbg) 380541af946SStephan Mueller * iv 381541af946SStephan Mueller * start: pad + drbg_blocklen(drbg) 382541af946SStephan Mueller * length: drbg_blocklen(drbg) 383541af946SStephan Mueller * temp 384541af946SStephan Mueller * start: iv + drbg_blocklen(drbg) 3858fecaad7SStephan Mueller * length: drbg_satelen(drbg) + drbg_blocklen(drbg) 3868fecaad7SStephan Mueller * note: temp is the buffer that the BCC function operates 3878fecaad7SStephan Mueller * on. BCC operates blockwise. drbg_statelen(drbg) 3888fecaad7SStephan Mueller * is sufficient when the DRBG state length is a multiple 3898fecaad7SStephan Mueller * of the block size. For AES192 (and maybe other ciphers) 3908fecaad7SStephan Mueller * this is not correct and the length for temp is 3918fecaad7SStephan Mueller * insufficient (yes, that also means for such ciphers, 3928fecaad7SStephan Mueller * the final output of all BCC rounds are truncated). 3938fecaad7SStephan Mueller * Therefore, add drbg_blocklen(drbg) to cover all 3948fecaad7SStephan Mueller * possibilities. 395541af946SStephan Mueller */ 396541af946SStephan Mueller 397541af946SStephan Mueller /* Derivation Function for CTR DRBG as defined in 10.4.2 */ 398541af946SStephan Mueller static int drbg_ctr_df(struct drbg_state *drbg, 399541af946SStephan Mueller unsigned char *df_data, size_t bytes_to_return, 4008c987166SStephan Mueller struct list_head *seedlist) 401541af946SStephan Mueller { 402541af946SStephan Mueller int ret = -EFAULT; 403541af946SStephan Mueller unsigned char L_N[8]; 404541af946SStephan Mueller /* S3 is input */ 405541af946SStephan Mueller struct drbg_string S1, S2, S4, cipherin; 4068c987166SStephan Mueller LIST_HEAD(bcc_list); 407541af946SStephan Mueller unsigned char *pad = df_data + drbg_statelen(drbg); 408541af946SStephan Mueller unsigned char *iv = pad + drbg_blocklen(drbg); 409541af946SStephan Mueller unsigned char *temp = iv + drbg_blocklen(drbg); 410541af946SStephan Mueller size_t padlen = 0; 411541af946SStephan Mueller unsigned int templen = 0; 412541af946SStephan Mueller /* 10.4.2 step 7 */ 413541af946SStephan Mueller unsigned int i = 0; 414541af946SStephan Mueller /* 10.4.2 step 8 */ 415541af946SStephan Mueller const unsigned char *K = (unsigned char *) 416541af946SStephan Mueller "\x00\x01\x02\x03\x04\x05\x06\x07" 417541af946SStephan Mueller "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" 418541af946SStephan Mueller "\x10\x11\x12\x13\x14\x15\x16\x17" 419541af946SStephan Mueller "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; 420541af946SStephan Mueller unsigned char *X; 421541af946SStephan Mueller size_t generated_len = 0; 422541af946SStephan Mueller size_t inputlen = 0; 4238c987166SStephan Mueller struct drbg_string *seed = NULL; 424541af946SStephan Mueller 425541af946SStephan Mueller memset(pad, 0, drbg_blocklen(drbg)); 426541af946SStephan Mueller memset(iv, 0, drbg_blocklen(drbg)); 427541af946SStephan Mueller 428541af946SStephan Mueller /* 10.4.2 step 1 is implicit as we work byte-wise */ 429541af946SStephan Mueller 430541af946SStephan Mueller /* 10.4.2 step 2 */ 431541af946SStephan Mueller if ((512/8) < bytes_to_return) 432541af946SStephan Mueller return -EINVAL; 433541af946SStephan Mueller 434541af946SStephan Mueller /* 10.4.2 step 2 -- calculate the entire length of all input data */ 4358c987166SStephan Mueller list_for_each_entry(seed, seedlist, list) 4368c987166SStephan Mueller inputlen += seed->len; 43772f3e00dSStephan Mueller drbg_cpu_to_be32(inputlen, &L_N[0]); 438541af946SStephan Mueller 439541af946SStephan Mueller /* 10.4.2 step 3 */ 44072f3e00dSStephan Mueller drbg_cpu_to_be32(bytes_to_return, &L_N[4]); 441541af946SStephan Mueller 442541af946SStephan Mueller /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */ 443541af946SStephan Mueller padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg)); 444541af946SStephan Mueller /* wrap the padlen appropriately */ 445541af946SStephan Mueller if (padlen) 446541af946SStephan Mueller padlen = drbg_blocklen(drbg) - padlen; 447541af946SStephan Mueller /* 448541af946SStephan Mueller * pad / padlen contains the 0x80 byte and the following zero bytes. 449541af946SStephan Mueller * As the calculated padlen value only covers the number of zero 450541af946SStephan Mueller * bytes, this value has to be incremented by one for the 0x80 byte. 451541af946SStephan Mueller */ 452541af946SStephan Mueller padlen++; 453541af946SStephan Mueller pad[0] = 0x80; 454541af946SStephan Mueller 455541af946SStephan Mueller /* 10.4.2 step 4 -- first fill the linked list and then order it */ 456541af946SStephan Mueller drbg_string_fill(&S1, iv, drbg_blocklen(drbg)); 4578c987166SStephan Mueller list_add_tail(&S1.list, &bcc_list); 458541af946SStephan Mueller drbg_string_fill(&S2, L_N, sizeof(L_N)); 4598c987166SStephan Mueller list_add_tail(&S2.list, &bcc_list); 4608c987166SStephan Mueller list_splice_tail(seedlist, &bcc_list); 461541af946SStephan Mueller drbg_string_fill(&S4, pad, padlen); 4628c987166SStephan Mueller list_add_tail(&S4.list, &bcc_list); 463541af946SStephan Mueller 464541af946SStephan Mueller /* 10.4.2 step 9 */ 465541af946SStephan Mueller while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) { 466541af946SStephan Mueller /* 467541af946SStephan Mueller * 10.4.2 step 9.1 - the padding is implicit as the buffer 468541af946SStephan Mueller * holds zeros after allocation -- even the increment of i 469541af946SStephan Mueller * is irrelevant as the increment remains within length of i 470541af946SStephan Mueller */ 47172f3e00dSStephan Mueller drbg_cpu_to_be32(i, iv); 472541af946SStephan Mueller /* 10.4.2 step 9.2 -- BCC and concatenation with temp */ 4738c987166SStephan Mueller ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list); 474541af946SStephan Mueller if (ret) 475541af946SStephan Mueller goto out; 476541af946SStephan Mueller /* 10.4.2 step 9.3 */ 477541af946SStephan Mueller i++; 478541af946SStephan Mueller templen += drbg_blocklen(drbg); 479541af946SStephan Mueller } 480541af946SStephan Mueller 481541af946SStephan Mueller /* 10.4.2 step 11 */ 482541af946SStephan Mueller X = temp + (drbg_keylen(drbg)); 483541af946SStephan Mueller drbg_string_fill(&cipherin, X, drbg_blocklen(drbg)); 484541af946SStephan Mueller 485541af946SStephan Mueller /* 10.4.2 step 12: overwriting of outval is implemented in next step */ 486541af946SStephan Mueller 487541af946SStephan Mueller /* 10.4.2 step 13 */ 488ed494d4fSStephan Mueller drbg_kcapi_symsetkey(drbg, temp); 489541af946SStephan Mueller while (generated_len < bytes_to_return) { 490541af946SStephan Mueller short blocklen = 0; 491541af946SStephan Mueller /* 492541af946SStephan Mueller * 10.4.2 step 13.1: the truncation of the key length is 493541af946SStephan Mueller * implicit as the key is only drbg_blocklen in size based on 494541af946SStephan Mueller * the implementation of the cipher function callback 495541af946SStephan Mueller */ 496ed494d4fSStephan Mueller ret = drbg_kcapi_sym(drbg, X, &cipherin); 497541af946SStephan Mueller if (ret) 498541af946SStephan Mueller goto out; 499541af946SStephan Mueller blocklen = (drbg_blocklen(drbg) < 500541af946SStephan Mueller (bytes_to_return - generated_len)) ? 501541af946SStephan Mueller drbg_blocklen(drbg) : 502541af946SStephan Mueller (bytes_to_return - generated_len); 503541af946SStephan Mueller /* 10.4.2 step 13.2 and 14 */ 504541af946SStephan Mueller memcpy(df_data + generated_len, X, blocklen); 505541af946SStephan Mueller generated_len += blocklen; 506541af946SStephan Mueller } 507541af946SStephan Mueller 508541af946SStephan Mueller ret = 0; 509541af946SStephan Mueller 510541af946SStephan Mueller out: 5111471f09fSHerbert Xu memset(iv, 0, drbg_blocklen(drbg)); 5128e0498d9SStephan Mueller memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg)); 5131471f09fSHerbert Xu memset(pad, 0, drbg_blocklen(drbg)); 514541af946SStephan Mueller return ret; 515541af946SStephan Mueller } 516541af946SStephan Mueller 51772e7c25aSStephan Mueller /* 51872e7c25aSStephan Mueller * update function of CTR DRBG as defined in 10.2.1.2 51972e7c25aSStephan Mueller * 52072e7c25aSStephan Mueller * The reseed variable has an enhanced meaning compared to the update 52172e7c25aSStephan Mueller * functions of the other DRBGs as follows: 52272e7c25aSStephan Mueller * 0 => initial seed from initialization 52372e7c25aSStephan Mueller * 1 => reseed via drbg_seed 52472e7c25aSStephan Mueller * 2 => first invocation from drbg_ctr_update when addtl is present. In 52572e7c25aSStephan Mueller * this case, the df_data scratchpad is not deleted so that it is 52672e7c25aSStephan Mueller * available for another calls to prevent calling the DF function 52772e7c25aSStephan Mueller * again. 52872e7c25aSStephan Mueller * 3 => second invocation from drbg_ctr_update. When the update function 52972e7c25aSStephan Mueller * was called with addtl, the df_data memory already contains the 53072e7c25aSStephan Mueller * DFed addtl information and we do not need to call DF again. 53172e7c25aSStephan Mueller */ 5328c987166SStephan Mueller static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed, 5338c987166SStephan Mueller int reseed) 534541af946SStephan Mueller { 535541af946SStephan Mueller int ret = -EFAULT; 536541af946SStephan Mueller /* 10.2.1.2 step 1 */ 537541af946SStephan Mueller unsigned char *temp = drbg->scratchpad; 538541af946SStephan Mueller unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) + 539541af946SStephan Mueller drbg_blocklen(drbg); 540541af946SStephan Mueller 54172e7c25aSStephan Mueller if (3 > reseed) 542541af946SStephan Mueller memset(df_data, 0, drbg_statelen(drbg)); 543541af946SStephan Mueller 54435591285SStephan Mueller if (!reseed) { 54535591285SStephan Mueller /* 54635591285SStephan Mueller * The DRBG uses the CTR mode of the underlying AES cipher. The 54735591285SStephan Mueller * CTR mode increments the counter value after the AES operation 54835591285SStephan Mueller * but SP800-90A requires that the counter is incremented before 54935591285SStephan Mueller * the AES operation. Hence, we increment it at the time we set 55035591285SStephan Mueller * it by one. 55135591285SStephan Mueller */ 55235591285SStephan Mueller crypto_inc(drbg->V, drbg_blocklen(drbg)); 55335591285SStephan Mueller 55435591285SStephan Mueller ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C, 55535591285SStephan Mueller drbg_keylen(drbg)); 55635591285SStephan Mueller if (ret) 55735591285SStephan Mueller goto out; 55835591285SStephan Mueller } 55935591285SStephan Mueller 560541af946SStephan Mueller /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */ 5618c987166SStephan Mueller if (seed) { 5628c987166SStephan Mueller ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed); 563541af946SStephan Mueller if (ret) 564541af946SStephan Mueller goto out; 565541af946SStephan Mueller } 566541af946SStephan Mueller 567a07203fbSStephan Mueller ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg), 568a07203fbSStephan Mueller temp, drbg_statelen(drbg)); 569541af946SStephan Mueller if (ret) 57035591285SStephan Mueller return ret; 571541af946SStephan Mueller 572541af946SStephan Mueller /* 10.2.1.2 step 5 */ 573103eb3f7SStephan Mueller ret = crypto_skcipher_setkey(drbg->ctr_handle, temp, 57435591285SStephan Mueller drbg_keylen(drbg)); 57535591285SStephan Mueller if (ret) 57635591285SStephan Mueller goto out; 577541af946SStephan Mueller /* 10.2.1.2 step 6 */ 578541af946SStephan Mueller memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg)); 57935591285SStephan Mueller /* See above: increment counter by one to compensate timing of CTR op */ 58035591285SStephan Mueller crypto_inc(drbg->V, drbg_blocklen(drbg)); 581541af946SStephan Mueller ret = 0; 582541af946SStephan Mueller 583541af946SStephan Mueller out: 5841471f09fSHerbert Xu memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg)); 58572e7c25aSStephan Mueller if (2 != reseed) 5861471f09fSHerbert Xu memset(df_data, 0, drbg_statelen(drbg)); 587541af946SStephan Mueller return ret; 588541af946SStephan Mueller } 589541af946SStephan Mueller 590541af946SStephan Mueller /* 591541af946SStephan Mueller * scratchpad use: drbg_ctr_update is called independently from 592541af946SStephan Mueller * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused 593541af946SStephan Mueller */ 594541af946SStephan Mueller /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */ 595541af946SStephan Mueller static int drbg_ctr_generate(struct drbg_state *drbg, 596541af946SStephan Mueller unsigned char *buf, unsigned int buflen, 59727e4de2bSStephan Mueller struct list_head *addtl) 598541af946SStephan Mueller { 59935591285SStephan Mueller int ret; 60035591285SStephan Mueller int len = min_t(int, buflen, INT_MAX); 601541af946SStephan Mueller 602541af946SStephan Mueller /* 10.2.1.5.2 step 2 */ 60327e4de2bSStephan Mueller if (addtl && !list_empty(addtl)) { 60427e4de2bSStephan Mueller ret = drbg_ctr_update(drbg, addtl, 2); 605541af946SStephan Mueller if (ret) 606541af946SStephan Mueller return 0; 607541af946SStephan Mueller } 608541af946SStephan Mueller 609541af946SStephan Mueller /* 10.2.1.5.2 step 4.1 */ 61043490e80SStephan Müller ret = drbg_kcapi_sym_ctr(drbg, NULL, 0, buf, len); 61135591285SStephan Mueller if (ret) 61235591285SStephan Mueller return ret; 613541af946SStephan Mueller 61472e7c25aSStephan Mueller /* 10.2.1.5.2 step 6 */ 61572e7c25aSStephan Mueller ret = drbg_ctr_update(drbg, NULL, 3); 616541af946SStephan Mueller if (ret) 617541af946SStephan Mueller len = ret; 618541af946SStephan Mueller 619541af946SStephan Mueller return len; 620541af946SStephan Mueller } 621541af946SStephan Mueller 622e4bc02acSJulia Lawall static const struct drbg_state_ops drbg_ctr_ops = { 623541af946SStephan Mueller .update = drbg_ctr_update, 624541af946SStephan Mueller .generate = drbg_ctr_generate, 625541af946SStephan Mueller .crypto_init = drbg_init_sym_kernel, 626541af946SStephan Mueller .crypto_fini = drbg_fini_sym_kernel, 627541af946SStephan Mueller }; 628541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_CTR */ 629541af946SStephan Mueller 630541af946SStephan Mueller /****************************************************************** 631541af946SStephan Mueller * HMAC DRBG callback functions 632541af946SStephan Mueller ******************************************************************/ 633541af946SStephan Mueller 634541af946SStephan Mueller #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC) 6354218ebe8SStephan Mueller static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval, 6364218ebe8SStephan Mueller const struct list_head *in); 6374218ebe8SStephan Mueller static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg, 6384218ebe8SStephan Mueller const unsigned char *key); 639541af946SStephan Mueller static int drbg_init_hash_kernel(struct drbg_state *drbg); 640541af946SStephan Mueller static int drbg_fini_hash_kernel(struct drbg_state *drbg); 641541af946SStephan Mueller #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */ 642541af946SStephan Mueller 643541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_HMAC 644e25e47ecSStephan Mueller #define CRYPTO_DRBG_HMAC_STRING "HMAC " 6450653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512"); 6460653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512"); 6470653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384"); 6480653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384"); 6490653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256"); 6500653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256"); 6510653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1"); 6520653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1"); 65362b62b6eSStephan Mueller 654541af946SStephan Mueller /* update function of HMAC DRBG as defined in 10.1.2.2 */ 6558c987166SStephan Mueller static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed, 6568c987166SStephan Mueller int reseed) 657541af946SStephan Mueller { 658541af946SStephan Mueller int ret = -EFAULT; 659541af946SStephan Mueller int i = 0; 6608c987166SStephan Mueller struct drbg_string seed1, seed2, vdata; 6618c987166SStephan Mueller LIST_HEAD(seedlist); 6628c987166SStephan Mueller LIST_HEAD(vdatalist); 663541af946SStephan Mueller 6644218ebe8SStephan Mueller if (!reseed) { 665f072f0e0SStephan Mueller /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */ 666541af946SStephan Mueller memset(drbg->V, 1, drbg_statelen(drbg)); 6674218ebe8SStephan Mueller drbg_kcapi_hmacsetkey(drbg, drbg->C); 6684218ebe8SStephan Mueller } 669541af946SStephan Mueller 670541af946SStephan Mueller drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg)); 6718c987166SStephan Mueller list_add_tail(&seed1.list, &seedlist); 672541af946SStephan Mueller /* buffer of seed2 will be filled in for loop below with one byte */ 673541af946SStephan Mueller drbg_string_fill(&seed2, NULL, 1); 6748c987166SStephan Mueller list_add_tail(&seed2.list, &seedlist); 675541af946SStephan Mueller /* input data of seed is allowed to be NULL at this point */ 6768c987166SStephan Mueller if (seed) 6778c987166SStephan Mueller list_splice_tail(seed, &seedlist); 678541af946SStephan Mueller 6798c987166SStephan Mueller drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg)); 6808c987166SStephan Mueller list_add_tail(&vdata.list, &vdatalist); 681541af946SStephan Mueller for (i = 2; 0 < i; i--) { 682541af946SStephan Mueller /* first round uses 0x0, second 0x1 */ 683541af946SStephan Mueller unsigned char prefix = DRBG_PREFIX0; 684541af946SStephan Mueller if (1 == i) 685541af946SStephan Mueller prefix = DRBG_PREFIX1; 686541af946SStephan Mueller /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */ 687541af946SStephan Mueller seed2.buf = &prefix; 6884218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist); 689541af946SStephan Mueller if (ret) 690541af946SStephan Mueller return ret; 6914218ebe8SStephan Mueller drbg_kcapi_hmacsetkey(drbg, drbg->C); 692541af946SStephan Mueller 693541af946SStephan Mueller /* 10.1.2.2 step 2 and 5 -- HMAC for V */ 6944218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist); 695541af946SStephan Mueller if (ret) 696541af946SStephan Mueller return ret; 697541af946SStephan Mueller 698541af946SStephan Mueller /* 10.1.2.2 step 3 */ 6998c987166SStephan Mueller if (!seed) 700541af946SStephan Mueller return ret; 701541af946SStephan Mueller } 702541af946SStephan Mueller 703541af946SStephan Mueller return 0; 704541af946SStephan Mueller } 705541af946SStephan Mueller 706541af946SStephan Mueller /* generate function of HMAC DRBG as defined in 10.1.2.5 */ 707541af946SStephan Mueller static int drbg_hmac_generate(struct drbg_state *drbg, 708541af946SStephan Mueller unsigned char *buf, 709541af946SStephan Mueller unsigned int buflen, 71027e4de2bSStephan Mueller struct list_head *addtl) 711541af946SStephan Mueller { 712541af946SStephan Mueller int len = 0; 713541af946SStephan Mueller int ret = 0; 714541af946SStephan Mueller struct drbg_string data; 7158c987166SStephan Mueller LIST_HEAD(datalist); 716541af946SStephan Mueller 717541af946SStephan Mueller /* 10.1.2.5 step 2 */ 71827e4de2bSStephan Mueller if (addtl && !list_empty(addtl)) { 71927e4de2bSStephan Mueller ret = drbg_hmac_update(drbg, addtl, 1); 720541af946SStephan Mueller if (ret) 721541af946SStephan Mueller return ret; 722541af946SStephan Mueller } 723541af946SStephan Mueller 724541af946SStephan Mueller drbg_string_fill(&data, drbg->V, drbg_statelen(drbg)); 7258c987166SStephan Mueller list_add_tail(&data.list, &datalist); 726541af946SStephan Mueller while (len < buflen) { 727541af946SStephan Mueller unsigned int outlen = 0; 728541af946SStephan Mueller /* 10.1.2.5 step 4.1 */ 7294218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, drbg->V, &datalist); 730541af946SStephan Mueller if (ret) 731541af946SStephan Mueller return ret; 732541af946SStephan Mueller outlen = (drbg_blocklen(drbg) < (buflen - len)) ? 733541af946SStephan Mueller drbg_blocklen(drbg) : (buflen - len); 734541af946SStephan Mueller 735541af946SStephan Mueller /* 10.1.2.5 step 4.2 */ 736541af946SStephan Mueller memcpy(buf + len, drbg->V, outlen); 737541af946SStephan Mueller len += outlen; 738541af946SStephan Mueller } 739541af946SStephan Mueller 740541af946SStephan Mueller /* 10.1.2.5 step 6 */ 74127e4de2bSStephan Mueller if (addtl && !list_empty(addtl)) 74227e4de2bSStephan Mueller ret = drbg_hmac_update(drbg, addtl, 1); 74327e4de2bSStephan Mueller else 7448c987166SStephan Mueller ret = drbg_hmac_update(drbg, NULL, 1); 745541af946SStephan Mueller if (ret) 746541af946SStephan Mueller return ret; 747541af946SStephan Mueller 748541af946SStephan Mueller return len; 749541af946SStephan Mueller } 750541af946SStephan Mueller 751e4bc02acSJulia Lawall static const struct drbg_state_ops drbg_hmac_ops = { 752541af946SStephan Mueller .update = drbg_hmac_update, 753541af946SStephan Mueller .generate = drbg_hmac_generate, 754541af946SStephan Mueller .crypto_init = drbg_init_hash_kernel, 755541af946SStephan Mueller .crypto_fini = drbg_fini_hash_kernel, 756541af946SStephan Mueller }; 757541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_HMAC */ 758541af946SStephan Mueller 759541af946SStephan Mueller /****************************************************************** 760541af946SStephan Mueller * Hash DRBG callback functions 761541af946SStephan Mueller ******************************************************************/ 762541af946SStephan Mueller 763541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_HASH 764e25e47ecSStephan Mueller #define CRYPTO_DRBG_HASH_STRING "HASH " 7650653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_sha512"); 7660653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_sha512"); 7670653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_sha384"); 7680653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_sha384"); 7690653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_sha256"); 7700653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_sha256"); 7710653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_pr_sha1"); 7720653a7cfSStephan Mueller MODULE_ALIAS_CRYPTO("drbg_nopr_sha1"); 77362b62b6eSStephan Mueller 774541af946SStephan Mueller /* 77541a84982SStephan Mueller * Increment buffer 77641a84982SStephan Mueller * 77741a84982SStephan Mueller * @dst buffer to increment 77841a84982SStephan Mueller * @add value to add 77941a84982SStephan Mueller */ 78041a84982SStephan Mueller static inline void drbg_add_buf(unsigned char *dst, size_t dstlen, 78141a84982SStephan Mueller const unsigned char *add, size_t addlen) 78241a84982SStephan Mueller { 78341a84982SStephan Mueller /* implied: dstlen > addlen */ 78441a84982SStephan Mueller unsigned char *dstptr; 78541a84982SStephan Mueller const unsigned char *addptr; 78641a84982SStephan Mueller unsigned int remainder = 0; 78741a84982SStephan Mueller size_t len = addlen; 78841a84982SStephan Mueller 78941a84982SStephan Mueller dstptr = dst + (dstlen-1); 79041a84982SStephan Mueller addptr = add + (addlen-1); 79141a84982SStephan Mueller while (len) { 79241a84982SStephan Mueller remainder += *dstptr + *addptr; 79341a84982SStephan Mueller *dstptr = remainder & 0xff; 79441a84982SStephan Mueller remainder >>= 8; 79541a84982SStephan Mueller len--; dstptr--; addptr--; 79641a84982SStephan Mueller } 79741a84982SStephan Mueller len = dstlen - addlen; 79841a84982SStephan Mueller while (len && remainder > 0) { 79941a84982SStephan Mueller remainder = *dstptr + 1; 80041a84982SStephan Mueller *dstptr = remainder & 0xff; 80141a84982SStephan Mueller remainder >>= 8; 80241a84982SStephan Mueller len--; dstptr--; 80341a84982SStephan Mueller } 80441a84982SStephan Mueller } 80541a84982SStephan Mueller 80641a84982SStephan Mueller /* 807541af946SStephan Mueller * scratchpad usage: as drbg_hash_update and drbg_hash_df are used 808541af946SStephan Mueller * interlinked, the scratchpad is used as follows: 809541af946SStephan Mueller * drbg_hash_update 810541af946SStephan Mueller * start: drbg->scratchpad 811541af946SStephan Mueller * length: drbg_statelen(drbg) 812541af946SStephan Mueller * drbg_hash_df: 813541af946SStephan Mueller * start: drbg->scratchpad + drbg_statelen(drbg) 814541af946SStephan Mueller * length: drbg_blocklen(drbg) 815541af946SStephan Mueller * 816541af946SStephan Mueller * drbg_hash_process_addtl uses the scratchpad, but fully completes 817541af946SStephan Mueller * before either of the functions mentioned before are invoked. Therefore, 818541af946SStephan Mueller * drbg_hash_process_addtl does not need to be specifically considered. 819541af946SStephan Mueller */ 820541af946SStephan Mueller 821541af946SStephan Mueller /* Derivation Function for Hash DRBG as defined in 10.4.1 */ 822541af946SStephan Mueller static int drbg_hash_df(struct drbg_state *drbg, 823541af946SStephan Mueller unsigned char *outval, size_t outlen, 8248c987166SStephan Mueller struct list_head *entropylist) 825541af946SStephan Mueller { 826541af946SStephan Mueller int ret = 0; 827541af946SStephan Mueller size_t len = 0; 828541af946SStephan Mueller unsigned char input[5]; 829541af946SStephan Mueller unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg); 8308c987166SStephan Mueller struct drbg_string data; 831541af946SStephan Mueller 832541af946SStephan Mueller /* 10.4.1 step 3 */ 833541af946SStephan Mueller input[0] = 1; 83472f3e00dSStephan Mueller drbg_cpu_to_be32((outlen * 8), &input[1]); 835541af946SStephan Mueller 836541af946SStephan Mueller /* 10.4.1 step 4.1 -- concatenation of data for input into hash */ 8378c987166SStephan Mueller drbg_string_fill(&data, input, 5); 8388c987166SStephan Mueller list_add(&data.list, entropylist); 839541af946SStephan Mueller 840541af946SStephan Mueller /* 10.4.1 step 4 */ 841541af946SStephan Mueller while (len < outlen) { 842541af946SStephan Mueller short blocklen = 0; 843541af946SStephan Mueller /* 10.4.1 step 4.1 */ 8444218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, tmp, entropylist); 845541af946SStephan Mueller if (ret) 846541af946SStephan Mueller goto out; 847541af946SStephan Mueller /* 10.4.1 step 4.2 */ 848541af946SStephan Mueller input[0]++; 849541af946SStephan Mueller blocklen = (drbg_blocklen(drbg) < (outlen - len)) ? 850541af946SStephan Mueller drbg_blocklen(drbg) : (outlen - len); 851541af946SStephan Mueller memcpy(outval + len, tmp, blocklen); 852541af946SStephan Mueller len += blocklen; 853541af946SStephan Mueller } 854541af946SStephan Mueller 855541af946SStephan Mueller out: 8561471f09fSHerbert Xu memset(tmp, 0, drbg_blocklen(drbg)); 857541af946SStephan Mueller return ret; 858541af946SStephan Mueller } 859541af946SStephan Mueller 860541af946SStephan Mueller /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */ 8618c987166SStephan Mueller static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed, 862541af946SStephan Mueller int reseed) 863541af946SStephan Mueller { 864541af946SStephan Mueller int ret = 0; 865541af946SStephan Mueller struct drbg_string data1, data2; 8668c987166SStephan Mueller LIST_HEAD(datalist); 8678c987166SStephan Mueller LIST_HEAD(datalist2); 868541af946SStephan Mueller unsigned char *V = drbg->scratchpad; 869541af946SStephan Mueller unsigned char prefix = DRBG_PREFIX1; 870541af946SStephan Mueller 871541af946SStephan Mueller if (!seed) 872541af946SStephan Mueller return -EINVAL; 873541af946SStephan Mueller 874541af946SStephan Mueller if (reseed) { 875541af946SStephan Mueller /* 10.1.1.3 step 1 */ 876541af946SStephan Mueller memcpy(V, drbg->V, drbg_statelen(drbg)); 877541af946SStephan Mueller drbg_string_fill(&data1, &prefix, 1); 8788c987166SStephan Mueller list_add_tail(&data1.list, &datalist); 879541af946SStephan Mueller drbg_string_fill(&data2, V, drbg_statelen(drbg)); 8808c987166SStephan Mueller list_add_tail(&data2.list, &datalist); 881541af946SStephan Mueller } 8828c987166SStephan Mueller list_splice_tail(seed, &datalist); 883541af946SStephan Mueller 884541af946SStephan Mueller /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */ 8858c987166SStephan Mueller ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist); 886541af946SStephan Mueller if (ret) 887541af946SStephan Mueller goto out; 888541af946SStephan Mueller 889541af946SStephan Mueller /* 10.1.1.2 / 10.1.1.3 step 4 */ 890541af946SStephan Mueller prefix = DRBG_PREFIX0; 891541af946SStephan Mueller drbg_string_fill(&data1, &prefix, 1); 8928c987166SStephan Mueller list_add_tail(&data1.list, &datalist2); 893541af946SStephan Mueller drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); 8948c987166SStephan Mueller list_add_tail(&data2.list, &datalist2); 895541af946SStephan Mueller /* 10.1.1.2 / 10.1.1.3 step 4 */ 8968c987166SStephan Mueller ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2); 897541af946SStephan Mueller 898541af946SStephan Mueller out: 8991471f09fSHerbert Xu memset(drbg->scratchpad, 0, drbg_statelen(drbg)); 900541af946SStephan Mueller return ret; 901541af946SStephan Mueller } 902541af946SStephan Mueller 903541af946SStephan Mueller /* processing of additional information string for Hash DRBG */ 904541af946SStephan Mueller static int drbg_hash_process_addtl(struct drbg_state *drbg, 90527e4de2bSStephan Mueller struct list_head *addtl) 906541af946SStephan Mueller { 907541af946SStephan Mueller int ret = 0; 908541af946SStephan Mueller struct drbg_string data1, data2; 9098c987166SStephan Mueller LIST_HEAD(datalist); 910541af946SStephan Mueller unsigned char prefix = DRBG_PREFIX2; 911541af946SStephan Mueller 912541af946SStephan Mueller /* 10.1.1.4 step 2 */ 91327e4de2bSStephan Mueller if (!addtl || list_empty(addtl)) 914541af946SStephan Mueller return 0; 915541af946SStephan Mueller 916541af946SStephan Mueller /* 10.1.1.4 step 2a */ 917541af946SStephan Mueller drbg_string_fill(&data1, &prefix, 1); 918541af946SStephan Mueller drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); 9198c987166SStephan Mueller list_add_tail(&data1.list, &datalist); 9208c987166SStephan Mueller list_add_tail(&data2.list, &datalist); 92127e4de2bSStephan Mueller list_splice_tail(addtl, &datalist); 9224218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist); 923541af946SStephan Mueller if (ret) 924541af946SStephan Mueller goto out; 925541af946SStephan Mueller 926541af946SStephan Mueller /* 10.1.1.4 step 2b */ 927541af946SStephan Mueller drbg_add_buf(drbg->V, drbg_statelen(drbg), 928541af946SStephan Mueller drbg->scratchpad, drbg_blocklen(drbg)); 929541af946SStephan Mueller 930541af946SStephan Mueller out: 9311471f09fSHerbert Xu memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); 932541af946SStephan Mueller return ret; 933541af946SStephan Mueller } 934541af946SStephan Mueller 935541af946SStephan Mueller /* Hashgen defined in 10.1.1.4 */ 936541af946SStephan Mueller static int drbg_hash_hashgen(struct drbg_state *drbg, 937541af946SStephan Mueller unsigned char *buf, 938541af946SStephan Mueller unsigned int buflen) 939541af946SStephan Mueller { 940541af946SStephan Mueller int len = 0; 941541af946SStephan Mueller int ret = 0; 942541af946SStephan Mueller unsigned char *src = drbg->scratchpad; 943541af946SStephan Mueller unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg); 944541af946SStephan Mueller struct drbg_string data; 9458c987166SStephan Mueller LIST_HEAD(datalist); 946541af946SStephan Mueller 947541af946SStephan Mueller /* 10.1.1.4 step hashgen 2 */ 948541af946SStephan Mueller memcpy(src, drbg->V, drbg_statelen(drbg)); 949541af946SStephan Mueller 950541af946SStephan Mueller drbg_string_fill(&data, src, drbg_statelen(drbg)); 9518c987166SStephan Mueller list_add_tail(&data.list, &datalist); 952541af946SStephan Mueller while (len < buflen) { 953541af946SStephan Mueller unsigned int outlen = 0; 954541af946SStephan Mueller /* 10.1.1.4 step hashgen 4.1 */ 9554218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, dst, &datalist); 956541af946SStephan Mueller if (ret) { 957541af946SStephan Mueller len = ret; 958541af946SStephan Mueller goto out; 959541af946SStephan Mueller } 960541af946SStephan Mueller outlen = (drbg_blocklen(drbg) < (buflen - len)) ? 961541af946SStephan Mueller drbg_blocklen(drbg) : (buflen - len); 962541af946SStephan Mueller /* 10.1.1.4 step hashgen 4.2 */ 963541af946SStephan Mueller memcpy(buf + len, dst, outlen); 964541af946SStephan Mueller len += outlen; 965541af946SStephan Mueller /* 10.1.1.4 hashgen step 4.3 */ 966541af946SStephan Mueller if (len < buflen) 96741a84982SStephan Mueller crypto_inc(src, drbg_statelen(drbg)); 968541af946SStephan Mueller } 969541af946SStephan Mueller 970541af946SStephan Mueller out: 9711471f09fSHerbert Xu memset(drbg->scratchpad, 0, 972541af946SStephan Mueller (drbg_statelen(drbg) + drbg_blocklen(drbg))); 973541af946SStephan Mueller return len; 974541af946SStephan Mueller } 975541af946SStephan Mueller 976541af946SStephan Mueller /* generate function for Hash DRBG as defined in 10.1.1.4 */ 977541af946SStephan Mueller static int drbg_hash_generate(struct drbg_state *drbg, 978541af946SStephan Mueller unsigned char *buf, unsigned int buflen, 97927e4de2bSStephan Mueller struct list_head *addtl) 980541af946SStephan Mueller { 981541af946SStephan Mueller int len = 0; 982541af946SStephan Mueller int ret = 0; 98372f3e00dSStephan Mueller union { 984541af946SStephan Mueller unsigned char req[8]; 9857c8ae03fSStephan Mueller __be64 req_int; 98672f3e00dSStephan Mueller } u; 987541af946SStephan Mueller unsigned char prefix = DRBG_PREFIX3; 988541af946SStephan Mueller struct drbg_string data1, data2; 9898c987166SStephan Mueller LIST_HEAD(datalist); 990541af946SStephan Mueller 991541af946SStephan Mueller /* 10.1.1.4 step 2 */ 992541af946SStephan Mueller ret = drbg_hash_process_addtl(drbg, addtl); 993541af946SStephan Mueller if (ret) 994541af946SStephan Mueller return ret; 995541af946SStephan Mueller /* 10.1.1.4 step 3 */ 996541af946SStephan Mueller len = drbg_hash_hashgen(drbg, buf, buflen); 997541af946SStephan Mueller 998541af946SStephan Mueller /* this is the value H as documented in 10.1.1.4 */ 999541af946SStephan Mueller /* 10.1.1.4 step 4 */ 1000541af946SStephan Mueller drbg_string_fill(&data1, &prefix, 1); 10018c987166SStephan Mueller list_add_tail(&data1.list, &datalist); 1002541af946SStephan Mueller drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg)); 10038c987166SStephan Mueller list_add_tail(&data2.list, &datalist); 10044218ebe8SStephan Mueller ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist); 1005541af946SStephan Mueller if (ret) { 1006541af946SStephan Mueller len = ret; 1007541af946SStephan Mueller goto out; 1008541af946SStephan Mueller } 1009541af946SStephan Mueller 1010541af946SStephan Mueller /* 10.1.1.4 step 5 */ 1011541af946SStephan Mueller drbg_add_buf(drbg->V, drbg_statelen(drbg), 1012541af946SStephan Mueller drbg->scratchpad, drbg_blocklen(drbg)); 1013541af946SStephan Mueller drbg_add_buf(drbg->V, drbg_statelen(drbg), 1014541af946SStephan Mueller drbg->C, drbg_statelen(drbg)); 101572f3e00dSStephan Mueller u.req_int = cpu_to_be64(drbg->reseed_ctr); 101672f3e00dSStephan Mueller drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8); 1017541af946SStephan Mueller 1018541af946SStephan Mueller out: 10191471f09fSHerbert Xu memset(drbg->scratchpad, 0, drbg_blocklen(drbg)); 1020541af946SStephan Mueller return len; 1021541af946SStephan Mueller } 1022541af946SStephan Mueller 1023541af946SStephan Mueller /* 1024541af946SStephan Mueller * scratchpad usage: as update and generate are used isolated, both 1025541af946SStephan Mueller * can use the scratchpad 1026541af946SStephan Mueller */ 1027e4bc02acSJulia Lawall static const struct drbg_state_ops drbg_hash_ops = { 1028541af946SStephan Mueller .update = drbg_hash_update, 1029541af946SStephan Mueller .generate = drbg_hash_generate, 1030541af946SStephan Mueller .crypto_init = drbg_init_hash_kernel, 1031541af946SStephan Mueller .crypto_fini = drbg_fini_hash_kernel, 1032541af946SStephan Mueller }; 1033541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_HASH */ 1034541af946SStephan Mueller 1035541af946SStephan Mueller /****************************************************************** 1036541af946SStephan Mueller * Functions common for DRBG implementations 1037541af946SStephan Mueller ******************************************************************/ 1038541af946SStephan Mueller 10393d6a5f75SStephan Mueller static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed, 10402bcd2544SNicolai Stange int reseed, enum drbg_seed_state new_seed_state) 10413d6a5f75SStephan Mueller { 10423d6a5f75SStephan Mueller int ret = drbg->d_ops->update(drbg, seed, reseed); 10433d6a5f75SStephan Mueller 10443d6a5f75SStephan Mueller if (ret) 10453d6a5f75SStephan Mueller return ret; 10463d6a5f75SStephan Mueller 10472bcd2544SNicolai Stange drbg->seeded = new_seed_state; 10488ea5ee00SNicolai Stange drbg->last_seed_time = jiffies; 10493d6a5f75SStephan Mueller /* 10.1.1.2 / 10.1.1.3 step 5 */ 10503d6a5f75SStephan Mueller drbg->reseed_ctr = 1; 10513d6a5f75SStephan Mueller 1052262d83a4SNicolai Stange switch (drbg->seeded) { 1053262d83a4SNicolai Stange case DRBG_SEED_STATE_UNSEEDED: 1054262d83a4SNicolai Stange /* Impossible, but handle it to silence compiler warnings. */ 1055262d83a4SNicolai Stange fallthrough; 1056262d83a4SNicolai Stange case DRBG_SEED_STATE_PARTIAL: 1057262d83a4SNicolai Stange /* 1058262d83a4SNicolai Stange * Require frequent reseeds until the seed source is 1059262d83a4SNicolai Stange * fully initialized. 1060262d83a4SNicolai Stange */ 1061262d83a4SNicolai Stange drbg->reseed_threshold = 50; 1062262d83a4SNicolai Stange break; 1063262d83a4SNicolai Stange 1064262d83a4SNicolai Stange case DRBG_SEED_STATE_FULL: 1065262d83a4SNicolai Stange /* 1066262d83a4SNicolai Stange * Seed source has become fully initialized, frequent 1067262d83a4SNicolai Stange * reseeds no longer required. 1068262d83a4SNicolai Stange */ 1069262d83a4SNicolai Stange drbg->reseed_threshold = drbg_max_requests(drbg); 1070262d83a4SNicolai Stange break; 1071262d83a4SNicolai Stange } 1072262d83a4SNicolai Stange 10733d6a5f75SStephan Mueller return ret; 10743d6a5f75SStephan Mueller } 10753d6a5f75SStephan Mueller 1076db07cd26SStephan Mueller static inline int drbg_get_random_bytes(struct drbg_state *drbg, 1077db07cd26SStephan Mueller unsigned char *entropy, 1078db07cd26SStephan Mueller unsigned int entropylen) 1079db07cd26SStephan Mueller { 1080db07cd26SStephan Mueller int ret; 1081db07cd26SStephan Mueller 1082db07cd26SStephan Mueller do { 1083db07cd26SStephan Mueller get_random_bytes(entropy, entropylen); 1084db07cd26SStephan Mueller ret = drbg_fips_continuous_test(drbg, entropy); 1085db07cd26SStephan Mueller if (ret && ret != -EAGAIN) 1086db07cd26SStephan Mueller return ret; 1087db07cd26SStephan Mueller } while (ret); 1088db07cd26SStephan Mueller 1089db07cd26SStephan Mueller return 0; 1090db07cd26SStephan Mueller } 1091db07cd26SStephan Mueller 1092074bcd40SNicolai Stange static int drbg_seed_from_random(struct drbg_state *drbg) 10934c787990SStephan Mueller { 10944c787990SStephan Mueller struct drbg_string data; 10954c787990SStephan Mueller LIST_HEAD(seedlist); 109657225e67SStephan Mueller unsigned int entropylen = drbg_sec_strength(drbg->core->flags); 109757225e67SStephan Mueller unsigned char entropy[32]; 1098db07cd26SStephan Mueller int ret; 10994c787990SStephan Mueller 110057225e67SStephan Mueller BUG_ON(!entropylen); 110157225e67SStephan Mueller BUG_ON(entropylen > sizeof(entropy)); 11024c787990SStephan Mueller 110357225e67SStephan Mueller drbg_string_fill(&data, entropy, entropylen); 11044c787990SStephan Mueller list_add_tail(&data.list, &seedlist); 110557225e67SStephan Mueller 1106db07cd26SStephan Mueller ret = drbg_get_random_bytes(drbg, entropy, entropylen); 1107db07cd26SStephan Mueller if (ret) 1108074bcd40SNicolai Stange goto out; 1109db07cd26SStephan Mueller 1110074bcd40SNicolai Stange ret = __drbg_seed(drbg, &seedlist, true, DRBG_SEED_STATE_FULL); 111157225e67SStephan Mueller 1112074bcd40SNicolai Stange out: 111357225e67SStephan Mueller memzero_explicit(entropy, entropylen); 1114074bcd40SNicolai Stange return ret; 11154c787990SStephan Mueller } 11164c787990SStephan Mueller 11178ea5ee00SNicolai Stange static bool drbg_nopr_reseed_interval_elapsed(struct drbg_state *drbg) 11188ea5ee00SNicolai Stange { 11198ea5ee00SNicolai Stange unsigned long next_reseed; 11208ea5ee00SNicolai Stange 11218ea5ee00SNicolai Stange /* Don't ever reseed from get_random_bytes() in test mode. */ 11228ea5ee00SNicolai Stange if (list_empty(&drbg->test_data.list)) 11238ea5ee00SNicolai Stange return false; 11248ea5ee00SNicolai Stange 11258ea5ee00SNicolai Stange /* 11268ea5ee00SNicolai Stange * Obtain fresh entropy for the nopr DRBGs after 300s have 11278ea5ee00SNicolai Stange * elapsed in order to still achieve sort of partial 11288ea5ee00SNicolai Stange * prediction resistance over the time domain at least. Note 11298ea5ee00SNicolai Stange * that the period of 300s has been chosen to match the 11308ea5ee00SNicolai Stange * CRNG_RESEED_INTERVAL of the get_random_bytes()' chacha 11318ea5ee00SNicolai Stange * rngs. 11328ea5ee00SNicolai Stange */ 11338ea5ee00SNicolai Stange next_reseed = drbg->last_seed_time + 300 * HZ; 11348ea5ee00SNicolai Stange return time_after(jiffies, next_reseed); 11358ea5ee00SNicolai Stange } 11368ea5ee00SNicolai Stange 1137541af946SStephan Mueller /* 1138541af946SStephan Mueller * Seeding or reseeding of the DRBG 1139541af946SStephan Mueller * 1140541af946SStephan Mueller * @drbg: DRBG state struct 1141541af946SStephan Mueller * @pers: personalization / additional information buffer 1142541af946SStephan Mueller * @reseed: 0 for initial seed process, 1 for reseeding 1143541af946SStephan Mueller * 1144541af946SStephan Mueller * return: 1145541af946SStephan Mueller * 0 on success 1146541af946SStephan Mueller * error value otherwise 1147541af946SStephan Mueller */ 1148541af946SStephan Mueller static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, 1149541af946SStephan Mueller bool reseed) 1150541af946SStephan Mueller { 115157225e67SStephan Mueller int ret; 115257225e67SStephan Mueller unsigned char entropy[((32 + 16) * 2)]; 115357225e67SStephan Mueller unsigned int entropylen = drbg_sec_strength(drbg->core->flags); 1154541af946SStephan Mueller struct drbg_string data1; 11558c987166SStephan Mueller LIST_HEAD(seedlist); 11562bcd2544SNicolai Stange enum drbg_seed_state new_seed_state = DRBG_SEED_STATE_FULL; 1157541af946SStephan Mueller 1158541af946SStephan Mueller /* 9.1 / 9.2 / 9.3.1 step 3 */ 1159541af946SStephan Mueller if (pers && pers->len > (drbg_max_addtl(drbg))) { 1160a9089571SStephan Mueller pr_devel("DRBG: personalization string too long %zu\n", 1161541af946SStephan Mueller pers->len); 1162541af946SStephan Mueller return -EINVAL; 1163541af946SStephan Mueller } 1164541af946SStephan Mueller 11658fded592SHerbert Xu if (list_empty(&drbg->test_data.list)) { 11668fded592SHerbert Xu drbg_string_fill(&data1, drbg->test_data.buf, 11678fded592SHerbert Xu drbg->test_data.len); 1168541af946SStephan Mueller pr_devel("DRBG: using test entropy\n"); 1169541af946SStephan Mueller } else { 117057225e67SStephan Mueller /* 117157225e67SStephan Mueller * Gather entropy equal to the security strength of the DRBG. 117257225e67SStephan Mueller * With a derivation function, a nonce is required in addition 117357225e67SStephan Mueller * to the entropy. A nonce must be at least 1/2 of the security 117457225e67SStephan Mueller * strength of the DRBG in size. Thus, entropy + nonce is 3/2 117557225e67SStephan Mueller * of the strength. The consideration of a nonce is only 117657225e67SStephan Mueller * applicable during initial seeding. 117757225e67SStephan Mueller */ 117857225e67SStephan Mueller BUG_ON(!entropylen); 117957225e67SStephan Mueller if (!reseed) 118057225e67SStephan Mueller entropylen = ((entropylen + 1) / 2) * 3; 118157225e67SStephan Mueller BUG_ON((entropylen * 2) > sizeof(entropy)); 1182b8ec5ba4SStephan Mueller 118357225e67SStephan Mueller /* Get seed from in-kernel /dev/urandom */ 11842bcd2544SNicolai Stange if (!rng_is_initialized()) 11852bcd2544SNicolai Stange new_seed_state = DRBG_SEED_STATE_PARTIAL; 11862bcd2544SNicolai Stange 1187db07cd26SStephan Mueller ret = drbg_get_random_bytes(drbg, entropy, entropylen); 1188db07cd26SStephan Mueller if (ret) 1189db07cd26SStephan Mueller goto out; 119057225e67SStephan Mueller 119157225e67SStephan Mueller if (!drbg->jent) { 119257225e67SStephan Mueller drbg_string_fill(&data1, entropy, entropylen); 119357225e67SStephan Mueller pr_devel("DRBG: (re)seeding with %u bytes of entropy\n", 119457225e67SStephan Mueller entropylen); 1195b8ec5ba4SStephan Mueller } else { 1196*8f797728SNicolai Stange /* 1197*8f797728SNicolai Stange * Get seed from Jitter RNG, failures are 1198*8f797728SNicolai Stange * fatal only in FIPS mode. 1199*8f797728SNicolai Stange */ 120057225e67SStephan Mueller ret = crypto_rng_get_bytes(drbg->jent, 120157225e67SStephan Mueller entropy + entropylen, 120257225e67SStephan Mueller entropylen); 1203*8f797728SNicolai Stange if (fips_enabled && ret) { 120457225e67SStephan Mueller pr_devel("DRBG: jent failed with %d\n", ret); 120597f2650eSStephan Müller 120697f2650eSStephan Müller /* 120797f2650eSStephan Müller * Do not treat the transient failure of the 120897f2650eSStephan Müller * Jitter RNG as an error that needs to be 120997f2650eSStephan Müller * reported. The combined number of the 121097f2650eSStephan Müller * maximum reseed threshold times the maximum 121197f2650eSStephan Müller * number of Jitter RNG transient errors is 121297f2650eSStephan Müller * less than the reseed threshold required by 121397f2650eSStephan Müller * SP800-90A allowing us to treat the 121497f2650eSStephan Müller * transient errors as such. 121597f2650eSStephan Müller * 121697f2650eSStephan Müller * However, we mandate that at least the first 121797f2650eSStephan Müller * seeding operation must succeed with the 121897f2650eSStephan Müller * Jitter RNG. 121997f2650eSStephan Müller */ 122097f2650eSStephan Müller if (!reseed || ret != -EAGAIN) 1221db07cd26SStephan Mueller goto out; 122257225e67SStephan Mueller } 122357225e67SStephan Mueller 122457225e67SStephan Mueller drbg_string_fill(&data1, entropy, entropylen * 2); 122557225e67SStephan Mueller pr_devel("DRBG: (re)seeding with %u bytes of entropy\n", 122657225e67SStephan Mueller entropylen * 2); 1227b8ec5ba4SStephan Mueller } 1228541af946SStephan Mueller } 12298c987166SStephan Mueller list_add_tail(&data1.list, &seedlist); 1230541af946SStephan Mueller 1231541af946SStephan Mueller /* 1232541af946SStephan Mueller * concatenation of entropy with personalization str / addtl input) 1233541af946SStephan Mueller * the variable pers is directly handed in by the caller, so check its 1234541af946SStephan Mueller * contents whether it is appropriate 1235541af946SStephan Mueller */ 12368c987166SStephan Mueller if (pers && pers->buf && 0 < pers->len) { 12378c987166SStephan Mueller list_add_tail(&pers->list, &seedlist); 1238541af946SStephan Mueller pr_devel("DRBG: using personalization string\n"); 1239541af946SStephan Mueller } 1240541af946SStephan Mueller 1241e6c0244aSStephan Mueller if (!reseed) { 1242e6c0244aSStephan Mueller memset(drbg->V, 0, drbg_statelen(drbg)); 1243e6c0244aSStephan Mueller memset(drbg->C, 0, drbg_statelen(drbg)); 1244e6c0244aSStephan Mueller } 1245e6c0244aSStephan Mueller 12462bcd2544SNicolai Stange ret = __drbg_seed(drbg, &seedlist, reseed, new_seed_state); 12473d6a5f75SStephan Mueller 1248db07cd26SStephan Mueller out: 124957225e67SStephan Mueller memzero_explicit(entropy, entropylen * 2); 1250541af946SStephan Mueller 1251541af946SStephan Mueller return ret; 1252541af946SStephan Mueller } 1253541af946SStephan Mueller 1254541af946SStephan Mueller /* Free all substructures in a DRBG state without the DRBG state structure */ 1255541af946SStephan Mueller static inline void drbg_dealloc_state(struct drbg_state *drbg) 1256541af946SStephan Mueller { 1257541af946SStephan Mueller if (!drbg) 1258541af946SStephan Mueller return; 1259453431a5SWaiman Long kfree_sensitive(drbg->Vbuf); 1260eea0d3eaSStephan Mueller drbg->Vbuf = NULL; 1261bd6227a1SStephan Mueller drbg->V = NULL; 1262453431a5SWaiman Long kfree_sensitive(drbg->Cbuf); 1263eea0d3eaSStephan Mueller drbg->Cbuf = NULL; 1264bd6227a1SStephan Mueller drbg->C = NULL; 1265453431a5SWaiman Long kfree_sensitive(drbg->scratchpadbuf); 12663cfc3b97SStephan Mueller drbg->scratchpadbuf = NULL; 1267541af946SStephan Mueller drbg->reseed_ctr = 0; 12682a57e424SHerbert Xu drbg->d_ops = NULL; 12692a57e424SHerbert Xu drbg->core = NULL; 1270db07cd26SStephan Mueller if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { 1271453431a5SWaiman Long kfree_sensitive(drbg->prev); 1272db07cd26SStephan Mueller drbg->prev = NULL; 1273db07cd26SStephan Mueller drbg->fips_primed = false; 1274db07cd26SStephan Mueller } 1275541af946SStephan Mueller } 1276541af946SStephan Mueller 1277541af946SStephan Mueller /* 1278541af946SStephan Mueller * Allocate all sub-structures for a DRBG state. 1279541af946SStephan Mueller * The DRBG state structure must already be allocated. 1280541af946SStephan Mueller */ 1281541af946SStephan Mueller static inline int drbg_alloc_state(struct drbg_state *drbg) 1282541af946SStephan Mueller { 1283541af946SStephan Mueller int ret = -ENOMEM; 1284541af946SStephan Mueller unsigned int sb_size = 0; 1285541af946SStephan Mueller 12862a57e424SHerbert Xu switch (drbg->core->flags & DRBG_TYPE_MASK) { 12872a57e424SHerbert Xu #ifdef CONFIG_CRYPTO_DRBG_HMAC 12882a57e424SHerbert Xu case DRBG_HMAC: 12892a57e424SHerbert Xu drbg->d_ops = &drbg_hmac_ops; 12902a57e424SHerbert Xu break; 12912a57e424SHerbert Xu #endif /* CONFIG_CRYPTO_DRBG_HMAC */ 12922a57e424SHerbert Xu #ifdef CONFIG_CRYPTO_DRBG_HASH 12932a57e424SHerbert Xu case DRBG_HASH: 12942a57e424SHerbert Xu drbg->d_ops = &drbg_hash_ops; 12952a57e424SHerbert Xu break; 12962a57e424SHerbert Xu #endif /* CONFIG_CRYPTO_DRBG_HASH */ 12972a57e424SHerbert Xu #ifdef CONFIG_CRYPTO_DRBG_CTR 12982a57e424SHerbert Xu case DRBG_CTR: 12992a57e424SHerbert Xu drbg->d_ops = &drbg_ctr_ops; 13002a57e424SHerbert Xu break; 13012a57e424SHerbert Xu #endif /* CONFIG_CRYPTO_DRBG_CTR */ 13022a57e424SHerbert Xu default: 13032a57e424SHerbert Xu ret = -EOPNOTSUPP; 13042a57e424SHerbert Xu goto err; 13052a57e424SHerbert Xu } 13062a57e424SHerbert Xu 13073cfc3b97SStephan Mueller ret = drbg->d_ops->crypto_init(drbg); 13083cfc3b97SStephan Mueller if (ret < 0) 1309541af946SStephan Mueller goto err; 13103cfc3b97SStephan Mueller 13113cfc3b97SStephan Mueller drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL); 13121a45d7e3SWei Yongjun if (!drbg->Vbuf) { 13131a45d7e3SWei Yongjun ret = -ENOMEM; 13143cfc3b97SStephan Mueller goto fini; 13151a45d7e3SWei Yongjun } 13163cfc3b97SStephan Mueller drbg->V = PTR_ALIGN(drbg->Vbuf, ret + 1); 13173cfc3b97SStephan Mueller drbg->Cbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL); 13181a45d7e3SWei Yongjun if (!drbg->Cbuf) { 13191a45d7e3SWei Yongjun ret = -ENOMEM; 13203cfc3b97SStephan Mueller goto fini; 13211a45d7e3SWei Yongjun } 13223cfc3b97SStephan Mueller drbg->C = PTR_ALIGN(drbg->Cbuf, ret + 1); 1323541af946SStephan Mueller /* scratchpad is only generated for CTR and Hash */ 1324541af946SStephan Mueller if (drbg->core->flags & DRBG_HMAC) 1325541af946SStephan Mueller sb_size = 0; 1326541af946SStephan Mueller else if (drbg->core->flags & DRBG_CTR) 1327541af946SStephan Mueller sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */ 1328541af946SStephan Mueller drbg_statelen(drbg) + /* df_data */ 1329541af946SStephan Mueller drbg_blocklen(drbg) + /* pad */ 1330541af946SStephan Mueller drbg_blocklen(drbg) + /* iv */ 13318fecaad7SStephan Mueller drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */ 1332541af946SStephan Mueller else 1333541af946SStephan Mueller sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg); 1334541af946SStephan Mueller 1335541af946SStephan Mueller if (0 < sb_size) { 13363cfc3b97SStephan Mueller drbg->scratchpadbuf = kzalloc(sb_size + ret, GFP_KERNEL); 13371a45d7e3SWei Yongjun if (!drbg->scratchpadbuf) { 13381a45d7e3SWei Yongjun ret = -ENOMEM; 13393cfc3b97SStephan Mueller goto fini; 13401a45d7e3SWei Yongjun } 13413cfc3b97SStephan Mueller drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1); 1342541af946SStephan Mueller } 13433d6a5f75SStephan Mueller 1344db07cd26SStephan Mueller if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) { 1345db07cd26SStephan Mueller drbg->prev = kzalloc(drbg_sec_strength(drbg->core->flags), 1346db07cd26SStephan Mueller GFP_KERNEL); 1347e0664ebcSWei Yongjun if (!drbg->prev) { 1348e0664ebcSWei Yongjun ret = -ENOMEM; 1349db07cd26SStephan Mueller goto fini; 1350e0664ebcSWei Yongjun } 1351db07cd26SStephan Mueller drbg->fips_primed = false; 1352db07cd26SStephan Mueller } 1353db07cd26SStephan Mueller 1354541af946SStephan Mueller return 0; 1355541af946SStephan Mueller 13563cfc3b97SStephan Mueller fini: 13573cfc3b97SStephan Mueller drbg->d_ops->crypto_fini(drbg); 1358541af946SStephan Mueller err: 1359541af946SStephan Mueller drbg_dealloc_state(drbg); 1360541af946SStephan Mueller return ret; 1361541af946SStephan Mueller } 1362541af946SStephan Mueller 1363541af946SStephan Mueller /************************************************************************* 1364541af946SStephan Mueller * DRBG interface functions 1365541af946SStephan Mueller *************************************************************************/ 1366541af946SStephan Mueller 1367541af946SStephan Mueller /* 1368541af946SStephan Mueller * DRBG generate function as required by SP800-90A - this function 1369541af946SStephan Mueller * generates random numbers 1370541af946SStephan Mueller * 1371541af946SStephan Mueller * @drbg DRBG state handle 1372541af946SStephan Mueller * @buf Buffer where to store the random numbers -- the buffer must already 1373541af946SStephan Mueller * be pre-allocated by caller 1374541af946SStephan Mueller * @buflen Length of output buffer - this value defines the number of random 1375541af946SStephan Mueller * bytes pulled from DRBG 1376541af946SStephan Mueller * @addtl Additional input that is mixed into state, may be NULL -- note 1377541af946SStephan Mueller * the entropy is pulled by the DRBG internally unconditionally 1378541af946SStephan Mueller * as defined in SP800-90A. The additional input is mixed into 1379541af946SStephan Mueller * the state in addition to the pulled entropy. 1380541af946SStephan Mueller * 1381cde001e4SStephan Mueller * return: 0 when all bytes are generated; < 0 in case of an error 1382541af946SStephan Mueller */ 1383541af946SStephan Mueller static int drbg_generate(struct drbg_state *drbg, 1384541af946SStephan Mueller unsigned char *buf, unsigned int buflen, 1385541af946SStephan Mueller struct drbg_string *addtl) 1386541af946SStephan Mueller { 1387541af946SStephan Mueller int len = 0; 138827e4de2bSStephan Mueller LIST_HEAD(addtllist); 1389541af946SStephan Mueller 13902a57e424SHerbert Xu if (!drbg->core) { 13912a57e424SHerbert Xu pr_devel("DRBG: not yet seeded\n"); 13922a57e424SHerbert Xu return -EINVAL; 13932a57e424SHerbert Xu } 1394541af946SStephan Mueller if (0 == buflen || !buf) { 1395541af946SStephan Mueller pr_devel("DRBG: no output buffer provided\n"); 1396541af946SStephan Mueller return -EINVAL; 1397541af946SStephan Mueller } 1398541af946SStephan Mueller if (addtl && NULL == addtl->buf && 0 < addtl->len) { 1399541af946SStephan Mueller pr_devel("DRBG: wrong format of additional information\n"); 1400541af946SStephan Mueller return -EINVAL; 1401541af946SStephan Mueller } 1402541af946SStephan Mueller 1403541af946SStephan Mueller /* 9.3.1 step 2 */ 1404541af946SStephan Mueller len = -EINVAL; 140576899a41SStephan Mueller if (buflen > (drbg_max_request_bytes(drbg))) { 1406541af946SStephan Mueller pr_devel("DRBG: requested random numbers too large %u\n", 1407541af946SStephan Mueller buflen); 1408541af946SStephan Mueller goto err; 1409541af946SStephan Mueller } 1410541af946SStephan Mueller 1411541af946SStephan Mueller /* 9.3.1 step 3 is implicit with the chosen DRBG */ 1412541af946SStephan Mueller 1413541af946SStephan Mueller /* 9.3.1 step 4 */ 141476899a41SStephan Mueller if (addtl && addtl->len > (drbg_max_addtl(drbg))) { 1415541af946SStephan Mueller pr_devel("DRBG: additional information string too long %zu\n", 1416541af946SStephan Mueller addtl->len); 1417541af946SStephan Mueller goto err; 1418541af946SStephan Mueller } 1419541af946SStephan Mueller /* 9.3.1 step 5 is implicit with the chosen DRBG */ 1420541af946SStephan Mueller 1421541af946SStephan Mueller /* 1422541af946SStephan Mueller * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented 1423541af946SStephan Mueller * here. The spec is a bit convoluted here, we make it simpler. 1424541af946SStephan Mueller */ 142542ea507fSStephan Mueller if (drbg->reseed_threshold < drbg->reseed_ctr) 1426ce8ce31bSNicolai Stange drbg->seeded = DRBG_SEED_STATE_UNSEEDED; 1427541af946SStephan Mueller 1428ce8ce31bSNicolai Stange if (drbg->pr || drbg->seeded == DRBG_SEED_STATE_UNSEEDED) { 1429541af946SStephan Mueller pr_devel("DRBG: reseeding before generation (prediction " 1430541af946SStephan Mueller "resistance: %s, state %s)\n", 1431541af946SStephan Mueller drbg->pr ? "true" : "false", 1432ce8ce31bSNicolai Stange (drbg->seeded == DRBG_SEED_STATE_FULL ? 1433ce8ce31bSNicolai Stange "seeded" : "unseeded")); 1434541af946SStephan Mueller /* 9.3.1 steps 7.1 through 7.3 */ 143576899a41SStephan Mueller len = drbg_seed(drbg, addtl, true); 1436541af946SStephan Mueller if (len) 1437541af946SStephan Mueller goto err; 1438541af946SStephan Mueller /* 9.3.1 step 7.4 */ 1439541af946SStephan Mueller addtl = NULL; 1440074bcd40SNicolai Stange } else if (rng_is_initialized() && 14418ea5ee00SNicolai Stange (drbg->seeded == DRBG_SEED_STATE_PARTIAL || 14428ea5ee00SNicolai Stange drbg_nopr_reseed_interval_elapsed(drbg))) { 1443074bcd40SNicolai Stange len = drbg_seed_from_random(drbg); 1444074bcd40SNicolai Stange if (len) 1445074bcd40SNicolai Stange goto err; 1446541af946SStephan Mueller } 144727e4de2bSStephan Mueller 144827e4de2bSStephan Mueller if (addtl && 0 < addtl->len) 144927e4de2bSStephan Mueller list_add_tail(&addtl->list, &addtllist); 1450541af946SStephan Mueller /* 9.3.1 step 8 and 10 */ 145176899a41SStephan Mueller len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist); 1452541af946SStephan Mueller 1453541af946SStephan Mueller /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */ 145476899a41SStephan Mueller drbg->reseed_ctr++; 1455541af946SStephan Mueller if (0 >= len) 1456541af946SStephan Mueller goto err; 1457541af946SStephan Mueller 1458541af946SStephan Mueller /* 1459541af946SStephan Mueller * Section 11.3.3 requires to re-perform self tests after some 1460541af946SStephan Mueller * generated random numbers. The chosen value after which self 1461541af946SStephan Mueller * test is performed is arbitrary, but it should be reasonable. 1462541af946SStephan Mueller * However, we do not perform the self tests because of the following 1463541af946SStephan Mueller * reasons: it is mathematically impossible that the initial self tests 1464541af946SStephan Mueller * were successfully and the following are not. If the initial would 1465541af946SStephan Mueller * pass and the following would not, the kernel integrity is violated. 1466541af946SStephan Mueller * In this case, the entire kernel operation is questionable and it 1467541af946SStephan Mueller * is unlikely that the integrity violation only affects the 1468541af946SStephan Mueller * correct operation of the DRBG. 1469541af946SStephan Mueller * 1470541af946SStephan Mueller * Albeit the following code is commented out, it is provided in 1471541af946SStephan Mueller * case somebody has a need to implement the test of 11.3.3. 1472541af946SStephan Mueller */ 1473541af946SStephan Mueller #if 0 147476899a41SStephan Mueller if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) { 1475541af946SStephan Mueller int err = 0; 1476541af946SStephan Mueller pr_devel("DRBG: start to perform self test\n"); 1477541af946SStephan Mueller if (drbg->core->flags & DRBG_HMAC) 1478541af946SStephan Mueller err = alg_test("drbg_pr_hmac_sha256", 1479541af946SStephan Mueller "drbg_pr_hmac_sha256", 0, 0); 1480541af946SStephan Mueller else if (drbg->core->flags & DRBG_CTR) 1481541af946SStephan Mueller err = alg_test("drbg_pr_ctr_aes128", 1482541af946SStephan Mueller "drbg_pr_ctr_aes128", 0, 0); 1483541af946SStephan Mueller else 1484541af946SStephan Mueller err = alg_test("drbg_pr_sha256", 1485541af946SStephan Mueller "drbg_pr_sha256", 0, 0); 1486541af946SStephan Mueller if (err) { 1487541af946SStephan Mueller pr_err("DRBG: periodical self test failed\n"); 1488541af946SStephan Mueller /* 1489541af946SStephan Mueller * uninstantiate implies that from now on, only errors 1490541af946SStephan Mueller * are returned when reusing this DRBG cipher handle 1491541af946SStephan Mueller */ 1492541af946SStephan Mueller drbg_uninstantiate(drbg); 1493541af946SStephan Mueller return 0; 1494541af946SStephan Mueller } else { 1495541af946SStephan Mueller pr_devel("DRBG: self test successful\n"); 1496541af946SStephan Mueller } 1497541af946SStephan Mueller } 1498541af946SStephan Mueller #endif 1499541af946SStephan Mueller 1500cde001e4SStephan Mueller /* 1501cde001e4SStephan Mueller * All operations were successful, return 0 as mandated by 1502cde001e4SStephan Mueller * the kernel crypto API interface. 1503cde001e4SStephan Mueller */ 1504cde001e4SStephan Mueller len = 0; 1505541af946SStephan Mueller err: 1506541af946SStephan Mueller return len; 1507541af946SStephan Mueller } 1508541af946SStephan Mueller 1509541af946SStephan Mueller /* 1510541af946SStephan Mueller * Wrapper around drbg_generate which can pull arbitrary long strings 1511541af946SStephan Mueller * from the DRBG without hitting the maximum request limitation. 1512541af946SStephan Mueller * 1513541af946SStephan Mueller * Parameters: see drbg_generate 1514541af946SStephan Mueller * Return codes: see drbg_generate -- if one drbg_generate request fails, 1515541af946SStephan Mueller * the entire drbg_generate_long request fails 1516541af946SStephan Mueller */ 1517541af946SStephan Mueller static int drbg_generate_long(struct drbg_state *drbg, 1518541af946SStephan Mueller unsigned char *buf, unsigned int buflen, 1519541af946SStephan Mueller struct drbg_string *addtl) 1520541af946SStephan Mueller { 1521082eb10bSStephan Mueller unsigned int len = 0; 1522541af946SStephan Mueller unsigned int slice = 0; 1523541af946SStephan Mueller do { 1524082eb10bSStephan Mueller int err = 0; 1525541af946SStephan Mueller unsigned int chunk = 0; 1526541af946SStephan Mueller slice = ((buflen - len) / drbg_max_request_bytes(drbg)); 1527541af946SStephan Mueller chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len); 152876899a41SStephan Mueller mutex_lock(&drbg->drbg_mutex); 1529082eb10bSStephan Mueller err = drbg_generate(drbg, buf + len, chunk, addtl); 153076899a41SStephan Mueller mutex_unlock(&drbg->drbg_mutex); 1531082eb10bSStephan Mueller if (0 > err) 1532082eb10bSStephan Mueller return err; 1533082eb10bSStephan Mueller len += chunk; 1534ce5481d0SStephan Mueller } while (slice > 0 && (len < buflen)); 1535082eb10bSStephan Mueller return 0; 1536541af946SStephan Mueller } 1537541af946SStephan Mueller 153857225e67SStephan Mueller static int drbg_prepare_hrng(struct drbg_state *drbg) 153957225e67SStephan Mueller { 154057225e67SStephan Mueller /* We do not need an HRNG in test mode. */ 154157225e67SStephan Mueller if (list_empty(&drbg->test_data.list)) 154257225e67SStephan Mueller return 0; 154357225e67SStephan Mueller 154497f2650eSStephan Müller drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0); 1545559edd47SNicolai Stange if (IS_ERR(drbg->jent)) { 1546559edd47SNicolai Stange const int err = PTR_ERR(drbg->jent); 1547559edd47SNicolai Stange 1548559edd47SNicolai Stange drbg->jent = NULL; 1549559edd47SNicolai Stange if (fips_enabled || err != -ENOENT) 1550559edd47SNicolai Stange return err; 1551559edd47SNicolai Stange pr_info("DRBG: Continuing without Jitter RNG\n"); 1552559edd47SNicolai Stange } 155397f2650eSStephan Müller 1554074bcd40SNicolai Stange return 0; 155557225e67SStephan Mueller } 155657225e67SStephan Mueller 1557541af946SStephan Mueller /* 1558541af946SStephan Mueller * DRBG instantiation function as required by SP800-90A - this function 1559541af946SStephan Mueller * sets up the DRBG handle, performs the initial seeding and all sanity 1560541af946SStephan Mueller * checks required by SP800-90A 1561541af946SStephan Mueller * 1562541af946SStephan Mueller * @drbg memory of state -- if NULL, new memory is allocated 1563541af946SStephan Mueller * @pers Personalization string that is mixed into state, may be NULL -- note 1564541af946SStephan Mueller * the entropy is pulled by the DRBG internally unconditionally 1565541af946SStephan Mueller * as defined in SP800-90A. The additional input is mixed into 1566541af946SStephan Mueller * the state in addition to the pulled entropy. 1567541af946SStephan Mueller * @coreref reference to core 1568541af946SStephan Mueller * @pr prediction resistance enabled 1569541af946SStephan Mueller * 1570541af946SStephan Mueller * return 1571541af946SStephan Mueller * 0 on success 1572541af946SStephan Mueller * error value otherwise 1573541af946SStephan Mueller */ 1574541af946SStephan Mueller static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, 1575541af946SStephan Mueller int coreref, bool pr) 1576541af946SStephan Mueller { 15772a57e424SHerbert Xu int ret; 15782a57e424SHerbert Xu bool reseed = true; 1579541af946SStephan Mueller 1580541af946SStephan Mueller pr_devel("DRBG: Initializing DRBG core %d with prediction resistance " 1581541af946SStephan Mueller "%s\n", coreref, pr ? "enabled" : "disabled"); 158276899a41SStephan Mueller mutex_lock(&drbg->drbg_mutex); 1583541af946SStephan Mueller 1584541af946SStephan Mueller /* 9.1 step 1 is implicit with the selected DRBG type */ 1585541af946SStephan Mueller 1586541af946SStephan Mueller /* 1587541af946SStephan Mueller * 9.1 step 2 is implicit as caller can select prediction resistance 1588541af946SStephan Mueller * and the flag is copied into drbg->flags -- 1589541af946SStephan Mueller * all DRBG types support prediction resistance 1590541af946SStephan Mueller */ 1591541af946SStephan Mueller 1592541af946SStephan Mueller /* 9.1 step 4 is implicit in drbg_sec_strength */ 1593541af946SStephan Mueller 15942a57e424SHerbert Xu if (!drbg->core) { 15952a57e424SHerbert Xu drbg->core = &drbg_cores[coreref]; 15962a57e424SHerbert Xu drbg->pr = pr; 1597ce8ce31bSNicolai Stange drbg->seeded = DRBG_SEED_STATE_UNSEEDED; 15988ea5ee00SNicolai Stange drbg->last_seed_time = 0; 159942ea507fSStephan Mueller drbg->reseed_threshold = drbg_max_requests(drbg); 16002a57e424SHerbert Xu 1601541af946SStephan Mueller ret = drbg_alloc_state(drbg); 1602541af946SStephan Mueller if (ret) 160376899a41SStephan Mueller goto unlock; 1604541af946SStephan Mueller 160557225e67SStephan Mueller ret = drbg_prepare_hrng(drbg); 160657225e67SStephan Mueller if (ret) 160757225e67SStephan Mueller goto free_everything; 160857225e67SStephan Mueller 16092a57e424SHerbert Xu reseed = false; 16102a57e424SHerbert Xu } 16112a57e424SHerbert Xu 16122a57e424SHerbert Xu ret = drbg_seed(drbg, pers, reseed); 16132a57e424SHerbert Xu 161457225e67SStephan Mueller if (ret && !reseed) 161557225e67SStephan Mueller goto free_everything; 1616541af946SStephan Mueller 161776899a41SStephan Mueller mutex_unlock(&drbg->drbg_mutex); 16182a57e424SHerbert Xu return ret; 1619541af946SStephan Mueller 162076899a41SStephan Mueller unlock: 162176899a41SStephan Mueller mutex_unlock(&drbg->drbg_mutex); 1622541af946SStephan Mueller return ret; 162357225e67SStephan Mueller 162457225e67SStephan Mueller free_everything: 162557225e67SStephan Mueller mutex_unlock(&drbg->drbg_mutex); 162657225e67SStephan Mueller drbg_uninstantiate(drbg); 162757225e67SStephan Mueller return ret; 1628541af946SStephan Mueller } 1629541af946SStephan Mueller 1630541af946SStephan Mueller /* 1631541af946SStephan Mueller * DRBG uninstantiate function as required by SP800-90A - this function 1632541af946SStephan Mueller * frees all buffers and the DRBG handle 1633541af946SStephan Mueller * 1634541af946SStephan Mueller * @drbg DRBG state handle 1635541af946SStephan Mueller * 1636541af946SStephan Mueller * return 1637541af946SStephan Mueller * 0 on success 1638541af946SStephan Mueller */ 1639541af946SStephan Mueller static int drbg_uninstantiate(struct drbg_state *drbg) 1640541af946SStephan Mueller { 1641819966c0SStephan Müller if (!IS_ERR_OR_NULL(drbg->jent)) 164257225e67SStephan Mueller crypto_free_rng(drbg->jent); 164357225e67SStephan Mueller drbg->jent = NULL; 164457225e67SStephan Mueller 16452a57e424SHerbert Xu if (drbg->d_ops) 1646fa3ae625SStephan Mueller drbg->d_ops->crypto_fini(drbg); 1647541af946SStephan Mueller drbg_dealloc_state(drbg); 1648541af946SStephan Mueller /* no scrubbing of test_data -- this shall survive an uninstantiate */ 1649541af946SStephan Mueller return 0; 1650541af946SStephan Mueller } 1651541af946SStephan Mueller 1652541af946SStephan Mueller /* 1653541af946SStephan Mueller * Helper function for setting the test data in the DRBG 1654541af946SStephan Mueller * 1655541af946SStephan Mueller * @drbg DRBG state handle 16568fded592SHerbert Xu * @data test data 16578fded592SHerbert Xu * @len test data length 1658541af946SStephan Mueller */ 16598fded592SHerbert Xu static void drbg_kcapi_set_entropy(struct crypto_rng *tfm, 16608fded592SHerbert Xu const u8 *data, unsigned int len) 1661541af946SStephan Mueller { 16628fded592SHerbert Xu struct drbg_state *drbg = crypto_rng_ctx(tfm); 16638fded592SHerbert Xu 16648fded592SHerbert Xu mutex_lock(&drbg->drbg_mutex); 16658fded592SHerbert Xu drbg_string_fill(&drbg->test_data, data, len); 166676899a41SStephan Mueller mutex_unlock(&drbg->drbg_mutex); 1667541af946SStephan Mueller } 1668541af946SStephan Mueller 1669541af946SStephan Mueller /*************************************************************** 1670541af946SStephan Mueller * Kernel crypto API cipher invocations requested by DRBG 1671541af946SStephan Mueller ***************************************************************/ 1672541af946SStephan Mueller 1673541af946SStephan Mueller #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC) 1674541af946SStephan Mueller struct sdesc { 1675541af946SStephan Mueller struct shash_desc shash; 1676541af946SStephan Mueller char ctx[]; 1677541af946SStephan Mueller }; 1678541af946SStephan Mueller 1679541af946SStephan Mueller static int drbg_init_hash_kernel(struct drbg_state *drbg) 1680541af946SStephan Mueller { 1681541af946SStephan Mueller struct sdesc *sdesc; 1682541af946SStephan Mueller struct crypto_shash *tfm; 1683541af946SStephan Mueller 1684541af946SStephan Mueller tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0); 1685541af946SStephan Mueller if (IS_ERR(tfm)) { 1686593dfbd9SSergey Senozhatsky pr_info("DRBG: could not allocate digest TFM handle: %s\n", 1687593dfbd9SSergey Senozhatsky drbg->core->backend_cra_name); 1688541af946SStephan Mueller return PTR_ERR(tfm); 1689541af946SStephan Mueller } 1690541af946SStephan Mueller BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm)); 1691541af946SStephan Mueller sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm), 1692541af946SStephan Mueller GFP_KERNEL); 1693541af946SStephan Mueller if (!sdesc) { 1694541af946SStephan Mueller crypto_free_shash(tfm); 1695541af946SStephan Mueller return -ENOMEM; 1696541af946SStephan Mueller } 1697541af946SStephan Mueller 1698541af946SStephan Mueller sdesc->shash.tfm = tfm; 1699541af946SStephan Mueller drbg->priv_data = sdesc; 17003cfc3b97SStephan Mueller 17013cfc3b97SStephan Mueller return crypto_shash_alignmask(tfm); 1702541af946SStephan Mueller } 1703541af946SStephan Mueller 1704541af946SStephan Mueller static int drbg_fini_hash_kernel(struct drbg_state *drbg) 1705541af946SStephan Mueller { 1706541af946SStephan Mueller struct sdesc *sdesc = (struct sdesc *)drbg->priv_data; 1707541af946SStephan Mueller if (sdesc) { 1708541af946SStephan Mueller crypto_free_shash(sdesc->shash.tfm); 1709453431a5SWaiman Long kfree_sensitive(sdesc); 1710541af946SStephan Mueller } 1711541af946SStephan Mueller drbg->priv_data = NULL; 1712541af946SStephan Mueller return 0; 1713541af946SStephan Mueller } 1714541af946SStephan Mueller 17154218ebe8SStephan Mueller static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg, 17164218ebe8SStephan Mueller const unsigned char *key) 17174218ebe8SStephan Mueller { 17184218ebe8SStephan Mueller struct sdesc *sdesc = (struct sdesc *)drbg->priv_data; 17194218ebe8SStephan Mueller 17204218ebe8SStephan Mueller crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg)); 17214218ebe8SStephan Mueller } 17224218ebe8SStephan Mueller 17234218ebe8SStephan Mueller static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval, 17244218ebe8SStephan Mueller const struct list_head *in) 1725541af946SStephan Mueller { 1726541af946SStephan Mueller struct sdesc *sdesc = (struct sdesc *)drbg->priv_data; 17278c987166SStephan Mueller struct drbg_string *input = NULL; 1728541af946SStephan Mueller 1729541af946SStephan Mueller crypto_shash_init(&sdesc->shash); 17308c987166SStephan Mueller list_for_each_entry(input, in, list) 17318c987166SStephan Mueller crypto_shash_update(&sdesc->shash, input->buf, input->len); 1732541af946SStephan Mueller return crypto_shash_final(&sdesc->shash, outval); 1733541af946SStephan Mueller } 1734541af946SStephan Mueller #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */ 1735541af946SStephan Mueller 1736541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_CTR 173735591285SStephan Mueller static int drbg_fini_sym_kernel(struct drbg_state *drbg) 173835591285SStephan Mueller { 173935591285SStephan Mueller struct crypto_cipher *tfm = 174035591285SStephan Mueller (struct crypto_cipher *)drbg->priv_data; 174135591285SStephan Mueller if (tfm) 174235591285SStephan Mueller crypto_free_cipher(tfm); 174335591285SStephan Mueller drbg->priv_data = NULL; 174435591285SStephan Mueller 174535591285SStephan Mueller if (drbg->ctr_handle) 174635591285SStephan Mueller crypto_free_skcipher(drbg->ctr_handle); 174735591285SStephan Mueller drbg->ctr_handle = NULL; 174835591285SStephan Mueller 174935591285SStephan Mueller if (drbg->ctr_req) 175088f1d316SWu Fengguang skcipher_request_free(drbg->ctr_req); 175135591285SStephan Mueller drbg->ctr_req = NULL; 175235591285SStephan Mueller 175351029812SStephan Mueller kfree(drbg->outscratchpadbuf); 175451029812SStephan Mueller drbg->outscratchpadbuf = NULL; 175551029812SStephan Mueller 175635591285SStephan Mueller return 0; 175735591285SStephan Mueller } 175835591285SStephan Mueller 1759541af946SStephan Mueller static int drbg_init_sym_kernel(struct drbg_state *drbg) 1760541af946SStephan Mueller { 176104bcbfcfSStephan Mueller struct crypto_cipher *tfm; 176235591285SStephan Mueller struct crypto_skcipher *sk_tfm; 176335591285SStephan Mueller struct skcipher_request *req; 176435591285SStephan Mueller unsigned int alignmask; 176535591285SStephan Mueller char ctr_name[CRYPTO_MAX_ALG_NAME]; 1766541af946SStephan Mueller 176704bcbfcfSStephan Mueller tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0); 1768541af946SStephan Mueller if (IS_ERR(tfm)) { 1769593dfbd9SSergey Senozhatsky pr_info("DRBG: could not allocate cipher TFM handle: %s\n", 1770593dfbd9SSergey Senozhatsky drbg->core->backend_cra_name); 1771541af946SStephan Mueller return PTR_ERR(tfm); 1772541af946SStephan Mueller } 177304bcbfcfSStephan Mueller BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm)); 1774541af946SStephan Mueller drbg->priv_data = tfm; 1775541af946SStephan Mueller 177635591285SStephan Mueller if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", 177735591285SStephan Mueller drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) { 177835591285SStephan Mueller drbg_fini_sym_kernel(drbg); 177935591285SStephan Mueller return -EINVAL; 178035591285SStephan Mueller } 178135591285SStephan Mueller sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0); 178235591285SStephan Mueller if (IS_ERR(sk_tfm)) { 178335591285SStephan Mueller pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n", 178435591285SStephan Mueller ctr_name); 178535591285SStephan Mueller drbg_fini_sym_kernel(drbg); 178635591285SStephan Mueller return PTR_ERR(sk_tfm); 178735591285SStephan Mueller } 178835591285SStephan Mueller drbg->ctr_handle = sk_tfm; 178985a2dea4SGilad Ben-Yossef crypto_init_wait(&drbg->ctr_wait); 179035591285SStephan Mueller 179135591285SStephan Mueller req = skcipher_request_alloc(sk_tfm, GFP_KERNEL); 179235591285SStephan Mueller if (!req) { 179335591285SStephan Mueller pr_info("DRBG: could not allocate request queue\n"); 179435591285SStephan Mueller drbg_fini_sym_kernel(drbg); 179501ac9458SDan Carpenter return -ENOMEM; 179635591285SStephan Mueller } 179735591285SStephan Mueller drbg->ctr_req = req; 179885a2dea4SGilad Ben-Yossef skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | 179985a2dea4SGilad Ben-Yossef CRYPTO_TFM_REQ_MAY_SLEEP, 180085a2dea4SGilad Ben-Yossef crypto_req_done, &drbg->ctr_wait); 180135591285SStephan Mueller 180235591285SStephan Mueller alignmask = crypto_skcipher_alignmask(sk_tfm); 180351029812SStephan Mueller drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask, 180451029812SStephan Mueller GFP_KERNEL); 180551029812SStephan Mueller if (!drbg->outscratchpadbuf) { 180651029812SStephan Mueller drbg_fini_sym_kernel(drbg); 180751029812SStephan Mueller return -ENOMEM; 180851029812SStephan Mueller } 180951029812SStephan Mueller drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf, 181051029812SStephan Mueller alignmask + 1); 181151029812SStephan Mueller 1812cf862cbcSStephan Mueller sg_init_table(&drbg->sg_in, 1); 181343490e80SStephan Müller sg_init_one(&drbg->sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN); 1814cf862cbcSStephan Mueller 18153cfc3b97SStephan Mueller return alignmask; 1816541af946SStephan Mueller } 1817541af946SStephan Mueller 1818ed494d4fSStephan Mueller static void drbg_kcapi_symsetkey(struct drbg_state *drbg, 1819ed494d4fSStephan Mueller const unsigned char *key) 1820541af946SStephan Mueller { 182104bcbfcfSStephan Mueller struct crypto_cipher *tfm = 182204bcbfcfSStephan Mueller (struct crypto_cipher *)drbg->priv_data; 1823541af946SStephan Mueller 182404bcbfcfSStephan Mueller crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg))); 1825ed494d4fSStephan Mueller } 1826ed494d4fSStephan Mueller 1827ed494d4fSStephan Mueller static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval, 1828ed494d4fSStephan Mueller const struct drbg_string *in) 1829ed494d4fSStephan Mueller { 1830ed494d4fSStephan Mueller struct crypto_cipher *tfm = 1831ed494d4fSStephan Mueller (struct crypto_cipher *)drbg->priv_data; 1832ed494d4fSStephan Mueller 1833541af946SStephan Mueller /* there is only component in *in */ 183404bcbfcfSStephan Mueller BUG_ON(in->len < drbg_blocklen(drbg)); 183504bcbfcfSStephan Mueller crypto_cipher_encrypt_one(tfm, outval, in->buf); 183604bcbfcfSStephan Mueller return 0; 1837541af946SStephan Mueller } 183835591285SStephan Mueller 1839a07203fbSStephan Mueller static int drbg_kcapi_sym_ctr(struct drbg_state *drbg, 1840a07203fbSStephan Mueller u8 *inbuf, u32 inlen, 1841a07203fbSStephan Mueller u8 *outbuf, u32 outlen) 184235591285SStephan Mueller { 1843cf862cbcSStephan Mueller struct scatterlist *sg_in = &drbg->sg_in, *sg_out = &drbg->sg_out; 184443490e80SStephan Müller u32 scratchpad_use = min_t(u32, outlen, DRBG_OUTSCRATCHLEN); 184551029812SStephan Mueller int ret; 184635591285SStephan Mueller 184743490e80SStephan Müller if (inbuf) { 184843490e80SStephan Müller /* Use caller-provided input buffer */ 1849cf862cbcSStephan Mueller sg_set_buf(sg_in, inbuf, inlen); 185043490e80SStephan Müller } else { 185143490e80SStephan Müller /* Use scratchpad for in-place operation */ 185243490e80SStephan Müller inlen = scratchpad_use; 185343490e80SStephan Müller memset(drbg->outscratchpad, 0, scratchpad_use); 185443490e80SStephan Müller sg_set_buf(sg_in, drbg->outscratchpad, scratchpad_use); 185543490e80SStephan Müller } 185635591285SStephan Mueller 185735591285SStephan Mueller while (outlen) { 185851029812SStephan Mueller u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN); 185935591285SStephan Mueller 186051029812SStephan Mueller /* Output buffer may not be valid for SGL, use scratchpad */ 1861cf862cbcSStephan Mueller skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out, 186235591285SStephan Mueller cryptlen, drbg->V); 186385a2dea4SGilad Ben-Yossef ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req), 186485a2dea4SGilad Ben-Yossef &drbg->ctr_wait); 186585a2dea4SGilad Ben-Yossef if (ret) 186651029812SStephan Mueller goto out; 186785a2dea4SGilad Ben-Yossef 186885a2dea4SGilad Ben-Yossef crypto_init_wait(&drbg->ctr_wait); 186935591285SStephan Mueller 187051029812SStephan Mueller memcpy(outbuf, drbg->outscratchpad, cryptlen); 187143490e80SStephan Müller memzero_explicit(drbg->outscratchpad, cryptlen); 187251029812SStephan Mueller 187335591285SStephan Mueller outlen -= cryptlen; 18748ff4c191SStephan Mueller outbuf += cryptlen; 187535591285SStephan Mueller } 187651029812SStephan Mueller ret = 0; 187735591285SStephan Mueller 187851029812SStephan Mueller out: 187951029812SStephan Mueller return ret; 188035591285SStephan Mueller } 1881541af946SStephan Mueller #endif /* CONFIG_CRYPTO_DRBG_CTR */ 1882541af946SStephan Mueller 1883541af946SStephan Mueller /*************************************************************** 1884541af946SStephan Mueller * Kernel crypto API interface to register DRBG 1885541af946SStephan Mueller ***************************************************************/ 1886541af946SStephan Mueller 1887541af946SStephan Mueller /* 1888541af946SStephan Mueller * Look up the DRBG flags by given kernel crypto API cra_name 1889541af946SStephan Mueller * The code uses the drbg_cores definition to do this 1890541af946SStephan Mueller * 1891541af946SStephan Mueller * @cra_name kernel crypto API cra_name 1892541af946SStephan Mueller * @coreref reference to integer which is filled with the pointer to 1893541af946SStephan Mueller * the applicable core 1894541af946SStephan Mueller * @pr reference for setting prediction resistance 1895541af946SStephan Mueller * 1896541af946SStephan Mueller * return: flags 1897541af946SStephan Mueller */ 1898541af946SStephan Mueller static inline void drbg_convert_tfm_core(const char *cra_driver_name, 1899541af946SStephan Mueller int *coreref, bool *pr) 1900541af946SStephan Mueller { 1901541af946SStephan Mueller int i = 0; 1902541af946SStephan Mueller size_t start = 0; 1903541af946SStephan Mueller int len = 0; 1904541af946SStephan Mueller 1905541af946SStephan Mueller *pr = true; 1906541af946SStephan Mueller /* disassemble the names */ 1907541af946SStephan Mueller if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) { 1908541af946SStephan Mueller start = 10; 1909541af946SStephan Mueller *pr = false; 1910541af946SStephan Mueller } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) { 1911541af946SStephan Mueller start = 8; 1912541af946SStephan Mueller } else { 1913541af946SStephan Mueller return; 1914541af946SStephan Mueller } 1915541af946SStephan Mueller 1916541af946SStephan Mueller /* remove the first part */ 1917541af946SStephan Mueller len = strlen(cra_driver_name) - start; 1918541af946SStephan Mueller for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) { 1919541af946SStephan Mueller if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name, 1920541af946SStephan Mueller len)) { 1921541af946SStephan Mueller *coreref = i; 1922541af946SStephan Mueller return; 1923541af946SStephan Mueller } 1924541af946SStephan Mueller } 1925541af946SStephan Mueller } 1926541af946SStephan Mueller 1927541af946SStephan Mueller static int drbg_kcapi_init(struct crypto_tfm *tfm) 1928541af946SStephan Mueller { 1929541af946SStephan Mueller struct drbg_state *drbg = crypto_tfm_ctx(tfm); 1930541af946SStephan Mueller 193176899a41SStephan Mueller mutex_init(&drbg->drbg_mutex); 19322a57e424SHerbert Xu 19332a57e424SHerbert Xu return 0; 1934541af946SStephan Mueller } 1935541af946SStephan Mueller 1936541af946SStephan Mueller static void drbg_kcapi_cleanup(struct crypto_tfm *tfm) 1937541af946SStephan Mueller { 1938541af946SStephan Mueller drbg_uninstantiate(crypto_tfm_ctx(tfm)); 1939541af946SStephan Mueller } 1940541af946SStephan Mueller 1941541af946SStephan Mueller /* 1942541af946SStephan Mueller * Generate random numbers invoked by the kernel crypto API: 1943541af946SStephan Mueller * The API of the kernel crypto API is extended as follows: 1944541af946SStephan Mueller * 19458fded592SHerbert Xu * src is additional input supplied to the RNG. 19468fded592SHerbert Xu * slen is the length of src. 19478fded592SHerbert Xu * dst is the output buffer where random data is to be stored. 19488fded592SHerbert Xu * dlen is the length of dst. 1949541af946SStephan Mueller */ 19508fded592SHerbert Xu static int drbg_kcapi_random(struct crypto_rng *tfm, 19518fded592SHerbert Xu const u8 *src, unsigned int slen, 19528fded592SHerbert Xu u8 *dst, unsigned int dlen) 1953541af946SStephan Mueller { 1954541af946SStephan Mueller struct drbg_state *drbg = crypto_rng_ctx(tfm); 19558fded592SHerbert Xu struct drbg_string *addtl = NULL; 19568fded592SHerbert Xu struct drbg_string string; 19578fded592SHerbert Xu 19588fded592SHerbert Xu if (slen) { 19598c987166SStephan Mueller /* linked list variable is now local to allow modification */ 19608fded592SHerbert Xu drbg_string_fill(&string, src, slen); 19618fded592SHerbert Xu addtl = &string; 1962541af946SStephan Mueller } 19638fded592SHerbert Xu 19648fded592SHerbert Xu return drbg_generate_long(drbg, dst, dlen, addtl); 1965541af946SStephan Mueller } 1966541af946SStephan Mueller 1967541af946SStephan Mueller /* 19682a57e424SHerbert Xu * Seed the DRBG invoked by the kernel crypto API 1969541af946SStephan Mueller */ 19708fded592SHerbert Xu static int drbg_kcapi_seed(struct crypto_rng *tfm, 19718fded592SHerbert Xu const u8 *seed, unsigned int slen) 1972541af946SStephan Mueller { 1973541af946SStephan Mueller struct drbg_state *drbg = crypto_rng_ctx(tfm); 1974541af946SStephan Mueller struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm); 1975541af946SStephan Mueller bool pr = false; 19768fded592SHerbert Xu struct drbg_string string; 19778fded592SHerbert Xu struct drbg_string *seed_string = NULL; 1978541af946SStephan Mueller int coreref = 0; 1979541af946SStephan Mueller 1980541af946SStephan Mueller drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref, 1981541af946SStephan Mueller &pr); 1982541af946SStephan Mueller if (0 < slen) { 19838fded592SHerbert Xu drbg_string_fill(&string, seed, slen); 19848fded592SHerbert Xu seed_string = &string; 1985541af946SStephan Mueller } 19868fded592SHerbert Xu 19878fded592SHerbert Xu return drbg_instantiate(drbg, seed_string, coreref, pr); 1988541af946SStephan Mueller } 1989541af946SStephan Mueller 1990541af946SStephan Mueller /*************************************************************** 1991541af946SStephan Mueller * Kernel module: code to load the module 1992541af946SStephan Mueller ***************************************************************/ 1993541af946SStephan Mueller 1994541af946SStephan Mueller /* 1995541af946SStephan Mueller * Tests as defined in 11.3.2 in addition to the cipher tests: testing 1996541af946SStephan Mueller * of the error handling. 1997541af946SStephan Mueller * 1998541af946SStephan Mueller * Note: testing of failing seed source as defined in 11.3.2 is not applicable 1999541af946SStephan Mueller * as seed source of get_random_bytes does not fail. 2000541af946SStephan Mueller * 2001541af946SStephan Mueller * Note 2: There is no sensible way of testing the reseed counter 2002541af946SStephan Mueller * enforcement, so skip it. 2003541af946SStephan Mueller */ 2004541af946SStephan Mueller static inline int __init drbg_healthcheck_sanity(void) 2005541af946SStephan Mueller { 2006541af946SStephan Mueller int len = 0; 2007541af946SStephan Mueller #define OUTBUFLEN 16 2008541af946SStephan Mueller unsigned char buf[OUTBUFLEN]; 2009541af946SStephan Mueller struct drbg_state *drbg = NULL; 201081f53028STim Gardner int ret; 2011541af946SStephan Mueller int rc = -EFAULT; 2012541af946SStephan Mueller bool pr = false; 2013541af946SStephan Mueller int coreref = 0; 2014541af946SStephan Mueller struct drbg_string addtl; 2015541af946SStephan Mueller size_t max_addtllen, max_request_bytes; 2016541af946SStephan Mueller 2017541af946SStephan Mueller /* only perform test in FIPS mode */ 2018541af946SStephan Mueller if (!fips_enabled) 2019541af946SStephan Mueller return 0; 2020541af946SStephan Mueller 2021541af946SStephan Mueller #ifdef CONFIG_CRYPTO_DRBG_CTR 2022541af946SStephan Mueller drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr); 2023e25e47ecSStephan Mueller #elif defined CONFIG_CRYPTO_DRBG_HASH 2024541af946SStephan Mueller drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr); 2025541af946SStephan Mueller #else 2026541af946SStephan Mueller drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr); 2027541af946SStephan Mueller #endif 2028541af946SStephan Mueller 2029541af946SStephan Mueller drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL); 2030541af946SStephan Mueller if (!drbg) 2031541af946SStephan Mueller return -ENOMEM; 2032541af946SStephan Mueller 2033e11a7548SHerbert Xu mutex_init(&drbg->drbg_mutex); 2034d89a6713SStephan Mueller drbg->core = &drbg_cores[coreref]; 2035d89a6713SStephan Mueller drbg->reseed_threshold = drbg_max_requests(drbg); 2036e11a7548SHerbert Xu 2037541af946SStephan Mueller /* 2038541af946SStephan Mueller * if the following tests fail, it is likely that there is a buffer 2039541af946SStephan Mueller * overflow as buf is much smaller than the requested or provided 2040541af946SStephan Mueller * string lengths -- in case the error handling does not succeed 2041541af946SStephan Mueller * we may get an OOPS. And we want to get an OOPS as this is a 2042541af946SStephan Mueller * grave bug. 2043541af946SStephan Mueller */ 2044541af946SStephan Mueller 2045541af946SStephan Mueller max_addtllen = drbg_max_addtl(drbg); 2046541af946SStephan Mueller max_request_bytes = drbg_max_request_bytes(drbg); 2047541af946SStephan Mueller drbg_string_fill(&addtl, buf, max_addtllen + 1); 2048541af946SStephan Mueller /* overflow addtllen with additonal info string */ 2049541af946SStephan Mueller len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl); 2050541af946SStephan Mueller BUG_ON(0 < len); 2051541af946SStephan Mueller /* overflow max_bits */ 2052541af946SStephan Mueller len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL); 2053541af946SStephan Mueller BUG_ON(0 < len); 2054541af946SStephan Mueller 2055541af946SStephan Mueller /* overflow max addtllen with personalization string */ 2056d89a6713SStephan Mueller ret = drbg_seed(drbg, &addtl, false); 2057541af946SStephan Mueller BUG_ON(0 == ret); 2058541af946SStephan Mueller /* all tests passed */ 2059541af946SStephan Mueller rc = 0; 2060541af946SStephan Mueller 2061541af946SStephan Mueller pr_devel("DRBG: Sanity tests for failure code paths successfully " 2062541af946SStephan Mueller "completed\n"); 2063541af946SStephan Mueller 2064d89a6713SStephan Mueller kfree(drbg); 2065541af946SStephan Mueller return rc; 2066541af946SStephan Mueller } 2067541af946SStephan Mueller 20688fded592SHerbert Xu static struct rng_alg drbg_algs[22]; 2069541af946SStephan Mueller 2070541af946SStephan Mueller /* 2071541af946SStephan Mueller * Fill the array drbg_algs used to register the different DRBGs 2072541af946SStephan Mueller * with the kernel crypto API. To fill the array, the information 2073541af946SStephan Mueller * from drbg_cores[] is used. 2074541af946SStephan Mueller */ 20758fded592SHerbert Xu static inline void __init drbg_fill_array(struct rng_alg *alg, 2076541af946SStephan Mueller const struct drbg_core *core, int pr) 2077541af946SStephan Mueller { 2078541af946SStephan Mueller int pos = 0; 207951ee1422SHerbert Xu static int priority = 200; 2080541af946SStephan Mueller 20818fded592SHerbert Xu memcpy(alg->base.cra_name, "stdrng", 6); 2082541af946SStephan Mueller if (pr) { 20838fded592SHerbert Xu memcpy(alg->base.cra_driver_name, "drbg_pr_", 8); 2084541af946SStephan Mueller pos = 8; 2085541af946SStephan Mueller } else { 20868fded592SHerbert Xu memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10); 2087541af946SStephan Mueller pos = 10; 2088541af946SStephan Mueller } 20898fded592SHerbert Xu memcpy(alg->base.cra_driver_name + pos, core->cra_name, 2090541af946SStephan Mueller strlen(core->cra_name)); 2091541af946SStephan Mueller 20928fded592SHerbert Xu alg->base.cra_priority = priority; 2093541af946SStephan Mueller priority++; 2094541af946SStephan Mueller /* 2095541af946SStephan Mueller * If FIPS mode enabled, the selected DRBG shall have the 2096541af946SStephan Mueller * highest cra_priority over other stdrng instances to ensure 2097541af946SStephan Mueller * it is selected. 2098541af946SStephan Mueller */ 2099541af946SStephan Mueller if (fips_enabled) 21008fded592SHerbert Xu alg->base.cra_priority += 200; 2101541af946SStephan Mueller 21028fded592SHerbert Xu alg->base.cra_ctxsize = sizeof(struct drbg_state); 21038fded592SHerbert Xu alg->base.cra_module = THIS_MODULE; 21048fded592SHerbert Xu alg->base.cra_init = drbg_kcapi_init; 21058fded592SHerbert Xu alg->base.cra_exit = drbg_kcapi_cleanup; 21068fded592SHerbert Xu alg->generate = drbg_kcapi_random; 21078fded592SHerbert Xu alg->seed = drbg_kcapi_seed; 21088fded592SHerbert Xu alg->set_ent = drbg_kcapi_set_entropy; 21098fded592SHerbert Xu alg->seedsize = 0; 2110541af946SStephan Mueller } 2111541af946SStephan Mueller 2112541af946SStephan Mueller static int __init drbg_init(void) 2113541af946SStephan Mueller { 2114541af946SStephan Mueller unsigned int i = 0; /* pointer to drbg_algs */ 2115541af946SStephan Mueller unsigned int j = 0; /* pointer to drbg_cores */ 21161a45d7e3SWei Yongjun int ret; 2117541af946SStephan Mueller 2118541af946SStephan Mueller ret = drbg_healthcheck_sanity(); 2119541af946SStephan Mueller if (ret) 2120541af946SStephan Mueller return ret; 2121541af946SStephan Mueller 2122541af946SStephan Mueller if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) { 2123541af946SStephan Mueller pr_info("DRBG: Cannot register all DRBG types" 2124a9089571SStephan Mueller "(slots needed: %zu, slots available: %zu)\n", 2125541af946SStephan Mueller ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs)); 21261a45d7e3SWei Yongjun return -EFAULT; 2127541af946SStephan Mueller } 2128541af946SStephan Mueller 2129541af946SStephan Mueller /* 2130541af946SStephan Mueller * each DRBG definition can be used with PR and without PR, thus 2131541af946SStephan Mueller * we instantiate each DRBG in drbg_cores[] twice. 2132541af946SStephan Mueller * 2133541af946SStephan Mueller * As the order of placing them into the drbg_algs array matters 2134541af946SStephan Mueller * (the later DRBGs receive a higher cra_priority) we register the 2135541af946SStephan Mueller * prediction resistance DRBGs first as the should not be too 2136541af946SStephan Mueller * interesting. 2137541af946SStephan Mueller */ 2138541af946SStephan Mueller for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++) 2139541af946SStephan Mueller drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1); 2140541af946SStephan Mueller for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++) 2141541af946SStephan Mueller drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0); 21428fded592SHerbert Xu return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2)); 2143541af946SStephan Mueller } 2144541af946SStephan Mueller 214596956aefSFengguang Wu static void __exit drbg_exit(void) 2146541af946SStephan Mueller { 21478fded592SHerbert Xu crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2)); 2148541af946SStephan Mueller } 2149541af946SStephan Mueller 2150c4741b23SEric Biggers subsys_initcall(drbg_init); 2151541af946SStephan Mueller module_exit(drbg_exit); 2152e25e47ecSStephan Mueller #ifndef CRYPTO_DRBG_HASH_STRING 2153e25e47ecSStephan Mueller #define CRYPTO_DRBG_HASH_STRING "" 2154e25e47ecSStephan Mueller #endif 2155e25e47ecSStephan Mueller #ifndef CRYPTO_DRBG_HMAC_STRING 2156e25e47ecSStephan Mueller #define CRYPTO_DRBG_HMAC_STRING "" 2157e25e47ecSStephan Mueller #endif 2158e25e47ecSStephan Mueller #ifndef CRYPTO_DRBG_CTR_STRING 2159e25e47ecSStephan Mueller #define CRYPTO_DRBG_CTR_STRING "" 2160e25e47ecSStephan Mueller #endif 2161541af946SStephan Mueller MODULE_LICENSE("GPL"); 2162541af946SStephan Mueller MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>"); 2163e25e47ecSStephan Mueller MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) " 2164e25e47ecSStephan Mueller "using following cores: " 2165e25e47ecSStephan Mueller CRYPTO_DRBG_HASH_STRING 2166e25e47ecSStephan Mueller CRYPTO_DRBG_HMAC_STRING 2167e25e47ecSStephan Mueller CRYPTO_DRBG_CTR_STRING); 216851ee1422SHerbert Xu MODULE_ALIAS_CRYPTO("stdrng"); 21690eb76ba2SArd Biesheuvel MODULE_IMPORT_NS(CRYPTO_INTERNAL); 2170