main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "ssi_pal_types.h"
  41. #include "ssi_pal_mem.h"
  42. #include "sns_silib.h"
  43. #include "integration_test_plat_defs.h"
  44. #include "integration_test_ssi_data.h"
  45. #include "integration_test_ssi_defs.h"
  46. #ifdef DX_LINUX_PLATFORM /*for linux platform only !!*/
  47. #include <pthread.h>
  48. #endif
  49. #include "crys_hkdf.h"
  50. /*HKDF test data vectors*/
  51. extern hkdfDataStuct hkdfVectors[];
  52. /*RNG Global variables*/
  53. extern CRYS_RND_State_t* rndState_ptr;
  54. extern CRYS_RND_WorkBuff_t* rndWorkBuff_ptr;
  55. int hkdf_tests(void)
  56. {
  57. uint32_t Error = 0;
  58. int test_index;
  59. int32_t tot_tests=0, tot_errs=0;
  60. /*Run all HKDF tests*/
  61. for (test_index = 0; test_index < HKDF_TESTS_NUMBER; test_index++)
  62. {
  63. hkdfDataStuct hkdfTestVector;
  64. CRYS_HKDF_HASH_OpMode_t hkdfHashMode;
  65. uint8_t OKMDataBuff[TST_HKDF_MAX_OKM_SIZE];
  66. SaSi_PalMemCopy(&hkdfTestVector, &hkdfVectors[test_index], sizeof(hkdfTestVector));
  67. hkdfHashMode = hkdfTestVector.hkdfHashMode;
  68. Error = CRYS_HKDF_KeyDerivFunc(
  69. hkdfHashMode,
  70. hkdfTestVector.hkdf_Salt,
  71. (size_t)hkdfTestVector.hkdf_SaltSize,
  72. hkdfTestVector.hkdf_IKM,
  73. hkdfTestVector.hkdf_IKMSize,
  74. hkdfTestVector.hkdf_Info,
  75. hkdfTestVector.hkdf_InfoSize,
  76. OKMDataBuff,
  77. hkdfTestVector.hkdf_LSize,
  78. SASI_FALSE
  79. );
  80. if (Error != CRYS_OK) {
  81. INTEG_TEST_PRINT("Test %d: %s: CRYS_HKDF_KeyDerivFunc() returned error 0x%08x - FAILED\n",
  82. (int)test_index, (uint32_t)hkdfTestVector.hkdf_Name, Error);
  83. goto end_loop;
  84. }
  85. /* compare the result and print result*/
  86. Error = SaSi_PalMemCmp(&OKMDataBuff[0], &hkdfTestVector.hkdf_ExpectOKM[0], hkdfTestVector.hkdf_LSize);
  87. if( Error != 0 ) {
  88. INTEG_TEST_PRINT("Test %d: %s: OKM content is not as expected - FAILED\n",
  89. (int)test_index, (uint32_t)hkdfTestVector.hkdf_Name);
  90. #ifdef PRINT_HKDF_RES
  91. /* print the actual and expected result */
  92. INTEG_TEST_PRINT(" Data : actual : expected : match :\n");
  93. for(j = 0 ; (j < hkdfTestVector.hkdf_LSize); j++)
  94. {
  95. INTEG_TEST_PRINT(" %02d : %02X : %02X : %d :\n",
  96. (int)j, (unsigned int)OKMDataBuff[j], (unsigned int)hkdfTestVector.hkdf_ExpectOKM[j],
  97. (int)(OKMDataBuff[j]==hkdfTestVector.hkdf_ExpectOKM[j]));
  98. }
  99. INTEG_TEST_PRINT("\n");
  100. #endif
  101. goto end_loop;
  102. } else {
  103. INTEG_TEST_PRINT("Test %d: %s: - PASSED\n",
  104. (int)test_index, (uint32_t)hkdfTestVector.hkdf_Name);
  105. }
  106. end_loop:
  107. tot_errs += Error?1:0; ++tot_tests;
  108. }
  109. if( tot_errs != 0 ) {
  110. INTEG_TEST_PRINT(" HKDF TotalTests= %d tot_errs= %d FAILED\n", (int)test_index, (int)tot_errs);
  111. goto end;
  112. } else {
  113. INTEG_TEST_PRINT(" HKDF TotalTests= %d PASSED\n", (int)test_index);
  114. }
  115. if (Error != SA_SILIB_RET_OK){
  116. INTEG_TEST_PRINT(" SaSi_PalMemCmp failed \n");
  117. goto end;
  118. }
  119. INTEG_TEST_PRINT("\nPassed \n ==========================\n");
  120. end:
  121. return Error;
  122. }
  123. #ifdef DX_LINUX_PLATFORM /*for linux platform only -> we need to use contiguous memory for stack !!*/
  124. /*hkdf_wrap_tests creates thread with defined stack address to and calls to hkdf test */
  125. void* hkdf_thread(void)
  126. {
  127. uint32_t* threadReturnValue = SaSi_PalMemMalloc(sizeof(uint32_t));
  128. *threadReturnValue =hkdf_tests();
  129. if (*threadReturnValue != SA_SILIB_RET_OK) {
  130. INTEG_TEST_PRINT("Failure in hkdf_tests,ret = 0x%x\n", *threadReturnValue);
  131. goto exit;
  132. }
  133. exit:
  134. pthread_exit(threadReturnValue);
  135. }
  136. int hkdf_wrap_tests(void){
  137. uint32_t rc = 0;
  138. pthread_t threadId;
  139. pthread_attr_t threadAttr;
  140. int threadRc;
  141. void *threadRet;
  142. int num = 6;
  143. threadRc = pthread_attr_init(&threadAttr);
  144. if (threadRc != 0) {
  145. INTEG_TEST_PRINT("pthread_attr_init failed\n");
  146. return -1;
  147. }
  148. threadRc = pthread_attr_setstack(&threadAttr, g_test_stack_base_addr, PTHREAD_STACK_SIZE);
  149. if (threadRc != 0) {
  150. INTEG_TEST_PRINT("pthread_attr_setstack failed\n");
  151. return -1;
  152. }
  153. /* Create independent thread which run with */
  154. threadRc = pthread_create( &threadId, &threadAttr, (void *)hkdf_thread, &num);
  155. if (threadRc != 0) {
  156. INTEG_TEST_PRINT( "pthread_create failed\n");
  157. return -1;
  158. }
  159. /* Wait till thread is complete before main continues */
  160. threadRc = pthread_join( threadId, &threadRet);
  161. if (threadRc != 0) {
  162. INTEG_TEST_PRINT( "pthread_join failed\n");
  163. return -1;
  164. }
  165. rc =*((uint32_t *)*&threadRet);
  166. SaSi_PalMemFree(threadRet);
  167. threadRc = pthread_attr_destroy(&threadAttr);
  168. if (threadRc != 0) {
  169. INTEG_TEST_PRINT("pthread_attr_destroy failed\n");
  170. }
  171. return rc;
  172. }
  173. #endif
  174. int main(void)
  175. {
  176. int ret = 0;
  177. /*Perform memory mapping*/
  178. ret = integration_tests_setup();
  179. if (ret != 0)
  180. {
  181. INTEG_TEST_PRINT("integration_tests_setup failed\n");
  182. return ret;
  183. }
  184. /*Init SaSi library*/
  185. ret = SaSi_LibInit();
  186. if(ret) {
  187. INTEG_TEST_PRINT("Failed SaSi_LibInit - ret = 0x%x\n", ret);
  188. }
  189. ret = CRYS_RndInit(rndState_ptr, rndWorkBuff_ptr);
  190. if (ret) {
  191. INTEG_TEST_PRINT("Failed CRYS_RndInit - ret = 0x%x\n", ret);
  192. goto exit_1;
  193. }
  194. /*Call hkdf test*/
  195. #ifdef DX_LINUX_PLATFORM
  196. ret = hkdf_wrap_tests(); /*Call wrap function to create thread and to define stack's address to use contiguous memory*/
  197. #else
  198. ret = hkdf_tests();
  199. #endif
  200. if (ret != SA_SILIB_RET_OK) {
  201. INTEG_TEST_PRINT("Failure in hkdf_tests,ret = 0x%x\n", ret);
  202. goto exit_0;
  203. }
  204. INTEG_TEST_PRINT("All tests passed \n");
  205. exit_0:
  206. /*Finish SaSi library*/
  207. SaSi_LibFini();
  208. ret = CRYS_RND_UnInstantiation(rndState_ptr);
  209. if (ret)
  210. {
  211. INTEG_TEST_PRINT("Failure in CRYS_RND_UnInstantiation,ret = 0x%x\n", ret);
  212. }
  213. exit_1:
  214. integration_tests_clear();
  215. return ret;
  216. }