[CRYPTO] padlock: Convert padlock-sha to use crypto_hash
[linux-3.10.git] / drivers / crypto / padlock-sha.c
index f7010038033beb826fddcad08d92488921c3a1fb..a781fd23b607d805038f72078c4a56a010f9cfd5 100644 (file)
  *
  */
 
+#include <crypto/algapi.h>
+#include <linux/err.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/errno.h>
-#include <linux/crypto.h>
 #include <linux/cryptohash.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
 #define SHA256_DIGEST_SIZE      32
 #define SHA256_HMAC_BLOCK_SIZE  64
 
-static char *sha1_fallback = SHA1_DEFAULT_FALLBACK;
-static char *sha256_fallback = SHA256_DEFAULT_FALLBACK;
-
-module_param(sha1_fallback, charp, 0644);
-module_param(sha256_fallback, charp, 0644);
-
-MODULE_PARM_DESC(sha1_fallback, "Fallback driver for SHA1. Default is "
-                SHA1_DEFAULT_FALLBACK);
-MODULE_PARM_DESC(sha256_fallback, "Fallback driver for SHA256. Default is "
-                SHA256_DEFAULT_FALLBACK);
-
 struct padlock_sha_ctx {
        char            *data;
        size_t          used;
        int             bypass;
        void (*f_sha_padlock)(const char *in, char *out, int count);
-       struct crypto_tfm *fallback_tfm;
+       struct hash_desc fallback;
 };
 
 static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm)
 {
-       return (struct padlock_sha_ctx *)(crypto_tfm_ctx(tfm));
+       return crypto_tfm_ctx(tfm);
 }
 
 /* We'll need aligned address on the stack */
@@ -65,14 +55,12 @@ static void padlock_sha_bypass(struct crypto_tfm *tfm)
        if (ctx(tfm)->bypass)
                return;
 
-       BUG_ON(!ctx(tfm)->fallback_tfm);
-
-       crypto_digest_init(ctx(tfm)->fallback_tfm);
+       crypto_hash_init(&ctx(tfm)->fallback);
        if (ctx(tfm)->data && ctx(tfm)->used) {
                struct scatterlist sg;
 
                sg_set_buf(&sg, ctx(tfm)->data, ctx(tfm)->used);
-               crypto_digest_update(ctx(tfm)->fallback_tfm, &sg, 1);
+               crypto_hash_update(&ctx(tfm)->fallback, &sg, sg.length);
        }
 
        ctx(tfm)->used = 0;
