]> err.no Git - linux-2.6/commitdiff
[CRYPTO] digest: Add alignment handling
authorAtsushi Nemoto <anemo@mba.ocn.ne.jp>
Sun, 9 Apr 2006 22:42:35 +0000 (08:42 +1000)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 26 Jun 2006 07:34:38 +0000 (17:34 +1000)
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>
crypto/digest.c
crypto/michael_mic.c
crypto/sha1.c
crypto/sha256.c
crypto/sha512.c
crypto/tgr192.c

index d9b6ac9dbf8d08e8ae88572c71f93d132a10df25..062d0a5a2c89ac84974908ac0f38b3ec880125ab 100644 (file)
@@ -27,6 +27,7 @@ static void update(struct crypto_tfm *tfm,
                    struct scatterlist *sg, unsigned int nsg)
 {
        unsigned int i;
+       unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
 
        for (i = 0; i < nsg; i++) {
 
@@ -38,12 +39,24 @@ static void update(struct crypto_tfm *tfm,
                        unsigned int bytes_from_page = min(l, ((unsigned int)
                                                           (PAGE_SIZE)) - 
                                                           offset);
-                       char *p = crypto_kmap(pg, 0) + offset;
+                       char *src = crypto_kmap(pg, 0);
+                       char *p = src + offset;
 
+                       if (unlikely(offset & alignmask)) {
+                               unsigned int bytes =
+                                       alignmask + 1 - (offset & alignmask);
+                               bytes = min(bytes, bytes_from_page);
+                               tfm->__crt_alg->cra_digest.dia_update
+                                               (crypto_tfm_ctx(tfm), p,
+                                                bytes);
+                               p += bytes;
+                               bytes_from_page -= bytes;
+                               l -= bytes;
+                       }
                        tfm->__crt_alg->cra_digest.dia_update
                                        (crypto_tfm_ctx(tfm), p,
                                         bytes_from_page);
-                       crypto_kunmap(p, 0);
+                       crypto_kunmap(src, 0);
                        crypto_yield(tfm);
                        offset = 0;
                        pg++;
@@ -54,7 +67,15 @@ static void update(struct crypto_tfm *tfm,
 
 static void final(struct crypto_tfm *tfm, u8 *out)
 {
-       tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
+       unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
+       if (unlikely((unsigned long)out & alignmask)) {
+               unsigned int size = crypto_tfm_alg_digestsize(tfm);
+               u8 buffer[size + alignmask];
+               u8 *dst = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
+               tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), dst);
+               memcpy(out, dst, size);
+       } else
+               tfm->__crt_alg->cra_digest.dia_final(crypto_tfm_ctx(tfm), out);
 }
 
 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)
 static void digest(struct crypto_tfm *tfm,
                    struct scatterlist *sg, unsigned int nsg, u8 *out)
 {
-       unsigned int i;
-
-       tfm->crt_digest.dit_init(tfm);
-               
-       for (i = 0; i < nsg; i++) {
-               char *p = crypto_kmap(sg[i].page, 0) + sg[i].offset;
-               tfm->__crt_alg->cra_digest.dia_update(crypto_tfm_ctx(tfm),
-                                                     p, sg[i].length);
-               crypto_kunmap(p, 0);
-               crypto_yield(tfm);
-       }
-       crypto_digest_final(tfm, out);
+       init(tfm);
+       update(tfm, sg, nsg);
+       final(tfm, out);
 }
 
 int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
index 4f6ab23e14adfb765332394495412a25957ac28f..701f859ed7674618da0c6969256df2f4f52fa544 100644 (file)
@@ -145,6 +145,7 @@ static struct crypto_alg michael_mic_alg = {
        .cra_blocksize  = 8,
        .cra_ctxsize    = sizeof(struct michael_mic_ctx),
        .cra_module     = THIS_MODULE,
+       .cra_alignmask  = 3,
        .cra_list       = LIST_HEAD_INIT(michael_mic_alg.cra_list),
        .cra_u          = { .digest = {
        .dia_digestsize = 8,
index 21571ed35b7ee7f90792a5f8b83ea2b42ff6cac6..b96f57d95a820b89dd4a5b3fffac9a54d2017af1 100644 (file)
@@ -112,6 +112,7 @@ static struct crypto_alg alg = {
        .cra_blocksize  =       SHA1_HMAC_BLOCK_SIZE,
        .cra_ctxsize    =       sizeof(struct sha1_ctx),
        .cra_module     =       THIS_MODULE,
+       .cra_alignmask  =       3,
        .cra_list       =       LIST_HEAD_INIT(alg.cra_list),
        .cra_u          =       { .digest = {
        .dia_digestsize =       SHA1_DIGEST_SIZE,
index 9d5ef674d6a9e4336d7c194fec2f3c270e323996..d62264a8a33e637d6d057b0a20ffd71da59b4813 100644 (file)
@@ -313,6 +313,7 @@ static struct crypto_alg alg = {
        .cra_blocksize  =       SHA256_HMAC_BLOCK_SIZE,
        .cra_ctxsize    =       sizeof(struct sha256_ctx),
        .cra_module     =       THIS_MODULE,
+       .cra_alignmask  =       3,
        .cra_list       =       LIST_HEAD_INIT(alg.cra_list),
        .cra_u          =       { .digest = {
        .dia_digestsize =       SHA256_DIGEST_SIZE,
index 3e6e9392310c0fe94adaebc9632de37089f0d87e..7dbec4f6b9471e9a59442c26a87ec0ef124872cd 100644 (file)
@@ -281,6 +281,7 @@ static struct crypto_alg sha512 = {
         .cra_blocksize  = SHA512_HMAC_BLOCK_SIZE,
         .cra_ctxsize    = sizeof(struct sha512_ctx),
         .cra_module     = THIS_MODULE,
+       .cra_alignmask  = 3,
         .cra_list       = LIST_HEAD_INIT(sha512.cra_list),
         .cra_u          = { .digest = {
                                 .dia_digestsize = SHA512_DIGEST_SIZE,
@@ -295,6 +296,7 @@ static struct crypto_alg sha384 = {
         .cra_flags      = CRYPTO_ALG_TYPE_DIGEST,
         .cra_blocksize  = SHA384_HMAC_BLOCK_SIZE,
         .cra_ctxsize    = sizeof(struct sha512_ctx),
+       .cra_alignmask  = 3,
         .cra_module     = THIS_MODULE,
         .cra_list       = LIST_HEAD_INIT(sha384.cra_list),
         .cra_u          = { .digest = {
index 2d8e44f6fbe906520ce07628d7a148b597731a7d..1eae1bb7e495d99250e14a7576319cedf1f052c6 100644 (file)
@@ -627,6 +627,7 @@ static struct crypto_alg tgr192 = {
        .cra_blocksize = TGR192_BLOCK_SIZE,
        .cra_ctxsize = sizeof(struct tgr192_ctx),
        .cra_module = THIS_MODULE,
+       .cra_alignmask = 7,
        .cra_list = LIST_HEAD_INIT(tgr192.cra_list),
        .cra_u = {.digest = {
                             .dia_digestsize = TGR192_DIGEST_SIZE,
@@ -641,6 +642,7 @@ static struct crypto_alg tgr160 = {
        .cra_blocksize = TGR192_BLOCK_SIZE,
        .cra_ctxsize = sizeof(struct tgr192_ctx),
        .cra_module = THIS_MODULE,
+       .cra_alignmask = 7,
        .cra_list = LIST_HEAD_INIT(tgr160.cra_list),
        .cra_u = {.digest = {
                             .dia_digestsize = TGR160_DIGEST_SIZE,
@@ -655,6 +657,7 @@ static struct crypto_alg tgr128 = {
        .cra_blocksize = TGR192_BLOCK_SIZE,
        .cra_ctxsize = sizeof(struct tgr192_ctx),
        .cra_module = THIS_MODULE,
+       .cra_alignmask = 7,
        .cra_list = LIST_HEAD_INIT(tgr128.cra_list),
        .cra_u = {.digest = {
                             .dia_digestsize = TGR128_DIGEST_SIZE,