Atmel Software Framework

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

Unit tests for Non Volatile Memory controller driver.

Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.

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

Functions

int main (void)
 Run DES driver unit tests. More...
 
Unit test functions
static void run_des_encryption_and_decryption_test (const struct test_case *test)
 Run DES encryption and decryption in series. More...
 
static void run_3des_encryption_and_decryption_test (const struct test_case *test)
 Run 3DES encryption and decryption in series. More...
 
static void run_des_encryption (const struct test_case *test)
 Run DES encryption with known result. More...
 
static void run_des_decryption (const struct test_case *test)
 Run DES decryption with known result. More...
 
static void run_3des_encryption (const struct test_case *test)
 Run 3DES encryption with known result. More...
 
static void run_3des_decryption (const struct test_case *test)
 Run 3DES decryption with known result. More...
 
static void run_des_cbc_encryption_test (const struct test_case *test)
 Run DES CBC encryption on three data blocks, with known result. More...
 
static void run_des_cbc_decryption_test (const struct test_case *test)
 Run DES CBC decryption on three data blocks, with known result. More...
 
static void run_3des_cbc_encryption_test (const struct test_case *test)
 Run 3DES CBC encryption on three data blocks, with known result. More...
 
static void run_3des_cbc_decryption_test (const struct test_case *test)
 Run 3DES CBC decryption on three data blocks, with known result. More...
 

Variables

Variables and expected results
uint8_t data [DES_BLOCK_LENGTH]
 Data which will be encrypted. More...
 
