]> gitweb.factorcode.org Git - factor.git/blob - basis/random/windows/windows.factor
basis: removing unnecessary method stack effects.
[factor.git] / basis / random / windows / windows.factor
1 USING: accessors alien.data byte-arrays continuations
2 destructors init kernel literals locals namespaces random
3 sequences windows.advapi32 windows.errors windows.handles
4 windows.types ;
5 IN: random.windows
6
7 TUPLE: windows-crypto-context < win32-handle provider type ;
8
9 M: windows-crypto-context dispose*
10     [ handle>> 0 CryptReleaseContext win32-error=0/f ]
11     [ f >>handle drop ] bi ;
12
13 CONSTANT: factor-crypto-container "FactorCryptoContainer"
14
15 :: (acquire-crypto-context) ( provider type flags -- handle )
16     { HCRYPTPROV } [
17         factor-crypto-container
18         provider
19         type
20         flags
21         CryptAcquireContextW win32-error=0/f
22     ] with-out-parameters ;
23
24 : acquire-crypto-context ( provider type -- handle )
25     CRYPT_MACHINE_KEYSET (acquire-crypto-context) ;
26
27 : create-crypto-context ( provider type -- handle )
28     flags{ CRYPT_MACHINE_KEYSET CRYPT_NEWKEYSET } (acquire-crypto-context) ;
29
30 ERROR: acquire-crypto-context-failed provider type error ;
31
32 : attempt-crypto-context ( provider type -- handle )
33     [ acquire-crypto-context ]
34     [
35         drop [ create-crypto-context ]
36         [ acquire-crypto-context-failed ] recover
37     ] recover ;
38
39 : initialize-crypto-context ( crypto-context -- crypto-context )
40     dup [ provider>> ] [ type>> ] bi attempt-crypto-context >>handle ;
41
42 : <windows-crypto-context> ( provider type -- windows-crypto-type )
43     windows-crypto-context new-disposable
44         swap >>type
45         swap >>provider
46         initialize-crypto-context ; inline
47
48 M: windows-crypto-context random-bytes*
49     handle>> swap dup <byte-array>
50     [ CryptGenRandom win32-error=0/f ] keep ;
51
52 ! Some Windows installations still don't work, so just set
53 ! system and secure rngs to f
54 : try-crypto-providers ( seq -- windows-crypto-context/f )
55     [
56         [ first2 <windows-crypto-context> ] attempt-all
57     ] [ 2drop f ] recover ;
58
59 [
60     {
61         ${ MS_ENHANCED_PROV PROV_RSA_FULL }
62         ${ MS_DEF_PROV PROV_RSA_FULL }
63     } try-crypto-providers system-random-generator set-global
64
65     {
66         ${ MS_STRONG_PROV PROV_RSA_FULL }
67         ${ MS_ENH_RSA_AES_PROV PROV_RSA_AES }
68     } try-crypto-providers secure-random-generator set-global
69 ] "random.windows" add-startup-hook