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 | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 29 | static const int INFO_LEN = 10; |
| 30 | static const uint8_t INFO[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9}; |
| 31 | |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 32 | class ECDHState |
| 33 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 34 | public: |
| 35 | ECDHState(); |
| 36 | ~ECDHState(); |
| 37 | |
| 38 | std::string |
| 39 | getBase64PubKey(); |
| 40 | |
| 41 | uint8_t* |
| 42 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 43 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 44 | uint8_t m_publicKey[256]; |
| 45 | size_t m_publicKeyLen; |
| 46 | uint8_t m_sharedSecret[256]; |
| 47 | size_t m_sharedSecretLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 49 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 50 | uint8_t* |
| 51 | deriveSecret(const uint8_t* peerkey, int peerKeySize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | |
| 53 | uint8_t* |
| 54 | getRawSelfPubKey(); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | struct ECDH_CTX; |
| 58 | unique_ptr<ECDH_CTX> context; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 61 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 62 | * @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 Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 73 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 74 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 75 | hkdf(const uint8_t* secret, int secret_len, |
| 76 | const uint8_t* salt, int salt_len, |
| 77 | uint8_t* output, int output_len, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 78 | const uint8_t* info = INFO, int info_len = INFO_LEN); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 80 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 81 | * @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 Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 88 | * @throw runtime_error when an error occurred in the underlying HMAC. |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 89 | */ |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 90 | void |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 91 | hmac_sha256(const uint8_t* data, const unsigned data_length, |
| 92 | const uint8_t* key, const unsigned key_length, |
| 93 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 96 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 108 | */ |
| 109 | int |
| 110 | aes_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 Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 114 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 126 | */ |
| 127 | int |
| 128 | aes_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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 130 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 131 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 132 | * @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 Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 141 | */ |
| 142 | Block |
| 143 | encodeBlockWithAesGcm128(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 Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame^] | 147 | * @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 Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 154 | */ |
| 155 | Buffer |
| 156 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, |
| 157 | const uint8_t* associatedData, size_t associatedDataSize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 158 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 159 | } // namespace ndncert |
| 160 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 162 | #endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP |