Skip to content

Commit 594e93a

Browse files
authored
macOS use SecRandomCopyBytes instead of getentropy (#20466)
* On macOS use SecRandomCopyBytes instead of getentropy (which is only available on macOS 10.12+) * Change passL to passl
1 parent 10355cb commit 594e93a

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`_ |
@@ -66,7 +66,7 @@ when defined(nimPreviewSlimSystem):
6666
import std/assertions
6767

6868
const
69-
batchImplOS = defined(freebsd) or defined(openbsd) or defined(zephyr) or (defined(macosx) and not defined(ios))
69+
batchImplOS = defined(freebsd) or defined(openbsd) or defined(zephyr)
7070
batchSize {.used.} = 256
7171

7272
when batchImplOS:
@@ -231,8 +231,8 @@ elif defined(freebsd):
231231
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
232232
result = getrandom(p, csize_t(size), 0)
233233

234-
elif defined(ios):
235-
{.passL: "-framework Security".}
234+
elif defined(ios) or defined(macosx):
235+
{.passl: "-framework Security".}
236236

237237
const errSecSuccess = 0 ## No error.
238238

@@ -254,19 +254,6 @@ elif defined(ios):
254254

255255
result = secRandomCopyBytes(nil, csize_t(size), addr dest[0])
256256

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

0 commit comments

Comments
 (0)