Skip to content

Commit 9570b0a

Browse files
committed
Merge branch 'simplify-test'
2 parents d178b97 + 87d50a6 commit 9570b0a

File tree

7 files changed

+60
-86
lines changed

7 files changed

+60
-86
lines changed

src/cipher/cipher.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <util.h>
2323
#include <wally_crypto.h>
2424

25+
#ifdef TESTING
26+
#include <mock_cipher.h>
27+
#endif
28+
2529
#define N_BLOCK (16U)
2630
// Used to sanity-check input to avoid large stack allocations
2731
#define CIPHER_MAX_ALLOC (200U)
@@ -65,7 +69,11 @@ static bool _aes_encrypt(
6569
}
6670

6771
uint8_t iv[32] = {0}; // only 16 bytes needed for IV.
72+
#ifdef TESTING
73+
cipher_mock_iv(iv);
74+
#else
6875
random_32_bytes(iv);
76+
#endif
6977
memcpy(out, iv, N_BLOCK);
7078

7179
AES256_CBC_ctx ctx = {0};

test/unit-test/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_library(bitbox_objects
6161
${CTAES-SOURCES}
6262
${ETHEREUM-SOURCES}
6363
framework/mock_blocking.c
64+
framework/mock_cipher.c
6465
framework/mock_screen.c
6566
framework/mock_screen_stack.c
6667
framework/mock_memory.c
@@ -210,7 +211,7 @@ set(TEST_LIST
210211
cleanup
211212
"-Wl,--wrap=util_cleanup_32"
212213
keystore
213-
"-Wl,--wrap=secp256k1_anti_exfil_sign,--wrap=memory_is_initialized,--wrap=memory_is_seeded,--wrap=memory_get_failed_unlock_attempts,--wrap=memory_reset_failed_unlock_attempts,--wrap=memory_increment_failed_unlock_attempts,--wrap=memory_set_encrypted_seed_and_hmac,--wrap=memory_get_encrypted_seed_and_hmac,--wrap=memory_get_salt_root,--wrap=reset_reset,--wrap=salt_hash_data,--wrap=cipher_aes_hmac_encrypt,--wrap=random_32_bytes,--wrap=securechip_kdf"
214+
"-Wl,--wrap=secp256k1_anti_exfil_sign,--wrap=memory_is_initialized,--wrap=memory_is_seeded,--wrap=memory_get_failed_unlock_attempts,--wrap=memory_reset_failed_unlock_attempts,--wrap=memory_increment_failed_unlock_attempts,--wrap=memory_set_encrypted_seed_and_hmac,--wrap=memory_get_encrypted_seed_and_hmac,--wrap=memory_get_salt_root,--wrap=reset_reset,--wrap=random_32_bytes"
214215
keystore_antiklepto
215216
""
216217
keystore_functional
@@ -234,7 +235,7 @@ set(TEST_LIST
234235
salt
235236
"-Wl,--wrap=memory_get_salt_root"
236237
cipher
237-
"-Wl,--wrap=random_32_bytes"
238+
"-Wl,--wrap=cipher_mock_iv"
238239
util
239240
""
240241
workflow_blocking
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _MOCK_CIPHER_H_
16+
#define _MOCK_CIPHER_H_
17+
18+
#include <stdint.h>
19+
20+
void cipher_mock_iv(uint8_t* iv_out);
21+
22+
#endif

test/unit-test/framework/includes/mock_memory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef _MOCK_MOCK_H_
16-
#define _MOCK_MOCK_H_
15+
#ifndef _MOCK_MEMORY_H_
16+
#define _MOCK_MEMORY_H_
1717

1818
#include <stdbool.h>
1919
#include <stdint.h>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string.h>
16+
17+
#include <mock_cipher.h>
18+
19+
void cipher_mock_iv(uint8_t* iv_out)
20+
{
21+
memset(iv_out, 'a', 32);
22+
}

test/unit-test/test_cipher.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include <stdlib.h>
2222
#include <string.h>
2323

24-
void __wrap_random_32_bytes(uint8_t* buf)
24+
void __wrap_cipher_mock_iv(uint8_t* iv_out)
2525
{
26-
memcpy(buf, (const uint8_t*)mock(), 32);
26+
memcpy(iv_out, (const uint8_t*)mock(), 32);
2727
}
2828

2929
typedef struct {
@@ -3839,7 +3839,7 @@ static void _test_cipher_aes_hmac_encrypt(void** state)
38393839
const test_t* test = &_tests[i];
38403840
uint8_t rand_mock[32] = {0};
38413841
memcpy(rand_mock, test->iv, 16);
3842-
will_return(__wrap_random_32_bytes, rand_mock);
3842+
will_return(__wrap_cipher_mock_iv, rand_mock);
38433843
size_t cipher_len = test->msg_len + 64;
38443844
uint8_t cipher[cipher_len];
38453845
assert_true(

test/unit-test/test_keystore.c

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@
3535

3636
#define PASSWORD ("password")
3737

38-
int __real_securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out);
39-
int __wrap_securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
40-
{
41-
check_expected(slot);
42-
return __real_securechip_kdf(slot, msg, len, kdf_out);
43-
}
44-
4538
static uint8_t _salt_root[KEYSTORE_MAX_SEED_LENGTH] = {
4639
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
4740
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
@@ -64,8 +57,6 @@ static uint8_t _mock_bip39_seed[64] = {
6457
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
6558
};
6659

67-
const uint8_t _aes_iv[32] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
68-
6960
static const uint32_t _keypath[] = {
7061
44 + BIP32_INITIAL_HARDENED_CHILD,
7162
0 + BIP32_INITIAL_HARDENED_CHILD,
@@ -112,41 +103,6 @@ int __wrap_secp256k1_anti_exfil_sign(
112103
return __real_secp256k1_anti_exfil_sign(ctx, sig, msg32, seckey, host_data32, recid);
113104
}
114105

115-
bool __real_salt_hash_data(
116-
const uint8_t* data,
117-
size_t data_len,
118-
const char* purpose,
119-
uint8_t* hash_out);
120-
bool __wrap_salt_hash_data(
121-
const uint8_t* data,
122-
size_t data_len,
123-
const char* purpose,
124-
uint8_t* hash_out)
125-
{
126-
check_expected(data);
127-
check_expected(data_len);
128-
check_expected(purpose);
129-
return __real_salt_hash_data(data, data_len, purpose, hash_out);
130-
}
131-
132-
bool __real_cipher_aes_hmac_encrypt(
133-
const unsigned char* in,
134-
int in_len,
135-
uint8_t* out,
136-
int* out_len,
137-
const uint8_t* secret);
138-
139-
bool __wrap_cipher_aes_hmac_encrypt(
140-
const unsigned char* in,
141-
int in_len,
142-
uint8_t* out,
143-
int* out_len,
144-
const uint8_t* secret)
145-
{
146-
check_expected(secret);
147-
return __real_cipher_aes_hmac_encrypt(in, in_len, out, out_len, secret);
148-
}
149-
150106
/** Reset the SmartEEPROM configuration. */
151107
static void _smarteeprom_reset(void)
152108
{
@@ -298,37 +254,9 @@ static void _test_keystore_secp256k1_sign(void** state)
298254
assert_true(_pubkeys_equal(ctx, &recovered_pubkey, &expected_pubkey));
299255
}
300256
}
301-
302-
static void _expect_stretch(const char* password)
303-
{
304-
expect_memory(__wrap_salt_hash_data, data, password, strlen(password));
305-
expect_value(__wrap_salt_hash_data, data_len, strlen(password));
306-
expect_string(__wrap_salt_hash_data, purpose, "keystore_seed_access_in");
307-
308-
// KDF 1
309-
expect_value(__wrap_securechip_kdf, slot, SECURECHIP_SLOT_ROLLKEY);
310-
311-
// KDF 2
312-
expect_value(__wrap_securechip_kdf, slot, SECURECHIP_SLOT_KDF);
313-
314-
// KDF 3
315-
expect_value(__wrap_securechip_kdf, slot, SECURECHIP_SLOT_KDF);
316-
317-
expect_memory(__wrap_salt_hash_data, data, password, strlen(password));
318-
expect_value(__wrap_salt_hash_data, data_len, strlen(password));
319-
expect_string(__wrap_salt_hash_data, purpose, "keystore_seed_access_out");
320-
}
321-
322257
static void _expect_encrypt_and_store_seed(void)
323258
{
324259
will_return(__wrap_memory_is_initialized, false);
325-
326-
_expect_stretch(PASSWORD); // first stretch to encrypt
327-
_expect_stretch(PASSWORD); // second stretch to verify
328-
329-
expect_memory(__wrap_cipher_aes_hmac_encrypt, secret, _expected_secret, 32);
330-
// For the AES IV:
331-
will_return(__wrap_random_32_bytes, _aes_iv);
332260
}
333261

334262
static void _test_keystore_encrypt_and_store_seed(void** state)
@@ -349,15 +277,13 @@ static void _test_keystore_create_and_unlock_twice(void** state)
349277
_smarteeprom_reset();
350278

351279
will_return(__wrap_memory_is_seeded, true);
352-
_expect_stretch(PASSWORD);
353280
assert_int_equal(KEYSTORE_OK, keystore_unlock(PASSWORD, &remaining_attempts, NULL));
354281

355282
// Create new (different) seed.
356283
_expect_encrypt_and_store_seed();
357284
assert_int_equal(keystore_encrypt_and_store_seed(_mock_seed_2, 32, PASSWORD), KEYSTORE_OK);
358285

359286
will_return(__wrap_memory_is_seeded, true);
360-
_expect_stretch(PASSWORD);
361287
assert_int_equal(KEYSTORE_OK, keystore_unlock(PASSWORD, &remaining_attempts, NULL));
362288
}
363289

@@ -375,7 +301,6 @@ static void _perform_some_unlocks(void)
375301
for (int i = 0; i < 3; i++) {
376302
_reset_reset_called = false;
377303
will_return(__wrap_memory_is_seeded, true);
378-
_expect_stretch(PASSWORD);
379304
assert_int_equal(KEYSTORE_OK, keystore_unlock(PASSWORD, &remaining_attempts, NULL));
380305
assert_int_equal(remaining_attempts, MAX_UNLOCK_ATTEMPTS);
381306
assert_false(_reset_reset_called);
@@ -402,7 +327,6 @@ static void _test_keystore_unlock(void** state)
402327
for (int i = 1; i <= MAX_UNLOCK_ATTEMPTS; i++) {
403328
_reset_reset_called = false;
404329
will_return(__wrap_memory_is_seeded, true);
405-
_expect_stretch("invalid password");
406330
assert_int_equal(
407331
i >= MAX_UNLOCK_ATTEMPTS ? KEYSTORE_ERR_MAX_ATTEMPTS_EXCEEDED
408332
: KEYSTORE_ERR_INCORRECT_PASSWORD,
@@ -483,9 +407,6 @@ static void _test_keystore_create_and_store_seed(void** state)
483407
size_t seed_len = test_sizes[i];
484408
// Seed random is xored with host entropy and the salted/hashed user password.
485409
will_return(__wrap_random_32_bytes, seed_random);
486-
expect_memory(__wrap_salt_hash_data, data, PASSWORD, strlen(PASSWORD));
487-
expect_value(__wrap_salt_hash_data, data_len, strlen(PASSWORD));
488-
expect_string(__wrap_salt_hash_data, purpose, "keystore_seed_generation");
489410
_expect_encrypt_and_store_seed();
490411
assert_int_equal(
491412
keystore_create_and_store_seed(PASSWORD, host_entropy, seed_len), KEYSTORE_OK);

0 commit comments

Comments
 (0)