AESContext.xml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="AESContext" inherits="Reference" version="3.4">
  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. </return>
  43. <description>
  44. Close this AES context so it can be started again. See [method start].
  45. </description>
  46. </method>
  47. <method name="get_iv_state">
  48. <return type="PoolByteArray">
  49. </return>
  50. <description>
  51. Get the current IV state for this context (IV gets updated when calling [method update]). You normally don't need this function.
  52. Note: This function only makes sense when the context is started with [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT].
  53. </description>
  54. </method>
  55. <method name="start">
  56. <return type="int" enum="Error">
  57. </return>
  58. <argument index="0" name="mode" type="int" enum="AESContext.Mode">
  59. </argument>
  60. <argument index="1" name="key" type="PoolByteArray">
  61. </argument>
  62. <argument index="2" name="iv" type="PoolByteArray" default="PoolByteArray( )">
  63. </argument>
  64. <description>
  65. 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].
  66. </description>
  67. </method>
  68. <method name="update">
  69. <return type="PoolByteArray">
  70. </return>
  71. <argument index="0" name="src" type="PoolByteArray">
  72. </argument>
  73. <description>
  74. 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.
  75. Note: The size of [code]src[/code] must be a multiple of 16. Apply some padding if needed.
  76. </description>
  77. </method>
  78. </methods>
  79. <constants>
  80. <constant name="MODE_ECB_ENCRYPT" value="0" enum="Mode">
  81. AES electronic codebook encryption mode.
  82. </constant>
  83. <constant name="MODE_ECB_DECRYPT" value="1" enum="Mode">
  84. AES electronic codebook decryption mode.
  85. </constant>
  86. <constant name="MODE_CBC_ENCRYPT" value="2" enum="Mode">
  87. AES cipher blocker chaining encryption mode.
  88. </constant>
  89. <constant name="MODE_CBC_DECRYPT" value="3" enum="Mode">
  90. AES cipher blocker chaining decryption mode.
  91. </constant>
  92. <constant name="MODE_MAX" value="4" enum="Mode">
  93. Maximum value for the mode enum.
  94. </constant>
  95. </constants>
  96. </class>