1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

core/common: Change delay_random() to wait_random().

This commit is contained in:
Andrew Kozlik 2019-06-08 13:53:17 +02:00
parent 531c610a17
commit c2bdd1cf32
5 changed files with 16 additions and 9 deletions

View File

@ -24,8 +24,8 @@
#include "common.h"
#include "display.h"
#include "flash.h"
#include "rand.h"
#include "hmac_drbg.h"
#include "rand.h"
#include "stm32f4xx_ll_utils.h"
@ -122,7 +122,11 @@ void __assert_func(const char *file, int line, const char *func,
void hal_delay(uint32_t ms) { HAL_Delay(ms); }
void delay_random(void) {
/*
* Generates a delay of random length. Use this to protect sensitive code
* against fault injection.
*/
void wait_random(void) {
int wait = drbg_random32() & 0xff;
volatile int i = 0;
volatile int j = wait;
@ -188,7 +192,7 @@ void collect_hw_entropy(void) {
NULL);
}
void drbg_init() {
void drbg_init(void) {
uint8_t entropy[48];
random_buffer(entropy, sizeof(entropy));
hmac_drbg_init(&drbg_ctx, entropy, sizeof(entropy), NULL, 0);

View File

@ -66,7 +66,7 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
void hal_delay(uint32_t ms);
void delay_random(void);
void wait_random(void);
void clear_otg_hs_memory(void);
@ -76,7 +76,7 @@ void collect_hw_entropy(void);
#define HW_ENTROPY_LEN (12 + 32)
extern uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
void drbg_init();
void drbg_init(void);
void drbg_reseed(const uint8_t *entropy, size_t len);
void drbg_generate(uint8_t *buf, size_t len);
uint32_t drbg_random32(void);

View File

@ -343,7 +343,7 @@ static uint8_t usb_class_deinit(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
USBD_SetupReqTypedef *req) {
delay_random();
wait_random();
if (((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_CLASS) &&
((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_STANDARD) &&
((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_VENDOR)) {
@ -472,7 +472,7 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
}
static uint8_t usb_class_data_in(USBD_HandleTypeDef *dev, uint8_t ep_num) {
delay_random();
wait_random();
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
switch (usb_ifaces[i].type) {
case USB_IFACE_TYPE_HID:
@ -492,7 +492,7 @@ static uint8_t usb_class_data_in(USBD_HandleTypeDef *dev, uint8_t ep_num) {
}
static uint8_t usb_class_data_out(USBD_HandleTypeDef *dev, uint8_t ep_num) {
delay_random();
wait_random();
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
switch (usb_ifaces[i].type) {
case USB_IFACE_TYPE_HID:
@ -512,7 +512,7 @@ static uint8_t usb_class_data_out(USBD_HandleTypeDef *dev, uint8_t ep_num) {
}
static uint8_t usb_class_sof(USBD_HandleTypeDef *dev) {
delay_random();
wait_random();
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
switch (usb_ifaces[i].type) {
case USB_IFACE_TYPE_VCP:

View File

@ -109,6 +109,8 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
void hal_delay(uint32_t ms) { usleep(1000 * ms); }
void wait_random(void) {}
uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
void collect_hw_entropy(void) { memzero(HW_ENTROPY_DATA, HW_ENTROPY_LEN); }

View File

@ -56,6 +56,7 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
void hal_delay(uint32_t ms);
void wait_random(void);
void collect_hw_entropy(void);
#define HW_ENTROPY_LEN (12 + 32)