uint8_t data_cbc [DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
 Data which will be encrypted with CBC. More...
 
uint8_t pre_des_encrypted_data [DES_BLOCK_LENGTH]
 Pre-DES-encrypted result of the original data. More...
 
uint8_t pre_3des_encrypted_data [DES_BLOCK_LENGTH]
 Pre-3DES-encrypted result of the original data. More...
 
uint8_t pre_des_cbc_encrypted_data [DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
 Pre CBC-DES-encrypted result of data_cbc. More...
 
uint8_t pre_3des_cbc_encrypted_data [DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
 Pre CBC-3DES-encrypted result of data_cbc. More...
 
uint8_t init [DES_BLOCK_LENGTH]
 Initialization vector used for starting CBC. More...
 
uint8_t keys [DES_BLOCK_LENGTH *DES_KEY_COUNT]
 Keys used for the 3DES-encryption. Only first key for DES. 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 (const uint8_t *block_1, const uint8_t *block_2, uint8_t block_size)
 Compare two data blocks of given size. More...
 

static bool compare_data_block ( const uint8_t *  block_1,
const uint8_t *  block_2,
uint8_t  block_size 
)
static

Compare two data blocks of given size.

Note
Compares two blocks of equal size to see if they are equal.
Parameters
block_1Pointer to first block.
block_2Pointer to second block.
block_sizeSize of blocks to compare.

Referenced by run_3des_cbc_decryption_test(), run_3des_cbc_encryption_test(), run_3des_decryption(), run_3des_encryption(), run_3des_encryption_and_decryption_test(), run_des_cbc_decryption_test(), run_des_cbc_encryption_test(), run_des_decryption(), run_des_encryption(), and run_des_encryption_and_decryption_test().

static void run_3des_cbc_decryption_test ( const struct test_case test)
static

Run 3DES CBC decryption on three data blocks, with known result.

Note
This test runs 3DES CBC decryption on the pre-encrypted data, using a known init vector. It then compares with the original data to verify the decryption.
Parameters
testCurrent test case.

References compare_data_block(), data_cbc, DES_BLOCK_LENGTH, des_cbc_decrypt(), init, keys, pre_3des_cbc_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_3des_cbc_encryption_test ( const struct test_case test)
static

Run 3DES CBC encryption on three data blocks, with known result.

Note
This test runs 3DES CBC encryption on three blocks of data, using a known init vector. It then compares with pre-calculated data to verify the encryption.
Parameters
testCurrent test case.

References compare_data_block(), data_cbc, DES_BLOCK_LENGTH, des_cbc_encrypt(), init, keys, pre_3des_cbc_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_3des_decryption ( const struct test_case test)
static

Run 3DES decryption with known result.

Note
This test decrypts the known pre-encrypted data block and compares it to the data which it was created from.
Parameters
testCurrent test case.

References compare_data_block(), data, des_3des_decrypt(), DES_BLOCK_LENGTH, keys, pre_3des_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_3des_encryption ( const struct test_case test)
static

Run 3DES encryption with known result.

Note
This test encrypts a data block, and compares it with a known pre-encrypted block from the same data, and compares them.
Parameters
testCurrent test case.

References compare_data_block(), data, des_3des_encrypt(), DES_BLOCK_LENGTH, keys, pre_3des_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_3des_encryption_and_decryption_test ( const struct test_case test)
static

Run 3DES encryption and decryption in series.

Note
This test encrypts a known data block with known keys, then decrypts it with the same keys, and compares the result with the original data.
Parameters
testCurrent test case.

References compare_data_block(), data, des_3des_decrypt(), des_3des_encrypt(), DES_BLOCK_LENGTH, keys, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_des_cbc_decryption_test ( const struct test_case test)
static

Run DES CBC decryption on three data blocks, with known result.

Note
This test runs DES CBC decryption on the pre-encrypted data, using a known init vector. It then compares with the original data to verify the decryption.
Parameters
testCurrent test case.

References compare_data_block(), data_cbc, DES_BLOCK_LENGTH, des_cbc_decrypt(), init, keys, pre_des_cbc_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_des_cbc_encryption_test ( const struct test_case test)
static

Run DES CBC encryption on three data blocks, with known result.

Note
This test runs DES CBC encryption on three blocks of data, using a known init vector. It then compares with pre-calculated data to verify the encryption.
Parameters
testCurrent test case.

References compare_data_block(), data_cbc, DES_BLOCK_LENGTH, des_cbc_encrypt(), init, keys, pre_des_cbc_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_des_decryption ( const struct test_case test)
static

Run DES decryption with known result.

Note
This test decrypts the known pre-encrypted data block and compares it to the data which it was created from.
Parameters
testCurrent test case.

References compare_data_block(), data, DES_BLOCK_LENGTH, des_decrypt(), keys, pre_des_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_des_encryption ( const struct test_case test)
static

Run DES encryption with known result.

Note
This test encrypts a data block, and compares it with a known pre-encrypted block from the same data, and compares them.
Parameters
testCurrent test case.

References compare_data_block(), data, DES_BLOCK_LENGTH, des_encrypt(), keys, pre_des_encrypted_data, set_buffer, success, and test_assert_true.

Referenced by main().

static void run_des_encryption_and_decryption_test ( const struct test_case test)
static

Run DES encryption and decryption in series.

Note
This test encrypts a known data block with a known key, then decrypts it with the same key, and compares the result with the original data.
Parameters
testCurrent test case.

References compare_data_block(), data, DES_BLOCK_LENGTH, des_decrypt(), des_encrypt(), keys, set_buffer, success, and test_assert_true.

Referenced by main().

uint8_t data[DES_BLOCK_LENGTH]
Initial value:
= {
0x41, 0x56, 0x52, 0x78, 0x6D, 0x65, 0x67, 0x61}

Data which will be encrypted.

uint8_t data_cbc[DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
Initial value:
= {
0x41, 0x56, 0x52, 0x78, 0x6D, 0x65, 0x67, 0x61,
0x25, 0x52, 0x11, 0x94, 0x00, 0x10, 0x99, 0xAC,
0xB2, 0xF0, 0xE3, 0xAE, 0xC7, 0x08, 0x43, 0x10}

Data which will be encrypted with CBC.

Referenced by run_3des_cbc_decryption_test(), run_3des_cbc_encryption_test(), run_des_cbc_decryption_test(), and run_des_cbc_encryption_test().

uint8_t init[DES_BLOCK_LENGTH]
Initial value:
= {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}

Initialization vector used for starting CBC.

uint8_t keys[DES_BLOCK_LENGTH *DES_KEY_COUNT]
Initial value:
= {
0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2,
0xFE, 0x98, 0x6C, 0x38, 0x23, 0xFC, 0x2D, 0x23}

Keys used for the 3DES-encryption. Only first key for DES.

uint8_t pre_3des_cbc_encrypted_data[DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
Initial value:
= {
0x97, 0xAF, 0x72, 0x58, 0xAE, 0xB5, 0xA4, 0x14,
0x97, 0x52, 0x2B, 0xD2, 0x78, 0x22, 0x5C, 0x1C,
0x06, 0x21, 0x59, 0x14, 0x29, 0x50, 0x4B, 0x41}

Pre CBC-3DES-encrypted result of data_cbc.

Referenced by run_3des_cbc_decryption_test(), and run_3des_cbc_encryption_test().

uint8_t pre_3des_encrypted_data[DES_BLOCK_LENGTH]
Initial value:
= {
0x89, 0x24, 0x41, 0x15, 0x1C, 0x68, 0x27, 0x2B}

Pre-3DES-encrypted result of the original data.

Referenced by run_3des_decryption(), and run_3des_encryption().

uint8_t pre_des_cbc_encrypted_data[DES_BLOCK_LENGTH *DES_CHAIN_BLOCK_COUNT]
Initial value:
= {
0xA2, 0x4C, 0xB5, 0x45, 0xE2, 0x23, 0x8F, 0xC3,
0xF3, 0xD0, 0x8D, 0xC1, 0x5A, 0x46, 0x86, 0xC0,
0xEC, 0x6E, 0x66, 0x28, 0x9D, 0xBD, 0xD8, 0xC9}

Pre CBC-DES-encrypted result of data_cbc.

Referenced by run_des_cbc_decryption_test(), and run_des_cbc_encryption_test().

uint8_t pre_des_encrypted_data[DES_BLOCK_LENGTH]
Initial value:
= {
0x3F, 0x9F, 0x66, 0xDA, 0xB7, 0x55, 0xF6, 0x6E}

Pre-DES-encrypted result of the original data.

Referenced by run_des_decryption(), and run_des_encryption().