sgx_aes_gcm.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "stdlib.h"
  32. #include "string.h"
  33. #include "sgx_tcrypto.h"
  34. #include "se_tcrypto_common.h"
  35. #include "openssl/aes.h"
  36. #include "openssl/evp.h"
  37. #include "openssl/err.h"
  38. #define OPENSSL_DEFAULT_IV_LEN 12
  39. /* Rijndael AES-GCM
  40. * Parameters:
  41. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  42. * Inputs: sgx_aes_gcm_128bit_key_t *p_key - Pointer to key used in encryption/decryption operation
  43. * uint8_t *p_src - Pointer to input stream to be encrypted/decrypted
  44. * uint32_t src_len - Length of input stream to be encrypted/decrypted
  45. * uint8_t *p_iv - Pointer to initialization vector to use
  46. * uint32_t iv_len - Length of initialization vector
  47. * uint8_t *p_aad - Pointer to input stream of additional authentication data
  48. * uint32_t aad_len - Length of additional authentication data stream
  49. * sgx_aes_gcm_128bit_tag_t *p_in_mac - Pointer to expected MAC in decryption process
  50. * Output: uint8_t *p_dst - Pointer to cipher text. Size of buffer should be >= src_len.
  51. * sgx_aes_gcm_128bit_tag_t *p_out_mac - Pointer to MAC generated from encryption process
  52. * NOTE: Wrapper is responsible for confirming decryption tag matches encryption tag */
  53. sgx_status_t sgx_rijndael128GCM_encrypt(const sgx_aes_gcm_128bit_key_t *p_key, const uint8_t *p_src, uint32_t src_len,
  54. uint8_t *p_dst, const uint8_t *p_iv, uint32_t iv_len, const uint8_t *p_aad, uint32_t aad_len,
  55. sgx_aes_gcm_128bit_tag_t *p_out_mac)
  56. {
  57. if ((src_len > INT_MAX) || (aad_len > INT_MAX) || (p_key == NULL) || ((src_len > 0) && (p_dst == NULL)) || ((src_len > 0) && (p_src == NULL))
  58. || (p_out_mac == NULL) || (iv_len != SGX_AESGCM_IV_SIZE) || ((aad_len > 0) && (p_aad == NULL))
  59. || (p_iv == NULL) || ((p_src == NULL) && (p_aad == NULL)))
  60. {
  61. return SGX_ERROR_INVALID_PARAMETER;
  62. }
  63. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  64. int len = 0;
  65. EVP_CIPHER_CTX * pState = NULL;
  66. CLEAR_OPENSSL_ERROR_QUEUE;
  67. do {
  68. // Create and init ctx
  69. //
  70. if (!(pState = EVP_CIPHER_CTX_new())) {
  71. ret = SGX_ERROR_OUT_OF_MEMORY;
  72. break;
  73. }
  74. // Initialise encrypt, key and IV
  75. //
  76. if (1 != EVP_EncryptInit_ex(pState, EVP_aes_128_gcm(), NULL, (unsigned char*)p_key, p_iv)) {
  77. break;
  78. }
  79. // Provide AAD data if exist
  80. //
  81. if (NULL != p_aad) {
  82. if (1 != EVP_EncryptUpdate(pState, NULL, &len, p_aad, aad_len)) {
  83. break;
  84. }
  85. }
  86. // Provide the message to be encrypted, and obtain the encrypted output.
  87. //
  88. if (1 != EVP_EncryptUpdate(pState, p_dst, &len, p_src, src_len)) {
  89. break;
  90. }
  91. // Finalise the encryption
  92. //
  93. if (1 != EVP_EncryptFinal_ex(pState, p_dst + len, &len)) {
  94. break;
  95. }
  96. // Get tag
  97. //
  98. if (1 != EVP_CIPHER_CTX_ctrl(pState, EVP_CTRL_GCM_GET_TAG, SGX_AESGCM_MAC_SIZE, p_out_mac)) {
  99. break;
  100. }
  101. ret = SGX_SUCCESS;
  102. } while (0);
  103. if (ret != SGX_SUCCESS) {
  104. GET_LAST_OPENSSL_ERROR;
  105. }
  106. // Clean up and return
  107. //
  108. if (pState) {
  109. EVP_CIPHER_CTX_free(pState);
  110. }
  111. return ret;
  112. }
  113. sgx_status_t sgx_rijndael128GCM_decrypt(const sgx_aes_gcm_128bit_key_t *p_key, const uint8_t *p_src,
  114. uint32_t src_len, uint8_t *p_dst, const uint8_t *p_iv, uint32_t iv_len,
  115. const uint8_t *p_aad, uint32_t aad_len, const sgx_aes_gcm_128bit_tag_t *p_in_mac)
  116. {
  117. uint8_t l_tag[SGX_AESGCM_MAC_SIZE];
  118. if ((src_len > INT_MAX) || (aad_len > INT_MAX) || (p_key == NULL) || ((src_len > 0) && (p_dst == NULL)) || ((src_len > 0) && (p_src == NULL))
  119. || (p_in_mac == NULL) || (iv_len != SGX_AESGCM_IV_SIZE) || ((aad_len > 0) && (p_aad == NULL))
  120. || (p_iv == NULL) || ((p_src == NULL) && (p_aad == NULL)))
  121. {
  122. return SGX_ERROR_INVALID_PARAMETER;
  123. }
  124. int len = 0;
  125. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  126. EVP_CIPHER_CTX * pState = NULL;
  127. CLEAR_OPENSSL_ERROR_QUEUE;
  128. // Autenthication Tag returned by Decrypt to be compared with Tag created during seal
  129. //
  130. memset_s(&l_tag, SGX_AESGCM_MAC_SIZE, 0, SGX_AESGCM_MAC_SIZE);
  131. memcpy(l_tag, p_in_mac, SGX_AESGCM_MAC_SIZE);
  132. do {
  133. // Create and initialise the context
  134. //
  135. if (!(pState = EVP_CIPHER_CTX_new())) {
  136. ret = SGX_ERROR_OUT_OF_MEMORY;
  137. break;
  138. }
  139. // Initialise decrypt, key and IV
  140. //
  141. if (!EVP_DecryptInit_ex(pState, EVP_aes_128_gcm(), NULL, (unsigned char*)p_key, p_iv)) {
  142. break;
  143. }
  144. // Provide AAD data if exist
  145. //
  146. if (NULL != p_aad) {
  147. if (!EVP_DecryptUpdate(pState, NULL, &len, p_aad, aad_len)) {
  148. break;
  149. }
  150. }
  151. // Decrypt message, obtain the plaintext output
  152. //
  153. if (!EVP_DecryptUpdate(pState, p_dst, &len, p_src, src_len)) {
  154. break;
  155. }
  156. // Update expected tag value
  157. //
  158. if (!EVP_CIPHER_CTX_ctrl(pState, EVP_CTRL_GCM_SET_TAG, SGX_AESGCM_MAC_SIZE, l_tag)) {
  159. break;
  160. }
  161. // Finalise the decryption. A positive return value indicates success,
  162. // anything else is a failure - the plaintext is not trustworthy.
  163. //
  164. if (EVP_DecryptFinal_ex(pState, p_dst + len, &len) <= 0) {
  165. break;
  166. }
  167. ret = SGX_SUCCESS;
  168. } while (0);
  169. if (ret != SGX_SUCCESS) {
  170. GET_LAST_OPENSSL_ERROR;
  171. }
  172. // Clean up and return
  173. //
  174. if (pState != NULL) {
  175. EVP_CIPHER_CTX_free(pState);
  176. }
  177. memset_s(&l_tag, SGX_AESGCM_MAC_SIZE, 0, SGX_AESGCM_MAC_SIZE);
  178. return ret;
  179. }