Microchip® Advanced Software Framework

xmega/drivers/aes/unit_tests/unit_tests.c File Reference

Unit tests for Advanced Encryption Standard crypto module.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include <stdint.h>
#include <stdbool.h>
#include <board.h>
#include <sysclk.h>
#include <aes.h>
#include <string.h>
#include <sleepmgr.h>
#include <unit_test/suite.h>
#include <stdio_serial.h>
#include <conf_test.h>
#include <pmic.h>

Functions

int main (void)
 Set up and run test suite. More...
 
AES interface test functions
static void run_aes_set_and_get_key_test (const struct test_case *test)
 Test AES get and set key value functions. More...
 
static void run_aes_state_interface_test (const struct test_case *test)
 Test AES state interface functions. More...
 
static void run_aes_encryption_test (const struct test_case *test)
 Test AES encryption function. More...
 
static void run_aes_decryption_test (const struct test_case *test)
 Test AES decryption function. More...
 
static void int_callback_aes (void)
 Callback for AES interrupt. More...
 
static void run_aes_encrypt_and_decrypt_test (const struct test_case *test)
 Run encryption, then decryption. More...
 

Variables

Variables and result data
t_key encryption_key
 Key used for encryption and generation of last subkey. More...
 
t_data encryption_data
 Data which will be encrypted. More...
 
t_data pre_encrypted_data
 Data which has been pre-encrypted with above key for comparison. More...
 
t_data dummy_data
 Dummy data used to fill AES state memory. More...
 
t_key lastsubkey
 Last subkey used in decryption of above encrypted data, generated from above encryption key. More...
 
volatile bool aes_is_finished = false
 Callback read finished variable. More...
 
t_data output_data
 Output data for enc/dec-test. More...
 

Helper functions and macros

#define set_buffer(buffer, value)   memset(buffer, value, sizeof(buffer))
 Set all bytes in buffer to value. More...
 
static bool compare_data_block (t_data block_1, t_data block_2)
 Compare two data blocks. More...
 
static bool aes_lastsubkey_generate (t_key key, t_key last_sub_key)
 Generate AES sub key. More...
 

#define set_buffer (   buffer,
  value 
)    memset(buffer, value, sizeof(buffer))

static bool aes_lastsubkey_generate ( t_key  key,
t_key  last_sub_key 
)
static

Generate AES sub key.

Get AES sub key by encryption of dummy data.

Parameters
keyPointer to AES key input.
last_sub_keyPointer to AES sub key output.

References aes_clear_error_flag(), aes_clear_interrupt_flag(), aes_configure(), AES_ENCRYPT, aes_get_key(), aes_is_busy(), aes_is_error(), AES_MANUAL, aes_set_key(), aes_software_reset(), aes_start(), aes_write_inputdata(), AES_XOR_OFF, and dummy_data.

Referenced by run_aes_decryption_test().

static bool compare_data_block ( t_data  block_1,
t_data  block_2 
)
static

Compare two data blocks.

Compares two blocks of equal length to see if they are equal.

Parameters
block_1Pointer to first block.
block_2Pointer to second block.

References AES_DATA_SIZE.

Referenced by run_aes_decryption_test(), run_aes_encrypt_and_decrypt_test(), run_aes_encryption_test(), and run_aes_set_and_get_key_test().

static void int_callback_aes ( void  )
static

Callback for AES interrupt.

References aes_is_finished, aes_read_outputdata(), and output_data.

Referenced by main().

static void run_aes_decryption_test ( const struct test_case test)
static

Test AES decryption function.

This test decrypts the pre-encrypted data and checks whether it is correct.

Parameters
testCurrent test case.

References aes_configure(), AES_DECRYPT, aes_is_busy(), aes_lastsubkey_generate(), AES_MANUAL, aes_read_outputdata(), aes_set_key(), aes_software_reset(), aes_start(), aes_write_inputdata(), AES_XOR_OFF, compare_data_block(), encryption_data, encryption_key, lastsubkey, pre_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_aes_encrypt_and_decrypt_test ( const struct test_case test)
static

