1*0eefd307SCy Schubert /* $OpenBSD: getentropy_win.c,v 1.5 2016/08/07 03:27:21 tb Exp $ */
2ff825849SDag-Erling Smørgrav
3ff825849SDag-Erling Smørgrav /*
4ff825849SDag-Erling Smørgrav * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
5ff825849SDag-Erling Smørgrav * Copyright (c) 2014, Bob Beck <beck@obtuse.com>
6ff825849SDag-Erling Smørgrav *
7ff825849SDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
8ff825849SDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
9ff825849SDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
10ff825849SDag-Erling Smørgrav *
11ff825849SDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12ff825849SDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13ff825849SDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14ff825849SDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15ff825849SDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16ff825849SDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17ff825849SDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0eefd307SCy Schubert *
19*0eefd307SCy Schubert * Emulation of getentropy(2) as documented at:
20*0eefd307SCy Schubert * http://man.openbsd.org/getentropy.2
21ff825849SDag-Erling Smørgrav */
22ff825849SDag-Erling Smørgrav
23ff825849SDag-Erling Smørgrav #include <windows.h>
24ff825849SDag-Erling Smørgrav #include <errno.h>
25ff825849SDag-Erling Smørgrav #include <stdint.h>
26ff825849SDag-Erling Smørgrav #include <sys/types.h>
27ff825849SDag-Erling Smørgrav #include <wincrypt.h>
28ff825849SDag-Erling Smørgrav #include <process.h>
29ff825849SDag-Erling Smørgrav
30ff825849SDag-Erling Smørgrav int getentropy(void *buf, size_t len);
31ff825849SDag-Erling Smørgrav
32ff825849SDag-Erling Smørgrav /*
33ff825849SDag-Erling Smørgrav * On Windows, CryptGenRandom is supposed to be a well-seeded
34ff825849SDag-Erling Smørgrav * cryptographically strong random number generator.
35ff825849SDag-Erling Smørgrav */
36ff825849SDag-Erling Smørgrav int
getentropy(void * buf,size_t len)37ff825849SDag-Erling Smørgrav getentropy(void *buf, size_t len)
38ff825849SDag-Erling Smørgrav {
39ff825849SDag-Erling Smørgrav HCRYPTPROV provider;
40ff825849SDag-Erling Smørgrav
41ff825849SDag-Erling Smørgrav if (len > 256) {
42ff825849SDag-Erling Smørgrav errno = EIO;
43*0eefd307SCy Schubert return (-1);
44ff825849SDag-Erling Smørgrav }
45ff825849SDag-Erling Smørgrav
46ff825849SDag-Erling Smørgrav if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
47ff825849SDag-Erling Smørgrav CRYPT_VERIFYCONTEXT) == 0)
48ff825849SDag-Erling Smørgrav goto fail;
49ff825849SDag-Erling Smørgrav if (CryptGenRandom(provider, len, buf) == 0) {
50ff825849SDag-Erling Smørgrav CryptReleaseContext(provider, 0);
51ff825849SDag-Erling Smørgrav goto fail;
52ff825849SDag-Erling Smørgrav }
53ff825849SDag-Erling Smørgrav CryptReleaseContext(provider, 0);
54ff825849SDag-Erling Smørgrav return (0);
55ff825849SDag-Erling Smørgrav
56ff825849SDag-Erling Smørgrav fail:
57ff825849SDag-Erling Smørgrav errno = EIO;
58ff825849SDag-Erling Smørgrav return (-1);
59ff825849SDag-Erling Smørgrav }
60