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