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.

122 lines
4.4KB

  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 <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "intreadwrite.h"
  23. #include "xtea.h"
  24. #define XTEA_NUM_TESTS 6
  25. static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = {
  26. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  27. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  28. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  30. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  32. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  34. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  36. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  38. };
  39. static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = {
  40. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  41. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  42. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  43. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  44. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  45. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  46. };
  47. static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = {
  48. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  49. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  50. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  51. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  52. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  53. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  54. };
  55. static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  56. const uint8_t *ref, int len, uint8_t *iv, int dir,
  57. const char *test,
  58. void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int))
  59. {
  60. crypt(ctx, dst, src, len, iv, dir);
  61. if (memcmp(dst, ref, 8*len)) {
  62. int i;
  63. printf("%s failed\ngot ", test);
  64. for (i = 0; i < 8*len; i++)
  65. printf("%02x ", dst[i]);
  66. printf("\nexpected ");
  67. for (i = 0; i < 8*len; i++)
  68. printf("%02x ", ref[i]);
  69. printf("\n");
  70. exit(1);
  71. }
  72. }
  73. int main(void)
  74. {
  75. AVXTEA ctx;
  76. uint8_t buf[16], iv[8];
  77. int i, j;
  78. static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
  79. uint8_t ct[32];
  80. uint8_t pl[32];
  81. for (i = 0; i < XTEA_NUM_TESTS; i++) {
  82. av_xtea_init(&ctx, xtea_test_key[i]);
  83. test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt);
  84. test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt);
  85. for (j = 0; j < 4; j++)
  86. AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j]));
  87. av_xtea_le_init(&ctx, buf);
  88. for (j = 0; j < 2; j++) {
  89. AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j]));
  90. AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j]));
  91. }
  92. test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt);
  93. test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt);
  94. /* encrypt */
  95. memcpy(iv, "HALLO123", 8);
  96. av_xtea_crypt(&ctx, ct, src, 4, iv, 0);
  97. /* decrypt into pl */
  98. memcpy(iv, "HALLO123", 8);
  99. test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt);
  100. memcpy(iv, "HALLO123", 8);
  101. test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt);
  102. }
  103. printf("Test encryption/decryption success.\n");
  104. return 0;
  105. }