Chris_M_Thomasson

Secret Key Store/Load Testing 123...

Sep 14th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <stdint.h>
  5.  
  6.  
  7. #if defined (NDEBUG)
  8. # error Not ready for NDEBUG to be defined!
  9. #endif
  10.  
  11.  
  12. void ct_hex_printf(
  13. FILE* fout,
  14. unsigned char* buf,
  15. size_t buf_sz
  16. ) {
  17. for (size_t i = 0; i < buf_sz; i++)
  18. {
  19. fprintf(fout, "%02x ", buf[i]);
  20. }
  21. }
  22.  
  23.  
  24. int
  25. ct_secret_key_store_test(void)
  26. {
  27. //int status = 0; // assume failed status... ;^)
  28.  
  29. FILE* file = fopen("sk.bin", "wb");
  30. assert(file);
  31.  
  32. printf(
  33. "ct_secret_key_store_test\n"
  34. "____________________________________\n"
  35. );
  36.  
  37. if (file)
  38. {
  39. #define HMAC_ALGO_SZ (32 + 1)
  40.  
  41. char hmac_algo[] = "sha256";
  42. unsigned long hmac_algo_sz = sizeof(hmac_algo) - 1;
  43.  
  44.  
  45. char hmac_key[] = "Password";
  46. unsigned long hmac_key_sz = sizeof(hmac_key) - 1;
  47.  
  48.  
  49. unsigned long rand_n = 32;
  50.  
  51.  
  52. fprintf(file, "%lu,%lu,%lu,", hmac_algo_sz, hmac_key_sz, rand_n);
  53. fwrite(hmac_algo, 1, hmac_algo_sz, file);
  54. fwrite(hmac_key, 1, hmac_key_sz, file);
  55.  
  56.  
  57. fprintf(
  58. stdout,
  59. "%lu,%lu,%lu,%s%s",
  60. hmac_algo_sz,
  61. hmac_key_sz,
  62. rand_n,
  63. hmac_algo,
  64. hmac_key
  65. );
  66. }
  67.  
  68. fclose(file);
  69.  
  70. printf(
  71. "\n____________________________________\n\n\n"
  72. );
  73.  
  74. return 1;
  75. }
  76.  
  77.  
  78. int
  79. ct_secret_key_load_test(void)
  80. {
  81. int status = 0; // assume failed status... ;^)
  82.  
  83. FILE* file = fopen("sk.bin", "rb");
  84. assert(file);
  85.  
  86. printf(
  87. "ct_secret_key_load_test\n"
  88. "____________________________________\n"
  89. );
  90.  
  91. if (file)
  92. {
  93. unsigned long hmac_algo_sz = 0;
  94. unsigned long hmac_key_sz = 0;
  95. unsigned long rand_n = 0;
  96.  
  97. status = fscanf(
  98. file,
  99. "%lu,%lu,%lu,",
  100. &hmac_algo_sz,
  101. &hmac_key_sz,
  102. &rand_n
  103. );
  104.  
  105.  
  106.  
  107. assert(status == 3);
  108.  
  109. if (status == 3)
  110. {
  111. size_t buf_sz = hmac_algo_sz + hmac_key_sz;
  112. unsigned char* buf = calloc(1, buf_sz); // calloc and peformance?
  113. assert(buf);
  114.  
  115. if (buf)
  116. {
  117. status = fread(buf, 1, hmac_algo_sz, file);
  118. assert((size_t)status == hmac_algo_sz);
  119.  
  120.  
  121. fprintf(
  122. stdout,
  123. "%lu,%lu,%lu,",
  124. hmac_algo_sz,
  125. hmac_key_sz,
  126. rand_n
  127. );
  128.  
  129. printf("[");
  130. ct_hex_printf(stdout, buf, hmac_algo_sz);
  131. printf("]");
  132.  
  133. if ((size_t)status == hmac_algo_sz)
  134. {
  135. status = fread(buf + hmac_algo_sz, 1, hmac_key_sz, file);
  136. assert((size_t)status == hmac_key_sz);
  137.  
  138. if ((size_t)status == hmac_key_sz)
  139. {
  140. // okay: we are one...
  141. printf("[");
  142. ct_hex_printf(stdout, buf + hmac_algo_sz, hmac_key_sz);
  143. printf("]");
  144.  
  145. status = 1;
  146. }
  147. }
  148.  
  149. free(buf);
  150. }
  151. }
  152. }
  153.  
  154. fclose(file);
  155.  
  156. printf(
  157. "\n____________________________________\n\n\n"
  158. );
  159.  
  160. return status;
  161. }
  162.  
  163.  
  164. int main(void)
  165. {
  166. {
  167. int status_store = ct_secret_key_store_test();
  168. assert(status_store == 1);
  169.  
  170.  
  171. int status_load = ct_secret_key_load_test();
  172. assert(status_load == 1);
  173. }
  174.  
  175. return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment