summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/ot_udp.c b/ot_udp.c
index a625dba..26fb979 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -25,8 +25,8 @@
25static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff }; 25static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
26#endif 26#endif
27static uint32_t g_rijndael_round_key[44] = {0}; 27static uint32_t g_rijndael_round_key[44] = {0};
28static uint32_t g_key_of_the_hour[2] = {0}; 28static volatile uint32_t g_key_of_the_hour[2] = {0};
29static ot_time g_hour_of_the_key; 29static volatile ot_time g_hour_of_the_key;
30 30
31static void udp_generate_rijndael_round_key(void) { 31static void udp_generate_rijndael_round_key(void) {
32 uint32_t key[16]; 32 uint32_t key[16];
@@ -52,19 +52,29 @@ static void udp_generate_rijndael_round_key(void) {
52static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int age) { 52static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int age) {
53 uint32_t plain[4], crypt[4]; 53 uint32_t plain[4], crypt[4];
54 int i; 54 int i;
55 uint32_t current_key_of_the_hour;
56
55 if (g_now_minutes - g_hour_of_the_key >= 60) { 57 if (g_now_minutes - g_hour_of_the_key >= 60) {
56 g_hour_of_the_key = g_now_minutes; 58 uint32_t old_key_of_the_hour = g_key_of_the_hour[0];
57 g_key_of_the_hour[1] = g_key_of_the_hour[0];
58#ifdef WANT_ARC4RANDOM 59#ifdef WANT_ARC4RANDOM
59 g_key_of_the_hour[0] = arc4random(); 60 uint32_t new_key_of_the_hour = arc4random();
60#else 61#else
61 g_key_of_the_hour[0] = random(); 62 uint32_t new_key_of_the_hour = random();
62#endif 63#endif
64 /* If in the meantime another thread has performed
65 key rotation, do not overwrite their results */
66 if (g_now_minutes - g_hour_of_the_key >= 60) {
67 /* Upgrade en bloc */
68 g_hour_of_the_key = g_now_minutes;
69 g_key_of_the_hour[0] = new_key_of_the_hour;
70 g_key_of_the_hour[1] = old_key_of_the_hour;
71 }
63 } 72 }
64 73
65 memcpy(plain, remoteip, sizeof(plain)); 74 memcpy(plain, remoteip, sizeof(plain));
75 current_key_of_the_hour = g_key_of_the_hour[age];
66 for (i = 0; i < 4; ++i) 76 for (i = 0; i < 4; ++i)
67 plain[i] ^= g_key_of_the_hour[age]; 77 plain[i] ^= current_key_of_the_hour;
68 rijndaelEncrypt128(g_rijndael_round_key, (uint8_t *)plain, (uint8_t *)crypt); 78 rijndaelEncrypt128(g_rijndael_round_key, (uint8_t *)plain, (uint8_t *)crypt);
69 connid[0] = crypt[0] ^ crypt[1]; 79 connid[0] = crypt[0] ^ crypt[1];
70 connid[1] = crypt[2] ^ crypt[3]; 80 connid[1] = crypt[2] ^ crypt[3];