diff --git a/src/client/msgr_encrypt.cpp b/src/client/msgr_encrypt.cpp index c87088ca..e0e2fdf6 100644 --- a/src/client/msgr_encrypt.cpp +++ b/src/client/msgr_encrypt.cpp @@ -87,7 +87,10 @@ void op_aes_xts_encrypt_t::update(uint8_t *in, size_t max_in, uint8_t *out, size done_out += max_out; tmp_pos += max_out; if (tmp_pos >= block_size) + { encrypted = false; + done_in += 1; + } } else if (max_in < block_size - offset%block_size) { @@ -115,7 +118,7 @@ void op_aes_xts_encrypt_t::update(uint8_t *in, size_t max_in, uint8_t *out, size encrypted = true; memcpy(out, tmp, max_out); tmp_pos = max_out; - done_in += max_in; + done_in += max_in-1; offset += max_in; done_out += max_out; } @@ -250,7 +253,10 @@ void op_aes_xts_decrypt_t::update(uint8_t *in, size_t max_in, uint8_t *out, size done_out += max_out; tmp_pos += max_out; if (tmp_pos >= block_size) + { decrypted = false; + done_in += 1; + } } else if (max_in < block_size - offset%block_size) { @@ -279,7 +285,7 @@ void op_aes_xts_decrypt_t::update(uint8_t *in, size_t max_in, uint8_t *out, size if (out) memcpy(out, tmp, max_out); tmp_pos = max_out; - done_in += max_in; + done_in += max_in-1; offset += max_in; done_out += max_out; } @@ -325,7 +331,7 @@ void osd_messenger_t::op_encrypted_copy_buf(osd_client_t *cl, uint8_t *enc_buf, assert(cl->write_op->enc->key_chain[0]); cl->xts_enc_ctx->start(cl->write_op->enc->key_chain[0], cl->write_op->req.rw.offset, cl->write_op->enc->bitmap_granularity); } - while (done_enc < enc_len && (done_plain < plain_len || cl->xts_enc_ctx->has_buffered())) + while (done_plain < plain_len && done_enc < enc_len) { size_t done_in = 0; size_t done_out = 0; diff --git a/src/client/msgr_encrypt.h b/src/client/msgr_encrypt.h index 957212df..25a375b1 100644 --- a/src/client/msgr_encrypt.h +++ b/src/client/msgr_encrypt.h @@ -26,7 +26,6 @@ public: op_aes_xts_encrypt_t(); ~op_aes_xts_encrypt_t(); - inline bool has_buffered() { return encrypted; }; void start(uint8_t *key, uint64_t start_offset, size_t block_size); void update(uint8_t *in, size_t max_in, uint8_t *out, size_t max_out, size_t & done_in, size_t & done_out); }; @@ -54,7 +53,6 @@ public: op_aes_xts_decrypt_t(); ~op_aes_xts_decrypt_t(); - inline bool has_buffered() { return decrypted; }; void start(uint8_t **key_chain, size_t chain_size, void *key_indexes, uint64_t start_offset, size_t block_size); void update(uint8_t *in, size_t max_in, uint8_t *out, size_t max_out, size_t & done_in, size_t & done_out); }; diff --git a/src/test/test_cluster_client.cpp b/src/test/test_cluster_client.cpp index c0329990..cd0ef7ef 100644 --- a/src/test/test_cluster_client.cpp +++ b/src/test/test_cluster_client.cpp @@ -690,7 +690,7 @@ void test_msgr_encrypt() enc->start(key, 4096 * 114, 4096); in_pos = out_pos = 0; enc->update(src+4096, 4096, crypt2, 4095, in_pos, out_pos); - assert(in_pos == 4096); + assert(in_pos == 4095); assert(out_pos == 4095); enc->update(src+4096+in_pos, 4096-in_pos, crypt2+out_pos, 4096-out_pos, in_pos, out_pos); assert(in_pos == 4096); @@ -721,10 +721,10 @@ void test_msgr_encrypt() assert(in_pos == 3000); assert(out_pos == 0); dec->update(crypt+4096+3000, 3000, decrypt, 500, in_pos, out_pos); - assert(in_pos == 4096); + assert(in_pos == 4095); assert(out_pos == 500); dec->update(crypt+4096+in_pos, 6000-in_pos, decrypt+out_pos, 3000, in_pos, out_pos); - assert(in_pos == 4096); + assert(in_pos == 4095); assert(out_pos == 3500); dec->update(crypt+4096+in_pos, 6000-in_pos, decrypt+out_pos, 1000, in_pos, out_pos); assert(in_pos == 4096); @@ -733,10 +733,10 @@ void test_msgr_encrypt() assert(in_pos == 6000); assert(out_pos == 4096); dec->update(crypt+4096+in_pos, 8192-in_pos, decrypt+out_pos, 4500-out_pos, in_pos, out_pos); - assert(in_pos == 8192); + assert(in_pos == 8191); assert(out_pos == 4500); dec->update(crypt+4096+in_pos, 8192-in_pos, decrypt+out_pos, 7500-out_pos, in_pos, out_pos); - assert(in_pos == 8192); + assert(in_pos == 8191); assert(out_pos == 7500); dec->update(crypt+4096+in_pos, 8192-in_pos, decrypt+out_pos, 8192-out_pos, in_pos, out_pos); assert(in_pos == 8192);