rand_linux.odin 870 B

123456789101112131415161718192021222324252627282930313233343536
  1. package crypto
  2. import "core:fmt"
  3. import "core:sys/linux"
  4. _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1
  5. _rand_bytes :: proc (dst: []byte) {
  6. dst := dst
  7. l := len(dst)
  8. for l > 0 {
  9. to_read := min(l, _MAX_PER_CALL_BYTES)
  10. n_read, errno := linux.getrandom(dst[:to_read], {})
  11. #partial switch errno {
  12. case .NONE:
  13. // Do nothing
  14. case .EINTR:
  15. // Call interupted by a signal handler, just retry the
  16. // request.
  17. continue
  18. case .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: %v", errno))
  27. }
  28. l -= n_read
  29. dst = dst[n_read:]
  30. }
  31. }