blob: 6b5f277d1862cd3912385623cb18bc99a371f3d3 [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhanga67fa462020-04-19 13:48:03 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07004 *
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 Zhang926b1852020-10-10 10:16:00 -070024#include "../ndncert-common.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#include <openssl/ec.h>
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070026#include <openssl/evp.h>
27
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028namespace ndn {
29namespace ndncert {
30
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070031static const int INFO_LEN = 10;
32static const uint8_t INFO[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
tylerliu8e170d62020-09-30 01:31:53 -070033static const int AES_128_KEY_LEN = 16;
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070034
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070035struct ECDH_CTX {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036 int EC_NID;
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070037 EVP_PKEY_CTX* ctx_params;
38 EVP_PKEY_CTX* ctx_keygen;
39 EVP_PKEY* privkey;
40 EVP_PKEY* peerkey;
41 EVP_PKEY* params;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042 uint8_t publicKey[256];
43 int publicKeyLen;
44 uint8_t sharedSecret[256];
45 int sharedSecretLen;
46};
47
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070048class ECDHState {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049public:
50 ECDHState();
51 ~ECDHState();
52
53 std::string
54 getBase64PubKey();
55
56 uint8_t*
57 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 unique_ptr<ECDH_CTX> context;
59
Zhiyi Zhang926b1852020-10-10 10:16:00 -070060 PUBLIC_WITH_TESTS_ELSE_PRIVATE :
61 uint8_t*
62 deriveSecret(const uint8_t* peerkey, int peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063
64 uint8_t*
65 getRawSelfPubKey();
66};
67
68int
69hkdf(const uint8_t* secret, int secretLen, const uint8_t* salt,
70 int saltLen, uint8_t* okm, int okm_len,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070071 const uint8_t* info = INFO, int info_len = INFO_LEN);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070073/**
74 * HMAC SHA 256 keyed hash function
75 * @param key the key for the function
76 * @param key_len the length of the key
77 * @param data the array to hmac
78 * @param data_length the length of the array
79 * @param result result. Enough memory (32 Bytes) must be allocated beforehand
80 * @return 0 if successful, -1 if failed
81 */
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070082int
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070083ndn_compute_hmac_sha256(const uint8_t* data, const unsigned data_length,
84 const uint8_t* key, const unsigned key_length,
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070085 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070086
87/**
88 * Authentication GCM 128 Encryption
89 * @p plaintext, input, plaintext
90 * @p plaintext_len, input, size of plaintext
91 * @p associated, input, associated authentication data
92 * @p associated_len, input, size of associated authentication data
93 * @p key, input, 16 bytes AES key
94 * @p iv, input, 12 bytes IV
95 * @p ciphertext, output
96 * @p tag, output, 16 bytes tag
97 * @return the size of ciphertext
98 * @throw CryptoError when there is an error in the process of encryption
99 */
100int
101aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
102 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
103
104/**
105 * Authentication GCM 128 Decryption
106 * @p ciphertext, input, ciphertext
107 * @p ciphertext_len, input, size of ciphertext
108 * @p associated, input, associated authentication data
109 * @p associated_len, input, size of associated authentication data
110 * @p tag, input, 16 bytes tag
111 * @p key, input, 16 bytes AES key
112 * @p iv, input, 12 bytes IV
113 * @p plaintext, output
114 * @return the size of plaintext or -1 if the verification fails
115 * @throw CryptoError when there is an error in the process of encryption
116 */
117int
118aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
119 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120
tylerliud2cbd6b2020-10-05 23:16:31 -0700121/**
122 * HMAC SHA 256 keyed hash function
123 * @param key the key for the function
124 * @param key_len the length of the key
125 * @param cleartext the cleartext array to be hashed
126 * @param cleartext_len the length of the array
127 * @param output the output array
128 * @param output_len the longest output len possible (changed to actual on return).
129 * @return 0 if successful, -1 if failed
130 */
131int
132hmac_sha_256(const uint8_t* key, size_t key_len,
133 const uint8_t* cleartext, size_t cleartext_len,
134 uint8_t* output, size_t* output_len);
135
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136void
137handleErrors(const std::string& errorInfo);
138
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700139class CryptoError : public std::runtime_error {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700140public:
141 using std::runtime_error::runtime_error;
142};
143
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700144} // namespace ndncert
145} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700146
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700147#endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP