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/aes.h"
9
10 #include <zephyr/bluetooth/crypto.h>
11 #include <zephyr/kernel.h>
12
13 #include <host/crypto.h>
14
15 ZTEST_SUITE(bt_encrypt_be_invalid_cases, NULL, NULL, NULL, NULL, NULL);
16
17 /*
18 * Test passing NULL reference for the key argument
19 *
20 * Constraints:
21 * - NULL reference is used for the key argument
22 * - Valid references are used for the other arguments
23 *
24 * Expected behaviour:
25 * - An assertion is raised and execution stops
26 */
ZTEST(bt_encrypt_be_invalid_cases,test_null_key_reference)27 ZTEST(bt_encrypt_be_invalid_cases, test_null_key_reference)
28 {
29 const uint8_t plaintext[16] = {0};
30 uint8_t enc_data[16] = {0};
31
32 expect_assert();
33 bt_encrypt_le(NULL, plaintext, enc_data);
34 }
35
36 /*
37 * Test passing NULL reference for the plain text argument
38 *
39 * Constraints:
40 * - NULL reference is used for the plain text argument
41 * - Valid references are used for the other arguments
42 *
43 * Expected behaviour:
44 * - An assertion is raised and execution stops
45 */
ZTEST(bt_encrypt_be_invalid_cases,test_null_plaintext_reference)46 ZTEST(bt_encrypt_be_invalid_cases, test_null_plaintext_reference)
47 {
48 const uint8_t key[16] = {0};
49 uint8_t enc_data[16] = {0};
50
51 expect_assert();
52 bt_encrypt_le(key, NULL, enc_data);
53 }
54
55 /*
56 * Test passing NULL reference for the encrypted data destination buffer argument
57 *
58 * Constraints:
59 * - NULL reference is used for the encrypted data destination buffer argument
60 * - Valid references are used for the other arguments
61 *
62 * Expected behaviour:
63 * - An assertion is raised and execution stops
64 */
ZTEST(bt_encrypt_be_invalid_cases,test_null_enc_data_reference)65 ZTEST(bt_encrypt_be_invalid_cases, test_null_enc_data_reference)
66 {
67 const uint8_t key[16] = {0};
68 const uint8_t plaintext[16] = {0};
69
70 expect_assert();
71 bt_encrypt_le(key, plaintext, NULL);
72 }
73
74 /*
75 * Test bt_encrypt_le() fails when tc_aes128_set_encrypt_key() fails
76 *
77 * Constraints:
78 * - psa_import_key() fails and returns 'PSA_ERROR_GENERIC_ERROR'.
79 *
80 * Expected behaviour:
81 * - bt_encrypt_le() returns a negative error code '-EINVAL' (failure)
82 */
ZTEST(bt_encrypt_be_invalid_cases,test_psa_import_key_fails)83 ZTEST(bt_encrypt_be_invalid_cases, test_psa_import_key_fails)
84 {
85 int err;
86 const uint8_t key[16] = {0};
87 const uint8_t plaintext[16] = {0};
88 uint8_t enc_data[16] = {0};
89
90 psa_import_key_fake.return_val = PSA_ERROR_GENERIC_ERROR;
91
92 err = bt_encrypt_le(key, plaintext, enc_data);
93
94 zassert_true(err == -EINVAL, "Unexpected error code '%d' was returned", err);
95 }
96
97 /*
98 * Test bt_encrypt_le() fails when tc_aes_encrypt() fails
99 *
100 * Constraints:
101 * - psa_import_key() succeeds and returns 'PSA_SUCCESS'.
102 * - psa_cipher_encrypt() fails and returns 'PSA_ERROR_GENERIC_ERROR'.
103 *
104 * Expected behaviour:
105 * - bt_encrypt_le() returns a negative error code '-EINVAL' (failure)
106 */
ZTEST(bt_encrypt_be_invalid_cases,test_psa_cipher_encrypt_fails)107 ZTEST(bt_encrypt_be_invalid_cases, test_psa_cipher_encrypt_fails)
108 {
109 int err;
110 const uint8_t key[16] = {0};
111 const uint8_t plaintext[16] = {0};
112 uint8_t enc_data[16] = {0};
113
114 psa_import_key_fake.return_val = PSA_SUCCESS;
115 psa_cipher_encrypt_fake.return_val = -EINVAL;
116
117 err = bt_encrypt_le(key, plaintext, enc_data);
118
119 zassert_true(err == -EIO, "Unexpected error code '%d' was returned", err);
120 }
121