| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/crypto/crypto.h" |
| 34 |
#include "tests/test_macros.h" |
| 35 |
|
| 36 |
namespace TestCrypto { |
| 37 |
|
| 38 |
class _MockCrypto : public Crypto { |
| 39 |
virtual PackedByteArray generate_random_bytes(int p_bytes) { return PackedByteArray(); } |
| 40 |
virtual Ref<CryptoKey> generate_rsa(int p_bytes) { return nullptr; } |
| 41 |
virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, const String &p_issuer_name, const String &p_not_before, const String &p_not_after) { return nullptr; } |
| 42 |
|
| 43 |
virtual Vector<uint8_t> sign(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, Ref<CryptoKey> p_key) { return Vector<uint8_t>(); } |
| 44 |
virtual bool verify(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, const Vector<uint8_t> &p_signature, Ref<CryptoKey> p_key) { return false; } |
| 45 |
virtual Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_plaintext) { return Vector<uint8_t>(); } |
| 46 |
virtual Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_ciphertext) { return Vector<uint8_t>(); } |
| 47 |
virtual PackedByteArray hmac_digest(HashingContext::HashType p_hash_type, const PackedByteArray &p_key, const PackedByteArray &p_msg) { return PackedByteArray(); } |
| 48 |
}; |
| 49 |
|
| 50 |
PackedByteArray raw_to_pba(const uint8_t *arr, size_t len) { |
| 51 |
PackedByteArray pba; |
| 52 |
pba.resize(len); |
| 53 |
for (size_t i = 0; i < len; i++) { |
| 54 |
pba.set(i, arr[i]); |
| 55 |
} |
| 56 |
return pba; |
| 57 |
} |
| 58 |
|
| 59 |
TEST_CASE("[Crypto] PackedByteArray constant time compare") { |
| 60 |
const uint8_t hm1[] = { 144, 140, 176, 38, 88, 113, 101, 45, 71, 105, 10, 91, 248, 16, 117, 244, 189, 30, 238, 29, 219, 134, 82, 130, 212, 114, 161, 166, 188, 169, 200, 106 }; |
| 61 |
const uint8_t hm2[] = { 80, 30, 144, 228, 108, 38, 188, 125, 150, 64, 165, 127, 221, 118, 144, 232, 45, 100, 15, 248, 193, 244, 245, 34, 116, 147, 132, 200, 110, 27, 38, 75 }; |
| 62 |
PackedByteArray p1 = raw_to_pba(hm1, std_size(hm1)); |
| 63 |
PackedByteArray p2 = raw_to_pba(hm2, std_size(hm2)); |
| 64 |
_MockCrypto crypto; |
| 65 |
bool equal = crypto.constant_time_compare(p1, p1); |
| 66 |
CHECK(equal); |
| 67 |
equal = crypto.constant_time_compare(p1, p2); |
| 68 |
CHECK(!equal); |
| 69 |
} |
| 70 |
} |