| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
#include <pjlib-util.h> |
| 21 |
#include <pjlib.h> |
| 22 |
|
| 23 |
|
| 24 |
#if INCLUDE_ENCRYPTION_TEST |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#define THIS_FILE "encryption.c" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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, int output_size) |
| 49 |
{ |
| 50 |
int i,j; |
| 51 |
char *c = output; |
| 52 |
|
| 53 |
for (i = 0; i < PJ_SHA1_DIGEST_SIZE/4 && output_size >= 2; i++) { |
| 54 |
for (j = 0; j < 4 && output_size >= 3; j++) { |
| 55 |
pj_ansi_snprintf(c, output_size, "%02X", digest[i*4+j] & 0xFF); |
| 56 |
c += 2; |
| 57 |
output_size -= 2; |
| 58 |
} |
| 59 |
pj_ansi_snprintf(c, output_size, " "); |
| 60 |
c += 1; |
| 61 |
output_size -= 1; |
| 62 |
} |
| 63 |
*(c - 1) = '\0'; |
| 64 |
} |
| 65 |
|
| 66 |
static int sha1_test1(void) |
| 67 |
{ |
| 68 |
enum { MILLION = 1000000 }; |
| 69 |
int k; |
| 70 |
pj_sha1_context context; |
| 71 |
pj_uint8_t digest[20]; |
| 72 |
char output[80]; |
| 73 |
pj_pool_t *pool; |
| 74 |
pj_uint8_t *block; |
| 75 |
|
| 76 |
PJ_LOG(3, (THIS_FILE, " SHA1 test vector 1 from sha1.c..")); |
| 77 |
|
| 78 |
for (k = 0; k < 2; k++){ |
| 79 |
pj_sha1_init(&context); |
| 80 |
pj_sha1_update(&context, (pj_uint8_t*)sha1_test_data[k], |
| 81 |
pj_ansi_strlen(sha1_test_data[k])); |
| 82 |
pj_sha1_final(&context, digest); |
| 83 |
digest_to_hex(digest, output, sizeof(output)); |
| 84 |
|
| 85 |
if (pj_ansi_strcmp(output, sha1_test_results[k])) { |
| 86 |
PJ_LOG(3, (THIS_FILE, " incorrect hash result on k=%d", k)); |
| 87 |
return -10; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pj_sha1_init(&context); |
| 93 |
for (k = 0; k < MILLION; k++) |
| 94 |
pj_sha1_update(&context, (pj_uint8_t*)"a", 1); |
| 95 |
pj_sha1_final(&context, digest); |
| 96 |
digest_to_hex(digest, output, sizeof(output)); |
| 97 |
if (strcmp(output, sha1_test_results[2])) { |
| 98 |
PJ_LOG(3, (THIS_FILE, " incorrect hash result!")); |
| 99 |
return -20; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
pool = pj_pool_create(mem, "sha1test", 256, 512, NULL); |
| 104 |
block = (pj_uint8_t*)pj_pool_alloc(pool, MILLION); |
| 105 |
pj_memset(block, 'a', MILLION); |
| 106 |
|
| 107 |
pj_sha1_init(&context); |
| 108 |
pj_sha1_update(&context, block, MILLION); |
| 109 |
pj_sha1_final(&context, digest); |
| 110 |
digest_to_hex(digest, output, sizeof(output)); |
| 111 |
if (strcmp(output, sha1_test_results[2])) { |
| 112 |
pj_pool_release(pool); |
| 113 |
PJ_LOG(3, (THIS_FILE, " incorrect hash result for block update!")); |
| 114 |
return -21; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
for (k=0; k<MILLION; ++k) { |
| 119 |
if (block[k] != 'a') { |
| 120 |
pj_pool_release(pool); |
| 121 |
PJ_LOG(3, (THIS_FILE, " block was modified!")); |
| 122 |
return -22; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
pj_pool_release(pool); |
| 127 |
|
| 128 |
|
| 129 |
return(0); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#define TEST1 "abc" |
| 140 |
#define TEST2a "abcdbcdecdefdefgefghfghighijhi" |
| 141 |
#define TEST2b "jkijkljklmklmnlmnomnopnopq" |
| 142 |
#define TEST2 TEST2a TEST2b |
| 143 |
#define TEST3 "a" |
| 144 |
#define TEST4a "01234567012345670123456701234567" |
| 145 |
#define TEST4b "01234567012345670123456701234567" |
| 146 |
|
| 147 |
#define TEST4 TEST4a TEST4b |
| 148 |
|
| 149 |
static char *testarray[4] = |
| 150 |
{ |
| 151 |
TEST1, |
| 152 |
TEST2, |
| 153 |
TEST3, |
| 154 |
TEST4 |
| 155 |
}; |
| 156 |
static int repeatcount[4] = { 1, 1, 1000000, 10 }; |
| 157 |
static char *resultarray[4] = |
| 158 |
{ |
| 159 |
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", |
| 160 |
"84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", |
| 161 |
"34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F", |
| 162 |
"DEA356A2 CDDD90C7 A7ECEDC5 EBB56393 4F460452" |
| 163 |
}; |
| 164 |
|
| 165 |
static int sha1_test2(void) |
| 166 |
{ |
| 167 |
pj_sha1_context sha; |
| 168 |
int i; |
| 169 |
pj_uint8_t digest[20]; |
| 170 |
char char_digest[64]; |
| 171 |
|
| 172 |
PJ_LOG(3, (THIS_FILE, " SHA1 test vector 2 from rfc 3174..")); |
| 173 |
|
| 174 |
for(i = 0; i < 4; ++i) { |
| 175 |
int j; |
| 176 |
|
| 177 |
pj_sha1_init(&sha); |
| 178 |
|
| 179 |
for(j = 0; j < repeatcount[i]; ++j) { |
| 180 |
pj_sha1_update(&sha, |
| 181 |
(const pj_uint8_t *) testarray[i], |
| 182 |
pj_ansi_strlen(testarray[i])); |
| 183 |
} |
| 184 |
|
| 185 |
pj_sha1_final(&sha, digest); |
| 186 |
|
| 187 |
digest_to_hex(digest, char_digest, sizeof(char_digest)); |
| 188 |
if (pj_ansi_strcmp(char_digest, resultarray[i])) { |
| 189 |
PJ_LOG(3, (THIS_FILE, " digest mismatch in test %d", i)); |
| 190 |
return -40; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
struct rfc2202_test |
| 202 |
{ |
| 203 |
char *key; |
| 204 |
unsigned key_len; |
| 205 |
char *input; |
| 206 |
unsigned input_len; |
| 207 |
char *md5_digest; |
| 208 |
char *sha1_digest; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
struct rfc2202_test rfc2202_test_vector[] = |
| 213 |
{ |
| 214 |
{ |
| 215 |
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", |
| 216 |
16, |
| 217 |
"Hi There", |
| 218 |
8, |
| 219 |
"\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d", |
| 220 |
NULL |
| 221 |
}, |
| 222 |
{ |
| 223 |
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b" |
| 224 |
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", |
| 225 |
20, |
| 226 |
"Hi There", |
| 227 |
8, |
| 228 |
NULL, |
| 229 |
"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00" |
| 230 |
}, |
| 231 |
{ |
| 232 |
"Jefe", |
| 233 |
4, |
| 234 |
"what do ya want for nothing?", |
| 235 |
28, |
| 236 |
"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38", |
| 237 |
"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79" |
| 238 |
}, |
| 239 |
{ |
| 240 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 241 |
"\xaa\xaa\xaa\xaa\xaa\xaa", |
| 242 |
16, |
| 243 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 244 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 245 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 246 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 247 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", |
| 248 |
50, |
| 249 |
"\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6", |
| 250 |
NULL |
| 251 |
}, |
| 252 |
{ |
| 253 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 254 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", |
| 255 |
20, |
| 256 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 257 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 258 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 259 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" |
| 260 |
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", |
| 261 |
50, |
| 262 |
NULL, |
| 263 |
"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3" |
| 264 |
}, |
| 265 |
{ |
| 266 |
"\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", |
| 267 |
25, |
| 268 |
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd" |
| 269 |
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd" |
| 270 |
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd" |
| 271 |
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd" |
| 272 |
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd", |
| 273 |
50, |
| 274 |
"\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79", |
| 275 |
"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda" |
| 276 |
}, |
| 277 |
{ |
| 278 |
"\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c" |
| 279 |
"\x0c\x0c\x0c\x0c\x0c\x0c", |
| 280 |
16, |
| 281 |
"Test With Truncation", |
| 282 |
20, |
| 283 |
"\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c", |
| 284 |
NULL |
| 285 |
}, |
| 286 |
{ |
| 287 |
"\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c" |
| 288 |
"\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c", |
| 289 |
20, |
| 290 |
"Test With Truncation", |
| 291 |
20, |
| 292 |
NULL, |
| 293 |
"\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04" |
| 294 |
}, |
| 295 |
{ |
| 296 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 297 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 298 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 299 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 300 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 301 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 302 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 303 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", |
| 304 |
80, |
| 305 |
"Test Using Larger Than Block-Size Key - Hash Key First", |
| 306 |
54, |
| 307 |
"\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd", |
| 308 |
"\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12" |
| 309 |
}, |
| 310 |
{ |
| 311 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 312 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 313 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 314 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 315 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 316 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 317 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 318 |
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", |
| 319 |
80, |
| 320 |
"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", |
| 321 |
73, |
| 322 |
"\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e", |
| 323 |
"\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91" |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
static int rfc2202_test(void) |
| 329 |
{ |
| 330 |
unsigned i; |
| 331 |
|
| 332 |
PJ_LOG(3, (THIS_FILE, " verifying test vectors from rfc 2202..")); |
| 333 |
|
| 334 |
|
| 335 |
for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) { |
| 336 |
PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].input) == |
| 337 |
rfc2202_test_vector[i].input_len, -50); |
| 338 |
PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].key) == |
| 339 |
rfc2202_test_vector[i].key_len, -52); |
| 340 |
PJ_ASSERT_RETURN(rfc2202_test_vector[i].md5_digest==NULL || |
| 341 |
pj_ansi_strlen(rfc2202_test_vector[i].md5_digest)<=16, |
| 342 |
-54); |
| 343 |
PJ_ASSERT_RETURN(rfc2202_test_vector[i].sha1_digest==NULL || |
| 344 |
pj_ansi_strlen(rfc2202_test_vector[i].sha1_digest)<=20, |
| 345 |
-56); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
PJ_LOG(3, (THIS_FILE, " HMAC-MD5 test vectors from rfc 2202..")); |
| 350 |
for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) { |
| 351 |
pj_uint8_t digest_buf[18], *digest; |
| 352 |
|
| 353 |
if (rfc2202_test_vector[i].md5_digest == NULL) |
| 354 |
continue; |
| 355 |
|
| 356 |
digest_buf[0] = '\0'; |
| 357 |
digest_buf[17] = '\0'; |
| 358 |
|
| 359 |
digest = digest_buf+1; |
| 360 |
|
| 361 |
pj_hmac_md5((pj_uint8_t*)rfc2202_test_vector[i].input, |
| 362 |
rfc2202_test_vector[i].input_len, |
| 363 |
(pj_uint8_t*)rfc2202_test_vector[i].key, |
| 364 |
rfc2202_test_vector[i].key_len, |
| 365 |
digest); |
| 366 |
|
| 367 |
|
| 368 |
if (digest_buf[0] != '\0' || digest_buf[17] != '\0') { |
| 369 |
PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i)); |
| 370 |
return -60; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
if (pj_memcmp(rfc2202_test_vector[i].md5_digest, digest, 16)) { |
| 375 |
PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i)); |
| 376 |
return -65; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
PJ_LOG(3, (THIS_FILE, " HMAC-SHA1 test vectors from rfc 2202..")); |
| 382 |
for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) { |
| 383 |
pj_uint8_t digest_buf[22], *digest; |
| 384 |
|
| 385 |
if (rfc2202_test_vector[i].sha1_digest == NULL) |
| 386 |
continue; |
| 387 |
|
| 388 |
digest_buf[0] = '\0'; |
| 389 |
digest_buf[21] = '\0'; |
| 390 |
|
| 391 |
digest = digest_buf+1; |
| 392 |
|
| 393 |
pj_hmac_sha1((pj_uint8_t*)rfc2202_test_vector[i].input, |
| 394 |
rfc2202_test_vector[i].input_len, |
| 395 |
(pj_uint8_t*)rfc2202_test_vector[i].key, |
| 396 |
rfc2202_test_vector[i].key_len, |
| 397 |
digest); |
| 398 |
|
| 399 |
|
| 400 |
if (digest_buf[0] != '\0' || digest_buf[21] != '\0') { |
| 401 |
PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i)); |
| 402 |
return -70; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
if (pj_memcmp(rfc2202_test_vector[i].sha1_digest, digest, 20)) { |
| 407 |
PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i)); |
| 408 |
return -75; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
struct crc32_test_t |
| 419 |
{ |
| 420 |
char *input; |
| 421 |
pj_uint32_t crc; |
| 422 |
} crc32_test_data[] = |
| 423 |
{ |
| 424 |
{ |
| 425 |
"", |
| 426 |
0x0 |
| 427 |
}, |
| 428 |
{ |
| 429 |
"Hello World", |
| 430 |
0x4a17b156 |
| 431 |
}, |
| 432 |
{ |
| 433 |
|
| 434 |
"\x21\x21\x98\x10\x62\x59\xbc\x58\x42\x24\xe5\xf3\x92\x0a\x68\x3c\xa7\x67\x73\xc3", |
| 435 |
0x506693be |
| 436 |
}, |
| 437 |
{ |
| 438 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 439 |
0xcab11777 |
| 440 |
}, |
| 441 |
{ |
| 442 |
"123456789", |
| 443 |
0xCBF43926 |
| 444 |
} |
| 445 |
}; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
static int crc32_test(void) |
| 451 |
{ |
| 452 |
unsigned i; |
| 453 |
|
| 454 |
PJ_LOG(3, (THIS_FILE, " crc32 test..")); |
| 455 |
|
| 456 |
|
| 457 |
for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) { |
| 458 |
pj_uint32_t crc; |
| 459 |
|
| 460 |
crc = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input, |
| 461 |
pj_ansi_strlen(crc32_test_data[i].input)); |
| 462 |
if (crc != crc32_test_data[i].crc) { |
| 463 |
PJ_LOG(3,(THIS_FILE, " error: crc mismatch on test %d", i)); |
| 464 |
return -80; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) { |
| 470 |
pj_crc32_context ctx; |
| 471 |
pj_uint32_t crc0, crc1; |
| 472 |
pj_size_t len; |
| 473 |
|
| 474 |
len = pj_ansi_strlen(crc32_test_data[i].input); |
| 475 |
crc0 = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input, len); |
| 476 |
|
| 477 |
pj_crc32_init(&ctx); |
| 478 |
pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input, |
| 479 |
len / 2); |
| 480 |
|
| 481 |
if (len/2 > 0) { |
| 482 |
pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input + len/2, |
| 483 |
len - len/2); |
| 484 |
} |
| 485 |
|
| 486 |
crc1 = pj_crc32_final(&ctx); |
| 487 |
|
| 488 |
if (crc0 != crc1) { |
| 489 |
PJ_LOG(3,(THIS_FILE, |
| 490 |
" error: crc algorithm error on test %d", i)); |
| 491 |
return -85; |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
return 0; |
| 496 |
} |
| 497 |
|
| 498 |
enum |
| 499 |
{ |
| 500 |
ENCODE = 1, |
| 501 |
DECODE = 2, |
| 502 |
ENCODE_DECODE = 3 |
| 503 |
}; |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static struct base64_test_vec |
| 509 |
{ |
| 510 |
const char *base256; |
| 511 |
const char *base64; |
| 512 |
unsigned flag; |
| 513 |
} base64_test_vec[] = |
| 514 |
{ |
| 515 |
{ |
| 516 |
"", |
| 517 |
"", |
| 518 |
ENCODE_DECODE |
| 519 |
}, |
| 520 |
{ |
| 521 |
"f", |
| 522 |
"Zg==", |
| 523 |
ENCODE_DECODE |
| 524 |
}, |
| 525 |
{ |
| 526 |
"fo", |
| 527 |
"Zm8=", |
| 528 |
ENCODE_DECODE |
| 529 |
}, |
| 530 |
{ |
| 531 |
"foo", |
| 532 |
"Zm9v", |
| 533 |
ENCODE_DECODE |
| 534 |
}, |
| 535 |
{ |
| 536 |
"foob", |
| 537 |
"Zm9vYg==", |
| 538 |
ENCODE_DECODE |
| 539 |
}, |
| 540 |
{ |
| 541 |
"fooba", |
| 542 |
"Zm9vYmE=", |
| 543 |
ENCODE_DECODE |
| 544 |
}, |
| 545 |
{ |
| 546 |
"foobar", |
| 547 |
"Zm9vYmFy", |
| 548 |
ENCODE_DECODE |
| 549 |
}, |
| 550 |
{ |
| 551 |
"\x14\xfb\x9c\x03\xd9\x7e", |
| 552 |
"FPucA9l+", |
| 553 |
ENCODE_DECODE |
| 554 |
}, |
| 555 |
{ |
| 556 |
"\x14\xfb\x9c\x03\xd9", |
| 557 |
"FPucA9k=", |
| 558 |
ENCODE_DECODE |
| 559 |
}, |
| 560 |
{ |
| 561 |
"\x14\xfb\x9c\x03", |
| 562 |
"FPucAw==", |
| 563 |
ENCODE_DECODE |
| 564 |
}, |
| 565 |
|
| 566 |
{ |
| 567 |
"foobar", |
| 568 |
"Zm9v\r\nYmFy", |
| 569 |
DECODE |
| 570 |
}, |
| 571 |
{ |
| 572 |
"foobar", |
| 573 |
"\nZ\r\nm 9\tv\nYm\nF\ny\n", |
| 574 |
DECODE |
| 575 |
}, |
| 576 |
}; |
| 577 |
|
| 578 |
|
| 579 |
static int base64_test(void) |
| 580 |
{ |
| 581 |
unsigned i; |
| 582 |
char output[80]; |
| 583 |
pj_status_t rc; |
| 584 |
|
| 585 |
PJ_LOG(3, (THIS_FILE, " base64 test..")); |
| 586 |
|
| 587 |
for (i=0; i<PJ_ARRAY_SIZE(base64_test_vec); ++i) { |
| 588 |
pj_str_t input; |
| 589 |
int out_len; |
| 590 |
|
| 591 |
|
| 592 |
if (base64_test_vec[i].flag & ENCODE) { |
| 593 |
out_len = sizeof(output); |
| 594 |
rc = pj_base64_encode((pj_uint8_t*)base64_test_vec[i].base256, |
| 595 |
(int)strlen(base64_test_vec[i].base256), |
| 596 |
output, &out_len); |
| 597 |
if (rc != PJ_SUCCESS) |
| 598 |
return -90; |
| 599 |
|
| 600 |
if (out_len != (int)strlen(base64_test_vec[i].base64)) |
| 601 |
return -91; |
| 602 |
|
| 603 |
output[out_len] = '\0'; |
| 604 |
if (strcmp(output, base64_test_vec[i].base64) != 0) |
| 605 |
return -92; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
if (base64_test_vec[i].flag & DECODE) { |
| 610 |
out_len = sizeof(output); |
| 611 |
input.ptr = (char*)base64_test_vec[i].base64; |
| 612 |
input.slen = strlen(base64_test_vec[i].base64); |
| 613 |
rc = pj_base64_decode(&input, (pj_uint8_t*)output, &out_len); |
| 614 |
if (rc != PJ_SUCCESS) |
| 615 |
return -95; |
| 616 |
|
| 617 |
if (out_len != (int)strlen(base64_test_vec[i].base256)) |
| 618 |
return -96; |
| 619 |
|
| 620 |
output[out_len] = '\0'; |
| 621 |
|
| 622 |
if (strcmp(output, base64_test_vec[i].base256) != 0) |
| 623 |
return -97; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
int encryption_test() |
| 632 |
{ |
| 633 |
int rc; |
| 634 |
|
| 635 |
rc = base64_test(); |
| 636 |
if (rc != 0) |
| 637 |
return rc; |
| 638 |
|
| 639 |
rc = sha1_test1(); |
| 640 |
if (rc != 0) |
| 641 |
return rc; |
| 642 |
|
| 643 |
rc = sha1_test2(); |
| 644 |
if (rc != 0) |
| 645 |
return rc; |
| 646 |
|
| 647 |
rc = rfc2202_test(); |
| 648 |
if (rc != 0) |
| 649 |
return rc; |
| 650 |
|
| 651 |
rc = crc32_test(); |
| 652 |
if (rc != 0) |
| 653 |
return rc; |
| 654 |
|
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
static void crc32_update(pj_crc32_context *c, const pj_uint8_t *data, |
| 659 |
pj_size_t nbytes) |
| 660 |
{ |
| 661 |
pj_crc32_update(c, data, nbytes); |
| 662 |
} |
| 663 |
|
| 664 |
static void crc32_final(pj_crc32_context *ctx, pj_uint32_t *digest) |
| 665 |
{ |
| 666 |
*digest = pj_crc32_final(ctx); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
static void md5_update_wrapper(pj_md5_context *ctx, |
| 673 |
unsigned char const *buf, pj_size_t len) |
| 674 |
{ |
| 675 |
pj_md5_update(ctx, buf, (unsigned)len); |
| 676 |
} |
| 677 |
|
| 678 |
int encryption_benchmark() |
| 679 |
{ |
| 680 |
pj_pool_t *pool; |
| 681 |
pj_uint8_t *input; |
| 682 |
union { |
| 683 |
pj_md5_context md5_context; |
| 684 |
pj_sha1_context sha1_context; |
| 685 |
} context; |
| 686 |
pj_uint8_t digest[32]; |
| 687 |
pj_size_t input_len; |
| 688 |
struct algorithm |
| 689 |
{ |
| 690 |
const char *name; |
| 691 |
void (*init_context)(void*); |
| 692 |
void (*update)(void*, const pj_uint8_t*, pj_size_t); |
| 693 |
void (*final)(void*, void*); |
| 694 |
pj_uint32_t t; |
| 695 |
} algorithms[] = |
| 696 |
{ |
| 697 |
{ |
| 698 |
"MD5 ", |
| 699 |
(void (*)(void*))&pj_md5_init, |
| 700 |
(void (*)(void*, const pj_uint8_t*, pj_size_t))&md5_update_wrapper, |
| 701 |
(void (*)(void*, void*))&pj_md5_final |
| 702 |
}, |
| 703 |
{ |
| 704 |
"SHA1 ", |
| 705 |
(void (*)(void*))&pj_sha1_init, |
| 706 |
(void (*)(void*, const pj_uint8_t*, pj_size_t))&pj_sha1_update, |
| 707 |
(void (*)(void*, void*))&pj_sha1_final |
| 708 |
}, |
| 709 |
{ |
| 710 |
"CRC32", |
| 711 |
(void (*)(void*))&pj_crc32_init, |
| 712 |
(void (*)(void*, const pj_uint8_t*, pj_size_t))&crc32_update, |
| 713 |
(void (*)(void*, void*))&crc32_final |
| 714 |
} |
| 715 |
}; |
| 716 |
#if defined(PJ_DEBUG) && PJ_DEBUG!=0 |
| 717 |
enum { LOOP = 1000 }; |
| 718 |
#else |
| 719 |
enum { LOOP = 10000 }; |
| 720 |
#endif |
| 721 |
unsigned i; |
| 722 |
double total_len; |
| 723 |
|
| 724 |
input_len = 2048; |
| 725 |
total_len = (unsigned)input_len * LOOP; |
| 726 |
pool = pj_pool_create(mem, "enc", input_len+256, 0, NULL); |
| 727 |
if (!pool) |
| 728 |
return PJ_ENOMEM; |
| 729 |
|
| 730 |
input = (pj_uint8_t*)pj_pool_alloc(pool, input_len); |
| 731 |
pj_memset(input, '\xaa', input_len); |
| 732 |
|
| 733 |
PJ_LOG(3, (THIS_FILE, " feeding %d Mbytes of data", |
| 734 |
(unsigned)(total_len/1024/1024))); |
| 735 |
|
| 736 |
|
| 737 |
for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 738 |
algorithms[i].init_context(&context); |
| 739 |
algorithms[i].update(&context, input, (unsigned)input_len); |
| 740 |
algorithms[i].final(&context, digest); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 745 |
int j; |
| 746 |
pj_timestamp t1, t2; |
| 747 |
|
| 748 |
pj_get_timestamp(&t1); |
| 749 |
algorithms[i].init_context(&context); |
| 750 |
for (j=0; j<LOOP; ++j) { |
| 751 |
algorithms[i].update(&context, input, (unsigned)input_len); |
| 752 |
} |
| 753 |
algorithms[i].final(&context, digest); |
| 754 |
pj_get_timestamp(&t2); |
| 755 |
|
| 756 |
algorithms[i].t = pj_elapsed_usec(&t1, &t2); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) { |
| 761 |
double bytes; |
| 762 |
|
| 763 |
bytes = (total_len * 1000000 / algorithms[i].t); |
| 764 |
PJ_LOG(3, (THIS_FILE, " %s:%8d usec (%3d.%03d Mbytes/sec)", |
| 765 |
algorithms[i].name, algorithms[i].t, |
| 766 |
(unsigned)(bytes / 1024 / 1024), |
| 767 |
((unsigned)(bytes) % (1024 * 1024)) / 1024)); |
| 768 |
} |
| 769 |
|
| 770 |
pj_pool_release(pool); |
| 771 |
|
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
#endif |
| 778 |
|