Pavona Software APIs
hardened_memory.h
Go to the documentation of this file.
1// Copyright lowRISC contributors (OpenTitan project).
2// Copyright zeroRISC Inc.
3// Licensed under the Apache License, Version 2.0, see LICENSE for details.
4// SPDX-License-Identifier: Apache-2.0
5
6#ifndef OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_MEMORY_H_
7#define OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_MEMORY_H_
8
9/**
10 * @file
11 * @brief Hardened memory operations for constant power buffer manipulation.
12 */
13
14#include <stddef.h>
15#include <stdint.h>
16
19
20#ifdef __cplusplus
21extern "C" {
22#endif // __cplusplus
23
24/**
25 * Expects some external implementation of randomness to be linked.
26 *
27 * @return A fresh random word.
28 */
29extern uint32_t hardened_memshred_random_word(void);
30
31/**
32 * Copies 32-bit words between non-overlapping regions.
33 *
34 * Unlike `memcpy()`, this function has important differences:
35 * - It is significantly slower, since it mitigates power-analysis attacks.
36 * - It performs operations on 32-bit words, rather than bytes.
37 * - It returns void.
38 *
39 * Input pointers *MUST* be 32-bit aligned, although they do not need to
40 * actually point to memory declared as `uint32_t` per the C aliasing rules.
41 * Internally, this function is careful to not dereference its operands
42 * directly, and instead uses dedicated load/store intrinsics.
43 *
44 * @param dest The destination of the copy.
45 * @param src The source of the copy.
46 * @param word_len The number of words to copy.
47 */
48void hardened_memcpy(uint32_t *OT_RESTRICT dest,
49 const uint32_t *OT_RESTRICT src, size_t word_len);
50
51/**
52 * Fills a 32-bit aligned region of memory with random data.
53 *
54 * Unlike `memset()`, this function has important differences:
55 * - It is significantly slower, since it mitigates power-analysis attacks.
56 * - It performs operations on 32-bit words, rather than bytes.
57 * - A fill value cannot be specified.
58 * - It returns void.
59 *
60 * Input pointers *MUST* be 32-bit aligned, although they do not need to
61 * actually point to memory declared as `uint32_t` per the C aliasing rules.
62 * Internally, this function is careful to not dereference its operands
63 * directly, and instead uses dedicated load/store intrinsics.
64 *
65 * @param dest The destination of the set.
66 * @param word_len The number of words to write.
67 */
68void hardened_memshred(uint32_t *dest, size_t word_len);
69
70/**
71 * Compare two potentially-overlapping 32-bit aligned regions of memory for
72 * equality.
73 *
74 * Unlike `memcmp()`, this function has important differences:
75 * - It is significantly slower, since it mitigates power-analysis attacks.
76 * - It performs operations on 32-bit words, rather than bytes.
77 * - It only computes equality, not lexicographic ordering, which would be even
78 * slower.
79 * - It returns a `hardened_bool_t`.
80 * - It is constant-time.
81 *
82 * Input pointers *MUST* be 32-bit aligned, although they do not need to
83 * actually point to memory declared as `uint32_t` per the C aliasing rules.
84 * Internally, this function is careful to not dereference its operands
85 * directly, and instead uses dedicated load/store intrinsics.
86 *
87 * @param lhs The first buffer to compare.
88 * @param rhs The second buffer to compare.
89 * @param word_len The number of words to write.
90 */
91hardened_bool_t hardened_memeq(const uint32_t *lhs, const uint32_t *rhs,
92 size_t word_len);
93
94/**
95 * Combines two word buffers with XOR.
96 *
97 * Callers should ensure the entropy complex is up before calling this
98 * function. The implementation uses random-order hardening primitives for
99 * side-channel defense.
100 *
101 * @param[in,out] x Pointer to the first operand (modified in-place).
102 * @param y Pointer to the second operand.
103 * @param word_len Length in words of each operand.
104 */
105void hardened_xor(uint32_t *OT_RESTRICT x, const uint32_t *OT_RESTRICT y,
106 size_t word_len);
107
108/**
109 * Writes 32-bit words into an MMIO location.
110 *
111 * Similar to `hardened_memcpy`, but treats the destination as volatile.
112 *
113 * @param dest The destination of the copy (MMIO address).
114 * @param src The source of the copy.
115 * @param word_len The number of words to copy.
116 */
117void hardened_mmio_write(uint32_t dest, const uint32_t *src, size_t word_len);
118
119/**
120 * Reads 32-bit words from an MMIO location.
121 *
122 * Similar to `hardened_memcpy`, but treats the destination as volatile.
123 *
124 * @param dest The destination of the copy.
125 * @param src The source of the copy (MMIO address).
126 * @param word_len The number of words to copy.
127 */
128void hardened_mmio_read(uint32_t *dest, uint32_t src, size_t word_len);
129
130#ifdef __cplusplus
131} // extern "C"
132#endif // __cplusplus
133
134#endif // OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_MEMORY_H_