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 | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 21 | #ifndef NDNCERT_DETAIL_CRYPTO_HELPER_HPP |
| 22 | #define NDNCERT_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 | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 29 | /** |
| 30 | * @brief State for ECDH. |
| 31 | * |
| 32 | * The ECDH is based on prime256v1. |
| 33 | */ |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 34 | class ECDHState |
| 35 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | public: |
| 37 | ECDHState(); |
| 38 | ~ECDHState(); |
| 39 | |
| 40 | std::string |
| 41 | getBase64PubKey(); |
| 42 | |
| 43 | uint8_t* |
| 44 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -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 | |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 53 | public: |
| 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 Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 59 | private: |
| 60 | struct ECDH_CTX; |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 61 | unique_ptr<ECDH_CTX> m_context; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 64 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 65 | * @brief HMAC based key derivation function (HKDF). |
| 66 | * |
| 67 | * @param secret The input to the HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 68 | * @param secretLen The length of the secret. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 69 | * @param salt The salt used in HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 70 | * @param saltLen The length of the salt. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 71 | * @param output The output of the HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 72 | * @param outputLen The length of expected output. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 73 | * @param info The additional information used in HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 74 | * @param infoLen The length of the additional information. |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 75 | * @return size_t The length of the derived key if successful. |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 76 | */ |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 77 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 78 | hkdf(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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 82 | |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 83 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 84 | * @brief HMAC based on SHA-256. |
| 85 | * |
| 86 | * @param data The intput array to hmac. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 87 | * @param dataLen The length of the input array. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 88 | * @param key The HMAC key. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 89 | * @param keyLen The length of the HMAC key. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 90 | * @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] | 91 | * @throw runtime_error when an error occurred in the underlying HMAC. |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 92 | */ |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 93 | void |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 94 | hmacSha256(const uint8_t* data, size_t dataLen, |
| 95 | const uint8_t* key, size_t keyLen, |
| 96 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 99 | * @brief Authenticated GCM 128 Encryption with associated data. |
| 100 | * |
| 101 | * @param plaintext The plaintext. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 102 | * @param plaintextLen The size of plaintext. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 103 | * @param associated The associated authentication data. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 104 | * @param associatedLen The size of associated authentication data. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 105 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 111 | */ |
| 112 | int |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 113 | aesGcm128Encrypt(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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 117 | * @brief Authenticated GCM 128 Decryption with associated data. |
| 118 | * |
| 119 | * @param ciphertext The ciphertext. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 120 | * @param ciphertextLen The size of ciphertext. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 121 | * @param associated The associated authentication data. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 122 | * @param associatedLen The size of associated authentication data. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 123 | * @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 Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 129 | */ |
| 130 | int |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame^] | 131 | aesGcm128Decrypt(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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 133 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 134 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 135 | * @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 Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 143 | * @param counter An opaque counter that must be passed to subsequent invocations of this function |
| 144 | * with the same @param key. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 145 | * @return Block The TLV block with @param tlv_type TLV TYPE. |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 146 | */ |
| 147 | Block |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 148 | encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 149 | const uint8_t* associatedData, size_t associatedDataSize, uint32_t& counter); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 152 | * @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 Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 159 | */ |
| 160 | Buffer |
| 161 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, |
| 162 | const uint8_t* associatedData, size_t associatedDataSize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 163 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 164 | } // namespace ndncert |
| 165 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 166 | |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 167 | #endif // NDNCERT_DETAIL_CRYPTO_HELPER_HPP |