libgpuverify

Signature verification on GPUs (WiP)
Log | Files | Refs | README | LICENSE

commit 5e844498e9e3724f06e24148f5f31f41f8cdfa83
parent 4bfb2795e527f9ce52f4293b4b55c69b94ecfc9a
Author: Cedric <cedric.zwahlen@students.bfh.ch>
Date:   Fri,  1 Dec 2023 07:46:36 +0100

Fix bug in parallel signature generation

Diffstat:
Msource/lib-gpu-verify.c | 35++++++++++-------------------------
Msource/rsa-test.c | 15++++++++++-----
Mxcode/lib-gpu-generate/main.c | 61++++++++++++++++++++++++++++++++++---------------------------
3 files changed, 54 insertions(+), 57 deletions(-)

diff --git a/source/lib-gpu-verify.c b/source/lib-gpu-verify.c @@ -33,28 +33,13 @@ int main(int argc, char** argv) // MARK: public facing -/* - - verify, on the gpu, a single signature - - */ -extern int gpu_verify_single(unsigned char* signature, unsigned char* prime, unsigned char* modulus, unsigned char* m); - -/* - - verify, on the gpu, several signatures - - */ -extern int gpu_verify_several(unsigned char* signatures, unsigned char* primes, unsigned char* moduli, unsigned char* ms, int n); - -//MARK: internal - - -/* - - setup OpenCL - - */ -int gpu_verify_setup_single(void); - -int gpu_verify_setup_several(void); +// more or less – this could be called in succession, and would block if the gpu is not ready +int verify_pairs_with_opencl(DIGIT_T *bases, unsigned long *b_len, + DIGIT_T *exponents, unsigned long *e_len, + DIGIT_T *moduli, unsigned long *m_len, + DIGIT_T *signatures, unsigned long *s_len, + const unsigned long *pks, + const unsigned long n); + +// this would wait for opencl +int get_result(); diff --git a/source/rsa-test.c b/source/rsa-test.c @@ -511,6 +511,7 @@ int verify_pairs_with_opencl(DIGIT_T *bases, unsigned long *b_len, size_t global; // global domain size for our calculation size_t local; // local domain size for our calculation + // MARK: this part, I can cache for the library cl_platform_id platform = select_platform(0, false); cl_device_id device_id = select_device (platform); @@ -520,6 +521,8 @@ int verify_pairs_with_opencl(DIGIT_T *bases, unsigned long *b_len, cl_kernel kernel = create_kernel (program, "several"); // Create the input and output arrays in device memory for our calculation + + // MARK: this has to be done every time cl_mem sig_mem; cl_mem exp_mem; @@ -639,6 +642,7 @@ int verify_pairs_with_opencl(DIGIT_T *bases, unsigned long *b_len, printf("KERNEL IS EXECUTING...\n"); + // MARK: we don't wait in the library, only when the cpu gives us another batch, or we want to know the result (which must also be a function of the library) // Wait for the command commands to get serviced before reading back results // @@ -717,8 +721,8 @@ int rsa_tests(void) { printf("READING KEYS...\n"); pairs_from_files(q, u, - r, v, - s, w, + r, v, + s, w, t, x, pks, &pairs); // this returns the actual amount of pairs unsigned long result = 0; @@ -820,9 +824,10 @@ int reference_tests(void) { } - gcry_sexp_t m_sexps[pairs]; - gcry_sexp_t s_sexps[pairs]; - gcry_sexp_t key_sexps[pk + 1]; + + gcry_sexp_t *m_sexps = malloc(pairs * sizeof(gcry_sexp_t)); + gcry_sexp_t *s_sexps = malloc(pairs * sizeof(gcry_sexp_t)); + gcry_sexp_t *key_sexps = malloc((pk + 1) * sizeof(gcry_sexp_t)); for (int i = 0; i < pairs; i++) { diff --git a/xcode/lib-gpu-generate/main.c b/xcode/lib-gpu-generate/main.c @@ -77,14 +77,14 @@ void *signatures_for_key(void *vargp) { size_t e_len = 0; // that is a bit much - unsigned char nn[2048]; - unsigned char ee[2048]; + unsigned char nn[1028]; + unsigned char ee[1028]; - memset(nn, 0, 2048); - memset(ee, 0, 2048); + memset(nn, 0, 1028); + memset(ee, 0, 1028); - gcry_mpi_print(GCRYMPI_FMT_HEX,nn,2048,&n_len,n_mpi); - gcry_mpi_print(GCRYMPI_FMT_HEX,ee,2048,&e_len,e_mpi); + gcry_mpi_print(GCRYMPI_FMT_HEX,nn,1028,&n_len,n_mpi); + gcry_mpi_print(GCRYMPI_FMT_HEX,ee,1028,&e_len,e_mpi); fprintf(pkfile, "%s\n",nn); fprintf(pkfile, "%s\n",ee); @@ -102,9 +102,7 @@ void *signatures_for_key(void *vargp) { char buf[len]; - gcry_mpi_t m_mpi; // = gcry_mpi_new(len * 8); // *8 because its bits not bytes - - //gcry_mpi_randomize(m_mpi, len * 8, GCRY_STRONG_RANDOM); // consider using gcry_create_nonce – it might be faster + gcry_mpi_t m_mpi; gcry_create_nonce(buf, len); @@ -132,14 +130,14 @@ void *signatures_for_key(void *vargp) { size_t s_len = 0; // that is a bit much - unsigned char mm[2048]; - unsigned char ss[2048]; + unsigned char mm[1028]; + unsigned char ss[1028]; - memset(mm, 0, 2048); - memset(ss, 0, 2048); + memset(mm, 0, 1028); + memset(ss, 0, 1028); - gcry_mpi_print(GCRYMPI_FMT_HEX,mm,2048,&m_len,m_mpi); - gcry_mpi_print(GCRYMPI_FMT_HEX,ss,2048,&s_len,s_mpi); + gcry_mpi_print(GCRYMPI_FMT_HEX,mm,1028,&m_len,m_mpi); + gcry_mpi_print(GCRYMPI_FMT_HEX,ss,1028,&s_len,s_mpi); fprintf(msfile, "%s\n",mm); fprintf(msfile, "%s\n",ss); @@ -202,10 +200,15 @@ int main(int argc, const char * argv[]) { for (int i = 0; i < pks; i++) { ids[i] = i; - pthread_create(&tid[i], NULL, signatures_for_key, (void *)&ids[i]); + int err = pthread_create(&tid[i], NULL, signatures_for_key, (void *)&ids[i]); + if ( err != 0 ) + printf("Error creating threads"); } - for (int i = 0; i < pks; i++) { - pthread_join(tid[i], NULL); + + for (int j = 0; j < pks; j++) { + int err = pthread_join(tid[j], NULL); + if ( err != 0 ) + printf("Error joining threads"); } FILE *pkfile; @@ -214,26 +217,27 @@ int main(int argc, const char * argv[]) { pkfile = fopen("publickey.txt", "w"); msfile = fopen("msgsig.txt", "w"); - for (int i = 0; i < pks; i++) { + for (int k = 0; k < pks; k++) { struct stat pk_st, ms_st; char pk_name[128]; char ms_name[128]; - sprintf(pk_name, "frag_publickey_%i.txt",i); - sprintf(ms_name, "frag_msgsig_%i.txt",i); + sprintf(pk_name, "frag_publickey_%i.txt",k); + sprintf(ms_name, "frag_msgsig_%i.txt",k); stat(pk_name, &pk_st); stat(ms_name, &ms_st); - FILE * pk_fd = fopen(pk_name, "r"); FILE * ms_fd = fopen(ms_name, "r"); - - char pk_buf[pk_st.st_size + 1]; - char ms_buf[ms_st.st_size + 1]; + void *pk_buf = malloc(pk_st.st_size + 1); + void *ms_buf = malloc(ms_st.st_size + 1); + + //char pk_buf[pk_st.st_size + 1]; + //char ms_buf[ms_st.st_size + 1]; memset(pk_buf, 0, pk_st.st_size + 1); memset(ms_buf, 0, ms_st.st_size + 1); @@ -241,8 +245,11 @@ int main(int argc, const char * argv[]) { fread(pk_buf, 1, pk_st.st_size, pk_fd); fread(ms_buf, 1, ms_st.st_size, ms_fd); - fprintf(pkfile, "%s",pk_buf); - fprintf(msfile, "%s",ms_buf); + fprintf(pkfile, "%s",(char *)pk_buf); + fprintf(msfile, "%s",(char *)ms_buf); + + free(pk_buf); + free(ms_buf); fclose(pk_fd); fclose(ms_fd);