upstream: https://github.com/mirage/mirage-crypto
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #261 from jonahbeckford/feature-android-compile

Fixes for the less common platforms

authored by

Hannes Mehnert and committed by
GitHub
1dd770d0 cadf0e12

+21 -5
+6
CHANGES.md
··· 1 + ## Pending 2 + 3 + * Use arc4random_buf instead of getrandom on Android before getrandom 4 + became available in API 28 (#259 @jonahbeckford) 5 + * Define fill_bytes for MSVC (#259 @jonahbeckford) 6 + 1 7 ## v2.0.0 (2025-02-03) 2 8 3 9 * Remove now superfluous mirage-crypto-rng-{eio,lwt,async} packages
+8 -1
rng/unix/mc_getrandom_stubs.c
··· 9 9 #include <caml/unixsupport.h> 10 10 #include <caml/bigarray.h> 11 11 12 - #if defined(__linux) || defined(__GNU__) 12 + #if defined(__ANDROID_API__) && __ANDROID_API__ < 28 13 + // on Android 27 and earlier, we use Google's <sys/random.h> recommended arc4random_buf 14 + # include <stdlib.h> 15 + 16 + void raw_getrandom (uint8_t *data, size_t len) { 17 + arc4random_buf(data, len); 18 + } 19 + #elif defined(__linux) || defined(__GNU__) 13 20 # include <errno.h> 14 21 // on Linux and GNU/Hurd, we use getrandom and loop 15 22
+7 -4
src/native/entropy_cpu_stubs.c
··· 15 15 #define random_t unsigned long long 16 16 #define _rdseed_step _rdseed64_step 17 17 #define _rdrand_step _rdrand64_step 18 - #define fill_bytes(buf, off, data) memcpy(_bp_uint8_off(buf, off), data, 8) 18 + #define fill_bytes(buf, bufsz, off, data) memcpy(_bp_uint8_off(buf, off), data, 8) 19 19 20 20 #elif defined (__i386__) 21 21 #define random_t unsigned int 22 22 #define _rdseed_step _rdseed32_step 23 23 #define _rdrand_step _rdrand32_step 24 - #define fill_bytes(buf, off, data) memcpy(_bp_uint8_off(buf, off), data, 4) 24 + #define fill_bytes(buf, bufsz, off, data) memcpy(_bp_uint8_off(buf, off), data, 4) 25 25 26 26 #endif 27 27 #endif /* __i386__ || __x86_64__ */ ··· 44 44 45 45 #if defined (_MSC_VER) 46 46 #include <immintrin.h> 47 + #include <memory.h> 47 48 48 49 #if defined (_WIN64) 49 50 #define random_t unsigned long long 50 51 #define _rdseed_step _rdseed64_step 51 52 #define _rdrand_step _rdrand64_step 53 + #define fill_bytes(buf, bufsz, off, data) memcpy_s(_bp_uint8_off(buf, off), bufsz, data, 8) 52 54 53 55 #elif defined (_WIN32) 54 56 #define random_t unsigned int 55 57 #define _rdseed_step _rdseed32_step 56 58 #define _rdrand_step _rdrand32_step 59 + #define fill_bytes(buf, bufsz, off, data) memcpy_s(_bp_uint8_off(buf, off), bufsz, data, 4) 57 60 #endif 58 61 59 62 #endif /* _MSC_VER */ ··· 235 238 int ok = 0; 236 239 int i = 100; 237 240 do { ok = _rdseed_step (&r); _mm_pause (); } while ( !(ok | !--i) ); 238 - fill_bytes(buf, off, &r); 241 + fill_bytes(buf, sizeof(r), off, &r); 239 242 return Val_bool (ok); 240 243 #else 241 244 /* ARM: CPU-assisted randomness here. */ ··· 251 254 int ok = 0; 252 255 int i = 10; 253 256 do { ok = _rdrand_step (&r); } while ( !(ok | !--i) ); 254 - fill_bytes(buf, off, &r); 257 + fill_bytes(buf, sizeof(r), off, &r); 255 258 return Val_bool (ok); 256 259 #else 257 260 /* ARM: CPU-assisted randomness here. */