Skip to content

Commit fc2ff9c

Browse files
committed
Map ICE credential bytes with encode6() instead of a lookup table
Replace the charset[] table lookup in ice_random_string() with a branch-light arithmetic mapping of 6 random bits onto 64 distinct ice-chars (+, /, A-Z, a-z, 0-9), see RFC 8445 section 15.1.
1 parent d2280cf commit fc2ff9c

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

‎modules/ice_lite/rtpp_ice_lite.c‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,34 @@ udp_local_get(const struct udp_sock *us, struct sa *local)
293293
return (0);
294294
}
295295

296+
static inline uint8_t
297+
encode6(unsigned x)
298+
{
299+
x &= 63u;
300+
unsigned k = x & 31u;
301+
302+
if (k == 0)
303+
return x ? '+' : '/';
304+
305+
if (k <= 26)
306+
return x | ('A' - 1);
307+
308+
return 0x30u | ((~x & 7u) << 1) | (x >> 5);
309+
}
310+
296311
/*
297312
* RFC 8445 section 5.3: ufrag needs at least 24 and pwd at least 128 bits
298-
* of randomness. The charset has 64 entries, so each byte gives 6 bits.
313+
* of randomness. Each byte gives 6 bits via encode6().
299314
*/
300315
static int
301316
ice_random_string(char *buffer, int length)
302317
{
303-
static const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
304-
"abcdefghijklmnopqrstuvwxyz"
305-
"0123456789"
306-
"+/";
307318
unsigned char *rb = (unsigned char *)buffer;
308319

309320
if (RAND_bytes(rb, length) != 1)
310321
return (-1);
311322
for (int i = 0; i < length; i++)
312-
buffer[i] = charset[rb[i] % (sizeof(charset) - 1)];
323+
buffer[i] = encode6(rb[i]);
313324
buffer[length] = '\0';
314325
return (0);
315326
}

0 commit comments

Comments
 (0)