[PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-08 Thread Jussi Kivilinna
Patch adds support for NIST recommended block cipher mode CMAC to CryptoAPI.

This work is based on Tom St Denis' earlier patch,
 http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2

Cc: Tom St Denis 
Signed-off-by: Jussi Kivilinna 
---
 crypto/Kconfig   |   11 ++
 crypto/Makefile  |1 
 crypto/cmac.c|  315 ++
 crypto/tcrypt.c  |   11 ++
 crypto/testmgr.c |   18 +++
 crypto/testmgr.h |  125 +
 6 files changed, 480 insertions(+), 1 deletion(-)
 create mode 100644 crypto/cmac.c

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 6cc27f1..c1142f3 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -283,6 +283,17 @@ config CRYPTO_XTS
 
 comment "Hash modes"
 
+config CRYPTO_CMAC
+   tristate "CMAC support"
+   select CRYPTO_HASH
+   select CRYPTO_MANAGER
+   help
+ Cipher-based Message Authentication Code (CMAC) specified by
+ The National Institute of Standards and Technology (NIST).
+
+ https://tools.ietf.org/html/rfc4493
+ http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf
+
 config CRYPTO_HMAC
tristate "HMAC support"
select CRYPTO_HASH
diff --git a/crypto/Makefile b/crypto/Makefile
index be1a1be..a8e9b0f 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -32,6 +32,7 @@ cryptomgr-y := algboss.o testmgr.o
 
 obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o
 obj-$(CONFIG_CRYPTO_USER) += crypto_user.o
+obj-$(CONFIG_CRYPTO_CMAC) += cmac.o
 obj-$(CONFIG_CRYPTO_HMAC) += hmac.o
 obj-$(CONFIG_CRYPTO_VMAC) += vmac.o
 obj-$(CONFIG_CRYPTO_XCBC) += xcbc.o
diff --git a/crypto/cmac.c b/crypto/cmac.c
new file mode 100644
index 000..50880cf
--- /dev/null
+++ b/crypto/cmac.c
@@ -0,0 +1,315 @@
+/*
+ * CMAC: Cipher Block Mode for Authentication
+ *
+ * Copyright © 2013 Jussi Kivilinna 
+ *
+ * Based on work by:
+ *  Copyright © 2013 Tom St Denis 
+ * Based on crypto/xcbc.c:
+ *  Copyright © 2006 USAGI/WIDE Project,
+ *   Author: Kazunori Miyazawa 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * +
+ * | 
+ * +
+ * | cmac_tfm_ctx
+ * +
+ * | consts (block size * 2)
+ * +
+ */
+struct cmac_tfm_ctx {
+   struct crypto_cipher *child;
+   u8 ctx[];
+};
+
+/*
+ * +
+ * | 
+ * +
+ * | cmac_desc_ctx
+ * +
+ * | odds (block size)
+ * +
+ * | prev (block size)
+ * +
+ */
+struct cmac_desc_ctx {
+   unsigned int len;
+   u8 ctx[];
+};
+
+static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
+const u8 *inkey, unsigned int keylen)
+{
+   unsigned long alignmask = crypto_shash_alignmask(parent);
+   struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
+   unsigned int bs = crypto_shash_blocksize(parent);
+   __be64 *consts = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
+   u64 _const[2];
+   int i, err = 0;
+   u8 msb_mask, gfmask;
+
+   err = crypto_cipher_setkey(ctx->child, inkey, keylen);
+   if (err)
+   return err;
+
+   /* encrypt the zero block */
+   memset(consts, 0, bs);
+   crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
+
+   switch (bs) {
+   case 16:
+   gfmask = 0x87;
+   _const[0] = be64_to_cpu(consts[1]);
+   _const[1] = be64_to_cpu(consts[0]);
+
+   /* gf(2^128) multiply zero-ciphertext with u and u^2 */
+   for (i = 0; i < 4; i += 2) {
+   msb_mask = ((s64)_const[1] >> 63) & gfmask;
+   _const[1] = (_const[1] << 1) | (_const[0] >> 63);
+   _const[0] = (_const[0] << 1) ^ msb_mask;
+
+   consts[i + 0] = cpu_to_be64(_const[1]);
+   consts[i + 1] = cpu_to_be64(_const[0]);
+   }
+
+   break;
+   case 8:
+   gfmask = 0x1B;
+   _const[0] = be64_to_cpu(consts[0]);
+
+   /* gf(2^64) multiply zero-ciphertext with u and u^2 */
+   for (i = 0; i < 2; i++) {
+   msb_mask = ((s64)_const[0] >> 63) & gfmask;
+   _const[0] = (_const[0] << 1) ^ msb_mask;
+
+   consts[i] = cpu_to_be64(_const[0]);
+   }
+
+   break;
+   }
+
+   return 0;
+}
+
+static int crypto_cmac_digest_init(struct shash_desc *pdesc)
+{
+   unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
+   struct cmac_desc_ctx *ctx = shash_desc_ct

Re: [PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-08 Thread Steffen Klassert
On Mon, Apr 08, 2013 at 10:48:44AM +0300, Jussi Kivilinna wrote:
> Patch adds support for NIST recommended block cipher mode CMAC to CryptoAPI.
> 
> This work is based on Tom St Denis' earlier patch,
>  http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2
> 
> Cc: Tom St Denis 
> Signed-off-by: Jussi Kivilinna 

This patch does not apply clean to the ipsec-next tree
because of some crypto changes I don't have in ipsec-next.
The IPsec part should apply to the cryptodev tree,
so it's probaply the best if we route this patchset
through the cryptodev tree.

Herbert,

are you going to take these patches?
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-08 Thread Jussi Kivilinna
On 08.04.2013 11:24, Steffen Klassert wrote:
> On Mon, Apr 08, 2013 at 10:48:44AM +0300, Jussi Kivilinna wrote:
>> Patch adds support for NIST recommended block cipher mode CMAC to CryptoAPI.
>>
>> This work is based on Tom St Denis' earlier patch,
>>  http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2
>>
>> Cc: Tom St Denis 
>> Signed-off-by: Jussi Kivilinna 
> 
> This patch does not apply clean to the ipsec-next tree
> because of some crypto changes I don't have in ipsec-next.
> The IPsec part should apply to the cryptodev tree,
> so it's probaply the best if we route this patchset
> through the cryptodev tree.

I should have mentioned that the patchset is on top of cryptodev tree and
previous crypto patches that I send yesterday, likely to cause problems
atleast at tcrypt.c:

http://marc.info/?l=linux-crypto-vger&m=136534223503368&w=2

-Jussi

> 
> Herbert,
> 
> are you going to take these patches?
> 

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


Re: [PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-08 Thread Herbert Xu
On Mon, Apr 08, 2013 at 10:24:16AM +0200, Steffen Klassert wrote:
> On Mon, Apr 08, 2013 at 10:48:44AM +0300, Jussi Kivilinna wrote:
> > Patch adds support for NIST recommended block cipher mode CMAC to CryptoAPI.
> > 
> > This work is based on Tom St Denis' earlier patch,
> >  http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2
> > 
> > Cc: Tom St Denis 
> > Signed-off-by: Jussi Kivilinna 
> 
> This patch does not apply clean to the ipsec-next tree
> because of some crypto changes I don't have in ipsec-next.
> The IPsec part should apply to the cryptodev tree,
> so it's probaply the best if we route this patchset
> through the cryptodev tree.
> 
> Herbert,
> 
> are you going to take these patches?

Sure I can do that.

Cheers,
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-08 Thread David Miller
From: Herbert Xu 
Date: Mon, 8 Apr 2013 17:33:40 +0800

> On Mon, Apr 08, 2013 at 10:24:16AM +0200, Steffen Klassert wrote:
>> On Mon, Apr 08, 2013 at 10:48:44AM +0300, Jussi Kivilinna wrote:
>> > Patch adds support for NIST recommended block cipher mode CMAC to 
>> > CryptoAPI.
>> > 
>> > This work is based on Tom St Denis' earlier patch,
>> >  http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2
>> > 
>> > Cc: Tom St Denis 
>> > Signed-off-by: Jussi Kivilinna 
>> 
>> This patch does not apply clean to the ipsec-next tree
>> because of some crypto changes I don't have in ipsec-next.
>> The IPsec part should apply to the cryptodev tree,
>> so it's probaply the best if we route this patchset
>> through the cryptodev tree.
>> 
>> Herbert,
>> 
>> are you going to take these patches?
> 
> Sure I can do that.

I'm fine with this:

Acked-by: David S. Miller 
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/2] crypto: add CMAC support to CryptoAPI

2013-04-09 Thread Herbert Xu
On Mon, Apr 08, 2013 at 10:48:44AM +0300, Jussi Kivilinna wrote:
> Patch adds support for NIST recommended block cipher mode CMAC to CryptoAPI.
> 
> This work is based on Tom St Denis' earlier patch,
>  http://marc.info/?l=linux-crypto-vger&m=135877306305466&w=2
> 
> Cc: Tom St Denis 
> Signed-off-by: Jussi Kivilinna 

Both patches applied.
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html