Skip to contentSkip to conversation

crypto: aesni - make driver-gcm-aes-aesni helper a proper aead alg

PATCH In review Tadeusz Struk 3 participants 7 messages Jan 23, 2015 – Jan 26, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: aesni - make driver-gcm-aes-aesni helper a proper aead alg

Changed the __driver-gcm-aes-aesni to be a proper aead algorithm.

---
 arch/x86/crypto/aesni-intel_glue.c |   53 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 14 deletions(-)
arch/x86/crypto/aesni-intel_glue.c +39 -14
index 947c6bf..5544ad9 100644
@@ -890,15 +890,12 @@ out_free_ablkcipher:
 	return ret;
 }
 
-static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
-						   unsigned int key_len)
+static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
+				  unsigned int key_len)
 {
 	int ret = 0;
-	struct crypto_tfm *tfm = crypto_aead_tfm(parent);
-	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
-	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
-	struct aesni_rfc4106_gcm_ctx *child_ctx =
-                                 aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	struct crypto_tfm *tfm = crypto_aead_tfm(aead);
+	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
 	u8 *new_key_align, *new_key_mem = NULL;
 
 	if (key_len < 4) {
@@ -943,20 +940,29 @@ static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
 		goto exit;
 	}
 	ret = rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
-	memcpy(child_ctx, ctx, sizeof(*ctx));
 exit:
 	kfree(new_key_mem);
 	return ret;
 }
 
