1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "host_mocks/assert.h"
8 #include "mocks/hci_core.h"
9 #include "mocks/hci_core_expects.h"
10 #include "mocks/prng.h"
11 #include "mocks/prng_expects.h"
12 
13 #include <zephyr/bluetooth/crypto.h>
14 #include <zephyr/kernel.h>
15 
16 #include <host/crypto.h>
17 
18 ZTEST_SUITE(bt_rand_invalid_cases, NULL, NULL, NULL, NULL, NULL);
19 
20 /*
21  *  Test passing NULL reference destination buffer argument
22  *
23  *  Constraints:
24  *   - NULL reference is used as an argument for the destination buffer
25  *
26  *  Expected behaviour:
27  *   - An assertion is raised and execution stops
28  */
ZTEST(bt_rand_invalid_cases,test_null_dst_buf_reference)29 ZTEST(bt_rand_invalid_cases, test_null_dst_buf_reference)
30 {
31 	expect_assert();
32 	bt_rand(NULL, 1);
33 }
34 
35 /*
36  *  Test passing a valid destination buffer reference with size 0
37  *
38  *  Constraints:
39  *   - A valid reference is used as an argument for the destination buffer
40  *   - Destination buffer size is passed as 0
41  *
42  *  Expected behaviour:
43  *   - An assertion is raised and execution stops
44  */
ZTEST(bt_rand_invalid_cases,test_zero_dst_buf_size_reference)45 ZTEST(bt_rand_invalid_cases, test_zero_dst_buf_size_reference)
46 {
47 	uint8_t buf[16];
48 
49 	expect_assert();
50 	bt_rand(buf, 0);
51 }
52 
53 /*
54  *  Test bt_rand() fails when bt_hci_le_rand() fails while 'CONFIG_BT_HOST_CRYPTO_PRNG'
55  *  isn't enabled.
56  *
57  *  Constraints:
58  *   - 'CONFIG_BT_HOST_CRYPTO_PRNG' isn't enabled
59  *   - bt_hci_le_rand() fails and returns a negative error code.
60  *
61  *  Expected behaviour:
62  *   - bt_rand() returns a negative error code (failure)
63  */
ZTEST(bt_rand_invalid_cases,test_bt_hci_le_rand_fails)64 ZTEST(bt_rand_invalid_cases, test_bt_hci_le_rand_fails)
65 {
66 	int err;
67 	uint8_t buf[16];
68 	size_t buf_len = 16;
69 	uint8_t expected_args_history[] = {16};
70 
71 	Z_TEST_SKIP_IFDEF(CONFIG_BT_HOST_CRYPTO_PRNG);
72 
73 	bt_hci_le_rand_fake.return_val = -1;
74 
75 	err = bt_rand(buf, buf_len);
76 
77 	expect_call_count_bt_hci_le_rand(1, expected_args_history);
78 
79 	zassert_true(err < 0, "Unexpected error code '%d' was returned", err);
80 }
81 
82 /*
83  *  Test bt_rand() fails when psa_generate_random() fails on the first call while
84  * 'CONFIG_BT_HOST_CRYPTO_PRNG' is enabled.
85  *
86  *  Constraints:
87  *   - 'CONFIG_BT_HOST_CRYPTO_PRNG' is enabled
88  *   - psa_generate_random() fails and returns '-EIO' on the first call.
89  *
90  *  Expected behaviour:
91  *   - bt_rand() returns a negative error code '-EIO' (failure)
92  */
ZTEST(bt_rand_invalid_cases,test_tc_hmac_prng_generate_fails_on_first_call)93 ZTEST(bt_rand_invalid_cases, test_tc_hmac_prng_generate_fails_on_first_call)
94 {
95 	int err;
96 	uint8_t buf[16];
97 	size_t buf_len = 16;
98 
99 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_HOST_CRYPTO_PRNG);
100 
101 	psa_generate_random_fake.return_val = -EIO;
102 
103 	err = bt_rand(buf, buf_len);
104 
105 	expect_single_call_psa_generate_random(buf, buf_len);
106 
107 	zassert_true(err == -EIO, "Unexpected error code '%d' was returned", err);
108 }
109