Skip to contentSkip to conversation

crypto: lib - implement library version of AES in CFB mode

PATCH In review Ard Biesheuvel 2 participants 17 messages Feb 17, 2023 – Mar 12, 2023

Subsystems (2)
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

PATCH v2 1/1 crypto: lib - implement library version of AES in CFB mode

Implement AES in CFB mode using the existing, mostly constant-time generic AES library implementation. This will be used by the TPM code to encrypt communications with TPM hardware, which is often a discrete component connected using sniffable wires or traces.

While a CFB template does exist, using a skcipher is a major pain for non-performance critical synchronous crypto where the algorithm is known at compile time and the data is in contiguous buffers with valid kernel virtual addresses.

---
v1 was sent out by James and is archived at the URL above

v2:
- add test cases and kerneldoc comments
- add memzero_explicit() calls to wipe the keystream buffers
- add module exports
- add James's Tb/Rb

 include/crypto/aes.h |   5 +
 lib/crypto/Kconfig   |   5 +
 lib/crypto/Makefile  |   3 +
 lib/crypto/aescfb.c  | 257 ++++++++++++++++++++
 4 files changed, 270 insertions(+)
include/crypto/aes.h +5 -0
index 2090729701ab6d7a..9339da7c20a8b54e 100644
@@ -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[AES_BLOCK_SIZE]);
+void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
+		    int len, const u8 iv[AES_BLOCK_SIZE]);
+
 #endif
lib/crypto/Kconfig +5 -0
index 45436bfc6dffe1db..b01253cac70a7499 100644
@@ -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
lib/crypto/Makefile +3 -0
index 6ec2d4543d9cad48..33213a01aab101e8 100644
@@ -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
 
lib/crypto/aescfb.c new file +257 -0

257 lines not on this page · raw message

Signature · 1 line
2.39.1
maintainer
#1 lore raw
Quoted 23 lines · Ard Biesheuvel

Implement AES in CFB mode using the existing, mostly constant-time generic AES library implementation. This will be used by the TPM code to encrypt communications with TPM hardware, which is often a discrete component connected using sniffable wires or traces.

While a CFB template does exist, using a skcipher is a major pain for non-performance critical synchronous crypto where the algorithm is known at compile time and the data is in contiguous buffers with valid kernel virtual addresses.

---
v1 was sent out by James and is archived at the URL above

v2:
- add test cases and kerneldoc comments
- add memzero_explicit() calls to wipe the keystream buffers
- add module exports
- add James's Tb/Rb

Quoted 5 lines
 include/crypto/aes.h |   5 +
 lib/crypto/Kconfig   |   5 +
 lib/crypto/Makefile  |   3 +
 lib/crypto/aescfb.c  | 257 ++++++++++++++++++++
 4 files changed, 270 insertions(+)

Could we remove the crypto/cfb.c implementation after this work is complete?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#2 lore raw
Quoted 25 lines · Herbert Xu
Quoted 23 lines · Ard Biesheuvel

Implement AES in CFB mode using the existing, mostly constant-time generic AES library implementation. This will be used by the TPM code to encrypt communications with TPM hardware, which is often a discrete component connected using sniffable wires or traces.

While a CFB template does exist, using a skcipher is a major pain for non-performance critical synchronous crypto where the algorithm is known at compile time and the data is in contiguous buffers with valid kernel virtual addresses.

---
v1 was sent out by James and is archived at the URL above

v2:
- add test cases and kerneldoc comments
- add memzero_explicit() calls to wipe the keystream buffers
- add module exports
- add James's Tb/Rb

Quoted 9 lines
Quoted 5 lines
 include/crypto/aes.h |   5 +
 lib/crypto/Kconfig   |   5 +
 lib/crypto/Makefile  |   3 +
 lib/crypto/aescfb.c  | 257 ++++++++++++++++++++
 4 files changed, 270 insertions(+)

Could we remove the crypto/cfb.c implementation after this work is complete?

We would still not have any in-tree users of cfb(aes) or any other cfb(*), so in that sense, yes.

However, skciphers can be called from user space, and we also rely on this template for the extended testing of the various cfb() hardware implementations that we have in the tree.

So the answer is no, I suppose. I would like to simplify it a bit, though - it is a bit more complicated than it needs to be.

maintainer
#3 lore raw
Quoted 10 lines · Ard Biesheuvel

We would still not have any in-tree users of cfb(aes) or any other cfb(*), so in that sense, yes.

However, skciphers can be called from user space, and we also rely on this template for the extended testing of the various cfb() hardware implementations that we have in the tree.

