2020-06-18 12:39:56 +02:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 10:08:05 +02:00
<class name= "AESContext" inherits= "RefCounted" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2020-06-18 12:39:56 +02:00
<brief_description >
2023-04-28 01:35:33 +02:00
Provides access to AES encryption/decryption of raw data.
2020-06-18 12:39:56 +02:00
</brief_description>
<description >
2023-04-28 01:35:33 +02:00
This class holds the context information required for encryption and decryption operations with AES (Advanced Encryption Standard). Both AES-ECB and AES-CBC modes are supported.
2020-07-31 16:07:26 +02:00
[codeblocks]
[gdscript]
2020-06-18 12:39:56 +02:00
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
2023-03-13 22:58:27 +01:00
aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer())
var encrypted = aes.update(data.to_utf8_buffer())
2020-06-18 12:39:56 +02:00
aes.finish()
# Decrypt ECB
2023-03-13 22:58:27 +01:00
aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer())
2020-06-18 12:39:56 +02:00
var decrypted = aes.update(encrypted)
aes.finish()
# Check ECB
2023-03-13 22:58:27 +01:00
assert(decrypted == data.to_utf8_buffer())
2020-06-18 12:39:56 +02:00
var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
# Encrypt CBC
2023-03-13 22:58:27 +01:00
aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
encrypted = aes.update(data.to_utf8_buffer())
2020-06-18 12:39:56 +02:00
aes.finish()
# Decrypt CBC
2023-03-13 22:58:27 +01:00
aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
2020-06-18 12:39:56 +02:00
decrypted = aes.update(encrypted)
aes.finish()
# Check CBC
2023-03-13 22:58:27 +01:00
assert(decrypted == data.to_utf8_buffer())
2020-07-31 16:07:26 +02:00
[/gdscript]
[csharp]
using Godot;
using System.Diagnostics;
2023-01-31 18:21:09 +01:00
public partial class MyNode : Node
2020-07-31 16:07:26 +02:00
{
2023-01-31 18:21:09 +01:00
private AesContext _aes = new AesContext();
2022-09-15 11:04:32 +02:00
2020-07-31 16:07:26 +02:00
public override void _Ready()
{
string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
// Encrypt ECB
2023-03-13 22:58:27 +01:00
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer());
byte[] encrypted = _aes.Update(data.ToUtf8Buffer());
2023-01-31 18:21:09 +01:00
_aes.Finish();
2020-07-31 16:07:26 +02:00
// Decrypt ECB
2023-03-13 22:58:27 +01:00
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer());
2023-01-31 18:21:09 +01:00
byte[] decrypted = _aes.Update(encrypted);
_aes.Finish();
2020-07-31 16:07:26 +02:00
// Check ECB
2023-03-13 22:58:27 +01:00
Debug.Assert(decrypted == data.ToUtf8Buffer());
2020-07-31 16:07:26 +02:00
string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
// Encrypt CBC
2023-03-13 22:58:27 +01:00
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
encrypted = _aes.Update(data.ToUtf8Buffer());
2023-01-31 18:21:09 +01:00
_aes.Finish();
2020-07-31 16:07:26 +02:00
// Decrypt CBC
2023-03-13 22:58:27 +01:00
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
2023-01-31 18:21:09 +01:00
decrypted = _aes.Update(encrypted);
_aes.Finish();
2020-07-31 16:07:26 +02:00
// Check CBC
2023-03-13 22:58:27 +01:00
Debug.Assert(decrypted == data.ToUtf8Buffer());
2020-07-31 16:07:26 +02:00
}
}
[/csharp]
[/codeblocks]
2020-06-18 12:39:56 +02:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "finish" >
2021-07-30 15:28:05 +02:00
<return type= "void" />
2020-06-18 12:39:56 +02:00
<description >
Close this AES context so it can be started again. See [method start].
</description>
</method>
<method name= "get_iv_state" >
2021-07-30 15:28:05 +02:00
<return type= "PackedByteArray" />
2020-06-18 12:39:56 +02:00
<description >
2020-07-16 18:54:15 +02:00
Get the current IV state for this context (IV gets updated when calling [method update]). You normally don't need this function.
2021-10-05 14:24:34 +02:00
[b]Note:[/b] This function only makes sense when the context is started with [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
2020-06-18 12:39:56 +02:00
</description>
</method>
<method name= "start" >
2021-07-30 15:28:05 +02:00
<return type= "int" enum= "Error" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "mode" type= "int" enum= "AESContext.Mode" />
<param index= "1" name= "key" type= "PackedByteArray" />
<param index= "2" name= "iv" type= "PackedByteArray" default= "PackedByteArray()" />
2020-06-18 12:39:56 +02:00
<description >
2022-08-12 19:51:01 +02:00
Start the AES context in the given [param mode]. A [param key] of either 16 or 32 bytes must always be provided, while an [param iv] (initialization vector) of exactly 16 bytes, is only needed when [param mode] is either [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
2020-06-18 12:39:56 +02:00
</description>
</method>
<method name= "update" >
2021-07-30 15:28:05 +02:00
<return type= "PackedByteArray" />
2022-08-06 20:11:48 +02:00
<param index= "0" name= "src" type= "PackedByteArray" />
2020-06-18 12:39:56 +02:00
<description >
2022-08-12 19:51:01 +02:00
Run the desired operation for this AES context. Will return a [PackedByteArray] containing the result of encrypting (or decrypting) the given [param src]. See [method start] for mode of operation.
[b]Note:[/b] The size of [param src] must be a multiple of 16. Apply some padding if needed.
2020-06-18 12:39:56 +02:00
</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>