rtthread.h 694 B

123456789101112131415161718192021222324252627
  1. #include <rtthread.h>
  2. #include <hw_rng.h>
  3. #define DBG_TAG "libhydrogen"
  4. #define DBG_LVL DBG_LOG
  5. #include <rtdbg.h>
  6. static int
  7. hydro_random_init(void)
  8. {
  9. const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
  10. hydro_hash_state st;
  11. uint16_t ebits = 0;
  12. hydro_hash_init(&st, ctx, NULL);
  13. while (ebits < 256) {
  14. uint32_t r = rt_hwcrypto_rng_update();
  15. hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
  16. ebits += 32;
  17. }
  18. hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
  19. hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
  20. return 0;
  21. }