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 | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame^] | 35 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndncert { |
| 39 | |
| 40 | const size_t HASH_SIZE = 32; |
| 41 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 42 | NDN_LOG_INIT(ndncert.cryptosupport); |
| 43 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame^] | 44 | void |
| 45 | handleErrors(const std::string& errorInfo) |
| 46 | { |
| 47 | NDN_LOG_DEBUG("Error in CRYPTO SUPPORT " << errorInfo); |
| 48 | NDN_THROW(std::runtime_error("Error in CRYPTO SUPPORT: " + errorInfo)); |
| 49 | } |
| 50 | |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 51 | struct ECDHState::ECDH_CTX |
| 52 | { |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 53 | int EC_NID; |
| 54 | EVP_PKEY_CTX* ctx_params; |
| 55 | EVP_PKEY_CTX* ctx_keygen; |
| 56 | EVP_PKEY* privkey; |
| 57 | EVP_PKEY* peerkey; |
| 58 | EVP_PKEY* params; |
| 59 | }; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | |
| 61 | ECDHState::ECDHState() |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 62 | : m_publicKeyLen(0) |
| 63 | , m_sharedSecretLen(0) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 64 | { |
| 65 | OpenSSL_add_all_algorithms(); |
| 66 | context = std::make_unique<ECDH_CTX>(); |
| 67 | context->EC_NID = NID_X9_62_prime256v1; |
| 68 | |
| 69 | // Create the context for parameter generation |
| 70 | if (nullptr == (context->ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr))) { |
| 71 | handleErrors("Could not create context contexts."); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Initialise the parameter generation |
| 76 | if (EVP_PKEY_paramgen_init(context->ctx_params) != 1) { |
| 77 | handleErrors("Could not initialize parameter generation."); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // We're going to use the ANSI X9.62 Prime 256v1 curve |
| 82 | if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(context->ctx_params, context->EC_NID)) { |
| 83 | handleErrors("Likely unknown elliptical curve ID specified."); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Create the parameter object params |
| 88 | if (!EVP_PKEY_paramgen(context->ctx_params, &context->params)) { |
| 89 | // the generated key is written to context->params |
| 90 | handleErrors("Could not create parameter object parameters."); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // Create the context for the key generation |
| 95 | if (nullptr == (context->ctx_keygen = EVP_PKEY_CTX_new(context->params, nullptr))) { |
| 96 | //The EVP_PKEY_CTX_new() function allocates public key algorithm context using |
| 97 | //the algorithm specified in pkey and ENGINE e (in this case nullptr). |
| 98 | handleErrors("Could not create the context for the key generation"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // initializes a public key algorithm context |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 103 | if (1 != EVP_PKEY_keygen_init(context->ctx_keygen)) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 104 | handleErrors("Could not init context for key generation."); |
| 105 | return; |
| 106 | } |
| 107 | if (1 != EVP_PKEY_keygen(context->ctx_keygen, &context->privkey)) { |
| 108 | //performs a key generation operation, the generated key is written to context->privkey. |
| 109 | handleErrors("Could not generate DHE keys in final step"); |
| 110 | return; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | ECDHState::~ECDHState() |
| 115 | { |
| 116 | // Contexts |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 117 | if (context->ctx_params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 118 | EVP_PKEY_CTX_free(context->ctx_params); |
| 119 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 120 | if (context->ctx_keygen != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 121 | EVP_PKEY_CTX_free(context->ctx_keygen); |
| 122 | } |
| 123 | |
| 124 | // Keys |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 125 | if (context->privkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 126 | EVP_PKEY_free(context->privkey); |
| 127 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 128 | if (context->peerkey != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 129 | EVP_PKEY_free(context->peerkey); |
| 130 | } |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 131 | if (context->params != nullptr) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | EVP_PKEY_free(context->params); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | uint8_t* |
| 137 | ECDHState::getRawSelfPubKey() |
| 138 | { |
| 139 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 140 | |
| 141 | if (privECKey == nullptr) { |
| 142 | handleErrors("Could not get referenced key when calling EVP_PKEY_get1_EC_KEY()."); |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | auto ecPoint = EC_KEY_get0_public_key(privECKey); |
| 147 | const EC_GROUP* group = EC_KEY_get0_group(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 148 | m_publicKeyLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED, |
Zhiyi Zhang | 938d703 | 2020-10-12 17:23:45 -0700 | [diff] [blame] | 149 | m_publicKey, 256, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 150 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 151 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 152 | handleErrors("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct."); |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 156 | return m_publicKey; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | std::string |
| 160 | ECDHState::getBase64PubKey() |
| 161 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 162 | namespace t = ndn::security::transform; |
| 163 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 164 | if (m_publicKeyLen == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 165 | this->getRawSelfPubKey(); |
| 166 | } |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 167 | std::ostringstream os; |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 168 | t::bufferSource(m_publicKey, m_publicKeyLen) >> t::base64Encode(false) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 169 | return os.str(); |
| 170 | } |
| 171 | |
| 172 | uint8_t* |
| 173 | ECDHState::deriveSecret(const uint8_t* peerkey, int peerKeySize) |
| 174 | { |
| 175 | auto privECKey = EVP_PKEY_get1_EC_KEY(context->privkey); |
| 176 | |
| 177 | if (privECKey == nullptr) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 178 | 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] | 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | auto group = EC_KEY_get0_group(privECKey); |
| 183 | auto peerPoint = EC_POINT_new(group); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 184 | int result = EC_POINT_oct2point(group, peerPoint, peerkey, peerKeySize, nullptr); |
| 185 | if (result == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 186 | EC_POINT_free(peerPoint); |
| 187 | EC_KEY_free(privECKey); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 188 | handleErrors("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()"); |
| 189 | } |
| 190 | |
Zhiyi Zhang | 938d703 | 2020-10-12 17:23:45 -0700 | [diff] [blame] | 191 | result = ECDH_compute_key(m_sharedSecret, 256, peerPoint, privECKey, nullptr); |
| 192 | if (result == -1) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 193 | EC_POINT_free(peerPoint); |
| 194 | EC_KEY_free(privECKey); |
| 195 | handleErrors("Cannot generate ECDH secret when calling ECDH_compute_key()"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 196 | } |
Zhiyi Zhang | 938d703 | 2020-10-12 17:23:45 -0700 | [diff] [blame] | 197 | m_sharedSecretLen = static_cast<size_t>(result); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 198 | EC_POINT_free(peerPoint); |
| 199 | EC_KEY_free(privECKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 200 | return m_sharedSecret; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | uint8_t* |
| 204 | ECDHState::deriveSecret(const std::string& peerKeyStr) |
| 205 | { |
| 206 | namespace t = ndn::security::transform; |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 207 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 208 | OBufferStream os; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 209 | t::bufferSource(peerKeyStr) >> t::base64Decode(false) >> t::streamSink(os); |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 210 | auto result = os.buf(); |
| 211 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 212 | return this->deriveSecret(result->data(), result->size()); |
| 213 | } |
| 214 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 215 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 216 | hmac_sha256(const uint8_t* data, const unsigned data_length, |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 217 | const uint8_t* key, const unsigned key_length, |
| 218 | uint8_t* result) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 219 | { |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 220 | HMAC(EVP_sha256(), key, key_length, |
| 221 | (unsigned char*)data, data_length, |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 222 | (unsigned char*)result, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 226 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 227 | hkdf(const uint8_t* secret, int secret_len, const uint8_t* salt, |
| 228 | int salt_len, uint8_t* output, int output_len, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 229 | const uint8_t* info, int info_len) |
| 230 | { |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 231 | namespace t = ndn::security::transform; |
| 232 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 233 | // hkdf generate prk |
| 234 | uint8_t prk[HASH_SIZE]; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 235 | if (salt_len == 0) { |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 236 | uint8_t realSalt[HASH_SIZE] = {0}; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 237 | hmac_sha256(secret, secret_len, realSalt, HASH_SIZE, prk); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 238 | } |
| 239 | else { |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 240 | hmac_sha256(secret, secret_len, salt, salt_len, prk); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 241 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 242 | |
| 243 | // hkdf expand |
| 244 | uint8_t prev[HASH_SIZE] = {0}; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 245 | int done_len = 0, dig_len = HASH_SIZE, n = output_len / dig_len; |
| 246 | if (output_len % dig_len) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 247 | n++; |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 248 | if (n > 255 || output == nullptr) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 249 | return 0; |
| 250 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 251 | for (int i = 1; i <= n; i++) { |
| 252 | size_t copy_len; |
| 253 | const uint8_t ctr = i; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 254 | |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 255 | t::StepSource source; |
| 256 | t::PrivateKey privKey; |
| 257 | privKey.loadRaw(KeyType::HMAC, prk, dig_len); |
| 258 | OBufferStream os; |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 259 | source >> t::signerFilter(DigestAlgorithm::SHA256, privKey) >> t::streamSink(os); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 260 | |
| 261 | if (i > 1) { |
| 262 | source.write(prev, dig_len); |
| 263 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 264 | source.write(info, info_len); |
| 265 | source.write(&ctr, 1); |
| 266 | source.end(); |
| 267 | |
| 268 | auto result = os.buf(); |
| 269 | memcpy(prev, result->data(), dig_len); |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 270 | copy_len = (done_len + dig_len > output_len) ? output_len - done_len : dig_len; |
| 271 | memcpy(output + done_len, prev, copy_len); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 272 | done_len += copy_len; |
| 273 | } |
| 274 | return done_len; |
| 275 | } |
| 276 | |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 277 | int |
| 278 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 279 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag) |
| 280 | { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 281 | EVP_CIPHER_CTX* ctx; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 282 | int len; |
| 283 | int ciphertext_len; |
| 284 | |
| 285 | // Create and initialise the context |
| 286 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 287 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 288 | } |
| 289 | |
| 290 | // Initialise the encryption operation. |
| 291 | 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] | 292 | handleErrors("Cannot initialise the encryption operation when calling EVP_EncryptInit_ex()"); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Set IV length if default 12 bytes (96 bits) is not appropriate |
| 296 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 297 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()"); |
| 298 | } |
| 299 | |
| 300 | // Initialise key and IV |
| 301 | if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 302 | handleErrors("Cannot initialize key and IV when calling EVP_EncryptInit_ex()"); |
| 303 | } |
| 304 | |
| 305 | // Provide any AAD data. This can be called zero or more times as required |
| 306 | if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 307 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 308 | } |
| 309 | |
| 310 | // Provide the message to be encrypted, and obtain the encrypted output. |
| 311 | // EVP_EncryptUpdate can be called multiple times if necessary |
| 312 | if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) { |
| 313 | handleErrors("Cannot encrypt when calling EVP_EncryptUpdate()"); |
| 314 | } |
| 315 | ciphertext_len = len; |
| 316 | |
| 317 | // Finalise the encryption. Normally ciphertext bytes may be written at |
| 318 | // this stage, but this does not occur in GCM mode |
| 319 | if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { |
| 320 | handleErrors("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()"); |
| 321 | } |
| 322 | ciphertext_len += len; |
| 323 | |
| 324 | // Get the tag |
| 325 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) { |
| 326 | handleErrors("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()"); |
| 327 | } |
| 328 | |
| 329 | // Clean up |
| 330 | EVP_CIPHER_CTX_free(ctx); |
| 331 | return ciphertext_len; |
| 332 | } |
| 333 | |
| 334 | int |
| 335 | aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len, |
| 336 | const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext) |
| 337 | { |
| 338 | EVP_CIPHER_CTX* ctx; |
| 339 | int len; |
| 340 | int plaintext_len; |
| 341 | int ret; |
| 342 | |
| 343 | // Create and initialise the context |
| 344 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
| 345 | handleErrors("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"); |
| 346 | } |
| 347 | |
| 348 | // Initialise the decryption operation. |
| 349 | if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
| 350 | handleErrors("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()"); |
| 351 | } |
| 352 | |
| 353 | // Set IV length. Not necessary if this is 12 bytes (96 bits) |
| 354 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
| 355 | handleErrors("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl"); |
| 356 | } |
| 357 | |
| 358 | // Initialise key and IV |
| 359 | if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
| 360 | handleErrors("Cannot initialise key and IV when calling EVP_DecryptInit_ex()"); |
| 361 | } |
| 362 | |
| 363 | // Provide any AAD data. This can be called zero or more times as required |
| 364 | if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associated_len)) { |
| 365 | handleErrors("Cannot set associated authentication data when calling EVP_EncryptUpdate()"); |
| 366 | } |
| 367 | |
| 368 | // Provide the message to be decrypted, and obtain the plaintext output. |
| 369 | // EVP_DecryptUpdate can be called multiple times if necessary |
| 370 | if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) { |
| 371 | handleErrors("Cannot decrypt when calling EVP_DecryptUpdate()"); |
| 372 | } |
| 373 | plaintext_len = len; |
| 374 | |
| 375 | // Set expected tag value. Works in OpenSSL 1.0.1d and later |
| 376 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag)) { |
| 377 | handleErrors("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl"); |
| 378 | } |
| 379 | |
| 380 | // Finalise the decryption. A positive return value indicates success, |
| 381 | // anything else is a failure - the plaintext is not trustworthy. |
| 382 | ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
| 383 | |
| 384 | // Clean up |
| 385 | EVP_CIPHER_CTX_free(ctx); |
| 386 | |
| 387 | if (ret > 0) { |
| 388 | // Success |
| 389 | plaintext_len += len; |
| 390 | return plaintext_len; |
| 391 | } |
| 392 | else { |
| 393 | // Verify failed |
| 394 | return -1; |
| 395 | } |
| 396 | } |
| 397 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame^] | 398 | Block |
| 399 | encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
| 400 | const uint8_t* associatedData, size_t associatedDataSize) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 401 | { |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame^] | 402 | Buffer iv; |
| 403 | iv.resize(12); |
| 404 | random::generateSecureBytes(iv.data(), iv.size()); |
| 405 | |
| 406 | uint8_t* encryptedPayload = new uint8_t[payloadSize]; |
| 407 | uint8_t tag[16]; |
| 408 | size_t encryptedPayloadLen = aes_gcm_128_encrypt(payload, payloadSize, associatedData, associatedDataSize, |
| 409 | key, iv.data(), encryptedPayload, tag); |
| 410 | auto content = makeEmptyBlock(tlv_type); |
| 411 | content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size())); |
| 412 | content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16)); |
| 413 | content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload, encryptedPayloadLen)); |
| 414 | content.encode(); |
| 415 | delete[] encryptedPayload; |
| 416 | return content; |
| 417 | } |
| 418 | |
| 419 | Buffer |
| 420 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, const uint8_t* associatedData, size_t associatedDataSize) |
| 421 | { |
| 422 | block.parse(); |
| 423 | Buffer result; |
| 424 | result.resize(block.get(tlv::EncryptedPayload).value_size()); |
| 425 | int resultLen = aes_gcm_128_decrypt(block.get(tlv::EncryptedPayload).value(), |
| 426 | block.get(tlv::EncryptedPayload).value_size(), |
| 427 | associatedData, associatedDataSize, block.get(tlv::AuthenticationTag).value(), |
| 428 | key, block.get(tlv::InitializationVector).value(), result.data()); |
| 429 | if (resultLen == -1 || resultLen != (int)block.get(tlv::EncryptedPayload).value_size()) { |
| 430 | return Buffer(); |
| 431 | } |
| 432 | return result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 435 | } // namespace ndncert |
| 436 | } // namespace ndn |