A secure communication protocol implementation using cryptographic primitives including SHA-256 hashing, HMAC-SHA256 authentication, and XOR encryption.
This project implements a Challenge-Response Protocol where Alice and Bob communicate securely using shared secret keys, counters, and nonces to prevent replay attacks and ensure message integrity.
- Secure Communication: XOR encryption with SHA-256 derived keys
- Message Authentication: HMAC-SHA256 for integrity verification
- Replay Attack Prevention: Counter-based state management
- Message Freshness: Nonce-based freshness verification
- Error Handling: Graceful handling of missing files and invalid inputs
- Cross-Platform: Works on macOS, Linux, and Windows
- GCC compiler
- OpenSSL development libraries
- macOS/Linux/Windows environment
-
Clone the repository
git clone https://github.com/sfwani/Challenge-Response-Protocol.git cd Challenge-Response-Protocol -
Install OpenSSL (if not already installed)
# macOS brew install openssl # Ubuntu/Debian sudo apt-get install libssl-dev # CentOS/RHEL sudo yum install openssl-devel
-
Compile the programs
# macOS gcc -I/opt/homebrew/include -L/opt/homebrew/lib alice.c -lssl -lcrypto -o alice gcc -I/opt/homebrew/include -L/opt/homebrew/lib bob.c -lssl -lcrypto -o bob # Linux gcc alice.c -lssl -lcrypto -o alice gcc bob.c -lssl -lcrypto -o bob
The protocol follows a three-step process:
- Alice creates challenge β Bob processes challenge β Alice verifies response
-
Create test files (see Testing Guide)
echo -n "Your32ByteMessageHere123456789" > Message.txt echo -n "YourSecretKey" > SharedKey.txt echo "1" > A_ctr.txt && echo "1" > B_ctr.txt echo "100" > A_nonce.txt && echo "100" > B_nonce.txt
-
Run Alice (creates challenge)
./alice Message.txt SharedKey.txt A_ctr.txt A_nonce.txt
-
Run Bob (processes challenge)
./bob Ciphertext.txt Signature.txt SharedKey.txt B_ctr.txt B_nonce.txt
-
Run Alice again (verifies response)
./alice Message.txt SharedKey.txt A_ctr.txt A_nonce.txt
After successful execution, you'll see:
Key.txt- Shared key in hex formatCiphertext.txt- Encrypted messageSignature.txt- HMAC signatureResponse.txt- Bob's responseAcknowledgment.txt- "Acknowledgment Successful"
- Reads message, shared key, counter, and nonce
- Writes key in hex format to
Key.txt - Encrypts message:
c = m β H(k || ctr) - Signs ciphertext:
sig = HMAC_k(c || nonce) - Waits for Bob's response
- Verifies response and writes acknowledgment
- Updates counter and nonce
- Reads ciphertext, signature, shared key, counter, and nonce
- Verifies Alice's signature
- Decrypts ciphertext:
m = c β H(k || ctr) - Computes response:
response = H(m || (ctr+1) || (nonce+1)) - Updates counter and nonce
Challenge-Response-Protocol/
βββ alice.c # Alice's implementation
βββ bob.c # Bob's implementation
βββ RequiredFunctionsHW1.c # Utility functions template
βββ test_cases/ # Test data directory
β βββ Message1.txt # Sample message
β βββ SharedKey1.txt # Sample shared key
β βββ CorrectA_ctr1.txt # Alice's counter
β βββ CorrectA_nonce1.txt # Alice's nonce
β βββ CorrectB_ctr1.txt # Bob's counter
β βββ CorrectB_nonce1.txt # Bob's nonce
β βββ VerifyingCRP.sh # Verification script
βββ PROJECT_DOCUMENTATION.md # Detailed documentation
βββ TESTING_GUIDE.md # Testing instructions
βββ README.md # This file
# Copy test files to current directory
cp test_cases/Message1.txt test_cases/SharedKey1.txt .
cp test_cases/CorrectA_ctr1.txt A_ctr.txt
cp test_cases/CorrectA_nonce1.txt A_nonce.txt
cp test_cases/CorrectB_ctr1.txt B_ctr.txt
cp test_cases/CorrectB_nonce1.txt B_nonce.txt
# Run the protocol
./alice Message1.txt SharedKey1.txt A_ctr.txt A_nonce.txt
./bob Ciphertext.txt Signature.txt SharedKey1.txt B_ctr.txt B_nonce.txt
./alice Message1.txt SharedKey1.txt A_ctr.txt A_nonce.txtbash test_cases/VerifyingCRP.sh- Confidentiality: XOR encryption with SHA-256 derived keys
- Integrity: HMAC-SHA256 message authentication
- Authenticity: Shared secret key verification
- Replay Protection: Counter-based state management
- Freshness: Nonce-based message freshness
- Project Documentation - Comprehensive technical details
- Testing Guide - How to create and test with custom files
- Requirements - Original project specifications
- Message Size: 32 bytes (256 bits)
- Hash Function: SHA-256
- HMAC: HMAC-SHA256
- Encryption: XOR with derived keys
- File Format: Hexadecimal for all outputs
- State Management: Synchronized counters and nonces
-
OpenSSL not found
# macOS export LDFLAGS="-L/opt/homebrew/lib" export CPPFLAGS="-I/opt/homebrew/include"
-
Message not 32 bytes
wc -c Message.txt # Should show 32 -
Permission denied
chmod +x alice bob
-
Files not found
- Ensure all input files exist
- Check file names match exactly
This project is part of a programming assignment for educational purposes.
Sanaan - GitHub
This is an educational project. For questions or issues, please open a GitHub issue.
Status: β
Complete and Functional
Last Updated: September 9, 2025