Skip to content

Commit

Permalink
Sys: fixed random option uniqueid
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocio committed Jul 5, 2022
1 parent 3a9eab8 commit ef6609c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 35 deletions.
5 changes: 0 additions & 5 deletions firmware/sys/uniqueid/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ menu "Subnet"
default "1111:2222"
depends on MODE_MANUAL

config SEED_XTIMER
bool "Using xtimer to seed"
default y
depends on MODE_RANDOM

endmenu # Prefix
endmenu
4 changes: 3 additions & 1 deletion firmware/sys/uniqueid/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USEMODULE += random
USEMODULE += xtimer
USEMODULE += ipv6_addr
USEMODULE += netdev_default
USEMODULE += gnrc_ipv6_default
USEMODULE += auto_init_gnrc_netif
19 changes: 7 additions & 12 deletions firmware/sys/uniqueid/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
@ingroup sys

### Unique Id
You have 3 options to get the unique ipv6 address, which can be edited in the config menu:

1) Static: It is make with the cpu id and it is the same between reboots,
unique for each device.
2) Random: A random address that is generated with the same fixed header
as the first option.
3) Custom: This option allows you to edit both the header and the default
address is "1111:2222".

Note: The default header is "2001:db8".
In this module you get a specific Id for each device and you have three options to get an unique IPV6 address,
which can be edited in the config menu:

1) Static: It is made with the cpu id and it is the same between reboots and unique for each device.
2) Random: A random address that is generated with the same fixed header. This option has a seed
cryptographically secure using NETOPT_RANDOM.
3) Custom: This option allows you to edit both the header and the address. The default address is "1111:2222".

### TO DO:
Use of ``` _xtimer_now ``` as seed is not cryptographically secure.
Note: The default header is "2001:db8".

Use of ``` seed = 0``` as seed is not cryptographically secure.
*/
42 changes: 36 additions & 6 deletions firmware/sys/uniqueid/uniqueid.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,37 @@
#include <string.h>
#include "uniqueid.h"
#include "random.h"
#include "xtimer.h"
#include "net/netdev/ieee802154.h"
#include "net/gnrc.h"

#ifdef CONFIG_MODE_RANDOM
static uint8_t radio_devices[] = {NETDEV_AT86RF215, NETDEV_AT86RF2XX, NETDEV_CC2538};

#ifdef CONFIG_MODE_SUB_24GHZ
int8_t subtract_to_interface_radio = 1;
#else
int8_t subtract_to_interface_radio = 0;
#endif

int8_t get_ieee802154_iface_radio(void) {
int max_ifaces = gnrc_netif_numof();
if (max_ifaces > 0) {
gnrc_netif_t *iface;
for (uint8_t i = 0; i < ARRAY_SIZE(radio_devices); i++) {
iface = gnrc_netif_get_by_type(radio_devices[i], NETDEV_INDEX_ANY);
if (iface != NULL) {
break;
}
}
if (iface != NULL) {
return iface->pid - subtract_to_interface_radio;
} else {
return -1;
}
}
return -1;
}
#endif

void subnet_to_ipv6(ipv6_addr_t *addr) {

Expand All @@ -44,14 +74,14 @@ void subnet_to_ipv6(ipv6_addr_t *addr) {
ipv6_addr_t header = {
.u8 = {0},
};
uint32_t test = 0;
int index = get_ieee802154_iface_radio();
netif_t *iface = netif_get_by_id(index);
ipv6_addr_from_str(&header, CONFIG_HEADER_ADDRESS_ID);
memcpy((char *)addr->u8, (char *)header.u8, 4);
union random_buff random_number;
#ifdef CONFIG_SEED_XTIMER
int seed = _xtimer_now(); // TO DO: This is not cryptographically secure (default)
#else
int seed = 0; // TO DO: this is an example, here put a seed cryptographically secure
#endif
netif_get_opt(iface, NETOPT_RANDOM, 0, &test, sizeof(test));
int seed = test;
random_init(seed);
random_number.u32 = random_uint32();
(void)random_number.u32;
Expand Down
7 changes: 1 addition & 6 deletions tests/uniqueid/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
menu "uniqueid"
menu "Uniqueid"

config SELECT_HEADER_IPV6
bool "Edit IPV6 Header"
Expand Down Expand Up @@ -33,10 +33,5 @@ menu "Subnet"
default "1111:2222"
depends on MODE_MANUAL

config SEED_XTIMER
bool "Using xtimer to seed"
default y
depends on MODE_RANDOM

endmenu # Prefix
endmenu
29 changes: 24 additions & 5 deletions tests/uniqueid/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
*/

/**
* @brief storage file
* @brief uniqueid file
*
* @author xkevin190 <kevinvelasco193@gmail.com>
* @author xkevin190 <kevinvelasco193@gmail.com>
* @author RocioRojas <rociorojas391@gmail.com>
*/
#include <string.h>
#include <errno.h>

#include "embUnit.h"
#include "uniqueid.h"

void get_unique_since_mac(ipv6_addr_t *output) {
void get_unique_from_mac(ipv6_addr_t *output) {
ipv6_addr_t header = {
.u8 = {0},
};
Expand All @@ -34,22 +35,40 @@ void get_unique_since_mac(ipv6_addr_t *output) {
ipv6_addr_from_str(&header, CONFIG_HEADER_ADDRESS_ID);
memcpy((char *)output->u8, (char *)header.u8, 4);
strncat((char *)output->u8, addr_cpu, 4);
ipv6_addr_print(output);
}

void test_get_ipv6Address(void) {
ipv6_addr_t ipv6 = {
.u8 = {0},
};
subnet_to_ipv6(&ipv6);

subnet_to_ipv6(&ipv6);
#ifdef CONFIG_MODE_STATIC
ipv6_addr_t output = {
.u8 = {0},
};
ipv6_addr_print(&ipv6);
get_unique_since_mac(&output);
get_unique_from_mac(&output);
TEST_ASSERT_EQUAL_INT(1, ipv6_addr_equal(&ipv6, &output));
#endif

#ifdef CONFIG_MODE_RANDOM
ipv6_addr_t output1 = {
.u8 = {0},
};
ipv6_addr_t output2 = {
.u8 = {0},
};

subnet_to_ipv6(&output1);
subnet_to_ipv6(&output2);
printf("\nFirst random IPv6\n");
ipv6_addr_print(&output1);
printf("\nSecond random IPv6\n");
ipv6_addr_print(&output2);
TEST_ASSERT_EQUAL_INT(0, ipv6_addr_equal(&output1, &output2));
#endif
}

Test *tests_get_unique_id(void) {
Expand Down

0 comments on commit ef6609c

Please sign in to comment.