utility.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "utility.h"
  32. bool verify_hmac_sha256(
  33. const uint8_t* mac_key,
  34. uint32_t key_len,
  35. const uint8_t* data_buf,
  36. uint32_t buf_size,
  37. const uint8_t* mac_buf)
  38. {
  39. uint8_t data_mac[SGX_SHA256_HASH_SIZE];
  40. if(!mac_key || !data_buf || !mac_buf)
  41. {
  42. return false;
  43. }
  44. // compute HMAC-SHA256 of the message with the specified Key
  45. if (ippsHMAC_Message(data_buf, buf_size, mac_key, key_len, data_mac, SGX_SHA256_HASH_SIZE, IPP_ALG_HASH_SHA256) != ippStsNoErr)
  46. {
  47. return false;
  48. }
  49. // compare HMAC values
  50. if(consttime_memequal(mac_buf, data_mac, SGX_SHA256_HASH_SIZE) == 0)
  51. {
  52. return false;
  53. }
  54. return true;
  55. }
  56. ae_error_t error_reinterpret(pse_op_error_t op_error)
  57. {
  58. switch(op_error)
  59. {
  60. case OP_SUCCESS:
  61. return AE_SUCCESS;
  62. case OP_ERROR_MAX_NUM_SESSION_REACHED:
  63. return PSE_OP_MAX_NUM_SESSION_REACHED;
  64. case OP_ERROR_INVALID_SESSION:
  65. return PSE_OP_SESSION_INVALID;
  66. case OP_ERROR_INVALID_EPH_SESSION:
  67. /* Ephemeral session is invalid */
  68. return PSE_OP_EPHEMERAL_SESSION_INVALID;
  69. case OP_ERROR_PSDA_SESSION_LOST:
  70. return AESM_PSDA_SESSION_LOST;
  71. case OP_ERROR_EPH_SESSION_ESTABLISHMENT_INTEGRITY_ERROR:
  72. /* Wrong message detected when establishing ephemeral session */
  73. return PSE_OP_ERROR_EPH_SESSION_ESTABLISHMENT_INTEGRITY_ERROR;
  74. case OP_ERROR_UNSEAL_PAIRING_BLOB:
  75. return PSE_PAIRING_BLOB_UNSEALING_ERROR;
  76. case OP_ERROR_INVALID_PAIRING_BLOB:
  77. return PSE_PAIRING_BLOB_INVALID_ERROR;
  78. case OP_ERROR_PSDA_BUSY:
  79. return PSE_OP_PSDA_BUSY_ERROR;
  80. case OP_ERROR_LTPB_SEALING_OUT_OF_DATE:
  81. return PSE_OP_LTPB_SEALING_OUT_OF_DATE;
  82. case OP_ERROR_KDF_MISMATCH:
  83. return PSE_OP_ERROR_KDF_MISMATCH;
  84. default:
  85. return PSE_OP_INTERNAL_ERROR;
  86. }
  87. }