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.

129 lines
4.0KB

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