You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

79 lines
1.8 KiB

  1. /**
  2. * WARNING
  3. *
  4. * This file generates a PREDICTABLE and NOT AT ALL RANDOM sequence of bytes.
  5. *
  6. * Its purpose is to support our testing suite and it MUST NOT be used in any
  7. * scenario where you are expecting actual cryptography to happen.
  8. */
  9. #include "randombytes.h"
  10. #include <stdint.h>
  11. static uint32_t seed[32] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3,
  12. 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5
  13. };
  14. static uint32_t in[12];
  15. static uint32_t out[8];
  16. static int32_t outleft = 0;
  17. #define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b))))
  18. #define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b));
  19. static void surf(void) {
  20. uint32_t t[12];
  21. uint32_t x;
  22. uint32_t sum = 0;
  23. int32_t r;
  24. int32_t i;
  25. int32_t loop;
  26. for (i = 0; i < 12; ++i) {
  27. t[i] = in[i] ^ seed[12 + i];
  28. }
  29. for (i = 0; i < 8; ++i) {
  30. out[i] = seed[24 + i];
  31. }
  32. x = t[11];
  33. for (loop = 0; loop < 2; ++loop) {
  34. for (r = 0; r < 16; ++r) {
  35. sum += 0x9e3779b9;
  36. MUSH(0, 5)
  37. MUSH(1, 7)
  38. MUSH(2, 9)
  39. MUSH(3, 13)
  40. MUSH(4, 5)
  41. MUSH(5, 7)
  42. MUSH(6, 9)
  43. MUSH(7, 13)
  44. MUSH(8, 5)
  45. MUSH(9, 7)
  46. MUSH(10, 9)
  47. MUSH(11, 13)
  48. }
  49. for (i = 0; i < 8; ++i) {
  50. out[i] ^= t[i + 4];
  51. }
  52. }
  53. }
  54. int randombytes(uint8_t *buf, size_t n) {
  55. while (n > 0) {
  56. if (!outleft) {
  57. if (!++in[0]) {
  58. if (!++in[1]) {
  59. if (!++in[2]) {
  60. ++in[3];
  61. }
  62. }
  63. }
  64. surf();
  65. outleft = 8;
  66. }
  67. *buf = (uint8_t) out[--outleft];
  68. ++buf;
  69. --n;
  70. }
  71. return 0;
  72. }