sys-get-random.int.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "tss2_sys.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "test.h"
  17. /**
  18. * This program contains integration test for SYS Tss2_Sys_GetRandom.
  19. * First, this test is checking the return code to make sure the
  20. * SYS is executed correctly(return code should return TPM2_RC_SUCCESS).
  21. * Second, the SYS is called twice to make sure the return randomBytes
  22. * are different by comparing the two randomBytes through memcmp.
  23. * It might not be the best test for random bytes generator but
  24. * at least this test shows the return randomBytes are different.
  25. */
  26. int
  27. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  28. {
  29. TSS2_RC rc;
  30. TPM2B_DIGEST randomBytes1 = {sizeof (TPM2B_DIGEST) - 2,};
  31. TPM2B_DIGEST randomBytes2 = {sizeof (TPM2B_DIGEST) - 2,};
  32. int bytes = 20;
  33. LOG_INFO("GetRandom tests started.");
  34. rc = Tss2_Sys_GetRandom(sys_context, 0, bytes, &randomBytes1, 0);
  35. if (rc != TSS2_RC_SUCCESS) {
  36. LOG_ERROR("GetRandom FAILED! Response Code : %x", rc);
  37. exit(1);
  38. }
  39. rc = Tss2_Sys_GetRandom(sys_context, 0, bytes, &randomBytes2, 0);
  40. if (rc != TSS2_RC_SUCCESS) {
  41. LOG_ERROR("GetRandom FAILED! Response Code : %x", rc);
  42. exit(1);
  43. }
  44. if(memcmp(&randomBytes1, &randomBytes2, bytes) == 0) {
  45. LOG_ERROR("Comparison FAILED! randomBytes 0x%p & 0x%p are the same.", &randomBytes1, &randomBytes2);
  46. exit(1);
  47. }
  48. LOG_INFO("GetRandom Test Passed!");
  49. return 0;
  50. }