Advertisement
Chris_M_Thomasson

Secret Key Test v:(0.0.0.3)

Sep 16th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. // version (0.0.0.3)
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <time.h>
  8.  
  9.  
  10. // undefine to corrupt the load
  11. //#define CORRUPT_LOAD
  12.  
  13.  
  14. void ct_hex_printf(
  15.     FILE* fout,
  16.     unsigned char* buf,
  17.     size_t buf_sz
  18. ) {
  19.     for (size_t i = 0; i < buf_sz; i++)
  20.     {
  21.         fprintf(fout, "%02x", buf[i]);
  22.     }
  23. }
  24.  
  25.  
  26. // Must use a better input for the rand bytes: TRNG indeed!
  27. #define CT_RAND_MOD(mp_mod) ((rand()) % (mp_mod))
  28.  
  29. unsigned char*
  30. ct_rand_bytes(
  31.     unsigned char* target,
  32.     size_t sz
  33. ) {
  34.     for (size_t i = 0; i < sz; ++i)
  35.     {
  36.         target[i] = CT_RAND_MOD(256);
  37.     }
  38.  
  39.     return target;
  40. }
  41.  
  42.  
  43. int
  44. ct_secret_key_store_test(void)
  45. {
  46.     int self_status = 0;
  47.  
  48.     FILE* file = fopen("sk.bin", "wb");
  49.  
  50.     if (file)
  51.     {
  52.         // The name of the hmac algo
  53.         char hmac_algo_buf[] = "sha256";
  54.         unsigned long hmac_algo_sz = sizeof(hmac_algo_buf) - 1;
  55.  
  56.         // Some random key bytes
  57.         unsigned char hmac_key_buf[23] = { '\0' };
  58.         unsigned long hmac_key_sz = sizeof(hmac_key_buf);
  59.  
  60.         ct_rand_bytes(hmac_key_buf, hmac_key_sz);
  61.  
  62.         // The number of random bytes per encryption
  63.         unsigned long rand_n = 32;
  64.  
  65.         // Write the meta data
  66.         int fprintf_status = fprintf(file, "%lu,%lu,%lu,", hmac_algo_sz, hmac_key_sz, rand_n);
  67.  
  68.         if (fprintf_status > 5)
  69.         {
  70.             // Write the hmac algo name bytes
  71.             size_t fwrite_status = fwrite(hmac_algo_buf, 1, hmac_algo_sz, file);
  72.  
  73.             if (fwrite_status == hmac_algo_sz)
  74.             {
  75.                 // Write the hmac key bytes
  76.                 fwrite_status = fwrite(hmac_key_buf, 1, hmac_key_sz, file);
  77.  
  78.                 if (fwrite_status == hmac_key_sz)
  79.                 {
  80.                     // We are doing good so far...
  81.                     self_status = 1;
  82.                 }
  83.             }
  84.         }
  85.  
  86.         fprintf(
  87.             stdout,
  88.             "%lu,%lu,%lu,",
  89.             hmac_algo_sz,
  90.             hmac_key_sz,
  91.             rand_n
  92.         );
  93.  
  94.         printf("[");
  95.         ct_hex_printf(stdout, (unsigned char*)hmac_algo_buf, hmac_algo_sz);
  96.         printf("]");
  97.  
  98.         printf("[");
  99.         ct_hex_printf(stdout, hmac_key_buf, hmac_key_sz);
  100.         printf("]");
  101.  
  102.  
  103.         int fclose_status = fclose(file);
  104.  
  105.         if (fclose_status != 0)
  106.         {
  107.             // Damn...
  108.             self_status = 0;
  109.         }
  110.     }
  111.  
  112.     return self_status;
  113. }
  114.  
  115.  
  116. int
  117. ct_secret_key_load_test(void)
  118. {
  119.     int self_status = 0;
  120.  
  121.     FILE* file = fopen("sk.bin", "rb");
  122.  
  123.     if (file)
  124.     {
  125.         unsigned long hmac_algo_sz = 0;
  126.         unsigned long hmac_key_sz = 0;
  127.         unsigned long rand_n = 0;
  128.  
  129.         // Read in 3 unsigned longs
  130.         int fscanf_status = fscanf(
  131.             file,
  132.             "%lu,%lu,%lu,",
  133.             &hmac_algo_sz,
  134.             &hmac_key_sz,
  135.             &rand_n
  136.         );
  137.  
  138.         fprintf(
  139.             stdout,
  140.             "%lu,%lu,%lu,",
  141.             hmac_algo_sz,
  142.             hmac_key_sz,
  143.             rand_n
  144.         );
  145.  
  146.         if (fscanf_status == 3)
  147.         {
  148.             #if defined (CORRUPT_LOAD)
  149.                 hmac_algo_sz = 12;
  150.                 hmac_key_sz = 42;
  151.             #endif
  152.  
  153.             unsigned char* hmac_algo_buf = calloc(1, hmac_algo_sz);
  154.  
  155.             if (hmac_algo_buf)
  156.             {
  157.                 // Read in the HMAC algorihtm name bytes
  158.                 size_t fread_status = fread(hmac_algo_buf, 1, hmac_algo_sz, file);
  159.  
  160.                 if (fread_status == hmac_algo_sz)
  161.                 {
  162.                     printf("[");
  163.                     ct_hex_printf(stdout, hmac_algo_buf, hmac_algo_sz);
  164.                     printf("]");
  165.  
  166.                     unsigned char* hmac_key_buf = calloc(1, hmac_key_sz);
  167.  
  168.                     if (hmac_key_buf)
  169.                     {
  170.                         // Read in the HMAC key bytes
  171.                         fread_status = fread(hmac_key_buf, 1, hmac_key_sz, file);
  172.  
  173.                         if (fread_status == hmac_key_sz)
  174.                         {
  175.                             printf("[");
  176.                             ct_hex_printf(stdout, hmac_key_buf, hmac_key_sz);
  177.                             printf("]");
  178.  
  179.                             self_status = 1;
  180.                         }
  181.  
  182.                         free(hmac_key_buf);
  183.                     }
  184.                 }
  185.  
  186.                 free(hmac_algo_buf);
  187.             }
  188.         }
  189.  
  190.         int fclose_status = fclose(file);
  191.  
  192.         if (fclose_status != 0)
  193.         {
  194.             self_status = 0;
  195.         }
  196.     }
  197.  
  198.     return self_status;
  199. }
  200.  
  201.  
  202. int main(void)
  203. {
  204.     int self_status = EXIT_FAILURE;
  205.  
  206.     srand((unsigned int)time(NULL));
  207.  
  208.     {
  209.         printf("ct_secret_key_store_test:\n");
  210.         int status_store = ct_secret_key_store_test();
  211.  
  212.         printf("\n\nct_secret_key_load_test:\n");
  213.         int status_load = ct_secret_key_load_test();
  214.  
  215.         if (status_store == 1 &&
  216.             status_load  == 1)
  217.         {
  218.             self_status = EXIT_SUCCESS;
  219.         }
  220.     }
  221.  
  222.     printf("\n\nmain::self_status:(%d)\n", self_status);
  223.  
  224.     return self_status;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement