* @brief Constant-time string comparison to prevent timing attacks. * * Uses OpenSSL's CRYPTO_memcmp for guaranteed constant-time behavior. */
| 86 | * Uses OpenSSL's CRYPTO_memcmp for guaranteed constant-time behavior. |
| 87 | */ |
| 88 | bool ConstantTimeCompare(const std::string& a, const std::string& b) |
| 89 | { |
| 90 | // Compare in constant time to avoid leaking token length through timing. |
| 91 | // XOR of sizes is non-zero if they differ; CRYPTO_memcmp compares up to |
| 92 | // min(len) bytes. The combined result is zero only when both size and |
| 93 | // content match exactly. |
| 94 | volatile size_t result = a.size() ^ b.size(); |
| 95 | const size_t len = std::min(a.size(), b.size()); |
| 96 | result |= static_cast<size_t>(CRYPTO_memcmp(a.data(), b.data(), len)); |
| 97 | return result == 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @brief Check if a request path is exempt from authentication. |
no test coverage detected