Skip to content

Commit

Permalink
Cipher_Mode::finish() allows arbitrary containers
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Mar 20, 2023
1 parent 57b4429 commit 7fc8b1f
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/lib/modes/aead/ccm/ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ secure_vector<uint8_t> CCM_Mode::format_c0()
return C;
}

void CCM_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CCM_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");

Expand Down Expand Up @@ -227,7 +227,7 @@ void CCM_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
reset();
}

void CCM_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CCM_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");

Expand Down
10 changes: 6 additions & 4 deletions src/lib/modes/aead/ccm/ccm.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ class CCM_Encryption final : public CCM_Mode
CCM_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16, size_t L = 3) :
CCM_Mode(std::move(cipher), tag_size, L) {}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override
{ return input_length + tag_size(); }

size_t minimum_final_size() const override { return 0; }

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -116,15 +117,16 @@ class CCM_Decryption final : public CCM_Mode
CCM_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16, size_t L = 3) :
CCM_Mode(std::move(cipher), tag_size, L) {}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override
{
BOTAN_ARG_CHECK(input_length >= tag_size(), "Sufficient input");
return input_length - tag_size();
}

size_t minimum_final_size() const override { return tag_size(); }

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ size_t ChaCha20Poly1305_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void ChaCha20Poly1305_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void ChaCha20Poly1305_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
update(buffer, offset);
if(cfrg_version())
Expand Down Expand Up @@ -141,7 +141,7 @@ size_t ChaCha20Poly1305_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void ChaCha20Poly1305_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void ChaCha20Poly1305_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
const size_t sz = buffer.size() - offset;
Expand Down
6 changes: 2 additions & 4 deletions src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ class ChaCha20Poly1305_Encryption final : public ChaCha20Poly1305_Mode

size_t minimum_final_size() const override { return 0; }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -97,10 +96,9 @@ class ChaCha20Poly1305_Decryption final : public ChaCha20Poly1305_Mode

size_t minimum_final_size() const override { return tag_size(); }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/eax/eax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ size_t EAX_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void EAX_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void EAX_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_STATE_CHECK(!m_nonce_mac.empty());
update(buffer, offset);
Expand Down Expand Up @@ -164,7 +164,7 @@ size_t EAX_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void EAX_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void EAX_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
const size_t sz = buffer.size() - offset;
Expand Down
6 changes: 2 additions & 4 deletions src/lib/modes/aead/eax/eax.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ class EAX_Encryption final : public EAX_Mode

size_t minimum_final_size() const override { return 0; }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -112,10 +111,9 @@ class EAX_Decryption final : public EAX_Mode

size_t minimum_final_size() const override { return tag_size(); }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/gcm/gcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ size_t GCM_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void GCM_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void GCM_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(offset <= buffer.size(), "Invalid offset");
const size_t sz = buffer.size() - offset;
Expand All @@ -158,7 +158,7 @@ size_t GCM_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void GCM_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void GCM_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(offset <= buffer.size(), "Invalid offset");
const size_t sz = buffer.size() - offset;
Expand Down
6 changes: 2 additions & 4 deletions src/lib/modes/aead/gcm/gcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ class GCM_Encryption final : public GCM_Mode

size_t minimum_final_size() const override { return 0; }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -110,10 +109,9 @@ class GCM_Decryption final : public GCM_Mode

size_t minimum_final_size() const override { return tag_size(); }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/ocb/ocb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ size_t OCB_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void OCB_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void OCB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
assert_key_material_set();
BOTAN_STATE_CHECK(m_L->initialized());
Expand Down Expand Up @@ -473,7 +473,7 @@ size_t OCB_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void OCB_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void OCB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
assert_key_material_set();
BOTAN_STATE_CHECK(m_L->initialized());
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/ocb/ocb.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ class BOTAN_TEST_API OCB_Encryption final : public OCB_Mode

size_t minimum_final_size() const override { return 0; }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
private:
void encrypt(uint8_t input[], size_t blocks);
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

