diff --git a/SConscript.firmware b/SConscript.firmware index 6799926405..67682baa9f 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -278,6 +278,7 @@ SOURCE_TREZORHAL = [ 'embed/trezorhal/usbd_core.c', 'embed/trezorhal/usbd_ctlreq.c', 'embed/trezorhal/usbd_ioreq.c', + 'embed/trezorhal/utils.c', 'embed/trezorhal/util.s', 'embed/trezorhal/vectortable.s', ] diff --git a/SConscript.unix b/SConscript.unix index 9609c7d329..d1f4c70100 100644 --- a/SConscript.unix +++ b/SConscript.unix @@ -257,6 +257,7 @@ SOURCE_UNIX = [ 'embed/unix/sbu.c', 'embed/unix/touch.c', 'embed/unix/usb.c', + 'embed/unix/utils.c', ] SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_UNIX diff --git a/embed/extmod/modtrezorconfig/modtrezorconfig.c b/embed/extmod/modtrezorconfig/modtrezorconfig.c index f492adb85d..4b368ac710 100644 --- a/embed/extmod/modtrezorconfig/modtrezorconfig.c +++ b/embed/extmod/modtrezorconfig/modtrezorconfig.c @@ -26,6 +26,7 @@ #include "embed/extmod/trezorobj.h" #include "storage.h" +#include "utils.h" STATIC mp_obj_t ui_wait_callback = mp_const_none; @@ -41,12 +42,18 @@ STATIC void wrapped_ui_wait_callback(uint32_t wait, uint32_t progress) { /// called from this module! /// ''' STATIC mp_obj_t mod_trezorconfig_init(size_t n_args, const mp_obj_t *args) { - // TODO: Add salt. + uint32_t salt[] = { + utils_get_cpu_id(), + utils_get_flash_size(), + utils_get_uid_word0(), + utils_get_uid_word1(), + utils_get_uid_word2() + }; if (n_args > 0) { ui_wait_callback = args[0]; - storage_init(wrapped_ui_wait_callback, (const uint8_t*)"", 0); + storage_init(wrapped_ui_wait_callback, (const uint8_t*)salt, sizeof(salt)); } else { - storage_init(NULL, (const uint8_t*)"", 0); + storage_init(NULL, (const uint8_t*)salt, sizeof(salt)); } return mp_const_none; } diff --git a/embed/trezorhal/utils.c b/embed/trezorhal/utils.c new file mode 100644 index 0000000000..01761a4b64 --- /dev/null +++ b/embed/trezorhal/utils.c @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +#include "utils.h" +#include STM32_HAL_H +#include "stm32l4xx_ll_utils.h" + +/* + * Returns the CPUID Base Register of the System Control Block. + */ +uint32_t utils_get_cpu_id() +{ + return SCB->CPUID; +} + +/* + * Returns the size of the device flash memory expressed in kilobytes, e.g. 0x040 corresponds to 64 kB. + */ +uint32_t utils_get_flash_size() +{ + return LL_GetFlashSize(); +} + +/* + * Returns word 0 of the unique device identifier. + */ +uint32_t utils_get_uid_word0() +{ + return LL_GetUID_Word0(); +} + +/* + * Returns word 1 of the unique device identifier. + */ +uint32_t utils_get_uid_word1() +{ + return LL_GetUID_Word1(); +} + +/* + * Returns word 2 of the unique device identifier. + */ +uint32_t utils_get_uid_word2() +{ + return LL_GetUID_Word2(); +} diff --git a/embed/trezorhal/utils.h b/embed/trezorhal/utils.h new file mode 100644 index 0000000000..3e3cbcb6a4 --- /dev/null +++ b/embed/trezorhal/utils.h @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +#ifndef __TREZORHAL_UTILS_H__ +#define __TREZORHAL_UTILS_H__ + +#include + +uint32_t utils_get_cpu_id(); +uint32_t utils_get_flash_size(); +uint32_t utils_get_uid_word0(); +uint32_t utils_get_uid_word1(); +uint32_t utils_get_uid_word2(); + +#endif diff --git a/embed/unix/utils.c b/embed/unix/utils.c new file mode 100644 index 0000000000..04867c0ba3 --- /dev/null +++ b/embed/unix/utils.c @@ -0,0 +1,60 @@ +/* + * 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 . + */ + +#include "utils.h" + +/* + * Returns the CPUID Base Register of the System Control Block. + */ +uint32_t utils_get_cpu_id() +{ + return 0; +} + +/* + * Returns the size of the device flash memory expressed in kilobytes, e.g. 0x040 corresponds to 64 kB. + */ +uint32_t utils_get_flash_size() +{ + return 0; +} + +/* + * Returns word 0 of the unique device identifier. + */ +uint32_t utils_get_uid_word0() +{ + return 0; +} + +/* + * Returns word 1 of the unique device identifier. + */ +uint32_t utils_get_uid_word1() +{ + return 0; +} + +/* + * Returns word 2 of the unique device identifier. + */ +uint32_t utils_get_uid_word2() +{ + return 0; +} diff --git a/embed/unix/utils.h b/embed/unix/utils.h new file mode 100644 index 0000000000..3e3cbcb6a4 --- /dev/null +++ b/embed/unix/utils.h @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +#ifndef __TREZORHAL_UTILS_H__ +#define __TREZORHAL_UTILS_H__ + +#include + +uint32_t utils_get_cpu_id(); +uint32_t utils_get_flash_size(); +uint32_t utils_get_uid_word0(); +uint32_t utils_get_uid_word1(); +uint32_t utils_get_uid_word2(); + +#endif