Mercurial > hg > orthanc-object-storage
annotate UnitTestsSources/EncryptionTests.cpp @ 154:e8ac39175eaf
added DownloadStaticPackages.sh for AWS
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 22 Jun 2024 16:55:11 +0200 |
parents | 6dd8bb916573 |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
145
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
3 * Copyright (C) 2020-2023 Osimis S.A., Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
37
diff
changeset
|
20 |
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
37
diff
changeset
|
21 |
1 | 22 #include "gtest/gtest.h" |
23 | |
24 #include "../Common/EncryptionHelpers.h" | |
25 #include <boost/chrono/chrono.hpp> | |
26 #include <boost/date_time.hpp> | |
27 | |
28 TEST(EncryptionHelpers, GenerateKey) | |
29 { | |
30 CryptoPP::SecByteBlock key1, key2; | |
31 EncryptionHelpers::GenerateKey(key1); | |
32 EncryptionHelpers::GenerateKey(key2); | |
33 | |
34 // std::cout << EncryptionHelpers::ToHexString(key1) << std::endl; | |
35 // std::cout << EncryptionHelpers::ToHexString(key2) << std::endl; | |
36 | |
37 ASSERT_NE(key1, key2); | |
38 | |
24
84c4ca822a13
fix warning in unit test
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1
diff
changeset
|
39 ASSERT_EQ(32u, key1.size()); // right now, we work with 256bits key |
84c4ca822a13
fix warning in unit test
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1
diff
changeset
|
40 ASSERT_EQ(32u * 2u, EncryptionHelpers::ToHexString(key1).size()); |
1 | 41 } |
42 | |
43 TEST(EncryptionHelpers, EncryptDecryptSimpleText) | |
44 { | |
45 CryptoPP::SecByteBlock masterKey; | |
46 EncryptionHelpers::GenerateKey(masterKey); | |
47 | |
48 EncryptionHelpers crypto; | |
49 crypto.SetCurrentMasterKey(1, masterKey); | |
50 | |
51 std::string plainTextMessage = "Plain text message"; | |
52 std::string encryptedMessage; | |
53 | |
54 crypto.Encrypt(encryptedMessage, plainTextMessage); | |
55 | |
56 std::string decryptedMessage; | |
57 | |
58 crypto.Decrypt(decryptedMessage, encryptedMessage); | |
59 | |
60 ASSERT_EQ(plainTextMessage, decryptedMessage); | |
61 } | |
62 | |
63 TEST(EncryptionHelpers, EncryptDecrypt1byteText) | |
64 { | |
65 CryptoPP::SecByteBlock masterKey; | |
66 EncryptionHelpers::GenerateKey(masterKey); | |
67 | |
68 EncryptionHelpers crypto; | |
69 crypto.SetCurrentMasterKey(1, masterKey); | |
70 | |
71 std::string plainTextMessage = "P"; | |
72 std::string encryptedMessage; | |
73 | |
74 crypto.Encrypt(encryptedMessage, plainTextMessage); | |
75 | |
76 std::string decryptedMessage; | |
77 | |
78 crypto.Decrypt(decryptedMessage, encryptedMessage); | |
79 | |
80 ASSERT_EQ(plainTextMessage, decryptedMessage); | |
81 } | |
82 | |
83 TEST(EncryptionHelpers, EncryptDecrypt0byteText) | |
84 { | |
85 CryptoPP::SecByteBlock masterKey; | |
86 EncryptionHelpers::GenerateKey(masterKey); | |
87 | |
88 EncryptionHelpers crypto; | |
89 crypto.SetCurrentMasterKey(1, masterKey); | |
90 | |
91 std::string plainTextMessage = ""; | |
92 std::string encryptedMessage; | |
93 | |
94 crypto.Encrypt(encryptedMessage, plainTextMessage); | |
95 | |
96 std::string decryptedMessage; | |
97 | |
98 crypto.Decrypt(decryptedMessage, encryptedMessage); | |
99 | |
100 ASSERT_EQ(plainTextMessage, decryptedMessage); | |
101 } | |
102 | |
103 TEST(EncryptionHelpers, EncryptDecryptTampering) | |
104 { | |
105 CryptoPP::SecByteBlock masterKey; | |
106 EncryptionHelpers::GenerateKey(masterKey); | |
107 | |
108 EncryptionHelpers crypto; | |
109 crypto.SetCurrentMasterKey(1, masterKey); | |
110 | |
111 std::string plainTextMessage = "Plain text message"; | |
112 std::string encryptedMessage; | |
113 std::string decryptedMessage; | |
114 | |
115 crypto.Encrypt(encryptedMessage, plainTextMessage); | |
116 | |
117 { | |
118 std::string tamperedEncryptedMessage = encryptedMessage; | |
119 // change the header | |
120 tamperedEncryptedMessage[0] = 'B'; | |
121 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
122 } | |
123 | |
124 { | |
125 std::string tamperedEncryptedMessage = encryptedMessage; | |
126 // tamper the masterKeyId: | |
127 tamperedEncryptedMessage[EncryptionHelpers::HEADER_VERSION_SIZE + 2] = 0xAF; | |
128 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
129 } | |
130 | |
131 { | |
132 std::string tamperedEncryptedMessage = encryptedMessage; | |
133 // tamper the iv: | |
134 tamperedEncryptedMessage[EncryptionHelpers::HEADER_VERSION_SIZE + EncryptionHelpers::MASTER_KEY_ID_SIZE + 2] = 0; | |
135 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
136 } | |
137 | |
138 { | |
139 std::string tamperedEncryptedMessage = encryptedMessage; | |
140 // tamper the encrypted text: | |
141 tamperedEncryptedMessage[EncryptionHelpers::HEADER_VERSION_SIZE + EncryptionHelpers::MASTER_KEY_ID_SIZE + EncryptionHelpers::IV_SIZE + 2] = 0; | |
142 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
143 } | |
144 | |
145 { | |
146 std::string tamperedEncryptedMessage = encryptedMessage; | |
147 // tamper the mac: | |
148 tamperedEncryptedMessage[tamperedEncryptedMessage.size() - 2] = 0; | |
149 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
150 } | |
151 | |
152 { | |
153 std::string tamperedEncryptedMessage = encryptedMessage; | |
154 // extend the file content | |
155 tamperedEncryptedMessage = tamperedEncryptedMessage + "TAMPER"; | |
156 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
157 } | |
158 | |
159 { | |
160 std::string tamperedEncryptedMessage = encryptedMessage; | |
161 // reduce the file content | |
162 tamperedEncryptedMessage = tamperedEncryptedMessage.substr(0, tamperedEncryptedMessage.size() - 5); | |
163 ASSERT_THROW(crypto.Decrypt(decryptedMessage, tamperedEncryptedMessage), EncryptionException); | |
164 } | |
165 } | |
166 | |
167 | |
168 TEST(EncryptionHelpers, EncryptDecrypt2TimesSameText) | |
169 { | |
170 CryptoPP::SecByteBlock masterKey; | |
171 EncryptionHelpers::GenerateKey(masterKey); | |
172 | |
173 EncryptionHelpers crypto; | |
174 crypto.SetCurrentMasterKey(1, masterKey); | |
175 | |
176 std::string plainTextMessage = "Plain text message"; | |
177 std::string encryptedMessage1; | |
178 std::string encryptedMessage2; | |
179 | |
180 crypto.Encrypt(encryptedMessage1, plainTextMessage); | |
181 crypto.Encrypt(encryptedMessage2, plainTextMessage); | |
182 | |
183 ASSERT_NE(encryptedMessage1, encryptedMessage2); | |
184 | |
185 std::string decryptedMessage1; | |
186 std::string decryptedMessage2; | |
187 | |
188 crypto.Decrypt(decryptedMessage1, encryptedMessage1); | |
189 crypto.Decrypt(decryptedMessage2, encryptedMessage2); | |
190 | |
191 ASSERT_EQ(plainTextMessage, decryptedMessage1); | |
192 ASSERT_EQ(plainTextMessage, decryptedMessage2); | |
193 } | |
194 | |
195 TEST(EncryptionHelpers, RotateMasterKeys) | |
196 { | |
197 std::string plainTextMessage = "Plain text message"; | |
198 std::string encryptedMessage1; | |
199 std::string encryptedMessage2; | |
200 std::string decryptedMessage; | |
201 | |
202 CryptoPP::SecByteBlock masterKey1; | |
203 CryptoPP::SecByteBlock masterKey2; | |
204 EncryptionHelpers::GenerateKey(masterKey1); | |
205 EncryptionHelpers::GenerateKey(masterKey2); | |
206 | |
207 { | |
208 EncryptionHelpers crypto; | |
209 crypto.SetCurrentMasterKey(1, masterKey1); | |
210 crypto.Encrypt(encryptedMessage1, plainTextMessage); | |
211 | |
212 crypto.SetCurrentMasterKey(2, masterKey2); | |
213 crypto.AddPreviousMasterKey(1, masterKey1); | |
214 | |
215 crypto.Encrypt(encryptedMessage2, plainTextMessage); | |
216 | |
217 // ensure that we can decrypt messages encrypted with both master keys | |
218 crypto.Decrypt(decryptedMessage, encryptedMessage1); | |
219 ASSERT_EQ(plainTextMessage, decryptedMessage); | |
220 | |
221 crypto.Decrypt(decryptedMessage, encryptedMessage2); | |
222 ASSERT_EQ(plainTextMessage, decryptedMessage); | |
223 } | |
224 | |
225 { | |
226 // if we don't know the old key, check we can not decrypt the old message | |
227 EncryptionHelpers crypto; | |
228 crypto.SetCurrentMasterKey(2, masterKey2); | |
229 | |
230 ASSERT_THROW(crypto.Decrypt(decryptedMessage, encryptedMessage1), EncryptionException); | |
231 } | |
232 } | |
233 | |
234 | |
235 void MeasurePerformance(size_t sizeInMB, EncryptionHelpers& crypto) | |
236 { | |
237 std::string encryptedMessage; | |
238 std::string decryptedMessage; | |
239 | |
240 { | |
153 | 241 const std::string largePlainText(sizeInMB * 1024 * 1024, 'A'); |
242 | |
1 | 243 auto start = boost::posix_time::microsec_clock::local_time(); |
244 crypto.Encrypt(encryptedMessage, largePlainText); | |
245 | |
246 auto end = boost::posix_time::microsec_clock::local_time(); | |
247 boost::posix_time::time_duration diff = end - start; | |
248 std::cout << "encryption of " << sizeInMB << " MB file took " << diff.total_milliseconds() << " ms" << std::endl; | |
249 } | |
250 | |
251 { | |
252 auto start = boost::posix_time::microsec_clock::local_time(); | |
253 crypto.Decrypt(decryptedMessage, encryptedMessage); | |
254 | |
255 auto end = boost::posix_time::microsec_clock::local_time(); | |
256 boost::posix_time::time_duration diff = end - start; | |
257 std::cout << "decryption of " << sizeInMB << " MB file took " << diff.total_milliseconds() << " ms" << std::endl; | |
258 } | |
259 | |
260 } | |
261 | |
262 TEST(EncryptionHelpers, Performance) | |
263 { | |
264 CryptoPP::SecByteBlock masterKey; | |
265 EncryptionHelpers::GenerateKey(masterKey); | |
266 | |
267 EncryptionHelpers crypto; | |
268 crypto.SetCurrentMasterKey(1, masterKey); | |
269 | |
270 MeasurePerformance(1, crypto); | |
271 MeasurePerformance(10, crypto); | |
272 // MeasurePerformance(100, crypto); | |
273 // MeasurePerformance(400, crypto); | |
274 } |