@@ -95,9 +83,8 @@ static void padlock_sha_update(struct crypto_tfm *tfm,
 
        if (unlikely(ctx(tfm)->bypass)) {
                struct scatterlist sg;
-               BUG_ON(!ctx(tfm)->fallback_tfm);
                sg_set_buf(&sg, (uint8_t *)data, length);
-               crypto_digest_update(ctx(tfm)->fallback_tfm, &sg, 1);
+               crypto_hash_update(&ctx(tfm)->fallback, &sg, length);
                return;
        }
 
@@ -112,7 +99,7 @@ static inline void padlock_output_block(uint32_t *src,
                *dst++ = swab32(*src++);
 }
 
-void padlock_do_sha1(const char *in, char *out, int count)
+static void padlock_do_sha1(const char *in, char *out, int count)
 {
        /* We can't store directly to *out as it may be unaligned. */
        /* BTW Don't reduce the buffer size below 128 Bytes!
@@ -133,7 +120,7 @@ void padlock_do_sha1(const char *in, char *out, int count)
        padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
 }
 
-void padlock_do_sha256(const char *in, char *out, int count)
+static void padlock_do_sha256(const char *in, char *out, int count)
 {
        /* We can't store directly to *out as it may be unaligned. */
        /* BTW Don't reduce the buffer size below 128 Bytes!
@@ -160,8 +147,7 @@ void padlock_do_sha256(const char *in, char *out, int count)
 static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
 {
        if (unlikely(ctx(tfm)->bypass)) {
-               BUG_ON(!ctx(tfm)->fallback_tfm);
-               crypto_digest_final(ctx(tfm)->fallback_tfm, out);
+               crypto_hash_final(&ctx(tfm)->fallback, out);
                ctx(tfm)->bypass = 0;
                return;
        }
@@ -172,8 +158,11 @@ static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
        ctx(tfm)->used = 0;
 }
 
-static int padlock_cra_init(struct crypto_tfm *tfm, const char *fallback_driver_name)
+static int padlock_cra_init(struct crypto_tfm *tfm)
 {
+       const char *fallback_driver_name = tfm->__crt_alg->cra_name;
+       struct crypto_hash *fallback_tfm;
+
        /* For now we'll allocate one page. This
         * could eventually be configurable one day. */
        ctx(tfm)->data = (char *)__get_free_page(GFP_KERNEL);
@@ -181,14 +170,17 @@ static int padlock_cra_init(struct crypto_tfm *tfm, const char *fallback_driver_
                return -ENOMEM;
 
        /* Allocate a fallback and abort if it failed. */
-       ctx(tfm)->fallback_tfm = crypto_alloc_tfm(fallback_driver_name, 0);
-       if (!ctx(tfm)->fallback_tfm) {
+       fallback_tfm = crypto_alloc_hash(fallback_driver_name, 0,
+                                        CRYPTO_ALG_ASYNC |
+                                        CRYPTO_ALG_NEED_FALLBACK);
+       if (IS_ERR(fallback_tfm)) {
                printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
                       fallback_driver_name);
                free_page((unsigned long)(ctx(tfm)->data));
-               return -ENOENT;
+               return PTR_ERR(fallback_tfm);
        }
 
+       ctx(tfm)->fallback.tfm = fallback_tfm;
        return 0;
 }
 
@@ -196,14 +188,14 @@ static int padlock_sha1_cra_init(struct crypto_tfm *tfm)
 {
        ctx(tfm)->f_sha_padlock = padlock_do_sha1;
 
-       return padlock_cra_init(tfm, sha1_fallback);
+       return padlock_cra_init(tfm);
 }
 
 static int padlock_sha256_cra_init(struct crypto_tfm *tfm)
 {
        ctx(tfm)->f_sha_padlock = padlock_do_sha256;
 
-       return padlock_cra_init(tfm, sha256_fallback);
+       return padlock_cra_init(tfm);
 }
 
 static void padlock_cra_exit(struct crypto_tfm *tfm)
@@ -213,16 +205,16 @@ static void padlock_cra_exit(struct crypto_tfm *tfm)
                ctx(tfm)->data = NULL;
        }
 
-       BUG_ON(!ctx(tfm)->fallback_tfm);
-       crypto_free_tfm(ctx(tfm)->fallback_tfm);
-       ctx(tfm)->fallback_tfm = NULL;
+       crypto_free_hash(ctx(tfm)->fallback.tfm);
+       ctx(tfm)->fallback.tfm = NULL;
 }
 
 static struct crypto_alg sha1_alg = {
        .cra_name               =       "sha1",
        .cra_driver_name        =       "sha1-padlock",
        .cra_priority           =       PADLOCK_CRA_PRIORITY,
-       .cra_flags              =       CRYPTO_ALG_TYPE_DIGEST,
+       .cra_flags              =       CRYPTO_ALG_TYPE_DIGEST |
+                                       CRYPTO_ALG_NEED_FALLBACK,
        .cra_blocksize          =       SHA1_HMAC_BLOCK_SIZE,
        .cra_ctxsize            =       sizeof(struct padlock_sha_ctx),
        .cra_module             =       THIS_MODULE,
@@ -243,7 +235,8 @@ static struct crypto_alg sha256_alg = {
        .cra_name               =       "sha256",
        .cra_driver_name        =       "sha256-padlock",
        .cra_priority           =       PADLOCK_CRA_PRIORITY,
-       .cra_flags              =       CRYPTO_ALG_TYPE_DIGEST,
+       .cra_flags              =       CRYPTO_ALG_TYPE_DIGEST |
+                                       CRYPTO_ALG_NEED_FALLBACK,
        .cra_blocksize          =       SHA256_HMAC_BLOCK_SIZE,
        .cra_ctxsize            =       sizeof(struct padlock_sha_ctx),
        .cra_module             =       THIS_MODULE,
@@ -262,29 +255,15 @@ static struct crypto_alg sha256_alg = {
 
 static void __init padlock_sha_check_fallbacks(void)
 {
-       static struct crypto_tfm *tfm_sha1, *tfm_sha256;
-
-       /* We'll try to allocate one TFM for each fallback
-        * to test that the modules are available. */
-       tfm_sha1 = crypto_alloc_tfm(sha1_fallback, 0);
-       if (!tfm_sha1) {
-               printk(KERN_WARNING PFX "Couldn't load fallback module for '%s'. Tried '%s'.\n",
-                      sha1_alg.cra_name, sha1_fallback);
-       } else {
-               printk(KERN_NOTICE PFX "Fallback for '%s' is driver '%s' (prio=%d)\n", sha1_alg.cra_name,
-                      crypto_tfm_alg_driver_name(tfm_sha1), crypto_tfm_alg_priority(tfm_sha1));
-               crypto_free_tfm(tfm_sha1);
-       }
-
-       tfm_sha256 = crypto_alloc_tfm(sha256_fallback, 0);
-       if (!tfm_sha256) {
-               printk(KERN_WARNING PFX "Couldn't load fallback module for '%s'. Tried '%s'.\n",
-                      sha256_alg.cra_name, sha256_fallback);
-       } else {
-               printk(KERN_NOTICE PFX "Fallback for '%s' is driver '%s' (prio=%d)\n", sha256_alg.cra_name,
-                      crypto_tfm_alg_driver_name(tfm_sha256), crypto_tfm_alg_priority(tfm_sha256));
-               crypto_free_tfm(tfm_sha256);
-       }
+       if (!crypto_has_hash("sha1", 0, CRYPTO_ALG_ASYNC |
+                                       CRYPTO_ALG_NEED_FALLBACK))
+               printk(KERN_WARNING PFX
+                      "Couldn't load fallback module for sha1.\n");
+
+       if (!crypto_has_hash("sha256", 0, CRYPTO_ALG_ASYNC |
+                                       CRYPTO_ALG_NEED_FALLBACK))
+               printk(KERN_WARNING PFX
+                      "Couldn't load fallback module for sha256.\n");
 }
 
 static int __init padlock_init(void)