comparison des.c @ 650:8e35c6f747dc libavutil

Fix a bug in 3DEC CBC decryption and add more extensive tests based on the official test vectors.
author reimar
date Tue, 03 Feb 2009 17:58:19 +0000
parents 9c0d8e9fe37b
children 825b98750e38
comparison
equal deleted inserted replaced
649:9c0d8e9fe37b 650:8e35c6f747dc
308 uint64_t iv_val = iv ? be2me_64(*(uint64_t *)iv) : 0; 308 uint64_t iv_val = iv ? be2me_64(*(uint64_t *)iv) : 0;
309 while (count-- > 0) { 309 while (count-- > 0) {
310 uint64_t dst_val; 310 uint64_t dst_val;
311 uint64_t src_val = src ? be2me_64(*(const uint64_t *)src) : 0; 311 uint64_t src_val = src ? be2me_64(*(const uint64_t *)src) : 0;
312 if (decrypt) { 312 if (decrypt) {
313 uint64_t tmp = src_val;
313 if (d->triple_des) { 314 if (d->triple_des) {
314 src_val = des_encdec(src_val, d->round_keys[2], 1); 315 src_val = des_encdec(src_val, d->round_keys[2], 1);
315 src_val = des_encdec(src_val, d->round_keys[1], 0); 316 src_val = des_encdec(src_val, d->round_keys[1], 0);
316 } 317 }
317 dst_val = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val; 318 dst_val = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val;
318 iv_val = iv ? src_val : 0; 319 iv_val = iv ? tmp : 0;
319 } else { 320 } else {
320 dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 0); 321 dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 0);
321 if (d->triple_des) { 322 if (d->triple_des) {
322 dst_val = des_encdec(dst_val, d->round_keys[1], 1); 323 dst_val = des_encdec(dst_val, d->round_keys[1], 1);
323 dst_val = des_encdec(dst_val, d->round_keys[2], 0); 324 dst_val = des_encdec(dst_val, d->round_keys[2], 0);
347 348
348 static const uint8_t test_key[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}; 349 static const uint8_t test_key[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
349 static const DECLARE_ALIGNED(8, uint8_t, plain[]) = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}; 350 static const DECLARE_ALIGNED(8, uint8_t, plain[]) = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
350 static const DECLARE_ALIGNED(8, uint8_t, crypt[]) = {0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18}; 351 static const DECLARE_ALIGNED(8, uint8_t, crypt[]) = {0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18};
351 static DECLARE_ALIGNED(8, uint8_t, tmp[8]); 352 static DECLARE_ALIGNED(8, uint8_t, tmp[8]);
353 static DECLARE_ALIGNED(8, uint8_t, large_buffer[10002][8]);
354 static const uint8_t cbc_key[] = {
355 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
356 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
357 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
358 };
359
360 int run_test(int cbc, int decrypt) {
361 AVDES d;
362 int delay = cbc && !decrypt ? 2 : 1;
363 uint64_t res;
364 AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
365 AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
366 AV_WB64(tmp, 0x1234567890abcdefULL);
367 av_des_init(&d, cbc_key, 192, decrypt);
368 av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
369 res = AV_RB64(large_buffer[9999 + delay]);
370 if (cbc) {
371 if (decrypt)
372 return res == 0xc5cecf63ecec514cULL;
373 else
374 return res == 0xcb191f85d1ed8439ULL;
375 } else {
376 if (decrypt)
377 return res == 0x8325397644091a0aULL;
378 else
379 return res == 0xdd17e8b8b437d232ULL;
380 }
381 }
352 382
353 int main(void) { 383 int main(void) {
354 AVDES d; 384 AVDES d;
355 int i; 385 int i;
356 #ifdef GENTABLES 386 #ifdef GENTABLES
372 #endif 402 #endif
373 av_des_init(&d, test_key, 64, 0); 403 av_des_init(&d, test_key, 64, 0);
374 av_des_crypt(&d, tmp, plain, 1, NULL, 0); 404 av_des_crypt(&d, tmp, plain, 1, NULL, 0);
375 if (memcmp(tmp, crypt, sizeof(crypt))) { 405 if (memcmp(tmp, crypt, sizeof(crypt))) {
376 printf("Public API decryption failed\n"); 406 printf("Public API decryption failed\n");
407 return 1;
408 }
409 run_test(0, 0); run_test(0, 1); run_test(1, 0); run_test(1, 1);
410 if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
411 printf("Partial Monte-Carlo test failed\n");
377 return 1; 412 return 1;
378 } 413 }
379 for (i = 0; i < 1000000; i++) { 414 for (i = 0; i < 1000000; i++) {
380 key[0] = rand64(); key[1] = rand64(); key[2] = rand64(); 415 key[0] = rand64(); key[1] = rand64(); key[2] = rand64();
381 data = rand64(); 416 data = rand64();