/* whirlpool.c - Whirlpool cryptographic hash */ /* Jon Mayo - PUBLIC DOMAIN - October 9, 2006 */ /* information taken from: * "The WHIRLPOOL Hashing Function", Paulo S.L.M. Barreto, Vincent Rijmen [Whirlpool.pdf] * * "A Compact FPGA Implementation of the Hash Function Whirlpool", Norber t Pramstaller, Christian Rechberger, and Vincent Rijmen. [p159-pramstaller.pdf] */ #include #include #ifndef NR #define NR(x) (sizeof(x)/sizeof*(x)) #endif #define WHIRLPOOL_DIGEST_SIZE 64 /* in bytes */ #define WHIRLPOOL_ROUNDS 10 struct whirlpool_ctx { uint32_t state[WHIRLPOOL_DIGEST_SIZE/4]; uint32_t block[WHIRLPOOL_DIGEST_SIZE/4]; uint32_t round[WHIRLPOOL_DIGEST_SIZE/4]; uint32_t hash[WHIRLPOOL_DIGEST_SIZE/4]; /* hash state */ uint32_t L[WHIRLPOOL_DIGEST_SIZE/4]; }; /* Whirlpool S-box */ static const unsigned char whirlpool_sbox[256] = { 0x18, 0x23, 0xc6, 0xe8, 0x87, 0xb8, 0x01, 0x4f, 0x36, 0xa6, 0xd2, 0xf5, 0x79, 0x6f, 0x91, 0x52, 0x60, 0xbc, 0x9b, 0x8e, 0xa3, 0x0c, 0x7b, 0x35, 0x1d, 0xe0, 0xd7, 0xc2, 0x2e, 0x4b, 0xfe, 0x57, 0x15, 0x77, 0x37, 0xe5, 0x9f, 0xf0, 0x4a, 0xda, 0x58, 0xc9, 0x29, 0x0a, 0xb1, 0xa0, 0x6b, 0x85, 0xbd, 0x5d, 0x10, 0xf4, 0xcb, 0x3e, 0x05, 0x67, 0xe4, 0x27, 0x41, 0x8b, 0xa7, 0x7d, 0x95, 0xd8, 0xfb, 0xee, 0x7c, 0x66, 0xdd, 0x17, 0x47, 0x9e, 0xca, 0x2d, 0xbf, 0x07, 0xad, 0x5a, 0x83, 0x33, 0x63, 0x02, 0xaa, 0x71, 0xc8, 0x19, 0x49, 0xd9, 0xf2, 0xe3, 0x5b, 0x88, 0x9a, 0x26, 0x32, 0xb0, 0xe9, 0x0f, 0xd5, 0x80, 0xbe, 0xcd, 0x34, 0x48, 0xff, 0x7a, 0x90, 0x5f, 0x20, 0x68, 0x1a, 0xae, 0xb4, 0x54, 0x93, 0x22, 0x64, 0xf1, 0x73, 0x12, 0x40, 0x08, 0xc3, 0xec, 0xdb, 0xa1, 0x8d, 0x3d, 0x97, 0x00, 0xcf, 0x2b, 0x76, 0x82, 0xd6, 0x1b, 0xb5, 0xaf, 0x6a, 0x50, 0x45, 0xf3, 0x30, 0xef, 0x3f, 0x55, 0xa2, 0xea, 0x65, 0xba, 0x2f, 0xc0, 0xde, 0x1c, 0xfd, 0x4d, 0x92, 0x75, 0x06, 0x8a, 0xb2, 0xe6, 0x0e, 0x1f, 0x62, 0xd4, 0xa8, 0x96, 0xf9, 0xc5, 0x25, 0x59, 0x84, 0x72, 0x39, 0x4c, 0x5e, 0x78, 0x38, 0x8c, 0xd1, 0xa5, 0xe2, 0x61, 0xb3, 0x21, 0x9c, 0x1e, 0x43, 0xc7, 0xfc, 0x04, 0x51, 0x99, 0x6d, 0x0d, 0xfa, 0xdf, 0x7e, 0x24, 0x3b, 0xab, 0xce, 0x11, 0x8f, 0x4e, 0xb7, 0xeb, 0x3c, 0x81, 0x94, 0xf7, 0xb9, 0x13, 0x2c, 0xd3, 0xe7, 0x6e, 0xc4, 0x03, 0x56, 0x44, 0x7f, 0xa9, 0x2a, 0xbb, 0xc1, 0x53, 0xdc, 0x0b, 0x9d, 0x6c, 0x31, 0x74, 0xf6, 0x46, 0xac, 0x89, 0x14, 0xe1, 0x16, 0x3a, 0x69, 0x09, 0x70, 0xb6, 0xd0, 0xed, 0xcc, 0x42, 0x98, 0xa4, 0x28, 0x5c, 0xf8, 0x86, }; struct whirlpool_ctx *whirlpool_alloc(void); void whirlpool_free(struct whirlpool_ctx *context); void whirlpool_init(struct whirlpool_ctx *context); void whirlpool_update(struct whirlpool_ctx *context, const uint8_t* data, const size_t len); void whirlpool_final(struct whirlpool_ctx *context, uint8_t digest[WHIRLPOOL_DIGEST_SIZE]); struct whirlpool_ctx *whirlpool_alloc(void) { return calloc(1, sizeof(struct whirlpool_ctx)); } void whirlpool_free(struct whirlpool_ctx *context) { free(context); } void whirlpool_init(struct whirlpool_ctx *context) { int i; /* h0 is 0 */ for(i=0;ihash);i++) context->hash[i]=0; } void whirlpool_update(struct whirlpool_ctx *context, const uint8_t* data, const size_t len) { abort(); /* not implemented */ } void whirlpool_final(struct whirlpool_ctx *context, uint8_t digest[WHIRLPOOL_DIGEST_SIZE]) { abort(); /* not implemented */ } #ifdef STAND_ALONE #include static void make_s_box(void) { unsigned char sbox[256]; /* E, E inverse and R miniboxes */ const unsigned char e[16] = {1, 11, 9, 12, 13, 6, 15, 3, 14, 8, 7, 4, 10, 2, 5, 0}, ei[16] = {15, 0, 13, 7, 11, 14, 5, 10, 9, 2, 12, 1, 3, 4, 8, 6}, r[16] = {7, 12, 11, 13, 14, 4, 9, 15, 6, 3, 8, 10, 2, 5, 1, 0}; unsigned u, i; /* dump the mini s-boxes */ printf("e = "); for(i=0;i<16;i++) { printf("%01x", e[i]); } printf("\nei = "); for(i=0;i<16;i++) { printf("%01x", ei[i]); } printf("\nr = "); for(i=0;i<16;i++) { printf("%01x", r[i]); } printf("\n"); for(u=0;u<256;u++) { unsigned x,y,c; x=e[u>>4]; y=ei[u&15]; c=r[x^y]; sbox[u]=(e[x^c]<<4)|ei[y^c]; } printf("static const unsigned char whirlpool_sbox[256] = {\n"); for(i=0;i<256;i++) { if(i%8==0) printf("\t"); printf("0x%02x, ", sbox[i]); if(i%8==7) printf("\n"); } printf("};\n"); } static void print_digest(uint8_t digest[WHIRLPOOL_DIGEST_SIZE]) { int i; for(i=0;i