blob: 8df3fa0266c6ac9d15cca6b7c5fee29c4ccebe38 [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 Zhangb8cb0472020-05-05 20:55:05 -070025
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070026namespace ndn {
27namespace ndncert {
28
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070029static const int INFO_LEN = 10;
30static const uint8_t INFO[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
tylerliu8e170d62020-09-30 01:31:53 -070031static const int AES_128_KEY_LEN = 16;
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070032
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070033class CryptoError : public std::runtime_error {
34public:
35 using std::runtime_error::runtime_error;
36};
37
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070038class ECDHState {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070039public:
40 ECDHState();
41 ~ECDHState();
42
43 std::string
44 getBase64PubKey();
45
46 uint8_t*
47 deriveSecret(const std::string& peerKeyStr);
Zhiyi Zhange4891b72020-10-10 15:11:57 -070048
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070049 uint8_t m_publicKey[256];
50 size_t m_publicKeyLen;
51 uint8_t m_sharedSecret[256];
52 size_t m_sharedSecretLen;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053
Zhiyi Zhange4891b72020-10-10 15:11:57 -070054PUBLIC_WITH_TESTS_ELSE_PRIVATE:
55 uint8_t*
56 deriveSecret(const uint8_t* peerkey, int peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057
58 uint8_t*
59 getRawSelfPubKey();
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -070060
61private:
62 struct ECDH_CTX;
63 unique_ptr<ECDH_CTX> context;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064};
65
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070066/**
67 * HMAC based key derivation function (HKDF)
68 * @p secret, intput, the input to the HKDF
69 * @p secretLen, intput, the length of the secret
70 * @p salt, intput, the salt used in HKDF
71 * @p saltLen, intput, the length of the salt
72 * @p output, output, the output of the HKDF
73 * @p output_len, intput, the length of expected output
74 * @p info, intput, the additional information used in HKDF
75 * @p info_len, intput, the additional information used in HKDF
76 * @return the length of the derived key if successful, -1 if failed
77 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078int
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070079hkdf(const uint8_t* secret, int secret_len,
80 const uint8_t* salt, int salt_len,
81 uint8_t* output, int output_len,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070082 const uint8_t* info = INFO, int info_len = INFO_LEN);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070084/**
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070085 * HMAC based on SHA-256
86 * @p data, intput, the array to hmac
87 * @p data_length, intput, the length of the array
88 * @p key, intput, the key for the function
89 * @p key_len, intput, the length of the key
90 * @p result, output, result of the HMAC. Enough memory (32 Bytes) must be allocated beforehands
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070091 * @return 0 if successful, -1 if failed
92 */
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070093int
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070094hmac_sha256(const uint8_t* data, const unsigned data_length,
95 const uint8_t* key, const unsigned key_length,
96 uint8_t* result);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070097
98/**
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070099 * Authenticated GCM 128 Encryption with associated data
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700100 * @p plaintext, input, plaintext
101 * @p plaintext_len, input, size of plaintext
102 * @p associated, input, associated authentication data
103 * @p associated_len, input, size of associated authentication data
104 * @p key, input, 16 bytes AES key
105 * @p iv, input, 12 bytes IV
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700106 * @p ciphertext, output, enough memory must be allocated beforehands
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700107 * @p tag, output, 16 bytes tag
108 * @return the size of ciphertext
109 * @throw CryptoError when there is an error in the process of encryption
110 */
111int
112aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
113 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
114
115/**
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700116 * Authenticated GCM 128 Decryption with associated data
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700117 * @p ciphertext, input, ciphertext
118 * @p ciphertext_len, input, size of ciphertext
119 * @p associated, input, associated authentication data
120 * @p associated_len, input, size of associated authentication data
121 * @p tag, input, 16 bytes tag
122 * @p key, input, 16 bytes AES key
123 * @p iv, input, 12 bytes IV
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700124 * @p plaintext, output, enough memory must be allocated beforehands
Zhiyi Zhanga67fa462020-04-19 13:48:03 -0700125 * @return the size of plaintext or -1 if the verification fails
126 * @throw CryptoError when there is an error in the process of encryption
127 */
128int
129aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
130 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700131
132void
133handleErrors(const std::string& errorInfo);
134
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700135} // namespace ndncert
136} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700138#endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP