aes_ctr.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/log.h"
  19. #include "libavutil/mem.h"
  20. #include "libavutil/aes_ctr.h"
  21. static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
  22. 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d
  23. };
  24. static DECLARE_ALIGNED(8, uint8_t, tmp)[11];
  25. int main (void)
  26. {
  27. int ret = 1;
  28. struct AVAESCTR *ae, *ad;
  29. const uint8_t *iv;
  30. ae = av_aes_ctr_alloc();
  31. ad = av_aes_ctr_alloc();
  32. if (!ae || !ad)
  33. goto ERROR;
  34. if (av_aes_ctr_init(ae, (const uint8_t*)"0123456789abcdef") < 0)
  35. goto ERROR;
  36. if (av_aes_ctr_init(ad, (const uint8_t*)"0123456789abcdef") < 0)
  37. goto ERROR;
  38. av_aes_ctr_set_random_iv(ae);
  39. iv = av_aes_ctr_get_iv(ae);
  40. av_aes_ctr_set_full_iv(ad, iv);
  41. av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
  42. av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp));
  43. if (memcmp(tmp, plain, sizeof(tmp)) != 0){
  44. av_log(NULL, AV_LOG_ERROR, "test failed\n");
  45. goto ERROR;
  46. }
  47. av_log(NULL, AV_LOG_INFO, "test passed\n");
  48. ret = 0;
  49. ERROR:
  50. av_aes_ctr_free(ae);
  51. av_aes_ctr_free(ad);
  52. return ret;
  53. }