test_session_common.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tpm2_alg_util.h"
  2. #include "tpm2_util.h"
  3. #define SESSION_HANDLE 0xBADC0DE
  4. typedef struct expected_data expected_data;
  5. struct expected_data {
  6. struct {
  7. ESYS_TR key;
  8. ESYS_TR bind;
  9. TPM2_SE session_type;
  10. TPMT_SYM_DEF symmetric;
  11. TPMI_ALG_HASH auth_hash;
  12. TPM2B_NONCE nonce_caller;
  13. } input;
  14. struct output {
  15. ESYS_TR handle;
  16. TPM2_RC rc;
  17. } output;
  18. };
  19. static inline void set_expected(ESYS_TR key, ESYS_TR bind,
  20. TPM2_SE session_type,
  21. TPMT_SYM_DEF *symmetric, TPMI_ALG_HASH auth_hash,
  22. TPM2B_NONCE *nonce_caller, ESYS_TR handle, TPM2_RC rc) {
  23. expected_data *e = calloc(1, sizeof(*e));
  24. assert_non_null(e);
  25. e->input.key = key;
  26. e->input.bind = bind;
  27. e->input.session_type = session_type;
  28. e->input.symmetric = *symmetric;
  29. e->input.auth_hash = auth_hash;
  30. if (nonce_caller) {
  31. e->input.nonce_caller = *nonce_caller;
  32. }
  33. e->output.handle = handle;
  34. e->output.rc = rc;
  35. will_return(__wrap_Esys_StartAuthSession, e);
  36. }
  37. static inline void set_expected_defaults(TPM2_SE session_type,
  38. ESYS_TR handle, TPM2_RC rc) {
  39. TPMT_SYM_DEF symmetric;
  40. memset(&symmetric, 0, sizeof(symmetric));
  41. symmetric.algorithm = TPM2_ALG_NULL;
  42. TPM2B_NONCE nonce_caller;
  43. memset(&nonce_caller, 0, sizeof(nonce_caller));
  44. nonce_caller.size = tpm2_alg_util_get_hash_size(TPM2_ALG_SHA1);
  45. set_expected(
  46. ESYS_TR_NONE,
  47. ESYS_TR_NONE, session_type, &symmetric,
  48. TPM2_ALG_SHA256, &nonce_caller, handle, rc);
  49. }
  50. TSS2_RC __wrap_Esys_StartAuthSession(ESYS_CONTEXT *esysContext,
  51. ESYS_TR tpmKey, ESYS_TR bind,
  52. ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3,
  53. const TPM2B_NONCE *nonceCaller, TPM2_SE sessionType,
  54. const TPMT_SYM_DEF *symmetric, TPMI_ALG_HASH authHash,
  55. ESYS_TR *sessionHandle) {
  56. UNUSED(esysContext);
  57. UNUSED(shandle1);
  58. UNUSED(shandle2);
  59. UNUSED(shandle3);
  60. UNUSED(sessionHandle);
  61. expected_data *e = mock_ptr_type(expected_data *);
  62. assert_int_equal(tpmKey, e->input.key);
  63. assert_int_equal(bind, e->input.bind);
  64. if (nonceCaller) {
  65. assert_int_equal(e->input.nonce_caller.size, nonceCaller->size);
  66. assert_memory_equal(e->input.nonce_caller.buffer, nonceCaller->buffer, nonceCaller->size);
  67. }
  68. assert_int_equal(sessionType, e->input.session_type);
  69. assert_memory_equal(symmetric, &e->input.symmetric,
  70. sizeof(*symmetric));
  71. assert_int_equal(authHash, e->input.auth_hash);
  72. *sessionHandle = e->output.handle;
  73. TSS2_RC rc = e->output.rc;
  74. free(e);
  75. return rc;
  76. }