So the answer is no, I suppose. I would like to simplify it a bit, though - it is a bit more complicated than it needs to be.

Could we hold onto this for a little bit? I'd like to finally remove crypto_cipher, and in doing so I will add a virtual address interface (i.e., not sg) to skcipher like we do with scomp and shash.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#4 lore raw
Quoted 10 lines · Herbert Xu
Quoted 8 lines · Ard Biesheuvel

We would still not have any in-tree users of cfb(aes) or any other cfb(*), so in that sense, yes.

However, skciphers can be called from user space, and we also rely on this template for the extended testing of the various cfb() hardware implementations that we have in the tree.

Quoted 7 lines
Quoted 2 lines

So the answer is no, I suppose. I would like to simplify it a bit, though - it is a bit more complicated than it needs to be.

Could we hold onto this for a little bit? I'd like to finally remove crypto_cipher, and in doing so I will add a virtual address interface (i.e., not sg) to skcipher like we do with scomp and shash.

Does that mean you are bringing back blkcipher? I think that would the right thing to do tbh, although it might make sense to enhance skcipher (and aead) to support this.

Could we perhaps update struct skcipher_request so it can describe virtually mapped address ranges, but permit this only for synchronous implementations? Then, we could update the skcipher walker code to produce a single walk step covering the entire range, and just use the provided virtual addresses directly, rather than going through a mapping interface?

maintainer
#5 lore raw
Quoted 4 lines · Ard Biesheuvel

Does that mean you are bringing back blkcipher? I think that would the right thing to do tbh, although it might make sense to enhance skcipher (and aead) to support this.

I haven't gone into that kind of detail yet but my first impression is that it would be the analogue of shash and skcipher would simply wrap around it just like ahash wraps around shash.

Quoted 6 lines

Could we perhaps update struct skcipher_request so it can describe virtually mapped address ranges, but permit this only for synchronous implementations? Then, we could update the skcipher walker code to produce a single walk step covering the entire range, and just use the provided virtual addresses directly, rather than going through a mapping interface?

Since skcipher doesn't actually need to carry any state with it I'd like to avoid having an skcipher_request at all. So it would look pretty much like the existing crypto_cipher interface except with the addition of length and IV.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#6 lore raw
Quoted 18 lines · Herbert Xu
Quoted 4 lines · Ard Biesheuvel

Does that mean you are bringing back blkcipher? I think that would the right thing to do tbh, although it might make sense to enhance skcipher (and aead) to support this.

I haven't gone into that kind of detail yet but my first impression is that it would be the analogue of shash and skcipher would simply wrap around it just like ahash wraps around shash.

Quoted 6 lines

Could we perhaps update struct skcipher_request so it can describe virtually mapped address ranges, but permit this only for synchronous implementations? Then, we could update the skcipher walker code to produce a single walk step covering the entire range, and just use the provided virtual addresses directly, rather than going through a mapping interface?

Quoted 2 lines

Since skcipher doesn't actually need to carry any state with it I'd like to avoid having an skcipher_request at all.

Doesn't that depend on the implementation? It might have a >0 size request context size, no? Or do we just allocate that on the stack?

maintainer
#7 lore raw
Quoted 3 lines · Ard Biesheuvel

Doesn't that depend on the implementation? It might have a >0 size request context size, no? Or do we just allocate that on the stack?

Do you have a concrete example of something that needs this? Is this a temporary scratch buffer used only during computation?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#8 lore raw
Quoted 9 lines · Herbert Xu
Quoted 3 lines · Ard Biesheuvel

Doesn't that depend on the implementation? It might have a >0 size request context size, no? Or do we just allocate that on the stack?

Do you have a concrete example of something that needs this? Is this a temporary scratch buffer used only during computation?

Every call to crypto_skcipher_set_reqsize(), no?

maintainer
#9 lore raw
Quoted 2 lines · Ard Biesheuvel

Every call to crypto_skcipher_set_reqsize(), no?

We'd only convert the software implementations. But you're right there does seem to be a few users such as aria that demand a large amount of temp space. I'd be tempted to just leave them on skcipher.

In other cases such as ctr we can easily put the IV on the stack.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#10 lore raw
Quoted 11 lines · Herbert Xu
Quoted 2 lines · Ard Biesheuvel

Every call to crypto_skcipher_set_reqsize(), no?

We'd only convert the software implementations. But you're right there does seem to be a few users such as aria that demand a large amount of temp space. I'd be tempted to just leave them on skcipher.

