int ripemd_test(void) { RipeMd ripemd; byte hash[RIPEMD_DIGEST_SIZE]; testVector a, b, c, d; testVector test_ripemd[4]; int times = sizeof(test_ripemd) / sizeof(struct testVector), i; a.input = "abc"; a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6" "\xb0\x87\xf1\x5a\x0b\xfc"; a.inLen = XSTRLEN(a.input); a.outLen = XSTRLEN(a.output); b.input = "message digest"; b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8" "\x5f\xfa\x21\x59\x5f\x36"; b.inLen = XSTRLEN(b.input); b.outLen = XSTRLEN(b.output); c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc" "\xf4\x9a\xda\x62\xeb\x2b"; c.inLen = XSTRLEN(c.input); c.outLen = XSTRLEN(c.output); d.input = "12345678901234567890123456789012345678901234567890123456" "789012345678901234567890"; d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab" "\x82\xbf\x63\x32\x6b\xfb"; d.inLen = XSTRLEN(d.input); d.outLen = XSTRLEN(d.output); test_ripemd[0] = a; test_ripemd[1] = b; test_ripemd[2] = c; test_ripemd[3] = d; wc_InitRipeMd(&ripemd); for (i = 0; i < times; ++i) { wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, (word32)test_ripemd[i].inLen); wc_RipeMdFinal(&ripemd, hash); if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) return -10 - i; } return 0; }
int wc_RipeMdFinal(RipeMd* ripemd, byte* hash) { byte* local; if (ripemd == NULL || hash == NULL) { return BAD_FUNC_ARG; } local = (byte*)ripemd->buffer; AddLength(ripemd, ripemd->buffLen); /* before adding pads */ local[ripemd->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (ripemd->buffLen > RIPEMD_PAD_SIZE) { XMEMSET(&local[ripemd->buffLen], 0, RIPEMD_BLOCK_SIZE - ripemd->buffLen); ripemd->buffLen += RIPEMD_BLOCK_SIZE - ripemd->buffLen; #ifdef BIG_ENDIAN_ORDER ByteReverseWords(ripemd->buffer, ripemd->buffer, RIPEMD_BLOCK_SIZE); #endif Transform(ripemd); ripemd->buffLen = 0; } XMEMSET(&local[ripemd->buffLen], 0, RIPEMD_PAD_SIZE - ripemd->buffLen); /* put lengths in bits */ ripemd->loLen = ripemd->loLen << 3; ripemd->hiLen = (ripemd->loLen >> (8*sizeof(ripemd->loLen) - 3)) + (ripemd->hiLen << 3); /* store lengths */ #ifdef BIG_ENDIAN_ORDER ByteReverseWords(ripemd->buffer, ripemd->buffer, RIPEMD_BLOCK_SIZE); #endif /* ! length ordering dependent on digest endian type ! */ XMEMCPY(&local[RIPEMD_PAD_SIZE], &ripemd->loLen, sizeof(word32)); XMEMCPY(&local[RIPEMD_PAD_SIZE + sizeof(word32)], &ripemd->hiLen, sizeof(word32)); Transform(ripemd); #ifdef BIG_ENDIAN_ORDER ByteReverseWords(ripemd->digest, ripemd->digest, RIPEMD_DIGEST_SIZE); #endif XMEMCPY(hash, ripemd->digest, RIPEMD_DIGEST_SIZE); return wc_InitRipeMd(ripemd); /* reset state */ }