Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/cham.cpp
Line
Count
Source (jump to first uncovered line)
1
// cham.cpp - 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
#include "pch.h"
7
#include "config.h"
8
9
#include "cham.h"
10
#include "misc.h"
11
#include "cpu.h"
12
13
//                 CHAM table of parameters
14
//  +-------------------------------------------------
15
//  +cipher          n      k      r     w      k/w
16
//  +-------------------------------------------------
17
//  +CHAM-64/128     64     128    80    16     8
18
//  +CHAM-128/128    128    128    80    32     4
19
//  +CHAM-128/256    128    256    96    32     8
20
//  +-------------------------------------------------
21
22
ANONYMOUS_NAMESPACE_BEGIN
23
24
using CryptoPP::rotlConstant;
25
using CryptoPP::rotrConstant;
26
27
/// \brief CHAM encryption round
28
/// \tparam RR the round number residue
29
/// \tparam KW the number of key words
30
/// \tparam T words type
31
/// \param x the state array
32
/// \param k the subkey table
33
/// \param i the round number
34
/// \details CHAM_EncRound applies the encryption round to the plain text.
35
///  RR is the "round residue" and it is used modulo 4. ProcessAndXorBlock
36
///  may provide a fully unrolled encryption transformation, or provide
37
///  a transformation that loops using multiples of 4 encryption rounds.
38
/// \details CHAM_EncRound calculates indexes into the x[] array based
39
///  on the round number residue. There is no need for the assignments
40
///  that shift values in preparations for the next round.
41
/// \details CHAM_EncRound depends on the round number. The actual round
42
///  being executed is passed through the parameter <tt>i</tt>. If
43
///  ProcessAndXorBlock fully unrolled the loop then the parameter
44
///  <tt>i</tt> would be unnecessary.
45
template <unsigned int RR, unsigned int KW, class T>
46
inline void CHAM_EncRound(T x[4], const T k[KW], unsigned int i)
47
640
{
48
640
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
640
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
640
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
640
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
640
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
640
    const T kk = k[i % KW];
56
640
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
640
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
640
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
640
}
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<0u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<1u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<2u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<3u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<4u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<5u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<6u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<7u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<8u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<9u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<10u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<11u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<12u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<13u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<14u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<15u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
cham.cpp:void (anonymous namespace)::CHAM_EncRound<0u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<1u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<2u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<3u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<4u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<5u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<6u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
cham.cpp:void (anonymous namespace)::CHAM_EncRound<7u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Line
Count
Source
47
80
{
48
80
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49
80
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50
80
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51
80
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52
80
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
53
54
    // Follows conventions in the ref impl
55
80
    const T kk = k[i % KW];
56
80
    const T aa = x[IDX0] ^ static_cast<T>(i);
57
80
    const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58
80
    x[IDX3] = rotlConstant<R2>(static_cast<T>(aa + bb));
59
80
}
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<0u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<1u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<2u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<3u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<4u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<5u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<6u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<7u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<8u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<9u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<10u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<11u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<12u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<13u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<14u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_EncRound<15u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
60
61
/// \brief CHAM decryption round
62
/// \tparam RR the round number residue
63
/// \tparam KW the number of key words
64
/// \tparam T words type
65
/// \param x the state array
66
/// \param k the subkey table
67
/// \param i the round number
68
/// \details CHAM_DecRound applies the decryption round to the cipher text.
69
///  RR is the "round residue" and it is used modulo 4. ProcessAndXorBlock
70
///  may provide a fully unrolled decryption transformation, or provide
71
///  a transformation that loops using multiples of 4 decryption rounds.
72
/// \details CHAM_DecRound calculates indexes into the x[] array based
73
///  on the round number residue. There is no need for the assignments
74
///  that shift values in preparations for the next round.
75
/// \details CHAM_DecRound depends on the round number. The actual round
76
///  being executed is passed through the parameter <tt>i</tt>. If
77
///  ProcessAndXorBlock fully unrolled the loop then the parameter
78
///  <tt>i</tt> would be unnecessary.
79
template <unsigned int RR, unsigned int KW, class T>
80
inline void CHAM_DecRound(T x[4], const T k[KW], unsigned int i)
81
0
{
82
0
    CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
83
0
    CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
84
0
    CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
85
0
    CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 8 : 1);
86
0
    CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 1 : 8);
87
88
    // Follows conventions in the ref impl
