get rid of static buffers

This commit is contained in:
git@daemon.de
2014-03-18 09:56:30 +01:00
parent 8336e57c43
commit 0476fa0795

View File

@@ -169,7 +169,7 @@ size_t pcp_decrypt_stream(Pcpstream *in, Pcpstream* out, pcp_key_t *s, byte *sym
byte head[1]; byte head[1];
size_t cur_bufsize, rec_size, nrec; size_t cur_bufsize, rec_size, nrec;
byte rec_buf[PCP_ASYM_RECIPIENT_SIZE]; byte *rec_buf = NULL;
#ifdef PCP_ASYM_ADD_SENDER_PUB #ifdef PCP_ASYM_ADD_SENDER_PUB
byte *senderpub; byte *senderpub;
@@ -207,7 +207,7 @@ size_t pcp_decrypt_stream(Pcpstream *in, Pcpstream* out, pcp_key_t *s, byte *sym
#ifdef PCP_ASYM_ADD_SENDER_PUB #ifdef PCP_ASYM_ADD_SENDER_PUB
/* step 2, sender's pubkey */ /* step 2, sender's pubkey */
cur_bufsize = ps_read(in, &in_buf, crypto_box_PUBLICKEYBYTES); /* fread(&in_buf, 1, crypto_box_PUBLICKEYBYTES, in); */ cur_bufsize = ps_read(in, &in_buf, crypto_box_PUBLICKEYBYTES);
if(cur_bufsize != crypto_box_PUBLICKEYBYTES && !ps_end(in) && !ps_err(in)) { if(cur_bufsize != crypto_box_PUBLICKEYBYTES && !ps_end(in) && !ps_err(in)) {
fatal("Error: input file doesn't contain senders public key\n"); fatal("Error: input file doesn't contain senders public key\n");
goto errdef1; goto errdef1;
@@ -229,10 +229,12 @@ size_t pcp_decrypt_stream(Pcpstream *in, Pcpstream* out, pcp_key_t *s, byte *sym
/* step 4, fetch recipient list and try to decrypt it for us */ /* step 4, fetch recipient list and try to decrypt it for us */
rec_buf = ucmalloc(PCP_ASYM_RECIPIENT_SIZE);
for(nrec=0; nrec<lenrec; nrec++) { for(nrec=0; nrec<lenrec; nrec++) {
cur_bufsize = ps_read(in, &rec_buf, PCP_ASYM_RECIPIENT_SIZE); /* fread(&rec_buf, 1, PCP_ASYM_RECIPIENT_SIZE, in); */ cur_bufsize = ps_read(in, rec_buf, PCP_ASYM_RECIPIENT_SIZE);
if(cur_bufsize != PCP_ASYM_RECIPIENT_SIZE && !ps_end(in) && !ps_err(in)) { if(cur_bufsize != PCP_ASYM_RECIPIENT_SIZE && !ps_end(in) && !ps_err(in)) {
fatal("Error: input file corrupted, incomplete or no recipients (got %ld, exp %ld)\n", cur_bufsize, PCP_ASYM_RECIPIENT_SIZE ); fatal("Error: input file corrupted, incomplete or no recipients (got %ld, exp %ld)\n", cur_bufsize, PCP_ASYM_RECIPIENT_SIZE );
ucfree(rec_buf, PCP_ASYM_RECIPIENT_SIZE);
goto errdef1; goto errdef1;
} }
recmatch = 0; recmatch = 0;
@@ -254,7 +256,7 @@ size_t pcp_decrypt_stream(Pcpstream *in, Pcpstream* out, pcp_key_t *s, byte *sym
memcpy(&reccipher[R], rec_buf, PCP_ASYM_RECIPIENT_SIZE); memcpy(&reccipher[R], rec_buf, PCP_ASYM_RECIPIENT_SIZE);
} }
} }
ucfree(rec_buf, PCP_ASYM_RECIPIENT_SIZE);
if(recmatch == 0) { if(recmatch == 0) {
fatal("Sorry, there's no matching public key in your vault for decryption\n"); fatal("Sorry, there's no matching public key in your vault for decryption\n");
@@ -394,7 +396,7 @@ size_t pcp_encrypt_stream_sym(Pcpstream *in, Pcpstream *out, byte *symkey, int h
byte *buf_nonce; byte *buf_nonce;
byte *buf_cipher; byte *buf_cipher;
byte in_buf[PCP_BLOCK_SIZE]; byte *in_buf = NULL;
size_t cur_bufsize = 0; size_t cur_bufsize = 0;
size_t out_size = 0; size_t out_size = 0;
size_t es; size_t es;
@@ -427,14 +429,12 @@ size_t pcp_encrypt_stream_sym(Pcpstream *in, Pcpstream *out, byte *symkey, int h
ps_write(out, ivpad, PCP_BLOCK_SIZE_IN - PCP_BLOCK_SIZE); ps_write(out, ivpad, PCP_BLOCK_SIZE_IN - PCP_BLOCK_SIZE);
ps_write(out, iv, PCP_BLOCK_SIZE); ps_write(out, iv, PCP_BLOCK_SIZE);
/*
fwrite(ivpad, PCP_BLOCK_SIZE_IN - PCP_BLOCK_SIZE, 1, out);
fwrite(iv, PCP_BLOCK_SIZE, 1, out);*/
#endif #endif
/* 32k-ECB-mode. FIXME: maybe support CBC as well or only use CBC? */ /* 32k-Block-mode. */
in_buf = ucmalloc(PCP_BLOCK_SIZE);
while(!ps_end(in)) { while(!ps_end(in)) {
cur_bufsize = ps_read(in, &in_buf, PCP_BLOCK_SIZE); /* fread(&in_buf, 1, PCP_BLOCK_SIZE, in); */ cur_bufsize = ps_read(in, in_buf, PCP_BLOCK_SIZE);
if(cur_bufsize <= 0) if(cur_bufsize <= 0)
break; break;
buf_nonce = pcp_gennonce(); buf_nonce = pcp_gennonce();
@@ -449,9 +449,6 @@ size_t pcp_encrypt_stream_sym(Pcpstream *in, Pcpstream *out, byte *symkey, int h
ps_write(out, buf_nonce, crypto_secretbox_NONCEBYTES); ps_write(out, buf_nonce, crypto_secretbox_NONCEBYTES);
ps_write(out, buf_cipher, es); ps_write(out, buf_cipher, es);
/*
fwrite(buf_nonce, crypto_secretbox_NONCEBYTES, 1, out);
fwrite(buf_cipher, es, 1, out); */
free(buf_nonce); free(buf_nonce);
free(buf_cipher); free(buf_cipher);
out_size += crypto_secretbox_NONCEBYTES + es; out_size += crypto_secretbox_NONCEBYTES + es;
@@ -485,15 +482,14 @@ size_t pcp_encrypt_stream_sym(Pcpstream *in, Pcpstream *out, byte *symkey, int h
ps_write(out, buf_nonce, crypto_secretbox_NONCEBYTES); ps_write(out, buf_nonce, crypto_secretbox_NONCEBYTES);
ps_write(out, buf_cipher, es); ps_write(out, buf_cipher, es);
/*
fwrite(buf_nonce, crypto_secretbox_NONCEBYTES, 1, out);
fwrite(buf_cipher, es, 1, out); */
free(st); free(st);
free(signature); ucfree(signature, siglen);
free(hash); free(hash);
} }
ucfree(in_buf, PCP_BLOCK_SIZE);
return out_size; return out_size;
errsym1: errsym1:
@@ -501,6 +497,7 @@ size_t pcp_encrypt_stream_sym(Pcpstream *in, Pcpstream *out, byte *symkey, int h
free(st); free(st);
free(hash); free(hash);
} }
ucfree(in_buf, PCP_BLOCK_SIZE);
return 0; return 0;
} }
@@ -510,7 +507,7 @@ size_t pcp_decrypt_stream_sym(Pcpstream *in, Pcpstream* out, byte *symkey, pcp_r
byte *buf_clear; byte *buf_clear;
size_t out_size, cur_bufsize, es; size_t out_size, cur_bufsize, es;
size_t ciphersize = (PCP_BLOCK_SIZE_IN) - crypto_secretbox_NONCEBYTES; size_t ciphersize = (PCP_BLOCK_SIZE_IN) - crypto_secretbox_NONCEBYTES;
byte in_buf[PCP_BLOCK_SIZE_IN]; byte *in_buf = NULL;
buf_nonce = ucmalloc(crypto_secretbox_NONCEBYTES); buf_nonce = ucmalloc(crypto_secretbox_NONCEBYTES);
buf_cipher = ucmalloc(ciphersize); buf_cipher = ucmalloc(ciphersize);
@@ -534,8 +531,10 @@ size_t pcp_decrypt_stream_sym(Pcpstream *in, Pcpstream* out, byte *symkey, pcp_r
byte *iv = NULL; /* will be filled during 1st loop */ byte *iv = NULL; /* will be filled during 1st loop */
#endif #endif
in_buf = ucmalloc(PCP_BLOCK_SIZE_IN);
while(!ps_end(in)) { while(!ps_end(in)) {
cur_bufsize = ps_read(in, &in_buf, PCP_BLOCK_SIZE_IN); /* fread(&in_buf, 1, PCP_BLOCK_SIZE_IN, in); */ cur_bufsize = ps_read(in, in_buf, PCP_BLOCK_SIZE_IN);
if(cur_bufsize <= PCP_CRYPTO_ADD) if(cur_bufsize <= PCP_CRYPTO_ADD)
break; /* no valid cipher block */ break; /* no valid cipher block */
@@ -596,7 +595,8 @@ size_t pcp_decrypt_stream_sym(Pcpstream *in, Pcpstream* out, byte *symkey, pcp_r
#endif #endif
} }
free(buf_cipher); ucfree(in_buf, PCP_BLOCK_SIZE_IN);
ucfree(buf_cipher, ciphersize);
if(recverify != NULL) { if(recverify != NULL) {
/* decrypt the signature */ /* decrypt the signature */
@@ -628,8 +628,8 @@ size_t pcp_decrypt_stream_sym(Pcpstream *in, Pcpstream* out, byte *symkey, pcp_r
} }
free(st); free(st);
free(hash); free(hash);
free(signature); ucfree(signature, siglen);
free(signature_cr); ucfree(signature_cr, siglen_cr);
} }
free(buf_nonce); free(buf_nonce);