Crypto.xml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Crypto" inherits="Reference" 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. </return>
  78. <argument index="0" name="trusted" type="PackedByteArray">
  79. </argument>
  80. <argument index="1" name="received" type="PackedByteArray">
  81. </argument>
  82. <description>
  83. Compares two [PackedByteArray]s for equality without leaking timing information in order to prevent timing attacks.
  84. 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.
  85. </description>
  86. </method>
  87. <method name="decrypt">
  88. <return type="PackedByteArray">
  89. </return>
  90. <argument index="0" name="key" type="CryptoKey">
  91. </argument>
  92. <argument index="1" name="ciphertext" type="PackedByteArray">
  93. </argument>
  94. <description>
  95. Decrypt the given [code]ciphertext[/code] with the provided private [code]key[/code].
  96. [b]Note[/b]: The maximum size of accepted ciphertext is limited by the key size.
  97. </description>
  98. </method>
  99. <method name="encrypt">
  100. <return type="PackedByteArray">
  101. </return>
  102. <argument index="0" name="key" type="CryptoKey">
  103. </argument>
  104. <argument index="1" name="plaintext" type="PackedByteArray">
  105. </argument>
  106. <description>
  107. Encrypt the given [code]plaintext[/code] with the provided public [code]key[/code].
  108. [b]Note[/b]: The maximum size of accepted plaintext is limited by the key size.
  109. </description>
  110. </method>
  111. <method name="generate_random_bytes">
  112. <return type="PackedByteArray">
  113. </return>
  114. <argument index="0" name="size" type="int">
  115. </argument>
  116. <description>
  117. Generates a [PackedByteArray] of cryptographically secure random bytes with given [code]size[/code].
  118. </description>
  119. </method>
  120. <method name="generate_rsa">
  121. <return type="CryptoKey">
  122. </return>
  123. <argument index="0" name="size" type="int">
  124. </argument>
  125. <description>
  126. Generates an RSA [CryptoKey] that can be used for creating self-signed certificates and passed to [method StreamPeerSSL.accept_stream].
  127. </description>
  128. </method>
  129. <method name="generate_self_signed_certificate">
  130. <return type="X509Certificate">
  131. </return>
  132. <argument index="0" name="key" type="CryptoKey">
  133. </argument>
  134. <argument index="1" name="issuer_name" type="String" default="&quot;CN=myserver,O=myorganisation,C=IT&quot;">
  135. </argument>
  136. <argument index="2" name="not_before" type="String" default="&quot;20140101000000&quot;">
  137. </argument>
  138. <argument index="3" name="not_after" type="String" default="&quot;20340101000000&quot;">
  139. </argument>
  140. <description>
  141. 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).
  142. A small example to generate an RSA key and a X509 self-signed certificate.
  143. [codeblocks]
  144. [gdscript]
  145. var crypto = Crypto.new()
  146. # Generate 4096 bits RSA key.
  147. var key = crypto.generate_rsa(4096)
  148. # Generate self-signed certificate using the given key.
  149. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  150. [/gdscript]
  151. [csharp]
  152. var crypto = new Crypto();
  153. // Generate 4096 bits RSA key.
  154. CryptoKey key = crypto.GenerateRsa(4096);
  155. // Generate self-signed certificate using the given key.
  156. X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
  157. [/csharp]
  158. [/codeblocks]
  159. </description>
  160. </method>
  161. <method name="hmac_digest">
  162. <return type="PackedByteArray">
  163. </return>
  164. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  165. </argument>
  166. <argument index="1" name="key" type="PackedByteArray">
  167. </argument>
  168. <argument index="2" name="msg" type="PackedByteArray">
  169. </argument>
  170. <description>
  171. 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.
  172. Currently, only [constant HashingContext.HASH_SHA256] and [constant HashingContext.HASH_SHA1] are supported.
  173. </description>
  174. </method>
  175. <method name="sign">
  176. <return type="PackedByteArray">
  177. </return>
  178. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  179. </argument>
  180. <argument index="1" name="hash" type="PackedByteArray">
  181. </argument>
  182. <argument index="2" name="key" type="CryptoKey">
  183. </argument>
  184. <description>
  185. Sign a given [code]hash[/code] of type [code]hash_type[/code] with the provided private [code]key[/code].
  186. </description>
  187. </method>
  188. <method name="verify">
  189. <return type="bool">
  190. </return>
  191. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  192. </argument>
  193. <argument index="1" name="hash" type="PackedByteArray">
  194. </argument>
  195. <argument index="2" name="signature" type="PackedByteArray">
  196. </argument>
  197. <argument index="3" name="key" type="CryptoKey">
  198. </argument>
  199. <description>
  200. Verify that a given [code]signature[/code] for [code]hash[/code] of type [code]hash_type[/code] against the provided public [code]key[/code].
  201. </description>
  202. </method>
  203. </methods>
  204. <constants>
  205. </constants>
  206. </class>