📄 rmd160.c
字号:
// RIPEMD-160 written and placed in the public domain by Wei Dai
/*
* This code implements the MD4 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
* written by Colin Plumb in 1993, no copyright is claimed.
* This code is in the public domain; do with it what you wish.
*/
/* Adapted by TrueCrypt Foundation */
#include <memory.h>
#include "Common/Tcdefs.h"
#include "Common/Endian.h"
#include "Rmd160.h"
#define F(x, y, z) (x ^ y ^ z)
#define G(x, y, z) (z ^ (x & (y^z)))
#define H(x, y, z) (z ^ (x | ~y))
#define I(x, y, z) (y ^ (z & (x^y)))
#define J(x, y, z) (x ^ (y | ~z))
#define PUT_64BIT_LE(cp, value) do { \
(cp)[7] = (byte) ((value) >> 56); \
(cp)[6] = (byte) ((value) >> 48); \
(cp)[5] = (byte) ((value) >> 40); \
(cp)[4] = (byte) ((value) >> 32); \
(cp)[3] = (byte) ((value) >> 24); \
(cp)[2] = (byte) ((value) >> 16); \
(cp)[1] = (byte) ((value) >> 8); \
(cp)[0] = (byte) (value); } while (0)
#define PUT_32BIT_LE(cp, value) do { \
(cp)[3] = (byte) ((value) >> 24); \
(cp)[2] = (byte) ((value) >> 16); \
(cp)[1] = (byte) ((value) >> 8); \
(cp)[0] = (byte) (value); } while (0)
#ifndef TC_MINIMIZE_CODE_SIZE
static byte PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#else
static byte PADDING[64];
#endif
void RMD160Init (RMD160_CTX *ctx)
{
ctx->count = 0;
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xefcdab89;
ctx->state[2] = 0x98badcfe;
ctx->state[3] = 0x10325476;
ctx->state[4] = 0xc3d2e1f0;
PADDING[0] = 0x80;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void RMD160Update (RMD160_CTX *ctx, const unsigned char *input, unsigned __int32 len)
{
size_t have, need;
/* Check how many bytes we already have and how many more we need. */
have = (size_t)((ctx->count >> 3) & (RIPEMD160_BLOCK_LENGTH - 1));
need = RIPEMD160_BLOCK_LENGTH - have;
/* Update bitcount */
ctx->count +=
#ifndef TC_NO_COMPILER_INT64
(uint64)
#endif
len << 3;
if (len >= need) {
if (have != 0) {
memcpy (ctx->buffer + have, input, need);
RMD160Transform ((uint32 *) ctx->state, (const uint32 *) ctx->buffer);
input += need;
len -= need;
have = 0;
}
/* Process data in RIPEMD160_BLOCK_LENGTH-byte chunks. */
while (len >= RIPEMD160_BLOCK_LENGTH) {
RMD160Transform ((uint32 *) ctx->state, (const uint32 *) input);
input += RIPEMD160_BLOCK_LENGTH;
len -= RIPEMD160_BLOCK_LENGTH;
}
}
/* Handle any remaining bytes of data. */
if (len != 0)
memcpy (ctx->buffer + have, input, (size_t) len);
}
/*
* Pad pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
static void RMD160Pad(RMD160_CTX *ctx)
{
byte count[8];
size_t padlen;
/* Convert count to 8 bytes in little endian order. */
#ifndef TC_NO_COMPILER_INT64
PUT_64BIT_LE(count, ctx->count);
#else
*(unsigned __int32 *) (count + 4) = 0;
PUT_32BIT_LE(count, ctx->count);
#endif
/* Pad out to 56 mod 64. */
padlen = RIPEMD160_BLOCK_LENGTH -
(size_t)((ctx->count >> 3) & (RIPEMD160_BLOCK_LENGTH - 1));
if (padlen < 1 + 8)
padlen += RIPEMD160_BLOCK_LENGTH;
RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
RMD160Update(ctx, count, 8);
}
/*
* Final wrapup--call RMD160Pad, fill in digest and zero out ctx.
*/
void RMD160Final(unsigned char *digest, RMD160_CTX *ctx)
{
int i;
RMD160Pad(ctx);
if (digest) {
for (i = 0; i < 5; i++)
PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
memset (ctx, 0, sizeof(*ctx));
}
}
#ifndef TC_MINIMIZE_CODE_SIZE
#define word32 unsigned __int32
#define k0 0
#define k1 0x5a827999UL
#define k2 0x6ed9eba1UL
#define k3 0x8f1bbcdcUL
#define k4 0xa953fd4eUL
#define k5 0x50a28be6UL
#define k6 0x5c4dd124UL
#define k7 0x6d703ef3UL
#define k8 0x7a6d76e9UL
#define k9 0
static word32 rotlFixed (word32 x, unsigned int y)
{
return (word32)((x<<y) | (x>>(sizeof(word32)*8-y)));
}
#define Subround(f, a, b, c, d, e, x, s, k) \
a += f(b, c, d) + x + k;\
a = rotlFixed((word32)a, s) + e;\
c = rotlFixed((word32)c, 10U)
void RMD160Transform (unsigned __int32 *digest, const unsigned __int32 *data)
{
#if BYTE_ORDER == LITTLE_ENDIAN
const word32 *X = data;
#else
word32 X[16];
int i;
#endif
word32 a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
a1 = a2 = digest[0];
b1 = b2 = digest[1];
c1 = c2 = digest[2];
d1 = d2 = digest[3];
e1 = e2 = digest[4];
#if BYTE_ORDER == BIG_ENDIAN
for (i = 0; i < 16; i++)
{
X[i] = LE32 (data[i]);
}
#endif
Subround(F, a1, b1, c1, d1, e1, X[ 0], 11, k0);
Subround(F, e1, a1, b1, c1, d1, X[ 1], 14, k0);
Subround(F, d1, e1, a1, b1, c1, X[ 2], 15, k0);
Subround(F, c1, d1, e1, a1, b1, X[ 3], 12, k0);
Subround(F, b1, c1, d1, e1, a1, X[ 4], 5, k0);
Subround(F, a1, b1, c1, d1, e1, X[ 5], 8, k0);
Subround(F, e1, a1, b1, c1, d1, X[ 6], 7, k0);
Subround(F, d1, e1, a1, b1, c1, X[ 7], 9, k0);
Subround(F, c1, d1, e1, a1, b1, X[ 8], 11, k0);
Subround(F, b1, c1, d1, e1, a1, X[ 9], 13, k0);
Subround(F, a1, b1, c1, d1, e1, X[10], 14, k0);
Subround(F, e1, a1, b1, c1, d1, X[11], 15, k0);
Subround(F, d1, e1, a1, b1, c1, X[12], 6, k0);
Subround(F, c1, d1, e1, a1, b1, X[13], 7, k0);
Subround(F, b1, c1, d1, e1, a1, X[14], 9, k0);
Subround(F, a1, b1, c1, d1, e1, X[15], 8, k0);
Subround(G, e1, a1, b1, c1, d1, X[ 7], 7, k1);
Subround(G, d1, e1, a1, b1, c1, X[ 4], 6, k1);
Subround(G, c1, d1, e1, a1, b1, X[13], 8, k1);
Subround(G, b1, c1, d1, e1, a1, X[ 1], 13, k1);
Subround(G, a1, b1, c1, d1, e1, X[10], 11, k1);
Subround(G, e1, a1, b1, c1, d1, X[ 6], 9, k1);
Subround(G, d1, e1, a1, b1, c1, X[15], 7, k1);
Subround(G, c1, d1, e1, a1, b1, X[ 3], 15, k1);
Subround(G, b1, c1, d1, e1, a1, X[12], 7, k1);
Subround(G, a1, b1, c1, d1, e1, X[ 0], 12, k1);
Subround(G, e1, a1, b1, c1, d1, X[ 9], 15, k1);
Subround(G, d1, e1, a1, b1, c1, X[ 5], 9, k1);
Subround(G, c1, d1, e1, a1, b1, X[ 2], 11, k1);
Subround(G, b1, c1, d1, e1, a1, X[14], 7, k1);
Subround(G, a1, b1, c1, d1, e1, X[11], 13, k1);
Subround(G, e1, a1, b1, c1, d1, X[ 8], 12, k1);
Subround(H, d1, e1, a1, b1, c1, X[ 3], 11, k2);
Subround(H, c1, d1, e1, a1, b1, X[10], 13, k2);
Subround(H, b1, c1, d1, e1, a1, X[14], 6, k2);
Subround(H, a1, b1, c1, d1, e1, X[ 4], 7, k2);
Subround(H, e1, a1, b1, c1, d1, X[ 9], 14, k2);
Subround(H, d1, e1, a1, b1, c1, X[15], 9, k2);
Subround(H, c1, d1, e1, a1, b1, X[ 8], 13, k2);
Subround(H, b1, c1, d1, e1, a1, X[ 1], 15, k2);
Subround(H, a1, b1, c1, d1, e1, X[ 2], 14, k2);
Subround(H, e1, a1, b1, c1, d1, X[ 7], 8, k2);
Subround(H, d1, e1, a1, b1, c1, X[ 0], 13, k2);
Subround(H, c1, d1, e1, a1, b1, X[ 6], 6, k2);
Subround(H, b1, c1, d1, e1, a1, X[13], 5, k2);
Subround(H, a1, b1, c1, d1, e1, X[11], 12, k2);
Subround(H, e1, a1, b1, c1, d1, X[ 5], 7, k2);
Subround(H, d1, e1, a1, b1, c1, X[12], 5, k2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -