1 USING: accessors alien.c-types byte-arrays
2 combinators.short-circuit continuations destructors init kernel
3 locals namespaces random windows.advapi32 windows.errors
7 TUPLE: windows-rng provider type ;
8 C: <windows-rng> windows-rng
10 TUPLE: windows-crypto-context handle ;
11 C: <windows-crypto-context> windows-crypto-context
13 M: windows-crypto-context dispose ( tuple -- )
14 handle>> 0 CryptReleaseContext win32-error=0/f ;
16 CONSTANT: factor-crypto-container "FactorCryptoContainer"
18 :: (acquire-crypto-context) ( provider type flags -- handle ret )
19 "HCRYPTPROV" <c-object> :> handle
21 factor-crypto-container
25 CryptAcquireContextW handle swap ;
27 : acquire-crypto-context ( provider type -- handle )
28 0 (acquire-crypto-context)
30 GetLastError NTE_BAD_KEYSET =
31 [ drop f ] [ win32-error-string throw ] if
36 : create-crypto-context ( provider type -- handle )
37 CRYPT_NEWKEYSET (acquire-crypto-context) win32-error=0/f *void* ;
39 ERROR: acquire-crypto-context-failed provider type ;
41 : attempt-crypto-context ( provider type -- handle )
43 [ acquire-crypto-context ]
44 [ create-crypto-context ]
45 [ acquire-crypto-context-failed ]
48 : windows-crypto-context ( provider type -- context )
49 acquire-crypto-context <windows-crypto-context> ;
51 M: windows-rng random-bytes* ( n tuple -- bytes )
53 [ provider>> ] [ type>> ] bi
54 windows-crypto-context &dispose
55 handle>> swap dup <byte-array>
56 [ CryptGenRandom win32-error=0/f ] keep
61 PROV_RSA_FULL <windows-rng> system-random-generator set-global
64 PROV_RSA_FULL <windows-rng> secure-random-generator set-global
67 ! PROV_RSA_AES <windows-rng> secure-random-generator set-global
68 ] "random.windows" add-init-hook