Skip to content

anonx3247/encryption_standard

Repository files navigation

Encryption Standard - Algebra & Cryptology Project

A comprehensive cryptographic system implementing custom encryption, hashing, digital signatures, and authenticated key exchange protocols.

πŸ“‹ Table of Contents

πŸ” Overview

This project implements a complete cryptographic protocol suite including:

  1. Block Cipher: Custom 64-bit block cipher with S-box and linear diffusion layer
  2. Hash Function: Sponge-Merkle–DamgΓ₯rd hybrid producing 128-bit hashes
  3. Authenticated Encryption: Encrypt-then-MAC mode with authentication tags
  4. Digital Signatures: Elliptic curve-based signature scheme
  5. Key Exchange: Authenticated Key Exchange (AKEX) protocol using elliptic curves

✨ Features

  • Custom S-box Design: 16-bit S-box with bit rotations and modular arithmetic
  • Linear Diffusion Layer: 64-bit circulant matrix for optimal branch number
  • Authenticated Encryption: Combined encryption and authentication using two keys
  • ECC-based Signatures: Elliptic curve cryptography over GF(2^128 + 51)
  • Secure Key Exchange: Authenticated key exchange with signature verification
  • Complete Protocol: End-to-end secure communication with encryption and signatures

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • SageMath (for elliptic curve operations)

Setup

  1. Clone the repository:
git clone https://github.com/anonx3247/encryption_standard
cd encryption_standard
  1. Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

πŸ“ Project Structure

encryption_standard/
β”œβ”€β”€ main.py              # Main implementation with all protocols
β”œβ”€β”€ encryption.py        # Block cipher implementation (modular)
β”œβ”€β”€ sbox.py             # S-box functions
β”œβ”€β”€ matrix_ops.py       # Matrix operations for linear layer
β”œβ”€β”€ hash.py             # Hash function implementation
β”œβ”€β”€ mode.py             # Authenticated encryption mode
β”œβ”€β”€ echange.py          # Key exchange protocol
β”œβ”€β”€ signature_sage.py   # Digital signature scheme
β”œβ”€β”€ tests.py            # Unit tests and benchmarks
β”œβ”€β”€ test.ipynb          # Jupyter notebook for testing
└── requirements.txt    # Python dependencies

πŸ”§ Components

1. S-box (Substitution Box)

The S-box is a 16-bit non-linear transformation using:

  • Bit rotations (5 and 7 positions)
  • Affine transformations with constants
  • Modular multiplication

Operations: x β†’ rotate β†’ NOT(x βŠ• a) β†’ multiply β†’ NOT(x βŠ• b) β†’ multiply β†’ rotate

2. Block Cipher

  • Block size: 64 bits
  • Key size: 64 bits
  • Rounds: 16 (default)
  • Structure: Substitution-Permutation Network (SPN)

Each round:

  1. XOR with round key
  2. Apply S-box to four 16-bit blocks
  3. Apply linear diffusion layer (circulant matrix)

3. Hash Function

  • Output size: 128 bits
  • Structure: Hybrid sponge and Merkle–DamgΓ₯rd
  • Compression function: f(M, S) = p_128(M βŠ• S) βŠ• S
  • Divides input into 128-bit chunks and processes sequentially

4. Authenticated Encryption

Mode: Encrypt-then-MAC

  • Uses two 64-bit keys (k1 for encryption, k2 for authentication)
  • Tag computed as: tag = hash(message βŠ• k2)
  • Verification before decryption (prevents padding oracle attacks)

5. Digital Signatures

Elliptic Curve: yΒ² = xΒ³ + 2x + 36 over GF(2^128 + 51)

Sign: (r, s) where:

  • r = (kΒ·G)_y mod q
  • s = (eΒ·skΒ·r) / k mod q
  • e = hash(message) mod q

Verify: Check if r == (uΒ·PK)_y where u = rΒ·eΒ·t mod q and t = 1/s mod q

6. Authenticated Key Exchange (AKEX)

Protocol for establishing shared keys with mutual authentication:

  1. Alice: Generates ephemeral key a, computes I = aΒ·G, signs I, sends (I, sig_A)
  2. Bob: Generates ephemeral key b, computes J = bΒ·G, signs J, sends (J, sig_B)
  3. Both: Verify signatures, compute shared secret S = aΒ·J = bΒ·I
  4. Derive: Two 64-bit keys from hash(S_x) for authenticated encryption

