blob: 43819771a2cc5883b7656c0be7b38c7858e540bf [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 Zhangaf7c2902019-03-14 22:13:21 -070024#include <openssl/ec.h>
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070025#include <openssl/evp.h>
26
27#include "certificate-request.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028
29static const int INFO_LEN = 10;
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070030static const uint8_t INFO[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070031
32namespace ndn {
33namespace ndncert {
34
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 Zhangb8cb0472020-05-05 20:55:05 -070060 PUBLIC_WITH_TESTS_ELSE_PRIVATE : uint8_t*
61 deriveSecret(const uint8_t* peerkey, int peerKeySize);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062
63 uint8_t*
64 getRawSelfPubKey();
65};
66
67int
68hkdf(const uint8_t* secret, int secretLen, const uint8_t* salt,
69 int saltLen, uint8_t* okm, int okm_len,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070070 const uint8_t* info = INFO, int info_len = INFO_LEN);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070072int
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070073ndn_compute_hmac_sha256(const uint8_t* data, const unsigned data_length,
74 const uint8_t* key, const unsigned key_length,
75 uint8_t* prk);
Zhiyi Zhanga67fa462020-04-19 13:48:03 -070076
77/**
78 * Authentication GCM 128 Encryption
79 * @p plaintext, input, plaintext
80 * @p plaintext_len, input, size of plaintext
81 * @p associated, input, associated authentication data
82 * @p associated_len, input, size of associated authentication data
83 * @p key, input, 16 bytes AES key
84 * @p iv, input, 12 bytes IV
85 * @p ciphertext, output
86 * @p tag, output, 16 bytes tag
87 * @return the size of ciphertext
88 * @throw CryptoError when there is an error in the process of encryption
89 */
90int
91aes_gcm_128_encrypt(const uint8_t* plaintext, size_t plaintext_len, const uint8_t* associated, size_t associated_len,
92 const uint8_t* key, const uint8_t* iv, uint8_t* ciphertext, uint8_t* tag);
93
94/**
95 * Authentication GCM 128 Decryption
96 * @p ciphertext, input, ciphertext
97 * @p ciphertext_len, input, size of ciphertext
98 * @p associated, input, associated authentication data
99 * @p associated_len, input, size of associated authentication data
100 * @p tag, input, 16 bytes tag
101 * @p key, input, 16 bytes AES key
102 * @p iv, input, 12 bytes IV
103 * @p plaintext, output
104 * @return the size of plaintext or -1 if the verification fails
105 * @throw CryptoError when there is an error in the process of encryption
106 */
107int
108aes_gcm_128_decrypt(const uint8_t* ciphertext, size_t ciphertext_len, const uint8_t* associated, size_t associated_len,
109 const uint8_t* tag, const uint8_t* key, const uint8_t* iv, uint8_t* plaintext);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110
111void
112handleErrors(const std::string& errorInfo);
113
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700114class CryptoError : public std::runtime_error {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115public:
116 using std::runtime_error::runtime_error;
117};
118
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700119} // namespace ndncert
120} // namespace ndn
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700122#endif // NDNCERT_CRYPTO_SUPPORT_CRYPTO_HELPER_HPP