Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 4 | * |
| 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 Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 21 | #ifndef NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP |
| 22 | #define NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 23 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 24 | #include "ndncert-common.hpp" |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 25 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 29 | class ECDHState |
| 30 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 31 | public: |
| 32 | ECDHState(); |
| 33 | ~ECDHState(); |
| 34 | |
| 35 | std::string |
| 36 | getBase64PubKey(); |
| 37 | |
| 38 | uint8_t* |
| 39 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 40 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 41 | uint8_t m_publicKey[256]; |
| 42 | size_t m_publicKeyLen; |
| 43 | uint8_t m_sharedSecret[256]; |
| 44 | size_t m_sharedSecretLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | |
tylerliu | dd35991 | 2020-10-20 13:05:22 -0700 | [diff] [blame^] | 46 | NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 47 | uint8_t* |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 48 | deriveSecret(const uint8_t* peerkey, size_t peerKeySize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | |
| 50 | uint8_t* |
| 51 | getRawSelfPubKey(); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | struct ECDH_CTX; |
| 55 | unique_ptr<ECDH_CTX> context; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 58 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 59 | * @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 Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 70 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 71 | int |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 72 | hkdf(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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 76 | |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 77 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 78 | * @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 Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 85 | * @throw runtime_error when an error occurred in the underlying HMAC. |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 86 | */ |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 87 | void |
Zhiyi Zhang | 5c05945 | 2020-10-16 17:43:31 -0700 | [diff] [blame] | 88 | hmac_sha256(const uint8_t* data, size_t data_length, |
| 89 | const uint8_t* key, size_t key_length, |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 90 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 93 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 105 | */ |
| 106 | int |
| 107 | aes_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 Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 111 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 123 | */ |
| 124 | int |
| 125 | aes_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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 127 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 128 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 129 | * @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 Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 137 | * @param counter The counter of blocks that have been encrypted by the requester/CA. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 138 | * @return Block The TLV block with @param tlv_type TLV TYPE. |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 139 | */ |
| 140 | Block |
| 141 | encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 142 | const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 145 | * @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 Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 152 | */ |
| 153 | Buffer |
| 154 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, |
| 155 | const uint8_t* associatedData, size_t associatedDataSize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 156 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 157 | } // namespace ndncert |
| 158 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 159 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 160 | #endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP |