A hardware-secured System-on-Chip (SoC) implementation featuring comprehensive security features for IoT applications
- Overview
- Key Features
- Architecture
- Security Features
- Getting Started
- Project Structure
- Memory Map
- Usage Examples
- Testing
- Documentation
- Contributing
- License
- Credits
This project implements a Secure RISC-V SoC designed for smart lock and IoT applications. The system provides hardware-assisted security features to protect against common attack vectors including firmware tampering, replay attacks, and unauthorized memory access.
- Hardware-enforced security through dedicated security modules
- Secure boot with cryptographic firmware verification
- Memory protection to prevent unauthorized access to sensitive data
- Anti-replay protection for secure communication protocols
- Production-ready implementation suitable for IoT devices
- Smart locks and access control systems
- IoT devices requiring secure firmware updates
- Embedded systems with security requirements
- Research and education in hardware security
- Hardware-enforced memory access control
- Region-based protection (Boot ROM, Firmware, Data, Key Store)
- Prevents unauthorized reads/writes to protected regions
- Privilege level enforcement
- HMAC-SHA256 firmware verification
- Hardware cryptographic accelerator
- Tamper detection and prevention
- Only signed firmware can execute
- Hardware monotonic counter (32-bit, lockable)
- LFSR-based nonce generator
- Nonce cache to prevent reuse
- Counter progression validation
- Blocks replay and out-of-order attacks
- SHA-256 hash computation
- HMAC-SHA256 message authentication
- Hardware-accelerated for performance
- Memory-mapped peripheral interface
- Protected memory region for cryptographic keys
- MPU-enforced access control
- Machine-mode only access
- Prevents key exfiltration
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Secure RISC-V SoC โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โ
โ โ PicoRV32 โโโโโโโโโบโ Memory Bus โ โ
โ โ CPU (RV32I) โ โ โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โ โผ โ
โ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ MPU โ โ
โ โ โ (Protection) โ โ
โ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โผ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Memory Subsystem โ โ
โ โ โข Boot ROM (4KB) โข Inst Mem (64KB) | โ
โ โ โข Data Mem (64KB) โข Key Store (256B) | โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Security Modules โ โ
โ โ โข Crypto Accelerator โข Anti-Replay โ โ
โ โ โข Monotonic Counter โข Nonce Generator โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโ โ
โ โ UART โ (Debug Console) โ
โ โโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- CPU: PicoRV32 (RISC-V RV32I ISA)
- Memory: Boot ROM, Instruction Memory, Data Memory
- Security: MPU, Crypto Accelerator, Anti-Replay Engine
- Peripherals: UART for debug output
- Interconnect: Memory-mapped bus architecture
The MPU enforces hardware-level memory access control:
- Boot ROM Protection: Read-only, execute-only
- Firmware Protection: Read/execute only (prevents modification)
- Key Store Protection: Machine-mode only access
- Region Isolation: Strict boundaries between memory regions
Attack Prevention: Prevents firmware modification, key exfiltration, and code injection.
The secure boot process ensures only authenticated firmware executes:
- Boot ROM loads HMAC key from protected Key Store
- Crypto accelerator calculates HMAC-SHA256 of firmware
- Compares with signature stored in firmware header
- Boots firmware only if signature matches
- Halts system if verification fails
Attack Prevention: Blocks tampered firmware, malware injection, and unauthorized code execution.
Protects against replay and out-of-order packet attacks:
- Monotonic Counter: Hardware counter that only increments
- Nonce Generator: LFSR-based unique nonce generation
- Nonce Cache: Tracks recent nonces to detect reuse
- Counter Validation: Ensures counter always progresses forward
Attack Prevention: Blocks replay attacks, out-of-order packets, and nonce reuse.
Hardware-accelerated cryptographic operations:
- SHA-256: FIPS 180-4 compliant hash function
- HMAC-SHA256: Keyed-hash message authentication
- Memory Interface: Reads data directly from memory
- Status Registers: Polling interface for completion
Benefits: Fast cryptographic operations without CPU overhead.
๐ก Tip: If you prefer not to install tools manually, use the Docker Setup section below. The Docker image includes all required tools pre-installed.
-
RISC-V Toolchain
# Ubuntu/Debian sudo apt-get install gcc-riscv64-unknown-elf # macOS (Homebrew) brew tap riscv/riscv brew install riscv-gnu-toolchain
-
Verilog Simulator (Icarus Verilog)
# Ubuntu/Debian sudo apt-get install iverilog # macOS brew install icarus-verilog
-
Python 3 (for build scripts)
# Usually pre-installed, verify with: python3 --version -
Make (build automation)
# Ubuntu/Debian sudo apt-get install build-essential
- GTKWave: Waveform viewer for debugging
sudo apt-get install gtkwave # Linux brew install gtkwave # macOS
-
Clone the repository
git clone https://github.com/AhsanRiaz786/secure-riscv-soc/edit/main/README.md cd secure-riscv-soc -
Verify toolchain installation
riscv64-unknown-elf-gcc --version iverilog -v python3 --version
-
Build the project
cd software make all
For a consistent development environment across all platforms, you can use Docker. This avoids manual installation of dependencies.
Pull the pre-built Docker image from Docker Hub:
docker pull ahsanriaz8000/secure-riscv-soc:latestThen run the container:
docker run -it --rm \
-v $(pwd):/workspace \
-w /workspace \
ahsanriaz8000/secure-riscv-soc:latest \
/bin/bash-
Start the development container
docker-compose up -d
-
Enter the container
docker-compose exec riscv-soc bash -
Build and run simulations
cd software make all cd .. ./scripts/simulate.sh
Build the Docker image locally:
docker build -t secure-riscv-soc:latest .Then run it:
docker run -it --rm \
-v $(pwd):/workspace \
-w /workspace \
secure-riscv-soc:latest \
/bin/bash- โ No manual toolchain setup - All tools pre-installed
- โ Consistent environment - Same tools on Linux, macOS, Windows
- โ Isolated workspace - No conflicts with system packages
- โ Easy sharing - Same image works for all team members
If using docker-compose:
docker-compose downIf running manually:
# Just exit the container (--rm flag auto-removes it)
exitcd software
make allThis will:
- Compile the secure boot ROM
- Compile and sign the application firmware
- Generate memory initialization files (
*.hex)
build/boot.elf- Boot ROM ELF filebuild/boot.bin- Boot ROM binarybuild/firmware.elf- Firmware ELF filebuild/firmware.bin.signed- Signed firmware binaryhardware/mem_init/boot_rom.hex- Boot ROM memory initializationhardware/mem_init/firmware.hex- Firmware memory initialization
cd software
make clean
make all./scripts/test_anti_replay_quick.shThis script:
- Builds the firmware
- Runs the simulation
- Verifies all tests pass
- Shows progress indicators
./scripts/simulate.shThis will:
- Compile all Verilog sources
- Run the testbench
- Generate waveform file (
soc_simulation.vcd) - Display UART output
gtkwave build/soc_simulation.vcdMPU Test:
cd software
# Edit Makefile: Change FW_SRCS to test_mpu.c
make clean all
cd ..
./scripts/simulate.shSecure Boot Test:
cd software
# Edit Makefile: Change FW_SRCS to test_secure_boot.c
make clean all
cd ..
./scripts/simulate.shAnti-Replay Test:
cd software
# Edit Makefile: Change FW_SRCS to test_anti_replay.c
make clean all
cd ..
./scripts/test_anti_replay_quick.shsecure-riscv-soc/
โ
โโโ hardware/
โ โโโ rtl/ # RTL (Register Transfer Level) code
โ โ โโโ cpu/
โ โ โ โโโ picorv32.v # PicoRV32 CPU core
โ โ โโโ memory/
โ โ โ โโโ boot_rom.v # Boot ROM (4KB)
โ โ โ โโโ instruction_mem.v # Instruction memory (64KB)
โ โ โ โโโ data_mem.v # Data memory (64KB)
โ โ โโโ peripherals/
โ โ โ โโโ uart.v # UART peripheral
โ โ โโโ security/ # Security modules
โ โ โ โโโ mpu.v # Memory Protection Unit
โ โ โ โโโ sha256.v # SHA-256 hash core
โ โ โ โโโ hmac_sha256.v # HMAC-SHA256 implementation
โ โ โ โโโ crypto_accelerator.v # Crypto accelerator
โ โ โ โโโ monotonic_counter.v # Monotonic counter
โ โ โ โโโ nonce_gen.v # Nonce generator (LFSR)
โ โ โ โโโ anti_replay.v # Anti-replay engine
โ โ โโโ top/
โ โ โโโ soc_top.v # Top-level SoC integration
โ โ
โ โโโ tb/ # Testbenches
โ โ โโโ tb_soc_top.v # Main SoC testbench
โ โ โโโ anti_replay_tb.v # Anti-replay unit testbench
โ โ
โ โโโ mem_init/ # Memory initialization files
โ โ โโโ boot_rom.hex # Boot ROM initialization
โ โ โโโ firmware.hex # Firmware initialization
โ โ
โ โโโ constraints/ # FPGA timing constraints (future)
โ
โโโ software/
โ โโโ boot/ # Boot ROM source
โ โ โโโ boot_secure.S # Secure boot implementation
โ โ โโโ boot.ld # Boot ROM linker script
โ โ
โ โโโ firmware/ # Application firmware
โ โ โโโ start.S # Firmware entry point
โ โ โโโ firmware.ld # Firmware linker script
โ โ โโโ test_mpu.c # MPU test suite
โ โ โโโ test_secure_boot.c # Secure boot test
โ โ โโโ test_anti_replay.c # Anti-replay test suite
โ โ
โ โโโ common/ # Shared code
โ โ โโโ soc_map.h # Memory map definitions
โ โ โโโ firmware_header.h # Firmware header structure
โ โ โโโ uart.h # UART interface
โ โ โโโ uart.c # UART implementation
โ โ
โ โโโ tools/ # Build tools
โ โ โโโ bin2hex.py # Binary to hex converter
โ โ โโโ sign_firmware.py # Firmware signing tool
โ โ
โ โโโ Makefile # Build automation
โ
โโโ scripts/ # Automation scripts
โ โโโ simulate.sh # Main simulation script
โ โโโ test_anti_replay_quick.sh # Quick anti-replay test
โ โโโ test_replay_attacks.sh # Replay attack scenarios
โ โโโ test_secure_boot_attacks.sh # Secure boot attack tests
โ
โโโ build/ # Build outputs (git-ignored)
โ โโโ *.elf # Compiled ELF files
โ โโโ *.bin # Binary files
โ โโโ *.hex # Hex files
โ โโโ *.vvp # Compiled simulation
โ
โโโ docs/ # Documentation
โ โโโ diagrams/ # Architecture diagrams
โ โโโ specifications/ # Design specifications
โ
โโโ .gitignore # Git ignore rules
โโโ README.md # This file
| Address Range | Size | Description | Access |
|---|---|---|---|
0x00000000 - 0x00000FFF |
4KB | Boot ROM | Read/Execute only |
0x00010000 - 0x0001FFFF |
64KB | Instruction Memory | Read/Execute only |
0x10000000 - 0x1000FFFF |
64KB | Data Memory | Read/Write/Execute |
0x20000000 - 0x200000FF |
256B | UART | Read/Write |
0x30000000 - 0x300000FF |
256B | Crypto Accelerator | Read/Write |
0x40000000 - 0x400000FF |
256B | Key Store | Machine-mode only |
0x50000000 - 0x500000FF |
256B | Anti-Replay Protection | Read/Write |
See software/common/soc_map.h for complete register definitions.
Example firmware that uses the security features:
#include "soc_map.h"
#include "uart.h"
void main(void) {
uart_puts("Secure SoC Firmware\n");
// Access protected Key Store (will trap if not privileged)
// volatile unsigned int key = *((unsigned int*)KEY_STORE_BASE);
// Use anti-replay protection
COUNTER_VALUE = 0;
REPLAY_CHECK_COUNTER = 1;
REPLAY_CHECK_NONCE = 0x12345678;
REPLAY_VALIDATE = 1;
// Wait for validation
while (!(REPLAY_STATUS & REPLAY_STATUS_READY));
if (REPLAY_STATUS & REPLAY_STATUS_VALID) {
uart_puts("Packet validated\n");
}
}Firmware must be signed before it can boot:
cd software
python3 tools/sign_firmware.py \
build/firmware.bin \
0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF \
1 \
build/firmware.bin.signedTest MPU Protection:
# Edit software/Makefile: Set FW_SRCS to test_mpu.c
cd software && make clean all && cd ..
./scripts/simulate.sh | grep -E "TEST|PASS|FAIL|TRAP"Test Secure Boot:
# Edit software/Makefile: Set FW_SRCS to test_secure_boot.c
cd software && make clean all && cd ..
./scripts/simulate.sh | grep -E "SECURE BOOT|OK|BAD"Test Anti-Replay:
./scripts/test_anti_replay_quick.sh-
MPU Test Suite (
test_mpu.c)- Normal memory access
- Key Store protection (should trap)
- UART peripheral access
-
Secure Boot Test (
test_secure_boot.c)- Boot success verification
- Firmware header validation
-
Anti-Replay Test Suite (
test_anti_replay.c)- 8 comprehensive tests:
- Monotonic counter increment
- Monotonic property (reject decrements)
- Counter lock mechanism
- Nonce uniqueness
- Valid packet acceptance
- Replay attack blocking
- Old counter rejection
- Valid sequence acceptance
- 8 comprehensive tests:
Quick Test:
./scripts/test_anti_replay_quick.shAll Tests:
# MPU Tests
cd software
sed -i 's/FW_SRCS = .*/FW_SRCS = firmware\/start.S common\/uart.c firmware\/test_mpu.c/' Makefile
make clean all && cd .. && ./scripts/simulate.sh
# Secure Boot Tests
cd software
sed -i 's/FW_SRCS = .*/FW_SRCS = firmware\/start.S common\/uart.c firmware\/test_secure_boot.c/' Makefile
make clean all && cd .. && ./scripts/simulate.sh
# Anti-Replay Tests
./scripts/test_anti_replay_quick.shAll tests should pass:
- โ MPU: Key Store access traps (security working)
- โ Secure Boot: Firmware verifies and boots
- โ Anti-Replay: All 8 tests pass, attacks blocked
- This README: Project overview and quick start
- Source Code Comments: Inline documentation in Verilog/C files
- Header Files:
soc_map.h,firmware_header.hcontain register definitions
- Start with:
hardware/rtl/top/soc_top.v- System integration - Security modules:
hardware/rtl/security/- All security features - Boot process:
software/boot/boot_secure.S- Secure boot implementation - Memory map:
software/common/soc_map.h- All addresses and registers
- MPU: See
hardware/rtl/security/mpu.vfor protection logic - Secure Boot: See
software/boot/boot_secure.Sfor boot sequence - Anti-Replay: See
hardware/rtl/security/anti_replay.vfor validation logic
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Make your changes: Follow existing code style
- Add tests: Ensure all tests pass
- Commit changes: Write clear commit messages
- Push to branch:
git push origin feature/your-feature - Create Pull Request: Describe your changes
- Verilog: Follow existing naming conventions
- C Code: Use consistent indentation (4 spaces)
- Comments: Document complex logic
- Test Coverage: Add tests for new features
- Additional security features
- Performance optimizations
- Documentation improvements
- Test coverage expansion
- FPGA synthesis support
This project is licensed under the MIT License - see the LICENSE file for details.
- Developed as part of CS339 Computer Architecture course
- Secure RISC-V SoC for Smart Lock System
- PicoRV32: CPU core by Claire Wolf (GitHub)
- RISC-V Foundation: Open ISA specification
- Icarus Verilog: Open-source Verilog simulator
- RISC-V Instruction Set Manual
- FIPS 180-4 (SHA-256 specification)
- RFC 2104 (HMAC specification)
- Hardware security best practices
For questions, issues, or contributions:
- Check Documentation: Read this README and code comments
- Review Issues: Check existing GitHub issues
- Create Issue: Report bugs or request features
- Contact: Reach out to project maintainers
This project is suitable for:
- Computer Architecture courses
- Hardware Security education
- RISC-V ISA learning
- SoC design projects
- Security research
Potential improvements:
- AES encryption/decryption support
- True Random Number Generator (TRNG)
- Anti-rollback protection
- FPGA synthesis support
- Additional test coverage
- Performance optimizations
- Power management features
Built with โค๏ธ for secure IoT applications