test_hmac_prng.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* test_hmac_prng.c - TinyCrypt implementation of some HMAC-PRNG tests */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. DESCRIPTION
  33. This module tests the following PRNG routines:
  34. Scenarios tested include:
  35. - HMAC-PRNG init
  36. - HMAC-PRNG reseed
  37. - HMAC-PRNG generate)
  38. */
  39. #include <tinycrypt/hmac_prng.h>
  40. #include <tinycrypt/constants.h>
  41. #include <test_utils.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #define TC_DEBUG_MODE 0
  46. #ifdef TC_DEBUG_MODE
  47. void show(const char *label, const uint8_t *s, size_t len)
  48. {
  49. unsigned int i;
  50. printf ("%s = ", label);
  51. for (i = 0; i < (unsigned int) len; ++i) {
  52. printf ("%02x", s[i]);
  53. }
  54. printf ("\n");
  55. }
  56. void printBinaryFile(const uint8_t *s, unsigned int slen)
  57. {
  58. FILE *write_ptr;
  59. write_ptr = fopen("pseudo-random-data.bin","wb");
  60. fwrite(s, slen, 1, write_ptr);
  61. }
  62. #endif
  63. /*
  64. * Main task to test AES
  65. */
  66. int main(void)
  67. {
  68. uint8_t seed[128];
  69. struct tc_hmac_prng_struct h;
  70. unsigned int size = (1 << 19);
  71. uint8_t random[size];
  72. unsigned int i;
  73. unsigned int result = TC_PASS;
  74. TC_START("Performing HMAC-PRNG tests:");
  75. TC_PRINT("HMAC-PRNG test#1 (init, reseed, generate):\n");
  76. /* Fake seed (replace by a a truly random seed): */
  77. for (i = 0; i < (unsigned int) sizeof(seed); ++i) {
  78. seed[i] = i;
  79. }
  80. /* Fake personalization and additional_input (replace by appropriate
  81. * values): *
  82. * e.g.: hostname+timestamp */
  83. uint8_t *personalization = (uint8_t *) "HOSTNAME";
  84. uint8_t *additional_input = (uint8_t *) "additional input";
  85. TC_PRINT("HMAC-PRNG test#1 (init):\n");
  86. if (tc_hmac_prng_init(&h, personalization,
  87. sizeof(personalization)) == 0) {
  88. TC_ERROR("HMAC-PRNG initialization failed.\n");
  89. result = TC_FAIL;
  90. goto exitTest;
  91. }
  92. TC_END_RESULT(result);
  93. TC_PRINT("HMAC-PRNG test#1 (reseed):\n");
  94. if (tc_hmac_prng_reseed(&h, seed, sizeof(seed), additional_input,
  95. sizeof(additional_input)) == 0) {
  96. TC_ERROR("HMAC-PRNG reseed failed.\n");
  97. result = TC_FAIL;
  98. goto exitTest;
  99. }
  100. TC_END_RESULT(result);
  101. TC_PRINT("HMAC-PRNG test#1 (generate):\n");
  102. if (tc_hmac_prng_generate(random, size, &h) < 1) {
  103. TC_ERROR("HMAC-PRNG generate failed.\n");
  104. result = TC_FAIL;
  105. goto exitTest;
  106. }
  107. TC_END_RESULT(result);
  108. #ifdef TC_DEBUG_MODE
  109. printBinaryFile(random, size);
  110. show ("Pseudo-random data", random, size);
  111. #endif
  112. TC_PRINT("All HMAC tests succeeded!\n");
  113. exitTest:
  114. TC_END_RESULT(result);
  115. TC_END_REPORT(result);
  116. }