des.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/timer.h"
  19. #include "libavutil/des.c"
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "libavutil/time.h"
  25. static uint64_t rand64(void)
  26. {
  27. uint64_t r = rand();
  28. r = (r << 32) | rand();
  29. return r;
  30. }
  31. static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
  32. static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  33. static const DECLARE_ALIGNED(8, uint8_t, crypt_ref)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
  34. static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
  35. static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
  36. static const uint8_t cbc_key[] = {
  37. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  38. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  39. 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
  40. };
  41. static int run_test(int cbc, int decrypt)
  42. {
  43. AVDES d;
  44. int delay = cbc && !decrypt ? 2 : 1;
  45. uint64_t res;
  46. AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
  47. AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
  48. AV_WB64(tmp, 0x1234567890abcdefULL);
  49. av_des_init(&d, cbc_key, 192, decrypt);
  50. av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
  51. res = AV_RB64(large_buffer[9999 + delay]);
  52. if (cbc) {
  53. if (decrypt)
  54. return res == 0xc5cecf63ecec514cULL;
  55. else
  56. return res == 0xcb191f85d1ed8439ULL;
  57. } else {
  58. if (decrypt)
  59. return res == 0x8325397644091a0aULL;
  60. else
  61. return res == 0xdd17e8b8b437d232ULL;
  62. }
  63. }
  64. union word_byte {
  65. uint64_t word;
  66. uint8_t byte[8];
  67. };
  68. int main(void)
  69. {
  70. AVDES d;
  71. int i;
  72. union word_byte key[3], data, ct;
  73. uint64_t roundkeys[16];
  74. srand(av_gettime());
  75. key[0].word = AV_RB64(test_key);
  76. data.word = AV_RB64(plain);
  77. gen_roundkeys(roundkeys, key[0].word);
  78. if (des_encdec(data.word, roundkeys, 0) != AV_RB64(crypt_ref)) {
  79. printf("Test 1 failed\n");
  80. return 1;
  81. }
  82. av_des_init(&d, test_key, 64, 0);
  83. av_des_crypt(&d, tmp, plain, 1, NULL, 0);
  84. if (memcmp(tmp, crypt_ref, sizeof(crypt_ref))) {
  85. printf("Public API decryption failed\n");
  86. return 1;
  87. }
  88. if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
  89. printf("Partial Monte-Carlo test failed\n");
  90. return 1;
  91. }
  92. for (i = 0; i < 1000; i++) {
  93. key[0].word = rand64();
  94. key[1].word = rand64();
  95. key[2].word = rand64();
  96. data.word = rand64();
  97. av_des_init(&d, key[0].byte, 192, 0);
  98. av_des_crypt(&d, ct.byte, data.byte, 1, NULL, 0);
  99. av_des_init(&d, key[0].byte, 192, 1);
  100. av_des_crypt(&d, ct.byte, ct.byte, 1, NULL, 1);
  101. if (ct.word != data.word) {
  102. printf("Test 2 failed\n");
  103. return 1;
  104. }
  105. }
  106. #ifdef GENTABLES
  107. printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
  108. for (i = 0; i < 8; i++) {
  109. int j;
  110. printf(" {");
  111. for (j = 0; j < 64; j++) {
  112. uint32_t v = S_boxes[i][j >> 1];
  113. v = j & 1 ? v >> 4 : v & 0xf;
  114. v <<= 28 - 4 * i;
  115. v = shuffle(v, P_shuffle, sizeof(P_shuffle));
  116. printf((j & 7) == 0 ? "\n " : " ");
  117. printf("0x%08X,", v);
  118. }
  119. printf("\n },\n");
  120. }
  121. printf("};\n");
  122. #endif
  123. return 0;
  124. }