Skip to content

Commit

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

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed Dec 25, 2008
1 parent 7c4468b commit e5835fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ config CRYPTO_RMD128

config CRYPTO_RMD160
tristate "RIPEMD-160 digest algorithm"
select CRYPTO_ALGAPI
select CRYPTO_HASH
help
RIPEMD-160 (ISO/IEC 10118-3:2004).

Expand Down
61 changes: 32 additions & 29 deletions crypto/rmd160.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
* 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/cryptohash.h>
#include <linux/types.h>
#include <asm/byteorder.h>

Expand Down Expand Up @@ -261,9 +260,9 @@ static void rmd160_transform(u32 *state, const __le32 *in)
return;
}

static void rmd160_init(struct crypto_tfm *tfm)
static int rmd160_init(struct shash_desc *desc)
{
struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
struct rmd160_ctx *rctx = shash_desc_ctx(desc);

rctx->byte_count = 0;

Expand All @@ -274,12 +273,14 @@ static void rmd160_init(struct crypto_tfm *tfm)
rctx->state[4] = RMD_H4;

memset(rctx->buffer, 0, sizeof(rctx->buffer));

return 0;
}

static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
unsigned int len)
static int rmd160_update(struct shash_desc *desc, const u8 *data,
unsigned int len)
{
struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
struct rmd160_ctx *rctx = shash_desc_ctx(desc);
const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);

rctx->byte_count += len;
Expand All @@ -288,7 +289,7 @@ static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
if (avail > len) {
memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
data, len);
return;
goto out;
}

memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
Expand All @@ -306,12 +307,15 @@ static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
}

memcpy(rctx->buffer, data, len);

out:
return 0;
}

/* Add padding and return the message digest. */
static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
static int rmd160_final(struct shash_desc *desc, u8 *out)
{
struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
struct rmd160_ctx *rctx = shash_desc_ctx(desc);
u32 i, index, padlen;
__le64 bits;
__le32 *dst = (__le32 *)out;
Expand All @@ -322,48 +326,47 @@ static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
/* Pad out to 56 mod 64 */
index = rctx->byte_count & 0x3f;
padlen = (index < 56) ? (56 - index) : ((64+56) - index);
rmd160_update(tfm, padding, padlen);
rmd160_update(desc, padding, padlen);

/* Append length */
rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
rmd160_update(desc, (const u8 *)&bits, sizeof(bits));

/* Store state in digest */
for (i = 0; i < 5; i++)
dst[i] = cpu_to_le32p(&rctx->state[i]);

/* Wipe context */
memset(rctx, 0, sizeof(*rctx));

return 0;
}

static struct crypto_alg alg = {
.cra_name = "rmd160",
.cra_driver_name = "rmd160",
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
.cra_blocksize = RMD160_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct rmd160_ctx),
.cra_module = THIS_MODULE,
.cra_list = LIST_HEAD_INIT(alg.cra_list),
.cra_u = { .digest = {
.dia_digestsize = RMD160_DIGEST_SIZE,
.dia_init = rmd160_init,
.dia_update = rmd160_update,
.dia_final = rmd160_final } }
static struct shash_alg alg = {
.digestsize = RMD160_DIGEST_SIZE,
.init = rmd160_init,
.update = rmd160_update,
.final = rmd160_final,
.descsize = sizeof(struct rmd160_ctx),
.base = {
.cra_name = "rmd160",
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
.cra_blocksize = RMD160_BLOCK_SIZE,
.cra_module = THIS_MODULE,
}
};

static int __init rmd160_mod_init(void)
{
return crypto_register_alg(&alg);
return crypto_register_shash(&alg);
}

static void __exit rmd160_mod_fini(void)
{
crypto_unregister_alg(&alg);
crypto_unregister_shash(&alg);
}

module_init(rmd160_mod_init);
module_exit(rmd160_mod_fini);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("RIPEMD-160 Message Digest");

MODULE_ALIAS("rmd160");

0 comments on commit e5835fb

Please sign in to comment.