class BOTAN_TEST_API OCB_Decryption final : public OCB_Mode
Expand All @@ -126,10 +126,10 @@ class BOTAN_TEST_API OCB_Decryption final : public OCB_Mode

size_t minimum_final_size() const override { return tag_size(); }

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
private:
void decrypt(uint8_t input[], size_t blocks);
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/aead/siv/siv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void SIV_Mode::set_ctr_iv(secure_vector<uint8_t> V)
ctr().set_iv(V.data(), V.size());
}

void SIV_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void SIV_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");

Expand All @@ -182,7 +182,7 @@ void SIV_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
}
}

void SIV_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void SIV_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");

Expand Down
10 changes: 6 additions & 4 deletions src/lib/modes/aead/siv/siv.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ class BOTAN_TEST_API SIV_Encryption final : public SIV_Mode
explicit SIV_Encryption(std::unique_ptr<BlockCipher> cipher) :
SIV_Mode(std::move(cipher)) {}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override
{ return input_length + tag_size(); }

size_t minimum_final_size() const override { return 0; }

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -119,15 +120,16 @@ class BOTAN_TEST_API SIV_Decryption final : public SIV_Mode
explicit SIV_Decryption(std::unique_ptr<BlockCipher> cipher) :
SIV_Mode(std::move(cipher)) {}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override
{
BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
return input_length - tag_size();
}

size_t minimum_final_size() const override { return tag_size(); }

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/modes/cbc/cbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ size_t CBC_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void CBC_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CBC_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_STATE_CHECK(state().empty() == false);
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
Expand Down Expand Up @@ -166,7 +166,7 @@ size_t CTS_Encryption::output_length(size_t input_length) const
return input_length; // no ciphertext expansion in CTS
}

void CTS_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_STATE_CHECK(state().empty() == false);
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
Expand Down Expand Up @@ -249,7 +249,7 @@ size_t CBC_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void CBC_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CBC_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_STATE_CHECK(state().empty() == false);
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
Expand Down Expand Up @@ -286,7 +286,7 @@ size_t CTS_Decryption::minimum_final_size() const
return block_size() + 1;
}

void CTS_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_STATE_CHECK(state().empty() == false);
BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
Expand Down
16 changes: 8 additions & 8 deletions src/lib/modes/cbc/cbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ class CBC_Encryption : public CBC_Mode
std::unique_ptr<BlockCipherModePaddingMethod> padding) :
CBC_Mode(std::move(cipher), std::move(padding)) {}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override;

size_t minimum_final_size() const override;

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -106,11 +105,12 @@ class CTS_Encryption final : public CBC_Encryption

size_t output_length(size_t input_length) const override;

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t minimum_final_size() const override;

bool valid_nonce_length(size_t n) const override;

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

/**
Expand All @@ -129,8 +129,6 @@ class CBC_Decryption : public CBC_Mode
m_tempbuf(ideal_granularity())
{}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t output_length(size_t input_length) const override;

size_t minimum_final_size() const override;
Expand All @@ -139,6 +137,7 @@ class CBC_Decryption : public CBC_Mode

private:
size_t process_msg(uint8_t buf[], size_t size) override;
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

secure_vector<uint8_t> m_tempbuf;
};
Expand All @@ -156,11 +155,12 @@ class CTS_Decryption final : public CBC_Decryption
CBC_Decryption(std::move(cipher), nullptr)
{}

void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;

size_t minimum_final_size() const override;

bool valid_nonce_length(size_t n) const override;

private:
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes/cfb/cfb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ size_t CFB_Encryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void CFB_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CFB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
update(buffer, offset);
}
Expand Down Expand Up @@ -232,7 +232,7 @@ size_t CFB_Decryption::process_msg(uint8_t buf[], size_t sz)
return sz;
}

void CFB_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
void CFB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset)
{
update(buffer, offset);
}
Expand Down
Loading

0 comments on commit 7fc8b1f

Please sign in to comment.