mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 07:28:10 +00:00
legacy: Move wait_random() to common.c and use HMAC DRBG to generate delays.
This commit is contained in:
parent
013929de0b
commit
ad5d9168c8
@ -21,6 +21,7 @@
|
||||
#include <stdio.h>
|
||||
#include "bitmaps.h"
|
||||
#include "firmware/usb.h"
|
||||
#include "hmac_drbg.h"
|
||||
#include "layout.h"
|
||||
#include "oled.h"
|
||||
#include "rng.h"
|
||||
@ -28,6 +29,8 @@
|
||||
|
||||
uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
|
||||
|
||||
static HMAC_DRBG_CTX drbg_ctx;
|
||||
|
||||
void __attribute__((noreturn))
|
||||
__fatal_error(const char *expr, const char *msg, const char *file, int line_num,
|
||||
const char *func) {
|
||||
@ -81,3 +84,40 @@ void __assert_func(const char *file, int line, const char *func,
|
||||
#endif
|
||||
|
||||
void hal_delay(uint32_t ms) { usbSleep(ms); }
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void drbg_init() {
|
||||
uint8_t entropy[48];
|
||||
random_buffer(entropy, sizeof(entropy));
|
||||
hmac_drbg_init(&drbg_ctx, entropy, sizeof(entropy), NULL, 0);
|
||||
}
|
||||
|
||||
void drbg_reseed(const uint8_t *entropy, size_t len) {
|
||||
hmac_drbg_reseed(&drbg_ctx, entropy, len, NULL, 0);
|
||||
}
|
||||
|
||||
void drbg_generate(uint8_t *buf, size_t len) {
|
||||
hmac_drbg_generate(&drbg_ctx, buf, len);
|
||||
}
|
||||
|
||||
uint32_t drbg_random32(void) {
|
||||
uint32_t value;
|
||||
drbg_generate((uint8_t *)&value, sizeof(value));
|
||||
return value;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#ifndef __TREZORHAL_COMMON_H__
|
||||
#define __TREZORHAL_COMMON_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "secbool.h"
|
||||
|
||||
@ -40,4 +41,11 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
|
||||
|
||||
void hal_delay(uint32_t ms);
|
||||
|
||||
void wait_random(void);
|
||||
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
@ -8,6 +8,8 @@ OBJS += ../vendor/trezor-crypto/bignum.o
|
||||
OBJS += ../vendor/trezor-crypto/bip32.o
|
||||
OBJS += ../vendor/trezor-crypto/ecdsa.o
|
||||
OBJS += ../vendor/trezor-crypto/hmac.o
|
||||
OBJS += ../vendor/trezor-crypto/hmac_drbg.o
|
||||
OBJS += ../vendor/trezor-crypto/rfc6979.o
|
||||
OBJS += ../vendor/trezor-crypto/ripemd160.o
|
||||
OBJS += ../vendor/trezor-crypto/secp256k1.o
|
||||
OBJS += ../vendor/trezor-crypto/sha2.o
|
||||
|
@ -47,6 +47,8 @@ OBJS += ../vendor/trezor-crypto/ecdsa.o
|
||||
OBJS += ../vendor/trezor-crypto/curves.o
|
||||
OBJS += ../vendor/trezor-crypto/secp256k1.o
|
||||
OBJS += ../vendor/trezor-crypto/nist256p1.o
|
||||
OBJS += ../vendor/trezor-crypto/hmac_drbg.o
|
||||
OBJS += ../vendor/trezor-crypto/rfc6979.o
|
||||
OBJS += ../vendor/trezor-crypto/rand.o
|
||||
OBJS += ../vendor/trezor-crypto/memzero.o
|
||||
|
||||
|
@ -128,6 +128,9 @@ int main(void) {
|
||||
__stack_chk_guard = random32(); // this supports compiler provided
|
||||
// unpredictable stack protection checks
|
||||
#endif
|
||||
|
||||
drbg_init();
|
||||
|
||||
if (!is_mode_unprivileged()) {
|
||||
collect_hw_entropy(true);
|
||||
timer_init();
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <libopencm3/usb/hid.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "config.h"
|
||||
#include "debug.h"
|
||||
#include "messages.h"
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "usb21_standard.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "common.h"
|
||||
#include "util.h"
|
||||
|
||||
static uint16_t build_bos_descriptor(const struct usb_bos_descriptor *bos,
|
||||
|
@ -38,6 +38,7 @@ LGPL License Terms @ref lgpl_license
|
||||
|
||||
#include <string.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include "common.h"
|
||||
#include "usb_private.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -18,29 +18,11 @@
|
||||
*/
|
||||
|
||||
#include "util.h"
|
||||
#include "rng.h"
|
||||
|
||||
inline void delay(uint32_t wait) {
|
||||
while (--wait > 0) __asm__("nop");
|
||||
}
|
||||
|
||||
void wait_random(void) {
|
||||
int wait = 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();
|
||||
}
|
||||
}
|
||||
|
||||
static const char *hexdigits = "0123456789ABCDEF";
|
||||
|
||||
void uint32hex(uint32_t num, char *str) {
|
||||
|
@ -52,8 +52,6 @@
|
||||
|
||||
void delay(uint32_t wait);
|
||||
|
||||
void wait_random(void);
|
||||
|
||||
// converts uint32 to hexa (8 digits)
|
||||
void uint32hex(uint32_t num, char *str);
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "usb21_standard.h"
|
||||
#include "util.h"
|
||||
#include "webusb.h"
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
#include "winusb.h"
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include "common.h"
|
||||
#include "util.h"
|
||||
|
||||
static int usb_descriptor_type(uint16_t wValue) { return wValue >> 8; }
|
||||
|
Loading…
Reference in New Issue
Block a user