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