-/* This is the Integrity Check Value (aka the authentication tag length and can
- * be 8, 12 or 16 bytes long. */
-static int rfc4106_set_authsize(struct crypto_aead *parent,
-				unsigned int authsize)
+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
+			   unsigned int key_len)
 {
 	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
 
+	ret = common_rfc4106_set_key(parent, key, key_len);
+	if (!ret)
+		memcpy(child_ctx, ctx, sizeof(*ctx));
+	return ret;
+}
+
+static int common_rfc4106_set_authsize(struct crypto_aead *aead,
+				       unsigned int authsize)
+{
 	switch (authsize) {
 	case 8:
 	case 12:
@@ -965,11 +971,26 @@ static int rfc4106_set_authsize(struct crypto_aead *parent,
 	default:
 		return -EINVAL;
 	}
-	crypto_aead_crt(parent)->authsize = authsize;
-	crypto_aead_crt(cryptd_child)->authsize = authsize;
+	crypto_aead_crt(aead)->authsize = authsize;
 	return 0;
 }
 
+/* This is the Integrity Check Value (aka the authentication tag length and can
+ * be 8, 12 or 16 bytes long. */
+static int rfc4106_set_authsize(struct crypto_aead *parent,
+				unsigned int authsize)
+{
+	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
+	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	int ret;
+
+	ret = common_rfc4106_set_authsize(parent, authsize);
+	if (!ret)
+		ret = common_rfc4106_set_authsize(cryptd_child, authsize);
+
+	return ret;
+}
+
 static int rfc4106_encrypt(struct aead_request *req)
 {
 	int ret;
@@ -1366,8 +1387,12 @@ static struct crypto_alg aesni_algs[] = { {
 	.cra_module		= THIS_MODULE,
 	.cra_u = {
 		.aead = {
+			.setkey		= common_rfc4106_set_key,
+			.setauthsize	= common_rfc4106_set_authsize,
 			.encrypt	= __driver_rfc4106_encrypt,
 			.decrypt	= __driver_rfc4106_decrypt,
+			.ivsize		= 8,
+			.maxauthsize	= 16,
 		},
 	},
 }, {
#1 lore raw

Am Freitag, 23. Januar 2015, 14:33:57 schrieb Tadeusz Struk:

Hi Tadeusz,

Quoted 46 lines

Changed the __driver-gcm-aes-aesni to be a proper aead algorithm.

---
 arch/x86/crypto/aesni-intel_glue.c |   53

++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 14 deletions(-)

(quoted diff) +0 -0

b/arch/x86/crypto/aesni-intel_glue.c index 947c6bf..5544ad9 100644

arch/x86/crypto/aesni-intel_glue.c +4 -9
@@ -890,15 +890,12 @@ out_free_ablkcipher:
 	return ret;
 }
 
-static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
-						   unsigned int key_len)
+static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
+				  unsigned int key_len)
 {
 	int ret = 0;
-	struct crypto_tfm *tfm = crypto_aead_tfm(parent);
-	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
-	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
-	struct aesni_rfc4106_gcm_ctx *child_ctx =
-                                 aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	struct crypto_tfm *tfm = crypto_aead_tfm(aead);
+	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
 	u8 *new_key_align, *new_key_mem = NULL;
 
 	if (key_len < 4) {
@@ -943,20 +940,29 @@ static int rfc4106_set_key(struct crypto_aead *parent,
 const u8 *key, goto exit;
 	}
 	ret = rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
-	memcpy(child_ctx, ctx, sizeof(*ctx));
 exit:
 	kfree(new_key_mem);
 	return ret;
 }
 
-/* This is the Integrity Check Value (aka the authentication tag length and

can - * be 8, 12 or 16 bytes long. */

arch/x86/crypto/aesni-intel_glue.c +0 -2
-static int rfc4106_set_authsize(struct crypto_aead *parent,
-				unsigned int authsize)

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key, · line 957

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +6 -0
+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
+			   unsigned int key_len)
 {
 	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
 
+	ret = common_rfc4106_set_key(parent, key, key_len);

Shouldn't that one be crypto_aead_setkey, i.e using the regular crypto API instead of internal calls?

Quoted 23 lines
arch/x86/crypto/aesni-intel_glue.c +10 -2
+	if (!ret)
+		memcpy(child_ctx, ctx, sizeof(*ctx));
+	return ret;
+}
+
+static int common_rfc4106_set_authsize(struct crypto_aead *aead,
+				       unsigned int authsize)
+{
 	switch (authsize) {
 	case 8:
 	case 12:
@@ -965,11 +971,26 @@ static int rfc4106_set_authsize(struct crypto_aead
 *parent, default:
 		return -EINVAL;
 	}
-	crypto_aead_crt(parent)->authsize = authsize;
-	crypto_aead_crt(cryptd_child)->authsize = authsize;
+	crypto_aead_crt(aead)->authsize = authsize;
 	return 0;
 }
 
+/* This is the Integrity Check Value (aka the authentication tag length and

can + * be 8, 12 or 16 bytes long. */

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_authsize(struct crypto_aead *parent, · line 987

Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +8 -0
+static int rfc4106_set_authsize(struct crypto_aead *parent,
+				unsigned int authsize)
+{
+	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
+	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	int ret;
+
+	ret = common_rfc4106_set_authsize(parent, authsize);

Same here, shouldn't that one be crypto_aead_setauthsize?

Quoted 27 lines
arch/x86/crypto/aesni-intel_glue.c +10 -1
+	if (!ret)
+		ret = common_rfc4106_set_authsize(cryptd_child, authsize);
+
+	return ret;
+}
+
 static int rfc4106_encrypt(struct aead_request *req)
 {
 	int ret;
@@ -1366,8 +1387,12 @@ static struct crypto_alg aesni_algs[] = { {
 	.cra_module		= THIS_MODULE,
 	.cra_u = {
 		.aead = {
+			.setkey		= common_rfc4106_set_key,
+			.setauthsize	= common_rfc4106_set_authsize,
 			.encrypt	= __driver_rfc4106_encrypt,
 			.decrypt	= __driver_rfc4106_decrypt,
+			.ivsize		= 8,
+			.maxauthsize	= 16,
 		},
 	},
 }, {
 
--

To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Ciao
Stephan
#2 lore raw

Hi Stephan,

Quoted 9 lines · Stephan Mueller
Quoted 9 lines

+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,

Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +4 -0
+			   unsigned int key_len)
 {
 	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
Quoted 4 lines
Quoted 1 line

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key, · line 957

Quoted 1 line
arch/x86/crypto/aesni-intel_glue.c +1 -0
+	ret = common_rfc4106_set_key(parent, key, key_len);

Shouldn't that one be crypto_aead_setkey, i.e using the regular crypto API instead of internal calls?

No, I don't think so. I think that would create an infinite loop.

Quoted 10 lines
Quoted 8 lines

+static int rfc4106_set_authsize(struct crypto_aead *parent,

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_authsize(struct crypto_aead *parent, · line 987

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +7 -0
+				unsigned int authsize)
+{
+	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
+	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	int ret;
+
+	ret = common_rfc4106_set_authsize(parent, authsize);

Same here, shouldn't that one be crypto_aead_setauthsize?

Same here.

Thanks,
Tadeusz

maintainer
#3 lore raw
Quoted 11 lines · Tadeusz Struk

Hi Stephan,

Quoted 9 lines · Stephan Mueller
Quoted 9 lines

+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,

Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +4 -0
+			   unsigned int key_len)
 {
 	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key, · line 957

Quoted 1 line
arch/x86/crypto/aesni-intel_glue.c +1 -0
+	ret = common_rfc4106_set_key(parent, key, key_len);

Shouldn't that one be crypto_aead_setkey, i.e using the regular crypto API instead of internal calls?

No, I don't think so. I think that would create an infinite loop.

So why does it work for ablk_helper but not for aead?

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
#4 lore raw
Quoted 12 lines · Herbert Xu
Quoted 11 lines · Tadeusz Struk
Quoted 11 lines

Hi Stephan,

Quoted 9 lines · Stephan Mueller
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,

Quoted 8 lines
Quoted 8 lines
arch/x86/crypto/aesni-intel_glue.c +4 -0
+			   unsigned int key_len)
 {
 	struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key, · line 957

Quoted 1 line
arch/x86/crypto/aesni-intel_glue.c +1 -0
+	ret = common_rfc4106_set_key(parent, key, key_len);

Shouldn't that one be crypto_aead_setkey, i.e using the regular crypto API instead of internal calls?

No, I don't think so. I think that would create an infinite loop.

So why does it work for ablk_helper but not for aead?

Here we have two instances of crypto_aead algorithm, one the rfc4106(gcm(aes)), whose setkey points to rfc4106_set_key(), and the internal helper __gcm-aes-aesni (wrapped in by the cryptd interface), whose setkey points to common_rfc4106_set_key(). If we would call crypto_aead_setkey() on the parent from rfc4106_set_key() then we would invoke the same rfc4106_set_key() function. It would be ok to call the crypto_aead_setkey() on the child, but what's the point? What we really want to do is to setup the context (authsize and key) for both the top level rfc4106(gcm(aes)) and the helper __gcm-aes-aesni. We can do it by calling the internal function directly or by the regular crypto API crypto_aead_setkey()/set_authsize() on the child, but I don't see any difference or benefit of it.
Hope that make sense.
Thanks,
Tadeusz

#5 lore raw

Am Montag, 26. Januar 2015, 08:58:33 schrieb Tadeusz Struk:

Hi Tadeusz,

Quoted 31 lines · Herbert Xu
Quoted 26 lines · Tadeusz Struk
Quoted 25 lines
Quoted 25 lines

Hi Stephan,

Quoted 21 lines · Stephan Mueller
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines

+static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,

Quoted 9 lines
Quoted 9 lines
(quoted diff) +1 -0
+			   unsigned int key_len)
 
 {
 
 	struct aesni_rfc4106_gcm_ctx *ctx =
 	aesni_rfc4106_gcm_ctx_get(parent);
 	struct crypto_aead *cryptd_child =
 	cryptd_aead_child(ctx->cryptd_tfm);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

arch/x86/crypto/aesni-intel_glue.c static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key, · line 957

Quoted 5 lines
arch/x86/crypto/aesni-intel_glue.c +4 -0
+	struct aesni_rfc4106_gcm_ctx *child_ctx =
+		aesni_rfc4106_gcm_ctx_get(cryptd_child);
+	int ret;
 
+	ret = common_rfc4106_set_key(parent, key, key_len);

Shouldn't that one be crypto_aead_setkey, i.e using the regular crypto API
instead of internal calls?

Quoted 3 lines
Quoted 1 line
Quoted 1 line

No, I don't think so. I think that would create an infinite loop.

So why does it work for ablk_helper but not for aead?

Quoted 7 lines

Here we have two instances of crypto_aead algorithm, one the rfc4106(gcm(aes)), whose setkey points to rfc4106_set_key(), and the internal helper __gcm-aes-aesni (wrapped in by the cryptd interface), whose setkey points to common_rfc4106_set_key(). If we would call crypto_aead_setkey() on the parent from rfc4106_set_key() then we would invoke the same rfc4106_set_key() function. It would be ok to call the crypto_aead_setkey() on the child, but what's the point?

The point is to maintain an onion style framework that is coherent. All other ciphers implement it (look at the generic gcm.c).

Quoted 5 lines

What we really want to do is to setup the context (authsize and key) for both the top level rfc4106(gcm(aes)) and the helper __gcm-aes-aesni. We can do it by calling the internal function directly or by the regular crypto API crypto_aead_setkey()/set_authsize() on the child, but I don't see any difference or benefit of it.

Then, why do we register the internal __driver "cipher" at all. On the one hand, the kernel crypto API is used for some aspects but not for others. Hm...

Quoted 7 lines

Hope that make sense.
Thanks,
Tadeusz
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Ciao
Stephan
#6 lore raw
Quoted 10 lines · Stephan Mueller
Quoted 7 lines
Quoted 7 lines

Here we have two instances of crypto_aead algorithm, one the rfc4106(gcm(aes)), whose setkey points to rfc4106_set_key(), and the internal helper __gcm-aes-aesni (wrapped in by the cryptd interface), whose setkey points to common_rfc4106_set_key(). If we would call crypto_aead_setkey() on the parent from rfc4106_set_key() then we would invoke the same rfc4106_set_key() function. It would be ok to call the crypto_aead_setkey() on the child, but what's the point?

The point is to maintain an onion style framework that is coherent. All other ciphers implement it (look at the generic gcm.c).

Hi Stephan,
Ok, to keep it consistent is a good enough reason. I'll send v2 soon. Thanks,
Tadeusz