blob: c3c3187e61f85833effec5f9e4fe3913e1ce5f7c [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhanga67fa462020-04-19 13:48:03 -07002/**
3 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07004 *
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 Zhangdc25ddf2020-10-20 14:28:55 -070021#include "crypto-helpers.hpp"
22
23#include <boost/endian/conversion.hpp>
Davide Pesavento368341b2019-08-13 23:57:50 -040024#include <ndn-cxx/encoding/buffer-stream.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#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 Zhangaf7c2902019-03-14 22:13:21 -070028#include <ndn-cxx/security/transform/stream-sink.hpp>
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -070029#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070030#include <openssl/ec.h>
31#include <openssl/err.h>
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070032#include <openssl/hmac.h>
33#include <openssl/kdf.h>
34#include <openssl/pem.h>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070035
36namespace ndn {
37namespace ndncert {
38
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070039ECDHState::ECDHState()
40{
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070041 auto EC_NID = NID_X9_62_prime256v1;
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070042 // params context
43 EVP_PKEY_CTX* ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr);
44 if (ctx_params == nullptr) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070045 NDN_THROW(std::runtime_error("Could not create context."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070046 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070047 if (EVP_PKEY_paramgen_init(ctx_params) != 1) {
48 EVP_PKEY_CTX_free(ctx_params);
Zhiyi Zhang5c059452020-10-16 17:43:31 -070049 NDN_THROW(std::runtime_error("Could not initialize parameter generation."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070050 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070051 if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx_params, EC_NID)) {
52 EVP_PKEY_CTX_free(ctx_params);
Zhiyi Zhang5c059452020-10-16 17:43:31 -070053 NDN_THROW(std::runtime_error("Likely unknown elliptical curve ID specified."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070055 // generate params
56 EVP_PKEY* params = nullptr;
57 if (!EVP_PKEY_paramgen(ctx_params, &params)) {
58 EVP_PKEY_CTX_free(ctx_params);
Zhiyi Zhang5c059452020-10-16 17:43:31 -070059 NDN_THROW(std::runtime_error("Could not create parameter object parameters."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070061 // key generation context
62 EVP_PKEY_CTX *ctx_keygen = EVP_PKEY_CTX_new(params, nullptr);
63 if (ctx_keygen == nullptr) {
64 EVP_PKEY_free(params);
65 EVP_PKEY_CTX_free(ctx_params);
Zhiyi Zhang5c059452020-10-16 17:43:31 -070066 NDN_THROW(std::runtime_error("Could not create the context for the key generation"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070068 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 Zhang5c059452020-10-16 17:43:31 -070072 NDN_THROW(std::runtime_error("Could not init context for key generation."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070074 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 Zhang5c059452020-10-16 17:43:31 -070078 NDN_THROW(std::runtime_error("Could not generate DHE keys in final step"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070080 EVP_PKEY_CTX_free(ctx_keygen);
81 EVP_PKEY_free(params);
82 EVP_PKEY_CTX_free(ctx_params);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083}
84
85ECDHState::~ECDHState()
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086{
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070087 if (m_privkey != nullptr) {
88 EVP_PKEY_free(m_privkey);
89 }
90}
91
92const std::vector<uint8_t>&
93ECDHState::getSelfPubKey()
94{
95 auto privECKey = EVP_PKEY_get1_EC_KEY(m_privkey);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096 if (privECKey == nullptr) {
Zhiyi Zhang5c059452020-10-16 17:43:31 -070097 NDN_THROW(std::runtime_error("Could not get key when calling EVP_PKEY_get1_EC_KEY()."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099 auto ecPoint = EC_KEY_get0_public_key(privECKey);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700100 auto group = EC_KEY_get0_group(privECKey);
101 auto requiredBufLen = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED, nullptr, 0, nullptr);
102 m_pubKey.resize(requiredBufLen);
103 auto rev = EC_POINT_point2oct(group, ecPoint, POINT_CONVERSION_COMPRESSED,
104 m_pubKey.data(), requiredBufLen, nullptr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700105 EC_KEY_free(privECKey);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700106 if (rev == 0) {
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700107 NDN_THROW(std::runtime_error("Could not convert EC_POINTS to octet string when calling EC_POINT_point2oct."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700109 return m_pubKey;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110}
111
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700112const std::vector<uint8_t>&
113ECDHState::deriveSecret(const std::vector<uint8_t>& peerKey)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114{
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700115 // prepare self private key
116 auto privECKey = EVP_PKEY_get1_EC_KEY(m_privkey);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700117 if (privECKey == nullptr) {
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700118 NDN_THROW(std::runtime_error("Cannot not get key when calling EVP_PKEY_get1_EC_KEY()"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120 auto group = EC_KEY_get0_group(privECKey);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700121 EC_KEY_free(privECKey);
122 // prepare the peer public key
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 auto peerPoint = EC_POINT_new(group);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700124 if (peerPoint == nullptr) {
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700125 NDN_THROW(std::runtime_error("Cannot create the EC_POINT for peer key when calling EC_POINT_new()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700126 }
Zhiyi Zhang0eb74242020-10-20 18:33:22 -0700127 if (EC_POINT_oct2point(group, peerPoint, peerKey.data(), peerKey.size(), nullptr) == 0) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128 EC_POINT_free(peerPoint);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700129 NDN_THROW(std::runtime_error("Cannot convert peer's key into a EC point when calling EC_POINT_oct2point()"));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800130 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700131 EC_KEY* ecPeerkey = EC_KEY_new();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700132 if (ecPeerkey == nullptr) {
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800133 EC_POINT_free(peerPoint);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700134 NDN_THROW(std::runtime_error("Cannot create EC_KEY for peer key when calling EC_KEY_new()"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700135 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700136 if (EC_KEY_set_group(ecPeerkey, group) != 1) {
137 EC_POINT_free(peerPoint);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700138 NDN_THROW(std::runtime_error("Cannot set group for peer key's EC_KEY when calling EC_KEY_set_group()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700139 }
140 if (EC_KEY_set_public_key(ecPeerkey, peerPoint) == 0) {
141 EC_KEY_free(ecPeerkey);
142 EC_POINT_free(peerPoint);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700143 NDN_THROW(std::runtime_error("Cannot initialize peer EC_KEY with the EC_POINT when calling EC_KEY_set_public_key()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700144 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700145 EVP_PKEY* evpPeerkey = EVP_PKEY_new();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700146 if (EVP_PKEY_set1_EC_KEY(evpPeerkey, ecPeerkey) == 0) {
147 EC_KEY_free(ecPeerkey);
148 EC_POINT_free(peerPoint);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700149 NDN_THROW(std::runtime_error("Cannot create EVP_PKEY for peer key when calling EVP_PKEY_new()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700150 }
151 EC_KEY_free(ecPeerkey);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700152 EC_POINT_free(peerPoint);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700153 // ECDH context
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700154 EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(m_privkey, nullptr);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700155 if (ctx == nullptr) {
156 EVP_PKEY_free(evpPeerkey);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700157 NDN_THROW(std::runtime_error("Cannot create context for ECDH when calling EVP_PKEY_CTX_new()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700158 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700159 // Initialize
160 if (1 != EVP_PKEY_derive_init(ctx)) {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700161 EVP_PKEY_CTX_free(ctx);
162 EVP_PKEY_free(evpPeerkey);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700163 NDN_THROW(std::runtime_error("Cannot initialize context for ECDH when calling EVP_PKEY_derive_init()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700164 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700165 // Provide the peer public key
166 if (1 != EVP_PKEY_derive_set_peer(ctx, evpPeerkey)) {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700167 EVP_PKEY_CTX_free(ctx);
168 EVP_PKEY_free(evpPeerkey);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700169 NDN_THROW(std::runtime_error("Cannot set peer key for ECDH when calling EVP_PKEY_derive_set_peer()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700170 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700171 // Determine buffer length for shared secret
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700172 size_t secretLen = 0;
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700173 if (1 != EVP_PKEY_derive(ctx, nullptr, &secretLen)) {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700174 EVP_PKEY_CTX_free(ctx);
175 EVP_PKEY_free(evpPeerkey);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700176 NDN_THROW(std::runtime_error("Cannot determine the needed buffer length when calling EVP_PKEY_derive()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700177 }
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700178 m_secret.resize(secretLen);
179 // Derive the shared secret
180 if (1 != (EVP_PKEY_derive(ctx, m_secret.data(), &secretLen))) {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700181 EVP_PKEY_CTX_free(ctx);
182 EVP_PKEY_free(evpPeerkey);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700183 NDN_THROW(std::runtime_error("Cannot derive ECDH secret when calling EVP_PKEY_derive()"));
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700184 }
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700185 EVP_PKEY_CTX_free(ctx);
186 EVP_PKEY_free(evpPeerkey);
187 return m_secret;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700188}
189
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700190void
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700191hmacSha256(const uint8_t* data, size_t dataLen,
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700192 const uint8_t* key, size_t keyLen,
193 uint8_t* result)
Davide Pesavento368341b2019-08-13 23:57:50 -0400194{
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700195 auto ret = HMAC(EVP_sha256(), key, keyLen,
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700196 data, dataLen, result, nullptr);
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700197 if (ret == nullptr) {
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700198 NDN_THROW(std::runtime_error("Error computing HMAC when calling HMAC()"));
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700199 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700200}
201
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700202size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700203hkdf(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 Zhangaf7c2902019-03-14 22:13:21 -0700206{
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700207 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr);
208 if (EVP_PKEY_derive_init(pctx) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700209 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700210 NDN_THROW(std::runtime_error("HKDF: Cannot init ctx when calling EVP_PKEY_derive_init()."));
Zhiyi Zhanga2ce5992019-08-14 17:35:00 -0700211 }
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700212 if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700213 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700214 NDN_THROW(std::runtime_error("HKDF: Cannot set md when calling EVP_PKEY_CTX_set_hkdf_md()."));
Zhiyi Zhanga2ce5992019-08-14 17:35:00 -0700215 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700216 if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltLen) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700217 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700218 NDN_THROW(std::runtime_error("HKDF: Cannot set salt when calling EVP_PKEY_CTX_set1_hkdf_salt()."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700219 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700220 if (EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, secretLen) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700221 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700222 NDN_THROW(std::runtime_error("HKDF: Cannot set secret when calling EVP_PKEY_CTX_set1_hkdf_key()."));
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700223 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700224 if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infoLen) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700225 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700226 NDN_THROW(std::runtime_error("HKDF: Cannot set info when calling EVP_PKEY_CTX_add1_hkdf_info()."));
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700227 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700228 size_t outLen = outputLen;
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700229 if (EVP_PKEY_derive(pctx, output, &outLen) <= 0) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700230 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700231 NDN_THROW(std::runtime_error("HKDF: Cannot derive result when calling EVP_PKEY_derive()."));
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700232 }
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700233 EVP_PKEY_CTX_free(pctx);
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700234 return outLen;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700235}
236
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700237int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700238aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen,
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700239 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag)
240{
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700241 EVP_CIPHER_CTX* ctx;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700242 int len;
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700243 int ciphertextLen;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700244 if (!(ctx = EVP_CIPHER_CTX_new())) {
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700245 NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700246 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700247 if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700248 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700249 NDN_THROW(std::runtime_error("Cannot initialize the encryption operation when calling EVP_EncryptInit_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700250 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700251 if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700252 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700253 NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700254 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700255 if (1 != EVP_EncryptInit_ex(ctx, nullptr, nullptr, key, iv)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700256 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700257 NDN_THROW(std::runtime_error("Cannot initialize key and IV when calling EVP_EncryptInit_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700258 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700259 if (1 != EVP_EncryptUpdate(ctx, nullptr, &len, associated, associatedLen)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700260 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700261 NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700262 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700263 if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintextLen)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700264 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700265 NDN_THROW(std::runtime_error("Cannot encrypt when calling EVP_EncryptUpdate()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700266 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700267 ciphertextLen = len;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700268 if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700269 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700270 NDN_THROW(std::runtime_error("Cannot finalise the encryption when calling EVP_EncryptFinal_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700271 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700272 ciphertextLen += len;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700273 if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700274 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700275 NDN_THROW(std::runtime_error("Cannot get tag when calling EVP_CIPHER_CTX_ctrl()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700276 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700277 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700278 return ciphertextLen;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700279}
280
281int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700282aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen,
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700283 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext)
284{
285 EVP_CIPHER_CTX* ctx;
286 int len;
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700287 int plaintextLen;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700288 int ret;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700289 if (!(ctx = EVP_CIPHER_CTX_new())) {
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700290 NDN_THROW(std::runtime_error("Cannot create and initialise the context when calling EVP_CIPHER_CTX_new()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700291 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700292 if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700293 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700294 NDN_THROW(std::runtime_error("Cannot initialise the decryption operation when calling EVP_DecryptInit_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700295 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700296 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700297 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700298 NDN_THROW(std::runtime_error("Cannot set IV length when calling EVP_CIPHER_CTX_ctrl"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700299 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700300 if (!EVP_DecryptInit_ex(ctx, nullptr, nullptr, key, iv)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700301 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700302 NDN_THROW(std::runtime_error("Cannot initialise key and IV when calling EVP_DecryptInit_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700303 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700304 if (!EVP_DecryptUpdate(ctx, nullptr, &len, associated, associatedLen)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700305 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700306 NDN_THROW(std::runtime_error("Cannot set associated authentication data when calling EVP_EncryptUpdate()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700307 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700308 if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertextLen)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700309 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang5c059452020-10-16 17:43:31 -0700310 NDN_THROW(std::runtime_error("Cannot decrypt when calling EVP_DecryptUpdate()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700311 }
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700312 plaintextLen = len;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700313 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag)) {
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -0700314 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700315 NDN_THROW(std::runtime_error("Cannot set tag value when calling EVP_CIPHER_CTX_ctrl()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700316 }
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700317 ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700318 EVP_CIPHER_CTX_free(ctx);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700319 if (ret > 0) {
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700320 plaintextLen += len;
321 return plaintextLen;
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700322 }
323 else {
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700324 NDN_THROW(std::runtime_error("Cannot finalize the decryption when calling EVP_DecryptFinal_ex()"));
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700325 }
326}
327
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700328Block
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700329encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700330 const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700331{
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700332 Buffer iv(12);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700333 random::generateSecureBytes(iv.data(), 8);
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700334 if (tlvType == ndn::tlv::ApplicationParameters) {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700335 // requester
336 iv[0] &= ~(1UL << 7);
337 }
338 else {
339 // CA
340 iv[0] |= 1UL << 7;
341 }
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700342 uint32_t temp = boost::endian::native_to_big(counter);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700343 std::memcpy(&iv[8], reinterpret_cast<const uint8_t*>(&temp), 4);
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700344 uint32_t increment = (payloadSize + 15) / 16;
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700345 if (std::numeric_limits<uint32_t>::max() - counter < increment) {
346 // simply set counter to be 0. Will not hurt the property of being unique.
347 counter = 0;
348 }
349 else {
350 counter += increment;
351 }
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700352
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700353 Buffer encryptedPayload(payloadSize);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700354 uint8_t tag[16];
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700355 size_t encryptedPayloadLen = aesGcm128Encrypt(payload, payloadSize, associatedData, associatedDataSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700356 key, iv.data(), encryptedPayload.data(), tag);
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700357 auto content = makeEmptyBlock(tlvType);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700358 content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size()));
359 content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16));
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700360 content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload.data(), encryptedPayloadLen));
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700361 content.encode();
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700362 return content;
363}
364
365Buffer
366decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, const uint8_t* associatedData, size_t associatedDataSize)
367{
368 block.parse();
Zhiyi Zhangcace6ab2020-10-16 21:51:44 -0700369 Buffer result(block.get(tlv::EncryptedPayload).value_size());
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700370 int resultLen = aesGcm128Decrypt(block.get(tlv::EncryptedPayload).value(),
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700371 block.get(tlv::EncryptedPayload).value_size(),
372 associatedData, associatedDataSize, block.get(tlv::AuthenticationTag).value(),
373 key, block.get(tlv::InitializationVector).value(), result.data());
374 if (resultLen == -1 || resultLen != (int)block.get(tlv::EncryptedPayload).value_size()) {
375 return Buffer();
376 }
377 return result;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700378}
379
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700380} // namespace ndncert
381} // namespace ndn