Bug Summary

File:builds/wireshark/wireshark/epan/dissectors/packet-rf4ce-secur.c
Warning:line 478, column 9
The first element of the 2nd argument is undefined
Note:line 478, column 9
Other elements might also be undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name packet-rf4ce-secur.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-23/lib/clang/23 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan/dissectors -isystem /builds/wireshark/wireshark/build/epan/dissectors -isystem /usr/include/mit-krb5 -isystem /builds/wireshark/wireshark/epan -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fwrapv -fwrapv-pointer -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-12-05-103046-3656-1 -x c /builds/wireshark/wireshark/epan/dissectors/packet-rf4ce-secur.c
1/* packet-rf4ce-secur.c
2 * Security related functions and objects for RF4CE dissector
3 * Copyright (C) Atmosic 2023
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <[email protected]>
7 * Copyright 1998 Gerald Combs
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 /* AI Generated comment
13 * RF4CE is a low-power, low-latency wireless protocol developed by the Connectivity Standards Alliance (formerly Zigbee Alliance)
14 * for remote controls and simple consumer electronics. It is built on the IEEE 802.15.4 standard and allows for bidirectional communication,
15 * eliminating the need for line-of-sight and providing features like on-remote information display.
16 * Its primary goal is to offer a robust and easy-to-implement alternative to infrared (IR) remotes for devices such as TVs,
17 * set-top boxes, and keyless entry systems.
18 */
19
20#include "packet-rf4ce-secur.h"
21#include "packet-zbee-security.h"
22#include "packet-ieee802154.h"
23#include <wsutil/wsgcrypt.h>
24#include <epan/proto_data.h>
25
26#ifdef RF4CE_DEBUG_EN
27void rf4ce_print_arr(const char *str, uint8_t *ptr, uint16_t len);
28#define RF4CE_PRINT_ARR(s, p, l) rf4ce_print_arr(s, p, l)
29#else
30#define RF4CE_PRINT_ARR(s, p, l)
31#endif /* RF4CE_DEBUG_EN */
32
33static keypair_context_t keypair_context;
34static key_exchange_context_t key_exchange_context;
35static addr_entry_t addr_table[RF4CE_ADDR_TABLE_SIZE(64 * 2)];
36static nwk_key_entry_t nwk_key_storage[RF4CE_NWK_KEY_STORAGE_SIZE64];
37static vendor_secret_entry_t vendor_secret_storage[RF4CE_VENDOR_SECRET_STORAGE_SIZE64];
38
39static void keypair_context_calc_key(uint8_t *nwk_key);
40static nwk_key_entry_t *nwk_key_storage_get_entry_by_key(uint8_t *nwk_key, bool_Bool key_from_gui);
41
42static void reverse(uint8_t *dest, uint8_t *src, uint16_t size);
43
44/* RF4CE GDP 2.0 spec, part 7.4.1 Key Exchange negotiation
45 * Default secret: This is a 128bit "secret" that is known to all devices that are certified to
46 * conform to this specification. The value shall be set to the following octet string (lowest order
47 * octet first)
48 * Note that this value should be expected to be widely known and the overall link security
49 * should not depend on this value remaining a secret.
50 */
51uint8_t DEFAULT_SECRET[SEC_STR_LEN16] =
52 {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
53 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
54
55void keypair_context_init(const uint8_t *controller_ieee, const uint8_t *target_ieee, uint8_t expected_transfer_count)
56{
57 if ((controller_ieee == NULL((void*)0)) || (target_ieee == NULL((void*)0)))
58 {
59 return;
60 }
61 memset(&keypair_context, 0, sizeof(keypair_context_t));
62
63 memcpy(keypair_context.controller_addr, controller_ieee, RF4CE_IEEE_ADDR_LEN8);
64 memcpy(keypair_context.target_addr, target_ieee, RF4CE_IEEE_ADDR_LEN8);
65
66 keypair_context.nwk_key_exchange_transfer_expected = expected_transfer_count;
67}
68
69static void keypair_context_calc_key(uint8_t *nwk_key)
70{
71 /* "Divide the result of phase 1 into five 128-bit blocks and compute
72 * their XOR." */
73 for (int i = 0; i < 4; i++)
74 {
75 for (int j = 0; j < KEY_LEN16; j++)
76 {
77 keypair_context.nwk_key_seed[(i + 1) * KEY_LEN16 + j] ^= keypair_context.nwk_key_seed[i * KEY_LEN16 + j];
78 }
79 }
80
81 memcpy(nwk_key, &keypair_context.nwk_key_seed[RF4CE_NWK_KEY_SEED_DATA_LENGTH80 - KEY_LEN16], KEY_LEN16);
82}
83
84void keypair_context_update_seed(uint8_t *seed, uint8_t seed_seqn)
85{
86 bool_Bool is_retransmit = (seed_seqn == keypair_context.nwk_key_exchange_transfer_received - 1);
87 bool_Bool is_latest_seed = (seed_seqn + 1 == keypair_context.nwk_key_exchange_transfer_expected);
88
89 /* retransmitt of the latest key seed - we must to re-calculate a NWK key */
90 if (is_retransmit && is_latest_seed)
91 {
92 memcpy(keypair_context.nwk_key_seed, keypair_context.nwk_key_seed_prev, RF4CE_NWK_KEY_SEED_DATA_LENGTH80);
93 }
94
95 if (seed_seqn == 0)
96 {
97 memcpy(keypair_context.nwk_key_seed_latest, seed, RF4CE_NWK_KEY_SEED_DATA_LENGTH80);
98 keypair_context.nwk_key_exchange_transfer_received = 1;
99 return;
100 }
101
102 /* Retransmit of the previous key seed. Should take this one */
103 if (is_retransmit)
104 {
105 /* save this one as a candidate */
106 memcpy(keypair_context.nwk_key_seed_latest, seed, RF4CE_NWK_KEY_SEED_DATA_LENGTH80);
107
108 /* move on if it's the latest seed to re-calculate a NWK key */
109 if (!is_latest_seed)
110 {
111 return;
112 }
113 }
114
115 if (seed_seqn == keypair_context.nwk_key_exchange_transfer_received)
116 {
117 /* Apply previous key seed, it has been accepted since we received the next one */
118 for (int i = 0; i < RF4CE_NWK_KEY_SEED_DATA_LENGTH80; i++)
119 {
120 keypair_context.nwk_key_seed[i] ^= keypair_context.nwk_key_seed_latest[i];
121 }
122
123 /* save this one as a candidate */
124 memcpy(keypair_context.nwk_key_seed_latest, seed, RF4CE_NWK_KEY_SEED_DATA_LENGTH80);
125 keypair_context.nwk_key_exchange_transfer_received += 1;
126 }
127
128 if (is_latest_seed)
129 {
130 uint8_t nwk_key[KEY_LEN16] = {0};
131 addr_entry_t *controller_addr_ent = rf4ce_addr_table_get_addr_entry_by_ieee(keypair_context.controller_addr);
132 addr_entry_t *target_addr_ent = rf4ce_addr_table_get_addr_entry_by_ieee(keypair_context.target_addr);
133
134 /* save the current key seed to avoid retransmitts of the latest one in future */
135 memcpy(keypair_context.nwk_key_seed_prev, keypair_context.nwk_key_seed, RF4CE_NWK_KEY_SEED_DATA_LENGTH80);
136
137 /* "Compute the XOR of the seed data fields from each of the (n + 1)
138 * key seed command frames." */
139 for (int i = 0; i < RF4CE_NWK_KEY_SEED_DATA_LENGTH80; i++)
140 {
141 keypair_context.nwk_key_seed[i] ^= keypair_context.nwk_key_seed_latest[i];
142 }
143
144 keypair_context_calc_key(nwk_key);
145
146 nwk_key_storage_add_entry(
147 nwk_key,
148 controller_addr_ent,
149 target_addr_ent,
150 false0, /* key from commissioning session */
151 true1); /* is_pairing_key */
152 }
153}
154
155static nwk_key_entry_t *nwk_key_storage_get_entry_by_key(uint8_t *nwk_key, bool_Bool key_from_gui)
156{
157 nwk_key_entry_t *entry = NULL((void*)0);
158 int idx = 0;
159
160 while (idx < RF4CE_NWK_KEY_STORAGE_SIZE64)
161 {
162 if (nwk_key_storage[idx].is_used && (nwk_key_storage[idx].key_from_gui == key_from_gui) && (memcmp(nwk_key_storage[idx].nwk_key, nwk_key, KEY_LEN16) == 0))
163 {
164 entry = nwk_key_storage + idx;
165 break;
166 }
167
168 idx++;
169 }
170
171 return entry;
172}
173
174void nwk_key_storage_add_entry(uint8_t *nwk_key, addr_entry_t *controller_addr_ent, addr_entry_t *target_addr_ent, bool_Bool key_from_gui, bool_Bool is_pairing_key)
175{
176 /* find an existing entry so as not to add duplicates */
177 nwk_key_entry_t *nwk_key_entry = nwk_key_storage_get_entry_by_key(nwk_key, key_from_gui);
178
179 if (nwk_key_entry == NULL((void*)0))
180 {
181 int idx = 0;
182
183 while (idx < RF4CE_NWK_KEY_STORAGE_SIZE64)
184 {
185 if (!nwk_key_storage[idx].is_used)
186 {
187 memcpy(nwk_key_storage[idx].nwk_key, nwk_key, KEY_LEN16);
188 nwk_key_storage[idx].controller_addr_ent = controller_addr_ent;
189 nwk_key_storage[idx].target_addr_ent = target_addr_ent;
190 nwk_key_storage[idx].key_from_gui = key_from_gui;
191 nwk_key_storage[idx].is_used = true1;
192 nwk_key_storage[idx].is_pairing_key = is_pairing_key;
193 break;
194 }
195
196 idx++;
197 }
198 }
199}
200
201void nwk_key_storage_release_entry(uint8_t *nwk_key, bool_Bool key_from_gui)
202{
203 nwk_key_entry_t *nwk_key_entry = nwk_key_storage_get_entry_by_key(nwk_key, key_from_gui);
204
205 if (nwk_key_entry != NULL((void*)0))
206 {
207 nwk_key_entry->is_used = false0;
208 }
209}
210
211void rf4ce_addr_table_add_addrs(const void *ieee_addr, uint16_t short_addr)
212{
213 unsigned idx = 0;
214
215 if (ieee_addr == NULL((void*)0))
216 {
217 return;
218 }
219
220 /* search for addresses so as not to add duplicates */
221 while (idx < RF4CE_ADDR_TABLE_SIZE(64 * 2))
222 {
223 if (addr_table[idx].is_used && (memcmp(addr_table[idx].ieee_addr, ieee_addr, RF4CE_IEEE_ADDR_LEN8) == 0) && addr_table[idx].short_addr == short_addr)
224 {
225 return;
226 }
227
228 idx++;
229 }
230
231 /* no duplicates found, search for a free slot */
232 idx = 0;
233 while (idx < RF4CE_ADDR_TABLE_SIZE(64 * 2) && addr_table[idx].is_used)
234 {
235 idx++;
236 }
237
238 if (idx < RF4CE_ADDR_TABLE_SIZE(64 * 2))
239 {
240 memcpy(addr_table[idx].ieee_addr, ieee_addr, RF4CE_IEEE_ADDR_LEN8);
241 addr_table[idx].short_addr = short_addr;
242 addr_table[idx].is_used = true1;
243 }
244}
245
246bool_Bool rf4ce_addr_table_get_ieee_addr(uint8_t *ieee_addr, packet_info *pinfo, bool_Bool is_src)
247{
248 bool_Bool addr_found = false0;
249 address_type addr_type;
250 ieee802154_hints_t *hints;
251 const void *p_addr = NULL((void*)0);
252 uint16_t short_addr = 0xffff;
253
254 /* Check inputs */
255 if ((ieee_addr == NULL((void*)0)) || (pinfo == NULL((void*)0)))
256 {
257 return false0;
258 }
259 if (is_src)
260 {
261 addr_type = pinfo->dl_src.type;
262 p_addr = pinfo->dl_src.data;
263 }
264 else
265 {
266 addr_type = pinfo->dl_dst.type;
267 p_addr = pinfo->dl_dst.data;
268 }
269 if (addr_type == AT_EUI64)
270 {
271 if (p_addr == NULL((void*)0))
272 {
273 return false0;
274 }
275 }
276 else
277 {
278 /* Get addresses */
279 hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(),
280 pinfo,
281 proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN"wpan"),
282 0
283 );
284 if (hints == NULL((void*)0))
285 {
286 return false0;
287 }
288 short_addr = (is_src) ? hints->src16 : hints->dst16;
289 }
290 /* Search address in address table */
291 for (unsigned idx = 0; idx < RF4CE_ADDR_TABLE_SIZE(64 * 2); idx++)
292 {
293 if (addr_table[idx].is_used)
294 {
295 if (addr_type == AT_EUI64)
296 {
297 if (memcmp(addr_table[idx].ieee_addr, p_addr, RF4CE_IEEE_ADDR_LEN8) == 0) {
298 addr_found = true1;
299 }
300 }
301 else
302 {
303 if (addr_table[idx].short_addr == short_addr) {
304 addr_found = true1;
305 }
306 }
307 if (addr_found)
308 {
309 memcpy(ieee_addr, addr_table[idx].ieee_addr, RF4CE_IEEE_ADDR_LEN8);
310 break;
311 }
312 }
313 }
314 return addr_found;
315}
316
317addr_entry_t *rf4ce_addr_table_get_addr_entry_by_ieee(uint8_t *ieee_addr)
318{
319 addr_entry_t *entry = NULL((void*)0);
320 unsigned idx = 0;
321
322 while (ieee_addr != NULL((void*)0) && idx < RF4CE_ADDR_TABLE_SIZE(64 * 2))
323 {
324 if (addr_table[idx].is_used && memcmp(addr_table[idx].ieee_addr, ieee_addr, RF4CE_IEEE_ADDR_LEN8) == 0)
325 {
326 entry = addr_table + idx;
327 break;
328 }
329
330 idx++;
331 }
332
333 return entry;
334}
335
336void key_exchange_context_init(void)
337{
338 memset(&key_exchange_context.rand_a, 0, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8);
339 memset(&key_exchange_context.rand_b, 0, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8);
340 memset(&key_exchange_context.mac_a, 0, RF4CE_IEEE_ADDR_LEN8);
341 memset(&key_exchange_context.mac_b, 0, RF4CE_IEEE_ADDR_LEN8);
342}
343
344void key_exchange_context_start_procedure(void)
345{
346 if (!key_exchange_context.is_proc_started)
347 {
348 key_exchange_context.is_proc_started = true1;
349 }
350}
351
352void key_exchange_context_stop_procedure(void)
353{
354 if (key_exchange_context.is_proc_started)
355 {
356 key_exchange_context.is_proc_started = false0;
357 }
358}
359
360bool_Bool key_exchange_context_is_procedure_started(void)
361{
362 return key_exchange_context.is_proc_started;
363}
364
365void key_exchange_context_set_rand_a(uint8_t *rand_a)
366{
367 if (rand_a != NULL((void*)0))
368 {
369 memcpy(key_exchange_context.rand_a, rand_a, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8);
370 }
371}
372
373void key_exchange_context_set_rand_b(uint8_t *rand_b)
374{
375 if (rand_b != NULL((void*)0))
376 {
377 memcpy(key_exchange_context.rand_b, rand_b, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8);
378 }
379}
380
381void key_exchange_context_set_mac_a(uint8_t *mac_a)
382{
383 if (mac_a != NULL((void*)0))
384 {
385 memcpy(key_exchange_context.mac_a, mac_a, RF4CE_IEEE_ADDR_LEN8);
386 }
387}
388
389void key_exchange_context_set_mac_b(uint8_t *mac_b)
390{
391 if (mac_b != NULL((void*)0))
392 {
393 memcpy(key_exchange_context.mac_b, mac_b, RF4CE_IEEE_ADDR_LEN8);
394 }
395}
396
397#ifdef RF4CE_DEBUG_EN
398void rf4ce_print_arr(const char *str, uint8_t *ptr, uint16_t len)
399{
400 g_print("%s: ", str);
401 for (uint16_t i = 0; i < len-1; i++)
402 {
403 g_print("%02x:", *(ptr+i));
404 }
405 g_print("%02x\n", *(ptr+len-1));
406}
407#endif /* RF4CE_DEBUG_EN */
408
409static bool_Bool calc_key_cmac(uint8_t *secret, uint8_t *nwk_key, uint32_t tag_b_pack, uint8_t *key_out)
410{
411 uint8_t mac_a[RF4CE_IEEE_ADDR_LEN8];
412 uint8_t mac_b[RF4CE_IEEE_ADDR_LEN8];
413
414 uint8_t *rand_a = key_exchange_context.rand_a;
415 uint8_t *rand_b = key_exchange_context.rand_b;
416
417 uint8_t k_dk_data[RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8 +
418 RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8];
419 uint8_t k_dk_data_reversed[RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8 +
420 RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8];
421
422 uint8_t context_data[CONTEXT_STR_LEN9 + RF4CE_IEEE_ADDR_LEN8 +
423 RF4CE_IEEE_ADDR_LEN8 + KEY_LEN16];
424
425 uint8_t k_dk_key[KEY_LEN16];
426 uint8_t new_key[KEY_LEN16];
8
'new_key' declared without an initial value
427
428 uint8_t dummy[KEY_LEN16];
429 uint32_t tag_b_calc;
430
431 uint8_t *data_ptr;
432
433 reverse(mac_a, key_exchange_context.mac_a, RF4CE_IEEE_ADDR_LEN8);
434 reverse(mac_b, key_exchange_context.mac_b, RF4CE_IEEE_ADDR_LEN8);
435
436 data_ptr = k_dk_data;
437 memcpy(data_ptr, rand_a, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8);
438 data_ptr += RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8;
439 memcpy(data_ptr, rand_b, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8);
440
441 data_ptr = k_dk_data_reversed;
442 memcpy(data_ptr, rand_b, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8);
443 data_ptr += RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_B_LENGTH8;
444 memcpy(data_ptr, rand_a, RF4CE_PROFILE_CMD_KEY_EXCHANGE_RAND_A_LENGTH8);
445
446 data_ptr = context_data;
447 memcpy(data_ptr, CONTEXT_STR"RF4CE GDP", CONTEXT_STR_LEN9);
448 data_ptr += CONTEXT_STR_LEN9;
449 memcpy(data_ptr, mac_a, RF4CE_IEEE_ADDR_LEN8);
450 data_ptr += RF4CE_IEEE_ADDR_LEN8;
451 memcpy(data_ptr, mac_b, RF4CE_IEEE_ADDR_LEN8);
452 data_ptr += RF4CE_IEEE_ADDR_LEN8;
453 memcpy(data_ptr, nwk_key, KEY_LEN16);
454
455 /* Generic Device Profile Version 2.0
456 * 7.4.2 Key generation
457 * Calculate derivation key
458 * K_dk = AES-128-CMAC (RAND-A || RAND-B, Shared secret)
459 */
460 rf4ce_aes_cmac(secret, SEC_STR_LEN16, k_dk_data, k_dk_key);
461
462 /* Calculate new link key
463 * Link key = AES-128-CMAC (K_dk, context || label || pairing key)
464 */
465 rf4ce_aes_cmac(context_data, sizeof(context_data), k_dk_key, new_key);
9
Calling 'rf4ce_aes_cmac'
13
Returning from 'rf4ce_aes_cmac'
466
467 /* Calculate TAG-B value
468 * TAG-B = AES-128-CMAC(link key, RAND-B || RAND-A)
469 */
470 rf4ce_aes_cmac(k_dk_data_reversed, sizeof(k_dk_data_reversed), new_key, dummy);
14
Calling 'rf4ce_aes_cmac'
24
Returning from 'rf4ce_aes_cmac'
471 memcpy((uint8_t *)&tag_b_calc, dummy, RF4CE_PROFILE_CMD_KEY_EXCHANGE_TAG_A_LENGTH4);
472
473 RF4CE_PRINT_ARR("tag_b_calc", (uint8_t *)&tag_b_calc, 4);
474 RF4CE_PRINT_ARR(" new_key", new_key, 16);
475
476 if (tag_b_pack == tag_b_calc)
25
Assuming 'tag_b_pack' is equal to 'tag_b_calc'
26
Taking true branch
477 {
478 memcpy(key_out, new_key, KEY_LEN16);
Other elements might also be undefined
27
The first element of the 2nd argument is undefined
479 return true1;
480 }
481
482 return false0;
483}
484
485static bool_Bool key_exchange_calc_key_cont(uint8_t *secret, uint32_t tag_b_pack, bool_Bool try_pairing_key, uint8_t *new_key_out)
486{
487 bool_Bool is_new_key_found = false0;
488
489 for (unsigned i = 0; i < RF4CE_NWK_KEY_STORAGE_SIZE64; i++)
490 {
491 if (nwk_key_storage[i].is_used && ((try_pairing_key
5.1
'try_pairing_key' is true
&& nwk_key_storage[i].is_pairing_key) || (!try_pairing_key && nwk_key_storage[i].key_from_gui)))
5
Assuming field 'is_used' is true
6
Assuming field 'is_pairing_key' is true
492 {
493 is_new_key_found = calc_key_cmac(secret, nwk_key_storage[i].nwk_key, tag_b_pack, new_key_out);
7
Calling 'calc_key_cmac'
494
495 if (is_new_key_found)
496 {
497 break;
498 }
499 }
500 }
501
502 return is_new_key_found;
503}
504
505void key_exchange_calc_key(uint32_t tag_b_pack)
506{
507 uint8_t *controller_addr = key_exchange_context.mac_a;
508 uint8_t *target_addr = key_exchange_context.mac_b;
509
510 addr_entry_t *controller_addr_ent = rf4ce_addr_table_get_addr_entry_by_ieee(controller_addr);
511 addr_entry_t *target_addr_ent = rf4ce_addr_table_get_addr_entry_by_ieee(target_addr);
512
513 uint8_t *secret;
514
515 uint8_t new_key[KEY_LEN16];
516 bool_Bool is_new_key_found = false0;
517
518 for (unsigned i = 0; i < RF4CE_VENDOR_SECRET_STORAGE_SIZE64; i++)
1
Loop condition is true. Entering loop body
519 {
520 if (!vendor_secret_storage[i].is_used)
2
Assuming field 'is_used' is true
3
Taking false branch
521 {
522 continue;
523 }
524
525 secret = vendor_secret_storage[i].secret;
526
527 /* try all the pairing keys first */
528 is_new_key_found = key_exchange_calc_key_cont(secret, tag_b_pack, true1, new_key);
4
Calling 'key_exchange_calc_key_cont'
529
530 /* try other keys */
531 if (!is_new_key_found)
532 {
533 is_new_key_found = key_exchange_calc_key_cont(secret, tag_b_pack, false0, new_key);
534 }
535
536 if (is_new_key_found)
537 {
538 nwk_key_storage_add_entry(
539 new_key,
540 controller_addr_ent,
541 target_addr_ent,
542 false0, /* key from the Key Exchange procedure */
543 false0); /* !is_pairing_key */
544
545 break;
546 }
547 }
548}
549
550static vendor_secret_entry_t *vendor_secret_storage_get_entry(uint8_t *secret)
551{
552 vendor_secret_entry_t *entry = NULL((void*)0);
553 int idx = 0;
554
555 while (idx < RF4CE_VENDOR_SECRET_STORAGE_SIZE64)
556 {
557 if (vendor_secret_storage[idx].is_used && (memcmp(vendor_secret_storage[idx].secret, secret, SEC_STR_LEN16) == 0))
558 {
559 entry = vendor_secret_storage + idx;
560 break;
561 }
562
563 idx++;
564 }
565
566 return entry;
567}
568
569void vendor_secret_storage_add_entry(uint8_t *secret)
570{
571 unsigned idx = 0;
572 vendor_secret_entry_t *entry = vendor_secret_storage_get_entry(secret);
573
574 if (entry != NULL((void*)0))
575 {
576 return;
577 }
578
579 while (idx < RF4CE_VENDOR_SECRET_STORAGE_SIZE64 && vendor_secret_storage[idx].is_used)
580 {
581 idx++;
582 }
583
584 if (idx < RF4CE_VENDOR_SECRET_STORAGE_SIZE64)
585 {
586 memcpy(vendor_secret_storage[idx].secret, secret, SEC_STR_LEN16);
587 vendor_secret_storage[idx].is_used = true1;
588 }
589}
590
591void vendor_secret_storage_release_entry(uint8_t *secret)
592{
593 vendor_secret_entry_t *entry = vendor_secret_storage_get_entry(secret);
594
595 if (entry != NULL((void*)0))
596 {
597 entry->is_used = false0;
598 }
599}
600
601void rf4ce_secur_cleanup(void)
602{
603 int idx = 0;
604
605 memset(&keypair_context, 0, sizeof(keypair_context));
606 memset(addr_table, 0, sizeof(addr_table));
607
608 while (idx < RF4CE_NWK_KEY_STORAGE_SIZE64)
609 {
610 if (nwk_key_storage[idx].is_used && !nwk_key_storage[idx].key_from_gui)
611 {
612 nwk_key_storage[idx].is_used = false0;
613 }
614
615 idx++;
616 }
617}
618
619static void reverse(uint8_t *dest, uint8_t *src, uint16_t size)
620{
621 for (int i = 0; i < size; i++)
622 {
623 dest[size - i - 1] = src[i];
624 }
625}
626
627bool_Bool decrypt_data(
628 const uint8_t *in, uint8_t *out,
629 uint16_t payload_offset,
630 uint16_t *len,
631 uint8_t src_ieee[RF4CE_IEEE_ADDR_LEN8], uint8_t dst_ieee[RF4CE_IEEE_ADDR_LEN8])
632{
633 bool_Bool ret = false0;
634 uint8_t frame_control = *in;
635 int idx = 0;
636
637 if (*len < RF4CE_MIN_NWK_LENGTH5 || *len > RF4CE_MAX_NWK_LENGTH148)
638 {
639 return false0;
640 }
641
642 if (*len < payload_offset + RF4CE_CCM_M4) {
643 return false0;
644 }
645
646 while (idx < RF4CE_NWK_KEY_STORAGE_SIZE64)
647 {
648 if (nwk_key_storage[idx].is_used)
649 {
650 uint8_t *data_ptr;
651
652 /* Form the nonce (3.5.11.3 Outgoing frame security) */
653 uint8_t nonce[RF4CE_IEEE_ADDR_LEN8 + 4 + 1];
654
655 data_ptr = nonce;
656 /* Source IEEE address */
657 reverse(data_ptr, src_ieee, RF4CE_IEEE_ADDR_LEN8);
658 data_ptr += RF4CE_IEEE_ADDR_LEN8;
659 /* Fetch frame counter from the packet (don't check) */
660 memcpy(data_ptr, in + 1, 4);
661 data_ptr += 4;
662 /* Security level */
663 *data_ptr = RF4CE_SECUR_CONTROL5;
664
665 /* Form the auth string (3.5.11.3 Outgoing frame security) */
666 uint8_t auth[1 + 4 + RF4CE_IEEE_ADDR_LEN8];
667
668 data_ptr = auth;
669 /* Frame control field */
670 *data_ptr = frame_control;
671 data_ptr += 1;
672 /* Fetch frame counter from the packet (don't check) */
673 memcpy(data_ptr, in + 1, 4);
674 data_ptr += 4;
675 /* Destination IEEE address */
676 reverse(data_ptr, dst_ieee, RF4CE_IEEE_ADDR_LEN8);
677
678 ret = zbee_sec_ccm_decrypt(nwk_key_storage[idx].nwk_key,
679 nonce,
680 auth,
681 in + payload_offset,
682 out,
683 sizeof(auth),
684 *len - payload_offset - RF4CE_CCM_M4,
685 RF4CE_CCM_M4);
686
687 if (ret)
688 {
689 *len = *len - payload_offset - RF4CE_CCM_M4;
690 break;
691 }
692 }
693
694 idx++;
695 }
696
697 return ret;
698}
699
700// Calculate the CMAC
701void rf4ce_aes_cmac(unsigned char *input, unsigned long length, unsigned char *key, unsigned char *mac_value)
702{
703 gcry_mac_hd_t mac_hd;
704 size_t l = length;
705
706 if (gcry_mac_open(&mac_hd, GCRY_MAC_CMAC_AES, 0, NULL((void*)0)))
10
Assuming the condition is true
11
Taking true branch
15
Assuming the condition is false
16
Taking false branch
707 {
708 return;
12
Returning without writing to '*mac_value'
709 }
710 if (gcry_mac_setkey(mac_hd, key, KEY_LEN16))
17
Assuming the condition is false
18
Taking false branch
711 {
712 gcry_mac_close(mac_hd);
713 return;
714 }
715 if (gcry_mac_write(mac_hd, input, length))
19
Assuming the condition is false
20
Taking false branch
716 {
717 gcry_mac_close(mac_hd);
718 return;
719 }
720 if (gcry_mac_read(mac_hd, mac_value, &l))
21
Assuming the condition is false
22
Taking false branch
721 {
722 gcry_mac_close(mac_hd);
723 return;
724 }
725 gcry_mac_close(mac_hd);
726}
23
Returning without writing to '*key'