-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: Added support for random generation
Signed-off-by: Michael Boquard <[email protected]>
- Loading branch information
1 parent
282e665
commit ec0b9ac
Showing
6 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ v_cc_library( | |
digest.cc | ||
hmac.cc | ||
key.cc | ||
random.cc | ||
signature.cc | ||
ssl_utils.cc | ||
DEPS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
#include "crypto/crypto.h" | ||
#include "crypto/ssl_utils.h" | ||
#include "internal.h" | ||
|
||
#include <openssl/rand.h> | ||
|
||
namespace crypto { | ||
bytes_span<> generate_random(bytes_span<> buf, use_private_rng private_rng) { | ||
if (private_rng) { | ||
if (1 != RAND_priv_bytes_ex(nullptr, buf.data(), buf.size(), 0)) { | ||
throw internal::ossl_error( | ||
"Failed to get random output from private RNG"); | ||
} | ||
} else { | ||
if (1 != RAND_bytes_ex(nullptr, buf.data(), buf.size(), 0)) { | ||
throw internal::ossl_error( | ||
"Failed to get random output from public RNG"); | ||
} | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
bytes generate_random(size_t len, use_private_rng private_rng) { | ||
bytes ret(bytes::initialized_later(), len); | ||
generate_random(ret, private_rng); | ||
return ret; | ||
} | ||
} // namespace crypto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
#include "crypto/crypto.h" | ||
#include "test_utils/test.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(crypto_rand, generate_rand) { | ||
const size_t len = 32; | ||
auto ret = crypto::generate_random(len, crypto::use_private_rng::no); | ||
ASSERT_EQ(ret.size(), len); | ||
} | ||
|
||
TEST(crypto_rand, generate_rand_priv) { | ||
const size_t len = 32; | ||
auto ret = crypto::generate_random(len, crypto::use_private_rng::yes); | ||
ASSERT_EQ(ret.size(), len); | ||
} | ||
|
||
TEST(crypto_rand, generate_rand_span) { | ||
std::vector<uint8_t> rand_data(32); | ||
auto copy_of_orig = rand_data; | ||
crypto::generate_random(rand_data, crypto::use_private_rng::no); | ||
ASSERT_NE(copy_of_orig, rand_data); | ||
} | ||
|
||
TEST(crypto_rand, generate_rand_span_private) { | ||
std::vector<uint8_t> rand_data(32); | ||
auto copy_of_orig = rand_data; | ||
crypto::generate_random(rand_data, crypto::use_private_rng::yes); | ||
ASSERT_NE(copy_of_orig, rand_data); | ||
} |