/* 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)); }
static gint lua_cryptobox_hash_gc (lua_State *L) { rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1); rspamd_explicit_memzero (h, sizeof (*h)); g_slice_free1 (sizeof (*h), h); return 0; }
/* finalize the hash */ void blake2b_final (blake2b_state *S, unsigned char *hash) { blake2b_state_internal *state = (blake2b_state_internal *) S; memset (&state->f[0], 0xff, 8); blake2b_opt->blake2b_blocks (state, state->buffer, state->leftover, BLAKE2B_STRIDE_NONE); blake2b_store_hash (state, hash); rspamd_explicit_memzero (state, sizeof (*state)); }
/* finalize, write out any leftover data */ size_t chacha_final (chacha_state *S, unsigned char *out) { chacha_state_internal *state = (chacha_state_internal *) S; size_t leftover = state->leftover; if (leftover) { if (chacha_is_aligned (out)) { chacha_impl->chacha_blocks (state, state->buffer, out, leftover); } else { chacha_impl->chacha_blocks (state, state->buffer, state->buffer, leftover); memcpy (out, state->buffer, leftover); } } rspamd_explicit_memzero (S, sizeof(chacha_state)); return leftover; }
static gint lua_cryptobox_hash_gc (lua_State *L) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); if (h->is_ssl) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) EVP_MD_CTX_cleanup (h->c); #else EVP_MD_CTX_reset (h->c); #endif EVP_MD_CTX_destroy (h->c); } else { rspamd_explicit_memzero (h->h, sizeof (*h->h)); g_slice_free1 (sizeof (*h->h), h->h); } g_slice_free1 (sizeof (*h), h); return 0; }