1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 07:18:09 +00:00

refactor(crypto): use goto cleanup

[no changelog]
This commit is contained in:
Ondřej Vejpustek 2024-08-12 16:18:46 +02:00
parent 3994c654e9
commit a5fd5e179f

View File

@ -432,42 +432,39 @@ int zkp_ecdh_multiply(const ecdsa_curve *curve,
} }
int result = 0; int result = 0;
secp256k1_context *context_writable = NULL;
const secp256k1_context *context_read_only = zkp_context_get_read_only();
secp256k1_pubkey public_key = {0};
size_t public_key_length = get_public_key_length(public_key_bytes); size_t public_key_length = get_public_key_length(public_key_bytes);
if (public_key_length == 0) { if (public_key_length == 0) {
result = 1; result = 1;
goto end;
} }
const secp256k1_context *context_read_only = zkp_context_get_read_only(); if (secp256k1_ec_pubkey_parse(context_read_only, &public_key,
secp256k1_pubkey public_key = {0}; public_key_bytes, public_key_length) != 1) {
result = 1;
if (result == 0) { goto end;
if (secp256k1_ec_pubkey_parse(context_read_only, &public_key,
public_key_bytes, public_key_length) != 1) {
result = 1;
}
} }
secp256k1_context *context_writable = NULL; context_writable = zkp_context_acquire_writable();
if (result == 0) { if (context_writable == NULL) {
context_writable = zkp_context_acquire_writable(); result = 3;
if (context_writable == NULL) { goto end;
result = 1;
}
} }
if (result == 0) { if (secp256k1_context_writable_randomize(context_writable) != 0) {
if (secp256k1_context_writable_randomize(context_writable) != 0) { result = 3;
result = 1; goto end;
}
} }
if (result == 0) { if (secp256k1_ecdh(context_writable, session_key, &public_key,
if (secp256k1_ecdh(context_writable, session_key, &public_key, private_key_bytes, plain_hash_function, NULL) != 1) {
private_key_bytes, plain_hash_function, NULL) != 1) { result = 2;
result = 2; goto end;
}
} }
end:
if (context_writable) { if (context_writable) {
zkp_context_release_writable(); zkp_context_release_writable();
context_writable = NULL; context_writable = NULL;