Crypto.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Crypto" inherits="RefCounted" version="4.0">
  3. <brief_description>
  4. Access to advanced cryptographic functionalities.
  5. </brief_description>
  6. <description>
  7. The Crypto class allows you to access some more advanced cryptographic functionalities in Godot.
  8. For now, this includes generating cryptographically secure random bytes, RSA keys and self-signed X509 certificates generation, asymmetric key encryption/decryption, and signing/verification.
  9. [codeblocks]
  10. [gdscript]
  11. extends Node
  12. var crypto = Crypto.new()
  13. var key = CryptoKey.new()
  14. var cert = X509Certificate.new()
  15. func _ready():
  16. # Generate new RSA key.
  17. key = crypto.generate_rsa(4096)
  18. # Generate new self-signed certificate with the given key.
  19. cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
  20. # Save key and certificate in the user folder.
  21. key.save("user://generated.key")
  22. cert.save("user://generated.crt")
  23. # Encryption
  24. var data = "Some data"
  25. var encrypted = crypto.encrypt(key, data.to_utf8())
  26. # Decryption
  27. var decrypted = crypto.decrypt(key, encrypted)
  28. # Signing
  29. var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
  30. # Verifying
  31. var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
  32. # Checks
  33. assert(verified)
  34. assert(data.to_utf8() == decrypted)
  35. [/gdscript]
  36. [csharp]
  37. using Godot;
  38. using System;
  39. using System.Diagnostics;
  40. public class CryptoNode : Node
  41. {
  42. public Crypto Crypto = new Crypto();
  43. public CryptoKey Key = new CryptoKey();
  44. public X509Certificate Cert = new X509Certificate();
  45. public override void _Ready()
  46. {
  47. // Generate new RSA key.
  48. Key = Crypto.GenerateRsa(4096);
  49. // Generate new self-signed certificate with the given key.
  50. Cert = Crypto.GenerateSelfSignedCertificate(Key, "CN=mydomain.com,O=My Game Company,C=IT");
  51. // Save key and certificate in the user folder.
  52. Key.Save("user://generated.key");
  53. Cert.Save("user://generated.crt");
  54. // Encryption
  55. string data = "Some data";
  56. byte[] encrypted = Crypto.Encrypt(Key, data.ToUTF8());
  57. // Decryption
  58. byte[] decrypted = Crypto.Decrypt(Key, encrypted);
  59. // Signing
  60. byte[] signature = Crypto.Sign(HashingContext.HashType.Sha256, Data.SHA256Buffer(), Key);
  61. // Verifying
  62. bool verified = Crypto.Verify(HashingContext.HashType.Sha256, Data.SHA256Buffer(), signature, Key);
  63. // Checks
  64. Debug.Assert(verified);
  65. Debug.Assert(data.ToUTF8() == decrypted);
  66. }
  67. }
  68. [/csharp]
  69. [/codeblocks]
  70. [b]Note:[/b] Not available in HTML5 exports.
  71. </description>
  72. <tutorials>
  73. </tutorials>
  74. <methods>
  75. <method name="constant_time_compare">
  76. <return type="bool" />
  77. <argument index="0" name="trusted" type="PackedByteArray" />
  78. <argument index="1" name="received" type="PackedByteArray" />
  79. <description>
  80. Compares two [PackedByteArray]s for equality without leaking timing information in order to prevent timing attacks.
  81. See [url=https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy]this blog post[/url] for more information.
  82. </description>
  83. </method>
  84. <method name="decrypt">
  85. <return type="PackedByteArray" />
  86. <argument index="0" name="key" type="CryptoKey" />
  87. <argument index="1" name="ciphertext" type="PackedByteArray" />
  88. <description>
  89. Decrypt the given [code]ciphertext[/code] with the provided private [code]key[/code].
  90. [b]Note:[/b] The maximum size of accepted ciphertext is limited by the key size.
  91. </description>
  92. </method>
  93. <method name="encrypt">
  94. <return type="PackedByteArray" />
  95. <argument index="0" name="key" type="CryptoKey" />
  96. <argument index="1" name="plaintext" type="PackedByteArray" />
  97. <description>
  98. Encrypt the given [code]plaintext[/code] with the provided public [code]key[/code].
  99. [b]Note:[/b] The maximum size of accepted plaintext is limited by the key size.
  100. </description>
  101. </method>
  102. <method name="generate_random_bytes">
  103. <return type="PackedByteArray" />
  104. <argument index="0" name="size" type="int" />
  105. <description>
  106. Generates a [PackedByteArray] of cryptographically secure random bytes with given [code]size[/code].
  107. </description>
  108. </method>
  109. <method name="generate_rsa">
  110. <return type="CryptoKey" />
  111. <argument index="0" name="size" type="int" />
  112. <description>
  113. Generates an RSA [CryptoKey] that can be used for creating self-signed certificates and passed to [method StreamPeerSSL.accept_stream].
  114. </description>
  115. </method>
  116. <method name="generate_self_signed_certificate">
  117. <return type="X509Certificate" />
  118. <argument index="0" name="key" type="CryptoKey" />
  119. <argument index="1" name="issuer_name" type="String" default="&quot;CN=myserver,O=myorganisation,C=IT&quot;" />
  120. <argument index="2" name="not_before" type="String" default="&quot;20140101000000&quot;" />
  121. <argument index="3" name="not_after" type="String" default="&quot;20340101000000&quot;" />
  122. <description>
  123. Generates a self-signed [X509Certificate] from the given [CryptoKey] and [code]issuer_name[/code]. The certificate validity will be defined by [code]not_before[/code] and [code]not_after[/code] (first valid date and last valid date). The [code]issuer_name[/code] must contain at least "CN=" (common name, i.e. the domain name), "O=" (organization, i.e. your company name), "C=" (country, i.e. 2 lettered ISO-3166 code of the country the organization is based in).
  124. A small example to generate an RSA key and a X509 self-signed certificate.
  125. [codeblocks]
  126. [gdscript]
  127. var crypto = Crypto.new()
  128. # Generate 4096 bits RSA key.
  129. var key = crypto.generate_rsa(4096)
  130. # Generate self-signed certificate using the given key.
  131. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  132. [/gdscript]
  133. [csharp]
  134. var crypto = new Crypto();
  135. // Generate 4096 bits RSA key.
  136. CryptoKey key = crypto.GenerateRsa(4096);
  137. // Generate self-signed certificate using the given key.
  138. X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
  139. [/csharp]
  140. [/codeblocks]
  141. </description>
  142. </method>
  143. <method name="hmac_digest">
  144. <return type="PackedByteArray" />
  145. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType" />
  146. <argument index="1" name="key" type="PackedByteArray" />
  147. <argument index="2" name="msg" type="PackedByteArray" />
  148. <description>
  149. Generates an [url=https://en.wikipedia.org/wiki/HMAC]HMAC[/url] digest of [code]msg[/code] using [code]key[/code]. The [code]hash_type[/code] parameter is the hashing algorithm that is used for the inner and outer hashes.
  150. Currently, only [constant HashingContext.HASH_SHA256] and [constant HashingContext.HASH_SHA1] are supported.
  151. </description>
  152. </method>
  153. <method name="sign">
  154. <return type="PackedByteArray" />
  155. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType" />
  156. <argument index="1" name="hash" type="PackedByteArray" />
  157. <argument index="2" name="key" type="CryptoKey" />
  158. <description>
  159. Sign a given [code]hash[/code] of type [code]hash_type[/code] with the provided private [code]key[/code].
  160. </description>
  161. </method>
  162. <method name="verify">
  163. <return type="bool" />
  164. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType" />
  165. <argument index="1" name="hash" type="PackedByteArray" />
  166. <argument index="2" name="signature" type="PackedByteArray" />
  167. <argument index="3" name="key" type="CryptoKey" />
  168. <description>
  169. Verify that a given [code]signature[/code] for [code]hash[/code] of type [code]hash_type[/code] against the provided public [code]key[/code].
  170. </description>
  171. </method>
  172. </methods>
  173. </class>