AESContext.xml 5.0 KB

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