rand.c 2.7 KB

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