/* * DRBG: Deterministic Random Bits Generator * Based on NIST Recommended DRBG from NIST SP800-90A with the following * properties: * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores * * with and without prediction resistance * * Copyright Stephan Mueller <smueller@chronox.de>, 2014 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, and the entire permission notice in its entirety, * including the disclaimer of warranties. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * ALTERNATIVELY, this product may be distributed under the terms of * the GNU General Public License, in which case the provisions of the GPL are * required INSTEAD OF the above restrictions. (This clause is * necessary due to a potential bad interaction between the GPL and * the restrictions contained in a BSD-style copyright.) * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * DRBG Usage * ========== * The SP 800-90A DRBG allows the user to specify a personalization string * for initialization as well as an additional information string for each * random number request. The following code fragments show how a caller * uses the kernel crypto API to use the full functionality of the DRBG. * * Usage without any additional data * --------------------------------- * struct crypto_rng *drng; * int err; * char data[DATALEN]; * * drng = crypto_alloc_rng(drng_name, 0, 0); * err = crypto_rng_get_bytes(drng, &data, DATALEN); * crypto_free_rng(drng); * * * Usage with personalization string during initialization * ------------------------------------------------------- * struct crypto_rng *drng; * int err; * char data[DATALEN]; * struct drbg_string pers; * char personalization[11] = "some-string"; * * drbg_string_fill(&pers, personalization, strlen(personalization)); * drng = crypto_alloc_rng(drng_name, 0, 0); * // The reset completely re-initializes the DRBG with the provided * // personalization string * err = crypto_rng_reset(drng, &personalization, strlen(personalization)); * err = crypto_rng_get_bytes(drng, &data, DATALEN); * crypto_free_rng(drng); * * * Usage with additional information string during random number request * --------------------------------------------------------------------- * struct crypto_rng *drng; * int err; * char data[DATALEN]; * char addtl_string[11] = "some-string"; * string drbg_string addtl; * * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string)); * drng = crypto_alloc_rng(drng_name, 0, 0); * // The following call is a wrapper to crypto_rng_get_bytes() and returns * // the same error codes. * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl); * crypto_free_rng(drng); * * * Usage with personalization and additional information strings * ------------------------------------------------------------- * Just mix both scenarios above.
*/
/*************************************************************** * Backend cipher definitions available to DRBG
***************************************************************/
/* * The order of the DRBG definitions here matter: every DRBG is registered * as stdrng. Each DRBG receives an increasing cra_priority values the later * they are defined in this array (see drbg_fill_array). * * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and the * HMAC-SHA512 / SHA256 / AES 256 over other ciphers. Thus, the * favored DRBGs are the latest entries in this array.
*/ staticconststruct drbg_core drbg_cores[] = { #ifdef CONFIG_CRYPTO_DRBG_CTR
{
.flags = DRBG_CTR | DRBG_STRENGTH128,
.statelen = 32, /* 256 bits as defined in 10.2.1 */
.blocklen_bytes = 16,
.cra_name = "ctr_aes128",
.backend_cra_name = "aes",
}, {
.flags = DRBG_CTR | DRBG_STRENGTH192,
.statelen = 40, /* 320 bits as defined in 10.2.1 */
.blocklen_bytes = 16,
.cra_name = "ctr_aes192",
.backend_cra_name = "aes",
}, {
.flags = DRBG_CTR | DRBG_STRENGTH256,
.statelen = 48, /* 384 bits as defined in 10.2.1 */
.blocklen_bytes = 16,
.cra_name = "ctr_aes256",
.backend_cra_name = "aes",
}, #endif/* CONFIG_CRYPTO_DRBG_CTR */ #ifdef CONFIG_CRYPTO_DRBG_HASH
{
.flags = DRBG_HASH | DRBG_STRENGTH256,
.statelen = 111, /* 888 bits */
.blocklen_bytes = 48,
.cra_name = "sha384",
.backend_cra_name = "sha384",
}, {
.flags = DRBG_HASH | DRBG_STRENGTH256,
.statelen = 111, /* 888 bits */
.blocklen_bytes = 64,
.cra_name = "sha512",
.backend_cra_name = "sha512",
}, {
.flags = DRBG_HASH | DRBG_STRENGTH256,
.statelen = 55, /* 440 bits */
.blocklen_bytes = 32,
.cra_name = "sha256",
.backend_cra_name = "sha256",
}, #endif/* CONFIG_CRYPTO_DRBG_HASH */ #ifdef CONFIG_CRYPTO_DRBG_HMAC
{
.flags = DRBG_HMAC | DRBG_STRENGTH256,
.statelen = 48, /* block length of cipher */
.blocklen_bytes = 48,
.cra_name = "hmac_sha384",
.backend_cra_name = "hmac(sha384)",
}, {
.flags = DRBG_HMAC | DRBG_STRENGTH256,
.statelen = 32, /* block length of cipher */
.blocklen_bytes = 32,
.cra_name = "hmac_sha256",
.backend_cra_name = "hmac(sha256)",
}, {
.flags = DRBG_HMAC | DRBG_STRENGTH256,
.statelen = 64, /* block length of cipher */
.blocklen_bytes = 64,
.cra_name = "hmac_sha512",
.backend_cra_name = "hmac(sha512)",
}, #endif/* CONFIG_CRYPTO_DRBG_HMAC */
};
/* * Return strength of DRBG according to SP800-90A section 8.4 * * @flags DRBG flags reference * * Return: normalized strength in *bytes* value or 32 as default * to counter programming errors
*/ staticinlineunsignedshort drbg_sec_strength(drbg_flag_t flags)
{ switch (flags & DRBG_STRENGTH_MASK) { case DRBG_STRENGTH128: return 16; case DRBG_STRENGTH192: return 24; case DRBG_STRENGTH256: return 32; default: return 32;
}
}
/* * FIPS 140-2 continuous self test for the noise source * The test is performed on the noise source input data. Thus, the function * implicitly knows the size of the buffer to be equal to the security * strength. * * Note, this function disregards the nonce trailing the entropy data during * initial seeding. * * drbg->drbg_mutex must have been taken. * * @drbg DRBG handle * @entropy buffer of seed data to be checked * * return: * 0 on success * -EAGAIN on when the CTRNG is not yet primed * < 0 on error
*/ staticint drbg_fips_continuous_test(struct drbg_state *drbg, constunsignedchar *entropy)
{ unsignedshort entropylen = drbg_sec_strength(drbg->core->flags); int ret = 0;
if (!IS_ENABLED(CONFIG_CRYPTO_FIPS)) return 0;
/* skip test if we test the overall system */ if (list_empty(&drbg->test_data.list)) return 0; /* only perform test in FIPS mode */ if (!fips_enabled) return 0;
if (!drbg->fips_primed) { /* Priming of FIPS test */
memcpy(drbg->prev, entropy, entropylen);
drbg->fips_primed = true; /* priming: another round is needed */ return -EAGAIN;
}
ret = memcmp(drbg->prev, entropy, entropylen); if (!ret)
panic("DRBG continuous self test failed\n");
memcpy(drbg->prev, entropy, entropylen);
/* the test shall pass when the two values are not equal */ return 0;
}
/* * Convert an integer into a byte representation of this integer. * The byte representation is big-endian * * @val value to be converted * @buf buffer holding the converted integer -- caller must ensure that * buffer size is at least 32 bit
*/ #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR)) staticinlinevoid drbg_cpu_to_be32(__u32 val, unsignedchar *buf)
{ struct s {
__be32 conv;
}; struct s *conversion = (struct s *) buf;
/* BCC function for CTR DRBG as defined in 10.4.3 */ staticint drbg_ctr_bcc(struct drbg_state *drbg, unsignedchar *out, constunsignedchar *key, struct list_head *in)
{ int ret = 0; struct drbg_string *curr = NULL; struct drbg_string data; short cnt = 0;
/* 10.4.3 step 2 / 4 */
drbg_kcapi_symsetkey(drbg, key);
list_for_each_entry(curr, in, list) { constunsignedchar *pos = curr->buf;
size_t len = curr->len; /* 10.4.3 step 4.1 */ while (len) { /* 10.4.3 step 4.2 */ if (drbg_blocklen(drbg) == cnt) {
cnt = 0;
ret = drbg_kcapi_sym(drbg, out, &data); if (ret) return ret;
}
out[cnt] ^= *pos;
pos++;
cnt++;
len--;
}
} /* 10.4.3 step 4.2 for last block */ if (cnt)
ret = drbg_kcapi_sym(drbg, out, &data);
return ret;
}
/* * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df * (and drbg_ctr_bcc, but this function does not need any temporary buffers), * the scratchpad is used as follows: * drbg_ctr_update: * temp * start: drbg->scratchpad * length: drbg_statelen(drbg) + drbg_blocklen(drbg) * note: the cipher writing into this variable works * blocklen-wise. Now, when the statelen is not a multiple * of blocklen, the generateion loop below "spills over" * by at most blocklen. Thus, we need to give sufficient * memory. * df_data * start: drbg->scratchpad + * drbg_statelen(drbg) + drbg_blocklen(drbg) * length: drbg_statelen(drbg) * * drbg_ctr_df: * pad * start: df_data + drbg_statelen(drbg) * length: drbg_blocklen(drbg) * iv * start: pad + drbg_blocklen(drbg) * length: drbg_blocklen(drbg) * temp * start: iv + drbg_blocklen(drbg) * length: drbg_satelen(drbg) + drbg_blocklen(drbg) * note: temp is the buffer that the BCC function operates * on. BCC operates blockwise. drbg_statelen(drbg) * is sufficient when the DRBG state length is a multiple * of the block size. For AES192 (and maybe other ciphers) * this is not correct and the length for temp is * insufficient (yes, that also means for such ciphers, * the final output of all BCC rounds are truncated). * Therefore, add drbg_blocklen(drbg) to cover all * possibilities.
*/
/* Derivation Function for CTR DRBG as defined in 10.4.2 */ staticint drbg_ctr_df(struct drbg_state *drbg, unsignedchar *df_data, size_t bytes_to_return, struct list_head *seedlist)
{ int ret = -EFAULT; unsignedchar L_N[8]; /* S3 is input */ struct drbg_string S1, S2, S4, cipherin;
LIST_HEAD(bcc_list); unsignedchar *pad = df_data + drbg_statelen(drbg); unsignedchar *iv = pad + drbg_blocklen(drbg); unsignedchar *temp = iv + drbg_blocklen(drbg);
size_t padlen = 0; unsignedint templen = 0; /* 10.4.2 step 7 */ unsignedint i = 0; /* 10.4.2 step 8 */ constunsignedchar *K = (unsignedchar *) "\x00\x01\x02\x03\x04\x05\x06\x07" "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" "\x10\x11\x12\x13\x14\x15\x16\x17" "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; unsignedchar *X;
size_t generated_len = 0;
size_t inputlen = 0; struct drbg_string *seed = NULL;
/* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg)); /* wrap the padlen appropriately */ if (padlen)
padlen = drbg_blocklen(drbg) - padlen; /* * pad / padlen contains the 0x80 byte and the following zero bytes. * As the calculated padlen value only covers the number of zero * bytes, this value has to be incremented by one for the 0x80 byte.
*/
padlen++;
pad[0] = 0x80;
/* 10.4.2 step 4 -- first fill the linked list and then order it */
drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
list_add_tail(&S1.list, &bcc_list);
drbg_string_fill(&S2, L_N, sizeof(L_N));
list_add_tail(&S2.list, &bcc_list);
list_splice_tail(seedlist, &bcc_list);
drbg_string_fill(&S4, pad, padlen);
list_add_tail(&S4.list, &bcc_list);
/* 10.4.2 step 9 */ while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) { /* * 10.4.2 step 9.1 - the padding is implicit as the buffer * holds zeros after allocation -- even the increment of i * is irrelevant as the increment remains within length of i
*/
drbg_cpu_to_be32(i, iv); /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list); if (ret) goto out; /* 10.4.2 step 9.3 */
i++;
templen += drbg_blocklen(drbg);
}
/* 10.4.2 step 12: overwriting of outval is implemented in next step */
/* 10.4.2 step 13 */
drbg_kcapi_symsetkey(drbg, temp); while (generated_len < bytes_to_return) { short blocklen = 0; /* * 10.4.2 step 13.1: the truncation of the key length is * implicit as the key is only drbg_blocklen in size based on * the implementation of the cipher function callback
*/
ret = drbg_kcapi_sym(drbg, X, &cipherin); if (ret) goto out;
blocklen = (drbg_blocklen(drbg) <
(bytes_to_return - generated_len)) ?
drbg_blocklen(drbg) :
(bytes_to_return - generated_len); /* 10.4.2 step 13.2 and 14 */
memcpy(df_data + generated_len, X, blocklen);
generated_len += blocklen;
}
/* * update function of CTR DRBG as defined in 10.2.1.2 * * The reseed variable has an enhanced meaning compared to the update * functions of the other DRBGs as follows: * 0 => initial seed from initialization * 1 => reseed via drbg_seed * 2 => first invocation from drbg_ctr_update when addtl is present. In * this case, the df_data scratchpad is not deleted so that it is * available for another calls to prevent calling the DF function * again. * 3 => second invocation from drbg_ctr_update. When the update function * was called with addtl, the df_data memory already contains the * DFed addtl information and we do not need to call DF again.
*/ staticint drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed, int reseed)
{ int ret = -EFAULT; /* 10.2.1.2 step 1 */ unsignedchar *temp = drbg->scratchpad; unsignedchar *df_data = drbg->scratchpad + drbg_statelen(drbg) +
drbg_blocklen(drbg);
if (3 > reseed)
memset(df_data, 0, drbg_statelen(drbg));
if (!reseed) { /* * The DRBG uses the CTR mode of the underlying AES cipher. The * CTR mode increments the counter value after the AES operation * but SP800-90A requires that the counter is incremented before * the AES operation. Hence, we increment it at the time we set * it by one.
*/
crypto_inc(drbg->V, drbg_blocklen(drbg));
ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
drbg_keylen(drbg)); if (ret) goto out;
}
/* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */ if (seed) {
ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed); if (ret) goto out;
}
ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg),
temp, drbg_statelen(drbg)); if (ret) return ret;
/* 10.2.1.2 step 5 */
ret = crypto_skcipher_setkey(drbg->ctr_handle, temp,
drbg_keylen(drbg)); if (ret) goto out; /* 10.2.1.2 step 6 */
memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg)); /* See above: increment counter by one to compensate timing of CTR op */
crypto_inc(drbg->V, drbg_blocklen(drbg));
ret = 0;
/* * scratchpad use: drbg_ctr_update is called independently from * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
*/ /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */ staticint drbg_ctr_generate(struct drbg_state *drbg, unsignedchar *buf, unsignedint buflen, struct list_head *addtl)
{ int ret; int len = min_t(int, buflen, INT_MAX);
/* 10.2.1.5.2 step 2 */ if (addtl && !list_empty(addtl)) {
ret = drbg_ctr_update(drbg, addtl, 2); if (ret) return 0;
}
/* 10.2.1.5.2 step 4.1 */
ret = drbg_kcapi_sym_ctr(drbg, NULL, 0, buf, len); if (ret) return ret;
/* 10.2.1.5.2 step 6 */
ret = drbg_ctr_update(drbg, NULL, 3); if (ret)
len = ret;
/* update function of HMAC DRBG as defined in 10.1.2.2 */ staticint drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed, int reseed)
{ int ret = -EFAULT; int i = 0; struct drbg_string seed1, seed2, vdata;
LIST_HEAD(seedlist);
LIST_HEAD(vdatalist);
if (!reseed) { /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
memset(drbg->V, 1, drbg_statelen(drbg));
drbg_kcapi_hmacsetkey(drbg, drbg->C);
}
drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
list_add_tail(&seed1.list, &seedlist); /* buffer of seed2 will be filled in for loop below with one byte */
drbg_string_fill(&seed2, NULL, 1);
list_add_tail(&seed2.list, &seedlist); /* input data of seed is allowed to be NULL at this point */ if (seed)
list_splice_tail(seed, &seedlist);
drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
list_add_tail(&vdata.list, &vdatalist); for (i = 2; 0 < i; i--) { /* first round uses 0x0, second 0x1 */ unsignedchar prefix = DRBG_PREFIX0; if (1 == i)
prefix = DRBG_PREFIX1; /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
seed2.buf = &prefix;
ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist); if (ret) return ret;
drbg_kcapi_hmacsetkey(drbg, drbg->C);
/* 10.1.2.2 step 2 and 5 -- HMAC for V */
ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist); if (ret) return ret;
/* 10.1.2.2 step 3 */ if (!seed) return ret;
}
return 0;
}
/* generate function of HMAC DRBG as defined in 10.1.2.5 */ staticint drbg_hmac_generate(struct drbg_state *drbg, unsignedchar *buf, unsignedint buflen, struct list_head *addtl)
{ int len = 0; int ret = 0; struct drbg_string data;
LIST_HEAD(datalist);
/* 10.1.2.5 step 2 */ if (addtl && !list_empty(addtl)) {
ret = drbg_hmac_update(drbg, addtl, 1); if (ret) return ret;
}
/* * scratchpad usage: as drbg_hash_update and drbg_hash_df are used * interlinked, the scratchpad is used as follows: * drbg_hash_update * start: drbg->scratchpad * length: drbg_statelen(drbg) * drbg_hash_df: * start: drbg->scratchpad + drbg_statelen(drbg) * length: drbg_blocklen(drbg) * * drbg_hash_process_addtl uses the scratchpad, but fully completes * before either of the functions mentioned before are invoked. Therefore, * drbg_hash_process_addtl does not need to be specifically considered.
*/
/* Derivation Function for Hash DRBG as defined in 10.4.1 */ staticint drbg_hash_df(struct drbg_state *drbg, unsignedchar *outval, size_t outlen, struct list_head *entropylist)
{ int ret = 0;
size_t len = 0; unsignedchar input[5]; unsignedchar *tmp = drbg->scratchpad + drbg_statelen(drbg); struct drbg_string data;
/* generate function for Hash DRBG as defined in 10.1.1.4 */ staticint drbg_hash_generate(struct drbg_state *drbg, unsignedchar *buf, unsignedint buflen, struct list_head *addtl)
{ int len = 0; int ret = 0; union { unsignedchar req[8];
__be64 req_int;
} u; unsignedchar prefix = DRBG_PREFIX3; struct drbg_string data1, data2;
LIST_HEAD(datalist);
/* 10.1.1.4 step 2 */
ret = drbg_hash_process_addtl(drbg, addtl); if (ret) return ret; /* 10.1.1.4 step 3 */
len = drbg_hash_hashgen(drbg, buf, buflen);
/* this is the value H as documented in 10.1.1.4 */ /* 10.1.1.4 step 4 */
drbg_string_fill(&data1, &prefix, 1);
list_add_tail(&data1.list, &datalist);
drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
list_add_tail(&data2.list, &datalist);
ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist); if (ret) {
len = ret; goto out;
}
/* * scratchpad usage: as update and generate are used isolated, both * can use the scratchpad
*/ staticconststruct drbg_state_ops drbg_hash_ops = {
.update = drbg_hash_update,
.generate = drbg_hash_generate,
.crypto_init = drbg_init_hash_kernel,
.crypto_fini = drbg_fini_hash_kernel,
}; #endif/* CONFIG_CRYPTO_DRBG_HASH */
/****************************************************************** * Functions common for DRBG implementations
******************************************************************/
staticinlineint __drbg_seed(struct drbg_state *drbg, struct list_head *seed, int reseed, enum drbg_seed_state new_seed_state)
{ int ret = drbg->d_ops->update(drbg, seed, reseed);
switch (drbg->seeded) { case DRBG_SEED_STATE_UNSEEDED: /* Impossible, but handle it to silence compiler warnings. */
fallthrough; case DRBG_SEED_STATE_PARTIAL: /* * Require frequent reseeds until the seed source is * fully initialized.
*/
drbg->reseed_threshold = 50; break;
case DRBG_SEED_STATE_FULL: /* * Seed source has become fully initialized, frequent * reseeds no longer required.
*/
drbg->reseed_threshold = drbg_max_requests(drbg); break;
}
/* Don't ever reseed from get_random_bytes() in test mode. */ if (list_empty(&drbg->test_data.list)) returnfalse;
/* * Obtain fresh entropy for the nopr DRBGs after 300s have * elapsed in order to still achieve sort of partial * prediction resistance over the time domain at least. Note * that the period of 300s has been chosen to match the * CRNG_RESEED_INTERVAL of the get_random_bytes()' chacha * rngs.
*/
next_reseed = drbg->last_seed_time + 300 * HZ; return time_after(jiffies, next_reseed);
}
/* * Seeding or reseeding of the DRBG * * @drbg: DRBG state struct * @pers: personalization / additional information buffer * @reseed: 0 for initial seed process, 1 for reseeding * * return: * 0 on success * error value otherwise
*/ staticint drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, bool reseed)
{ int ret; unsignedchar entropy[((32 + 16) * 2)]; unsignedint entropylen = drbg_sec_strength(drbg->core->flags); struct drbg_string data1;
LIST_HEAD(seedlist); enum drbg_seed_state new_seed_state = DRBG_SEED_STATE_FULL;
/* 9.1 / 9.2 / 9.3.1 step 3 */ if (pers && pers->len > (drbg_max_addtl(drbg))) {
pr_devel("DRBG: personalization string too long %zu\n",
pers->len); return -EINVAL;
}
if (list_empty(&drbg->test_data.list)) {
drbg_string_fill(&data1, drbg->test_data.buf,
drbg->test_data.len);
pr_devel("DRBG: using test entropy\n");
} else { /* * Gather entropy equal to the security strength of the DRBG. * With a derivation function, a nonce is required in addition * to the entropy. A nonce must be at least 1/2 of the security * strength of the DRBG in size. Thus, entropy + nonce is 3/2 * of the strength. The consideration of a nonce is only * applicable during initial seeding.
*/
BUG_ON(!entropylen); if (!reseed)
entropylen = ((entropylen + 1) / 2) * 3;
BUG_ON((entropylen * 2) > sizeof(entropy));
/* Get seed from in-kernel /dev/urandom */ if (!rng_is_initialized())
new_seed_state = DRBG_SEED_STATE_PARTIAL;
ret = drbg_get_random_bytes(drbg, entropy, entropylen); if (ret) goto out;
if (!drbg->jent) {
drbg_string_fill(&data1, entropy, entropylen);
pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
entropylen);
} else { /* * Get seed from Jitter RNG, failures are * fatal only in FIPS mode.
*/
ret = crypto_rng_get_bytes(drbg->jent,
entropy + entropylen,
entropylen); if (fips_enabled && ret) {
pr_devel("DRBG: jent failed with %d\n", ret);
/* * Do not treat the transient failure of the * Jitter RNG as an error that needs to be * reported. The combined number of the * maximum reseed threshold times the maximum * number of Jitter RNG transient errors is * less than the reseed threshold required by * SP800-90A allowing us to treat the * transient errors as such. * * However, we mandate that at least the first * seeding operation must succeed with the * Jitter RNG.
*/ if (!reseed || ret != -EAGAIN) goto out;
}
drbg_string_fill(&data1, entropy, entropylen * 2);
pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
entropylen * 2);
}
}
list_add_tail(&data1.list, &seedlist);
/* * concatenation of entropy with personalization str / addtl input) * the variable pers is directly handed in by the caller, so check its * contents whether it is appropriate
*/ if (pers && pers->buf && 0 < pers->len) {
list_add_tail(&pers->list, &seedlist);
pr_devel("DRBG: using personalization string\n");
}
if (!reseed) {
memset(drbg->V, 0, drbg_statelen(drbg));
memset(drbg->C, 0, drbg_statelen(drbg));
}
ret = __drbg_seed(drbg, &seedlist, reseed, new_seed_state);
out:
memzero_explicit(entropy, entropylen * 2);
return ret;
}
/* Free all substructures in a DRBG state without the DRBG state structure */ staticinlinevoid drbg_dealloc_state(struct drbg_state *drbg)
{ if (!drbg) return;
kfree_sensitive(drbg->Vbuf);
drbg->Vbuf = NULL;
drbg->V = NULL;
kfree_sensitive(drbg->Cbuf);
drbg->Cbuf = NULL;
drbg->C = NULL;
kfree_sensitive(drbg->scratchpadbuf);
drbg->scratchpadbuf = NULL;
drbg->reseed_ctr = 0;
drbg->d_ops = NULL;
drbg->core = NULL; if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
kfree_sensitive(drbg->prev);
drbg->prev = NULL;
drbg->fips_primed = false;
}
}
/* * Allocate all sub-structures for a DRBG state. * The DRBG state structure must already be allocated.
*/ staticinlineint drbg_alloc_state(struct drbg_state *drbg)
{ int ret = -ENOMEM; unsignedint sb_size = 0;
/* * DRBG generate function as required by SP800-90A - this function * generates random numbers * * @drbg DRBG state handle * @buf Buffer where to store the random numbers -- the buffer must already * be pre-allocated by caller * @buflen Length of output buffer - this value defines the number of random * bytes pulled from DRBG * @addtl Additional input that is mixed into state, may be NULL -- note * the entropy is pulled by the DRBG internally unconditionally * as defined in SP800-90A. The additional input is mixed into * the state in addition to the pulled entropy. * * return: 0 when all bytes are generated; < 0 in case of an error
*/ staticint drbg_generate(struct drbg_state *drbg, unsignedchar *buf, unsignedint buflen, struct drbg_string *addtl)
{ int len = 0;
LIST_HEAD(addtllist);
if (!drbg->core) {
pr_devel("DRBG: not yet seeded\n"); return -EINVAL;
} if (0 == buflen || !buf) {
pr_devel("DRBG: no output buffer provided\n"); return -EINVAL;
} if (addtl && NULL == addtl->buf && 0 < addtl->len) {
pr_devel("DRBG: wrong format of additional information\n"); return -EINVAL;
}
/* 9.3.1 step 2 */
len = -EINVAL; if (buflen > (drbg_max_request_bytes(drbg))) {
pr_devel("DRBG: requested random numbers too large %u\n",
buflen); goto err;
}
/* 9.3.1 step 3 is implicit with the chosen DRBG */
/* 9.3.1 step 4 */ if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
pr_devel("DRBG: additional information string too long %zu\n",
addtl->len); goto err;
} /* 9.3.1 step 5 is implicit with the chosen DRBG */
/* * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented * here. The spec is a bit convoluted here, we make it simpler.
*/ if (drbg->reseed_threshold < drbg->reseed_ctr)
drbg->seeded = DRBG_SEED_STATE_UNSEEDED;
if (drbg->pr || drbg->seeded == DRBG_SEED_STATE_UNSEEDED) {
pr_devel("DRBG: reseeding before generation (prediction " "resistance: %s, state %s)\n",
str_true_false(drbg->pr),
(drbg->seeded == DRBG_SEED_STATE_FULL ? "seeded" : "unseeded")); /* 9.3.1 steps 7.1 through 7.3 */
len = drbg_seed(drbg, addtl, true); if (len) goto err; /* 9.3.1 step 7.4 */
addtl = NULL;
} elseif (rng_is_initialized() &&
(drbg->seeded == DRBG_SEED_STATE_PARTIAL ||
drbg_nopr_reseed_interval_elapsed(drbg))) {
len = drbg_seed_from_random(drbg); if (len) goto err;
}
if (addtl && 0 < addtl->len)
list_add_tail(&addtl->list, &addtllist); /* 9.3.1 step 8 and 10 */
len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
/* * Section 11.3.3 requires to re-perform self tests after some * generated random numbers. The chosen value after which self * test is performed is arbitrary, but it should be reasonable. * However, we do not perform the self tests because of the following * reasons: it is mathematically impossible that the initial self tests * were successfully and the following are not. If the initial would * pass and the following would not, the kernel integrity is violated. * In this case, the entire kernel operation is questionable and it * is unlikely that the integrity violation only affects the * correct operation of the DRBG. * * Albeit the following code is commented out, it is provided in * case somebody has a need to implement the test of 11.3.3.
*/ #if 0 if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) { int err = 0;
pr_devel("DRBG: start to perform self test\n"); if (drbg->core->flags & DRBG_HMAC)
err = alg_test("drbg_pr_hmac_sha512", "drbg_pr_hmac_sha512", 0, 0); elseif (drbg->core->flags & DRBG_CTR)
err = alg_test("drbg_pr_ctr_aes256", "drbg_pr_ctr_aes256", 0, 0); else
err = alg_test("drbg_pr_sha256", "drbg_pr_sha256", 0, 0); if (err) {
pr_err("DRBG: periodical self test failed\n"); /* * uninstantiate implies that from now on, only errors * are returned when reusing this DRBG cipher handle
*/
drbg_uninstantiate(drbg); return 0;
} else {
pr_devel("DRBG: self test successful\n");
}
} #endif
/* * All operations were successful, return 0 as mandated by * the kernel crypto API interface.
*/
len = 0;
err: return len;
}
/* * Wrapper around drbg_generate which can pull arbitrary long strings * from the DRBG without hitting the maximum request limitation. * * Parameters: see drbg_generate * Return codes: see drbg_generate -- if one drbg_generate request fails, * the entire drbg_generate_long request fails
*/ staticint drbg_generate_long(struct drbg_state *drbg, unsignedchar *buf, unsignedint buflen, struct drbg_string *addtl)
{ unsignedint len = 0; unsignedint slice = 0; do { int err = 0; unsignedint chunk = 0;
slice = ((buflen - len) / drbg_max_request_bytes(drbg));
chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
mutex_lock(&drbg->drbg_mutex);
err = drbg_generate(drbg, buf + len, chunk, addtl);
mutex_unlock(&drbg->drbg_mutex); if (0 > err) return err;
len += chunk;
} while (slice > 0 && (len < buflen)); return 0;
}
staticint drbg_prepare_hrng(struct drbg_state *drbg)
{ /* We do not need an HRNG in test mode. */ if (list_empty(&drbg->test_data.list)) return 0;
drbg->jent = NULL; if (fips_enabled) return err;
pr_info("DRBG: Continuing without Jitter RNG\n");
}
return 0;
}
/* * DRBG instantiation function as required by SP800-90A - this function * sets up the DRBG handle, performs the initial seeding and all sanity * checks required by SP800-90A * * @drbg memory of state -- if NULL, new memory is allocated * @pers Personalization string that is mixed into state, may be NULL -- note * the entropy is pulled by the DRBG internally unconditionally * as defined in SP800-90A. The additional input is mixed into * the state in addition to the pulled entropy. * @coreref reference to core * @pr prediction resistance enabled * * return * 0 on success * error value otherwise
*/ staticint drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers, int coreref, bool pr)
{ int ret; bool reseed = true;
/* 9.1 step 1 is implicit with the selected DRBG type */
/* * 9.1 step 2 is implicit as caller can select prediction resistance * and the flag is copied into drbg->flags -- * all DRBG types support prediction resistance
*/
/* * DRBG uninstantiate function as required by SP800-90A - this function * frees all buffers and the DRBG handle * * @drbg DRBG state handle * * return * 0 on success
*/ staticint drbg_uninstantiate(struct drbg_state *drbg)
{ if (!IS_ERR_OR_NULL(drbg->jent))
crypto_free_rng(drbg->jent);
drbg->jent = NULL;
if (drbg->d_ops)
drbg->d_ops->crypto_fini(drbg);
drbg_dealloc_state(drbg); /* no scrubbing of test_data -- this shall survive an uninstantiate */ return 0;
}
/* * Helper function for setting the test data in the DRBG * * @drbg DRBG state handle * @data test data * @len test data length
*/ staticvoid drbg_kcapi_set_entropy(struct crypto_rng *tfm, const u8 *data, unsignedint len)
{ struct drbg_state *drbg = crypto_rng_ctx(tfm);
if (inbuf) { /* Use caller-provided input buffer */
sg_set_buf(sg_in, inbuf, inlen);
} else { /* Use scratchpad for in-place operation */
inlen = scratchpad_use;
memset(drbg->outscratchpad, 0, scratchpad_use);
sg_set_buf(sg_in, drbg->outscratchpad, scratchpad_use);
}
while (outlen) {
u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
/* Output buffer may not be valid for SGL, use scratchpad */
skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out,
cryptlen, drbg->V);
ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req),
&drbg->ctr_wait); if (ret) goto out;
/*************************************************************** * Kernel crypto API interface to register DRBG
***************************************************************/
/* * Look up the DRBG flags by given kernel crypto API cra_name * The code uses the drbg_cores definition to do this * * @cra_name kernel crypto API cra_name * @coreref reference to integer which is filled with the pointer to * the applicable core * @pr reference for setting prediction resistance * * return: flags
*/ staticinlinevoid drbg_convert_tfm_core(constchar *cra_driver_name, int *coreref, bool *pr)
{ int i = 0;
size_t start = 0; int len = 0;
/* remove the first part */
len = strlen(cra_driver_name) - start; for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) { if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
len)) {
*coreref = i; return;
}
}
}
/* * Generate random numbers invoked by the kernel crypto API: * The API of the kernel crypto API is extended as follows: * * src is additional input supplied to the RNG. * slen is the length of src. * dst is the output buffer where random data is to be stored. * dlen is the length of dst.
*/ staticint drbg_kcapi_random(struct crypto_rng *tfm, const u8 *src, unsignedint slen,
u8 *dst, unsignedint dlen)
{ struct drbg_state *drbg = crypto_rng_ctx(tfm); struct drbg_string *addtl = NULL; struct drbg_string string;
if (slen) { /* linked list variable is now local to allow modification */
drbg_string_fill(&string, src, slen);
addtl = &string;
}
/*************************************************************** * Kernel module: code to load the module
***************************************************************/
/* * Tests as defined in 11.3.2 in addition to the cipher tests: testing * of the error handling. * * Note: testing of failing seed source as defined in 11.3.2 is not applicable * as seed source of get_random_bytes does not fail. * * Note 2: There is no sensible way of testing the reseed counter * enforcement, so skip it.
*/ staticinlineint __init drbg_healthcheck_sanity(void)
{ int len = 0; #define OUTBUFLEN 16 unsignedchar buf[OUTBUFLEN]; struct drbg_state *drbg = NULL; int ret; int rc = -EFAULT; bool pr = false; int coreref = 0; struct drbg_string addtl;
size_t max_addtllen, max_request_bytes;
/* only perform test in FIPS mode */ if (!fips_enabled) return 0;
/* * if the following tests fail, it is likely that there is a buffer * overflow as buf is much smaller than the requested or provided * string lengths -- in case the error handling does not succeed * we may get an OOPS. And we want to get an OOPS as this is a * grave bug.
*/
/* overflow max addtllen with personalization string */
ret = drbg_seed(drbg, &addtl, false);
BUG_ON(0 == ret); /* all tests passed */
rc = 0;
pr_devel("DRBG: Sanity tests for failure code paths successfully " "completed\n");
kfree(drbg); return rc;
}
staticstruct rng_alg drbg_algs[22];
/* * Fill the array drbg_algs used to register the different DRBGs * with the kernel crypto API. To fill the array, the information * from drbg_cores[] is used.
*/ staticinlinevoid __init drbg_fill_array(struct rng_alg *alg, conststruct drbg_core *core, int pr)
{ int pos = 0; staticint priority = 200;
alg->base.cra_priority = priority;
priority++; /* * If FIPS mode enabled, the selected DRBG shall have the * highest cra_priority over other stdrng instances to ensure * it is selected.
*/ if (fips_enabled)
alg->base.cra_priority += 200;
/* * each DRBG definition can be used with PR and without PR, thus * we instantiate each DRBG in drbg_cores[] twice. * * As the order of placing them into the drbg_algs array matters * (the later DRBGs receive a higher cra_priority) we register the * prediction resistance DRBGs first as the should not be too * interesting.
*/ for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1); for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0); return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.