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.

132 lines
4.1KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/des.c"
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libavutil/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. union word_byte {
  64. uint64_t word;
  65. uint8_t byte[8];
  66. };
  67. int main(void)
  68. {
  69. AVDES d;
  70. int i;
  71. union word_byte key[3], data, ct;
  72. uint64_t roundkeys[16];
  73. srand(av_gettime());
  74. key[0].word = AV_RB64(test_key);
  75. data.word = AV_RB64(plain);
  76. gen_roundkeys(roundkeys, key[0].word);
  77. if (des_encdec(data.word, roundkeys, 0) != AV_RB64(crypt)) {
  78. printf("Test 1 failed\n");
  79. return 1;
  80. }
  81. av_des_init(&d, test_key, 64, 0);
  82. av_des_crypt(&d, tmp, plain, 1, NULL, 0);
  83. if (memcmp(tmp, crypt, sizeof(crypt))) {
  84. printf("Public API decryption failed\n");
  85. return 1;
  86. }
  87. if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
  88. printf("Partial Monte-Carlo test failed\n");
  89. return 1;
  90. }
  91. for (i = 0; i < 1000; i++) {
  92. key[0].word = rand64();
  93. key[1].word = rand64();
  94. key[2].word = rand64();
  95. data.word = rand64();
  96. av_des_init(&d, key[0].byte, 192, 0);
  97. av_des_crypt(&d, ct.byte, data.byte, 1, NULL, 0);
  98. av_des_init(&d, key[0].byte, 192, 1);
  99. av_des_crypt(&d, ct.byte, ct.byte, 1, NULL, 1);
  100. if (ct.word != data.word) {
  101. printf("Test 2 failed\n");
  102. return 1;
  103. }
  104. }
  105. #ifdef GENTABLES
  106. printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
  107. for (i = 0; i < 8; i++) {
  108. int j;
  109. printf(" {");
  110. for (j = 0; j < 64; j++) {
  111. uint32_t v = S_boxes[i][j >> 1];
  112. v = j & 1 ? v >> 4 : v & 0xf;
  113. v <<= 28 - 4 * i;
  114. v = shuffle(v, P_shuffle, sizeof(P_shuffle));
  115. printf((j & 7) == 0 ? "\n " : " ");
  116. printf("0x%08X,", v);
  117. }
  118. printf("\n },\n");
  119. }
  120. printf("};\n");
  121. #endif
  122. return 0;
  123. }