blob: 6e22cf01bf248b3e4248bfff5abf2f50b545e4da [file] [log] [blame]
Tom Josephd08b5232017-01-24 18:15:39 +05301#pragma once
2
3#include <openssl/sha.h>
4#include <array>
5#include <vector>
6
7namespace cipher
8{
9
10namespace crypt
11{
12
13using buffer = std::vector<uint8_t>;
14using key = std::array<uint8_t, SHA_DIGEST_LENGTH>;
15
16/**
17 * @enum Confidentiality Algorithms
18 *
19 * The Confidentiality Algorithm Number specifies the encryption/decryption
20 * algorithm field that is used for encrypted payload data under the session.
21 * The ‘encrypted’ bit in the payload type field being set identifies packets
22 * with payloads that include data that is encrypted per this specification.
23 * When payload data is encrypted, there may be additional “Confidentiality
24 * Header” and/or “Confidentiality Trailer” fields that are included within the
25 * payload. The size and definition of those fields is specific to the
26 * particular confidentiality algorithm.
27 */
28enum class Algorithms : uint8_t
29{
30 NONE, /**< No encryption (mandatory option) */
31 AES_CBC_128, /**< AES-CBC-128 Algorithm (mandatory option) */
32 xRC4_128, /**< xRC4-128 Algorithm (optional option) */
33 xRC4_40, /**< xRC4-40 Algorithm (optional option) */
34};
35
36/**
37 * @class Interface
38 *
39 * Interface is the base class for the Confidentiality Algorithms.
40 */
41class Interface
42{
43 public:
44 /**
45 * @brief Constructor for Interface
46 *
47 * @param[in] - Session Integrity key to generate K2
48 * @param[in] - Additional keying material to generate K2
49 */
50 explicit Interface(const buffer& sik, const key& addKey);
51
52 Interface() = delete;
53 virtual ~Interface() = default;
54 Interface(const Interface&) = default;
55 Interface& operator=(const Interface&) = default;
56 Interface(Interface&&) = default;
57 Interface& operator=(Interface&&) = default;
58
59 /**
60 * @brief Decrypt the incoming payload
61 *
62 * @param[in] packet - Incoming IPMI packet
63 * @param[in] sessHeaderLen - Length of the IPMI Session Header
64 * @param[in] payloadLen - Length of the encrypted IPMI payload
65 *
66 * @return decrypted payload if the operation is successful
67 */
68 virtual buffer decryptPayload(
69 const buffer& packet,
70 const size_t sessHeaderLen,
71 const size_t payloadLen) const = 0;
72
73 /**
74 * @brief Encrypt the outgoing payload
75 *
76 * @param[in] payload - plain payload for the outgoing IPMI packet
77 *
78 * @return encrypted payload if the operation is successful
79 *
80 */
81 virtual buffer encryptPayload(buffer& payload) const = 0;
82
83 protected:
84
Tom Joseph54690652017-02-16 20:25:23 +053085 /**
86 * @brief The Cipher Key is the first 128-bits of key “K2”, K2 is
87 * generated by processing a pre-defined constant keyed by Session
88 * Integrity Key (SIK) that was created during session activation.
89 */
Tom Josephd08b5232017-01-24 18:15:39 +053090 key k2;
91};
92
Tom Joseph06f7a3c2017-01-25 14:32:05 +053093/**
94 * @class AlgoAES128
95 *
96 * @brief Implementation of the AES-CBC-128 Confidentiality algorithm
97 *
98 * AES-128 uses a 128-bit Cipher Key. The Cipher Key is the first 128-bits of
99 * key “K2”.Once the Cipher Key has been generated it is used to encrypt
100 * the payload data. The payload data is padded to make it an integral numbers
101 * of blocks in length (a block is 16 bytes for AES). The payload is then
102 * encrypted one block at a time from the lowest data offset to the highest
103 * using Cipher_Key as specified in AES.
104 */
105class AlgoAES128 final : public Interface
106{
107 public:
108 static constexpr size_t AESCBC128ConfHeader = 16;
109 static constexpr size_t AESCBC128BlockSize = 16;
110
111 /**
112 * RSP needs more keying material than can be provided by session
113 * integrity key alone. As a result all keying material for the RSP
114 * confidentiality algorithms will be generated by processing a
115 * pre-defined set of constants using HMAC per [RFC2104], keyed by SIK.
116 * These constants are constructed using a hexadecimal octet value
117 * repeated up to the HMAC block size in length starting with the
118 * constant 01h. This mechanism can be used to derive up to 255
119 * HMAC-block-length pieces of keying material from a single SIK.For the
120 * mandatory confidentiality algorithm AES-CBC-128, processing the
121 * following constant will generate the required amount of keying
122 * material.
123 */
124 static constexpr key const2 = { 0x02, 0x02, 0x02, 0x02, 0x02,
125 0x02, 0x02, 0x02, 0x02, 0x02,
126 0x02, 0x02, 0x02, 0x02, 0x02,
127 0x02, 0x02, 0x02, 0x02, 0x02
128 };
129
130 /**
131 * If confidentiality bytes are present, the value of the first byte is
132 * one (01h). and all subsequent bytes shall have a monotonically
133 * increasing value (e.g., 02h, 03h, 04h, etc). The receiver, as an
134 * additional check for proper decryption, shall check the value of each
135 * byte of Confidentiality Pad. For AES algorithm, the pad bytes will
136 * range from 0 to 15 bytes. This predefined array would help in
137 * doing the additional check.
138 */
139 static constexpr std::array<uint8_t, AESCBC128BlockSize -1>
140 confPadBytes =
141 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
142 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
143
144 /**
145 * @brief Constructor for AlgoAES128
146 *
147 * @param[in] - Session Integrity key
148 */
149 explicit AlgoAES128(const buffer& sik) : Interface(sik, const2) {}
150
151 AlgoAES128() = delete;
152 ~AlgoAES128() = default;
153 AlgoAES128(const AlgoAES128&) = default;
154 AlgoAES128& operator=(const AlgoAES128&) = default;
155 AlgoAES128(AlgoAES128&&) = default;
156 AlgoAES128& operator=(AlgoAES128&&) = default;
157
158 /**
159 * @brief Decrypt the incoming payload
160 *
161 * @param[in] packet - Incoming IPMI packet
162 * @param[in] sessHeaderLen - Length of the IPMI Session Header
163 * @param[in] payloadLen - Length of the encrypted IPMI payload
164 *
165 * @return decrypted payload if the operation is successful
166 */
167 buffer decryptPayload(
168 const buffer& packet,
169 const size_t sessHeaderLen,
170 const size_t payloadLen) const override;
171
172 /**
173 * @brief Encrypt the outgoing payload
174 *
175 * @param[in] payload - plain payload for the outgoing IPMI packet
176 *
177 * @return encrypted payload if the operation is successful
178 *
179 */
180 buffer encryptPayload(buffer& payload) const override;
181
182 private:
183
184 /**
185 * @brief Decrypt the passed data
186 *
187 * @param[in] iv - Initialization vector
188 * @param[in] input - Pointer to input data
189 * @param[in] inputLen - Length of input data
190 *
191 * @return decrypted data if the operation is successful
192 */
193 buffer decryptData(const uint8_t* iv,
194 const uint8_t* input,
195 const int inputLen) const;
196
197 /**
198 * @brief Encrypt the passed data
199 *
200 * @param[in] input - Pointer to input data
201 * @param[in] inputLen - Length of input data
202 *
203 * @return encrypted data if the operation is successful
204 */
205 buffer encryptData(const uint8_t* input,
206 const int inputLen) const;
207};
208
Tom Josephd08b5232017-01-24 18:15:39 +0530209}// namespace crypt
210
211}// namespace cipher
212