RandomSeed.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/seed/RandomSeed.h"
  16. #include "util/Identity.h"
  17. #include "util/log/Log.h"
  18. #include "util/Bits.h"
  19. #include "rust/cjdns_sys/Rffi.h"
  20. struct RandomSeed_pvt
  21. {
  22. RandomSeed_t pub;
  23. RandomSeed_t** rsList;
  24. int rsCount;
  25. struct Log* logger;
  26. Identity
  27. };
  28. struct RandomSeed_Buffer
  29. {
  30. uint64_t output[8];
  31. uint64_t input[8];
  32. };
  33. #define RandomSeed_Buffer_SIZE 128
  34. Assert_compileTime(sizeof(struct RandomSeed_Buffer) == RandomSeed_Buffer_SIZE);
  35. static int get(RandomSeed_t* rs, uint64_t buffer[8])
  36. {
  37. struct RandomSeed_pvt* ctx = Identity_check((struct RandomSeed_pvt*) rs);
  38. Log_info(ctx->logger, "Attempting to seed random number generator");
  39. // each provider overwrites input and output is a rolling hash.
  40. struct RandomSeed_Buffer buff = { .output = {0} };
  41. int successCount = 0;
  42. for (int i = 0; i < ctx->rsCount; i++) {
  43. if (!ctx->rsList[i]->get(ctx->rsList[i], buff.input)) {
  44. Log_info(ctx->logger, "Trying random seed [%s] Success", ctx->rsList[i]->name);
  45. Rffi_crypto_hash_sha512((uint8_t*)buff.output,
  46. (uint8_t*)&buff,
  47. RandomSeed_Buffer_SIZE);
  48. successCount++;
  49. } else {
  50. Log_info(ctx->logger, "Trying random seed [%s] Failed", ctx->rsList[i]->name);
  51. }
  52. }
  53. Assert_true(sizeof(buff.output) == 64);
  54. Bits_memcpy(buffer, buff.output, 64);
  55. if (successCount > 0) {
  56. Log_info(ctx->logger, "Seeding random number generator succeeded with [%d] sources",
  57. successCount);
  58. return 0;
  59. } else {
  60. Log_error(ctx->logger, "Seeding random number generator failed");
  61. return -1;
  62. }
  63. }
  64. int RandomSeed_get(RandomSeed_t* rs, uint64_t buffer[8])
  65. {
  66. return rs->get(rs, buffer);
  67. }
  68. RandomSeed_t* RandomSeed_new(RandomSeed_Provider* providers,
  69. int providerCount,
  70. struct Log* logger,
  71. struct Allocator* alloc)
  72. {
  73. RandomSeed_t** rsList = Allocator_calloc(alloc, sizeof(RandomSeed_t), providerCount);
  74. int i = 0;
  75. for (int j = 0; j < providerCount; j++) {
  76. RandomSeed_t* rs = providers[j](alloc);
  77. if (rs) {
  78. rsList[i++] = rs;
  79. }
  80. }
  81. Assert_true(i > 0);
  82. struct RandomSeed_pvt* out = Allocator_malloc(alloc, sizeof(struct RandomSeed_pvt));
  83. out->rsList = rsList;
  84. out->rsCount = i;
  85. out->logger = logger;
  86. out->pub.get = get;
  87. out->pub.name = "RandomSeed conglomeration of random seed providers";
  88. Identity_set(out);
  89. return &out->pub;
  90. }