Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable random bootloader for esp #14

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/uuid4.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* under the terms of the MIT license. See LICENSE for details.
*/

#include <stdio.h>
#include <stdint.h>
#include <stdio.h>

#if defined(_WIN32)
#include <windows.h>
#include <wincrypt.h>
#include <windows.h>
#endif

#if defined(CONFIG_IDF_TARGET_ESP32)
Expand All @@ -22,8 +22,7 @@
#if (__STDC_VERSION__ >= 201112L)
_Thread_local
#endif
static uint64_t seed[2];

static uint64_t seed[2];

static uint64_t xorshift128plus(uint64_t *s) {
/* http://xorshift.di.unimi.it/xorshift128plus.c */
Expand All @@ -35,49 +34,54 @@ static uint64_t xorshift128plus(uint64_t *s) {
return s[1] + s0;
}


int uuid4_init(void) {
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
defined(__OpenBSD__) || defined(__NetBSD__)
int res;
FILE *fp = fopen("/dev/urandom", "rb");
if (!fp) {
return UUID4_EFAILURE;
}
res = fread(seed, 1, sizeof(seed), fp);
fclose(fp);
if ( res != sizeof(seed) ) {
if (res != sizeof(seed)) {
return UUID4_EFAILURE;
}

#elif defined(_WIN32)
int res;
HCRYPTPROV hCryptProv;
res = CryptAcquireContext(
&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
res = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT);
if (!res) {
return UUID4_EFAILURE;
}
res = CryptGenRandom(hCryptProv, (DWORD) sizeof(seed), (PBYTE) seed);
res = CryptGenRandom(hCryptProv, (DWORD)sizeof(seed), (PBYTE)seed);
CryptReleaseContext(hCryptProv, 0);
if (!res) {
return UUID4_EFAILURE;
}

#elif defined(CONFIG_IDF_TARGET_ESP32)
bootloader_random_enable();
for (int i = 0; i < 2; i++) {
seed[i] = ((uint64_t)esp_random() << 32) | esp_random(); // Generate random 64-bit values
seed[i] = ((uint64_t)esp_random() << 32) |
esp_random(); // Generate random 64-bit values
}

#else
#error "unsupported platform"
#error "unsupported platform"
#endif
return UUID4_ESUCCESS;
}

void uuid4_generate(char *dst) {
static const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
static const char *chars = "0123456789abcdef";
union { unsigned char b[16]; uint64_t word[2]; } s;
union {
unsigned char b[16];
uint64_t word[2];
} s;
const char *p;
int i, n;
/* get random */
Expand All @@ -90,9 +94,16 @@ void uuid4_generate(char *dst) {
n = s.b[i >> 1];
n = (i & 1) ? (n >> 4) : (n & 0xf);
switch (*p) {
case 'x' : *dst = chars[n]; i++; break;
case 'y' : *dst = chars[(n & 0x3) + 8]; i++; break;
default : *dst = *p;
case 'x':
*dst = chars[n];
i++;
break;
case 'y':
*dst = chars[(n & 0x3) + 8];
i++;
break;
default:
*dst = *p;
}
dst++, p++;
}
Expand Down