wrapper.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "aes.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #if defined(AES256) && (AES256 == 1)
  6. #define AES_KEYSIZE 256
  7. #elif defined(AES192) && (AES192 == 1)
  8. #define AES_KEYSIZE 192
  9. #else
  10. #define AES_KEYSIZE 128
  11. #endif
  12. void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
  13. {
  14. printf("AES_ECB_encrypt not implemented\n");
  15. exit(-1);
  16. }
  17. void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
  18. {
  19. printf("AES_ECB_encrypt not implemented\n");
  20. exit(-1);
  21. }
  22. void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  23. {
  24. static aes_context ctx;
  25. char tmp_iv[16];
  26. if(key!=0)
  27. {
  28. aes_init( &ctx);
  29. aes_setkey_enc(&ctx,key,AES_KEYSIZE);
  30. }
  31. memcpy(tmp_iv,iv,16);
  32. int ret=aes_crypt_cbc( &ctx, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
  33. assert(ret==0);
  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. char tmp_iv[16];
  40. if(key!=0)
  41. {
  42. aes_init( &ctx);
  43. aes_setkey_dec(&ctx,key,AES_KEYSIZE);
  44. }
  45. memcpy(tmp_iv,iv,16);
  46. int ret=aes_crypt_cbc( &ctx,AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
  47. assert(ret==0);
  48. }
  49. void AES_CFB_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  50. {
  51. static aes_context ctx;
  52. char tmp_iv[16];
  53. if(key!=0)
  54. {
  55. aes_init( &ctx);
  56. aes_setkey_enc(&ctx,key,AES_KEYSIZE);
  57. }
  58. memcpy(tmp_iv,iv,16);
  59. size_t offset=0;
  60. int ret=aes_crypt_cfb128( &ctx, AES_ENCRYPT, length,&offset, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
  61. assert(ret==0);
  62. return ;
  63. }
  64. void AES_CFB_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  65. {
  66. static aes_context ctx;
  67. char tmp_iv[16];
  68. if(key!=0)
  69. {
  70. aes_init( &ctx);
  71. aes_setkey_enc(&ctx,key,AES_KEYSIZE);// its aes_setkey_enc again, no typo
  72. }
  73. memcpy(tmp_iv,iv,16);
  74. size_t offset=0;
  75. int ret=aes_crypt_cfb128( &ctx,AES_DECRYPT, length,&offset, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
  76. assert(ret==0);
  77. return;
  78. }