blob: 50aa7bb9d935e46b4e07f5349a07395653e71c6e [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};
tylerliu8e170d62020-09-30 01:31:53 -070031static const int AES_128_KEY_LEN = 16;
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070032
Zhiyi Zhang63123172020-10-12 14:24:44 -070033class ECDHState
34{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070035public:
36 ECDHState();
37 ~ECDHState();
38
39 std::string
40 getBase64PubKey();
41
42 uint8_t*
43 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhange4891b72020-10-10 15:11:57 -070044
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070045 uint8_t m_publicKey[256];
46 size_t m_publicKeyLen;
47 uint8_t m_sharedSecret[256];
48 size_t m_sharedSecretLen;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049
Zhiyi Zhange4891b72020-10-10 15:11:57 -070050PUBLIC_WITH_TESTS_ELSE_PRIVATE:
51 uint8_t*
52 deriveSecret(const uint8_t* peerkey, int peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053
54 uint8_t*
55 getRawSelfPubKey();
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070056
57private:
58 struct ECDH_CTX;
59 unique_ptr<ECDH_CTX> context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060};
61
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070062/**
63 * HMAC based key derivation function (HKDF)
64 * @p secret, intput, the input to the HKDF
65 * @p secretLen, intput, the length of the secret
66 * @p salt, intput, the salt used in HKDF
67 * @p saltLen, intput, the length of the salt
68 * @p output, output, the output of the HKDF
69 * @p output_len, intput, the length of expected output
70 * @p info, intput, the additional information used in HKDF
71 * @p info_len, intput, the additional information used in HKDF
72 * @return the length of the derived key if successful, -1 if failed
73 */
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 Zhang97bedb82020-10-10 11:11:35 -070081 * HMAC based on SHA-256
82 * @p data, intput, the array to hmac
83 * @p data_length, intput, the length of the array
84 * @p key, intput, the key for the function
85 * @p key_len, intput, the length of the key
86 * @p result, output, result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070087 * @return 0 if successful, -1 if failed
88 */
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070089int
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070090hmac_sha256(const uint8_t* data, const unsigned data_length,
91 const uint8_t* key, const unsigned key_length,
92 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070093
94/**
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070095 * Authenticated GCM 128 Encryption with associated data
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070096 * @p plaintext, input, plaintext
97 * @p plaintext_len, input, size of plaintext
98 * @p associated, input, associated authentication data
99 * @p associated_len, input, size of associated authentication data
100 * @p key, input, 16 bytes AES key
101 * @p iv, input, 12 bytes IV
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700102 * @p ciphertext, output, enough memory must be allocated beforehands
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700103 * @p tag, output, 16 bytes tag
104 * @return the size of ciphertext
Zhiyi Zhangccd5a0c2020-10-12 13:48:16 -0700105 * @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 Zhang97bedb82020-10-10 11:11:35 -0700112 * Authenticated GCM 128 Decryption with associated data
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700113 * @p ciphertext, input, ciphertext
114 * @p ciphertext_len, input, size of ciphertext
115 * @p associated, input, associated authentication data
116 * @p associated_len, input, size of associated authentication data
117 * @p tag, input, 16 bytes tag
118 * @p key, input, 16 bytes AES key
119 * @p iv, input, 12 bytes IV
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700120 * @p plaintext, output, enough memory must be allocated beforehands
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700121 * @return the size of plaintext or -1 if the verification fails
Zhiyi Zhangccd5a0c2020-10-12 13:48:16 -0700122 * @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/**
129 * Encode the payload into TLV block with Authenticated GCM 128 Encryption
130 * @p tlv::type, intput, the TLV TYPE of the encoded block, either ApplicationParameters or Content
131 * @p key, intput, 16 Bytes, the AES key used for encryption
132 * @p payload, input, the plaintext payload
133 * @p payloadSize, input, the size of the plaintext payload
134 * @p associatedData, input, associated data used for authentication
135 * @p associatedDataSize, input, the size of associated data
136 * @return the TLV block with @p tlv::type TLV TYPE
137 */
138Block
139encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
140 const uint8_t* associatedData, size_t associatedDataSize);
141
142/**
143 * Decode the payload from TLV block with Authenticated GCM 128 Encryption
144 * @p block, intput, the TLV block in the format of NDNCERT protocol
145 * @p key, intput, 16 Bytes, the AES key used for encryption
146 * @p associatedData, input, associated data used for authentication
147 * @p associatedDataSize, input, the size of associated data
148 * @return the plaintext buffer
149 */
150Buffer
151decodeBlockWithAesGcm128(const Block& block, const uint8_t* key,
152 const uint8_t* associatedData, size_t associatedDataSize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700153
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700154} // namespace ndncert
155} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700156
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700157#endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP