rmd160.c (b2aaf8f74cdc84a9182f6cabf198b7763bcb9d40) rmd160.c (e5835fba0206a331bdefdf5d805d1a384af37c44)
1/*
2 * Cryptographic API.
3 *
4 * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
5 *
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
7 *
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 */
1/*
2 * Cryptographic API.
3 *
4 * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
5 *
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
7 *
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 */
16#include <crypto/internal/hash.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/mm.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
19#include <linux/crypto.h>
20#include <linux/cryptohash.h>
21#include <linux/types.h>
22#include <asm/byteorder.h>
23
24#include "ripemd.h"
25
26struct rmd160_ctx {
27 u64 byte_count;
28 u32 state[5];

--- 227 unchanged lines hidden (view full) ---

256 state[2] = state[3] + ee + aaa;
257 state[3] = state[4] + aa + bbb;
258 state[4] = state[0] + bb + ccc;
259 state[0] = ddd;
260
261 return;
262}
263
20#include <linux/types.h>
21#include <asm/byteorder.h>
22
23#include "ripemd.h"
24
25struct rmd160_ctx {
26 u64 byte_count;
27 u32 state[5];

--- 227 unchanged lines hidden (view full) ---

255 state[2] = state[3] + ee + aaa;
256 state[3] = state[4] + aa + bbb;
257 state[4] = state[0] + bb + ccc;
258 state[0] = ddd;
259
260 return;
261}
262
264static void rmd160_init(struct crypto_tfm *tfm)
263static int rmd160_init(struct shash_desc *desc)
265{
264{
266 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
265 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
267
268 rctx->byte_count = 0;
269
270 rctx->state[0] = RMD_H0;
271 rctx->state[1] = RMD_H1;
272 rctx->state[2] = RMD_H2;
273 rctx->state[3] = RMD_H3;
274 rctx->state[4] = RMD_H4;
275
276 memset(rctx->buffer, 0, sizeof(rctx->buffer));
266
267 rctx->byte_count = 0;
268
269 rctx->state[0] = RMD_H0;
270 rctx->state[1] = RMD_H1;
271 rctx->state[2] = RMD_H2;
272 rctx->state[3] = RMD_H3;
273 rctx->state[4] = RMD_H4;
274
275 memset(rctx->buffer, 0, sizeof(rctx->buffer));
276
277 return 0;
277}
278
278}
279
279static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
280 unsigned int len)
280static int rmd160_update(struct shash_desc *desc, const u8 *data,
281 unsigned int len)
281{
282{
282 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
283 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
283 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
284
285 rctx->byte_count += len;
286
287 /* Enough space in buffer? If so copy and we're done */
288 if (avail > len) {
289 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
290 data, len);
284 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
285
286 rctx->byte_count += len;
287
288 /* Enough space in buffer? If so copy and we're done */
289 if (avail > len) {
290 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
291 data, len);
291 return;
292 goto out;
292 }
293
294 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
295 data, avail);
296
297 rmd160_transform(rctx->state, rctx->buffer);
298 data += avail;
299 len -= avail;
300
301 while (len >= sizeof(rctx->buffer)) {
302 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
303 rmd160_transform(rctx->state, rctx->buffer);
304 data += sizeof(rctx->buffer);
305 len -= sizeof(rctx->buffer);
306 }
307
308 memcpy(rctx->buffer, data, len);
293 }
294
295 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
296 data, avail);
297
298 rmd160_transform(rctx->state, rctx->buffer);
299 data += avail;
300 len -= avail;
301
302 while (len >= sizeof(rctx->buffer)) {
303 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
304 rmd160_transform(rctx->state, rctx->buffer);
305 data += sizeof(rctx->buffer);
306 len -= sizeof(rctx->buffer);
307 }
308
309 memcpy(rctx->buffer, data, len);
310
311out:
312 return 0;
309}
310
311/* Add padding and return the message digest. */
313}
314
315/* Add padding and return the message digest. */
312static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
316static int rmd160_final(struct shash_desc *desc, u8 *out)
313{
317{
314 struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
318 struct rmd160_ctx *rctx = shash_desc_ctx(desc);
315 u32 i, index, padlen;
316 __le64 bits;
317 __le32 *dst = (__le32 *)out;
318 static const u8 padding[64] = { 0x80, };
319
320 bits = cpu_to_le64(rctx->byte_count << 3);
321
322 /* Pad out to 56 mod 64 */
323 index = rctx->byte_count & 0x3f;
324 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
319 u32 i, index, padlen;
320 __le64 bits;
321 __le32 *dst = (__le32 *)out;
322 static const u8 padding[64] = { 0x80, };
323
324 bits = cpu_to_le64(rctx->byte_count << 3);
325
326 /* Pad out to 56 mod 64 */
327 index = rctx->byte_count & 0x3f;
328 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
325 rmd160_update(tfm, padding, padlen);
329 rmd160_update(desc, padding, padlen);
326
327 /* Append length */
330
331 /* Append length */
328 rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
332 rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
329
330 /* Store state in digest */
331 for (i = 0; i < 5; i++)
332 dst[i] = cpu_to_le32p(&rctx->state[i]);
333
334 /* Wipe context */
335 memset(rctx, 0, sizeof(*rctx));
333
334 /* Store state in digest */
335 for (i = 0; i < 5; i++)
336 dst[i] = cpu_to_le32p(&rctx->state[i]);
337
338 /* Wipe context */
339 memset(rctx, 0, sizeof(*rctx));
340
341 return 0;
336}
337
342}
343
338static struct crypto_alg alg = {
339 .cra_name = "rmd160",
340 .cra_driver_name = "rmd160",
341 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
342 .cra_blocksize = RMD160_BLOCK_SIZE,
343 .cra_ctxsize = sizeof(struct rmd160_ctx),
344 .cra_module = THIS_MODULE,
345 .cra_list = LIST_HEAD_INIT(alg.cra_list),
346 .cra_u = { .digest = {
347 .dia_digestsize = RMD160_DIGEST_SIZE,
348 .dia_init = rmd160_init,
349 .dia_update = rmd160_update,
350 .dia_final = rmd160_final } }
344static struct shash_alg alg = {
345 .digestsize = RMD160_DIGEST_SIZE,
346 .init = rmd160_init,
347 .update = rmd160_update,
348 .final = rmd160_final,
349 .descsize = sizeof(struct rmd160_ctx),
350 .base = {
351 .cra_name = "rmd160",
352 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
353 .cra_blocksize = RMD160_BLOCK_SIZE,
354 .cra_module = THIS_MODULE,
355 }
351};
352
353static int __init rmd160_mod_init(void)
354{
356};
357
358static int __init rmd160_mod_init(void)
359{
355 return crypto_register_alg(&alg);
360 return crypto_register_shash(&alg);
356}
357
358static void __exit rmd160_mod_fini(void)
359{
361}
362
363static void __exit rmd160_mod_fini(void)
364{
360 crypto_unregister_alg(&alg);
365 crypto_unregister_shash(&alg);
361}
362
363module_init(rmd160_mod_init);
364module_exit(rmd160_mod_fini);
365
366MODULE_LICENSE("GPL");
367MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
366}
367
368module_init(rmd160_mod_init);
369module_exit(rmd160_mod_fini);
370
371MODULE_LICENSE("GPL");
372MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
368
369MODULE_ALIAS("rmd160");