prov_msg_size.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef _PROV_MSG_SIZE_H_
  32. #define _PROV_MSG_SIZE_H_
  33. #include "type_length_value.h"
  34. /*Inline functions to estimate size of ProvMsg1, ProvMsg3 etc*/
  35. /*Function to estimate the size of ProvMsg1
  36. TLV_CIPHER_TEXT(SK, PSID): E+MAC(CIPHER_TLV:PLATFORM_INFO_TLV[:FLAG_TLV])*/
  37. inline uint32_t estimate_msg1_size(bool performance_rekey)
  38. {
  39. size_t field0_size = CIPHER_TEXT_TLV_SIZE(RSA_3072_KEY_BYTES);
  40. size_t field1_0_size = CIPHER_TEXT_TLV_SIZE(RSA_3072_KEY_BYTES);
  41. size_t field1_1_size = PLATFORM_INFO_TLV_SIZE();
  42. size_t field1_2_size = performance_rekey? FLAGS_TLV_SIZE():0;
  43. size_t field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(field1_0_size+field1_1_size+field1_2_size);
  44. size_t field2_size = MAC_TLV_SIZE(MAC_SIZE);
  45. return static_cast<uint32_t>(PROVISION_REQUEST_HEADER_SIZE+field0_size+field1_size+field2_size); /*no checking for integer overflow since the size of msg1 is fixed and small*/
  46. }
  47. /*Function to estimate the size of ProvMsg3
  48. NONCE_TLV(NONCE_SIZE):E+MAC(E+MAC(EPID_JOIN_PROOF_TLV):NONCE_TLV(NONCE_2):CIPHER_TLV:SE_REPRT_TLV):E+MAC(EPID_SIGNATURE_TLV)*/
  49. inline uint32_t calc_msg3_size_by_sigrl_count(uint32_t sigrl_count)
  50. {
  51. size_t field0_size = NONCE_TLV_SIZE(NONCE_SIZE);
  52. size_t field1_0_size = BLOCK_CIPHER_TEXT_TLV_SIZE(EPID_JOIN_PROOF_TLV_SIZE());
  53. size_t field1_1_size = MAC_TLV_SIZE(MAC_SIZE);
  54. size_t field1_2_size = NONCE_TLV_SIZE(NONCE_2_SIZE);
  55. size_t field1_3_size = CIPHER_TEXT_TLV_SIZE(RSA_3072_KEY_BYTES);
  56. size_t field1_4_size = SE_REPORT_TLV_SIZE();
  57. size_t field3_0_size = EPID_SIGNATURE_TLV_SIZE(sigrl_count);
  58. size_t field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(field1_0_size+field1_1_size+field1_2_size+field1_3_size+field1_4_size);
  59. size_t field2_size = MAC_TLV_SIZE(MAC_SIZE);
  60. size_t field3_size = BLOCK_CIPHER_TEXT_TLV_SIZE(field3_0_size);
  61. size_t field4_size = MAC_TLV_SIZE(MAC_SIZE);
  62. return static_cast<uint32_t>(PROVISION_REQUEST_HEADER_SIZE+field0_size+field1_size+field2_size+field3_size+field4_size);
  63. }
  64. /*Function to estimate the count of SigRl Entry inside a ProvMsg2
  65. Nonce_TLV(NONCE_SIZE):E+MAC(PubGroupCert:ChallengeNonce[:PlatformInfoPSVN]:PSID:EPID_GID:PlatformInfo)[:signed SigRl]*/
  66. inline uint32_t estimate_sigrl_count_by_msg2_size(uint32_t msg2_size)
  67. {
  68. size_t field_0_size = NONCE_TLV_SIZE(NONCE_SIZE);
  69. size_t field_1_0_size = EPID_GROUP_CERT_TLV_SIZE();
  70. size_t field_1_1_size = NONCE_TLV_SIZE(CHALLENGE_NONCE_SIZE);
  71. size_t field_1_2_size = PLATFORM_INFO_TLV_SIZE(); //It is always present if sigrl entry count is nonzero
  72. size_t field_1_3_size = PSID_TLV_SIZE();
  73. size_t field_1_4_size = EPID_GID_TLV_SIZE();
  74. size_t field_1_5_size = PLATFORM_INFO_TLV_SIZE();
  75. size_t field_1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(field_1_0_size+field_1_1_size+field_1_2_size
  76. + field_1_3_size + field_1_4_size + field_1_5_size );
  77. size_t field_2_size = MAC_TLV_SIZE(MAC_SIZE);
  78. size_t field_3_size = 0;
  79. if(PROVISION_RESPONSE_HEADER_SIZE+field_0_size+field_1_size+field_2_size>=msg2_size)
  80. return 0;
  81. field_3_size = msg2_size - (PROVISION_RESPONSE_HEADER_SIZE+field_0_size+field_1_size+field_2_size);
  82. if(field_3_size < ECDSA_SIGN_SIZE*2 +sizeof(SigRl))
  83. return 0;
  84. field_3_size -= ECDSA_SIGN_SIZE*2 + sizeof(SigRl);
  85. /*The first SigRlEntry has been included into SigRl structure so that an extra 1 is added*/
  86. return static_cast<uint32_t>(1+field_3_size/sizeof(SigRlEntry));
  87. }
  88. inline uint32_t estimate_msg3_size_by_msg2_size(uint32_t msg2_size)
  89. {
  90. return calc_msg3_size_by_sigrl_count(estimate_sigrl_count_by_msg2_size(msg2_size));
  91. }
  92. inline uint32_t estimate_es_msg1_size(void)
  93. {
  94. return static_cast<uint32_t>(PROVISION_REQUEST_HEADER_SIZE+ES_SELECTOR_TLV_SIZE());
  95. }
  96. #endif