blob: 3eee55325909c448814f50e54643c00124defd50 [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhanga67fa462020-04-19 13:48:03 -07003 * 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 Zhang159ba632020-10-20 15:09:12 -070021#ifndef NDNCERT_DETAIL_CRYPTO_HELPER_HPP
22#define NDNCERT_DETAIL_CRYPTO_HELPER_HPP
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023
tylerliu50d679e2020-10-14 14:08:39 -070024#include "ndncert-common.hpp"
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070025#include <openssl/evp.h>
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070026
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070027namespace ndn {
28namespace ndncert {
29
Zhiyi Zhang159ba632020-10-20 15:09:12 -070030/**
31 * @brief State for ECDH.
32 *
33 * The ECDH is based on prime256v1.
34 */
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070035class ECDHState : noncopyable
Zhiyi Zhang63123172020-10-12 14:24:44 -070036{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070037public:
38 ECDHState();
39 ~ECDHState();
40
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070041 const std::vector<uint8_t>&
Zhiyi Zhang5c059452020-10-16 17:43:31 -070042 deriveSecret(const uint8_t* peerkey, size_t peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070044 const std::vector<uint8_t>&
45 deriveSecret(const std::vector<uint8_t>& peerkey);
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070046
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070047 const std::vector<uint8_t>&
48 getSelfPubKey();
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070049
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070050private:
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070051 EVP_PKEY* m_privkey = nullptr;
52 std::vector<uint8_t> m_pubKey;
53 std::vector<uint8_t> m_secret;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054};
55
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070056/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070057 * @brief HMAC based key derivation function (HKDF).
58 *
59 * @param secret The input to the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070060 * @param secretLen The length of the secret.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070061 * @param salt The salt used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070062 * @param saltLen The length of the salt.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070063 * @param output The output of the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070064 * @param outputLen The length of expected output.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070065 * @param info The additional information used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070066 * @param infoLen The length of the additional information.
Zhiyi Zhang159ba632020-10-20 15:09:12 -070067 * @return size_t The length of the derived key if successful.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070068 */
Zhiyi Zhang159ba632020-10-20 15:09:12 -070069size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070070hkdf(const uint8_t* secret, size_t secretLen,
71 const uint8_t* salt, size_t saltLen,
72 uint8_t* output, size_t outputLen,
73 const uint8_t* info = nullptr, size_t infoLen = 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070075/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070076 * @brief HMAC based on SHA-256.
77 *
78 * @param data The intput array to hmac.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070079 * @param dataLen The length of the input array.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070080 * @param key The HMAC key.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070081 * @param keyLen The length of the HMAC key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070082 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070083 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070084 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070085void
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070086hmacSha256(const uint8_t* data, size_t dataLen,
87 const uint8_t* key, size_t keyLen,
88 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070089
90/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070091 * @brief Authenticated GCM 128 Encryption with associated data.
92 *
93 * @param plaintext The plaintext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070094 * @param plaintextLen The size of plaintext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070095 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070096 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070097 * @param key 16 bytes AES key.
98 * @param iv 12 bytes IV.
99 * @param ciphertext The output and enough memory must be allocated beforehands.
100 * @param tag 16 bytes tag.
101 * @return int The size of ciphertext.
102 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700103 */
104int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700105aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen,
106 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700107
108/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700109 * @brief Authenticated GCM 128 Decryption with associated data.
110 *
111 * @param ciphertext The ciphertext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700112 * @param ciphertextLen The size of ciphertext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700113 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700114 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700115 * @param tag 16 bytes tag.
116 * @param key 16 bytes AES key.
117 * @param iv 12 bytes IV.
118 * @param plaintext The output and enough memory must be allocated beforehands.
119 * @return int The size of plaintext or -1 if the verification fails.
120 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700121 */
122int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700123aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen,
124 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700125
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700126/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700127 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
128 *
129 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
130 * @param key The AES key used for encryption.
131 * @param payload The plaintext payload.
132 * @param payloadSize The size of the plaintext payload.
133 * @param associatedData The associated data used for authentication.
134 * @param associatedDataSize The size of associated data.
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700135 * @param counter An opaque counter that must be passed to subsequent invocations of this function
136 * with the same @param key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700137 * @return Block The TLV block with @param tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700138 */
139Block
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700140encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700141 const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700142
143/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700144 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
145 *
146 * @param block The TLV block in the format of NDNCERT protocol.
147 * @param key The AES key used for encryption.
148 * @param associatedData The associated data used for authentication.
149 * @param associatedDataSize The size of associated data.
150 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700151 */
152Buffer
153decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
154 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700155
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700156} // namespace ndncert
157} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700158
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700159#endif // NDNCERT_DETAIL_CRYPTO_HELPER_HPP