blob: 64056ff4bf641422d270c9c65eb8b4bf56b3cca0 [file] [log] [blame]
Benny Prijono015cbfd2007-02-25 15:38:32 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono015cbfd2007-02-25 15:38:32 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20#include <pjlib-util.h>
21#include <pjlib.h>
22
23
24#if INCLUDE_ENCRYPTION_TEST
25
26/*
27 * Encryption algorithm tests.
28 */
29#define THIS_FILE "encryption.c"
30
31
32/*
33 * SHA1 test from the original sha1.c source file.
34 */
35static char *sha1_test_data[] = {
36 "abc",
37 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
38 "A million repetitions of 'a'"
39};
40static char *sha1_test_results[] = {
41 "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
42 "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
43 "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
44};
45
46
47static void digest_to_hex(const pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE],
48 char *output)
49{
50 int i,j;
51 char *c = output;
52
53 for (i = 0; i < PJ_SHA1_DIGEST_SIZE/4; i++) {
54 for (j = 0; j < 4; j++) {
55 sprintf(c,"%02X", digest[i*4+j]);
56 c += 2;
57 }
58 sprintf(c, " ");
59 c += 1;
60 }
61 *(c - 1) = '\0';
62}
63
64static int sha1_test1(void)
65{
66 int k;
67 pj_sha1_context context;
68 pj_uint8_t digest[20];
69 char output[80];
70
71 PJ_LOG(3, (THIS_FILE, " SHA1 test vector 1 from sha1.c.."));
72
73 for (k = 0; k < 2; k++){
74 pj_sha1_init(&context);
75 pj_sha1_update(&context, (pj_uint8_t*)sha1_test_data[k],
76 pj_ansi_strlen(sha1_test_data[k]));
77 pj_sha1_final(&context, digest);
78 digest_to_hex(digest, output);
79
80 if (pj_ansi_strcmp(output, sha1_test_results[k])) {
81 PJ_LOG(3, (THIS_FILE, " incorrect hash result on k=%d", k));
82 return -10;
83 }
84 }
85
86 /* million 'a' vector we feed separately */
87 pj_sha1_init(&context);
88 for (k = 0; k < 1000000; k++)
89 pj_sha1_update(&context, (pj_uint8_t*)"a", 1);
90 pj_sha1_final(&context, digest);
91 digest_to_hex(digest, output);
92 if (strcmp(output, sha1_test_results[2])) {
93 PJ_LOG(3, (THIS_FILE, " incorrect hash result!"));
94 return -20;
95 }
96
97 /* success */
98 return(0);
99}
100
101
102/*
103 * SHA1 test from RFC 3174
104 */
105/*
106 * Define patterns for testing
107 */
108#define TEST1 "abc"
109#define TEST2a "abcdbcdecdefdefgefghfghighijhi"
110#define TEST2b "jkijkljklmklmnlmnomnopnopq"
111#define TEST2 TEST2a TEST2b
112#define TEST3 "a"
113#define TEST4a "01234567012345670123456701234567"
114#define TEST4b "01234567012345670123456701234567"
115 /* an exact multiple of 512 bits */
116#define TEST4 TEST4a TEST4b
117
118static char *testarray[4] =
119{
120 TEST1,
121 TEST2,
122 TEST3,
123 TEST4
124};
125static int repeatcount[4] = { 1, 1, 1000000, 10 };
126static char *resultarray[4] =
127{
128 "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
129 "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
130 "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F",
131 "DEA356A2 CDDD90C7 A7ECEDC5 EBB56393 4F460452"
132};
133
134static int sha1_test2(void)
135{
136 pj_sha1_context sha;
137 int i;
138 pj_uint8_t digest[20];
139 char char_digest[64];
140
141 PJ_LOG(3, (THIS_FILE, " SHA1 test vector 2 from rfc 3174.."));
142
143 for(i = 0; i < 4; ++i) {
144 int j;
145
146 pj_sha1_init(&sha);
147
148 for(j = 0; j < repeatcount[i]; ++j) {
149 pj_sha1_update(&sha,
150 (const pj_uint8_t *) testarray[i],
151 pj_ansi_strlen(testarray[i]));
152 }
153
154 pj_sha1_final(&sha, digest);
155
156 digest_to_hex(digest, char_digest);
157 if (pj_ansi_strcmp(char_digest, resultarray[i])) {
158 PJ_LOG(3, (THIS_FILE, " digest mismatch in test %d", i));
159 return -40;
160 }
161 }
162
163 return 0;
164}
165
166
167/*
168 * HMAC-MD5 and HMAC-SHA1 test vectors from RFC 2202
169 */
170struct rfc2202_test
171{
172 char *key;
173 unsigned key_len;
174 char *input;
175 unsigned input_len;
176 char *md5_digest;
177 char *sha1_digest;
178};
179
180
181struct rfc2202_test rfc2202_test_vector[] =
182{
183 {
184 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
185 16,
186 "Hi There",
187 8,
188 "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d",
189 NULL
190 },
191 {
192 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
193 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
194 20,
195 "Hi There",
196 8,
197 NULL,
198 "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
199 },
200 {
201 "Jefe",
202 4,
203 "what do ya want for nothing?",
204 28,
205 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",
206 "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
207 },
208 {
209 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
210 "\xaa\xaa\xaa\xaa\xaa\xaa",
211 16,
212 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
213 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
214 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
215 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
216 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
217 50,
218 "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6",
219 NULL
220 },
221 {
222 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
223 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
224 20,
225 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
226 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
227 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
228 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
229 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
230 50,
231 NULL,
232 "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
233 },
234 {
235 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
236 25,
237 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
238 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
239 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
240 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
241 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
242 50,
243 "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79",
244 "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
245 },
246 {
247 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
248 "\x0c\x0c\x0c\x0c\x0c\x0c",
249 16,
250 "Test With Truncation",
251 20,
252 "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c",
253 NULL
254 },
255 {
256 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
257 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
258 20,
259 "Test With Truncation",
260 20,
261 NULL,
262 "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
263 },
264 {
265 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
266 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
267 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
268 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
269 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
270 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
271 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
272 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
273 80,
274 "Test Using Larger Than Block-Size Key - Hash Key First",
275 54,
276 "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd",
277 "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
278 },
279 {
280 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
281 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
282 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
283 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
284 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
285 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
286 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
287 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
288 80,
289 "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
290 73,
291 "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e",
292 "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
293 }
294};
295
296
297static int rfc2202_test(void)
298{
299 unsigned i;
300
301 PJ_LOG(3, (THIS_FILE, " verifying test vectors from rfc 2202.."));
302
303 /* Verify that test vectors are valid */
304 for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
305 PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].input) ==
306 rfc2202_test_vector[i].input_len, -50);
307 PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].key) ==
308 rfc2202_test_vector[i].key_len, -52);
309 PJ_ASSERT_RETURN(rfc2202_test_vector[i].md5_digest==NULL ||
310 pj_ansi_strlen(rfc2202_test_vector[i].md5_digest)<=16,
311 -54);
312 PJ_ASSERT_RETURN(rfc2202_test_vector[i].sha1_digest==NULL ||
313 pj_ansi_strlen(rfc2202_test_vector[i].sha1_digest)<=20,
314 -56);
315 }
316
317 /* Test HMAC-MD5 */
318 PJ_LOG(3, (THIS_FILE, " HMAC-MD5 test vectors from rfc 2202.."));
319 for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
320 pj_uint8_t digest_buf[18], *digest;
321
322 if (rfc2202_test_vector[i].md5_digest == NULL)
323 continue;
324
325 digest_buf[0] = '\0';
326 digest_buf[17] = '\0';
327
328 digest = digest_buf+1;
329
330 pj_hmac_md5((pj_uint8_t*)rfc2202_test_vector[i].input,
331 rfc2202_test_vector[i].input_len,
332 (pj_uint8_t*)rfc2202_test_vector[i].key,
333 rfc2202_test_vector[i].key_len,
334 digest);
335
336 /* Check for overwrites */
337 if (digest_buf[0] != '\0' || digest_buf[17] != '\0') {
338 PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i));
339 return -60;
340 }
341
342 /* Compare digest */
343 if (pj_memcmp(rfc2202_test_vector[i].md5_digest, digest, 16)) {
344 PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i));
345 return -65;
346 }
347 }
348
349 /* Test HMAC-SHA1 */
350 PJ_LOG(3, (THIS_FILE, " HMAC-SHA1 test vectors from rfc 2202.."));
351 for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
352 pj_uint8_t digest_buf[22], *digest;
353
354 if (rfc2202_test_vector[i].sha1_digest == NULL)
355 continue;
356
357 digest_buf[0] = '\0';
358 digest_buf[21] = '\0';
359
360 digest = digest_buf+1;
361
362 pj_hmac_sha1((pj_uint8_t*)rfc2202_test_vector[i].input,
363 rfc2202_test_vector[i].input_len,
364 (pj_uint8_t*)rfc2202_test_vector[i].key,
365 rfc2202_test_vector[i].key_len,
366 digest);
367
368 /* Check for overwrites */
369 if (digest_buf[0] != '\0' || digest_buf[21] != '\0') {
370 PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i));
371 return -70;
372 }
373
374 /* Compare digest */
375 if (pj_memcmp(rfc2202_test_vector[i].sha1_digest, digest, 20)) {
376 PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i));
377 return -75;
378 }
379 }
380
381
382 /* Success */
383 return 0;
384}
385
Benny Prijono3dc1f402007-02-26 02:33:14 +0000386/* CRC32 test data, generated from crc32 test on a Linux box */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000387struct crc32_test_t
Benny Prijono3dc1f402007-02-26 02:33:14 +0000388{
389 char *input;
390 pj_uint32_t crc;
391} crc32_test_data[] =
392{
393 {
394 "",
395 0x0
396 },
397 {
398 "Hello World",
399 0x4a17b156
400 },
401 {
402 /* Something read from /dev/random */
403 "\x21\x21\x98\x10\x62\x59\xbc\x58\x42\x24\xe5\xf3\x92\x0a\x68\x3c\xa7\x67\x73\xc3",
404 0x506693be
405 },
406 {
407 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
408 0xcab11777
409 },
410 {
411 "123456789",
412 0xCBF43926
413 }
414};
415
416/*
417 * CRC32 test
418 */
419static int crc32_test(void)
420{
421 unsigned i;
422
423 PJ_LOG(3, (THIS_FILE, " crc32 test.."));
424
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000425 /* testing pj_crc32_calc */
Benny Prijono3dc1f402007-02-26 02:33:14 +0000426 for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
427 pj_uint32_t crc;
428
429 crc = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input,
430 pj_ansi_strlen(crc32_test_data[i].input));
431 if (crc != crc32_test_data[i].crc) {
432 PJ_LOG(3,(THIS_FILE, " error: crc mismatch on test %d", i));
433 return -80;
434 }
435 }
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000436
437 /* testing incremental CRC32 calculation */
438 for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
439 pj_crc32_context ctx;
440 pj_uint32_t crc0, crc1;
441 unsigned len;
442
443 len = pj_ansi_strlen(crc32_test_data[i].input);
444 crc0 = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input, len);
445
446 pj_crc32_init(&ctx);
447 pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input,
448 len / 2);
449
450 if (len/2 > 0) {
451 pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input + len/2,
452 len - len/2);
453 }
454
455 crc1 = pj_crc32_final(&ctx);
456
457 if (crc0 != crc1) {
458 PJ_LOG(3,(THIS_FILE,
459 " error: crc algorithm error on test %d", i));
460 return -85;
461 }
462
463 }
Benny Prijono3dc1f402007-02-26 02:33:14 +0000464 return 0;
465}
466
Benny Prijono015cbfd2007-02-25 15:38:32 +0000467
Benny Prijono7977f9f2007-10-10 11:37:56 +0000468/*
469 * Base64 test vectors (RFC 4648)
470 */
471static struct base64_test_vec
472{
473 const char *base256;
474 const char *base64;
475} base64_test_vec[] =
476{
477 {
478 "",
479 ""
480 },
481 {
482 "f",
483 "Zg=="
484 },
485 {
486 "fo",
487 "Zm8="
488 },
489 {
490 "foo",
491 "Zm9v"
492 },
493 {
494 "foob",
495 "Zm9vYg=="
496 },
497 {
498 "fooba",
499 "Zm9vYmE=",
500 },
501 {
502 "foobar",
503 "Zm9vYmFy"
504 },
505 {
506 "\x14\xfb\x9c\x03\xd9\x7e",
507 "FPucA9l+"
508 },
509 {
510 "\x14\xfb\x9c\x03\xd9",
511 "FPucA9k="
512 },
513 {
514 "\x14\xfb\x9c\x03",
515 "FPucAw=="
516 }
517};
518
519
520static int base64_test(void)
521{
522 unsigned i;
523 char output[80];
524 pj_status_t rc;
525
526 PJ_LOG(3, (THIS_FILE, " base64 test.."));
527
528 for (i=0; i<PJ_ARRAY_SIZE(base64_test_vec); ++i) {
529 /* Encode test */
530 pj_str_t input;
531 int out_len = sizeof(output);
532
Benny Prijonob071a782007-10-10 13:12:37 +0000533 rc = pj_base64_encode((pj_uint8_t*)base64_test_vec[i].base256,
Benny Prijono7977f9f2007-10-10 11:37:56 +0000534 strlen(base64_test_vec[i].base256),
535 output, &out_len);
536 if (rc != PJ_SUCCESS)
537 return -90;
538
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000539 if (out_len != (int)strlen(base64_test_vec[i].base64))
Benny Prijono7977f9f2007-10-10 11:37:56 +0000540 return -91;
541
542 output[out_len] = '\0';
543 if (strcmp(output, base64_test_vec[i].base64) != 0)
544 return -92;
545
546 /* Decode test */
547 out_len = sizeof(output);
Benny Prijonob071a782007-10-10 13:12:37 +0000548 input.ptr = (char*)base64_test_vec[i].base64;
Benny Prijono7977f9f2007-10-10 11:37:56 +0000549 input.slen = strlen(base64_test_vec[i].base64);
550 rc = pj_base64_decode(&input, (pj_uint8_t*)output, &out_len);
551 if (rc != PJ_SUCCESS)
552 return -95;
553
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000554 if (out_len != (int)strlen(base64_test_vec[i].base256))
Benny Prijono7977f9f2007-10-10 11:37:56 +0000555 return -96;
556
557 output[out_len] = '\0';
558
559 if (strcmp(output, base64_test_vec[i].base256) != 0)
560 return -97;
561 }
562
563 return 0;
564}
565
566
Benny Prijono015cbfd2007-02-25 15:38:32 +0000567int encryption_test()
568{
569 int rc;
570
Benny Prijono7977f9f2007-10-10 11:37:56 +0000571 rc = base64_test();
572 if (rc != 0)
573 return rc;
574
Benny Prijono015cbfd2007-02-25 15:38:32 +0000575 rc = sha1_test1();
576 if (rc != 0)
577 return rc;
578
579 rc = sha1_test2();
580 if (rc != 0)
581 return rc;
582
583 rc = rfc2202_test();
584 if (rc != 0)
585 return rc;
586
Benny Prijono3dc1f402007-02-26 02:33:14 +0000587 rc = crc32_test();
588 if (rc != 0)
589 return rc;
590
Benny Prijono015cbfd2007-02-25 15:38:32 +0000591 return 0;
592}
593
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000594static void crc32_update(pj_crc32_context *c, const pj_uint8_t *data,
595 pj_size_t nbytes)
596{
597 pj_crc32_update(c, data, nbytes);
598}
599
600static void crc32_final(pj_crc32_context *ctx, pj_uint32_t *digest)
601{
602 *digest = pj_crc32_final(ctx);
603}
604
605int encryption_benchmark()
606{
607 pj_pool_t *pool;
608 pj_uint8_t *input;
609 union {
610 pj_md5_context md5_context;
611 pj_sha1_context sha1_context;
612 } context;
613 pj_uint8_t digest[32];
614 pj_size_t input_len;
615 struct algorithm
616 {
617 const char *name;
618 void (*init_context)(void*);
619 void (*update)(void*, const pj_uint8_t*, unsigned);
620 void (*final)(void*, void*);
621 pj_uint32_t t;
622 } algorithms[] =
623 {
624 {
625 "MD5 ",
Benny Prijonof83dc1d2007-02-28 14:35:50 +0000626 (void (*)(void*))&pj_md5_init,
627 (void (*)(void*, const pj_uint8_t*, unsigned))&pj_md5_update,
628 (void (*)(void*, void*))&pj_md5_final
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000629 },
630 {
631 "SHA1 ",
Benny Prijonof83dc1d2007-02-28 14:35:50 +0000632 (void (*)(void*))&pj_sha1_init,
633 (void (*)(void*, const pj_uint8_t*, unsigned))&pj_sha1_update,
634 (void (*)(void*, void*))&pj_sha1_final
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000635 },
636 {
637 "CRC32",
Benny Prijonof83dc1d2007-02-28 14:35:50 +0000638 (void (*)(void*))&pj_crc32_init,
639 (void (*)(void*, const pj_uint8_t*, unsigned))&crc32_update,
640 (void (*)(void*, void*))&crc32_final
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000641 }
642 };
643#if defined(PJ_DEBUG) && PJ_DEBUG!=0
644 enum { LOOP = 1000 };
645#else
646 enum { LOOP = 10000 };
647#endif
648 unsigned i;
649 double total_len;
650
651 input_len = 2048;
652 total_len = input_len * LOOP;
653 pool = pj_pool_create(mem, "enc", input_len+256, 0, NULL);
654 if (!pool)
655 return PJ_ENOMEM;
656
Benny Prijonoa1e69682007-05-11 15:14:34 +0000657 input = (pj_uint8_t*)pj_pool_alloc(pool, input_len);
Benny Prijono8fe2ed52007-02-26 22:31:06 +0000658 pj_memset(input, '\xaa', input_len);
659
660 PJ_LOG(3, (THIS_FILE, " feeding %d Mbytes of data",
661 (unsigned)(total_len/1024/1024)));
662
663 /* Dry run */
664 for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
665 algorithms[i].init_context(&context);
666 algorithms[i].update(&context, input, input_len);
667 algorithms[i].final(&context, digest);
668 }
669
670 /* Run */
671 for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
672 int j;
673 pj_timestamp t1, t2;
674
675 pj_get_timestamp(&t1);
676 algorithms[i].init_context(&context);
677 for (j=0; j<LOOP; ++j) {
678 algorithms[i].update(&context, input, input_len);
679 }
680 algorithms[i].final(&context, digest);
681 pj_get_timestamp(&t2);
682
683 algorithms[i].t = pj_elapsed_usec(&t1, &t2);
684 }
685
686 /* Results */
687 for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
688 double bytes;
689
690 bytes = (total_len * 1000000 / algorithms[i].t);
691 PJ_LOG(3, (THIS_FILE, " %s:%8d usec (%3d.%03d Mbytes/sec)",
692 algorithms[i].name, algorithms[i].t,
693 (unsigned)(bytes / 1024 / 1024),
694 ((unsigned)(bytes) % (1024 * 1024)) / 1024));
695 }
696
697 return 0;
698}
699
Benny Prijono015cbfd2007-02-25 15:38:32 +0000700
701
702#endif /* INCLUDE_ENCRYPTION_TEST */
703