All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: James Bottomley <James.Bottomley@hansenpartnership.com>
Cc: linux-integrity@vger.kernel.org, keyrings@vger.kernel.org,
	Ard Biesheuvel <ardb@kernel.org>
Subject: Re: [PATCH 01/12] crypto: lib - implement library version of AES in CFB mode
Date: Mon, 27 Feb 2023 09:47:47 +0200	[thread overview]
Message-ID: <Y/xgIzTC854qhr+G@kernel.org> (raw)
In-Reply-To: <20230216201410.15010-2-James.Bottomley@HansenPartnership.com>

On Thu, Feb 16, 2023 at 03:13:59PM -0500, James Bottomley wrote:
> From: Ard Biesheuvel <ardb@kernel.org>
> 
> Implement AES in CFB mode using the existing, mostly constant-time
> generic AES library implementation.
> 
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
> ---
>  include/crypto/aes.h |  5 +++
>  lib/crypto/Kconfig   |  5 +++
>  lib/crypto/Makefile  |  3 ++
>  lib/crypto/aescfb.c  | 75 ++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 88 insertions(+)
>  create mode 100644 lib/crypto/aescfb.c
> 
> diff --git a/include/crypto/aes.h b/include/crypto/aes.h
> index 2090729701ab..7b9e1df1ccb0 100644
> --- a/include/crypto/aes.h
> +++ b/include/crypto/aes.h
> @@ -87,4 +87,9 @@ void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in);
>  extern const u8 crypto_aes_sbox[];
>  extern const u8 crypto_aes_inv_sbox[];
>  
> +void aescfb_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
> +		    int len, const u8 *iv);
> +void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
> +		    int len, const u8 *iv);
> +
>  #endif
> diff --git a/lib/crypto/Kconfig b/lib/crypto/Kconfig
> index 45436bfc6dff..b01253cac70a 100644
> --- a/lib/crypto/Kconfig
> +++ b/lib/crypto/Kconfig
> @@ -8,6 +8,11 @@ config CRYPTO_LIB_UTILS
>  config CRYPTO_LIB_AES
>  	tristate
>  
> +config CRYPTO_LIB_AESCFB
> +	tristate
> +	select CRYPTO_LIB_AES
> +	select CRYPTO_LIB_UTILS
> +
>  config CRYPTO_LIB_AESGCM
>  	tristate
>  	select CRYPTO_LIB_AES
> diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
> index 6ec2d4543d9c..33213a01aab1 100644
> --- a/lib/crypto/Makefile
> +++ b/lib/crypto/Makefile
> @@ -10,6 +10,9 @@ obj-$(CONFIG_CRYPTO_LIB_CHACHA_GENERIC)		+= libchacha.o
>  obj-$(CONFIG_CRYPTO_LIB_AES)			+= libaes.o
>  libaes-y					:= aes.o
>  
> +obj-$(CONFIG_CRYPTO_LIB_AESCFB)			+= libaescfb.o
> +libaescfb-y					:= aescfb.o
> +
>  obj-$(CONFIG_CRYPTO_LIB_AESGCM)			+= libaesgcm.o
>  libaesgcm-y					:= aesgcm.o
>  
> diff --git a/lib/crypto/aescfb.c b/lib/crypto/aescfb.c
> new file mode 100644
> index 000000000000..e9de1c6d874a
> --- /dev/null
> +++ b/lib/crypto/aescfb.c
> @@ -0,0 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Minimal library implementation of AES in CFB mode
> + *
> + * Copyright 2023 Google LLC
> + */
> +
> +#include <linux/module.h>
> +
> +#include <crypto/algapi.h>
> +#include <crypto/aes.h>
> +
> +#include <asm/irqflags.h>

I'd remove the newlines in-between.

