Skip to content

[fips-9-compliant] Rebase Custom changes to 5.14.0-570.23.1.el9_6 #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,7 @@ config CRYPTO_ANSI_CPRNG
tristate "Pseudo Random Number Generation for Cryptographic modules"
select CRYPTO_AES
select CRYPTO_RNG
select CRYPTO_SHA3
help
This option enables the generic pseudo random number generator
for cryptographic modules. Uses the Algorithm specified in
Expand Down
3 changes: 1 addition & 2 deletions crypto/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
memset(alignbuffer, 0, keylen);
kfree(buffer);
kfree_sensitive(buffer);
return ret;
}

Expand Down
3 changes: 1 addition & 2 deletions crypto/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen);
memset(alignbuffer, 0, keylen);
kfree(buffer);
kfree_sensitive(buffer);
return ret;

}
Expand Down
6 changes: 6 additions & 0 deletions crypto/drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,12 @@ static inline int drbg_alloc_state(struct drbg_state *drbg)
if (ret < 0)
goto err;

/*
* Align to at least a cache line for better performance. This also
* prevents false sharing of cache lines between different instances.
*/
ret = max(ret, L1_CACHE_BYTES - 1);

drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
if (!drbg->Vbuf) {
ret = -ENOMEM;
Expand Down
33 changes: 33 additions & 0 deletions crypto/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <crypto/kpp.h>
#include <crypto/ecdh.h>
#include <linux/scatterlist.h>
#include <linux/fips.h>
#include "ecc.h"

struct ecdh_ctx {
Expand All @@ -33,6 +34,8 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
params.key_size > sizeof(u64) * ctx->ndigits)
return -EINVAL;

memset(ctx->private_key, 0, sizeof(ctx->private_key));

if (!params.key || !params.key_size)
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
ctx->private_key);
Expand Down Expand Up @@ -94,6 +97,36 @@ static int ecdh_compute_value(struct kpp_request *req)
ctx->private_key, public_key);
buf = public_key;
nbytes = public_key_sz;

/*
* SP800-56Arev3, 5.6.2.1.4: ("Owner Assurance of
* Pair-wise Consistency"): recompute the public key
* and check if the results match.
*/
if (fips_enabled) {
u64 *public_key_pct;

if (ret < 0)
goto free_all;

public_key_pct = kmalloc(public_key_sz, GFP_KERNEL);
if (!public_key_pct) {
ret = -ENOMEM;
goto free_all;
}

ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
ctx->private_key,
public_key_pct);
if (ret < 0) {
kfree(public_key_pct);
goto free_all;
}

if (memcmp(public_key, public_key_pct, public_key_sz))
panic("ECDH PCT failed in FIPS mode");
kfree(public_key_pct);
}
}

if (ret < 0)
Expand Down
9 changes: 6 additions & 3 deletions crypto/essiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
crypto_shash_update(desc, keys.enckey, keys.enckeylen) ?:
crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
if (err)
return err;
goto out;

crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
CRYPTO_TFM_REQ_MASK);
return crypto_cipher_setkey(tctx->essiv_cipher, salt,
crypto_shash_digestsize(tctx->hash));
err = crypto_cipher_setkey(tctx->essiv_cipher, salt,
crypto_shash_digestsize(tctx->hash));
out:
memzero_explicit(&keys, sizeof(keys));
return err;
}

static int essiv_aead_setauthsize(struct crypto_aead *tfm,
Expand Down
183 changes: 163 additions & 20 deletions crypto/jitterentropy-kcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Non-physical true random number generator based on timing jitter --
* Linux Kernel Crypto API specific code
*
* Copyright Stephan Mueller <[email protected]>, 2015
* Copyright Stephan Mueller <[email protected]>, 2015 - 2023
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -37,6 +37,8 @@
* DAMAGE.
*/

#include <crypto/hash.h>
#include <crypto/sha3.h>
#include <linux/fips.h>
#include <linux/kernel.h>
#include <linux/module.h>
Expand All @@ -46,6 +48,8 @@

#include "jitterentropy.h"

#define JENT_CONDITIONING_HASH "sha3-256-generic"

/***************************************************************************
* Helper function
***************************************************************************/
Expand All @@ -60,11 +64,6 @@ void jent_zfree(void *ptr)
kfree_sensitive(ptr);
}

void jent_memcpy(void *dest, const void *src, unsigned int n)
{
memcpy(dest, src, n);
}

/*
* Obtain a high-resolution time stamp value. The time stamp is used to measure
* the execution time of a given code path and its variations. Hence, the time
Expand All @@ -91,39 +90,174 @@ void jent_get_nstime(__u64 *out)
*out = tmp;
}

int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
unsigned int addtl_len, __u64 hash_loop_cnt,
unsigned int stuck)
{
struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
u8 intermediary[SHA3_256_DIGEST_SIZE];
__u64 j = 0;
int ret;

desc->tfm = hash_state_desc->tfm;

if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {
pr_warn_ratelimited("Unexpected digest size\n");
return -EINVAL;
}

/*
* This loop fills a buffer which is injected into the entropy pool.
* The main reason for this loop is to execute something over which we
* can perform a timing measurement. The injection of the resulting
* data into the pool is performed to ensure the result is used and
* the compiler cannot optimize the loop away in case the result is not
* used at all. Yet that data is considered "additional information"
* considering the terminology from SP800-90A without any entropy.
*
* Note, it does not matter which or how much data you inject, we are
* interested in one Keccack1600 compression operation performed with
* the crypto_shash_final.
*/
for (j = 0; j < hash_loop_cnt; j++) {
ret = crypto_shash_init(desc) ?:
crypto_shash_update(desc, intermediary,
sizeof(intermediary)) ?:
crypto_shash_finup(desc, addtl, addtl_len, intermediary);
if (ret)
goto err;
}

