Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2019, 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_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |
| 22 | #define NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |
| 23 | |
| 24 | #include "certificate-request.hpp" |
| 25 | #include <openssl/evp.h> |
| 26 | #include <openssl/ec.h> |
| 27 | |
| 28 | static const int INFO_LEN = 10; |
| 29 | static const uint8_t INFO[] = {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9}; |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace ndncert { |
| 33 | |
| 34 | struct ECDH_CTX{ |
| 35 | int EC_NID; |
| 36 | EVP_PKEY_CTX *ctx_params; |
| 37 | EVP_PKEY_CTX *ctx_keygen; |
| 38 | EVP_PKEY *privkey; |
| 39 | EVP_PKEY *peerkey; |
| 40 | EVP_PKEY *params; |
| 41 | uint8_t publicKey[256]; |
| 42 | int publicKeyLen; |
| 43 | uint8_t sharedSecret[256]; |
| 44 | int sharedSecretLen; |
| 45 | }; |
| 46 | |
| 47 | class ECDHState |
| 48 | { |
| 49 | public: |
| 50 | ECDHState(); |
| 51 | ~ECDHState(); |
| 52 | |
| 53 | std::string |
| 54 | getBase64PubKey(); |
| 55 | |
| 56 | uint8_t* |
| 57 | deriveSecret(const std::string& peerKeyStr); |
| 58 | //unique_ptr<ECDH_CTX_T> context; |
| 59 | unique_ptr<ECDH_CTX> context; |
| 60 | |
| 61 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 62 | uint8_t* |
| 63 | deriveSecret(const uint8_t* peerkey, int peerKeySize); |
| 64 | |
| 65 | uint8_t* |
| 66 | getRawSelfPubKey(); |
| 67 | }; |
| 68 | |
| 69 | int |
| 70 | hkdf(const uint8_t* secret, int secretLen, const uint8_t* salt, |
| 71 | int saltLen, uint8_t* okm, int okm_len, |
| 72 | const uint8_t* info=INFO, int info_len=INFO_LEN); |
| 73 | |
| 74 | int ndn_compute_hmac_sha256 (const uint8_t *data, const unsigned data_length, |
| 75 | const uint8_t *key, const unsigned key_length, |
| 76 | uint8_t *prk); |
| 77 | |
| 78 | void |
| 79 | handleErrors(const std::string& errorInfo); |
| 80 | |
| 81 | class CryptoError : public std::runtime_error |
| 82 | { |
| 83 | public: |
| 84 | using std::runtime_error::runtime_error; |
| 85 | }; |
| 86 | |
| 87 | } // namespace ndncert |
| 88 | } // namespace ndn |
| 89 | |
| 90 | #endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP |