xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/rand-w32.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: rand-w32.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include <config.h>
37ca1c9b0cSelric #include <krb5/roken.h>
38ca1c9b0cSelric 
39ca1c9b0cSelric #include <wincrypt.h>
40ca1c9b0cSelric 
41ca1c9b0cSelric #include <rand.h>
42ca1c9b0cSelric #include <heim_threads.h>
43ca1c9b0cSelric 
44ca1c9b0cSelric #include "randi.h"
45ca1c9b0cSelric 
46b9d004c6Schristos volatile static HCRYPTPROV g_cryptprovider = NULL;
47ca1c9b0cSelric 
48ca1c9b0cSelric static HCRYPTPROV
_hc_CryptProvider(void)49ca1c9b0cSelric _hc_CryptProvider(void)
50ca1c9b0cSelric {
51ca1c9b0cSelric     BOOL rv;
52b9d004c6Schristos     HCRYPTPROV cryptprovider = NULL;
53ca1c9b0cSelric 
54b9d004c6Schristos     if (g_cryptprovider != NULL)
55b9d004c6Schristos 	goto out;
56ca1c9b0cSelric 
57ca1c9b0cSelric     rv = CryptAcquireContext(&cryptprovider, NULL,
58ca1c9b0cSelric 			      MS_ENHANCED_PROV, PROV_RSA_FULL,
594f77a458Spettai 			      CRYPT_VERIFYCONTEXT);
60ca1c9b0cSelric 
61ca1c9b0cSelric     if (GetLastError() == NTE_BAD_KEYSET) {
62ca1c9b0cSelric         rv = CryptAcquireContext(&cryptprovider, NULL,
63ca1c9b0cSelric                                  MS_ENHANCED_PROV, PROV_RSA_FULL,
64ca1c9b0cSelric                                  CRYPT_NEWKEYSET);
65ca1c9b0cSelric     }
66ca1c9b0cSelric 
674f77a458Spettai     if (rv) {
684f77a458Spettai         /* try the default provider */
694f77a458Spettai         rv = CryptAcquireContext(&cryptprovider, NULL, 0, PROV_RSA_FULL,
704f77a458Spettai                                  CRYPT_VERIFYCONTEXT);
714f77a458Spettai 
724f77a458Spettai         if (GetLastError() == NTE_BAD_KEYSET) {
734f77a458Spettai             rv = CryptAcquireContext(&cryptprovider, NULL,
744f77a458Spettai                                      MS_ENHANCED_PROV, PROV_RSA_FULL,
754f77a458Spettai                                      CRYPT_NEWKEYSET);
764f77a458Spettai         }
774f77a458Spettai     }
784f77a458Spettai 
794f77a458Spettai     if (rv) {
804f77a458Spettai         /* try just a default random number generator */
814f77a458Spettai         rv = CryptAcquireContext(&cryptprovider, NULL, 0, PROV_RNG,
824f77a458Spettai                                  CRYPT_VERIFYCONTEXT);
834f77a458Spettai     }
844f77a458Spettai 
85b9d004c6Schristos     if (rv == 0 &&
86ca1c9b0cSelric         InterlockedCompareExchangePointer((PVOID *) &g_cryptprovider,
87b9d004c6Schristos 					  (PVOID) cryptprovider, NULL) != 0) {
88ca1c9b0cSelric 
89ca1c9b0cSelric         CryptReleaseContext(cryptprovider, 0);
90ca1c9b0cSelric     }
91ca1c9b0cSelric 
92b9d004c6Schristos out:
93b9d004c6Schristos     return g_cryptprovider;
94ca1c9b0cSelric }
95ca1c9b0cSelric 
96ca1c9b0cSelric /*
97ca1c9b0cSelric  *
98ca1c9b0cSelric  */
99ca1c9b0cSelric 
100ca1c9b0cSelric 
101ca1c9b0cSelric static void
w32crypto_seed(const void * indata,int size)102ca1c9b0cSelric w32crypto_seed(const void *indata, int size)
103ca1c9b0cSelric {
104ca1c9b0cSelric }
105ca1c9b0cSelric 
106ca1c9b0cSelric 
107ca1c9b0cSelric static int
w32crypto_bytes(unsigned char * outdata,int size)108ca1c9b0cSelric w32crypto_bytes(unsigned char *outdata, int size)
109ca1c9b0cSelric {
110ca1c9b0cSelric     if (CryptGenRandom(_hc_CryptProvider(), size, outdata))
111ca1c9b0cSelric 	return 1;
112ca1c9b0cSelric     return 0;
113ca1c9b0cSelric }
114ca1c9b0cSelric 
115ca1c9b0cSelric static void
w32crypto_cleanup(void)116ca1c9b0cSelric w32crypto_cleanup(void)
117ca1c9b0cSelric {
1184f77a458Spettai     HCRYPTPROV cryptprovider;
1194f77a458Spettai 
1204f77a458Spettai     if (InterlockedCompareExchangePointer((PVOID *) &cryptprovider,
1214f77a458Spettai 					  0, (PVOID) g_cryptprovider) == 0) {
1224f77a458Spettai         CryptReleaseContext(cryptprovider, 0);
1234f77a458Spettai     }
124ca1c9b0cSelric }
125ca1c9b0cSelric 
126ca1c9b0cSelric static void
w32crypto_add(const void * indata,int size,double entropi)127ca1c9b0cSelric w32crypto_add(const void *indata, int size, double entropi)
128ca1c9b0cSelric {
129ca1c9b0cSelric }
130ca1c9b0cSelric 
131ca1c9b0cSelric static int
w32crypto_status(void)132ca1c9b0cSelric w32crypto_status(void)
133ca1c9b0cSelric {
134ca1c9b0cSelric     if (_hc_CryptProvider() == 0)
135ca1c9b0cSelric 	return 0;
136ca1c9b0cSelric     return 1;
137ca1c9b0cSelric }
138ca1c9b0cSelric 
139ca1c9b0cSelric const RAND_METHOD hc_rand_w32crypto_method = {
140ca1c9b0cSelric     w32crypto_seed,
141ca1c9b0cSelric     w32crypto_bytes,
142ca1c9b0cSelric     w32crypto_cleanup,
143ca1c9b0cSelric     w32crypto_add,
144ca1c9b0cSelric     w32crypto_bytes,
145ca1c9b0cSelric     w32crypto_status
146ca1c9b0cSelric };
147ca1c9b0cSelric 
148ca1c9b0cSelric const RAND_METHOD *
RAND_w32crypto_method(void)149ca1c9b0cSelric RAND_w32crypto_method(void)
150ca1c9b0cSelric {
151ca1c9b0cSelric     return &hc_rand_w32crypto_method;
152ca1c9b0cSelric }
153