21
21
* OTHER DEALINGS IN THE SOFTWARE.
22
22
*/
23
23
24
- // NOTE:
25
- // random32() and random_buffer() have been replaced in this implementation
26
- // with Flipper Zero specific code. The original code is commented out below.
24
+ #define FLIPPER_HAL_RANDOM
27
25
28
26
#include "rand.h"
29
27
30
- // Flipper Zero RNG code:
31
- #include <furi_hal_random.h>
32
-
33
- #ifndef RAND_PLATFORM_INDEPENDENT
28
+ #ifdef FLIPPER_HAL_RANDOM
34
29
35
- // Original code:
36
- // #pragma message("NOT SUITABLE FOR PRODUCTION USE! Replace random32() function with your own secure code.")
30
+ // NOTE:
31
+ // random32() and random_buffer() have been replaced in this implementation
32
+ // with Flipper Zero specific code. The original code is disabled by #define.
37
33
38
- // The following code is not supposed to be used in a production environment.
39
- // It's included only to make the library testable.
40
- // The message above tries to prevent any accidental use outside of the test
41
- // environment.
42
- //
43
- // You are supposed to replace the random8() and random32() function with your
44
- // own secure code. There is also a possibility to replace the random_buffer()
45
- // function as it is defined as a weak symbol.
34
+ // Flipper Zero RNG code:
35
+ #include <furi_hal_random.h>
46
36
47
37
static uint32_t seed = 0 ;
48
38
49
39
void random_reseed (const uint32_t value ) {
50
40
seed = value ;
51
41
}
52
42
53
- // Original code:
54
- // uint32_t random32(void) {
55
- // // Linear congruential generator from Numerical Recipes
56
- // // https://en.wikipedia.org/wiki/Linear_congruential_generator
57
- // seed = 1664525 * seed + 1013904223;
58
- // return seed;
59
- // }
60
-
61
43
// Flipper Zero RNG code:
62
44
uint32_t random32 (void ) {
63
45
return furi_hal_random_get ();
@@ -68,22 +50,41 @@ void random_buffer(uint8_t* buf, size_t len) {
68
50
furi_hal_random_fill_buf (buf , len );
69
51
}
70
52
71
- #endif /* RAND_PLATFORM_INDEPENDENT */
53
+ #else /* PLATFORM INDEPENDENT */
54
+
55
+ #pragma message("NOT SUITABLE FOR PRODUCTION USE! Replace random32() function with your own secure code.")
56
+
57
+ // The following code is not supposed to be used in a production environment.
58
+ // It's included only to make the library testable.
59
+ // The message above tries to prevent any accidental use outside of the test
60
+ // environment.
61
+ //
62
+ // You are supposed to replace the random8() and random32() function with your
63
+ // own secure code. There is also a possibility to replace the random_buffer()
64
+ // function as it is defined as a weak symbol.
72
65
73
66
//
74
67
// The following code is platform independent
75
68
//
76
69
77
- // Original code:
78
- // void __attribute__((weak)) random_buffer(uint8_t *buf, size_t len) {
79
- // uint32_t r = 0;
80
- // for (size_t i = 0; i < len; i++) {
81
- // if (i % 4 == 0) {
82
- // r = random32();
83
- // }
84
- // buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
85
- // }
86
- // }
70
+ uint32_t random32 (void ) {
71
+ // Linear congruential generator from Numerical Recipes
72
+ // https://en.wikipedia.org/wiki/Linear_congruential_generator
73
+ seed = 1664525 * seed + 1013904223 ;
74
+ return seed ;
75
+ }
76
+
77
+ void __attribute__((weak )) random_buffer (uint8_t * buf , size_t len ) {
78
+ uint32_t r = 0 ;
79
+ for (size_t i = 0 ; i < len ; i ++ ) {
80
+ if (i % 4 == 0 ) {
81
+ r = random32 ();
82
+ }
83
+ buf [i ] = (r >> ((i % 4 ) * 8 )) & 0xFF ;
84
+ }
85
+ }
86
+
87
+ #endif /* FLIPPER_HAL_RANDOM */
87
88
88
89
uint32_t random_uniform (uint32_t n ) {
89
90
uint32_t x = 0 , max = 0xFFFFFFFF - (0xFFFFFFFF % n );
0 commit comments