Re: [PATCH] crypto: algif_aead - copy AAD from src to dst

2017-08-09 Thread Herbert Xu
On Sun, Jul 30, 2017 at 02:32:58PM +0200, Stephan Müller wrote:
> Use the NULL cipher to copy the AAD and PT/CT from the TX SGL
> to the RX SGL. This allows an in-place crypto operation on the
> RX SGL for encryption, because the TX data is always smaller or
> equal to the RX data (the RX data will hold the tag).
> 
> For decryption, a per-request TX SGL is created which will only hold
> the tag value. As the RX SGL will have no space for the tag value and
> an in-place operation will not write the tag buffer, the TX SGL with the
> tag value is chained to the RX SGL. This now allows an in-place
> crypto operation.
> 
> For example:
> 
> * without the patch:
> kcapi -x 2 -e -c "gcm(aes)" -p 89154d0d4129d322e4487bafaa4f6b46 -k 
> c0ece3e63198af382b5603331cc23fa8 -i 7e489b83622e7228314d878d -a 
> afcd7202d621e06ca53b70c2bdff7fb2 -l 16 -u -s
> f4a3eacfbdadd3b1a17117b1d67ffc1f1e21efbbc6d83724a8c296e3bb8cda0c
> 
> * with the patch:
> kcapi -x 2 -e -c "gcm(aes)" -p 89154d0d4129d322e4487bafaa4f6b46 -k 
> c0ece3e63198af382b5603331cc23fa8 -i 7e489b83622e7228314d878d -a 
> afcd7202d621e06ca53b70c2bdff7fb2 -l 16 -u -s
> afcd7202d621e06ca53b70c2bdff7fb2f4a3eacfbdadd3b1a17117b1d67ffc1f1e21efbbc6d83724a8c296e3bb8cda0c
> 
> Tests covering this functionality have been added to libkcapi.
> 
> Signed-off-by: Stephan Mueller 

Patch applied.  Thanks.
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


[PATCH] crypto: algif_aead - copy AAD from src to dst

2017-07-30 Thread Stephan Müller
Use the NULL cipher to copy the AAD and PT/CT from the TX SGL
to the RX SGL. This allows an in-place crypto operation on the
RX SGL for encryption, because the TX data is always smaller or
equal to the RX data (the RX data will hold the tag).

For decryption, a per-request TX SGL is created which will only hold
the tag value. As the RX SGL will have no space for the tag value and
an in-place operation will not write the tag buffer, the TX SGL with the
tag value is chained to the RX SGL. This now allows an in-place
crypto operation.

For example:

* without the patch:
kcapi -x 2 -e -c "gcm(aes)" -p 89154d0d4129d322e4487bafaa4f6b46 -k 
c0ece3e63198af382b5603331cc23fa8 -i 7e489b83622e7228314d878d -a 
afcd7202d621e06ca53b70c2bdff7fb2 -l 16 -u -s
f4a3eacfbdadd3b1a17117b1d67ffc1f1e21efbbc6d83724a8c296e3bb8cda0c

* with the patch:
kcapi -x 2 -e -c "gcm(aes)" -p 89154d0d4129d322e4487bafaa4f6b46 -k 
c0ece3e63198af382b5603331cc23fa8 -i 7e489b83622e7228314d878d -a 
afcd7202d621e06ca53b70c2bdff7fb2 -l 16 -u -s
afcd7202d621e06ca53b70c2bdff7fb2f4a3eacfbdadd3b1a17117b1d67ffc1f1e21efbbc6d83724a8c296e3bb8cda0c

Tests covering this functionality have been added to libkcapi.

Signed-off-by: Stephan Mueller 
---
 crypto/Kconfig  |   2 +
 crypto/algif_aead.c | 183 +---
 2 files changed, 162 insertions(+), 23 deletions(-)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index caa770e535a2..0a121f9ddf8e 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -1753,6 +1753,8 @@ config CRYPTO_USER_API_AEAD
tristate "User-space interface for AEAD cipher algorithms"
depends on NET
select CRYPTO_AEAD
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_NULL
select CRYPTO_USER_API
help
  This option enables the user-spaces interface for AEAD
diff --git a/crypto/algif_aead.c b/crypto/algif_aead.c
index 2de056c3139c..1f0696dd64f4 100644
--- a/crypto/algif_aead.c
+++ b/crypto/algif_aead.c
@@ -30,6 +30,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -70,6 +72,7 @@ struct aead_async_req {
 struct aead_tfm {
struct crypto_aead *aead;
bool has_key;
+   struct crypto_skcipher *null_tfm;
 };
 
 struct aead_ctx {
@@ -168,7 +171,12 @@ static int aead_alloc_tsgl(struct sock *sk)
return 0;
 }
 
-static unsigned int aead_count_tsgl(struct sock *sk, size_t bytes)
+/**
+ * Count number of SG entries from the beginning of the SGL to @bytes. If
+ * an offset is provided, the counting of the SG entries starts at the offset.
+ */
+static unsigned int aead_count_tsgl(struct sock *sk, size_t bytes,
+   size_t offset)
 {
struct alg_sock *ask = alg_sk(sk);
struct aead_ctx *ctx = ask->private;
@@ -183,32 +191,55 @@ static unsigned int aead_count_tsgl(struct sock *sk, 
size_t bytes)
struct scatterlist *sg = sgl->sg;
 
for (i = 0; i < sgl->cur; i++) {
+   size_t bytes_count;
+
+   /* Skip offset */
+   if (offset >= sg[i].length) {
+   offset -= sg[i].length;
+   bytes -= sg[i].length;
+   continue;
+   }
+
+   bytes_count = sg[i].length - offset;
+
+   offset = 0;
sgl_count++;
-   if (sg[i].length >= bytes)
+
+   /* If we have seen requested number of bytes, stop */
+   if (bytes_count >= bytes)
return sgl_count;
 
-   bytes -= sg[i].length;
+   bytes -= bytes_count;
}
}
 
return sgl_count;
 }
 
+/**
+ * Release the specified buffers from TX SGL pointed to by ctx->tsgl_list for
+ * @used bytes.
+ *
+ * If @dst is non-null, reassign the pages to dst. The caller must release
+ * the pages. If @dst_offset is given only reassign the pages to @dst starting
+ * at the @dst_offset (byte). The caller must ensure that @dst is large
+ * enough (e.g. by using aead_count_tsgl with the same offset).
+ */
 static void aead_pull_tsgl(struct sock *sk, size_t used,
-  struct scatterlist *dst)
+  struct scatterlist *dst, size_t dst_offset)
 {
struct alg_sock *ask = alg_sk(sk);
struct aead_ctx *ctx = ask->private;
struct aead_tsgl *sgl;
struct scatterlist *sg;
-   unsigned int i;
+   unsigned int i, j;
 
while (!list_empty(&ctx->tsgl_list)) {
sgl = list_first_entry(&ctx->tsgl_list, struct aead_tsgl,
   list);
sg = sgl->sg;
 
-   for (i = 0; i < sgl->cur; i++) {
+   for (i = 0, j = 0; i < sgl->cur; i++) {
size_