|
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 @@ |
| 25 | static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff }; |
25 | static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff }; |
| 26 | #endif |
26 | #endif |
| 27 | static uint32_t g_rijndael_round_key[44] = {0}; |
27 | static uint32_t g_rijndael_round_key[44] = {0}; |
| 28 | static uint32_t g_key_of_the_hour[2] = {0}; |
28 | static volatile uint32_t g_key_of_the_hour[2] = {0}; |
| 29 | static ot_time g_hour_of_the_key; |
29 | static volatile ot_time g_hour_of_the_key; |
| 30 | |
30 | |
| 31 | static void udp_generate_rijndael_round_key(void) { |
31 | static 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) { |
| 52 | static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int age) { |
52 | static 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]; |
|