Skip to content

Commit 87d50a6

Browse files
committed
unit-test/keystore: simplify mocking of random AES IV
`random_32_bytes()` is used for multiple purposes, e.g. creating a seed, creating random IVs, etc. In combination with cmocka, this makes it difficult to write and maintain unit tests. This commit allows to mock the IV specifically without affecting other uses of random_32_bytes().
1 parent aad37ef commit 87d50a6

File tree

7 files changed

+59
-11
lines changed

7 files changed

+59
-11
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: 2 additions & 1 deletion
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
@@ -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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ static uint8_t _mock_bip39_seed[64] = {
5757
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
5858
};
5959

60-
const uint8_t _aes_iv[32] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
61-
6260
static const uint32_t _keypath[] = {
6361
44 + BIP32_INITIAL_HARDENED_CHILD,
6462
0 + BIP32_INITIAL_HARDENED_CHILD,
@@ -259,9 +257,6 @@ static void _test_keystore_secp256k1_sign(void** state)
259257
static void _expect_encrypt_and_store_seed(void)
260258
{
261259
will_return(__wrap_memory_is_initialized, false);
262-
263-
// For the AES IV:
264-
will_return(__wrap_random_32_bytes, _aes_iv);
265260
}
266261

267262
static void _test_keystore_encrypt_and_store_seed(void** state)

0 commit comments

Comments
 (0)