aes_gcm.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifndef PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H
  16. #define PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H
  17. #include <openssl/aes.h>
  18. #include <openssl/evp.h>
  19. #include <iostream>
  20. #include <string>
  21. #include "encryption/util/include/crypto/basic.h"
  22. namespace util {
  23. namespace crypto {
  24. // aes key 32 byte for 256 bit
  25. #define AES_GCM_KEY_LENGTH 32
  26. // aes tag 16 byte for 128 bit
  27. #define AES_GCM_TAG_LENGTH 16
  28. // aes iv 12 byte for 96 bit
  29. #define AES_GCM_IV_LENGTH 16
  30. class AesGcm {
  31. public:
  32. /**
  33. * \brief initial aes-gcm-256 context use key & iv
  34. *
  35. * \note initial aes-gcm-256 context use key & iv. gcm mode
  36. * will generate a tag(16 byte), so the ciphertext's length
  37. * should be longer 16 byte than plaintext.
  38. *
  39. *
  40. * \param plaintext plain text to be encrypted(in)
  41. * \param len plain text's length(in)
  42. * \param key aes key (in)
  43. * \param iv aes iv (in)
  44. * \param ciphertext encrypted text(out)
  45. * \param out_len encrypted length(out)
  46. *
  47. * \return return 0 if successful
  48. * -1 EVP_CIPHER_CTX_new or aes_gcm_key error
  49. * -2 EVP_EncryptUpdate error
  50. * -3 EVP_EncryptFinal_ex error
  51. * -4 EVP_CIPHER_CTX_ctrl error
  52. */
  53. static int encrypt_aes_gcm(const unsigned char* plaintext, const int& len,
  54. const unsigned char* key, const unsigned char* iv,
  55. unsigned char* ciphertext,
  56. int& out_len); // NOLINT
  57. /**
  58. * \brief encrypt using aes-gcm-256
  59. *
  60. * \note encrypt using aes-gcm-256
  61. *
  62. * \param ciphertext cipher text to be decrypted(in)
  63. * \param len plain text's length(in)
  64. * \param key aes key (in)
  65. * \param iv aes iv (in)
  66. * \param plaintext decrypted text(out)
  67. * \param out_len decrypted length(out)
  68. *
  69. * \return return 0 if successful
  70. * -1 EVP_CIPHER_CTX_new or aes_gcm_key error
  71. * -2 EVP_DecryptUpdate error
  72. * -3 EVP_CIPHER_CTX_ctrl error
  73. * -4 EVP_DecryptFinal_ex error
  74. */
  75. static int decrypt_aes_gcm(const unsigned char* ciphertext, const int& len,
  76. const unsigned char* key, const unsigned char* iv,
  77. unsigned char* plaintext, int& out_len); // NOLINT
  78. private:
  79. /**
  80. * \brief initial aes-gcm-256 context use key & iv
  81. *
  82. * \note initial aes-gcm-256 context use key & iv
  83. *
  84. * \param key aes key (in)
  85. * \param iv aes iv (in)
  86. * \param e_ctx encryption context(out)
  87. * \param d_ctx decryption context(out)
  88. *
  89. * \return return 0 if successful
  90. * -1 EVP_xxcryptInit_ex error
  91. * -2 EVP_CIPHER_CTX_ctrl error
  92. * -3 EVP_xxcryptInit_ex error
  93. */
  94. static int aes_gcm_key(const unsigned char* key, const unsigned char* iv,
  95. EVP_CIPHER_CTX* e_ctx, EVP_CIPHER_CTX* d_ctx);
  96. /**
  97. * \brief initial aes-gcm-256 context use key & iv
  98. *
  99. * \note initial aes-gcm-256 context use key & iv
  100. *
  101. * \param key aes key (in)
  102. * \param iv aes iv (in)
  103. * \param e_ctx encryption context(out)
  104. * \param d_ctx decryption context(out)
  105. *
  106. * \return return 0 if successful
  107. * -1 EVP_xxcryptInit_ex error
  108. * -2 EVP_CIPHER_CTX_ctrl error
  109. * -3 EVP_xxcryptInit_ex error
  110. * -4 invalid key length or iv length
  111. * -5 hex_to_byte error
  112. */
  113. static int aes_gcm_key(const std::string& key_hex, const std::string& iv_hex,
  114. EVP_CIPHER_CTX* e_ctx, EVP_CIPHER_CTX* d_ctx);
  115. };
  116. } // namespace crypto
  117. } // namespace util
  118. #endif // PADDLE_MODEL_PROTECT_UTIL_CRYPTO_AES_GCM_H