Software APIs
status.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_CRYPTO_IMPL_STATUS_H_
6#define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_STATUS_H_
7
9#include "sw/device/lib/base/status.h"
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/**
18 * Values in `status_t` that are guaranteed to correspond to each
19 * `otcrypto_status_t` value.
20 *
21 * If `OTCRYPTO_STATUS_DEBUG` is set, full line-number and module information
22 * is included to ease debugging. Otherwise, we use the cryptolib error codes
23 * directly.
24 *
25 * Note: These values bypass `status_create` to avoid having a function call in
26 * error cases, where we may be under attack and complexity should be
27 * minimized.
28 */
29#define OTCRYPTO_OK ((status_t){.value = kHardenedBoolTrue})
30
31#define LAUNDERED_OTCRYPTO_OK \
32 ((status_t){.value = (int)launder32(kHardenedBoolTrue)})
33
34#ifdef OTCRYPTO_STATUS_DEBUG
35
36#define OTCRYPTO_RECOV_ERR \
37 ((status_t){.value = (int32_t)(0x80000000 | \
38 status_encode_module_id(MODULE_ID) << 16 | \
39 ((__LINE__ & 0x7ff) << 5) | kAborted)})
40#define OTCRYPTO_FATAL_ERR \
41 ((status_t){.value = \
42 (int32_t)(0x80000000 | \
43 status_encode_module_id(MODULE_ID) << 16 | \
44 ((__LINE__ & 0x7ff) << 5) | kFailedPrecondition)})
45#define OTCRYPTO_BAD_ARGS \
46 ((status_t){.value = \
47 (int32_t)(0x80000000 | \
48 status_encode_module_id(MODULE_ID) << 16 | \
49 ((__LINE__ & 0x7ff) << 5) | kInvalidArgument)})
50#define OTCRYPTO_ASYNC_INCOMPLETE \
51 ((status_t){.value = (int32_t)(0x80000000 | \
52 status_encode_module_id(MODULE_ID) << 16 | \
53 ((__LINE__ & 0x7ff) << 5) | kUnavailable)})
54#define OTCRYPTO_NOT_IMPLEMENTED \
55 ((status_t){.value = (int32_t)(0x80000000 | \
56 status_encode_module_id(MODULE_ID) << 16 | \
57 ((__LINE__ & 0x7ff) << 5) | kUnimplemented)})
58#else
59
60#define OTCRYPTO_RECOV_ERR \
61 ((status_t){.value = kOtcryptoStatusValueInternalError})
62#define OTCRYPTO_FATAL_ERR ((status_t){.value = kOtcryptoStatusValueFatalError})
63#define OTCRYPTO_BAD_ARGS ((status_t){.value = kOtcryptoStatusValueBadArgs})
64#define OTCRYPTO_ASYNC_INCOMPLETE \
65 ((status_t){.value = kOtcryptoStatusValueAsyncIncomplete})
66#define OTCRYPTO_NOT_IMPLEMENTED \
67 ((status_t){.value = kOtcryptoStatusValueNotImplemented})
68
69#endif
70
71#if !defined(OT_DISABLE_HARDENING) && defined(OT_PLATFORM_RV32)
72/**
73 * Compiler optimization barrier for `HARDENED_TRY`.
74 *
75 * Placed in a `static OT_ALWAYS_INLINE` helper without `volatile` so LLVM
76 * preserves identical source-location metadata and allows tail-merging of error
77 * return blocks, while still preventing correlated-value propagation from
78 * narrowing the `orig_ >= 0` sign check to `status_ == -1`.
79 */
80static OT_ALWAYS_INLINE uint32_t hardened_try_barrier_(uint32_t val) {
81 asm("" : "+r"(val));
82 return val;
83}
84
85/**
86 * Hardened version of the `TRY` macro from `status.h`.
87 *
88 * Returns an error if `expr_` represents an error status (negative `int32_t`),
89 * continues if `expr_` equals `OTCRYPTO_OK` (`kHardenedBoolTrue`), and traps on
90 * any other value (including non-hardened `0` or corrupted positive values).
91 *
92 * To resist both single-instruction skips and wider instruction-fetch skips
93 * (up to 8 bytes / two fetch words on Ibex) without bloating call sites, the
94 * error and trap guards are split across an 8-byte distance:
95 * 1. `addi rd, rd, -1` (2B compressed, in-place): maps all valid error codes
96 * (bit 31 set, non-zero lower bits) to negative values, `0` to `-1`,
97 * `INT32_MIN` (`0x80000000`) to `0x7fffffff` (traps fail-closed), and
98 * `kHardenedBoolTrue` (1849) to `1848`.
99 * 2. `bltz rd, .L_err` (4B): branches to the tail-merged error epilogue,
100 * which restores `rd + 1`, traps if the restored value is non-negative
101 * (`bgez rd, .L_trap`, catching both `0` and any faulted branch from the
102 * OK path), and returns the original error status.
103 * 3. `xori rd, rd, (kHardenedBoolTrue - 1)` (4B, in-place): maps `1848` to
104 * `0`, leaving any skipped error status (bit 31 set) non-zero.
105 * 4. `bnez rd, .L_trap` (2B compressed): traps if non-zero. Because `bltz`
106 * and `xori` span 8 bytes, no 4B or 8B instruction-fetch skip can cover
107 * both `bltz` and `bnez`, so skipping the error branch always falls into
108 * the trap.
109 *
110 * @param expr_ An expression that evaluates to a `status_t`.
111 * @return The enclosed OK value.
112 */
113#define HARDENED_TRY(expr_) \
114 do { \
115 int32_t status_ = (expr_).value; \
116 asm volatile("addi %0, %0, -1" : "+r"(status_)); \
117 if (status_ < 0) { \
118 int32_t orig_ = \
119 (int32_t)(hardened_try_barrier_(OT_UNSIGNED(status_)) + 1u); \
120 if (orig_ >= 0) { \
121 __builtin_trap(); \
122 } \
123 return (status_t){.value = orig_}; \
124 } \
125 asm volatile("xori %0, %0, %1" \
126 : "+r"(status_) \
127 : "i"(kHardenedBoolTrue - 1)); \
128 if (status_ != 0) { \
129 __builtin_trap(); \
130 } \
131 } while (false)
132// COVERAGE (FI CM) We do not cover the redundant checks of the HARDENED_TRY as
133// they serve as redundant encoding against fault attacks.
134#else // !OT_PLATFORM_RV32 || OT_DISABLE_HARDENING
135/**
136 * Alternate version of HARDENED_TRY that is logically equivalent.
137 *
138 * This can be used to measure the code size and performance impact of
139 * control-flow countermeasures.
140 *
141 * @param expr_ An expression that evaluates to a `status_t`.
142 * @return The enclosed OK value.
143 */
144#define HARDENED_TRY(expr_) \
145 do { \
146 status_t status_ = expr_; \
147 if (status_.value != kHardenedBoolTrue) { \
148 return (status_t){ \
149 .value = (int32_t)(OT_UNSIGNED(status_.value) | 0x80000000)}; \
150 } \
151 } while (false)
152
153#endif // OT_DISABLE_HARDENING
154
155#ifdef __cplusplus
156}
157#endif
158
159#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_STATUS_H_