Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // version (0.0.0.3)
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <time.h>
- // undefine to corrupt the load
- //#define CORRUPT_LOAD
- void ct_hex_printf(
- FILE* fout,
- unsigned char* buf,
- size_t buf_sz
- ) {
- for (size_t i = 0; i < buf_sz; i++)
- {
- fprintf(fout, "%02x", buf[i]);
- }
- }
- // Must use a better input for the rand bytes: TRNG indeed!
- #define CT_RAND_MOD(mp_mod) ((rand()) % (mp_mod))
- unsigned char*
- ct_rand_bytes(
- unsigned char* target,
- size_t sz
- ) {
- for (size_t i = 0; i < sz; ++i)
- {
- target[i] = CT_RAND_MOD(256);
- }
- return target;
- }
- int
- ct_secret_key_store_test(void)
- {
- int self_status = 0;
- FILE* file = fopen("sk.bin", "wb");
- if (file)
- {
- // The name of the hmac algo
- char hmac_algo_buf[] = "sha256";
- unsigned long hmac_algo_sz = sizeof(hmac_algo_buf) - 1;
- // Some random key bytes
- unsigned char hmac_key_buf[23] = { '\0' };
- unsigned long hmac_key_sz = sizeof(hmac_key_buf);
- ct_rand_bytes(hmac_key_buf, hmac_key_sz);
- // The number of random bytes per encryption
- unsigned long rand_n = 32;
- // Write the meta data
- int fprintf_status = fprintf(file, "%lu,%lu,%lu,", hmac_algo_sz, hmac_key_sz, rand_n);
- if (fprintf_status > 5)
- {
- // Write the hmac algo name bytes
- size_t fwrite_status = fwrite(hmac_algo_buf, 1, hmac_algo_sz, file);
- if (fwrite_status == hmac_algo_sz)
- {
- // Write the hmac key bytes
- fwrite_status = fwrite(hmac_key_buf, 1, hmac_key_sz, file);
- if (fwrite_status == hmac_key_sz)
- {
- // We are doing good so far...
- self_status = 1;
- }
- }
- }
- fprintf(
- stdout,
- "%lu,%lu,%lu,",
- hmac_algo_sz,
- hmac_key_sz,
- rand_n
- );
- printf("[");
- ct_hex_printf(stdout, (unsigned char*)hmac_algo_buf, hmac_algo_sz);
- printf("]");
- printf("[");
- ct_hex_printf(stdout, hmac_key_buf, hmac_key_sz);
- printf("]");
- int fclose_status = fclose(file);
- if (fclose_status != 0)
- {
- // Damn...
- self_status = 0;
- }
- }
- return self_status;
- }
- int
- ct_secret_key_load_test(void)
- {
- int self_status = 0;
- FILE* file = fopen("sk.bin", "rb");
- if (file)
- {
- unsigned long hmac_algo_sz = 0;
- unsigned long hmac_key_sz = 0;
- unsigned long rand_n = 0;
- // Read in 3 unsigned longs
- int fscanf_status = fscanf(
- file,
- "%lu,%lu,%lu,",
- &hmac_algo_sz,
- &hmac_key_sz,
- &rand_n
- );
- fprintf(
- stdout,
- "%lu,%lu,%lu,",
- hmac_algo_sz,
- hmac_key_sz,
- rand_n
- );
- if (fscanf_status == 3)
- {
- #if defined (CORRUPT_LOAD)
- hmac_algo_sz = 12;
- hmac_key_sz = 42;
- #endif
- unsigned char* hmac_algo_buf = calloc(1, hmac_algo_sz);
- if (hmac_algo_buf)
- {
- // Read in the HMAC algorihtm name bytes
- size_t fread_status = fread(hmac_algo_buf, 1, hmac_algo_sz, file);
- if (fread_status == hmac_algo_sz)
- {
- printf("[");
- ct_hex_printf(stdout, hmac_algo_buf, hmac_algo_sz);
- printf("]");
- unsigned char* hmac_key_buf = calloc(1, hmac_key_sz);
- if (hmac_key_buf)
- {
- // Read in the HMAC key bytes
- fread_status = fread(hmac_key_buf, 1, hmac_key_sz, file);
- if (fread_status == hmac_key_sz)
- {
- printf("[");
- ct_hex_printf(stdout, hmac_key_buf, hmac_key_sz);
- printf("]");
- self_status = 1;
- }
- free(hmac_key_buf);
- }
- }
- free(hmac_algo_buf);
- }
- }
- int fclose_status = fclose(file);
- if (fclose_status != 0)
- {
- self_status = 0;
- }
- }
- return self_status;
- }
- int main(void)
- {
- int self_status = EXIT_FAILURE;
- srand((unsigned int)time(NULL));
- {
- printf("ct_secret_key_store_test:\n");
- int status_store = ct_secret_key_store_test();
- printf("\n\nct_secret_key_load_test:\n");
- int status_load = ct_secret_key_load_test();
- if (status_store == 1 &&
- status_load == 1)
- {
- self_status = EXIT_SUCCESS;
- }
- }
- printf("\n\nmain::self_status:(%d)\n", self_status);
- return self_status;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement