Skip to content

Commit 1e68917

Browse files
authored
BoringSSL (#252)
Signed-off-by: turuslan <[email protected]>
1 parent cbed516 commit 1e68917

20 files changed

Lines changed: 157 additions & 256 deletions

File tree

cmake/Hunter/init.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set(
3131
include(${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake)
3232

3333
HunterGate(
34-
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.25.3-qdrvm12.zip
35-
SHA1 9d4b9844b84d3dfbf4a90923eedb3875718abf54
34+
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.25.3-qdrvm15.zip
35+
SHA1 b338eb3b6a989f19257d6d4acbc9f810abcf1b32
3636
LOCAL
3737
)

cmake/dependencies.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ hunter_add_package(Boost COMPONENTS random filesystem program_options)
1515
find_package(Boost CONFIG REQUIRED random filesystem program_options)
1616

1717
# https://www.openssl.org/
18-
hunter_add_package(OpenSSL)
19-
find_package(OpenSSL REQUIRED)
18+
hunter_add_package(BoringSSL)
19+
find_package(OpenSSL CONFIG REQUIRED)
20+
21+
hunter_add_package(libsecp256k1)
22+
find_package(libsecp256k1 CONFIG REQUIRED)
2023

2124
# https://developers.google.com/protocol-buffers/
2225
hunter_add_package(Protobuf)

cmake/libp2pConfig.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include(CMakeFindDependencyMacro)
44

55
find_dependency(Boost CONFIG REQUIRED random filesystem program_options)
6-
find_dependency(OpenSSL REQUIRED)
6+
find_dependency(OpenSSL CONFIG REQUIRED)
77
find_dependency(Protobuf CONFIG REQUIRED)
88
find_dependency(Threads)
99
find_dependency(c-ares CONFIG REQUIRED)

include/libp2p/crypto/chachapoly/chachapoly_impl.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ namespace libp2p::crypto::chachapoly {
2525
BytesIn aad) override;
2626

2727
private:
28+
outcome::result<void> init(EVP_AEAD_CTX *ctx, bool encrypt);
29+
2830
const Key key_;
29-
const EVP_CIPHER *cipher_;
30-
const int block_size_;
31+
const EVP_AEAD *aead_;
32+
const size_t overhead_;
3133
libp2p::log::Logger log_ = libp2p::log::createLogger("ChaChaPoly");
3234
};
3335

include/libp2p/crypto/ecdsa_provider/ecdsa_provider_impl.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ namespace libp2p::crypto::ecdsa {
4545
*/
4646
template <typename KeyType>
4747
outcome::result<KeyType> convertEcKeyToBytes(
48-
const std::shared_ptr<EC_KEY> &ec_key,
49-
int (*converter)(EC_KEY *, uint8_t **)) const;
48+
const std::shared_ptr<EC_KEY> &ec_key, auto converter) const;
5049

5150
/**
5251
* @brief Convert bytes to EC_KEY

include/libp2p/crypto/secp256k1_provider/secp256k1_provider_impl.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
#pragma once
88

9+
#include <secp256k1.h>
910
#include <memory>
1011

11-
#include <openssl/ec.h>
1212
#include <libp2p/crypto/secp256k1_provider.hpp>
1313

14+
namespace libp2p::crypto::random {
15+
class CSPRNG;
16+
} // namespace libp2p::crypto::random
17+
1418
namespace libp2p::crypto::secp256k1 {
1519
class Secp256k1ProviderImpl : public Secp256k1Provider {
1620
public:
21+
Secp256k1ProviderImpl(std::shared_ptr<random::CSPRNG> random);
22+
1723
outcome::result<KeyPair> generate() const override;
1824

1925
outcome::result<PublicKey> derive(const PrivateKey &key) const override;
@@ -26,20 +32,7 @@ namespace libp2p::crypto::secp256k1 {
2632
const PublicKey &key) const override;
2733

2834
private:
29-
/**
30-
* @brief Convert secp256k1 private key bytes to OpenSSL EC_KEY structure
31-
* @param input - private key bytes
32-
* @return OpenSSL EC_KEY structure or error code
33-
*/
34-
static outcome::result<std::shared_ptr<EC_KEY>> bytesToPrivateKey(
35-
const PrivateKey &input);
36-
37-
/**
38-
* @brief Convert secp256k1 public key bytes to OpenSSL EC_KEY structure
39-
* @param input - private key bytes
40-
* @return OpenSSL EC_KEY structure or error code
41-
*/
42-
static outcome::result<std::shared_ptr<EC_KEY>> bytesToPublicKey(
43-
const PublicKey &input);
35+
std::shared_ptr<random::CSPRNG> random_;
36+
std::unique_ptr<secp256k1_context, void (*)(secp256k1_context *)> ctx_;
4437
};
4538
} // namespace libp2p::crypto::secp256k1

