]> git.ipfire.org Git - thirdparty/opentracker.git/commitdiff
Reduce chance of collisions master
authorDirk Engling <erdgeist@erdgeist.org>
Tue, 26 May 2026 15:10:15 +0000 (17:10 +0200)
committerDirk Engling <erdgeist@erdgeist.org>
Tue, 26 May 2026 15:10:15 +0000 (17:10 +0200)
ot_udp.c

index a625dbace7e8f42e0cb150c290a1ee36358422a3..26fb9796d33f201688b14c9af49a447da067bc65 100644 (file)
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -25,8 +25,8 @@
 static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
 #endif
 static uint32_t g_rijndael_round_key[44] = {0};
-static uint32_t g_key_of_the_hour[2]     = {0};
-static ot_time  g_hour_of_the_key;
+static volatile uint32_t g_key_of_the_hour[2] = {0};
+static volatile ot_time  g_hour_of_the_key;
 
 static void     udp_generate_rijndael_round_key(void) {
   uint32_t key[16];
@@ -52,19 +52,29 @@ static void     udp_generate_rijndael_round_key(void) {
 static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int age) {
   uint32_t plain[4], crypt[4];
   int      i;
+  uint32_t current_key_of_the_hour;
+
   if (g_now_minutes - g_hour_of_the_key >= 60) {
-    g_hour_of_the_key    = g_now_minutes;
-    g_key_of_the_hour[1] = g_key_of_the_hour[0];
+    uint32_t old_key_of_the_hour = g_key_of_the_hour[0];
 #ifdef WANT_ARC4RANDOM
-    g_key_of_the_hour[0] = arc4random();
+    uint32_t new_key_of_the_hour = arc4random();
 #else
-    g_key_of_the_hour[0] = random();
+    uint32_t new_key_of_the_hour = random();
 #endif
+    /* If in the meantime another thread has performed
+       key rotation, do not overwrite their results */
+    if (g_now_minutes - g_hour_of_the_key >= 60) {
+      /* Upgrade en bloc */
+      g_hour_of_the_key    = g_now_minutes;
+      g_key_of_the_hour[0] = new_key_of_the_hour;
+      g_key_of_the_hour[1] = old_key_of_the_hour;
+    }
   }
 
   memcpy(plain, remoteip, sizeof(plain));
+  current_key_of_the_hour = g_key_of_the_hour[age];
   for (i = 0; i < 4; ++i)
-    plain[i] ^= g_key_of_the_hour[age];
+    plain[i] ^= current_key_of_the_hour;
   rijndaelEncrypt128(g_rijndael_round_key, (uint8_t *)plain, (uint8_t *)crypt);
   connid[0] = crypt[0] ^ crypt[1];
   connid[1] = crypt[2] ^ crypt[3];