ecp.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "sgx_ecp_types.h"
  32. #include "ecp_interface.h"
  33. #include "stdlib.h"
  34. #include "string.h"
  35. #ifndef ERROR_BREAK
  36. #define ERROR_BREAK(x) if(x != ippStsNoErr){break;}
  37. #endif
  38. #ifndef NULL_BREAK
  39. #define NULL_BREAK(x) if(!x){break;}
  40. #endif
  41. #ifndef SAFE_FREE
  42. #define SAFE_FREE(ptr) {if (NULL != (ptr)) {free(ptr); (ptr)=NULL;}}
  43. #endif
  44. #define MAC_KEY_SIZE 16
  45. #define EC_DERIVATION_BUFFER_SIZE(label_length) ((label_length) +4)
  46. sgx_status_t derive_key(
  47. const sgx_ec256_dh_shared_t* shared_key,
  48. const char* label,
  49. uint32_t label_length,
  50. sgx_ec_key_128bit_t* derived_key)
  51. {
  52. sgx_status_t se_ret = SGX_SUCCESS;
  53. uint8_t cmac_key[MAC_KEY_SIZE];
  54. sgx_ec_key_128bit_t key_derive_key;
  55. if (!shared_key || !derived_key || !label)
  56. {
  57. return SGX_ERROR_INVALID_PARAMETER;
  58. }
  59. /*check integer overflow */
  60. if (label_length > EC_DERIVATION_BUFFER_SIZE(label_length))
  61. {
  62. return SGX_ERROR_INVALID_PARAMETER;
  63. }
  64. memset(cmac_key, 0, MAC_KEY_SIZE);
  65. se_ret = sgx_rijndael128_cmac_msg((sgx_cmac_128bit_key_t *)cmac_key,
  66. (uint8_t*)shared_key,
  67. sizeof(sgx_ec256_dh_shared_t),
  68. (sgx_cmac_128bit_tag_t *)&key_derive_key);
  69. if (SGX_SUCCESS != se_ret)
  70. {
  71. memset_s(&key_derive_key, sizeof(key_derive_key), 0, sizeof(key_derive_key));
  72. INTERNAL_SGX_ERROR_CODE_CONVERTOR(se_ret);
  73. return se_ret;
  74. }
  75. /* derivation_buffer = counter(0x01) || label || 0x00 || output_key_len(0x0080) */
  76. uint32_t derivation_buffer_length = EC_DERIVATION_BUFFER_SIZE(label_length);
  77. uint8_t *p_derivation_buffer = (uint8_t *)malloc(derivation_buffer_length);
  78. if (p_derivation_buffer == NULL)
  79. {
  80. return SGX_ERROR_OUT_OF_MEMORY;
  81. }
  82. memset(p_derivation_buffer, 0, derivation_buffer_length);
  83. /*counter = 0x01 */
  84. p_derivation_buffer[0] = 0x01;
  85. /*label*/
  86. memcpy(&p_derivation_buffer[1], label, label_length);
  87. /*output_key_len=0x0080*/
  88. uint16_t *key_len = (uint16_t *)&p_derivation_buffer[derivation_buffer_length - 2];
  89. *key_len = 0x0080;
  90. se_ret = sgx_rijndael128_cmac_msg((sgx_cmac_128bit_key_t *)&key_derive_key,
  91. p_derivation_buffer,
  92. derivation_buffer_length,
  93. (sgx_cmac_128bit_tag_t *)derived_key);
  94. memset_s(&key_derive_key, sizeof(key_derive_key), 0, sizeof(key_derive_key));
  95. free(p_derivation_buffer);
  96. if(SGX_SUCCESS != se_ret)
  97. {
  98. INTERNAL_SGX_ERROR_CODE_CONVERTOR(se_ret);
  99. }
  100. return se_ret;
  101. }