blob: 97017863f630f561067758d1ce8e816bbd4e960f [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa34d6de2021-11-18 22:16:04 -05002/*
3 * Copyright (c) 2017-2021, 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 Zhangdef00e42020-10-20 22:41:56 -070021#ifndef NDNCERT_DETAIL_CRYPTO_HELPERS_HPP
22#define NDNCERT_DETAIL_CRYPTO_HELPERS_HPP
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023
Zhiyi Zhangb041d442020-10-22 21:57:11 -070024#include "detail/ndncert-common.hpp"
Davide Pesaventoa34d6de2021-11-18 22:16:04 -050025
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070026#include <openssl/evp.h>
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070027
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028namespace ndn {
29namespace ndncert {
30
Zhiyi Zhang159ba632020-10-20 15:09:12 -070031/**
32 * @brief State for ECDH.
33 *
34 * The ECDH is based on prime256v1.
35 */
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070036class ECDHState : noncopyable
Zhiyi Zhang63123172020-10-12 14:24:44 -070037{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070038public:
39 ECDHState();
Davide Pesaventoa34d6de2021-11-18 22:16:04 -050040
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041 ~ECDHState();
42
Zhiyi Zhang5e74ecd2020-10-21 12:52:25 -070043 /**
44 * @brief Derive ECDH secret from peer's EC public key and self's private key.
45 *
46 * @param peerkey Peer's EC public key in the uncompressed octet string format.
47 * See details in https://www.openssl.org/docs/man1.1.1/man3/EC_POINT_point2oct.html.
48 * @return const std::vector<uint8_t>& the derived secret.
49 */
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070050 const std::vector<uint8_t>&
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070051 deriveSecret(const std::vector<uint8_t>& peerkey);
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070052
Zhiyi Zhang5e74ecd2020-10-21 12:52:25 -070053 /**
54 * @brief Get the Self Pub Key object
55 *
56 * @return const std::vector<uint8_t>& the Self public key in the uncompressed oct string format.
57 * See details in https://www.openssl.org/docs/man1.1.1/man3/EC_POINT_point2oct.html.
58 */
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070059 const std::vector<uint8_t>&
60 getSelfPubKey();
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070061
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070062private:
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070063 EVP_PKEY* m_privkey = nullptr;
64 std::vector<uint8_t> m_pubKey;
65 std::vector<uint8_t> m_secret;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066};
67
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070068/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070069 * @brief HMAC based key derivation function (HKDF).
70 *
71 * @param secret The input to the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070072 * @param secretLen The length of the secret.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070073 * @param salt The salt used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070074 * @param saltLen The length of the salt.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070075 * @param output The output of the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070076 * @param outputLen The length of expected output.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070077 * @param info The additional information used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070078 * @param infoLen The length of the additional information.
Zhiyi Zhang159ba632020-10-20 15:09:12 -070079 * @return size_t The length of the derived key if successful.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070080 */
Zhiyi Zhang159ba632020-10-20 15:09:12 -070081size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070082hkdf(const uint8_t* secret, size_t secretLen,
83 const uint8_t* salt, size_t saltLen,
84 uint8_t* output, size_t outputLen,
85 const uint8_t* info = nullptr, size_t infoLen = 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070087/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070088 * @brief HMAC based on SHA-256.
89 *
90 * @param data The intput array to hmac.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070091 * @param dataLen The length of the input array.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070092 * @param key The HMAC key.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070093 * @param keyLen The length of the HMAC key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070094 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070095 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070096 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070097void
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070098hmacSha256(const uint8_t* data, size_t dataLen,
99 const uint8_t* key, size_t keyLen,
100 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700101
102/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700103 * @brief Authenticated GCM 128 Encryption with associated data.
104 *
105 * @param plaintext The plaintext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700106 * @param plaintextLen The size of plaintext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700107 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700108 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700109 * @param key 16 bytes AES key.
110 * @param iv 12 bytes IV.
111 * @param ciphertext The output and enough memory must be allocated beforehands.
112 * @param tag 16 bytes tag.
Zhiyi Zhange5a07c92020-10-21 10:51:37 -0700113 * @return size_t The size of ciphertext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700114 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700115 */
Zhiyi Zhange5a07c92020-10-21 10:51:37 -0700116size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700117aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen,
118 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700119
120/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700121 * @brief Authenticated GCM 128 Decryption with associated data.
122 *
123 * @param ciphertext The ciphertext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700124 * @param ciphertextLen The size of ciphertext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700125 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700126 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700127 * @param tag 16 bytes tag.
128 * @param key 16 bytes AES key.
129 * @param iv 12 bytes IV.
130 * @param plaintext The output and enough memory must be allocated beforehands.
Zhiyi Zhange5a07c92020-10-21 10:51:37 -0700131 * @return size_t The size of plaintext.
132 * @throw runtime_error When there is an error in the process of decryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700133 */
Zhiyi Zhange5a07c92020-10-21 10:51:37 -0700134size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700135aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen,
136 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700138/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700139 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
140 *
Zhiyi Zhang5e74ecd2020-10-21 12:52:25 -0700141 * The TLV spec: https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption.
142 *
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700143 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
144 * @param key The AES key used for encryption.
145 * @param payload The plaintext payload.
146 * @param payloadSize The size of the plaintext payload.
147 * @param associatedData The associated data used for authentication.
148 * @param associatedDataSize The size of associated data.
Zhiyi Zhangc1e98152020-12-21 16:15:39 -0800149 * @param encryptionIv The IV used by the last AES encryption and is needed by subsequent invocations of this function
150 * with the same @p key.
Zhiyi Zhang199508a2020-10-21 10:45:50 -0700151 * @return Block The TLV block with @p tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700152 */
153Block
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800154encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key,
155 const uint8_t* payload, size_t payloadSize,
156 const uint8_t* associatedData, size_t associatedDataSize,
Zhiyi Zhangc1e98152020-12-21 16:15:39 -0800157 std::vector<uint8_t>& encryptionIv);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700158
159/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700160 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
161 *
Zhiyi Zhang5e74ecd2020-10-21 12:52:25 -0700162 * The TLV spec: https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption.
163 *
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700164 * @param block The TLV block in the format of NDNCERT protocol.
165 * @param key The AES key used for encryption.
166 * @param associatedData The associated data used for authentication.
167 * @param associatedDataSize The size of associated data.
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800168 * @param decryptionIv The IV observed in the last AES decryption and is needed by subsequent
169 * invocations of this function with the same @p key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700170 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700171 */
172Buffer
173decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800174 const uint8_t* associatedData, size_t associatedDataSize,
Zhiyi Zhang3b9a5032021-02-18 11:15:09 -0800175 std::vector<uint8_t>& decryptionIv, const std::vector<uint8_t>& encryptionIv);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700176
Davide Pesaventoa34d6de2021-11-18 22:16:04 -0500177#ifdef NDNCERT_HAVE_TESTS
178uint32_t
179loadBigU32(const uint8_t* src) noexcept;
180
181void
182storeBigU32(uint8_t* dest, uint32_t src) noexcept;
183#endif
184
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700185} // namespace ndncert
186} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700187
Zhiyi Zhangdef00e42020-10-20 22:41:56 -0700188#endif // NDNCERT_DETAIL_CRYPTO_HELPERS_HPP