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 | |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 21 | #include "crypto-helpers.hpp" |
| 22 | |
| 23 | #include <boost/endian/conversion.hpp> |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 24 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/transform/base64-decode.hpp> |
| 26 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 27 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 30 | #include <openssl/ec.h> |
| 31 | #include <openssl/err.h> |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 32 | #include <openssl/hmac.h> |
| 33 | #include <openssl/kdf.h> |
| 34 | #include <openssl/pem.h> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace ndncert { |
| 38 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 39 | ECDHState::ECDHState() |
| 40 | { |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 41 | auto EC_NID = NID_X9_62_prime256v1; |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 42 | // params context |
| 43 | EVP_PKEY_CTX* ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr); |
| 44 | if (ctx_params == nullptr) { |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 45 | NDN_THROW(std::runtime_error("Could not create context")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 46 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 47 | if (EVP_PKEY_paramgen_init(ctx_params) != 1) { |
| 48 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 49 | NDN_THROW(std::runtime_error("Could not initialize parameter generation")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 50 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 51 | if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx_params, EC_NID)) { |
| 52 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 53 | NDN_THROW(std::runtime_error("Likely unknown elliptical curve ID specified")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 54 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 55 | // generate params |
| 56 | EVP_PKEY* params = nullptr; |
| 57 | if (!EVP_PKEY_paramgen(ctx_params, ¶ms)) { |
| 58 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 59 | NDN_THROW(std::runtime_error("Could not create parameter object parameters")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 61 | // key generation context |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 62 | EVP_PKEY_CTX* ctx_keygen = EVP_PKEY_CTX_new(params, nullptr); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 63 | if (ctx_keygen == nullptr) { |
| 64 | EVP_PKEY_free(params); |
| 65 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 66 | NDN_THROW(std::runtime_error("Could not create the context for the key generation")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 67 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 68 | if (1 != EVP_PKEY_keygen_init(ctx_keygen)) { |
| 69 | EVP_PKEY_CTX_free(ctx_keygen); |
| 70 | EVP_PKEY_free(params); |
| 71 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 72 | NDN_THROW(std::runtime_error("Could not init context for key generation")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 73 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 74 | if (1 != EVP_PKEY_keygen(ctx_keygen, &m_privkey)) { |
| 75 | EVP_PKEY_CTX_free(ctx_keygen); |
| 76 | EVP_PKEY_free(params); |
| 77 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 78 | NDN_THROW(std::runtime_error("Could not generate DHE keys in final step")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 80 | EVP_PKEY_CTX_free(ctx_keygen); |
| 81 | EVP_PKEY_free(params); |
| 82 | EVP_PKEY_CTX_free(ctx_params); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | ECDHState::~ECDHState() |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 86 | { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 87 | if (m_privkey != nullptr) { |
| 88 | EVP_PKEY_free(m_privkey); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const std::vector<uint8_t>& |
| 93 | ECDHState::getSelfPubKey() |
| 94 | { |
| 95 | auto privECKey = EVP_PKEY_get1_EC_KEY(m_privkey); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | if (privECKey == nullptr) { |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 97 | NDN_THROW(std::runtime_error("Could not get key when calling EVP_PKEY_get1_EC_KEY()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 98 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 99 | auto ecPoint = EC_KEY_get0_public_key(privECKey); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 100 | auto group = EC_KEY_get0_group(privECKey); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 101 | auto requiredBufLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 102 | m_pubKey.resize(requiredBufLen); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 103 | auto rev = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_UNCOMPRESSED, |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 104 | m_pubKey.data(), requiredBufLen, nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 105 | EC_KEY_free(privECKey); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 106 | if (rev == 0) { |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 107 | NDN_THROW(std::runtime_error("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 108 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 109 | return m_pubKey; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 112 | const std::vector<uint8_t>& |
| 113 | ECDHState::deriveSecret(const std::vector<uint8_t>& peerKey) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 115 | // prepare self private key |
| 116 | auto privECKey = EVP_PKEY_get1_EC_KEY(m_privkey); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | if (privECKey == nullptr) { |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 118 | NDN_THROW(std::runtime_error("Cannot not get key when calling EVP_PKEY_get1_EC_KEY()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 119 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | auto group = EC_KEY_get0_group(privECKey); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 121 | EC_KEY_free(privECKey); |
| 122 | // prepare the peer public key |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | auto peerPoint = EC_POINT_new(group); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 124 | if (peerPoint == nullptr) { |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 125 | NDN_THROW(std::runtime_error("Cannot create the EC_POINT for peer key when calling EC_POINT_new()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 126 | } |
Zhiyi Zhang | 0eb7424 | 2020-10-20 18:33:22 -0700 | [diff] [blame] | 127 | if (EC_POINT_oct2point(group, peerPoint, peerKey.data(), peerKey.size(), nullptr) == 0) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 128 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 129 | NDN_THROW(std::runtime_error("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()")); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 130 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 131 | EC_KEY* ecPeerkey = EC_KEY_new(); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 132 | if (ecPeerkey == nullptr) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 133 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 134 | NDN_THROW(std::runtime_error("Cannot create EC_KEY for peer key when calling EC_KEY_new()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 136 | if (EC_KEY_set_group(ecPeerkey, group) != 1) { |
| 137 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 138 | NDN_THROW(std::runtime_error("Cannot set group for peer key's EC_KEY when calling EC_KEY_set_group()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 139 | } |
| 140 | if (EC_KEY_set_public_key(ecPeerkey, peerPoint) == 0) { |
| 141 | EC_KEY_free(ecPeerkey); |
| 142 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 143 | NDN_THROW(std::runtime_error("Cannot initialize peer EC_KEY with the EC_POINT when calling EC_KEY_set_public_key()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 144 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 145 | EVP_PKEY* evpPeerkey = EVP_PKEY_new(); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 146 | if (EVP_PKEY_set1_EC_KEY(evpPeerkey, ecPeerkey) == 0) { |
| 147 | EC_KEY_free(ecPeerkey); |
| 148 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 149 | NDN_THROW(std::runtime_error("Cannot create EVP_PKEY for peer key when calling EVP_PKEY_new()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 150 | } |
| 151 | EC_KEY_free(ecPeerkey); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 152 | EC_POINT_free(peerPoint); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 153 | // ECDH context |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 154 | EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(m_privkey, nullptr); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 155 | if (ctx == nullptr) { |
| 156 | EVP_PKEY_free(evpPeerkey); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 157 | NDN_THROW(std::runtime_error("Cannot create context for ECDH when calling EVP_PKEY_CTX_new()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 158 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 159 | // Initialize |
| 160 | if (1 != EVP_PKEY_derive_init(ctx)) { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 161 | EVP_PKEY_CTX_free(ctx); |
| 162 | EVP_PKEY_free(evpPeerkey); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 163 | NDN_THROW(std::runtime_error("Cannot initialize context for ECDH when calling EVP_PKEY_derive_init()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 164 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 165 | // Provide the peer public key |
| 166 | if (1 != EVP_PKEY_derive_set_peer(ctx, evpPeerkey)) { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 167 | EVP_PKEY_CTX_free(ctx); |
| 168 | EVP_PKEY_free(evpPeerkey); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 169 | NDN_THROW(std::runtime_error("Cannot set peer key for ECDH when calling EVP_PKEY_derive_set_peer()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 170 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 171 | // Determine buffer length for shared secret |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 172 | size_t secretLen = 0; |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 173 | if (1 != EVP_PKEY_derive(ctx, nullptr, &secretLen)) { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 174 | EVP_PKEY_CTX_free(ctx); |
| 175 | EVP_PKEY_free(evpPeerkey); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 176 | NDN_THROW(std::runtime_error("Cannot determine the needed buffer length when calling EVP_PKEY_derive()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 177 | } |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 178 | m_secret.resize(secretLen); |
| 179 | // Derive the shared secret |
| 180 | if (1 != (EVP_PKEY_derive(ctx, m_secret.data(), &secretLen))) { |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 181 | EVP_PKEY_CTX_free(ctx); |
| 182 | EVP_PKEY_free(evpPeerkey); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 183 | NDN_THROW(std::runtime_error("Cannot derive ECDH secret when calling EVP_PKEY_derive()")); |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 184 | } |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 185 | EVP_PKEY_CTX_free(ctx); |
| 186 | EVP_PKEY_free(evpPeerkey); |
| 187 | return m_secret; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 190 | void |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 191 | hmacSha256(const uint8_t* data, size_t dataLen, |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 192 | const uint8_t* key, size_t keyLen, |
| 193 | uint8_t* result) |
Davide Pesavento | 368341b | 2019-08-13 23:57:50 -0400 | [diff] [blame] | 194 | { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 195 | auto ret = HMAC(EVP_sha256(), key, keyLen, |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 196 | data, dataLen, result, nullptr); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 197 | if (ret == nullptr) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 198 | NDN_THROW(std::runtime_error("Error computing HMAC when calling HMAC()")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 199 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 202 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 203 | hkdf(const uint8_t* secret, size_t secretLen, const uint8_t* salt, |
| 204 | size_t saltLen, uint8_t* output, size_t outputLen, |
| 205 | const uint8_t* info, size_t infoLen) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 206 | { |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 207 | EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 208 | if (EVP_PKEY_derive_init(pctx) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 209 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 210 | NDN_THROW(std::runtime_error("HKDF: Cannot init ctx when calling EVP_PKEY_derive_init()")); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 211 | } |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 212 | if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 213 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 214 | NDN_THROW(std::runtime_error("HKDF: Cannot set md when calling EVP_PKEY_CTX_set_hkdf_md()")); |
Zhiyi Zhang | a2ce599 | 2019-08-14 17:35:00 -0700 | [diff] [blame] | 215 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 216 | if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 217 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 218 | NDN_THROW(std::runtime_error("HKDF: Cannot set salt when calling EVP_PKEY_CTX_set1_hkdf_salt()")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 219 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 220 | if (EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, secretLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 221 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 222 | NDN_THROW(std::runtime_error("HKDF: Cannot set secret when calling EVP_PKEY_CTX_set1_hkdf_key()")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 223 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 224 | if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infoLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 225 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 226 | NDN_THROW(std::runtime_error("HKDF: Cannot set info when calling EVP_PKEY_CTX_add1_hkdf_info()")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 227 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 228 | size_t outLen = outputLen; |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 229 | if (EVP_PKEY_derive(pctx, output, &outLen) <= 0) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 230 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 231 | NDN_THROW(std::runtime_error("HKDF: Cannot derive result when calling EVP_PKEY_derive()")); |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 232 | } |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 233 | EVP_PKEY_CTX_free(pctx); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 234 | return outLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 237 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 238 | aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen, |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 239 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag) |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 240 | { |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 241 | EVP_CIPHER_CTX* ctx; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 242 | int len; |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 243 | size_t ciphertextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 244 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 245 | NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 246 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 247 | if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 248 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 249 | NDN_THROW(std::runtime_error("Cannot initialize the encryption operation when calling EVP_EncryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 250 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 251 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 252 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 253 | NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 254 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 255 | if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 256 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 257 | NDN_THROW(std::runtime_error("Cannot initialize key and IV when calling EVP_EncryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 258 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 259 | if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associatedLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 260 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 261 | NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 262 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 263 | if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintextLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 264 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 265 | NDN_THROW(std::runtime_error("Cannot encrypt when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 266 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 267 | ciphertextLen = len; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 268 | if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 269 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 270 | NDN_THROW(std::runtime_error("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 271 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 272 | ciphertextLen += len; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 273 | if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 274 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 275 | NDN_THROW(std::runtime_error("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 276 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 277 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 278 | return ciphertextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 281 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 282 | aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen, |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 283 | const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext) |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 284 | { |
| 285 | EVP_CIPHER_CTX* ctx; |
| 286 | int len; |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 287 | size_t plaintextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 288 | if (!(ctx = EVP_CIPHER_CTX_new())) { |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 289 | NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 290 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 291 | if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 292 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 293 | NDN_THROW(std::runtime_error("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 294 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 295 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 296 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 297 | NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 298 | } |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 299 | if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 300 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 301 | NDN_THROW(std::runtime_error("Cannot initialise key and IV when calling EVP_DecryptInit_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 302 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 303 | if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associatedLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 304 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 305 | NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 306 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 307 | if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertextLen)) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 308 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 309 | NDN_THROW(std::runtime_error("Cannot decrypt when calling EVP_DecryptUpdate()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 310 | } |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 311 | plaintextLen = len; |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 312 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, const_cast<void*>(reinterpret_cast<const void*>(tag)))) { |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 313 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 314 | NDN_THROW(std::runtime_error("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 315 | } |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 316 | auto ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 317 | EVP_CIPHER_CTX_free(ctx); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 318 | if (ret > 0) { |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 319 | plaintextLen += len; |
| 320 | return plaintextLen; |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 321 | } |
| 322 | else { |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 323 | NDN_THROW(std::runtime_error("Cannot finalize the decryption when calling EVP_DecryptFinal_ex()")); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 327 | Block |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 328 | encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 329 | const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 330 | { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 331 | Buffer iv(12); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 332 | random::generateSecureBytes(iv.data(), 8); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 333 | if (tlvType == ndn::tlv::ApplicationParameters) { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 334 | // requester |
| 335 | iv[0] &= ~(1UL << 7); |
| 336 | } |
| 337 | else { |
| 338 | // CA |
| 339 | iv[0] |= 1UL << 7; |
| 340 | } |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 341 | uint32_t temp = boost::endian::native_to_big(counter); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 342 | std::memcpy(&iv[8], reinterpret_cast<const uint8_t*>(&temp), 4); |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 343 | uint32_t increment = (payloadSize + 15) / 16; |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 344 | if (std::numeric_limits<uint32_t>::max() - counter < increment) { |
Zhiyi Zhang | e11a2d6 | 2020-10-22 09:59:12 -0700 | [diff] [blame^] | 345 | NDN_THROW(std::runtime_error("Error incrementing the AES block counter:" |
| 346 | "too many blocks have been encrypted for the same request instance")); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 347 | } |
| 348 | else { |
| 349 | counter += increment; |
| 350 | } |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 351 | |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 352 | // The spec of AES encrypted payload TLV used in NDNCERT: |
| 353 | // https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 354 | Buffer encryptedPayload(payloadSize); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 355 | uint8_t tag[16]; |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 356 | size_t encryptedPayloadLen = aesGcm128Encrypt(payload, payloadSize, associatedData, associatedDataSize, |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 357 | key, iv.data(), encryptedPayload.data(), tag); |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 358 | auto content = makeEmptyBlock(tlvType); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 359 | content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size())); |
| 360 | content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16)); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 361 | content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload.data(), encryptedPayloadLen)); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 362 | content.encode(); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 363 | return content; |
| 364 | } |
| 365 | |
| 366 | Buffer |
| 367 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, const uint8_t* associatedData, size_t associatedDataSize) |
| 368 | { |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 369 | // The spec of AES encrypted payload TLV used in NDNCERT: |
| 370 | // https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 371 | block.parse(); |
Zhiyi Zhang | cace6ab | 2020-10-16 21:51:44 -0700 | [diff] [blame] | 372 | Buffer result(block.get(tlv::EncryptedPayload).value_size()); |
Zhiyi Zhang | f908f66 | 2020-10-21 12:55:10 -0700 | [diff] [blame] | 373 | auto resultLen = aesGcm128Decrypt(block.get(tlv::EncryptedPayload).value(), |
| 374 | block.get(tlv::EncryptedPayload).value_size(), |
| 375 | associatedData, associatedDataSize, block.get(tlv::AuthenticationTag).value(), |
| 376 | key, block.get(tlv::InitializationVector).value(), result.data()); |
Zhiyi Zhang | adc0b74 | 2020-10-21 12:58:12 -0700 | [diff] [blame] | 377 | if (resultLen != block.get(tlv::EncryptedPayload).value_size()) { |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 378 | return Buffer(); |
| 379 | } |
| 380 | return result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 383 | } // namespace ndncert |
| 384 | } // namespace ndn |