Re: [RFC] [crypto] S390-AES add fallback driver.

2007-11-13 Thread Sebastian Siewior
* Jan Glauber | 2007-11-12 18:04:29 [+]:

Sebastian, thanks for working on this! Do you know if I need other
posted patches that are not yet in cryptodev-2.6 for this to work?
Nope I should work. I tested it on Herbert's cryptodev tree.

I'm asking becuase I'm getting the following crash using tcrypt (aes
192-bit key, ecb-mode) :(
Too bad it doesn't work out of the box :D

Call Trace:
(?02ee5680? 0x2ee5680)
 ?0001008292ae? crypto_ecb_setkey+0x52/0x74 ?ecb?
 ?00010082316e? setkey_fallback_blk+0x5e/0x98 ?aes_s390?
 ?000100886d76? test_cipher+0x2da/0x8f0 ?tcrypt?
 ?00010080570e? init+0x70e/0x1808 ?tcrypt?
 ?000674f4? sys_init_module+0x148/0x1e64
 ?000222f8? sysc_noemu+0x10/0x16
 ?0211ff6e? 0x211ff6e

From my limited understanding of the internals of crypto API I think
this is because crypto_ecb_setkey() calls crypto_cipher_setkey() instead
of crypto_blkcipher_setkey() and the layout of struct blkcipher_tfm
has the *iv where cipher_tfm has the setkey(). And oops, since the *iv
is zero we have a null pointer call. But maybe I'm just missing another 
patch...
Please send me (private if you prefer) a full log and I look into it.

thanks, Jan

Sebastian
-
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


Re: [RFC] [crypto] S390-AES add fallback driver.

2007-11-12 Thread Jan Glauber
On Sun, 2007-11-11 at 22:10 +0100, Sebastian Siewior wrote:
 Some CPUs support only 128 bit keys in HW. This patch adds SW fallback
 support for the other keys which may be required. The generic algorithm
 (and the block mode) must be availble in case of a fallback.
 
 Signed-off-by: Sebastian Siewior [EMAIL PROTECTED]
 ---
 
 Jan, please I didn't have the time to compile test. My compiler nagged
 only about the header file so it could work :)

Sebastian, thanks for working on this! Do you know if I need other
posted patches that are not yet in cryptodev-2.6 for this to work?

