main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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 "crys_rsa_kg.h"
  44. #include "crys_rsa_build.h"
  45. #include "crys_rsa_schemes.h"
  46. #include "crys_rsa_prim.h"
  47. #include "integration_test_plat_defs.h"
  48. #include "integration_test_ssi_defs.h"
  49. #include "integration_test_ssi_data.h"
  50. #ifdef DX_LINUX_PLATFORM
  51. #include <pthread.h>
  52. #endif
  53. /*RSA test data vectors*/
  54. extern rsaEncDecDataStuct rsaEncDecDataVectors[];
  55. extern rsaSignVerifyDataStuct rsaSignVerifyDataVectors[];
  56. /*RNG Global variables*/
  57. extern CRYS_RND_State_t* rndState_ptr;
  58. extern CRYS_RND_WorkBuff_t* rndWorkBuff_ptr;
  59. int rsa_SignVerify_tests(void);
  60. int rsa_EncDec_tests(void);
  61. int rsa_tests(void){
  62. uint32_t ret = 0;
  63. /*Init SaSi library*/
  64. ret = SaSi_LibInit();
  65. if (ret != SA_SILIB_RET_OK) {
  66. INTEG_TEST_PRINT("Failed SaSi_LibInit - ret = 0x%x\n", ret);
  67. goto exit_1;
  68. }
  69. ret = CRYS_RndInit(rndState_ptr, rndWorkBuff_ptr);
  70. if (ret != SA_SILIB_RET_OK) {
  71. INTEG_TEST_PRINT("Failed CRYS_RndInit - ret = 0x%x\n", ret);
  72. goto exit_1;
  73. }
  74. ret = rsa_EncDec_tests();
  75. if (ret != SA_SILIB_RET_OK) {
  76. INTEG_TEST_PRINT("Failure in encrypt/decrypt RSA tests,ret = 0x%x\n", ret);
  77. goto endRSA;
  78. }
  79. /*Call to RSA Sign/Verify Tests*/
  80. ret = rsa_SignVerify_tests();
  81. if (ret != SA_SILIB_RET_OK) {
  82. INTEG_TEST_PRINT("Failure in sign/verify tests,ret = 0x%x\n", ret);
  83. goto endRSA;
  84. }
  85. endRSA:
  86. /*Finish SaSi library*/
  87. SaSi_LibFini();
  88. ret = CRYS_RND_UnInstantiation(rndState_ptr);
  89. if (ret) {
  90. INTEG_TEST_PRINT("Failure in CRYS_RND_UnInstantiation,ret = 0x%x\n", ret);
  91. }
  92. exit_1:
  93. return ret;
  94. }
  95. int rsa_SignVerify_tests(void){
  96. uint32_t ret = 0;
  97. int test_index;
  98. RSAType_enum RSA_mode;
  99. CRYS_RSAUserPrivKey_t UserPrivKey;
  100. CRYS_RSAUserPubKey_t UserPubKey;
  101. CRYS_RSAKGData_t KeyGenData;
  102. CRYS_RSAKGFipsContext_t FipsCtx;
  103. CRYS_RSAPrivUserContext_t ContextPrivate;
  104. CRYS_RSAPubUserContext_t ContextPub;
  105. uint16_t ActualSignatureSize;
  106. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc;
  107. rndGenerateVectFunc = CRYS_RND_GenerateVector;
  108. /*Run all RSA tests*/
  109. for (RSA_mode = NON_CRT_MODE ; RSA_mode <= CRT_MODE; RSA_mode++) /* Check both CRT and non CRT modes*/
  110. {
  111. for (test_index = 0; test_index < RSA_SIGN_VERIFY_OEP_TESTS_NUMBER ; test_index++) /*Run with all RSA data vectors*/
  112. {
  113. if (test_index == 0){ /*Check internal keys*/
  114. if (RSA_mode == NON_CRT_MODE ){ /*Check non CRT keys*/
  115. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPair \n");
  116. ret = CRYS_RSA_KG_GenerateKeyPair(rndState_ptr, rndGenerateVectFunc,
  117. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PublicExponent_E,
  118. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PubExponentSize,
  119. rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize*8,
  120. &UserPrivKey,
  121. &UserPubKey,
  122. &KeyGenData,
  123. &FipsCtx);
  124. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPair for key pair with 0x%x \n",ret);
  125. if (ret != SA_SILIB_RET_OK){
  126. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPair for key pair failed with 0x%x \n",ret);
  127. goto endRSA;
  128. }
  129. } else {/*Check CRT keys*/
  130. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT \n");
  131. ret = CRYS_RSA_KG_GenerateKeyPairCRT(rndState_ptr, rndGenerateVectFunc,
  132. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PublicExponent_E,
  133. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PubExponentSize,
  134. rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize*8,
  135. &UserPrivKey,
  136. &UserPubKey,
  137. &KeyGenData,
  138. &FipsCtx);
  139. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT for key pair returned with 0x%x \n",ret);
  140. if (ret != SA_SILIB_RET_OK){
  141. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT for key pair failed with 0x%x \n",ret);
  142. goto endRSA;
  143. }
  144. }
  145. } else { /*Check external keys*/
  146. /*Call CRYS_RSA_Build_PrivKey to create public key from external key buffer*/
  147. ret = CRYS_RSA_Build_PubKey(&UserPubKey,
  148. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PublicExponent_E,
  149. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PubExponentSize,
  150. rsaSignVerifyDataVectors[test_index].rsaSignVerify_Modulus_N,
  151. rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize);
  152. if (ret != SA_SILIB_RET_OK){
  153. INTEG_TEST_PRINT("CRYS_RSA_Build_PubKey for key failed with 0x%x \n",ret);
  154. goto endRSA;
  155. }
  156. INTEG_TEST_PRINT("CRYS_RSA_Build_PubKey for key pair with 0x%x \n",ret);
  157. if (RSA_mode == NON_CRT_MODE) {/*Check non CRT keys*/
  158. /*Call CRYS_RSA_Build_PrivKey to create private key from external key buffer*/
  159. ret = CRYS_RSA_Build_PrivKey (&UserPrivKey,
  160. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PrivetExponent_D,
  161. rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize,
  162. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PublicExponent_E,
  163. rsaSignVerifyDataVectors[test_index].rsaSignVerify_PubExponentSize,
  164. rsaSignVerifyDataVectors[test_index].rsaSignVerify_Modulus_N,
  165. rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize);
  166. if (ret != SA_SILIB_RET_OK){
  167. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKey for key failed with 0x%x \n",ret);
  168. goto endRSA;
  169. }
  170. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKey for key pair returned with 0x%x \n",ret);
  171. } else { /*Check CRT keys*/
  172. /*Call CRYS_RSA_Build_PrivKey to create private key from external key buffer*/
  173. ret = CRYS_RSA_Build_PrivKeyCRT (&UserPrivKey,
  174. rsaSignVerifyDataVectors[test_index].rsaSignVerify_P,
  175. rsaSignVerifyDataVectors[test_index].rsaSignVerify_DPSize,
  176. rsaSignVerifyDataVectors[test_index].rsaSignVerify_Q,
  177. rsaSignVerifyDataVectors[test_index].rsaSignVerify_DPSize,
  178. rsaSignVerifyDataVectors[test_index].rsaSignVerify_dP,
  179. rsaSignVerifyDataVectors[test_index].rsaSignVerify_DPSize,
  180. rsaSignVerifyDataVectors[test_index].rsaSignVerify_dQ,
  181. rsaSignVerifyDataVectors[test_index].rsaSignVerify_DPSize,
  182. rsaSignVerifyDataVectors[test_index].rsaSignVerify_Qinv,
  183. rsaSignVerifyDataVectors[test_index].rsaSignVerify_DPSize);
  184. if (ret != SA_SILIB_RET_OK){
  185. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKeyCRT for key failed with 0x%x \n",ret);
  186. goto endRSA;
  187. }
  188. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKeyCRT for key pair returned with 0x%x \n",ret);
  189. }
  190. }
  191. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKeyCRT for key pair returned with 0x%x \n",ret);
  192. ActualSignatureSize = rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize;
  193. /*Call CRYS_RSA_PSS_Sign for PKCS#1 ver2.1 using SHA512*/
  194. ret = CRYS_RSA_PSS_Sign(rndState_ptr, rndGenerateVectFunc,
  195. &ContextPrivate,
  196. &UserPrivKey,
  197. CRYS_RSA_HASH_SHA512_mode,
  198. CRYS_PKCS1_MGF1,
  199. rsaSignVerifyDataVectors[test_index].rsaSignVerify_SaltLength,
  200. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_data,//DataIn_ptr,
  201. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_dataSize,//DataInSize,
  202. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture,//Output_ptr,
  203. &ActualSignatureSize);
  204. if (ret != SA_SILIB_RET_OK){
  205. INTEG_TEST_PRINT("CRYS_RSA_PSS_Sign failed with 0x%x \n",ret);
  206. goto endRSA;
  207. }
  208. INTEG_TEST_PRINT("CRYS_RSA_PSS_Sign passed \n");
  209. /*Verify the signature using using SHA512*/
  210. ret = CRYS_RSA_PSS_Verify(
  211. &ContextPub,
  212. &UserPubKey,
  213. CRYS_RSA_HASH_SHA512_mode,
  214. CRYS_PKCS1_MGF1,
  215. rsaSignVerifyDataVectors[test_index].rsaSignVerify_SaltLength,
  216. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_data,
  217. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_dataSize,
  218. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture);
  219. if (ret != SA_SILIB_RET_OK){
  220. INTEG_TEST_PRINT("CRYS_RSA_PSS_Verify failed with 0x%x \n",ret);
  221. goto endRSA;
  222. }
  223. INTEG_TEST_PRINT("CRYS_RSA_PSS_Verify passed \n");
  224. ActualSignatureSize = rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize;
  225. /*Call CRYS_RSA_PSS_SHA1_Sign PKCS#1 ver2.1 to sign on precalculated hash input using SHA1*/
  226. ret = CRYS_RSA_PSS_SHA1_Sign(rndState_ptr, rndGenerateVectFunc,
  227. &ContextPrivate,
  228. &UserPrivKey,
  229. CRYS_PKCS1_MGF1,
  230. rsaSignVerifyDataVectors[test_index].rsaSignVerify_SaltLength,
  231. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA1,//rsaSignVerify_input_data,
  232. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture,
  233. &ActualSignatureSize);
  234. if (ret != SA_SILIB_RET_OK){
  235. INTEG_TEST_PRINT("CRYS_RSA_PSS_SHA1_Sign failed with 0x%x \n",ret);
  236. goto endRSA;
  237. }
  238. INTEG_TEST_PRINT("CRYS_RSA_PSS_SHA1_Sign passed \n");
  239. /*Verify signed hash using SHA1*/
  240. ret = CRYS_RSA_PSS_SHA1_Verify(&ContextPub,
  241. &UserPubKey,
  242. CRYS_PKCS1_MGF1,
  243. rsaSignVerifyDataVectors[test_index].rsaSignVerify_SaltLength,
  244. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA1,
  245. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture);
  246. if (ret != SA_SILIB_RET_OK){
  247. INTEG_TEST_PRINT("CRYS_RSA_PSS_SHA1_Verify failed with 0x%x \n",ret);
  248. goto endRSA;
  249. }
  250. INTEG_TEST_PRINT("CRYS_RSA_PSS_SHA1_Verify passed \n");
  251. ActualSignatureSize = rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize;
  252. /*Call CRYS_RSA_PKCS1v15_Sign PKCS#1 ver1.5 to sign on input data*/
  253. ret = CRYS_RSA_PKCS1v15_Sign(rndState_ptr, rndGenerateVectFunc,
  254. &ContextPrivate,
  255. &UserPrivKey,
  256. CRYS_RSA_HASH_SHA256_mode,
  257. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_data,
  258. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_dataSize,
  259. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture,
  260. &ActualSignatureSize);
  261. if (ret != SA_SILIB_RET_OK){
  262. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Sign failed with 0x%x \n",ret);
  263. goto endRSA;
  264. }
  265. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Sign passed \n");
  266. /*Verify the signature*/
  267. ret = CRYS_RSA_PKCS1v15_Verify(&ContextPub,
  268. &UserPubKey,
  269. CRYS_RSA_HASH_SHA256_mode,
  270. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_data,
  271. rsaSignVerifyDataVectors[test_index].rsaSignVerify_input_dataSize,
  272. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture);
  273. if (ret != SA_SILIB_RET_OK){
  274. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Verify failed with 0x%x \n",ret);
  275. goto endRSA;
  276. }
  277. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Verify passed \n");
  278. ActualSignatureSize = rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize;
  279. /*Call CRYS_RSA_PKCS1v15_SHA256_Sign PKCS#1 ver1.5 to sign on precalculated hash input using SHA256*/
  280. ret = CRYS_RSA_PKCS1v15_SHA256_Sign(rndState_ptr, rndGenerateVectFunc,
  281. &ContextPrivate,
  282. &UserPrivKey,
  283. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA256,
  284. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture,
  285. &ActualSignatureSize);
  286. if (ret != SA_SILIB_RET_OK){
  287. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA256_Sign failed with 0x%x \n",ret);
  288. goto endRSA;
  289. }
  290. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA256_Sign passed \n");
  291. /*Verify the signature*/
  292. ret = CRYS_RSA_PKCS1v15_SHA256_Verify(&ContextPub,
  293. &UserPubKey,
  294. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA256,
  295. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture);
  296. if (ret != SA_SILIB_RET_OK){
  297. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA256_Verify failed with 0x%x \n",ret);
  298. goto endRSA;
  299. }
  300. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA256_Verify passed \n");
  301. ActualSignatureSize = rsaSignVerifyDataVectors[test_index].rsaSignVerify_KeySize;
  302. /*Call CRYS_RSA_PKCS1v15_SHA256_Sign PKCS#1 ver1.5 to sign on precalculated hash input using SHA512*/
  303. ret = CRYS_RSA_PKCS1v15_SHA512_Sign(rndState_ptr, rndGenerateVectFunc,
  304. &ContextPrivate,
  305. &UserPrivKey,
  306. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA256,
  307. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture,
  308. &ActualSignatureSize);
  309. if (ret != SA_SILIB_RET_OK){
  310. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA512_Sign failed with 0x%x \n",ret);
  311. goto endRSA;
  312. }
  313. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA512_Sign passed \n");
  314. /*Verify the signature*/
  315. ret = CRYS_RSA_PKCS1v15_SHA512_Verify(&ContextPub,
  316. &UserPubKey,
  317. rsaSignVerifyDataVectors[test_index].rsaSignVerify_hash_SHA256,
  318. rsaSignVerifyDataVectors[test_index].rsaSignVerify_output_signiture);
  319. if (ret != SA_SILIB_RET_OK){
  320. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA512_Verify failed with 0x%x \n",ret);
  321. goto endRSA;
  322. }
  323. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_SHA512_Verify passed \n");
  324. }
  325. }
  326. INTEG_TEST_PRINT("All RSA SIGN/VERIFY tests passed \n=======================\n");
  327. endRSA:
  328. return ret;
  329. }
  330. int rsa_EncDec_tests(void){
  331. uint32_t ret = 0;
  332. int test_index;
  333. uint16_t ActualDecDataSize;
  334. RSAType_enum RSA_mode;
  335. CRYS_RSAUserPrivKey_t UserPrivKey;
  336. CRYS_RSAUserPubKey_t UserPubKey;
  337. CRYS_RSAKGData_t KeyGenData;
  338. CRYS_RSAKGFipsContext_t FipsCtx;
  339. CRYS_RSAPrimeData_t PrimeData;
  340. CRYS_RSA_HASH_OpMode_t HASH_OpMode;
  341. uint16_t LessData_for_OAEP;
  342. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc;
  343. rndGenerateVectFunc = CRYS_RND_GenerateVector;
  344. /*Run all RSA tests*/
  345. for (RSA_mode = NON_CRT_MODE ; RSA_mode <= CRT_MODE; RSA_mode++) /* Check both CRT and non CRT modes*/
  346. {
  347. for (test_index = 0; test_index < RSA_ENCDEC_OEP_TESTS_NUMBER ; test_index++) /*Run with all RSA data vectors*/
  348. {
  349. if (test_index == 0){ /*Check different hash modes*/
  350. HASH_OpMode = CRYS_RSA_HASH_SHA1_mode;
  351. LessData_for_OAEP = TST_LESS_DATA_FOR_OAEP_ENCRYPT_SHA1; //Select the size according to the hash mode*/
  352. }else {
  353. HASH_OpMode = CRYS_RSA_HASH_SHA256_mode;
  354. LessData_for_OAEP = TST_LESS_DATA_FOR_OAEP_ENCRYPT_SHA256;//Select the size according to the hash mode*/
  355. }
  356. /*Print test's parameters*/
  357. if (HASH_OpMode == CRYS_RSA_HASH_SHA1_mode ){
  358. INTEG_TEST_PRINT("\n\nRSA encrypt decrypt test number 0x%x Parameters : \n-----%s CRYS_RSA_HASH_SHA1_mode -----\n ",test_index,(uint32_t)rsaEncDecDataVectors[test_index].rsaEncDec_Name);
  359. } else {
  360. INTEG_TEST_PRINT("\n\nRSA encrypt decrypt test number 0x%x Parameters : \n-----%s CRYS_RSA_HASH_SHA256_mode -----\n ",test_index,(uint32_t)rsaEncDecDataVectors[test_index].rsaEncDec_Name);
  361. }
  362. /*Update ActualDecDataSize to output size*/
  363. ActualDecDataSize = rsaEncDecDataVectors[test_index].rsaEncDec_KeySize*8;
  364. if (test_index == 0){ /*Check internal keys*/
  365. if (RSA_mode == NON_CRT_MODE ){ /*Check non CRT keys*/
  366. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPair \n");
  367. ret = CRYS_RSA_KG_GenerateKeyPair(rndState_ptr, rndGenerateVectFunc,
  368. rsaEncDecDataVectors[test_index].rsaEncDec_PublicExponent_E,
  369. rsaEncDecDataVectors[test_index].rsaEncDec_PubExponentSize,
  370. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize*8,
  371. &UserPrivKey,
  372. &UserPubKey,
  373. &KeyGenData,
  374. &FipsCtx);
  375. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPair for key pair with 0x%x \n",ret);
  376. if (ret != SA_SILIB_RET_OK){
  377. INTEG_TEST_PRINT(" CRYS_RSA_KG_GenerateKeyPair for key pair failed with 0x%x \n",ret);
  378. goto endRSA;
  379. }
  380. } else {/*Check CRT keys*/
  381. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT \n");
  382. ret = CRYS_RSA_KG_GenerateKeyPairCRT(rndState_ptr, rndGenerateVectFunc,
  383. rsaEncDecDataVectors[test_index].rsaEncDec_PublicExponent_E,
  384. rsaEncDecDataVectors[test_index].rsaEncDec_PubExponentSize,
  385. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize*8,
  386. &UserPrivKey,
  387. &UserPubKey,
  388. &KeyGenData,
  389. &FipsCtx);
  390. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT for key pair returned with 0x%x \n",ret);
  391. if (ret != SA_SILIB_RET_OK){
  392. INTEG_TEST_PRINT("CRYS_RSA_KG_GenerateKeyPairCRT for key pair failed with 0x%x \n",ret);
  393. goto endRSA;
  394. }
  395. }
  396. } else { /*Check external keys*/
  397. /*Call CRYS_RSA_Build_PrivKey to create public key from external key buffer*/
  398. ret = CRYS_RSA_Build_PubKey(&UserPubKey,
  399. rsaEncDecDataVectors[test_index].rsaEncDec_PublicExponent_E,
  400. rsaEncDecDataVectors[test_index].rsaEncDec_PubExponentSize,
  401. rsaEncDecDataVectors[test_index].rsaEncDec_Modulus_N,
  402. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize);
  403. if (ret != SA_SILIB_RET_OK){
  404. INTEG_TEST_PRINT("CRYS_RSA_Build_PubKey for key failed with 0x%x \n",ret);
  405. goto endRSA;
  406. }
  407. INTEG_TEST_PRINT("CRYS_RSA_Build_PubKey for key pair with 0x%x \n",ret);
  408. if (RSA_mode == NON_CRT_MODE) {/*Check non CRT keys*/
  409. /*Call CRYS_RSA_Build_PrivKey to create private key from external key buffer*/
  410. ret = CRYS_RSA_Build_PrivKey (&UserPrivKey,
  411. rsaEncDecDataVectors[test_index].rsaEncDec_PrivetExponent_D,
  412. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize,
  413. rsaEncDecDataVectors[test_index].rsaEncDec_PublicExponent_E,
  414. rsaEncDecDataVectors[test_index].rsaEncDec_PubExponentSize,
  415. rsaEncDecDataVectors[test_index].rsaEncDec_Modulus_N,
  416. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize);
  417. if (ret != SA_SILIB_RET_OK){
  418. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKey for key failed with 0x%x \n",ret);
  419. goto endRSA;
  420. }
  421. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKey for key pair returned with 0x%x \n",ret);
  422. } else { /*Check CRT keys*/
  423. /*Call CRYS_RSA_Build_PrivKey to create private key from external key buffer*/
  424. ret = CRYS_RSA_Build_PrivKeyCRT (&UserPrivKey,
  425. rsaEncDecDataVectors[test_index].rsaEncDec_P,
  426. rsaEncDecDataVectors[test_index].rsaEncDec_DPSize,
  427. rsaEncDecDataVectors[test_index].rsaEncDec_Q,
  428. rsaEncDecDataVectors[test_index].rsaEncDec_DPSize,
  429. rsaEncDecDataVectors[test_index].rsaEncDec_dP,
  430. rsaEncDecDataVectors[test_index].rsaEncDec_DPSize,
  431. rsaEncDecDataVectors[test_index].rsaEncDec_dQ,
  432. rsaEncDecDataVectors[test_index].rsaEncDec_DPSize,
  433. rsaEncDecDataVectors[test_index].rsaEncDec_Qinv,
  434. rsaEncDecDataVectors[test_index].rsaEncDec_DPSize);
  435. if (ret != SA_SILIB_RET_OK){
  436. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKeyCRT for key failed with 0x%x \n",ret);
  437. goto endRSA;
  438. }
  439. INTEG_TEST_PRINT("CRYS_RSA_Build_PrivKeyCRT for key pair returned with 0x%x \n",ret);
  440. }
  441. }
  442. /*Call CRYS_RSA_OAEP_Encrypt to ecrypt data buffer */
  443. ret = CRYS_RSA_OAEP_Encrypt(rndState_ptr,
  444. rndGenerateVectFunc,
  445. &UserPubKey,
  446. &PrimeData,
  447. HASH_OpMode,
  448. NULL,
  449. 0,
  450. CRYS_PKCS1_MGF1,
  451. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  452. (uint16_t)(rsaEncDecDataVectors[test_index].rsaEncDec_KeySize-LessData_for_OAEP),
  453. rsaEncDecDataVectors[test_index].rsaEncDec_output_data);
  454. if (ret != SA_SILIB_RET_OK){
  455. INTEG_TEST_PRINT("CRYS_RSA_OAEP_Encrypt for key failed with 0x%x \n",ret);
  456. goto endRSA;
  457. }
  458. INTEG_TEST_PRINT("CRYS_RSA_OAEP_Encrypt returned with 0x%x \n",ret);
  459. /*Call CRYS_RSA_OAEP_Decrypt to decrypt data buffer */
  460. ret = CRYS_RSA_OAEP_Decrypt(&UserPrivKey,
  461. &PrimeData,
  462. HASH_OpMode,
  463. NULL,
  464. 0,
  465. CRYS_PKCS1_MGF1,
  466. rsaEncDecDataVectors[test_index].rsaEncDec_output_data,
  467. (uint16_t)rsaEncDecDataVectors[test_index].rsaEncDec_KeySize,
  468. rsaEncDecDataVectors[test_index].rsaEncDec_output_data,//Inplace operation
  469. &ActualDecDataSize);
  470. if (ret != SA_SILIB_RET_OK){
  471. INTEG_TEST_PRINT("CRYS_RSA_OAEP_Decrypt failed with 0x%x \n",ret);
  472. goto endRSA;
  473. }
  474. INTEG_TEST_PRINT("CRYS_RSA_OAEP_Decrypt returned with 0x%x \n",ret);
  475. /*Compare decrypted buffer with input buffer*/
  476. ret = SaSi_PalMemCmp(rsaEncDecDataVectors[test_index].rsaEncDec_output_data ,
  477. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  478. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize-LessData_for_OAEP);
  479. if (ret != 0){
  480. INTEG_TEST_PRINT("Decrypted buffer for OAEP is wrong");
  481. goto endRSA;
  482. }
  483. /*Update ActualDecDataSize to output size*/
  484. ActualDecDataSize = rsaEncDecDataVectors[test_index].rsaEncDec_KeySize*8;
  485. /*CAll to CRYS_RSA_PKCS1v15_Encrypt to encrypt input buffer using PKCS#1 1.5*/
  486. ret = CRYS_RSA_PKCS1v15_Encrypt(rndState_ptr, rndGenerateVectFunc,
  487. &UserPubKey,
  488. &PrimeData,
  489. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  490. (uint16_t)(rsaEncDecDataVectors[test_index].rsaEncDec_KeySize-TST_LESS_DATA_FOR_PKCS1V15_ENCRYPT),
  491. rsaEncDecDataVectors[test_index].rsaEncDec_output_data);
  492. if (ret != SA_SILIB_RET_OK){
  493. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Encrypt failed with 0x%x \n",ret);
  494. goto endRSA;
  495. }
  496. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Encrypt returned with 0x%x \n",ret);
  497. /*DEcrypt oupu buffer*/
  498. ret = CRYS_RSA_PKCS1v15_Decrypt(&UserPrivKey,
  499. &PrimeData,
  500. rsaEncDecDataVectors[test_index].rsaEncDec_output_data,
  501. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize,
  502. rsaEncDecDataVectors[test_index].rsaEncDec_output_data,//Inplace operation
  503. &ActualDecDataSize);
  504. if (ret != SA_SILIB_RET_OK){
  505. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Decrypt failed with 0x%x \n",ret);
  506. goto endRSA;
  507. }
  508. INTEG_TEST_PRINT("CRYS_RSA_PKCS1v15_Decrypt returned with 0x%x \n",ret);
  509. /*Compare decrypted buffer with input buffer*/
  510. ret = SaSi_PalMemCmp(rsaEncDecDataVectors[test_index].rsaEncDec_output_data ,
  511. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  512. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize-LessData_for_OAEP);
  513. if (ret != 0){
  514. INTEG_TEST_PRINT("Decrypted buffer for PKCS1v15 is wrong");
  515. goto endRSA;
  516. }
  517. /*Call to CRYS_RSA_PRIM_Encrypt to encrypt input buffer*/
  518. ret = CRYS_RSA_PRIM_Encrypt(&UserPubKey,
  519. &PrimeData,
  520. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  521. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize,
  522. rsaEncDecDataVectors[test_index].rsaEncDec_output_data);
  523. if (ret != SA_SILIB_RET_OK){
  524. INTEG_TEST_PRINT("CRYS_RSA_PRIM_Encrypt failed with 0x%x \n",ret);
  525. goto endRSA;
  526. }
  527. INTEG_TEST_PRINT("CRYS_RSA_PRIM_Encrypt returned with 0x%x \n",ret);
  528. /*Decrypt oupu buffer*/
  529. ret = CRYS_RSA_PRIM_Decrypt(&UserPrivKey,
  530. &PrimeData,
  531. rsaEncDecDataVectors[test_index].rsaEncDec_output_data,
  532. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize,
  533. rsaEncDecDataVectors[test_index].rsaEncDec_output_data);//Inplace operation
  534. if (ret != SA_SILIB_RET_OK){
  535. INTEG_TEST_PRINT(" CRYS_RSA_PRIM_Decrypt failed with 0x%x \n",ret);
  536. goto endRSA;
  537. }
  538. INTEG_TEST_PRINT("CRYS_RSA_PRIM_Decrypt returned with 0x%x \n",ret);
  539. /*Compare decrypted buffer with input buffer*/
  540. ret = SaSi_PalMemCmp(rsaEncDecDataVectors[test_index].rsaEncDec_output_data ,
  541. rsaEncDecDataVectors[test_index].rsaEncDec_input_data,
  542. rsaEncDecDataVectors[test_index].rsaEncDec_KeySize);
  543. if (ret != 0){
  544. INTEG_TEST_PRINT("Decrypted buffer for PRIM is wrong");
  545. goto endRSA;
  546. }
  547. }
  548. }
  549. INTEG_TEST_PRINT("All RSA ENCRYPT/DECRYPT tests passed \n=======================\n");
  550. endRSA:
  551. return ret;
  552. }
  553. #ifdef DX_LINUX_PLATFORM /*for linux platform only -> we need to use contiguous memory for stack !!*/
  554. /*rsa_wrap_tests creates thread with defined stack address to and calls to rsa test */
  555. void* rsa_thread(int (*funcPtr)(void))
  556. {
  557. uint32_t* threadReturnValue = SaSi_PalMemMalloc(sizeof(uint32_t));
  558. *threadReturnValue =funcPtr();
  559. if (*threadReturnValue != SA_SILIB_RET_OK) {
  560. INTEG_TEST_PRINT("Failure in funcPtr,ret = 0x%x\n", *threadReturnValue);
  561. goto exit;
  562. }
  563. exit:
  564. pthread_exit(threadReturnValue);
  565. }
  566. int rsa_wrap_tests(int (*funcPtr)(void)){
  567. uint32_t rc = 0;
  568. pthread_t threadId;
  569. pthread_attr_t threadAttr;
  570. int threadRc;
  571. void *threadRet;
  572. threadRc = pthread_attr_init(&threadAttr);
  573. if (threadRc != 0) {
  574. INTEG_TEST_PRINT("pthread_attr_init failed\n");
  575. return -1;
  576. }
  577. threadRc = pthread_attr_setstack(&threadAttr, g_test_stack_base_addr, PTHREAD_STACK_SIZE);
  578. if (threadRc != 0) {
  579. INTEG_TEST_PRINT("pthread_attr_setstack failed\n");
  580. return -1;
  581. }
  582. /* Create independent thread which run with */
  583. threadRc = pthread_create( &threadId, &threadAttr, (void*)rsa_thread, funcPtr);
  584. if (threadRc != 0) {
  585. INTEG_TEST_PRINT( "pthread_create failed\n");
  586. return -1;
  587. }
  588. /* Wait till thread is complete before main continues */
  589. threadRc = pthread_join( threadId, &threadRet);
  590. if (threadRc != 0) {
  591. INTEG_TEST_PRINT( "pthread_join failed\n");
  592. return -1;
  593. }
  594. rc =*((uint32_t *)*&threadRet);
  595. SaSi_PalMemFree(threadRet);
  596. threadRc = pthread_attr_destroy(&threadAttr);
  597. if (threadRc != 0) {
  598. INTEG_TEST_PRINT("pthread_attr_destroy failed\n");
  599. }
  600. return rc;
  601. }
  602. #endif
  603. int main(void)
  604. {
  605. int ret = 0;
  606. /*Perform memory mapping*/
  607. ret = integration_tests_setup();
  608. if (ret != 0)
  609. {
  610. INTEG_TEST_PRINT("integration_tests_setup failed\n");
  611. return ret;
  612. }
  613. #ifdef DX_LINUX_PLATFORM
  614. ret = rsa_wrap_tests(rsa_tests);
  615. #else
  616. ret = rsa_tests();
  617. #endif
  618. if (ret != SA_SILIB_RET_OK) {
  619. INTEG_TEST_PRINT("Failure in rsa_tests,ret = 0x%x\n", ret);
  620. goto exit_0;
  621. }
  622. INTEG_TEST_PRINT("All tests passed \n");
  623. exit_0:
  624. integration_tests_clear();
  625. return ret;
  626. }