Commit 0a76c98f authored by Rusty Russell's avatar Rusty Russell

crypto/ripemd160: new module.

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 4e6c85ea
......@@ -48,6 +48,7 @@ MODS_WITH_SRC := antithread \
crc \
crcsync \
cpuid \
crypto/ripemd160 \
crypto/sha256 \
crypto/shachain \
daemonize \
......
../../../licenses/BSD-MIT
\ No newline at end of file
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* crypto/ripemd160 - implementation of RIPEMD 160 bit digest algorithm.
*
* This code is either a wrapper for openssl (if CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
* is defined) or an open-coded implementation based on Bitcoin's.
*
* License: BSD-MIT
* Maintainer: Rusty Russell <rusty@rustcorp.com.au>
*
* Example:
* #include <ccan/crypto/ripemd160/ripemd160.h>
* #include <err.h>
* #include <stdio.h>
* #include <string.h>
*
* // Simple demonstration: idential strings will have the same hash, but
* // two different strings will not.
* int main(int argc, char *argv[])
* {
* struct ripemd160 hash1, hash2;
*
* if (argc != 3)
* errx(1, "Usage: %s <string1> <string2>", argv[0]);
*
* ripemd160(&hash1, argv[1], strlen(argv[1]));
* ripemd160(&hash2, argv[2], strlen(argv[2]));
* printf("Hash is %s\n", memcmp(&hash1, &hash2, sizeof(hash1))
* ? "different" : "same");
* return 0;
* }
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/endian\n");
return 0;
}
if (strcmp(argv[1], "libs") == 0) {
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
printf("crypto\n");
#endif
return 0;
}
return 1;
}
/* MIT (BSD) license - see LICENSE file for details */
/* RIPEMD core code translated from the Bitcoin project's C++:
*
* src/crypto/ripemd160.cpp commit f914f1a746d7f91951c1da262a4a749dd3ebfa71
* Copyright (c) 2014 The Bitcoin Core developers
* Distributed under the MIT software license, see the accompanying
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/endian/endian.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
static void invalidate_ripemd160(struct ripemd160_ctx *ctx)
{
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
ctx->c.num = -1U;
#else
ctx->bytes = -1ULL;
#endif
}
static void check_ripemd160(struct ripemd160_ctx *ctx)
{
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
assert(ctx->c.num != -1U);
#else
assert(ctx->bytes != -1ULL);
#endif
}
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
void ripemd160_init(struct ripemd160_ctx *ctx)
{
RIPEMD160_Init(&ctx->c);
}
void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
{
check_ripemd160(ctx);
RIPEMD160_Update(&ctx->c, p, size);
}
void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
{
RIPEMD160_Final(res->u.u8, &ctx->c);
invalidate_ripemd160(ctx);
}
#else
static uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
static uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); }
static uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; }
static uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); }
static uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); }
/** Initialize RIPEMD-160 state. */
static void inline Initialize(uint32_t* s)
{
s[0] = 0x67452301ul;
s[1] = 0xEFCDAB89ul;
s[2] = 0x98BADCFEul;
s[3] = 0x10325476ul;
s[4] = 0xC3D2E1F0ul;
}
static uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); }
static void inline Round(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)
{
*a = rol(*a + f + x + k, r) + e;
*c = rol(*c, 10);
}
static void inline R11(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); }
static void inline R21(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x5A827999ul, r); }
static void inline R31(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6ED9EBA1ul, r); }
static void inline R41(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x8F1BBCDCul, r); }
static void inline R51(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0xA953FD4Eul, r); }
static void inline R12(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0x50A28BE6ul, r); }
static void inline R22(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x5C4DD124ul, r); }
static void inline R32(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6D703EF3ul, r); }
static void inline R42(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x7A6D76E9ul, r); }
static void inline R52(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); }
/** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */
static void Transform(uint32_t *s, const uint32_t *chunk)
{
uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
uint32_t w0 = le32_to_cpu(chunk[0]), w1 = le32_to_cpu(chunk[1]), w2 = le32_to_cpu(chunk[2]), w3 = le32_to_cpu(chunk[3]);
uint32_t w4 = le32_to_cpu(chunk[4]), w5 = le32_to_cpu(chunk[5]), w6 = le32_to_cpu(chunk[6]), w7 = le32_to_cpu(chunk[7]);
uint32_t w8 = le32_to_cpu(chunk[8]), w9 = le32_to_cpu(chunk[9]), w10 = le32_to_cpu(chunk[10]), w11 = le32_to_cpu(chunk[11]);
uint32_t w12 = le32_to_cpu(chunk[12]), w13 = le32_to_cpu(chunk[13]), w14 = le32_to_cpu(chunk[14]), w15 = le32_to_cpu(chunk[15]);
R11(&a1, b1, &c1, d1, e1, w0, 11);
R12(&a2, b2, &c2, d2, e2, w5, 8);
R11(&e1, a1, &b1, c1, d1, w1, 14);
R12(&e2, a2, &b2, c2, d2, w14, 9);
R11(&d1, e1, &a1, b1, c1, w2, 15);
R12(&d2, e2, &a2, b2, c2, w7, 9);
R11(&c1, d1, &e1, a1, b1, w3, 12);
R12(&c2, d2, &e2, a2, b2, w0, 11);
R11(&b1, c1, &d1, e1, a1, w4, 5);
R12(&b2, c2, &d2, e2, a2, w9, 13);
R11(&a1, b1, &c1, d1, e1, w5, 8);
R12(&a2, b2, &c2, d2, e2, w2, 15);
R11(&e1, a1, &b1, c1, d1, w6, 7);
R12(&e2, a2, &b2, c2, d2, w11, 15);
R11(&d1, e1, &a1, b1, c1, w7, 9);
R12(&d2, e2, &a2, b2, c2, w4, 5);
R11(&c1, d1, &e1, a1, b1, w8, 11);
R12(&c2, d2, &e2, a2, b2, w13, 7);
R11(&b1, c1, &d1, e1, a1, w9, 13);
R12(&b2, c2, &d2, e2, a2, w6, 7);
R11(&a1, b1, &c1, d1, e1, w10, 14);
R12(&a2, b2, &c2, d2, e2, w15, 8);
R11(&e1, a1, &b1, c1, d1, w11, 15);
R12(&e2, a2, &b2, c2, d2, w8, 11);
R11(&d1, e1, &a1, b1, c1, w12, 6);
R12(&d2, e2, &a2, b2, c2, w1, 14);
R11(&c1, d1, &e1, a1, b1, w13, 7);
R12(&c2, d2, &e2, a2, b2, w10, 14);
R11(&b1, c1, &d1, e1, a1, w14, 9);
R12(&b2, c2, &d2, e2, a2, w3, 12);
R11(&a1, b1, &c1, d1, e1, w15, 8);
R12(&a2, b2, &c2, d2, e2, w12, 6);
R21(&e1, a1, &b1, c1, d1, w7, 7);
R22(&e2, a2, &b2, c2, d2, w6, 9);
R21(&d1, e1, &a1, b1, c1, w4, 6);
R22(&d2, e2, &a2, b2, c2, w11, 13);
R21(&c1, d1, &e1, a1, b1, w13, 8);
R22(&c2, d2, &e2, a2, b2, w3, 15);
R21(&b1, c1, &d1, e1, a1, w1, 13);
R22(&b2, c2, &d2, e2, a2, w7, 7);
R21(&a1, b1, &c1, d1, e1, w10, 11);
R22(&a2, b2, &c2, d2, e2, w0, 12);
R21(&e1, a1, &b1, c1, d1, w6, 9);
R22(&e2, a2, &b2, c2, d2, w13, 8);
R21(&d1, e1, &a1, b1, c1, w15, 7);
R22(&d2, e2, &a2, b2, c2, w5, 9);
R21(&c1, d1, &e1, a1, b1, w3, 15);
R22(&c2, d2, &e2, a2, b2, w10, 11);
R21(&b1, c1, &d1, e1, a1, w12, 7);
R22(&b2, c2, &d2, e2, a2, w14, 7);
R21(&a1, b1, &c1, d1, e1, w0, 12);
R22(&a2, b2, &c2, d2, e2, w15, 7);
R21(&e1, a1, &b1, c1, d1, w9, 15);
R22(&e2, a2, &b2, c2, d2, w8, 12);
R21(&d1, e1, &a1, b1, c1, w5, 9);
R22(&d2, e2, &a2, b2, c2, w12, 7);
R21(&c1, d1, &e1, a1, b1, w2, 11);
R22(&c2, d2, &e2, a2, b2, w4, 6);
R21(&b1, c1, &d1, e1, a1, w14, 7);
R22(&b2, c2, &d2, e2, a2, w9, 15);
R21(&a1, b1, &c1, d1, e1, w11, 13);
R22(&a2, b2, &c2, d2, e2, w1, 13);
R21(&e1, a1, &b1, c1, d1, w8, 12);
R22(&e2, a2, &b2, c2, d2, w2, 11);
R31(&d1, e1, &a1, b1, c1, w3, 11);
R32(&d2, e2, &a2, b2, c2, w15, 9);
R31(&c1, d1, &e1, a1, b1, w10, 13);
R32(&c2, d2, &e2, a2, b2, w5, 7);
R31(&b1, c1, &d1, e1, a1, w14, 6);
R32(&b2, c2, &d2, e2, a2, w1, 15);
R31(&a1, b1, &c1, d1, e1, w4, 7);
R32(&a2, b2, &c2, d2, e2, w3, 11);
R31(&e1, a1, &b1, c1, d1, w9, 14);
R32(&e2, a2, &b2, c2, d2, w7, 8);
R31(&d1, e1, &a1, b1, c1, w15, 9);
R32(&d2, e2, &a2, b2, c2, w14, 6);
R31(&c1, d1, &e1, a1, b1, w8, 13);
R32(&c2, d2, &e2, a2, b2, w6, 6);
R31(&b1, c1, &d1, e1, a1, w1, 15);
R32(&b2, c2, &d2, e2, a2, w9, 14);
R31(&a1, b1, &c1, d1, e1, w2, 14);
R32(&a2, b2, &c2, d2, e2, w11, 12);
R31(&e1, a1, &b1, c1, d1, w7, 8);
R32(&e2, a2, &b2, c2, d2, w8, 13);
R31(&d1, e1, &a1, b1, c1, w0, 13);
R32(&d2, e2, &a2, b2, c2, w12, 5);
R31(&c1, d1, &e1, a1, b1, w6, 6);
R32(&c2, d2, &e2, a2, b2, w2, 14);
R31(&b1, c1, &d1, e1, a1, w13, 5);
R32(&b2, c2, &d2, e2, a2, w10, 13);
R31(&a1, b1, &c1, d1, e1, w11, 12);
R32(&a2, b2, &c2, d2, e2, w0, 13);
R31(&e1, a1, &b1, c1, d1, w5, 7);
R32(&e2, a2, &b2, c2, d2, w4, 7);
R31(&d1, e1, &a1, b1, c1, w12, 5);
R32(&d2, e2, &a2, b2, c2, w13, 5);
R41(&c1, d1, &e1, a1, b1, w1, 11);
R42(&c2, d2, &e2, a2, b2, w8, 15);
R41(&b1, c1, &d1, e1, a1, w9, 12);
R42(&b2, c2, &d2, e2, a2, w6, 5);
R41(&a1, b1, &c1, d1, e1, w11, 14);
R42(&a2, b2, &c2, d2, e2, w4, 8);
R41(&e1, a1, &b1, c1, d1, w10, 15);
R42(&e2, a2, &b2, c2, d2, w1, 11);
R41(&d1, e1, &a1, b1, c1, w0, 14);
R42(&d2, e2, &a2, b2, c2, w3, 14);
R41(&c1, d1, &e1, a1, b1, w8, 15);
R42(&c2, d2, &e2, a2, b2, w11, 14);
R41(&b1, c1, &d1, e1, a1, w12, 9);
R42(&b2, c2, &d2, e2, a2, w15, 6);
R41(&a1, b1, &c1, d1, e1, w4, 8);
R42(&a2, b2, &c2, d2, e2, w0, 14);
R41(&e1, a1, &b1, c1, d1, w13, 9);
R42(&e2, a2, &b2, c2, d2, w5, 6);
R41(&d1, e1, &a1, b1, c1, w3, 14);
R42(&d2, e2, &a2, b2, c2, w12, 9);
R41(&c1, d1, &e1, a1, b1, w7, 5);
R42(&c2, d2, &e2, a2, b2, w2, 12);
R41(&b1, c1, &d1, e1, a1, w15, 6);
R42(&b2, c2, &d2, e2, a2, w13, 9);
R41(&a1, b1, &c1, d1, e1, w14, 8);
R42(&a2, b2, &c2, d2, e2, w9, 12);
R41(&e1, a1, &b1, c1, d1, w5, 6);
R42(&e2, a2, &b2, c2, d2, w7, 5);
R41(&d1, e1, &a1, b1, c1, w6, 5);
R42(&d2, e2, &a2, b2, c2, w10, 15);
R41(&c1, d1, &e1, a1, b1, w2, 12);
R42(&c2, d2, &e2, a2, b2, w14, 8);
R51(&b1, c1, &d1, e1, a1, w4, 9);
R52(&b2, c2, &d2, e2, a2, w12, 8);
R51(&a1, b1, &c1, d1, e1, w0, 15);
R52(&a2, b2, &c2, d2, e2, w15, 5);
R51(&e1, a1, &b1, c1, d1, w5, 5);
R52(&e2, a2, &b2, c2, d2, w10, 12);
R51(&d1, e1, &a1, b1, c1, w9, 11);
R52(&d2, e2, &a2, b2, c2, w4, 9);
R51(&c1, d1, &e1, a1, b1, w7, 6);
R52(&c2, d2, &e2, a2, b2, w1, 12);
R51(&b1, c1, &d1, e1, a1, w12, 8);
R52(&b2, c2, &d2, e2, a2, w5, 5);
R51(&a1, b1, &c1, d1, e1, w2, 13);
R52(&a2, b2, &c2, d2, e2, w8, 14);
R51(&e1, a1, &b1, c1, d1, w10, 12);
R52(&e2, a2, &b2, c2, d2, w7, 6);
R51(&d1, e1, &a1, b1, c1, w14, 5);
R52(&d2, e2, &a2, b2, c2, w6, 8);
R51(&c1, d1, &e1, a1, b1, w1, 12);
R52(&c2, d2, &e2, a2, b2, w2, 13);
R51(&b1, c1, &d1, e1, a1, w3, 13);
R52(&b2, c2, &d2, e2, a2, w13, 6);
R51(&a1, b1, &c1, d1, e1, w8, 14);
R52(&a2, b2, &c2, d2, e2, w14, 5);
R51(&e1, a1, &b1, c1, d1, w11, 11);
R52(&e2, a2, &b2, c2, d2, w0, 15);
R51(&d1, e1, &a1, b1, c1, w6, 8);
R52(&d2, e2, &a2, b2, c2, w3, 13);
R51(&c1, d1, &e1, a1, b1, w15, 5);
R52(&c2, d2, &e2, a2, b2, w9, 11);
R51(&b1, c1, &d1, e1, a1, w13, 6);
R52(&b2, c2, &d2, e2, a2, w11, 11);
uint32_t t = s[0];
s[0] = s[1] + c1 + d2;
s[1] = s[2] + d1 + e2;
s[2] = s[3] + e1 + a2;
s[3] = s[4] + a1 + b2;
s[4] = t + b1 + c2;
}
static bool alignment_ok(const void *p, size_t n)
{
#if HAVE_UNALIGNED_ACCESS
return true;
#else
return ((size_t)p % n == 0);
#endif
}
static void add(struct ripemd160_ctx *ctx, const void *p, size_t len)
{
const unsigned char *data = p;
size_t bufsize = ctx->bytes % 64;
if (bufsize + len >= 64) {
// Fill the buffer, and process it.
memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
ctx->bytes += 64 - bufsize;
data += 64 - bufsize;
len -= 64 - bufsize;
Transform(ctx->s, ctx->buf.u32);
bufsize = 0;
}
while (len >= 64) {
// Process full chunks directly from the source.
if (alignment_ok(data, sizeof(uint32_t)))
Transform(ctx->s, (const uint32_t *)data);
else {
memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
Transform(ctx->s, ctx->buf.u32);
}
ctx->bytes += 64;
data += 64;
len -= 64;
}
if (len) {
// Fill the buffer with what remains.
memcpy(ctx->buf.u8 + bufsize, data, len);
ctx->bytes += len;
}
}
void ripemd160_init(struct ripemd160_ctx *ctx)
{
struct ripemd160_ctx init = RIPEMD160_INIT;
*ctx = init;
}
void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
{
check_ripemd160(ctx);
add(ctx, p, size);
}
void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
{
static const unsigned char pad[64] = {0x80};
uint64_t sizedesc;
size_t i;
sizedesc = cpu_to_le64(ctx->bytes << 3);
/* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
/* Add number of bits of data (big endian) */
add(ctx, &sizedesc, 8);
for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
res->u.u32[i] = cpu_to_le32(ctx->s[i]);
invalidate_ripemd160(ctx);
}
#endif
void ripemd160(struct ripemd160 *sha, const void *p, size_t size)
{
struct ripemd160_ctx ctx;
ripemd160_init(&ctx);
ripemd160_update(&ctx, p, size);
ripemd160_done(&ctx, sha);
}
void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v)
{
ripemd160_update(ctx, &v, sizeof(v));
}
void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v)
{
ripemd160_update(ctx, &v, sizeof(v));
}
void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v)
{
ripemd160_update(ctx, &v, sizeof(v));
}
void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v)
{
ripemd160_update(ctx, &v, sizeof(v));
}
/* Add as little-endian */
void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v)
{
leint16_t lev = cpu_to_le16(v);
ripemd160_update(ctx, &lev, sizeof(lev));
}
void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v)
{
leint32_t lev = cpu_to_le32(v);
ripemd160_update(ctx, &lev, sizeof(lev));
}
void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v)
{
leint64_t lev = cpu_to_le64(v);
ripemd160_update(ctx, &lev, sizeof(lev));
}
/* Add as big-endian */
void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v)
{
beint16_t bev = cpu_to_be16(v);
ripemd160_update(ctx, &bev, sizeof(bev));
}
void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v)
{
beint32_t bev = cpu_to_be32(v);
ripemd160_update(ctx, &bev, sizeof(bev));
}
void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v)
{
beint64_t bev = cpu_to_be64(v);
ripemd160_update(ctx, &bev, sizeof(bev));
}
#ifndef CCAN_CRYPTO_RIPEMD160_H
#define CCAN_CRYPTO_RIPEMD160_H
/* BSD-MIT - see LICENSE file for details */
#include "config.h"
#include <stdint.h>
#include <stdlib.h>
/* Uncomment this to use openssl's RIPEMD160 routines (and link with -lcrypto) */
//#define CCAN_CRYPTO_RIPEMD160_USE_OPENSSL 1
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
#include <openssl/ripemd.h>
#endif
/**
* struct ripemd160 - structure representing a completed RIPEMD160.
* @u.u8: an unsigned char array.
* @u.u32: a 32-bit integer array.
*
* Other fields may be added to the union in future.
*/
struct ripemd160 {
union {
/* Array of chars */
unsigned char u8[20];
/* Array of uint32_t */
uint32_t u32[5];
} u;
};
/**
* ripemd160 - return ripemd160 of an object.
* @ripemd160: the ripemd160 to fill in
* @p: pointer to memory,
* @size: the number of bytes pointed to by @p
*
* The bytes pointed to by @p is RIPEMD160 hashed into @ripemd160. This is
* equivalent to ripemd160_init(), ripemd160_update() then ripemd160_done().
*/
void ripemd160(struct ripemd160 *sha, const void *p, size_t size);
/**
* struct ripemd160_ctx - structure to store running context for ripemd160
*/
struct ripemd160_ctx {
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
RIPEMD160_CTX c;
#else
uint32_t s[5];
uint64_t bytes;
union {
uint32_t u32[8];
unsigned char u8[64];
} buf;
#endif
};
/**
* ripemd160_init - initialize an RIPEMD160 context.
* @ctx: the ripemd160_ctx to initialize
*
* This must be called before ripemd160_update or ripemd160_done, or
* alternately you can assign RIPEMD160_INIT.
*
* If it was already initialized, this forgets anything which was
* hashed before.
*
* Example:
* static void hash_all(const char **arr, struct ripemd160 *hash)
* {
* size_t i;
* struct ripemd160_ctx ctx;
*
* ripemd160_init(&ctx);
* for (i = 0; arr[i]; i++)
* ripemd160_update(&ctx, arr[i], strlen(arr[i]));
* ripemd160_done(&ctx, hash);
* }
*/
void ripemd160_init(struct ripemd160_ctx *ctx);
/**
* RIPEMD160_INIT - initializer for an RIPEMD160 context.
*
* This can be used to staticly initialize an RIPEMD160 context (instead
* of ripemd160_init()).
*
* Example:
* static void hash_all(const char **arr, struct ripemd160 *hash)
* {
* size_t i;
* struct ripemd160_ctx ctx = RIPEMD160_INIT;
*
* for (i = 0; arr[i]; i++)
* ripemd160_update(&ctx, arr[i], strlen(arr[i]));
* ripemd160_done(&ctx, hash);
* }
*/
#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
#define RIPEMD160_INIT \
{ { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul, \
0xC3D2E1F0ul, \
0x0, 0x0, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
0 } }
#else
#define RIPEMD160_INIT \
{ { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul, \
0xC3D2E1F0ul }, 0 }
#endif
/**
* ripemd160_update - include some memory in the hash.
* @ctx: the ripemd160_ctx to use
* @p: pointer to memory,
* @size: the number of bytes pointed to by @p
*
* You can call this multiple times to hash more data, before calling
* ripemd160_done().
*/
void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size);
/**
* ripemd160_done - finish RIPEMD160 and return the hash
* @ctx: the ripemd160_ctx to complete
* @res: the hash to return.
*
* Note that @ctx is *destroyed* by this, and must be reinitialized.
* To avoid that, pass a copy instead.
*/
void ripemd160_done(struct ripemd160_ctx *ripemd160, struct ripemd160 *res);
/* Add various types to an RIPEMD160 hash */
void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v);
void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v);
void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v);
void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v);
/* Add as little-endian */
void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v);
void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v);
void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v);
/* Add as big-endian */
void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v);
void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v);
void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v);
#endif /* CCAN_CRYPTO_RIPEMD160_H */
#include <ccan/crypto/ripemd160/ripemd160.h>
/* Include the C files directly. */
#include <ccan/crypto/ripemd160/ripemd160.c>
#include <ccan/tap/tap.h>
int main(void)
{
struct ripemd160 h, expected;
static const char zeroes[1000];
size_t i;
plan_tests(63);
/* Test different alignments. */
ripemd160(&expected, zeroes, sizeof(zeroes) - 64);
for (i = 1; i < 64; i++) {
ripemd160(&h, zeroes + i, sizeof(zeroes) - 64);
ok1(memcmp(&h, &expected, sizeof(h)) == 0);
}
/* This exits depending on whether all tests passed */
return exit_status();
}
#include <ccan/crypto/ripemd160/ripemd160.h>
/* Include the C files directly. */
#include <ccan/crypto/ripemd160/ripemd160.c>
#include <ccan/tap/tap.h>
/* Test vectors. */
struct test {
const char *test;
size_t repetitions;
beint32_t result[5];
};
/* Test vectors from: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html */
static struct test tests[] = {
{ "", 1,
{ CPU_TO_BE32(0x9c1185a5), CPU_TO_BE32(0xc5e9fc54),
CPU_TO_BE32(0x61280897), CPU_TO_BE32(0x7ee8f548),
CPU_TO_BE32(0xb2258d31) } },
{ "abc", 1,
{ CPU_TO_BE32(0x8eb208f7), CPU_TO_BE32(0xe05d987a),
CPU_TO_BE32(0x9b044a8e), CPU_TO_BE32(0x98c6b087),
CPU_TO_BE32(0xf15a0bfc) } },
{ "message digest", 1,
{ CPU_TO_BE32(0x5d0689ef), CPU_TO_BE32(0x49d2fae5),
CPU_TO_BE32(0x72b881b1), CPU_TO_BE32(0x23a85ffa),
CPU_TO_BE32(0x21595f36) } },
{ "abcdefghijklmnopqrstuvwxyz", 1,
{ CPU_TO_BE32(0xf71c2710), CPU_TO_BE32(0x9c692c1b),
CPU_TO_BE32(0x56bbdceb), CPU_TO_BE32(0x5b9d2865),
CPU_TO_BE32(0xb3708dbc) } },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
{ CPU_TO_BE32(0x12a05338), CPU_TO_BE32(0x4a9c0c88),
CPU_TO_BE32(0xe405a06c), CPU_TO_BE32(0x27dcf49a),
CPU_TO_BE32(0xda62eb2b) } },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
{ CPU_TO_BE32(0xb0e20b6e), CPU_TO_BE32(0x31166402),
CPU_TO_BE32(0x86ed3a87), CPU_TO_BE32(0xa5713079),
CPU_TO_BE32(0xb21f5189) } },
{ "1234567890", 8,
{ CPU_TO_BE32(0x9b752e45), CPU_TO_BE32(0x573d4b39),
CPU_TO_BE32(0xf4dbd332), CPU_TO_BE32(0x3cab82bf),
CPU_TO_BE32(0x63326bfb) } },
{ "a", 1000000,
{ CPU_TO_BE32(0x52783243), CPU_TO_BE32(0xc1697bdb),
CPU_TO_BE32(0xe16d37f9), CPU_TO_BE32(0x7f68f083),
CPU_TO_BE32(0x25dc1528) } }
};
static bool do_test(const struct test *t, bool single)
{
struct ripemd160 h;
if (single) {
if (t->repetitions != 1)
return true;
ripemd160(&h, t->test, strlen(t->test));
} else {
struct ripemd160_ctx ctx = RIPEMD160_INIT;
size_t i;
for (i = 0; i < t->repetitions; i++)
ripemd160_update(&ctx, t->test, strlen(t->test));
ripemd160_done(&ctx, &h);
}
return memcmp(&h.u, t->result, sizeof(t->result)) == 0;
}
int main(void)
{
size_t i;
/* This is how many tests you plan to run */
plan_tests(sizeof(tests) / sizeof(struct test) * 2);
for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
ok1(do_test(&tests[i], false));
for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
ok1(do_test(&tests[i], true));
/* This exits depending on whether all tests passed */
return exit_status();
}
#include <ccan/crypto/ripemd160/ripemd160.h>
/* Include the C files directly. */
#include <ccan/crypto/ripemd160/ripemd160.c>
#include <ccan/tap/tap.h>
static unsigned char arr[] = {
0x12,
#if HAVE_BIG_ENDIAN
/* u16 */
0x12, 0x34,
/* u32 */
0x12, 0x34, 0x56, 0x78,
/* u64 */
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
#else
/* u16 */
0x34, 0x12,
/* u32 */
0x78, 0x56, 0x34, 0x12,
/* u64 */
0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12,
#endif
/* le16 */
0x34, 0x12,
/* le32 */
0x78, 0x56, 0x34, 0x12,
/* le64 */
0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12,
/* be16 */
0x12, 0x34,
/* be32 */
0x12, 0x34, 0x56, 0x78,
/* be64 */
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
};
int main(void)
{
struct ripemd160 h, expected;
struct ripemd160_ctx ctx;
/* This is how many tests you plan to run */
plan_tests(1);
ripemd160_init(&ctx);
ripemd160_u8(&ctx, 0x12);
ripemd160_u16(&ctx, 0x1234);
ripemd160_u32(&ctx, 0x12345678);
ripemd160_u64(&ctx, 0x123456789abcdef0ULL);
ripemd160_le16(&ctx, 0x1234);
ripemd160_le32(&ctx, 0x12345678);
ripemd160_le64(&ctx, 0x123456789abcdef0ULL);
ripemd160_be16(&ctx, 0x1234);
ripemd160_be32(&ctx, 0x12345678);
ripemd160_be64(&ctx, 0x123456789abcdef0ULL);
ripemd160_done(&ctx, &h);
ripemd160(&expected, arr, sizeof(arr));
ok1(memcmp(&h, &expected, sizeof(h)) == 0);
/* This exits depending on whether all tests passed */
return exit_status();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment