deriv.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2011-2016 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 license key in simulation mode
  44. static const uint8_t BASE_LICENSE_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: return BASE_SEAL_KEY;
  62. case SGX_KEYSELECT_REPORT: return BASE_REPORT_KEY;
  63. case SGX_KEYSELECT_LICENSE: return BASE_LICENSE_KEY;
  64. case SGX_KEYSELECT_PROVISION: return BASE_PROVISION_KEY;
  65. case SGX_KEYSELECT_PROVISION_SEAL: return BASE_PROV_SEAL_KEY;
  66. }
  67. // Should not come here - error should have been reported
  68. // when the key name is not supported in the caller.
  69. return (uint8_t*)0;
  70. }
  71. // Compute the CMAC of derivation data with corresponding base key
  72. // and save it to `okey'.
  73. void derive_key(const derivation_data_t* dd, sgx_key_128bit_t okey)
  74. {
  75. sgx_rijndael128_cmac_msg((const sgx_cmac_128bit_key_t*)(get_base_key(dd->key_name)),
  76. dd->ddbuf, dd->size, (sgx_cmac_128bit_tag_t*)okey);
  77. }
  78. // Compute the CMAC of a `buf' with a given `key'.
  79. void cmac(const sgx_key_128bit_t *key, const uint8_t* buf, int buf_len, sgx_mac_t* cmac)
  80. {
  81. sgx_rijndael128_cmac_msg((const sgx_cmac_128bit_key_t*)key, buf, buf_len, cmac);
  82. }