Timing attack against DES 0.1
Functions | Variables
des.h File Reference

The des library, a software library dedicated to the Data Encryption Standard (DES). More...

#include <stdint.h>
#include <inttypes.h>

Go to the source code of this file.

Functions

int hamming_weight (uint64_t val)
 Returns the Hamming weight of a 64 bits word.
 
int hamming_distance (uint64_t val1, uint64_t val2)
 Returns the Hamming distance between two 64 bits words.
 
uint64_t des_ip (uint64_t val)
 Initial permutation (64 to 64 bits).
 
uint64_t des_n_ip (uint64_t val)
 Inverse of initial permutation (64 to 64 bits).
 
uint64_t des_fp (uint64_t val)
 Final permutation (64 to 64 bits).
 
uint64_t des_n_fp (uint64_t val)
 Inverse of final permutation (64 to 64 bits).
 
uint64_t des_e (uint64_t val)
 E expansion - permutation (32 to 48 bits).
 
uint64_t des_n_e (uint64_t val)
 Inverse of E expansion - permutation (48 to 32 bits).
 
uint64_t des_p (uint64_t val)
 P permutation (32 to 32 bits).
 
uint64_t des_n_p (uint64_t val)
 Inverse of P permutation (32 to 32 bits).
 
uint64_t des_pc1 (uint64_t val)
 PC1 permutation - selection (64 to 56 bits).
 
uint64_t des_set_parity_bits (uint64_t val)
 Computes the 8 parity bits of a 64 bits word.
 
uint64_t des_n_pc1 (uint64_t val)
 Inverse of PC1 permutation - selection (56 to 64 bits).
 
uint64_t des_pc2 (uint64_t val)
 PC2 permutation - selection (56 to 48 bits).
 
uint64_t des_n_pc2 (uint64_t val)
 Inverse of PC2 permutation - selection (48 to 56 bits).
 
uint64_t des_sbox (int sbox, uint64_t val)
 Single SBox computation (6 to 4 bits).
 
uint64_t des_sboxes (uint64_t val)
 All SBoxes computation (48 to 32 bits).
 
uint64_t des_right_half (uint64_t val)
 Returns the 32 bits right half of a 64 bits word.
 
uint64_t des_left_half (uint64_t val)
 Returns the 32 bits left half of a 64 bits word.
 
uint64_t des_ls (uint64_t val)
 Applies the left shift rotation of the standard (56 to 56 bits).
 
uint64_t des_rs (uint64_t val)
 Applies the right shift rotation of the standard (56 to 56 bits).
 
uint64_t des_f (uint64_t rk, uint64_t val)
 The F function of the standard (48+32 bits to 32 bits).
 
void des_ks (uint64_t *ks, uint64_t val)
 Computes the whole key schedule from a 64 bits secret key and stores the sixteen 48 bits round keys in an array.
 
uint64_t des_enc (uint64_t *ks, uint64_t val)
 Enciphers a 64 bits plaintext with a pre-computed key schedule.
 
uint64_t des_dec (uint64_t *ks, uint64_t val)
 Deciphers a 64 bits plaintext with a pre-computed key schedule.
 
int des_check (void)
 A functional verification of the DES implementation.
 

Variables

uint8_t left_shifts [16]
 Number of left shifts per round.
 

Detailed Description

The des library, a software library dedicated to the Data Encryption Standard (DES).

Author
Renaud Pacalet, renau.nosp@m.d.pa.nosp@m.calet.nosp@m.@tel.nosp@m.ecom-.nosp@m.pari.nosp@m.s.fr
Date
2009-07-08
Attention
  • Most functions of the library check their input parameters and issue warnings or errors when they carry illegal values. Warnings are printed on the standard error output. Errors are also printed on the standard error output and the program exits with a -1 exit status.
  • The library uses a single data type to represent all the data of the DES standard: uint64_t. It is a 64 bits unsigned integer.
  • Data are always right aligned: when the data width is less than 64 bits, the meaningful bits are always the rightmost bits of the uint64_t.

