wrapper.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "aes.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #if defined(AES256) && (AES256 == 1)
  5. #define AES_KEYSIZE 256
  6. #elif defined(AES192) && (AES192 == 1)
  7. #define AES_KEYSIZE 192
  8. #else
  9. #define AES_KEYSIZE 128
  10. #endif
  11. void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
  12. {
  13. printf("AES_ECB_encrypt not implemented\n");
  14. exit(-1);
  15. }
  16. void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
  17. {
  18. printf("AES_ECB_encrypt not implemented\n");
  19. exit(-1);
  20. }
  21. void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  22. {
  23. static aes_context ctx;
  24. static int done=0;
  25. if(done==0)
  26. {
  27. aes_init( &ctx);
  28. done=1;
  29. }
  30. char tmp_iv[16];
  31. if(key!=0) aes_setkey_enc(&ctx,key,AES_KEYSIZE);
  32. memcpy(tmp_iv,iv,16);
  33. aes_crypt_cbc( &ctx, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
  34. return ;
  35. }
  36. void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  37. {
  38. static aes_context ctx;
  39. static int done=0;
  40. if(done==0)
  41. {
  42. aes_init( &ctx);
  43. done=1;
  44. }
  45. char tmp_iv[16];
  46. if(key!=0) aes_setkey_dec(&ctx,key,AES_KEYSIZE);
  47. memcpy(tmp_iv,iv,16);
  48. aes_crypt_cbc( &ctx,AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
  49. return;
  50. }