5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_H_
10 #include "sw/device/lib/base/hardened_asm.h"
193 inline uint32_t launder32(uint32_t val) {
244 #if OT_BUILD_FOR_STATIC_ANALYZER || OT_DISABLE_HARDENING
251 asm volatile(
"" :
"+r"(val));
266 #if OT_BUILD_FOR_STATIC_ANALYZER || OT_DISABLE_HARDENING
269 asm volatile(
"" :
"+r"(val));
360 inline void barrier32(uint32_t val) {
asm volatile(
"" ::
"r"(val)); }
369 inline void barrierw(uintptr_t val) {
asm volatile(
"" ::
"r"(val)); }
474 return (launder32(c) & a) | (launder32(~c) & b);
550 #ifdef OT_PLATFORM_RV32
553 #define HARDENED_UNIMP_SEQUENCE_() "unimp; unimp; unimp;"
555 #define HARDENED_CHECK_OP_EQ_ "beq"
556 #define HARDENED_CHECK_OP_NE_ "bne"
557 #define HARDENED_CHECK_OP_LT_ "bltu"
558 #define HARDENED_CHECK_OP_GT_ "bgtu"
559 #define HARDENED_CHECK_OP_LE_ "bleu"
560 #define HARDENED_CHECK_OP_GE_ "bgeu"
564 #define HARDENED_CHECK_INV_EQ_ "bne"
565 #define HARDENED_CHECK_INV_NE_ "beq"
566 #define HARDENED_CHECK_INV_LT_ "bgeu"
567 #define HARDENED_CHECK_INV_GT_ "bleu"
568 #define HARDENED_CHECK_INV_LE_ "bgtu"
569 #define HARDENED_CHECK_INV_GE_ "bltu"
571 #ifndef OT_DISABLE_HARDENING
573 #define HARDENED_CHECK_(op_, a_, b_) \
575 OT_CAT(HARDENED_CHECK_OP_, op_) " %0, %1, .L_HARDENED_OK_%=;" \
576 ".L_HARDENED_BAD_%=:;" \
578 ".L_HARDENED_OK_%=:;" \
579 OT_CAT(HARDENED_CHECK_INV_, op_) " %0, %1, .L_HARDENED_BAD_%=;" \
583 #define HARDENED_TRAP_() \
585 asm volatile(HARDENED_UNIMP_SEQUENCE_()); \
588 #else // OT_DISABLE_HARDENING
591 #define HARDENED_CHECK_(op_, a_, b_) \
596 #define HARDENED_TRAP_() \
599 #endif // OT_DISABLE_HARDENING
600 #else // OT_PLATFORM_RV32
603 #define HARDENED_CHECK_OP_EQ_ ==
604 #define HARDENED_CHECK_OP_NE_ !=
605 #define HARDENED_CHECK_OP_LT_ <
606 #define HARDENED_CHECK_OP_GT_ >
607 #define HARDENED_CHECK_OP_LE_ <=
608 #define HARDENED_CHECK_OP_GE_ >=
610 #define HARDENED_CHECK_(op_, a_, b_) \
611 assert((uint64_t)(a_)OT_CAT(HARDENED_CHECK_OP_, op_)(uint64_t)(b_))
613 #define HARDENED_TRAP_() __builtin_trap()
614 #endif // OT_PLATFORM_RV32
620 #define HARDENED_TRAP() HARDENED_TRAP_()
646 #define HARDENED_CHECK_EQ(a_, b_) HARDENED_CHECK_(EQ_, a_, b_)
647 #define HARDENED_CHECK_NE(a_, b_) HARDENED_CHECK_(NE_, a_, b_)
648 #define HARDENED_CHECK_LT(a_, b_) HARDENED_CHECK_(LT_, a_, b_)
649 #define HARDENED_CHECK_GT(a_, b_) HARDENED_CHECK_(GT_, a_, b_)
650 #define HARDENED_CHECK_LE(a_, b_) HARDENED_CHECK_(LE_, a_, b_)
651 #define HARDENED_CHECK_GE(a_, b_) HARDENED_CHECK_(GE_, a_, b_)
655 #endif // __cplusplus
657 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_HARDENED_H_