提交 7d024608 编写于 作者: H Herbert Xu

crypto: padlock - Use shash fallback for sha

This patch changes padlock sha fallback to shash instead of hash.
Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
上级 f63559be
...@@ -12,28 +12,24 @@ ...@@ -12,28 +12,24 @@
* *
*/ */
#include <crypto/algapi.h> #include <crypto/internal/hash.h>
#include <crypto/sha.h> #include <crypto/sha.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/cryptohash.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/scatterlist.h> #include <linux/scatterlist.h>
#include <asm/i387.h> #include <asm/i387.h>
#include "padlock.h" #include "padlock.h"
#define SHA1_DEFAULT_FALLBACK "sha1-generic"
#define SHA256_DEFAULT_FALLBACK "sha256-generic"
struct padlock_sha_ctx { struct padlock_sha_ctx {
char *data; char *data;
size_t used; size_t used;
int bypass; int bypass;
void (*f_sha_padlock)(const char *in, char *out, int count); void (*f_sha_padlock)(const char *in, char *out, int count);
struct hash_desc fallback; struct shash_desc *fallback;
}; };
static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm) static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm)
...@@ -47,21 +43,26 @@ static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm) ...@@ -47,21 +43,26 @@ static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm)
static struct crypto_alg sha1_alg, sha256_alg; static struct crypto_alg sha1_alg, sha256_alg;
static void padlock_sha_bypass(struct crypto_tfm *tfm) static int padlock_sha_bypass(struct crypto_tfm *tfm)
{ {
int err = 0;
if (ctx(tfm)->bypass) if (ctx(tfm)->bypass)
return; goto out;
crypto_hash_init(&ctx(tfm)->fallback); err = crypto_shash_init(ctx(tfm)->fallback);
if (ctx(tfm)->data && ctx(tfm)->used) { if (err)
struct scatterlist sg; goto out;
sg_init_one(&sg, ctx(tfm)->data, ctx(tfm)->used); if (ctx(tfm)->data && ctx(tfm)->used)
crypto_hash_update(&ctx(tfm)->fallback, &sg, sg.length); err = crypto_shash_update(ctx(tfm)->fallback, ctx(tfm)->data,
} ctx(tfm)->used);
ctx(tfm)->used = 0; ctx(tfm)->used = 0;
ctx(tfm)->bypass = 1; ctx(tfm)->bypass = 1;
out:
return err;
} }
static void padlock_sha_init(struct crypto_tfm *tfm) static void padlock_sha_init(struct crypto_tfm *tfm)
...@@ -73,15 +74,18 @@ static void padlock_sha_init(struct crypto_tfm *tfm) ...@@ -73,15 +74,18 @@ static void padlock_sha_init(struct crypto_tfm *tfm)
static void padlock_sha_update(struct crypto_tfm *tfm, static void padlock_sha_update(struct crypto_tfm *tfm,
const uint8_t *data, unsigned int length) const uint8_t *data, unsigned int length)
{ {
int err;
/* Our buffer is always one page. */ /* Our buffer is always one page. */
if (unlikely(!ctx(tfm)->bypass && if (unlikely(!ctx(tfm)->bypass &&
(ctx(tfm)->used + length > PAGE_SIZE))) (ctx(tfm)->used + length > PAGE_SIZE))) {
padlock_sha_bypass(tfm); err = padlock_sha_bypass(tfm);
BUG_ON(err);
}
if (unlikely(ctx(tfm)->bypass)) { if (unlikely(ctx(tfm)->bypass)) {
struct scatterlist sg; err = crypto_shash_update(ctx(tfm)->fallback, data, length);
sg_init_one(&sg, (uint8_t *)data, length); BUG_ON(err);
crypto_hash_update(&ctx(tfm)->fallback, &sg, length);
return; return;
} }
...@@ -151,8 +155,11 @@ static void padlock_do_sha256(const char *in, char *out, int count) ...@@ -151,8 +155,11 @@ static void padlock_do_sha256(const char *in, char *out, int count)
static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out) static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
{ {
int err;
if (unlikely(ctx(tfm)->bypass)) { if (unlikely(ctx(tfm)->bypass)) {
crypto_hash_final(&ctx(tfm)->fallback, out); err = crypto_shash_final(ctx(tfm)->fallback, out);
BUG_ON(err);
ctx(tfm)->bypass = 0; ctx(tfm)->bypass = 0;
return; return;
} }
...@@ -166,27 +173,41 @@ static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out) ...@@ -166,27 +173,41 @@ static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
static int padlock_cra_init(struct crypto_tfm *tfm) static int padlock_cra_init(struct crypto_tfm *tfm)
{ {
const char *fallback_driver_name = tfm->__crt_alg->cra_name; const char *fallback_driver_name = tfm->__crt_alg->cra_name;
struct crypto_hash *fallback_tfm; struct crypto_shash *fallback_tfm;
int err = -ENOMEM;
/* For now we'll allocate one page. This /* For now we'll allocate one page. This
* could eventually be configurable one day. */ * could eventually be configurable one day. */
ctx(tfm)->data = (char *)__get_free_page(GFP_KERNEL); ctx(tfm)->data = (char *)__get_free_page(GFP_KERNEL);
if (!ctx(tfm)->data) if (!ctx(tfm)->data)
return -ENOMEM; goto out;
/* Allocate a fallback and abort if it failed. */ /* Allocate a fallback and abort if it failed. */
fallback_tfm = crypto_alloc_hash(fallback_driver_name, 0, fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK); CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(fallback_tfm)) { if (IS_ERR(fallback_tfm)) {
printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n", printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
fallback_driver_name); fallback_driver_name);
free_page((unsigned long)(ctx(tfm)->data)); err = PTR_ERR(fallback_tfm);
return PTR_ERR(fallback_tfm); goto out_free_page;
} }
ctx(tfm)->fallback.tfm = fallback_tfm; ctx(tfm)->fallback = kmalloc(sizeof(struct shash_desc) +
crypto_shash_descsize(fallback_tfm),
GFP_KERNEL);
if (!ctx(tfm)->fallback)
goto out_free_tfm;
ctx(tfm)->fallback->tfm = fallback_tfm;
ctx(tfm)->fallback->flags = 0;
return 0; return 0;
out_free_tfm:
crypto_free_shash(fallback_tfm);
out_free_page:
free_page((unsigned long)(ctx(tfm)->data));
out:
return err;
} }
static int padlock_sha1_cra_init(struct crypto_tfm *tfm) static int padlock_sha1_cra_init(struct crypto_tfm *tfm)
...@@ -210,8 +231,9 @@ static void padlock_cra_exit(struct crypto_tfm *tfm) ...@@ -210,8 +231,9 @@ static void padlock_cra_exit(struct crypto_tfm *tfm)
ctx(tfm)->data = NULL; ctx(tfm)->data = NULL;
} }
crypto_free_hash(ctx(tfm)->fallback.tfm); crypto_free_shash(ctx(tfm)->fallback->tfm);
ctx(tfm)->fallback.tfm = NULL;
kzfree(ctx(tfm)->fallback);
} }
static struct crypto_alg sha1_alg = { static struct crypto_alg sha1_alg = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册