Skip to content

Commit 0e535e6

Browse files
committed
keystore: port _test_keystore_secp256k1_nonce_commit to Rust
1 parent 2a6c2f8 commit 0e535e6

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ mod tests {
401401
.is_ok());
402402
}
403403

404+
#[test]
405+
fn test_secp256k1_nonce_commit() {
406+
lock();
407+
let keypath = [44 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 5];
408+
let msg = [0x88u8; 32];
409+
let host_commitment = [0xabu8; 32];
410+
411+
// Fails because keystore is locked.
412+
assert!(secp256k1_nonce_commit(&keypath, &msg, &host_commitment).is_err());
413+
414+
mock_unlocked();
415+
let client_commitment = secp256k1_nonce_commit(&keypath, &msg, &host_commitment).unwrap();
416+
assert_eq!(
417+
hex::encode(client_commitment),
418+
"0381e4136251c87f2947b735159c6dd644a7b58d35b437e20c878e5129f1320e5e",
419+
);
420+
}
421+
404422
#[test]
405423
fn test_bip39_mnemonic_to_seed() {
406424
assert!(bip39_mnemonic_to_seed("invalid").is_err());

test/unit-test/test_keystore.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ static uint8_t _mock_bip39_seed[64] = {
5252
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
5353
};
5454

55-
static const uint32_t _keypath[] = {
56-
44 + BIP32_INITIAL_HARDENED_CHILD,
57-
0 + BIP32_INITIAL_HARDENED_CHILD,
58-
0 + BIP32_INITIAL_HARDENED_CHILD,
59-
0,
60-
5,
61-
};
62-
6355
// Same as Python:
6456
// import hmac, hashlib; hmac.digest(b"unit-test", b"password", hashlib.sha256).hex()
6557
// See also: securechip_mock.c
@@ -112,39 +104,6 @@ void _mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t* bip39_s
112104
keystore_mock_unlocked(seed, seed_len, bip39_seed);
113105
}
114106

115-
static void _test_keystore_secp256k1_nonce_commit(void** state)
116-
{
117-
uint8_t msg[32] = {0};
118-
memset(msg, 0x88, sizeof(msg));
119-
uint8_t client_commitment[EC_PUBLIC_KEY_LEN] = {0};
120-
uint8_t host_commitment[32] = {0};
121-
memset(host_commitment, 0xAB, sizeof(host_commitment));
122-
123-
{
124-
_mock_unlocked(NULL, 0, NULL);
125-
// fails because keystore is locked
126-
assert_false(keystore_secp256k1_nonce_commit(
127-
_keypath,
128-
sizeof(_keypath) / sizeof(uint32_t),
129-
msg,
130-
host_commitment,
131-
client_commitment));
132-
}
133-
{
134-
_mock_unlocked(_mock_seed, sizeof(_mock_seed), _mock_bip39_seed);
135-
assert_true(keystore_secp256k1_nonce_commit(
136-
_keypath,
137-
sizeof(_keypath) / sizeof(uint32_t),
138-
msg,
139-
host_commitment,
140-
client_commitment));
141-
const uint8_t expected_commitment[EC_PUBLIC_KEY_LEN] =
142-
"\x02\xfd\xcf\x79\xf9\xc0\x3f\x6a\xcc\xc6\x56\x95\xa1\x90\x82\xe3\x0b\xfb\x9e\xdc\x93"
143-
"\x04\x5a\x03\x05\x8a\x99\x09\xe4\x9b\x1a\x37\x7b";
144-
assert_memory_equal(expected_commitment, client_commitment, sizeof(client_commitment));
145-
}
146-
}
147-
148107
static void _expect_encrypt_and_store_seed(void)
149108
{
150109
will_return(__wrap_memory_is_initialized, false);
@@ -323,7 +282,6 @@ int main(void)
323282
mock_memory_set_salt_root(_salt_root);
324283

325284
const struct CMUnitTest tests[] = {
326-
cmocka_unit_test(_test_keystore_secp256k1_nonce_commit),
327285
cmocka_unit_test(_test_keystore_lock),
328286
cmocka_unit_test(_test_keystore_create_and_store_seed),
329287
cmocka_unit_test(_test_secp256k1_schnorr_sign),

0 commit comments

Comments
 (0)