entropy_hardware_alt.c 930 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-01-11 02:04:49
  5. * @LastEditTime: 2020-02-19 23:53:22
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "mbedtls/entropy.h"
  9. #include "random.h"
  10. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  11. static int mbedtls_get_random(unsigned char *buf, size_t len)
  12. {
  13. int i, j;
  14. unsigned long tmp;
  15. for (i = 0; i < ((len + 3) & ~3) / 4; i++) {
  16. tmp = random_number();
  17. for (j = 0; j < 4; j++) {
  18. if ((i * 4 + j) < len) {
  19. buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8));
  20. } else {
  21. break;
  22. }
  23. }
  24. }
  25. return 0;
  26. }
  27. int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
  28. {
  29. mbedtls_get_random(output, len);
  30. *olen = len;
  31. return 0;
  32. }
  33. #endif