/* Initialize the state. key is optional */ static int blake2b_init( blake2b_ctx * ctx, HB_SIZE outlen, const void * key, HB_SIZE keylen ) /* (keylen=0: no key) */ { HB_SIZE i; if( outlen == 0 || outlen > 64 || keylen > 64 ) return -1; /* illegal parameters */ for( i = 0; i < 8; i++ ) /* state, "param block" */ ctx->h[ i ] = blake2b_iv[ i ]; ctx->h[ 0 ] ^= 0x01010000 ^ ( keylen << 8 ) ^ outlen; ctx->t[ 0 ] = 0; /* input count low word */ ctx->t[ 1 ] = 0; /* input count high word */ ctx->c = 0; /* pointer within buffer */ ctx->outlen = outlen; for( i = keylen; i < 128; i++ ) /* zero input block */ ctx->b[ i ] = 0; if( keylen > 0 ) { blake2b_update( ctx, key, keylen ); ctx->c = 128; /* at the end */ } return 0; }
int blake2b_init(blake2b_ctx *ctx, size_t outlen, const void *key, size_t keylen) // (keylen=0: no key) { size_t i; if (outlen == 0 || outlen > 64 || keylen > 64) return -1; // illegal parameters for (i = 0; i < 8; i++) // state, "param block" ctx->h[i] = blake2b_iv[i]; ctx->h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen; ctx->t[0] = 0; // input count low word ctx->t[1] = 0; // input count high word ctx->c = 0; // pointer within buffer ctx->outlen = outlen; for (i = keylen; i < 128; i++) // zero input block ctx->b[i] = 0; if (keylen > 0) { blake2b_update(ctx, key, keylen); ctx->c = 128; // at the end } return 0; }
int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) { const blake2b_param P = { outlen, keylen, 1, 1, 0, 0, 0, 0, {0}, {0}, {0} }; if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; if ( ( !keylen ) || keylen > BLAKE2B_KEYBYTES ) return -1; if( blake2b_init_param( S, &P ) < 0 ) return 0; { uint8_t block[BLAKE2B_BLOCKBYTES]; memset( block, 0, BLAKE2B_BLOCKBYTES ); memcpy( block, key, keylen ); blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ } return 0; }
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 && 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 ) { 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; }
/* 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 ); }
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); }
int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) { if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1; if( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; memset( S->buf, 0, sizeof( S->buf ) ); S->buflen = 0; if( blake2bp_init_root( S->R, outlen, keylen ) < 0 ) return -1; for( size_t i = 0; i < PARALLELISM_DEGREE; ++i ) if( blake2bp_init_leaf( S->S[i], outlen, keylen, i ) < 0 ) return -1; S->R->last_node = 1; S->S[PARALLELISM_DEGREE - 1]->last_node = 1; { uint8_t block[BLAKE2B_BLOCKBYTES]; memset( block, 0, BLAKE2B_BLOCKBYTES ); memcpy( block, key, keylen ); for( size_t i = 0; i < PARALLELISM_DEGREE; ++i ) blake2b_update( S->S[i], block, BLAKE2B_BLOCKBYTES ); secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ } return 0; }
int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) { blake2b_param P[1]; if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) abort(); if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) abort(); P->digest_length = outlen; P->key_length = keylen; P->fanout = 1; P->depth = 1; STORE32_LE( P->leaf_length, 0 ); STORE64_LE( P->node_offset, 0 ); P->node_depth = 0; P->inner_length = 0; memset( P->reserved, 0, sizeof( P->reserved ) ); memset( P->salt, 0, sizeof( P->salt ) ); memset( P->personal, 0, sizeof( P->personal ) ); if( blake2b_init_param( S, P ) < 0 ) abort(); { uint8_t block[BLAKE2B_BLOCKBYTES]; memset( block, 0, BLAKE2B_BLOCKBYTES ); memcpy( block, key, keylen ); blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ } 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; }
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); }
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); }
int crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, const unsigned char *in, unsigned long long inlen) { return blake2b_update((blake2b_state *) (void *) state, (const uint8_t *) in, (uint64_t) inlen); }
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); }
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; }
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); }
void blake2b_init(struct blake2b *B, size_t dlen, const void *key, size_t keylen) { uint64_t param0; unsigned i; assert(0 < dlen); assert(dlen <= 64); assert(keylen <= 64); /* Record the digest length. */ B->dlen = dlen; /* Initialize the buffer. */ B->nb = 0; /* Initialize the state. */ B->c = 0; for (i = 0; i < 8; i++) B->h[i] = blake2b_iv[i]; /* * Set the parameters. We support only variable digest and key * lengths: no tree hashing, no salt, no personalization. */ param0 = 0; param0 |= (uint64_t)dlen << 0; param0 |= (uint64_t)keylen << 8; param0 |= (uint64_t)1 << 16; /* tree fanout = 1 */ param0 |= (uint64_t)1 << 24; /* tree depth = 1 */ B->h[0] ^= param0; /* If there's a key, compress it as the first message block. */ if (keylen) { static const uint8_t zero_block[128]; blake2b_update(B, key, keylen); blake2b_update(B, zero_block, 128 - keylen); } }
/* 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; }
/* 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; }
int blake2bp_final( blake2bp_state *S, uint8_t *out, const uint8_t outlen ) { uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES]; 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 ); }
ERL_NIF_TERM blake2_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { blake2_handle *handle = NULL; enif_get_resource(env, argv[0], blake2_hashstate, (void**)&handle); ErlNifBinary bin; enif_inspect_binary(env, argv[1], &bin); int r = blake2b_update(&(handle->state), (const uint8_t *)bin.data, (uint64_t)bin.size); if (r == 0) { return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_resource(env, handle)); } else { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "update_failure")); } }
int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen ) { blake2b_param P[1]; if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; P->digest_length = outlen; P->key_length = keylen; P->fanout = 1; P->depth = 1; store32( &P->leaf_length, 0 ); store64( &P->node_offset, 0 ); P->node_depth = 0; P->inner_length = 0; XMEMSET( P->reserved, 0, sizeof( P->reserved ) ); XMEMSET( P->salt, 0, sizeof( P->salt ) ); XMEMSET( P->personal, 0, sizeof( P->personal ) ); if( blake2b_init_param( S, P ) < 0 ) return -1; { #ifdef WOLFSSL_SMALL_STACK byte* block; block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); if ( block == NULL ) return -1; #else byte block[BLAKE2B_BLOCKBYTES]; #endif XMEMSET( block, 0, BLAKE2B_BLOCKBYTES ); XMEMCPY( block, key, keylen ); blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */ /* memory */ #ifdef WOLFSSL_SMALL_STACK XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif } return 0; }
bool MUtils::Hash::Blake2::process(const quint8 *const data, const quint32 len) { if(m_finalized) { MUTILS_THROW("BLAKE2 was already finalized!"); } if(data && (len > 0)) { if(blake2b_update(m_context->state, data, len) != 0) { MUTILS_THROW("BLAKE2 internal error!"); } } return true; }
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; }
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key, size_t keylen) { blake2b_param P; if (S == NULL) { return -1; } if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) { blake2b_invalidate_state(S); return -1; } if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) { blake2b_invalidate_state(S); return -1; } /* Setup Parameter Block for keyed BLAKE2 */ P.digest_length = (uint8_t)outlen; P.key_length = (uint8_t)keylen; P.fanout = 1; P.depth = 1; P.leaf_length = 0; P.node_offset = 0; P.node_depth = 0; P.inner_length = 0; memset(P.reserved, 0, sizeof(P.reserved)); memset(P.salt, 0, sizeof(P.salt)); memset(P.personal, 0, sizeof(P.personal)); if (blake2b_init_param(S, &P) < 0) { blake2b_invalidate_state(S); return -1; } { uint8_t block[BLAKE2B_BLOCKBYTES]; memset(block, 0, BLAKE2B_BLOCKBYTES); memcpy(block, key, keylen); blake2b_update(S, block, BLAKE2B_BLOCKBYTES); burn(block, BLAKE2B_BLOCKBYTES); /* Burn the key from stack */ } return 0; }
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_Update(hash_ctx *const ctx, const unsigned char *const msg, const size_t size) { switch(ctx->hashType) { case STD_HASHTYPE_CRC_32: rhash_crc32_update(&ctx->data.crc32, msg, size); return; case STD_HASHTYPE_MD5_128: rhash_md5_update(&ctx->data.md5, msg, size); return; case STD_HASHTYPE_SHA1_160: rhash_sha1_update(&ctx->data.sha1, msg, size); return; case STD_HASHTYPE_SHA2_224: case STD_HASHTYPE_SHA2_256: rhash_sha256_update(&ctx->data.sha256, msg, size); return; case STD_HASHTYPE_SHA2_384: case STD_HASHTYPE_SHA2_512: rhash_sha512_update(&ctx->data.sha512, msg, size); return; case STD_HASHTYPE_SHA3_224: case STD_HASHTYPE_SHA3_256: case STD_HASHTYPE_SHA3_384: case STD_HASHTYPE_SHA3_512: rhash_sha3_update(&ctx->data.sha3, msg, size); return; case STD_HASHTYPE_BLK2_224: case STD_HASHTYPE_BLK2_256: case STD_HASHTYPE_BLK2_384: case STD_HASHTYPE_BLK2_512: if(blake2b_update(&ctx->data.balke2, msg, size) != 0) { MessageBox(NULL, T("Blake2_Update internal error, going to abort!"), T("StdUtils::HashFunction_Update"), MB_ICONSTOP | MB_TOPMOST); abort(); } return; default: MessageBox(NULL, T("Inconsistent state detected, going to abort!"), T("StdUtils::HashFunction_Update"), 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 ()); }
int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen, const void *salt, const void *personal ) { blake2b_param P[1]; if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; P->digest_length = outlen; P->key_length = keylen; P->fanout = 1; P->depth = 1; store32( &P->leaf_length, 0 ); store64( &P->node_offset, 0 ); P->node_depth = 0; P->inner_length = 0; memset( P->reserved, 0, sizeof( P->reserved ) ); if (salt != NULL) { blake2b_param_set_salt( P, (const uint8_t *) salt ); } else { memset( P->salt, 0, sizeof( P->salt ) ); } if (personal != NULL) { blake2b_param_set_personal( P, (const uint8_t *) personal ); } else { memset( P->personal, 0, sizeof( P->personal ) ); } if( blake2b_init_param( S, P ) < 0 ) return -1; { uint8_t block[BLAKE2B_BLOCKBYTES]; memset( block, 0, BLAKE2B_BLOCKBYTES ); memcpy( block, key, keylen ); blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ } 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 ); }