aes_gcm.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H
  2. #define PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H
  3. #include <iostream>
  4. #include <openssl/aes.h>
  5. #include <openssl/evp.h>
  6. #include <string>
  7. #include "basic.h"
  8. namespace util {
  9. namespace crypto {
  10. // aes key 32 byte for 256 bit
  11. #define AES_GCM_KEY_LENGTH 32
  12. // aes tag 16 byte for 128 bit
  13. #define AES_GCM_TAG_LENGTH 16
  14. // aes iv 12 byte for 96 bit
  15. #define AES_GCM_IV_LENGTH 16
  16. class AesGcm {
  17. public:
  18. /**
  19. * \brief initial aes-gcm-256 context use key & iv
  20. *
  21. * \note initial aes-gcm-256 context use key & iv. gcm mode
  22. * will generate a tag(16 byte), so the ciphertext's length
  23. * should be longer 16 byte than plaintext.
  24. *
  25. *
  26. * \param plaintext plain text to be encrypted(in)
  27. * \param len plain text's length(in)
  28. * \param key aes key (in)
  29. * \param iv aes iv (in)
  30. * \param ciphertext encrypted text(out)
  31. * \param out_len encrypted length(out)
  32. *
  33. * \return return 0 if successful
  34. * -1 EVP_CIPHER_CTX_new or aes_gcm_key error
  35. * -2 EVP_EncryptUpdate error
  36. * -3 EVP_EncryptFinal_ex error
  37. * -4 EVP_CIPHER_CTX_ctrl error
  38. */
  39. static int encrypt_aes_gcm(
  40. const unsigned char* plaintext,
  41. const int& len,
  42. const unsigned char* key,
  43. const unsigned char* iv,
  44. unsigned char* ciphertext,
  45. int& out_len);
  46. /**
  47. * \brief encrypt using aes-gcm-256
  48. *
  49. * \note encrypt using aes-gcm-256
  50. *
  51. * \param ciphertext cipher text to be decrypted(in)
  52. * \param len plain text's length(in)
  53. * \param key aes key (in)
  54. * \param iv aes iv (in)
  55. * \param plaintext decrypted text(out)
  56. * \param out_len decrypted length(out)
  57. *
  58. * \return return 0 if successful
  59. * -1 EVP_CIPHER_CTX_new or aes_gcm_key error
  60. * -2 EVP_DecryptUpdate error
  61. * -3 EVP_CIPHER_CTX_ctrl error
  62. * -4 EVP_DecryptFinal_ex error
  63. */
  64. static int decrypt_aes_gcm(
  65. const unsigned char* ciphertext,
  66. const int& len,
  67. const unsigned char* key,
  68. const unsigned char* iv,
  69. unsigned char* plaintext,
  70. int& out_len);
  71. private:
  72. /**
  73. * \brief initial aes-gcm-256 context use key & iv
  74. *
  75. * \note initial aes-gcm-256 context use key & iv
  76. *
  77. * \param key aes key (in)
  78. * \param iv aes iv (in)
  79. * \param e_ctx encryption context(out)
  80. * \param d_ctx decryption context(out)
  81. *
  82. * \return return 0 if successful
  83. * -1 EVP_xxcryptInit_ex error
  84. * -2 EVP_CIPHER_CTX_ctrl error
  85. * -3 EVP_xxcryptInit_ex error
  86. */
  87. static int aes_gcm_key(
  88. const unsigned char* key,
  89. const unsigned char* iv,
  90. EVP_CIPHER_CTX* e_ctx,
  91. EVP_CIPHER_CTX* d_ctx);
  92. /**
  93. * \brief initial aes-gcm-256 context use key & iv
  94. *
  95. * \note initial aes-gcm-256 context use key & iv
  96. *
  97. * \param key aes key (in)
  98. * \param iv aes iv (in)
  99. * \param e_ctx encryption context(out)
  100. * \param d_ctx decryption context(out)
  101. *
  102. * \return return 0 if successful
  103. * -1 EVP_xxcryptInit_ex error
  104. * -2 EVP_CIPHER_CTX_ctrl error
  105. * -3 EVP_xxcryptInit_ex error
  106. * -4 invalid key length or iv length
  107. * -5 hex_to_byte error
  108. */
  109. static int aes_gcm_key(
  110. const std::string& key_hex,
  111. const std::string& iv_hex,
  112. EVP_CIPHER_CTX* e_ctx,
  113. EVP_CIPHER_CTX* d_ctx);
  114. };
  115. } // namespace crypt
  116. } // namespace common
  117. #endif // PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H