blob: db7c061e7e806b18129bc2b005ee780903bbc00f [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 Zhangb8cb0472020-05-05 20:55:05 -070025
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070026namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang159ba632020-10-20 15:09:12 -070029/**
30 * @brief State for ECDH.
31 *
32 * The ECDH is based on prime256v1.
33 */
Zhiyi Zhang63123172020-10-12 14:24:44 -070034class ECDHState
35{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036public:
37 ECDHState();
38 ~ECDHState();
39
40 std::string
41 getBase64PubKey();
42
43 uint8_t*
44 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhange4891b72020-10-10 15:11:57 -070045
tylerliudd359912020-10-20 13:05:22 -070046NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhange4891b72020-10-10 15:11:57 -070047 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
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070053public:
54 uint8_t m_publicKey[256];
55 size_t m_publicKeyLen = 0;
56 uint8_t m_sharedSecret[256];
57 size_t m_sharedSecretLen = 0;
58
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070059private:
60 struct ECDH_CTX;
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070061 unique_ptr<ECDH_CTX> m_context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062};
63
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070064/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070065 * @brief HMAC based key derivation function (HKDF).
66 *
67 * @param secret The input to the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070068 * @param secretLen The length of the secret.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070069 * @param salt The salt used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070070 * @param saltLen The length of the salt.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070071 * @param output The output of the HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070072 * @param outputLen The length of expected output.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070073 * @param info The additional information used in HKDF.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070074 * @param infoLen The length of the additional information.
Zhiyi Zhang159ba632020-10-20 15:09:12 -070075 * @return size_t The length of the derived key if successful.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070076 */
Zhiyi Zhang159ba632020-10-20 15:09:12 -070077size_t
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070078hkdf(const uint8_t* secret, size_t secretLen,
79 const uint8_t* salt, size_t saltLen,
80 uint8_t* output, size_t outputLen,
81 const uint8_t* info = nullptr, size_t infoLen = 0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070083/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070084 * @brief HMAC based on SHA-256.
85 *
86 * @param data The intput array to hmac.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070087 * @param dataLen The length of the input array.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070088 * @param key The HMAC key.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070089 * @param keyLen The length of the HMAC key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070090 * @param result The result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands.
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070091 * @throw runtime_error when an error occurred in the underlying HMAC.
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070092 */
Zhiyi Zhangf12ee302020-10-14 17:46:44 -070093void
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -070094hmacSha256(const uint8_t* data, size_t dataLen,
95 const uint8_t* key, size_t keyLen,
96 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070097
98/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070099 * @brief Authenticated GCM 128 Encryption with associated data.
100 *
101 * @param plaintext The plaintext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700102 * @param plaintextLen The size of plaintext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700103 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700104 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700105 * @param key 16 bytes AES key.
106 * @param iv 12 bytes IV.
107 * @param ciphertext The output and enough memory must be allocated beforehands.
108 * @param tag 16 bytes tag.
109 * @return int The size of ciphertext.
110 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700111 */
112int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700113aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen,
114 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700115
116/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700117 * @brief Authenticated GCM 128 Decryption with associated data.
118 *
119 * @param ciphertext The ciphertext.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700120 * @param ciphertextLen The size of ciphertext.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700121 * @param associated The associated authentication data.
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700122 * @param associatedLen The size of associated authentication data.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700123 * @param tag 16 bytes tag.
124 * @param key 16 bytes AES key.
125 * @param iv 12 bytes IV.
126 * @param plaintext The output and enough memory must be allocated beforehands.
127 * @return int The size of plaintext or -1 if the verification fails.
128 * @throw runtime_error When there is an error in the process of encryption.
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700129 */
130int
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700131aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen,
132 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700133
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700134/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700135 * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption.
136 *
137 * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content.
138 * @param key The AES key used for encryption.
139 * @param payload The plaintext payload.
140 * @param payloadSize The size of the plaintext payload.
141 * @param associatedData The associated data used for authentication.
142 * @param associatedDataSize The size of associated data.
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700143 * @param counter An opaque counter that must be passed to subsequent invocations of this function
144 * with the same @param key.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700145 * @return Block The TLV block with @param tlv_type TLV TYPE.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700146 */
147Block
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700148encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700149 const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700150
151/**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700152 * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption.
153 *
154 * @param block The TLV block in the format of NDNCERT protocol.
155 * @param key The AES key used for encryption.
156 * @param associatedData The associated data used for authentication.
157 * @param associatedDataSize The size of associated data.
158 * @return Buffer The plaintext buffer.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700159 */
160Buffer
161decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
162 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700163
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700164} // namespace ndncert
165} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700166
Zhiyi Zhang159ba632020-10-20 15:09:12 -0700167#endif // NDNCERT_DETAIL_CRYPTO_HELPER_HPP