Software APIs
kmac_testutils.h
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_KMAC_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_KMAC_TESTUTILS_H_
7
8#include "sw/device/lib/base/status.h"
10
11/**
12 * Configure the KMAC block.
13 *
14 * This is a thin wrapper around `dif_kmac_configure`. It sets up a testing
15 * configuration that prioritizes speed over security, since we don't care
16 * about protecting test keys. For instance, it sets the entropy source to
17 * software so KMAC doesn't wait for fresh entropy, and minimizes masking.
18 *
19 * @param kmac KMAC context.
20 * @param sideload Whether to configure KMAC to read a sideloaded key.
21 * @return The result of the operation.
22 */
24status_t kmac_testutils_config(dif_kmac_t *kmac, bool sideload);
25
26/**
27 * Runs the full KMAC operation.
28 *
29 * Assumes that the KMAC block has already been initialized and configured, and
30 * that the sideloaded key has been loaded (if applicable).
31 *
32 * @param kmac KMAC block context.
33 * @param mode Mode (security strength) for KMAC.
34 * @param key KMAC key.
35 * @param custom_string Customization string.
36 * @param custom_string_len Length of customization string.
37 * @param message Input message.
38 * @param message_len Length of message in bytes.
39 * @param output_len Requested length of output in words.
40 * @param[out] output Pre-allocated output buffer (length must match
41 * output_len).
42 * @param[out] capacity Optional buffer to read the capacity of Keccak state.
43 * @return The result of the operation.
44 */
46status_t kmac_testutils_kmac(const dif_kmac_t *kmac, dif_kmac_mode_kmac_t mode,
47 const dif_kmac_key_t *key,
48 const char *custom_string,
49 const size_t custom_string_len,
50 const char *message, const size_t message_len,
51 const size_t output_len, uint32_t *output,
52 uint32_t *capacity);
53
54/**
55 * Check if the KMAC HW has flagged any errors and acknowledge them.
56 *
57 * @param kmac A KMAC DIF handle.
58 * @return Ok if there were no errors; non-Ok if there was any error.
59 */
61status_t kmac_testutils_check_error(const dif_kmac_t *kmac);
62
63#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_KMAC_TESTUTILS_H_