void ed25519_sign(unsigned char *signature, const unsigned char *message, int32_t message_len, const unsigned char *public_key, const unsigned char *private_key) { sha3_context ctx; unsigned char hram[64]; unsigned char r[64]; ge_p3 R; sha3_init512(&ctx); sha3_update(&ctx, private_key + 32, 32); sha3_update(&ctx, message, message_len); sha3_finalize(&ctx, &r); sc_reduce(r); ge_scalarmult_base(&R, r); ge_p3_tobytes(signature, &R); sha3_init512(&ctx); sha3_update(&ctx, signature, 32); sha3_update(&ctx, public_key, 32); sha3_update(&ctx, message, message_len); sha3_finalize(&ctx, hram); sc_reduce(hram); sc_muladd(signature + 32, hram, private_key, r); }
void notrand_seed(void *data, size_t len) { sha3_ctx_t ctx; sha3_init(&ctx, 64); sha3_update(&ctx, nrpool, 3 * 64); sha3_update(&ctx, data, len); sha3_final(nrpool[2], &ctx); }
static int calc_steps_and_compare_hash_512(const uint8_t *msg1, size_t msg1_len, const uint8_t *msg2, size_t msg2_len, const uint8_t *expected) { static unsigned char hash[SHA3_512_DIGEST_LENGTH]; keccak_state_t state; sha3_512_init(&state); sha3_update(&state, msg1, msg1_len); sha3_update(&state, msg2, msg2_len); sha3_512_final(&state, hash); return (memcmp(expected, hash, sizeof(hash)) == 0); }
void sha3_512(const unsigned char *message, size_t message_len, unsigned char *out) { sha3_context ctx; sha3_init512(&ctx); sha3_update(&ctx, message, message_len); sha3_finalize(&ctx, out); }
static void sha3_256_extremely_long_message_1_t (ut_test_t *const t) { const char *const pattern = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; const size_t repeats = 16777216; const size_t pattern_len = strlen(pattern); const char *const expected_msg_digest_as_string = "ecbbc42cbf296603" "acb2c6bc0410ef43" "78bafb24b710357f" "12df607758b33e2b"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. sha3_ctx_t ctx; sha3_init(&ctx, sha3_256_digest_lenght); for (int i = 0; i < repeats; i++) { sha3_update(&ctx, pattern, pattern_len); } sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_256_1M_a_1_t (ut_test_t *const t) { char msg[1000 + 1]; for (int i = 0; i < 1000; i++) { msg[i] = 'a'; } msg[1000] = '\0'; const char *const expected_msg_digest_as_string = "5c8875ae474a3634" "ba4fd55ec85bffd6" "61f32aca75c6d699" "d0cdcb6c115891c1"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_256_digest_lenght); for (int i = 0; i < 1000; i++) sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_256_896_bits_t (ut_test_t *const t) { const char *const msg = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; const char *const expected_msg_digest_as_string = "916f6061fe879741" "ca6469b43971dfdb" "28b1a32dc36cb325" "4e812be27aad1d18"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_256_digest_lenght); sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_256_empty_string_t (ut_test_t *const t) { const char *const msg = ""; const char *const expected_msg_digest_as_string = "a7ffc6f8bf1ed766" "51c14756a061d662" "f580ff4de43b49fa" "82d80a4b80f8434a"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_256_digest_lenght); sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_256_abc_t (ut_test_t *const t) { const char *const msg = "abc"; const char *const expected_msg_digest_as_string = "3a985da74fe225b2" "045c172d6bd390bd" "855f086e3e9d525b" "46bfe24511431532"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_256_digest_lenght); sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_512_empty_string_t (ut_test_t *const t) { const char *const msg = ""; const char *const expected_msg_digest_as_string = "a69f73cca23a9ac5" "c8b567dc185a756e" "97c982164fe25859" "e0d1dcc1475c80a6" "15b2123af1f5f94c" "11e3e9402c3ac558" "f500199d95b6d3e3" "01758586281dcd26"; char msg_digest[sha3_512_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_512_digest_lenght); sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_512_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
static void sha3_512_abc_t (ut_test_t *const t) { const char *const msg = "abc"; const char *const expected_msg_digest_as_string = "b751850b1a57168a" "5693cd924b6b096e" "08f621827444f70d" "884f5d0240d2712e" "10e116e9192af3c9" "1a7ec57647e39340" "57340b4cf408d5a5" "6592f8274eec53f0"; char msg_digest[sha3_512_digest_lenght]; char msg_digest_as_string[sha3_256_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_ctx_t ctx; sha3_init(&ctx, sha3_512_digest_lenght); sha3_update(&ctx, msg, msg_len); sha3_final(&ctx, msg_digest); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_512_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------// // update hash void sha3_224_t::update(const void * const p, uint64_t size) { if (this->psha3_224_item == NULL) return; sha3_update(&this->ctx, (uint8_t *)p, size); }
void hash_update_sha3(void * ctx, const uint8_t * input, size_t length) { sha3_update((sha3_state *)ctx, input, length); }