rtthread.h 900 B

12345678910111213141516171819202122232425262728293031323334353637
  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. hydrogen_init(void) {
  8. if (hydro_init() != 0) {
  9. LOG_E("libhydrogen failed to initialize");
  10. }
  11. LOG_I("libhydrogen initialized");
  12. return 0;
  13. }
  14. INIT_APP_EXPORT(hydrogen_init);
  15. static int
  16. hydro_random_init(void)
  17. {
  18. const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
  19. hydro_hash_state st;
  20. uint16_t ebits = 0;
  21. hydro_hash_init(&st, ctx, NULL);
  22. while (ebits < 256) {
  23. uint32_t r = rt_hwcrypto_rng_update();
  24. hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
  25. ebits += 32;
  26. }
  27. hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
  28. hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
  29. return 0;
  30. }