Skip to content

Commit 5c36f24

Browse files
iffyVarriountringabout
authored
Backport #20466 - macOS use SecRandomCopyBytes instead of getentropy (#21389)
* On macOS use SecRandomCopyBytes instead of getentropy (which is only available on macOS 10.12+) * Change passL to passl --------- Co-authored-by: Clay Sweetser <[email protected]> Co-authored-by: ringabout <[email protected]>
1 parent 1cdce31 commit 5c36f24

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

lib/std/sysrand.nim

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
## | :--- | ----: |
2121
## | Windows | `BCryptGenRandom`_ |
2222
## | Linux | `getrandom`_ |
23-
## | MacOSX | `getentropy`_ |
23+
## | MacOSX | `SecRandomCopyBytes`_ |
2424
## | iOS | `SecRandomCopyBytes`_ |
2525
## | OpenBSD | `getentropy openbsd`_ |
2626
## | FreeBSD | `getrandom freebsd`_ |
@@ -63,7 +63,7 @@ when defined(posix):
6363
import posix
6464

6565
const
66-
batchImplOS = defined(freebsd) or defined(openbsd) or defined(zephyr) or (defined(macosx) and not defined(ios))
66+
batchImplOS = defined(freebsd) or defined(openbsd) or defined(zephyr)
6767
batchSize {.used.} = 256
6868

6969
when batchImplOS:
@@ -228,8 +228,8 @@ elif defined(freebsd):
228228
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
229229
result = getrandom(p, csize_t(size), 0)
230230

231-
elif defined(ios):
232-
{.passL: "-framework Security".}
231+
elif defined(ios) or defined(macosx):
232+
{.passl: "-framework Security".}
233233

234234
const errSecSuccess = 0 ## No error.
235235

@@ -251,19 +251,6 @@ elif defined(ios):
251251

252252
result = secRandomCopyBytes(nil, csize_t(size), addr dest[0])
253253

254-
elif defined(macosx):
255-
const sysrandomHeader = """#include <Availability.h>
256-
#include <sys/random.h>
257-
"""
258-
259-
proc getentropy(p: pointer, size: csize_t): cint {.importc: "getentropy", header: sysrandomHeader.}
260-
# getentropy() fills a buffer with random data, which can be used as input
261-
# for process-context pseudorandom generators like arc4random(3).
262-
# The maximum buffer size permitted is 256 bytes.
263-
264-
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
265-
result = getentropy(p, csize_t(size)).int
266-
267254
else:
268255
template urandomImpl(result: var int, dest: var openArray[byte]) =
269256
let size = dest.len

0 commit comments

Comments
 (0)