Skip to content

Commit 68b6e3b

Browse files
[libc] add windows implementation
1 parent cb85833 commit 68b6e3b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

libc/config/windows/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ set(TARGET_LIBC_ENTRYPOINTS
101101
# time.h entrypoints
102102
libc.src.time.time
103103
libc.src.time.clock_getres
104+
105+
# unistd.h entrypoints
106+
libc.src.unistd.getentropy
104107
)
105108

106109
set(TARGET_LIBM_ENTRYPOINTS

libc/config/windows/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ set(TARGET_PUBLIC_HEADERS
66
libc.include.errno
77
libc.include.fenv
88
libc.include.math
9+
libc.include.unistd
910
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_entrypoint_object(
2+
getentropy
3+
SRCS
4+
getentropy.cpp
5+
HDRS
6+
../getentropy.h
7+
DEPENDS
8+
libc.hdr.types.size_t
9+
libc.hdr.errno_macros
10+
libc.src.errno.errno
11+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Windows implementation of getentropy ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/getentropy.h"
10+
#include "hdr/errno_macros.h"
11+
#include "src/__support/common.h"
12+
#include "src/errno/libc_errno.h"
13+
14+
#define WIN32_LEAN_AND_MEAN
15+
#include <Windows.h>
16+
#include <bcrypt.h>
17+
#include <ntstatus.h>
18+
#pragma comment(lib, "bcrypt.lib")
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
23+
__try {
24+
// check the length limit
25+
if (length > 256)
26+
__leave;
27+
28+
NTSTATUS result = ::BCryptGenRandom(nullptr, static_cast<PUCHAR>(buffer),
29+
static_cast<ULONG>(length),
30+
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
31+
32+
if (result == STATUS_SUCCESS)
33+
return 0;
34+
35+
} __except (EXCEPTION_EXECUTE_HANDLER) {
36+
// no need to handle exceptions specially
37+
}
38+
39+
libc_errno = EIO;
40+
return -1;
41+
}
42+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)