crypto: api - Fix crypto_alloc_tfm/create_create_tfm return convention
[safe/jmp/linux-2.6] / crypto / sha256_generic.c
index 5f4332e..caa3542 100644 (file)
@@ -9,6 +9,7 @@
  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
+ * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
  * any later version.
  *
  */
+#include <crypto/internal/hash.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/mm.h>
-#include <linux/crypto.h>
 #include <linux/types.h>
 #include <crypto/sha.h>
-#include <asm/scatterlist.h>
 #include <asm/byteorder.h>
 
 struct sha256_ctx {
@@ -69,7 +69,7 @@ static void sha256_transform(u32 *state, const u8 *input)
        /* now blend */
        for (i = 16; i < 64; i++)
                BLEND_OP(i, W);
-    
+
        /* load the state into our registers */
        a=state[0];  b=state[1];  c=state[2];  d=state[3];
        e=state[4];  f=state[5];  g=state[6];  h=state[7];
@@ -219,9 +219,27 @@ static void sha256_transform(u32 *state, const u8 *input)
        memset(W, 0, 64 * sizeof(u32));
 }
 
-static void sha256_init(struct crypto_tfm *tfm)
+
+static int sha224_init(struct shash_desc *desc)
+{
+       struct sha256_ctx *sctx = shash_desc_ctx(desc);
+       sctx->state[0] = SHA224_H0;
+       sctx->state[1] = SHA224_H1;
+       sctx->state[2] = SHA224_H2;
+       sctx->state[3] = SHA224_H3;
+       sctx->state[4] = SHA224_H4;
+       sctx->state[5] = SHA224_H5;
+       sctx->state[6] = SHA224_H6;
+       sctx->state[7] = SHA224_H7;
+       sctx->count[0] = 0;
+       sctx->count[1] = 0;
+
+       return 0;
+}
+
+static int sha256_init(struct shash_desc *desc)
 {
-       struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
+       struct sha256_ctx *sctx = shash_desc_ctx(desc);
        sctx->state[0] = SHA256_H0;
        sctx->state[1] = SHA256_H1;
        sctx->state[2] = SHA256_H2;
@@ -231,12 +249,14 @@ static void sha256_init(struct crypto_tfm *tfm)
        sctx->state[6] = SHA256_H6;
        sctx->state[7] = SHA256_H7;
        sctx->count[0] = sctx->count[1] = 0;
+
+       return 0;
 }
 
-static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
+static int sha256_update(struct shash_desc *desc, const u8 *data,
                          unsigned int len)
 {
-       struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
+       struct sha256_ctx *sctx = shash_desc_ctx(desc);
        unsigned int i, index, part_len;
 
        /* Compute number of bytes mod 128 */
@@ -261,14 +281,16 @@ static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
        } else {
                i = 0;
        }
-       
+
        /* Buffer remaining input */
        memcpy(&sctx->buf[index], &data[i], len-i);
+
+       return 0;
 }
 
-static void sha256_final(struct crypto_tfm *tfm, u8 *out)
+static int sha256_final(struct shash_desc *desc, u8 *out)
 {
-       struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
+       struct sha256_ctx *sctx = shash_desc_ctx(desc);
        __be32 *dst = (__be32 *)out;
        __be32 bits[2];
        unsigned int index, pad_len;
@@ -282,10 +304,10 @@ static void sha256_final(struct crypto_tfm *tfm, u8 *out)
        /* Pad out to 56 mod 64. */
        index = (sctx->count[0] >> 3) & 0x3f;
        pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
-       sha256_update(tfm, padding, pad_len);
+       sha256_update(desc, padding, pad_len);
 
        /* Append length (before padding) */
-       sha256_update(tfm, (const u8 *)bits, sizeof(bits));
+       sha256_update(desc, (const u8 *)bits, sizeof(bits));
 
        /* Store state in digest */
        for (i = 0; i < 8; i++)
@@ -293,39 +315,80 @@ static void sha256_final(struct crypto_tfm *tfm, u8 *out)
 
        /* Zeroize sensitive information. */
        memset(sctx, 0, sizeof(*sctx));
+
+       return 0;
 }
 
+static int sha224_final(struct shash_desc *desc, u8 *hash)
+{
+       u8 D[SHA256_DIGEST_SIZE];
+
+       sha256_final(desc, D);
+
+       memcpy(hash, D, SHA224_DIGEST_SIZE);
+       memset(D, 0, SHA256_DIGEST_SIZE);
 
-static struct crypto_alg alg = {
-       .cra_name       =       "sha256",
-       .cra_driver_name=       "sha256-generic",
-       .cra_flags      =       CRYPTO_ALG_TYPE_DIGEST,
-       .cra_blocksize  =       SHA256_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,
-       .dia_init       =       sha256_init,
-       .dia_update     =       sha256_update,
-       .dia_final      =       sha256_final } }
+       return 0;
+}
+
+static struct shash_alg sha256 = {
+       .digestsize     =       SHA256_DIGEST_SIZE,
+       .init           =       sha256_init,
+       .update         =       sha256_update,
+       .final          =       sha256_final,
+       .descsize       =       sizeof(struct sha256_ctx),
+       .base           =       {
+               .cra_name       =       "sha256",
+               .cra_driver_name=       "sha256-generic",
+               .cra_flags      =       CRYPTO_ALG_TYPE_SHASH,
+               .cra_blocksize  =       SHA256_BLOCK_SIZE,
+               .cra_module     =       THIS_MODULE,
+       }
+};
+
+static struct shash_alg sha224 = {
+       .digestsize     =       SHA224_DIGEST_SIZE,
+       .init           =       sha224_init,
+       .update         =       sha256_update,
+       .final          =       sha224_final,
+       .descsize       =       sizeof(struct sha256_ctx),
+       .base           =       {
+               .cra_name       =       "sha224",
+               .cra_driver_name=       "sha224-generic",
+               .cra_flags      =       CRYPTO_ALG_TYPE_SHASH,
+               .cra_blocksize  =       SHA224_BLOCK_SIZE,
+               .cra_module     =       THIS_MODULE,
+       }
 };
 
-static int __init init(void)
+static int __init sha256_generic_mod_init(void)
 {
-       return crypto_register_alg(&alg);
+       int ret = 0;
+
+       ret = crypto_register_shash(&sha224);
+
+       if (ret < 0)
+               return ret;
+
+       ret = crypto_register_shash(&sha256);
+
+       if (ret < 0)
+               crypto_unregister_shash(&sha224);
+
+       return ret;
 }
 
-static void __exit fini(void)
+static void __exit sha256_generic_mod_fini(void)
 {
-       crypto_unregister_alg(&alg);
+       crypto_unregister_shash(&sha224);
+       crypto_unregister_shash(&sha256);
 }
 
-module_init(init);
-module_exit(fini);
+module_init(sha256_generic_mod_init);
+module_exit(sha256_generic_mod_fini);
 
 MODULE_LICENSE("GPL");
-MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");
+MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
 
+MODULE_ALIAS("sha224");
 MODULE_ALIAS("sha256");