QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 1 | #include "http2/test_tools/http2_random.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 2 | |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 3 | #include "absl/strings/escaping.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 4 | #include "http2/platform/api/http2_logging.h" |
| 5 | #include "http2/platform/api/http2_string_utils.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 6 | #include "third_party/boringssl/src/include/openssl/chacha.h" |
| 7 | #include "third_party/boringssl/src/include/openssl/rand.h" |
| 8 | |
| 9 | static const uint8_t kZeroNonce[12] = {0}; |
| 10 | |
| 11 | namespace http2 { |
| 12 | namespace test { |
| 13 | |
| 14 | Http2Random::Http2Random() { |
| 15 | RAND_bytes(key_, sizeof(key_)); |
| 16 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 17 | HTTP2_LOG(INFO) << "Initialized test RNG with the following key: " << Key(); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 18 | } |
| 19 | |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 20 | Http2Random::Http2Random(absl::string_view key) { |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 21 | std::string decoded_key = absl::HexStringToBytes(key); |
vasilvv | afcc317 | 2021-02-02 12:01:07 -0800 | [diff] [blame] | 22 | QUICHE_CHECK_EQ(sizeof(key_), decoded_key.size()); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 23 | memcpy(key_, decoded_key.data(), sizeof(key_)); |
| 24 | } |
| 25 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 26 | std::string Http2Random::Key() const { |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 27 | return absl::BytesToHexString( |
| 28 | absl::string_view(reinterpret_cast<const char*>(key_), sizeof(key_))); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void Http2Random::FillRandom(void* buffer, size_t buffer_size) { |
| 32 | memset(buffer, 0, buffer_size); |
| 33 | uint8_t* buffer_u8 = reinterpret_cast<uint8_t*>(buffer); |
| 34 | CRYPTO_chacha_20(buffer_u8, buffer_u8, buffer_size, key_, kZeroNonce, |
| 35 | counter_++); |
| 36 | } |
| 37 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 38 | std::string Http2Random::RandString(int length) { |
| 39 | std::string result; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 40 | result.resize(length); |
| 41 | FillRandom(&result[0], length); |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | uint64_t Http2Random::Rand64() { |
| 46 | union { |
| 47 | uint64_t number; |
| 48 | uint8_t bytes[sizeof(uint64_t)]; |
| 49 | } result; |
| 50 | FillRandom(result.bytes, sizeof(result.bytes)); |
| 51 | return result.number; |
| 52 | } |
| 53 | |
| 54 | double Http2Random::RandDouble() { |
| 55 | union { |
| 56 | double f; |
| 57 | uint64_t i; |
| 58 | } value; |
| 59 | value.i = (1023ull << 52ull) | (Rand64() & 0xfffffffffffffu); |
| 60 | return value.f - 1.0; |
| 61 | } |
| 62 | |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 63 | std::string Http2Random::RandStringWithAlphabet(int length, |
| 64 | absl::string_view alphabet) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 65 | std::string result; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 66 | result.resize(length); |
| 67 | for (int i = 0; i < length; i++) { |
| 68 | result[i] = alphabet[Uniform(alphabet.size())]; |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | } // namespace test |
| 74 | } // namespace http2 |