Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <stdint.h>
- #if defined (NDEBUG)
- # error Not ready for NDEBUG to be defined!
- #endif
- 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]);
- }
- }
- int
- ct_secret_key_store_test(void)
- {
- //int status = 0; // assume failed status... ;^)
- FILE* file = fopen("sk.bin", "wb");
- assert(file);
- printf(
- "ct_secret_key_store_test\n"
- "____________________________________\n"
- );
- if (file)
- {
- #define HMAC_ALGO_SZ (32 + 1)
- char hmac_algo[] = "sha256";
- unsigned long hmac_algo_sz = sizeof(hmac_algo) - 1;
- char hmac_key[] = "Password";
- unsigned long hmac_key_sz = sizeof(hmac_key) - 1;
- unsigned long rand_n = 32;
- fprintf(file, "%lu,%lu,%lu,", hmac_algo_sz, hmac_key_sz, rand_n);
- fwrite(hmac_algo, 1, hmac_algo_sz, file);
- fwrite(hmac_key, 1, hmac_key_sz, file);
- fprintf(
- stdout,
- "%lu,%lu,%lu,%s%s",
- hmac_algo_sz,
- hmac_key_sz,
- rand_n,
- hmac_algo,
- hmac_key
- );
- }
- fclose(file);
- printf(
- "\n____________________________________\n\n\n"
- );
- return 1;
- }
- int
- ct_secret_key_load_test(void)
- {
- int status = 0; // assume failed status... ;^)
- FILE* file = fopen("sk.bin", "rb");
- assert(file);
- printf(
- "ct_secret_key_load_test\n"
- "____________________________________\n"
- );
- if (file)
- {
- unsigned long hmac_algo_sz = 0;
- unsigned long hmac_key_sz = 0;
- unsigned long rand_n = 0;
- status = fscanf(
- file,
- "%lu,%lu,%lu,",
- &hmac_algo_sz,
- &hmac_key_sz,
- &rand_n
- );
- assert(status == 3);
- if (status == 3)
- {
- size_t buf_sz = hmac_algo_sz + hmac_key_sz;
- unsigned char* buf = calloc(1, buf_sz); // calloc and peformance?
- assert(buf);
- if (buf)
- {
- status = fread(buf, 1, hmac_algo_sz, file);
- assert((size_t)status == hmac_algo_sz);
- fprintf(
- stdout,
- "%lu,%lu,%lu,",
- hmac_algo_sz,
- hmac_key_sz,
- rand_n
- );
- printf("[");
- ct_hex_printf(stdout, buf, hmac_algo_sz);
- printf("]");
- if ((size_t)status == hmac_algo_sz)
- {
- status = fread(buf + hmac_algo_sz, 1, hmac_key_sz, file);
- assert((size_t)status == hmac_key_sz);
- if ((size_t)status == hmac_key_sz)
- {
- // okay: we are one...
- printf("[");
- ct_hex_printf(stdout, buf + hmac_algo_sz, hmac_key_sz);
- printf("]");
- status = 1;
- }
- }
- free(buf);
- }
- }
- }
- fclose(file);
- printf(
- "\n____________________________________\n\n\n"
- );
- return status;
- }
- int main(void)
- {
- {
- int status_store = ct_secret_key_store_test();
- assert(status_store == 1);
- int status_load = ct_secret_key_load_test();
- assert(status_load == 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment