/* initialized the state in serial-key'd mode */ void blake2b_keyed_init (blake2b_state *S, const unsigned char *key, size_t keylen) { unsigned char k[BLAKE2B_BLOCKBYTES]; blake2b_state _ks; blake2b_state_internal *state = (blake2b_state_internal *)S; memset (k, 0, sizeof (k)); if (keylen <= BLAKE2B_KEYBYTES) { memcpy (k, key, keylen); blake2b_init (S); state->h[1] ^= keylen; blake2b_update (S, k, sizeof (k)); } else { blake2b_init (S); /* * We use additional blake2 iteration to store large key * XXX: it is not compatible with the original implementation but safe */ blake2b_init (&_ks); blake2b_update (&_ks, key, keylen); blake2b_final (&_ks, k); blake2b_keyed_init (S, k, BLAKE2B_KEYBYTES); } rspamd_explicit_memzero (k, sizeof (k)); }
/* MMXVI Team - Begin Code */ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) { uint8_t *out = (uint8_t *)pout; blake2b_state blake_state; uint8_t outlen_bytes[sizeof(uint32_t)] = {0}; int ret = -1; if (outlen > UINT32_MAX) { goto fail; } /* Ensure little-endian byte order! */ store32(outlen_bytes, (uint32_t)outlen); #define TRY(statement) \ do { \ ret = statement; \ if (ret < 0) { \ goto fail; \ } \ } while ((void)0, 0) if (outlen <= BLAKE2B_OUTBYTES) { TRY(blake2b_init(&blake_state, outlen)); TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); TRY(blake2b_update(&blake_state, in, inlen)); TRY(blake2b_final(&blake_state, out, outlen)); } else { uint32_t toproduce; uint8_t out_buffer[BLAKE2B_OUTBYTES]; uint8_t in_buffer[BLAKE2B_OUTBYTES]; TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES)); TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); TRY(blake2b_update(&blake_state, in, inlen)); TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES)); memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); out += BLAKE2B_OUTBYTES / 2; toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2; while (toproduce > BLAKE2B_OUTBYTES) { memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer, BLAKE2B_OUTBYTES, NULL, 0)); memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); out += BLAKE2B_OUTBYTES / 2; toproduce -= BLAKE2B_OUTBYTES / 2; } memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL, 0)); memcpy(out, out_buffer, toproduce); } fail: burn(&blake_state, sizeof(blake_state)); return ret; #undef TRY }
/* inlen, at least, should be word64. Others can be size_t. */ int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ) { blake2b_state S[1]; /* Verify parameters */ if ( NULL == in ) return -1; if ( NULL == out ) return -1; if( NULL == key ) keylen = 0; if( keylen > 0 ) { if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2b_init( S, outlen ) < 0 ) return -1; } if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1; return blake2b_final( S, out, outlen ); }
static inline bool HashFunction_Init(const int hashType, hash_ctx *const ctx) { memset(ctx, 0, sizeof(hash_ctx)); switch(ctx->hashType = hashType) { case STD_HASHTYPE_CRC_32: rhash_crc32_init(&ctx->data.crc32); return true; case STD_HASHTYPE_MD5_128: rhash_md5_init(&ctx->data.md5); return true; case STD_HASHTYPE_SHA1_160: rhash_sha1_init(&ctx->data.sha1); return true; case STD_HASHTYPE_SHA2_224: rhash_sha224_init(&ctx->data.sha256); return true; case STD_HASHTYPE_SHA2_256: rhash_sha256_init(&ctx->data.sha256); return true; case STD_HASHTYPE_SHA2_384: rhash_sha384_init(&ctx->data.sha512); return true; case STD_HASHTYPE_SHA2_512: rhash_sha512_init(&ctx->data.sha512); return true; case STD_HASHTYPE_SHA3_224: rhash_sha3_224_init(&ctx->data.sha3); return true; case STD_HASHTYPE_SHA3_256: rhash_sha3_256_init(&ctx->data.sha3); return true; case STD_HASHTYPE_SHA3_384: rhash_sha3_384_init(&ctx->data.sha3); return true; case STD_HASHTYPE_SHA3_512: rhash_sha3_512_init(&ctx->data.sha3); return true; case STD_HASHTYPE_BLK2_224: case STD_HASHTYPE_BLK2_256: case STD_HASHTYPE_BLK2_384: case STD_HASHTYPE_BLK2_512: if(blake2b_init(&ctx->data.balke2, Blake2_Size(ctx->hashType)) != 0) { MessageBox(NULL, T("Blake2_Init internal error, going to abort!"), T("StdUtils::HashFunction_Init"), MB_ICONSTOP | MB_TOPMOST); abort(); } return true; default: ERROR_MSG(T("The specified hash type is unknown/unsupported!")); return false; } }
/* inlen, at least, should be uint64_t. Others can be size_t. */ int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) { blake2b_state S[1]; /* Verify parameters */ if ( NULL == in && inlen > 0 ) return -1; if ( NULL == out ) return -1; if( NULL == key && keylen > 0 ) return -1; if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1; if( keylen > BLAKE2B_KEYBYTES ) return -1; if( keylen > 0 ) { if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2b_init( S, outlen ) < 0 ) return -1; } blake2b_update( S, ( const uint8_t * )in, inlen ); blake2b_final( S, out, outlen ); return 0; }
void rs_calc_blake2_sum(void const *buf, size_t len, rs_strong_sum_t *sum) { blake2b_state ctx; blake2b_init(&ctx, RS_MAX_STRONG_SUM_LENGTH); blake2b_update(&ctx, (const uint8_t *)buf, len); blake2b_final(&ctx, (uint8_t *)sum, RS_MAX_STRONG_SUM_LENGTH); }
int blake2b_selftest(void) { const uint8_t d0[32] = { 0xc2,0x3a,0x78,0x00,0xd9,0x81,0x23,0xbd, 0x10,0xf5,0x06,0xc6,0x1e,0x29,0xda,0x56, 0x03,0xd7,0x63,0xb8,0xbb,0xad,0x2e,0x73, 0x7f,0x5e,0x76,0x5a,0x7b,0xcc,0xd4,0x75, }; const unsigned dlen[4] = { 20, 32, 48, 64 }; const unsigned mlen[6] = { 0, 3, 128, 129, 255, 1024 }; uint8_t m[1024], d[64], k[64]; struct blake2b ctx; unsigned di, mi, i; blake2b_init(&ctx, 32, NULL, 0); for (di = 0; di < 4; di++) { for (mi = 0; mi < 6; mi++) { blake2_selftest_prng(m, mlen[mi], mlen[mi]); blake2b(d, dlen[di], NULL, 0, m, mlen[mi]); blake2b_update(&ctx, d, dlen[di]); blake2_selftest_prng(k, dlen[di], dlen[di]); blake2b(d, dlen[di], k, dlen[di], m, mlen[mi]); blake2b_update(&ctx, d, dlen[di]); } } blake2b_final(&ctx, d); for (i = 0; i < 32; i++) { if (d[i] != d0[i]) return -1; } return 0; }
void blake2b_too(void *pout, const void *in) { uint8_t *out = (uint8_t *)pout; uint8_t out_buffer[64]; uint8_t in_buffer[64]; blake2b_state blake_state; blake2b_init(&blake_state, 64); blake_state.buflen = blake_state.buf[1] = 4; my_blake2b_update(&blake_state, in, 72); blake2b_final(&blake_state, out_buffer, 64); memcpy(out, out_buffer, 32); out += 32; register uint8_t i = 29; while (i--) { memcpy(in_buffer, out_buffer, 64); blake2b(out_buffer, in_buffer, NULL, 0); memcpy(out, out_buffer, 32); out += 32; } memcpy(in_buffer, out_buffer, 64); blake2b(out_buffer, in_buffer, NULL, 0); memcpy(out, out_buffer, 64); burn(&blake_state, sizeof(blake_state)); }
inline void __Hash1(const uint8_t *input, const uint32_t inputlen, uint8_t hash[H_LEN]) { blake2b_state ctx; blake2b_init(&ctx,H_LEN); blake2b_update(&ctx, input, inputlen); blake2b_final(&ctx, hash, H_LEN); }
void blake2b(void *digest, size_t dlen, const void *key, size_t keylen, const void *in, size_t inlen) { struct blake2b ctx; blake2b_init(&ctx, dlen, key, keylen); blake2b_update(&ctx, in, inlen); blake2b_final(&ctx, digest); }
inline void __Hash2(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, const uint8_t i2len, uint8_t hash[H_LEN]) { blake2b_state ctx; blake2b_init(&ctx,H_LEN); blake2b_update(&ctx, i1, i1len); blake2b_update(&ctx, i2, i2len); blake2b_final(&ctx, hash, H_LEN); }
uint64_t nano::work_value (nano::block_hash const & root_a, uint64_t work_a) { uint64_t result; blake2b_state hash; blake2b_init (&hash, sizeof (result)); blake2b_update (&hash, reinterpret_cast<uint8_t *> (&work_a), sizeof (work_a)); blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ()); blake2b_final (&hash, reinterpret_cast<uint8_t *> (&result), sizeof (result)); return result; }
int blake2b(void *out, const void *in, const void *key, size_t keylen) { blake2b_state S; blake2b_init(&S, 64); my_blake2b_update(&S, in, 64); blake2b_final(&S, out, 64); burn(&S, sizeof(S)); return 0; }
/* convenience function for all-in-one computation */ static int blake2b( void * out, HB_SIZE outlen, const void * key, HB_SIZE keylen, const void * in, HB_SIZE inlen ) { blake2b_ctx ctx; if( blake2b_init( &ctx, outlen, key, keylen ) ) return -1; blake2b_update( &ctx, in, inlen ); blake2b_final( &ctx, out ); return 0; }
int blake2b(void *out, size_t outlen, const void *key, size_t keylen, const void *in, size_t inlen) { blake2b_ctx ctx; if (blake2b_init(&ctx, outlen, key, keylen)) return -1; blake2b_update(&ctx, in, inlen); blake2b_final(&ctx, out); return 0; }
MUtils::Hash::Blake2::Blake2(const char *const key) : m_context(new Blake2_Context()), m_finalized(false) { if(key && key[0]) { blake2b_init_key(m_context->state, HASH_SIZE, key, (uint8_t)strlen(key)); } else { blake2b_init(m_context->state, HASH_SIZE); } }
int blake2b_stream( FILE *stream, void *resstream ) { int ret = -1; size_t sum, n; blake2b_state S[1]; static const size_t buffer_length = 32768; uint8_t *buffer = ( uint8_t * )malloc( buffer_length ); if( !buffer ) return -1; blake2b_init( S, BLAKE2B_OUTBYTES ); while( 1 ) { sum = 0; while( 1 ) { n = fread( buffer + sum, 1, buffer_length - sum, stream ); sum += n; if( buffer_length == sum ) break; if( 0 == n ) { if( ferror( stream ) ) goto cleanup_buffer; goto final_process; } if( feof( stream ) ) goto final_process; } blake2b_update( S, buffer, buffer_length ); } final_process:; if( sum > 0 ) blake2b_update( S, buffer, sum ); blake2b_final( S, resstream, BLAKE2B_OUTBYTES ); ret = 0; cleanup_buffer: free( buffer ); return ret; }
static void rspamd_regexp_generate_id (const gchar *pattern, const gchar *flags, regexp_id_t out) { blake2b_state st; blake2b_init (&st, sizeof (regexp_id_t)); if (flags) { blake2b_update (&st, flags, strlen (flags)); } blake2b_update (&st, pattern, strlen (pattern)); blake2b_final (&st, out, sizeof (regexp_id_t)); }
/* Argon2 Team - Begin Code */ int blake2b_long(void *pout, const void *in) { uint8_t *out = (uint8_t *)pout; blake2b_state blake_state; uint8_t outlen_bytes[sizeof(uint32_t)] = {0}; store32(outlen_bytes, 32); blake2b_init(&blake_state, 32); my_blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)); blake2b_update(&blake_state, in, 1024); blake2b_final(&blake_state, out, 32); burn(&blake_state, sizeof(blake_state)); return 0; }
inline void __Hash5(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, const uint8_t *i4, const uint8_t i4len, const uint8_t *i5, const uint8_t i5len, uint8_t hash[H_LEN]) { blake2b_state ctx; blake2b_init(&ctx,H_LEN); blake2b_update(&ctx, i1, i1len); blake2b_update(&ctx, i2, i2len); blake2b_update(&ctx, i3, i3len); blake2b_update(&ctx, i4, i4len); blake2b_update(&ctx, i5, i5len); blake2b_final(&ctx, hash, H_LEN); }
static int compress_blake2b (uint8_t *out, const uint8_t *blocks[], unsigned int blocks_to_comp) { blake2b_state s; if (blake2b_init (&s, BLAKE_2B_BLOCK_SIZE)) return ERROR_BLAKE_2B; for (unsigned int i = 0; i < blocks_to_comp; i++) { if (blake2b_update (&s, blocks[i], BLAKE_2B_BLOCK_SIZE)) return ERROR_BLAKE_2B; } if (blake2b_final (&s, out, BLAKE_2B_BLOCK_SIZE)) return ERROR_BLAKE_2B; return ERROR_NONE; }
bool nano::uint256_union::decode_account (std::string const & source_a) { auto error (source_a.size () < 5); if (!error) { auto xrb_prefix (source_a[0] == 'x' && source_a[1] == 'r' && source_a[2] == 'b' && (source_a[3] == '_' || source_a[3] == '-')); auto nano_prefix (source_a[0] == 'n' && source_a[1] == 'a' && source_a[2] == 'n' && source_a[3] == 'o' && (source_a[4] == '_' || source_a[4] == '-')); error = (xrb_prefix && source_a.size () != 64) || (nano_prefix && source_a.size () != 65); if (!error) { if (xrb_prefix || nano_prefix) { auto i (source_a.begin () + (xrb_prefix ? 4 : 5)); if (*i == '1' || *i == '3') { nano::uint512_t number_l; for (auto j (source_a.end ()); !error && i != j; ++i) { uint8_t character (*i); error = character < 0x30 || character >= 0x80; if (!error) { uint8_t byte (account_decode (character)); error = byte == '~'; if (!error) { number_l <<= 5; number_l += byte; } } } if (!error) { *this = (number_l >> 40).convert_to<nano::uint256_t> (); uint64_t check (number_l & static_cast<uint64_t> (0xffffffffff)); uint64_t validation (0); blake2b_state hash; blake2b_init (&hash, 5); blake2b_update (&hash, bytes.data (), bytes.size ()); blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), 5); error = check != validation; } }
/* one-shot hash inlen bytes from in */ void blake2b (unsigned char *hash, const unsigned char *in, size_t inlen) { blake2b_state S; blake2b_state_internal *state = (blake2b_state_internal *) &S; size_t bytes; blake2b_init (&S); /* hash until <= 128 bytes left */ bytes = blake2b_consume_blocks (state, in, inlen); in += bytes; inlen -= bytes; /* final block */ memset (&state->f[0], 0xff, 8); blake2b_opt->blake2b_blocks (state, in, inlen, BLAKE2B_STRIDE_NONE); blake2b_store_hash (state, hash); }
ERL_NIF_TERM blake2_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM handle_term; int bits = 0; if(!enif_get_int(env, argv[0], &bits)) return enif_make_badarg(env); blake2_handle *handle = (blake2_handle *)enif_alloc_resource_compat(env, blake2_hashstate, sizeof(blake2_handle)); handle->digest_length = bits / 8; int r = blake2b_init(&(handle->state), handle->digest_length); if (r == 0) { handle_term = enif_make_resource(env, handle); enif_release_resource_compat(env, handle); return enif_make_tuple2(env, enif_make_atom(env, "ok"), handle_term); } else { enif_release_resource_compat(env, handle); return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "init_failure")); } }
void nano::uint256_union::encode_account (std::string & destination_a) const { assert (destination_a.empty ()); destination_a.reserve (65); uint64_t check (0); blake2b_state hash; blake2b_init (&hash, 5); blake2b_update (&hash, bytes.data (), bytes.size ()); blake2b_final (&hash, reinterpret_cast<uint8_t *> (&check), 5); nano::uint512_t number_l (number ()); number_l <<= 40; number_l |= nano::uint512_t (check); for (auto i (0); i < 60; ++i) { uint8_t r (number_l & static_cast<uint8_t> (0x1f)); number_l >>= 5; destination_a.push_back (account_encode (r)); } destination_a.append ("_onan"); // nano_ std::reverse (destination_a.begin (), destination_a.end ()); }
int crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, const unsigned char *key, const size_t keylen, const size_t outlen) { if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || keylen > BLAKE2B_KEYBYTES) { return -1; } assert(outlen <= UINT8_MAX); assert(keylen <= UINT8_MAX); COMPILER_ASSERT(sizeof(blake2b_state) <= sizeof *state); if (key == NULL || keylen <= 0U) { if (blake2b_init((blake2b_state *) (void *) state, (uint8_t) outlen) != 0) { return -1; /* LCOV_EXCL_LINE */ } } else if (blake2b_init_key((blake2b_state *) (void *) state, (uint8_t) outlen, key, (uint8_t) keylen) != 0) { return -1; /* LCOV_EXCL_LINE */ } return 0; }
int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen ) { blake2b_state S[1]; /* Verify parameters */ if ( NULL == in ) return -1; if ( NULL == out ) return -1; if( NULL == key && key > 0 ) return -1; if( keylen > 0 ) { if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2b_init( S, outlen ) < 0 ) return -1; } if( blake2b_update( S, ( uint8_t * )in, inlen ) < 0 ) return -1; return blake2b_final( S, out, outlen ); }
const char* program_signature(ast_t* program) { pony_assert(program != NULL); pony_assert(ast_id(program) == TK_PROGRAM); program_t* data = (program_t*)ast_data(program); pony_assert(data != NULL); if(data->signature == NULL) { ast_t* first_package = ast_child(program); pony_assert(first_package != NULL); pony_assert(data->package_groups == NULL); data->package_groups = package_dependency_groups(first_package); blake2b_state hash_state; int status = blake2b_init(&hash_state, SIGNATURE_LENGTH); pony_assert(status == 0); package_group_list_t* iter = data->package_groups; while(iter != NULL) { package_group_t* group = package_group_list_data(iter); const char* group_sig = package_group_signature(group); blake2b_update(&hash_state, group_sig, SIGNATURE_LENGTH); iter = package_group_list_next(iter); } data->signature = (char*)ponyint_pool_alloc_size(SIGNATURE_LENGTH); status = blake2b_final(&hash_state, data->signature, SIGNATURE_LENGTH); pony_assert(status == 0); } return data->signature; }
int blake2b(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen) { blake2b_state S; int ret = -1; /* Verify parameters */ if (NULL == in && inlen > 0) { goto fail; } if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) { goto fail; } if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) { goto fail; } if (keylen > 0) { if (blake2b_init_key(&S, outlen, key, keylen) < 0) { goto fail; } } else { if (blake2b_init(&S, outlen) < 0) { goto fail; } } if (blake2b_update(&S, in, inlen) < 0) { goto fail; } ret = blake2b_final(&S, out, outlen); fail: burn(&S, sizeof(S)); return ret; }
int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) { blake2b_state S[1]; /* Verify parameters */ if ( NULL == in ) return -1; if ( NULL == out ) return -1; if( NULL == key ) keylen = 0; if( keylen ) { if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; } else { if( blake2b_init( S, outlen ) < 0 ) return -1; } blake2b_update( S, ( const uint8_t * )in, inlen ); blake2b_final( S, out, outlen ); return 0; }