Crypto++ 8.7
Free C++ class library of cryptographic schemes
panama.h
Go to the documentation of this file.
1// panama.h - originally written and placed in the public domain by Wei Dai
2
3/// \file panama.h
4/// \brief Classes for Panama hash and stream cipher
5
6#ifndef CRYPTOPP_PANAMA_H
7#define CRYPTOPP_PANAMA_H
8
9#include "strciphr.h"
10#include "iterhash.h"
11#include "secblock.h"
12
13// Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler error with .intel_syntax
14//#if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
15//# define CRYPTOPP_DISABLE_PANAMA_ASM
16//#endif
17
18// https://github.com/weidai11/cryptopp/issues/758
19#define CRYPTOPP_DISABLE_PANAMA_ASM 1
20
21NAMESPACE_BEGIN(CryptoPP)
22
23// Base class, do not use directly
24template <class B>
25class CRYPTOPP_NO_VTABLE Panama
26{
27public:
28 virtual ~Panama() {}
29 std::string AlgorithmProvider() const;
30 void Reset();
31 void Iterate(size_t count, const word32 *p=NULLPTR, byte *output=NULLPTR, const byte *input=NULLPTR, KeystreamOperation operation=WRITE_KEYSTREAM);
32
33protected:
34 typedef word32 Stage[8];
35 CRYPTOPP_CONSTANT(STAGES = 32);
36
38};
39
40namespace Weak {
41/// \brief Panama hash
42/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
43template <class B = LittleEndian>
44class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
45{
46public:
47 CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
48 virtual ~PanamaHash() {}
50 unsigned int DigestSize() const {return DIGESTSIZE;}
51 void TruncatedFinal(byte *hash, size_t size);
52 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
53 std::string AlgorithmProvider() const {return Panama<B>::AlgorithmProvider();} // Fix https://github.com/weidai11/cryptopp/issues/801
54
55protected:
56 void Init() {Panama<B>::Reset();}
57 void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
58 size_t HashMultipleBlocks(const word32 *input, size_t length);
59 word32* StateBuf() {return NULLPTR;}
60
62};
63}
64
65/// \brief MAC construction using a hermetic hash function
66template <class T_Hash, class T_Info = T_Hash>
67class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
68{
69public:
70 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
71 {
72 CRYPTOPP_UNUSED(params);
73
74 m_key.Assign(key, length);
75 Restart();
76 }
77
78 void Restart()
79 {
80 m_hash.Restart();
81 m_keyed = false;
82 }
83
84 void Update(const byte *input, size_t length)
85 {
86 if (!m_keyed)
87 KeyHash();
88 m_hash.Update(input, length);
89 }
90
91 void TruncatedFinal(byte *digest, size_t digestSize)
92 {
93 if (!m_keyed)
94 KeyHash();
95 m_hash.TruncatedFinal(digest, digestSize);
96 m_keyed = false;
97 }
98
99 unsigned int DigestSize() const
100 {return m_hash.DigestSize();}
101 unsigned int BlockSize() const
102 {return m_hash.BlockSize();}
103 unsigned int OptimalBlockSize() const
104 {return m_hash.OptimalBlockSize();}
105 unsigned int OptimalDataAlignment() const
106 {return m_hash.OptimalDataAlignment();}
107
108protected:
109 void KeyHash()
110 {
111 m_hash.Update(m_key, m_key.size());
112 m_keyed = true;
113 }
114
115 T_Hash m_hash;
116 bool m_keyed;
117 SecByteBlock m_key;
118};
119
120namespace Weak {
121/// \brief Panama message authentication code
122template <class B = LittleEndian>
123class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
124{
125public:
126 PanamaMAC() {}
127 PanamaMAC(const byte *key, unsigned int length)
128 {this->SetKey(key, length);}
129};
130}
131
132/// \brief Panama stream cipher information
133template <class B>
134struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
135{
136 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
137};
138
139/// \brief Panama stream cipher operation
140template <class B>
142 public PanamaCipherInfo<B>,
143 protected Panama<B>
144{
145protected:
146 virtual ~PanamaCipherPolicy() {}
147 std::string AlgorithmProvider() const;
148 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
149 void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
150 bool CipherIsRandomAccess() const {return false;}
151 void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
152 unsigned int GetAlignment() const;
153
156};
157
158/// \brief Panama stream cipher
159/// \sa <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
160template <class B = LittleEndian>
162{
164 typedef Encryption Decryption;
165};
166
167NAMESPACE_END
168
169#endif
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:298
Base class information.
Definition: simple.h:40
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:125
MAC construction using a hermetic hash function.
Definition: panama.h:68
Interface for retrieving values given their names.
Definition: cryptlib.h:322
Panama stream cipher operation.
Definition: panama.h:144
Definition: panama.h:26
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:898
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:867
SecBlock<byte> typedef.
Definition: secblock.h:1226
SymmetricCipher implementation.
Definition: strciphr.h:684
Panama hash.
Definition: panama.h:45
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: panama.cpp:440
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: panama.h:53
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:50
Panama message authentication code.
Definition: panama.h:124
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition: cryptlib.h:147
Base classes for iterated hashes.
Crypto++ library namespace.
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
Classes and functions for secure memory allocations.
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
@ WRITE_KEYSTREAM
Write the keystream to the output buffer, input is NULL.
Definition: strciphr.h:90
Base class for additive stream ciphers.
Definition: strciphr.h:202
Panama stream cipher.
Definition: panama.h:162
Panama stream cipher information.
Definition: panama.h:135
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition: seckey.h:414