File indexing completed on 2025-01-18 09:54:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef CRYPTOPP_CHACHA_POLY1305_H
0015 #define CRYPTOPP_CHACHA_POLY1305_H
0016
0017 #include "cryptlib.h"
0018 #include "authenc.h"
0019 #include "chacha.h"
0020 #include "poly1305.h"
0021
0022 NAMESPACE_BEGIN(CryptoPP)
0023
0024
0025
0026
0027
0028
0029 class ChaCha20Poly1305_Base : public AuthenticatedSymmetricCipherBase
0030 {
0031 public:
0032 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName()
0033 {return "ChaCha20/Poly1305";}
0034
0035 virtual ~ChaCha20Poly1305_Base() {}
0036
0037
0038 std::string AlgorithmName() const
0039 {return std::string("ChaCha20/Poly1305");}
0040 std::string AlgorithmProvider() const
0041 {return GetSymmetricCipher().AlgorithmProvider();}
0042 size_t MinKeyLength() const
0043 {return 32;}
0044 size_t MaxKeyLength() const
0045 {return 32;}
0046 size_t DefaultKeyLength() const
0047 {return 32;}
0048 size_t GetValidKeyLength(size_t n) const
0049 {CRYPTOPP_UNUSED(n); return 32;}
0050 bool IsValidKeyLength(size_t n) const
0051 {return n==32;}
0052 unsigned int OptimalDataAlignment() const
0053 {return GetSymmetricCipher().OptimalDataAlignment();}
0054 IV_Requirement IVRequirement() const
0055 {return UNIQUE_IV;}
0056 unsigned int IVSize() const
0057 {return 12;}
0058 unsigned int MinIVLength() const
0059 {return 12;}
0060 unsigned int MaxIVLength() const
0061 {return 12;}
0062 unsigned int DigestSize() const
0063 {return 16;}
0064 lword MaxHeaderLength() const
0065 {return LWORD_MAX;}
0066 lword MaxMessageLength() const
0067 {return W64LIT(274877906880);}
0068 lword MaxFooterLength() const
0069 {return 0;}
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 virtual void EncryptAndAuthenticate(byte *ciphertext, byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *aad, size_t aadLength, const byte *message, size_t messageLength);
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 virtual bool DecryptAndVerify(byte *message, const byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *aad, size_t aadLength, const byte *ciphertext, size_t ciphertextLength);
0101
0102 protected:
0103
0104 bool AuthenticationIsOnPlaintext() const {return false;}
0105 unsigned int AuthenticationBlockSize() const {return 1;}
0106 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms);
0107 void Resync(const byte *iv, size_t len);
0108 size_t AuthenticateBlocks(const byte *data, size_t len);
0109 void AuthenticateLastHeaderBlock();
0110 void AuthenticateLastConfidentialBlock();
0111 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
0112
0113
0114 void RekeyCipherAndMac(const byte *userKey, size_t userKeyLength, const NameValuePairs ¶ms);
0115
0116 virtual const MessageAuthenticationCode & GetMAC() const = 0;
0117 virtual MessageAuthenticationCode & AccessMAC() = 0;
0118
0119 private:
0120 SecByteBlock m_userKey;
0121 };
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 template <bool T_IsEncryption>
0133 class ChaCha20Poly1305_Final : public ChaCha20Poly1305_Base
0134 {
0135 public:
0136 virtual ~ChaCha20Poly1305_Final() {}
0137
0138 protected:
0139 const SymmetricCipher & GetSymmetricCipher()
0140 {return const_cast<ChaCha20Poly1305_Final *>(this)->AccessSymmetricCipher();}
0141 SymmetricCipher & AccessSymmetricCipher()
0142 {return m_cipher;}
0143 bool IsForwardTransformation() const
0144 {return T_IsEncryption;}
0145
0146 const MessageAuthenticationCode & GetMAC() const
0147 {return const_cast<ChaCha20Poly1305_Final *>(this)->AccessMAC();}
0148 MessageAuthenticationCode & AccessMAC()
0149 {return m_mac;}
0150
0151 private:
0152 ChaChaTLS::Encryption m_cipher;
0153 Poly1305TLS m_mac;
0154 };
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 struct ChaCha20Poly1305 : public AuthenticatedSymmetricCipherDocumentation
0165 {
0166
0167 typedef ChaCha20Poly1305_Final<true> Encryption;
0168
0169 typedef ChaCha20Poly1305_Final<false> Decryption;
0170 };
0171
0172
0173
0174
0175
0176
0177 class XChaCha20Poly1305_Base : public AuthenticatedSymmetricCipherBase
0178 {
0179 public:
0180 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName()
0181 {return "XChaCha20/Poly1305";}
0182
0183 virtual ~XChaCha20Poly1305_Base() {}
0184
0185
0186 std::string AlgorithmName() const
0187 {return std::string("XChaCha20/Poly1305");}
0188 std::string AlgorithmProvider() const
0189 {return GetSymmetricCipher().AlgorithmProvider();}
0190 size_t MinKeyLength() const
0191 {return 32;}
0192 size_t MaxKeyLength() const
0193 {return 32;}
0194 size_t DefaultKeyLength() const
0195 {return 32;}
0196 size_t GetValidKeyLength(size_t n) const
0197 {CRYPTOPP_UNUSED(n); return 32;}
0198 bool IsValidKeyLength(size_t n) const
0199 {return n==32;}
0200 unsigned int OptimalDataAlignment() const
0201 {return GetSymmetricCipher().OptimalDataAlignment();}
0202 IV_Requirement IVRequirement() const
0203 {return UNIQUE_IV;}
0204 unsigned int IVSize() const
0205 {return 24;}
0206 unsigned int MinIVLength() const
0207 {return 24;}
0208 unsigned int MaxIVLength() const
0209 {return 24;}
0210 unsigned int DigestSize() const
0211 {return 16;}
0212 lword MaxHeaderLength() const
0213 {return LWORD_MAX;}
0214 lword MaxMessageLength() const
0215 {return W64LIT(274877906880);}
0216 lword MaxFooterLength() const
0217 {return 0;}
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231 virtual void EncryptAndAuthenticate(byte *ciphertext, byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *aad, size_t aadLength, const byte *message, size_t messageLength);
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 virtual bool DecryptAndVerify(byte *message, const byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *aad, size_t aadLength, const byte *ciphertext, size_t ciphertextLength);
0249
0250 protected:
0251
0252 bool AuthenticationIsOnPlaintext() const {return false;}
0253 unsigned int AuthenticationBlockSize() const {return 1;}
0254 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms);
0255 void Resync(const byte *iv, size_t len);
0256 size_t AuthenticateBlocks(const byte *data, size_t len);
0257 void AuthenticateLastHeaderBlock();
0258 void AuthenticateLastConfidentialBlock();
0259 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
0260
0261
0262 void RekeyCipherAndMac(const byte *userKey, size_t userKeyLength, const NameValuePairs ¶ms);
0263
0264 virtual const MessageAuthenticationCode & GetMAC() const = 0;
0265 virtual MessageAuthenticationCode & AccessMAC() = 0;
0266
0267 private:
0268 SecByteBlock m_userKey;
0269 };
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 template <bool T_IsEncryption>
0281 class XChaCha20Poly1305_Final : public XChaCha20Poly1305_Base
0282 {
0283 public:
0284 virtual ~XChaCha20Poly1305_Final() {}
0285
0286 protected:
0287 const SymmetricCipher & GetSymmetricCipher()
0288 {return const_cast<XChaCha20Poly1305_Final *>(this)->AccessSymmetricCipher();}
0289 SymmetricCipher & AccessSymmetricCipher()
0290 {return m_cipher;}
0291 bool IsForwardTransformation() const
0292 {return T_IsEncryption;}
0293
0294 const MessageAuthenticationCode & GetMAC() const
0295 {return const_cast<XChaCha20Poly1305_Final *>(this)->AccessMAC();}
0296 MessageAuthenticationCode & AccessMAC()
0297 {return m_mac;}
0298
0299 private:
0300 XChaCha20::Encryption m_cipher;
0301 Poly1305TLS m_mac;
0302 };
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 struct XChaCha20Poly1305 : public AuthenticatedSymmetricCipherDocumentation
0313 {
0314
0315 typedef XChaCha20Poly1305_Final<true> Encryption;
0316
0317 typedef XChaCha20Poly1305_Final<false> Decryption;
0318 };
0319
0320 NAMESPACE_END
0321
0322 #endif