You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.2KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/mem_internal.h"
  19. #include "libavutil/timer.h"
  20. #include "libavutil/des.c"
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "libavutil/time.h"
  26. static uint64_t rand64(void)
  27. {
  28. uint64_t r = rand();
  29. r = (r << 32) | rand();
  30. return r;
  31. }
  32. static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
  33. static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  34. static const DECLARE_ALIGNED(8, uint8_t, crypt_ref)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
  35. static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
  36. static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
  37. static const uint8_t cbc_key[] = {
  38. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  39. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  40. 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
  41. };
  42. static int run_test(int cbc, int decrypt)
  43. {
  44. AVDES d;
  45. int delay = cbc && !decrypt ? 2 : 1;
  46. uint64_t res;
  47. AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
  48. AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
  49. AV_WB64(tmp, 0x1234567890abcdefULL);
  50. av_des_init(&d, cbc_key, 192, decrypt);
  51. av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
  52. res = AV_RB64(large_buffer[9999 + delay]);
  53. if (cbc) {
  54. if (decrypt)
  55. return res == 0xc5cecf63ecec514cULL;
  56. else
  57. return res == 0xcb191f85d1ed8439ULL;
  58. } else {
  59. if (decrypt)
  60. return res == 0x8325397644091a0aULL;
  61. else
  62. return res == 0xdd17e8b8b437d232ULL;
  63. }
  64. }
  65. union word_byte {
  66. uint64_t word;
  67. uint8_t byte[8];
  68. };
  69. int main(void)
  70. {
  71. AVDES d;
  72. int i;
  73. union word_byte key[3], data, ct;
  74. uint64_t roundkeys[16];
  75. srand(av_gettime());
  76. key[0].word = AV_RB64(test_key);
  77. data.word = AV_RB64(plain);
  78. gen_roundkeys(roundkeys, key[0].word);
  79. if (des_encdec(data.word, roundkeys, 0) != AV_RB64(crypt_ref)) {
  80. printf("Test 1 failed\n");
  81. return 1;
  82. }
  83. av_des_init(&d, test_key, 64, 0);
  84. av_des_crypt(&d, tmp, plain, 1, NULL, 0);
  85. if (memcmp(tmp, crypt_ref, sizeof(crypt_ref))) {
  86. printf("Public API decryption failed\n");
  87. return 1;
  88. }
  89. if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
  90. printf("Partial Monte-Carlo test failed\n");
  91. return 1;
  92. }
  93. for (i = 0; i < 1000; i++) {
  94. key[0].word = rand64();
  95. key[1].word = rand64();
  96. key[2].word = rand64();
  97. data.word = rand64();
  98. av_des_init(&d, key[0].byte, 192, 0);
  99. av_des_crypt(&d, ct.byte, data.byte, 1, NULL, 0);
  100. av_des_init(&d, key[0].byte, 192, 1);
  101. av_des_crypt(&d, ct.byte, ct.byte, 1, NULL, 1);
  102. if (ct.word != data.word) {
  103. printf("Test 2 failed\n");
  104. return 1;
  105. }
  106. }
  107. #ifdef GENTABLES
  108. printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
  109. for (i = 0; i < 8; i++) {
  110. int j;
  111. printf(" {");
  112. for (j = 0; j < 64; j++) {
  113. uint32_t v = S_boxes[i][j >> 1];
  114. v = j & 1 ? v >> 4 : v & 0xf;
  115. v <<= 28 - 4 * i;
  116. v = shuffle(v, P_shuffle, sizeof(P_shuffle));
  117. printf((j & 7) == 0 ? "\n " : " ");
  118. printf("0x%08X,", v);
  119. }
  120. printf("\n },\n");
  121. }
  122. printf("};\n");
  123. #endif
  124. return 0;
  125. }