AESContext.xml 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="AESContext" inherits="Reference" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  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. [codeblock]
  9. extends Node
  10. var aes = AESContext.new()
  11. func _ready():
  12. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  13. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  14. # Encrypt ECB
  15. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8())
  16. var encrypted = aes.update(data.to_utf8())
  17. aes.finish()
  18. # Decrypt ECB
  19. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
  20. var decrypted = aes.update(encrypted)
  21. aes.finish()
  22. # Check ECB
  23. assert(decrypted == data.to_utf8())
  24. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  25. # Encrypt CBC
  26. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8(), iv.to_utf8())
  27. encrypted = aes.update(data.to_utf8())
  28. aes.finish()
  29. # Decrypt CBC
  30. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
  31. decrypted = aes.update(encrypted)
  32. aes.finish()
  33. # Check CBC
  34. assert(decrypted == data.to_utf8())
  35. [/codeblock]
  36. </description>
  37. <tutorials>
  38. </tutorials>
  39. <methods>
  40. <method name="finish">
  41. <return type="void" />
  42. <description>
  43. Close this AES context so it can be started again. See [method start].
  44. </description>
  45. </method>
  46. <method name="get_iv_state">
  47. <return type="PoolByteArray" />
  48. <description>
  49. Get the current IV state for this context (IV gets updated when calling [method update]). You normally don't need this function.
  50. [b]Note:[/b] This function only makes sense when the context is started with [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
  51. </description>
  52. </method>
  53. <method name="start">
  54. <return type="int" enum="Error" />
  55. <argument index="0" name="mode" type="int" enum="AESContext.Mode" />
  56. <argument index="1" name="key" type="PoolByteArray" />
  57. <argument index="2" name="iv" type="PoolByteArray" default="PoolByteArray( )" />
  58. <description>
  59. 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].
  60. </description>
  61. </method>
  62. <method name="update">
  63. <return type="PoolByteArray" />
  64. <argument index="0" name="src" type="PoolByteArray" />
  65. <description>
  66. 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.
  67. [b]Note:[/b] The size of [code]src[/code] must be a multiple of 16. Apply some padding if needed.
  68. </description>
  69. </method>
  70. </methods>
  71. <constants>
  72. <constant name="MODE_ECB_ENCRYPT" value="0" enum="Mode">
  73. AES electronic codebook encryption mode.
  74. </constant>
  75. <constant name="MODE_ECB_DECRYPT" value="1" enum="Mode">
  76. AES electronic codebook decryption mode.
  77. </constant>
  78. <constant name="MODE_CBC_ENCRYPT" value="2" enum="Mode">
  79. AES cipher blocker chaining encryption mode.
  80. </constant>
  81. <constant name="MODE_CBC_DECRYPT" value="3" enum="Mode">
  82. AES cipher blocker chaining decryption mode.
  83. </constant>
  84. <constant name="MODE_MAX" value="4" enum="Mode">
  85. Maximum value for the mode enum.
  86. </constant>
  87. </constants>
  88. </class>