Skip to content

Commit 3cf4851

Browse files
ochavezmirandarzr
authored andcommitted
ZGW-3413: Improve keys randomization
ZGW-3413: unit test added Forwarded: #12 Signed-off-by: Philippe Coval <[email protected]>
1 parent 063eec6 commit 3cf4851

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/transport/Security_Scheme0.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ZW_PRNG.h"
99
#include "ZIP_Router_logging.h"
1010
#include "zip_router_config.h"
11+
#include "random.h"
1112
#define NONCE_OPT 0
1213

1314
/**/
@@ -23,6 +24,8 @@
2324
#define NONCE_BLACKLIST_SIZE 10
2425
#define RECEIVERS_NONCE_SIZE 8 /* The size of the nonce field in a Nonce Report */
2526

27+
#define S0_KEY_SIZE 16 /* The S0 Key Size */
28+
2629
typedef enum {
2730
NONCE_GET,
2831
NONCE_GET_SENT,
@@ -59,7 +62,7 @@ typedef struct _AUTHDATA_ {
5962
extern u8_t send_data(ts_param_t* p, const u8_t* data, u16_t len,ZW_SendDataAppl_Callback_t cb,void* user);
6063

6164
static sec_tx_session_t tx_sessions[NUM_TX_SESSIONS];
62-
uint8_t networkKey[16]; /* The master key */
65+
uint8_t networkKey[S0_KEY_SIZE] = {0}; /* The master key */
6366

6467

6568
/* Nonce blacklist type*/
@@ -885,10 +888,24 @@ uint8_t sec0_decrypt_message(uint8_t snode, uint8_t dnode, uint8_t* enc_data, ui
885888
}
886889

887890

888-
void sec0_reset_netkey() {
891+
void sec0_reset_netkey(void) {
892+
uint8_t n=0;
893+
bool bSuccess=false;
894+
889895
LOG_PRINTF("Reinitializing S0 network key (S2 keys are unchanged)\n");
890-
aes_random8( &networkKey[0] );
891-
aes_random8( &networkKey[8] );
896+
do {
897+
bSuccess = dev_urandom(sizeof(networkKey),networkKey);
898+
if (bSuccess) {
899+
break;
900+
}
901+
} while (n++ <= 10);
902+
903+
if(bSuccess) {
904+
nvm_config_set(security_netkey,networkKey);
905+
}
906+
else {
907+
ERR_PRINTF("Failed to generate random S0 key. Security compromised!\n");
908+
}
892909

893-
nvm_config_set(security_netkey,networkKey);
910+
ASSERT(bSuccess);
894911
}

test/S0/test_S0.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ void sec0_sd_callback(BYTE txStatus,void* user, TX_STATUS_TYPE *txStatEx) {
9393
printf("sec0_sd_callback from line %u\n", (unsigned int)user);
9494
}
9595

96+
int dev_urandom(int len, uint8_t *buf)
97+
{ FILE * fp;
98+
size_t read;
99+
int error = 0;
100+
101+
printf("-------in my dev_urandom\n");
102+
103+
fp = fopen("/dev/urandom", "r");
104+
read = fread(buf, 1, len, fp);
105+
if (read < len) {
106+
printf("Error: Random number generation failed\n");
107+
error = 1;
108+
}
109+
110+
fclose(fp);
111+
112+
return error ? 0 : 1;
113+
}
96114

97115
/****************************************Test cases*******************************************************/
98116

@@ -382,3 +400,13 @@ void test_nonce_blacklist() {
382400
TEST_ASSERT_TRUE(sec0_is_nonce_blacklisted(1, 2, (const uint8_t*)"AAAAAAAA"));
383401
TEST_ASSERT_TRUE(sec0_is_nonce_blacklisted(1, 2, (const uint8_t*)"BBBBBBBB"));
384402
}
403+
404+
void test_sec0_reset_netkey()
405+
{ uint8_t s0_key[S0_KEY_SIZE]={0};
406+
407+
sec0_reset_netkey();
408+
memcpy(s0_key, networkKey, sizeof(s0_key));
409+
sec0_reset_netkey();
410+
411+
TEST_ASSERT_NOT_EQUAL(memcmp(s0_key, networkKey, sizeof(s0_key)), 0);
412+
}

0 commit comments

Comments
 (0)