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"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /**
17  * Values in `status_t` that are guaranteed to correspond to each
18  * `otcrypto_status_t` value.
19  *
20  * If `OTCRYPTO_STATUS_DEBUG` is set, full line-number and module information
21  * is included to ease debugging. Otherwise, we use the cryptolib error codes
22  * directly.
23  *
24  * Note: These values bypass `status_create` to avoid having a function call in
25  * error cases, where we may be under attack and complexity should be
26  * minimized.
27  */
28 #define OTCRYPTO_OK ((status_t){.value = kHardenedBoolTrue})
29 #ifdef OTCRYPTO_STATUS_DEBUG
30 
31 #define OTCRYPTO_RECOV_ERR \
32  ((status_t){.value = (int32_t)(0x80000000 | MODULE_ID | \
33  ((__LINE__ & 0x7ff) << 5) | kAborted)})
34 #define OTCRYPTO_FATAL_ERR \
35  ((status_t){.value = \
36  (int32_t)(0x80000000 | MODULE_ID | \
37  ((__LINE__ & 0x7ff) << 5) | kFailedPrecondition)})
38 #define OTCRYPTO_BAD_ARGS \
39  ((status_t){.value = \
40  (int32_t)(0x80000000 | MODULE_ID | \
41  ((__LINE__ & 0x7ff) << 5) | kInvalidArgument)})
42 #define OTCRYPTO_ASYNC_INCOMPLETE \
43  ((status_t){.value = (int32_t)(0x80000000 | MODULE_ID | \
44  ((__LINE__ & 0x7ff) << 5) | kUnavailable)})
45 #define OTCRYPTO_NOT_IMPLEMENTED \
46  ((status_t){.value = (int32_t)(0x80000000 | MODULE_ID | \
47  ((__LINE__ & 0x7ff) << 5) | kUnimplemented)})
48 #else
49 
50 #define OTCRYPTO_RECOV_ERR \
51  ((status_t){.value = kOtcryptoStatusValueInternalError})
52 #define OTCRYPTO_FATAL_ERR ((status_t){.value = kOtcryptoStatusValueFatalError})
53 #define OTCRYPTO_BAD_ARGS ((status_t){.value = kOtcryptoStatusValueBadArgs})
54 #define OTCRYPTO_ASYNC_INCOMPLETE \
55  ((status_t){.value = kOtcryptoStatusValueAsyncIncomplete})
56 #define OTCRYPTO_NOT_IMPLEMENTED \
57  ((status_t){.value = kOtcryptoStatusValueNotImplemented})
58 
59 #endif
60 
61 /**
62  * Hardened version of the `TRY` macro from `status.h`.
63  *
64  * Returns the error unmodified if `status_ok` fails, and throws a
65  * fatal error if the OK code does not match the hardened value.
66  *
67  * @param expr_ An expression that evaluates to a `status_t`.
68  * @return The enclosed OK value.
69  */
70 #define HARDENED_TRY(expr_) \
71  ({ \
72  status_t status_ = expr_; \
73  if (!status_ok(status_)) { \
74  return status_; \
75  } \
76  if (launder32(OT_UNSIGNED(status_.value)) != kHardenedBoolTrue) { \
77  return OTCRYPTO_FATAL_ERR; \
78  } \
79  HARDENED_CHECK_EQ(status_.value, kHardenedBoolTrue); \
80  status_.value; \
81  })
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_STATUS_H_