forked from sascha/godot
Merge pull request #48144 from Faless/crypto/3.3_encryption_stable
commit
2d57df60f7
@ -0,0 +1,116 @@
|
||||
/*************************************************************************/
|
||||
/* aes_context.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/aes_context.h"
|
||||
|
||||
Error AESContext::start(Mode p_mode, PoolByteArray p_key, PoolByteArray p_iv) {
|
||||
ERR_FAIL_COND_V_MSG(mode != MODE_MAX, ERR_ALREADY_IN_USE, "AESContext already started. Call 'finish' before starting a new one.");
|
||||
ERR_FAIL_COND_V_MSG(p_mode < 0 || p_mode >= MODE_MAX, ERR_INVALID_PARAMETER, "Invalid mode requested.");
|
||||
// Key check.
|
||||
int key_bits = p_key.size() << 3;
|
||||
ERR_FAIL_COND_V_MSG(key_bits != 128 && key_bits != 256, ERR_INVALID_PARAMETER, "AES key must be either 16 or 32 bytes");
|
||||
// Initialization vector.
|
||||
if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_CBC_DECRYPT) {
|
||||
ERR_FAIL_COND_V_MSG(p_iv.size() != 16, ERR_INVALID_PARAMETER, "The initialization vector (IV) must be exactly 16 bytes.");
|
||||
iv.resize(0);
|
||||
iv.append_array(p_iv);
|
||||
}
|
||||
// Encryption/decryption key.
|
||||
if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_ECB_ENCRYPT) {
|
||||
ctx.set_encode_key(p_key.read().ptr(), key_bits);
|
||||
} else {
|
||||
ctx.set_decode_key(p_key.read().ptr(), key_bits);
|
||||
}
|
||||
mode = p_mode;
|
||||
return OK;
|
||||
}
|
||||
|
||||
PoolByteArray AESContext::update(PoolByteArray p_src) {
|
||||
ERR_FAIL_COND_V_MSG(mode < 0 || mode >= MODE_MAX, PoolByteArray(), "AESContext not started. Call 'start' before calling 'update'.");
|
||||
int len = p_src.size();
|
||||
ERR_FAIL_COND_V_MSG(len % 16, PoolByteArray(), "The number of bytes to be encrypted must be multiple of 16. Add padding if needed");
|
||||
PoolByteArray out;
|
||||
out.resize(len);
|
||||
const uint8_t *src_ptr = p_src.read().ptr();
|
||||
uint8_t *out_ptr = out.write().ptr();
|
||||
switch (mode) {
|
||||
case MODE_ECB_ENCRYPT: {
|
||||
for (int i = 0; i < len; i += 16) {
|
||||
Error err = ctx.encrypt_ecb(src_ptr + i, out_ptr + i);
|
||||
ERR_FAIL_COND_V(err != OK, PoolByteArray());
|
||||
}
|
||||
} break;
|
||||
case MODE_ECB_DECRYPT: {
|
||||
for (int i = 0; i < len; i += 16) {
|
||||
Error err = ctx.decrypt_ecb(src_ptr + i, out_ptr + i);
|
||||
ERR_FAIL_COND_V(err != OK, PoolByteArray());
|
||||
}
|
||||
} break;
|
||||
case MODE_CBC_ENCRYPT: {
|
||||
Error err = ctx.encrypt_cbc(len, iv.write().ptr(), p_src.read().ptr(), out.write().ptr());
|
||||
ERR_FAIL_COND_V(err != OK, PoolByteArray());
|
||||
} break;
|
||||
case MODE_CBC_DECRYPT: {
|
||||
Error err = ctx.decrypt_cbc(len, iv.write().ptr(), p_src.read().ptr(), out.write().ptr());
|
||||
ERR_FAIL_COND_V(err != OK, PoolByteArray());
|
||||
} break;
|
||||
default:
|
||||
ERR_FAIL_V_MSG(PoolByteArray(), "Bug!");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
PoolByteArray AESContext::get_iv_state() {
|
||||
ERR_FAIL_COND_V_MSG(mode != MODE_CBC_ENCRYPT && mode != MODE_CBC_DECRYPT, PoolByteArray(), "Calling 'get_iv_state' only makes sense when the context is started in CBC mode.");
|
||||
PoolByteArray out;
|
||||
out.append_array(iv);
|
||||
return out;
|
||||
}
|
||||
|
||||
void AESContext::finish() {
|
||||
mode = MODE_MAX;
|
||||
iv.resize(0);
|
||||
}
|
||||
|
||||
void AESContext::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("start", "mode", "key", "iv"), &AESContext::start, DEFVAL(PoolByteArray()));
|
||||
ClassDB::bind_method(D_METHOD("update", "src"), &AESContext::update);
|
||||
ClassDB::bind_method(D_METHOD("get_iv_state"), &AESContext::get_iv_state);
|
||||
ClassDB::bind_method(D_METHOD("finish"), &AESContext::finish);
|
||||
BIND_ENUM_CONSTANT(MODE_ECB_ENCRYPT);
|
||||
BIND_ENUM_CONSTANT(MODE_ECB_DECRYPT);
|
||||
BIND_ENUM_CONSTANT(MODE_CBC_ENCRYPT);
|
||||
BIND_ENUM_CONSTANT(MODE_CBC_DECRYPT);
|
||||
BIND_ENUM_CONSTANT(MODE_MAX);
|
||||
}
|
||||
|
||||
AESContext::AESContext() {
|
||||
mode = MODE_MAX;
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
/*************************************************************************/
|
||||
/* aes_context.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 AES_CONTEXT_H
|
||||
#define AES_CONTEXT_H
|
||||
|
||||
#include "core/crypto/crypto_core.h"
|
||||
#include "core/reference.h"
|
||||
|
||||
class AESContext : public Reference {
|
||||
GDCLASS(AESContext, Reference);
|
||||
|
||||
public:
|
||||
enum Mode {
|
||||
MODE_ECB_ENCRYPT,
|
||||
MODE_ECB_DECRYPT,
|
||||
MODE_CBC_ENCRYPT,
|
||||
MODE_CBC_DECRYPT,
|
||||
MODE_MAX
|
||||
};
|
||||
|
||||
private:
|
||||
Mode mode;
|
||||
CryptoCore::AESContext ctx;
|
||||
PoolByteArray iv;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
Error start(Mode p_mode, PoolByteArray p_key, PoolByteArray p_iv = PoolByteArray());
|
||||
PoolByteArray update(PoolByteArray p_src);
|
||||
PoolByteArray get_iv_state();
|
||||
void finish();
|
||||
|
||||
AESContext();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(AESContext::Mode);
|
||||
|
||||
#endif // AES_CONTEXT_H
|
||||
@ -0,0 +1,99 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="AESContext" inherits="Reference" version="3.2">
|
||||
<brief_description>
|
||||
Interface to low level AES encryption features.
|
||||
</brief_description>
|
||||
<description>
|
||||
This class provides access to AES encryption/decryption of raw data. Both AES-ECB and AES-CBC mode are supported.
|
||||
[codeblock]
|
||||
extends Node
|
||||
|
||||
var aes = AESContext.new()
|
||||
|
||||
func _ready():
|
||||
var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
|
||||
var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
|
||||
# Encrypt ECB
|
||||
aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8())
|
||||
var encrypted = aes.update(data.to_utf8())
|
||||
aes.finish()
|
||||
# Decrypt ECB
|
||||
aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
|
||||
var decrypted = aes.update(encrypted)
|
||||
aes.finish()
|
||||
# Check ECB
|
||||
assert(decrypted == data.to_utf8())
|
||||
|
||||
var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
|
||||
# Encrypt CBC
|
||||
aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8(), iv.to_utf8())
|
||||
encrypted = aes.update(data.to_utf8())
|
||||
aes.finish()
|
||||
# Decrypt CBC
|
||||
aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
|
||||
decrypted = aes.update(encrypted)
|
||||
aes.finish()
|
||||
# Check CBC
|
||||
assert(decrypted == data.to_utf8())
|
||||
[/codeblock]
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="finish">
|
||||
<return type="void">
|
||||
</return>
|
||||
<description>
|
||||
Close this AES context so it can be started again. See [method start].
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_iv_state">
|
||||
<return type="PoolByteArray">
|
||||
</return>
|
||||
<description>
|
||||
Get the current IV state for this context (IV gets updated when calling [method update]). You normally don't need this funciton.
|
||||
Note: This function only makes sense when the context is started with [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
|
||||
</description>
|
||||
</method>
|
||||
<method name="start">
|
||||
<return type="int" enum="Error">
|
||||
</return>
|
||||
<argument index="0" name="mode" type="int" enum="AESContext.Mode">
|
||||
</argument>
|
||||
<argument index="1" name="key" type="PoolByteArray">
|
||||
</argument>
|
||||
<argument index="2" name="iv" type="PoolByteArray" default="PoolByteArray( )">
|
||||
</argument>
|
||||
<description>
|
||||
Start the AES context in the given [code]mode[/code]. A [code]key[/code] of either 16 or 32 bytes must always be provided, while an [code]iv[/code] (initialization vector) of exactly 16 bytes, is only needed when [code]mode[/code] is either [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
|
||||
</description>
|
||||
</method>
|
||||
<method name="update">
|
||||
<return type="PoolByteArray">
|
||||
</return>
|
||||
<argument index="0" name="src" type="PoolByteArray">
|
||||
</argument>
|
||||
<description>
|
||||
Run the desired operation for this AES context. Will return a [PoolByteArray] containing the result of encrypting (or decrypting) the given [code]src[/code]. See [method start] for mode of operation.
|
||||
Note: The size of [code]src[/code] must be a multiple of 16. Apply some padding if needed.
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
<constants>
|
||||
<constant name="MODE_ECB_ENCRYPT" value="0" enum="Mode">
|
||||
AES electronic codebook encryption mode.
|
||||
</constant>
|
||||
<constant name="MODE_ECB_DECRYPT" value="1" enum="Mode">
|
||||
AES electronic codebook decryption mode.
|
||||
</constant>
|
||||
<constant name="MODE_CBC_ENCRYPT" value="2" enum="Mode">
|
||||
AES cipher blocker chaining encryption mode.
|
||||
</constant>
|
||||
<constant name="MODE_CBC_DECRYPT" value="3" enum="Mode">
|
||||
AES cipher blocker chaining decryption mode.
|
||||
</constant>
|
||||
<constant name="MODE_MAX" value="4" enum="Mode">
|
||||
Maximum value for the mode enum.
|
||||
</constant>
|
||||
</constants>
|
||||
</class>
|
||||
Loading…
Reference in New Issue