blob: 00b91b1196f99d9ecfde2225962002ace2dac219 [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 Zhangc5d93a92020-10-14 17:07:35 -070021#ifndef NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP
22#define NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023
tylerliu50d679e2020-10-14 14:08:39 -070024#include "ndncert-common.hpp"
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070025
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070026namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang63123172020-10-12 14:24:44 -070029class ECDHState
30{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070031public:
32 ECDHState();
33 ~ECDHState();
34
35 std::string
36 getBase64PubKey();
37
38 uint8_t*
39 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhange4891b72020-10-10 15:11:57 -070040
tylerliudd359912020-10-20 13:05:22 -070041NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhange4891b72020-10-10 15:11:57 -070042 uint8_t*
Zhiyi Zhang5c059452020-10-16 17:43:31 -070043 deriveSecret(const uint8_t* peerkey, size_t peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044
45 uint8_t*
46 getRawSelfPubKey();
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070047
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070048public:
49 uint8_t m_publicKey[256];
50 size_t m_publicKeyLen = 0;
51 uint8_t m_sharedSecret[256];
52 size_t m_sharedSecretLen = 0;
53
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070054private:
55 struct ECDH_CTX;
56 unique_ptr<ECDH_CTX> context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057};
58
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070059/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070060 * @brief HMAC based key derivation function (HKDF).
61 *
62 * @param secret The input to the HKDF.
63 * @param secret_len The length of the secret.
64 * @param salt The salt used in HKDF.
65 * @param salt_len The length of the salt.
66 * @param output The output of the HKDF.
67 * @param output_len The length of expected output.
68 * @param info The additional information used in HKDF.
69 * @param info_len The length of the additional information.
70 * @return int The length of the derived key if successful, -1 if failed.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070071 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072int
Zhiyi Zhang5c059452020-10-16 17:43:31 -070073hkdf(const uint8_t* secret, size_t secret_len,
74 const uint8_t* salt, size_t salt_len,
75 uint8_t* output, size_t output_len,
76 const uint8_t* info = nullptr, size_t info_len = 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070078/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070079 * @brief HMAC based on SHA-256.
80 *
81 * @param data The intput array to hmac.
82 * @param data_length The length of the input array.
83 * @param key The HMAC key.
84 * @param key_length The length of the HMAC key.
85 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070086 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070087 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070088void
Zhiyi Zhang5c059452020-10-16 17:43:31 -070089hmac_sha256(const uint8_t* data, size_t data_length,
90 const uint8_t* key, size_t key_length,
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070091 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070092
93/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070094 * @brief Authenticated GCM 128 Encryption with associated data.
95 *
96 * @param plaintext The plaintext.
97 * @param plaintext_len The size of plaintext.
98 * @param associated The associated authentication data.
99 * @param associated_len The size of associated authentication data.
100 * @param key 16 bytes AES key.
101 * @param iv 12 bytes IV.
102 * @param ciphertext The output and enough memory must be allocated beforehands.
103 * @param tag 16 bytes tag.
104 * @return int The size of ciphertext.
105 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700106 */
107int
108aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
109 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
110
111/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700112 * @brief Authenticated GCM 128 Decryption with associated data.
113 *
114 * @param ciphertext The ciphertext.
115 * @param ciphertext_len The size of ciphertext.
116 * @param associated The associated authentication data.
117 * @param associated_len The size of associated authentication data.
118 * @param tag 16 bytes tag.
119 * @param key 16 bytes AES key.
120 * @param iv 12 bytes IV.
121 * @param plaintext The output and enough memory must be allocated beforehands.
122 * @return int The size of plaintext or -1 if the verification fails.
123 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700124 */
125int
126aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
127 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700129/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700130 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
131 *
132 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
133 * @param key The AES key used for encryption.
134 * @param payload The plaintext payload.
135 * @param payloadSize The size of the plaintext payload.
136 * @param associatedData The associated data used for authentication.
137 * @param associatedDataSize The size of associated data.
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700138 * @param counter The counter of blocks that have been encrypted by the requester/CA.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700139 * @return Block The TLV block with @param tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700140 */
141Block
142encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700143 const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700144
145/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700146 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
147 *
148 * @param block The TLV block in the format of NDNCERT protocol.
149 * @param key The AES key used for encryption.
150 * @param associatedData The associated data used for authentication.
151 * @param associatedDataSize The size of associated data.
152 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700153 */
154Buffer
155decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
156 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700157
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700158} // namespace ndncert
159} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700160
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700161#endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP