blob: a10fd56b24488df13d7efe75f387c23948493df2 [file] [log] [blame]
Tom Josephd08b5232017-01-24 18:15:39 +05301#include <openssl/evp.h>
2#include <openssl/hmac.h>
3#include <openssl/rand.h>
Tom Joseph518ecce2017-01-25 14:34:19 +05304#include <algorithm>
Tom Josephd08b5232017-01-24 18:15:39 +05305#include <numeric>
6#include "crypt_algo.hpp"
7#include "message_parsers.hpp"
8
9namespace cipher
10{
11
12namespace crypt
13{
14
Tom Joseph518ecce2017-01-25 14:34:19 +053015constexpr std::array<uint8_t, AlgoAES128::AESCBC128BlockSize - 1>
16 AlgoAES128::confPadBytes;
17
Vernon Mauery70fd29c2017-11-30 13:11:43 -080018std::vector<uint8_t> AlgoAES128::decryptPayload(
19 const std::vector<uint8_t>& packet,
20 const size_t sessHeaderLen,
21 const size_t payloadLen) const
Tom Joseph518ecce2017-01-25 14:34:19 +053022{
23 auto plainPayload = decryptData(
24 packet.data() + sessHeaderLen,
25 packet.data() + sessHeaderLen + AESCBC128ConfHeader,
26 payloadLen - AESCBC128ConfHeader);
27
28 /*
29 * The confidentiality pad length is the last byte in the payload, it would
30 * tell the number of pad bytes in the payload. We added a condition, so
31 * that buffer overrun does't happen.
32 */
33 size_t confPadLength = plainPayload.back();
34 auto padLength = std::min(plainPayload.size() -1, confPadLength);
35
36 auto plainPayloadLen = plainPayload.size() - padLength - 1;
37
38 // Additional check if the confidentiality pad bytes are as expected
39 if(!std::equal(plainPayload.begin() + plainPayloadLen,
40 plainPayload.begin() + plainPayloadLen + padLength,
41 confPadBytes.begin()))
42 {
43 throw std::runtime_error("Confidentiality pad bytes check failed");
44 }
45
46 plainPayload.resize(plainPayloadLen);
47
48 return plainPayload;
49}
50
Vernon Mauery70fd29c2017-11-30 13:11:43 -080051std::vector<uint8_t> AlgoAES128::encryptPayload(
52 std::vector<uint8_t>& payload) const
Tom Joseph518ecce2017-01-25 14:34:19 +053053{
54 auto payloadLen = payload.size();
55
56 /*
57 * The following logic calculates the number of padding bytes to be added to
58 * the payload data. This would ensure that the length is a multiple of the
59 * block size of algorithm being used. For the AES algorithm, the block size
60 * is 16 bytes.
61 */
62 auto paddingLen = AESCBC128BlockSize - ((payloadLen + 1) & 0xF);
63
64 /*
65 * The additional field is for the Confidentiality Pad Length field. For the
66 * AES algorithm, this number will range from 0 to 15 bytes. This field is
67 * mandatory.
68 */
69 payload.resize(payloadLen + paddingLen + 1);
70
71 /*
72 * If no Confidentiality Pad bytes are required, the Confidentiality Pad
73 * Length field is set to 00h. If present, the value of the first byte of
74 * Confidentiality Pad shall be one (01h) and all subsequent bytes shall
75 * have a monotonically increasing value (e.g., 02h, 03h, 04h, etc).
76 */
77 if (0 != paddingLen)
78 {
79 std::iota(payload.begin() + payloadLen,
80 payload.begin() + payloadLen + paddingLen,
81 1);
82 }
83
84 payload.back() = paddingLen;
85
86 return encryptData(payload.data(), payload.size());
87}
88
Vernon Mauery70fd29c2017-11-30 13:11:43 -080089std::vector<uint8_t> AlgoAES128::decryptData(const uint8_t* iv,
Tom Joseph518ecce2017-01-25 14:34:19 +053090 const uint8_t* input,
91 const int inputLen) const
92{
93 EVP_CIPHER_CTX ctx;
94
95 // Initializes Cipher context
96 EVP_CIPHER_CTX_init(&ctx);
97
Tom Josephf6c97e12017-08-03 00:23:57 +053098 auto cleanupFunc = [](EVP_CIPHER_CTX* ctx)
99 {
100 EVP_CIPHER_CTX_cleanup(ctx);
101 };
102
103 std::unique_ptr<EVP_CIPHER_CTX, decltype(cleanupFunc)>
104 ctxPtr(&ctx, cleanupFunc);
105
Tom Joseph518ecce2017-01-25 14:34:19 +0530106 /*
107 * EVP_DecryptInit_ex sets up cipher context ctx for encryption with type
108 * AES-CBC-128. ctx must be initialized before calling this function. K2 is
109 * the symmetric key used and iv is the initialization vector used.
110 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530111 if (!EVP_DecryptInit_ex(ctxPtr.get(), EVP_aes_128_cbc(), NULL, k2.data(),
112 iv))
Tom Joseph518ecce2017-01-25 14:34:19 +0530113 {
Tom Joseph518ecce2017-01-25 14:34:19 +0530114 throw std::runtime_error("EVP_DecryptInit_ex failed for type "
115 "AES-CBC-128");
116 }
117
118 /*
119 * EVP_CIPHER_CTX_set_padding() enables or disables padding. If the pad
120 * parameter is zero then no padding is performed. This function always
121 * returns 1.
122 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530123 EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0);
Tom Joseph518ecce2017-01-25 14:34:19 +0530124
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800125 std::vector<uint8_t> output(inputLen + AESCBC128BlockSize);
Tom Joseph518ecce2017-01-25 14:34:19 +0530126
127 int outputLen = 0;
128
129 /*
130 * If padding is disabled then EVP_DecryptFinal_ex() will not encrypt any
131 * more data and it will return an error if any data remains in a partial
132 * block: that is if the total data length is not a multiple of the block
133 * size. Since AES-CBC-128 encrypted payload format adds padding bytes and
134 * ensures that payload is a multiple of block size, we are not making the
135 * call to EVP_DecryptFinal_ex().
136 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530137 if (!EVP_DecryptUpdate(ctxPtr.get(), output.data(), &outputLen, input,
138 inputLen))
Tom Joseph518ecce2017-01-25 14:34:19 +0530139 {
Tom Joseph518ecce2017-01-25 14:34:19 +0530140 throw std::runtime_error("EVP_DecryptUpdate failed");
141 }
142
143 output.resize(outputLen);
Tom Joseph518ecce2017-01-25 14:34:19 +0530144
145 return output;
146}
147
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800148std::vector<uint8_t> AlgoAES128::encryptData(const uint8_t* input,
149 const int inputLen) const
Tom Joseph518ecce2017-01-25 14:34:19 +0530150{
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800151 std::vector<uint8_t> output(inputLen + AESCBC128BlockSize);
Tom Joseph518ecce2017-01-25 14:34:19 +0530152
153 // Generate the initialization vector
154 if (!RAND_bytes(output.data(), AESCBC128ConfHeader))
155 {
156 throw std::runtime_error("RAND_bytes failed");
157 }
158
159 EVP_CIPHER_CTX ctx;
160
161 // Initializes Cipher context
162 EVP_CIPHER_CTX_init(&ctx);
163
Tom Josephf6c97e12017-08-03 00:23:57 +0530164 auto cleanupFunc = [](EVP_CIPHER_CTX* ctx)
165 {
166 EVP_CIPHER_CTX_cleanup(ctx);
167 };
168
169 std::unique_ptr<EVP_CIPHER_CTX, decltype(cleanupFunc)>
170 ctxPtr(&ctx, cleanupFunc);
171
Tom Joseph518ecce2017-01-25 14:34:19 +0530172 /*
173 * EVP_EncryptInit_ex sets up cipher context ctx for encryption with type
174 * AES-CBC-128. ctx must be initialized before calling this function. K2 is
175 * the symmetric key used and iv is the initialization vector used.
176 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530177 if (!EVP_EncryptInit_ex(ctxPtr.get(), EVP_aes_128_cbc(), NULL, k2.data(),
Tom Joseph518ecce2017-01-25 14:34:19 +0530178 output.data()))
179 {
Tom Joseph518ecce2017-01-25 14:34:19 +0530180 throw std::runtime_error("EVP_EncryptInit_ex failed for type "
181 "AES-CBC-128");
182 }
183
184 /*
185 * EVP_CIPHER_CTX_set_padding() enables or disables padding. If the pad
186 * parameter is zero then no padding is performed. This function always
187 * returns 1.
188 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530189 EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0);
Tom Joseph518ecce2017-01-25 14:34:19 +0530190
191 int outputLen = 0;
192
193 /*
194 * If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any
195 * more data and it will return an error if any data remains in a partial
196 * block: that is if the total data length is not a multiple of the block
197 * size. Since we are adding padding bytes and ensures that payload is a
198 * multiple of block size, we are not making the call to
199 * EVP_DecryptFinal_ex()
200 */
Tom Josephf6c97e12017-08-03 00:23:57 +0530201 if (!EVP_EncryptUpdate(ctxPtr.get(),
Tom Joseph518ecce2017-01-25 14:34:19 +0530202 output.data() + AESCBC128ConfHeader,
203 &outputLen,
204 input, inputLen))
205 {
Tom Joseph518ecce2017-01-25 14:34:19 +0530206 throw std::runtime_error("EVP_EncryptUpdate failed for type "
207 "AES-CBC-128");
208 }
209
210 output.resize(AESCBC128ConfHeader + outputLen);
Tom Joseph518ecce2017-01-25 14:34:19 +0530211
212 return output;
213}
214
Tom Josephd08b5232017-01-24 18:15:39 +0530215}// namespace crypt
216
217}// namespace cipher
218
219