include/libp2p/injector/network_injector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ namespace libp2p::injector {
285285
auto rsa_provider = std::make_shared<crypto::rsa::RsaProviderImpl>();
286286
auto ecdsa_provider = std::make_shared<crypto::ecdsa::EcdsaProviderImpl>();
287287
auto secp256k1_provider =
288-
std::make_shared<crypto::secp256k1::Secp256k1ProviderImpl>();
288+
std::make_shared<crypto::secp256k1::Secp256k1ProviderImpl>(csprng);
289289
auto hmac_provider = std::make_shared<crypto::hmac::HmacProviderImpl>();
290290
std::shared_ptr<crypto::CryptoProvider> crypto_provider =
291291
std::make_shared<crypto::CryptoProviderImpl>(csprng,

src/crypto/chachapoly/chachapoly_impl.cpp

Lines changed: 51 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <libp2p/common/final_action.hpp>
7+
#include <openssl/aead.h>
88
#include <libp2p/crypto/chachapoly/chachapoly_impl.hpp>
9-
#include <libp2p/crypto/common_functions.hpp>
109
#include <libp2p/crypto/error.hpp>
1110

12-
using libp2p::common::FinalAction;
13-
1411
namespace libp2p::crypto::chachapoly {
12+
constexpr size_t kTagSize = 16;
1513

1614
#define IF1(expr, err, result) \
1715
if (1 != (expr)) { \
@@ -21,125 +19,75 @@ namespace libp2p::crypto::chachapoly {
2119

2220
ChaCha20Poly1305Impl::ChaCha20Poly1305Impl(Key key)
2321
: key_{key},
24-
cipher_{EVP_chacha20_poly1305()},
25-
block_size_{EVP_CIPHER_block_size(cipher_)} {}
22+
aead_{EVP_aead_chacha20_poly1305()},
23+
overhead_{EVP_AEAD_max_overhead(aead_)} {}
24+
25+
outcome::result<void> ChaCha20Poly1305Impl::init(EVP_AEAD_CTX *ctx,
26+
bool encrypt) {
27+
IF1(EVP_AEAD_CTX_init_with_direction(
28+
ctx,
29+
aead_,
30+
key_.data(),
31+
key_.size(),
32+
kTagSize,
33+
encrypt ? evp_aead_seal : evp_aead_open),
34+
"EVP_AEAD_CTX_init",
35+
OpenSslError::FAILED_INITIALIZE_CONTEXT);
36+
return outcome::success();
37+
}
2638

2739
outcome::result<Bytes> ChaCha20Poly1305Impl::encrypt(const Nonce &nonce,
2840
BytesIn plaintext,
2941
BytesIn aad) {
30-
const auto init_failure = OpenSslError::FAILED_INITIALIZE_OPERATION;
31-
std::vector<uint8_t> result;
42+
bssl::ScopedEVP_AEAD_CTX ctx;
43+
OUTCOME_TRY(init(ctx.get(), true));
44+
Bytes result;
3245
// just reserving the space, possibly without actual memory allocation:
3346
// ciphertext length equals to plaintext length
3447
// one block size for EncryptFinal_ex (usually not required)
3548
// 16 bytes for the tag
36-
result.reserve(plaintext.size() + block_size_ + 16);
37-
auto *ctx = EVP_CIPHER_CTX_new();
38-
if (nullptr == ctx) {
39-
return OpenSslError::FAILED_INITIALIZE_CONTEXT;
40-
}
41-
FinalAction free_ctx([ctx] { EVP_CIPHER_CTX_free(ctx); });
42-
43-
IF1(EVP_EncryptInit_ex(ctx, cipher_, nullptr, nullptr, nullptr),
44-
"Failed to initialize EVP encryption.",
45-
init_failure)
46-
47-
IF1(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 12, nullptr),
48-
"Cannot set AEAD initialization vector length.",
49-
init_failure)
50-
51-
IF1(EVP_EncryptInit_ex(ctx, nullptr, nullptr, key_.data(), nonce.data()),
52-
"Cannot finalize init of encryption engine.",
53-
init_failure)
54-
55-
int len{0};
56-
IF1(EVP_EncryptUpdate(ctx, nullptr, &len, nullptr, plaintext.size()),
57-
"Failed to calculate cipher text length.",
58-
OpenSslError::FAILED_ENCRYPT_UPDATE)
59-
60-
// does actual memory allocation
61-
result.resize(len + block_size_ + 16, 0u);
62-
if (not aad.empty()) {
63-
IF1(EVP_EncryptUpdate(ctx, nullptr, &len, aad.data(), aad.size()),
64-
"Failed to apply additional authentication data during encryption.",
65-
OpenSslError::FAILED_ENCRYPT_UPDATE)
66-
}
67-
68-
IF1(EVP_EncryptUpdate(
69-
ctx, result.data(), &len, plaintext.data(), plaintext.size()),
70-
"Plaintext encryption failed.",
71-
OpenSslError::FAILED_ENCRYPT_UPDATE)
72-
int ciphertext_len = len; // without tag size
73-
IF1(EVP_EncryptFinal_ex(ctx, result.data() + len, &len), // NOLINT
74-
"Unable to finalize encryption.",
75-
OpenSslError::FAILED_ENCRYPT_FINALIZE)
76-
77-
ciphertext_len += len;
78-
IF1(EVP_CIPHER_CTX_ctrl(ctx,
79-
EVP_CTRL_AEAD_GET_TAG,
80-
EVP_CTRL_AEAD_GET_TAG,
81-
result.data() + ciphertext_len), // NOLINT
82-
"Failed to write tag.",
83-
OpenSslError::FAILED_ENCRYPT_FINALIZE)
84-
85-
// remove the last block_size bytes if those were not used
86-
result.resize(ciphertext_len + 16);
49+
result.resize(plaintext.size() + overhead_);
50+
size_t out_size = 0;
51+
IF1(EVP_AEAD_CTX_seal(ctx.get(),
52+
result.data(),
53+
&out_size,
54+
result.size(),
55+
nonce.data(),
56+
nonce.size(),
57+
plaintext.data(),
58+
plaintext.size(),
59+
aad.data(),
60+
aad.size()),
61+
"EVP_AEAD_CTX_seal",
62+
OpenSslError::FAILED_ENCRYPT_UPDATE);
63+
result.resize(out_size);
8764
return result;
8865
}
8966

9067
outcome::result<Bytes> ChaCha20Poly1305Impl::decrypt(const Nonce &nonce,
9168
BytesIn ciphertext,
9269
BytesIn aad) {
70+
bssl::ScopedEVP_AEAD_CTX ctx;
71+
OUTCOME_TRY(init(ctx.get(), false));
9372
Bytes result;
9473
// plain text should take less bytes than cipher text,
9574
// at least it would not contain tag-length bytes (16).
9675
// single block size for the case when len(in) == len(out)
97-
result.resize(ciphertext.size() + block_size_, 0u);
98-
auto *ctx = EVP_CIPHER_CTX_new();
99-
FinalAction free_ctx([ctx] { EVP_CIPHER_CTX_free(ctx); });
100-
101-
IF1(EVP_DecryptInit_ex(ctx, cipher_, nullptr, nullptr, nullptr),
102-
"Failed to initialize decryption engine.",
103-
OpenSslError::FAILED_INITIALIZE_OPERATION)
104-
105-
IF1(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 12, nullptr),
106-
"Cannot set AEAD initialization vector length.",
107-
OpenSslError::FAILED_INITIALIZE_OPERATION)
108-
109-
IF1(EVP_CIPHER_CTX_ctrl(
110-
ctx,
111-
EVP_CTRL_AEAD_SET_TAG,
112-
EVP_CTRL_AEAD_GET_TAG,
113-
(uint8_t *)ciphertext.data() + ciphertext.size() - 16), // NOLINT
114-
"Failed to specify buffer for further tag reading.",
115-
OpenSslError::FAILED_DECRYPT_UPDATE)
116-
117-
IF1(EVP_DecryptInit_ex(ctx, nullptr, nullptr, key_.data(), nonce.data()),
118-
"Cannot finalize init of decryption engine.",
119-
OpenSslError::FAILED_INITIALIZE_OPERATION)
120-
121-
int len{0};
122-
if (not aad.empty()) {
123-
IF1(EVP_DecryptUpdate(ctx, nullptr, &len, aad.data(), aad.size()),
124-
"Failed to apply additional authentication data during decryption.",
125-
OpenSslError::FAILED_DECRYPT_UPDATE)
126-
}
127-
128-
IF1(EVP_DecryptUpdate(ctx,
76+
result.resize(ciphertext.size());
77+
size_t out_size = 0;
78+
IF1(EVP_AEAD_CTX_open(ctx.get(),
12979
result.data(),
130-
&len,
80+
&out_size,
81+
result.size(),
82+
nonce.data(),
83+
nonce.size(),
13184
ciphertext.data(),
132-
ciphertext.size() - 16),
133-
"Ciphertext decryption failed.",
134-
OpenSslError::FAILED_DECRYPT_UPDATE)
135-
136-
IF1(EVP_DecryptFinal_ex(ctx,
137-
result.data() + len, // NOLINT
138-
&len),
139-
"Failed to finalize decryption.",
140-
OpenSslError::FAILED_DECRYPT_FINALIZE);
141-
142-
result.resize(ciphertext.size() - 16);
85+
ciphertext.size(),
86+
aad.data(),
87+
aad.size()),
88+
"EVP_AEAD_CTX_open",
89+
OpenSslError::FAILED_DECRYPT_UPDATE);
90+
result.resize(out_size);
14391
return result;
14492
}
14593

src/crypto/common_functions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <libp2p/crypto/common_functions.hpp>
88

9+
#include <openssl/bn.h>
10+
#include <openssl/ecdsa.h>
911
#include <libp2p/common/final_action.hpp>
1012
#include <libp2p/crypto/error.hpp>
1113

src/crypto/crypto_provider/crypto_provider_impl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include <exception>
1010

11+
#include <openssl/ecdsa.h>
1112
#include <openssl/err.h>
1213
#include <openssl/evp.h>
14+
#include <openssl/mem.h>
1315
#include <openssl/rand.h>
1416
#include <openssl/rsa.h>
1517
#include <openssl/x509.h>
@@ -509,7 +511,7 @@ namespace libp2p::crypto {
509511
}
510512

511513
// calculate the size of the buffer for the shared secret
512-
int field_size{EC_GROUP_get_degree(curve_group)};
514+
auto field_size = static_cast<int>(EC_GROUP_get_degree(curve_group));
513515
int secret_len{(field_size + 7) / 8};
514516
uint8_t *secret_buffer{(uint8_t *)OPENSSL_malloc(secret_len)}; // NOLINT
515517
if (nullptr == secret_buffer) {

0 commit comments

Comments
 (0)