> +
> +static void aescfb_encrypt_block(const struct crypto_aes_ctx *ctx, void *dst,
> +				 const void *src)
> +{
> +	unsigned long flags;
> +
> +	/*
> +	 * In AES-CFB, the AES encryption operates on known 'plaintext' (the IV
> +	 * and ciphertext), making it susceptible to timing attacks on the
> +	 * encryption key. The AES library already mitigates this risk to some
> +	 * extent by pulling the entire S-box into the caches before doing any
> +	 * substitutions, but this strategy is more effective when running with
> +	 * interrupts disabled.
> +	 */
> +	local_irq_save(flags);
> +	aes_encrypt(ctx, dst, src);
> +	local_irq_restore(flags);
> +}
> +
> +void aescfb_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
> +		    int len, const u8 *iv)
> +{
> +	while (len > 0) {
> +		u8 ks[AES_BLOCK_SIZE];
> +
> +		aescfb_encrypt_block(ctx, ks, iv);
> +		crypto_xor_cpy(dst, src, ks, min(len, AES_BLOCK_SIZE));
> +		iv = dst;
> +
> +		dst += AES_BLOCK_SIZE;
> +		src += AES_BLOCK_SIZE;
> +		len -= AES_BLOCK_SIZE;
> +	}
> +}
> +
> +void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
> +		    int len, const u8 *iv)
> +{
> +	u8 ks[2][AES_BLOCK_SIZE];
> +
> +	aescfb_encrypt_block(ctx, ks[0], iv);
> +
> +	for (int i = 0; len > 0; i ^= 1) {
> +		if (len > AES_BLOCK_SIZE)
> +			/*
> +			 * Generate the keystream for the next block before
> +			 * performing the XOR, as that may update in place and
> +			 * overwrite the ciphertext.
> +			 */
> +			aescfb_encrypt_block(ctx, ks[!i], src);
> +
> +		crypto_xor_cpy(dst, src, ks[i], min(len, AES_BLOCK_SIZE));
> +
> +		dst += AES_BLOCK_SIZE;
> +		src += AES_BLOCK_SIZE;
> +		len -= AES_BLOCK_SIZE;
> +	}
> +}
> +
> +MODULE_DESCRIPTION("Generic AES-CFB library");
> +MODULE_AUTHOR("Ard Biesheuvel <ardb@kernel.org>");
> +MODULE_LICENSE("GPL");
> -- 
> 2.35.3
> 

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

  reply	other threads:[~2023-02-27  7:47 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-16 20:13 [PATCH 00/12] add integrity and security to TPM2 transactions James Bottomley
2023-02-16 20:13 ` [PATCH 01/12] crypto: lib - implement library version of AES in CFB mode James Bottomley
2023-02-27  7:47   ` Jarkko Sakkinen [this message]
2023-02-16 20:14 ` [PATCH 02/12] tpm: move buffer handling from static inlines to real functions James Bottomley
2023-02-27  8:18   ` Jarkko Sakkinen
2023-02-16 20:14 ` [PATCH 03/12] tpm: add buffer handling for TPM2B types James Bottomley
2023-02-27  8:31   ` Jarkko Sakkinen
2023-03-28 19:42     ` James Bottomley
2023-02-16 20:14 ` [PATCH 04/12] tpm: add cursor based buffer functions for response parsing James Bottomley
2023-02-27  8:34   ` Jarkko Sakkinen
2023-02-16 20:14 ` [PATCH 05/12] tpm: add buffer function to point to returned parameters James Bottomley
2023-02-27  8:36   ` Jarkko Sakkinen
2023-02-16 20:14 ` [PATCH 06/12] tpm: export the context save and load commands James Bottomley
2023-02-27  8:37   ` Jarkko Sakkinen
2023-04-03 16:54     ` James Bottomley
2023-02-16 20:14 ` [PATCH 07/12] tpm: Add full HMAC and encrypt/decrypt session handling code James Bottomley
2023-02-16 23:37   ` kernel test robot
2023-02-17 11:30   ` kernel test robot
2023-02-17 14:22     ` James Bottomley
2023-02-17 14:23       ` Ard Biesheuvel
2023-02-16 20:14 ` [PATCH 08/12] tpm: add hmac checks to tpm2_pcr_extend() James Bottomley
2023-02-27  9:16   ` Jarkko Sakkinen
2023-02-16 20:14 ` [PATCH 09/12] tpm: add session encryption protection to tpm2_get_random() James Bottomley
2023-02-16 20:14 ` [PATCH 10/12] KEYS: trusted: Add session encryption protection to the seal/unseal path James Bottomley
2023-02-16 20:40 ` [PATCH 11/12] tpm: add the null key name as a sysfs export James Bottomley
2023-02-17 10:59   ` kernel test robot
2023-02-16 20:41 ` [PATCH 12/12] Documentation: add tpm-security.rst James Bottomley
2023-02-17 22:43 ` [PATCH 00/12] add integrity and security to TPM2 transactions Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y/xgIzTC854qhr+G@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=ardb@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.