avr.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <Arduino.h>
  2. static bool
  3. hydro_random_rbit(uint16_t x)
  4. {
  5. uint8_t x8;
  6. x8 = ((uint8_t) (x >> 8)) ^ (uint8_t) x;
  7. x8 = (x8 >> 4) ^ (x8 & 0xf);
  8. x8 = (x8 >> 2) ^ (x8 & 0x3);
  9. x8 = (x8 >> 1) ^ x8;
  10. return (bool) (x8 & 1);
  11. }
  12. static int
  13. hydro_random_init(void)
  14. {
  15. const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
  16. hydro_hash_state st;
  17. uint16_t ebits = 0;
  18. uint16_t tc;
  19. bool a, b;
  20. cli();
  21. MCUSR = 0;
  22. WDTCSR |= _BV(WDCE) | _BV(WDE);
  23. WDTCSR = _BV(WDIE);
  24. sei();
  25. hydro_hash_init(&st, ctx, NULL);
  26. while (ebits < 256) {
  27. delay(1);
  28. tc = TCNT1;
  29. hydro_hash_update(&st, (const uint8_t *) &tc, sizeof tc);
  30. a = hydro_random_rbit(tc);
  31. delay(1);
  32. tc = TCNT1;
  33. b = hydro_random_rbit(tc);
  34. hydro_hash_update(&st, (const uint8_t *) &tc, sizeof tc);
  35. if (a == b) {
  36. continue;
  37. }
  38. hydro_hash_update(&st, (const uint8_t *) &b, sizeof b);
  39. ebits++;
  40. }
  41. cli();
  42. MCUSR = 0;
  43. WDTCSR |= _BV(WDCE) | _BV(WDE);
  44. WDTCSR = 0;
  45. sei();
  46. hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
  47. hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
  48. return 0;
  49. }
  50. ISR(WDT_vect) { }