diff options
author | Atsushi Nemoto <anemo@mba.ocn.ne.jp> | 2006-04-09 18:42:35 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2006-06-26 03:34:38 -0400 |
commit | e1147d8f47eb8fef93f98a30858192145137d2b2 (patch) | |
tree | 7e1bc0ab3d263e5bd801900195ab310625d9ab59 /crypto | |
parent | d00e708cef16442cabaf23f653baf924f5d66e83 (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')
-rw-r--r-- | crypto/digest.c | 42 | ||||
-rw-r--r-- | crypto/michael_mic.c | 1 | ||||
-rw-r--r-- | crypto/sha1.c | 1 | ||||
-rw-r--r-- | crypto/sha256.c | 1 | ||||
-rw-r--r-- | crypto/sha512.c | 2 | ||||
-rw-r--r-- | crypto/tgr192.c | 3 |
6 files changed, 35 insertions, 15 deletions
diff --git a/crypto/digest.c b/crypto/digest.c index d9b6ac9dbf8d..062d0a5a2c89 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 | ||
55 | static void final(struct crypto_tfm *tfm, u8 *out) | 68 | static 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 | ||
60 | static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) | 81 | static 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) | |||
69 | static void digest(struct crypto_tfm *tfm, | 90 | static 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 | ||
86 | int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags) | 98 | int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags) |
diff --git a/crypto/michael_mic.c b/crypto/michael_mic.c index 4f6ab23e14ad..701f859ed767 100644 --- a/crypto/michael_mic.c +++ b/crypto/michael_mic.c | |||
@@ -145,6 +145,7 @@ static struct crypto_alg michael_mic_alg = { | |||
145 | .cra_blocksize = 8, | 145 | .cra_blocksize = 8, |
146 | .cra_ctxsize = sizeof(struct michael_mic_ctx), | 146 | .cra_ctxsize = sizeof(struct michael_mic_ctx), |
147 | .cra_module = THIS_MODULE, | 147 | .cra_module = THIS_MODULE, |
148 | .cra_alignmask = 3, | ||
148 | .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list), | 149 | .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list), |
149 | .cra_u = { .digest = { | 150 | .cra_u = { .digest = { |
150 | .dia_digestsize = 8, | 151 | .dia_digestsize = 8, |
diff --git a/crypto/sha1.c b/crypto/sha1.c index 21571ed35b7e..b96f57d95a82 100644 --- a/crypto/sha1.c +++ b/crypto/sha1.c | |||
@@ -112,6 +112,7 @@ static struct crypto_alg alg = { | |||
112 | .cra_blocksize = SHA1_HMAC_BLOCK_SIZE, | 112 | .cra_blocksize = SHA1_HMAC_BLOCK_SIZE, |
113 | .cra_ctxsize = sizeof(struct sha1_ctx), | 113 | .cra_ctxsize = sizeof(struct sha1_ctx), |
114 | .cra_module = THIS_MODULE, | 114 | .cra_module = THIS_MODULE, |
115 | .cra_alignmask = 3, | ||
115 | .cra_list = LIST_HEAD_INIT(alg.cra_list), | 116 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
116 | .cra_u = { .digest = { | 117 | .cra_u = { .digest = { |
117 | .dia_digestsize = SHA1_DIGEST_SIZE, | 118 | .dia_digestsize = SHA1_DIGEST_SIZE, |
diff --git a/crypto/sha256.c b/crypto/sha256.c index 9d5ef674d6a9..d62264a8a33e 100644 --- a/crypto/sha256.c +++ b/crypto/sha256.c | |||
@@ -313,6 +313,7 @@ static struct crypto_alg alg = { | |||
313 | .cra_blocksize = SHA256_HMAC_BLOCK_SIZE, | 313 | .cra_blocksize = SHA256_HMAC_BLOCK_SIZE, |
314 | .cra_ctxsize = sizeof(struct sha256_ctx), | 314 | .cra_ctxsize = sizeof(struct sha256_ctx), |
315 | .cra_module = THIS_MODULE, | 315 | .cra_module = THIS_MODULE, |
316 | .cra_alignmask = 3, | ||
316 | .cra_list = LIST_HEAD_INIT(alg.cra_list), | 317 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
317 | .cra_u = { .digest = { | 318 | .cra_u = { .digest = { |
318 | .dia_digestsize = SHA256_DIGEST_SIZE, | 319 | .dia_digestsize = SHA256_DIGEST_SIZE, |
diff --git a/crypto/sha512.c b/crypto/sha512.c index 3e6e9392310c..7dbec4f6b947 100644 --- a/crypto/sha512.c +++ b/crypto/sha512.c | |||
@@ -281,6 +281,7 @@ static struct crypto_alg sha512 = { | |||
281 | .cra_blocksize = SHA512_HMAC_BLOCK_SIZE, | 281 | .cra_blocksize = SHA512_HMAC_BLOCK_SIZE, |
282 | .cra_ctxsize = sizeof(struct sha512_ctx), | 282 | .cra_ctxsize = sizeof(struct sha512_ctx), |
283 | .cra_module = THIS_MODULE, | 283 | .cra_module = THIS_MODULE, |
284 | .cra_alignmask = 3, | ||
284 | .cra_list = LIST_HEAD_INIT(sha512.cra_list), | 285 | .cra_list = LIST_HEAD_INIT(sha512.cra_list), |
285 | .cra_u = { .digest = { | 286 | .cra_u = { .digest = { |
286 | .dia_digestsize = SHA512_DIGEST_SIZE, | 287 | .dia_digestsize = SHA512_DIGEST_SIZE, |
@@ -295,6 +296,7 @@ static struct crypto_alg sha384 = { | |||
295 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, | 296 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
296 | .cra_blocksize = SHA384_HMAC_BLOCK_SIZE, | 297 | .cra_blocksize = SHA384_HMAC_BLOCK_SIZE, |
297 | .cra_ctxsize = sizeof(struct sha512_ctx), | 298 | .cra_ctxsize = sizeof(struct sha512_ctx), |
299 | .cra_alignmask = 3, | ||
298 | .cra_module = THIS_MODULE, | 300 | .cra_module = THIS_MODULE, |
299 | .cra_list = LIST_HEAD_INIT(sha384.cra_list), | 301 | .cra_list = LIST_HEAD_INIT(sha384.cra_list), |
300 | .cra_u = { .digest = { | 302 | .cra_u = { .digest = { |
diff --git a/crypto/tgr192.c b/crypto/tgr192.c index 2d8e44f6fbe9..1eae1bb7e495 100644 --- a/crypto/tgr192.c +++ b/crypto/tgr192.c | |||
@@ -627,6 +627,7 @@ static struct crypto_alg tgr192 = { | |||
627 | .cra_blocksize = TGR192_BLOCK_SIZE, | 627 | .cra_blocksize = TGR192_BLOCK_SIZE, |
628 | .cra_ctxsize = sizeof(struct tgr192_ctx), | 628 | .cra_ctxsize = sizeof(struct tgr192_ctx), |
629 | .cra_module = THIS_MODULE, | 629 | .cra_module = THIS_MODULE, |
630 | .cra_alignmask = 7, | ||
630 | .cra_list = LIST_HEAD_INIT(tgr192.cra_list), | 631 | .cra_list = LIST_HEAD_INIT(tgr192.cra_list), |
631 | .cra_u = {.digest = { | 632 | .cra_u = {.digest = { |
632 | .dia_digestsize = TGR192_DIGEST_SIZE, | 633 | .dia_digestsize = TGR192_DIGEST_SIZE, |
@@ -641,6 +642,7 @@ static struct crypto_alg tgr160 = { | |||
641 | .cra_blocksize = TGR192_BLOCK_SIZE, | 642 | .cra_blocksize = TGR192_BLOCK_SIZE, |
642 | .cra_ctxsize = sizeof(struct tgr192_ctx), | 643 | .cra_ctxsize = sizeof(struct tgr192_ctx), |
643 | .cra_module = THIS_MODULE, | 644 | .cra_module = THIS_MODULE, |
645 | .cra_alignmask = 7, | ||
644 | .cra_list = LIST_HEAD_INIT(tgr160.cra_list), | 646 | .cra_list = LIST_HEAD_INIT(tgr160.cra_list), |
645 | .cra_u = {.digest = { | 647 | .cra_u = {.digest = { |
646 | .dia_digestsize = TGR160_DIGEST_SIZE, | 648 | .dia_digestsize = TGR160_DIGEST_SIZE, |
@@ -655,6 +657,7 @@ static struct crypto_alg tgr128 = { | |||
655 | .cra_blocksize = TGR192_BLOCK_SIZE, | 657 | .cra_blocksize = TGR192_BLOCK_SIZE, |
656 | .cra_ctxsize = sizeof(struct tgr192_ctx), | 658 | .cra_ctxsize = sizeof(struct tgr192_ctx), |
657 | .cra_module = THIS_MODULE, | 659 | .cra_module = THIS_MODULE, |
660 | .cra_alignmask = 7, | ||
658 | .cra_list = LIST_HEAD_INIT(tgr128.cra_list), | 661 | .cra_list = LIST_HEAD_INIT(tgr128.cra_list), |
659 | .cra_u = {.digest = { | 662 | .cra_u = {.digest = { |
660 | .dia_digestsize = TGR128_DIGEST_SIZE, | 663 | .dia_digestsize = TGR128_DIGEST_SIZE, |