Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a34d6de | 2021-11-18 22:16:04 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2021, 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 | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 21 | #ifndef NDNCERT_DETAIL_CRYPTO_HELPERS_HPP |
| 22 | #define NDNCERT_DETAIL_CRYPTO_HELPERS_HPP |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 23 | |
Zhiyi Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 24 | #include "detail/ndncert-common.hpp" |
Davide Pesavento | a34d6de | 2021-11-18 22:16:04 -0500 | [diff] [blame] | 25 | |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 26 | #include <openssl/evp.h> |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 27 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 28 | namespace ndncert { |
| 29 | |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 30 | /** |
| 31 | * @brief State for ECDH. |
| 32 | * |
| 33 | * The ECDH is based on prime256v1. |
| 34 | */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame^] | 35 | class ECDHState : boost::noncopyable |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 36 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 37 | public: |
| 38 | ECDHState(); |
Davide Pesavento | a34d6de | 2021-11-18 22:16:04 -0500 | [diff] [blame] | 39 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 40 | ~ECDHState(); |
| 41 | |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 42 | /** |
| 43 | * @brief Derive ECDH secret from peer's EC public key and self's private key. |
| 44 | * |
| 45 | * @param peerkey Peer's EC public key in the uncompressed octet string format. |
| 46 | * See details in https://www.openssl.org/docs/man1.1.1/man3/EC_POINT_point2oct.html. |
| 47 | * @return const std::vector<uint8_t>& the derived secret. |
| 48 | */ |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 49 | const std::vector<uint8_t>& |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 50 | deriveSecret(const std::vector<uint8_t>& peerkey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 51 | |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 52 | /** |
| 53 | * @brief Get the Self Pub Key object |
| 54 | * |
| 55 | * @return const std::vector<uint8_t>& the Self public key in the uncompressed oct string format. |
| 56 | * See details in https://www.openssl.org/docs/man1.1.1/man3/EC_POINT_point2oct.html. |
| 57 | */ |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 58 | const std::vector<uint8_t>& |
| 59 | getSelfPubKey(); |
Zhiyi Zhang | dc25ddf | 2020-10-20 14:28:55 -0700 | [diff] [blame] | 60 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 61 | private: |
Zhiyi Zhang | bed854c | 2020-10-20 18:25:35 -0700 | [diff] [blame] | 62 | EVP_PKEY* m_privkey = nullptr; |
| 63 | std::vector<uint8_t> m_pubKey; |
| 64 | std::vector<uint8_t> m_secret; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 67 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 68 | * @brief HMAC based key derivation function (HKDF). |
| 69 | * |
| 70 | * @param secret The input to the HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 71 | * @param secretLen The length of the secret. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 72 | * @param salt The salt used in HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 73 | * @param saltLen The length of the salt. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 74 | * @param output The output of the HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 75 | * @param outputLen The length of expected output. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 76 | * @param info The additional information used in HKDF. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 77 | * @param infoLen The length of the additional information. |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 78 | * @return size_t The length of the derived key if successful. |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 79 | */ |
Zhiyi Zhang | 159ba63 | 2020-10-20 15:09:12 -0700 | [diff] [blame] | 80 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 81 | hkdf(const uint8_t* secret, size_t secretLen, |
| 82 | const uint8_t* salt, size_t saltLen, |
| 83 | uint8_t* output, size_t outputLen, |
| 84 | const uint8_t* info = nullptr, size_t infoLen = 0); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 86 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 87 | * @brief HMAC based on SHA-256. |
| 88 | * |
| 89 | * @param data The intput array to hmac. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 90 | * @param dataLen The length of the input array. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 91 | * @param key The HMAC key. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 92 | * @param keyLen The length of the HMAC key. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 93 | * @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] | 94 | * @throw runtime_error when an error occurred in the underlying HMAC. |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 95 | */ |
Zhiyi Zhang | f12ee30 | 2020-10-14 17:46:44 -0700 | [diff] [blame] | 96 | void |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 97 | hmacSha256(const uint8_t* data, size_t dataLen, |
| 98 | const uint8_t* key, size_t keyLen, |
| 99 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 102 | * @brief Authenticated GCM 128 Encryption with associated data. |
| 103 | * |
| 104 | * @param plaintext The plaintext. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 105 | * @param plaintextLen The size of plaintext. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 106 | * @param associated The associated authentication data. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 107 | * @param associatedLen The size of associated authentication data. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 108 | * @param key 16 bytes AES key. |
| 109 | * @param iv 12 bytes IV. |
| 110 | * @param ciphertext The output and enough memory must be allocated beforehands. |
| 111 | * @param tag 16 bytes tag. |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 112 | * @return size_t The size of ciphertext. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 113 | * @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] | 114 | */ |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 115 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 116 | aesGcm128Encrypt(const uint8_t* plaintext, size_t plaintextLen, const uint8_t* associated, size_t associatedLen, |
| 117 | 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] | 118 | |
| 119 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 120 | * @brief Authenticated GCM 128 Decryption with associated data. |
| 121 | * |
| 122 | * @param ciphertext The ciphertext. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 123 | * @param ciphertextLen The size of ciphertext. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 124 | * @param associated The associated authentication data. |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 125 | * @param associatedLen The size of associated authentication data. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 126 | * @param tag 16 bytes tag. |
| 127 | * @param key 16 bytes AES key. |
| 128 | * @param iv 12 bytes IV. |
| 129 | * @param plaintext The output and enough memory must be allocated beforehands. |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 130 | * @return size_t The size of plaintext. |
| 131 | * @throw runtime_error When there is an error in the process of decryption. |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 132 | */ |
Zhiyi Zhang | e5a07c9 | 2020-10-21 10:51:37 -0700 | [diff] [blame] | 133 | size_t |
Zhiyi Zhang | 11cf7eb | 2020-10-20 15:43:36 -0700 | [diff] [blame] | 134 | aesGcm128Decrypt(const uint8_t* ciphertext, size_t ciphertextLen, const uint8_t* associated, size_t associatedLen, |
| 135 | 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] | 136 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 137 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 138 | * @brief Encode the payload into TLV block with Authenticated GCM 128 Encryption. |
| 139 | * |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 140 | * The TLV spec: https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption. |
| 141 | * |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 142 | * @param tlv_type The TLV TYPE of the encoded block, either ApplicationParameters or Content. |
| 143 | * @param key The AES key used for encryption. |
| 144 | * @param payload The plaintext payload. |
| 145 | * @param payloadSize The size of the plaintext payload. |
| 146 | * @param associatedData The associated data used for authentication. |
| 147 | * @param associatedDataSize The size of associated data. |
Zhiyi Zhang | c1e9815 | 2020-12-21 16:15:39 -0800 | [diff] [blame] | 148 | * @param encryptionIv The IV used by the last AES encryption and is needed by subsequent invocations of this function |
| 149 | * with the same @p key. |
Zhiyi Zhang | 199508a | 2020-10-21 10:45:50 -0700 | [diff] [blame] | 150 | * @return Block The TLV block with @p tlv_type TLV TYPE. |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 151 | */ |
| 152 | Block |
Zhiyi Zhang | 4f1c010 | 2020-12-21 15:08:09 -0800 | [diff] [blame] | 153 | encodeBlockWithAesGcm128(uint32_t tlvType, const uint8_t* key, |
| 154 | const uint8_t* payload, size_t payloadSize, |
| 155 | const uint8_t* associatedData, size_t associatedDataSize, |
Zhiyi Zhang | c1e9815 | 2020-12-21 16:15:39 -0800 | [diff] [blame] | 156 | std::vector<uint8_t>& encryptionIv); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 159 | * @brief Decode the payload from TLV block with Authenticated GCM 128 Encryption. |
| 160 | * |
Zhiyi Zhang | 5e74ecd | 2020-10-21 12:52:25 -0700 | [diff] [blame] | 161 | * The TLV spec: https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#242-aes-gcm-encryption. |
| 162 | * |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 163 | * @param block The TLV block in the format of NDNCERT protocol. |
| 164 | * @param key The AES key used for encryption. |
| 165 | * @param associatedData The associated data used for authentication. |
| 166 | * @param associatedDataSize The size of associated data. |
Zhiyi Zhang | 4f1c010 | 2020-12-21 15:08:09 -0800 | [diff] [blame] | 167 | * @param decryptionIv The IV observed in the last AES decryption and is needed by subsequent |
| 168 | * invocations of this function with the same @p key. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 169 | * @return Buffer The plaintext buffer. |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 170 | */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame^] | 171 | ndn::Buffer |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 172 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, |
Zhiyi Zhang | 4f1c010 | 2020-12-21 15:08:09 -0800 | [diff] [blame] | 173 | const uint8_t* associatedData, size_t associatedDataSize, |
Zhiyi Zhang | 3b9a503 | 2021-02-18 11:15:09 -0800 | [diff] [blame] | 174 | std::vector<uint8_t>& decryptionIv, const std::vector<uint8_t>& encryptionIv); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | a34d6de | 2021-11-18 22:16:04 -0500 | [diff] [blame] | 176 | #ifdef NDNCERT_HAVE_TESTS |
| 177 | uint32_t |
| 178 | loadBigU32(const uint8_t* src) noexcept; |
| 179 | |
| 180 | void |
| 181 | storeBigU32(uint8_t* dest, uint32_t src) noexcept; |
| 182 | #endif |
| 183 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 184 | } // namespace ndncert |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 185 | |
Zhiyi Zhang | def00e4 | 2020-10-20 22:41:56 -0700 | [diff] [blame] | 186 | #endif // NDNCERT_DETAIL_CRYPTO_HELPERS_HPP |