In other cases such as ctr we can easily put the IV on the stack.

But why can't we make skcipher just a hybrid?

- make the scatterlist members in skcipher_request unions with virtual src and dst alternatives
- add an API that assigns those alternative members and checks that the tfm is not ALG_ASYNC
- make the existing skcipher_en/decrypt() implementations check the request type, and hand off to a 'sync' alternative that allocates the request ctx on the stack, and make the accessor return the stack version instead of the heap version
- update skcipher_walk_xxx() to return the virtually addressable dst and src if the sync request type is encountered.

That way, the skcipher implementations can remain as they are, and the callers can just put a struct skcipher_request on the stack (without the padding and ctx overhead) and call the new interface with virtual addresses.

That way, all the SYNC_SKCIPHER hacks can go, and we don't need yet another algo type.

That way, the implementations can remain the same,

maintainer
#11 lore raw
Quoted 2 lines · Ard Biesheuvel

That way, the implementations can remain the same,

That's like doing a house renovation and keeping the scaffold around forever :)

Yes I agree that it would save a little bit of work for now but all the implementations would have to carry this unnecessary walking code with them forever.

With a setup like ahash/shash the walking code disappears totally from the underlying implementations.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#12 lore raw
Quoted 8 lines · Herbert Xu
Quoted 2 lines · Ard Biesheuvel

That way, the implementations can remain the same,

That's like doing a house renovation and keeping the scaffold around forever :)

Quoted 7 lines

Yes I agree that it would save a little bit of work for now but all the implementations would have to carry this unnecessary walking code with them forever.

With a setup like ahash/shash the walking code disappears totally from the underlying implementations.

So we are basically going back to ablkcipher/blkcipher then? How about aead?

maintainer
#13 lore raw
Quoted 2 lines · Ard Biesheuvel

So we are basically going back to ablkcipher/blkcipher then? How about aead?

No I just dug up the old blkcipher code and it's based on SGs just like skcipher.

I went back to the beginning of git and we've only ever had an SG-based encryption interface. This would be the very first time that we've had this in the Crypto API.

Do we have any potential users for such an AEAD interface?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#14 lore raw
Quoted 5 lines · Herbert Xu
Quoted 2 lines · Ard Biesheuvel

So we are basically going back to ablkcipher/blkcipher then? How about aead?

Quoted 9 lines

No I just dug up the old blkcipher code and it's based on SGs just like skcipher.

I went back to the beginning of git and we've only ever had an SG-based encryption interface. This would be the very first time that we've had this in the Crypto API.

Do we have any potential users for such an AEAD interface?

Not sure. I just added the libaesgcm library interface, but that is used in SMP secondary bringup, so that shouldn't use the crypto API in any case.

Synchronous AEADs are being used in the wifi code, but I am not aware of any problematic use cases.

So what use case is the driver for this sync skcipher change? And how will this work with existing templates? Do they all have to implement two flavors now?

maintainer
#15 lore raw
Quoted 2 lines · Ard Biesheuvel

So what use case is the driver for this sync skcipher change? And how

The main reason I wanted to do this is because I'd like to get rid of crypto_cipher. I'm planning on replacing the underlying simple ciphers with their ECB equivalent.

Quoted 2 lines

will this work with existing templates? Do they all have to implement two flavors now?

Let's say we're calling this vkcipher. Because the existing skcipher templates should continue to work with an underlying vkcipher algorithm, I won't be adding any vkcipher template unless there is a specific use-case, such as CFB here.

But I will do the common ones like CBC/CTR.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#16 lore raw
Quoted 12 lines · Herbert Xu
Quoted 2 lines · Ard Biesheuvel

So what use case is the driver for this sync skcipher change? And how

The main reason I wanted to do this is because I'd like to get rid of crypto_cipher. I'm planning on replacing the underlying simple ciphers with their ECB equivalent.

Quoted 2 lines

will this work with existing templates? Do they all have to implement two flavors now?

Quoted 7 lines

Let's say we're calling this vkcipher. Because the existing skcipher templates should continue to work with an underlying vkcipher algorithm, I won't be adding any vkcipher template unless there is a specific use-case, such as CFB here.

But I will do the common ones like CBC/CTR.

Interesting. So I think having an interface like this would be useful.

However, to answer your original question, I don't think it makes sense for James's stuff to be gated on this. In fact, I think communication with the TPM should have as few moving parts as possible, given how disruptive it might be if it fails, so I'd suggest we merge this code in any case, and stick to simple library interfaces where we can.