static int ALL_RRI_GenerateBlock(byte *output, size_t size, unsigned int safety) { CRYPTOPP_ASSERT((output && size) || !(output || size)); #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 word32 val; #else word64 val; #endif while (size >= sizeof(val)) { #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step((word32*)output)) #else // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 if (_rdrand64_step(reinterpret_cast<unsigned long long*>(output))) #endif { output += sizeof(val); size -= sizeof(val); } else { if (!safety--) { CRYPTOPP_ASSERT(0); return 0; } } } if (size) { #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step(&val)) #else // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 if (_rdrand64_step(reinterpret_cast<unsigned long long*>(&val))) #endif { memcpy(output, &val, size); size = 0; } else { if (!safety--) { CRYPTOPP_ASSERT(0); return 0; } } } SecureWipeBuffer(&val, 1); return int(size == 0); }
static int ALL_RRI_GenerateBlock(byte *output, size_t size, unsigned int safety) { assert((output && size) || !(output || size)); #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 word32 val; #else word64 val; #endif while (size >= sizeof(val)) { #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step((word32*)output)) #else if (_rdrand64_step((word64*)output)) #endif { output += sizeof(val); size -= sizeof(val); } else { if (!safety--) { assert(0); return 0; } } } if (size) { #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step(&val)) #else if (_rdrand64_step(&val)) #endif { memcpy(output, &val, size); size = 0; } else { if (!safety--) { assert(0); return 0; } } } SecureWipeBuffer(&val, 1); return int(size == 0); }
uint32_t RDRAND_RNG::rdrand_status(bool& ok) { ok = false; uint32_t r = 0; for(size_t i = 0; i != BOTAN_ENTROPY_RDRAND_RETRIES; ++i) { #if defined(BOTAN_USE_GCC_INLINE_ASM) int cf = 0; // Encoding of rdrand %eax asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" : "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc"); #else int cf = _rdrand32_step(&r); #endif if(1 == cf) { ok = true; break; } } return r; }
int main(void) { unsigned int x; return(!_rdrand32_step(&x)); }
bool rdrand32(uint32_t* result) { int res = 0; while (res == 0) { res = _rdrand32_step(result); } return (res == 1); }
inline bool rdrand( UIntType *rand, std::integral_constant<std::size_t, sizeof(std::uint32_t)>) { unsigned r; int cf = _rdrand32_step(&r); *rand = static_cast<UIntType>(r); return cf != 0; }
/* * @func init_random_iv initiates iv with a random number * @param OUT uint8_t* iv, pointer to output random IV * @return encip_ret_e: * ENCIP_ERROR_RANDIV_INVALID_PARAM if iv is NULL * ENCIP_ERROR_RDRAND_NOT_SUPPORTED if platform does not support RDRAND * ENCIP_ERROR_RDRAND_FAILED if random number gneration fails * ENCIP_SUCCESS if successfull */ static encip_ret_e init_random_iv(OUT uint8_t* iv) { if(NULL == iv) return ENCIP_ERROR_RANDIV_INVALID_PARAM; if(rdrand_supported()) { uint32_t* ivp = (uint32_t*)iv; // Get a random IV for encryption: for(uint32_t i=0;i < SGX_AESGCM_IV_SIZE / sizeof(uint32_t);i++) { uint32_t randval = 0; int rdrand32ret = _rdrand32_step(&randval); if(RDRAND_SUCCESS != rdrand32ret) return ENCIP_ERROR_RDRAND_FAILED; *ivp = randval; ivp++; } } else { return ENCIP_ERROR_RDRAND_NOT_SUPPORTED; } return ENCIP_SUCCESS; }
int foo (unsigned int *x) { return _rdrand32_step (x); }
inline int rdrand32_step(uint32_t *x) { return _rdrand32_step ( (unsigned int*) x ); }