./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
5 / 31
16.1%
Branches:
2 / 22
9.1%
Functions:
1 / 3
33.3%

kfsw-modules/radio-uhf/src/radio_crypto_store.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3 #include <zephyr/fs/fs.h>
4 #include <zephyr/sys/byteorder.h>
5 #include <zephyr/sys/crc.h>
6
7 #include <kfsw/services/snapshot.h>
8
9 #include "radio_crypto_internal.h"
10
11 #define SETTINGS_PATH "/kfsw/radio-uhf.key"
12 #define TEMP_PATH SETTINGS_PATH ".tmp"
13 #define SETTINGS_BYTES 44U
14
15 140 void radio_crypto_clear(void *data, size_t size)
16 {
17 140 volatile uint8_t *bytes = data;
18
19
2/2
✓ Branch 0 taken 22760 times.
✓ Branch 1 taken 140 times.
22900 while (size-- != 0U) {
20 22760 *bytes++ = 0;
21 }
22 140 }
23
24 ✗ int radio_crypto_store_load(struct radio_crypto_settings *settings)
25 {
26 struct fs_file_t file;
27 uint8_t data[SETTINGS_BYTES + 1U];
28
29 ✗ fs_file_t_init(&file);
30 ✗ int result = fs_open(&file, SETTINGS_PATH, FS_O_READ);
31
32
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != 0) {
33 ✗ return result;
34 }
35 ✗ ssize_t bytes = fs_read(&file, data, sizeof(data));
36 ✗ int closed = fs_close(&file);
37
38
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ if (bytes != SETTINGS_BYTES || closed != 0 || memcmp(data, "KRU1", 4) != 0 ||
39
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ crc32_ieee(data, 40) != sys_get_be32(data + 40) || (data[4] & ~15U) != 0U) {
40 ✗ result = -EBADMSG;
41 } else {
42 ✗ *settings = (struct radio_crypto_settings){.enabled = (data[4] & 1U) != 0U,
43 ✗ .tx = (data[4] & 2U) != 0U,
44 ✗ .rx = (data[4] & 4U) != 0U,
45 ✗ .key_set = (data[4] & 8U) != 0U};
46 ✗ memcpy(settings->key, data + 8, sizeof(settings->key));
47 }
48 ✗ radio_crypto_clear(data, sizeof(data));
49 ✗ return result;
50 }
51
52 ✗ int radio_crypto_store_save(const struct radio_crypto_settings *settings)
53 {
54 ✗ uint8_t data[SETTINGS_BYTES] = {'K', 'R', 'U', '1'};
55
56
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ data[4] = (settings->enabled ? 1U : 0U) | (settings->tx ? 2U : 0U) |
57
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ (settings->rx ? 4U : 0U) | (settings->key_set ? 8U : 0U);
58 ✗ memcpy(data + 8, settings->key, sizeof(settings->key));
59 ✗ sys_put_be32(crc32_ieee(data, 40), data + 40);
60
61 ✗ int result = kfsw_snapshot_write(SETTINGS_PATH, TEMP_PATH, data, sizeof(data));
62
63 ✗ radio_crypto_clear(data, sizeof(data));
64 ✗ return result;
65 }
66