Function Documentation

◆ hamming_weight()

int hamming_weight ( uint64_t  val)

Returns the Hamming weight of a 64 bits word.

Note: the input's width can be anything between 0 and 64, as long as the unused bits are all zeroes.

Returns
The Hamming weight of the input as a 64 bits uint64_t.
Parameters
valThe 64 bits input.

◆ hamming_distance()

int hamming_distance ( uint64_t  val1,
uint64_t  val2 
)

Returns the Hamming distance between two 64 bits words.

Note: the width of the inputs can be anything between 0 and 64, as long as they are the same, aligned and that the unused bits are all zeroes.

Returns
The Hamming distance between the two inputs as a 64 bits uint64_t.
Parameters
val1The first 64 bits input.
val2The second 64 bits input.

◆ des_ip()

uint64_t des_ip ( uint64_t  val)

Initial permutation (64 to 64 bits).

Same as des_n_fp().

Returns
The permutated input as a 64 bits uint64_t.
Parameters
valThe 64 bits input.

◆ des_n_ip()

uint64_t des_n_ip ( uint64_t  val)

Inverse of initial permutation (64 to 64 bits).

Same as des_fp().

Returns
The permutated input as a 64 bits uint64_t.
Parameters
val64 bits input.

◆ des_fp()

uint64_t des_fp ( uint64_t  val)

Final permutation (64 to 64 bits).

Inverse of initial permutation, same as des_n_ip().

Returns
The permutated input as a 64 bits uint64_t.
Parameters
val64 bits input.

◆ des_n_fp()

uint64_t des_n_fp ( uint64_t  val)

Inverse of final permutation (64 to 64 bits).

Same as des_ip().

Returns
The permutated input as a 64 bits uint64_t.
Parameters
val64 bits input.

◆ des_e()

uint64_t des_e ( uint64_t  val)

E expansion - permutation (32 to 48 bits).

Returns
The expanded and permutated input as a 48 bits uint64_t.
Parameters
val32 bits input.

◆ des_n_e()

uint64_t des_n_e ( uint64_t  val)

Inverse of E expansion - permutation (48 to 32 bits).

Duplicated bits must have the same value. If they do not, an error is raised.

Returns
The permutated and selected input as a 32 bits uint64_t.
Parameters
val48 bits input.

◆ des_p()

uint64_t des_p ( uint64_t  val)

P permutation (32 to 32 bits).

Returns
The permutated input as a 32 bits uint64_t.
Parameters
val32 bits input.

◆ des_n_p()

uint64_t des_n_p ( uint64_t  val)

Inverse of P permutation (32 to 32 bits).

Returns
The permutated input as a 32 bits uint64_t.
Parameters
val32 bits input.

◆ des_pc1()

uint64_t des_pc1 ( uint64_t  val)

PC1 permutation - selection (64 to 56 bits).

No parity check.

Returns
The permutated and selected input as a 56 bits uint64_t.
Parameters
val64 bits input.

◆ des_set_parity_bits()

uint64_t des_set_parity_bits ( uint64_t  val)

Computes the 8 parity bits of a 64 bits word.

Parity bits are the rightmost bit of each byte. Once computed, the number of set bits of each byte is odd, as specified in the DES standard.

Returns
The input with odd parity bits, as a 64 bits uint64_t.
Parameters
val64 bits input.

◆ des_n_pc1()

uint64_t des_n_pc1 ( uint64_t  val)

Inverse of PC1 permutation - selection (56 to 64 bits).

Parity bits are computed.

Returns
The permutated and expanded input as a 64 bits uint64_t.
Parameters
val56 bits input.

◆ des_pc2()

uint64_t des_pc2 ( uint64_t  val)

PC2 permutation - selection (56 to 48 bits).

Returns
The permutated and selected input as a 48 bits uint64_t.
Parameters
val56 bits input.

