-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backport HMACContext to 3.x #48869
Merged
+416
−4
Merged
Backport HMACContext to 3.x #48869
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="HMACContext" inherits="Reference" version="3.4"> | ||
<brief_description> | ||
Used to create an HMAC for a message using a key. | ||
</brief_description> | ||
<description> | ||
The HMACContext class is useful for advanced HMAC use cases, such as streaming the message as it supports creating the message over time rather than providing it all at once. | ||
[codeblock] | ||
extends Node | ||
var ctx = HMACContext.new() | ||
|
||
func _ready(): | ||
var key = "supersecret".to_utf8() | ||
var err = ctx.start(HashingContext.HASH_SHA256, key) | ||
assert(err == OK) | ||
var msg1 = "this is ".to_utf8() | ||
var msg2 = "vewy vewy secret".to_utf8() | ||
err = ctx.update(msg1) | ||
assert(err == OK) | ||
err = ctx.update(msg2) | ||
assert(err == OK) | ||
var hmac = ctx.finish() | ||
print(hmac.hex_encode()) | ||
[/codeblock] | ||
And in C# we can use the following. | ||
[codeblock] | ||
using Godot; | ||
using System; | ||
using System.Diagnostics; | ||
|
||
public class CryptoNode : Node | ||
{ | ||
private HMACContext ctx = new HMACContext(); | ||
public override void _Ready() | ||
{ | ||
PoolByteArray key = String("supersecret").to_utf8(); | ||
Error err = ctx.Start(HashingContext.HASH_SHA256, key); | ||
GD.Assert(err == OK); | ||
PoolByteArray msg1 = String("this is ").to_utf8(); | ||
PoolByteArray msg2 = String("vewy vew secret").to_utf8(); | ||
err = ctx.Update(msg1); | ||
GD.Assert(err == OK); | ||
err = ctx.Update(msg2); | ||
GD.Assert(err == OK); | ||
PoolByteArray hmac = ctx.Finish(); | ||
GD.Print(hmac.HexEncode()); | ||
} | ||
} | ||
[/codeblock] | ||
[b]Note:[/b] Not available in HTML5 exports. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="finish"> | ||
<return type="PoolByteArray"> | ||
</return> | ||
<description> | ||
Returns the resulting HMAC. If the HMAC failed, an empty [PoolByteArray] is returned. | ||
</description> | ||
</method> | ||
<method name="start"> | ||
<return type="int" enum="Error"> | ||
</return> | ||
<argument index="0" name="hash_type" type="int" enum="HashingContext.HashType"> | ||
</argument> | ||
<argument index="1" name="key" type="PoolByteArray"> | ||
</argument> | ||
<description> | ||
Initializes the HMACContext. This method cannot be called again on the same HMACContext until [method finish] has been called. | ||
</description> | ||
</method> | ||
<method name="update"> | ||
<return type="int" enum="Error"> | ||
</return> | ||
<argument index="0" name="data" type="PoolByteArray"> | ||
</argument> | ||
<description> | ||
Updates the message to be HMACed. This can be called multiple times before [method finish] is called to append [code]data[/code] to the message, but cannot be called until [method start] has been called. | ||
</description> | ||
</method> | ||
</methods> | ||
<constants> | ||
</constants> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/*************************************************************************/ | ||
/* test_crypto.cpp */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#include "core/crypto/crypto.h" | ||
#include "core/os/os.h" | ||
|
||
namespace TestCrypto { | ||
|
||
class _MockCrypto : public Crypto { | ||
virtual PoolByteArray generate_random_bytes(int p_bytes) { return PoolByteArray(); } | ||
virtual Ref<CryptoKey> generate_rsa(int p_bytes) { return nullptr; } | ||
virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) { return nullptr; } | ||
|
||
virtual Vector<uint8_t> sign(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Ref<CryptoKey> p_key) { return Vector<uint8_t>(); } | ||
virtual bool verify(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Vector<uint8_t> p_signature, Ref<CryptoKey> p_key) { return false; } | ||
virtual Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_plaintext) { return Vector<uint8_t>(); } | ||
virtual Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_ciphertext) { return Vector<uint8_t>(); } | ||
virtual PoolByteArray hmac_digest(HashingContext::HashType p_hash_type, PoolByteArray p_key, PoolByteArray p_msg) { return PoolByteArray(); } | ||
}; | ||
|
||
PoolByteArray raw_to_pba(const uint8_t *arr, size_t len) { | ||
PoolByteArray pba; | ||
pba.resize(len); | ||
for (size_t i = 0; i < len; i++) { | ||
pba.set(i, arr[i]); | ||
} | ||
return pba; | ||
} | ||
|
||
bool test_PoolByteArray_constant_time_compare() { | ||
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 }; | ||
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 }; | ||
PoolByteArray p1 = raw_to_pba(hm1, sizeof(hm1) / sizeof(hm1[0])); | ||
PoolByteArray p2 = raw_to_pba(hm2, sizeof(hm2) / sizeof(hm2[0])); | ||
_MockCrypto crypto; | ||
bool equal = crypto.constant_time_compare(p1, p1); | ||
bool ok = true; | ||
ok = ok && equal; | ||
equal = crypto.constant_time_compare(p1, p2); | ||
ok = ok && !equal; | ||
return ok; | ||
} | ||
|
||
typedef bool (*TestFunc)(); | ||
|
||
TestFunc test_funcs[] = { | ||
test_PoolByteArray_constant_time_compare, | ||
nullptr | ||
}; | ||
|
||
MainLoop *test() { | ||
int count = 0; | ||
int passed = 0; | ||
|
||
while (true) { | ||
if (!test_funcs[count]) { | ||
break; | ||
} | ||
bool pass = test_funcs[count](); | ||
if (pass) { | ||
passed++; | ||
} | ||
OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED"); | ||
|
||
count++; | ||
} | ||
OS::get_singleton()->print("\n"); | ||
OS::get_singleton()->print("Passed %i of %i tests\n", passed, count); | ||
return nullptr; | ||
} | ||
|
||
} // namespace TestCrypto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*************************************************************************/ | ||
/* test_crypto.h */ | ||
/*************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/*************************************************************************/ | ||
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ | ||
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/*************************************************************************/ | ||
|
||
#ifndef TEST_CRYPTO_H | ||
#define TEST_CRYPTO_H | ||
|
||
#include "core/os/main_loop.h" | ||
|
||
namespace TestCrypto { | ||
|
||
MainLoop *test(); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the test pass when compiling with
module_mbedtls_enabled=no
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I compiled just now using:
And tests are passing, is that correct?