Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "crypto-helper.hpp" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 22 | #include <openssl/err.h> |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 23 | #include <openssl/hmac.h> |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 24 | #include <openssl/pem.h> |
Zhiyi Zhang | 2b6d80c | 2020-10-12 12:49:14 -0700 | [diff] [blame^] | 25 | #include <openssl/ec.h> |
| 26 | #include <openssl/evp.h> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 27 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/security/transform/base64-decode.hpp> |
| 29 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 30 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/security/transform/private-key.hpp> |
| 32 | #include <ndn-cxx/security/transform/signer-filter.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/security/transform/step-source.hpp> |
| 34 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace ndncert { |
| 38 | |
| 39 | const size_t HASH_SIZE = 32; |
| 40 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 41 | NDN_LOG_INIT(ndncert.cryptosupport); |
| 42 | |
| 43 | struct ECDHState::ECDH_CTX { |
| 44 | int EC_NID; |
| 45 | EVP_PKEY_CTX* ctx_params; |
| 46 | EVP_PKEY_CTX* ctx_keygen; |
| 47 | EVP_PKEY* privkey; |
| 48 | EVP_PKEY* peerkey; |
| 49 | EVP_PKEY* params; |
| 50 | }; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 51 | |
| 52 | ECDHState::ECDHState() |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 53 | : m_publicKeyLen(0) |
| 54 | , m_sharedSecretLen(0) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | { |
| 56 | OpenSSL_add_all_algorithms(); |
| 57 | context = std::make_unique<ECDH_CTX>(); |
| 58 | context->EC_NID = NID_X9_62_prime256v1; |
| 59 | |
| 60 | // Create the context for parameter generation |
| 61 | if (nullptr == (context->ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr))) { |
| 62 | handleErrors("Could not create context contexts."); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Initialise the parameter generation |
| 67 | if (EVP_PKEY_paramgen_init(context->ctx_params) != 1) { |
| 68 | handleErrors("Could not initialize parameter generation."); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // We're going to use the ANSI X9.62 Prime 256v1 curve |
| 73 | if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(context->ctx_params, context->EC_NID)) { |
| 74 | handleErrors("Likely unknown elliptical curve ID specified."); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Create the parameter object params |
| 79 | if (!EVP_PKEY_paramgen(context->ctx_params, &context->params)) { |
| 80 | // the generated key is written to context->params |
| 81 | handleErrors("Could not create parameter object parameters."); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Create the context for the key generation |
| 86 | if (nullptr == (context->ctx_keygen = EVP_PKEY_CTX_new(context->params, nullptr))) { |
| 87 | //The EVP_PKEY_CTX_new() function allocates public key algorithm context using |
| 88 | //the algorithm specified in pkey and ENGINE e (in this case nullptr). |
| 89 | handleErrors("Could not create the context for the key generation"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // initializes a public key algorithm context |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 94 | if (1 != EVP_PKEY_keygen_init(context->ctx_keygen)) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 95 | handleErrors("Could not init context for key generation."); |
| 96 | return; |
| 97 | } |
| 98 | if (1 != EVP_PKEY_keygen(context->ctx_keygen, &context->privkey)) { |
| 99 | //performs a key generation operation, the generated key is written to context->privkey. |
| 100 | handleErrors("Could not generate DHE keys in final step"); |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ECDHState::~ECDHState() |
| 106 | { |
| 107 | // Contexts |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 108 | if (context->ctx_params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | EVP_PKEY_CTX_free(context->ctx_params); |
| 110 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 111 | if (context->ctx_keygen != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 112 | EVP_PKEY_CTX_free(context->ctx_keygen); |
| 113 | } |
| 114 | |
| 115 | // Keys |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 116 | if (context->privkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | EVP_PKEY_free(context->privkey); |
| 118 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 119 | if (context->peerkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | EVP_PKEY_free(context->peerkey); |
| 121 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 122 | if (context->params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | EVP_PKEY_free(context->params); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | uint8_t* |
| 128 | ECDHState::getRawSelfPubKey() |
| 129 | { |
| 130 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 131 | |
| 132 | if (privECKey == nullptr) { |
| 133 | handleErrors("Could not get referenced key when calling EVP_PKEY_get1_EC_KEY()."); |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | auto ecPoint = EC_KEY_get0_public_key(privECKey); |
| 138 | const EC_GROUP* group = EC_KEY_get0_group(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 139 | m_publicKeyLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED, |
| 140 | m_publicKey, 256, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 142 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | handleErrors("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct."); |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 147 | return m_publicKey; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | std::string |
| 151 | ECDHState::getBase64PubKey() |
| 152 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 153 | namespace t = ndn::security::transform; |
| 154 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 155 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 156 | this->getRawSelfPubKey(); |
| 157 | } |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 158 | std::ostringstream os; |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 159 | t::bufferSource(m_publicKey, m_publicKeyLen) >> t::base64Encode(false) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 160 | return os.str(); |
| 161 | } |
| 162 | |
| 163 | uint8_t* |
| 164 | ECDHState::deriveSecret(const uint8_t* peerkey, int peerKeySize) |
| 165 | { |
| 166 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 167 | |
| 168 | if (privECKey == nullptr) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 169 | handleErrors("Could not get referenced key when calling EVP_PKEY_get1_EC_KEY()"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | auto group = EC_KEY_get0_group(privECKey); |
| 174 | auto peerPoint = EC_POINT_new(group); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 175 | int result = EC_POINT_oct2point(group, peerPoint, peerkey, peerKeySize, nullptr); |
| 176 | if (result == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 177 | EC_POINT_free(peerPoint); |
| 178 | EC_KEY_free(privECKey); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 179 | handleErrors("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()"); |
| 180 | } |
| 181 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 182 | if (-1 == (m_sharedSecretLen = ECDH_compute_key(m_sharedSecret, 256, peerPoint, privECKey, nullptr))) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 183 | EC_POINT_free(peerPoint); |
| 184 | EC_KEY_free(privECKey); |
| 185 | handleErrors("Cannot generate ECDH secret when calling ECDH_compute_key()"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 186 | } |
| 187 | EC_POINT_free(peerPoint); |
| 188 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 189 | return m_sharedSecret; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | uint8_t* |
| 193 | ECDHState::deriveSecret(const std::string& peerKeyStr) |
| 194 | { |
| 195 | namespace t = ndn::security::transform; |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 196 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 197 | OBufferStream os; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 198 | t::bufferSource(peerKeyStr) >> t::base64Decode(false) >> t::streamSink(os); |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 199 | auto result = os.buf(); |
| 200 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 201 | return this->deriveSecret(result->data(), result->size()); |
| 202 | } |
| 203 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 204 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 205 | hmac_sha256(const uint8_t* data, const unsigned data_length, |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 206 | const uint8_t* key, const unsigned key_length, |
| 207 | uint8_t* result) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 208 | { |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 209 | HMAC(EVP_sha256(), key, key_length, |
| 210 | (unsigned char*)data, data_length, |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 211 | (unsigned char*)result, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 215 | // avoid dependency on OpenSSL >= 1.1 |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 216 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 217 | hkdf(const uint8_t* secret, int secret_len, const uint8_t* salt, |
| 218 | int salt_len, uint8_t* output, int output_len, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 219 | const uint8_t* info, int info_len) |
| 220 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 221 | namespace t = ndn::security::transform; |
| 222 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 223 | // hkdf generate prk |
| 224 | uint8_t prk[HASH_SIZE]; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 225 | if (salt_len == 0) { |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 226 | uint8_t realSalt[HASH_SIZE] = {0}; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 227 | hmac_sha256(secret, secret_len, realSalt, HASH_SIZE, prk); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 228 | } |
| 229 | else { |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 230 | hmac_sha256(secret, secret_len, salt, salt_len, prk); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 231 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 232 | |
| 233 | // hkdf expand |
| 234 | uint8_t prev[HASH_SIZE] = {0}; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 235 | int done_len = 0, dig_len = HASH_SIZE, n = output_len / dig_len; |
| 236 | if (output_len % dig_len) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 237 | n++; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 238 | if (n > 255 || output == nullptr) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 239 | return 0; |
| 240 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 241 | for (int i = 1; i <= n; i++) { |
| 242 | size_t copy_len; |
| 243 | const uint8_t ctr = i; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 244 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 245 | t::StepSource source; |
| 246 | t::PrivateKey privKey; |
| 247 | privKey.loadRaw(KeyType::HMAC, prk, dig_len); |
| 248 | OBufferStream os; |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 249 | source >> t::signerFilter(DigestAlgorithm::SHA256, privKey) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 250 | |
| 251 | if (i > 1) { |
| 252 | source.write(prev, dig_len); |
| 253 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 254 | source.write(info, info_len); |
| 255 | source.write(&ctr, 1); |
| 256 | source.end(); |
| 257 | |
| 258 | auto result = os.buf(); |
| 259 | memcpy(prev, result->data(), dig_len); |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 260 | copy_len = (done_len + dig_len > output_len) ? output_len - done_len : dig_len; |
| 261 | memcpy(output + done_len, prev, copy_len); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 262 | done_len += copy_len; |
| 263 | } |
| 264 | return done_len; |
| 265 | } |
| 266 | |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 267 | int |
| 268 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 269 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag) |
| 270 | { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 271 | EVP_CIPHER_CTX* ctx; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 272 | int len; |
| 273 | int ciphertext_len; |
| 274 | |
| 275 | // Create and initialise the context |
| 276 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 277 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 278 | } |
| 279 | |
| 280 | // Initialise the encryption operation. |
| 281 | if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 282 | handleErrors("Cannot initialise the encryption operation when calling EVP_EncryptInit_ex()"); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Set IV length if default 12 bytes (96 bits) is not appropriate |
| 286 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 287 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()"); |
| 288 | } |
| 289 | |
| 290 | // Initialise key and IV |
| 291 | if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 292 | handleErrors("Cannot initialize key and IV when calling EVP_EncryptInit_ex()"); |
| 293 | } |
| 294 | |
| 295 | // Provide any AAD data. This can be called zero or more times as required |
| 296 | if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 297 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 298 | } |
| 299 | |
| 300 | // Provide the message to be encrypted, and obtain the encrypted output. |
| 301 | // EVP_EncryptUpdate can be called multiple times if necessary |
| 302 | if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) { |
| 303 | handleErrors("Cannot encrypt when calling EVP_EncryptUpdate()"); |
| 304 | } |
| 305 | ciphertext_len = len; |
| 306 | |
| 307 | // Finalise the encryption. Normally ciphertext bytes may be written at |
| 308 | // this stage, but this does not occur in GCM mode |
| 309 | if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { |
| 310 | handleErrors("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()"); |
| 311 | } |
| 312 | ciphertext_len += len; |
| 313 | |
| 314 | // Get the tag |
| 315 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) { |
| 316 | handleErrors("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()"); |
| 317 | } |
| 318 | |
| 319 | // Clean up |
| 320 | EVP_CIPHER_CTX_free(ctx); |
| 321 | return ciphertext_len; |
| 322 | } |
| 323 | |
| 324 | int |
| 325 | aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len, |
| 326 | const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext) |
| 327 | { |
| 328 | EVP_CIPHER_CTX* ctx; |
| 329 | int len; |
| 330 | int plaintext_len; |
| 331 | int ret; |
| 332 | |
| 333 | // Create and initialise the context |
| 334 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 335 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 336 | } |
| 337 | |
| 338 | // Initialise the decryption operation. |
| 339 | if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
| 340 | handleErrors("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()"); |
| 341 | } |
| 342 | |
| 343 | // Set IV length. Not necessary if this is 12 bytes (96 bits) |
| 344 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 345 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl"); |
| 346 | } |
| 347 | |
| 348 | // Initialise key and IV |
| 349 | if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 350 | handleErrors("Cannot initialise key and IV when calling EVP_DecryptInit_ex()"); |
| 351 | } |
| 352 | |
| 353 | // Provide any AAD data. This can be called zero or more times as required |
| 354 | if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 355 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 356 | } |
| 357 | |
| 358 | // Provide the message to be decrypted, and obtain the plaintext output. |
| 359 | // EVP_DecryptUpdate can be called multiple times if necessary |
| 360 | if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) { |
| 361 | handleErrors("Cannot decrypt when calling EVP_DecryptUpdate()"); |
| 362 | } |
| 363 | plaintext_len = len; |
| 364 | |
| 365 | // Set expected tag value. Works in OpenSSL 1.0.1d and later |
| 366 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag)) { |
| 367 | handleErrors("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl"); |
| 368 | } |
| 369 | |
| 370 | // Finalise the decryption. A positive return value indicates success, |
| 371 | // anything else is a failure - the plaintext is not trustworthy. |
| 372 | ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
| 373 | |
| 374 | // Clean up |
| 375 | EVP_CIPHER_CTX_free(ctx); |
| 376 | |
| 377 | if (ret > 0) { |
| 378 | // Success |
| 379 | plaintext_len += len; |
| 380 | return plaintext_len; |
| 381 | } |
| 382 | else { |
| 383 | // Verify failed |
| 384 | return -1; |
| 385 | } |
| 386 | } |
| 387 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 388 | void |
| 389 | handleErrors(const std::string& errorInfo) |
| 390 | { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 391 | NDN_LOG_DEBUG("Error in CRYPTO SUPPORT " << errorInfo); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 392 | NDN_THROW(CryptoError("Error in CRYPTO SUPPORT: " + errorInfo)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 395 | } // namespace ndncert |
| 396 | } // namespace ndn |