commit 8fe9341fccace908b5a268cab7ee8e8612ae7d28
parent 7ec6614d7e3febcfc7f35217aff44bb1a9e0394e
Author: Christian Grothoff <christian@grothoff.org>
Date: Sat, 18 Nov 2023 19:20:14 +0100
-fix includes
Diffstat:
3 files changed, 265 insertions(+), 263 deletions(-)
diff --git a/source/Makefile b/source/Makefile
@@ -0,0 +1,2 @@
+all:
+ gcc -D CL_TARGET_OPENCL_VERSION=100 -o foo rsa-test.c lib-gpu-verify.c big-int-test.c -lgcrypt -lOpenCL -lm
diff --git a/source/opencl-test.h b/source/opencl-test.h
@@ -16,7 +16,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <OpenCL/opencl.h>
+#include <CL/opencl.h>
#include "big-int-test.h"
diff --git a/source/rsa-test.c b/source/rsa-test.c
@@ -10,7 +10,7 @@
#include "openssl-test.h"
-#include <OpenCL/opencl.h>
+#include <CL/opencl.h>
#include "ctype.h"
#include "time.h"
@@ -24,8 +24,8 @@
void setup_gcry(void) {
-
-
+
+
/* Version check should be the very first call because it
makes sure that important subsystems are initialized.
#define NEED_LIBGCRYPT_VERSION to the minimum required version. */
@@ -39,7 +39,7 @@ void setup_gcry(void) {
/* ... If required, other initialization goes here. */
/* Tell Libgcrypt that initialization has completed. */
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
-
+
}
void generate_random_pairs(DIGIT_T *bases, size_t *b_len,
@@ -47,102 +47,102 @@ void generate_random_pairs(DIGIT_T *bases, size_t *b_len,
DIGIT_T *moduli, size_t *m_len,
DIGIT_T *signatures, size_t *s_len,
const unsigned int n) {
-
+
int i;
-
+
int sz = 2048 / sizeof(char);
-
+
for (i = 0; i < n; i++) {
-
+
char *template = "(genkey(rsa(nbits 4:2048)))";
gcry_sexp_t parms;
-
+
gcry_sexp_new(&parms, template, strlen(template), 1);
-
-
+
+
gcry_sexp_t key;
-
+
gcry_pk_genkey(&key,parms);
-
+
char *val = "1234567890ABCDEF"; // MARK: try random values as well
gcry_mpi_t m_mpi = gcry_mpi_new((int)strlen(val) * 8);
size_t scanned = 0;
-
+
gcry_mpi_scan(&m_mpi, GCRYMPI_FMT_HEX, val, 0, &scanned);
-
+
gcry_sexp_t toSign;
size_t errOff = 0;
char *dataformat = "(data (flags raw) (value %m))";
-
+
gcry_sexp_build(&toSign,&errOff,dataformat,m_mpi);
-
+
gcry_sexp_t resSign;
-
+
gcry_pk_sign(&resSign, toSign, key);
-
+
// these must be freed manually
gcry_mpi_t n_mpi;
gcry_mpi_t e_mpi;
-
+
gcry_sexp_extract_param(key,NULL,"n e",&n_mpi, &e_mpi, NULL);
-
+
gcry_mpi_t sig_mpi;
-
+
gcry_sexp_extract_param(resSign,NULL,"s",&sig_mpi, NULL);
-
+
char *bb = malloc(sz);
char *ee = malloc(sz);
char *ss = malloc(sz);
char *mm = malloc(sz);
-
+
size_t nL = 0;
-
+
// check returns
gcry_mpi_print(GCRYMPI_FMT_HEX,(unsigned char *)mm,sz,&nL,n_mpi); // MARK: don't use magic numbers
gcry_mpi_print(GCRYMPI_FMT_HEX,(unsigned char *)ee,sz,&nL,e_mpi);
gcry_mpi_print(GCRYMPI_FMT_HEX,(unsigned char *)ss,sz,&nL,sig_mpi);
gcry_mpi_print(GCRYMPI_FMT_HEX,(unsigned char *)bb,sz,&nL,m_mpi);
-
+
DIGIT_T base [sz*2];
DIGIT_T exponent [sz*2];
DIGIT_T modulus [sz*2];
DIGIT_T signature [sz*2];
-
+
mpSetZero(base, sz*2);
mpSetZero(exponent, sz*2);
mpSetZero(modulus, sz*2);
mpSetZero(signature, sz*2);
-
+
mpConvFromHex(base, strlen(bb), bb);
mpConvFromHex(exponent, strlen(ee), ee);
mpConvFromHex(modulus, strlen(mm), mm);
mpConvFromHex(signature, strlen(ss), ss);
-
+
size_t max_len = max( max( mpSizeof(base, sz*2), mpSizeof(modulus, sz*2) ), mpSizeof(signature, sz*2) );
-
+
b_len[i] = (i == 0 ? 0 : b_len[i - 1]) + max_len;
e_len[i] = (i == 0 ? 0 : e_len[i - 1]) + mpSizeof(exponent, sz*2);
m_len[i] = (i == 0 ? 0 : m_len[i - 1]) + max_len;
s_len[i] = (i == 0 ? 0 : s_len[i - 1]) + max_len;
-
+
memcpy(&bases[i == 0 ? 0 : b_len[i - 1]], base, ( b_len[i] - (i == 0 ? 0 : b_len[i - 1]) ) * sizeof(DIGIT_T));
memcpy(&exponents[i == 0 ? 0 : e_len[i - 1]], exponent, ( e_len[i] - (i == 0 ? 0 : e_len[i - 1]) ) * sizeof(DIGIT_T));
memcpy(&moduli[i == 0 ? 0 : m_len[i - 1]], modulus, ( m_len[i] - (i == 0 ? 0 : m_len[i - 1]) ) * sizeof(DIGIT_T));
memcpy(&signatures[i == 0 ? 0 : s_len[i - 1]], signature, ( s_len[i] - (i == 0 ? 0 : s_len[i - 1]) ) * sizeof(DIGIT_T));
-
+
gcry_free(n_mpi);
gcry_free(e_mpi);
gcry_free(m_mpi);
gcry_free(sig_mpi);
-
+
free(bb);
free(ee);
free(ss);
free(mm);
-
+
}
-
+
}
int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
@@ -150,9 +150,9 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
DIGIT_T *moduli, size_t *m_len,
DIGIT_T *signatures, size_t *s_len,
const unsigned int n) {
-
+
int err; // error code returned from api calls
-
+
size_t global; // global domain size for our calculation
size_t local; // local domain size for our calculation
@@ -161,8 +161,8 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
cl_command_queue commands; // compute command queue
cl_program program; // compute program
cl_kernel kernel; // compute kernel
-
-
+
+
// Connect to a compute device
//
int gpu = 1;
@@ -172,23 +172,23 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
printf("Error: Failed to create a device group!\n");
return EXIT_FAILURE;
}
-
+
size_t retSize_1 = 0;
clGetDeviceInfo(device_id, CL_DRIVER_VERSION, 0, NULL, &retSize_1);
char driver_version[retSize_1];
clGetDeviceInfo(device_id, CL_DRIVER_VERSION, retSize_1, &driver_version, &retSize_1);
-
+
//printf("driver version: %s\n", driver_version);
-
-
+
+
size_t retSize_2 = sizeof(cl_uint);
cl_uint address_bits = 0;
clGetDeviceInfo(device_id, CL_DEVICE_ADDRESS_BITS, 0, NULL, &retSize_2);
clGetDeviceInfo(device_id, CL_DEVICE_ADDRESS_BITS, retSize_2, &address_bits, &retSize_2);
-
+
//printf("device address bits: %i\n", address_bits);
-
-
+
+
// Create a compute context
//
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
@@ -206,15 +206,15 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
printf("Error: Failed to create a command commands!\n");
return EXIT_FAILURE;
}
-
+
// get the kernel from a file instead of a constant
-
+
FILE *fp = fopen("./verify.cl", "r");
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
rewind(fp);
-
+
char *kernelBuf = malloc(sz);
fread(kernelBuf, sizeof(char), sz, fp);
fclose(fp);
@@ -253,43 +253,43 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
}
// Create the input and output arrays in device memory for our calculation
-
+
cl_mem sig_mem;
cl_mem exp_mem;
cl_mem mod_mem;
cl_mem comp_mem;
-
+
cl_mem sig_len;
cl_mem exp_len;
cl_mem mod_len;
cl_mem comp_len;
-
-
+
+
cl_mem valid; // needs to be a buffer because it goes out
-
+
unsigned long signature_is_valid = 0;
-
+
mod_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * m_len[n-1], NULL, NULL);
exp_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * e_len[n-1], NULL, NULL);
sig_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * s_len[n-1], NULL, NULL);
comp_mem = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(DIGIT_T) * b_len[n-1], NULL, NULL); // the base, to compare whether we get the same signature
-
+
mod_len = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(size_t) * n, NULL, NULL);
exp_len = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(size_t) * n, NULL, NULL);
sig_len = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(size_t) * n, NULL, NULL);
comp_len = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(size_t) * n, NULL, NULL);
-
+
valid = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(unsigned long) ,NULL, NULL);
-
+
global = 65536; // MARK: ???
-
+
if (!sig_mem || !exp_mem || !mod_mem || !comp_mem || !valid )
{
printf("Error: Failed to allocate device memory!\n");
exit(1);
}
-
-
+
+
// Write our data set into the input array in device memory
//
err = clEnqueueWriteBuffer(commands, sig_mem, CL_TRUE, 0, sizeof(DIGIT_T) * s_len[n-1], signatures, 0, NULL, NULL);
@@ -320,7 +320,7 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
err |= clSetKernelArg(kernel, 7, sizeof(cl_mem), &comp_len);
err |= clSetKernelArg(kernel, 8, sizeof(cl_mem), &valid);
err |= clSetKernelArg(kernel, 9, sizeof(unsigned int), &n);
-
+
//err |= clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
if (err != CL_SUCCESS)
{
@@ -336,18 +336,18 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
printf("Error: Failed to retrieve kernel work group info! %d\n", err);
exit(1);
}
-
+
// Execute the kernel over the entire range of our 1d input data set
// using the maximum number of work group items for this device
//
-
+
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
if (err)
{
printf("Error: Failed to execute kernel!\n");
return EXIT_FAILURE;
}
-
+
// Wait for the command commands to get serviced before reading back results
//
clFinish(commands);
@@ -361,213 +361,213 @@ int verify_with_opencl(DIGIT_T *bases, size_t *b_len,
printf("Error: Failed to read output array! %d\n", err);
exit(1);
}
-
-
+
+
printf("VERIFICATION RESULT: %lu\n",signature_is_valid);
-
+
// Shutdown and cleanup
//
clReleaseMemObject(comp_mem);
clReleaseMemObject(exp_mem);
clReleaseMemObject(mod_mem);
clReleaseMemObject(sig_mem);
-
+
clReleaseMemObject(comp_len);
clReleaseMemObject(exp_len);
clReleaseMemObject(mod_len);
clReleaseMemObject(sig_len);
-
+
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commands);
clReleaseContext(context);
-
+
}
int rsa_tests(void) {
-
+
setup_gcry();
-
+
int gen_n_pairs = 16;
-
+
DIGIT_T *q = malloc(32768); // does not set memory to 0 on linux, keep that in mind
DIGIT_T *r = malloc(32768);
DIGIT_T *s = malloc(32768);
DIGIT_T *t = malloc(32768);
-
+
size_t *u = malloc(gen_n_pairs * sizeof(size_t));
size_t *v = malloc(gen_n_pairs * sizeof(size_t));
size_t *w = malloc(gen_n_pairs * sizeof(size_t));
size_t *x = malloc(gen_n_pairs * sizeof(size_t));
-
+
generate_random_pairs(q, u,
r, v,
s, w,
t, x, gen_n_pairs);
-
-
-
+
+
+
verify_with_opencl(q, u,
r, v,
s, w,
t, x, gen_n_pairs);
-
-
-
-
-
-
+
+
+
+
+
+
printf("____\n");
-
-
-
-
+
+
+
+
DIGIT_T *y = malloc(2048);
-
+
DIGIT_T *s_window = &s[0];
DIGIT_T *r_window = &r[0];
DIGIT_T *t_window = &t[0];
-
+
mpModExpO(y, t_window, r_window, s_window, x[0], v[0]);
-
+
size_t sz_y = x[0];
-
+
char comp[sz_y];
-
+
mpConvToHex(y, sz_y, comp, sz_y);
-
+
printf("%s",comp);
-
-
+
+
char *template = "(genkey(rsa(nbits 4:2048)))";
gcry_sexp_t parms;
-
+
gcry_sexp_new(&parms, template, strlen(template), 1);
-
+
// contains key material
gcry_sexp_t key;
-
+
gcry_pk_genkey(&key,parms);
-
+
// show_sexp("\n", key);
-
+
// create a large number, we want to encrypt it
-
-
+
+
char *val = "1234567890ABCDEFFEDCBA0987654321";
gcry_mpi_t mpi = gcry_mpi_new((int)strlen(val) * 8);
size_t scanned = 0;
-
+
gcry_mpi_scan(&mpi, GCRYMPI_FMT_HEX, val, 0, &scanned);
-
+
gcry_sexp_t toSign;
size_t errOff = 0;
char *dataformat = "(data (flags raw) (value %m))";
-
+
gcry_sexp_build(&toSign,&errOff,dataformat,mpi);
-
+
// show_sexp("\n", toSign);
-
+
// MARK: RSA verification
-
+
// use gcry_rsa_sign – without padding?
gcry_sexp_t resSign;
-
+
gcry_pk_sign(&resSign, toSign, key);
-
+
// measure time
-
+
// do the same thing with our bigNum library – do we even get the same signature?
-
+
// these must be freed manually
gcry_mpi_t n_mpi;
gcry_mpi_t e_mpi;
gcry_mpi_t d_mpi;
-
+
gcry_sexp_extract_param(key,NULL,"n e",&n_mpi, &e_mpi, NULL);
-
+
gcry_mpi_t sig_mpi;
-
+
gcry_sexp_extract_param(resSign,NULL,"s",&sig_mpi, NULL);
-
-
+
+
// may be a lot shorter – these will contain the numbers in HEX string form – for use in my bigNum
unsigned char *n = malloc(2048);
unsigned char *e = malloc(2048);
//unsigned char *d = malloc(2048);
size_t nL = 0;
-
+
// check returns
gcry_mpi_print(GCRYMPI_FMT_HEX,n,2048,&nL,n_mpi);
gcry_mpi_print(GCRYMPI_FMT_HEX,e,2048,&nL,e_mpi);
// gcry_mpi_print(GCRYMPI_FMT_HEX,d,2048,&nL,d_mpi);
-
+
unsigned char *sgn = malloc(2048);
gcry_mpi_print(GCRYMPI_FMT_HEX,sgn,2048,&nL,sig_mpi);
-
-
-
+
+
+
// openssl test
-
+
// test(e, d, n);
struct timespec t1, t2;
-
+
clock_t start, end;
double cpu_time_used;
-
+
start = clock();
-
+
// main_mmul();
-
+
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
-
+
printf("\nSign montgomery (my algorithm): %f seconds\n", cpu_time_used);
-
-
-
+
+
+
clock_gettime(CLOCK_REALTIME, &t1);
-
+
if (verify(sgn, e, n, val)) {
-
+
printf("\nverification failed\n");
-
+
} else {
-
+
printf("\nverification successful\n");
-
+
}
-
+
clock_gettime(CLOCK_REALTIME, &t2);
-
-
+
+
float seconds_1 = (t2.tv_nsec - t1.tv_nsec) / 1000;
-
+
printf("\nSign 1 (my algorithm): %f micro seconds\n", seconds_1);
-
-
+
+
clock_gettime(CLOCK_REALTIME, &t1);
// maybe we want to do more here
if (gcry_pk_verify(resSign, toSign, key)) {
-
+
printf("\nverification failed\n");
-
+
} else {
-
+
printf("\nverification successful\n");
-
+
}
-
+
clock_gettime(CLOCK_REALTIME, &t2);
float seconds_0 = (t2.tv_nsec - t1.tv_nsec) / 1000;
printf("\nSign 0 (gcrpypt): %f micro seconds\n", seconds_0);
-
+
// try to put it onto the gpu
-
+
// MARK: GPU Code
-
+
int err; // error code returned from api calls
-
+
// number of correct results returned
size_t global; // global domain size for our calculation
@@ -578,12 +578,12 @@ int rsa_tests(void) {
cl_command_queue commands; // compute command queue
cl_program program; // compute program
cl_kernel kernel; // compute kernel
-
+
//cl_mem input; // device memory used for the input array
//cl_mem output; // device memory used for the output array
-
-
-
+
+
+
// Connect to a compute device
//
int gpu = 1;
@@ -593,23 +593,23 @@ int rsa_tests(void) {
printf("Error: Failed to create a device group!\n");
return EXIT_FAILURE;
}
-
+
size_t retSize_1 = 0;
clGetDeviceInfo(device_id, CL_DRIVER_VERSION, 0, NULL, &retSize_1);
char driver_version[retSize_1];
clGetDeviceInfo(device_id, CL_DRIVER_VERSION, retSize_1, &driver_version, &retSize_1);
-
+
//printf("driver version: %s\n", driver_version);
-
-
+
+
size_t retSize_2 = sizeof(cl_uint);
cl_uint address_bits = 0;
clGetDeviceInfo(device_id, CL_DEVICE_ADDRESS_BITS, 0, NULL, &retSize_2);
clGetDeviceInfo(device_id, CL_DEVICE_ADDRESS_BITS, retSize_2, &address_bits, &retSize_2);
-
+
//printf("device address bits: %i\n", address_bits);
-
-
+
+
// Create a compute context
//
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
@@ -627,15 +627,15 @@ int rsa_tests(void) {
printf("Error: Failed to create a command commands!\n");
return EXIT_FAILURE;
}
-
+
// get the kernel from a file instead of a constant
-
+
FILE *fp = fopen("./verify.cl", "r");
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
rewind(fp);
-
+
char *kernelBuf = malloc(sz);
fread(kernelBuf, sizeof(char), sz, fp);
fclose(fp);
@@ -677,65 +677,65 @@ int rsa_tests(void) {
//
//input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * count, NULL, NULL);
//output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * count, NULL, NULL);
-
+
cl_mem s_mem;
cl_mem e_mem;
cl_mem n_mem;
-
+
cl_mem res_mem;
cl_mem valid; // needs to be a buffer because it goes out
-
+
int8_t signature_is_valid = 0;
-
+
DIGIT_T n_buf [MAX_ALLOC_SIZE*2];
DIGIT_T e_buf [MAX_ALLOC_SIZE*2];
DIGIT_T s_buf [MAX_ALLOC_SIZE*2];
-
+
DIGIT_T res_buf [MAX_ALLOC_SIZE*2];
-
+
mpSetZero(n_buf, MAX_ALLOC_SIZE*2);
mpSetZero(e_buf, MAX_ALLOC_SIZE*2);
mpSetZero(s_buf, MAX_ALLOC_SIZE*2);
-
+
mpSetZero(res_buf, MAX_ALLOC_SIZE*2);
-
+
mpConvFromHex(n_buf, strlen(n), n);
mpConvFromHex(e_buf, strlen(e), e);
mpConvFromHex(s_buf, strlen(sgn), sgn);
-
+
mpConvFromHex(res_buf, strlen(val), val);
-
+
size_t sz_n = mpSizeof(n_buf, MAX_ALLOC_SIZE*2);
size_t sz_s = mpSizeof(s_buf, MAX_ALLOC_SIZE*2);
-
-
+
+
unsigned long s_len = mpSizeof(s_buf, MAX_ALLOC_SIZE*2);
unsigned long e_len = mpSizeof(e_buf, MAX_ALLOC_SIZE*2);
unsigned long n_len = mpSizeof(n_buf, MAX_ALLOC_SIZE*2);
-
+
unsigned long res_len = mpSizeof(res_buf, MAX_ALLOC_SIZE*2);
-
+
unsigned long max_len = max(sz_s,sz_n);
-
+
n_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * n_len, NULL, NULL);
e_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * e_len, NULL, NULL);
s_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(DIGIT_T) * s_len, NULL, NULL);
-
+
res_mem = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(DIGIT_T) * res_len, NULL, NULL);
valid = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int8_t) ,NULL, NULL);
-
-
+
+
global = 4096;
//local = global;
-
-
+
+
if (!s_mem || !e_mem || !n_mem || !res_mem || !valid )
{
printf("Error: Failed to allocate device memory!\n");
exit(1);
}
-
-
+
+
// Write our data set into the input array in device memory
//
err = clEnqueueWriteBuffer(commands, s_mem, CL_TRUE, 0, s_len * sizeof(DIGIT_T), s_buf, 0, NULL, NULL);
@@ -762,7 +762,7 @@ int rsa_tests(void) {
err |= clSetKernelArg(kernel, 7, sizeof(unsigned int), &res_len);
err |= clSetKernelArg(kernel, 8, sizeof(unsigned int), &max_len);
err |= clSetKernelArg(kernel, 9, sizeof(cl_mem), &valid);
-
+
//err |= clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
if (err != CL_SUCCESS)
{
@@ -778,35 +778,35 @@ int rsa_tests(void) {
printf("Error: Failed to retrieve kernel work group info! %d\n", err);
exit(1);
}
-
-
-
-
+
+
+
+
// Execute the kernel over the entire range of our 1d input data set
// using the maximum number of work group items for this device
//
-
+
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
if (err)
{
printf("Error: Failed to execute kernel!\n");
return EXIT_FAILURE;
}
-
+
struct timespec t3, t4;
-
+
clock_gettime(CLOCK_REALTIME, &t3);
// Wait for the command commands to get serviced before reading back results
//
clFinish(commands);
-
+
clock_gettime(CLOCK_REALTIME, &t4);
-
+
float seconds_2 = (t3.tv_nsec - t4.tv_nsec) / 1000;
-
+
printf("\nGPU verification: %f micro seconds\n", seconds_2);
-
+
// Read back the results from the device to verify the output
//
@@ -817,144 +817,144 @@ int rsa_tests(void) {
printf("Error: Failed to read output array! %d\n", err);
exit(1);
}
-
-
+
+
printf("%i\n",signature_is_valid);
-
+
// Print a brief summary detailing the results
-
+
// Shutdown and cleanup
//
clReleaseMemObject(res_mem);
clReleaseMemObject(e_mem);
clReleaseMemObject(n_mem);
clReleaseMemObject(s_mem);
-
+
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commands);
clReleaseContext(context);
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
return 0;
-
+
}
/*
//void bigNum_tests(void) {
void bigNum_tests(unsigned char* nn,unsigned char* ee,unsigned char* dd) {
-
-
+
+
// MARK: BIG NUM TESTs
-
+
DIGIT_T N [MAX_ALLOC_SIZE*2];
DIGIT_T e [MAX_ALLOC_SIZE*2];
DIGIT_T d [MAX_ALLOC_SIZE*2];
-
+
DIGIT_T res [MAX_ALLOC_SIZE*2];
-
+
mpSetZero(N, MAX_ALLOC_SIZE*2);
mpSetZero(e, MAX_ALLOC_SIZE*2);
mpSetZero(d, MAX_ALLOC_SIZE*2);
-
+
mpSetZero(res, MAX_ALLOC_SIZE*2);
-
+
mpConvFromHex(N, strlen(nn), nn);
mpConvFromHex(e, strlen(ee), ee);
mpConvFromHex(d, strlen(dd), dd);
-
+
size_t sz_n = mpSizeof(N, MAX_ALLOC_SIZE*2);
size_t sz_d = mpSizeof(d, MAX_ALLOC_SIZE*2);
-
+
//mpModMult(res, e, d, N, max(sz_d,sz_n)); // that works :)
-
+
mpModExpO(res, e, d, N, max(sz_d,sz_n));
-
+
size_t sz_res = mpSizeof(res, MAX_ALLOC_SIZE*2);
-
+
char* pref = "\nHEX:\n";
char* suf = "\n";
// mpPrintHex(pref, res, sz_res, suf);
-
-
-
+
+
+
}
*/
int verify(unsigned char* sign, unsigned char* ee, unsigned char* nn, unsigned char* mm) {
-
-
+
+
DIGIT_T N [MAX_ALLOC_SIZE*2];
DIGIT_T e [MAX_ALLOC_SIZE*2];
DIGIT_T s [MAX_ALLOC_SIZE*2];
-
+
DIGIT_T res [MAX_ALLOC_SIZE*2];
-
+
mpSetZero(N, MAX_ALLOC_SIZE*2);
mpSetZero(e, MAX_ALLOC_SIZE*2);
mpSetZero(s, MAX_ALLOC_SIZE*2);
-
+
mpSetZero(res, MAX_ALLOC_SIZE*2);
-
+
mpConvFromHex(N, strlen(nn), nn);
mpConvFromHex(e, strlen(ee), ee);
mpConvFromHex(s, strlen(sign), sign);
-
+
size_t sz_n = mpSizeof(N, MAX_ALLOC_SIZE*2);
size_t sz_s = mpSizeof(s, MAX_ALLOC_SIZE*2);
-
+
//mpModMult(res, e, d, N, max(sz_d,sz_n)); // that works :)
-
+
mpModExpO(res, s, e, N, max(sz_s,sz_n), mpSizeof(e, MAX_ALLOC_SIZE*2));
-
+
size_t sz_res = mpSizeof(res, MAX_ALLOC_SIZE*2);
-
+
int sz_mm = strlen(mm) + 2;
-
+
unsigned char comp[sz_mm];
-
+
mpConvToHex(res, sz_res, comp, sz_mm);
-
+
upper(comp);
-
+
return strcmp(comp, mm);
}
/*
int verify_gmp(unsigned char* base, unsigned char* exponent, unsigned char* modulus, unsigned char* comp) {
-
-
+
+
mpz_t b;
mpz_init_set_str(b,base,16);
-
+
mpz_t e;
mpz_init_set_str(e,exponent,16);
-
+
mpz_t m;
mpz_init_set_str(m,modulus,16);
-
+
mpz_t c;
mpz_init_set_str(c,comp,16);
-
+
mpz_t res;
mpz_init(res);
-
+
mpz_powm(res, b, e, m);
-
+
char *str;
str = mpz_get_str ((char *) 0, 16, res);
-
-
+
+
return 0;
-
+
}
*/
static void show_sexp(const char *prefix, gcry_sexp_t a) {
@@ -972,9 +972,9 @@ static void show_sexp(const char *prefix, gcry_sexp_t a) {
}
static void upper(unsigned char* str) {
-
+
for(int i = 0; str[i]; i++){
str[i] = toupper(str[i]);
}
-
+
}