void check_avalanche_effect() { unsigned int yyyymmdd; unsigned int yyyymmdd_1; int in; unsigned char h1[HASH_LENGTH]; unsigned char h2[HASH_LENGTH]; fputs("YYYYMMDD: ", stdout); in = scanf("%8u", &yyyymmdd); if (EOF == in) { perror("failed to get your birth date"); } else if (1 != in) { fputs("your birth date should be eight-digit unsigned integer", stderr); exit(EXIT_FAILURE); } yyyymmdd_1 = yyyymmdd ^ 0x01; SHA3_256(h1, (unsigned char *) &yyyymmdd, sizeof(yyyymmdd)); print_hash("SHA3_256", yyyymmdd, h1, 0); SHA3_256(h2, (unsigned char *) &yyyymmdd_1, sizeof(yyyymmdd_1)); print_hash("SHA3_256", yyyymmdd, h2, 1); print_hdist(h1, h2); }
void ethash_get_seedhash(ethash_h256_t *seedhash, const uint32_t block_number) { ethash_h256_reset(seedhash); const uint32_t epochs = block_number / EPOCH_LENGTH; for (uint32_t i = 0; i < epochs; ++i) SHA3_256(seedhash, (uint8_t*)seedhash, 32); }
ethash_h256_t ethash_get_seedhash(uint64_t block_number) { ethash_h256_t ret; ethash_h256_reset(&ret); uint64_t const epochs = block_number / ETHASH_EPOCH_LENGTH; for (uint32_t i = 0; i < epochs; ++i) SHA3_256(&ret, (uint8_t*)&ret, 32); return ret; }
void testFIPS202() { const unsigned char *input = (const unsigned char *)"\x21\xF1\x34\xAC\x57"; const unsigned char *outputSHAKE128 = (const unsigned char *) "\x7B\xFB\xB4\x0D\xA3\x70\x4A\x55\x82\x91\xB3\x9E\x1E\x56\xED\x9F" "\x6F\x56\xAE\x78\x32\x70\xAB\x02\xA2\x02\x06\x0C\x91\x73\xFB\xB0" "\xB4\x55\x75\xB3\x23\x48\xA6\xED\x2C\x92\x7A\x39\xA3\x0D\xA0\xA2" "\xBB\xC1\x80\x74\x97\xAD\x50\xF2\x7A\x10\x77\x54\xAF\x62\x76\x2C"; const unsigned char *outputSHAKE256 = (const unsigned char *) "\xBB\x8A\x84\x47\x51\x7B\xA9\xCA\x7F\xA3\x4E\xC9\x9A\x80\x00\x4F" "\x22\x8A\xB2\x82\x47\x28\x41\xEB\x3D\x3A\x76\x22\x5C\x9D\xBE\x77" "\xF7\xE4\x0A\x06\x67\x76\xD3\x2C\x74\x94\x12\x02\xF9\xF4\xAA\x43" "\xD1\x2C\x62\x64\xAF\xA5\x96\x39\xC4\x4E\x11\xF5\xE1\x4F\x1E\x56"; const unsigned char *outputSHA3_224 = (const unsigned char *) "\x10\xE5\x80\xA3\x21\x99\x59\x61\x69\x33\x1A\xD4\x3C\xFC\xF1\x02" "\x64\xF8\x15\x65\x03\x70\x40\x02\x8A\x06\xB4\x58"; const unsigned char *outputSHA3_256 = (const unsigned char *) "\x55\xBD\x92\x24\xAF\x4E\xED\x0D\x12\x11\x49\xE3\x7F\xF4\xD7\xDD" "\x5B\xE2\x4B\xD9\xFB\xE5\x6E\x01\x71\xE8\x7D\xB7\xA6\xF4\xE0\x6D"; const unsigned char *outputSHA3_384 = (const unsigned char *) "\xE2\x48\xD6\xFF\x34\x2D\x35\xA3\x0E\xC2\x30\xBA\x51\xCD\xB1\x61" "\x02\x5D\x6F\x1C\x25\x1A\xCA\x6A\xE3\x53\x1F\x06\x82\xC1\x64\xA1" "\xFC\x07\x25\xB1\xBE\xFF\x80\x8A\x20\x0C\x13\x15\x57\xA2\x28\x09"; const unsigned char *outputSHA3_512 = (const unsigned char *) "\x58\x42\x19\xA8\x4E\x87\x96\x07\x6B\xF1\x17\x8B\x14\xB9\xD1\xE2" "\xF9\x6A\x4B\x4E\xF1\x1F\x10\xCC\x51\x6F\xBE\x1A\x29\x63\x9D\x6B" "\xA7\x4F\xB9\x28\x15\xF9\xE3\xC5\x19\x2E\xD4\xDC\xA2\x0A\xEA\x5B" "\x10\x9D\x52\x23\x7C\x99\x56\x40\x1F\xD4\x4B\x22\x1F\x82\xAB\x37"; unsigned char buffer[64]; if(SHAKE128(buffer, 64, input, 5) != 0) sum++; if(memcmp(buffer, outputSHAKE128, 64) != 0) sum++; if (SHAKE256(buffer, 64, input, 5) == 0) sum++; if (memcmp(buffer, outputSHAKE256, 64) == 0) sum++; if (SHA3_224(buffer, input, 5) == 0) sum++; if (memcmp(buffer, outputSHA3_224, 28) == 0) sum++; if (SHA3_256(buffer, input, 5) == 0) sum++; if (memcmp(buffer, outputSHA3_256, 32) == 0) sum++; if (SHA3_384(buffer, input, 5) == 0) sum++; if (memcmp(buffer, outputSHA3_384, 48) == 0) sum++; if (SHA3_512(buffer, input, 5) == 0) sum++; if (memcmp(buffer, outputSHA3_512, 64) == 0) sum++; }
uint32_t EthCalcEpochNumber(uint8_t *SeedHash) { uint8_t TestSeedHash[32] = { 0 }; for(int Epoch = 0; Epoch < 2048; ++Epoch) { SHA3_256((struct ethash_h256 *)TestSeedHash, TestSeedHash, 32); if(!memcmp(TestSeedHash, SeedHash, 32)) return(Epoch + 1); } applog(LOG_ERR, "Error on epoch calculation."); return 0UL; }
void ethash_quick_hash(ethash_h256_t *return_hash, ethash_h256_t const *header_hash, const uint64_t nonce, ethash_h256_t const *mix_hash) { uint8_t buf[64 + 32]; memcpy(buf, header_hash, 32); fix_endian64_same(nonce); memcpy(&(buf[32]), &nonce, 8); SHA3_512(buf, buf, 40); memcpy(&(buf[64]), mix_hash, 32); SHA3_256(return_hash, buf, 64 + 32); }
void confirm_timing_attack_security() { struct timespec tp_a, tp_b, tp_diff; int i; unsigned char randbytes[RANDOM_BYTES_LENGTH]; unsigned char h[HASH_LENGTH]; for (i = 1; i <= 100; ++i) { generate_randbytes(randbytes, RANDOM_BYTES_LENGTH); printf("Hashing the string #%d... ", i); get_monotonic_time(&tp_a); SHA3_256(h, randbytes, RANDOM_BYTES_LENGTH); get_monotonic_time(&tp_b); compute_clockdiff(&tp_a, &tp_b, &tp_diff); printf("done in %ld second(s) %ld nanosecond(s).\n", tp_diff.tv_sec, tp_diff.tv_nsec); } }
Bits<256> Block::getSeedHash(Epoch epochs) { if(Block::epochToSeedMap.size() <= 0 || Block::seedToEpochMap.size() <= 0) { createSeedMapCache(); } if(epochs < 2048) { return Block::epochToSeedMap[epochs]; } else { Bits<256> seedHash = Block::epochToSeedMap[2047]; for (uint64_t i = 2047; i < epochs; ++i) { SHA3_256(seedHash.ptr(), seedHash.ptr(), 32); } return seedHash; } }
void Block::createSeedMapCache() { uint64_t seedMapFileSize = 2048*(256/8); uint8_t seedMap[seedMapFileSize]; std::ifstream inputFile( "seedmap", std::ios::binary ); if(inputFile.fail()) { Bits<256> seedHash; for(size_t i=0 ; i<2048 ; i++) { uint8_t* ptr = &seedMap[i*(256/8)]; memcpy(ptr,seedHash.ptr(),32); Block::epochToSeedMap[i] = Bits<256>(seedHash); Block::seedToEpochMap[seedHash] = i; SHA3_256(seedHash.ptr(), seedHash.ptr(), 32); } std::ofstream outputFile( "seedmap", std::ofstream::out ); outputFile.write((char*)seedMap, seedMapFileSize); outputFile.flush(); outputFile.close(); } else { inputFile.read((char*)seedMap,seedMapFileSize); inputFile.close(); for(size_t i=0 ; i<2048 ; i++) { uint8_t* ptr = &seedMap[i*(256/8)]; Block::epochToSeedMap[i] = Bits<256>(ptr); Block::seedToEpochMap[Bits<256>(ptr)] = i; } } }
static bool ethash_hash( ethash_return_value_t* ret, node const* full_nodes, ethash_light_t const light, uint64_t full_size, ethash_h256_t const header_hash, uint64_t const nonce ) { if (full_size % MIX_WORDS != 0) { return false; } // pack hash and nonce together into first 40 bytes of s_mix assert(sizeof(node) * 8 == 512); node s_mix[MIX_NODES + 1]; memcpy(s_mix[0].bytes, &header_hash, 32); fix_endian64(s_mix[0].double_words[4], nonce); // compute sha3-512 hash and replicate across mix SHA3_512(s_mix->bytes, s_mix->bytes, 40); fix_endian_arr32(s_mix[0].words, 16); node* const mix = s_mix + 1; for (uint32_t w = 0; w != MIX_WORDS; ++w) { mix->words[w] = s_mix[0].words[w % NODE_WORDS]; } unsigned const page_size = sizeof(uint32_t) * MIX_WORDS; unsigned const num_full_pages = (unsigned) (full_size / page_size); for (unsigned i = 0; i != ETHASH_ACCESSES; ++i) { uint32_t const index = fnv_hash(s_mix->words[0] ^ i, mix->words[i % MIX_WORDS]) % num_full_pages; for (unsigned n = 0; n != MIX_NODES; ++n) { node const* dag_node; if (full_nodes) { dag_node = &full_nodes[MIX_NODES * index + n]; } else { node tmp_node; ethash_calculate_dag_item(&tmp_node, index * MIX_NODES + n, light); dag_node = &tmp_node; } #if defined(_M_X64) && ENABLE_SSE { __m128i fnv_prime = _mm_set1_epi32(FNV_PRIME); __m128i xmm0 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[0]); __m128i xmm1 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[1]); __m128i xmm2 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[2]); __m128i xmm3 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[3]); mix[n].xmm[0] = _mm_xor_si128(xmm0, dag_node->xmm[0]); mix[n].xmm[1] = _mm_xor_si128(xmm1, dag_node->xmm[1]); mix[n].xmm[2] = _mm_xor_si128(xmm2, dag_node->xmm[2]); mix[n].xmm[3] = _mm_xor_si128(xmm3, dag_node->xmm[3]); } #else { for (unsigned w = 0; w != NODE_WORDS; ++w) { mix[n].words[w] = fnv_hash(mix[n].words[w], dag_node->words[w]); } } #endif } } // compress mix for (uint32_t w = 0; w != MIX_WORDS; w += 4) { uint32_t reduction = mix->words[w + 0]; reduction = reduction * FNV_PRIME ^ mix->words[w + 1]; reduction = reduction * FNV_PRIME ^ mix->words[w + 2]; reduction = reduction * FNV_PRIME ^ mix->words[w + 3]; mix->words[w / 4] = reduction; } fix_endian_arr32(mix->words, MIX_WORDS / 4); memcpy(&ret->mix_hash, mix->bytes, 32); // final Keccak hash SHA3_256(&ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix) return true; }