aes-gcm.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // aes-gcm.c
  3. // Pods
  4. //
  5. // Created by Markus Kosmal on 20/11/14.
  6. //
  7. //
  8. #include "aes-gcm.h"
  9. int aes_gcm_encrypt(unsigned char* output, const unsigned char* input, int input_length, const unsigned char* key, const size_t key_len, const unsigned char * iv, const size_t iv_len){
  10. int ret = 0; // our return value
  11. gcm_context ctx; // includes the AES context structure
  12. size_t tag_len = 0;
  13. unsigned char * tag_buf = NULL;
  14. gcm_setkey( &ctx, key, (const uint)key_len );
  15. ret = gcm_crypt_and_tag( &ctx, ENCRYPT, iv, iv_len, NULL, 0,
  16. input, output, input_length, tag_buf, tag_len);
  17. gcm_zero_ctx( &ctx );
  18. return( ret );
  19. }
  20. int aes_gcm_decrypt(unsigned char* output, const unsigned char* input, int input_length, const unsigned char* key, const size_t key_len, const unsigned char * iv, const size_t iv_len){
  21. int ret = 0; // our return value
  22. gcm_context ctx; // includes the AES context structure
  23. size_t tag_len = 0;
  24. unsigned char * tag_buf = NULL;
  25. gcm_setkey( &ctx, key, (const uint)key_len );
  26. ret = gcm_crypt_and_tag( &ctx, DECRYPT, iv, iv_len, NULL, 0,
  27. input, output, input_length, tag_buf, tag_len);
  28. gcm_zero_ctx( &ctx );
  29. return( ret );
  30. }