aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/arith/init_random.win32.c
blob: ec7f8732e38ecadc5ad02b52f8c19b1116ceb91c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}