Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 1 | #include <openssl/evp.h> |
| 2 | #include <openssl/hmac.h> |
| 3 | #include <openssl/rand.h> |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 4 | #include <algorithm> |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 5 | #include <numeric> |
| 6 | #include "crypt_algo.hpp" |
| 7 | #include "message_parsers.hpp" |
| 8 | |
| 9 | namespace cipher |
| 10 | { |
| 11 | |
| 12 | namespace crypt |
| 13 | { |
| 14 | |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 15 | constexpr std::array<uint8_t, AlgoAES128::AESCBC128BlockSize - 1> |
| 16 | AlgoAES128::confPadBytes; |
| 17 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 18 | std::vector<uint8_t> AlgoAES128::decryptPayload( |
| 19 | const std::vector<uint8_t>& packet, |
| 20 | const size_t sessHeaderLen, |
| 21 | const size_t payloadLen) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 22 | { |
| 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 Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 51 | std::vector<uint8_t> AlgoAES128::encryptPayload( |
| 52 | std::vector<uint8_t>& payload) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 53 | { |
| 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 Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 89 | std::vector<uint8_t> AlgoAES128::decryptData(const uint8_t* iv, |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 90 | 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 98 | 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 Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 106 | /* |
| 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 111 | if (!EVP_DecryptInit_ex(ctxPtr.get(), EVP_aes_128_cbc(), NULL, k2.data(), |
| 112 | iv)) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 113 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 114 | 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 123 | EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 124 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 125 | std::vector<uint8_t> output(inputLen + AESCBC128BlockSize); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 126 | |
| 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 137 | if (!EVP_DecryptUpdate(ctxPtr.get(), output.data(), &outputLen, input, |
| 138 | inputLen)) |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 139 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 140 | throw std::runtime_error("EVP_DecryptUpdate failed"); |
| 141 | } |
| 142 | |
| 143 | output.resize(outputLen); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 144 | |
| 145 | return output; |
| 146 | } |
| 147 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 148 | std::vector<uint8_t> AlgoAES128::encryptData(const uint8_t* input, |
| 149 | const int inputLen) const |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 150 | { |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 151 | std::vector<uint8_t> output(inputLen + AESCBC128BlockSize); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 152 | |
| 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 164 | 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 Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 172 | /* |
| 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 177 | 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] | 178 | output.data())) |
| 179 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 180 | 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 189 | EVP_CIPHER_CTX_set_padding(ctxPtr.get(), 0); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 190 | |
| 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 Joseph | f6c97e1 | 2017-08-03 00:23:57 +0530 | [diff] [blame] | 201 | if (!EVP_EncryptUpdate(ctxPtr.get(), |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 202 | output.data() + AESCBC128ConfHeader, |
| 203 | &outputLen, |
| 204 | input, inputLen)) |
| 205 | { |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 206 | throw std::runtime_error("EVP_EncryptUpdate failed for type " |
| 207 | "AES-CBC-128"); |
| 208 | } |
| 209 | |
| 210 | output.resize(AESCBC128ConfHeader + outputLen); |
Tom Joseph | 518ecce | 2017-01-25 14:34:19 +0530 | [diff] [blame] | 211 | |
| 212 | return output; |
| 213 | } |
| 214 | |
Tom Joseph | d08b523 | 2017-01-24 18:15:39 +0530 | [diff] [blame] | 215 | }// namespace crypt |
| 216 | |
| 217 | }// namespace cipher |
| 218 | |
| 219 | |