./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
295 / 319
92.5%
Branches:
134 / 188
71.3%
Functions:
23 / 23
100.0%

kfsw-modules/radio-uhf/src/radio_crypto.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/kernel.h>
5 #include <zephyr/sys/byteorder.h>
6 #include <zephyr/sys/util.h>
7 #include <psa/crypto.h>
8 #include <kfsw/comms/csp.h>
9 #include <kfsw/comms/uart_codec.h>
10 #include <kfsw/modules/radio_uhf.h>
11 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_RADIO
12 #include <kfsw/services/log.h>
13
14 #include "radio_crypto_internal.h"
15
16 #define LOCAL CONFIG_KFSW_CSP_ADDRESS
17 #define PEER CONFIG_KFSW_CSP_UART_PEER_ADDRESS
18 #define NONCE_BYTES 16U
19 /* A 256-bit key written as hexadecimal, two characters to the byte. */
20 #define KEY_TEXT_LENGTH 64U
21 #define MAC_BYTES 32U
22 #define TAG_BYTES 16U
23 #define DATA_HEADER 12U
24 #define HELLO_BYTES 68U
25 #define REPLY_BYTES 84U
26 #define AAD_BYTES 8U
27 #define RETRY_MS 1000
28 #define HMAC_ALG PSA_ALG_HMAC(PSA_ALG_SHA_256)
29
30 /* The budget services size against; this keeps the two equal. */
31 BUILD_ASSERT(DATA_HEADER + TAG_BYTES + sizeof(uint32_t) == KFSW_CSP_SECURE_OVERHEAD,
32 "the framing here is what KFSW_CSP_PAYLOAD_MAX subtracts");
33
34 enum frame_type { DATA, HELLO, REPLY };
35
36 #if CONFIG_BOARD_NATIVE_SIM
37 int kfsw_radio_host_random(void *data, size_t size);
38 96 static int radio_random(uint8_t *data, size_t size)
39 {
40 96 return kfsw_radio_host_random(data, size);
41 }
42 #else
43 BUILD_ASSERT(IS_ENABLED(CONFIG_CSPRNG_ENABLED) && !IS_ENABLED(CONFIG_TEST_CSPRNG_GENERATOR),
44 "Radio sessions require a secure entropy source");
45 static int radio_random(uint8_t *data, size_t size)
46 {
47 return psa_generate_random(data, size) == PSA_SUCCESS ? 0 : -EIO;
48 }
49 #endif
50
51 static K_MUTEX_DEFINE(crypto_lock);
52 /* Serialize settings writers without making packets wait for flash I/O. */
53 static K_MUTEX_DEFINE(settings_lock);
54 static struct radio_crypto_settings settings = {.enabled = true, .tx = true, .rx = true};
55 static struct kfsw_radio_crypto_info status;
56 static psa_key_id_t master, tx_key, rx_key;
57 static uint8_t boot_nonce[NONCE_BYTES], request_nonce[NONCE_BYTES];
58 static uint8_t peer_boot[NONCE_BYTES], peer_request[NONCE_BYTES], server_nonce[NONCE_BYTES];
59 static uint8_t scratch[CSP_BUFFER_SIZE];
60 static uint64_t tx_sequence, rx_sequence;
61 static int64_t last_request = -RETRY_MS, last_hello = -RETRY_MS;
62 static bool initialized, pending;
63
64 60 static void clear_sessions(void)
65 {
66 60 psa_destroy_key(tx_key);
67 60 psa_destroy_key(rx_key);
68 60 tx_key = rx_key = 0;
69 60 tx_sequence = rx_sequence = 0;
70 60 pending = false;
71 60 last_request = last_hello = -RETRY_MS;
72 60 }
73
74 40 static int import_master(void)
75 {
76 40 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
77 40 psa_key_id_t key = 0;
78
79 40 clear_sessions();
80 40 psa_destroy_key(master);
81 40 master = 0;
82 40 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
83 40 psa_set_key_bits(&attributes, 256);
84 40 psa_set_key_usage_flags(&attributes,
85 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE);
86 40 psa_set_key_algorithm(&attributes, HMAC_ALG);
87 40 psa_status_t result = psa_import_key(&attributes, settings.key, sizeof(settings.key), &key);
88
89 40 psa_reset_key_attributes(&attributes);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (result != PSA_SUCCESS) {
91 ✗ return -EIO;
92 }
93 40 master = key;
94 40 return 0;
95 }
96
97 108 static void put_header(uint8_t *out, const csp_id_t *id)
98 {
99 108 sys_put_be16(id->src, out);
100 108 sys_put_be16(id->dst, out + 2);
101 108 out[4] = id->sport;
102 108 out[5] = id->dport;
103 108 out[6] = id->pri;
104 108 out[7] = id->flags;
105 108 }
106
107 68 static int sign_control(uint8_t *data, size_t size, bool verify)
108 {
109 uint8_t input[AAD_BYTES + REPLY_BYTES - MAC_BYTES];
110
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 36 times.
68 csp_id_t id = {.src = verify ? PEER : LOCAL, .dst = verify ? LOCAL : PEER};
111 68 size_t written = 0;
112 psa_status_t result;
113
114 68 put_header(input, &id);
115 68 memcpy(input + AAD_BYTES, data, size - MAC_BYTES);
116
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 36 times.
68 if (verify) {
117 32 result = psa_mac_verify(master, HMAC_ALG, input, AAD_BYTES + size - MAC_BYTES,
118 32 data + size - MAC_BYTES, MAC_BYTES);
119 } else {
120 36 result = psa_mac_compute(master, HMAC_ALG, input, AAD_BYTES + size - MAC_BYTES,
121 36 data + size - MAC_BYTES, MAC_BYTES, &written);
122 }
123
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
68 return result == PSA_SUCCESS ? 0 : -EACCES;
124 }
125
126 /* The master key is uniformly random key material, not a human password.
127 * Bind each directional key to both endpoints and fresh handshake challenges.
128 */
129 20 static int derive_key(const uint8_t *client_boot, const uint8_t *request, const uint8_t *server,
130 bool transmit)
131 {
132 static const uint8_t label[] = "KFSW UHF session v1";
133 uint8_t context[sizeof(label) + 4U + 3U * NONCE_BYTES];
134 uint8_t material[32];
135 size_t written;
136 20 psa_key_id_t key = 0;
137 20 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
138
139 20 memcpy(context, label, sizeof(label));
140
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 sys_put_be16(transmit ? LOCAL : PEER, context + sizeof(label));
141
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 sys_put_be16(transmit ? PEER : LOCAL, context + sizeof(label) + 2U);
142 20 memcpy(context + sizeof(label) + 4U, client_boot, NONCE_BYTES);
143 20 memcpy(context + sizeof(label) + 4U + NONCE_BYTES, request, NONCE_BYTES);
144 20 memcpy(context + sizeof(label) + 4U + 2U * NONCE_BYTES, server, NONCE_BYTES);
145 20 psa_status_t result = psa_mac_compute(master, HMAC_ALG, context, sizeof(context), material,
146 sizeof(material), &written);
147
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (result == PSA_SUCCESS) {
148 20 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
149 20 psa_set_key_bits(&attributes, 256);
150 20 psa_set_key_algorithm(&attributes, PSA_ALG_GCM);
151
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 psa_set_key_usage_flags(&attributes,
152 transmit ? PSA_KEY_USAGE_ENCRYPT : PSA_KEY_USAGE_DECRYPT);
153 20 result = psa_import_key(&attributes, material, sizeof(material), &key);
154 }
155 20 psa_reset_key_attributes(&attributes);
156 20 radio_crypto_clear(material, sizeof(material));
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (result != PSA_SUCCESS) {
158 ✗ return -EIO;
159 }
160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (transmit) {
161 4 psa_destroy_key(tx_key);
162 4 tx_key = key;
163 4 tx_sequence = 0;
164 } else {
165 16 psa_destroy_key(rx_key);
166 16 rx_key = key;
167 16 rx_sequence = 0;
168 }
169 20 return 0;
170 }
171
172 44 static void frame_header(uint8_t *data, enum frame_type type)
173 {
174 44 data[0] = 'K';
175 44 data[1] = 'R';
176 44 data[2] = 1;
177 44 data[3] = (uint8_t)type;
178 44 }
179
180 20 static int connect_locked(void)
181 {
182 uint8_t hello[HELLO_BYTES];
183 20 int64_t now = k_uptime_get();
184
185
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if (master == 0 || !settings.enabled) {
186 ✗ return -EACCES;
187 }
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (now - last_request < RETRY_MS) {
189 ✗ return -EAGAIN;
190 }
191 20 last_request = now;
192
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
20 if (radio_random(request_nonce, sizeof(request_nonce)) != 0) {
193 4 return -EIO;
194 }
195 16 frame_header(hello, HELLO);
196 16 memcpy(hello + 4, boot_nonce, NONCE_BYTES);
197 16 memcpy(hello + 4 + NONCE_BYTES, request_nonce, NONCE_BYTES);
198 16 int result = sign_control(hello, sizeof(hello), false);
199
200
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (result == 0) {
201 16 pending = true;
202 16 result = kfsw_uart_codec_control(PEER, hello, sizeof(hello));
203 }
204 16 return result;
205 }
206
207 4 int kfsw_radio_uhf_crypto_connect(void)
208 {
209 4 k_mutex_lock(&crypto_lock, K_FOREVER);
210 4 int result = connect_locked();
211 4 status.last_error = result;
212 4 k_mutex_unlock(&crypto_lock);
213 4 return result;
214 }
215
216 20 static int hello_received(csp_packet_t *packet)
217 {
218 uint8_t reply[REPLY_BYTES];
219 20 uint8_t *client = packet->data + 4;
220 20 uint8_t *request = client + NONCE_BYTES;
221
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
20 bool new_boot = rx_key == 0 || memcmp(peer_boot, client, NONCE_BYTES) != 0;
222
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
20 bool duplicate = !new_boot && memcmp(peer_request, request, NONCE_BYTES) == 0;
223 20 int64_t now = k_uptime_get();
224
225
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
20 if (!duplicate && now - last_hello < RETRY_MS) {
226 ✗ return -EAGAIN;
227 }
228 20 last_hello = now;
229
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 if (!duplicate) {
230 uint8_t fresh[NONCE_BYTES];
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (radio_random(fresh, sizeof(fresh)) != 0) {
232 ✗ return -EIO;
233 }
234 16 int result = derive_key(client, request, fresh, false);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (result != 0) {
236 ✗ return result;
237 }
238 16 memcpy(peer_boot, client, NONCE_BYTES);
239 16 memcpy(peer_request, request, NONCE_BYTES);
240 16 memcpy(server_nonce, fresh, NONCE_BYTES);
241 16 kfsw_log_info("Radio session from node %u accepted", PEER);
242 }
243 20 frame_header(reply, REPLY);
244 20 memcpy(reply + 4, client, NONCE_BYTES);
245 20 memcpy(reply + 4 + NONCE_BYTES, request, NONCE_BYTES);
246 20 memcpy(reply + 4 + 2U * NONCE_BYTES, server_nonce, NONCE_BYTES);
247 20 int result = sign_control(reply, sizeof(reply), false);
248
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (result == 0) {
249 20 result = kfsw_uart_codec_control(PEER, reply, sizeof(reply));
250 }
251 /* A peer restart also invalidates the old outbound session. */
252
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
20 if (new_boot && !pending) {
253 16 psa_destroy_key(tx_key);
254 16 tx_key = 0;
255 16 (void)connect_locked();
256 }
257 20 return result;
258 }
259
260 32 static int control_received(csp_packet_t *packet)
261 {
262
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 size_t expected = packet->data[3] == HELLO ? HELLO_BYTES : REPLY_BYTES;
263
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 if (packet->length != expected || packet->id.src != PEER || packet->id.dst != LOCAL ||
264
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 packet->id.sport != 0 || packet->id.dport != 0 || packet->id.flags != 0 ||
265
5/8
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 28 times.
64 packet->id.pri != 0 || master == 0 || !settings.enabled ||
266 32 sign_control(packet->data, expected, true) != 0) {
267 4 return -EACCES;
268 }
269
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
28 if (packet->data[3] == HELLO) {
270 20 return hello_received(packet);
271 }
272
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
12 if (!pending || memcmp(packet->data + 4, boot_nonce, NONCE_BYTES) != 0 ||
273 4 memcmp(packet->data + 4 + NONCE_BYTES, request_nonce, NONCE_BYTES) != 0) {
274 4 return -EALREADY;
275 }
276 int result =
277 4 derive_key(boot_nonce, request_nonce, packet->data + 4 + 2U * NONCE_BYTES, true);
278
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (result == 0) {
279 4 pending = false;
280 4 kfsw_log_info("Radio session to node %u established", PEER);
281 } else {
282 ✗ kfsw_log_error("Radio session key not derived: %d", result);
283 }
284 4 return result;
285 }
286
287 24 static int encode(csp_packet_t *packet)
288 {
289 uint8_t nonce[12], aad[AAD_BYTES + DATA_HEADER];
290 size_t written;
291 24 int result = 0;
292
293 24 k_mutex_lock(&crypto_lock, K_FOREVER);
294
3/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 16 times.
24 if (!settings.enabled || !settings.tx) {
295 8 goto done;
296 }
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (tx_key == 0) {
298 ✗ (void)connect_locked();
299 ✗ result = -EAGAIN;
300 ✗ goto done;
301 }
302
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (packet->length > KFSW_CSP_PAYLOAD_MAX) {
303 8 result = -EMSGSIZE;
304 8 goto done;
305 }
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (tx_sequence == UINT64_MAX) {
307 ✗ result = -EOVERFLOW;
308 ✗ goto done;
309 }
310 8 ++tx_sequence;
311 8 frame_header(scratch, DATA);
312 8 sys_put_be64(tx_sequence, scratch + 4);
313 8 put_header(aad, &packet->id);
314 8 memcpy(aad + AAD_BYTES, scratch, DATA_HEADER);
315 8 sys_put_be32(LOCAL, nonce);
316 8 sys_put_be64(tx_sequence, nonce + 4);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (psa_aead_encrypt(tx_key, PSA_ALG_GCM, nonce, sizeof(nonce), aad, sizeof(aad),
318 8 packet->data, packet->length, scratch + DATA_HEADER,
319 sizeof(scratch) - DATA_HEADER - sizeof(uint32_t),
320 &written) != PSA_SUCCESS) {
321 ✗ result = -EIO;
322 ✗ goto done;
323 }
324 8 memcpy(packet->data, scratch, DATA_HEADER + written);
325 8 packet->length = (uint16_t)(DATA_HEADER + written);
326 24 done:
327 24 status.last_error = result;
328 24 k_mutex_unlock(&crypto_lock);
329 24 return result;
330 }
331
332 80 static int decode(csp_packet_t *packet)
333 {
334 uint8_t nonce[12], aad[AAD_BYTES + DATA_HEADER];
335 size_t written;
336 80 int result = 0;
337
4/6
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
80 bool framed = packet->length >= 4 && packet->data[0] == 'K' && packet->data[1] == 'R';
338
339 80 k_mutex_lock(&crypto_lock, K_FOREVER);
340
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
80 if (framed && packet->data[2] == 1 &&
341
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 36 times.
68 (packet->data[3] == HELLO || packet->data[3] == REPLY)) {
342 32 result = control_received(packet);
343
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 if (result == 0) {
344 24 result = 1;
345 }
346 32 goto done;
347 }
348
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
48 if (!settings.enabled || !settings.rx) {
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 result = framed ? -EACCES : 0;
350 4 goto done;
351 }
352
6/8
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 4 times.
44 if (!framed || packet->data[2] != 1 || packet->data[3] != DATA || rx_key == 0 ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 packet->length < DATA_HEADER + TAG_BYTES) {
354 12 result = -EACCES;
355 12 goto done;
356 }
357 32 uint64_t sequence = sys_get_be64(packet->data + 4);
358 32 put_header(aad, &packet->id);
359 32 memcpy(aad + AAD_BYTES, packet->data, DATA_HEADER);
360 32 sys_put_be32(PEER, nonce);
361 32 sys_put_be64(sequence, nonce + 4);
362
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (psa_aead_decrypt(rx_key, PSA_ALG_GCM, nonce, sizeof(nonce), aad, sizeof(aad),
363 32 packet->data + DATA_HEADER, packet->length - DATA_HEADER, scratch,
364 sizeof(scratch), &written) != PSA_SUCCESS) {
365 16 result = -EACCES;
366 16 goto done;
367 }
368
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (sequence <= rx_sequence) {
369 8 status.replays++;
370 8 result = -EALREADY;
371 8 goto done;
372 }
373 8 rx_sequence = sequence;
374 8 memcpy(packet->data, scratch, written);
375 8 packet->length = (uint16_t)written;
376 8 status.authenticated++;
377 80 done:
378
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 36 times.
80 if (result < 0) {
379 44 status.rejected++;
380 44 status.last_error = result;
381 }
382 80 radio_crypto_clear(scratch, sizeof(scratch));
383 80 k_mutex_unlock(&crypto_lock);
384 80 return result;
385 }
386
387 static const struct kfsw_uart_codec codec = {.encode = encode, .decode = decode};
388
389 36 int kfsw_radio_uhf_crypto_init(void)
390 {
391 36 int result = kfsw_uart_codec_register(CONFIG_KFSW_RADIO_UHF_CRYPTO_INTERFACE, &codec);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (result != 0) {
393 ✗ return result;
394 }
395 36 k_mutex_lock(&crypto_lock, K_FOREVER);
396
2/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 36 times.
36 if (psa_crypto_init() != PSA_SUCCESS || radio_random(boot_nonce, sizeof(boot_nonce)) != 0) {
397 ✗ result = -EIO;
398 } else {
399 36 result = radio_crypto_store_load(&settings);
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (result == -ENOENT) {
401 ✗ result = 0;
402 }
403
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 if (result == 0 && settings.key_set) {
404 36 result = import_master();
405 }
406 }
407 36 initialized = true;
408 36 status.last_error = result;
409 36 k_mutex_unlock(&crypto_lock);
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (result != 0) {
411 ✗ kfsw_log_error("Radio protection not started: %d", result);
412 } else {
413
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 kfsw_log_info("Radio protection %s, key %s", settings.enabled ? "on" : "off",
414 settings.key_set ? "loaded" : "not set");
415 }
416 36 return result;
417 }
418
419 16 void kfsw_radio_uhf_crypto_get(struct kfsw_radio_crypto_info *info)
420 {
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (info == NULL) {
422 ✗ return;
423 }
424 16 k_mutex_lock(&crypto_lock, K_FOREVER);
425 16 *info = status;
426 16 info->enabled = settings.enabled;
427 16 info->encrypt_tx = settings.tx;
428 16 info->encrypt_rx = settings.rx;
429 16 info->key_set = master != 0;
430 16 info->tx_ready = tx_key != 0;
431 16 info->rx_ready = rx_key != 0;
432 16 k_mutex_unlock(&crypto_lock);
433 }
434
435 12 int radio_crypto_validate_switch(const union kfsw_param_scalar *value)
436 {
437
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 return value->u8 <= 1U ? 0 : -EINVAL;
438 }
439
440 16 int radio_crypto_validate_key(const char *text)
441 {
442
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
16 if (!initialized && text[0] == '\0') {
443 4 return 0;
444 }
445 uint8_t key[KEY_TEXT_LENGTH / 2U];
446 bool valid;
447
448
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
12 if (strlen(text) != KEY_TEXT_LENGTH) {
449 4 return -EINVAL;
450 }
451 /* hex2bin refuses a byte that is not hexadecimal, so this both checks the
452 text and proves it fits the key. */
453 8 valid = hex2bin(text, KEY_TEXT_LENGTH, key, sizeof(key)) == sizeof(key);
454 8 radio_crypto_clear(key, sizeof(key));
455
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 return valid ? 0 : -EINVAL;
456 }
457
458 24 static void save_settings(struct radio_crypto_settings *next, bool changed_key)
459 {
460 uint8_t fresh[NONCE_BYTES];
461 24 int result = radio_random(fresh, sizeof(fresh));
462
463
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (result == 0) {
464 24 result = radio_crypto_store_save(next);
465 }
466 24 k_mutex_lock(&crypto_lock, K_FOREVER);
467
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4 times.
24 if (result == 0) {
468 20 settings = *next;
469 /* The peer must refresh its outbound session after our RX state resets. */
470 20 memcpy(boot_nonce, fresh, sizeof(boot_nonce));
471 20 clear_sessions();
472
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (changed_key) {
473 4 result = import_master();
474 }
475 }
476 24 status.last_error = result;
477 24 k_mutex_unlock(&crypto_lock);
478 24 }
479
480 8 void radio_crypto_set_key(const char *text)
481 {
482
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
8 if (!initialized || strlen(text) != KEY_TEXT_LENGTH) {
483 4 return;
484 }
485 4 k_mutex_lock(&settings_lock, K_FOREVER);
486 4 k_mutex_lock(&crypto_lock, K_FOREVER);
487 4 struct radio_crypto_settings next = settings;
488 4 k_mutex_unlock(&crypto_lock);
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (hex2bin(text, KEY_TEXT_LENGTH, next.key, sizeof(next.key)) != sizeof(next.key)) {
490 /* Reached only if the text was never validated: refuse rather than
491 install a key built from whatever the bad bytes decoded to. */
492 ✗ radio_crypto_clear(&next, sizeof(next));
493 ✗ k_mutex_unlock(&settings_lock);
494 ✗ return;
495 }
496 4 next.key_set = true;
497 4 save_settings(&next, true);
498 4 radio_crypto_clear(&next, sizeof(next));
499 4 k_mutex_unlock(&settings_lock);
500 }
501
502 32 static void set_switch(uint8_t which, bool value)
503 {
504
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (!initialized) {
505 12 return;
506 }
507 20 k_mutex_lock(&settings_lock, K_FOREVER);
508 20 k_mutex_lock(&crypto_lock, K_FOREVER);
509 20 struct radio_crypto_settings next = settings;
510 20 k_mutex_unlock(&crypto_lock);
511
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12 times.
20 if (which == 0) {
512 8 next.enabled = value;
513 }
514
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
20 if (which == 1) {
515 12 next.tx = value;
516 }
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (which == 2) {
518 ✗ next.rx = value;
519 }
520 20 save_settings(&next, false);
521 20 radio_crypto_clear(&next, sizeof(next));
522 20 k_mutex_unlock(&settings_lock);
523 }
524
525 12 void radio_crypto_set_enable(const union kfsw_param_scalar *value)
526 {
527 12 set_switch(0, value->u8 != 0);
528 12 }
529 16 void radio_crypto_set_tx(const union kfsw_param_scalar *value)
530 {
531 16 set_switch(1, value->u8 != 0);
532 16 }
533 4 void radio_crypto_set_rx(const union kfsw_param_scalar *value)
534 {
535 4 set_switch(2, value->u8 != 0);
536 4 }
537