I'm asking becuase I'm getting the following crash using tcrypt (aes
192-bit key, ecb-mode) :(

Call Trace:
(Ý02ee5680¨ 0x2ee5680)
 Ý0001008292ae¨ crypto_ecb_setkey+0x52/0x74 Ýecb¨
 Ý00010082316e¨ setkey_fallback_blk+0x5e/0x98 Ýaes_s390¨
 Ý000100886d76¨ test_cipher+0x2da/0x8f0 Ýtcrypt¨
 Ý00010080570e¨ init+0x70e/0x1808 Ýtcrypt¨
 Ý000674f4¨ sys_init_module+0x148/0x1e64
 Ý000222f8¨ sysc_noemu+0x10/0x16
 Ý0211ff6e¨ 0x211ff6e

From my limited understanding of the internals of crypto API I think
this is because crypto_ecb_setkey() calls crypto_cipher_setkey() instead
of crypto_blkcipher_setkey() and the layout of struct blkcipher_tfm
has the *iv where cipher_tfm has the setkey(). And oops, since the *iv
is zero we have a null pointer call. But maybe I'm just missing another patch...

thanks, Jan


 diff --git a/arch/s390/crypto/aes_s390.c b/arch/s390/crypto/aes_s390.c
 index 812511b..393a450 100644
 --- a/arch/s390/crypto/aes_s390.c
 +++ b/arch/s390/crypto/aes_s390.c
 @@ -6,6 +6,7 @@
   * s390 Version:
   *   Copyright IBM Corp. 2005,2007
   *   Author(s): Jan Glauber ([EMAIL PROTECTED])
 + *   Sebastian Siewior ([EMAIL PROTECTED] SW-Fallback
   *
   * Derived from crypto/aes_generic.c
   *
 @@ -18,6 +19,7 @@
 
  #include crypto/aes.h
  #include crypto/algapi.h
 +#include linux/err.h
  #include linux/module.h
  #include linux/init.h
  #include crypt_s390.h
 @@ -34,45 +36,89 @@ struct s390_aes_ctx {
   long enc;
   long dec;
   int key_len;
 + union {
 + struct crypto_blkcipher *blk;
 + struct crypto_cipher *cip;
 + } fallback;
  };
 
 -static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
 -unsigned int key_len)
 +/*
 + * Check if the key_len is supported by the HW.
 + * Returns 0 if it is, a positive number if it is not and software fallback 
 is
 + * required or a negative number in case the key size is not valid
 + */
 +static int need_fallback(unsigned int key_len)
  {
 - struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 - u32 *flags = tfm-crt_flags;
 -
   switch (key_len) {
   case 16:
   if (!(keylen_flag  AES_KEYLEN_128))
 - goto fail;
 + return 1;
   break;
   case 24:
   if (!(keylen_flag  AES_KEYLEN_192))
 - goto fail;
 -
 + return 1;
   break;
   case 32:
   if (!(keylen_flag  AES_KEYLEN_256))
 - goto fail;
 + return 1;
   break;
   default:
 - goto fail;
 + return -1;
   break;
   }
 + return 0;
 +}
 +
 +static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
 + unsigned int key_len)
 +{
 + struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 + int ret;
 +
 + sctx-fallback.blk-base.crt_flags = ~CRYPTO_TFM_REQ_MASK;
 + sctx-fallback.blk-base.crt_flags |= (tfm-crt_flags 
 + CRYPTO_TFM_REQ_MASK);
 +
 + ret = crypto_cipher_setkey(sctx-fallback.cip, in_key, key_len);
 + if (ret) {
 + tfm-crt_flags = ~CRYPTO_TFM_RES_MASK;
 + tfm-crt_flags |= (sctx-fallback.blk-base.crt_flags 
 + CRYPTO_TFM_RES_MASK);
 + }
 + return ret;
 +}
 +
 +static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
 +unsigned int key_len)
 +{
 + struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 + u32 *flags = tfm-crt_flags;
 + int ret;
 +
 + ret = need_fallback(key_len);
 + if (ret  0) {
 + *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
 + return -EINVAL;
 + }
 
   sctx-key_len = key_len;
 - memcpy(sctx-key, in_key, key_len);
 - return 0;
 -fail:
 - *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
 - return -EINVAL;
 + if (!ret) {
 + memcpy(sctx-key, in_key, key_len);
 + return 0;
 + }
 +
 + return setkey_fallback_cip(tfm, in_key, key_len);
  }
 
  static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  {
   const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 
 + if (unlikely(need_fallback(sctx-key_len))) {
 + crypto_cipher_encrypt_one(sctx-fallback.cip, out, in);
 + return;
 + 

[RFC] [crypto] S390-AES add fallback driver.

2007-11-11 Thread Sebastian Siewior
Some CPUs support only 128 bit keys in HW. This patch adds SW fallback
support for the other keys which may be required. The generic algorithm
(and the block mode) must be availble in case of a fallback.

Signed-off-by: Sebastian Siewior [EMAIL PROTECTED]
---

Jan, please I didn't have the time to compile test. My compiler nagged
only about the header file so it could work :)

diff --git a/arch/s390/crypto/aes_s390.c b/arch/s390/crypto/aes_s390.c
index 812511b..393a450 100644
--- a/arch/s390/crypto/aes_s390.c
+++ b/arch/s390/crypto/aes_s390.c
@@ -6,6 +6,7 @@
  * s390 Version:
  *   Copyright IBM Corp. 2005,2007
  *   Author(s): Jan Glauber ([EMAIL PROTECTED])
+ * Sebastian Siewior ([EMAIL PROTECTED] SW-Fallback
  *
  * Derived from crypto/aes_generic.c
  *
@@ -18,6 +19,7 @@
 
 #include crypto/aes.h
 #include crypto/algapi.h
+#include linux/err.h
 #include linux/module.h
 #include linux/init.h
 #include crypt_s390.h
@@ -34,45 +36,89 @@ struct s390_aes_ctx {
long enc;
long dec;
int key_len;
+   union {
+   struct crypto_blkcipher *blk;
+   struct crypto_cipher *cip;
+   } fallback;
 };
 
-static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
-  unsigned int key_len)
+/*
+ * Check if the key_len is supported by the HW.
+ * Returns 0 if it is, a positive number if it is not and software fallback is
+ * required or a negative number in case the key size is not valid
+ */
+static int need_fallback(unsigned int key_len)
 {
-   struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
-   u32 *flags = tfm-crt_flags;
-
switch (key_len) {
case 16:
if (!(keylen_flag  AES_KEYLEN_128))
-   goto fail;
+   return 1;
break;
case 24:
if (!(keylen_flag  AES_KEYLEN_192))
-   goto fail;
-
+   return 1;
break;
case 32:
if (!(keylen_flag  AES_KEYLEN_256))
-   goto fail;
+   return 1;
break;
default:
-   goto fail;
+   return -1;
break;
}
+   return 0;
+}
+
+static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
+   unsigned int key_len)
+{
+   struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
+   int ret;
+
+   sctx-fallback.blk-base.crt_flags = ~CRYPTO_TFM_REQ_MASK;
+   sctx-fallback.blk-base.crt_flags |= (tfm-crt_flags 
+   CRYPTO_TFM_REQ_MASK);
+
+   ret = crypto_cipher_setkey(sctx-fallback.cip, in_key, key_len);
+   if (ret) {
+   tfm-crt_flags = ~CRYPTO_TFM_RES_MASK;
+   tfm-crt_flags |= (sctx-fallback.blk-base.crt_flags 
+   CRYPTO_TFM_RES_MASK);
+   }
+   return ret;
+}
+
+static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
+  unsigned int key_len)
+{
+   struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
+   u32 *flags = tfm-crt_flags;
+   int ret;
+
+   ret = need_fallback(key_len);
+   if (ret  0) {
+   *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
+   return -EINVAL;
+   }
 
sctx-key_len = key_len;
-   memcpy(sctx-key, in_key, key_len);
-   return 0;
-fail:
-   *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
-   return -EINVAL;
+   if (!ret) {
+   memcpy(sctx-key, in_key, key_len);
+   return 0;
+   }
+
+   return setkey_fallback_cip(tfm, in_key, key_len);
 }
 
 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 {
const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 
+   if (unlikely(need_fallback(sctx-key_len))) {
+   crypto_cipher_encrypt_one(sctx-fallback.cip, out, in);
+   return;
+   }
+
switch (sctx-key_len) {
case 16:
crypt_s390_km(KM_AES_128_ENCRYPT, sctx-key, out, in,
@@ -93,6 +139,11 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, 
const u8 *in)
 {
const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
 
+   if (unlikely(need_fallback(sctx-key_len))) {
+   crypto_cipher_decrypt_one(sctx-fallback.cip, out, in);
+   return;
+   }
+
switch (sctx-key_len) {
case 16:
crypt_s390_km(KM_AES_128_DECRYPT, sctx-key, out, in,
@@ -109,7 +160,6 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, 
const u8 *in)
}
 }
 
-
 static struct crypto_alg aes_alg = {
.cra_name   =   aes,
.cra_driver_name=   aes-s390,
@@ -131,10 +181,71 @@ static struct crypto_alg aes_alg = {
}
 };
 
+static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
+   unsigned int len)
+{
+   struct s390_aes_ctx *sctx =