◆ des_n_pc2()

uint64_t des_n_pc2 ( uint64_t  val)

Inverse of PC2 permutation - selection (48 to 56 bits).

Missing bits are set to 0.

Returns
The permutated and expanded input as a 56 bits uint64_t.
Parameters
val48 bits input.

◆ des_sbox()

uint64_t des_sbox ( int  sbox,
uint64_t  val 
)

Single SBox computation (6 to 4 bits).

Returns
The 4 bits output of SBox number sbox corresponding to the 6 bits input, as a 4 bits uint64_t.
Parameters
sboxSBox number, from 1 to 8.
val6 bits input.

◆ des_sboxes()

uint64_t des_sboxes ( uint64_t  val)

All SBoxes computation (48 to 32 bits).

Returns
The 32 bits output of all SBoxes corresponding to the 48 bits input, as a 32 bits uint64_t.
Parameters
val48 bits input.

◆ des_right_half()

uint64_t des_right_half ( uint64_t  val)

Returns the 32 bits right half of a 64 bits word.

Returns
The 32 bits right half of a 64 bits word, as a 32 bits uint64_t.
Parameters
val64 bits input.

◆ des_left_half()

uint64_t des_left_half ( uint64_t  val)

Returns the 32 bits left half of a 64 bits word.

Returns
The 32 bits left half of a 64 bits word, as a 32 bits uint64_t.
Parameters
val64 bits input.

◆ des_ls()

uint64_t des_ls ( uint64_t  val)

Applies the left shift rotation of the standard (56 to 56 bits).

Returns
The rotated input, as a 56 bits uint64_t.
Parameters
val56 bits input.

◆ des_rs()

uint64_t des_rs ( uint64_t  val)

Applies the right shift rotation of the standard (56 to 56 bits).

Returns
The rotated input, as a 56 bits uint64_t.
Parameters
val56 bits input.

◆ des_f()

uint64_t des_f ( uint64_t  rk,
uint64_t  val 
)

The F function of the standard (48+32 bits to 32 bits).

Returns
The transformed input, as a 32 bits uint64_t.
Parameters
rk48 bits round key.
val32 bits data input.

◆ des_ks()

void des_ks ( uint64_t *  ks,
uint64_t  val 
)

Computes the whole key schedule from a 64 bits secret key and stores the sixteen 48 bits round keys in an array.

The sixteen 48 bits round keys are returned in the array passed as first parameter.

Parameters
ksThe array where to store the sixteen 48 bits round keys. On return ks[0] holds the first round key, ..., ks[15] holds the last round key. Must be allocated prior the call.
val64 bits key.

◆ des_enc()

uint64_t des_enc ( uint64_t *  ks,
uint64_t  val 
)

Enciphers a 64 bits plaintext with a pre-computed key schedule.

Returns
The enciphered plaintext as a 64 bits uint64_t.
Parameters
ksThe pre-computed key schedule.
valThe 64 bits plaintext.

◆ des_dec()

uint64_t des_dec ( uint64_t *  ks,
uint64_t  val 
)

Deciphers a 64 bits plaintext with a pre-computed key schedule.

Returns
The deciphered ciphertext as a 64 bits uint64_t.
Parameters
ksThe pre-computed key schedule.
valThe 64 bits ciphertext.

◆ des_check()

int des_check ( void  )

A functional verification of the DES implementation.

Runs a number of encipherments with des_enc() and the corresponding decipherments with des_dec() and checks the results against pre-computed plaintext, ciphertexts and secret keys. If compiled in DEBUG mode, prints warnings on mismatches or a OK message if the tests pass.

Returns
One on success, zero on errors.

Variable Documentation

◆ left_shifts

uint8_t left_shifts[16]
extern

Number of left shifts per round.

left_shifts[0] corresponds to round #1, ... left_shifts[15] corresponds to round #16. A value of 0 means one shift. A value of 1 means two shifts.