static int seed_from_urandom(uint32_t *seed) { /* Use unbuffered I/O if we have open(), close() and read(). Otherwise fall back to fopen() */ char data[sizeof(uint32_t)]; int ok; #if defined(HAVE_OPEN) && defined(HAVE_CLOSE) && defined(HAVE_READ) int urandom; urandom = open("/dev/urandom", O_RDONLY); if (urandom == -1) return 1; ok = read(urandom, data, sizeof(uint32_t)) == sizeof(uint32_t); close(urandom); #else FILE *urandom; urandom = fopen("/dev/urandom", "rb"); if (!urandom) return 1; ok = fread(data, 1, sizeof(uint32_t), urandom) == sizeof(uint32_t); fclose(urandom); #endif if (!ok) return 1; *seed = buf_to_uint32(data); return 0; }
static int seed_from_windows_cryptoapi(uint32_t *seed) { HINSTANCE hAdvAPI32 = NULL; CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; CRYPTGENRANDOM pCryptGenRandom = NULL; CRYPTRELEASECONTEXT pCryptReleaseContext = NULL; HCRYPTPROV hCryptProv = 0; BYTE data[sizeof(uint32_t)]; int ok; hAdvAPI32 = GetModuleHandle("advapi32.dll"); if(hAdvAPI32 == NULL) return 1; pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA"); if (!pCryptAcquireContext) return 1; pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom"); if (!pCryptGenRandom) return 1; pCryptReleaseContext = (CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext"); if (!pCryptReleaseContext) return 1; if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return 1; ok = CryptGenRandom(hCryptProv, sizeof(uint32_t), data); pCryptReleaseContext(hCryptProv, 0); if (!ok) return 1; *seed = buf_to_uint32((char *)data); return 0; }
/* * Read IO expanders pins: * - Switch IRQ, * - Wake and detect pins from U96 and U90. * * Note: reading the state of the IO expanders de-asserts the #INT line */ void ioexp_read_iopins(void) { uint8_t msg[4]; uint32_t io_exp_chg; int i; dbg_info("%s()\n", __func__); /* Read 24 bits from U96 */ msg[0] = 0x80; msg[1] = 0x00; msg[2] = 0x00; msg[3] = 0x00; i2c_ioexp_read(msg, 3, I2C_ADDR_IOEXP_U96); /* Read 8 bits from U90 */ msg[3] = 0x00; i2c_ioexp_read(&msg[3], 1, I2C_ADDR_IOEXP_U90); /* Store the state of the 32 signals */ io_exp_state = buf_to_uint32(msg); /* Detect state changes */ io_exp_chg = io_exp_state ^ io_exp_last_state; if (io_exp_chg) { dbg_info("%s(): IO EXP 0x%04x\n", __func__, io_exp_state); for (i = 0; i < 32; i++) { if (io_exp_chg & (1 << i)) { if (i == IO_EXP_SWITCH_IRQ) dbg_info("%s(): *** Switch IRQ ***\n", __func__); dbg_info("%s(): bit%02d <- %d\n", __func__, i, !!(io_exp_state & (1 << i))); } } io_exp_last_state = io_exp_state; } }