How generate REAL random number using STM32 MCU?

后端 未结 4 773
挽巷
挽巷 2021-01-12 13:09

I\'m working on a project with STM32F103E arm cortex-m3 MCU in keil microvision IDE.
I need to generate random numbers for some purposes, but I don\'t want to use pseud

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 13:51

    As pointed out, the chip does not have a hardware RNG.

    But you can roll your own. The usual approach is to measure jitter between INDEPENDENT clocks. Independent means that the two clocks are backed by different christals or RC-oscillators and not derived from the same.

    I would use:

    • SysTick timer / counter derived from system clock (MHz range)
    • One of the kHz-range RC oscillators

    Set up a counter on the kHz-range RC oscillator to give you an interrupt several times a second. In the interrupt handler you read the current value of the SysTick counter. Whether or not SysTick is used for other purposes (scheduling), the lower 5 or so bits are by all means unpredictable.

    For getting random numbers out of this, use a normal pseudo RNG. Use the entropy gathered above to unpredictably mutate the internal state of the pseudo RNG. For key generation, don't read all the bits at once but allow for a couple of mutations to happen.

    Attacks against this are obvious: If the attacker can measure or control the kHz-range RC oscillator up to MHz precision, the randomness goes away. If you are worried about that, use a smart card or other security co-processor.

提交回复
热议问题