89
0
    const T kk = k[i % KW];
90
0
    const T aa = rotrConstant<R1>(x[IDX3]);
91
0
    const T bb = rotlConstant<R2>(x[IDX1]) ^ kk;
92
0
    x[IDX0] = static_cast<T>(aa - bb) ^ static_cast<T>(i);
93
0
}
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<15u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<14u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<13u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<12u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<11u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<10u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<9u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<8u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<7u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<6u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<5u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<4u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<3u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<2u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<1u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<0u, 16u, unsigned short>(unsigned short*, unsigned short const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<7u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<6u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<5u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<4u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<3u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<2u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<1u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<0u, 8u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<15u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<14u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<13u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<12u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<11u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<10u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<9u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<8u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<7u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<6u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<5u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<4u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<3u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<2u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<1u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
Unexecuted instantiation: cham.cpp:void (anonymous namespace)::CHAM_DecRound<0u, 16u, unsigned int>(unsigned int*, unsigned int const*, unsigned int)
94
95
ANONYMOUS_NAMESPACE_END
96
97
NAMESPACE_BEGIN(CryptoPP)
98
99
#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
100
# if (CRYPTOPP_SSSE3_AVAILABLE)
101
extern size_t CHAM64_Enc_AdvancedProcessBlocks_SSSE3(const word16* subKeys, size_t rounds,
102
    const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags);
103
104
extern size_t CHAM64_Dec_AdvancedProcessBlocks_SSSE3(const word16* subKeys, size_t rounds,
105
    const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags);
106
107
extern size_t CHAM128_Enc_AdvancedProcessBlocks_SSSE3(const word32* subKeys, size_t rounds,
108
    const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags);
109
110
extern size_t CHAM128_Dec_AdvancedProcessBlocks_SSSE3(const word32* subKeys, size_t rounds,
111
    const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags);
112
# endif  // CRYPTOPP_SSSE3_AVAILABLE
113
#endif  // CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
114
115
void CHAM64::Base::UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params)
116
1
{
117
1
    CRYPTOPP_UNUSED(params);
118
1
    m_kw = keyLength/sizeof(word16);
119
1
    m_rk.New(2*m_kw);
120
121
5
    for (size_t i = 0; i < m_kw; userKey += sizeof(word32))
122
4
    {
123
        // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
124
4
        const word32 rk = GetWord<word32>(false, BIG_ENDIAN_ORDER, userKey);
125
126
4
        const word16 rk1 = static_cast<word16>(rk >> 16);
127
4
        m_rk[i] = rk1 ^ rotlConstant<1>(rk1) ^ rotlConstant<8>(rk1);
128
4
        m_rk[(i + m_kw) ^ 1] = rk1 ^ rotlConstant<1>(rk1) ^ rotlConstant<11>(rk1);
129
4
        i++;
130
131
4
        const word16 rk2 = static_cast<word16>(rk & 0xffff);
132
4
        m_rk[i] = rk2 ^ rotlConstant<1>(rk2) ^ rotlConstant<8>(rk2);
133
4
        m_rk[(i + m_kw) ^ 1] = rk2 ^ rotlConstant<1>(rk2) ^ rotlConstant<11>(rk2);
134
4
        i++;
135
4
    }
136
1
}
137
138
void CHAM64::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
139
0
{
140
    // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
141
0
    GetBlock<word16, BigEndian> iblock(inBlock);
142
0
    iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
143
144
0
    const int R = 80;
145
0
    for (int i = 0; i < R; i+=16)
146
0
    {
147
0
        CHAM_EncRound< 0, 16>(m_x.begin(), m_rk.begin(),  i+0);
148
0
        CHAM_EncRound< 1, 16>(m_x.begin(), m_rk.begin(),  i+1);
149
0
        CHAM_EncRound< 2, 16>(m_x.begin(), m_rk.begin(),  i+2);
150
0
        CHAM_EncRound< 3, 16>(m_x.begin(), m_rk.begin(),  i+3);
151
0
        CHAM_EncRound< 4, 16>(m_x.begin(), m_rk.begin(),  i+4);
152
0
        CHAM_EncRound< 5, 16>(m_x.begin(), m_rk.begin(),  i+5);
153
0
        CHAM_EncRound< 6, 16>(m_x.begin(), m_rk.begin(),  i+6);
154
0
        CHAM_EncRound< 7, 16>(m_x.begin(), m_rk.begin(),  i+7);
155
0
        CHAM_EncRound< 8, 16>(m_x.begin(), m_rk.begin(),  i+8);
156
0
        CHAM_EncRound< 9, 16>(m_x.begin(), m_rk.begin(),  i+9);
157
0
        CHAM_EncRound<10, 16>(m_x.begin(), m_rk.begin(), i+10);
158
0
        CHAM_EncRound<11, 16>(m_x.begin(), m_rk.begin(), i+11);
159
0
        CHAM_EncRound<12, 16>(m_x.begin(), m_rk.begin(), i+12);
160
0
        CHAM_EncRound<13, 16>(m_x.begin(), m_rk.begin(), i+13);
161
0
        CHAM_EncRound<14, 16>(m_x.begin(), m_rk.begin(), i+14);
162
0
        CHAM_EncRound<15, 16>(m_x.begin(), m_rk.begin(), i+15);
163
0
    }
164
165
0
    PutBlock<word16, BigEndian> oblock(xorBlock, outBlock);
166
0
    oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
167
0
}
168
169
void CHAM64::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
170
0
{
171
    // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
172
0
    GetBlock<word16, BigEndian> iblock(inBlock);
173
0
    iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
174
175
0
    const int R = 80;
176
0
    for (int i = R-1; i >=0 ; i-=16)
177
0
    {
178
0
        CHAM_DecRound<15, 16>(m_x.begin(), m_rk.begin(),  i-0);
179
0
        CHAM_DecRound<14, 16>(m_x.begin(), m_rk.begin(),  i-1);
180
0
        CHAM_DecRound<13, 16>(m_x.begin(), m_rk.begin(),  i-2);
181
0
        CHAM_DecRound<12, 16>(m_x.begin(), m_rk.begin(),  i-3);
182
0
        CHAM_DecRound<11, 16>(m_x.begin(), m_rk.begin(),  i-4);
183
0
        CHAM_DecRound<10, 16>(m_x.begin(), m_rk.begin(),  i-5);
184
0
        CHAM_DecRound< 9, 16>(m_x.begin(), m_rk.begin(),  i-6);
185
0
        CHAM_DecRound< 8, 16>(m_x.begin(), m_rk.begin(),  i-7);
186
0
        CHAM_DecRound< 7, 16>(m_x.begin(), m_rk.begin(),  i-8);
187
0
        CHAM_DecRound< 6, 16>(m_x.begin(), m_rk.begin(),  i-9);
188
0
        CHAM_DecRound< 5, 16>(m_x.begin(), m_rk.begin(), i-10);
189
0
        CHAM_DecRound< 4, 16>(m_x.begin(), m_rk.begin(), i-11);
190
0
        CHAM_DecRound< 3, 16>(m_x.begin(), m_rk.begin(), i-12);
191
0
        CHAM_DecRound< 2, 16>(m_x.begin(), m_rk.begin(), i-13);
192
0
        CHAM_DecRound< 1, 16>(m_x.begin(), m_rk.begin(), i-14);
193
0
        CHAM_DecRound< 0, 16>(m_x.begin(), m_rk.begin(), i-15);
194
0
    }
195
196
0
    PutBlock<word16, BigEndian> oblock(xorBlock, outBlock);
197
0
    oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
198
0
}
199
200
std::string CHAM128::Base::AlgorithmProvider() const
201
0
{
202
0
#if defined(CRYPTOPP_SSSE3_AVAILABLE)
203
0
    if (HasSSSE3())
204
0
        return "SSSE3";
205
0
#endif
206
0
    return "C++";
207
0
}
208
209
void CHAM128::Base::UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params)
210
9
{
211
9
    CRYPTOPP_UNUSED(params);
212
9
    m_kw = keyLength/sizeof(word32);
213
9
    m_rk.New(2*m_kw);
214
215
45
    for (size_t i = 0; i < m_kw; userKey += sizeof(word32))
216
36
    {
217
        // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
218
36
        const word32 rk = GetWord<word32>(false, BIG_ENDIAN_ORDER, userKey);
219
36
        m_rk[i] = rk ^ rotlConstant<1>(rk) ^ rotlConstant<8>(rk);
220
36
        m_rk[(i + m_kw) ^ 1] = rk ^ rotlConstant<1>(rk) ^ rotlConstant<11>(rk);
221
36
        i++;
222
36
    }
223
9
}
224
225
void CHAM128::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
226
8
{
227
    // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
228
8
    GetBlock<word32, BigEndian> iblock(inBlock);
229
8
    iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
230
231
8
    switch (m_kw)
232
8
    {
233
8
    case 4:  // 128-bit key
234
8
    {
235
8
        const int R = 80;
236
88
        for (int i = 0; i < R; i+=8)
237
80
        {
238
80
            CHAM_EncRound<0, 8>(m_x.begin(), m_rk.begin(), i+0);
239
80
            CHAM_EncRound<1, 8>(m_x.begin(), m_rk.begin(), i+1);
240
80
            CHAM_EncRound<2, 8>(m_x.begin(), m_rk.begin(), i+2);
241
80
            CHAM_EncRound<3, 8>(m_x.begin(), m_rk.begin(), i+3);
242
80
            CHAM_EncRound<4, 8>(m_x.begin(), m_rk.begin(), i+4);
243
80
            CHAM_EncRound<5, 8>(m_x.begin(), m_rk.begin(), i+5);
244
80
            CHAM_EncRound<6, 8>(m_x.begin(), m_rk.begin(), i+6);
245
80
            CHAM_EncRound<7, 8>(m_x.begin(), m_rk.begin(), i+7);
246
80
        }
247
8
        break;
248
0
    }
249
0
    case 8:  // 256-bit key
250
0
    {
251
0
        const int R = 96;
252
0
        for (int i = 0; i < R; i+=16)
253
0
        {
254
0
            CHAM_EncRound< 0, 16>(m_x.begin(), m_rk.begin(),  i+0);
255
0
            CHAM_EncRound< 1, 16>(m_x.begin(), m_rk.begin(),  i+1);
256
0
            CHAM_EncRound< 2, 16>(m_x.begin(), m_rk.begin(),  i+2);
257
0
            CHAM_EncRound< 3, 16>(m_x.begin(), m_rk.begin(),  i+3);
258
0
            CHAM_EncRound< 4, 16>(m_x.begin(), m_rk.begin(),  i+4);
259
0
            CHAM_EncRound< 5, 16>(m_x.begin(), m_rk.begin(),  i+5);
260
0
            CHAM_EncRound< 6, 16>(m_x.begin(), m_rk.begin(),  i+6);
261
0
            CHAM_EncRound< 7, 16>(m_x.begin(), m_rk.begin(),  i+7);
262
0
            CHAM_EncRound< 8, 16>(m_x.begin(), m_rk.begin(),  i+8);
263
0
            CHAM_EncRound< 9, 16>(m_x.begin(), m_rk.begin(),  i+9);
264
0
            CHAM_EncRound<10, 16>(m_x.begin(), m_rk.begin(), i+10);
265
0
            CHAM_EncRound<11, 16>(m_x.begin(), m_rk.begin(), i+11);
266
0
            CHAM_EncRound<12, 16>(m_x.begin(), m_rk.begin(), i+12);
267
0
            CHAM_EncRound<13, 16>(m_x.begin(), m_rk.begin(), i+13);
268
0
            CHAM_EncRound<14, 16>(m_x.begin(), m_rk.begin(), i+14);
269
0
            CHAM_EncRound<15, 16>(m_x.begin(), m_rk.begin(), i+15);
270
0
        }
271
0
        break;
272
0
    }
273
0
    default:
274
0
        CRYPTOPP_ASSERT(0);
275
8
    }
276
277
8
    PutBlock<word32, BigEndian> oblock(xorBlock, outBlock);
278
8
    oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
279
8
}
280
281
void CHAM128::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
282
0
{
283
    // Do not cast the buffer. It will SIGBUS on some ARM and SPARC.
284
0
    GetBlock<word32, BigEndian> iblock(inBlock);
285
0
    iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
286
287
0
    switch (m_kw)
288
0
    {
289
0
    case 4:  // 128-bit key
290
0
    {
291
0
        const int R = 80;
292
0
        for (int i = R-1; i >= 0; i-=8)
293
0
        {
294
0
            CHAM_DecRound<7, 8>(m_x.begin(), m_rk.begin(), i-0);
295
0
            CHAM_DecRound<6, 8>(m_x.begin(), m_rk.begin(), i-1);
296
0
            CHAM_DecRound<5, 8>(m_x.begin(), m_rk.begin(), i-2);
297
0
            CHAM_DecRound<4, 8>(m_x.begin(), m_rk.begin(), i-3);
298
0
            CHAM_DecRound<3, 8>(m_x.begin(), m_rk.begin(), i-4);
299
0
            CHAM_DecRound<2, 8>(m_x.begin(), m_rk.begin(), i-5);
300
0
            CHAM_DecRound<1, 8>(m_x.begin(), m_rk.begin(), i-6);
301
0
            CHAM_DecRound<0, 8>(m_x.begin(), m_rk.begin(), i-7);
302
0
        }
303
0
        break;
304
0
    }
305
0
    case 8:  // 256-bit key
306
0
    {
307
0
        const int R = 96;
308
0
        for (int i = R-1; i >= 0; i-=16)
309
0
        {
310
0
            CHAM_DecRound<15, 16>(m_x.begin(), m_rk.begin(),  i-0);
311
0
            CHAM_DecRound<14, 16>(m_x.begin(), m_rk.begin(),  i-1);
312
0
            CHAM_DecRound<13, 16>(m_x.begin(), m_rk.begin(),  i-2);
313
0
            CHAM_DecRound<12, 16>(m_x.begin(), m_rk.begin(),  i-3);
314
0
            CHAM_DecRound<11, 16>(m_x.begin(), m_rk.begin(),  i-4);
315
0
            CHAM_DecRound<10, 16>(m_x.begin(), m_rk.begin(),  i-5);
316
0
            CHAM_DecRound< 9, 16>(m_x.begin(), m_rk.begin(),  i-6);
317
0
            CHAM_DecRound< 8, 16>(m_x.begin(), m_rk.begin(),  i-7);
318
0
            CHAM_DecRound< 7, 16>(m_x.begin(), m_rk.begin(),  i-8);
319
0
            CHAM_DecRound< 6, 16>(m_x.begin(), m_rk.begin(),  i-9);
320
0
            CHAM_DecRound< 5, 16>(m_x.begin(), m_rk.begin(), i-10);
321
0
            CHAM_DecRound< 4, 16>(m_x.begin(), m_rk.begin(), i-11);
322
0
            CHAM_DecRound< 3, 16>(m_x.begin(), m_rk.begin(), i-12);
323
0
            CHAM_DecRound< 2, 16>(m_x.begin(), m_rk.begin(), i-13);
324
0
            CHAM_DecRound< 1, 16>(m_x.begin(), m_rk.begin(), i-14);
325
0
            CHAM_DecRound< 0, 16>(m_x.begin(), m_rk.begin(), i-15);
326
0
        }
327
0
        break;
328
0
    }
329
0
    default:
330
0
        CRYPTOPP_ASSERT(0);
331
0
    }
332
333
0
    PutBlock<word32, BigEndian> oblock(xorBlock, outBlock);
334
0
    oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
335
0
}
336
337
#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
338
size_t CHAM128::Enc::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks,
339
        byte *outBlocks, size_t length, word32 flags) const
