PRESENT Block Cipher Implementation
Implementation of PRESENT, an ultra-lightweight block cipher designed for resource-constrained devices like RFID tags and IoT sensors.
PRESENT: Ultra-Lightweight Block Cipher
Lab 4 implements PRESENT, one of the most efficient lightweight block ciphers designed specifically for extremely constrained environments such as RFID tags, smart cards, and IoT devices. This project explores the elegant Substitution-Permutation Network (SPN) architecture that balances security and minimal hardware footprint, making PRESENT an ISO/IEC standard for lightweight cryptography.
What is PRESENT?
PRESENT is a symmetric block cipher introduced in 2007 by Andrey Bogdanov, Lars R. Knudsen, and others at the CHES (Cryptographic Hardware and Embedded Systems) conference. It was specifically designed to address the growing need for secure encryption in resource-constrained devices where traditional ciphers like AES would be too expensive in terms of power consumption, chip area, and processing requirements.
The cipher operates on 64-bit blocks and supports key sizes of 80 bits or 128 bits. PRESENT uses a simple yet effective Substitution-Permutation Network structure consisting of 31 rounds, where each round applies three operations: XOR with a round key, substitution through an S-box, and permutation through a P-layer. This design philosophy prioritizes hardware efficiency while maintaining strong security properties against known cryptanalytic attacks.
- Block size: 64 bits (8 bytes)
- Key sizes: 80 bits or 128 bits
- Number of rounds: 31
- Structure: Substitution-Permutation Network (SPN)
- S-box: 4-bit to 4-bit substitution (16 S-boxes applied in parallel)
- P-layer: Bit permutation for diffusion
- Hardware-optimized: Requires only 1000-1500 gate equivalents
- ISO/IEC 29192-2:2019 standard for lightweight cryptography
Why Lightweight Cryptography?
As the Internet of Things expands, billions of resource-constrained devices need cryptographic protection. Traditional ciphers like AES-128, while secure and efficient on standard computers, require significant resources when implemented in hardware. PRESENT achieves comparable security levels while using approximately 70% less hardware area than AES. This makes it ideal for applications like contactless payment cards, medical implants, sensor networks, and RFID tags where power consumption, physical size, and manufacturing cost are critical constraints.
Technologies Used
Key Features
Complete PRESENT cipher encryption implementation
PRESENT cipher decryption functionality
Support for both 80-bit and 128-bit keys
S-box substitution layer (16 parallel 4-bit S-boxes)
P-layer permutation for bit diffusion
Round key generation algorithm
Showing 6 of 13 features
Implementation Details
The S-Box: Substitution Layer
The S-box (Substitution Box) is the only non-linear component in PRESENT and provides confusion in the cipher. PRESENT uses a single 4-bit S-box applied 16 times in parallel to the 64-bit state. This means the 64-bit block is divided into 16 nibbles (4-bit chunks), and each nibble is independently substituted using the same S-box lookup table.
The PRESENT S-box was carefully designed with optimal cryptographic properties:
- Non-linearity: The S-box is highly non-linear to resist linear cryptanalysis
- Differential uniformity: Minimizes probability of differential attacks
- Algebraic complexity: High algebraic degree prevents algebraic attacks
- Hardware efficiency: Can be implemented with minimal logic gates
- Balanced output: Each output bit appears equally often for all inputs
The S-box mapping is defined as a lookup table where input nibble x (0-15) maps to output S[x]. For example, input 0x0 maps to 0xC, input 0x1 maps to 0x5, and so on. This substitution breaks the mathematical relationship between plaintext and ciphertext, making the cipher resistant to linear attacks.
In each round, after XORing the state with the round key, all 16 nibbles undergo S-box substitution simultaneously. This parallel application makes hardware implementation extremely efficient while providing strong confusion properties across the entire 64-bit block.
Code Explanation
The S-box is implemented as a simple lookup table. The apply_sbox function processes the 64-bit state by extracting each 4-bit nibble, substituting it through the S-box, and reconstructing the output. The inverse S-box reverses this operation for decryption. This design achieves maximum hardware efficiency while providing strong non-linearity.
The P-Layer: Permutation for Diffusion
The P-layer (Permutation Layer) provides diffusion in PRESENT by rearranging the bit positions of the 64-bit state. While the S-box provides confusion by substituting values, the P-layer ensures that changes propagate throughout the entire block quickly. This is essential for the avalanche effect: a single bit change in the input should affect approximately half the output bits after a few rounds.
PRESENT uses a bit permutation where bit i moves to position P(i). The permutation is carefully designed such that the output bits of one S-box in one round are distributed across different S-boxes in the next round. This optimal diffusion ensures that after just a few rounds, each output bit depends on every input bit.
The permutation formula is P(i) = (i mod 16) × 4 + floor(i / 16), where i ranges from 0 to 63. This mathematical formula ensures:
- Optimal diffusion: Output bits from each S-box spread to different S-boxes
- Hardware efficiency: Can be implemented as simple wire crossings
- Invertibility: The permutation can be easily reversed for decryption
- No fixed points: No bit remains in the same position (except by mathematical coincidence)
- Full avalanche: After 2-3 rounds, changing one bit affects the entire state
In hardware, the P-layer costs almost nothing—it's just wires connecting different positions. In software, it requires bit extraction and reconstruction, but the simple mathematical formula makes it very efficient. The P-layer is applied after the S-box in each of the 31 rounds (except the final round, where it's omitted).
Code Explanation
The P-layer implementation uses pre-computed permutation tables for efficiency. The apply_permutation function extracts each bit from its original position and places it in the new position according to the permutation table. The bit extraction uses bitwise AND with 1, and bit placement uses bitwise OR with left shift. The inverse permutation reverses this operation for decryption.
ECB Mode Implementation
Electronic Codebook (ECB) is the simplest block cipher mode of operation. It divides the plaintext into fixed-size blocks and encrypts each block independently using the same key. For PRESENT with its 64-bit block size, this means processing data in 8-byte chunks.
In ECB mode, each 64-bit plaintext block is encrypted to produce a 64-bit ciphertext block using the PRESENT cipher with the provided key. The same plaintext block always encrypts to the same ciphertext block (with the same key), which is both a feature and a weakness of ECB mode.
Advantages and Limitations of ECB Mode:
- Simplicity: Easy to implement and understand
- Parallelizable: Blocks can be encrypted/decrypted independently
- No error propagation: Corruption in one block doesn't affect others
- Random access: Any block can be decrypted without decrypting others
- Pattern leakage: Identical plaintext blocks produce identical ciphertext
- Not semantically secure: Same plaintext always produces same ciphertext
- Vulnerable to block manipulation: Blocks can be reordered or replaced
The ECB wrapper handles file I/O, converts data to 64-bit blocks, applies PRESENT encryption or decryption to each block, and writes the output. Padding is applied to the last block if necessary to ensure all blocks are exactly 64 bits.
Code Explanation
The ECB implementation reads the key from a hex file and converts it to an integer. The input file is read as binary data and divided into 64-bit (8-byte) blocks. Each block is converted to an integer using big-endian byte order. For encryption mode, each block is independently encrypted using present(block, key). For decryption, present_inv(block, key) is used. The last block is zero-padded if needed. After processing, all blocks are converted back to bytes and written to the output file. This demonstrates the core principle of ECB: independent, deterministic block-by-block encryption.
Breaking ECB: Pattern Extraction Attack
ECB mode has a critical security weakness: identical plaintext blocks always encrypt to identical ciphertext blocks. This property leaks structural information about the plaintext, even without knowing the encryption key. This vulnerability is most dramatically demonstrated with image encryption, where the visual structure of the image remains visible in the ciphertext - famously illustrated by the "ECB penguin" where an encrypted penguin image still shows the penguin's outline.
Why ECB Leaks Patterns:
- Deterministic mapping: Same plaintext block → same ciphertext block
- No chaining: Each block is encrypted independently
- Pattern preservation: Repeating patterns in plaintext create repeating patterns in ciphertext
- Frequency analysis: Block frequency in ciphertext matches plaintext frequency
- No diffusion across blocks: Changes in one block don't affect others
- Structure visibility: High-level structure and boundaries remain visible
The Attack Strategy:
When a bitmap image (PBM format) is encrypted with ECB mode, we can extract the image structure without decrypting the actual data or knowing the key. The attack works by recognizing that in a binary (black and white) image, there are only two possible plaintext block types: blocks of white pixels (often represented as 0) and blocks of black pixels (often represented as 1). By analyzing the frequency of ciphertext blocks, we can deduce which ciphertext block corresponds to which plaintext value.
For example, in an image with a white background, the "all white" plaintext block will appear very frequently. In the ciphertext, whichever ciphertext block appears most frequently likely corresponds to the white background. By mapping the most common ciphertext block to 0 and other blocks to 1, we can reconstruct the image structure and reveal the visual content without ever decrypting the data.
Code Explanation
The extraction attack exploits ECB's deterministic nature through a 4-step process: (1) Parse the PBM header to get image dimensions, (2) Read the encrypted file and divide it into 64-bit blocks, then perform frequency analysis using a defaultdict to count how many times each unique ciphertext block appears, (3) Map the most frequent ciphertext block to 0 (background) and all others to 1 (foreground) - this works because images typically have large uniform areas, (4) Reconstruct the binary image by replacing each ciphertext block with its mapped bit value and output as a PBM file. The attack succeeds without knowing the encryption key because ECB preserves patterns: identical plaintext blocks always produce identical ciphertext blocks. This demonstrates why ECB mode is fundamentally insecure for any data with patterns or structure, such as images, documents, or databases. Modern cipher modes like CBC, CTR, or GCM solve this by chaining blocks together or using initialization vectors, ensuring identical plaintext blocks encrypt to different ciphertext blocks.
Challenges
Correctly implementing bitwise permutations, ensuring proper key scheduling, handling 64-bit block operations efficiently, debugging subtle bit-manipulation errors, understanding the tradeoffs of ECB mode, exploiting ECB pattern leakage through frequency analysis.
Outcome & Impact
Successfully implemented PRESENT cipher with correct S-box and P-layer operations, achieved encryption/decryption matching test vectors, implemented ECB mode wrapper for file encryption, demonstrated ECB vulnerability by extracting image patterns without the key.
How I Grew
Mastered Substitution-Permutation Network (SPN) architecture
Learned principles of lightweight cryptography design
Developed expertise in bitwise operations and manipulation
Understood S-box substitution for confusion
Learned P-layer permutation for optimal diffusion
Gained insights into block cipher modes of operation
Understood ECB mode advantages and critical security limitations
Learned frequency analysis attacks on block ciphers
Demonstrated why ECB mode leaks structural information
Gained practical experience in cryptanalysis and cipher mode vulnerabilities
Learned to balance security and performance in resource-constrained contexts