Overview
This project provides a fully functional, synthesizable AES-128 encryption/decryption written in SystemVerilog. The implementation follows the FIPS-197 standard.
The algorithm consists of:
- Initial round: AddRoundKey only
- Rounds 1-9: SubBytes → ShiftRows → MixColumns → AddRoundKey
- Final round: SubBytes → ShiftRows → AddRoundKey
Each round uses a unique round key generated by the key scheduler, ensuring identical plaintext blocks produce different intermediate states.
Quick Start
Prerequisites
- ModelSim/QuestaSim
- Python 3.7+
- PyCryptodome
Running Tests
Generate Test Vectors
Run Test Suite
Run Individual Tests
Sbox/Inverse Sbox Test
MixColumns/Inverse MixColumns test
Key scheduling/Inverse KS test
Full AES/Inverse AES test
Architecture Details
State Machine
My implementation uses a 4-bit finite state machine that elegantly maps to the AES round structure:
State | Value | Description |
---|---|---|
IDLE | 4'h0 | Waiting for input data |
Round 1-9 | 4'h1-4'h9 | Middle rounds (full transformations) |
FINAL | 4'hA | Final round (no MixColumns) |
DONE | 4'hB | Output result |
This design uses combinational logic for all transformations, achieving single-cycle execution per round.
Data Flow
- Initial Round: AddRoundKey only
- Rounds 1-9: SubBytes → ShiftRows → MixColumns → AddRoundKey
- Final Round: SubBytes → ShiftRows → AddRoundKey
- Output: Result available with valid signal
Core Transformations
SubBytes: S-box Implementation
Rather than using lookup tables, I implemented the S-box using optimized combinational logic based on Boyar & Peralta's 2009 research. This approach reduces area and eliminates memory dependencies:
The S-box module contains 67 internal logic terms implementing the mathematical transformation in GF(2^8), resulting in a compact design.
ShiftRows: Byte Permutation
ShiftRows is implemented as a pure wire remapping, making it zero-cost in hardware:
MixColumns: Galois Field Arithmetic
MixColumns performs matrix multiplication in GF(2^8). The key insight is that all operations can be implemented using XOR and a single multiplication by 2 (xtime):
Four parallel MixColumns units process all columns simultaneously, maintaining the single-cycle-per-round performance.
AddRoundKey
The simplest transformation is just XOR with the round key:
The multiplexer logic handles the different data paths for initial input, middle rounds, and final round.
Key Scheduler Implementation
The key scheduler generates round keys using three operations: RotWord, SubWord, and Rcon XOR:
The Rcon values follow a specific pattern in GF(2^8) implemented as a simple lookup function.
Decryption
Decryption uses inverse transformations in reverse order. The key insight is that the inverse operations can reuse much of the forward encryption logic:
- InvSubBytes: Uses inverse S-box lookup table
- InvShiftRows: Right shifts instead of left shifts
- InvMixColumns: Matrix multiplication with inverse matrix
- AddRoundKey: Same operation (XOR is self-inverse)
The inverse key scheduler works backward from the final round key to generate previous round keys:
Verification and Testing
I created a Python script using PyCryptodome as a golden model to generate a small set of test vectors:
Test Suite Architecture
The verification environment includes:
- Component-level tests: Individual S-box, MixColumns, and key scheduler verification
- System-level tests: Full encryption/decryption with NIST test vectors
- Corner case testing: All zeros, all ones, and custom patterns
- Roundtrip verification: Encrypt then decrypt to verify data integrity
Performance
Timing and Throughput
- Latency: 12 clock cycles (10 encryption rounds + 2 control cycles)
- Throughput: 1.067 Gbps @ 100 MHz
- Efficiency: 10.67 bits per clock cycle
Resource Utilization
The design optimizes for balanced area/performance:
- Logic elements: Moderate usage due to combinational S-box
- Memory: Zero block RAM usage (no lookup tables)
- DSP blocks: Not required
- Routing: Clean due to regular structure
Resources
- NIST FIPS 197 AES and its test vectors
- AES Animation
- Galois Field Arithmetic
- Rijndael Block Cipher: AES
- Boyar & Peralta (2009): "A new combinational logic minimization technique with applications to cryptology"
⚠️ Disclaimer: This implementation is for educational and research purposes. Production cryptographic systems require additional security analysis and validation.