| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="Crypto" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Provides access to advanced cryptographic functionalities. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | The Crypto class provides access to advanced cryptographic functionalities. |
| 8 | Currently, this includes asymmetric key encryption/decryption, signing/verification, and generating cryptographically secure random bytes, RSA keys, HMAC digests, and self-signed [X509Certificate]s. |
| 9 | [codeblocks] |
| 10 | [gdscript] |
| 11 | var crypto = Crypto.new() |
| 12 | |
| 13 | # Generate new RSA key. |
| 14 | var key = crypto.generate_rsa(4096) |
| 15 | |
| 16 | # Generate new self-signed certificate with the given key. |
| 17 | var cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT") |
| 18 | |
| 19 | # Save key and certificate in the user folder. |
| 20 | key.save("user://generated.key") |
| 21 | cert.save("user://generated.crt") |
| 22 | |
| 23 | # Encryption |
| 24 | var data = "Some data" |
| 25 | var encrypted = crypto.encrypt(key, data.to_utf8_buffer()) |
| 26 | |
| 27 | # Decryption |
| 28 | var decrypted = crypto.decrypt(key, encrypted) |
| 29 | |
| 30 | # Signing |
| 31 | var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key) |
| 32 | |
| 33 | # Verifying |
| 34 | var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key) |
| 35 | |
| 36 | # Checks |
| 37 | assert(verified) |
| 38 | assert(data.to_utf8_buffer() == decrypted) |
| 39 | [/gdscript] |
| 40 | [csharp] |
| 41 | using Godot; |
| 42 | using System.Diagnostics; |
| 43 | |
| 44 | Crypto crypto = new Crypto(); |
| 45 | |
| 46 | // Generate new RSA key. |
| 47 | CryptoKey key = crypto.GenerateRsa(4096); |
| 48 | |
| 49 | // Generate new self-signed certificate with the given key. |
| 50 | X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT"); |
| 51 | |
| 52 | // Save key and certificate in the user folder. |
| 53 | key.Save("user://generated.key"); |
| 54 | cert.Save("user://generated.crt"); |
| 55 | |
| 56 | // Encryption |
| 57 | string data = "Some data"; |
| 58 | byte[] encrypted = crypto.Encrypt(key, data.ToUtf8Buffer()); |
| 59 | |
| 60 | // Decryption |
| 61 | byte[] decrypted = crypto.Decrypt(key, encrypted); |
| 62 | |
| 63 | // Signing |
| 64 | byte[] signature = crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), key); |
| 65 | |
| 66 | // Verifying |
| 67 | bool verified = crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, key); |
| 68 | |
| 69 | // Checks |
| 70 | Debug.Assert(verified); |
| 71 | Debug.Assert(data.ToUtf8Buffer() == decrypted); |
| 72 | [/csharp] |
| 73 | [/codeblocks] |
| 74 | </description> |
| 75 | <tutorials> |
| 76 | </tutorials> |
| 77 | <methods> |
| 78 | <method name="constant_time_compare"> |
| 79 | <return type="bool" /> |
| 80 | <param index="0" name="trusted" type="PackedByteArray" /> |
| 81 | <param index="1" name="received" type="PackedByteArray" /> |
| 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 | <param index="0" name="key" type="CryptoKey" /> |
| 90 | <param index="1" name="ciphertext" type="PackedByteArray" /> |
| 91 | <description> |
| 92 | Decrypt the given [param ciphertext] with the provided private [param key]. |
| 93 | [b]Note:[/b] The maximum size of accepted ciphertext is limited by the key size. |
| 94 | </description> |
| 95 | </method> |
| 96 | <method name="encrypt"> |
| 97 | <return type="PackedByteArray" /> |
| 98 | <param index="0" name="key" type="CryptoKey" /> |
| 99 | <param index="1" name="plaintext" type="PackedByteArray" /> |
| 100 | <description> |
| 101 | Encrypt the given [param plaintext] with the provided public [param key]. |
| 102 | [b]Note:[/b] The maximum size of accepted plaintext is limited by the key size. |
| 103 | </description> |
| 104 | </method> |
| 105 | <method name="generate_random_bytes"> |
| 106 | <return type="PackedByteArray" /> |
| 107 | <param index="0" name="size" type="int" /> |
| 108 | <description> |
| 109 | Generates a [PackedByteArray] of cryptographically secure random bytes with given [param size]. |
| 110 | </description> |
| 111 | </method> |
| 112 | <method name="generate_rsa"> |
| 113 | <return type="CryptoKey" /> |
| 114 | <param index="0" name="size" type="int" /> |
| 115 | <description> |
| 116 | Generates an RSA [CryptoKey] that can be used for creating self-signed certificates and passed to [method StreamPeerTLS.accept_stream]. |
| 117 | </description> |
| 118 | </method> |
| 119 | <method name="generate_self_signed_certificate"> |
| 120 | <return type="X509Certificate" /> |
| 121 | <param index="0" name="key" type="CryptoKey" /> |
| 122 | <param index="1" name="issuer_name" type="String" default=""CN=myserver,O=myorganisation,C=IT"" /> |
| 123 | <param index="2" name="not_before" type="String" default=""20140101000000"" /> |
| 124 | <param index="3" name="not_after" type="String" default=""20340101000000"" /> |
| 125 | <description> |
| 126 | Generates a self-signed [X509Certificate] from the given [CryptoKey] and [param issuer_name]. The certificate validity will be defined by [param not_before] and [param not_after] (first valid date and last valid date). The [param issuer_name] 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). |
| 127 | A small example to generate an RSA key and an X509 self-signed certificate. |
| 128 | [codeblocks] |
| 129 | [gdscript] |
| 130 | var crypto = Crypto.new() |
| 131 | # Generate 4096 bits RSA key. |
| 132 | var key = crypto.generate_rsa(4096) |
| 133 | # Generate self-signed certificate using the given key. |
| 134 | var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT") |
| 135 | [/gdscript] |
| 136 | [csharp] |
| 137 | var crypto = new Crypto(); |
| 138 | // Generate 4096 bits RSA key. |
| 139 | CryptoKey key = crypto.GenerateRsa(4096); |
| 140 | // Generate self-signed certificate using the given key. |
| 141 | X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT"); |
| 142 | [/csharp] |
| 143 | [/codeblocks] |
| 144 | </description> |
| 145 | </method> |
| 146 | <method name="hmac_digest"> |
| 147 | <return type="PackedByteArray" /> |
| 148 | <param index="0" name="hash_type" type="int" enum="HashingContext.HashType" /> |
| 149 | <param index="1" name="key" type="PackedByteArray" /> |
| 150 | <param index="2" name="msg" type="PackedByteArray" /> |
| 151 | <description> |
| 152 | Generates an [url=https://en.wikipedia.org/wiki/HMAC]HMAC[/url] digest of [param msg] using [param key]. The [param hash_type] parameter is the hashing algorithm that is used for the inner and outer hashes. |
| 153 | Currently, only [constant HashingContext.HASH_SHA256] and [constant HashingContext.HASH_SHA1] are supported. |
| 154 | </description> |
| 155 | </method> |
| 156 | <method name="sign"> |
| 157 | <return type="PackedByteArray" /> |
| 158 | <param index="0" name="hash_type" type="int" enum="HashingContext.HashType" /> |
| 159 | <param index="1" name="hash" type="PackedByteArray" /> |
| 160 | <param index="2" name="key" type="CryptoKey" /> |
| 161 | <description> |
| 162 | Sign a given [param hash] of type [param hash_type] with the provided private [param key]. |
| 163 | </description> |
| 164 | </method> |
| 165 | <method name="verify"> |
| 166 | <return type="bool" /> |
| 167 | <param index="0" name="hash_type" type="int" enum="HashingContext.HashType" /> |
| 168 | <param index="1" name="hash" type="PackedByteArray" /> |
| 169 | <param index="2" name="signature" type="PackedByteArray" /> |
| 170 | <param index="3" name="key" type="CryptoKey" /> |
| 171 | <description> |
| 172 | Verify that a given [param signature] for [param hash] of type [param hash_type] against the provided public [param key]. |
| 173 | </description> |
| 174 | </method> |
| 175 | </methods> |
| 176 | </class> |