aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/digest.c
diff options
context:
space:
mode:
authorAtsushi Nemoto <anemo@mba.ocn.ne.jp>2006-04-09 18:42:35 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2006-06-26 03:34:38 -0400
commite1147d8f47eb8fef93f98a30858192145137d2b2 (patch)
tree7e1bc0ab3d263e5bd801900195ab310625d9ab59 /crypto/digest.c
parentd00e708cef16442cabaf23f653baf924f5d66e83 (diff)
[CRYPTO] digest: Add alignment handling
Some hash modules load/store data words directly. The digest layer should pass properly aligned buffer to update()/final() method. This patch also add cra_alignmask to some hash modules. Signed-off-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/digest.c')
-rw-r--r--crypto/digest.c42
1 files changed, 27 insertions, 15 deletions
diff --git a/crypto/digest.c b/crypto/digest.c
index d9b6ac9dbf8..062d0a5a2c8 100644
--- a/crypto/digest.c
+++ b/crypto/digest.c
@@ -27,6 +27,7 @@ static void update(struct crypto_tfm *tfm,
27 struct scatterlist *sg, unsigned int nsg) 27 struct scatterlist *sg, unsigned int nsg)
28{ 28{
29 unsigned int i; 29 unsigned int i;
30 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
30 31
31 for (i = 0; i < nsg; i++) { 32 for (i = 0; i < nsg; i++) {
32 33
@@ -38,12 +39,24 @@ static void update(struct crypto_tfm *tfm,
38 unsigned int bytes_from_page = min(l, ((unsigned int) 39 unsigned int bytes_from_page = min(l, ((unsigned int)
39 (PAGE_SIZE)) - 40 (PAGE_SIZE)) -
40 offset); 41 offset);
41 char *p = crypto_kmap(pg, 0) + offset; 42 char *src = crypto_kmap(pg, 0);
43 char *p = src + offset;
42 44
45 if (unlikely(offset & alignmask)) {
46 unsigned int bytes =
47 alignmask + 1 - (offset & alignmask);
48 bytes = min(bytes, bytes_from_page);
49 tfm->__crt_alg->cra_digest.dia_update
50 (crypto_tfm_ctx(tfm), p,
51 bytes);
52 p += bytes;
53 bytes_from_page -= bytes;
54 l -= bytes;
55 }
43 tfm->__crt_alg->cra_digest.dia_update 56 tfm->__crt_alg->cra_digest.dia_update
44 (crypto_tfm_ctx(tfm), p, 57 (crypto_tfm_ctx(tfm), p,
45 bytes_from_page); 58 bytes_from_page);
46 crypto_kunmap(p, 0); 59 crypto_kunmap(src, 0);
47 crypto_yield(tfm); 60 crypto_yield(tfm);
48 offset = 0; 61 offset = 0;
49 pg++; 62 pg++;
@@ -54,7 +67,15 @@ static void update(struct crypto_tfm *tfm,
54 67
55static void final(struct crypto_tfm *tfm, u8 *out) 68static void final(struct crypto_tfm *tfm, u8 *out)
56{ 69{
57 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out); 70 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
71 if (unlikely((unsigned long)out & alignmask)) {
72 unsigned int size = crypto_tfm_alg_digestsize(tfm);
73 u8 buffer[size + alignmask];
74 u8 *dst = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
75 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), dst);
76 memcpy(out, dst, size);
77 } else
78 tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
58} 79}
59 80
60static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) 81static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
@@ -69,18 +90,9 @@ static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
69static void digest(struct crypto_tfm *tfm, 90static void digest(struct crypto_tfm *tfm,
70 struct scatterlist *sg, unsigned int nsg, u8 *out) 91 struct scatterlist *sg, unsigned int nsg, u8 *out)
71{ 92{
72 unsigned int i; 93 init(tfm);
73 94 update(tfm, sg, nsg);
74 tfm->crt_digest.dit_init(tfm); 95 final(tfm, out);
75
76 for (i = 0; i < nsg; i++) {
77 char *p = crypto_kmap(sg[i].page, 0) + sg[i].offset;
78 tfm->__crt_alg->cra_digest.dia_update(crypto_tfm_ctx(tfm),
79 p, sg[i].length);
80 crypto_kunmap(p, 0);
81 crypto_yield(tfm);
82 }
83 crypto_digest_final(tfm, out);
84} 96}
85 97
86int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags) 98int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)