AESContext.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="AESContext" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  3. <brief_description>
  4. Provides access to AES encryption/decryption of raw data.
  5. </brief_description>
  6. <description>
  7. 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.
  8. [codeblocks]
  9. [gdscript]
  10. extends Node
  11. var aes = AESContext.new()
  12. func _ready():
  13. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  14. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  15. # Encrypt ECB
  16. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer())
  17. var encrypted = aes.update(data.to_utf8_buffer())
  18. aes.finish()
  19. # Decrypt ECB
  20. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer())
  21. var decrypted = aes.update(encrypted)
  22. aes.finish()
  23. # Check ECB
  24. assert(decrypted == data.to_utf8_buffer())
  25. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  26. # Encrypt CBC
  27. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  28. encrypted = aes.update(data.to_utf8_buffer())
  29. aes.finish()
  30. # Decrypt CBC
  31. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  32. decrypted = aes.update(encrypted)
  33. aes.finish()
  34. # Check CBC
  35. assert(decrypted == data.to_utf8_buffer())
  36. [/gdscript]
  37. [csharp]
  38. using Godot;
  39. using System.Diagnostics;
  40. public partial class MyNode : Node
  41. {
  42. private AesContext _aes = new AesContext();
  43. public override void _Ready()
  44. {
  45. string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
  46. string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
  47. // Encrypt ECB
  48. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer());
  49. byte[] encrypted = _aes.Update(data.ToUtf8Buffer());
  50. _aes.Finish();
  51. // Decrypt ECB
  52. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer());
  53. byte[] decrypted = _aes.Update(encrypted);
  54. _aes.Finish();
  55. // Check ECB
  56. Debug.Assert(decrypted == data.ToUtf8Buffer());
  57. string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
  58. // Encrypt CBC
  59. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  60. encrypted = _aes.Update(data.ToUtf8Buffer());
  61. _aes.Finish();
  62. // Decrypt CBC
  63. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  64. decrypted = _aes.Update(encrypted);
  65. _aes.Finish();
  66. // Check CBC
  67. Debug.Assert(decrypted == data.ToUtf8Buffer());
  68. }
  69. }
  70. [/csharp]
  71. [/codeblocks]
  72. </description>
  73. <tutorials>
  74. </tutorials>
  75. <methods>
  76. <method name="finish">
  77. <return type="void" />
  78. <description>
  79. Close this AES context so it can be started again. See [method start].
  80. </description>
  81. </method>
  82. <method name="get_iv_state">
  83. <return type="PackedByteArray" />
  84. <description>
  85. Get the current IV state for this context (IV gets updated when calling [method update]). You normally don't need this function.
  86. [b]Note:[/b] This function only makes sense when the context is started with [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
  87. </description>
  88. </method>
  89. <method name="start">
  90. <return type="int" enum="Error" />
  91. <param index="0" name="mode" type="int" enum="AESContext.Mode" />
  92. <param index="1" name="key" type="PackedByteArray" />
  93. <param index="2" name="iv" type="PackedByteArray" default="PackedByteArray()" />
  94. <description>
  95. 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].
  96. </description>
  97. </method>
  98. <method name="update">
  99. <return type="PackedByteArray" />
  100. <param index="0" name="src" type="PackedByteArray" />
  101. <description>
  102. 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.
  103. [b]Note:[/b] The size of [param src] must be a multiple of 16. Apply some padding if needed.
  104. </description>
  105. </method>
  106. </methods>
  107. <constants>
  108. <constant name="MODE_ECB_ENCRYPT" value="0" enum="Mode">
  109. AES electronic codebook encryption mode.
  110. </constant>
  111. <constant name="MODE_ECB_DECRYPT" value="1" enum="Mode">
  112. AES electronic codebook decryption mode.
  113. </constant>
  114. <constant name="MODE_CBC_ENCRYPT" value="2" enum="Mode">
  115. AES cipher blocker chaining encryption mode.
  116. </constant>
  117. <constant name="MODE_CBC_DECRYPT" value="3" enum="Mode">
  118. AES cipher blocker chaining decryption mode.
  119. </constant>
  120. <constant name="MODE_MAX" value="4" enum="Mode">
  121. Maximum value for the mode enum.
  122. </constant>
  123. </constants>
  124. </class>