rand_linux.odin 892 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package crypto
  2. import "core:fmt"
  3. import "core:os"
  4. import "core:sys/unix"
  5. _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1
  6. _rand_bytes :: proc (dst: []byte) {
  7. dst := dst
  8. l := len(dst)
  9. for l > 0 {
  10. to_read := min(l, _MAX_PER_CALL_BYTES)
  11. ret := unix.sys_getrandom(raw_data(dst), uint(to_read), 0)
  12. if ret < 0 {
  13. switch os.Errno(-ret) {
  14. case os.EINTR:
  15. // Call interupted by a signal handler, just retry the
  16. // request.
  17. continue
  18. case os.ENOSYS:
  19. // The kernel is apparently prehistoric (< 3.17 circa 2014)
  20. // and does not support getrandom.
  21. panic("crypto: getrandom not available in kernel")
  22. case:
  23. // All other failures are things that should NEVER happen
  24. // unless the kernel interface changes (ie: the Linux
  25. // developers break userland).
  26. panic(fmt.tprintf("crypto: getrandom failed: %d", ret))
  27. }
  28. }
  29. l -= ret
  30. dst = dst[ret:]
  31. }
  32. }