diff options
author | 2015-09-04 07:35:06 +0000 | |
---|---|---|
committer | 2015-09-04 07:35:06 +0000 | |
commit | ca6aa8198d2335f8c326c3dd4d26bf5899064214 (patch) | |
tree | 6274a2d971fc0cac0896efe8583927d0190e3d20 /moon-abe/pbc-0.5.14/arith/init_random.win32.c | |
parent | 92fd2dbfb672d7b2b1cdfd5dd5cf89f7716b3e12 (diff) | |
parent | 3baeb11a8fbcfcdbc31976d421f17b85503b3ecd (diff) |
Merge "init attribute-based encryption"
Diffstat (limited to 'moon-abe/pbc-0.5.14/arith/init_random.win32.c')
-rw-r--r-- | moon-abe/pbc-0.5.14/arith/init_random.win32.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/moon-abe/pbc-0.5.14/arith/init_random.win32.c b/moon-abe/pbc-0.5.14/arith/init_random.win32.c new file mode 100644 index 00000000..ec7f8732 --- /dev/null +++ b/moon-abe/pbc-0.5.14/arith/init_random.win32.c @@ -0,0 +1,52 @@ +// Win32 Compatibility Code added by Yulian Kalev and Stefan Georg Weber. +#include <stdio.h> +#include <stdint.h> // for intptr_t +#include <stdlib.h> +#include <windows.h> +#include <wincrypt.h> +#include <gmp.h> +#include "pbc_random.h" +#include "pbc_utils.h" +#include "pbc_memory.h" + +static void win32_mpz_random(mpz_t r, mpz_t limit, void *data) { + UNUSED_VAR (data); + HCRYPTPROV phProv; + unsigned int error; + if (!CryptAcquireContext(&phProv,NULL,NULL,PROV_RSA_FULL,0)) { + error = GetLastError(); + if (error == 0x80090016) { //need to create a new keyset + if (!CryptAcquireContext(&phProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET)) { + pbc_error("Couldn't create CryptContext: %x", (int)GetLastError()); + return; + } + } else { + pbc_error("Couldn't create CryptContext: %x", error); + return; + } + } + int n, bytecount, leftover; + unsigned char *bytes; + mpz_t z; + mpz_init(z); + n = mpz_sizeinbase(limit, 2); + bytecount = (n + 7) / 8; + leftover = n % 8; + bytes = (unsigned char *) pbc_malloc(bytecount); + for (;;) { + CryptGenRandom(phProv,bytecount,(byte *)bytes); + if (leftover) { + *bytes = *bytes % (1 << leftover); + } + mpz_import(z, bytecount, 1, 1, 0, 0, bytes); + if (mpz_cmp(z, limit) < 0) break; + } + CryptReleaseContext(phProv,0); + mpz_set(r, z); + mpz_clear(z); + pbc_free(bytes); +} + +void pbc_init_random(void) { + pbc_random_set_function(win32_mpz_random, NULL); +} |