DES Cryptographic modules
logo slogan

Segger emLib:DES

 

emLibThe emLib DES module allows encryption and decryption of data using DES, the Data Encryption Standard as published in 1976. This chapter describes the DES API functions and shows their usage based on example code.


What is DES?


The Data Encryption Standard, short DES, is a symmetric-key algorithm for en- and decryption of data. It was developed in the 1970's and established as a standard for the United States by the National Bureau of Standards (NBS, now NIST).

 

DES is a block cypher, taking a fixed-length block of data (64 bits). The key used for processing consists of 64 bits, where only 56 are actually used for transformations and 8 bits are used for parity checks.
DES performs an initial permutation of the data, 16 rounds of transformation, and a final permutation, the inverse of the initial permutation. In the transformations the data block is initially split in two 32 bit blocks where the first block is transformed with the round key using a Feistel cipher and XOR-linked with the second block. The first block and the resulting block are used for the next round.


emLib DES uses a key of 64 bits to encrypt a block of 68 bits of data at a time. To optimize the performance of the algorithms the generation of the round keys can be done before the actual encryption or decryption and used more than one time. DES can also be used in cipher block chaining (CBC) mode to process more than 64 bits.


In CBC mode every chunk of 64 bits is XOR linked with the result of the previous encryption (the cipher text), before being encrypted. To decrypt one block, all previous blocks have to be known.


For the encryption of the first block an initialization vector which will be linked with the block, can be used to make sure the first block cannot be brute-force decrypted by comparing it to common first data blocks.

 

Using emLib DES


The emLib DES module has a simple yet powerful API. It can be easily integrated into an existing application. The code is completely written in ANSI-C and MISRA-C compliant.

All functionality can be verified with standard test patterns using the Validation API functions. The functions for generating the tables used for higher optimization levels are also included for full transparency. To simply encrypt or decrypt data the application would only need to call one function.

If more than one block needs to be processed with the same key, a context containing the round keys calculated from the key can be prepared and directly used by the encryption and decryption functions. For more than one call of these functions this method results in a slightly higher processing speed.

 

DES API functions
The table below lists the available DES API functions.

 

Function

Description

DES_CBC_Encrypt()

Encrypts data with DES using CBC.

DES_CBC_Decrypt()

Decrypts data with DES using CBC.

DES_Decrypt()

Decrypts 8 Bytes with DES.

DES_Encrypt()

Encrypts 8 Bytes with DES.

DES_Prepare()

Prepares the context for de-/encryption.

DES_Validate()

Test function for validation of DES.


Detailed descriptions of all functions can be found in the emLib user manual.


Example code


DES en-/decryption of 16 Bytes using CBC


#include <DES.h>
int main(void) {
 	DES_CONTEXT Context;
 	const U8  aKey[8]        = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
 	const U8  aPlain[16]     = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xE7,
 								0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xE7};
 	U8  aRefPlain[16];
 	U8  aCipher[16];
 	int r;
 	//
 	// Prepare the DES Context with aKey
 	//
 	DES_PrepareKey(&Context, &aKey[0]);
 	//
 	// Encrypt the data of aPlain
 	//
 	DES_CBC_Encrypt(&Context, &aCipher[0], &aPlain[0], sizeof(aPlain), NULL);
 	//
 	// Decrypt the data of aCipher
 	//
 	DES_CBC_Decrypt(&Context, &aRefPlain[0], &aCipher[0], sizeof(aCipher), NULL);
 	r = memcmp(&aPlain[0], &aRefPlain[0], sizeof(aRefPlain));
 	if (r != 0) 
	{
 		return -2;
 	}
	return r; // DES works fine.
 }

 

Sample applications


emLib includes some sample applications to show the modules functionality and provide an easy to use starting point for your application. The application's source code is included within the module. The following applications are included in emLib DES:

 

Application name

Target platform

Description

DESSpeedtest.exe

Windows Console

application testing the speed of emLib DES.

DESValidate.exe

Windows Console

application validating emLib DES with standard test patterns.


Download emLib DES sample applications click here

 

DESSpeedTest


DESSpeedtest is a windows application, testing the performance of the emLib DES algorithms.



DESValidate


DESValidate is a Windows application used to test and validate the implementation of the DES algorithms.
The application uses the Validation API and compares the results of encryption and decryption with the expected results. DESValidate will show an error message, if a validation test fails.

 

Performance and memory footprint


emLib DES aims for portability and is designed to fit speed and size requirements for different targets.


Performance test


The following system has been used to measure the performance and memory footprint of the module with different optimization levels.

 

Performance test

The following system has been used to measure the performance and memory footprint of the module with different optimization levels.

 

Detail

Description

Target

STM32F417 running at 168 MHz, internal flash used

Tool chain

IAR EWARM V6.40E

 

Results

The following table shows the en- and decryption speed of emLib DES:

 

Compiler options

Speed

ROM usage

Optimize high for speed

~0.8 MByte/sec

~3.2 KBytes

Optimize high for size

~0.6 MByte/sec

~3.0 KBytes

The performance depends on the MCU speed and the flash memory speed. Results may vary if a different setup is used.