summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCedric <cedric.zwahlen@students.bfh.ch>2024-01-18 23:41:08 +0100
committerCedric <cedric.zwahlen@students.bfh.ch>2024-01-18 23:41:08 +0100
commita6b997d516086dd7d72dea67626ec815a147ff08 (patch)
tree0bb01f5558de206cc2860cb6387e09e24f812a79
parent6de36dce671c293d9b3471bbfbd9c5b5242a03f7 (diff)
downloadlibgpuverify-a6b997d516086dd7d72dea67626ec815a147ff08.tar.gz
libgpuverify-a6b997d516086dd7d72dea67626ec815a147ff08.tar.bz2
libgpuverify-a6b997d516086dd7d72dea67626ec815a147ff08.zip
Fix lib-gpu-generate segmentation fault
-rw-r--r--lib-gpu-generate/main.c161
1 files changed, 96 insertions, 65 deletions
diff --git a/lib-gpu-generate/main.c b/lib-gpu-generate/main.c
index 39aa0b7..afd08c9 100644
--- a/lib-gpu-generate/main.c
+++ b/lib-gpu-generate/main.c
@@ -1,9 +1,20 @@
-//
-// main.c
-// lib-gpu-generate
-//
-// Created by Cedric Zwahlen on 19.11.2023.
-//
+/*
+ * main.c
+ * This file is part of lib-gpu-verify.
+ *
+ * lib-gpu-verify is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * lib-gpu-verify is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * Created by Cedric Zwahlen
+ *
+ */
#include <stdio.h>
#include <gcrypt.h>
@@ -77,8 +88,8 @@ void *signatures_for_key(void *vargp) {
size_t e_len = 0;
// that is a bit much
- unsigned char nn[1028];
- unsigned char ee[1028];
+ unsigned char *nn = malloc(1028);
+ unsigned char *ee = malloc(1028);
memset(nn, 0, 1028);
memset(ee, 0, 1028);
@@ -90,6 +101,9 @@ void *signatures_for_key(void *vargp) {
fprintf(pkfile, "%s\n",ee);
fprintf(pkfile, "%lu\n",((*id + 1) * n) - 1); // this key is used for the signatures up to the index described by this number
+ free(nn);
+ free(ee);
+
gcry_mpi_release(n_mpi);
gcry_mpi_release(e_mpi);
@@ -130,8 +144,8 @@ void *signatures_for_key(void *vargp) {
size_t s_len = 0;
// that is a bit much
- unsigned char mm[1028];
- unsigned char ss[1028];
+ unsigned char *mm = malloc(1028);
+ unsigned char *ss = malloc(1028);
memset(mm, 0, 1028);
memset(ss, 0, 1028);
@@ -142,6 +156,9 @@ void *signatures_for_key(void *vargp) {
fprintf(msfile, "%s\n",mm);
fprintf(msfile, "%s\n",ss);
+ free(mm);
+ free(ss);
+
gcry_mpi_release(s_mpi);
gcry_mpi_release(m_mpi);
@@ -159,7 +176,7 @@ void *signatures_for_key(void *vargp) {
}
-
+#define INCREMENT 8
int main(int argc, const char * argv[]) {
@@ -192,81 +209,95 @@ int main(int argc, const char * argv[]) {
}
+
+
if (pks == 1) {
printf("generating %lu signatures with the same key.\n",n);
} else {
+
+ pks = pks - (pks % INCREMENT);
+
printf("generating %lu signatures with %i keys.\n",n * pks, pks);
}
gpuv_prepare_gcry();
- pthread_t tid[pks];
+ pthread_t *tid = malloc(pks * sizeof(pthread_t));
// not the best, but it is safe
int ids[pks];
- for (int i = 0; i < pks; i++) {
- ids[i] = i;
- int err = pthread_create(&tid[i], NULL, signatures_for_key, (void *)&ids[i]);
- if ( err != 0 )
- printf("Error creating threads");
- }
-
- for (int j = 0; j < pks; j++) {
- int err = pthread_join(tid[j], NULL);
- if ( err != 0 )
- printf("Error joining threads");
- }
-
FILE *pkfile;
FILE *msfile;
pkfile = fopen("publickey.txt", "w");
msfile = fopen("msgsig.txt", "w");
- 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",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");
-
- 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);
-
- fread(pk_buf, 1, pk_st.st_size, pk_fd);
- fread(ms_buf, 1, ms_st.st_size, ms_fd);
-
- fprintf(pkfile, "%s",(char *)pk_buf);
- fprintf(msfile, "%s",(char *)ms_buf);
-
- free(pk_buf);
- free(ms_buf);
-
- unlink(pk_name);
- unlink(ms_name);
-
- fclose(pk_fd);
- fclose(ms_fd);
-
+ int total = 0;
+
+ for (int x = 0; x < (pks / INCREMENT); x++) {
+
+ for (int i = total; i < (total + INCREMENT); i++) {
+ ids[i] = i;
+ int err = pthread_create(&tid[i], NULL, signatures_for_key, (void *)&ids[i]);
+ if ( err != 0 )
+ printf("Error creating threads");
+ }
+
+ for (int j = total; j < (total + INCREMENT); j++) {
+ int err = pthread_join(tid[j], NULL);
+ if ( err != 0 )
+ printf("Error joining threads");
+ }
+
+ for (int k = total; k < (total + INCREMENT); k++) {
+
+ struct stat pk_st, ms_st;
+
+ char pk_name[128];
+ char ms_name[128];
+
+ 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");
+
+ 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);
+
+ fread(pk_buf, 1, pk_st.st_size, pk_fd);
+ fread(ms_buf, 1, ms_st.st_size, ms_fd);
+
+ fprintf(pkfile, "%s",(char *)pk_buf);
+ fprintf(msfile, "%s",(char *)ms_buf);
+
+ free(pk_buf);
+ free(ms_buf);
+
+ unlink(pk_name);
+ unlink(ms_name);
+
+ fclose(pk_fd);
+ fclose(ms_fd);
+
+ }
+
+ total += INCREMENT;
}
fclose(pkfile);
fclose(msfile);
+ free(tid);
+
return 0;
}