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}; |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 31 | static const int AES_128_KEY_LEN = 16; |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 32 | |
Zhiyi Zhang | 6312317 | 2020-10-12 14:24:44 -0700 | [diff] [blame] | 33 | class ECDHState |
| 34 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 35 | public: |
| 36 | ECDHState(); |
| 37 | ~ECDHState(); |
| 38 | |
| 39 | std::string |
| 40 | getBase64PubKey(); |
| 41 | |
| 42 | uint8_t* |
| 43 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 44 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 45 | uint8_t m_publicKey[256]; |
| 46 | size_t m_publicKeyLen; |
| 47 | uint8_t m_sharedSecret[256]; |
| 48 | size_t m_sharedSecretLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 50 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 51 | uint8_t* |
| 52 | deriveSecret(const uint8_t* peerkey, int peerKeySize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 53 | |
| 54 | uint8_t* |
| 55 | getRawSelfPubKey(); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | struct ECDH_CTX; |
| 59 | unique_ptr<ECDH_CTX> context; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 62 | /** |
| 63 | * HMAC based key derivation function (HKDF) |
| 64 | * @p secret, intput, the input to the HKDF |
| 65 | * @p secretLen, intput, the length of the secret |
| 66 | * @p salt, intput, the salt used in HKDF |
| 67 | * @p saltLen, intput, the length of the salt |
| 68 | * @p output, output, the output of the HKDF |
| 69 | * @p output_len, intput, the length of expected output |
| 70 | * @p info, intput, the additional information used in HKDF |
| 71 | * @p info_len, intput, the additional information used in HKDF |
| 72 | * @return the length of the derived key if successful, -1 if failed |
| 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 | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 81 | * HMAC based on SHA-256 |
| 82 | * @p data, intput, the array to hmac |
| 83 | * @p data_length, intput, the length of the array |
| 84 | * @p key, intput, the key for the function |
| 85 | * @p key_len, intput, the length of the key |
| 86 | * @p result, output, result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame] | 87 | * @return 0 if successful, -1 if failed |
| 88 | */ |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 89 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 90 | hmac_sha256(const uint8_t* data, const unsigned data_length, |
| 91 | const uint8_t* key, const unsigned key_length, |
| 92 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 95 | * Authenticated GCM 128 Encryption with associated data |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 96 | * @p plaintext, input, plaintext |
| 97 | * @p plaintext_len, input, size of plaintext |
| 98 | * @p associated, input, associated authentication data |
| 99 | * @p associated_len, input, size of associated authentication data |
| 100 | * @p key, input, 16 bytes AES key |
| 101 | * @p iv, input, 12 bytes IV |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 102 | * @p ciphertext, output, enough memory must be allocated beforehands |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 103 | * @p tag, output, 16 bytes tag |
| 104 | * @return the size of ciphertext |
Zhiyi Zhang | ccd5a0c | 2020-10-12 13:48:16 -0700 | [diff] [blame] | 105 | * @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] | 106 | */ |
| 107 | int |
| 108 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 109 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag); |
| 110 | |
| 111 | /** |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 112 | * Authenticated GCM 128 Decryption with associated data |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 113 | * @p ciphertext, input, ciphertext |
| 114 | * @p ciphertext_len, input, size of ciphertext |
| 115 | * @p associated, input, associated authentication data |
| 116 | * @p associated_len, input, size of associated authentication data |
| 117 | * @p tag, input, 16 bytes tag |
| 118 | * @p key, input, 16 bytes AES key |
| 119 | * @p iv, input, 12 bytes IV |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 120 | * @p plaintext, output, enough memory must be allocated beforehands |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 121 | * @return the size of plaintext or -1 if the verification fails |
Zhiyi Zhang | ccd5a0c | 2020-10-12 13:48:16 -0700 | [diff] [blame] | 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 | /** |
| 129 | * Encode the payload into TLV block with Authenticated GCM 128 Encryption |
| 130 | * @p tlv::type, intput, the TLV TYPE of the encoded block, either ApplicationParameters or Content |
| 131 | * @p key, intput, 16 Bytes, the AES key used for encryption |
| 132 | * @p payload, input, the plaintext payload |
| 133 | * @p payloadSize, input, the size of the plaintext payload |
| 134 | * @p associatedData, input, associated data used for authentication |
| 135 | * @p associatedDataSize, input, the size of associated data |
| 136 | * @return the TLV block with @p tlv::type TLV TYPE |
| 137 | */ |
| 138 | Block |
| 139 | encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize, |
| 140 | const uint8_t* associatedData, size_t associatedDataSize); |
| 141 | |
| 142 | /** |
| 143 | * Decode the payload from TLV block with Authenticated GCM 128 Encryption |
| 144 | * @p block, intput, the TLV block in the format of NDNCERT protocol |
| 145 | * @p key, intput, 16 Bytes, the AES key used for encryption |
| 146 | * @p associatedData, input, associated data used for authentication |
| 147 | * @p associatedDataSize, input, the size of associated data |
| 148 | * @return the plaintext buffer |
| 149 | */ |
| 150 | Buffer |
| 151 | decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, |
| 152 | const uint8_t* associatedData, size_t associatedDataSize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 153 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 154 | } // namespace ndncert |
| 155 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 156 | |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame^] | 157 | #endif // NDNCERT_PROTOCOL_DETAIL_CRYPTO_HELPER_HPP |