|
@@ -21,28 +21,18 @@
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-// NOTE:
|
|
|
|
|
-// random32() and random_buffer() have been replaced in this implementation
|
|
|
|
|
-// with Flipper Zero specific code. The original code is commented out below.
|
|
|
|
|
|
|
+#define FLIPPER_HAL_RANDOM
|
|
|
|
|
|
|
|
#include "rand.h"
|
|
#include "rand.h"
|
|
|
|
|
|
|
|
-// Flipper Zero RNG code:
|
|
|
|
|
-#include <furi_hal_random.h>
|
|
|
|
|
-
|
|
|
|
|
-#ifndef RAND_PLATFORM_INDEPENDENT
|
|
|
|
|
|
|
+#ifdef FLIPPER_HAL_RANDOM
|
|
|
|
|
|
|
|
-// Original code:
|
|
|
|
|
-// #pragma message("NOT SUITABLE FOR PRODUCTION USE! Replace random32() function with your own secure code.")
|
|
|
|
|
|
|
+// NOTE:
|
|
|
|
|
+// random32() and random_buffer() have been replaced in this implementation
|
|
|
|
|
+// with Flipper Zero specific code. The original code is disabled by #define.
|
|
|
|
|
|
|
|
-// The following code is not supposed to be used in a production environment.
|
|
|
|
|
-// It's included only to make the library testable.
|
|
|
|
|
-// The message above tries to prevent any accidental use outside of the test
|
|
|
|
|
-// environment.
|
|
|
|
|
-//
|
|
|
|
|
-// You are supposed to replace the random8() and random32() function with your
|
|
|
|
|
-// own secure code. There is also a possibility to replace the random_buffer()
|
|
|
|
|
-// function as it is defined as a weak symbol.
|
|
|
|
|
|
|
+// Flipper Zero RNG code:
|
|
|
|
|
+#include <furi_hal_random.h>
|
|
|
|
|
|
|
|
static uint32_t seed = 0;
|
|
static uint32_t seed = 0;
|
|
|
|
|
|
|
@@ -50,14 +40,6 @@ void random_reseed(const uint32_t value) {
|
|
|
seed = value;
|
|
seed = value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Original code:
|
|
|
|
|
-// uint32_t random32(void) {
|
|
|
|
|
-// // Linear congruential generator from Numerical Recipes
|
|
|
|
|
-// // https://en.wikipedia.org/wiki/Linear_congruential_generator
|
|
|
|
|
-// seed = 1664525 * seed + 1013904223;
|
|
|
|
|
-// return seed;
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
// Flipper Zero RNG code:
|
|
// Flipper Zero RNG code:
|
|
|
uint32_t random32(void) {
|
|
uint32_t random32(void) {
|
|
|
return furi_hal_random_get();
|
|
return furi_hal_random_get();
|
|
@@ -68,22 +50,41 @@ void random_buffer(uint8_t* buf, size_t len) {
|
|
|
furi_hal_random_fill_buf(buf, len);
|
|
furi_hal_random_fill_buf(buf, len);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#endif /* RAND_PLATFORM_INDEPENDENT */
|
|
|
|
|
|
|
+#else /* PLATFORM INDEPENDENT */
|
|
|
|
|
+
|
|
|
|
|
+#pragma message("NOT SUITABLE FOR PRODUCTION USE! Replace random32() function with your own secure code.")
|
|
|
|
|
+
|
|
|
|
|
+// The following code is not supposed to be used in a production environment.
|
|
|
|
|
+// It's included only to make the library testable.
|
|
|
|
|
+// The message above tries to prevent any accidental use outside of the test
|
|
|
|
|
+// environment.
|
|
|
|
|
+//
|
|
|
|
|
+// You are supposed to replace the random8() and random32() function with your
|
|
|
|
|
+// own secure code. There is also a possibility to replace the random_buffer()
|
|
|
|
|
+// function as it is defined as a weak symbol.
|
|
|
|
|
|
|
|
//
|
|
//
|
|
|
// The following code is platform independent
|
|
// The following code is platform independent
|
|
|
//
|
|
//
|
|
|
|
|
|
|
|
-// Original code:
|
|
|
|
|
-// void __attribute__((weak)) random_buffer(uint8_t *buf, size_t len) {
|
|
|
|
|
-// uint32_t r = 0;
|
|
|
|
|
-// for (size_t i = 0; i < len; i++) {
|
|
|
|
|
-// if (i % 4 == 0) {
|
|
|
|
|
-// r = random32();
|
|
|
|
|
-// }
|
|
|
|
|
-// buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+uint32_t random32(void) {
|
|
|
|
|
+ // Linear congruential generator from Numerical Recipes
|
|
|
|
|
+ // https://en.wikipedia.org/wiki/Linear_congruential_generator
|
|
|
|
|
+ seed = 1664525 * seed + 1013904223;
|
|
|
|
|
+ return seed;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void __attribute__((weak)) random_buffer(uint8_t *buf, size_t len) {
|
|
|
|
|
+ uint32_t r = 0;
|
|
|
|
|
+ for (size_t i = 0; i < len; i++) {
|
|
|
|
|
+ if (i % 4 == 0) {
|
|
|
|
|
+ r = random32();
|
|
|
|
|
+ }
|
|
|
|
|
+ buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#endif /* FLIPPER_HAL_RANDOM */
|
|
|
|
|
|
|
|
uint32_t random_uniform(uint32_t n) {
|
|
uint32_t random_uniform(uint32_t n) {
|
|
|
uint32_t x = 0, max = 0xFFFFFFFF - (0xFFFFFFFF % n);
|
|
uint32_t x = 0, max = 0xFFFFFFFF - (0xFFFFFFFF % n);
|