1 /*
2  * Copyright (c) 2023, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef CC3XX_STDLIB_H
9 #define CC3XX_STDLIB_H
10 
11 #include "stdint.h"
12 #include "stddef.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief                        Generate a randomized sequence of integers from
20  *                               0 to len-1 inclusive, in randomized order.
21  *
22  * @param[out] permutation_buf   Destination buffer to copy into. Must be len
23  *                               bytes in length.
24  * @param[in]  len               The size of the sequence to generate.
25  */
26 void cc3xx_random_permutation_generate(uint8_t *permutation_buf, size_t len);
27 
28 /**
29  * @brief                        Copy a series of words in a randomised order.
30  *                               Intended to be used as a DPA countermeasure
31  *                               when copying key materal.
32  *
33  * @note                         This function may take a variable amount of
34  *                               time to execute.
35  *
36  * @param[out] dst               Destination buffer to copy into
37  * @param[in]  src               Source buffer to copy from.
38  * @param[in]  word_count        The amount of words to copy.
39  */
40 void cc3xx_dpa_hardened_word_copy(volatile uint32_t *dst,
41                                   volatile const uint32_t *src, size_t word_count);
42 /**
43  * @brief Securely erases the buffer pointed by buf by overwriting it with random values. Assumes
44  *        the size of the buffer in bytes is a multiple of 4
45  *
46  * @param[in,out] buf        Pointer to the input buffer, must be 4 byte aligned
47  * @param[in]     word_count Length in words of the input buffer
48  *
49  */
50 void cc3xx_secure_erase_buffer(uint32_t *buf, size_t word_count);
51 
52 #ifdef __cplusplus
53 }
54 #endif
55 
56 #endif /* CC3XX_STDLIB_H */
57