Run encryption, then decryption.

Announce end by interrupt.

This test encrypts a data block, decrypts it, and checks if it's correct with respect to the input data block. Utilizes interrupts to let the software know when a encryption/decryption is done. AUTO mode will be used in this test.

Parameters
testCurrent test case.

References AES_AUTO, aes_configure(), AES_DECRYPT, AES_ENCRYPT, AES_INTLVL_LO, aes_is_finished, aes_isr_configure(), aes_set_key(), aes_software_reset(), aes_write_inputdata(), AES_XOR_OFF, AES_XOR_ON, compare_data_block(), encryption_data, encryption_key, lastsubkey, output_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_aes_encryption_test ( const struct test_case test)
static

Test AES encryption function.

This test generates an encrypted byte string from a key and input, and compares it to a pre-encrypted value.

Parameters
testCurrent test case.

References aes_configure(), AES_ENCRYPT, aes_is_busy(), AES_MANUAL, aes_read_outputdata(), aes_set_key(), aes_software_reset(), aes_start(), aes_write_inputdata(), AES_XOR_OFF, compare_data_block(), encryption_data, encryption_key, pre_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_aes_set_and_get_key_test ( const struct test_case test)
static

Test AES get and set key value functions.

This test sets a key in the AES module, and reads it back with the get function.

Parameters
testCurrent test case.

References aes_get_key(), aes_set_key(), aes_software_reset(), compare_data_block(), encryption_key, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_aes_state_interface_test ( const struct test_case test)
static

Test AES state interface functions.

This test verifies that the functions used to check the state of the module are working, and that the clear functions work correctly.

Parameters
testCurrent test case

References aes_clear_error_flag(), aes_clear_interrupt_flag(), aes_configure(), AES_ENCRYPT, aes_is_busy(), aes_is_error(), AES_MANUAL, aes_set_key(), aes_software_reset(), aes_start(), aes_write_inputdata(), AES_XOR_OFF, dummy_data, encryption_key, and test_assert_true.

Referenced by main().

volatile bool aes_is_finished = false

Callback read finished variable.

Referenced by int_callback_aes(), and run_aes_encrypt_and_decrypt_test().

t_data dummy_data
Initial value:
= {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}

Dummy data used to fill AES state memory.

Referenced by aes_lastsubkey_generate(), hmc5883l_calibrate(), and run_aes_state_interface_test().

t_data encryption_data
Initial value:
= {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
}

Data which will be encrypted.

Referenced by run_aes_decryption_test(), run_aes_encrypt_and_decrypt_test(), and run_aes_encryption_test().

t_key encryption_key
Initial value:
= {
0x30, 0x70, 0x97, 0x1A, 0xB7, 0xCE, 0x45, 0x06,
0x3F, 0xD2, 0x57, 0x3F, 0x49, 0xF5, 0x42, 0x0D
}

Key used for encryption and generation of last subkey.

Referenced by run_aes_decryption_test(), run_aes_encrypt_and_decrypt_test(), run_aes_encryption_test(), run_aes_set_and_get_key_test(), and run_aes_state_interface_test().

t_key lastsubkey

Last subkey used in decryption of above encrypted data, generated from above encryption key.

t_data output_data

Output data for enc/dec-test.

Referenced by int_callback_aes(), and run_aes_encrypt_and_decrypt_test().

t_data pre_encrypted_data
Initial value:
= {
0x59, 0x1D, 0xA5, 0xBF, 0xEA, 0x0E, 0xD7, 0x61,
0x24, 0x4E, 0x81, 0xBA, 0x1E, 0xF6, 0x24, 0xB5
}

Data which has been pre-encrypted with above key for comparison.

Referenced by run_aes_decryption_test(), and run_aes_encryption_test().