Commit 2f8155a8 authored by Your Name's avatar Your Name
Browse files
parents 3f1030a9 5bf65d3b
#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);
}
obj-m += cryptocore_driver.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
This diff is collapsed.
/*
* cryptocore_ioctl_header.h - the header file with the ioctl definitions.
* The declarations here have to be in a header file, because
* they need to be known both the kernel module in *_driver.c
* and the application *_app.c
*/
#include <linux/ioctl.h>
// CryptoCore Struct Declarations:
typedef struct MontMult_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 a[128];
__u32 b[128];
__u32 c[128];
} MontMult_params_t;
typedef struct MontR_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 r[128];
} MontR_params_t;
typedef struct MontR2_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 r2[128];
} MontR2_params_t;
typedef struct MontExp_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 b[128];
__u32 e[128];
__u32 c[128];
} MontExp_params_t;
typedef struct ModAdd_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 a[128];
__u32 b[128];
__u32 c[128];
} ModAdd_params_t;
typedef struct ModSub_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 a[128];
__u32 b[128];
__u32 c[128];
} ModSub_params_t;
typedef struct CopyH2V_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 a[128];
__u32 acopy[128];
} CopyH2V_params_t;
typedef struct CopyV2V_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 a[128];
__u32 acopy[128];
} CopyV2V_params_t;
typedef struct CopyH2H_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 a[128];
__u32 acopy[128];
} CopyH2H_params_t;
typedef struct CopyV2H_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 a[128];
__u32 acopy[128];
} CopyV2H_params_t;
typedef struct MontMult1_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 b[128];
__u32 c[128];
} MontMult1_params_t;
typedef struct ModExp_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[128];
__u32 b[128];
__u32 e[128];
__u32 c[128];
} ModExp_params_t;
typedef struct ModRed_params{
__u32 prec;
__u32 f_sel;
__u32 sec_calc;
__u32 n[64];
__u32 a[128];
__u32 c[128];
} ModRed_params_t ;
// Add CryptoCore Struct Declarations here...
#define IOCTL_BASE 'k' // magic number
// NOTE: magic | cmdnumber | size of data to pass
#define IOCTL_SET_TRNG_CMD _IOW(IOCTL_BASE, 1, __u32)
#define IOCTL_SET_TRNG_CTR _IOW(IOCTL_BASE, 2, __u32)
#define IOCTL_SET_TRNG_TSTAB _IOW(IOCTL_BASE, 3, __u32)
#define IOCTL_SET_TRNG_TSAMPLE _IOW(IOCTL_BASE, 4, __u32)
#define IOCTL_READ_TRNG_FIFO _IOR(IOCTL_BASE, 5, __u32)
#define IOCTL_MWMAC_MONTMULT _IOWR(IOCTL_BASE, 6, MontMult_params_t)
#define IOCTL_MWMAC_MONTR _IOWR(IOCTL_BASE, 7, MontR_params_t)
#define IOCTL_MWMAC_MONTR2 _IOWR(IOCTL_BASE, 8, MontR2_params_t)
#define IOCTL_MWMAC_MONTEXP _IOWR(IOCTL_BASE, 9, MontExp_params_t)
#define IOCTL_MWMAC_MODADD _IOWR(IOCTL_BASE, 10, ModAdd_params_t)
#define IOCTL_MWMAC_MODSUB _IOWR(IOCTL_BASE, 11, ModSub_params_t)
#define IOCTL_MWMAC_COPYH2V _IOWR(IOCTL_BASE, 12, CopyH2V_params_t)
#define IOCTL_MWMAC_COPYV2V _IOWR(IOCTL_BASE, 13, CopyV2V_params_t)
#define IOCTL_MWMAC_COPYH2H _IOWR(IOCTL_BASE, 14, CopyH2H_params_t)
#define IOCTL_MWMAC_COPYV2H _IOWR(IOCTL_BASE, 15, CopyV2H_params_t)
#define IOCTL_MWMAC_MONTMULT1 _IOWR(IOCTL_BASE, 16, MontMult1_params_t)
#define IOCTL_MWMAC_MODEXP _IOWR(IOCTL_BASE, 17, ModExp_params_t)
#define IOCTL_MWMAC_MODRED _IOWR(IOCTL_BASE, 18, ModRed_params_t)
// Define further IOCTL commands here...
\ No newline at end of file
prec = 191
F = GF(2^191, 'g', modulus = x^191 + x^9 + 1)
F.inject_variables()
R.<x,y> = F[]
def str_to_poly(str):
I=Integer(str, base=16)
v=F(0)
for i in range (0,F.degree()):
if(I >> i) & 1 > 0:
v = v + g^i
return v
def poly_to_str(poly):
vec=poly._vector_()
string = ""
for i in range(0,len(vec)):
string = string + str(vec[len(vec) - i - 1])
return hex(Integer(string, base=2))
n = 0x800000000000000000000000000000000000000000000201
npoly = str_to_poly(n)
def Binary_MontMult(a,b,n,prec):
r=(2^prec).__xor__(n); rpoly = str_to_poly(r)
r2poly = rpoly*rpoly; r2 = poly_to_str(r2poly)
rinvpoly = rpoly^-1; rinv = poly_to_str(rinvpoly)
apoly = str_to_poly(a); bpoly = str_to_poly(b)
cpoly = apoly * bpoly * rinvpoly; c = poly_to_str(cpoly)
return (c,cpoly)
def Binary_MontR(n,prec):
r=(2^prec).__xor__(n); rpoly = str_to_poly(r); r = poly_to_str(rpoly)
return(r,rpoly)
def Binary_MontR2(n,prec):
r=(2^prec).__xor__(n); rpoly = str_to_poly(r);
r2poly = rpoly*rpoly; r2 = poly_to_str(r2poly)
return(r2,r2poly)
def Binary_MontExp(b,e,n,prec):
r=(2^prec).__xor__(n); rpoly = str_to_poly(r)
r2poly = rpoly*rpoly; r2 = poly_to_str(r2poly)
rinvpoly = rpoly^-1; rinv = poly_to_str(rinvpoly)
bpoly = str_to_poly(b)
xpoly = (1 * r2poly * rinvpoly)
exp = e
for i in reversed(xrange(prec)):
xpoly = xpoly * xpoly * rinvpoly
if(exp.digits(base=2,padto=prec)[i] == 1):
xpoly = bpoly * xpoly * rinvpoly
cpoly = xpoly; c = poly_to_str(cpoly)
return(c,cpoly)
def Binary_ModAdd(a,b,n,prec):
apoly = str_to_poly(a); bpoly = str_to_poly(b)
cpoly = apoly + bpoly; c = poly_to_str(cpoly)
return (c,cpoly)
def Binary_ModSub(a,b,n,prec):
apoly = str_to_poly(a); bpoly = str_to_poly(b)
cpoly = apoly + bpoly; c = poly_to_str(cpoly)
return (c,cpoly)
def Binary_MontMult1(b,n,prec):
r=(2^prec).__xor__(n); rpoly = str_to_poly(r)
r2poly = rpoly*rpoly; r2 = poly_to_str(r2poly)
rinvpoly = rpoly^-1; rinv = poly_to_str(rinvpoly)
bpoly = str_to_poly(b)
cpoly = 1 * bpoly * rinvpoly; c = poly_to_str(cpoly)
return (c,cpoly)
def Binary_ModExp(b,e,n,prec):
bpoly = str_to_poly(b)
xpoly = (1)
exp = e
for i in reversed(xrange(prec)):
xpoly = xpoly * xpoly
if(exp.digits(base=2,padto=prec)[i] == 1):
xpoly = bpoly * xpoly
cpoly = xpoly; c = poly_to_str(cpoly)
return(c,cpoly)
Markdown is supported
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