// Gives the entry-point for the application int main(int argc, const char* argv[]) { if (rdrand_supported( )){ if (argc < 2){ println( "Usage: RdRand.exe [number of random numbers to generate]" ); } println( "RDRAND is supported on this host." ); const uint32_t iterations = argc > 1 ? (uint32_t) abs( atoi( argv[1] ) ) : 32U; uint32_t counter = 0; for( uint32_t u = 0; u < iterations; u++ ){ uint32_t value = 0; if (rdrand_next( &value )){ println( "RDRAND returned: %u", value ); counter++; }else{ println( "RDRAND did not return a value." ); } } println( "Generated %u random numbers.", counter ); for (uint32_t bound = iterations, u = 0; u < bound; u++){ const uint32_t r = (uint32_t) rdrand_uniform( bound ); if (r >= bound){ println( "%u) Generated a value (%u) equal to or greater than the bound (%u)!?", u, r, bound ); break; } println( "%u) Generated %u with bound of %u", u, r, bound ); } }else{ println( "RDRAND is not supported on this host." ); } return 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; }
/* Main program, which outputs N bytes of random data. */ int main (int argc, char **argv) { /* Check arguments. */ bool valid = false; long long nbytes; if (argc == 2) { char *endptr; errno = 0; nbytes = strtoll (argv[1], &endptr, 10); if (errno) perror (argv[1]); else valid = !*endptr && 0 <= nbytes; } if (!valid) { fprintf (stderr, "%s: usage: %s NBYTES\n", argv[0], argv[0]); return 1; } /* If there's no work to do, don't worry about which library to use. */ if (nbytes == 0) return 0; /* Now that we know we have work to do, arrange to use the * appropriate library. */ unsigned long long (*rand64) (void); void* software = NULL; void* hardware = NULL; if (rdrand_supported ()) { hardware = dlopen("randlibhw.so", RTLD_NOW); char * error = dlerror(); if (error) { exit(1); } rand64 = dlsym(hardware, "hardware_rand64"); } else { software = dlopen("randlibsw.so", RTLD_NOW); char * error = dlerror(); if (error) { exit(1); } rand64 = dlsym(software, "software_rand64"); } int wordsize = sizeof rand64 (); int output_errno = 0; do { unsigned long long x = rand64 (); size_t outbytes = nbytes < wordsize ? nbytes : wordsize; if (fwrite (&x, 1, outbytes, stdout) != outbytes) { output_errno = errno; break; } nbytes -= outbytes; } while (0 < nbytes); if (fclose (stdout) != 0) output_errno = errno; if (output_errno) { errno = output_errno; perror ("output"); return 1; } return 0; }
int main (int argc, char **argv) { /* Check arguments. */ bool valid = false; long long nbytes; if (argc == 2) { char *endptr; errno = 0; nbytes = strtoll (argv[1], &endptr, 10); if (errno) perror (argv[1]); else valid = !*endptr && 0 <= nbytes; } if (!valid) { fprintf (stderr, "%s: usage: %s NBYTES\n", argv[0], argv[0]); return 1; } /* If there's no work to do, don't worry about which library to use. */ if (nbytes == 0) return 0; /* Now that we know we have work to do, arrange to use the appropriate library. */ // void (*initialize) (void); unsigned long long (*rand64) (void); //void (*finalize) (void); void *dl_handle; char *error; if (rdrand_supported ()) { dl_handle=dlopen("randlibhw.so",RTLD_LAZY); if(!dl_handle) { printf("dlopen() error - %s \n",dlerror()); return 1; } rand64 = dlsym(dl_handle,"rand64"); error=dlerror(); if(error!=NULL) { printf("dlsym hardware_rand64 - %s\n",error); return 1; } } else { dl_handle=dlopen("randlibsw.so",RTLD_LAZY); if(!dl_handle) { printf("dlopen() error - %s \n",dlerror()); return 1; } rand64 = dlsym(dl_handle,"rand64"); error=dlerror(); if(error!=NULL) { printf("dlsym software_rand64 error - %s\n",error); return 1; } /*finalize = dlsym(dl_handle,"software_rand64_fini"); error=dlerror(); if(error!=NULL) { printf("dlsym software_rand64_fini error - %s\n",error); return 1; }*/ } int wordsize = sizeof rand64 (); int output_errno = 0; do { unsigned long long x = rand64 (); size_t outbytes = nbytes < wordsize ? nbytes : wordsize; if (fwrite (&x, 1, outbytes, stdout) != outbytes) { output_errno = errno; break; } nbytes -= outbytes; } while (0 < nbytes); if (fclose (stdout) != 0) output_errno = errno; if (output_errno) { errno = output_errno; perror ("output"); dlclose(dl_handle); // finalize (); return 1; } // finalize (); dlclose(dl_handle); return 0; }
/* Main program, which outputs N bytes of random data. */ int main (int argc, char **argv) { /* Check arguments. */ bool valid = false; long long nbytes; if (argc == 2) { char *endptr; errno = 0; nbytes = strtoll (argv[1], &endptr, 10); if (errno) perror (argv[1]); else valid = !*endptr && 0 <= nbytes; } if (!valid) { fprintf (stderr, "%s: usage: %s NBYTES\n", argv[0], argv[0]); return 1; } /* If there's no work to do, don't worry about which library to use. */ if (nbytes == 0) return 0; /* Now that we know we have work to do, arrange to use the appropriate library. */ //void (*initialize) (void); unsigned long long (*rand64) (void); //void (*finalize) (void); // for dynamically loading void* lib; char* err; if (rdrand_supported ()) { //initialize = hardware_rand64_init; //rand64 = hardware_rand64; //finalize = hardware_rand64_fini; lib=dlopen("randlibhw.so",RTLD_NOW); if (lib==NULL) { fprintf(stderr,"failed to open randlibhw.so: %s\n",dlerror()); exit(1); } rand64 = dlsym(lib,"rand64"); err=dlerror(); if (err) { fprintf(stderr,"failed to locate rand64():%s\n", err); exit(1); } } else { //initialize = software_rand64_init; //rand64 = software_rand64; //finalize = software_rand64_fini; lib=dlopen("randlibsw.so",RTLD_NOW); if (lib==NULL) { fprintf(stderr,"failed to open randlibsw.so: %s\n",dlerror()); exit(1); } rand64 = dlsym(lib,"rand64"); err=dlerror(); if (err) { fprintf(stderr,"failed to locate rand64():%s\n", err); exit(1); } } //initialize (); int wordsize = sizeof rand64 (); int output_errno = 0; do { unsigned long long x = rand64 (); size_t outbytes = nbytes < wordsize ? nbytes : wordsize; if (fwrite (&x, 1, outbytes, stdout) != outbytes) { output_errno = errno; break; } nbytes -= outbytes; } while (0 < nbytes); if (fclose (stdout) != 0) output_errno = errno; if (output_errno) { errno = output_errno; perror ("output"); // finalize (); return 1; } //finalize (); return 0; }