/*
* Inject the data from the previous loop into the pool. This data is
* not considered to contain any entropy, but it stirs the pool a bit.
*/
ret = crypto_shash_update(desc, intermediary, sizeof(intermediary));
if (ret)
goto err;

/*
* Insert the time stamp into the hash context representing the pool.
*
* If the time stamp is stuck, do not finally insert the value into the
* entropy pool. Although this operation should not do any harm even
* when the time stamp has no entropy, SP800-90B requires that any
* conditioning operation to have an identical amount of input data
* according to section 3.1.5.
*/
if (!stuck) {
ret = crypto_shash_update(hash_state_desc, (u8 *)&time,
sizeof(__u64));
}

err:
shash_desc_zero(desc);
memzero_explicit(intermediary, sizeof(intermediary));

return ret;
}

int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
{
struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
u8 jent_block[SHA3_256_DIGEST_SIZE];
/* Obtain data from entropy pool and re-initialize it */
int ret = crypto_shash_final(hash_state_desc, jent_block) ?:
crypto_shash_init(hash_state_desc) ?:
crypto_shash_update(hash_state_desc, jent_block,
sizeof(jent_block));

if (!ret && dst_len)
memcpy(dst, jent_block, dst_len);

memzero_explicit(jent_block, sizeof(jent_block));
return ret;
}

/***************************************************************************
* Kernel crypto API interface
***************************************************************************/

struct jitterentropy {
spinlock_t jent_lock;
struct rand_data *entropy_collector;
struct crypto_shash *tfm;
struct shash_desc *sdesc;
};

static int jent_kcapi_init(struct crypto_tfm *tfm)
static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
{
struct jitterentropy *rng = crypto_tfm_ctx(tfm);
int ret = 0;

rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
if (!rng->entropy_collector)
ret = -ENOMEM;
spin_lock(&rng->jent_lock);

spin_lock_init(&rng->jent_lock);
return ret;
}
if (rng->sdesc) {
shash_desc_zero(rng->sdesc);
kfree(rng->sdesc);
}
rng->sdesc = NULL;

static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
{
struct jitterentropy *rng = crypto_tfm_ctx(tfm);
if (rng->tfm)
crypto_free_shash(rng->tfm);
rng->tfm = NULL;

spin_lock(&rng->jent_lock);
if (rng->entropy_collector)
jent_entropy_collector_free(rng->entropy_collector);
rng->entropy_collector = NULL;
spin_unlock(&rng->jent_lock);
}

static int jent_kcapi_init(struct crypto_tfm *tfm)
{
struct jitterentropy *rng = crypto_tfm_ctx(tfm);
struct crypto_shash *hash;
struct shash_desc *sdesc;
int size, ret = 0;

spin_lock_init(&rng->jent_lock);

/*
* Use SHA3-256 as conditioner. We allocate only the generic
* implementation as we are not interested in high-performance. The
* execution time of the SHA3 operation is measured and adds to the
* Jitter RNG's unpredictable behavior. If we have a slower hash
* implementation, the execution timing variations are larger. When
* using a fast implementation, we would need to call it more often
* as its variations are lower.
*/
hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
if (IS_ERR(hash)) {
pr_err("Cannot allocate conditioning digest\n");
return PTR_ERR(hash);
}
rng->tfm = hash;

size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
sdesc = kmalloc(size, GFP_KERNEL);
if (!sdesc) {
ret = -ENOMEM;
goto err;
}

sdesc->tfm = hash;
crypto_shash_init(sdesc);
rng->sdesc = sdesc;

rng->entropy_collector = jent_entropy_collector_alloc(1, 0, sdesc);
if (!rng->entropy_collector) {
ret = -ENOMEM;
goto err;
}

spin_lock_init(&rng->jent_lock);
return 0;

err:
jent_kcapi_cleanup(tfm);
return ret;
}

static int jent_kcapi_random(struct crypto_rng *tfm,
const u8 *src, unsigned int slen,
u8 *rdata, unsigned int dlen)
Expand Down Expand Up @@ -180,15 +314,24 @@ static struct rng_alg jent_alg = {
.cra_module = THIS_MODULE,
.cra_init = jent_kcapi_init,
.cra_exit = jent_kcapi_cleanup,

}
};

static int __init jent_mod_init(void)
{
SHASH_DESC_ON_STACK(desc, tfm);
struct crypto_shash *tfm;
int ret = 0;

ret = jent_entropy_init();
tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
if (IS_ERR(tfm))
return PTR_ERR(tfm);

desc->tfm = tfm;
crypto_shash_init(desc);
ret = jent_entropy_init(desc);
shash_desc_zero(desc);
crypto_free_shash(tfm);
if (ret) {
/* Handle permanent health test error */
if (fips_enabled)
Expand Down
Loading