Commit 187a1177 authored by Naga Suramouli's avatar Naga Suramouli
Browse files

Upload New File

parent 471bc341
#include <asm-generic/fcntl.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdint.h>
#include <inttypes.h>
#include "../include/cryptocore_ioctl_header.h"
/* Prototypes for functions used to access physical memory addresses */
int open_physical (int);
void close_physical (int);
static struct TRNG_params{
__u32 prec;
__u32 rand[128];
};
int main(void)
{
int dd = -1;
int ret_val;
__u32 trng_val = 0;
__u32 i = 0;
double seconds;
struct timespec tstart={0,0}, tend={0,0};
if ((dd = open_physical (dd)) == -1)
return (-1);
// Stop TRNG and clear FIFO
trng_val = 0x00000010;
ret_val = ioctl(dd, IOCTL_SET_TRNG_CMD, &trng_val);
if(ret_val != 0) {
printf("Error occured\n");
}
usleep(10);
// Configure Feedback Control Polynomial
trng_val = 0x0003ffff;
ret_val = ioctl(dd, IOCTL_SET_TRNG_CTR, &trng_val);
if(ret_val != 0) {
printf("Error occured\n");
}
// Configure Stabilisation Time
trng_val = 0x00000050;
ret_val = ioctl(dd, IOCTL_SET_TRNG_TSTAB, &trng_val);
if(ret_val != 0) {
printf("Error occured\n");
}
// Configure Sample Time
trng_val = 0x00000006;
ret_val = ioctl(dd, IOCTL_SET_TRNG_TSAMPLE, &trng_val);
if(ret_val != 0) {
printf("Error occured\n");
}
// Start TRNG
trng_val = 0x00000001;
ret_val = ioctl(dd, IOCTL_SET_TRNG_CMD, &trng_val);
if(ret_val != 0) {
printf("Error occured\n");
}
usleep(10);
struct TRNG_params TRNG_512_test = { 512,
{ } };
clock_gettime(CLOCK_MONOTONIC, &tstart);
// Read TRNG FIRO
for(i=0; i<TRNG_512_test.prec/32; i++){
ret_val = ioctl(dd, IOCTL_READ_TRNG_FIFO, &trng_val);
if(ret_val == 0) {
TRNG_512_test.rand[i] = trng_val;
} else{
printf("Error occured\n");
}
}
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("rand: ");
for(i=0; i<TRNG_512_test.prec/32; i++){
printf("%08x", TRNG_512_test.rand[i]);
}
printf("\n\n");
seconds = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
if (seconds*1000000.0 > 1000.0)
printf("Reading 512 random bits took about %.5f ms\n", seconds*1000.0);
else
printf("Reading 512 random bits took about %.5f us\n", seconds*1000000.0);
close_physical (dd); // close /dev/cryptocore
return 0;
}
// Open /dev/cryptocore, if not already done, to give access to physical addresses
int open_physical (int dd)
{
if (dd == -1)
if ((dd = open( "/dev/cryptocore", (O_RDWR | O_SYNC))) == -1)
{
printf ("ERROR: could not open \"/dev/cryptocore\"...\n");
return (-1);
}
return dd;
}
// Close /dev/mem to give access to physical addresses
void close_physical (int dd)
{
close (dd);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment