pcl_deriv.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* Content from sdk/simulation/tinst/deriv.cpp */
  32. #include "t_instructions.h" /* for `g_global_data_sim' */
  33. #include "deriv.h"
  34. #include <sgx_tseal.h>
  35. #include <pcl_common.h>
  36. #include <pcl_unseal_internal.h>
  37. extern global_data_sim_t g_global_data_sim;
  38. int pcl_cmac(const sgx_cmac_128bit_key_t *p_key, const uint8_t *p_src,
  39. uint32_t src_len, sgx_cmac_128bit_tag_t *p_mac);
  40. // The built-in seal key in simulation mode
  41. static const uint8_t PCL_BASE_SEAL_KEY[] = {
  42. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  43. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  44. };
  45. static const uint8_t* pcl_get_base_key(uint16_t key_name)
  46. {
  47. // Shouldn't use switch else requires relocation
  48. /* PCL UNUSED START *
  49. switch (key_name) {
  50. case SGX_KEYSELECT_SEAL:
  51. return BASE_SEAL_KEY;
  52. case SGX_KEYSELECT_REPORT:
  53. return BASE_REPORT_KEY;
  54. case SGX_KEYSELECT_EINITTOKEN:
  55. return BASE_EINITTOKEN_KEY;
  56. case SGX_KEYSELECT_PROVISION:
  57. return BASE_PROVISION_KEY;
  58. case SGX_KEYSELECT_PROVISION_SEAL:
  59. return BASE_PROV_SEAL_KEY;
  60. }
  61. /* PCL UNUSED END */
  62. if (key_name == SGX_KEYSELECT_SEAL) return PCL_BASE_SEAL_KEY;
  63. // Should not come here - error should have been reported
  64. // when the key name is not supported in the caller.
  65. return (uint8_t*)0;
  66. }
  67. // Compute the CMAC of derivation data with corresponding base key
  68. // and save it to `okey'.
  69. void pcl_derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey)
  70. {
  71. const uint8_t* base_key = pcl_get_base_key(dd->key_name);
  72. pcl_cmac((const sgx_cmac_128bit_key_t*)base_key,dd->ddbuf, dd->size, (sgx_cmac_128bit_tag_t*)okey);
  73. }