CRYPTO_HMAC_SHA512(3MONOCYPHER) | 3MONOCYPHER | CRYPTO_HMAC_SHA512(3MONOCYPHER) |
crypto_hmac_sha512
,
crypto_hmac_sha512_init
,
crypto_hmac_sha512_update
,
crypto_hmac_sha512_final
—
#include <monocypher-ed25519.h>
void
crypto_hmac_sha512
(uint8_t
hmac[64], const uint8_t *key,
size_t key_size, const uint8_t
*message, size_t message_size);
void
crypto_hmac_sha512_init
(crypto_hmac_sha512_ctx
*ctx, const uint8_t *key, size_t
key_size);
void
crypto_hmac_sha512_update
(crypto_hmac_sha512_ctx
*ctx, const uint8_t *message,
size_t message_size);
void
crypto_hmac_sha512_final
(crypto_hmac_sha512_ctx
*ctx, uint8_t hmac[64]);
The arguments are:
NULL
if
message_size is 0.An incremental interface is provided. It is useful for handling streams of data or large files without using too much memory. This interface uses three steps:
crypto_hmac_sha512_init
(),
which sets up a context with the hashing parameters;crypto_hmac_sha512_update
(), which
hashes the message chunk by chunk and keeps the intermediary result in the
context;crypto_hmac_sha512_final
(),
which produces the final hash. The
crypto_hmac_sha512_ctx is automatically wiped upon
finalisation.crypto_hmac_sha512
() is a convenience
function that performs crypto_hmac_sha512_init
(),
crypto_hmac_sha512_update
(), and
crypto_hmac_sha512_final
().
MACs may be truncated safely down to at most 16 bytes; the crypto_verify64(3monocypher), crypto_verify32(3monocypher), and crypto_verify16(3monocypher) functions can be used to compare (possibly truncated) MACs.
arc4random_buf
(), which fills the given buffer with
cryptographically secure random bytes. If
arc4random_buf
() does not exist on your system, see
intro(3monocypher) for advice about how to
generate cryptographically secure random bytes.
Computing a message authentication code all at once:
uint8_t hash [64]; /* Output hash */ uint8_t key [32]; /* Key */ uint8_t message[10] = "Lorem ipsu"; /* Message to authenticate */ arc4random_buf(key, 32); crypto_hmac_sha512(hash, key, 32, message, 10); /* Wipe secrets if they are no longer needed */ crypto_wipe(message, 10); crypto_wipe(key, 32);
Computing a message authentication code incrementally:
uint8_t hash [64]; /* Output hash */ uint8_t key [32]; /* Key */ uint8_t message[500] = {1}; /* Message to authenticate */ crypto_hmac_sha512_ctx ctx; arc4random_buf(key, 32); crypto_hmac_sha512_init(&ctx, key, 32); /* Wipe the key */ crypto_wipe(key, 32); for (size_t i = 0; i < 500; i += 100) { crypto_hmac_sha512_update(&ctx, message + i, 100); /* Wipe secrets if they are no longer needed */ crypto_wipe(message + i, 100); } crypto_hmac_sha512_final(&ctx, hash);
crypto_hmac_sha512
(),
crypto_hmac_sha512_init
(),
crypto_hmac_sha512_update
(), and
crypto_hmac_sha512_final
() functions first appeared in
Monocypher 3.0.0.
March 2, 2020 | Debian |