blob: 8463eb66c32a881fb5752fd84ff1a4b3c9221f88 [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
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070041 uint8_t m_publicKey[256];
42 size_t m_publicKeyLen;
43 uint8_t m_sharedSecret[256];
44 size_t m_sharedSecretLen;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045
Zhiyi Zhange4891b72020-10-10 15:11:57 -070046PUBLIC_WITH_TESTS_ELSE_PRIVATE:
47 uint8_t*
Zhiyi Zhang5c059452020-10-16 17:43:31 -070048 deriveSecret(const uint8_t* peerkey, size_t peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049
50 uint8_t*
51 getRawSelfPubKey();
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070052
53private:
54 struct ECDH_CTX;
55 unique_ptr<ECDH_CTX> context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056};
57
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070058/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070059 * @brief HMAC based key derivation function (HKDF).
60 *
61 * @param secret The input to the HKDF.
62 * @param secret_len The length of the secret.
63 * @param salt The salt used in HKDF.
64 * @param salt_len The length of the salt.
65 * @param output The output of the HKDF.
66 * @param output_len The length of expected output.
67 * @param info The additional information used in HKDF.
68 * @param info_len The length of the additional information.
69 * @return int The length of the derived key if successful, -1 if failed.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070070 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071int
Zhiyi Zhang5c059452020-10-16 17:43:31 -070072hkdf(const uint8_t* secret, size_t secret_len,
73 const uint8_t* salt, size_t salt_len,
74 uint8_t* output, size_t output_len,
75 const uint8_t* info = nullptr, size_t info_len = 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070077/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070078 * @brief HMAC based on SHA-256.
79 *
80 * @param data The intput array to hmac.
81 * @param data_length The length of the input array.
82 * @param key The HMAC key.
83 * @param key_length The length of the HMAC key.
84 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070085 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070086 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070087void
Zhiyi Zhang5c059452020-10-16 17:43:31 -070088hmac_sha256(const uint8_t* data, size_t data_length,
89 const uint8_t* key, size_t key_length,
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070090 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070091
92/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070093 * @brief Authenticated GCM 128 Encryption with associated data.
94 *
95 * @param plaintext The plaintext.
96 * @param plaintext_len The size of plaintext.
97 * @param associated The associated authentication data.
98 * @param associated_len The size of associated authentication data.
99 * @param key 16 bytes AES key.
100 * @param iv 12 bytes IV.
101 * @param ciphertext The output and enough memory must be allocated beforehands.
102 * @param tag 16 bytes tag.
103 * @return int The size of ciphertext.
104 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700105 */
106int
107aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
108 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
109
110/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700111 * @brief Authenticated GCM 128 Decryption with associated data.
112 *
113 * @param ciphertext The ciphertext.
114 * @param ciphertext_len The size of ciphertext.
115 * @param associated The associated authentication data.
116 * @param associated_len The size of associated authentication data.
117 * @param tag 16 bytes tag.
118 * @param key 16 bytes AES key.
119 * @param iv 12 bytes IV.
120 * @param plaintext The output and enough memory must be allocated beforehands.
121 * @return int The size of plaintext or -1 if the verification fails.
122 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700123 */
124int
125aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
126 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700127
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700128/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700129 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
130 *
131 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
132 * @param key The AES key used for encryption.
133 * @param payload The plaintext payload.
134 * @param payloadSize The size of the plaintext payload.
135 * @param associatedData The associated data used for authentication.
136 * @param associatedDataSize The size of associated data.
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700137 * @param counter The counter of blocks that have been encrypted by the requester/CA.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700138 * @return Block The TLV block with @param tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700139 */
140Block
141encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700142 const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700143
144/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700145 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
146 *
147 * @param block The TLV block in the format of NDNCERT protocol.
148 * @param key The AES key used for encryption.
149 * @param associatedData The associated data used for authentication.
150 * @param associatedDataSize The size of associated data.
151 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700152 */
153Buffer
154decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
155 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700156
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700157} // namespace ndncert
158} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700159
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700160#endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP