ret_code_t nrf_crypto_rng_backend_reseed(void * const p_context, void * p_temp_buffer, uint8_t * p_input_data, size_t size) { bool mutex_locked; CRYSError_t err_code; ret_code_t ret_val = NRF_SUCCESS; CRYS_RND_WorkBuff_t * p_work_buffer = (CRYS_RND_WorkBuff_t *)p_temp_buffer; CRYS_RND_State_t * p_crys_rnd_state = &((nrf_crypto_backend_rng_context_t *)p_context)->crys_rnd_state; VERIFY_TRUE(size <= CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS, NRF_ERROR_CRYPTO_INPUT_LENGTH); VERIFY_TRUE((size & 0x3) == 0, NRF_ERROR_CRYPTO_INTERNAL); mutex_locked = cc310_backend_mutex_trylock(); VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); cc310_backend_enable(); if (size > 0) { err_code = CRYS_RND_AddAdditionalInput(p_crys_rnd_state, p_input_data, size); ret_val = result_get(err_code); if (ret_val != NRF_SUCCESS) { goto exit; } } err_code = CRYS_RND_Reseeding(p_crys_rnd_state, p_work_buffer); ret_val = result_get(err_code); exit: cc310_backend_disable(); cc310_backend_mutex_unlock(); return ret_val; }
/*rnd_test - performs basic integration test for RND module*/ int rnd_tests(void) { uint32_t ret = 0/*,MaxVectorSize = 0*/; int test_index = 0; /*Set additional input for rng seed*/ ret = CRYS_RND_AddAdditionalInput(rndContext_ptr, rndVectors[test_index].rndTest_AddInputData, rndVectors[test_index].rndTest_AddInputSize); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_AddAdditionalInput failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_AddAdditionalInput passed\n"); /*Reseed rnd using added input (new seed will be generated using additional input)*/ ret = CRYS_RND_Reseeding (rndContext_ptr, rndWorkBuff_ptr); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_Reseeding failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_Reseeding passed\n"); /*Generate random vector 1*/ ret = CRYS_RND_GenerateVector(&rndContext_ptr->rndState, rndVectors[test_index].rndTest_RandomVectorSize, rndVectors[test_index].rndTest_RandomVectorData1); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector for vector 1 failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector for first vector passed\n"); /*Generate rnadom vector 2*/ ret = CRYS_RND_GenerateVector(&rndContext_ptr->rndState, rndVectors[test_index].rndTest_RandomVectorSize, rndVectors[test_index].rndTest_RandomVectorData2); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector for vector 2 failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector for second vector passed\n"); /*Compare two generated vectors - should not be the same value*/ ret = SaSi_PalMemCmp(rndVectors[test_index].rndTest_RandomVectorData1, rndVectors[test_index].rndTest_RandomVectorData2, rndVectors[test_index].rndTest_RandomVectorSize); if (ret == SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n Two random vectors should not are the same \n"); return ret; } INTEG_TEST_PRINT("\n Two generated vectors are different as expected\n"); /*Generate random vector in range when max value is NULL*/ ret = CRYS_RND_GenerateVectorInRange(rndContext_ptr, rndVectors[test_index].rndTest_RandomVectorInRangeSize1, NULL, rndVectors[test_index].rndTest_RandomVectorInRangeData); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVectorInRange for vector with fixed size failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVectorInRange 1 passed\n"); /*Generate random vector in range with max vector */ ret = CRYS_RND_GenerateVectorInRange(rndContext_ptr, rndVectors[test_index].rndTest_RandomVectorInRangeSize2, rndVectors[test_index].rndTest_MaxVectorInRange2, rndVectors[test_index].rndTest_RandomVectorInRangeData2); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVectorInRange failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVectorInRange 2 passed\n"); /*Perform UnInstantiation*/ ret = CRYS_RND_UnInstantiation(rndContext_ptr); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_UnInstantiation failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_UnInstantiation passed\n"); /*Try to create random vector without instantiation - should fail*/ ret = CRYS_RND_GenerateVector(&rndContext_ptr->rndState, rndVectors[test_index].rndTest_RandomVectorSize, rndVectors[test_index].rndTest_RandomVectorData2); if (ret != CRYS_RND_STATE_VALIDATION_TAG_ERROR){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector returned wrong error 0x%x,\n CRYS_RND_STATE_VALIDATION_TAG_ERROR should be returned\n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector failed as expected\n"); /*Set additional input for RND seed*/ ret = CRYS_RND_AddAdditionalInput(rndContext_ptr, rndVectors[test_index].rndTest_AddInputData, rndVectors[test_index].rndTest_AddInputSize); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_AddAdditionalInput failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_AddAdditionalInput passed\n"); /*Perform instantiation for new seed*/ ret = CRYS_RND_Instantiation(rndContext_ptr, rndWorkBuff_ptr); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_Instantiation failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_Instantiation passed\n"); /*Try to create two vectors and check that the vectors are different*/ ret = CRYS_RND_GenerateVector(&rndContext_ptr->rndState, rndVectors[test_index].rndTest_RandomVectorSize, rndVectors[test_index].rndTest_RandomVectorData1); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector for vector 1 failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector 1 passed\n"); ret = CRYS_RND_GenerateVector(&rndContext_ptr->rndState, rndVectors[test_index].rndTest_RandomVectorSize, rndVectors[test_index].rndTest_RandomVectorData2); if (ret != SA_SILIB_RET_OK){ INTEG_TEST_PRINT(" CRYS_RND_GenerateVector for vector 2 failed with 0x%x \n",ret); return ret; } INTEG_TEST_PRINT("\n CRYS_RND_GenerateVector 2 passed\n"); ret = SaSi_PalMemCmp(rndVectors[test_index].rndTest_RandomVectorData1, rndVectors[test_index].rndTest_RandomVectorData2, rndVectors[test_index].rndTest_RandomVectorSize); if (ret == SA_SILIB_RET_OK){ INTEG_TEST_PRINT(" Two random vectors should not are the same \n"); return ret; } INTEG_TEST_PRINT("\n Compare passed\n"); INTEG_TEST_PRINT("\n All RND tests passed\n"); INTEG_TEST_PRINT("\n==========================\n"); ret = SA_SILIB_RET_OK; return ret; }