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 | |
| 21 | #ifndef NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |
| 22 | #define NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |
| 23 | |
Zhiyi Zhang | 926b185 | 2020-10-10 10:16:00 -0700 | [diff] [blame] | 24 | #include "../ndncert-common.hpp" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 25 | #include <openssl/ec.h> |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 26 | #include <openssl/evp.h> |
| 27 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 31 | static const int INFO_LEN = 10; |
| 32 | 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] | 33 | static const int AES_128_KEY_LEN = 16; |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 34 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 35 | class CryptoError : public std::runtime_error { |
| 36 | public: |
| 37 | using std::runtime_error::runtime_error; |
| 38 | }; |
| 39 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 40 | class ECDHState { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 41 | public: |
| 42 | ECDHState(); |
| 43 | ~ECDHState(); |
| 44 | |
| 45 | std::string |
| 46 | getBase64PubKey(); |
| 47 | |
| 48 | uint8_t* |
| 49 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 50 | |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame^] | 51 | uint8_t m_publicKey[256]; |
| 52 | size_t m_publicKeyLen; |
| 53 | uint8_t m_sharedSecret[256]; |
| 54 | size_t m_sharedSecretLen; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 56 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 57 | uint8_t* |
| 58 | deriveSecret(const uint8_t* peerkey, int peerKeySize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | |
| 60 | uint8_t* |
| 61 | getRawSelfPubKey(); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame^] | 62 | |
| 63 | private: |
| 64 | struct ECDH_CTX; |
| 65 | unique_ptr<ECDH_CTX> context; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 68 | /** |
| 69 | * HMAC based key derivation function (HKDF) |
| 70 | * @p secret, intput, the input to the HKDF |
| 71 | * @p secretLen, intput, the length of the secret |
| 72 | * @p salt, intput, the salt used in HKDF |
| 73 | * @p saltLen, intput, the length of the salt |
| 74 | * @p output, output, the output of the HKDF |
| 75 | * @p output_len, intput, the length of expected output |
| 76 | * @p info, intput, the additional information used in HKDF |
| 77 | * @p info_len, intput, the additional information used in HKDF |
| 78 | * @return the length of the derived key if successful, -1 if failed |
| 79 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 80 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 81 | hkdf(const uint8_t* secret, int secret_len, |
| 82 | const uint8_t* salt, int salt_len, |
| 83 | uint8_t* output, int output_len, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 84 | const uint8_t* info = INFO, int info_len = INFO_LEN); |
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 | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 87 | * HMAC based on SHA-256 |
| 88 | * @p data, intput, the array to hmac |
| 89 | * @p data_length, intput, the length of the array |
| 90 | * @p key, intput, the key for the function |
| 91 | * @p key_len, intput, the length of the key |
| 92 | * @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] | 93 | * @return 0 if successful, -1 if failed |
| 94 | */ |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 95 | int |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 96 | hmac_sha256(const uint8_t* data, const unsigned data_length, |
| 97 | const uint8_t* key, const unsigned key_length, |
| 98 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 101 | * Authenticated GCM 128 Encryption with associated data |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 102 | * @p plaintext, input, plaintext |
| 103 | * @p plaintext_len, input, size of plaintext |
| 104 | * @p associated, input, associated authentication data |
| 105 | * @p associated_len, input, size of associated authentication data |
| 106 | * @p key, input, 16 bytes AES key |
| 107 | * @p iv, input, 12 bytes IV |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 108 | * @p ciphertext, output, enough memory must be allocated beforehands |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 109 | * @p tag, output, 16 bytes tag |
| 110 | * @return the size of ciphertext |
| 111 | * @throw CryptoError when there is an error in the process of encryption |
| 112 | */ |
| 113 | int |
| 114 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 115 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag); |
| 116 | |
| 117 | /** |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 118 | * Authenticated GCM 128 Decryption with associated data |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 119 | * @p ciphertext, input, ciphertext |
| 120 | * @p ciphertext_len, input, size of ciphertext |
| 121 | * @p associated, input, associated authentication data |
| 122 | * @p associated_len, input, size of associated authentication data |
| 123 | * @p tag, input, 16 bytes tag |
| 124 | * @p key, input, 16 bytes AES key |
| 125 | * @p iv, input, 12 bytes IV |
Zhiyi Zhang | 97bedb8 | 2020-10-10 11:11:35 -0700 | [diff] [blame] | 126 | * @p plaintext, output, enough memory must be allocated beforehands |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 127 | * @return the size of plaintext or -1 if the verification fails |
| 128 | * @throw CryptoError when there is an error in the process of encryption |
| 129 | */ |
| 130 | int |
| 131 | aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len, |
| 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 | |
| 134 | void |
| 135 | handleErrors(const std::string& errorInfo); |
| 136 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 137 | } // namespace ndncert |
| 138 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 139 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 140 | #endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |