Skip to content
0.2.0-dev Download ZIP
211 lines ·8.7 KB Raw
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/crypto/crypto_resource_format.h"
36#include "core/io/file_access.h"
37#include "tests/test_macros.h"
38#include "tests/test_utils.h"
39
40namespace TestCryptoMbedTLS {
41
42void hmac_digest_test(HashingContext::HashType ht, String expected_hex) {
43 CryptoMbedTLS crypto;
44 PackedByteArray key = String("supersecretkey").to_utf8_buffer();
45 PackedByteArray msg = String("Return of the MAC!").to_utf8_buffer();
46 PackedByteArray digest = crypto.hmac_digest(ht, key, msg);
47 String hex = String::hex_encode_buffer(digest.ptr(), digest.size());
48 CHECK(hex == expected_hex);
49}
50
51void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex) {
52 HMACContextMbedTLS ctx;
53 PackedByteArray key = String("supersecretkey").to_utf8_buffer();
54 PackedByteArray msg1 = String("Return of ").to_utf8_buffer();
55 PackedByteArray msg2 = String("the MAC!").to_utf8_buffer();
56 Error err = ctx.start(ht, key);
57 CHECK(err == OK);
58 err = ctx.update(msg1);
59 CHECK(err == OK);
60 err = ctx.update(msg2);
61 CHECK(err == OK);
62 PackedByteArray digest = ctx.finish();
63 String hex = String::hex_encode_buffer(digest.ptr(), digest.size());
64 CHECK(hex == expected_hex);
65}
66
67Ref<CryptoKey> load_key(const String &p_key_path, bool p_public_only) {
68 Ref<CryptoKey> key(memnew(CryptoKeyMbedTLS));
69 REQUIRE(key.is_valid());
70 Error err = key->load(p_key_path, p_public_only);
71 REQUIRE(err == OK);
72 return key;
73}
74
75String read_file_s(const String &p_file_path) {
76 Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ);
77 REQUIRE(file_access.is_valid());
78 return file_access->get_as_utf8_string();
79}
80
81bool files_equal(const String &p_in_path, const String &p_out_path) {
82 const String s_in = read_file_s(p_in_path);
83 const String s_out = read_file_s(p_out_path);
84 return s_in == s_out;
85}
86
87void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) {
88 Ref<CryptoKey> crypto_key = load_key(p_key_path, p_public_only);
89 bool is_equal = crypto_key->is_public_only() == p_public_only;
90 CHECK(is_equal);
91}
92
93void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) {
94 Ref<CryptoKey> crypto_key = load_key(p_in_path, p_public_only);
95 crypto_key->save(p_out_path, p_public_only);
96 bool is_equal = files_equal(p_in_path, p_out_path);
97 CHECK(is_equal);
98}
99
100void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) {
101 Ref<CryptoKey> crypto_key = load_key(p_in_priv_path, false);
102 crypto_key->save(p_out_path, true);
103 bool is_equal = files_equal(p_in_pub_path, p_out_path);
104 CHECK(is_equal);
105}
106
107void crypto_key_generate_save_load(const String &p_out_priv_path, const String &p_out_pub_path) {
108 Ref<Crypto> c(memnew(CryptoMbedTLS));
109 REQUIRE(c.is_valid());
110 Ref<CryptoKey> key = c->generate_rsa(1024);
111 CHECK(key->save_to_string() != key->save_to_string(true));
112 Error err = key->save(p_out_priv_path);
113 REQUIRE(err == OK);
114 err = key->save(p_out_pub_path, true);
115 REQUIRE(err == OK);
116 Ref<CryptoKey> priv = load_key(p_out_priv_path, false);
117 Ref<CryptoKey> pub = load_key(p_out_pub_path, true);
118 CHECK(pub->save_to_string(true) != priv->save_to_string());
119 CHECK(pub->save_to_string(true) == priv->save_to_string(true));
120 CHECK(pub->save_to_string(true) == key->save_to_string(true));
121 CHECK(key->save_to_string() == priv->save_to_string());
122}
123
124void crypto_generate_sign_verify_test(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash) {
125 Ref<Crypto> c(memnew(CryptoMbedTLS));
126 REQUIRE(c.is_valid());
127 Ref<CryptoKey> key = c->generate_rsa(1024);
128 Vector<uint8_t> signature = c->sign(p_hash_type, p_hash, key);
129 CHECK(signature.size() > 0);
130 bool valid = c->verify(p_hash_type, p_hash, signature, key);
131 CHECK(valid);
132 Ref<CryptoKey> pub(memnew(CryptoKeyMbedTLS));
133 pub->load_from_string(key->save_to_string(true), true);
134 valid = c->verify(p_hash_type, p_hash, signature, pub);
135 CHECK(valid);
136}
137
138void crypto_sign_verify_test(const String &p_in_priv_path, const String &p_in_pub_path, HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash) {
139 Ref<CryptoKey> priv = load_key(p_in_priv_path, false);
140 Ref<CryptoKey> pub = load_key(p_in_pub_path, true);
141 Ref<Crypto> c(memnew(CryptoMbedTLS));
142 REQUIRE(c.is_valid());
143 Vector<uint8_t> signature = c->sign(p_hash_type, p_hash, priv);
144 CHECK(signature.size() > 0);
145 bool valid = c->verify(p_hash_type, p_hash, signature, pub);
146 CHECK(valid);
147}
148
149void crypto_verify_test(const String &p_in_pub_path, HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, const Vector<uint8_t> &p_signature, bool p_expected) {
150 Ref<CryptoKey> pub = load_key(p_in_pub_path, true);
151 Ref<Crypto> c(memnew(CryptoMbedTLS));
152 REQUIRE(c.is_valid());
153 bool valid = c->verify(p_hash_type, p_hash, p_signature, pub);
154 CHECK_EQ(valid, p_expected);
155}
156
157bool equals(const PackedByteArray &p_a, const PackedByteArray &p_b) {
158 if (p_a.size() != p_b.size()) {
159 return false;
160 }
161 if (p_a.is_empty()) {
162 return true;
163 }
164 return memcmp(p_a.ptr(), p_b.ptr(), p_a.size()) == 0;
165}
166
167void crypto_encrypt_decrypt_test(const String &p_in_priv_path, const String &p_in_pub_path, const Vector<uint8_t> &p_message) {
168 Ref<CryptoKey> priv = load_key(p_in_priv_path, false);
169 Ref<CryptoKey> pub = load_key(p_in_pub_path, true);
170 Ref<Crypto> c(memnew(CryptoMbedTLS));
171 REQUIRE(c.is_valid());
172 Vector<uint8_t> ciphertext = c->encrypt(pub, p_message);
173 Vector<uint8_t> plaintext = c->decrypt(priv, ciphertext);
174 CHECK(equals(plaintext, p_message));
175}
176
177void crypto_decrypt_test(const String &p_in_priv_path, const Vector<uint8_t> &p_ciphertext, const Vector<uint8_t> &p_message, bool p_valid, bool p_try_pub_key) {
178 Ref<CryptoKey> priv = load_key(p_in_priv_path, p_try_pub_key);
179 Ref<Crypto> c(memnew(CryptoMbedTLS));
180 REQUIRE(c.is_valid());
181 Vector<uint8_t> plaintext = c->decrypt(priv, p_ciphertext);
182 CHECK_EQ(equals(plaintext, p_message), p_valid);
183}
184
185void crypto_resource_format_loader_test(const String &p_path, Error p_expected) {
186 Ref<ResourceFormatLoaderCrypto> loader;
187 const String el = p_path.get_extension().to_lower();
188 loader.instantiate();
189 Error err = ERR_BUG;
190 Ref<Resource> res = loader->load(p_path, "", &err);
191 CHECK_EQ(err, p_expected);
192 CHECK_EQ(p_expected == OK, res.is_valid());
193 if (res.is_null()) {
194 return;
195 }
196 if (el == "pub" || el == "key") {
197 CHECK_EQ(res->get_class(), "CryptoKey");
198 CryptoKey *key = Object::cast_to<CryptoKey>(res.ptr());
199 CHECK(key != nullptr);
200 if (!key) {
201 return;
202 }
203 CHECK_EQ(key->is_public_only(), el == "pub");
204 } else if (el == "crt") {
205 CHECK_EQ(res->get_class(), "X509Certificate");
206 } else {
207 CHECK(false);
208 }
209}
210
211} // namespace TestCryptoMbedTLS