Botan  1.11.15
src/lib/modes/aead/eax/eax.h
Go to the documentation of this file.
00001 /*
00002 * EAX Mode
00003 * (C) 1999-2007,2013 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_AEAD_EAX_H__
00009 #define BOTAN_AEAD_EAX_H__
00010 
00011 #include <botan/aead.h>
00012 #include <botan/block_cipher.h>
00013 #include <botan/stream_cipher.h>
00014 #include <botan/mac.h>
00015 
00016 namespace Botan {
00017 
00018 /**
00019 * EAX base class
00020 */
00021 class BOTAN_DLL EAX_Mode : public AEAD_Mode
00022    {
00023    public:
00024       void set_associated_data(const byte ad[], size_t ad_len) override;
00025 
00026       std::string name() const override;
00027 
00028       size_t update_granularity() const override;
00029 
00030       Key_Length_Specification key_spec() const override;
00031 
00032       // EAX supports arbitrary nonce lengths
00033       bool valid_nonce_length(size_t) const override { return true; }
00034 
00035       size_t tag_size() const override { return m_tag_size; }
00036 
00037       void clear() override;
00038    protected:
00039       /**
00040       * @param cipher the cipher to use
00041       * @param tag_size is how big the auth tag will be
00042       */
00043       EAX_Mode(BlockCipher* cipher, size_t tag_size);
00044 
00045       size_t block_size() const { return m_cipher->block_size(); }
00046 
00047       size_t m_tag_size;
00048 
00049       std::unique_ptr<BlockCipher> m_cipher;
00050       std::unique_ptr<StreamCipher> m_ctr;
00051       std::unique_ptr<MessageAuthenticationCode> m_cmac;
00052 
00053       secure_vector<byte> m_ad_mac;
00054 
00055       secure_vector<byte> m_nonce_mac;
00056    private:
00057       secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override;
00058 
00059       void key_schedule(const byte key[], size_t length) override;
00060    };
00061 
00062 /**
00063 * EAX Encryption
00064 */
00065 class BOTAN_DLL EAX_Encryption : public EAX_Mode
00066    {
00067    public:
00068       /**
00069       * @param cipher a 128-bit block cipher
00070       * @param tag_size is how big the auth tag will be
00071       */
00072       EAX_Encryption(BlockCipher* cipher, size_t tag_size = 0) :
00073          EAX_Mode(cipher, tag_size) {}
00074 
00075       size_t output_length(size_t input_length) const override
00076          { return input_length + tag_size(); }
00077 
00078       size_t minimum_final_size() const override { return 0; }
00079 
00080       void update(secure_vector<byte>& blocks, size_t offset = 0) override;
00081 
00082       void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
00083    };
00084 
00085 /**
00086 * EAX Decryption
00087 */
00088 class BOTAN_DLL EAX_Decryption : public EAX_Mode
00089    {
00090    public:
00091       /**
00092       * @param cipher a 128-bit block cipher
00093       * @param tag_size is how big the auth tag will be
00094       */
00095       EAX_Decryption(BlockCipher* cipher, size_t tag_size = 0) :
00096          EAX_Mode(cipher, tag_size) {}
00097 
00098       size_t output_length(size_t input_length) const override
00099          {
00100          BOTAN_ASSERT(input_length > tag_size(), "Sufficient input");
00101          return input_length - tag_size();
00102          }
00103 
00104       size_t minimum_final_size() const override { return tag_size(); }
00105 
00106       void update(secure_vector<byte>& blocks, size_t offset = 0) override;
00107 
00108       void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
00109    };
00110 
00111 }
00112 
00113 #endif