πŸ’» Usage Examples

Basic Encryption/Decryption

import numpy as np
from main import block_encrypt, block_decrypt

# Generate a 64-bit key
key = np.uint64(0x0123456789ABCDEF)

# Encrypt a message
message = 0x48656C6C6F576F726C64  # "HelloWorld" in hex
ciphertext, num_blocks = block_encrypt(key, message, rounds=16)

# Decrypt
plaintext = block_decrypt(key, ciphertext, rounds=16, num_blocks=num_blocks)

Authenticated Encryption

from main import encrypt_and_authenticate, decrypt_and_verify

# Two keys required
keys = [np.uint64(0x1234567890ABCDEF), np.uint64(0xFEDCBA0987654321)]

# Encrypt with authentication
message = 0x48656C6C6F
aux, cipher, tag = encrypt_and_authenticate(keys, message)

# Decrypt and verify
verified, decrypted = decrypt_and_verify(keys, cipher, tag, aux)
if verified:
    print(f"Message: {decrypted:x}")

Digital Signatures

from main import signature_keygen, sign, verify

# Generate key pair
secret_key, public_key = signature_keygen()

# Sign a message
message = 0x1234567890ABCDEF
signature = sign(secret_key, message)

# Verify signature
is_valid = verify(public_key, signature, message)
print(f"Signature valid: {is_valid}")

Complete Protocol (Key Exchange + Encrypted Communication)

from main import simulate_complete_protocol

# Simulates full protocol:
# 1. Key pair generation for Alice and Bob
# 2. Authenticated key exchange
# 3. Encrypted and signed message transmission
# 4. Decryption and signature verification
simulate_complete_protocol()

Running Simulations

from main import simulate_encryption, simulate_akex, simulate_signature

# Test encryption/decryption
simulate_encryption()

# Test key exchange protocol
simulate_akex()

# Test signature scheme
simulate_signature()

πŸ”¬ Technical Details

S-box Parameters

  • a_sbox = 11109 (0x2B65)
  • b_sbox = 10403 (0x28A3)
  • Bit rotations: 5 and 7 positions

Linear Layer

  • Circulant matrix from vector: 0xc063fe883bca8005
  • Provides optimal diffusion (maximizes differential branch number)
  • Invertible for decryption

Elliptic Curve Parameters

  • Field: p = 2^128 + 51
  • Curve: yΒ² = xΒ³ + 2x + 36
  • Generator: G (first generator from SageMath)
  • Order: q = G.order()

Security Considerations

⚠️ Educational Purpose: This is an academic project for learning cryptography.

Do NOT use in production. This implementation:

  • Has not undergone formal security analysis
  • May contain implementation vulnerabilities
  • Uses custom (non-standard) cryptographic primitives
  • Is not optimized for side-channel resistance

For production systems, use established standards like AES, SHA-256, ECDSA, and TLS.

πŸ“¦ Requirements

galois==0.4.6
llvmlite==0.44.0
numba==0.61.2
numpy==2.2.5
typing-extensions==4.13.2

Plus SageMath for elliptic curve operations.

πŸ§ͺ Testing

Run the test suite:

python tests.py

Or use the Jupyter notebook for interactive testing:

jupyter notebook test.ipynb

πŸ“ Notes

  • All operations use little-endian byte order
  • Messages are automatically padded to block boundaries
  • Keys must be exactly 64 bits for encryption
  • Signatures require both parties to exchange public keys securely beforehand

πŸŽ“ Academic Context

This project was developed as part of an Algebra and Cryptology course, demonstrating:

  • Design and implementation of cryptographic primitives
  • Security properties (confusion, diffusion)
  • Protocol composition
  • Mathematical foundations (finite fields, elliptic curves)

Authors: Anas Lecaillon, Timothée Colette, Eliott Tadros, Jeanne Petit, Capucine Leroy, and Joseph Cohen Course: Algèbre et Cryptologie
Language: Python 3 + SageMath

About

A group-project consisting of creating a cryptographic suite

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •