sgx_aes_ctr.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "sgx_tcrypto.h"
  32. #include "ippcp.h"
  33. #include "stdlib.h"
  34. #include "string.h"
  35. /* AES-CTR 128-bit
  36. * Parameters:
  37. * Return:
  38. * sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  39. * Inputs:
  40. * sgx_aes_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  41. * uint8_t *p_src - Pointer to the input stream to be encrypted/decrypted
  42. * uint32_t src_len - Length of the input stream to be encrypted/decrypted
  43. * uint8_t *p_ctr - Pointer to the counter block
  44. * uint32_t ctr_inc_bits - Number of bits in counter to be incremented
  45. * Output:
  46. * uint8_t *p_dst - Pointer to the cipher text. Size of buffer should be >= src_len.
  47. */
  48. sgx_status_t sgx_aes_ctr_encrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
  49. const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
  50. uint8_t *p_dst)
  51. {
  52. IppStatus error_code = ippStsNoErr;
  53. IppsAESSpec* ptr_ctx = NULL;
  54. int ctx_size = 0;
  55. if ((p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
  56. {
  57. return SGX_ERROR_INVALID_PARAMETER;
  58. }
  59. // AES-CTR-128 encryption
  60. error_code = ippsAESGetSize(&ctx_size);
  61. if (error_code != ippStsNoErr)
  62. {
  63. return SGX_ERROR_UNEXPECTED;
  64. }
  65. ptr_ctx = (IppsAESSpec*)malloc(ctx_size);
  66. if (ptr_ctx == NULL)
  67. {
  68. return SGX_ERROR_OUT_OF_MEMORY;
  69. }
  70. // Init
  71. error_code = ippsAESInit((const Ipp8u*)p_key, SGX_AESCTR_KEY_SIZE, ptr_ctx, ctx_size);
  72. if (error_code != ippStsNoErr)
  73. {
  74. // Clear temp State before free.
  75. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  76. free(ptr_ctx);
  77. switch (error_code)
  78. {
  79. case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
  80. case ippStsNullPtrErr:
  81. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  82. default: return SGX_ERROR_UNEXPECTED;
  83. }
  84. }
  85. error_code = ippsAESEncryptCTR(p_src, p_dst, src_len, ptr_ctx, p_ctr, ctr_inc_bits);
  86. if (error_code != ippStsNoErr)
  87. {
  88. // Clear temp State before free.
  89. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  90. free(ptr_ctx);
  91. switch (error_code)
  92. {
  93. case ippStsCTRSizeErr:
  94. case ippStsNullPtrErr:
  95. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  96. default: return SGX_ERROR_UNEXPECTED;
  97. }
  98. }
  99. // Clear temp State before free.
  100. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  101. free(ptr_ctx);
  102. return SGX_SUCCESS;
  103. }
  104. sgx_status_t sgx_aes_ctr_decrypt(const sgx_aes_ctr_128bit_key_t *p_key, const uint8_t *p_src,
  105. const uint32_t src_len, uint8_t *p_ctr, const uint32_t ctr_inc_bits,
  106. uint8_t *p_dst)
  107. {
  108. IppStatus error_code = ippStsNoErr;
  109. IppsAESSpec* ptr_ctx = NULL;
  110. int ctx_size = 0;
  111. if ((p_key == NULL) || (p_src == NULL) || (p_ctr == NULL) || (p_dst == NULL))
  112. {
  113. return SGX_ERROR_INVALID_PARAMETER;
  114. }
  115. // AES-CTR-128 encryption
  116. error_code = ippsAESGetSize(&ctx_size);
  117. if (error_code != ippStsNoErr)
  118. {
  119. return SGX_ERROR_UNEXPECTED;
  120. }
  121. ptr_ctx = (IppsAESSpec*)malloc(ctx_size);
  122. if (ptr_ctx == NULL)
  123. {
  124. return SGX_ERROR_OUT_OF_MEMORY;
  125. }
  126. // Init
  127. error_code = ippsAESInit((const Ipp8u*)p_key, SGX_AESCTR_KEY_SIZE, ptr_ctx, ctx_size);
  128. if (error_code != ippStsNoErr)
  129. {
  130. // Clear temp State before free.
  131. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  132. free(ptr_ctx);
  133. switch (error_code)
  134. {
  135. case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
  136. case ippStsNullPtrErr:
  137. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  138. default: return SGX_ERROR_UNEXPECTED;
  139. }
  140. }
  141. error_code = ippsAESDecryptCTR(p_src, p_dst, src_len, ptr_ctx, p_ctr, ctr_inc_bits);
  142. if (error_code != ippStsNoErr)
  143. {
  144. // Clear temp State before free.
  145. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  146. free(ptr_ctx);
  147. switch (error_code)
  148. {
  149. case ippStsCTRSizeErr:
  150. case ippStsNullPtrErr:
  151. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  152. default: return SGX_ERROR_UNEXPECTED;
  153. }
  154. }
  155. // Clear temp State before free.
  156. memset_s(ptr_ctx, ctx_size, 0, ctx_size);
  157. free(ptr_ctx);
  158. return SGX_SUCCESS;
  159. }