| 1 | /**************************************************************************/ |
| 2 | /* test_crypto_mbedtls.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "test_crypto_mbedtls.h" |
| 32 | |
| 33 | #include "../crypto_mbedtls.h" |
| 34 | |
| 35 | #include "core/io/file_access.h" |
| 36 | #include "tests/test_macros.h" |
| 37 | #include "tests/test_utils.h" |
| 38 | |
| 39 | namespace TestCryptoMbedTLS { |
| 40 | |
| 41 | void hmac_digest_test(HashingContext::HashType ht, String expected_hex) { |
| 42 | CryptoMbedTLS crypto; |
| 43 | PackedByteArray key = String("supersecretkey").to_utf8_buffer(); |
| 44 | PackedByteArray msg = String("Return of the MAC!").to_utf8_buffer(); |
| 45 | PackedByteArray digest = crypto.hmac_digest(ht, key, msg); |
| 46 | String hex = String::hex_encode_buffer(digest.ptr(), digest.size()); |
| 47 | CHECK(hex == expected_hex); |
| 48 | } |
| 49 | |
| 50 | void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex) { |
| 51 | HMACContextMbedTLS ctx; |
| 52 | PackedByteArray key = String("supersecretkey").to_utf8_buffer(); |
| 53 | PackedByteArray msg1 = String("Return of ").to_utf8_buffer(); |
| 54 | PackedByteArray msg2 = String("the MAC!").to_utf8_buffer(); |
| 55 | Error err = ctx.start(ht, key); |
| 56 | CHECK(err == OK); |
| 57 | err = ctx.update(msg1); |
| 58 | CHECK(err == OK); |
| 59 | err = ctx.update(msg2); |
| 60 | CHECK(err == OK); |
| 61 | PackedByteArray digest = ctx.finish(); |
| 62 | String hex = String::hex_encode_buffer(digest.ptr(), digest.size()); |
| 63 | CHECK(hex == expected_hex); |
| 64 | } |
| 65 | |
| 66 | Ref<CryptoKey> create_crypto_key(const String &p_key_path, bool p_public_only) { |
| 67 | Ref<CryptoKey> crypto_key = Ref<CryptoKey>(CryptoKey::create()); |
| 68 | crypto_key->load(p_key_path, p_public_only); |
| 69 | return crypto_key; |
| 70 | } |
| 71 | |
| 72 | String read_file_s(const String &p_file_path) { |
| 73 | Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ); |
| 74 | REQUIRE(file_access.is_valid()); |
| 75 | return file_access->get_as_utf8_string(); |
| 76 | } |
| 77 | |
| 78 | bool files_equal(const String &p_in_path, const String &p_out_path) { |
| 79 | const String s_in = read_file_s(p_in_path); |
| 80 | const String s_out = read_file_s(p_out_path); |
| 81 | return s_in == s_out; |
| 82 | } |
| 83 | |
| 84 | void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) { |
| 85 | Ref<CryptoKey> crypto_key = create_crypto_key(p_key_path, p_public_only); |
| 86 | bool is_equal = crypto_key->is_public_only() == p_public_only; |
| 87 | CHECK(is_equal); |
| 88 | } |
| 89 | |
| 90 | void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) { |
| 91 | Ref<CryptoKey> crypto_key = create_crypto_key(p_in_path, p_public_only); |
| 92 | crypto_key->save(p_out_path, p_public_only); |
| 93 | bool is_equal = files_equal(p_in_path, p_out_path); |
| 94 | CHECK(is_equal); |
| 95 | } |
| 96 | |
| 97 | void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) { |
| 98 | Ref<CryptoKey> crypto_key = create_crypto_key(p_in_priv_path, false); |
| 99 | crypto_key->save(p_out_path, true); |
| 100 | bool is_equal = files_equal(p_in_pub_path, p_out_path); |
| 101 | CHECK(is_equal); |
| 102 | } |
| 103 | } // namespace TestCryptoMbedTLS |