rand.c 3.2 KB

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