rand.c 3.1 KB

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