| static BIGNUM *get_y2(BIGNUM *x, const BIGNUM *mod, BN_CTX *big_num_context) { |
| 6 | // returns y^2 = x^3 + 486662 * x^2 + x |
| 7 | BIGNUM *y = BN_dup(x); |
| 8 | assert(y != NULL); |
| 9 | BIGNUM *coef = BN_new(); |
| 10 | BN_set_word(coef, 486662); |
| 11 | BN_mod_add(y, y, coef, mod, big_num_context); |
| 12 | BN_mod_mul(y, y, x, mod, big_num_context); |
| 13 | BN_one(coef); |
| 14 | BN_mod_add(y, y, coef, mod, big_num_context); |
| 15 | BN_mod_mul(y, y, x, mod, big_num_context); |
| 16 | BN_clear_free(coef); |
| 17 | return y; |
| 18 | } |
| 19 |
| 20 | static BIGNUM *get_double_x(BIGNUM *x, const BIGNUM *mod, BN_CTX *big_num_context) { |
| 21 | // returns x_2 =(x^2 - 1)^2/(4*y^2) |
| 22 | BIGNUM *denominator = get_y2(x, mod, big_num_context); |
| 23 | assert(denominator != NULL); |
| 24 | BIGNUM *coef = BN_new(); |
| 25 | BN_set_word(coef, 4); |
| 26 | BN_mod_mul(denominator, denominator, coef, mod, big_num_context); |
| 27 |
| 28 | BIGNUM *numerator = BN_new(); |
| 29 | assert(numerator != NULL); |
| 30 | BN_mod_mul(numerator, x, x, mod, big_num_context); |
| 31 | BN_one(coef); |
| 32 | BN_mod_sub(numerator, numerator, coef, mod, big_num_context); |
| 33 | BN_mod_mul(numerator, numerator, numerator, mod, big_num_context); |
| 34 |
| 35 | BN_mod_inverse(denominator, denominator, mod, big_num_context); |
| 36 | BN_mod_mul(numerator, numerator, denominator, mod, big_num_context); |
| 37 |
| 38 | BN_clear_free(coef); |
| 39 | BN_clear_free(denominator); |
| 40 | return numerator; |
| 41 | } |
| 42 |
| 43 | static void generate_key_ml_kem_768(unsigned char *key) { |
| 44 | constexpr uint32_t Q = 3329; |
| 45 | constexpr int N = 384; |
| 46 |
| 47 | std::vector<uint32_t> values(N * 2); |
| 48 | RAND_bytes(reinterpret_cast<unsigned char*>(values.data()),values.size() * sizeof(uint32_t)); |
| 49 |
| 50 | for (int i = 0; i < N; ++i) { |
| 51 | uint32_t a = values[i * 2] % Q; |
| 52 | uint32_t b = values[i * 2 + 1] % Q; |
| 53 |
| 54 | key[i * 3 + 0] = static_cast<unsigned char>(a & 0xFFu); |
| 55 | key[i * 3 + 1] = static_cast<unsigned char>((a >> 8) | ((b & 0x0Fu) << 4)); |
| 56 | key[i * 3 + 2] = static_cast<unsigned char>(b >> 4); |
| 57 | } |
| 58 |
| 59 | RAND_bytes(key + 1152, 32); |
| 60 | } |
| 61 |
| 62 | static void generate_public_key(unsigned char *key) { |
| 63 | BIGNUM *mod = NULL; |
| 64 | BN_hex2bn(&mod, "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"); |
| 65 | BIGNUM *pow = NULL; |
| 66 | BN_hex2bn(&pow, "3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6"); |
| 67 | BN_CTX *big_num_context = BN_CTX_new(); |
| 68 | assert(big_num_context != NULL); |
| 69 |
| 70 | BIGNUM *x = BN_new(); |
| 71 | while (1) { |
| 72 | RAND_bytes(key, 32); |
| 73 | key[31] &= 127; |
| 74 | BN_bin2bn(key, 32, x); |
| 75 | assert(x != NULL); |
| 76 | BN_mod_mul(x, x, x, mod, big_num_context); |
| 77 |
| 78 | BIGNUM *y = get_y2(x, mod, big_num_context); |
| 79 |
| 80 | BIGNUM *r = BN_new(); |
| 81 | BN_mod_exp(r, y, pow, mod, big_num_context); |
| 82 | BN_clear_free(y); |
| 83 | if (BN_is_one(r)) { |
| 84 | BN_clear_free(r); |
| 85 | break; |
| 86 | } |
| 87 | BN_clear_free(r); |
| 88 | } |
| 89 |
| 90 | int i; |
| 91 | for (i = 0; i < 3; i++) { |
| 92 | BIGNUM *x2 = get_double_x(x, mod, big_num_context); |
| 93 | BN_clear_free(x); |
| 94 | x = x2; |
| 95 | } |
| 96 |
| 97 | int num_size = BN_num_bytes(x); |
| 98 | assert(num_size <= 32); |
| 99 | memset(key, '\0', 32 - num_size); |
| 100 | BN_bn2bin(x, key + (32 - num_size)); |
| 101 | for (i = 0; i < 16; i++) { |
| 102 | unsigned char t = key[i]; |
| 103 | key[i] = key[31 - i]; |
| 104 | key[31 - i] = t; |
| 105 | } |
| 106 |
| 107 | BN_clear_free(x); |
| 108 | BN_CTX_free(big_num_context); |
| 109 | BN_clear_free(pow); |
| 110 | BN_clear_free(mod); |
| 111 | } |
| 112 |
| 113 | TLSHello::TLSHello() { |
| 114 | RAND_bytes(grease, MAX_GREASE); |
| 115 | for (int a = 0; a < MAX_GREASE; a++) { |
| 116 | grease[a] = (uint8_t) ((grease[a] & 0xf0) + 0x0A); |
| 117 | } |
| 118 | for (size_t i = 1; i < MAX_GREASE; i += 2) { |
| 119 | if (grease[i] == grease[i + 1]) { |
| 120 | grease[i] ^= 0x10; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 |
| 125 | TLSHello::Op TLSHello::Op::string(const char str[], size_t len) { |
| 126 | Op res; |
| 127 | res.type = Type::String; |
| 128 | res.data = std::string(str, len); |
| 129 | return res; |
| 130 | } |
| 131 |
| 132 | TLSHello::Op TLSHello::Op::random(size_t length) { |
| 133 | Op res; |
| 134 | res.type = Type::Random; |
| 135 | res.length = length; |
| 136 | return res; |
| 137 | } |
| 138 |
| 139 | TLSHello::Op TLSHello::Op::K() { |
| 140 | Op res; |
| 141 | res.type = Type::K; |
| 142 | res.length = 32; |
| 143 | return res; |
| 144 | } |
| 145 |
| 146 | TLSHello::Op TLSHello::Op::E() { |
| 147 | Op res; |
| 148 | res.type = Type::E; |
| 149 | return res; |
| 150 | } |
| 151 |
| 152 | TLSHello::Op TLSHello::Op::M() { |
| 153 | Op res; |
| 154 | res.type = Type::M; |
| 155 | return res; |
| 156 | } |
| 157 |
| 158 | TLSHello::Op TLSHello::Op::P() { |
| 159 | Op res; |
| 160 | res.type = Type::P; |
| 161 | return res; |
| 162 | } |
| 163 |
| 164 | TLSHello::Op TLSHello::Op::zero(size_t length) { |
| 165 | Op res; |
| 166 | res.type = Type::Zero; |
| 167 | res.length = length; |
| 168 | return res; |
| 169 | } |
| 170 |
| 171 | TLSHello::Op TLSHello::Op::domain() { |
| 172 | Op res; |
| 173 | res.type = Type::Domain; |
| 174 | return res; |
| 175 | } |
| 176 |
| 177 | TLSHello::Op TLSHello::Op::grease(int seed) { |
| 178 | Op res; |
| 179 | res.type = Type::Grease; |
| 180 | res.seed = seed; |
| 181 | return res; |
| 182 | } |
| 183 |
| 184 | TLSHello::Op TLSHello::Op::begin_scope() { |
| 185 | Op res; |
| 186 | res.type = Type::BeginScope; |
| 187 | return res; |
| 188 | } |
| 189 |
| 190 | TLSHello::Op TLSHello::Op::end_scope() { |
| 191 | Op res; |
| 192 | res.type = Type::EndScope; |
| 193 | return res; |
| 194 | } |
| 195 |
| 196 | TLSHello::Op TLSHello::Op::permutation(std::vector<std::vector<Op>> entities) { |
| 197 | Op res; |
| 198 | res.type = Type::Permutation; |
| 199 | res.entities = std::move(entities); |
| 200 | return res; |
| 201 | } |
| 202 |
| 203 | const TLSHello &TLSHello::getDefault() { |
| 204 | static TLSHello result = [] { |
| 205 | TLSHello res; |
| 206 | res.ops = { |
| 207 | Op::string("\x16\x03\x01", 3), |
| 208 | Op::begin_scope(), |
| 209 | Op::string("\x01\x00", 2), |
| 210 | Op::begin_scope(), |
| 211 | Op::string("\x03\x03", 2), |
| 212 | Op::zero(32), |
| 213 | Op::string("\x20", 1), |
| 214 | Op::random(32), |
| 215 | Op::string("\x00\x20", 2), |
| 216 | Op::grease(0), |
| 217 | Op::string("\x13\x01\x13\x02\x13\x03\xc0\x2b\xc0\x2f\xc0\x2c\xc0\x30\xcc\xa9\xcc\xa8\xc0\x13\xc0\x14\x00\x9c\x00\x9d\x00\x2f\x00\x35\x01\x00", 32), |
| 218 | Op::begin_scope(), |
| 219 | Op::grease(2), |
| 220 | Op::string("\x00\x00", 2), |
| 221 | Op::permutation({ |
| 222 | { |
| 223 | Op::string("\x00\x00", 2), |
| 224 | Op::begin_scope(), |
| 225 | Op::begin_scope(), |
| 226 | Op::string("\x00", 1), |
| 227 | Op::begin_scope(), |
| 228 | Op::domain(), |
| 229 | Op::end_scope(), |
| 230 | Op::end_scope(), |
| 231 | Op::end_scope() |
| 232 | }, |
| 233 | { Op::string("\x00\x05\x00\x05\x01\x00\x00\x00\x00",9) }, |
| 234 | { |
| 235 | Op::string("\x00\x0a\x00\x0c\x00\x0a", 6), |
| 236 | Op::grease(4), |
| 237 | Op::string("\x11\xec\x00\x1d\x00\x17\x00\x18", 8) |
| 238 | }, |
| 239 | { Op::string("\x00\x0b\x00\x02\x01\x00", 6) }, |
| 240 | { Op::string("\x00\x0d\x00\x18\x00\x16\x09\x04\x09\x05\x09\x06\x04\x03\x08\x04\x04\x01\x05\x03\x08\x05\x05\x01\x08\x06\x06\x01",28) }, |
| 241 | { Op::string("\x00\x10\x00\x0e\x00\x0c\x02\x68\x32\x08\x68\x74\x74\x70\x2f\x31\x2e\x31", 18) }, |
| 242 | { Op::string("\x00\x12\x00\x00", 4) }, |
| 243 | { Op::string("\x00\x17\x00\x00", 4) }, |
| 244 | { Op::string("\x00\x1b\x00\x03\x02\x00\x02", 7) }, |
| 245 | { Op::string("\x00\x23\x00\x00", 4) }, |
| 246 | { |
| 247 | Op::string("\x00\x2b\x00\x07\x06", 5), |
| 248 | Op::grease(6), |
| 249 | Op::string("\x03\x04\x03\x03", 4) |
| 250 | }, |
| 251 | { Op::string("\x00\x2d\x00\x02\x01\x01", 6) }, |
| 252 | { |
| 253 | Op::string("\x00\x33\x04\xef\x04\xed", 6), |
| 254 | Op::grease(4), |
| 255 | Op::string("\x00\x01\x00\x11\xec\x04\xc0", 7), |
| 256 | Op::M(), |
| 257 | Op::K(), |
| 258 | Op::string("\x00\x1d\x00\x20", 4), |
| 259 | Op::K(), |
| 260 | }, |
| 261 | { Op::string("\x44\xcd\x00\x05\x00\x03\x02\x68\x32", 9) }, |
| 262 | { |
| 263 | Op::string("\xfe\x0d", 2), |
| 264 | Op::begin_scope(), |
| 265 | Op::string("\x00\x00\x01\x00\x01", 5), |
| 266 | Op::random(1), |
| 267 | Op::string("\x00\x20", 2), |
| 268 | Op::K(), |
| 269 | Op::begin_scope(), |
| 270 | Op::E(), |
| 271 | Op::end_scope(), |
| 272 | Op::end_scope() |
| 273 | }, |
| 274 | { Op::string("\xff\x01\x00\x01\x00", 5) } |
| 275 | }), |
| 276 | Op::grease(3), |
| 277 | Op::string("\x00\x01\x00", 3), |
| 278 | Op::P(), |
| 279 | Op::end_scope(), |
| 280 | Op::end_scope(), |
| 281 | Op::end_scope() |
| 282 | }; |
| 283 | return res; |
| 284 | }(); |
| 285 | return result; |
| 286 | } |
| 287 |
| 288 | uint32_t TLSHello::writeToBuffer(uint8_t *data) { |
| 289 | uint32_t offset = 0; |
| 290 | for (auto op : ops) { |
| 291 | writeOp(op, data, offset); |
| 292 | } |
| 293 | return offset; |
| 294 | } |
| 295 |
| 296 | void TLSHello::setDomain(std::string value) { |
| 297 | domain = std::move(value); |
| 298 | } |
| 299 |
| 300 | void TLSHello::writeOp(const TLSHello::Op &op, uint8_t *data, uint32_t &offset) { |
| 301 | using Type = TLSHello::Op::Type; |
| 302 | switch (op.type) { |
| 303 | case Type::String: |
| 304 | memcpy(data + offset, op.data.data(), op.data.size()); |
| 305 | offset += op.data.size(); |
| 306 | break; |
| 307 | case Type::Random: |
| 308 | RAND_bytes(data + offset, (size_t) op.length); |
| 309 | offset += op.length; |
| 310 | break; |
| 311 | case Type::K: |
| 312 | generate_public_key(data + offset); |
| 313 | offset += op.length; |
| 314 | break; |
| 315 | case Type::M: |
| 316 | generate_key_ml_kem_768(data + offset); |
| 317 | offset += 1184; |
| 318 | break; |
| 319 | case Type::Zero: |
| 320 | std::memset(data + offset, 0, op.length); |
| 321 | offset += op.length; |
| 322 | break; |
| 323 | case Type::Domain: { |
| 324 | size_t size = domain.size(); |
| 325 | if (size > 253) { |
| 326 | size = 253; |
| 327 | } |
| 328 | memcpy(data + offset, domain.data(), size); |
| 329 | offset += size; |
| 330 | break; |
| 331 | } |
| 332 | case Type::Grease: { |
| 333 | data[offset] = grease[op.seed]; |
| 334 | data[offset + 1] = grease[op.seed]; |
| 335 | offset += 2; |
| 336 | break; |
| 337 | } |
| 338 | case Type::BeginScope: |
| 339 | scopeOffset.push_back(offset); |
| 340 | offset += 2; |
| 341 | break; |
| 342 | case Type::EndScope: { |
| 343 | auto begin_offset = scopeOffset.back(); |
| 344 | scopeOffset.pop_back(); |
| 345 | size_t size = offset - begin_offset - 2; |
| 346 | data[begin_offset] = static_cast<uint8_t>((size >> 8) & 0xff); |
| 347 | data[begin_offset + 1] = static_cast<uint8_t>(size & 0xff); |
| 348 | break; |
| 349 | } |
| 350 | case Type::E: { |
| 351 | size_t r = rand() % 4; |
| 352 | size_t length = (r == 0 ? 144 : |
| 353 | (r == 1 ? 176 : |
| 354 | (r == 2 ? 208 : 240))); |
| 355 | RAND_bytes(data + offset, (size_t) length); |
| 356 | offset += length; |
| 357 | break; |
| 358 | } |
| 359 | case Type::P: { |
| 360 | auto length = offset; |
| 361 | if (length <= 513) { |
| 362 | writeOp(Op::string("\x00\x15", 2), data, offset); |
| 363 | writeOp(Op::begin_scope(), data, offset); |
| 364 | writeOp(Op::zero(513 - length), data, offset); |
| 365 | writeOp(Op::end_scope(), data, offset); |
| 366 | } |
| 367 | break; |
| 368 | } |
| 369 | case Type::Permutation: { |
| 370 | std::vector<std::vector<Op>> list = {}; |
| 371 | for (const auto &part : op.entities) { |
| 372 | list.push_back(part); |
| 373 | } |
| 374 | size_t size = list.size(); |
| 375 | for (int i = 0; i < size - 1; i++) { |
| 376 | int j = i + rand() % (size - i); |
| 377 | if (i != j) { |
| 378 | std::swap(list[i], list[j]); |
| 379 | } |
| 380 | } |
| 381 | for (const auto &part : list) { |
| 382 | for (const auto &op_local: part) { |
| 383 | writeOp(op_local, data, offset); |
| 384 | } |
| 385 | } |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |