sgx_aes_ctr.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2011-2017 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 SGXSSL_CTR_BITS 128
  39. #define SHIFT_BYTE 8
  40. /*
  41. * code taken from OpenSSL project.
  42. * increment counter (128-bit int) by 1
  43. */
  44. static void ctr128_inc(unsigned char *counter)
  45. {
  46. unsigned int n = 16, c = 1;
  47. do {
  48. --n;
  49. c += counter[n];
  50. counter[n] = (unsigned char)c;
  51. c >>= SHIFT_BYTE;
  52. } while (n);
  53. }
  54. /* AES-CTR 128-bit
  55. * Parameters:
  56. * Return:
  57. * sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  58. * Inputs:
  59. * sgx_aes_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  60. * uint8_t *p_src - Pointer to the input stream to be encrypted/decrypted
  61. * uint32_t src_len - Length of the input stream to be encrypted/decrypted
  62. * uint8_t *p_ctr - Pointer to the counter block
  63. * uint32_t ctr_inc_bits - Number of bits in counter to be incremented
  64. * Output:
  65. * uint8_t *p_dst - Pointer to the cipher text. Size of buffer should be >= src_len.
  66. */
  67. sgx_status_t sgx_aes_ctr_encrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
  68. const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
  69. uint8_t *p_dst)
  70. {
  71. if ((src_len > INT_MAX) || (p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
  72. {
  73. return SGX_ERROR_INVALID_PARAMETER;
  74. }
  75. /* SGXSSL based crypto implementation */
  76. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  77. int len = 0;
  78. EVP_CIPHER_CTX* ptr_ctx = NULL;
  79. // OpenSSL assumes that the counter is in the x lower bits of the IV(ivec), and that the
  80. // application has full control over overflow and the rest of the IV. This
  81. // implementation takes NO responsibility for checking that the counter
  82. // doesn't overflow into the rest of the IV when incremented.
  83. //
  84. if (ctr_inc_bits != SGXSSL_CTR_BITS)
  85. {
  86. return SGX_ERROR_INVALID_PARAMETER;
  87. }
  88. CLEAR_OPENSSL_ERROR_QUEUE;
  89. do {
  90. // Create and init ctx
  91. //
  92. if (!(ptr_ctx = EVP_CIPHER_CTX_new())) {
  93. ret = SGX_ERROR_OUT_OF_MEMORY;
  94. break;
  95. }
  96. // Initialise encrypt, key
  97. //
  98. if (1 != EVP_EncryptInit_ex(ptr_ctx, EVP_aes_128_ctr(), NULL, (unsigned char*)p_key, p_ctr)) {
  99. break;
  100. }
  101. // Provide the message to be encrypted, and obtain the encrypted output.
  102. //
  103. if (1 != EVP_EncryptUpdate(ptr_ctx, p_dst, &len, p_src, src_len)) {
  104. break;
  105. }
  106. // Finalise the encryption
  107. //
  108. if (1 != EVP_EncryptFinal_ex(ptr_ctx, p_dst + len, &len)) {
  109. break;
  110. }
  111. // Encryption success, increment counter
  112. //
  113. len = src_len;
  114. while (len >= 0) {
  115. ctr128_inc(p_ctr);
  116. len -= 16;
  117. }
  118. ret = SGX_SUCCESS;
  119. } while (0);
  120. if (ret != SGX_SUCCESS) {
  121. GET_LAST_OPENSSL_ERROR;
  122. }
  123. //clean up ctx and return
  124. //
  125. if (ptr_ctx) {
  126. EVP_CIPHER_CTX_free(ptr_ctx);
  127. }
  128. return ret;
  129. }
  130. sgx_status_t sgx_aes_ctr_decrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
  131. const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
  132. uint8_t *p_dst)
  133. {
  134. if ((src_len > INT_MAX) || (p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL)) {
  135. return SGX_ERROR_INVALID_PARAMETER;
  136. }
  137. /* SGXSSL based crypto implementation */
  138. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  139. int len = 0;
  140. EVP_CIPHER_CTX* ptr_ctx = NULL;
  141. // OpenSSL assumes that the counter is in the x lower bits of the IV(ivec), and that the
  142. // application has full control over overflow and the rest of the IV. This
  143. // implementation takes NO responsibility for checking that the counter
  144. // doesn't overflow into the rest of the IV when incremented.
  145. //
  146. if (ctr_inc_bits != SGXSSL_CTR_BITS) {
  147. return SGX_ERROR_INVALID_PARAMETER;
  148. }
  149. CLEAR_OPENSSL_ERROR_QUEUE;
  150. do {
  151. // Create and initialise the context
  152. //
  153. if (!(ptr_ctx = EVP_CIPHER_CTX_new())) {
  154. ret = SGX_ERROR_OUT_OF_MEMORY;
  155. break;
  156. }
  157. // Initialise decrypt, key and CTR
  158. //
  159. if (!EVP_DecryptInit_ex(ptr_ctx, EVP_aes_128_ctr(), NULL, (unsigned char*)p_key, p_ctr)) {
  160. break;
  161. }
  162. // Decrypt message, obtain the plaintext output
  163. //
  164. if (!EVP_DecryptUpdate(ptr_ctx, p_dst, &len, p_src, src_len)) {
  165. break;
  166. }
  167. // Finalise the decryption. A positive return value indicates success,
  168. // anything else is a failure - the plaintext is not trustworthy.
  169. //
  170. if (EVP_DecryptFinal_ex(ptr_ctx, p_dst + len, &len) <= 0) { // same notes as above - you can't write beyond src_len
  171. break;
  172. }
  173. // Success
  174. // Increment counter
  175. //
  176. len = src_len;
  177. while (len >= 0) {
  178. ctr128_inc(p_ctr);
  179. len -= 16;
  180. }
  181. ret = SGX_SUCCESS;
  182. } while (0);
  183. if (ret != SGX_SUCCESS) {
  184. GET_LAST_OPENSSL_ERROR;
  185. }
  186. //cleanup ctx, and return
  187. //
  188. if (ptr_ctx) {
  189. EVP_CIPHER_CTX_free(ptr_ctx);
  190. }
  191. return ret;
  192. }