| 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 | |
| 31 | #include "core/crypto/aes_context.h" |
| 32 | |
| 33 | Error AESContext::start(Mode p_mode, PackedByteArray p_key, PackedByteArray p_iv) { |
| 34 | ERR_FAIL_COND_V_MSG(mode != MODE_MAX, ERR_ALREADY_IN_USE, "AESContext already started. Call 'finish' before starting a new one." ); |
| 35 | ERR_FAIL_COND_V_MSG(p_mode < 0 || p_mode >= MODE_MAX, ERR_INVALID_PARAMETER, "Invalid mode requested." ); |
| 36 | // Key check. |
| 37 | int key_bits = p_key.size() << 3; |
| 38 | ERR_FAIL_COND_V_MSG(key_bits != 128 && key_bits != 256, ERR_INVALID_PARAMETER, "AES key must be either 16 or 32 bytes" ); |
| 39 | // Initialization vector. |
| 40 | if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_CBC_DECRYPT) { |
| 41 | ERR_FAIL_COND_V_MSG(p_iv.size() != 16, ERR_INVALID_PARAMETER, "The initialization vector (IV) must be exactly 16 bytes." ); |
| 42 | iv.resize(0); |
| 43 | iv.append_array(p_iv); |
| 44 | } |
| 45 | // Encryption/decryption key. |
| 46 | if (p_mode == MODE_CBC_ENCRYPT || p_mode == MODE_ECB_ENCRYPT) { |
| 47 | ctx.set_encode_key(p_key.ptr(), key_bits); |
| 48 | } else { |
| 49 | ctx.set_decode_key(p_key.ptr(), key_bits); |
| 50 | } |
| 51 | mode = p_mode; |
| 52 | return OK; |
| 53 | } |
| 54 | |
| 55 | PackedByteArray AESContext::update(PackedByteArray p_src) { |
| 56 | ERR_FAIL_COND_V_MSG(mode < 0 || mode >= MODE_MAX, PackedByteArray(), "AESContext not started. Call 'start' before calling 'update'." ); |
| 57 | int len = p_src.size(); |
| 58 | ERR_FAIL_COND_V_MSG(len % 16, PackedByteArray(), "The number of bytes to be encrypted must be multiple of 16. Add padding if needed" ); |
| 59 | PackedByteArray out; |
| 60 | out.resize(len); |
| 61 | const uint8_t *src_ptr = p_src.ptr(); |
| 62 | uint8_t *out_ptr = out.ptrw(); |
| 63 | switch (mode) { |
| 64 | case MODE_ECB_ENCRYPT: { |
| 65 | for (int i = 0; i < len; i += 16) { |
| 66 | Error err = ctx.encrypt_ecb(src_ptr + i, out_ptr + i); |
| 67 | ERR_FAIL_COND_V(err != OK, PackedByteArray()); |
| 68 | } |
| 69 | } break; |
| 70 | case MODE_ECB_DECRYPT: { |
| 71 | for (int i = 0; i < len; i += 16) { |
| 72 | Error err = ctx.decrypt_ecb(src_ptr + i, out_ptr + i); |
| 73 | ERR_FAIL_COND_V(err != OK, PackedByteArray()); |
| 74 | } |
| 75 | } break; |
| 76 | case MODE_CBC_ENCRYPT: { |
| 77 | Error err = ctx.encrypt_cbc(len, iv.ptrw(), p_src.ptr(), out.ptrw()); |
| 78 | ERR_FAIL_COND_V(err != OK, PackedByteArray()); |
| 79 | } break; |
| 80 | case MODE_CBC_DECRYPT: { |
| 81 | Error err = ctx.decrypt_cbc(len, iv.ptrw(), p_src.ptr(), out.ptrw()); |
| 82 | ERR_FAIL_COND_V(err != OK, PackedByteArray()); |
| 83 | } break; |
| 84 | default: |
| 85 | ERR_FAIL_V_MSG(PackedByteArray(), "Bug!" ); |
| 86 | } |
| 87 | return out; |
| 88 | } |
| 89 | |
| 90 | PackedByteArray AESContext::get_iv_state() { |
| 91 | 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." ); |
| 92 | PackedByteArray out; |
| 93 | out.append_array(iv); |
| 94 | return out; |
| 95 | } |
| 96 | |
| 97 | void AESContext::finish() { |
| 98 | mode = MODE_MAX; |
| 99 | iv.resize(0); |
| 100 | } |
| 101 | |
| 102 | void AESContext::_bind_methods() { |
| 103 | ClassDB::bind_method(D_METHOD("start" , "mode" , "key" , "iv" ), &AESContext::start, DEFVAL(PackedByteArray())); |
| 104 | ClassDB::bind_method(D_METHOD("update" , "src" ), &AESContext::update); |
| 105 | ClassDB::bind_method(D_METHOD("get_iv_state" ), &AESContext::get_iv_state); |
| 106 | ClassDB::bind_method(D_METHOD("finish" ), &AESContext::finish); |
| 107 | BIND_ENUM_CONSTANT(MODE_ECB_ENCRYPT); |
| 108 | BIND_ENUM_CONSTANT(MODE_ECB_DECRYPT); |
| 109 | BIND_ENUM_CONSTANT(MODE_CBC_ENCRYPT); |
| 110 | BIND_ENUM_CONSTANT(MODE_CBC_DECRYPT); |
| 111 | BIND_ENUM_CONSTANT(MODE_MAX); |
| 112 | } |
| 113 | |
| 114 | AESContext::AESContext() { |
| 115 | } |
| 116 | |