int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen ) { uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES]; if(S->outlen != outlen) return -1; for( size_t i = 0; i < PARALLELISM_DEGREE; ++i ) { if( S->buflen > i * BLAKE2B_BLOCKBYTES ) { size_t left = S->buflen - i * BLAKE2B_BLOCKBYTES; if( left > BLAKE2B_BLOCKBYTES ) left = BLAKE2B_BLOCKBYTES; blake2b_update( S->S[i], S->buf + i * BLAKE2B_BLOCKBYTES, left ); } blake2b_final( S->S[i], hash[i], BLAKE2B_OUTBYTES ); } for( size_t i = 0; i < PARALLELISM_DEGREE; ++i ) blake2b_update( S->R, hash[i], BLAKE2B_OUTBYTES ); return blake2b_final( S->R, out, outlen ); }
/* 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 ); }
/* 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)); }
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; }
int blake2b_salt_personal( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen, const void *salt, const void *personal ) { blake2b_state S[1]; /* Verify parameters */ if( NULL == in && inlen > 0 ) abort(); if( NULL == out ) abort(); if( !outlen || outlen > BLAKE2B_OUTBYTES ) abort(); if( NULL == key && keylen > 0 ) abort(); if( keylen > BLAKE2B_KEYBYTES ) abort(); if( keylen > 0 ) { if( blake2b_init_key_salt_personal( S, outlen, key, keylen, salt, personal ) < 0 ) abort(); } else { if( blake2b_init_salt_personal( S, outlen, salt, personal ) < 0 ) abort(); } blake2b_update( S, ( const uint8_t * )in, inlen ); blake2b_final( S, out, outlen ); return 0; }
/* 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; }
int blake2b_salt_personal( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen, const void *salt, const void *personal ) { 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_salt_personal( S, outlen, key, keylen, salt, personal ) < 0 ) return -1; } else { if( blake2b_init_salt_personal( S, outlen, salt, personal ) < 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); }
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)); }
int crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, unsigned char *out, const size_t outlen) { assert(outlen <= UINT8_MAX); return blake2b_final((blake2b_state *) (void *) state, (uint8_t *) out, (uint8_t) outlen); }
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; }
QByteArray MUtils::Hash::Blake2::finalize(void) { QByteArray result(HASH_SIZE, '\0'); if(blake2b_final(m_context->state, (uint8_t*) result.data(), result.size()) != 0) { MUTILS_THROW("BLAKE2 internal error!"); } m_finalized = true; return result; }
void blake2b_keyed (unsigned char *hash, const unsigned char *in, size_t inlen, const unsigned char *key, size_t keylen) { blake2b_state S; blake2b_keyed_init (&S, key, keylen); blake2b_update (&S, in, inlen); blake2b_final (&S, hash); }
int svi_encfile(const char* outfn, const char* tagfn, const char* headerfn, const char* infn, const uint8_t* gkn) { int err = 0; mmfile inmf; err = mmfile_open(&inmf, infn, O_RDONLY); if (err != 0) { E(done, "mmfile_open %s", infn); } uint64_t outlen = svi_buflen(inmf.length); uint64_t numblocks = outlen / 65536; uint64_t taglen = numblocks * 64; mmfile outmf; err = mmfile_create(&outmf, outfn, outlen); if (err != 0) { E(cleanup_inmf, "mmfile_create %s", infn); } mmfile tagsmf; err = mmfile_create(&tagsmf, tagfn, taglen); if (err != 0) { E(cleanup_outmf, "mmfile_create %s", tagfn); } mmfile headermf; err = mmfile_create(&headermf, headerfn, 4096); if (err != 0) { E(cleanup_tagsmf, "mmfile_create %s", headerfn); } memset_s(headermf.mem, headermf.length, 0, headermf.length); err = svi_encrypt(outmf.mem, tagsmf.mem, headermf.mem, inmf.mem); if (err != 0) { // Something really bad happened. We can't assess what went // wrong, so we sanitize all of the output files. memset_s(outmf.mem, outmf.length, 0, outmf.length); memset_s(tagsmf.mem, tagsmf.length, 0, tagsmf.length); memset_s(headermf.mem, headermf.length, 0, headermf.length); E(cleanup_headermf, "svi_inplace %u", err); } uint8_t headertag[64] = {0}; blake2b_state s; blake2b_init_key(&s, 64, gkn, 64); blake2b_update(&s, header, HEADERLEN); blake2b_final(&s, headertag, 64); // Clean up the blake2b state. memset_s(&s, sizeof(blake2b_state), 0, sizeof(blake2b_state)); cleanup_headermf: mmfile_close(&headermf); cleanup_tagsmf: mmfile_close(&tagsmf); cleanup_outmf: mmfile_close(&outmf); cleanup_inmf: mmfile_close(&inmf); done: return err; }
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; }
/* 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_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)); }
ERL_NIF_TERM blake2_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { blake2_handle *handle = NULL; enif_get_resource(env, argv[0], blake2_hashstate, (void**)&handle); ErlNifBinary out; enif_alloc_binary_compat(env, (size_t)(handle->digest_length), &out); int r = blake2b_final(&(handle->state), (uint8_t *)out.data, handle->digest_length); if (r == 0) { return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_binary(env, &out)); } else { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "finalization_failure")); } }
/* 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; } }
static inline void HashFunction_Final(hash_ctx *const ctx, unsigned char *const result) { switch(ctx->hashType) { case STD_HASHTYPE_CRC_32: rhash_crc32_final(&ctx->data.crc32, result); return; case STD_HASHTYPE_MD5_128: rhash_md5_final(&ctx->data.md5, result); return; case STD_HASHTYPE_SHA1_160: rhash_sha1_final(&ctx->data.sha1, result); return; case STD_HASHTYPE_SHA2_224: case STD_HASHTYPE_SHA2_256: rhash_sha256_final(&ctx->data.sha256, result); return; case STD_HASHTYPE_SHA2_384: case STD_HASHTYPE_SHA2_512: rhash_sha512_final(&ctx->data.sha512, result); return; case STD_HASHTYPE_SHA3_224: case STD_HASHTYPE_SHA3_256: case STD_HASHTYPE_SHA3_384: case STD_HASHTYPE_SHA3_512: rhash_sha3_final(&ctx->data.sha3, result); return; case STD_HASHTYPE_BLK2_224: case STD_HASHTYPE_BLK2_256: case STD_HASHTYPE_BLK2_384: case STD_HASHTYPE_BLK2_512: if(blake2b_final(&ctx->data.balke2, result, Blake2_Size(ctx->hashType)) != 0) { MessageBox(NULL, T("Blake2_Final internal error, going to abort!"), T("StdUtils::HashFunction_Final"), MB_ICONSTOP | MB_TOPMOST); abort(); } return; default: MessageBox(NULL, T("Inconsistent state detected, going to abort!"), T("StdUtils::HashFunction_Final"), MB_ICONSTOP | MB_TOPMOST); abort(); } }
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 ()); }