main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * main.c
  3. *
  4. * Created on: May 3, 2018
  5. * Author: Gustavo
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <inttypes.h>
  13. #include <unistd.h>
  14. #include <sys/resource.h>
  15. #include <sodium.h>
  16. #include "../include/api.h"
  17. #include "../include/keygeneration.h"
  18. #include "../include/encapsulation.h"
  19. #include "../include/decapsulation.h"
  20. #include "../include/gf/gf.h"
  21. #include "../include/structures/polynomial.h"
  22. #include "../include/definitions.h"
  23. #include "cpucycles.h"
  24. #define MAX_MARKER_LEN 50
  25. #define KAT_SUCCESS 0
  26. #define KAT_FILE_OPEN_ERROR -1
  27. #define KAT_DATA_ERROR -3
  28. #define KAT_CRYPTO_FAILURE -4
  29. static const int EXECUTION_TIMES = 2;
  30. int FindMarker(FILE *infile, const char *marker);
  31. int ReadHex(FILE *infile, unsigned char *A, int Length, char *str);
  32. void fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L);
  33. long long todstart;
  34. long long todend;
  35. long long cpustart;
  36. long long cpuend;
  37. long long t_gen[1001];
  38. long long t_enc[1001];
  39. long long t_dec[1001];
  40. long long t_gen_avg;
  41. long long t_enc_avg;
  42. long long t_dec_avg;
  43. int main(void) {
  44. t_gen_avg = 0;
  45. t_enc_avg = 0;
  46. t_dec_avg = 0;
  47. if (sodium_init() < 0) {
  48. /* panic! the library couldn't be initialized, it is not safe to use */
  49. return 1;
  50. }
  51. const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
  52. struct rlimit rl;
  53. int result;
  54. result = getrlimit(RLIMIT_STACK, &rl);
  55. if (result == 0) {
  56. if (rl.rlim_cur < kStackSize) {
  57. rl.rlim_cur = kStackSize;
  58. result = setrlimit(RLIMIT_STACK, &rl);
  59. if (result != 0) {
  60. fprintf(stderr, "setrlimit returned result = %d\n", result);
  61. }
  62. }
  63. }
  64. char fn_req[32], fn_rsp[32];
  65. FILE *fp_req, *fp_rsp;
  66. unsigned char seed[48];
  67. unsigned char random_values[48];
  68. unsigned char entropy_input[48];
  69. int count = 0;
  70. unsigned char pk[CRYPTO_PUBLICKEYBYTES] = { 0 };
  71. unsigned char ct[CRYPTO_CIPHERTEXTBYTES], ss[CRYPTO_BYTES]; //, ss1[CRYPTO_BYTES];
  72. unsigned char sk[CRYPTO_SECRETKEYBYTES] = { 0 };
  73. int byte_count = 48;
  74. int counter = 0;
  75. int i;
  76. // Create the REQUEST file
  77. sprintf(fn_req, "PQCkemKAT_%d.req", CRYPTO_SECRETKEYBYTES);
  78. if ((fp_req = fopen(fn_req, "w")) == NULL) {
  79. printf("Couldn't open <%s> for write\n", fn_req);
  80. return KAT_FILE_OPEN_ERROR;
  81. }
  82. sprintf(fn_rsp, "PQCkemKAT_%d.rsp", CRYPTO_SECRETKEYBYTES);
  83. if ((fp_rsp = fopen(fn_rsp, "w")) == NULL) {
  84. printf("Couldn't open <%s> for write\n", fn_rsp);
  85. return KAT_FILE_OPEN_ERROR;
  86. }
  87. // Read from /dev/urandom to initialize entropy for random_bytes()
  88. FILE *fp_urandom;
  89. fp_urandom = fopen("/dev/urandom", "r");
  90. if (0 == fread(&entropy_input, 1, byte_count, fp_urandom)) {
  91. PRINT_DEBUG("Failed to read in entropy input\n");
  92. return EXIT_FAILURE;
  93. }
  94. fclose(fp_urandom);
  95. randombytes_init(entropy_input, NULL, 256);
  96. for (i = 0; i < EXECUTION_TIMES; i++) {
  97. fprintf(fp_req, "count = %d\n", i);
  98. randombytes_NIST(random_values, 48);
  99. fprintBstr(fp_req, "seed = ", random_values, 48);
  100. fprintf(fp_req, "pk =\n");
  101. fprintf(fp_req, "sk =\n");
  102. fprintf(fp_req, "ct =\n");
  103. fprintf(fp_req, "ss =\n\n");
  104. }
  105. fclose(fp_req);
  106. //Create the RESPONSE file based on what's in the REQUEST file
  107. if ((fp_req = fopen(fn_req, "r")) == NULL) {
  108. printf("Couldn't open <%s> for read\n", fn_req);
  109. return KAT_FILE_OPEN_ERROR;
  110. }
  111. fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME);
  112. //sk = malloc(CRYPTO_SECRETKEYBYTES);
  113. if (EOF == fscanf(fp_req, "%d", &count)) {
  114. PRINT_DEBUG("Failed to fscanf %d:%s\n", errno, strerror(errno));
  115. return EXIT_FAILURE;
  116. }
  117. fprintf(fp_rsp, "count = %d\n", count);
  118. if (!ReadHex(fp_req, seed, 48, "seed = ")) {
  119. return KAT_DATA_ERROR;
  120. }
  121. fprintBstr(fp_rsp, "seed = ", seed, 48);
  122. randombytes_init(seed, NULL, 256);
  123. int success = 0;
  124. int fails = 0;
  125. do {
  126. int ret_val = 0;
  127. printf("Starting iteration: %d\n", counter);
  128. long long start = cpucycles();
  129. //key_gen(v, y, &G);
  130. ret_val = key_pair_generation(pk, sk);
  131. //int ret_val = key_pair_generation(pk, sk);
  132. long long final = cpucycles();
  133. t_gen_avg = t_gen_avg + (final - start);
  134. printf("KeyGen: %lld\n", (final - start) / 1000000);
  135. start = cpucycles();
  136. ret_val = encapsulation(ct, ss, pk);
  137. //encrypt(ct, ss, &G);
  138. final = cpucycles();
  139. t_enc_avg = t_enc_avg + (final - start);
  140. printf("Encaps: %lld\n", (final - start) / 1000000);
  141. if (ret_val != 0) {
  142. printf("fail encrypt");
  143. return KAT_CRYPTO_FAILURE;
  144. }
  145. start = cpucycles();
  146. ret_val = decapsulation(ss, ct, sk);
  147. //ret_val = decrypt(ss, ct, v_k, y_k);
  148. final = cpucycles();
  149. t_dec_avg = t_dec_avg + (final - start);
  150. printf("decaps: %lld\n", (final - start) / 1000000);
  151. if (ret_val != 0) {
  152. printf("FAIL\n");
  153. fails++;
  154. //return -1;
  155. } else {
  156. printf("SUCCESS\n\n");
  157. success++;
  158. }
  159. counter++;
  160. } while (counter < 100);
  161. printf("SUCCESS: %d\n", success);
  162. printf("fails: %d\n", fails);
  163. printf("gen_avg: %lld\n", (t_gen_avg / counter));
  164. printf("enc_avg: %lld\n", (t_enc_avg / counter));
  165. printf("dec_avg: %lld\n", (t_dec_avg / counter));
  166. //free(sk);
  167. return 0;
  168. }
  169. int FindMarker(FILE *infile, const char *marker) {
  170. char line[MAX_MARKER_LEN];
  171. int i, len;
  172. int curr_line;
  173. len = (int) strlen(marker);
  174. if (len > MAX_MARKER_LEN - 1)
  175. len = MAX_MARKER_LEN - 1;
  176. for (i = 0; i < len; i++) {
  177. curr_line = fgetc(infile);
  178. line[i] = curr_line;
  179. if (curr_line == EOF)
  180. return 0;
  181. }
  182. line[len] = '\0';
  183. while (1) {
  184. if (!strncmp(line, marker, len))
  185. return 1;
  186. for (i = 0; i < len - 1; i++)
  187. line[i] = line[i + 1];
  188. curr_line = fgetc(infile);
  189. line[len - 1] = curr_line;
  190. if (curr_line == EOF)
  191. return 0;
  192. line[len] = '\0';
  193. }
  194. // shouldn't get here
  195. return 0;
  196. }
  197. int ReadHex(FILE *infile, unsigned char *A, int Length, char *str) {
  198. int i, ch, started;
  199. unsigned char ich;
  200. if (Length == 0) {
  201. A[0] = 0x00;
  202. return 1;
  203. }
  204. memset(A, 0x00, Length);
  205. started = 0;
  206. if (FindMarker(infile, str))
  207. while ((ch = fgetc(infile)) != EOF) {
  208. if (!isxdigit(ch)) {
  209. if (!started) {
  210. if (ch == '\n')
  211. break;
  212. else
  213. continue;
  214. } else
  215. break;
  216. }
  217. started = 1;
  218. if ((ch >= '0') && (ch <= '9'))
  219. ich = ch - '0';
  220. else if ((ch >= 'A') && (ch <= 'F'))
  221. ich = ch - 'A' + 10;
  222. else if ((ch >= 'a') && (ch <= 'f'))
  223. ich = ch - 'a' + 10;
  224. else
  225. // shouldn't ever get here
  226. ich = 0;
  227. for (i = 0; i < Length - 1; i++)
  228. A[i] = (A[i] << 4) | (A[i + 1] >> 4);
  229. A[Length - 1] = (A[Length - 1] << 4) | ich;
  230. }
  231. else
  232. return 0;
  233. return 1;
  234. }
  235. void fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L) {
  236. unsigned long long i;
  237. fprintf(fp, "%s", S);
  238. for (i = 0; i < L; i++)
  239. fprintf(fp, "%02X", A[i]);
  240. if (L == 0)
  241. fprintf(fp, "00");
  242. fprintf(fp, "\n");
  243. }