main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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
  47. #include <pthread.h>
  48. #endif
  49. /*HMAC test data*/
  50. extern hmacDataStuct hmacVectors[];
  51. /*RND global variables*/
  52. extern CRYS_RND_State_t* rndState_ptr;
  53. extern CRYS_RND_WorkBuff_t* rndWorkBuff_ptr;
  54. int hmac_tests(void)
  55. {
  56. uint32_t ret = 0;
  57. int test_index,operation_index;
  58. uint8_t dataInBuff[MAX_TEST_DATA_SIZE];
  59. uint8_t keyptr[CRYS_HMAC_KEY_SIZE_IN_BYTES];
  60. CRYS_HASH_Result_t hmacOutBuff = {0};
  61. CRYS_HMACUserContext_t ContextID;
  62. /*Run all HMAC tests*/
  63. for(operation_index = INTEGRATED_OPERATION; operation_index <= NON_INTEGRATED_OPERATION; operation_index++){
  64. for (test_index = 0; test_index < HMAC_TESTS_NUMBER; test_index++)
  65. {
  66. SaSi_PalMemSetZero(dataInBuff,MAX_TEST_DATA_SIZE);
  67. SaSi_PalMemSetZero(keyptr,CRYS_HMAC_KEY_SIZE_IN_BYTES);
  68. /*Copy input text data to input buffer*/
  69. SaSi_PalMemCopy(dataInBuff, hmacVectors[test_index].hmacTest_InputData, hmacVectors[test_index].hmacTest_InputDataSize);
  70. SaSi_PalMemCopy(keyptr, hmacVectors[test_index].hmacTest_Key, hmacVectors[test_index].hmacTest_KeySize);
  71. INTEG_TEST_PRINT("\n HASH Test numer 0x%x Parameters : \n-----%s-----\n",test_index,(uint32_t)hmacVectors[test_index].hmacTest_Name);
  72. if (operation_index == INTEGRATED_OPERATION){ /*Perform Inegrated operation*/
  73. INTEG_TEST_PRINT("Integrated operation\n");
  74. ret = CRYS_HMAC(hmacVectors[test_index].hmacTest_TST_OperationMode,
  75. keyptr,
  76. hmacVectors[test_index].hmacTest_KeySize,
  77. dataInBuff,
  78. hmacVectors[test_index].hmacTest_InputDataSize,
  79. hmacOutBuff);
  80. if (ret != SA_SILIB_RET_OK){
  81. INTEG_TEST_PRINT(" CRYS_HMAC failed with 0x%x \n",ret);
  82. goto end;
  83. }
  84. } else { /*Perform NonInegrated operation*/
  85. INTEG_TEST_PRINT("Non integrated operation\n");
  86. ret = CRYS_HMAC_Init(&ContextID,
  87. hmacVectors[test_index].hmacTest_TST_OperationMode,
  88. keyptr,
  89. hmacVectors[test_index].hmacTest_KeySize);
  90. if (ret != SA_SILIB_RET_OK){
  91. INTEG_TEST_PRINT(" CRYS_HMAC_Init failed with 0x%x \n",ret);
  92. goto end;
  93. }
  94. ret = CRYS_HMAC_Update(&ContextID,
  95. dataInBuff,
  96. hmacVectors[test_index].hmacTest_InputDataSize);
  97. if (ret != SA_SILIB_RET_OK){
  98. INTEG_TEST_PRINT(" CRYS_HMAC_Update failed with 0x%x \n",ret);
  99. goto end;
  100. }
  101. ret = CRYS_HMAC_Finish(&ContextID,
  102. hmacOutBuff);
  103. if (ret != SA_SILIB_RET_OK){
  104. INTEG_TEST_PRINT(" CRYS_HMAC_Finish failed with 0x%x \n",ret);
  105. goto end;
  106. }
  107. }
  108. ret = SaSi_PalMemCmp(hmacOutBuff,hmacVectors[test_index].hmacTest_ExpOutData,hmacVectors[test_index].hmacTest_ExpOutDataSize);
  109. if (ret != SA_SILIB_RET_OK){
  110. INTEG_TEST_PRINT(" SaSi_PalMemCmp failed \n");
  111. goto end;
  112. }
  113. INTEG_TEST_PRINT("Passed\n");
  114. INTEG_TEST_PRINT("==========================\n");
  115. }
  116. }
  117. end:
  118. return ret;
  119. }
  120. #ifdef DX_LINUX_PLATFORM /*for linux platform only -> we need to use contiguous memory for stack !!*/
  121. /*hmac_wrap_tests creates thread with defined stack address to and calls to AES HMAC test */
  122. void* hash_thread(void)
  123. {
  124. uint32_t* threadReturnValue = SaSi_PalMemMalloc(sizeof(uint32_t));
  125. *threadReturnValue = hmac_tests();
  126. if (*threadReturnValue != SA_SILIB_RET_OK) {
  127. INTEG_TEST_PRINT("Failure in hmac_tests,ret = 0x%x\n", *threadReturnValue);
  128. goto exit;
  129. }
  130. exit:
  131. pthread_exit(threadReturnValue);
  132. }
  133. int hmac_wrap_tests(void){
  134. uint32_t rc = 0;
  135. pthread_t threadId;
  136. pthread_attr_t threadAttr;
  137. int threadRc;
  138. void *threadRet;
  139. //int num = 6;
  140. threadRc = pthread_attr_init(&threadAttr);
  141. if (threadRc != 0) {
  142. INTEG_TEST_PRINT("pthread_attr_init failed\n");
  143. return -1;
  144. }
  145. threadRc = pthread_attr_setstack(&threadAttr, g_test_stack_base_addr, PTHREAD_STACK_SIZE);
  146. if (threadRc != 0) {
  147. INTEG_TEST_PRINT("pthread_attr_setstack failed\n");
  148. return -1;
  149. }
  150. /* Create independent thread which run with */
  151. threadRc = pthread_create( &threadId, &threadAttr, (void *)hash_thread, NULL);
  152. if (threadRc != 0) {
  153. INTEG_TEST_PRINT( "pthread_create failed\n");
  154. return -1;
  155. }
  156. /* Wait till thread is complete before main continues */
  157. threadRc = pthread_join( threadId, &threadRet);
  158. if (threadRc != 0) {
  159. INTEG_TEST_PRINT( "pthread_join failed\n");
  160. return -1;
  161. }
  162. rc =*((uint32_t *)*&threadRet);
  163. SaSi_PalMemFree(threadRet);
  164. threadRc = pthread_attr_destroy(&threadAttr);
  165. if (threadRc != 0) {
  166. INTEG_TEST_PRINT("pthread_attr_destroy failed\n");
  167. }
  168. return rc;
  169. }
  170. #endif
  171. int main(void)
  172. {
  173. int ret = 0;
  174. /*Perform memory mapping*/
  175. ret = integration_tests_setup();
  176. if (ret != 0)
  177. {
  178. INTEG_TEST_PRINT("integration_tests_setup failed\n");
  179. return ret;
  180. }
  181. /*Init SaSi library*/
  182. ret = SaSi_LibInit();
  183. if (ret != SA_SILIB_RET_OK) {
  184. INTEG_TEST_PRINT("Failed SaSi_LibInit - ret = 0x%x\n", ret);
  185. goto exit_1;
  186. }
  187. ret = CRYS_RndInit(rndState_ptr, rndWorkBuff_ptr);
  188. if (ret != SA_SILIB_RET_OK) {
  189. INTEG_TEST_PRINT("Failed CRYS_RndInit - ret = 0x%x\n", ret);
  190. goto exit_1;
  191. }
  192. /*Call hmac test*/
  193. #ifdef DX_LINUX_PLATFORM
  194. ret = hmac_wrap_tests(); /*Call wrap function to create thread and to define stack's address to use contiguous memory*/
  195. #else
  196. ret = hmac_tests();
  197. #endif
  198. if (ret != SA_SILIB_RET_OK) {
  199. INTEG_TEST_PRINT("Failure in ecc_sign_tests,ret = 0x%x\n", ret);
  200. goto exit_0;
  201. }
  202. INTEG_TEST_PRINT("All tests passed \n");
  203. exit_0:
  204. /*Finish SaSi library*/
  205. SaSi_LibFini();
  206. ret = CRYS_RND_UnInstantiation(rndState_ptr);
  207. if (ret) {
  208. INTEG_TEST_PRINT("Failure in CRYS_RND_UnInstantiation,ret = 0x%x\n", ret);
  209. }
  210. exit_1:
  211. integration_tests_clear();
  212. return ret;
  213. }