refactor(core): move wait_random and rdi into separate file

pull/1625/head
Ondřej Vejpustek 3 years ago
parent f771dc6f60
commit 8ee17f69b3

@ -94,6 +94,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/flash.c',
'embed/trezorhal/mini_printf.c',
'embed/trezorhal/mpu.c',
'embed/trezorhal/random_delays.c',
'embed/trezorhal/rng.c',
'embed/trezorhal/stm32.c',
'embed/trezorhal/systick.c',

@ -94,6 +94,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/flash.c',
'embed/trezorhal/mini_printf.c',
'embed/trezorhal/mpu.c',
'embed/trezorhal/random_delays.c',
'embed/trezorhal/rng.c',
'embed/trezorhal/stm32.c',
'embed/trezorhal/systick.c',

@ -333,6 +333,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/flash.c',
'embed/trezorhal/mini_printf.c',
'embed/trezorhal/mpu.c',
'embed/trezorhal/random_delays.c',
'embed/trezorhal/rng.c',
'embed/trezorhal/sbu.c',
'embed/trezorhal/sdcard.c',
@ -349,9 +350,6 @@ SOURCE_TREZORHAL = [
]
if FEATURE_FLAGS["RDI"]:
SOURCE_TREZORHAL += [
'embed/trezorhal/rdi.c',
]
CPPDEFINES_MOD += ['RDI']
if FEATURE_FLAGS["SYSTEM_VIEW"]:

@ -70,6 +70,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/dma.c',
'embed/trezorhal/flash.c',
'embed/trezorhal/mini_printf.c',
'embed/trezorhal/random_delays.c',
'embed/trezorhal/rng.c',
'embed/trezorhal/sbu.c',
'embed/trezorhal/sdcard.c',

@ -305,6 +305,7 @@ SOURCE_UNIX = [
'embed/unix/flash.c',
'embed/unix/main.c',
'embed/unix/profile.c',
'embed/unix/random_delays.c',
'embed/unix/rng.c',
'embed/unix/sbu.c',
'embed/unix/sdcard.c',

@ -41,7 +41,7 @@
#include "flash.h"
#include "mpu.h"
#ifdef RDI
#include "rdi.h"
#include "random_delays.h"
#endif
#ifdef SYSTEM_VIEW
#include "systemview.h"

@ -122,27 +122,6 @@ void __assert_func(const char *file, int line, const char *func,
void hal_delay(uint32_t ms) { HAL_Delay(ms); }
/*
* 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;
while (i < wait) {
if (i + j != wait) {
shutdown();
}
++i;
--j;
}
// Double-check loop completion.
if (i != wait || j != 0) {
shutdown();
}
}
// reference RM0090 section 35.12.1 Figure 413
#define USB_OTG_HS_DATA_FIFO_RAM (USB_OTG_HS_PERIPH_BASE + 0x20000U)
#define USB_OTG_HS_DATA_FIFO_SIZE (4096U)

@ -66,8 +66,6 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
void hal_delay(uint32_t ms);
void wait_random(void);
void clear_otg_hs_memory(void);
extern uint32_t __stack_chk_guard;

@ -34,7 +34,7 @@ https://link.springer.com/content/pdf/10.1007%2F3-540-44499-8_20.pdf
https://link.springer.com/content/pdf/10.1007%2F978-3-540-72354-7_3.pdf
*/
#include "rdi.h"
#include "random_delays.h"
#include <stdbool.h>
@ -42,7 +42,9 @@ https://link.springer.com/content/pdf/10.1007%2F978-3-540-72354-7_3.pdf
#include "common.h"
#include "memzero.h"
#include "rand.h"
#include "secbool.h"
// from util.s
extern void shutdown(void);
#define BUFFER_LENGTH 64
#define RESEED_INTERVAL 65536
@ -141,3 +143,24 @@ void rdi_stop(void) {
memzero(&drbg_ctx, sizeof(drbg_ctx));
}
}
/*
* 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;
while (i < wait) {
if (i + j != wait) {
shutdown();
}
++i;
--j;
}
// Double-check loop completion.
if (i != wait || j != 0) {
shutdown();
}
}

@ -17,8 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TREZORHAL_RDI_H__
#define __TREZORHAL_RDI_H__
#ifndef __TREZORHAL_RANDOM_DELAYS_H__
#define __TREZORHAL_RANDOM_DELAYS_H__
#include <stdint.h>
@ -26,4 +26,6 @@ void rdi_start(void);
void rdi_stop(void);
void rdi_refresh_session_delay(void);
void rdi_handler(uint32_t uw_tick);
void wait_random(void);
#endif

@ -49,7 +49,7 @@
#include "systick.h"
#ifdef RDI
#include "rdi.h"
#include "random_delays.h"
#endif
#include "systemview.h"

@ -21,7 +21,7 @@
#include "usb.h"
#include "common.h"
#include "rdi.h"
#include "random_delays.h"
#include "usbd_core.h"
#define USB_MAX_CONFIG_DESC_SIZE 256

@ -56,7 +56,6 @@ 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)

@ -0,0 +1,22 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "random_delays.h"
void wait_random(void) {}

@ -0,0 +1,24 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TREZORHAL_RANDOM_DELAYS_H__
#define __TREZORHAL_RANDOM_DELAYS_H__
void wait_random(void);
#endif

@ -27,6 +27,7 @@
#include "norcow.h"
#include "pbkdf2.h"
#include "rand.h"
#include "random_delays.h"
#include "sha2.h"
#include "storage.h"

@ -6,6 +6,7 @@ BASE = ../../../
SRC = storage/tests/c/flash.c
SRC += storage/tests/c/common.c
SRC += storage/tests/c/random_delays.c
SRC += storage/storage.c
SRC += storage/norcow.c
SRC += crypto/pbkdf2.c

@ -23,8 +23,6 @@
#include "common.h"
void wait_random(void) {}
void __shutdown(void) {
printf("SHUTDOWN\n");
exit(3);

@ -22,8 +22,6 @@
#include "secbool.h"
void wait_random(void);
void __fatal_error(const char *expr, const char *msg, const char *file,
int line, const char *func);
void error_shutdown(const char *line1, const char *line2, const char *line3,

@ -0,0 +1,22 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "random_delays.h"
void wait_random(void) {}

@ -0,0 +1,25 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TREZORHAL_RANDOM_DELAYS_H__
#define __TREZORHAL_RANDOM_DELAYS_H__
void wait_random(void);
#endif
Loading…
Cancel
Save