
This is a straightforward implementation of the 1977 FIPS Data Encryption Standard (DES) algorithm. It functions as a SPI (Mode 0) peripheral, to encrypt or decrypt a single 64-bit data block using a 64-bit symmetric key, returning a 64-bit result.
NB: do not use DES where any real security is required. The key length is too short to protect against brute force attacks on modern hardware.
DES is specified as a purely combinational function which theoretically could encrypt or decrypt a block in a single clock cycle. That would require a large circuit area and probably pipelining in order to run at a reasonable clock rate. The algorithm is defined as a composition of 16 rounds of permutation and substitution operations, all identical except that the key is shifted circularly into a different position for each round. Therefore it is natural to implement it with a sequential circuit, with one register holding the current key value to be shifted at each step, and one register holding the current state of the data block to be updated by permutation and substitution functions into the next state.
After the complete encryption of one data block, the key register has been shifted back into its original position, so another block could be loaded via the SPI interface and encrypted without having to reload the key. Similarly, the encrypted block remains in the data register so the correctness of the encryption could be tested by immediately decrypting the result, without having to reload the key.
The SPI interface is used to send 9-byte requests, while simultaneously receiving a 9-byte response to the previous request. The request consists of a 1-byte command (see table below), and 8 bytes of data which may be a key, a block of plaintext to be encrypted, or a block of ciphertext to be decrypted. Data is sent with most significant bit first. The response consists of an unused leading byte, and 8 bytes of encrypted or decrypted result.
Command byte definitions:
| Bit | Name | Meaning |
|---|---|---|
0x01 |
KEY | Data is a 64-bit key |
0x02 |
BLOCK | Data is a 64-bit plaintext or ciphertext block |
0x04 |
ENCRYPT | Encryption request |
0x08 |
DECRYPT | Decryption request |
Thus encryption of a block takes at least three SPI transactions:
(KEY, key value)(DATA|ENCRYPT, plaintext block)(0, 0) and receive ciphertext blockTriple DES (3DES or TDES) is an extension of DES with a 128-bit key, using one half of the key for each of three sequential DES operations, making it (theoretically) much more resistant to brute force attacks. A 3DES encryption of one block can be done in five SPI transactions, as follows:
(DATA, plaintext)(KEY|ENCRYPT, key 1)(KEY|DECRYPT, key 2)(KEY|ENCRYPT, key 1)(0, 0) and receive ciphertext blockConnect to any client processor which has a SPI master interface (eg an Arduino or Raspberry Pi). Or use the Micropython implementation on the Tiny Tapeout dev board (see the cocotb test as a pattern).
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | SS | ||
| 1 | MOSI | ||
| 2 | MISO | ||
| 3 | SCLK | ||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 |