mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-14 07:03:44 +01:00
Add a function to compute an XOF in one shot.
Motivation: 1. It's convenient. 2. It's all that openssl supports. Part of 28837.
This commit is contained in:
parent
c393171403
commit
9b0dd1ae04
@ -955,3 +955,27 @@ crypto_xof_free_(crypto_xof_t *xof)
|
|||||||
memwipe(xof, 0, sizeof(crypto_xof_t));
|
memwipe(xof, 0, sizeof(crypto_xof_t));
|
||||||
tor_free(xof);
|
tor_free(xof);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compute the XOF (SHAKE256) of a <b>input_len</b> bytes at <b>input</b>,
|
||||||
|
* putting <b>output_len</b> bytes at <b>output</b>. */
|
||||||
|
void
|
||||||
|
crypto_xof(uint8_t *output, size_t output_len,
|
||||||
|
const uint8_t *input, size_t input_len)
|
||||||
|
{
|
||||||
|
#ifdef OPENSSL_HAS_SHA3
|
||||||
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
||||||
|
tor_assert(ctx);
|
||||||
|
int r = EVP_DigestInit(ctx, EVP_shake256());
|
||||||
|
tor_assert(r == 1);
|
||||||
|
r = EVP_DigestUpdate(ctx, input, input_len);
|
||||||
|
tor_assert(r == 1);
|
||||||
|
r = EVP_DigestFinalXOF(ctx, output, output_len);
|
||||||
|
tor_assert(r == 1);
|
||||||
|
EVP_MD_CTX_free(ctx);
|
||||||
|
#else
|
||||||
|
crypto_xof_t *xof = crypto_xof_new();
|
||||||
|
crypto_xof_add_bytes(xof, input, input_len);
|
||||||
|
crypto_xof_squeeze_bytes(xof, output, output_len);
|
||||||
|
crypto_xof_free(xof);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@ -124,6 +124,8 @@ void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
|
|||||||
void crypto_xof_free_(crypto_xof_t *xof);
|
void crypto_xof_free_(crypto_xof_t *xof);
|
||||||
#define crypto_xof_free(xof) \
|
#define crypto_xof_free(xof) \
|
||||||
FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
|
FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
|
||||||
|
void crypto_xof(uint8_t *output, size_t output_len,
|
||||||
|
const uint8_t *input, size_t input_len);
|
||||||
|
|
||||||
#ifdef TOR_UNIT_TESTS
|
#ifdef TOR_UNIT_TESTS
|
||||||
digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
|
digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
|
||||||
|
@ -1173,6 +1173,11 @@ test_crypto_sha3_xof(void *arg)
|
|||||||
crypto_xof_free(xof);
|
crypto_xof_free(xof);
|
||||||
memset(out, 0, sizeof(out));
|
memset(out, 0, sizeof(out));
|
||||||
|
|
||||||
|
/* Test one-function absorb/squeeze. */
|
||||||
|
crypto_xof(out, sizeof(out), msg, sizeof(msg));
|
||||||
|
test_memeq_hex(out, squeezed_hex);
|
||||||
|
memset(out, 0, sizeof(out));
|
||||||
|
|
||||||
/* Test incremental absorb/squeeze. */
|
/* Test incremental absorb/squeeze. */
|
||||||
xof = crypto_xof_new();
|
xof = crypto_xof_new();
|
||||||
tt_assert(xof);
|
tt_assert(xof);
|
||||||
|
Loading…
Reference in New Issue
Block a user