]> git.stg.codes - stg.git/blob - tests/test_crypto.cpp
Fix sizeof.
[stg.git] / tests / test_crypto.cpp
1 #define BOOST_TEST_MODULE STGCrypto
2
3 #include "stg/blowfish.h"
4
5 #pragma GCC diagnostic push
6 #pragma GCC diagnostic ignored "-Wold-style-cast"
7 #pragma GCC diagnostic ignored "-Wunused-parameter"
8 #pragma GCC diagnostic ignored "-Wsign-compare"
9 #pragma GCC diagnostic ignored "-Wparentheses"
10 #include <boost/test/unit_test.hpp>
11 #pragma GCC diagnostic pop
12
13 #include <string>
14 #include <vector>
15 #include <array>
16
17 namespace
18 {
19
20 void dumpCheckCtx(const std::string& nameA, const std::string& nameB, const BLOWFISH_CTX& a, const BLOWFISH_CTX& b)
21 {
22     printf("CTX '%s':                                            CTX '%s':\n", nameA.c_str(), nameB.c_str());
23     const auto* ptrA = reinterpret_cast<const uint8_t*>(&a);
24     const auto* ptrB = reinterpret_cast<const uint8_t*>(&b);
25     size_t row = 0;
26     while (row * 16 < sizeof(a))
27     {
28         for (size_t j = 0; j < 8; ++j)
29             if (row * 16 + j < sizeof(a))
30             {
31                 if (ptrA[row * 16 + j] == ptrB[row * 16 + j])
32                     printf(" %02x", static_cast<unsigned>(ptrA[row * 16 + j]));
33                 else
34                     printf(" \033[31m%02x\033[0m", static_cast<unsigned>(ptrA[row * 16 + j]));
35             }
36             else
37                 printf("   ");
38         printf(" ");
39         for (size_t j = 0; j < 8; ++j)
40             if (row * 16 + j + 8 < sizeof(a))
41             {
42                 if (ptrA[row * 16 + j + 8] == ptrB[row * 16 + j + 8])
43                     printf(" %02x", static_cast<unsigned>(ptrA[row * 16 + j + 8]));
44                 else
45                     printf(" \033[31m%02x\033[0m", static_cast<unsigned>(ptrA[row * 16 + j + 8]));
46             }
47             else
48                 printf("   ");
49         printf(" | ");
50         for (size_t j = 0; j < 8; ++j)
51             if (row * 16 + j < sizeof(b))
52             {
53                 if (ptrA[row * 16 + j] == ptrB[row * 16 + j])
54                     printf(" %02x", static_cast<unsigned>(ptrB[row * 16 + j]));
55                 else
56                     printf(" \033[31m%02x\033[0m", static_cast<unsigned>(ptrB[row * 16 + j]));
57             }
58             else
59                 printf("   ");
60         printf(" ");
61         for (size_t j = 0; j < 8; ++j)
62             if (row * 16 + j + 8 < sizeof(b))
63             {
64                 if (ptrA[row * 16 + j + 8] == ptrB[row * 16 + j + 8])
65                     printf(" %02x", static_cast<unsigned>(ptrB[row * 16 + j + 8]));
66                 else
67                     printf(" \033[31m%02x\033[0m", static_cast<unsigned>(ptrB[row * 16 + j + 8]));
68             }
69             else
70                 printf("   ");
71         printf("\n");
72         ++row;
73     }
74     printf("\n\n");
75 }
76
77 bool equalCtx(const BLOWFISH_CTX& a, const BLOWFISH_CTX& b)
78 {
79     for (size_t i = 0; i < 18; ++i)
80         if (a.P[i] != b.P[i])
81         {
82             printf("Failed for P at %ld: 0x%x != 0x%x\n", i, a.P[i], b.P[i]);
83             dumpCheckCtx("a", "b", a, b);
84             return false;
85         }
86     for (size_t i = 0; i < 4; ++i)
87         for (size_t j = 0; j < 256; ++j)
88             if (a.S[i][j] != b.S[i][j])
89             {
90                 printf("Failed for S at %ld, %ld: 0x%x != 0x%x\n", i, j, a.S[i][j], b.S[i][j]);
91                 dumpCheckCtx("a", "b", a, b);
92                 return false;
93             }
94     return true;
95 }
96
97 bool equalString(const char* a, const char* b, size_t length)
98 {
99     for (size_t i = 0; i < length; ++i)
100         if (a[i] != b[i])
101         {
102             //printf("Failed at pos %d: %hhu != %hhu\n", i, a[i], b[i]);
103             return false;
104         }
105     return true;
106 }
107
108 const BLOWFISH_CTX testCtx =
109 {
110     {
111         0x84ddf841, 0xbbdc17f,
112         0x3dd821af, 0x66fdf0c,
113         0x426a2fff, 0x30223f2c,
114         0x8325f1a3, 0xf5c99ab9,
115         0x417ce6da, 0xba4c9637,
116         0xd226c646, 0xe50ea3b5,
117         0x3860f82c, 0x1565ecc8,
118         0xf4461621, 0x68058dd4,
119         0x217da3bb, 0xd6cd3ddc,
120     },
121     {
122         {
123             0x72f9dc5c, 0xf701a2e4, 0xf30e3ec3, 0x2f1c9683,
124             0x985b9801, 0xfb28c4f5, 0xf6f5c777, 0xfa7b5b24,
125             0xd761c693, 0xf53e1484, 0x8622da13, 0xb240d1b4,
126             0xd1bc58f8, 0x43f462ef, 0x6a4e97a3, 0xb0619beb,
127             0xff07b462, 0xd390c3b9, 0xe91f2840, 0x869566ae,
128             0xfebd249c, 0x567002c1, 0x342e0e8e, 0x31b01687,
129             0xd7e3a5bd, 0xa933de2e, 0xb4ef05f2, 0x33e4919f,
130             0x724e8b3e, 0xc6b9d44f, 0x849b1674, 0x90758946,
131             0x295986fa, 0xa2e11855, 0xf2099a4e, 0x42ed46f9,
132             0x597cbb6d, 0xc3691952, 0x408f873e, 0xdee0d6cf,
133             0x6570a45d, 0xc6a24b81, 0x2d3a1c24, 0x4a2edda4,
134             0x443dcfa5, 0x5971a9a5, 0xfd2258c0, 0x72c36b37,
135             0xf8696ce, 0xf2e84f7, 0xc543bf6d, 0x352818c5,
136             0xdc01f156, 0xc67bfce5, 0xdadf0cbf, 0x6194b6a9,
137             0xdb3cc22f, 0xc7873bcb, 0x9b9e622c, 0xa7869810,
138             0xcb1ba91, 0x137b1bf5, 0x3e71252b, 0x5229f33e,
139             0x30ded88f, 0x5763ef1f, 0xee7f01b8, 0xdac73dee,
140             0x2f4d1efb, 0x5f518b97, 0xe7c7d61e, 0x9d5ca3f3,
141             0xa196c020, 0x74634a7b, 0xdd1d0042, 0x454268aa,
142             0x5c610ac8, 0x277e8d2c, 0x4a8b4ee8, 0xf959f5a8,
143             0xc0c63a7e, 0xea9f2ed7, 0xaab4a699, 0x6f872358,
144             0x3927189a, 0x23ae56ac, 0xcfa5b09c, 0x9751e84,
145             0x7ec68c4b, 0x99f9502b, 0x886e764, 0x957c89a4,
146             0x48c5ddb0, 0x9f4d5b7e, 0xa84d5c52, 0xf2ae7bde,
147             0x75e6ddaf, 0xc2b45798, 0x5f846059, 0x5bf85e2a,
148             0x5f6c4382, 0xb621465a, 0x5bf2977c, 0x2c1eb17a,
149             0xbf3e5b18, 0x5f0bdcb7, 0xd6195c47, 0x2c61ec83,
150             0x2315c2da, 0x95195c86, 0x4c4f9855, 0xc5e3787d,
151             0xe5014918, 0x714dd230, 0xd2abd926, 0x9229a99e,
152             0x8306695a, 0x66f7bb54, 0x544affdc, 0x84aa830c,
153             0xad8c8db6, 0x48b29b2, 0xcf33d204, 0xc78ac51d,
154             0xacb18e60, 0x49b516ac, 0x614e2887, 0xe39e1a7c,
155             0xdb1f9652, 0x8c8378a3, 0x8cbe2070, 0x773fe7f,
156             0x899e1922, 0x12dd7b8b, 0xccf8198d, 0xcc48faff,
157             0x30ca9cab, 0x9ebd7129, 0xf254b601, 0x8ac97b38,
158             0x504804d1, 0x3568fbbe, 0x9a40bc99, 0x9c23761b,
159             0x28e26904, 0xe396e583, 0x2f2c4053, 0xa45caf19,
160             0xb7996794, 0x4c825669, 0x3f07f001, 0xf24a084b,
161             0x884888fd, 0xa7fc71f4, 0x21abeff4, 0x719ecba6,
162             0x90104162, 0x50c2b5f1, 0x86135070, 0xc9523fc,
163             0x9c219eca, 0xc12ef7f0, 0x6b3f6b8c, 0x288ee9ef,
164             0xe722f758, 0x229c0bac, 0x3d934d09, 0x4a5cf131,
165             0x14189fc9, 0x10c8aa2e, 0x74e9fe96, 0xcfc110d4,
166             0x35202bfb, 0x90bc66f1, 0x5d4c6616, 0x498927ea,
167             0x581934cc, 0x43e712a, 0x5f095b3c, 0xea0e1406,
168             0xa4921c29, 0xe8a306f0, 0xfec369ef, 0x992921c1,
169             0xcdc5abb6, 0x3ebd83fa, 0x8a7f16e9, 0xfaf73bb3,
170             0x3e8181, 0x34fc47c9, 0xdb92d725, 0x8b40795,
171             0x14273fa0, 0xd704e4f8, 0xd3435939, 0x3450c0a8,
172             0xce721627, 0x10d4a466, 0x7bc1a724, 0x5efc63b,
173             0x153ae5ef, 0xa0aa6945, 0xe5a3b8c9, 0xf0bec5ba,
174             0xde02619e, 0xd155e989, 0xf5e3a551, 0x951d9d74,
175             0x41544875, 0x6aa455e8, 0xda28a584, 0x72d17a11,
176             0x417c066c, 0xe6f82546, 0xbd258cd0, 0x8a3979b4,
177             0xe896e0d9, 0x76871921, 0x709842f1, 0x5dd3756b,
178             0x2ebe8dfb, 0x81ab8b0d, 0x4c9d635, 0x3fd75cf2,
179             0x37876ef8, 0x1c5a806f, 0x96de1b30, 0x2a8e971a,
180             0x89ac843a, 0x96c23371, 0x95ee2343, 0xffb1614f,
181             0x1116003c, 0x25b45917, 0xad0783c2, 0x4662ce95,
182             0xfc751ade, 0xa13e75ae, 0xc75010c3, 0x9170a859,
183             0x86ca62d6, 0x3acb094f, 0x87349773, 0x2765ba9b,
184             0xaaebd756, 0x7b3d6693, 0x92193a80, 0x765f4eb8,
185             0x42f88c21, 0x27ff9f9b, 0x9db88664, 0xe302e6d9,
186             0x2e8fb36e, 0xfc58b9bc, 0x3a649297, 0x76876ce1
187         },
188         {
189             0x8e3ea6e4, 0x1dd37803, 0xffd051c0, 0xec395f57,
190             0x2eae16f7, 0x821505b4, 0x7ab81f78, 0xf2c88c63,
191             0xa7655b7c, 0xc436644, 0x18e67d7c, 0xa5c8e283,
192             0x6e44a66e, 0xe7547024, 0x280ce7d0, 0x8ec06da2,
193             0x93683af4, 0x5e39165c, 0x77721743, 0x7b9d837e,
194             0x32fed2ef, 0x9726079b, 0x98a579a, 0xc51b8dc5,
195             0xedd45d61, 0xbf70e897, 0x5c8187f8, 0x83909156,
196             0x469c6dc2, 0xf6fb34f5, 0xf14e0646, 0x5e988165,
197             0x3dea4f92, 0x51b2d53c, 0xb543c4e, 0xae450075,
198             0xf9aaffee, 0x4bee49e7, 0x60ffe4f6, 0x395318e3,
199             0x8acb2b46, 0x6df56797, 0x52c9d67, 0x1ffd024a,
200             0x2499d763, 0x951c8244, 0x560ceb0, 0x5d0490a2,
201             0x7208da46, 0xfe40f9d7, 0xd2b96165, 0x696f3072,
202             0x2ba20225, 0x8758c6b4, 0x71f59b60, 0xd84d9bd3,
203             0x8bc05ab, 0xa09fa33, 0xd14769cf, 0x238c630a,
204             0x5635a041, 0x9af1f0bc, 0xa347515, 0xa90ac427,
205             0xdbcbc880, 0x9434490c, 0xcb041e0f, 0x4101f58,
206             0xcb057a30, 0x721e4a62, 0x8d59b73, 0x76d1c883,
207             0x724d675, 0x57833e73, 0xf65e3cc7, 0x4025c165,
208             0x61caa9fa, 0xecf3ffad, 0xc9ccba59, 0xbf1e78c0,
209             0xe7c9f808, 0x5f3ae0ad, 0xe338467, 0x5baf328,
210             0x77ea9442, 0x232d063c, 0x44c1f91f, 0xcfaaa06,
211             0xcd95c059, 0xf92f3f43, 0xd105984a, 0x4ea4eac2,
212             0xdb17fb06, 0xed567697, 0x896b421d, 0x40c3b884,
213             0xf3258ace, 0xbdb93689, 0xb75abba5, 0xc69f182c,
214             0x5798ae31, 0xff3ba2e7, 0x1ef383b4, 0xc689850c,
215             0xa0e850e, 0x7478b55d, 0xf0cd99e7, 0x78128b44,
216             0x99139a31, 0x191a4f69, 0x4dcb9de1, 0x80d9512c,
217             0x6dd3001a, 0xcb6f465e, 0xaa760561, 0x19cc07b3,
218             0x383c9dc4, 0x620f19d8, 0xbc621d05, 0x888061f0,
219             0x549fb42, 0xd21c5ac2, 0x4e37b133, 0xda38e932,
220             0x66ac40ae, 0x855de0db, 0x29c32890, 0xfc79f537,
221             0xea0bc017, 0xb30fd396, 0x9f6e65d0, 0x1fbd8e56,
222             0xa6a79b7b, 0x63871bcf, 0x977361fe, 0x572fc22e,
223             0xcac4d530, 0xee9631a1, 0xd66ca305, 0xd8648a0b,
224             0xa0fa4f44, 0x1b49d414, 0xb9b89101, 0xbb3dc53b,
225             0x9267697d, 0x998ee205, 0xca1a4a0b, 0x16e0c5d3,
226             0xb099860c, 0x3ec1b23b, 0x5d11b6a5, 0x78daf576,
227             0xd58d0926, 0x7e77c453, 0x42a2daf5, 0x6465314c,
228             0xa25d7243, 0x33eb1745, 0x9d662159, 0x61e86fcb,
229             0xedca8ac0, 0xc4afdc42, 0x866e5492, 0x9e72fe28,
230             0x9e5b7096, 0xb3121ab3, 0x9eafbc0d, 0xb03b48,
231             0x3a29c9d, 0x517d9ea7, 0xd12dd262, 0x8965c881,
232             0x3f1575bf, 0x811afeb2, 0xb8699338, 0xa49b7870,
233             0x8b72b9aa, 0xa56dbc23, 0xbdc66113, 0xc80baa85,
234             0x2f6e2fe2, 0xe2f32eaa, 0x4942b340, 0x3782424d,
235             0xc7f3fd93, 0x29c6dbac, 0x181c1bc1, 0x7db26d5e,
236             0x49ecc6f6, 0x7f600a7f, 0xf73b236a, 0x264d7fd1,
237             0x68bac57f, 0xd276c9e, 0x1784d004, 0xb98b5fc7,
238             0xd03f81ca, 0xd44d46a1, 0xcfbea6d1, 0x5c06d9d0,
239             0xbec1bad1, 0xa3bacc94, 0xaacc518c, 0xf8e20ef9,
240             0x8749c85a, 0xe4f865cd, 0x819fdb8a, 0x3c9924f6,
241             0xcb96ac1, 0x3a2bc069, 0x34b28508, 0xdb02aaf2,
242             0x231dccd0, 0xe088dc6a, 0x4d54c84, 0x9096e29f,
243             0x92db735a, 0x44ebca10, 0x8bbe55f2, 0x17287085,
244             0xf782f3b5, 0xc98a9a13, 0x117c0991, 0x69d34986,
245             0xa53303c9, 0x786c1b84, 0x5c174525, 0x4887510b,
246             0xaaf68a8e, 0xff4cba8f, 0xb6b66cc7, 0x1d5de8ea,
247             0xb98783a8, 0x82525dd6, 0x41200306, 0xaf354785,
248             0xb3479dc5, 0x508c2a19, 0x61a3104e, 0x5688a9f2,
249             0xca4bee32, 0x45df7f55, 0x2d2caedd, 0xdfdffc59,
250             0x911454c1, 0xd9dd2d80, 0x4e328041, 0x582ebac0,
251             0xbda7aab2, 0x10c52f26, 0x472690d0, 0x258304cf,
252             0x54e2e210, 0xd4482bf8, 0x22086561, 0x98407334
253         },
254         {
255             0x900aa5b7, 0x4e649e1f, 0xc9d635c9, 0x69ffda97,
256             0xdce34ccd, 0xf08ff29b, 0xee9360d3, 0xd9c59300,
257             0x7190589d, 0x69d75cc1, 0x9e3c3dc1, 0x45541e71,
258             0x6f1bccc2, 0x46f39b64, 0xc2622672, 0xfacf3cca,
259             0x867a7957, 0xd14995f, 0xfaa6011d, 0xaeaa8c37,
260             0x3d911958, 0x5f672f3a, 0x80ffb57, 0x7c27f22d,
261             0x295fd1f, 0x55f3a836, 0x55476bd5, 0x1d717b6b,
262             0x99b01c2, 0xf7a4359a, 0xfbf0d88f, 0x90f0a4fd,
263             0xdd1bed5d, 0x395e2267, 0x84473b44, 0x216d1700,
264             0xd91466b6, 0xf6723abc, 0xb58917f7, 0xe4055634,
265             0x5e5c94a9, 0x371a7adc, 0xa9444660, 0x9cd864c7,
266             0x942160f8, 0xfa57e367, 0x3ac900aa, 0xfed09d6c,
267             0xab1e8ce6, 0x1c3440b2, 0x2921f7ef, 0xcbc8aad9,
268             0xaee21a52, 0xb65bac8c, 0x4e24db59, 0xa0afe727,
269             0xd6a6a2ce, 0xcda6dc7c, 0xde7b8be, 0xac98135d,
270             0x61ebab33, 0xd6c91e9a, 0x42afb446, 0x5ffdebdd,
271             0x22d4df79, 0xd19e8fe7, 0xbc6b9029, 0x597ff071,
272             0xa4f6f069, 0xa41ed335, 0xa933f4c8, 0x3a4b5456,
273             0xca3df8d6, 0x91915bbd, 0x7c656515, 0x6ae7e70d,
274             0x517118c, 0x2a9565e0, 0x3046470a, 0x4d0b8c6,
275             0x788182c, 0x3bf3c684, 0x1e7a37f5, 0x45507790,
276             0xb9ab569e, 0x1d3927fe, 0x2aac516d, 0x8eb519e0,
277             0xc7563e93, 0x8572cdd1, 0xcfe62ded, 0x78eeb7d6,
278             0x152032bb, 0x5f480f75, 0xa1d06607, 0x51da64b0,
279             0xbc666eb1, 0x34ea7eec, 0x6ccff8b1, 0xd952ba,
280             0x32d16b6, 0xcbfe265e, 0x9daab452, 0xb12f4349,
281             0xceac2beb, 0x4b9787f9, 0x85719171, 0xf0b9c027,
282             0x7feeaa15, 0x990aebcc, 0x767bf1a9, 0xcd56802d,
283             0xe718ab1e, 0x661ef622, 0x3e217d8f, 0x8604b297,
284             0x712d7706, 0xff2a8240, 0xe149f2cd, 0x49c36be2,
285             0x3ac40571, 0xa041a748, 0xaa1580cb, 0x7a9a3e62,
286             0x9def968d, 0x908f3347, 0x40dddf27, 0xfd078597,
287             0x30b9d025, 0xd40d138, 0xde35af82, 0x5718e93,
288             0x529cd4ac, 0x41632ca0, 0x169c0ff3, 0xbac5b27a,
289             0x771b0fa7, 0xdcdf8bd, 0x4faf140e, 0x5aec8617,
290             0x24597031, 0x6407c4a6, 0x70fc5d31, 0x9085ff31,
291             0x8b1397d8, 0x99633b58, 0x91d5b56f, 0xfb451a1a,
292             0xe379e1fc, 0xe6ba2f3f, 0x462cb5c1, 0x1dd06ff4,
293             0x2cf546ba, 0x70800782, 0xdc27b1eb, 0x7965ad5d,
294             0x39ffac7a, 0x862ffce9, 0x569e938e, 0xd42c7cc3,
295             0x614f729b, 0x842ba8d5, 0xc4c56289, 0x77df9d86,
296             0xf51eb421, 0xec140c11, 0x2b891d79, 0x58b98151,
297             0x49955e, 0x6d2ae4cb, 0xdb9bd16f, 0x816ed489,
298             0x5187b66a, 0xe2428c32, 0x340ca67c, 0x6002c78f,
299             0xe7479dfd, 0xcc924e44, 0xc73ec342, 0xf17cfd8d,
300             0x8942a89b, 0x75d34569, 0xac22e545, 0x8375e876,
301             0x8395884a, 0x586698a6, 0x3f3d8f12, 0xdc5943d3,
302             0x66d1996b, 0xd06a5896, 0x6a8afde7, 0xb4d9accb,
303             0x17289e6a, 0xff9b64ef, 0x47c3dab, 0xfa2e58,
304             0x26790980, 0x5163e6f6, 0x65e17492, 0x62e55e79,
305             0x8af1b5a0, 0x2c72e6ba, 0x47da4a0a, 0x8a20bab4,
306             0xf3bcbd52, 0x16d2a483, 0xe5877bcf, 0x3cade4ba,
307             0xf65721a2, 0xb5815461, 0xb3fb53b5, 0xf74508db,
308             0x5c5c1af, 0x3b53868, 0xd9c6825d, 0x4a55bb36,
309             0x785ce186, 0xd5732f60, 0xd94fadb0, 0x1676d0cf,
310             0x1090d57f, 0x7bd7a227, 0xac20c78c, 0x2c68e495,
311             0xcd6478d2, 0xc7f4641c, 0x5ed5d389, 0x2cdc09ca,
312             0xff6d94d2, 0x47d4921c, 0xc3da6fc4, 0x6231f4c6,
313             0x28643ecd, 0x9367f303, 0x2319a3a2, 0x2f86fd8e,
314             0x7f13a567, 0x785e871a, 0xf88564cf, 0x27203f51,
315             0x7f7fe69a, 0xf0bbc1f4, 0x6ac32b0, 0xfa87113a,
316             0x1533dab5, 0x9e168c28, 0x621b244d, 0xc26cdca8,
317             0xe2b12ff1, 0xb1fb41d, 0x74abc0bf, 0x6faf66ed,
318             0x7f96eef7, 0x8dd96699, 0xe0afd88c, 0x5666ae1e
319         },
320         {
321             0x429f8c4d, 0x7a6f751a, 0xe1f9f104, 0x8f3352a2,
322             0xee11c98f, 0xd3e00903, 0x834b9614, 0x1ca1cd5f,
323             0xc5da95e5, 0x48aa87f9, 0xf3e28f3a, 0xb3d2f1cd,
324             0x11a99c75, 0x6fc626d4, 0x75c5dbf4, 0x9a055ab0,
325             0x878bd7d0, 0x797cb4fe, 0xb56af6f2, 0xd8dad771,
326             0xd79d651f, 0xecc885c2, 0xe36ba61b, 0x3af46b8b,
327             0xd4c4dec7, 0x1ef69803, 0x24c094f2, 0x4796f5ca,
328             0xd0b4f1a4, 0x35b71a50, 0xc93bda59, 0x88744ee4,
329             0xe435da7e, 0xc2add479, 0xf89eba5b, 0xeb7b3be7,
330             0x25abda16, 0x31bc2a9, 0xd46c3697, 0xbdab88a5,
331             0xaf765372, 0x776715c1, 0x91f39946, 0x5a46fca5,
332             0xb7345689, 0xe5ccffc6, 0x83094686, 0xd29b145,
333             0x947c5b14, 0xee6a48b9, 0xf80bcf7e, 0x80364978,
334             0x28785457, 0x7f01f829, 0x2480cb12, 0x1c4e853d,
335             0x9c5be553, 0xbc234906, 0xf48c6576, 0x20d7163e,
336             0xd13fb6aa, 0x7911e4f2, 0x74a4c545, 0xcd37db61,
337             0x923daabd, 0x3671e21f, 0x474b7b1, 0xd6c142a8,
338             0xfd74fe19, 0x967d3c94, 0x76507cd3, 0x1860dcdf,
339             0x40511fa8, 0x19feec2b, 0xa624cb9, 0x3125d93b,
340             0xc4bed3ba, 0xaa215b42, 0xa44f7c85, 0x63065ae5,
341             0xde95bfe, 0x6b6e92e8, 0xf3e516d1, 0x8bc680c8,
342             0x5fdc0cad, 0x6e7281d4, 0x1a4a5a56, 0x5edb95a3,
343             0xb91324c1, 0xbbfbce4c, 0x9e90028c, 0x4825c43d,
344             0xe5f599a4, 0xcacc8db0, 0xf1fd746b, 0x2d7baad9,
345             0xcc3148c4, 0xffefb22f, 0x9ec43aee, 0x7556ebfc,
346             0x7ce98693, 0x19eb04dd, 0xc5c3d0e2, 0x5d0c1bea,
347             0x801fe923, 0xcb47549d, 0xada4ca94, 0x953a3ff6,
348             0x92d8bc7e, 0x1a8b21fa, 0x7e7d47ca, 0x345de887,
349             0x668fc1d5, 0xb191b107, 0x29f30bbd, 0x669cdc3a,
350             0x907fad0c, 0xa8432d5d, 0x8125844d, 0x6f8ee014,
351             0x96fe6488, 0x6b812b55, 0x96e27fb5, 0xb68e0b09,
352             0x6d28e784, 0x370fff6c, 0x65c77431, 0xa6ba0fe6,
353             0x85ede90f, 0x6ebc916e, 0xd2927076, 0x8f7f7b17,
354             0x690c30f5, 0x88c8404e, 0x76b88278, 0x27eb857b,
355             0xfd20c71d, 0x53b63cc4, 0x93fb6326, 0xca3a6e63,
356             0x879e4bc7, 0x7e75fa05, 0x2dc60e82, 0xc4b2b95,
357             0xe6e17a43, 0x6e6c53a7, 0xe6baa075, 0x7b50f168,
358             0xf95e02a8, 0x3422dd2d, 0x4eb38261, 0xd17615c8,
359             0x238c1d99, 0x24def9f8, 0x250a45d1, 0xaeb97793,
360             0xd6bf65e6, 0xf4080ae1, 0x90814d9e, 0x2d9d8436,
361             0x2d742949, 0xa9afb64c, 0xdd4d18f8, 0x8855711e,
362             0x856bf7df, 0xbeb28a6b, 0xff5d789b, 0xd1e24c20,
363             0x92581004, 0xf9ee25e5, 0xdd445cff, 0xb5bc99cd,
364             0x2e90fb9d, 0x86e95fa6, 0x3ce3fc89, 0x2ccff4c9,
365             0x42b6541c, 0xf2cdb401, 0x76b6cd96, 0x5cbc0f31,
366             0x5d988c8e, 0x5535eb69, 0x1354397b, 0x5f105b42,
367             0x70359c13, 0x8053d6a9, 0x43ee0e25, 0x7279a17b,
368             0x23b44313, 0x99dbf710, 0x3671f4f1, 0x212fb10a,
369             0xd61b85ef, 0x5370eec8, 0xb03ac259, 0x53068883,
370             0x10325203, 0x2550b3b5, 0x3afa6580, 0x5945266f,
371             0xf8f21594, 0x1f2feef2, 0xcaebbffc, 0xcc55f93a,
372             0x21f277bf, 0x4e1c1b9c, 0xdd74f5a4, 0x34e84677,
373             0xb85d1834, 0xf9fdccaa, 0x5f13cc3, 0x8d1a00d9,
374             0xd3b7f623, 0x2d8886bf, 0x1505627e, 0x476ea29b,
375             0x7758fa96, 0xc59274f8, 0x5d6cd56, 0xf892f3ce,
376             0xc26a482c, 0xe00f252c, 0x38c981c3, 0x223869ac,
377             0xaa8bc73f, 0xe8ca1111, 0xed51fdd6, 0xeb0724d4,
378             0x61b05512, 0x290876c2, 0x50b855d1, 0x20cf2267,
379             0x894a6a06, 0xd87f0ab4, 0x5961c4f5, 0xdd7781a,
380             0x8f898f63, 0xe5bcfae8, 0x2ab999c3, 0x6d25c711,
381             0x11b0621c, 0x7a097422, 0xa7f23f1, 0x9fbfb450,
382             0xd5e5019b, 0xbe3427a9, 0x70967c5c, 0x18f65981,
383             0x2c9bde6b, 0xfe6f03c4, 0xc754ef9a, 0x637d1ff5,
384             0x9749ef7a, 0xae0c1da6, 0x4e05c89c, 0x87e4150a
385         }
386     }
387 };
388
389 const unsigned char testString[] = { 68, 100, 2, 115, 6, 54, 226, 228 };
390
391 } // namespace anonymous
392
393 BOOST_AUTO_TEST_SUITE(Crypto)
394
395 BOOST_AUTO_TEST_CASE(ContextCreation)
396 {
397     BLOWFISH_CTX ctx;
398     InitContext("pr7Hhen", 7, &ctx);
399     BOOST_CHECK(equalCtx(ctx, testCtx));
400 }
401
402 BOOST_AUTO_TEST_CASE(Encryption)
403 {
404     BLOWFISH_CTX ctx;
405     InitContext("pr7Hhen", 7, &ctx);
406     uint32_t a = 0x12345678;
407     uint32_t b = 0x87654321;
408     Blowfish_Encrypt(&ctx, &a, &b);
409
410     BOOST_CHECK_EQUAL(a, 0xd3988cd);
411     BOOST_CHECK_EQUAL(b, 0x7996c6d6);
412 }
413
414 BOOST_AUTO_TEST_CASE(Decryption)
415 {
416     BLOWFISH_CTX ctx;
417     InitContext("pr7Hhen", 7, &ctx);
418     uint32_t a = 0xd3988cd;
419     uint32_t b = 0x7996c6d6;
420     Blowfish_Decrypt(&ctx, &a, &b);
421
422     BOOST_CHECK_EQUAL(a, 0x12345678);
423     BOOST_CHECK_EQUAL(b, 0x87654321);
424 }
425
426 BOOST_AUTO_TEST_CASE(BlockEncryption)
427 {
428     BLOWFISH_CTX ctx;
429     InitContext("pr7Hhen", 7, &ctx);
430     uint32_t block[2] = {0x12345678, 0x87654321};
431     EncryptBlock(&block, &block, &ctx);
432
433     BOOST_CHECK_EQUAL(block[0], 0xd3988cd);
434     BOOST_CHECK_EQUAL(block[1], 0x7996c6d6);
435 }
436
437 BOOST_AUTO_TEST_CASE(BlockDecryption)
438 {
439     BLOWFISH_CTX ctx;
440     InitContext("pr7Hhen", 7, &ctx);
441     uint32_t block[2] = {0xd3988cd, 0x7996c6d6};
442     DecryptBlock(&block, &block, &ctx);
443
444     BOOST_CHECK_EQUAL(block[0], 0x12345678);
445     BOOST_CHECK_EQUAL(block[1], 0x87654321);
446 }
447
448 BOOST_AUTO_TEST_CASE(StringEncryption)
449 {
450     BLOWFISH_CTX ctx;
451     InitContext("pr7Hhen", 7, &ctx);
452     char res[8];
453     EncryptString(res, "testtest", 8, &ctx);
454
455     BOOST_CHECK(equalString(res, reinterpret_cast<const char*>(testString), 8));
456 }
457
458 BOOST_AUTO_TEST_CASE(LongStringEncryption)
459 {
460     BLOWFISH_CTX ctx;
461     InitContext("pr7Hhen", 7, &ctx);
462     const std::string source("abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ");
463     std::vector<char> longTest(source.length() + 8);
464     EncryptString(longTest.data(), source.c_str(), source.length() + 1, &ctx);
465     DecryptString(longTest.data(), longTest.data(), longTest.size(), &ctx);
466
467     BOOST_CHECK_EQUAL(source, std::string(longTest.data()));
468 }
469
470 BOOST_AUTO_TEST_CASE(OldStringEncryption)
471 {
472     BLOWFISH_CTX ctx;
473     InitContext("123456", 7, &ctx);
474     const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39,
475                                     0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb,
476                                     0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb,
477                                     0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb};
478     std::array<char, 32> res{};
479     DecryptString(res.data(), source, res.size(), &ctx);
480
481     BOOST_CHECK_EQUAL(std::string(res.data()), "admin");
482 }
483
484 BOOST_AUTO_TEST_CASE(NewStringEncryption)
485 {
486     BLOWFISH_CTX ctx;
487     InitContext("123456", 7, &ctx);
488     const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39,
489                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
492     std::array<char, 32> res{};
493     DecryptString(res.data(), source, 32, &ctx);
494
495     BOOST_CHECK_EQUAL(std::string(res.data()), "admin");
496 }
497
498 BOOST_AUTO_TEST_SUITE_END()