From 507d926da825fbc1d9d74b4517dbab47702c66b9 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 1 Sep 2016 13:40:26 -0400 Subject: Cipher_Mode API improvements The Cipher_Mode::update API is more general than needed to just support ciphers (this is due to it previously being an API of Transform which before 8b85b780515 was Cipher_Mode's base class) Define a less general interface `process` which either processes the blocks in-place, producing exactly as much output as there was input, or (SIV/CCM case) saves the entire message for processing in `finish`. These two uses cover all current or anticipated cipher modes. Leaves `update` for compatability with existing callers; all that is needed is an inline function forwarding to `process`. Removes the return type from `start` - in all cipher implementations, this always returned an empty vector. Adds BOTAN_ARG_CHECK macro; right now BOTAN_ASSERT is being used for argument checking in some places, which is not right at all. --- src/lib/modes/xts/xts.cpp | 20 +++++++------------- src/lib/modes/xts/xts.h | 6 +++--- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'src/lib/modes/xts') diff --git a/src/lib/modes/xts/xts.cpp b/src/lib/modes/xts/xts.cpp index b369fde29..1993bf15f 100644 --- a/src/lib/modes/xts/xts.cpp +++ b/src/lib/modes/xts/xts.cpp @@ -105,7 +105,7 @@ void XTS_Mode::key_schedule(const byte key[], size_t length) m_tweak_cipher->set_key(&key[key_half], key_half); } -secure_vector XTS_Mode::start_raw(const byte nonce[], size_t nonce_len) +void XTS_Mode::start_msg(const byte nonce[], size_t nonce_len) { if(!valid_nonce_length(nonce_len)) throw Invalid_IV_Length(name(), nonce_len); @@ -114,8 +114,6 @@ secure_vector XTS_Mode::start_raw(const byte nonce[], size_t nonce_len) m_tweak_cipher->encrypt(m_tweak.data()); update_tweak(0); - - return secure_vector(); } void XTS_Mode::update_tweak(size_t which) @@ -136,12 +134,8 @@ size_t XTS_Encryption::output_length(size_t input_length) const return input_length; } -void XTS_Encryption::update(secure_vector& buffer, size_t offset) +size_t XTS_Encryption::process(uint8_t buf[], size_t sz) { - BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); - const size_t sz = buffer.size() - offset; - byte* buf = buffer.data() + offset; - const size_t BS = cipher().block_size(); BOTAN_ASSERT(sz % BS == 0, "Input is full blocks"); @@ -163,6 +157,8 @@ void XTS_Encryption::update(secure_vector& buffer, size_t offset) update_tweak(to_proc); } + + return sz; } void XTS_Encryption::finish(secure_vector& buffer, size_t offset) @@ -214,12 +210,8 @@ size_t XTS_Decryption::output_length(size_t input_length) const return input_length; } -void XTS_Decryption::update(secure_vector& buffer, size_t offset) +size_t XTS_Decryption::process(uint8_t buf[], size_t sz) { - BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); - const size_t sz = buffer.size() - offset; - byte* buf = buffer.data() + offset; - const size_t BS = cipher().block_size(); BOTAN_ASSERT(sz % BS == 0, "Input is full blocks"); @@ -241,6 +233,8 @@ void XTS_Decryption::update(secure_vector& buffer, size_t offset) update_tweak(to_proc); } + + return sz; } void XTS_Decryption::finish(secure_vector& buffer, size_t offset) diff --git a/src/lib/modes/xts/xts.h b/src/lib/modes/xts/xts.h index e751b1644..6c4ba8d99 100644 --- a/src/lib/modes/xts/xts.h +++ b/src/lib/modes/xts/xts.h @@ -42,7 +42,7 @@ class BOTAN_DLL XTS_Mode : public Cipher_Mode void update_tweak(size_t last_used); private: - secure_vector start_raw(const byte nonce[], size_t nonce_len) override; + void start_msg(const byte nonce[], size_t nonce_len) override; void key_schedule(const byte key[], size_t length) override; std::unique_ptr m_cipher, m_tweak_cipher; @@ -57,7 +57,7 @@ class BOTAN_DLL XTS_Encryption final : public XTS_Mode public: explicit XTS_Encryption(BlockCipher* cipher) : XTS_Mode(cipher) {} - void update(secure_vector& blocks, size_t offset = 0) override; + size_t process(uint8_t buf[], size_t size) override; void finish(secure_vector& final_block, size_t offset = 0) override; @@ -72,7 +72,7 @@ class BOTAN_DLL XTS_Decryption final : public XTS_Mode public: explicit XTS_Decryption(BlockCipher* cipher) : XTS_Mode(cipher) {} - void update(secure_vector& blocks, size_t offset = 0) override; + size_t process(uint8_t buf[], size_t size) override; void finish(secure_vector& final_block, size_t offset = 0) override; -- cgit v1.2.3