rand.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #define FLIPPER_HAL_RANDOM
  24. #include "rand.h"
  25. #ifdef FLIPPER_HAL_RANDOM
  26. // NOTE:
  27. // random32() and random_buffer() have been replaced in this implementation
  28. // with Flipper Zero specific code. The original code is disabled by #define.
  29. // Flipper Zero RNG code:
  30. #include <furi_hal_random.h>
  31. static uint32_t seed = 0;
  32. void random_reseed(const uint32_t value) {
  33. seed = value;
  34. }
  35. // Flipper Zero RNG code:
  36. uint32_t random32(void) {
  37. return furi_hal_random_get();
  38. }
  39. // Flipper Zero RNG code:
  40. void random_buffer(uint8_t* buf, size_t len) {
  41. furi_hal_random_fill_buf(buf, len);
  42. }
  43. #else /* PLATFORM INDEPENDENT */
  44. #pragma message( \
  45. "NOT SUITABLE FOR PRODUCTION USE! Replace random32() function with your own secure code.")
  46. // The following code is not supposed to be used in a production environment.
  47. // It's included only to make the library testable.
  48. // The message above tries to prevent any accidental use outside of the test
  49. // environment.
  50. //
  51. // You are supposed to replace the random8() and random32() function with your
  52. // own secure code. There is also a possibility to replace the random_buffer()
  53. // function as it is defined as a weak symbol.
  54. //
  55. // The following code is platform independent
  56. //
  57. uint32_t random32(void) {
  58. // Linear congruential generator from Numerical Recipes
  59. // https://en.wikipedia.org/wiki/Linear_congruential_generator
  60. seed = 1664525 * seed + 1013904223;
  61. return seed;
  62. }
  63. void __attribute__((weak)) random_buffer(uint8_t* buf, size_t len) {
  64. uint32_t r = 0;
  65. for(size_t i = 0; i < len; i++) {
  66. if(i % 4 == 0) {
  67. r = random32();
  68. }
  69. buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
  70. }
  71. }
  72. #endif /* FLIPPER_HAL_RANDOM */
  73. uint32_t random_uniform(uint32_t n) {
  74. uint32_t x = 0, max = 0xFFFFFFFF - (0xFFFFFFFF % n);
  75. while((x = random32()) >= max)
  76. ;
  77. return x / (max / n);
  78. }
  79. void random_permute(char* str, size_t len) {
  80. for(int i = len - 1; i >= 1; i--) {
  81. int j = random_uniform(i + 1);
  82. char t = str[j];
  83. str[j] = str[i];
  84. str[i] = t;
  85. }
  86. }