On 9/8/20 6:49 PM, Andrew Zaborowski wrote:
Add a utility to select random characters from the set defined in
v1.7 Section 3.2.1.
src/p2putil.c | 26 ++++++++++++++++++++++++++
src/p2putil.h | 2 ++
2 files changed, 28 insertions(+)
diff --git a/src/p2putil.c b/src/p2putil.c
index d0f3a444..99ab4ca0 100644
@@ -2611,3 +2611,29 @@ uint8_t *p2p_build_go_disc_req(size_t *out_len)
return p2p_build_action_frame(false, P2P_ACTION_GO_DISCOVERABILITY_REQ,
0, NULL, NULL, NULL, 0, out_len);
+ * Select a random char from the set defined in section 3.2.1:
+ * "Following "DIRECT-" the SSID shall contain two ASCII characters
+ * randomly selected with a uniform distribution from the following
+ * character set: upper case letters, lower case letters and numbers."
+ * "The WPA2-Personal pass-phrase shall contain at least eight ASCII
+ * characters randomly selected with a uniform distribution from the
+ * following character set: upper case letters, lower case letters and
+ * numbers."
+#define CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
might as well make this static char
+ static const int set_size = strlen(CHARSET);
+ static const uint32_t limit = -(0x100000000LL % set_size);
I find this written like:
limit = UINT_MAX - UINTMAX % set_size
a bit more clear.
+ uint32_t index;
+ while ((index = l_getrandom_uint32()) >= limit);
So I actually wonder if this should be an ell utility. I'm not too sure I like
the idea of hitting getrandom in a loop. The chances are pretty small but
still... Perhaps using rand_r with a random seed would be better.
But if you want to direct code, then in theory you require only 5 bits of
randomness here since the set is 62 long. You could make due with just grabbing
a single byte of randomness and you'd have 3 attempts at being in the range (low
5 bits, then 6..1, 7..2)
Maybe I'm overthinking this, what do you think?
+ return CHARSET[index % set_size];
I'm sure this syntax works, but man I find this weird ;)