Benny Prijono | 015cbfd | 2007-02-25 15:38:32 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 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 | */ |
| 35 | static char *sha1_test_data[] = { |
| 36 | "abc", |
| 37 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 38 | "A million repetitions of 'a'" |
| 39 | }; |
| 40 | static 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 | |
| 47 | static 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 | |
| 64 | static 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 | |
| 118 | static char *testarray[4] = |
| 119 | { |
| 120 | TEST1, |
| 121 | TEST2, |
| 122 | TEST3, |
| 123 | TEST4 |
| 124 | }; |
| 125 | static int repeatcount[4] = { 1, 1, 1000000, 10 }; |
| 126 | static 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 | |
| 134 | static 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 | */ |
| 170 | struct 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 | |
| 181 | struct 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 | |
| 297 | static 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 Prijono | 3dc1f40 | 2007-02-26 02:33:14 +0000 | [diff] [blame] | 386 | /* CRC32 test data, generated from crc32 test on a Linux box */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 387 | struct crc32_test_t |
Benny Prijono | 3dc1f40 | 2007-02-26 02:33:14 +0000 | [diff] [blame] | 388 | { |
| 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 | */ |
| 419 | static int crc32_test(void) |
| 420 | { |
| 421 | unsigned i; |
| 422 | |
| 423 | PJ_LOG(3, (THIS_FILE, " crc32 test..")); |
| 424 | |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 425 | /* testing pj_crc32_calc */ |
Benny Prijono | 3dc1f40 | 2007-02-26 02:33:14 +0000 | [diff] [blame] | 426 | 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 Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 436 | |
| 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 Prijono | 3dc1f40 | 2007-02-26 02:33:14 +0000 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
Benny Prijono | 015cbfd | 2007-02-25 15:38:32 +0000 | [diff] [blame] | 467 | |
| 468 | int encryption_test() |
| 469 | { |
| 470 | int rc; |
| 471 | |
| 472 | rc = sha1_test1(); |
| 473 | if (rc != 0) |
| 474 | return rc; |
| 475 | |
| 476 | rc = sha1_test2(); |
| 477 | if (rc != 0) |
| 478 | return rc; |
| 479 | |
| 480 | rc = rfc2202_test(); |
| 481 | if (rc != 0) |
| 482 | return rc; |
| 483 | |
Benny Prijono | 3dc1f40 | 2007-02-26 02:33:14 +0000 | [diff] [blame] | 484 | rc = crc32_test(); |
| 485 | if (rc != 0) |
| 486 | return rc; |
| 487 | |
Benny Prijono | 015cbfd | 2007-02-25 15:38:32 +0000 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 491 | static void crc32_update(pj_crc32_context *c, const pj_uint8_t *data, |
| 492 | pj_size_t nbytes) |
| 493 | { |
| 494 | pj_crc32_update(c, data, nbytes); |
| 495 | } |
| 496 | |
| 497 | static void crc32_final(pj_crc32_context *ctx, pj_uint32_t *digest) |
| 498 | { |
| 499 | *digest = pj_crc32_final(ctx); |
| 500 | } |
| 501 | |
| 502 | int encryption_benchmark() |
| 503 | { |
| 504 | pj_pool_t *pool; |
| 505 | pj_uint8_t *input; |
| 506 | union { |
| 507 | pj_md5_context md5_context; |
| 508 | pj_sha1_context sha1_context; |
| 509 | } context; |
| 510 | pj_uint8_t digest[32]; |
| 511 | pj_size_t input_len; |
| 512 | struct algorithm |
| 513 | { |
| 514 | const char *name; |
| 515 | void (*init_context)(void*); |
| 516 | void (*update)(void*, const pj_uint8_t*, unsigned); |
| 517 | void (*final)(void*, void*); |
| 518 | pj_uint32_t t; |
| 519 | } algorithms[] = |
| 520 | { |
| 521 | { |
| 522 | "MD5 ", |
Benny Prijono | f83dc1d | 2007-02-28 14:35:50 +0000 | [diff] [blame] | 523 | (void (*)(void*))&pj_md5_init, |
| 524 | (void (*)(void*, const pj_uint8_t*, unsigned))&pj_md5_update, |
| 525 | (void (*)(void*, void*))&pj_md5_final |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 526 | }, |
| 527 | { |
| 528 | "SHA1 ", |
Benny Prijono | f83dc1d | 2007-02-28 14:35:50 +0000 | [diff] [blame] | 529 | (void (*)(void*))&pj_sha1_init, |
| 530 | (void (*)(void*, const pj_uint8_t*, unsigned))&pj_sha1_update, |
| 531 | (void (*)(void*, void*))&pj_sha1_final |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 532 | }, |
| 533 | { |
| 534 | "CRC32", |
Benny Prijono | f83dc1d | 2007-02-28 14:35:50 +0000 | [diff] [blame] | 535 | (void (*)(void*))&pj_crc32_init, |
| 536 | (void (*)(void*, const pj_uint8_t*, unsigned))&crc32_update, |
| 537 | (void (*)(void*, void*))&crc32_final |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 538 | } |
| 539 | }; |
| 540 | #if defined(PJ_DEBUG) && PJ_DEBUG!=0 |
| 541 | enum { LOOP = 1000 }; |
| 542 | #else |
| 543 | enum { LOOP = 10000 }; |
| 544 | #endif |
| 545 | unsigned i; |
| 546 | double total_len; |
| 547 | |
| 548 | input_len = 2048; |
| 549 | total_len = input_len * LOOP; |
| 550 | pool = pj_pool_create(mem, "enc", input_len+256, 0, NULL); |
| 551 | if (!pool) |
| 552 | return PJ_ENOMEM; |
| 553 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 554 | input = (pj_uint8_t*)pj_pool_alloc(pool, input_len); |
Benny Prijono | 8fe2ed5 | 2007-02-26 22:31:06 +0000 | [diff] [blame] | 555 | pj_memset(input, '\xaa', input_len); |
| 556 | |
| 557 | PJ_LOG(3, (THIS_FILE, " feeding %d Mbytes of data", |
| 558 | (unsigned)(total_len/1024/1024))); |
| 559 | |
| 560 | /* Dry run */ |
| 561 | for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 562 | algorithms[i].init_context(&context); |
| 563 | algorithms[i].update(&context, input, input_len); |
| 564 | algorithms[i].final(&context, digest); |
| 565 | } |
| 566 | |
| 567 | /* Run */ |
| 568 | for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 569 | int j; |
| 570 | pj_timestamp t1, t2; |
| 571 | |
| 572 | pj_get_timestamp(&t1); |
| 573 | algorithms[i].init_context(&context); |
| 574 | for (j=0; j<LOOP; ++j) { |
| 575 | algorithms[i].update(&context, input, input_len); |
| 576 | } |
| 577 | algorithms[i].final(&context, digest); |
| 578 | pj_get_timestamp(&t2); |
| 579 | |
| 580 | algorithms[i].t = pj_elapsed_usec(&t1, &t2); |
| 581 | } |
| 582 | |
| 583 | /* Results */ |
| 584 | for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 585 | double bytes; |
| 586 | |
| 587 | bytes = (total_len * 1000000 / algorithms[i].t); |
| 588 | PJ_LOG(3, (THIS_FILE, " %s:%8d usec (%3d.%03d Mbytes/sec)", |
| 589 | algorithms[i].name, algorithms[i].t, |
| 590 | (unsigned)(bytes / 1024 / 1024), |
| 591 | ((unsigned)(bytes) % (1024 * 1024)) / 1024)); |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Benny Prijono | 015cbfd | 2007-02-25 15:38:32 +0000 | [diff] [blame] | 597 | |
| 598 | |
| 599 | #endif /* INCLUDE_ENCRYPTION_TEST */ |
| 600 | |