mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-14 09:20:55 +00:00
refactor(crypto,core): make zkp_context_init() return status
This commit is contained in:
parent
ec808050ba
commit
ad38d8e324
@ -75,10 +75,6 @@ int main(void) {
|
|||||||
enable_systemview();
|
enable_systemview();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_SECP256K1_ZKP
|
|
||||||
zkp_context_init();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if TREZOR_MODEL == T
|
#if TREZOR_MODEL == T
|
||||||
#if PRODUCTION
|
#if PRODUCTION
|
||||||
check_and_replace_bootloader();
|
check_and_replace_bootloader();
|
||||||
@ -109,6 +105,10 @@ int main(void) {
|
|||||||
display_clear();
|
display_clear();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_SECP256K1_ZKP
|
||||||
|
ensure(sectrue * (zkp_context_init() == 0), NULL);
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("CORE: Preparing stack\n");
|
printf("CORE: Preparing stack\n");
|
||||||
// Stack limit should be less than real stack size, so we have a chance
|
// Stack limit should be less than real stack size, so we have a chance
|
||||||
// to recover from limit hit.
|
// to recover from limit hit.
|
||||||
|
@ -13,7 +13,7 @@ int main(int argc, char **argv) {
|
|||||||
collect_hw_entropy();
|
collect_hw_entropy();
|
||||||
|
|
||||||
#ifdef USE_SECP256K1_ZKP
|
#ifdef USE_SECP256K1_ZKP
|
||||||
zkp_context_init();
|
ensure(sectrue * (zkp_context_init() == 0), NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_PY_THREAD
|
#if MICROPY_PY_THREAD
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <check.h>
|
#include <check.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -9920,7 +9921,7 @@ Suite *test_suite(void) {
|
|||||||
|
|
||||||
// run suite
|
// run suite
|
||||||
int main(void) {
|
int main(void) {
|
||||||
zkp_context_init();
|
assert(zkp_context_init() == 0);
|
||||||
int number_failed;
|
int number_failed;
|
||||||
Suite *s = test_suite();
|
Suite *s = test_suite();
|
||||||
SRunner *sr = srunner_create(s);
|
SRunner *sr = srunner_create(s);
|
||||||
|
@ -80,7 +80,7 @@ random_iters = int(os.environ.get("ITERS", 1))
|
|||||||
DIR = os.path.abspath(os.path.dirname(__file__))
|
DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
lib = c.cdll.LoadLibrary(os.path.join(DIR, "libtrezor-crypto.so"))
|
lib = c.cdll.LoadLibrary(os.path.join(DIR, "libtrezor-crypto.so"))
|
||||||
if not lib.zkp_context_is_initialized():
|
if not lib.zkp_context_is_initialized():
|
||||||
lib.zkp_context_init()
|
assert lib.zkp_context_init() == 0
|
||||||
|
|
||||||
BIGNUM = c.c_uint32 * 9
|
BIGNUM = c.c_uint32 * 9
|
||||||
|
|
||||||
|
@ -600,7 +600,7 @@ def generate_eddsa(filename):
|
|||||||
dir = os.path.abspath(os.path.dirname(__file__))
|
dir = os.path.abspath(os.path.dirname(__file__))
|
||||||
lib = ctypes.cdll.LoadLibrary(os.path.join(dir, "libtrezor-crypto.so"))
|
lib = ctypes.cdll.LoadLibrary(os.path.join(dir, "libtrezor-crypto.so"))
|
||||||
if not lib.zkp_context_is_initialized():
|
if not lib.zkp_context_is_initialized():
|
||||||
lib.zkp_context_init()
|
assert lib.zkp_context_init() == 0
|
||||||
testvectors_directory = os.path.join(dir, "wycheproof/testvectors")
|
testvectors_directory = os.path.join(dir, "wycheproof/testvectors")
|
||||||
context_structure_length = 1024
|
context_structure_length = 1024
|
||||||
|
|
||||||
|
@ -44,7 +44,8 @@ void secp256k1_context_writable_randomize(secp256k1_context *context_writable) {
|
|||||||
|
|
||||||
bool zkp_context_is_initialized(void) { return context != NULL; }
|
bool zkp_context_is_initialized(void) { return context != NULL; }
|
||||||
|
|
||||||
void zkp_context_init() {
|
// returns 0 on success
|
||||||
|
int zkp_context_init() {
|
||||||
assert(context == NULL);
|
assert(context == NULL);
|
||||||
|
|
||||||
const unsigned int context_flags =
|
const unsigned int context_flags =
|
||||||
@ -52,16 +53,23 @@ void zkp_context_init() {
|
|||||||
|
|
||||||
const size_t context_size =
|
const size_t context_size =
|
||||||
secp256k1_context_preallocated_size(context_flags);
|
secp256k1_context_preallocated_size(context_flags);
|
||||||
assert(context_size != 0);
|
// Assert the context is as small as possible
|
||||||
assert(context_size <= SECP256K1_CONTEXT_SIZE);
|
assert(context_size == SECP256K1_CONTEXT_SIZE);
|
||||||
|
if (context_size == 0 || context_size > SECP256K1_CONTEXT_SIZE) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
context =
|
context =
|
||||||
secp256k1_context_preallocated_create(context_buffer, context_flags);
|
secp256k1_context_preallocated_create(context_buffer, context_flags);
|
||||||
assert(context != NULL);
|
if (context == NULL) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
secp256k1_context_writable_randomize(context);
|
secp256k1_context_writable_randomize(context);
|
||||||
|
|
||||||
atomic_flag_clear(&locked);
|
atomic_flag_clear(&locked);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void zkp_context_destroy() {
|
void zkp_context_destroy() {
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
|
#include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
|
||||||
|
|
||||||
void secp256k1_context_writable_randomize(secp256k1_context *context);
|
void secp256k1_context_writable_randomize(secp256k1_context *context);
|
||||||
void zkp_context_init(void);
|
int zkp_context_init(void);
|
||||||
void zkp_context_destroy(void);
|
void zkp_context_destroy(void);
|
||||||
const secp256k1_context *zkp_context_get_read_only(void);
|
const secp256k1_context *zkp_context_get_read_only(void);
|
||||||
secp256k1_context *zkp_context_acquire_writable(void);
|
secp256k1_context *zkp_context_acquire_writable(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user