|
|
|
@ -8,23 +8,74 @@
|
|
|
|
|
|
|
|
|
|
const char write_string[] = "Hello world?";
|
|
|
|
|
|
|
|
|
|
FILE* wrap_stream(FILE* wrap, enum cc20_mode mode, const cc20_key_t* k, const cc20_iv_t* i, cc20_meta_t *restrict meta)
|
|
|
|
|
{
|
|
|
|
|
cc20_meta_t _meta = {0};
|
|
|
|
|
if(!meta) meta = &_meta;
|
|
|
|
|
|
|
|
|
|
if(CC20_OK(cc20_gen_meta(wrap, k, i, mode, meta)))
|
|
|
|
|
{
|
|
|
|
|
cc20_sink_t* sink;
|
|
|
|
|
if(CC20_OK(cc20_gen_sink(meta, &sink)))
|
|
|
|
|
return cc20_wrap_sink(sink, (struct cc20_wrap_cfg[1]) {{ .keep_alive = 1 }} );
|
|
|
|
|
else perror("cc20_gen_sink()");
|
|
|
|
|
} else perror("cc20_gen_meta()");
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* wrap_file(const char* filename, enum cc20_mode mode, cc20_meta_t* restrict meta)
|
|
|
|
|
{
|
|
|
|
|
cc20_meta_t _meta = {0};
|
|
|
|
|
if(!meta) meta = &_meta;
|
|
|
|
|
|
|
|
|
|
FILE* wrap = fopen(filename, "w+b");
|
|
|
|
|
if(!wrap) {
|
|
|
|
|
perror("fopen()");
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
return wrap_stream(wrap, mode, NULL, NULL, meta);
|
|
|
|
|
/*
|
|
|
|
|
if(CC20_OK(cc20_gen_meta(wrap, NULL, NULL, mode, meta))
|
|
|
|
|
&& CC20_OK(cc20_gen(meta, &wrap))) return wrap;
|
|
|
|
|
perror("cc20_gen()");
|
|
|
|
|
exit(-1);*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
((void)argc);
|
|
|
|
|
if(!argv[1]) return 1;
|
|
|
|
|
|
|
|
|
|
cc20_meta_t meta;
|
|
|
|
|
cc20_result_t res;
|
|
|
|
|
|
|
|
|
|
FILE* output = argv[1] ? fopen(argv[1], "wb") : stdout;
|
|
|
|
|
if(!output) { perror("failed to open output"); return -1; }
|
|
|
|
|
FILE* output = wrap_file(argv[1], CC20_ENCRYPT, &meta);
|
|
|
|
|
size_t wsz;
|
|
|
|
|
printf("written %lu bytes\n", (wsz=fwrite(write_string, 1, strlen(write_string), output)));
|
|
|
|
|
|
|
|
|
|
fclose(output);
|
|
|
|
|
|
|
|
|
|
FILE* input = meta.backing;
|
|
|
|
|
long sz = ftell(input);
|
|
|
|
|
|
|
|
|
|
printf(" -> backing stream tell: %ld\n", sz);
|
|
|
|
|
if(sz<=0) return (perror("ftell()"), -1);
|
|
|
|
|
else if(wsz != (size_t)sz) return (fprintf(stderr, "incorrect ftell(): (expected %lu, got %ld)\n", wsz, sz), -2);
|
|
|
|
|
|
|
|
|
|
TRY(cc20_gen_meta(output, NULL, NULL, CC20_ENCRYPT, &meta));
|
|
|
|
|
TRY(cc20_gen(&meta, &output));
|
|
|
|
|
if(fseek(input, 0L, SEEK_SET)!=0) return (perror("fseek()"), -3);
|
|
|
|
|
|
|
|
|
|
unsigned char encbuf[wsz];
|
|
|
|
|
if(fread(encbuf, 1, wsz, input)!=wsz) return (perror("fread()"), -4);
|
|
|
|
|
|
|
|
|
|
printf("written %lu bytes\n", fwrite(write_string, 1, strlen(write_string), output));
|
|
|
|
|
printf("decrypted: '"); {
|
|
|
|
|
/* owning stdout */
|
|
|
|
|
output = wrap_stream(stdout, CC20_DECRYPT, (const cc20_key_t*) &meta.key, (const cc20_iv_t*) &meta.iv, NULL);
|
|
|
|
|
wsz = fwrite(encbuf, 1, wsz, output);
|
|
|
|
|
fclose(output);
|
|
|
|
|
/* releases stdout */
|
|
|
|
|
printf("'\n"); }
|
|
|
|
|
printf("written %lu bytes\n", wsz);
|
|
|
|
|
|
|
|
|
|
//fprintf(output, "Hello world!");
|
|
|
|
|
fclose(input);
|
|
|
|
|
|
|
|
|
|
res = CC20_ERR_NONE;
|
|
|
|
|
fail:
|
|
|
|
|
if(output) fclose(output);
|
|
|
|
|
return (int)res;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|