340
0
{
341
0
# if (CRYPTOPP_SSSE3_AVAILABLE)
342
0
    if (HasSSSE3()) {
343
0
        const size_t rounds = (m_kw == 4 ? 80 : 96);
344
0
        return CHAM128_Enc_AdvancedProcessBlocks_SSSE3(m_rk, rounds,
345
0
            inBlocks, xorBlocks, outBlocks, length, flags);
346
0
    }
347
0
# endif  // CRYPTOPP_SSSE3_AVAILABLE
348
0
    return BlockTransformation::AdvancedProcessBlocks(inBlocks, xorBlocks, outBlocks, length, flags);
349
0
}
350
351
size_t CHAM128::Dec::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks,
352
        byte *outBlocks, size_t length, word32 flags) const
353
0
{
354
0
# if (CRYPTOPP_SSSE3_AVAILABLE)
355
0
    if (HasSSSE3()) {
356
0
        const size_t rounds = (m_kw == 4 ? 80 : 96);
357
0
        return CHAM128_Dec_AdvancedProcessBlocks_SSSE3(m_rk, rounds,
358
0
            inBlocks, xorBlocks, outBlocks, length, flags);
359
0
    }
360
0
# endif  // CRYPTOPP_SSSE3_AVAILABLE
361
0
    return BlockTransformation::AdvancedProcessBlocks(inBlocks, xorBlocks, outBlocks, length, flags);
362
0
}
363
#endif  // CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
364
365
NAMESPACE_END