Fabcoin Core  0.16.2
P2P Digital Currency
poly1305.h
Go to the documentation of this file.
1 // poly1305.h - written and placed in the public domain by Jeffrey Walton and Jean-Pierre Munch
2 // Based on Andy Polyakov's Base-2^26 scalar multiplication implementation for OpenSSL.
3 // Copyright assigned to the Crypto++ project
4 
44 
45 #ifndef CRYPTOPP_POLY1305_H
46 #define CRYPTOPP_POLY1305_H
47 
48 #include "cryptlib.h"
49 #include "seckey.h"
50 #include "secblock.h"
51 #include "argnames.h"
52 #include "algparam.h"
53 
55 
56 template <class T>
62 {
63  CRYPTOPP_COMPILE_ASSERT(T::DEFAULT_KEYLENGTH == 16);
64  CRYPTOPP_COMPILE_ASSERT(T::BLOCKSIZE == 16);
65 
66 public:
67  static std::string StaticAlgorithmName() {return std::string("Poly1305(") + T::StaticAlgorithmName() + ")";}
68 
69  CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
70  CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
71 
72  Poly1305_Base() : m_used(true) {}
73 
74  void Resynchronize (const byte *iv, int ivLength=-1);
75  void GetNextIV (RandomNumberGenerator &rng, byte *iv);
76 
77  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
78  void Update(const byte *input, size_t length);
79  void TruncatedFinal(byte *mac, size_t size);
80  void Restart();
81 
82  unsigned int BlockSize() const {return BLOCKSIZE;}
83  unsigned int DigestSize() const {return DIGESTSIZE;}
84 
85 protected:
86  void HashBlocks(const byte *input, size_t length, word32 padbit);
87  void HashFinal(byte *mac, size_t length);
88 
89  typename T::Encryption m_cipher;
90 
91  // Accumulated hash, clamped r-key, and encrypted nonce
95 
96  // Accumulated message bytes and index
98  size_t m_idx;
99 
100  // Track nonce reuse; assert in debug but continue
101  bool m_used;
102 };
103 
146 template <class T>
147 class Poly1305 : public MessageAuthenticationCodeFinal<Poly1305_Base<T> >
148 {
149 public:
151 
152 
153  Poly1305() {}
154 
164  Poly1305(const byte *key, size_t keyLength=DEFAULT_KEYLENGTH, const byte *nonce=NULL, size_t nonceLength=0)
165  {this->SetKey(key, keyLength, MakeParameters(Name::IV(), ConstByteArrayParameter(nonce, nonceLength)));}
166 };
167 
169 
170 #endif // CRYPTOPP_POLY1305_H
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
Standard names for retrieving values by name when working with NameValuePairs.
Interface for message authentication codes.
Definition: cryptlib.h:1111
FixedSizeAlignedSecBlock< byte, BLOCKSIZE > m_acc
Definition: poly1305.h:97
Poly1305 message authentication code base class.
Definition: poly1305.h:61
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:127
Classes for working with NameValuePairs.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: poly1305.h:83
uint8_t byte
Definition: Common.h:57
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: cryptlib.cpp:97
FixedSizeAlignedSecBlock< word32, 5 > m_h
Definition: poly1305.h:92
#define T(i, x)
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static std::string StaticAlgorithmName()
Definition: poly1305.h:67
Abstract base classes that provide a uniform interface to this library.
T::Encryption m_cipher
Definition: poly1305.h:89
FixedSizeAlignedSecBlock< word32, 4 > m_r
Definition: poly1305.h:93
Interface for random number generators.
Definition: cryptlib.h:1188
Provides class member functions to key a message authentication code.
Definition: seckey.h:379
Classes and functions for secure memory allocations.
FixedSizeAlignedSecBlock< word32, 4 > m_n
Definition: poly1305.h:94
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
size_t m_idx
Definition: poly1305.h:98
Classes and functions for implementing secret key algorithms.
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:524
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
Poly1305 message authentication code.
Definition: poly1305.h:147
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
uint8_t const size_t const size
Definition: sha3.h:20
#define NAMESPACE_END
Definition: config.h:201
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: poly1305.h:82
Poly1305(const byte *key, size_t keyLength=DEFAULT_KEYLENGTH, const byte *nonce=NULL, size_t nonceLength=0)
Construct a Poly1305.
Definition: poly1305.h:164
unsigned int word32
Definition: config.h:231
Interface for retrieving values given their names.
Definition: cryptlib.h:279