deriv.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "deriv.h"
  32. #include "sgx_tcrypto.h"
  33. // The built-in seal key in simulation mode
  34. static const uint8_t BASE_SEAL_KEY[] = {
  35. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  36. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  37. };
  38. // The built-in report key in simulation mode
  39. static const uint8_t BASE_REPORT_KEY[] = {
  40. 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
  41. 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
  42. };
  43. // The built-in EINIT token key in simulation mode
  44. static const uint8_t BASE_EINITTOKEN_KEY[] = {
  45. 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
  46. 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
  47. };
  48. // The built-in provision key in simulation mode
  49. static const uint8_t BASE_PROVISION_KEY[] = {
  50. 0xbb, 0xaa, 0xbb, 0xee, 0xff, 0x00, 0x00, 0xdd,
  51. 0xbb, 0xaa, 0xbb, 0xee, 0xff, 0x00, 0x00, 0xdd,
  52. };
  53. // The built-in provision-seal key in simulation mode
  54. static const uint8_t BASE_PROV_SEAL_KEY[] = {
  55. 0x50, 0x52, 0x4f, 0x56, 0x49, 0x53, 0x49, 0x4f,
  56. 0x4e, 0x53, 0x45, 0x41, 0x4c, 0x4b, 0x45, 0x59,
  57. };
  58. const uint8_t* get_base_key(uint16_t key_name)
  59. {
  60. switch (key_name) {
  61. case SGX_KEYSELECT_SEAL:
  62. return BASE_SEAL_KEY;
  63. case SGX_KEYSELECT_REPORT:
  64. return BASE_REPORT_KEY;
  65. case SGX_KEYSELECT_EINITTOKEN:
  66. return BASE_EINITTOKEN_KEY;
  67. case SGX_KEYSELECT_PROVISION:
  68. return BASE_PROVISION_KEY;
  69. case SGX_KEYSELECT_PROVISION_SEAL:
  70. return BASE_PROV_SEAL_KEY;
  71. }
  72. // Should not come here - error should have been reported
  73. // when the key name is not supported in the caller.
  74. return (uint8_t*)0;
  75. }
  76. // Compute the CMAC of derivation data with corresponding base key
  77. // and save it to `okey'.
  78. void derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey)
  79. {
  80. sgx_rijndael128_cmac_msg((const sgx_cmac_128bit_key_t*)(get_base_key(dd->key_name)),
  81. dd->ddbuf, dd->size, (sgx_cmac_128bit_tag_t*)okey);
  82. }
  83. // Compute the CMAC of a `buf' with a given `key'.
  84. void cmac(const sgx_key_128bit_t *key, const uint8_t* buf, int buf_len, sgx_mac_t* cmac)
  85. {
  86. sgx_rijndael128_cmac_msg((const sgx_cmac_128bit_key_t*)key, buf, buf_len, cmac);
  87. }