Skip to content

Minimal SHA-1 algorithm written entirely in NASM for x86-64 with a clean C interface. Implements padding, message expansion, and 80-round compression core. Designed for exploring low-level cryptography, bitwise operations, and deterministic hashing.

License

Notifications You must be signed in to change notification settings

ruskaruma/sha1x86

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sha1x86

High-performance SHA-1 implementation in x86-64 NASM assembly with C ABI compatibility.

Build Status License RFC 3174

Overview

Pure x86-64 assembly implementation of the SHA-1 cryptographic hash function. Features complete 80-round compression, RFC 3174 compliance, and optimized register usage for educational and legacy compatibility purposes.

Features

  • Pure x86-64 NASM assembly implementation
  • C ABI compatible for easy integration
  • RFC 3174 compliant and verified
  • Zero external dependencies (only NASM and GCC required)
  • Comprehensive test suite with official test vectors
  • Clean, readable code with detailed comments

Quick Start

Prerequisites

sudo apt-get install nasm gcc

Or run the setup script:

./setup.sh

Build

make

Usage

# Hash a string
./sha1_cli "Hello, World!"

# Hash from stdin
echo -n "test" | ./sha1_cli

# Run test suite
make test

Example Output

$ ./sha1_cli "abc"
a9993e364706816aba3e25717850c26c9cd0d89d

API

C Interface

#include "src/sha1.h"

uint8_t digest[20];
const char* message = "abc";
sha1_compute(message, strlen(message), digest);

Function Signature

int sha1_compute(const void* msg, uint64_t len, uint8_t* out20);

Parameters:

  • msg: Pointer to input message
  • len: Length of message in bytes
  • out20: Pointer to 20-byte output buffer for digest

Returns: 0 on success

Test Vectors (RFC 3174)

Input Expected SHA-1
"" da39a3ee5e6b4b0d3255bfef95601890afd80709
"abc" a9993e364706816aba3e25717850c26c9cd0d89d
"abcd...nopq" (56 bytes) 84983e441c3bd26ebaae4aa1f95129e5e54670f1

Run verification:

./verify.sh

Project Structure

sha1x86/
├── src/
│   ├── sha1.asm       # Assembly implementation (295 lines)
│   ├── sha1.h         # C header
│   └── cli.c          # Command-line interface
├── test/
│   └── test_sha1.c    # RFC test vectors
├── examples/
│   ├── example.c      # Usage examples
│   └── Makefile
├── .github/workflows/
│   └── ci.yml         # CI/CD pipeline
├── Makefile           # Build system
├── setup.sh           # Dependency installer
├── verify.sh          # Test runner
├── README.md          # This file
├── ARCHITECTURE.md    # Technical architecture
└── LICENSE            # MIT License

Algorithm Details

SHA-1 processes messages in 512-bit (64-byte) blocks through an 80-round compression function. The implementation features:

  • Message Schedule: Expands 16 words to 80 words using XOR and rotate operations
  • Round Functions: Four distinct functions for rounds 0-19, 20-39, 40-59, 60-79
  • Hash State: Five 32-bit words (h0-h4) updated after each block
  • Padding: Appends 0x80 byte, zeros, and 64-bit length field

See ARCHITECTURE.md for detailed technical documentation.

Performance

Typical performance on modern x86-64 CPUs:

  • Small messages (<1KB): ~1-2 µs
  • Large messages (1MB): ~5-10 ms
  • Throughput: ~300-500 MB/s (unoptimized)

Security Notice

SHA-1 is cryptographically broken. Collision attacks are practical as of 2017. This implementation is provided for:

  • Educational purposes
  • Legacy system compatibility
  • Non-cryptographic checksums
  • Understanding hash algorithms

For new applications, use SHA-256, SHA-3, or BLAKE3.

Build Targets

make              # Build sha1_cli
make test         # Build and run tests
make verify       # Run comprehensive verification
make examples     # Build example programs
make clean        # Remove build artifacts
make install      # Install to /usr/local/bin

Integration

Compile and Link

nasm -f elf64 src/sha1.asm -o sha1.o
gcc your_program.c sha1.o -o your_program

Create Static Library

ar rcs libsha1.a src/sha1.o
gcc your_program.c -L. -lsha1 -o your_program

Testing

# Run RFC test vectors
make test

# Run comprehensive verification
./verify.sh

# Compare with system tools
echo -n "abc" | sha1sum
./sha1_cli "abc"

Implementation Highlights

  • Efficient Register Usage: Leverages x86-64 extended registers (r8-r15)
  • Stack Management: Proper frame pointers and workspace allocation
  • Endianness Handling: BSWAP instruction for big-endian conversion
  • Bit Operations: ROL for circular shifts, optimized boolean functions
  • ABI Compliance: System V AMD64 calling convention

Contributing

Contributions welcome! Areas of interest:

  • Performance optimizations (SIMD, loop unrolling)
  • Additional test vectors
  • Documentation improvements
  • Architecture ports (ARM, RISC-V)

References

License

MIT License - See LICENSE file for details.

CAUTION PLEASE

Developed as an educational implementation of SHA-1 in pure x86-64 assembly.


Note: This is an educational project demonstrating low-level cryptographic implementation. For production use, rely on established libraries like OpenSSL or libsodium.

About

Minimal SHA-1 algorithm written entirely in NASM for x86-64 with a clean C interface. Implements padding, message expansion, and 80-round compression core. Designed for exploring low-level cryptography, bitwise operations, and deterministic hashing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published