📄 ripemd.c
字号:
for (i = 0; i < 20; i += 4) { *(HashRes+i) = (UCHAR)hws->stomach[i>>2]; *(HashRes+i+1) = (UCHAR)(hws->stomach[i>>2] >> 8); /*lint !e661 !e662 */ *(HashRes+i+2) = (UCHAR)(hws->stomach[i>>2] >> 16); /*lint !e661 !e662 */ *(HashRes+i+3) = (UCHAR)(hws->stomach[i>>2] >> 24); /*lint !e661 !e662 */ } return;}/*****************************************************************************//* RIPEMD-128 API *//*****************************************************************************//******************************************************************************//* *//* Function: Hashing of a message of length byte in a single step *//* Syntax: void *//* ripemd128_l (UCHAR *HashRes, UCHAR *clear, ULONG length); *//* Input: UCHAR *clear (Pointer to message block) *//* ULONG length (Length of message block) *//* Output: UCHAR *HashRes (Hash Value) *//* Returns: - *//* *//******************************************************************************/void __FLINT_APIripemd128_l (UCHAR *HashRes, UCHAR *clear, ULONG length){ RMDSTAT hws; ripeinit128_l (&hws); ripefinish128_l (HashRes, &hws, clear, length);#ifdef FLINT_SECURE ZeroUcharArray (&hws, sizeof (hws));#endif}/******************************************************************************//* Functions for blockwise hashing a message in several steps *//* Procedure: Initialization with ripeinit *//* Hashing of block1, block2,... ,blockn with function ripehash *//* Finish operation with function ripefinish *//******************************************************************************//******************************************************************************//* *//* Function: Initialization of RIPEMD-128 function *//* Syntax: void ripeinit128_l (RMDSTAT *hws); *//* Input: RMDSTAT *hws (RIPEMD status buffer) *//* Output: - *//* Returns: - *//* *//******************************************************************************/void __FLINT_APIripeinit128_l (RMDSTAT *hws){ appetize128 (hws->stomach); hws->total[0] = 0; hws->total[1] = 0;}/******************************************************************************//* *//* Function: Hashing of a message block *//* Syntax: int ripehash128_l (RMDSTAT *hws, UCHAR *clear, ULONG length); *//* Input: RMDSTAT *hws (RIPEMD status buffer) *//* UCHAR *clear (Pointer to message block) *//* ULONG length (Length of message block i bytes = 0 mod 64) *//* Output: - *//* Returns: E_CLINT_OK if everything is O.K. *//* E_CLINT_RMD if length != 0 mod 64 *//* *//******************************************************************************/int __FLINT_APIripehash128_l (RMDSTAT *hws, UCHAR *clear, ULONG length){ ULONG ULBlock[16]; /* aktueller 16-ULONG (512 Bit) Message-Block */ ULONG noofblocks; /* Anzahl 16-ULONG- (512 Bit-) Bloecke */ ULONG i, j; /* Zaehler */ /* If incomplete 64 byte block exists... */ if (length & 63) { return E_CLINT_RMD; /* ...return error code */ } /* Number of 64 byte message blocks in clear */ noofblocks = length >> 6; /* Process 64 byte message blocks in clear */ for (i = 0; i < noofblocks; i++) { for (j = 0; j < 16; j++) { ULBlock[j] = UC2UL (clear); clear += 4; } swallow128 (hws->stomach, ULBlock); } /* Add length of message in clear to hws->total */ ADDC (hws->total, length);#ifdef FLINT_SECURE /* Overwrite temporary variables */ Zero4Ulong (&ULBlock[0], &ULBlock[1], &ULBlock[2], &ULBlock[3]); Zero4Ulong (&ULBlock[4], &ULBlock[5], &ULBlock[6], &ULBlock[7]); Zero4Ulong (&ULBlock[8], &ULBlock[9], &ULBlock[10], &ULBlock[11]); Zero4Ulong (&ULBlock[12], &ULBlock[13], &ULBlock[14], &ULBlock[15]);#endif return 0;}/******************************************************************************//* *//* Function: Finish hash function RIPEMD-128 *//* Syntax: void ripefinish128_l (UCHAR *HashRes, RMDSTAT *hws, *//* UCHAR *clear, ULONG length); *//* Input: RMDSTAT *hws (RIPEMD status buffer) *//* UCHAR *clear (Pointer to the last message block *//* ULONG length (Length of message block in bytes) *//* Output: UCHAR HashRes (16 byte hash value) *//* Returns: - *//* *//******************************************************************************/void __FLINT_APIripefinish128_l (UCHAR *HashRes, RMDSTAT *hws, UCHAR *clear, ULONG length){ unsigned i; /* Number of bytes in complete blocks */ ULONG blength = (length >> 6) << 6; /* Process complete blocks in clear */ ripehash128_l (hws, clear, blength); /* Add length of message in clear to hws->total */ ADDC (hws->total, length - blength); /* Process last incomplete block with padding and length of message */ digest128 (hws->stomach, clear + blength, hws->total); for (i = 0; i < 16; i += 4) { *(HashRes+i) = (UCHAR)hws->stomach[i>>2]; *(HashRes+i+1) = (UCHAR)(hws->stomach[i>>2] >> 8); /*lint !e661 !e662 */ *(HashRes+i+2) = (UCHAR)(hws->stomach[i>>2] >> 16); /*lint !e661 !e662 */ *(HashRes+i+3) = (UCHAR)(hws->stomach[i>>2] >> 24); /*lint !e661 !e662 */ } return;}/******************************************************************************//* RIPEMD-160 kernel functions *//******************************************************************************/static voidappetize (ULONG *stomach){ stomach[0] = 0x67452301UL; stomach[1] = 0xefcdab89UL; stomach[2] = 0x98badcfeUL; stomach[3] = 0x10325476UL; stomach[4] = 0xc3d2e1f0UL; return;}static voidswallow (ULONG *stomach, ULONG *ULBlock){ int round, rol; ULONG x; ULONG a1 = stomach[0]; ULONG b1 = stomach[1]; ULONG c1 = stomach[2]; ULONG d1 = stomach[3]; ULONG e1 = stomach[4]; ULONG a2 = stomach[0]; ULONG b2 = stomach[1]; ULONG c2 = stomach[2]; ULONG d2 = stomach[3]; ULONG e2 = stomach[4]; /*lint -e123 Don't complain about "Macros ... defined with arguments" */ /* Rounds and parallel rounds 0-15 */ for (round = 0; round < 16; round++) { rol = s1[round]; x = a1 + (b1 ^ c1 ^ d1) + ULBlock[round]; CHAIN160 (a1, b1, c1, d1, e1, x, rol); rol = s2[round]; x = a2 + (b2 ^ (c2 | ~d2)) + ULBlock[r2[round]] + 0x50a28BE6UL; CHAIN160 (a2, b2, c2, d2, e2, x, rol); } /* Rounds and parallel rounds 16-31 */ for (round = 16; round < 32; round++) { rol = s1[round]; x = a1 + (d1 ^ (b1 & (c1 ^ d1))) + ULBlock[r1[round]] + 0x5A827999UL; CHAIN160 (a1, b1, c1, d1, e1, x, rol); rol = s2[round]; x = a2 + (c2 ^ (d2 & (b2 ^ c2))) + ULBlock[r2[round]] + 0x5C4Dd124UL; CHAIN160 (a2, b2, c2, d2, e2, x, rol); } /* Rounds and parallel rounds 32-47 */ for (round = 32; round < 48; round++) { rol = s1[round]; x = a1 + ((b1 | ~c1) ^ d1) + ULBlock[r1[round]] + 0x6ED9EBA1UL; CHAIN160 (a1, b1, c1, d1, e1, x, rol); rol = s2[round]; x = a2 + ((b2 | ~c2) ^ d2) + ULBlock[r2[round]] + 0x6D703EF3UL; CHAIN160 (a2, b2, c2, d2, e2, x, rol); } /* Rounds and parallel rounds 48-63 */ for (round = 48; round < 64; round++) { rol = s1[round]; x = a1 + (c1 ^ (d1 & (b1 ^ c1))) + ULBlock[r1[round]] + 0x8F1BBCDCUL; CHAIN160 (a1, b1, c1, d1, e1, x, rol); rol = s2[round]; x = a2 + (d2 ^ (b2 & (c2 ^ d2))) + ULBlock[r2[round]] + 0x7A6D76E9UL; CHAIN160 (a2, b2, c2, d2, e2, x, rol); } /* Rounds and parallel rounds 64-79 */ for (round = 64; round < 80; round++) { rol = s1[round]; x = a1 + (b1 ^ (c1 | ~d1)) + ULBlock[r1[round]] + 0xA953FD4EUL; CHAIN160 (a1, b1, c1, d1, e1, x, rol); rol = s2[round]; x = a2 + (b2 ^ c2 ^ d2) + ULBlock[r2[round]]; CHAIN160 (a2, b2, c2, d2, e2, x, rol); } /* Result in stomach */ d2 += c1 + stomach[1]; stomach[1] = stomach[2] + d1 + e2; stomach[2] = stomach[3] + e1 + a2; stomach[3] = stomach[4] + a1 + b2; stomach[4] = stomach[0] + b1 + c2; stomach[0] = d2;#ifdef FLINT_SECURE /* Overwrite temporary variables */ Zero4Ulong (&a1, &b1, &c1, &d1); Zero4Ulong (&e1, &a2, &b2, &c2); Zero4Ulong (&d2, &e2, &x, &a1);#endif return;}static voiddigest (ULONG *stomach, UCHAR *clear, ULONG total[]){ ULONG i,j, rest; ULONG ULBlock[16]; memset (ULBlock, 0, sizeof (ULONG) << 4); /* Padding to achieve block length of 512 Bit */ /* by example of the message "abc" of length l = 24 bit: */ /* 1. One bit "1" is appended to the end of the masseage. */ /* 2. k "0"-bits are appended, with k the smallest non-negative */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -