Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "crypt_algo.hpp" |
| 2 | |
| 3 | #include "message_parsers.hpp" |
| 4 | |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 5 | #include <openssl/evp.h> |
| 6 | #include <openssl/hmac.h> |
| 7 | #include <openssl/rand.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 9 | #include <algorithm> |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 10 | #include <numeric> |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 11 | |
| 12 | namespace cipher |
| 13 | { |
| 14 | |
| 15 | namespace crypt |
| 16 | { |
| 17 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 18 | constexpr std::array<uint8_t, AlgoAES128::AESCBC128BlockSize - 1> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 19 | AlgoAES128::confPadBytes; |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 20 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 21 | std::vector<uint8_t> |
| 22 | AlgoAES128::decryptPayload(const std::vector<uint8_t>& packet, |
| 23 | const size_t sessHeaderLen, |
| 24 | const size_t payloadLen) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 25 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 26 | auto plainPayload = |
| 27 | decryptData(packet.data() + sessHeaderLen, |
| 28 | packet.data() + sessHeaderLen + AESCBC128ConfHeader, |
| 29 | payloadLen - AESCBC128ConfHeader); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * The confidentiality pad length is the last byte in the payload, it would |
| 33 | * tell the number of pad bytes in the payload. We added a condition, so |
Gunnar Mills | 62ec622 | 2018-04-08 16:28:23 -0500 | [diff] [blame] | 34 | * that buffer overrun doesn't happen. |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 35 | */ |
| 36 | size_t confPadLength = plainPayload.back(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 37 | auto padLength = std::min(plainPayload.size() - 1, confPadLength); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 38 | |
| 39 | auto plainPayloadLen = plainPayload.size() - padLength - 1; |
| 40 | |
| 41 | // Additional check if the confidentiality pad bytes are as expected |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 42 | if (!std::equal(plainPayload.begin() + plainPayloadLen, |
| 43 | plainPayload.begin() + plainPayloadLen + padLength, |
| 44 | confPadBytes.begin())) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 45 | { |
| 46 | throw std::runtime_error("Confidentiality pad bytes check failed"); |
| 47 | } |
| 48 | |
| 49 | plainPayload.resize(plainPayloadLen); |
| 50 | |
| 51 | return plainPayload; |
| 52 | } |
| 53 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 54 | std::vector<uint8_t> |
| 55 | AlgoAES128::encryptPayload(std::vector<uint8_t>& payload) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 56 | { |
| 57 | auto payloadLen = payload.size(); |
| 58 | |
| 59 | /* |
| 60 | * The following logic calculates the number of padding bytes to be added to |
| 61 | * the payload data. This would ensure that the length is a multiple of the |
| 62 | * block size of algorithm being used. For the AES algorithm, the block size |
| 63 | * is 16 bytes. |
| 64 | */ |
| 65 | auto paddingLen = AESCBC128BlockSize - ((payloadLen + 1) & 0xF); |
| 66 | |
| 67 | /* |
| 68 | * The additional field is for the Confidentiality Pad Length field. For the |
| 69 | * AES algorithm, this number will range from 0 to 15 bytes. This field is |
| 70 | * mandatory. |
| 71 | */ |
| 72 | payload.resize(payloadLen + paddingLen + 1); |
| 73 | |
| 74 | /* |
| 75 | * If no Confidentiality Pad bytes are required, the Confidentiality Pad |
| 76 | * Length field is set to 00h. If present, the value of the first byte of |
| 77 | * Confidentiality Pad shall be one (01h) and all subsequent bytes shall |
| 78 | * have a monotonically increasing value (e.g., 02h, 03h, 04h, etc). |
| 79 | */ |
| 80 | if (0 != paddingLen) |
| 81 | { |
| 82 | std::iota(payload.begin() + payloadLen, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 83 | payload.begin() + payloadLen + paddingLen, 1); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | payload.back() = paddingLen; |
| 87 | |
| 88 | return encryptData(payload.data(), payload.size()); |
| 89 | } |
| 90 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 91 | std::vector<uint8_t> AlgoAES128::decryptData(const uint8_t* iv, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 92 | const uint8_t* input, |
| 93 | const int inputLen) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 94 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 95 | // Initializes Cipher context |
Adriana Kobylak | 584fa88 | 2018-09-06 15:52:05 -0500 | [diff] [blame] | 96 | EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 97 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 98 | auto cleanupFunc = [](EVP_CIPHER_CTX* ctx) { EVP_CIPHER_CTX_free(ctx); }; |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 99 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 100 | std::unique_ptr<EVP_CIPHER_CTX, decltype(cleanupFunc)> ctxPtr(ctx, |
| 101 | cleanupFunc); |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 102 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 103 | /* |
| 104 | * EVP_DecryptInit_ex sets up cipher context ctx for encryption with type |
| 105 | * AES-CBC-128. ctx must be initialized before calling this function. K2 is |
| 106 | * the symmetric key used and iv is the initialization vector used. |
| 107 | */ |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 108 | if (!EVP_DecryptInit_ex(ctxPtr.get(), EVP_aes_128_cbc(), NULL, k2.data(), |
| 109 | iv)) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 110 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 111 | throw std::runtime_error("EVP_DecryptInit_ex failed for type " |
| 112 | "AES-CBC-128"); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * EVP_CIPHER_CTX_set_padding() enables or disables padding. If the pad |
| 117 | * parameter is zero then no padding is performed. This function always |
| 118 | * returns 1. |
| 119 | */ |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 120 | EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 121 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 122 | std::vector<uint8_t> output(inputLen + AESCBC128BlockSize); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 123 | |
| 124 | int outputLen = 0; |
| 125 | |
| 126 | /* |
| 127 | * If padding is disabled then EVP_DecryptFinal_ex() will not encrypt any |
| 128 | * more data and it will return an error if any data remains in a partial |
| 129 | * block: that is if the total data length is not a multiple of the block |
| 130 | * size. Since AES-CBC-128 encrypted payload format adds padding bytes and |
| 131 | * ensures that payload is a multiple of block size, we are not making the |
| 132 | * call to EVP_DecryptFinal_ex(). |
| 133 | */ |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 134 | if (!EVP_DecryptUpdate(ctxPtr.get(), output.data(), &outputLen, input, |
| 135 | inputLen)) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 136 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 137 | throw std::runtime_error("EVP_DecryptUpdate failed"); |
| 138 | } |
| 139 | |
| 140 | output.resize(outputLen); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 141 | |
| 142 | return output; |
| 143 | } |
| 144 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 145 | std::vector<uint8_t> AlgoAES128::encryptData(const uint8_t* input, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 146 | const int inputLen) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 147 | { |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 148 | std::vector<uint8_t> output(inputLen + AESCBC128BlockSize); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 149 | |
| 150 | // Generate the initialization vector |
| 151 | if (!RAND_bytes(output.data(), AESCBC128ConfHeader)) |
| 152 | { |
| 153 | throw std::runtime_error("RAND_bytes failed"); |
| 154 | } |
| 155 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 156 | // Initializes Cipher context |
Adriana Kobylak | 584fa88 | 2018-09-06 15:52:05 -0500 | [diff] [blame] | 157 | EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 158 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 159 | auto cleanupFunc = [](EVP_CIPHER_CTX* ctx) { EVP_CIPHER_CTX_free(ctx); }; |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 160 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 161 | std::unique_ptr<EVP_CIPHER_CTX, decltype(cleanupFunc)> ctxPtr(ctx, |
| 162 | cleanupFunc); |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 163 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 164 | /* |
| 165 | * EVP_EncryptInit_ex sets up cipher context ctx for encryption with type |
| 166 | * AES-CBC-128. ctx must be initialized before calling this function. K2 is |
| 167 | * the symmetric key used and iv is the initialization vector used. |
| 168 | */ |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 169 | if (!EVP_EncryptInit_ex(ctxPtr.get(), EVP_aes_128_cbc(), NULL, k2.data(), |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 170 | output.data())) |
| 171 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 172 | throw std::runtime_error("EVP_EncryptInit_ex failed for type " |
| 173 | "AES-CBC-128"); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * EVP_CIPHER_CTX_set_padding() enables or disables padding. If the pad |
| 178 | * parameter is zero then no padding is performed. This function always |
| 179 | * returns 1. |
| 180 | */ |
Tom Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 181 | EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 182 | |
| 183 | int outputLen = 0; |
| 184 | |
| 185 | /* |
| 186 | * If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any |
| 187 | * more data and it will return an error if any data remains in a partial |
| 188 | * block: that is if the total data length is not a multiple of the block |
| 189 | * size. Since we are adding padding bytes and ensures that payload is a |
| 190 | * multiple of block size, we are not making the call to |
| 191 | * EVP_DecryptFinal_ex() |
| 192 | */ |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 193 | if (!EVP_EncryptUpdate(ctxPtr.get(), output.data() + AESCBC128ConfHeader, |
| 194 | &outputLen, input, inputLen)) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 195 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 196 | throw std::runtime_error("EVP_EncryptUpdate failed for type " |
| 197 | "AES-CBC-128"); |
| 198 | } |
| 199 | |
| 200 | output.resize(AESCBC128ConfHeader + outputLen); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 201 | |
| 202 | return output; |
| 203 | } |
| 204 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 205 | } // namespace crypt |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 206 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 207 | } // namespace cipher |