2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "rng.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
inline void delay(uint32_t wait) {
|
|
|
|
while (--wait > 0) __asm__("nop");
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void wait_random(void) {
|
|
|
|
int wait = random32() & 0xff;
|
|
|
|
volatile int i = 0;
|
|
|
|
volatile int j = wait;
|
|
|
|
while (i < wait) {
|
|
|
|
if (i + j != wait) {
|
|
|
|
shutdown();
|
2019-02-20 19:34:00 +00:00
|
|
|
}
|
2019-03-29 16:10:31 +00:00
|
|
|
++i;
|
|
|
|
--j;
|
|
|
|
}
|
|
|
|
// Double-check loop completion.
|
|
|
|
if (i != wait || j != 0) {
|
|
|
|
shutdown();
|
|
|
|
}
|
2019-02-20 19:34:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
static const char *hexdigits = "0123456789ABCDEF";
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void uint32hex(uint32_t num, char *str) {
|
|
|
|
for (uint32_t i = 0; i < 8; i++) {
|
|
|
|
str[i] = hexdigits[(num >> (28 - i * 4)) & 0xF];
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// converts data to hexa
|
2019-03-29 16:10:31 +00:00
|
|
|
void data2hex(const void *data, uint32_t len, char *str) {
|
|
|
|
const uint8_t *cdata = (uint8_t *)data;
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
str[i * 2] = hexdigits[(cdata[i] >> 4) & 0xF];
|
|
|
|
str[i * 2 + 1] = hexdigits[cdata[i] & 0xF];
|
|
|
|
}
|
|
|
|
str[len * 2] = 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t readprotobufint(const uint8_t **ptr) {
|
|
|
|
uint32_t result = (**ptr & 0x7F);
|
|
|
|
if (**ptr & 0x80) {
|
|
|
|
(*ptr)++;
|
|
|
|
result += (**ptr & 0x7F) * 128;
|
|
|
|
if (**ptr & 0x80) {
|
|
|
|
(*ptr)++;
|
|
|
|
result += (**ptr & 0x7F) * 128 * 128;
|
|
|
|
if (**ptr & 0x80) {
|
|
|
|
(*ptr)++;
|
|
|
|
result += (**ptr & 0x7F) * 128 * 128 * 128;
|
|
|
|
if (**ptr & 0x80) {
|
|
|
|
(*ptr)++;
|
|
|
|
result += (**ptr & 0x7F) * 128 * 128 * 128 * 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*ptr)++;
|
|
|
|
return result;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|