The nrf_crypto Test Example verifies the functionality of the nrf_crypto operations by using known test vectors approved by the National Institute of Standards and Technology (NIST) and others. All available nrf_crypto backends can be tested by modifying the nrf_crypto section in the
sdk_config
file, as described in
Configuring nrf_crypto frontend and backends
.
You can find the source code and the project file of the example in the following folder:
<InstallFolder>\examples\crypto\nrf_crypto\test_app
Testing
Test the nrf_crypto test example application by performing the following steps:
- Compile and program the application.
-
Observe the result of the different test vectors in the log using an RTT viewer or a terminal emulator. Make sure the test vector verdict is "passed".
#0022 Test vector passed: SHA SHA256 message_len=64
-
A summary stating the number of passed and failed test vectors will be printed in the end of execution. The total number of executed test cases and test vectors depends on the current nrf_crypto backend. Make sure no test vectors failed.
*************************************** All Tests Done X test vectors passed 0 test vectors failed ***************************************
Adding additional test cases and test vectors
Test cases and test vectors can be added to the test suite either by including additional source files or by extending the existing files.
Test Case
A test case is a function designed to verify parts of the functionality of an nrf_crypto operation. Most nrf_crypto operations, like hash calculations and ECDH, have multiple test cases to be able to cover all features. A typical test case loops over all related test vectors, and logs the verdict of each.
An example of a hash test case in a simplified form is shown in the code block below.
Registering a test case
A new test case must be registered to the test_case_data section using
Experimental: Section variables
. The
NRF_SECTION_ITEM_REGISTER
macro below places the variable test_hash in a section named "test_case_data", which is initialized by main.
- Note
-
The macro call to NRF_SECTION_ITEM_REGISTER must be done in a
.c file.
Test Vectors
A test vector is a set of inputs and expected outputs to verify the functionality provided in a test case. The
p_test_vector_name
variable is the only required member in the test_vector structures.
Register a test vector
Test vectors are added by registering them for a section defined in the test case code. The test vector is registered in the section "test_vector_hash_data", which is defined in the test case example
exec_test_case_hash
. The test vector can reuse the already defined hash test vector structure
test_vector_hash_t
, as shown in the code block below.
Output logging
The test project generates a test log using RTT transport, as described in the
Testing
section. The new test case
exec_test_case_hash
and its test vector will be added to the test log when the test is executed. The additional test log output will look like the lines below.
*************************************** Test case Hash Test Started #00XX Test vector passed: SHA256 test vector example Test case Hash Test Done ***************************************