xtea.c 4.5 KB

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