I2C toy code
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

74 wiersze
3.5 KiB

  1. Tech Note 0001
  2. How to Gather Entropy on Embedded Systems
  3. Tom St Denis
  4. Introduction
  5. ------------
  6. This tech note explains a relatively simple way to gather entropy for a PRNG (Yarrow in this case) in embedded systems
  7. where there are few sources of entropy or physical sources.
  8. When trying to setup a secure random number generator a fresh source of random data (entropy) is required to ensure the
  9. deterministic state of the PRNG is not known or predetermined with respect to an attacker.
  10. At the very least the system requires one timer and one source of un-timed interrupts. by "un-timed" I mean interrupts
  11. that do not occur at regular intervals [e.g. joypad/keypad input, network packets, etc...].
  12. First we shall begin by taking an overview of how the Yarrow PRNG works within libtomcrypt. At the heart of all
  13. PRNGs is the "prng_state" data type. This is a union of structures that hold the PRNG state for the various prngs. The
  14. first thing we require is a state...
  15. prng_state myPrng;
  16. Next we must initialize the state once to get the ball rolling
  17. if (yarrow_start(&myPrng) != CRYPT_OK) {
  18. // error should never happen!
  19. }
  20. At this point the PRNG is ready to accept fresh entropy which is added with
  21. int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
  22. This function is **NOT** thread safe which will come under consideration later. To add entropy to our PRNG we must
  23. call this function with fresh data as its sampled. Lets say we have a timer counter called "uTimer" which is a 32-bit
  24. long and say a 32-bit joyPad state called "uPad". An example interrupt handler would look like
  25. void joypad_interrupt(...) {
  26. unsigned char buf[8];
  27. STORE32L(uTimer, buf);
  28. STORE32L(uPad, buf+4)
  29. if (yarrow_add_entropy(buf, 8, &myPrng) != CRYPT_OK) {
  30. // this should never occur either unless you didn't call yarrow_start
  31. }
  32. // handle interrupt
  33. }
  34. In this snippet the timer count and state of the joypad are added together into the entropy pool. The timer is important
  35. because with respect to the joypad it is a good source of entropy (on its own its not). For example, the probability of
  36. the user pushing the up arrow is fairly high, but at a specific time is not.
  37. This method doesn't gather alot of entropy and has to be used to for quite a while. One way to speed it up is to tap
  38. multiple sources. If you have a network adapter and other sources of events (keyboard, mouse, etc...) trapping their
  39. data is ideal as well. Its important to gather the timer along with the event data.
  40. As mentioned the "yarrow_add_entropy()" function is not thread safe. If your system allows interrupt handlers to be
  41. interrupted themselves then you could have trouble. One simple way is to detect when an interrupt is in progress and
  42. simply not add entropy during the call (jump over the yarrow_add_entropy() call)
  43. Once you feel that there has been enough entropy added to the pool then within a single thread you can call
  44. int yarrow_ready(prng_state *prng)
  45. Now the PRNG is ready to read via the
  46. unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng)
  47. It is a very good idea that once you call the yarrow_ready() function that you stop harvesting entropy in your interrupt
  48. functions. This will free up alot of CPU time. Also one more final note. The yarrow_read() function is not thread
  49. safe either. This means if you have multiple threads or processes that read from it you will have to add your own semaphores
  50. around calls to it.