Software APIs
aes_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_AES_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_AES_TESTUTILS_H_
7
9#ifndef OPENTITAN_IS_ENGLISHBREAKFAST
12#endif
14#include "sw/device/lib/testing/test_framework/check.h"
15
16/**
17 * Returns the value of the AES status flag.
18 *
19 * @param aes An aes DIF handle.
20 * @param status Status value to query.
21 */
22inline bool aes_testutils_get_status(dif_aes_t *aes, dif_aes_status_t status) {
23 bool set;
24 dif_result_t res = dif_aes_get_status(aes, status, &set);
25 return (res == kDifOk) && set;
26}
27
28/**
29 * Waits for the given AES status flag to be set the given value.
30 *
31 * @param aes An aes DIF handle.
32 * @param flag Status flag value.
33 * @param status The status value.
34 * @param timeout_usec Timeout in microseconds.
35 */
36#define AES_TESTUTILS_WAIT_FOR_STATUS(aes_, status_, flag_, timeout_usec_) \
37 IBEX_TRY_SPIN_FOR(aes_testutils_get_status((aes_), (status_)) == (flag_), \
38 (timeout_usec_))
39
40#ifndef OPENTITAN_IS_ENGLISHBREAKFAST
41/**
42 * Initializes the entropy complex for performing AES SCA measurements with
43 * masking switched off.
44 *
45 * Initializes CSRNG and EDN0 to produce a fixed seed which after being loaded
46 * into AES causes the AES masking PRNG to output an all-zero vector. Entropy
47 * src and EDN1 are left untouched.
48 *
49 * @param csrng A CSRNG DIF handle.
50 * @param edn0 An EDN DIF handle.
51 * @return The result of the operation.
52 */
54status_t aes_testutils_masking_prng_zero_output_seed(const dif_csrng_t *csrng,
55 const dif_edn_t *edn0);
56
57/**
58 * CTR_DRBG Known-Answer-Test (KAT) using the CSRNG SW application interface.
59 *
60 * Initializes CSRNG and then runs multiple generate and a reseed command to
61 * ensure the seed leading to an all-zero output of the AES masking PRNG can
62 * repeatedly be generated.
63 *
64 * @param csrng A CSRNG DIF handle.
65 * @return The result of the operation.
66 */
68status_t aes_testutils_csrng_kat(const dif_csrng_t *csrng);
69#endif
70
71/**
72 * Sets up an AES encryption.
73 *
74 * Starts the encryption operation by loading a key share along with some
75 * plain text and transaction data into the AES block.
76 * This function is used in tandem with aes_testutils_decrypt_ciphertext.
77 *
78 * @param transaction The AES transaction settings.
79 * @param aes An AES handle.
80 * @return The result of the operation.
81 */
83status_t aes_testutils_setup_encryption(dif_aes_transaction_t transaction,
84 dif_aes_t *aes);
85
86/**
87 * Reads some produced cipher text and sets up an AES decryption.
88 *
89 * Starts the decryption operation by loading a key along with some
90 * cipher text and transaction data into the AES block. Also checks whether
91 * the produced cipher text and decrypted cipher text are correct.
92 * This function is used in tandem with aes_testutils_setup_encryption.
93 *
94 * @param transaction The AES transaction settings used for encryption.
95 * @param aes An AES handle.
96 * @return The result of the operation.
97 */
99status_t aes_testutils_decrypt_ciphertext(dif_aes_transaction_t transaction,
100 dif_aes_t *aes);
101
102#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_AES_TESTUTILS_H_