Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/cham.h
Line
Count
Source
1
// cham.h - written and placed in the public domain by Kim Sung Hee and Jeffrey Walton
2
//          Based on "CHAM: A Family of Lightweight Block Ciphers for
3
//          Resource-Constrained Devices" by Bonwook Koo, Dongyoung Roh,
4
//          Hyeonjin Kim, Younghoon Jung, Dong-Geon Lee, and Daesung Kwon
5
6
/// \file cham.h
7
/// \brief Classes for the CHAM block cipher
8
/// \since Crypto++ 8.0
9
10
#ifndef CRYPTOPP_CHAM_H
11
#define CRYPTOPP_CHAM_H
12
13
#include "config.h"
14
#include "seckey.h"
15
#include "secblock.h"
16
#include "algparam.h"
17
18
#if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)
19
# define CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS 1
20
#endif
21
22
// Yet another SunStudio/SunCC workaround. Failed self tests
23
// in SSE code paths on i386 for SunStudio 12.3 and below.
24
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
25
# undef CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
26
#endif
27
28
NAMESPACE_BEGIN(CryptoPP)
29
30
/// \brief CHAM block cipher information
31
/// \since Crypto++ 8.0
32
struct CHAM64_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
33
{
34
    /// \brief The algorithm name
35
    /// \return the algorithm name
36
    /// \details StaticAlgorithmName returns the algorithm's name as a static
37
    ///   member function.
38
    static const std::string StaticAlgorithmName()
39
18
    {
40
        // Format is Cipher-Blocksize
41
18
        return "CHAM-64";
42
18
    }
43
};
44
45
/// \brief CHAM block cipher information
46
/// \since Crypto++ 8.0
47
struct CHAM128_Info : public FixedBlockSize<16>, public VariableKeyLength<16,16,32,16>
48
{
49
    /// \brief The algorithm name
50
    /// \return the algorithm name
51
    /// \details StaticAlgorithmName returns the algorithm's name as a static
52
    ///   member function.
53
    static const std::string StaticAlgorithmName()
54
56
    {
55
        // Format is Cipher-Blocksize
56
56
        return "CHAM-128";
57
56
    }
58
};
59
60
/// \brief CHAM 64-bit block cipher
61
/// \details CHAM64 provides 64-bit block size. The valid key size is 128-bit.
62
/// \note Crypto++ provides a byte oriented implementation
63
/// \sa CHAM128, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,
64
///   <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">
65
///   CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>
66
/// \since Crypto++ 8.0
67
class CRYPTOPP_NO_VTABLE CHAM64 : public CHAM64_Info, public BlockCipherDocumentation
68
{
69
public:
70
    /// \brief CHAM block cipher transformation functions
71
    /// \details Provides implementation common to encryption and decryption
72
    /// \since Crypto++ 8.0
73
    class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM64_Info>
74
    {
75
    protected:
76
        void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
77
78
        SecBlock<word16> m_rk;
79
        mutable FixedSizeSecBlock<word16, 4> m_x;
80
        unsigned int m_kw;
81
    };
82
83
    /// \brief Encryption transformation
84
    /// \details Enc provides implementation for encryption transformation. All key and block
85
    ///   sizes are supported.
86
    /// \since Crypto++ 8.0
87
    class CRYPTOPP_NO_VTABLE Enc : public Base
88
    {
89
    public:
90
        void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
91
    };
92
93
    /// \brief Decryption transformation
94
    /// \details Dec provides implementation for decryption transformation. All key and block
95
    ///   sizes are supported.
96
    /// \since Crypto++ 8.0
97
    class CRYPTOPP_NO_VTABLE Dec : public Base
98
    {
99
    public:
100
        void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
101
    };
102
103
    /// \brief CHAM64 encryption
104
    typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
105
    /// \brief CHAM64 decryption
106
    typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
107
};
108
109
/// \brief CHAM64 encryption
110
typedef CHAM64::Encryption CHAM64Encryption;
111
/// \brief CHAM64 decryption
112
typedef CHAM64::Decryption CHAM64Decryption;
113
114
/// \brief CHAM 128-bit block cipher
115
/// \details CHAM128 provides 128-bit block size. The valid key size is 128-bit and 256-bit.
116
/// \note Crypto++ provides a byte oriented implementation
117
/// \sa CHAM64, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,
118
///   <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">
119
///   CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>
120
/// \since Crypto++ 8.0
121
class CRYPTOPP_NO_VTABLE CHAM128 : public CHAM128_Info, public BlockCipherDocumentation
122
{
123
public:
124
    /// \brief CHAM block cipher transformation functions
125
    /// \details Provides implementation common to encryption and decryption
126
    /// \since Crypto++ 8.0
127
    class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM128_Info>
128
    {
129
    protected:
130
        void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
131
        std::string AlgorithmProvider() const;
132
133
        SecBlock<word32> m_rk;
134
        mutable FixedSizeSecBlock<word32, 4> m_x;
135
        unsigned int m_kw;
136
    };
137
138
    /// \brief Encryption transformation
139
    /// \details Enc provides implementation for encryption transformation. All key and block
140
    ///   sizes are supported.
141
    /// \since Crypto++ 8.0
142
    class CRYPTOPP_NO_VTABLE Enc : public Base
143
    {
144
    public:
145
        void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
146
147
#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
148
        size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
149
#endif
150
    };
151
152
    /// \brief Decryption transformation
153
    /// \details Dec provides implementation for decryption transformation. All key and block
154
    ///   sizes are supported.
155
    /// \since Crypto++ 8.0
156
    class CRYPTOPP_NO_VTABLE Dec : public Base
157
    {
158
    public:
159
        void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
160
161
#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
162
        size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
163
#endif
164
    };
165
166
    /// \brief CHAM128 encryption
167
    typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
168
    /// \brief CHAM128 decryption
169
    typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
170
};
171
172
/// \brief CHAM128 encryption
173
typedef CHAM128::Encryption CHAM128Encryption;
174
/// \brief CHAM128 decryption
175
typedef CHAM128::Decryption CHAM128Decryption;
176
177
NAMESPACE_END
178
179
#endif  // CRYPTOPP_CHAM_H