Crypto.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Crypto" inherits="Reference" version="3.4">
  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. [codeblock]
  10. extends Node
  11. var crypto = Crypto.new()
  12. var key = CryptoKey.new()
  13. var cert = X509Certificate.new()
  14. func _ready():
  15. # Generate new RSA key.
  16. key = crypto.generate_rsa(4096)
  17. # Generate new self-signed certificate with the given key.
  18. cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
  19. # Save key and certificate in the user folder.
  20. key.save("user://generated.key")
  21. cert.save("user://generated.crt")
  22. # Encryption
  23. var data = "Some data"
  24. var encrypted = crypto.encrypt(key, data.to_utf8())
  25. # Decryption
  26. var decrypted = crypto.decrypt(key, encrypted)
  27. # Signing
  28. var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
  29. # Verifying
  30. var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
  31. # Checks
  32. assert(verified)
  33. assert(data.to_utf8() == decrypted)
  34. [/codeblock]
  35. [b]Note:[/b] Not available in HTML5 exports.
  36. </description>
  37. <tutorials>
  38. </tutorials>
  39. <methods>
  40. <method name="constant_time_compare">
  41. <return type="bool">
  42. </return>
  43. <argument index="0" name="trusted" type="PoolByteArray">
  44. </argument>
  45. <argument index="1" name="received" type="PoolByteArray">
  46. </argument>
  47. <description>
  48. Compares two [PoolByteArray]s for equality without leaking timing information in order to prevent timing attacks.
  49. 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.
  50. </description>
  51. </method>
  52. <method name="decrypt">
  53. <return type="PoolByteArray">
  54. </return>
  55. <argument index="0" name="key" type="CryptoKey">
  56. </argument>
  57. <argument index="1" name="ciphertext" type="PoolByteArray">
  58. </argument>
  59. <description>
  60. Decrypt the given [code]ciphertext[/code] with the provided private [code]key[/code].
  61. [b]Note[/b]: The maximum size of accepted ciphertext is limited by the key size.
  62. </description>
  63. </method>
  64. <method name="encrypt">
  65. <return type="PoolByteArray">
  66. </return>
  67. <argument index="0" name="key" type="CryptoKey">
  68. </argument>
  69. <argument index="1" name="plaintext" type="PoolByteArray">
  70. </argument>
  71. <description>
  72. Encrypt the given [code]plaintext[/code] with the provided public [code]key[/code].
  73. [b]Note[/b]: The maximum size of accepted plaintext is limited by the key size.
  74. </description>
  75. </method>
  76. <method name="generate_random_bytes">
  77. <return type="PoolByteArray">
  78. </return>
  79. <argument index="0" name="size" type="int">
  80. </argument>
  81. <description>
  82. Generates a [PoolByteArray] of cryptographically secure random bytes with given [code]size[/code].
  83. </description>
  84. </method>
  85. <method name="generate_rsa">
  86. <return type="CryptoKey">
  87. </return>
  88. <argument index="0" name="size" type="int">
  89. </argument>
  90. <description>
  91. Generates an RSA [CryptoKey] that can be used for creating self-signed certificates and passed to [method StreamPeerSSL.accept_stream].
  92. </description>
  93. </method>
  94. <method name="generate_self_signed_certificate">
  95. <return type="X509Certificate">
  96. </return>
  97. <argument index="0" name="key" type="CryptoKey">
  98. </argument>
  99. <argument index="1" name="issuer_name" type="String" default="&quot;CN=myserver,O=myorganisation,C=IT&quot;">
  100. </argument>
  101. <argument index="2" name="not_before" type="String" default="&quot;20140101000000&quot;">
  102. </argument>
  103. <argument index="3" name="not_after" type="String" default="&quot;20340101000000&quot;">
  104. </argument>
  105. <description>
  106. 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).
  107. A small example to generate an RSA key and a X509 self-signed certificate.
  108. [codeblock]
  109. var crypto = Crypto.new()
  110. # Generate 4096 bits RSA key.
  111. var key = crypto.generate_rsa(4096)
  112. # Generate self-signed certificate using the given key.
  113. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  114. [/codeblock]
  115. </description>
  116. </method>
  117. <method name="hmac_digest">
  118. <return type="PoolByteArray">
  119. </return>
  120. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  121. </argument>
  122. <argument index="1" name="key" type="PoolByteArray">
  123. </argument>
  124. <argument index="2" name="msg" type="PoolByteArray">
  125. </argument>
  126. <description>
  127. 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.
  128. Currently, only [constant HashingContext.HASH_SHA256] and [constant HashingContext.HASH_SHA1] are supported.
  129. </description>
  130. </method>
  131. <method name="sign">
  132. <return type="PoolByteArray">
  133. </return>
  134. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  135. </argument>
  136. <argument index="1" name="hash" type="PoolByteArray">
  137. </argument>
  138. <argument index="2" name="key" type="CryptoKey">
  139. </argument>
  140. <description>
  141. Sign a given [code]hash[/code] of type [code]hash_type[/code] with the provided private [code]key[/code].
  142. </description>
  143. </method>
  144. <method name="verify">
  145. <return type="bool">
  146. </return>
  147. <argument index="0" name="hash_type" type="int" enum="HashingContext.HashType">
  148. </argument>
  149. <argument index="1" name="hash" type="PoolByteArray">
  150. </argument>
  151. <argument index="2" name="signature" type="PoolByteArray">
  152. </argument>
  153. <argument index="3" name="key" type="CryptoKey">
  154. </argument>
  155. <description>
  156. Verify that a given [code]signature[/code] for [code]hash[/code] of type [code]hash_type[/code] against the provided public [code]key[/code].
  157. </description>
  158. </method>
  159. </methods>
  160. <constants>
  161. </constants>
  162. </class>