blob: c3546c4e32cdf9bc9f0a464451465a1920f29225 [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 Zhangef6b36a2020-09-22 21:20:59 -070029static const int INFO_LEN = 10;
30static const uint8_t INFO[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
31
Zhiyi Zhang63123172020-10-12 14:24:44 -070032class ECDHState
33{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070034public:
35 ECDHState();
36 ~ECDHState();
37
38 std::string
39 getBase64PubKey();
40
41 uint8_t*
42 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhange4891b72020-10-10 15:11:57 -070043
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070044 uint8_t m_publicKey[256];
45 size_t m_publicKeyLen;
46 uint8_t m_sharedSecret[256];
47 size_t m_sharedSecretLen;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048
Zhiyi Zhange4891b72020-10-10 15:11:57 -070049PUBLIC_WITH_TESTS_ELSE_PRIVATE:
50 uint8_t*
51 deriveSecret(const uint8_t* peerkey, int peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052
53 uint8_t*
54 getRawSelfPubKey();
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070055
56private:
57 struct ECDH_CTX;
58 unique_ptr<ECDH_CTX> context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059};
60
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070061/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070062 * @brief HMAC based key derivation function (HKDF).
63 *
64 * @param secret The input to the HKDF.
65 * @param secret_len The length of the secret.
66 * @param salt The salt used in HKDF.
67 * @param salt_len The length of the salt.
68 * @param output The output of the HKDF.
69 * @param output_len The length of expected output.
70 * @param info The additional information used in HKDF.
71 * @param info_len The length of the additional information.
72 * @return int The length of the derived key if successful, -1 if failed.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070073 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074int
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070075hkdf(const uint8_t* secret, int secret_len,
76 const uint8_t* salt, int salt_len,
77 uint8_t* output, int output_len,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070078 const uint8_t* info = INFO, int info_len = INFO_LEN);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070080/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070081 * @brief HMAC based on SHA-256.
82 *
83 * @param data The intput array to hmac.
84 * @param data_length The length of the input array.
85 * @param key The HMAC key.
86 * @param key_length The length of the HMAC key.
87 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070088 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070089 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070090void
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070091hmac_sha256(const uint8_t* data, const unsigned data_length,
92 const uint8_t* key, const unsigned key_length,
93 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070094
95/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070096 * @brief Authenticated GCM 128 Encryption with associated data.
97 *
98 * @param plaintext The plaintext.
99 * @param plaintext_len The size of plaintext.
100 * @param associated The associated authentication data.
101 * @param associated_len The size of associated authentication data.
102 * @param key 16 bytes AES key.
103 * @param iv 12 bytes IV.
104 * @param ciphertext The output and enough memory must be allocated beforehands.
105 * @param tag 16 bytes tag.
106 * @return int The size of ciphertext.
107 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700108 */
109int
110aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
111 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
112
113/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700114 * @brief Authenticated GCM 128 Decryption with associated data.
115 *
116 * @param ciphertext The ciphertext.
117 * @param ciphertext_len The size of ciphertext.
118 * @param associated The associated authentication data.
119 * @param associated_len The size of associated authentication data.
120 * @param tag 16 bytes tag.
121 * @param key 16 bytes AES key.
122 * @param iv 12 bytes IV.
123 * @param plaintext The output and enough memory must be allocated beforehands.
124 * @return int The size of plaintext or -1 if the verification fails.
125 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700126 */
127int
128aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
129 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700130
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700131/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700132 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
133 *
134 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
135 * @param key The AES key used for encryption.
136 * @param payload The plaintext payload.
137 * @param payloadSize The size of the plaintext payload.
138 * @param associatedData The associated data used for authentication.
139 * @param associatedDataSize The size of associated data.
140 * @return Block The TLV block with @param tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700141 */
142Block
143encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
144 const uint8_t* associatedData, size_t associatedDataSize);
145
146/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700147 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
148 *
149 * @param block The TLV block in the format of NDNCERT protocol.
150 * @param key The AES key used for encryption.
151 * @param associatedData The associated data used for authentication.
152 * @param associatedDataSize The size of associated data.
153 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700154 */
155Buffer
156decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
157 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700158
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700159} // namespace ndncert
160} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700161
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700162#endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP