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 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 24 | #include "ca-state.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 | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 35 | struct ECDH_CTX { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | int EC_NID; |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 37 | EVP_PKEY_CTX* ctx_params; |
| 38 | EVP_PKEY_CTX* ctx_keygen; |
| 39 | EVP_PKEY* privkey; |
| 40 | EVP_PKEY* peerkey; |
| 41 | EVP_PKEY* params; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | uint8_t publicKey[256]; |
| 43 | int publicKeyLen; |
| 44 | uint8_t sharedSecret[256]; |
| 45 | int sharedSecretLen; |
| 46 | }; |
| 47 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 48 | class ECDHState { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | public: |
| 50 | ECDHState(); |
| 51 | ~ECDHState(); |
| 52 | |
| 53 | std::string |
| 54 | getBase64PubKey(); |
| 55 | |
| 56 | uint8_t* |
| 57 | deriveSecret(const std::string& peerKeyStr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | unique_ptr<ECDH_CTX> context; |
| 59 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 60 | PUBLIC_WITH_TESTS_ELSE_PRIVATE : uint8_t* |
| 61 | deriveSecret(const uint8_t* peerkey, int peerKeySize); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | |
| 63 | uint8_t* |
| 64 | getRawSelfPubKey(); |
| 65 | }; |
| 66 | |
| 67 | int |
| 68 | hkdf(const uint8_t* secret, int secretLen, const uint8_t* salt, |
| 69 | int saltLen, uint8_t* okm, int okm_len, |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 70 | const uint8_t* info = INFO, int info_len = INFO_LEN); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 71 | |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame^] | 72 | /** |
| 73 | * HMAC SHA 256 keyed hash function |
| 74 | * @param key the key for the function |
| 75 | * @param key_len the length of the key |
| 76 | * @param data the array to hmac |
| 77 | * @param data_length the length of the array |
| 78 | * @param result result. Enough memory (32 Bytes) must be allocated beforehand |
| 79 | * @return 0 if successful, -1 if failed |
| 80 | */ |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 81 | int |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 82 | ndn_compute_hmac_sha256(const uint8_t* data, const unsigned data_length, |
| 83 | const uint8_t* key, const unsigned key_length, |
Zhiyi Zhang | a2c39f7 | 2020-10-06 16:45:55 -0700 | [diff] [blame^] | 84 | uint8_t* result); |
Zhiyi Zhang | a67fa46 | 2020-04-19 13:48:03 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Authentication GCM 128 Encryption |
| 88 | * @p plaintext, input, plaintext |
| 89 | * @p plaintext_len, input, size of plaintext |
| 90 | * @p associated, input, associated authentication data |
| 91 | * @p associated_len, input, size of associated authentication data |
| 92 | * @p key, input, 16 bytes AES key |
| 93 | * @p iv, input, 12 bytes IV |
| 94 | * @p ciphertext, output |
| 95 | * @p tag, output, 16 bytes tag |
| 96 | * @return the size of ciphertext |
| 97 | * @throw CryptoError when there is an error in the process of encryption |
| 98 | */ |
| 99 | int |
| 100 | aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len, |
| 101 | const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag); |
| 102 | |
| 103 | /** |
| 104 | * Authentication GCM 128 Decryption |
| 105 | * @p ciphertext, input, ciphertext |
| 106 | * @p ciphertext_len, input, size of ciphertext |
| 107 | * @p associated, input, associated authentication data |
| 108 | * @p associated_len, input, size of associated authentication data |
| 109 | * @p tag, input, 16 bytes tag |
| 110 | * @p key, input, 16 bytes AES key |
| 111 | * @p iv, input, 12 bytes IV |
| 112 | * @p plaintext, output |
| 113 | * @return the size of plaintext or -1 if the verification fails |
| 114 | * @throw CryptoError when there is an error in the process of encryption |
| 115 | */ |
| 116 | int |
| 117 | aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len, |
| 118 | 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] | 119 | |
tylerliu | d2cbd6b | 2020-10-05 23:16:31 -0700 | [diff] [blame] | 120 | /** |
| 121 | * HMAC SHA 256 keyed hash function |
| 122 | * @param key the key for the function |
| 123 | * @param key_len the length of the key |
| 124 | * @param cleartext the cleartext array to be hashed |
| 125 | * @param cleartext_len the length of the array |
| 126 | * @param output the output array |
| 127 | * @param output_len the longest output len possible (changed to actual on return). |
| 128 | * @return 0 if successful, -1 if failed |
| 129 | */ |
| 130 | int |
| 131 | hmac_sha_256(const uint8_t* key, size_t key_len, |
| 132 | const uint8_t* cleartext, size_t cleartext_len, |
| 133 | uint8_t* output, size_t* output_len); |
| 134 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | void |
| 136 | handleErrors(const std::string& errorInfo); |
| 137 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 138 | class CryptoError : public std::runtime_error { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 139 | public: |
| 140 | using std::runtime_error::runtime_error; |
| 141 | }; |
| 142 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 143 | } // namespace ndncert |
| 144 | } // namespace ndn |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 145 | |
Zhiyi Zhang | b8cb047 | 2020-05-05 20:55:05 -0700 | [diff] [blame] | 146 | #endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |