| 1 | #include "grease.h" |
| 2 | |
| 3 | #include <random> |
| 4 | #include <set> |
| 5 | |
| 6 | namespace mlspp { |
| 7 | |
| 8 | #ifdef DISABLE_GREASE |
| 9 | |
| 10 | void |
| 11 | grease([[maybe_unused]] Capabilities& capabilities, |
| 12 | [[maybe_unused]] const ExtensionList& extensions) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void |
| 17 | grease([[maybe_unused]] ExtensionList& extensions) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | #else |
| 22 | |
| 23 | // Randomness parmeters: |
| 24 | // * Given a list of N items, insert max(1, rand(p_grease * N)) GREASE values |
| 25 | // * Each GREASE value added is distinct, unless more than 15 values are needed |
| 26 | // * For extensions, each GREASE extension has rand(n_grease_ext) random bytes |
| 27 | // of data |
| 28 | const size_t log_p_grease = 1; // -log2(p_grease) => p_grease = 1/2 |
| 29 | const size_t max_grease_ext_size = 16; |
| 30 | |
| 31 | const std::array<uint16_t, 15> grease_values = { 0x0A0A, 0x1A1A, 0x2A2A, 0x3A3A, |
| 32 | 0x4A4A, 0x5A5A, 0x6A6A, 0x7A7A, |
| 33 | 0x8A8A, 0x9A9A, 0xAAAA, 0xBABA, |
| 34 | 0xCACA, 0xDADA, 0xEAEA }; |
| 35 | |
| 36 | static size_t |
| 37 | rand_int(size_t n) |
| 38 | { |
| 39 | static auto seed = std::random_device()(); |
| 40 | static auto rng = std::mt19937(seed); |
| 41 | return std::uniform_int_distribution<size_t>(0, n)(rng); |
| 42 | } |
| 43 | |
| 44 | static uint16_t |
| 45 | grease_value() |
| 46 | { |
| 47 | const auto where = rand_int(grease_values.size() - 1); |
| 48 | return grease_values.at(where); |
| 49 | } |
| 50 | |
| 51 | static bool |
| 52 | grease_value(uint16_t val) |
| 53 | { |
| 54 | static constexpr auto grease_mask = uint16_t(0x0F0F); |
| 55 | return ((val & grease_mask) == 0x0A0A) && val != 0xFAFA; |
| 56 | } |
| 57 | |
| 58 | static std::set<uint16_t> |
| 59 | grease_sample(size_t count) |
| 60 | { |
| 61 | auto vals = std::set<uint16_t>{}; |
| 62 | |
| 63 | while (vals.size() < count) { |
| 64 | uint16_t val = grease_value(); |
| 65 | while (vals.count(val) > 0 && vals.size() < grease_values.size()) { |
| 66 | val = grease_value(); |
| 67 | } |
| 68 | |
| 69 | vals.insert(val); |
| 70 | } |
| 71 | |
| 72 | return vals; |
| 73 | } |
| 74 | |
| 75 | template<typename T> |
| 76 | static void |
| 77 | grease(std::vector<T>& vec) |
| 78 | { |
| 79 | const auto count = std::max(size_t(1), rand_int(vec.size() >> log_p_grease)); |
| 80 | for (const auto val : grease_sample(count)) { |
| 81 | const auto where = static_cast<ptrdiff_t>(rand_int(vec.size())); |
| 82 | vec.insert(std::begin(vec) + where, static_cast<T>(val)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | grease(Capabilities& capabilities, const ExtensionList& extensions) |
| 88 | { |
| 89 | // Add GREASE to the appropriate portions of the capabilities |
| 90 | grease(capabilities.cipher_suites); |
| 91 | grease(capabilities.extensions); |
| 92 | grease(capabilities.proposals); |
| 93 | grease(capabilities.credentials); |
| 94 | |
| 95 | // Ensure that the GREASE extensions are reflected in Capabilities.extensions |
| 96 | for (const auto& ext : extensions.extensions) { |
| 97 | if (!grease_value(ext.type)) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (stdx::contains(capabilities.extensions, ext.type)) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | const auto where = |
| 106 | static_cast<ptrdiff_t>(rand_int(capabilities.extensions.size())); |
| 107 | const auto where_ptr = std::begin(capabilities.extensions) + where; |
| 108 | capabilities.extensions.insert(where_ptr, ext.type); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | grease(ExtensionList& extensions) |
| 114 | { |
| 115 | auto& ext = extensions.extensions; |
| 116 | const auto count = std::max(size_t(1), rand_int(ext.size() >> log_p_grease)); |
| 117 | for (const auto ext_type : grease_sample(count)) { |
| 118 | const auto where = static_cast<ptrdiff_t>(rand_int(ext.size())); |
| 119 | auto ext_data = random_bytes(rand_int(max_grease_ext_size)); |
| 120 | ext.insert(std::begin(ext) + where, { ext_type, std::move(ext_data) }); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #endif // DISABLE_GREASE |
| 125 | |
| 126 | } // namespace mlspp |