wrapper.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_buffer(const uint8_t* input, const uint8_t* key, uint8_t *output)
  13. {
  14. static aes_context ctx;
  15. if(key!=0)
  16. {
  17. aes_init( &ctx);
  18. aes_setkey_enc(&ctx,key,AES_KEYSIZE);
  19. }
  20. int ret=aes_crypt_ecb( &ctx, AES_ENCRYPT, (const unsigned char*)input,(unsigned char*) output );
  21. assert(ret==0);
  22. return ;
  23. }
  24. void AES_ECB_decrypt_buffer(const uint8_t* input, const uint8_t* key, uint8_t *output)
  25. {
  26. static aes_context ctx;
  27. if(key!=0)
  28. {
  29. aes_init( &ctx);
  30. aes_setkey_dec(&ctx,key,AES_KEYSIZE);
  31. }
  32. int ret=aes_crypt_ecb( &ctx, AES_DECRYPT, (const unsigned char*)input,(unsigned char*) output );
  33. assert(ret==0);
  34. return ;
  35. }
  36. void AES_CBC_encrypt_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_enc(&ctx,key,AES_KEYSIZE);
  44. }
  45. memcpy(tmp_iv,iv,16);
  46. int ret=aes_crypt_cbc( &ctx, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
  47. assert(ret==0);
  48. return ;
  49. }
  50. void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  51. {
  52. static aes_context ctx;
  53. char tmp_iv[16];
  54. if(key!=0)
  55. {
  56. aes_init( &ctx);
  57. aes_setkey_dec(&ctx,key,AES_KEYSIZE);
  58. }
  59. memcpy(tmp_iv,iv,16);
  60. int ret=aes_crypt_cbc( &ctx,AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
  61. assert(ret==0);
  62. }
  63. void AES_CFB_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  64. {
  65. static aes_context ctx;
  66. char tmp_iv[16];
  67. if(key!=0)
  68. {
  69. aes_init( &ctx);
  70. aes_setkey_enc(&ctx,key,AES_KEYSIZE);
  71. }
  72. memcpy(tmp_iv,iv,16);
  73. size_t offset=0;
  74. int ret=aes_crypt_cfb128( &ctx, AES_ENCRYPT, length,&offset, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
  75. assert(ret==0);
  76. return ;
  77. }
  78. void AES_CFB_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  79. {
  80. static aes_context ctx;
  81. char tmp_iv[16];
  82. if(key!=0)
  83. {
  84. aes_init( &ctx);
  85. aes_setkey_enc(&ctx,key,AES_KEYSIZE);// its aes_setkey_enc again, no typo
  86. }
  87. memcpy(tmp_iv,iv,16);
  88. size_t offset=0;
  89. int ret=aes_crypt_cfb128( &ctx,AES_DECRYPT, length,&offset, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
  90. assert(ret==0);
  91. return;
  92. }