static void cgc_initialize_flag_page(void) { // Initially reserve the flag page to see where it ends up LPVOID flag_addr, rsrv_addr = VirtualAlloc(CGC_FLAG_PAGE_ADDRESS, PAGE_SIZE, MEM_RESERVE, PAGE_READWRITE); if (rsrv_addr != CGC_FLAG_PAGE_ADDRESS) { // Address was rounded down // Figure out how much more space we need to reserve intptr_t padding = CGC_FLAG_PAGE_ADDRESS - (intptr_t) rsrv_addr; // Free the old space and reserve the correct size VirtualFree(rsrv_addr, 0, MEM_RELEASE); rsrv_addr = VirtualAlloc(rsrv_addr, PAGE_SIZE + padding, MEM_RESERVE, PAGE_READWRITE); } // Allocate the flag page flag_addr = VirtualAlloc(CGC_FLAG_PAGE_ADDRESS, PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE); // Make sure it worked if (flag_addr != CGC_FLAG_PAGE_ADDRESS) { fprintf(stderr, "[!] Failed to map the flag page"); exit(1); } // Fill the flag page with bytes from the prng cgc_try_init_prng(); cgc_aes_get_bytes(cgc_internal_prng, PAGE_SIZE, flag_addr); }
int cgc_random(void *buf, cgc_size_t count, cgc_size_t *rnd_bytes) { // Get random bytes from the prng cgc_try_init_prng(); cgc_aes_get_bytes(cgc_internal_prng, count, buf); if (rnd_bytes) *rnd_bytes = count; return 0; }
static void __attribute__ ((constructor)) cgc_initialize_flag_page(void) { void *mmap_addr = mmap(CGC_FLAG_PAGE_ADDRESS, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (mmap_addr != CGC_FLAG_PAGE_ADDRESS) { err(1, "[!] Failed to map the flag page"); } // Fill the flag page with bytes from the prng try_init_prng(); cgc_aes_get_bytes(cgc_internal_prng, PAGE_SIZE, mmap_addr); }