main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #ifdef DX_LINUX_PLATFORM /*for linux platform only !!*/
  41. #include <pthread.h>
  42. #endif
  43. #include "ssi_pal_types.h"
  44. #include "ssi_pal_mem.h"
  45. #include "sns_silib.h"
  46. #include "ssi_aes.h"
  47. #include "crys_aesccm.h"
  48. #include "integration_test_plat_defs.h"
  49. #include "integration_test_ssi_data.h"
  50. #include "integration_test_ssi_defs.h"
  51. /*AES test data vectors*/
  52. extern aesDataStuct aesVectors[];
  53. extern aesCCMDataStuct aesCCMVectors[];
  54. /*Globals*/
  55. extern CRYS_RND_State_t* rndState_ptr;
  56. extern CRYS_RND_WorkBuff_t* rndWorkBuff_ptr;
  57. int aes_ccm_tests(void)
  58. {
  59. uint32_t ret = 1;
  60. int test_index;
  61. uint8_t dataInBuff[MAX_AES_CCM_DATA_SIZE_VECTOR];
  62. uint8_t dataOutBuff[MAX_AES_CCM_DATA_SIZE_VECTOR];
  63. uint8_t aDataBuff[MAX_AES_CCM_DATA_SIZE_VECTOR];
  64. uint8_t macOutRes[MAX_AES_CCM_DATA_SIZE_VECTOR];
  65. uint8_t nonce[MAX_AES_CCM_DATA_SIZE_VECTOR];
  66. for (test_index = 0;test_index < AES_CCM_TESTS_NUMBER ;test_index++)
  67. {
  68. SaSi_PalMemSetZero(dataInBuff,MAX_AES_CCM_DATA_SIZE_VECTOR);
  69. SaSi_PalMemSetZero(dataOutBuff,MAX_AES_CCM_DATA_SIZE_VECTOR);
  70. SaSi_PalMemSetZero(aDataBuff,MAX_AES_CCM_DATA_SIZE_VECTOR);
  71. SaSi_PalMemSetZero(macOutRes,MAX_AES_CCM_DATA_SIZE_VECTOR);
  72. SaSi_PalMemSetZero(nonce,MAX_AES_CCM_DATA_SIZE_VECTOR);
  73. SaSi_PalMemCopy(dataInBuff, aesCCMVectors[test_index].aesCCMTest_TextData, aesCCMVectors[test_index].aesCCMTest_TextdataSize);
  74. /*Copy input additional data to input buffer*/
  75. SaSi_PalMemCopy(aDataBuff, aesCCMVectors[test_index].aesCCMTest_AddData, aesCCMVectors[test_index].aesCCMTest_AddDataSize);
  76. /*Copy nonce data to nonce buffer*/
  77. SaSi_PalMemCopy(nonce, aesCCMVectors[test_index].aesCCMTest_NData, aesCCMVectors[test_index].aesCCMTest_NDataSize /*7*/);
  78. INTEG_TEST_PRINT(" Test Parameters : %s \n",(uint32_t)aesCCMVectors[test_index].aesCCMTest_name);
  79. /*Call CRYS_AESCCM integrated function*/
  80. ret = CRYS_AESCCM(SASI_AES_ENCRYPT,
  81. aesCCMVectors[test_index].aesCCMTest_Key,
  82. aesCCMVectors[test_index].aesCCMTest_KeySize,
  83. nonce,//aesCCMVectors[test_index].aesCCMTest_NData,
  84. aesCCMVectors[test_index].aesCCMTest_NDataSize,
  85. aDataBuff,//aesCCMVectors[test_index].aesCCMTest_AddData,,
  86. aesCCMVectors[test_index].aesCCMTest_AddDataSize,
  87. dataInBuff,//aesCCMVectors[test_index].aesCCMTest_TextData,
  88. aesCCMVectors[test_index].aesCCMTest_TextdataSize,
  89. dataOutBuff,//aesCCMVectors[test_index] uint8_t *TextDataOut_ptr,
  90. aesCCMVectors[test_index].aesCCMTest_Output_CCM_MACSize,
  91. macOutRes /*aesCCMVectors[test_index] CRYS_AESCCM_Mac_Res_t MacRes*/);
  92. if (ret != SA_SILIB_RET_OK){
  93. INTEG_TEST_PRINT(" CRYS_AESCCM failed with 0x%x \n",ret);
  94. goto aesTester_end;
  95. }
  96. /*Check output text data with expected data*/
  97. ret = SaSi_PalMemCmp(dataOutBuff,aesCCMVectors[test_index].aesCCMTest_Expected_output_data,aesCCMVectors[test_index].aesCCMTest_TextdataSize);
  98. if (ret != 0){
  99. INTEG_TEST_PRINT("output text buffer of the test failed");
  100. goto aesTester_end;
  101. }
  102. /*Check mac output with expected data*/
  103. ret = SaSi_PalMemCmp(macOutRes,aesCCMVectors[test_index].aesCCMTest_Expected_MAC_output_data,aesCCMVectors[test_index].aesCCMTest_Output_CCM_MACSize);
  104. if (ret != 0){
  105. INTEG_TEST_PRINT("output mac buffer of the test failed");
  106. goto aesTester_end;
  107. }
  108. }
  109. INTEG_TEST_PRINT("Passed \n");
  110. aesTester_end:
  111. return ret;
  112. }
  113. static bool should_set_iv(SaSiAesOperationMode_t operationMode)
  114. {
  115. return (operationMode == SASI_AES_MODE_CBC) ||
  116. (operationMode == SASI_AES_MODE_CTR) ||
  117. (operationMode == SASI_AES_MODE_CBC_MAC);
  118. }
  119. int aes_tests(void){
  120. int ret = 0;
  121. uint32_t test_index,block_index;
  122. BufferType_t buffer_index;
  123. uint8_t dataInBuff[MAX_TEST_DATA_SIZE];
  124. uint8_t dataOutBuff[MAX_TEST_DATA_SIZE];
  125. uint8_t *dataOutBuff_ptr = dataOutBuff;
  126. SaSiAesUserContext_t ContextID;
  127. SaSiAesUserKeyData_t keyData;
  128. size_t outSize = 0;
  129. /*Loop for aes test vectors*/
  130. for (buffer_index = NON_INPLACE_BUFFER ; buffer_index <= INPLACE_BUFFER; buffer_index++)
  131. for (test_index = 0; test_index < AES_TESTS_NUMBER; test_index++)
  132. {
  133. SaSi_PalMemSetZero(dataInBuff,MAX_TEST_DATA_SIZE);
  134. SaSi_PalMemSetZero(dataOutBuff,MAX_TEST_DATA_SIZE);
  135. SaSi_PalMemCopy(dataInBuff, aesVectors[test_index].aesTest_input_data, aesVectors[test_index].aesTest_DataSize);
  136. INTEG_TEST_PRINT(" Test Parameters : %s \n",(uint32_t)aesVectors[test_index].aesTest_name);
  137. /*If current iteration is for inplace buffer type, set the output data pointer to input data pointer*
  138. * to check inplace operation */
  139. if (buffer_index == INPLACE_BUFFER ){
  140. /*Perform inplace operation*/
  141. dataOutBuff_ptr = dataInBuff;
  142. INTEG_TEST_PRINT(" Inplace buffer operaion \n");
  143. } else {
  144. INTEG_TEST_PRINT(" Non-Inplace buffer operaion \n");
  145. }
  146. /*Call non-integrated APIs - first SaSi_AesInit*/
  147. ret = SaSi_AesInit(&ContextID,
  148. aesVectors[test_index].aesTest_EncDecMode,
  149. aesVectors[test_index].aesTest_OperationMode,SASI_AES_PADDING_NONE);
  150. /*,
  151. aesVectors[test_index].aesTest_Key, aesVectors[test_index].aesTest_KeySize,*/
  152. if (ret != SA_SILIB_RET_OK){
  153. INTEG_TEST_PRINT("CRYS_AES_Init failed with error 0x%x\n",ret);
  154. return ret;
  155. }
  156. if ( should_set_iv(aesVectors[test_index].aesTest_OperationMode)){
  157. ret = SaSi_AesSetIv(&ContextID, aesVectors[test_index].aesTest_IvCounter);
  158. if (ret != SA_SILIB_RET_OK){
  159. INTEG_TEST_PRINT("SaSi_AesSetIv failed with error 0x%x\n",ret);
  160. return ret;
  161. }
  162. }
  163. keyData.pKey = aesVectors[test_index].TST_KeyData;
  164. keyData.keySize = aesVectors[test_index].TST_KeySize;
  165. ret = SaSi_AesSetKey(&ContextID, SASI_AES_USER_KEY, &keyData, sizeof(keyData) );
  166. if (ret != SA_SILIB_RET_OK){
  167. INTEG_TEST_PRINT("SaSi_AesSetKey failed with error 0x%x\n",ret);
  168. return ret;
  169. }
  170. /*Call CRYS_AES_Block for each 16 byte block data of the input buffer*/
  171. for (block_index = 0; block_index<aesVectors[test_index].aesTest_DataSize/SASI_AES_BLOCK_SIZE_IN_BYTES-1; block_index++)
  172. {
  173. ret = SaSi_AesBlock(&ContextID,
  174. dataInBuff + (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES),
  175. SASI_AES_BLOCK_SIZE_IN_BYTES,
  176. dataOutBuff_ptr + (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES));
  177. if (ret != SA_SILIB_RET_OK){
  178. INTEG_TEST_PRINT("CRYS_AES_Block failed with error 0x%x\n",ret);
  179. return ret;
  180. }
  181. }
  182. outSize = aesVectors[test_index].aesTest_DataSize - (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES);
  183. /*Call CRYS_AES_Finish for the last block (the size may be 0)*/
  184. if (aesVectors[test_index].aesTest_OperationMode == SASI_AES_MODE_XCBC_MAC ||
  185. aesVectors[test_index].aesTest_OperationMode == SASI_AES_MODE_CMAC){
  186. /*For XCBC_MAC and CMAC modes we should pass to SaSi_AesFinish the begining of the output buffer*/
  187. ret = SaSi_AesFinish(&ContextID,
  188. outSize,
  189. dataInBuff + (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES),
  190. outSize,
  191. dataOutBuff_ptr,
  192. &outSize);
  193. } else {
  194. /*For the rest of the modes we should pass to SaSi_AesFinish the relevant offset of output buffer*/
  195. ret = SaSi_AesFinish(&ContextID,
  196. outSize,
  197. dataInBuff + (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES),
  198. outSize,
  199. dataOutBuff_ptr + (block_index*SASI_AES_BLOCK_SIZE_IN_BYTES),
  200. &outSize);
  201. }
  202. if (ret != SA_SILIB_RET_OK){
  203. INTEG_TEST_PRINT("CRYS_AES_Finish failed with error 0x%x\n",ret);
  204. return ret;
  205. }
  206. /*Check the output buffer with expected data */
  207. ret = SaSi_PalMemCmp(dataOutBuff_ptr,aesVectors[test_index].aesTest_Expected_output_data,aesVectors[test_index].aesTest_OutputDataSize);
  208. if (ret != 0){
  209. INTEG_TEST_PRINT("output buffer of the test failed\n");
  210. return ret;
  211. }
  212. INTEG_TEST_PRINT(" Passed \n");
  213. }
  214. return ret;
  215. }
  216. #ifdef DX_LINUX_PLATFORM /*for linux platform only -> we need to use contiguous memory for stack !!*/
  217. /*aes_ccm_wrap_tests creates thread with defined stack address to and calls to AES CCM test */
  218. void* aes_thread(void)
  219. {
  220. uint32_t* threadReturnValue = SaSi_PalMemMalloc(sizeof(uint32_t));
  221. *threadReturnValue =aes_tests();
  222. if (*threadReturnValue != SA_SILIB_RET_OK) {
  223. INTEG_TEST_PRINT("Failure in aes_tests,ret = 0x%x\n", *threadReturnValue);
  224. goto exit;
  225. }
  226. /*Call chacha poly test*/
  227. *threadReturnValue = aes_ccm_tests();
  228. if (*threadReturnValue != SA_SILIB_RET_OK) {
  229. INTEG_TEST_PRINT("Failure in aes_ccm_tests,ret = 0x%x\n", *threadReturnValue);
  230. goto exit;
  231. }
  232. exit:
  233. pthread_exit(threadReturnValue);
  234. }
  235. int aes_wrap_tests(void){
  236. uint32_t rc = 0;
  237. pthread_t threadId;
  238. pthread_attr_t threadAttr;
  239. int threadRc;
  240. void *threadRet;
  241. int num = 6;
  242. threadRc = pthread_attr_init(&threadAttr);
  243. if (threadRc != 0) {
  244. INTEG_TEST_PRINT("pthread_attr_init failed\n");
  245. return -1;
  246. }
  247. threadRc = pthread_attr_setstack(&threadAttr, g_test_stack_base_addr, PTHREAD_STACK_SIZE);
  248. if (threadRc != 0) {
  249. INTEG_TEST_PRINT("pthread_attr_setstack failed\n");
  250. return -1;
  251. }
  252. /* Create independent thread which run with */
  253. threadRc = pthread_create( &threadId, &threadAttr, (void *)aes_thread, &num);
  254. if (threadRc != 0) {
  255. INTEG_TEST_PRINT( "pthread_create failed\n");
  256. return -1;
  257. }
  258. /* Wait till thread is complete before main continues */
  259. threadRc = pthread_join( threadId, &threadRet);
  260. if (threadRc != 0) {
  261. INTEG_TEST_PRINT( "pthread_join failed\n");
  262. return -1;
  263. }
  264. rc =*((uint32_t *)*&threadRet);
  265. SaSi_PalMemFree(threadRet);
  266. threadRc = pthread_attr_destroy(&threadAttr);
  267. if (threadRc != 0) {
  268. INTEG_TEST_PRINT("pthread_attr_destroy failed\n");
  269. }
  270. return rc;
  271. }
  272. #endif
  273. int main(void)
  274. {
  275. int ret = 0;
  276. /*Perform memory mapping*/
  277. ret = integration_tests_setup();
  278. if (ret != 0)
  279. {
  280. INTEG_TEST_PRINT("integration_tests_setup failed\n");
  281. return ret;
  282. }
  283. INTEG_TEST_PRINT("****************MAIN***********************\n");
  284. /*Init SaSi library*/
  285. ret = SaSi_LibInit();
  286. if (ret != SA_SILIB_RET_OK) {
  287. INTEG_TEST_PRINT("Failed SaSi_LibInit - ret = 0x%x\n", ret);
  288. goto exit_1;
  289. }
  290. ret = CRYS_RndInit(rndState_ptr, rndWorkBuff_ptr);
  291. if (ret != SA_SILIB_RET_OK) {
  292. INTEG_TEST_PRINT("Failed CRYS_RndInit - ret = 0x%x\n", ret);
  293. goto exit_1;
  294. }
  295. /*Call aes test*/
  296. #ifdef DX_LINUX_PLATFORM
  297. ret = aes_wrap_tests(); /*Call wrap function to create thread and to define stack's address to use contiguous memory*/
  298. #else
  299. /*Call aes test*/
  300. ret = aes_tests();
  301. if (ret != SA_SILIB_RET_OK) {
  302. INTEG_TEST_PRINT("Failure in aes test,ret = 0x%x\n", ret);
  303. goto exit_0;
  304. }
  305. ret = aes_ccm_tests();
  306. #endif
  307. if (ret != SA_SILIB_RET_OK) {
  308. INTEG_TEST_PRINT("Failure in aes ccm test,ret = 0x%x\n", ret);
  309. goto exit_0;
  310. }
  311. INTEG_TEST_PRINT("All tests passed \n");
  312. exit_0:
  313. /*Finish SaSi library*/
  314. SaSi_LibFini();
  315. ret = CRYS_RND_UnInstantiation(rndState_ptr);
  316. if (ret) {
  317. INTEG_TEST_PRINT("Failure in CRYS_RND_UnInstantiation,ret = 0x%x\n", ret);
  318. }
  319. exit_1:
  320. integration_tests_clear();
  321. return ret;
  322. }