aes_context.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**************************************************************************/
  2. /* aes_context.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "core/crypto/aes_context.h"
  31. Error AESContext::start(Mode p_mode, PackedByteArray p_key, PackedByteArray p_iv) {
  32. ERR_FAIL_COND_V_MSG(mode != MODE_MAX, ERR_ALREADY_IN_USE, "AESContext already started. Call 'finish' before starting a new one.");
  33. ERR_FAIL_COND_V_MSG(p_mode < 0 || p_mode >= MODE_MAX, ERR_INVALID_PARAMETER, "Invalid mode requested.");
  34. // Key check.
  35. int key_bits = p_key.size() << 3;
  36. ERR_FAIL_COND_V_MSG(key_bits != 128 && key_bits != 256, ERR_INVALID_PARAMETER, "AES key must be either 16 or 32 bytes");
  37. // Initialization vector.
  38. if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_CBC_DECRYPT) {
  39. ERR_FAIL_COND_V_MSG(p_iv.size() != 16, ERR_INVALID_PARAMETER, "The initialization vector (IV) must be exactly 16 bytes.");
  40. iv.resize(0);
  41. iv.append_array(p_iv);
  42. }
  43. // Encryption/decryption key.
  44. if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_ECB_ENCRYPT) {
  45. ctx.set_encode_key(p_key.ptr(), key_bits);
  46. } else {
  47. ctx.set_decode_key(p_key.ptr(), key_bits);
  48. }
  49. mode = p_mode;
  50. return OK;
  51. }
  52. PackedByteArray AESContext::update(PackedByteArray p_src) {
  53. ERR_FAIL_COND_V_MSG(mode < 0 || mode >= MODE_MAX, PackedByteArray(), "AESContext not started. Call 'start' before calling 'update'.");
  54. int len = p_src.size();
  55. ERR_FAIL_COND_V_MSG(len % 16, PackedByteArray(), "The number of bytes to be encrypted must be multiple of 16. Add padding if needed");
  56. PackedByteArray out;
  57. out.resize(len);
  58. const uint8_t *src_ptr = p_src.ptr();
  59. uint8_t *out_ptr = out.ptrw();
  60. switch (mode) {
  61. case MODE_ECB_ENCRYPT: {
  62. for (int i = 0; i < len; i += 16) {
  63. Error err = ctx.encrypt_ecb(src_ptr + i, out_ptr + i);
  64. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  65. }
  66. } break;
  67. case MODE_ECB_DECRYPT: {
  68. for (int i = 0; i < len; i += 16) {
  69. Error err = ctx.decrypt_ecb(src_ptr + i, out_ptr + i);
  70. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  71. }
  72. } break;
  73. case MODE_CBC_ENCRYPT: {
  74. Error err = ctx.encrypt_cbc(len, iv.ptrw(), p_src.ptr(), out.ptrw());
  75. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  76. } break;
  77. case MODE_CBC_DECRYPT: {
  78. Error err = ctx.decrypt_cbc(len, iv.ptrw(), p_src.ptr(), out.ptrw());
  79. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  80. } break;
  81. default:
  82. ERR_FAIL_V_MSG(PackedByteArray(), "Bug!");
  83. }
  84. return out;
  85. }
  86. PackedByteArray AESContext::get_iv_state() {
  87. ERR_FAIL_COND_V_MSG(mode != MODE_CBC_ENCRYPT && mode != MODE_CBC_DECRYPT, PackedByteArray(), "Calling 'get_iv_state' only makes sense when the context is started in CBC mode.");
  88. PackedByteArray out;
  89. out.append_array(iv);
  90. return out;
  91. }
  92. void AESContext::finish() {
  93. mode = MODE_MAX;
  94. iv.resize(0);
  95. }
  96. void AESContext::_bind_methods() {
  97. ClassDB::bind_method(D_METHOD("start", "mode", "key", "iv"), &AESContext::start, DEFVAL(PackedByteArray()));
  98. ClassDB::bind_method(D_METHOD("update", "src"), &AESContext::update);
  99. ClassDB::bind_method(D_METHOD("get_iv_state"), &AESContext::get_iv_state);
  100. ClassDB::bind_method(D_METHOD("finish"), &AESContext::finish);
  101. BIND_ENUM_CONSTANT(MODE_ECB_ENCRYPT);
  102. BIND_ENUM_CONSTANT(MODE_ECB_DECRYPT);
  103. BIND_ENUM_CONSTANT(MODE_CBC_ENCRYPT);
  104. BIND_ENUM_CONSTANT(MODE_CBC_DECRYPT);
  105. BIND_ENUM_CONSTANT(MODE_MAX);
  106. }
  107. AESContext::AESContext() {
  108. }