| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <algorithm> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include "include/my_rnd.h" |
| 29 | #include "sql/stream_cipher.h" |
| 30 | |
| 31 | /* |
| 32 | Tests of replication ciphers used in binary log encryption. |
| 33 | */ |
| 34 | |
| 35 | namespace stream_cipher_unittest { |
| 36 | |
| 37 | /** |
| 38 | A test data set that is passed to the test functions. |
| 39 | */ |
| 40 | struct Test_data_set { |
| 41 | const unsigned char *source_ref = nullptr; |
| 42 | const int stream_size = 0; |
| 43 | const unsigned char *encrypted_ref = nullptr; |
| 44 | const unsigned char *key = nullptr; |
| 45 | /** |
| 46 | The test data set constructor. |
| 47 | |
| 48 | @param source_ref_arg The buffer to be encrypted. |
| 49 | @param stream_size_arg The size of the buffer to be encrypted. |
| 50 | @param encrypted_ref_arg A buffer with the source_ref_arg buffer encrypted. |
| 51 | @param key_arg The key that shall be used to open the ciphers. |
| 52 | */ |
| 53 | Test_data_set(const unsigned char *source_ref_arg, const int stream_size_arg, |
| 54 | const unsigned char *encrypted_ref_arg = nullptr, |
| 55 | const unsigned char *key_arg = nullptr) |
| 56 | : source_ref(source_ref_arg), |
| 57 | stream_size(stream_size_arg), |
| 58 | encrypted_ref(encrypted_ref_arg), |
| 59 | key(key_arg) {} |
| 60 | }; |
| 61 | |
| 62 | struct Random_test_data_set : public Test_data_set { |
| 63 | /** |
| 64 | The random test data set constructor. |
| 65 | |
| 66 | The source_ref_arg buffer will be initialized with random data. |
| 67 | |
| 68 | @param source_ref_arg The buffer to be encrypted. |
| 69 | @param stream_size_arg The size of the buffer to be encrypted. |
| 70 | */ |
| 71 | Random_test_data_set(unsigned char *source_ref_arg, const int stream_size_arg) |
| 72 | : Test_data_set(source_ref_arg, stream_size_arg) { |
| 73 | my_rand_buffer(source_ref_arg, stream_size_arg); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | /* Binlog issue */ |
| 78 | const unsigned char binlog_issue_ref[] = { |
| 79 | 0xFE, 0x62, 0x69, 0x6E, 0xEB, 0xCB, 0xB5, 0x5B, 0x0F, 0x01, 0x00, 0x00, |
| 80 | 0x00, 0x78, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, |
| 81 | 0x00, 0x38, 0x2E, 0x30, 0x2E, 0x31, 0x34, 0x2D, 0x74, 0x72, 0x2D, 0x64, |
| 82 | 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 84 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x0D, 0x00, 0x08, |
| 86 | 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x60, 0x00, |
| 87 | 0x04, 0x1A, 0x08, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00, |
| 88 | 0x00, 0x0A, 0x0A, 0x0A, 0x2A, 0x2A, 0x00, 0x12, 0x34, 0x00, 0x0A, 0x01, |
| 89 | 0xEE, 0xFB, 0xD1, 0x2A, 0xEB, 0xCB, 0xB5, 0x5B, 0x23, 0x01, 0x00, 0x00, |
| 90 | 0x00, 0x1F, 0x00, 0x00, 0x00, 0x9B, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, |
| 91 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD3, 0xB1, 0xF8, 0xE5, 0xEB, |
| 92 | 0xCB, 0xB5, 0x5B, 0x22, 0x01, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00, |
| 93 | 0xE8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 94 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 95 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, |
| 96 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 97 | 0x7E, 0xEA, 0xDD, 0xBE, 0x62, 0x77, 0x05, 0xBD, 0x8E, 0x38, 0x01, 0x00, |
| 98 | 0x56, 0x16, 0xCD, 0xBA, 0xEB, 0xCB, 0xB5, 0x5B, 0x02, 0x01, 0x00, 0x00, |
| 99 | 0x00, 0x70, 0x00, 0x00, 0x00, 0x58, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0D, |
| 100 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x2F, 0x00, |
| 101 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0xA0, 0x45, 0x00, 0x00, |
| 102 | 0x00, 0x00, 0x06, 0x03, 0x73, 0x74, 0x64, 0x04, 0xFF, 0x00, 0xFF, 0x00, |
| 103 | 0xFF, 0x00, 0x0C, 0x01, 0x74, 0x65, 0x73, 0x74, 0x00, 0x11, 0xCA, 0x00, |
| 104 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xFF, 0x00, 0x13, 0x00, 0x74, |
| 105 | 0x65, 0x73, 0x74, 0x00, 0x43, 0x52, 0x45, 0x41, 0x54, 0x45, 0x20, 0x54, |
| 106 | 0x41, 0x42, 0x4C, 0x45, 0x20, 0x74, 0x31, 0x20, 0x28, 0x63, 0x31, 0x20, |
| 107 | 0x49, 0x4E, 0x54, 0x29, 0xFB, 0x98, 0xC5, 0x50, 0xEC, 0xCB, 0xB5, 0x5B, |
| 108 | 0x22, 0x01, 0x00, 0x00, 0x00, 0x4F, 0x00, 0x00, 0x00, 0xA7, 0x01, 0x00, |
| 109 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 111 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 112 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0xE0, 0xE0, |
| 113 | 0xBE, 0x62, 0x77, 0x05, 0xFC, 0x13, 0x01, 0x8E, 0x38, 0x01, 0x00, 0xA1, |
| 114 | 0xD4, 0x48, 0xBC, 0xEC, 0xCB, 0xB5, 0x5B, 0x02, 0x01, 0x00, 0x00, 0x00, |
| 115 | 0x4D, 0x00, 0x00, 0x00, 0xF4, 0x01, 0x00, 0x00, 0x08, 0x00, 0x0D, 0x00, |
| 116 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x1F, 0x00, 0x00, |
| 117 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0xA0, 0x45, 0x00, 0x00, 0x00, |
| 118 | 0x00, 0x06, 0x03, 0x73, 0x74, 0x64, 0x04, 0xFF, 0x00, 0xFF, 0x00, 0xFF, |
| 119 | 0x00, 0x12, 0xFF, 0x00, 0x13, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00, 0x42, |
| 120 | 0x45, 0x47, 0x49, 0x4E, 0xA6, 0x27, 0x90, 0x66, 0xEC, 0xCB, 0xB5, 0x5B, |
| 121 | 0x13, 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x24, 0x02, 0x00, |
| 122 | 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, |
| 123 | 0x74, 0x65, 0x73, 0x74, 0x00, 0x02, 0x74, 0x31, 0x00, 0x01, 0x03, 0x00, |
| 124 | 0x01, 0x01, 0x01, 0x00, 0x9E, 0xBA, 0xF3, 0xF0, 0xEC, 0xCB, 0xB5, 0x5B, |
| 125 | 0x1E, 0x01, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x4C, 0x02, 0x00, |
| 126 | 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, |
| 127 | 0x00, 0x01, 0xFF, 0x00, 0x02, 0x00, 0x00, 0x00, 0x6A, 0x07, 0xB0, 0x80, |
| 128 | 0xEC, 0xCB, 0xB5, 0x5B, 0x10, 0x01, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, |
| 129 | 0x00, 0x6B, 0x02, 0x00, 0x00, 0x00, 0x00, 0xCB, 0x00, 0x00, 0x00, 0x00, |
| 130 | 0x00, 0x00, 0x00, 0x30, 0xFF, 0xD6, 0x8A}; |
| 131 | const unsigned char binlog_issue_encrypted[] = { |
| 132 | 0xA0, 0xC9, 0x08, 0x5D, 0x2C, 0x1D, 0x8C, 0x49, 0x12, 0x90, 0xA6, 0x59, |
| 133 | 0xEB, 0xE8, 0xB6, 0xE4, 0x96, 0x41, 0x80, 0x82, 0xD2, 0xD9, 0x37, 0xDE, |
| 134 | 0x26, 0x52, 0xA4, 0xFE, 0xB8, 0xE0, 0x40, 0x6A, 0x8B, 0xC4, 0xDF, 0x25, |
| 135 | 0x8E, 0xDC, 0x18, 0xD6, 0x87, 0x90, 0xD6, 0xFD, 0xE5, 0xDA, 0xE2, 0xD2, |
| 136 | 0x8A, 0x5F, 0x21, 0x9F, 0xFD, 0xAB, 0x87, 0xAE, 0xA2, 0xDD, 0x89, 0x60, |
| 137 | 0xDC, 0x01, 0x58, 0xCE, 0xE0, 0xB1, 0xC3, 0x66, 0x2D, 0xF5, 0x71, 0x3B, |
| 138 | 0xB2, 0xD8, 0xE3, 0x57, 0xEF, 0x27, 0x6F, 0x18, 0xE1, 0xF7, 0xB3, 0x39, |
| 139 | 0x45, 0x8D, 0x9C, 0x22, 0xB7, 0x2C, 0x02, 0x8D, 0x0C, 0xCB, 0x02, 0xFE, |
| 140 | 0xFC, 0xE0, 0xEB, 0xCB, 0x52, 0xE0, 0x5D, 0x47, 0xD3, 0x08, 0xB1, 0x8F, |
| 141 | 0x75, 0xEC, 0xF1, 0xD0, 0xA9, 0x52, 0xEF, 0x95, 0x82, 0x0F, 0xF5, 0x03, |
| 142 | 0x7D, 0x5B, 0x10, 0x61, 0x6D, 0x1D, 0xAA, 0x84, 0x55, 0x95, 0x89, 0x6B, |
| 143 | 0x1C, 0xD5, 0x76, 0x16, 0xF0, 0x32, 0x6C, 0xA1, 0x0D, 0xD4, 0xDB, 0x97, |
| 144 | 0x66, 0x8E, 0xED, 0xB5, 0xAE, 0xFD, 0x18, 0xA6, 0xBC, 0x62, 0x08, 0x92, |
| 145 | 0x03, 0xDE, 0x2E, 0xAA, 0x65, 0xB5, 0x16, 0x6B, 0x15, 0xD2, 0x3C, 0x9F, |
| 146 | 0x76, 0xEE, 0x71, 0x55, 0x51, 0x4F, 0xC0, 0xE7, 0x3B, 0x5E, 0xD0, 0xC5, |
| 147 | 0x01, 0x7C, 0xEC, 0xEF, 0x3F, 0xE6, 0xBD, 0x8F, 0xA7, 0xBE, 0x8F, 0x78, |
| 148 | 0xB1, 0xCC, 0x18, 0x5A, 0x32, 0x32, 0x39, 0x42, 0x7F, 0xA6, 0x8D, 0xBF, |
| 149 | 0xBD, 0x62, 0xD3, 0x26, 0x0C, 0xEC, 0xDF, 0x08, 0x12, 0xD7, 0x6F, 0x8B, |
| 150 | 0xE8, 0x1A, 0x79, 0x83, 0xD0, 0x10, 0xFB, 0xED, 0x62, 0x4A, 0x0E, 0xA6, |
| 151 | 0xE9, 0x0C, 0xFD, 0x05, 0x59, 0x79, 0xD4, 0x8D, 0x0B, 0x34, 0xC9, 0x5E, |
| 152 | 0xE1, 0x00, 0x04, 0xCA, 0x7B, 0xC8, 0xEE, 0x89, 0x40, 0xA4, 0x14, 0xF6, |
| 153 | 0x8C, 0xEE, 0xFD, 0x66, 0x11, 0x7E, 0x5B, 0x86, 0xBE, 0x48, 0xC6, 0x25, |
| 154 | 0x7A, 0x9E, 0x2C, 0x91, 0x9C, 0xB2, 0x08, 0xA4, 0x66, 0x39, 0xE4, 0x91, |
| 155 | 0x1C, 0x80, 0xB4, 0xFD, 0xC7, 0x52, 0x97, 0xC7, 0x38, 0x84, 0x1D, 0x76, |
| 156 | 0xEA, 0x4A, 0x71, 0xEA, 0x04, 0xCE, 0xEB, 0x28, 0x2B, 0xDC, 0x76, 0x9F, |
| 157 | 0xBC, 0x3D, 0x43, 0xF5, 0xC9, 0x1F, 0x62, 0xDF, 0x38, 0xF8, 0x4F, 0xBE, |
| 158 | 0x1B, 0xD2, 0x0A, 0x12, 0xA3, 0x47, 0x95, 0x59, 0x6C, 0x7E, 0x37, 0x98, |
| 159 | 0xA1, 0x15, 0x2A, 0x24, 0x74, 0x8F, 0x18, 0x97, 0xCC, 0x2A, 0x19, 0xF6, |
| 160 | 0xBC, 0x76, 0x1F, 0xCA, 0x5B, 0xB1, 0xB9, 0xE3, 0xB3, 0xA3, 0x37, 0x37, |
| 161 | 0x23, 0x70, 0x91, 0xC2, 0x9B, 0xB8, 0x15, 0xB3, 0xA4, 0x6E, 0x4E, 0x16, |
| 162 | 0xAE, 0xAA, 0x4D, 0x3C, 0x3E, 0x97, 0x83, 0xD0, 0x2D, 0x0D, 0x67, 0x72, |
| 163 | 0xA0, 0x0C, 0x7B, 0x45, 0x55, 0xC6, 0xDC, 0x62, 0x73, 0xDC, 0xC7, 0xEB, |
| 164 | 0x8B, 0x95, 0x07, 0x4F, 0xD4, 0xAB, 0xC9, 0x42, 0x25, 0x1D, 0x7D, 0xAE, |
| 165 | 0xFC, 0x3E, 0x8A, 0x4B, 0x83, 0xF9, 0xF9, 0xA4, 0xE4, 0xEB, 0xA6, 0x28, |
| 166 | 0x29, 0x48, 0x55, 0x83, 0x58, 0xB9, 0x74, 0x2E, 0x38, 0xD2, 0x30, 0xEC, |
| 167 | 0x5E, 0x61, 0x68, 0xDB, 0x94, 0x00, 0x6D, 0xE6, 0x01, 0xB2, 0x5F, 0x29, |
| 168 | 0xC3, 0x6D, 0xBB, 0x5D, 0xD8, 0x17, 0x45, 0xB2, 0xA7, 0xDE, 0x84, 0x79, |
| 169 | 0xDD, 0xFF, 0x6C, 0x9E, 0x50, 0x3A, 0xF5, 0x97, 0x45, 0x0A, 0x87, 0xC1, |
| 170 | 0x38, 0x37, 0x3A, 0x18, 0x0E, 0x31, 0x78, 0x47, 0x3C, 0xC1, 0x05, 0x35, |
| 171 | 0x48, 0x3B, 0xCD, 0x01, 0x62, 0x03, 0x3A, 0x62, 0xB3, 0x3B, 0xF1, 0xC8, |
| 172 | 0x82, 0x13, 0x7E, 0x25, 0xD0, 0x61, 0xAD, 0x8A, 0x89, 0x0E, 0xDE, 0x33, |
| 173 | 0xD4, 0x83, 0x0F, 0x6C, 0x97, 0x4A, 0xEC, 0xA4, 0x7C, 0x74, 0xC9, 0xCA, |
| 174 | 0x50, 0x08, 0x56, 0x15, 0x9C, 0xE3, 0xC0, 0x1E, 0xF5, 0x43, 0x6B, 0x98, |
| 175 | 0xFD, 0x1B, 0x33, 0x74, 0xCD, 0xD7, 0x5D, 0xBB, 0xC6, 0x54, 0x3B, 0xB3, |
| 176 | 0x8B, 0x53, 0x86, 0x3C, 0x31, 0x83, 0x3B, 0xD1, 0xC5, 0x77, 0x0B, 0x82, |
| 177 | 0x7F, 0x5F, 0xC2, 0x8A, 0x4D, 0x32, 0xD9, 0x6A, 0x3C, 0x9C, 0xB0, 0x0D, |
| 178 | 0x53, 0x34, 0xFE, 0xDB, 0x47, 0xB5, 0x9D, 0xD1, 0x76, 0x9A, 0x4E, 0x4C, |
| 179 | 0x41, 0xF2, 0xB6, 0xA1, 0x24, 0x57, 0x66, 0xC7, 0xAE, 0x73, 0x15, 0x7A, |
| 180 | 0x53, 0x07, 0xE4, 0x63, 0xB0, 0x5B, 0xE0, 0x84, 0x46, 0xDE, 0x49, 0x59, |
| 181 | 0xDA, 0xA3, 0x31, 0xFC, 0x01, 0xA8, 0x3D, 0x58, 0x48, 0xB4, 0xCD, 0x37, |
| 182 | 0x01, 0x67, 0x50, 0x48, 0xB2, 0x81, 0x85, 0x79, 0x84, 0x1F, 0x49, 0x38, |
| 183 | 0x30, 0x6A, 0x7D, 0x48, 0x6D, 0xF2, 0x1D}; |
| 184 | const int binlog_issue_size = sizeof(binlog_issue_encrypted); |
| 185 | Test_data_set binlog_issue(binlog_issue_ref, binlog_issue_size, |
| 186 | binlog_issue_encrypted); |
| 187 | |
| 188 | /* Data for the Basic tests */ |
| 189 | const int basic_size = 128; |
| 190 | const unsigned char basic_source_ref[basic_size] = { |
| 191 | 0xFE, 0x62, 0x69, 0x6E, 0xEB, 0xCB, 0xB5, 0x5B, 0x0F, 0x01, 0x00, 0x00, |
| 192 | 0x00, 0x78, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, |
| 193 | 0x00, 0x38, 0x2E, 0x30, 0x2E, 0x31, 0x34, 0x2D, 0x74, 0x72, 0x2D, 0x64, |
| 194 | 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 195 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 196 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 197 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x0D, 0x00, 0x08, |
| 198 | 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x60, 0x00, |
| 199 | 0x04, 0x1A, 0x08, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00, |
| 200 | 0x00, 0x0A, 0x0A, 0x0A, 0x2A, 0x2A, 0x00, 0x12, 0x34, 0x00, 0x0A, 0x01, |
| 201 | 0xEE, 0xFB, 0xD1, 0x2A, 0xEB, 0xCB, 0xB5, 0x5B}; |
| 202 | /* Encrypted references are also available to Basic tests */ |
| 203 | const unsigned char aes_ctr_encrypted_ref[] = { |
| 204 | 0xa0, 0xc9, 0x08, 0x5d, 0x2c, 0x1d, 0x8c, 0x49, 0x12, 0x90, 0xa6, 0x59, |
| 205 | 0xeb, 0xe8, 0xb6, 0xe4, 0x96, 0x41, 0x80, 0x82, 0xd2, 0xd9, 0x37, 0xde, |
| 206 | 0x26, 0x52, 0xa4, 0xfe, 0xb8, 0xe0, 0x40, 0x6a, 0x8b, 0xc4, 0xdf, 0x25, |
| 207 | 0x8e, 0xdc, 0x18, 0xd6, 0x87, 0x90, 0xd6, 0xfd, 0xe5, 0xda, 0xe2, 0xd2, |
| 208 | 0x8a, 0x5f, 0x21, 0x9f, 0xfd, 0xab, 0x87, 0xae, 0xa2, 0xdd, 0x89, 0x60, |
| 209 | 0xdc, 0x01, 0x58, 0xce, 0xe0, 0xb1, 0xc3, 0x66, 0x2d, 0xf5, 0x71, 0x3b, |
| 210 | 0xb2, 0xd8, 0xe3, 0x57, 0xef, 0x27, 0x6f, 0x18, 0xe1, 0xf7, 0xb3, 0x39, |
| 211 | 0x45, 0x8d, 0x9c, 0x22, 0xb7, 0x2c, 0x02, 0x8d, 0x0c, 0xcb, 0x02, 0xfe, |
| 212 | 0xfc, 0xe0, 0xeb, 0xcb, 0x52, 0xe0, 0x5d, 0x47, 0xd3, 0x08, 0xb1, 0x8f, |
| 213 | 0x75, 0xec, 0xf1, 0xd0, 0xa9, 0x52, 0xef, 0x95, 0x82, 0x0f, 0xf5, 0x03, |
| 214 | 0x7d, 0x5b, 0x10, 0x61, 0x6d, 0x1d, 0xaa, 0x84}; |
| 215 | Test_data_set aes_ctr_basic(basic_source_ref, basic_size, |
| 216 | aes_ctr_encrypted_ref); |
| 217 | |
| 218 | /* Same password used to protect rpl_nogtid_encryption_master-bin.000002 */ |
| 219 | const unsigned char aes_ctr_password[] = { |
| 220 | 0x14, 0x99, 0x6d, 0x52, 0xe3, 0x49, 0x4d, 0xb7, 0x81, 0x6a, 0x77, |
| 221 | 0x5e, 0xa9, 0x05, 0x28, 0x17, 0xd3, 0x98, 0x31, 0x6d, 0x17, 0xd4, |
| 222 | 0xd8, 0xee, 0xfa, 0x3b, 0x24, 0xf3, 0x96, 0x50, 0x32, 0x5c}; |
| 223 | const unsigned char aes_ctr_password_encrypted_ref[] = { |
| 224 | 0x46, 0x94, 0x9a, 0xe7, 0x5e, 0x35, 0xe5, 0x41, 0x50, 0x67, 0xd1, 0x99, |
| 225 | 0x16, 0xf9, 0x4f, 0x2c, 0xcf, 0x7c, 0x66, 0xaf, 0x11, 0x11, 0x4a, 0x04, |
| 226 | 0x08, 0x08, 0x81, 0x46, 0xed, 0x2d, 0x73, 0x16, 0xf0, 0x64, 0x2a, 0xdc, |
| 227 | 0xf8, 0x02, 0xf2, 0x06, 0x48, 0xfa, 0xd8, 0xa1, 0x38, 0xcb, 0x0c, 0x00, |
| 228 | 0x33, 0x1c, 0x82, 0x0d, 0x13, 0x1b, 0x92, 0x4d, 0x1d, 0xcb, 0x2b, 0x03, |
| 229 | 0x9d, 0x33, 0x06, 0x16, 0x59, 0x4f, 0xc7, 0x76, 0xb8, 0x68, 0x88, 0x6a, |
| 230 | 0x9c, 0xa3, 0x10, 0xfa, 0x8c, 0x0e, 0xfc, 0xe0, 0x43, 0x05, 0x1a, 0xe7, |
| 231 | 0x9c, 0x51, 0x18, 0xc0, 0x3b, 0xd2, 0x34, 0xd0, 0xf4, 0xb9, 0x0e, 0xc6, |
| 232 | 0xf7, 0x75, 0x39, 0xec, 0xd8, 0x98, 0xa6, 0x59, 0xa8, 0x91, 0x60, 0x5c, |
| 233 | 0x1b, 0xa0, 0xc3, 0x1a, 0x68, 0x2a, 0x5b, 0x9c, 0x30, 0x83, 0xb1, 0xe2, |
| 234 | 0xda, 0x30, 0x42, 0x6f, 0xaf, 0x9d, 0xc7, 0xce}; |
| 235 | Test_data_set aes_ctr_password_basic(basic_source_ref, basic_size, |
| 236 | aes_ctr_password_encrypted_ref, |
| 237 | aes_ctr_password); |
| 238 | |
| 239 | /* Only source reference is available to random tests */ |
| 240 | const int max_size = 32 * 1024; |
| 241 | unsigned char max_source_ref[max_size]; |
| 242 | Random_test_data_set max(max_source_ref, max_size); |
| 243 | |
| 244 | /** |
| 245 | Sets the key to be used when opening the ciphers. |
| 246 | |
| 247 | @param key_str Key_string to be used when opening the ciphers. |
| 248 | @param key Key to be assigned to key_str. A key filled with zeros will be |
| 249 | used when key is null. |
| 250 | */ |
| 251 | template <typename T> |
| 252 | void SetKeyStr(Key_string &key_str, const unsigned char *key) { |
| 253 | if (key) { |
| 254 | key_str.assign(key, T::PASSWORD_LENGTH); |
| 255 | } else { |
| 256 | const unsigned char new_key[T::PASSWORD_LENGTH]{0}; |
| 257 | key_str.assign(new_key, T::PASSWORD_LENGTH); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | A simple test decrypting 4, 19 and 25 bytes in sequence and then decrypting |
| 263 | the rest of the buffer. |
| 264 | |
| 265 | - 4 = binlog magic; |
| 266 | - 19 = FDE header; |
| 267 | - 25 = FDE body; |
| 268 | |
| 269 | @param ds The test data set. |
| 270 | */ |
| 271 | template <typename T> |
| 272 | void Binlog(Test_data_set ds) { |
| 273 | Key_string key_str; |
| 274 | SetKeyStr<T>(key_str, ds.key); |
| 275 | |
| 276 | std::unique_ptr<unsigned char[]> buffer(new unsigned char[ds.stream_size]); |
| 277 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 278 | /* Decryption will be performed into same encrypted buffer */ |
| 279 | unsigned char *encrypted = buffer.get(); |
| 280 | unsigned char *decrypted = buffer.get(); |
| 281 | |
| 282 | std::unique_ptr<Stream_cipher> encryptor = T::get_encryptor(); |
| 283 | std::unique_ptr<Stream_cipher> decryptor = T::get_decryptor(); |
| 284 | |
| 285 | /* Encrypt all and compare with encrypted reference if provided */ |
| 286 | EXPECT_FALSE(encryptor->open(key_str, 0)); |
| 287 | EXPECT_FALSE(encryptor->encrypt(encrypted, ds.source_ref, ds.stream_size)); |
| 288 | if (ds.encrypted_ref) { |
| 289 | EXPECT_FALSE(memcmp(encrypted, ds.encrypted_ref, ds.stream_size)); |
| 290 | } |
| 291 | |
| 292 | /* Decrypt as a stream 4, 19, 25 and the rest of the buffer */ |
| 293 | EXPECT_FALSE(decryptor->open(key_str, 0)); |
| 294 | EXPECT_FALSE(decryptor->decrypt(decrypted, encrypted, 4)); |
| 295 | EXPECT_FALSE(decryptor->decrypt(decrypted + 4, encrypted + 4, 19)); |
| 296 | EXPECT_FALSE(decryptor->decrypt(decrypted + 4 + 19, encrypted + 4 + 19, 25)); |
| 297 | EXPECT_FALSE(decryptor->decrypt(decrypted + (25 + 19 + 4), |
| 298 | encrypted + (25 + 19 + 4), |
| 299 | ds.stream_size - (25 + 19 + 4))); |
| 300 | EXPECT_FALSE(memcmp(decrypted, ds.source_ref, ds.stream_size)); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | Encrypt the source to a new buffer and decrypts the new buffer into itself. |
| 305 | |
| 306 | @param ds The test data set. |
| 307 | */ |
| 308 | template <typename T> |
| 309 | void EncryptAndDecrypt(Test_data_set ds) { |
| 310 | Key_string key_str; |
| 311 | SetKeyStr<T>(key_str, ds.key); |
| 312 | |
| 313 | std::unique_ptr<unsigned char[]> buffer(new unsigned char[ds.stream_size]); |
| 314 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 315 | /* Decryption will be performed into same encrypted buffer */ |
| 316 | unsigned char *encrypted = buffer.get(); |
| 317 | unsigned char *decrypted = buffer.get(); |
| 318 | |
| 319 | std::unique_ptr<Stream_cipher> encryptor = T::get_encryptor(); |
| 320 | std::unique_ptr<Stream_cipher> decryptor = T::get_decryptor(); |
| 321 | |
| 322 | /* Encrypt all and compare with encrypted reference if provided */ |
| 323 | EXPECT_FALSE(encryptor->open(key_str, 0)); |
| 324 | EXPECT_FALSE(encryptor->encrypt(encrypted, ds.source_ref, ds.stream_size)); |
| 325 | if (ds.encrypted_ref) { |
| 326 | EXPECT_FALSE(memcmp(encrypted, ds.encrypted_ref, ds.stream_size)); |
| 327 | } |
| 328 | |
| 329 | /* Decrypt all and compare with source reference */ |
| 330 | EXPECT_FALSE(decryptor->open(key_str, 0)); |
| 331 | EXPECT_FALSE(decryptor->decrypt(decrypted, encrypted, ds.stream_size)); |
| 332 | EXPECT_FALSE(memcmp(decrypted, ds.source_ref, ds.stream_size)); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | This is the standard binary log encryption behavior. |
| 337 | |
| 338 | It feeds the encryptor cipher with a stream in pieces and also feeds the |
| 339 | decryptor cipher with a stream in pieces. |
| 340 | |
| 341 | Decryption is done to the same buffer provided as its source. |
| 342 | |
| 343 | @param ds The test data set. |
| 344 | */ |
| 345 | template <typename T> |
| 346 | void SequentialEncryptAndDecrypt(Test_data_set ds) { |
| 347 | Key_string key_str; |
| 348 | SetKeyStr<T>(key_str, ds.key); |
| 349 | |
| 350 | std::unique_ptr<unsigned char[]> buffer(new unsigned char[ds.stream_size]); |
| 351 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 352 | /* Decryption will be performed into same encrypted buffer */ |
| 353 | unsigned char *encrypted = buffer.get(); |
| 354 | unsigned char *decrypted = buffer.get(); |
| 355 | |
| 356 | /* Buffer to store an encrypted reference */ |
| 357 | std::unique_ptr<unsigned char[]> full_encrypted_buffer( |
| 358 | new unsigned char[ds.stream_size]); |
| 359 | my_rand_buffer(full_encrypted_buffer.get(), ds.stream_size); |
| 360 | unsigned char *full_encrypted = full_encrypted_buffer.get(); |
| 361 | |
| 362 | std::unique_ptr<Stream_cipher> encryptor = T::get_encryptor(); |
| 363 | std::unique_ptr<Stream_cipher> decryptor = T::get_decryptor(); |
| 364 | |
| 365 | /* Encrypt all and compare with encrypted reference if provided */ |
| 366 | EXPECT_FALSE(encryptor->open(key_str, 0)); |
| 367 | EXPECT_FALSE( |
| 368 | encryptor->encrypt(full_encrypted, ds.source_ref, ds.stream_size)); |
| 369 | if (ds.encrypted_ref) { |
| 370 | EXPECT_FALSE(memcmp(full_encrypted, ds.encrypted_ref, ds.stream_size)); |
| 371 | } |
| 372 | |
| 373 | /* Decrypt all and compare with source reference */ |
| 374 | EXPECT_FALSE(decryptor->open(key_str, 0)); |
| 375 | EXPECT_FALSE(decryptor->decrypt(decrypted, full_encrypted, ds.stream_size)); |
| 376 | EXPECT_FALSE(memcmp(ds.source_ref, decrypted, ds.stream_size)); |
| 377 | |
| 378 | /* Encryption and decryption will be performed in step_size chunks */ |
| 379 | for (int step_size = 1; |
| 380 | step_size <= std::min(ds.stream_size, T::PASSWORD_LENGTH * 8); |
| 381 | step_size++) { |
| 382 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 383 | EXPECT_FALSE(encryptor->set_stream_offset(0)); |
| 384 | EXPECT_FALSE(decryptor->set_stream_offset(0)); |
| 385 | int offset = 0; |
| 386 | while (offset < ds.stream_size) { |
| 387 | /* Process next chunk up to the stream size */ |
| 388 | int length = std::min(step_size, ds.stream_size - offset); |
| 389 | EXPECT_FALSE(encryptor->encrypt(encrypted + offset, |
| 390 | ds.source_ref + offset, length)); |
| 391 | EXPECT_FALSE(memcmp(encrypted + offset, full_encrypted + offset, length)); |
| 392 | EXPECT_FALSE( |
| 393 | decryptor->decrypt(decrypted + offset, encrypted + offset, length)); |
| 394 | EXPECT_FALSE(memcmp(ds.source_ref + offset, decrypted + offset, length)); |
| 395 | offset = offset + length; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | Test if the ciphers handle encrypting and decrypting many chunk sizes. |
| 402 | |
| 403 | Decryption is done to the same buffer provided as its source. |
| 404 | |
| 405 | @param ds The test data set. |
| 406 | */ |
| 407 | template <typename T> |
| 408 | void MultiLengthEncryptAndDecrypt(Test_data_set ds) { |
| 409 | Key_string key_str; |
| 410 | SetKeyStr<T>(key_str, ds.key); |
| 411 | |
| 412 | std::unique_ptr<unsigned char[]> buffer(new unsigned char[ds.stream_size]); |
| 413 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 414 | /* Decryption will be performed into same encrypted buffer */ |
| 415 | unsigned char *encrypted = buffer.get(); |
| 416 | unsigned char *decrypted = buffer.get(); |
| 417 | |
| 418 | /* Buffer to store an encrypted reference */ |
| 419 | std::unique_ptr<unsigned char[]> full_encrypted_buffer( |
| 420 | new unsigned char[ds.stream_size]); |
| 421 | my_rand_buffer(full_encrypted_buffer.get(), ds.stream_size); |
| 422 | unsigned char *full_encrypted = full_encrypted_buffer.get(); |
| 423 | |
| 424 | std::unique_ptr<Stream_cipher> encryptor = T::get_encryptor(); |
| 425 | std::unique_ptr<Stream_cipher> decryptor = T::get_decryptor(); |
| 426 | |
| 427 | /* Encrypt all and compare with encrypted reference if provided */ |
| 428 | EXPECT_FALSE(encryptor->open(key_str, 0)); |
| 429 | EXPECT_FALSE( |
| 430 | encryptor->encrypt(full_encrypted, ds.source_ref, ds.stream_size)); |
| 431 | if (ds.encrypted_ref) { |
| 432 | EXPECT_FALSE(memcmp(full_encrypted, ds.encrypted_ref, ds.stream_size)); |
| 433 | } |
| 434 | |
| 435 | /* Decrypt all and compare with source reference */ |
| 436 | EXPECT_FALSE(decryptor->open(key_str, 0)); |
| 437 | EXPECT_FALSE(decryptor->decrypt(decrypted, full_encrypted, ds.stream_size)); |
| 438 | EXPECT_FALSE(memcmp(ds.source_ref, decrypted, ds.stream_size)); |
| 439 | |
| 440 | for (int length = 0; length <= ds.stream_size; length++) { |
| 441 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 442 | /* Encrypt the length and compare with encrypted reference */ |
| 443 | EXPECT_FALSE(encryptor->set_stream_offset(0)); |
| 444 | EXPECT_FALSE(encryptor->encrypt(encrypted, ds.source_ref, length)); |
| 445 | EXPECT_FALSE(memcmp(encrypted, full_encrypted, length)); |
| 446 | /* Decrypt the length and compare with source reference */ |
| 447 | EXPECT_FALSE(decryptor->set_stream_offset(0)); |
| 448 | EXPECT_FALSE(decryptor->decrypt(decrypted, encrypted, length)); |
| 449 | EXPECT_FALSE(memcmp(ds.source_ref, decrypted, length)); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | Test if the ciphers are able to (re)start encrypting in the middle of the |
| 455 | stream. |
| 456 | |
| 457 | Decryption is done to the same buffer provided as its source. |
| 458 | |
| 459 | @param ds The test data set. |
| 460 | */ |
| 461 | template <typename T> |
| 462 | void SeekAndEncryptAndDecrypt(Test_data_set ds) { |
| 463 | Key_string key_str; |
| 464 | SetKeyStr<T>(key_str, ds.key); |
| 465 | |
| 466 | std::unique_ptr<unsigned char[]> buffer(new unsigned char[ds.stream_size]); |
| 467 | my_rand_buffer(buffer.get(), ds.stream_size); |
| 468 | /* Decryption will be performed into same encrypted buffer */ |
| 469 | unsigned char *encrypted = buffer.get(); |
| 470 | unsigned char *decrypted = buffer.get(); |
| 471 | |
| 472 | /* Buffer to store an encrypted reference */ |
| 473 | std::unique_ptr<unsigned char[]> full_encrypted_buffer( |
| 474 | new unsigned char[ds.stream_size]); |
| 475 | my_rand_buffer(full_encrypted_buffer.get(), ds.stream_size); |
| 476 | unsigned char *full_encrypted = full_encrypted_buffer.get(); |
| 477 | |
| 478 | /* The source will be changed during the test */ |
| 479 | std::unique_ptr<unsigned char[]> source_buffer( |
| 480 | new unsigned char[ds.stream_size]); |
| 481 | unsigned char *source = source_buffer.get(); |
| 482 | |
| 483 | std::unique_ptr<Stream_cipher> encryptor = T::get_encryptor(); |
| 484 | std::unique_ptr<Stream_cipher> decryptor = T::get_decryptor(); |
| 485 | |
| 486 | /* Encrypt all and compare with encrypted reference if provided */ |
| 487 | EXPECT_FALSE(encryptor->open(key_str, 0)); |
| 488 | EXPECT_FALSE( |
| 489 | encryptor->encrypt(full_encrypted, ds.source_ref, ds.stream_size)); |
| 490 | if (ds.encrypted_ref) { |
| 491 | EXPECT_FALSE(memcmp(full_encrypted, ds.encrypted_ref, ds.stream_size)); |
| 492 | } |
| 493 | |
| 494 | /* Decrypt all and compare with source reference */ |
| 495 | EXPECT_FALSE(decryptor->open(key_str, 0)); |
| 496 | EXPECT_FALSE(decryptor->decrypt(decrypted, full_encrypted, ds.stream_size)); |
| 497 | EXPECT_FALSE(memcmp(ds.source_ref, decrypted, ds.stream_size)); |
| 498 | |
| 499 | for (int length = 1; |
| 500 | length <= std::min(ds.stream_size, T::PASSWORD_LENGTH * 8); length++) { |
| 501 | for (int offset = 0; |
| 502 | offset < std::min(ds.stream_size, T::PASSWORD_LENGTH * 8) - length; |
| 503 | offset++) { |
| 504 | /* Encrypt all source buffer */ |
| 505 | memcpy(source, ds.source_ref, ds.stream_size); |
| 506 | EXPECT_FALSE(encryptor->set_stream_offset(0)); |
| 507 | EXPECT_FALSE(encryptor->encrypt(encrypted, source, ds.stream_size)); |
| 508 | /* |
| 509 | Change some part of the source and encrypt everything to generate an |
| 510 | encrypted reference of it. |
| 511 | */ |
| 512 | my_rand_buffer(source + offset, length); |
| 513 | EXPECT_FALSE(encryptor->set_stream_offset(0)); |
| 514 | EXPECT_FALSE(encryptor->encrypt(full_encrypted, source, ds.stream_size)); |
| 515 | /* Re-encrypt the changed part and compare with encrypted reference */ |
| 516 | EXPECT_FALSE(encryptor->set_stream_offset(offset)); |
| 517 | EXPECT_FALSE( |
| 518 | encryptor->encrypt(encrypted + offset, source + offset, length)); |
| 519 | EXPECT_FALSE(memcmp(encrypted, full_encrypted, ds.stream_size)); |
| 520 | /* Decrypt everything and compare with source reference */ |
| 521 | EXPECT_FALSE(decryptor->set_stream_offset(0)); |
| 522 | EXPECT_FALSE(decryptor->decrypt(decrypted, encrypted, ds.stream_size)); |
| 523 | EXPECT_FALSE(memcmp(source, decrypted, ds.stream_size)); |
| 524 | /* Decrypt only the changed part and compare with source reference */ |
| 525 | memcpy(encrypted, full_encrypted, ds.stream_size); |
| 526 | EXPECT_FALSE(decryptor->set_stream_offset(offset)); |
| 527 | EXPECT_FALSE( |
| 528 | decryptor->decrypt(decrypted + offset, encrypted + offset, length)); |
| 529 | EXPECT_FALSE(memcmp(source + offset, decrypted + offset, length)); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /* Aes_ctr tests */ |
| 535 | |
| 536 | TEST(Aes_ctr, BinlogIssue) { Binlog<Aes_ctr>(binlog_issue); } |
| 537 | TEST(Aes_ctr, BinlogIssueMax) { Binlog<Aes_ctr>(max); } |
| 538 | |
| 539 | TEST(Aes_ctr, BasicEncryptAndDecrypt) { |
| 540 | EncryptAndDecrypt<Aes_ctr>(aes_ctr_basic); |
| 541 | } |
| 542 | TEST(Aes_ctr, BasicSequentialEncryptAndDecrypt) { |
| 543 | SequentialEncryptAndDecrypt<Aes_ctr>(aes_ctr_basic); |
| 544 | } |
| 545 | TEST(Aes_ctr, BasicMultiLengthEncryptAndDecrypt) { |
| 546 | MultiLengthEncryptAndDecrypt<Aes_ctr>(aes_ctr_basic); |
| 547 | } |
| 548 | TEST(Aes_ctr, BasicSeekAndEncryptAndDecrypt) { |
| 549 | SeekAndEncryptAndDecrypt<Aes_ctr>(aes_ctr_basic); |
| 550 | } |
| 551 | |
| 552 | TEST(Aes_ctr, BasicEncryptAndDecryptKnownPassword) { |
| 553 | EncryptAndDecrypt<Aes_ctr>(aes_ctr_password_basic); |
| 554 | } |
| 555 | TEST(Aes_ctr, BasicSequentialEncryptAndDecryptKnownPassword) { |
| 556 | SequentialEncryptAndDecrypt<Aes_ctr>(aes_ctr_password_basic); |
| 557 | } |
| 558 | TEST(Aes_ctr, BasicMultiLengthEncryptAndDecryptKnownPassword) { |
| 559 | MultiLengthEncryptAndDecrypt<Aes_ctr>(aes_ctr_password_basic); |
| 560 | } |
| 561 | TEST(Aes_ctr, BasicSeekAndEncryptAndDecryptKnownPassword) { |
| 562 | SeekAndEncryptAndDecrypt<Aes_ctr>(aes_ctr_password_basic); |
| 563 | } |
| 564 | |
| 565 | TEST(Aes_ctr, MaxEncryptAndDecrypt) { EncryptAndDecrypt<Aes_ctr>(max); } |
| 566 | TEST(Aes_ctr, MaxSequentialEncryptAndDecrypt) { |
| 567 | SequentialEncryptAndDecrypt<Aes_ctr>(max); |
| 568 | } |
| 569 | TEST(Aes_ctr, MaxMultiLengthEncryptAndDecrypt) { |
| 570 | MultiLengthEncryptAndDecrypt<Aes_ctr>(max); |
| 571 | } |
| 572 | TEST(Aes_ctr, MaxSeekAndEncryptAndDecrypt) { |
| 573 | SeekAndEncryptAndDecrypt<Aes_ctr>(max); |
| 574 | } |
| 575 | |
| 576 | } // namespace stream_cipher_unittest |