sgx_cmac128.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* Message Authentication - Rijndael 128 CMAC
  36. * Parameters:
  37. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  38. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to key used in encryption/decryption operation
  39. * uint8_t *p_src - Pointer to input stream to be MACed
  40. * uint32_t src_len - Length of input stream to be MACed
  41. * Output: sgx_cmac_gcm_128bit_tag_t *p_mac - Pointer to resultant MAC */
  42. sgx_status_t sgx_rijndael128_cmac_msg(const sgx_cmac_128bit_key_t *p_key, const uint8_t *p_src,
  43. uint32_t src_len, sgx_cmac_128bit_tag_t *p_mac)
  44. {
  45. IppsAES_CMACState* pState = NULL;
  46. int ippStateSize = 0;
  47. IppStatus error_code = ippStsNoErr;
  48. if ((p_key == NULL) || (p_src == NULL) || (p_mac == NULL))
  49. {
  50. return SGX_ERROR_INVALID_PARAMETER;
  51. }
  52. error_code = ippsAES_CMACGetSize(&ippStateSize);
  53. if (error_code != ippStsNoErr)
  54. {
  55. return SGX_ERROR_UNEXPECTED;
  56. }
  57. pState = (IppsAES_CMACState*)malloc(ippStateSize);
  58. if (pState == NULL)
  59. {
  60. return SGX_ERROR_OUT_OF_MEMORY;
  61. }
  62. error_code = ippsAES_CMACInit((const Ipp8u *)p_key, SGX_CMAC_KEY_SIZE, pState, ippStateSize);
  63. if (error_code != ippStsNoErr)
  64. {
  65. // Clear temp State before free.
  66. memset_s(pState, ippStateSize, 0, ippStateSize);
  67. free(pState);
  68. switch (error_code)
  69. {
  70. case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
  71. case ippStsNullPtrErr:
  72. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  73. default: return SGX_ERROR_UNEXPECTED;
  74. }
  75. }
  76. error_code = ippsAES_CMACUpdate((const Ipp8u *)p_src, src_len, pState);
  77. if (error_code != ippStsNoErr)
  78. {
  79. // Clear temp State before free.
  80. memset_s(pState, ippStateSize, 0, ippStateSize);
  81. free(pState);
  82. switch (error_code)
  83. {
  84. case ippStsNullPtrErr:
  85. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  86. default: return SGX_ERROR_UNEXPECTED;
  87. }
  88. }
  89. error_code = ippsAES_CMACFinal((Ipp8u *)p_mac, SGX_CMAC_MAC_SIZE, pState);
  90. if (error_code != ippStsNoErr)
  91. {
  92. // Clear temp State before free.
  93. memset_s(pState, ippStateSize, 0, ippStateSize);
  94. free(pState);
  95. switch (error_code)
  96. {
  97. case ippStsNullPtrErr:
  98. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  99. default: return SGX_ERROR_UNEXPECTED;
  100. }
  101. }
  102. // Clear temp State before free.
  103. memset_s(pState, ippStateSize, 0, ippStateSize);
  104. free(pState);
  105. return SGX_SUCCESS;
  106. }
  107. static void sgx_secure_free_cmac128_state(IppsAES_CMACState *pState)
  108. {
  109. if (pState == NULL)
  110. return;
  111. int ippStateSize = 0;
  112. IppStatus error_code = ippStsNoErr;
  113. error_code = ippsAES_CMACGetSize(&ippStateSize);
  114. if (error_code != ippStsNoErr)
  115. {
  116. free(pState);
  117. return;
  118. }
  119. memset_s(pState, ippStateSize, 0, ippStateSize);
  120. free(pState);
  121. return;
  122. }
  123. /* Allocates and initializes CMAC state
  124. * Parameters:
  125. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  126. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  127. * Output: sgx_cmac_state_handle_t *p_cmac_handle - Pointer to the handle of the CMAC state */
  128. sgx_status_t sgx_cmac128_init(const sgx_cmac_128bit_key_t *p_key, sgx_cmac_state_handle_t* p_cmac_handle)
  129. {
  130. if ((p_key == NULL) || (p_cmac_handle == NULL))
  131. {
  132. return SGX_ERROR_INVALID_PARAMETER;
  133. }
  134. IppsAES_CMACState* pState = NULL;
  135. int ippStateSize = 0;
  136. IppStatus error_code = ippStsNoErr;
  137. error_code = ippsAES_CMACGetSize(&ippStateSize);
  138. if (error_code != ippStsNoErr)
  139. {
  140. return SGX_ERROR_UNEXPECTED;
  141. }
  142. pState = (IppsAES_CMACState*)malloc(ippStateSize);
  143. if (pState == NULL)
  144. {
  145. return SGX_ERROR_OUT_OF_MEMORY;
  146. }
  147. error_code = ippsAES_CMACInit((const Ipp8u *)p_key, SGX_CMAC_KEY_SIZE, pState, ippStateSize);
  148. if (error_code != ippStsNoErr)
  149. {
  150. // Clear state before free.
  151. memset_s(pState, ippStateSize, 0, ippStateSize);
  152. free(pState);
  153. *p_cmac_handle = NULL;
  154. switch (error_code)
  155. {
  156. case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
  157. case ippStsNullPtrErr:
  158. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  159. default: return SGX_ERROR_UNEXPECTED;
  160. }
  161. }
  162. *p_cmac_handle = pState;
  163. return SGX_SUCCESS;
  164. }
  165. /* Updates CMAC hash calculation based on the input message
  166. * Parameters:
  167. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.
  168. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  169. * uint8_t *p_src - Pointer to the input stream to be hashed
  170. * uint32_t src_len - Length of the input stream to be hashed */
  171. sgx_status_t sgx_cmac128_update(const uint8_t *p_src, uint32_t src_len, sgx_cmac_state_handle_t cmac_handle)
  172. {
  173. if ((p_src == NULL) || (cmac_handle == NULL))
  174. {
  175. return SGX_ERROR_INVALID_PARAMETER;
  176. }
  177. IppStatus error_code = ippStsNoErr;
  178. error_code = ippsAES_CMACUpdate(p_src, src_len, (IppsAES_CMACState*)cmac_handle);
  179. switch (error_code)
  180. {
  181. case ippStsNoErr: return SGX_SUCCESS;
  182. case ippStsNullPtrErr:
  183. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  184. default: return SGX_ERROR_UNEXPECTED;
  185. }
  186. }
  187. /* Returns Hash calculation
  188. * Parameters:
  189. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  190. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  191. * Output: sgx_cmac_128bit_tag_t *p_hash - Resultant hash from operation */
  192. sgx_status_t sgx_cmac128_final(sgx_cmac_state_handle_t cmac_handle, sgx_cmac_128bit_tag_t *p_hash)
  193. {
  194. if ((cmac_handle == NULL) || (p_hash == NULL))
  195. {
  196. return SGX_ERROR_INVALID_PARAMETER;
  197. }
  198. IppStatus error_code = ippStsNoErr;
  199. error_code = ippsAES_CMACFinal((Ipp8u *)p_hash, SGX_CMAC_MAC_SIZE, (IppsAES_CMACState*)cmac_handle);
  200. switch (error_code)
  201. {
  202. case ippStsNoErr: return SGX_SUCCESS;
  203. case ippStsNullPtrErr:
  204. case ippStsLengthErr: return SGX_ERROR_INVALID_PARAMETER;
  205. default: return SGX_ERROR_UNEXPECTED;
  206. }
  207. }
  208. /* Clean up the CMAC state
  209. * Parameters:
  210. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  211. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state */
  212. sgx_status_t sgx_cmac128_close(sgx_cmac_state_handle_t cmac_handle)
  213. {
  214. if (cmac_handle == NULL)
  215. return SGX_ERROR_INVALID_PARAMETER;
  216. sgx_secure_free_cmac128_state((IppsAES_CMACState*)cmac_handle);
  217. return SGX_SUCCESS;
  218. }