1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <inttypes.h>
33
34 #include "crypto.h"
35
36 #include <cx/string.h>
37 #include <cx/utils.h>
38 #include <cx/buffer.h>
39 #include <libidav/utils.h>
40 #include <libidav/crypto.h>
41
42 static DavKey keys256[
16];
43 static DavKey keys128[
16];
44
45 static char *strings[] = {
46 "a",
47 "ab",
48 "abc",
49 "abcd",
50 "a string",
51 "hello world",
52 "0 1",
53 "0123 abcd XXXX <>",
54 "libidav+dav+dav-sync",
55 "R2xd1dI9o0HHd3aDuQrvuPyB",
56 "7XxUqWvOvQOFo0SlKyEWzerR",
57 "Ert2g8bjjyKpAebBNxdaJ5o7",
58 "C0jReDRLHxF6MW1stR00OFiF",
59 "eRD76vIQIUNRRFrnUrGtTI13",
60 "VmfdNDkSzLKiEgCkWQ4Es4XaiY7TlW3LCm1gt3r66uJ0TeQm",
61 "AdeoBAIo8Q54yO1Pwe1WH3rgDe4m5ZBUSgNUlrNqGjtN4UNN",
62 "Ajkt3kmdG9uPRi41Q5299yuW9DJNKc0yGlF08K6PfodpYyOxzOiL7TbeDML0RbJAOaL2fpCC",
63 "mmGmeUTxxAEyhx1Qw9CAodQ550rQpQhSR4ZuC4im9SDSl5ykGJOEJrdrTPciaIdyY9M6WSmg",
64 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
65 "--aabb2234000000001ffffffffffffffffas"
66 "very long string ..............................................."
67 "----------------------------------------------------------------"
68 "0000000000000000000000000000000000000000000000000000000000000000"
69 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
70 };
71
72 static char *cryptkey0aes256strings[] = {
73 "UtIyzMm3e39YFEccglOxyJ4WPlqA01tohXCJSeaVKIA=",
74 "rfHc8KMEp9HoGAHz08gyne+YH053EIoiheacHe0yiOY=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us7kqqQuITRoCicaykYoGA==",
115 "2yVLOZEMpy2kXSqtmgGJ10FwIVs1I8sPkDuSQmSb3nwBw5NA96A7ul9N9Z8Qxr7oxiYi+1ZADEdKRUOlIbntSHel6zW8XJAdnP8fqfV/HFv6DdwNbdc9Ofm3PkfkKcudkLhofGr8Tma5kIyESGmA7jLUrLLPtXvM4jneCZoUh/DpWwTcfgeaAuujNO/FPSJsheEZu4ck2wv5qvlYaQx716xkTsXugnpecOSOq5uQkqmSGI0O+aVJ8sgel/JsiGEJew3JDh+jnuK/lKHnsKxhHcX/ZX+OluGK0KbCHYM5RWl5Fb2zJA1SDaDneFqgOqLdSryBOKJeLoNdiFsL7LQY+RK/ElUZn07QoQDLk+xJwFMbF/KTH9KjWA3vEVdh9C0Wc02Jf3AwpaRmrq9/q62dhm+bc+I4NRH1BmROll8PS5Y="
116 };
117
118 static char *cryptkey0aes128strings[] = {
119 "yWwheGMLOFi7bUia+105EHpuS2Tv4shfRZh3T0CaXts=",
120 "wdpntIG/wpTGUOzgt7Pi6GSnc8dVOtvHbjyF6h1p1LY=",
121 "IJnMdm2WinhlDsQItx/DSW3CFygtEtxAw7cEacoP9j8=",
122 "vd3IRIru28Xb35+3jKA7PfftbBMvF2z/Cb4e7/2e1vE=",
123 "ub3TJHUtIsc2Sb1ZeNDfcDYEq9qDcAb6j+qm7Xyg1VA=",
124 "tXgBCUyxvh7AXiw5ysbMM75v36aeoDniDLUjH723xsA=",
125 "8g/Vj58LmuGz64biZiA+Oz8nG/e7s0EyF3kcWd5Lmgy86K+AG8c4FlQiAHk7Bscb",
126 "BuEFNArQgT2uSrK6IsXeFiGlSz9Y3Sh1O7Xgqy6TzGt3XKx6Ixubv7sU8Xo0nKDz",
127 "jjr0Qliga7PjqJ4rdTqCbd6p3jt3tIQ0xCftctoxaPFZQuiumWT47CxV+alclgV9",
128 "G0rsiGmM4HZlpnkrPdjEI2+K1d/unmn2QAkl8I68j2+rswPY9VY1zPsRV3x0SfVZ",
129 "Wao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SXBhtUcxaMxDEWTFK67pqMT6q51t2BV83eAp/BXkis8wpamhYqdv0yiSt4yamLm8IecGfPsHgfCcrnTtK220xbVBRvHMEbP3gui1nApB2vQiweE6Gk5IXIgwN+P/YrihjUzKtJgqCoQ0FigNTQemUGkNalg9+LxiXSg4AFundZQMaRNMNJA/MRKKvHcCps6POwENtYfBY="
139 };
140
141 static char *cryptkey1aes128strings[] = {
142 "jJyZsBeggvHnPFCt4ABzJPPI88RHWXLREw760Pwh5VU=",
143 "R+NDojkR6jmLQBKNk0lu11Ks5OpcOhhnNEh1st5bUB8=",
144 "ZPjJ09PT65Nb/avJZ/rKiKdTQh2EqFj8C4SCggP8Tdo=",
145 "Wq4poohvLvgTH07olEbU+1DJ+X5R+rxpeWwHJjeD6/w=",
146 "mJy65R4TKUIaavlMi06zNxEWHFe+emV9y/EpqGfiR4c=",
147 "X9ukqaTv+yl8P0xq3+lsSnq0Kq2Vra9QStpT7NWrSUo=",
148 "2wZ4OBX6UlQ8BKdqVgM4zxfdHhh/GPdWqob0/ryBz9mp4w15omTd1Yy8+1xZ/saz",
149 "5TV7mnMX48QTjJ3mvn1QQnQCj8OfMbx4mRy1r+OJS/p5lPB3kzGaikaxhtVPuOIU",
150 "wm5Wx9IhOwteBXjoO4UI79gxBd8LHYfp98akutwmOqYLSz2mJ1ZJPjhlu+fB84Jc",
151 "3m+JFgTgi0djCnU/hcsFFh4AefpkJ5S1ucX1EDhukm0Dbmxtlw8SWO6sIyA8UsUp",
152 "G96QAM7L9XHn7/z50CAPadqDEyxHQvJJvvHkiL9VZ7LA2WYfm6/gmJIOPgMZfSxT",
153 "ZxnZLW2M2P1pYIS6JcTwG3qIA5NlcFsLZvQWpiDTwSGZWcOq5mv+fo6i+sDFu3Gl",
154 "jXgcKe5k6+B7Jgv5erI571JoR39QlaCtv7IJbYb3Y8C1jTq1DLLgYM7mvBiOaCDT",
155 "rUWtaNg3xqeRqKl/F6Pjg0C6+uPLOX1DXVwwql6Izer89E1vQTGGpjvN5O0UmWNb",
156 "LRS2XeeIGPzuMFn3ClMS6HRqnfKqmNQcJx41jkL0JitnufZtnyQYojp4LRf331Z0uE3ZCxwCMxJ+GqX8CLpbR6KVWEdhBmnOXjWFY1nZsiw=",
157 "jg4aA6BJ7RVy4l9sLz/WPGZfSfUxnFbmfGErnMy6LWJGIQzPvISHkLY6pSH3FSMZMUtnLLDW8KVIGstt5lo8AcflSryGrOwvzm+88HUO5Z8=",
158 "hkS1WQs4SvLTzr95MaIzV56inCkHMWAt1prL6mzv9UadUYEkpPQK/w3Sq9N/3oeNCb21TqF1I9tAbWJOhRqTj098EnmnmJVI6p2mfRESEBEJULBYKsdQPE85CdL2b0Sh",
159 "wscy5Q5mi1l8tdA/8v6MrdFQY1PuS10uQNFe/FXnnJS/MRcbcA/R9wlLuWiMt7cUg9Zs74lXc8w7tKkOrSX6zMOHu3hpDjCJA+ZE9gkbP70KIGRnddW5aFhXNy/Sz808",
160 "92S3C7m5lQnj5Gpjz9zbPyYdPO5j03b/AZ5sSPHbuM/HIlIfcj289sNnFz08MLgDSxLqZsJnlVjLLahWWo1KSA==",
161 "0WQozX4DdbVUATNnxn8kzkG30QGLB0kCUMmm1wdbPCqCWzOqo/id98xh++8PHfD/gmOHwIeZgcW6PTsIT7KI17LRq+ryHI8xdRYMW/oPzXQcaKCis0MbBYpaS9OZp6Wbrkbk9NUShqV340DdydBCtl3RXGhEJLyLWqF2XTyuXNL4srNPFP934rHLSSkU7eUD7FzNiE7mTgQ73E5o/h8porPRtNRnbH1hT5SZK71qC8pRlJjMSVBdMgNHfOJDvjFd0Xv+QwDqm3QqcrpirYhkP5M1ofiwBsuFNra1bnojMPiyXgduhUpxsJ0StvBlzEixrtA2NsxfEgDtrJik8ScOQ7GkB8alHPn3jEFa8KsucyQwxuoJ7W1pIzBYKQqZ3QU3I75aSZ5mAM8PxVVxLdC75iZuBe1PvUNle/bljmKJ3QA="
162 };
163
164 static char *d0 =
"01234567890123456789012345678901";
165 static char *d1 =
"gfXa3IJa;.d-afHS1237.:_A12=$%&%!";
166 static char *d2 =
"%$dhXfdhgf(z@pWqoiv)Zal=di}weQ<c";
167 static char *d3 =
"%$dhXfdhgf(z@pWqoiv)Zal=di}weQ<c";
168 static char *d4 =
"<>/(!''§)._,;*++-<Qq@,.=)($%§1aX:";
169 static char *d5 =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
170 static char *d6 =
".-.-.-.-.-.-.-.-.-$$$$$$$$$$$$$$";
171 static char *d7 =
"uAh281._;(12<?2@@$2..;---assf34v";
172 static uint32_t d8[] = {
17635,
0,
4274927276,
76546,
6098723,
516,
4193367221,
1224964255};
173 static uint32_t d9[] = {
2234966275,
643123,
427492276,
3334964712,
1337123,
722464014,
4193367111,
3234966518};
174 static uint32_t d10[] = {
3231664275,
1578414324,
427496129,
4034960712,
93371873,
1722499994,
4193300111,
8765678};
175 static uint32_t d11[] = {
764564,
12348792,
999432723,
128943278,
664512343,
84327634,
709712334};
176 static uint8_t d12[] = {
14,
94,
240,
253,
14,
0,
123,
94,
72,
197,
71,
56,
64,
156,
204,
231,
17,
18,
101,
24,
200,
15,
208,
150,
1,
85,
171,
18,
205,
30,
231,
7};
177 static uint8_t d13[] = {
25,
43,
219,
253,
14,
0,
223,
94,
87,
127,
171,
56,
34,
26,
204,
31,
64,
18,
151,
124,
233,
12,
118,
154,
1,
185,
11,
18,
25,
30,
231,
207};
178 static uint8_t d14[] = {
15,
0,
220,
253,
14,
5,
223,
234,
157,
157,
121,
56,
71,
216,
254,
31,
61,
192,
151,
255,
0,
12,
118,
14,
5,
185,
124,
18,
25,
54,
131,
227};
179 static uint8_t d15[] = {
0,
43,
210,
25,
14,
0,
243,
94,
87,
0,
1,
156,
234,
6,
214,
31,
64,
18,
151,
124,
3,
12,
118,
154,
1,
18,
111,
18,
225,
31,
251,
207};
180
181 static void create_keys() {
182 void *data[
16] = {d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15};
183
184 for(
int i=
0;i<
16;i++) {
185 keys256[i].length =
32;
186 keys256[i].type =
DAV_KEY_AES256;
187 keys256[i].data = data[i];
188
189 keys128[i].length =
16;
190 keys128[i].type =
DAV_KEY_AES128;
191 keys128[i].data = data[i];
192 }
193 }
194
195 UCX_TEST(test_util_decrypt_str_k) {
196 create_keys();
197
198 DavContext *ctx = dav_context_new();
199 DavSession *sn = dav_session_new(ctx,
"http://localhost/");
200
201 UCX_TEST_BEGIN;
202
203 for(
int k=
0;k<
2;k++) {
204 for(
int i=
0;i<
20;i++) {
205 char *c1;
206 char *c2;
207 if(k ==
0) {
208 c1 = cryptkey0aes128strings[i];
209 c2 = cryptkey0aes256strings[i];
210 }
else {
211 c1 = cryptkey1aes128strings[i];
212 c2 = cryptkey1aes256strings[i];
213 }
214
215 char *s128 = util_decrypt_str_k(sn, c1, &keys128[k]);
216 char *s256 = util_decrypt_str_k(sn, c2, &keys256[k]);
217
218 UCX_TEST_ASSERT(!strcmp(s128, strings[i]),
"s128 wrong");
219 UCX_TEST_ASSERT(!strcmp(s256, strings[i]),
"s256 wrong");
220 }
221 }
222
223 UCX_TEST_END;
224
225 dav_context_destroy(ctx);
226 }
227
228 UCX_TEST(test_util_encrypt_str_k) {
229 DavContext *ctx = dav_context_new();
230 DavSession *sn = dav_session_new(ctx,
"http://localhost/");
231
232 UCX_TEST_BEGIN;
233
234 for(
int k=
0;k<
16;k++) {
235 for(
int i=
0;i<
20;i++) {
236 char *aes128 = util_encrypt_str_k(sn, strings[i], &keys128[k]);
237 char *aes256 = util_encrypt_str_k(sn, strings[i], &keys256[k]);
238
239 char *dec1 = util_decrypt_str_k(sn, aes128, &keys128[k]);
240 char *dec2 = util_decrypt_str_k(sn, aes256, &keys256[k]);
241
242 UCX_TEST_ASSERT(!strcmp(dec1, strings[i]),
"aes128 encrypt failed");
243 UCX_TEST_ASSERT(!strcmp(dec2, strings[i]),
"aes256 encrypt failed");
244 }
245 }
246
247 UCX_TEST_END;
248 }
249
250 UCX_TEST(test_crypto_buffer) {
251 UCX_TEST_BEGIN;
252
253 for(
int i=
0;i<
32;i++) {
254 DavKey *key = i <
16 ? &keys256[i] : &keys128[i%
16];
255
256 for(
int j=
0;j<
20;j++) {
257 CxBuffer *content = cxBufferCreate(
NULL,
256, cxDefaultAllocator,
CX_BUFFER_FREE_CONTENTS|
CX_BUFFER_AUTO_EXTEND);
258 cxBufferPutString(content, strings[j]);
259 content->pos =
0;
260
261 CxBuffer *enc = aes_encrypt_buffer(content, key);
262 UCX_TEST_ASSERT(enc->size >= content->size +
16,
"aes_encrypt_buffer failed");
263
264 char *base64 = util_base64encode(enc->space, enc->size);
265 size_t plainlen =
0;
266 char *plain = aes_decrypt(base64, &plainlen, key);
267
268 UCX_TEST_ASSERT(plain,
"aes_decrypt failed");
269 UCX_TEST_ASSERT(plainlen == content->size,
"aes_decrypt: wrong length");
270 UCX_TEST_ASSERT(!memcmp(plain, content->space, plainlen),
"aes_decrypt: wrong content");
271
272 CxBuffer *dec = aes_decrypt_buffer(enc, key);
273 UCX_TEST_ASSERT(dec->size == content->size,
"aes_decrypt_buffer failed");
274
275 UCX_TEST_ASSERT(!memcmp(content->space, dec->space, dec->size),
"decrypted buffer has wrong content");
276
277 cxBufferFree(content);
278 cxBufferFree(enc);
279 cxBufferFree(dec);
280 free(base64);
281 free(plain);
282 }
283 }
284
285 UCX_TEST_END;
286 }
287
288 UCX_TEST(test_crypto_stream) {
289 CxBuffer *data = cxBufferCreate(
NULL,
1024, cxDefaultAllocator,
CX_BUFFER_FREE_CONTENTS|
CX_BUFFER_AUTO_EXTEND);
290 CxBuffer *cbuf = cxBufferCreate(
NULL,
1024, cxDefaultAllocator,
CX_BUFFER_FREE_CONTENTS|
CX_BUFFER_AUTO_EXTEND);
291 CxBuffer *pbuf = cxBufferCreate(
NULL,
1024, cxDefaultAllocator,
CX_BUFFER_FREE_CONTENTS|
CX_BUFFER_AUTO_EXTEND);
292
293 UCX_TEST_BEGIN;
294
295 for(
int i=
0;i<
32;i++) {
296 DavKey *key = i <
16 ? &keys256[i] : &keys128[i%
16];
297 for(
int j=
0;j<
20;j++) {
298 data->pos =
0;
299 data->size =
0;
300 size_t slen = strlen(strings[j]);
301 cxBufferWrite(strings[j],
1, slen, data);
302 cxBufferSeek(data,
0,
SEEK_SET);
303
304 cbuf->pos =
0;
305 cbuf->size =
0;
306 pbuf->pos =
0;
307 pbuf->size =
0;
308
309 AESEncrypter *enc = aes_encrypter_new(key, data, (dav_read_func)cxBufferRead,
NULL);
310 char buf[
1024];
311 size_t r =
0;
312 while((r = aes_read(buf,
1,
1024, enc)) !=
0) {
313 cxBufferWrite(buf,
1, r, cbuf);
314 }
315 aes_encrypter_close(enc);
316
317 AESDecrypter *dec = aes_decrypter_new(key, pbuf, (dav_write_func)cxBufferWrite);
318 aes_write(cbuf->space,
1, cbuf->pos, dec);
319 aes_decrypter_shutdown(dec);
320 aes_decrypter_close(dec);
321
322 UCX_TEST_ASSERT(slen == pbuf->pos,
"wrong length after enc-dec");
323 UCX_TEST_ASSERT(!memcmp(strings[j], pbuf->space, slen),
"wrong content after enc-dec");
324
325 data->pos =
0;
326 CxBuffer *enc2 = aes_encrypt_buffer(data, key);
327 CxBuffer *dec2 = aes_decrypt_buffer(enc2, key);
328
329 UCX_TEST_ASSERT(dec2->size == data->size,
"dec2 has wrong size");
330 UCX_TEST_ASSERT(!memcmp(strings[j], dec2->space, dec2->size),
"dec2 has wrong content");
331 }
332 }
333
334 UCX_TEST_END;
335 }
336
337 static char *pws[] = {
338 "1234",
339 "abdefgh",
340 "AAAAAAAAAAAAAAAAA",
341 "5478FFJD:Loire923!$$123sd;fhgfdee432ASDAfd432"
342 };
343
344 static uint8_t salt[][
16] = {
345 {
1,
10,
100,
222},
346 {
123,
12,
92,
14,
11,
0,
255,
190 },
347 {
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12 },
348 {
100,
240,
1,
190,
17,
0,
189,
50,
31,
13,
37,
25,
01,
0,
0,
7}
349 };
350 static int saltlen[] = {
4,
8,
12,
16 };
351
352 static uint8_t pwgenkeys[][
32] = {
353
354 {
94,
67,
10,
1,
46,
198,
81,
162,
236,
220,
211,
196,
141,
127,
122,
39 },
355 {
94,
67,
10,
1,
46,
198,
81,
162,
236,
220,
211,
196,
141,
127,
122,
39,
96,
93,
105,
0,
131,
184,
21,
87,
165,
76,
247,
96,
74,
130,
90,
81 },
356 {
23,
185,
87,
96,
109,
149,
18,
87,
24,
246,
70,
196,
142,
67,
187,
156 },
357 {
23,
185,
87,
96,
109,
149,
18,
87,
24,
246,
70,
196,
142,
67,
187,
156,
131,
104,
108,
119,
175,
221,
108,
220,
75,
83,
91,
205,
157,
142,
132,
175 },
358 {
224,
80,
236,
51,
156,
38,
77,
11,
198,
205,
190,
210,
193,
40,
188,
215 },
359 {
224,
80,
236,
51,
156,
38,
77,
11,
198,
205,
190,
210,
193,
40,
188,
215,
247,
201,
231,
19,
243,
61,
20,
166,
155,
147,
234,
3,
148,
240,
255,
190 },
360 {
29,
209,
169,
101,
7,
80,
162,
82,
196,
20,
110,
36,
66,
41,
189,
16 },
361 {
29,
209,
169,
101,
7,
80,
162,
82,
196,
20,
110,
36,
66,
41,
189,
16,
65,
37,
90,
2,
226,
228,
201,
21,
87,
230,
115,
201,
87,
157,
53,
251 },
362 {
107,
129,
5,
22,
175,
18,
160,
61,
29,
249,
96,
108,
136,
1,
238,
168 },
363 {
107,
129,
5,
22,
175,
18,
160,
61,
29,
249,
96,
108,
136,
1,
238,
168,
32,
208,
199,
177,
44,
251,
145,
195,
189,
18,
119,
161,
167,
101,
248,
83 },
364 {
14,
193,
58,
165,
156,
202,
237,
35,
113,
184,
24,
180,
221,
81,
213,
62 },
365 {
14,
193,
58,
165,
156,
202,
237,
35,
113,
184,
24,
180,
221,
81,
213,
62,
211,
25,
211,
30,
47,
253,
168,
169,
84,
31,
162,
185,
75,
136,
22,
244 },
366 {
83,
193,
218,
177,
173,
5,
134,
61,
45,
14,
141,
225,
194,
114,
91,
221 },
367 {
83,
193,
218,
177,
173,
5,
134,
61,
45,
14,
141,
225,
194,
114,
91,
221,
61,
56,
69,
123,
1,
149,
247,
141,
71,
48,
55,
182,
237,
32,
20,
20 },
368 {
223,
221,
209,
7,
249,
39,
34,
180,
121,
44,
144,
153,
47,
188,
104,
33 },
369 {
223,
221,
209,
7,
249,
39,
34,
180,
121,
44,
144,
153,
47,
188,
104,
33,
29,
159,
51,
8,
200,
85,
137,
129,
97,
255,
26,
23,
147,
133,
11,
240 },
370 {
142,
13,
212,
33,
112,
149,
245,
39,
20,
237,
141,
46,
35,
253,
228,
43 },
371 {
142,
13,
212,
33,
112,
149,
245,
39,
20,
237,
141,
46,
35,
253,
228,
43,
219,
69,
181,
28,
235,
85,
202,
135,
6,
215,
130,
67,
235,
114,
91,
79 },
372 {
53,
10,
18,
131,
28,
151,
84,
93,
2,
102,
69,
224,
174,
140,
2,
168 },
373 {
53,
10,
18,
131,
28,
151,
84,
93,
2,
102,
69,
224,
174,
140,
2,
168,
213,
236,
43,
135,
148,
195,
134,
160,
143,
216,
109,
167,
150,
72,
162,
22 },
374 {
26,
199,
244,
1,
219,
53,
16,
109,
121,
230,
129,
204,
69,
120,
246,
83 },
375 {
26,
199,
244,
1,
219,
53,
16,
109,
121,
230,
129,
204,
69,
120,
246,
83,
93,
9,
117,
21,
49,
67,
56,
255,
147,
16,
18,
39,
245,
93,
20,
201 },
376 {
195,
217,
248,
6,
86,
212,
115,
132,
32,
81,
238,
97,
116,
173,
176,
250 },
377 {
195,
217,
248,
6,
86,
212,
115,
132,
32,
81,
238,
97,
116,
173,
176,
250,
207,
152,
10,
150,
113,
90,
246,
88,
41,
9,
195,
244,
70,
190,
165,
251 },
378 {
60,
114,
156,
195,
119,
103,
167,
179,
70,
3,
11,
189,
25,
27,
175,
30 },
379 {
60,
114,
156,
195,
119,
103,
167,
179,
70,
3,
11,
189,
25,
27,
175,
30,
115,
175,
250,
36,
181,
250,
22,
148,
210,
198,
36,
182,
207,
10,
150,
104 },
380 {
204,
63,
177,
165,
41,
0,
40,
99,
117,
9,
245,
59,
176,
66,
3,
234 },
381 {
204,
63,
177,
165,
41,
0,
40,
99,
117,
9,
245,
59,
176,
66,
3,
234,
106,
50,
241,
16,
254,
158,
60,
115,
221,
22,
141,
140,
220,
241,
69,
59 },
382 {
253,
68,
7,
137,
52,
78,
146,
207,
4,
68,
42,
89,
166,
74,
108,
34 },
383 {
253,
68,
7,
137,
52,
78,
146,
207,
4,
68,
42,
89,
166,
74,
108,
34,
183,
51,
183,
187,
192,
254,
20,
197,
159,
173,
10,
2,
137,
81,
148,
147 },
384 {
242,
67,
91,
24,
61,
250,
13,
44,
131,
236,
224,
118,
37,
250,
67,
160 },
385 {
242,
67,
91,
24,
61,
250,
13,
44,
131,
236,
224,
118,
37,
250,
67,
160,
57,
53,
134,
248,
210,
25,
218,
240,
124,
195,
151,
234,
220,
136,
193,
14 },
386 {
226,
55,
98,
79,
192,
117,
212,
68,
109,
46,
58,
233,
95,
180,
2,
154 },
387 {
226,
55,
98,
79,
192,
117,
212,
68,
109,
46,
58,
233,
95,
180,
2,
154,
135,
111,
74,
105,
94,
113,
77,
37,
133,
53,
72,
43,
15,
131,
69,
209 },
388 {
36,
177,
96,
156,
175,
123,
208,
63,
203,
224,
204,
179,
152,
35,
5,
115 },
389 {
36,
177,
96,
156,
175,
123,
208,
63,
203,
224,
204,
179,
152,
35,
5,
115,
142,
221,
213,
107,
223,
91,
99,
91,
126,
121,
22,
199,
195,
202,
123,
181 },
390 {
223,
189,
161,
102,
183,
210,
183,
174,
247,
101,
81,
10,
73,
169,
246,
255 },
391 {
223,
189,
161,
102,
183,
210,
183,
174,
247,
101,
81,
10,
73,
169,
246,
255,
248,
174,
81,
251,
70,
4,
221,
232,
147,
106,
138,
118,
156,
196,
58,
244 },
392 {
175,
105,
140,
110,
225,
120,
1,
72,
76,
149,
8,
203,
116,
79,
187,
131 },
393 {
175,
105,
140,
110,
225,
120,
1,
72,
76,
149,
8,
203,
116,
79,
187,
131,
218,
82,
5,
84,
22,
185,
236,
252,
165,
72,
154,
6,
50,
0,
89,
33 },
394 {
90,
125,
87,
16,
162,
186,
98,
211,
44,
165,
26,
118,
138,
83,
6,
121 },
395 {
90,
125,
87,
16,
162,
186,
98,
211,
44,
165,
26,
118,
138,
83,
6,
121,
66,
33,
63,
5,
235,
119,
12,
150,
13,
118,
198,
35,
176,
99,
252,
232 },
396 {
97,
17,
86,
48,
46,
241,
84,
19,
149,
72,
120,
235,
239,
7,
165,
117 },
397 {
97,
17,
86,
48,
46,
241,
84,
19,
149,
72,
120,
235,
239,
7,
165,
117,
226,
124,
56,
147,
68,
13,
49,
222,
211,
30,
121,
158,
74,
98,
244,
10 },
398 {
20,
216,
59,
111,
87,
145,
3,
117,
255,
156,
25,
154,
190,
118,
78,
20 },
399 {
20,
216,
59,
111,
87,
145,
3,
117,
255,
156,
25,
154,
190,
118,
78,
20,
248,
214,
78,
139,
61,
192,
140,
235,
59,
160,
40,
78,
54,
47,
25,
109 },
400 {
32,
229,
86,
178,
39,
205,
7,
101,
40,
38,
134,
141,
19,
69,
80,
243 },
401 {
32,
229,
86,
178,
39,
205,
7,
101,
40,
38,
134,
141,
19,
69,
80,
243,
28,
53,
160,
113,
203,
108,
57,
9,
231,
117,
86,
17,
206,
148,
110,
224 },
402 {
210,
190,
211,
92,
237,
193,
7,
64,
87,
132,
86,
141,
30,
125,
82,
106 },
403 {
210,
190,
211,
92,
237,
193,
7,
64,
87,
132,
86,
141,
30,
125,
82,
106,
25,
187,
172,
61,
235,
190,
17,
208,
203,
92,
238,
153,
58,
16,
49,
223 },
404 {
244,
127,
138,
189,
35,
167,
166,
55,
4,
191,
212,
31,
233,
227,
153,
170 },
405 {
244,
127,
138,
189,
35,
167,
166,
55,
4,
191,
212,
31,
233,
227,
153,
170,
40,
148,
206,
207,
252,
157,
92,
212,
241,
88,
224,
141,
179,
209,
252,
43 },
406 {
144,
204,
246,
93,
75,
176,
77,
125,
131,
249,
85,
13,
133,
132,
179,
13 },
407 {
144,
204,
246,
93,
75,
176,
77,
125,
131,
249,
85,
13,
133,
132,
179,
13,
49,
133,
237,
34,
95,
96,
223,
131,
7,
116,
177,
234,
58,
175,
154,
61 },
408 {
168,
143,
20,
221,
217,
4,
182,
225,
236,
110,
92,
32,
110,
49,
20,
94 },
409 {
168,
143,
20,
221,
217,
4,
182,
225,
236,
110,
92,
32,
110,
49,
20,
94,
184,
23,
40,
57,
120,
148,
146,
245,
154,
136,
43,
183,
113,
171,
180,
149 },
410 {
187,
226,
167,
37,
42,
163,
119,
0,
115,
104,
56,
58,
207,
206,
35,
0 },
411 {
187,
226,
167,
37,
42,
163,
119,
0,
115,
104,
56,
58,
207,
206,
35,
0,
93,
176,
200,
184,
33,
23,
15,
179,
21,
58,
210,
183,
59,
173,
205,
98 },
412 {
46,
112,
16,
101,
211,
3,
163,
33,
2,
36,
37,
139,
46,
0,
29,
198 },
413 {
46,
112,
16,
101,
211,
3,
163,
33,
2,
36,
37,
139,
46,
0,
29,
198,
128,
252,
22,
247,
105,
129,
84,
50,
252,
218,
215,
235,
200,
254,
34,
109 },
414 {
115,
186,
123,
201,
63,
21,
207,
229,
86,
0,
7,
254,
45,
115,
84,
8 },
415 {
115,
186,
123,
201,
63,
21,
207,
229,
86,
0,
7,
254,
45,
115,
84,
8,
39,
27,
113,
37,
108,
123,
230,
153,
209,
218,
180,
77,
66,
207,
239,
26 },
416 {
252,
208,
199,
95,
179,
33,
229,
155,
159,
226,
234,
213,
21,
83,
30,
206 },
417 {
252,
208,
199,
95,
179,
33,
229,
155,
159,
226,
234,
213,
21,
83,
30,
206,
174,
151,
174,
207,
58,
169,
148,
244,
37,
88,
44,
46,
72,
172,
15,
214 }
418 };
419
420
421 UCX_TEST(test_dav_pw2key) {
422
423
424 UCX_TEST_BEGIN;
425
426 for(
int p=
0;p<
4;p++) {
427 for(
int s=
0;s<
4;s++) {
428 DavKey *keys[
4];
429 keys[
0] = dav_pw2key(
430 pws[p],
431 salt[s],
432 saltlen[s],
433 DAV_PWFUNC_PBKDF2_SHA256,
434 DAV_KEY_AES128);
435 keys[
1] = dav_pw2key(
436 pws[p],
437 salt[s],
438 saltlen[s],
439 DAV_PWFUNC_PBKDF2_SHA256,
440 DAV_KEY_AES256);
441 keys[
2] = dav_pw2key(
442 pws[p],
443 salt[s],
444 saltlen[s],
445 DAV_PWFUNC_PBKDF2_SHA512,
446 DAV_KEY_AES128);
447 keys[
3] = dav_pw2key(
448 pws[p],
449 salt[s],
450 saltlen[s],
451 DAV_PWFUNC_PBKDF2_SHA512,
452 DAV_KEY_AES256);
453
454 for(
int i=
0;i<
4;i++) {
455 DavKey *key = keys[i];
456
457 int index =
16*p +
4*s + i;
458 int keylen = index %
2 ==
0 ?
16 :
32;
459
460 UCX_TEST_ASSERT(key,
"no key");
461 UCX_TEST_ASSERT(keylen == key->length,
"wrong key length");
462 UCX_TEST_ASSERT(!memcmp(key->data, pwgenkeys[index], keylen),
"wrong key data");
463 }
464 }
465 }
466
467 UCX_TEST_END;
468 }
469