Skip to content

Commit

Permalink
crypto: sha512 - Switch to shash
Browse files Browse the repository at this point in the history
This patch changes sha512 and sha384 to the new shash interface.

Signed-off-by: Adrian-Ken Rueegsegger <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
Kensan authored and herbertx committed Dec 25, 2008
1 parent f9e2bca commit bd9d20d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
2 changes: 1 addition & 1 deletion crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ config CRYPTO_SHA256

config CRYPTO_SHA512
tristate "SHA384 and SHA512 digest algorithms"
select CRYPTO_ALGAPI
select CRYPTO_HASH
help
SHA512 secure hash standard (DFIPS 180-2).

Expand Down
112 changes: 59 additions & 53 deletions crypto/sha512_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* later version.
*
*/

#include <crypto/internal/hash.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mm.h>
Expand Down Expand Up @@ -138,10 +138,10 @@ sha512_transform(u64 *state, const u8 *input)
put_cpu_var(msg_schedule);
}

static void
sha512_init(struct crypto_tfm *tfm)
static int
sha512_init(struct shash_desc *desc)
{
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
struct sha512_ctx *sctx = shash_desc_ctx(desc);
sctx->state[0] = SHA512_H0;
sctx->state[1] = SHA512_H1;
sctx->state[2] = SHA512_H2;
Expand All @@ -151,12 +151,14 @@ sha512_init(struct crypto_tfm *tfm)
sctx->state[6] = SHA512_H6;
sctx->state[7] = SHA512_H7;
sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;

return 0;
}

static void
sha384_init(struct crypto_tfm *tfm)
static int
sha384_init(struct shash_desc *desc)
{
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
struct sha512_ctx *sctx = shash_desc_ctx(desc);
sctx->state[0] = SHA384_H0;
sctx->state[1] = SHA384_H1;
sctx->state[2] = SHA384_H2;
Expand All @@ -166,12 +168,14 @@ sha384_init(struct crypto_tfm *tfm)
sctx->state[6] = SHA384_H6;
sctx->state[7] = SHA384_H7;
sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;

return 0;
}

static void
sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
static int
sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
{
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
struct sha512_ctx *sctx = shash_desc_ctx(desc);

unsigned int i, index, part_len;

Expand Down Expand Up @@ -203,12 +207,14 @@ sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)

/* Buffer remaining input */
memcpy(&sctx->buf[index], &data[i], len - i);

return 0;
}

static void
sha512_final(struct crypto_tfm *tfm, u8 *hash)
static int
sha512_final(struct shash_desc *desc, u8 *hash)
{
struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
struct sha512_ctx *sctx = shash_desc_ctx(desc);
static u8 padding[128] = { 0x80, };
__be64 *dst = (__be64 *)hash;
__be32 bits[4];
Expand All @@ -224,77 +230,77 @@ sha512_final(struct crypto_tfm *tfm, u8 *hash)
/* Pad out to 112 mod 128. */
index = (sctx->count[0] >> 3) & 0x7f;
pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
sha512_update(tfm, padding, pad_len);
sha512_update(desc, padding, pad_len);

/* Append length (before padding) */
sha512_update(tfm, (const u8 *)bits, sizeof(bits));
sha512_update(desc, (const u8 *)bits, sizeof(bits));

/* Store state in digest */
for (i = 0; i < 8; i++)
dst[i] = cpu_to_be64(sctx->state[i]);

/* Zeroize sensitive information. */
memset(sctx, 0, sizeof(struct sha512_ctx));

return 0;
}

static void sha384_final(struct crypto_tfm *tfm, u8 *hash)
static int sha384_final(struct shash_desc *desc, u8 *hash)
{
u8 D[64];
u8 D[64];

sha512_final(tfm, D);
sha512_final(desc, D);

memcpy(hash, D, 48);
memset(D, 0, 64);
memcpy(hash, D, 48);
memset(D, 0, 64);

return 0;
}

static struct crypto_alg sha512 = {
.cra_name = "sha512",
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
.cra_blocksize = SHA512_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,
.dia_init = sha512_init,
.dia_update = sha512_update,
.dia_final = sha512_final }
}
static struct shash_alg sha512 = {
.digestsize = SHA512_DIGEST_SIZE,
.init = sha512_init,
.update = sha512_update,
.final = sha512_final,
.descsize = sizeof(struct sha512_ctx),
.base = {
.cra_name = "sha512",
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
.cra_blocksize = SHA512_BLOCK_SIZE,
.cra_module = THIS_MODULE,
}
};

static struct crypto_alg sha384 = {
.cra_name = "sha384",
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
.cra_blocksize = SHA384_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 = {
.dia_digestsize = SHA384_DIGEST_SIZE,
.dia_init = sha384_init,
.dia_update = sha512_update,
.dia_final = sha384_final }
}
static struct shash_alg sha384 = {
.digestsize = SHA384_DIGEST_SIZE,
.init = sha384_init,
.update = sha512_update,
.final = sha384_final,
.descsize = sizeof(struct sha512_ctx),
.base = {
.cra_name = "sha384",
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
.cra_blocksize = SHA384_BLOCK_SIZE,
.cra_module = THIS_MODULE,
}
};

static int __init sha512_generic_mod_init(void)
{
int ret = 0;

if ((ret = crypto_register_alg(&sha384)) < 0)
if ((ret = crypto_register_shash(&sha384)) < 0)
goto out;
if ((ret = crypto_register_alg(&sha512)) < 0)
crypto_unregister_alg(&sha384);
if ((ret = crypto_register_shash(&sha512)) < 0)
crypto_unregister_shash(&sha384);
out:
return ret;
}

static void __exit sha512_generic_mod_fini(void)
{
crypto_unregister_alg(&sha384);
crypto_unregister_alg(&sha512);
crypto_unregister_shash(&sha384);
crypto_unregister_shash(&sha512);
}

module_init(sha512_generic_mod_init);
Expand Down

0 comments on commit bd9d20d

Please sign in to comment.