From 3baeb11a8fbcfcdbc31976d421f17b85503b3ecd Mon Sep 17 00:00:00 2001 From: WuKong Date: Fri, 4 Sep 2015 09:25:34 +0200 Subject: init attribute-based encryption Change-Id: Iba1a3d722110abf747a0fba366f3ebc911d25b25 --- moon-abe/pbc-0.5.14/arith/init_random.win32.c | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 moon-abe/pbc-0.5.14/arith/init_random.win32.c (limited to 'moon-abe/pbc-0.5.14/arith/init_random.win32.c') 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 +#include // for intptr_t +#include +#include +#include +#include +#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); +} -- cgit 1.2.3-korg