Skip to content

Commit 7d02460

Browse files
committed
crypto: padlock - Use shash fallback for sha
This patch changes padlock sha fallback to shash instead of hash. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent f63559b commit 7d02460

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

‎drivers/crypto/padlock-sha.c‎

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,24 @@
1212
*
1313
*/
1414

15-
#include <crypto/algapi.h>
15+
#include <crypto/internal/hash.h>
1616
#include <crypto/sha.h>
1717
#include <linux/err.h>
1818
#include <linux/module.h>
1919
#include <linux/init.h>
2020
#include <linux/errno.h>
21-
#include <linux/cryptohash.h>
2221
#include <linux/interrupt.h>
2322
#include <linux/kernel.h>
2423
#include <linux/scatterlist.h>
2524
#include <asm/i387.h>
2625
#include "padlock.h"
2726

28-
#define SHA1_DEFAULT_FALLBACK "sha1-generic"
29-
#define SHA256_DEFAULT_FALLBACK "sha256-generic"
30-
3127
struct padlock_sha_ctx {
3228
char *data;
3329
size_t used;
3430
int bypass;
3531
void (*f_sha_padlock)(const char *in, char *out, int count);
36-
struct hash_desc fallback;
32+
struct shash_desc *fallback;
3733
};
3834

3935
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)
4743

4844
static struct crypto_alg sha1_alg, sha256_alg;
4945

50-
static void padlock_sha_bypass(struct crypto_tfm *tfm)
46+
static int padlock_sha_bypass(struct crypto_tfm *tfm)
5147
{
48+
int err = 0;
49+
5250
if (ctx(tfm)->bypass)
53-
return;
51+
goto out;
5452

55-
crypto_hash_init(&ctx(tfm)->fallback);
56-
if (ctx(tfm)->data && ctx(tfm)->used) {
57-
struct scatterlist sg;
53+
err = crypto_shash_init(ctx(tfm)->fallback);
54+
if (err)
55+
goto out;
5856

59-
sg_init_one(&sg, ctx(tfm)->data, ctx(tfm)->used);
60-
crypto_hash_update(&ctx(tfm)->fallback, &sg, sg.length);
61-
}
57+
if (ctx(tfm)->data && ctx(tfm)->used)
58+
err = crypto_shash_update(ctx(tfm)->fallback, ctx(tfm)->data,
59+
ctx(tfm)->used);
6260

6361
ctx(tfm)->used = 0;
6462
ctx(tfm)->bypass = 1;
63+
64+
out:
65+
return err;
6566
}
6667

6768
static void padlock_sha_init(struct crypto_tfm *tfm)
@@ -73,15 +74,18 @@ static void padlock_sha_init(struct crypto_tfm *tfm)
7374
static void padlock_sha_update(struct crypto_tfm *tfm,
7475
const uint8_t *data, unsigned int length)
7576
{
77+
int err;
78+
7679
/* Our buffer is always one page. */
7780
if (unlikely(!ctx(tfm)->bypass &&
78-
(ctx(tfm)->used + length > PAGE_SIZE)))
79-
padlock_sha_bypass(tfm);
81+
(ctx(tfm)->used + length > PAGE_SIZE))) {
82+
err = padlock_sha_bypass(tfm);
83+
BUG_ON(err);
84+
}
8085

8186
if (unlikely(ctx(tfm)->bypass)) {
82-
struct scatterlist sg;
83-
sg_init_one(&sg, (uint8_t *)data, length);
84-
crypto_hash_update(&ctx(tfm)->fallback, &sg, length);
87+
err = crypto_shash_update(ctx(tfm)->fallback, data, length);
88+
BUG_ON(err);
8589
return;
8690
}
8791

@@ -151,8 +155,11 @@ static void padlock_do_sha256(const char *in, char *out, int count)
151155

152156
static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
153157
{
158+
int err;
159+
154160
if (unlikely(ctx(tfm)->bypass)) {
155-
crypto_hash_final(&ctx(tfm)->fallback, out);
161+
err = crypto_shash_final(ctx(tfm)->fallback, out);
162+
BUG_ON(err);
156163
ctx(tfm)->bypass = 0;
157164
return;
158165
}
@@ -166,27 +173,41 @@ static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
166173
static int padlock_cra_init(struct crypto_tfm *tfm)
167174
{
168175
const char *fallback_driver_name = tfm->__crt_alg->cra_name;
169-
struct crypto_hash *fallback_tfm;
176+
struct crypto_shash *fallback_tfm;
177+
int err = -ENOMEM;
170178

171179
/* For now we'll allocate one page. This
172180
* could eventually be configurable one day. */
173181
ctx(tfm)->data = (char *)__get_free_page(GFP_KERNEL);
174182
if (!ctx(tfm)->data)
175-
return -ENOMEM;
183+
goto out;
176184

177185
/* Allocate a fallback and abort if it failed. */
178-
fallback_tfm = crypto_alloc_hash(fallback_driver_name, 0,
179-
CRYPTO_ALG_ASYNC |
180-
CRYPTO_ALG_NEED_FALLBACK);
186+
fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
187+
CRYPTO_ALG_NEED_FALLBACK);
181188
if (IS_ERR(fallback_tfm)) {
182189
printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
183190
fallback_driver_name);
184-
free_page((unsigned long)(ctx(tfm)->data));
185-
return PTR_ERR(fallback_tfm);
191+
err = PTR_ERR(fallback_tfm);
192+
goto out_free_page;
186193
}
187194

188-
ctx(tfm)->fallback.tfm = fallback_tfm;
195+
ctx(tfm)->fallback = kmalloc(sizeof(struct shash_desc) +
196+
crypto_shash_descsize(fallback_tfm),
197+
GFP_KERNEL);
198+
if (!ctx(tfm)->fallback)
199+
goto out_free_tfm;
200+
201+
ctx(tfm)->fallback->tfm = fallback_tfm;
202+
ctx(tfm)->fallback->flags = 0;
189203
return 0;
204+
205+
out_free_tfm:
206+
crypto_free_shash(fallback_tfm);
207+
out_free_page:
208+
free_page((unsigned long)(ctx(tfm)->data));
209+
out:
210+
return err;
190211
}
191212

192213
static int padlock_sha1_cra_init(struct crypto_tfm *tfm)
@@ -210,8 +231,9 @@ static void padlock_cra_exit(struct crypto_tfm *tfm)
210231
ctx(tfm)->data = NULL;
211232
}
212233

213-
crypto_free_hash(ctx(tfm)->fallback.tfm);
214-
ctx(tfm)->fallback.tfm = NULL;
234+
crypto_free_shash(ctx(tfm)->fallback->tfm);
235+
236+
kzfree(ctx(tfm)->fallback);
215237
}
216238

217239
static struct crypto_alg sha1_alg = {

0 commit comments

Comments
 (0)