Skip to content

crypto,uuid: use a native UUIDv4 implementation#357

Merged
saghul merged 1 commit intomasterfrom
native-uuid4
Jan 23, 2023
Merged

crypto,uuid: use a native UUIDv4 implementation#357
saghul merged 1 commit intomasterfrom
native-uuid4

Conversation

@saghul
Copy link
Copy Markdown
Owner

@saghul saghul commented Jan 23, 2023

No description provided.

@saghul
Copy link
Copy Markdown
Owner Author

saghul commented Jan 23, 2023

I still need to fixup the uuid4 library:

  • make it use libuv's random byte generator
  • make the seed thread local / a context struct?

@bnoordhuis
Copy link
Copy Markdown
Contributor

I don't know what security properties you're aiming for but that library uses xorshift128+, which is not a CSPRNG.

Maybe easiest is to obtain 16 bytes of randomness from libuv, patch in the 6 uuidv4 marker bits, and format it.

@bnoordhuis
Copy link
Copy Markdown
Contributor

I suppose I should put my money where my mouth is. :-)

What I mean is this:

// usage:
//  char s[37];
//  int err = uuidv4(&s);
int uuidv4(char (*s)[37]) {
  unsigned char u[16];
  int err;

  err = uv_random(NULL, NULL, u, sizeof(u), 0, NULL);
  if (err)
    return err;

  u[6] &= 15;
  u[6] |= 64; // '4x'

  u[8] &= 63;
  u[8] |= 128; // 0b10xxxxxx

  snprintf(*s, sizeof(*s),
           "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
           "%02x%02x-%02x%02x%02x%02x%02x%02x",
           u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7],
           u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]);

  return 0;
}

@saghul
Copy link
Copy Markdown
Owner Author

saghul commented Jan 23, 2023

Ha, even simpler! I ended up replacing the seed material with uv_random. But I like yours better :-)

Thanks @bnoordhuis for the nice and small uuid v4 implementation!
@saghul saghul marked this pull request as ready for review January 23, 2023 15:30
@saghul saghul merged commit 185be30 into master Jan 23, 2023
@saghul saghul deleted the native-uuid4 branch January 23, 2023 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants