unsigned int rdrand_get_n_uints (unsigned int n, unsigned int *dest) { unsigned int i; uint64_t *qptr= (uint64_t *) dest; unsigned int total_uints= 0; unsigned int qwords= n/2; for (i= 0; i< qwords; ++i, ++qptr) { if ( rdrand64_retry(RDRAND_RETRIES, qptr) ) { total_uints+= 2; } else { return total_uints; } } /* Fill the residual */ if ( n%2 ) { unsigned int *uptr= (unsigned int *) qptr; if ( rdrand32_step(uptr) ) { ++total_uints; } } return total_uints; }
size_t rdrand_get_uint32_array_retry(uint32_t *dest, size_t size, int retry_limit) { int rc; int retry_count; size_t generated_32 = 0; size_t generated_64 = 0; size_t count_64 = size / 2;; size_t count_32 = size - 2 * count_64; size_t i; uint32_t x_32; uint64_t x_64; uint64_t* dest_64; if ( retry_limit < 0 ) retry_limit = RETRY_LIMIT; if ( count_32 > 0 ) { retry_count = 0; do { rc=rdrand32_step( &x_32 ); ++retry_count; } while((rc == 0) || (retry_count < retry_limit)); if (rc == 1) { *dest = x_32; ++dest; ++generated_32; } else { return generated_32; } } dest_64 = (uint64_t* ) dest; for ( i=0; i<count_64; ++i) { retry_count = 0; do { rc=rdrand64_step( &x_64 ); ++retry_count; } while((rc == 0) || (retry_count < retry_limit)); if (rc == 1) { *dest_64 = x_64; ++dest_64; ++generated_64; } else { generated_32 += 2 * generated_64; return generated_32; } } generated_32 += 2 * generated_64; return generated_32; }
END_TEST START_TEST (rdrand_step_32_stub) { unsigned char dst[DEST_SIZE] = {0}; ck_assert_int_eq (rdrand32_step((uint32_t *)&dst),RDRAND_SUCCESS); // test if it wrote just into the place it should ck_assert(test_zeros(dst, DEST_SIZE, 4, DEST_SIZE)); // test if it set all to 1 ck_assert(test_ones(dst, DEST_SIZE, 0, 4)); }
unsigned int rdrand_get_n_uints (unsigned int n, unsigned int *dest) { unsigned int i; uint32_t *lptr= (uint32_t *) dest; for (i= 0; i< n; ++i, ++dest) { if ( ! rdrand32_step(dest) ) { return i; } } return n; }
int rdrand32_retry (unsigned int retries, uint32_t *rand) { unsigned int count= 0; while ( count <= retries ) { if ( rdrand32_step(rand) ) { return 1; } ++count; } return 0; }
int fill_uint32_step(uint32_t* buf, int size, int retry_limit) { int j,k; int rc; for (j=0; j<size; ++j) { k = 0; do { rc = rdrand32_step (&buf[j] ); ++k; } while ( rc == RDRAND_FAILURE && k < retry_limit); } return size; }
int rdrand_get_uint32_retry(uint32_t *dest, int retry_limit) { int rc; int count; uint32_t x; if ( retry_limit < 0 ) retry_limit = RETRY_LIMIT; count = 0; do { rc=rdrand32_step( &x ); ++count; } while((rc == 0) || (count < retry_limit)); if (rc == 1) { *dest = x; return 1; } else { return 0; } }