Coe301 t241 Programming Assignment 2
Coe301 t241 Programming Assignment 2
1 Introduction
As computers are becoming more intrusive and prevalent in different facets of life, digital privacy and
security is evermore becoming important. To protect the integrity and privacy of data, encryption aims at
obscuring the data by performing multiple operations given a secret key. In its essence, data encryption is
meant to be computational expensive to prohibit exhaustive search of encryption keys. When designing a
compute system, data encryption is a very important operation, as it is used very often and incurs heavy
load on the compute system. In non-homomorphic encryption, an encrypted data is decrypted first to
perform various operations, and the result must be encrypted before it is stored again. To design an
efficient instruction set architectures (ISA), one must be familiar with the complexity of data encryption
and decryption, as it is a necessity in today’s digital world.
The goal of this programming assignment is to develop a library for simplified data encryption
standard (S-DES), which is a simplified encryption scheme of data encryption standard (DES) [7]. The
program will take as an input two strings: a password and a message. The program will create a key
from the password. The program will ask the user for the requested operation: either encryption or
decryption. Then, the program will use the implemented library of (S-DES) to either encrypt or decrypt
the given message. If the requested operation is encryption, then the provided message from the user is
treated as a string of 8-bit ASCII characters. On the other hand, if the requested operation is decryption,
then the provided message is treated as a hexadecimal number (the digits of the hexadecimal number
is represented as 8-bit ASCII characters), which needs to be parsed correctly. The result of either
encryption (a string of hexadecimal digits represented through 8-bit ASCII characters) or decryption (a
string of 8-bit ASCII characters) is finally printed to the user.
The rest of this document is structured as follows: Section 2 discusses the simplified data
encryption standard (S-DES), Section 3 provides an example C programming language implementation
of the simplified data encryption standard (S-DES) library, and Section 4 clarifies the deliverables and
the interface required for the program.
1
Data Key
Block
32 32
Initial
Permutation
32
32
Round 0
. 32
32
ROTR 4
Round 1
.
32 ROTR 4
. 32
Round 2
In Block
Round-Key
Generate Round-Key
32 ROTR 4
32
Round 3
32 ROTR 4
Sbox Sbox Sbox Sbox Sbox Sbox Sbox Sbox
32
Round 4
32
Out Block ROTR 4
32
. Round 5
32
. 32
ROTR 4
Round 6
. 32
ROTR 4
32
Round 7
32
Final
Permutation
32
Cipher
Block
Figure 1: S-DES Overview–General overview of the S-DES encryption scheme; to decrypt, the flow
goes from the bottom to the top; while to encrypt, the flow goes from the top to the bottom.
The S-DES is composed of four major functionalities: generating key from plain-text password,
generating round-key, permutation, and S-DES rounds. Figure 1 shows the overview of the S-DES
scheme. Input data to encrypt is divided into blocks of 32-bit, hence the block-cipher nature of S-DES.
If the data length is not divisible by 32, the data is padded with zeros. Each block is encrypted with
the S-DES scheme shown in Figure 1. To decrypt a cipher data, the S-DES scheme is applied in exact
reverse (i.e., starting from the bottom of the scheme towards the top).
2
Hexadecimal Digit
round num % 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9
1 14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6
2 4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14
3 11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3
Table 1: S-Box Substitution–This table shows the substitution for each hexadecimal digit (i.e., nibble)
of the block based on the round number modulo four.
1. The 8-bit ASCII of the character is XOR’ed with the 32-bit key (which is initialized to zero).
2.3 Permutation
Each block of data is first permutated (i.e., the bits of the block are shuffled) before being operated and
processed. At the end of the S-DES scheme, a reverse permutation is applied to restore the original order
of the bits. In S-DES, the initial permutation is a simple bit-reversal of the block; therefore, the reverse
of the initial permutation is the same (i.e., a simple bit-reseveral).
2.4 Round
In S-DES, the block will go through consecutive eight rounds of cipher; the input of each round is the
output of the previous round. First round takes its input from the initial permutation, while the last
round gives its output to the final permutation. In each round, the incoming block is XOR’ed with the
corresponding round-key (i.e., for round x, the incoming block is XOR’ed with round-key x). Then,
the XOR’ed block is passed through S-boxes (substitution boxes). S-boxes simply replace each 4-bit
hexadecimal digit (i.e., a nibble) of the block with a different value according to fixed dictionary. The
dictionary is shown in Table 1. There are four dictionaries–first four rounds use different dictionaries,
then the subsequent four rounds re-use the four dictionaries. The S-boxes replacement is what makes
the S-DES non-linear making it resilient against differential cryptanalysis.
3. The key is 56 bit rather than 32 bit. The 48-bit round-key is generated by dividing the 56 bits into
two 28-bit numbers; the halves are rotated left by either one bit or two bits, respectively. Then
a 48-bit round-key is selected from these 28-bit halves–24 bits from one half and 24 bits from the
other half. Each round will have different selection depending on predetermined permutation.
3
4. The round divides the 64-bit block to 32-bit halves. The round-key is XOR’ed with a one half. The
half is expanded into 48 bits by duplicating bits (known as expansion permutation). The S-boxes
replace each 6 bits into 4 bits. Instead of using a modulo of the round number to determine the
second dimension, the two outer bits (i.e., the most-significant and least-significant bits) are used
to address the second dimension. Finally, the outcome of the S-boxes is XOR’ed with the other
half of the block.
4
3.3 Permutation
In the S-DES scheme, both initial and final permutation are implemented the same (since a reverse of a
bit-reversal is a bit-reversal):
1 unsigned int sdes_perm ( unsigned int d ) {
2
3 unsigned int r = d ;
4
5 r = (( r & 0 xFFFF0000llu ) >> 16) | (( r & 0 x0000FFFFllu ) << 16) ;
6 r = (( r & 0 xFF00FF00llu ) >> 8) | (( r & 0 x00FF00FFllu ) << 8) ;
7 r = (( r & 0 xF0F0F0F0llu ) >> 4) | (( r & 0 x0F0F0F0Fllu ) << 4) ;
8 r = (( r & 0 xCCCCCCCCllu ) >> 2) | (( r & 0 x33333333llu ) << 2) ;
9 r = (( r & 0 xAAAAAAAAllu ) >> 1) | (( r & 0 x55555555llu ) << 1) ;
10
11 return r ;
12 }
The encryption routine for S-DES is just the application ot initial permutation, followed by
eight rounds (with generating their respective round-keys), and then concluding with final permutation:
1 unsigned int s d e s _ e n c r y p t _ b l o c k ( unsigned int x , unsigned int key ) {
2 unsigned int d = 0;
3
4 d = sdes_perm ( x ) ;
5 for ( int i = 0; i < 8; i ++) {
6 unsigned int roundkey = s d e s _ g e n _ r o u n d k e y (i , key ) ;
7 d = s d e s _ e n c r y p t _ r o u n d (i , d , roundkey ) ;
8 }
9 d = sdes_perm ( d ) ;
10
11 return d ;
12 }
5
The S-DES encryption rounds are simply an XOR followed by S-boxes:
1 unsigned int s d e s _ e n c r y p t _ r o u n d ( unsigned int i , unsigned int block , unsigned int rkey ) {
2 unsigned int cipher = 0;
3
4 cipher = b ;
5 cipher = cipher ^ rkey ; /* XOR */
6 cipher = s d e s _ e n c r y p t _ s b o x (i , cipher ) ; /* Substitution */
7
8 return cipher ;
9 }
First, we start with the a top function to break incoming hexadecimal data into blocks:
1 void sdes_decrypt ( char * _cipher , char * _clear , unsigned int key , unsigned int nblocks ) {
2 unsigned int * clear = ( unsigned int *) _clear ;
3 unsigned int * cipher = ( unsigned int *) _cipher ;
4
5 for ( int i = 0; i < nblocks ; i ++) {
6 /* Get a block from the string */
7 unsigned int e_block = cipher [ i ];
8
9 /* Encrypt */
10 unsigned int block = s d e s _ d e c r y p t _ b l o c k ( e_block , key ) ;
11 clear [ i ] = block ;
12 }
13 }
Please, note that the rounds are sequenced from 7 down to 0 (i.e., in opposite order of the
encryption)
6
Then, we implement a decryption round:
1 unsigned int s d e s _ d e c r y p t _ r o u n d ( unsigned int i , unsigned int ciph , unsigned int rkey ) {
2 unsigned int block = 0;
3
4 block = s d e s _ d e c r y p t _ s b o x (i , ciph ) ; /* Substitution */
5 block = block ^ rkey ; /* XOR */
6
7 return block ;
8 }
4 Deliverables
The main deliverable of the programming assignment is a MIPS32 assembly file that implements a
program. The program asks the user for requested operation (“e” for encryption and “d” for decryption).
The program then asks the user for the data to encrypt or decrypt; the data is a plain-text message for
encryption, whereas the data is a hexadecimal string (i.e., a string of hexadecimal digits) for decryption.
Finally, the program asks for plain-text password to use for encryption or decryption. An example of
how the inputs and outputs should be handled is shown in Figure 2. Please, pay attention to the format
of the output. The output of the program should be the last line with either: “Encrypted Message = ”
or “Decrypted Message = ”. The output strings should be between double-qoutes, as shown in Figure 2.
Testing and Verification–You should spend a lot of effort writing testing and verification
for your program. Make sure that you test every corner case you can think of. Also, verify your
implementation with different emulators. Possible emulators: SPIM, jsSPIM, or QEMU. As usual,
refrain from using pseudo-instructions as they are not universally supported by every assembler.
Figure 2: Example Output of The Program–Input is given in the exact order shown. The output
is expected to be the last line. Do not forget to wrap your messages with double-quotes.
7
References
[1] Eli Biham and Adi Shamir. “Differential Cryptanalysis of DES-Like Cryptosystem”. In: Journal of
Cryptology 4.1 (1991).
[2] Eli Biham and Adi Shamir. “Differential Cryptanalysis of the Full 16-Round DES”. In: Proc. of
Crypto (1992).
[3] Whitfield Diffie and Martin E. Hellman. “Special Feature: Exhaustive Cryptanalysis of the NBS
Data Encryption Standard”. In: Computer 10.6 (1977), pp. 74–84.
[4] Schaefer Edward F. “A Simplified Data Encryption Standard Algorithm”. In: Cryptologia 20.1
(1996), pp. 77–84.
[5] Horst Feistel. “Cryptography and Computer Privacy”. In: Scientific American 228.5 (1973), pp. 15–
23.
[6] Horst Feistel. “US3798359A: Block Cipher Cryptographic System”. In: US Patent (1971).
[7] National Bureau of Standard (NBS). “Data Encryption Standard”. In: Federal Information Process-
ing Standards Publication (FIPS PUB) 46 (1977).