sgx_cmac128.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/cmac.h"
  36. #include "openssl/err.h"
  37. /* Message Authentication - Rijndael 128 CMAC
  38. * Parameters:
  39. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  40. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to key used in encryption/decryption operation
  41. * uint8_t *p_src - Pointer to input stream to be MACed
  42. * uint32_t src_len - Length of input stream to be MACed
  43. * Output: sgx_cmac_gcm_128bit_tag_t *p_mac - Pointer to resultant MAC */
  44. sgx_status_t sgx_rijndael128_cmac_msg(const sgx_cmac_128bit_key_t *p_key, const uint8_t *p_src,
  45. uint32_t src_len, sgx_cmac_128bit_tag_t *p_mac)
  46. {
  47. void* pState = NULL;
  48. if ((p_key == NULL) || (p_src == NULL) || (p_mac == NULL)) {
  49. return SGX_ERROR_INVALID_PARAMETER;
  50. }
  51. size_t mactlen;
  52. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  53. CLEAR_OPENSSL_ERROR_QUEUE;
  54. do {
  55. //create a new ctx of CMAC
  56. //
  57. pState = CMAC_CTX_new();
  58. if (pState == NULL) {
  59. ret = SGX_ERROR_OUT_OF_MEMORY;
  60. break;
  61. }
  62. // init CMAC ctx with the corresponding size, key and AES alg.
  63. //
  64. if (!CMAC_Init((CMAC_CTX*)pState, (const void *)p_key, SGX_CMAC_KEY_SIZE, EVP_aes_128_cbc(), NULL)) {
  65. break;
  66. }
  67. // perform CMAC hash on p_src
  68. //
  69. if (!CMAC_Update((CMAC_CTX *)pState, p_src, src_len)) {
  70. break;
  71. }
  72. // finalize CMAC hashing
  73. //
  74. if (!CMAC_Final((CMAC_CTX*)pState, (unsigned char*)p_mac, &mactlen)) {
  75. break;
  76. }
  77. //validate mac size
  78. //
  79. if (mactlen != SGX_CMAC_MAC_SIZE) {
  80. break;
  81. }
  82. ret = SGX_SUCCESS;
  83. } while (0);
  84. // in case of error in debug mode, update openssl last error variable.
  85. //
  86. if (ret != SGX_SUCCESS) {
  87. GET_LAST_OPENSSL_ERROR;
  88. }
  89. // we're done, clear and free CMAC ctx
  90. //
  91. if (pState) {
  92. CMAC_CTX_free((CMAC_CTX*)pState);
  93. }
  94. return ret;
  95. }
  96. /* Allocates and initializes CMAC state
  97. * Parameters:
  98. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  99. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  100. * Output: sgx_cmac_state_handle_t *p_cmac_handle - Pointer to the handle of the CMAC state */
  101. sgx_status_t sgx_cmac128_init(const sgx_cmac_128bit_key_t *p_key, sgx_cmac_state_handle_t* p_cmac_handle)
  102. {
  103. if ((p_key == NULL) || (p_cmac_handle == NULL)) {
  104. return SGX_ERROR_INVALID_PARAMETER;
  105. }
  106. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  107. void* pState = NULL;
  108. CLEAR_OPENSSL_ERROR_QUEUE;
  109. do {
  110. // create CMAC ctx
  111. //
  112. pState = CMAC_CTX_new();
  113. if (pState == NULL) {
  114. ret = SGX_ERROR_OUT_OF_MEMORY;
  115. break;
  116. }
  117. //init CMAC ctx
  118. //
  119. if (!CMAC_Init((CMAC_CTX*)pState, (const void *)p_key, SGX_CMAC_KEY_SIZE, EVP_aes_128_cbc(), NULL)) {
  120. CMAC_CTX_free((CMAC_CTX*)pState);
  121. break;
  122. }
  123. *p_cmac_handle = pState;
  124. ret = SGX_SUCCESS;
  125. } while (0);
  126. // in case of error in debug mode, update openssl last error variable.
  127. //
  128. if (ret != SGX_SUCCESS) {
  129. GET_LAST_OPENSSL_ERROR;
  130. }
  131. return ret;
  132. }
  133. /* Updates CMAC has calculation based on the input message
  134. * Parameters:
  135. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.
  136. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  137. * uint8_t *p_src - Pointer to the input stream to be hashed
  138. * uint32_t src_len - Length of the input stream to be hashed */
  139. sgx_status_t sgx_cmac128_update(const uint8_t *p_src, uint32_t src_len, sgx_cmac_state_handle_t cmac_handle)
  140. {
  141. if ((p_src == NULL) || (cmac_handle == NULL)) {
  142. return SGX_ERROR_INVALID_PARAMETER;
  143. }
  144. CLEAR_OPENSSL_ERROR_QUEUE;
  145. if (!CMAC_Update((CMAC_CTX *)cmac_handle, p_src, src_len)) {
  146. GET_LAST_OPENSSL_ERROR;
  147. return SGX_ERROR_UNEXPECTED;
  148. }
  149. return SGX_SUCCESS;
  150. }
  151. /* Returns Hash calculation
  152. * Parameters:
  153. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  154. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  155. * Output: sgx_cmac_128bit_tag_t *p_hash - Resultant hash from operation */
  156. sgx_status_t sgx_cmac128_final(sgx_cmac_state_handle_t cmac_handle, sgx_cmac_128bit_tag_t *p_hash)
  157. {
  158. if ((cmac_handle == NULL) || (p_hash == NULL))
  159. {
  160. return SGX_ERROR_INVALID_PARAMETER;
  161. }
  162. size_t mactlen;
  163. CLEAR_OPENSSL_ERROR_QUEUE;
  164. if (!CMAC_Final((CMAC_CTX*)cmac_handle, (unsigned char*)p_hash, &mactlen)) {
  165. GET_LAST_OPENSSL_ERROR;
  166. return SGX_ERROR_UNEXPECTED;
  167. }
  168. return SGX_SUCCESS;
  169. }
  170. /* Clean up the CMAC state
  171. * Parameters:
  172. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  173. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state */
  174. sgx_status_t sgx_cmac128_close(sgx_cmac_state_handle_t cmac_handle)
  175. {
  176. if (cmac_handle == NULL) {
  177. return SGX_ERROR_INVALID_PARAMETER;
  178. }
  179. CMAC_CTX* pState = (CMAC_CTX*)cmac_handle;
  180. CMAC_CTX_free(pState);
  181. return SGX_SUCCESS;
  182. }