deriv.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /* deriv.h - it defines C++ interfaces for derivation data. */
  32. #ifndef DERIVE_H__
  33. #define DERIVE_H__
  34. #include "arch.h"
  35. #include "sgx_key.h"
  36. #include "sgx_error.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #define OWNEREPOCH_SIZE 16
  41. typedef uint8_t se_owner_epoch_t[OWNEREPOCH_SIZE];
  42. /* Derive data for seal key */
  43. typedef struct {
  44. uint16_t key_name; /* should always be 'SGX_KEYSELECT_SEAL' */
  45. sgx_attributes_t tmp_attr;
  46. sgx_attributes_t attribute_mask; /* attribute mask from KEYREQUEST */
  47. se_owner_epoch_t csr_owner_epoch;
  48. sgx_cpu_svn_t cpu_svn; /* CPUSVN from KEYREQUEST */
  49. sgx_isv_svn_t isv_svn; /* ISVSVN from KEYREQUEST */
  50. sgx_prod_id_t isv_prod_id; /* ISV PRODID from SECS */
  51. sgx_measurement_t mrenclave;
  52. sgx_measurement_t mrsigner;
  53. sgx_key_id_t key_id; /* KEYID from KEYREQUEST */
  54. } dd_seal_key_t;
  55. /* Derive data for report key */
  56. typedef struct {
  57. uint16_t key_name; /* should always be 'SGX_KEYSELECT_REPORT' */
  58. sgx_attributes_t attributes; /* attributes from SECS */
  59. se_owner_epoch_t csr_owner_epoch;
  60. sgx_measurement_t mrenclave;
  61. sgx_cpu_svn_t cpu_svn; /* CPUSVN from CPUSVN register */
  62. sgx_key_id_t key_id; /* KEYID from KEYREQUEST */
  63. } dd_report_key_t;
  64. /* Derive data for license key */
  65. typedef struct {
  66. uint16_t key_name; /* should always be 'SGX_KEYSELECT_EINITTOKEN' */
  67. sgx_attributes_t attributes; /* attributes from SECS */
  68. se_owner_epoch_t csr_owner_epoch;
  69. sgx_cpu_svn_t cpu_svn; /* CPUSVN from KEYREQUEST */
  70. sgx_isv_svn_t isv_svn; /* ISVSVN from KEYREQUEST */
  71. sgx_prod_id_t isv_prod_id; /* ISV PRODID from SECS */
  72. sgx_key_id_t key_id; /* KEYID from KEYREQUEST */
  73. } dd_license_key_t;
  74. /* Derive data for provision key */
  75. typedef struct {
  76. uint16_t key_name; /* should always be 'SGX_KEYSELECT_PROVISION' */
  77. sgx_attributes_t tmp_attr;
  78. sgx_attributes_t attribute_mask; /* attribute mask from KEYREQUEST */
  79. sgx_cpu_svn_t cpu_svn; /* CPUSVN from KEYREQUEST */
  80. sgx_isv_svn_t isv_svn; /* ISVSVN from KEYREQUEST */
  81. sgx_prod_id_t isv_prod_id; /* ISV PRODID from SECS */
  82. sgx_measurement_t mrsigner;
  83. } dd_provision_key_t;
  84. /* The derivation data. */
  85. typedef struct {
  86. int size; /* the size of derivation data */
  87. union {
  88. /* key_name is the first field of all the following derivation data */
  89. uint16_t key_name;
  90. uint8_t ddbuf[1];
  91. dd_seal_key_t ddsk;
  92. dd_report_key_t ddrk;
  93. dd_license_key_t ddlk;
  94. dd_provision_key_t ddpk;
  95. };
  96. } derivation_data_t;
  97. /**
  98. * Get the internal CPU keys.
  99. *
  100. * @param key_name - the key name
  101. * @return NULL for invalid key name.
  102. */
  103. const uint8_t* get_base_key(uint16_t key_name);
  104. /** The internal routine to derive requested key. Parameter checking
  105. * is done in the caller.
  106. *
  107. * @param dd - the pointer to derive data
  108. * @param okey - the output derived key
  109. */
  110. void derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey);
  111. /** Compute the CMAC of a buffer.
  112. * @param key - the key used to compute the CMAC
  113. * @param buf - the buf to be digested
  114. * @param buf_len - length of the buffer in Bytes
  115. * @param cmac - the pointer to the output buffer to store CMAC
  116. */
  117. void cmac(const sgx_key_128bit_t *key, const uint8_t* buf, int buf_len, sgx_mac_t* cmac);
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif