Skip to content

HyphaCrypt Encryption

Sergi edited this page Nov 16, 2024 · 1 revision

HyphaCrypt Encryption

HyphaCrypt is Seigr’s advanced encryption framework designed to support secure, modular data storage within the ecosystem. Developed to accommodate Seigr’s eco-conscious, modular structure, HyphaCrypt provides robust data protection through adaptive hashing, encryption, and hierarchical integrity verification.


Key Components of HyphaCrypt

  1. Encryption: Symmetric encryption (Fernet) ensures data privacy within Seigr Cells.
  2. Senary Encoding Support: Optional senary encoding increases efficiency and aligns with Seigr’s eco-conscious goals.
  3. Hierarchical Hash Tree: Creates a multi-layered hash structure for adaptive integrity verification.
  4. Error Logging: Uses protocol buffers to log issues with severity, resolution, and additional metadata.
  5. Integrity Verification: Checks data consistency by comparing computed hash trees to reference trees.

Core Functionalities

1. Initialization and Setup

The HyphaCrypt class initializes with key parameters:

  • data: The data bytes to be encrypted, decrypted, or hashed.
  • segment_id: Unique identifier for the data segment or Seigr Cell.
  • hash_depth: Specifies the depth of the hierarchical hash tree.
  • use_senary: Boolean to enable or disable senary encoding for hash outputs.

Upon initialization, HyphaCrypt sets up a logging structure for tracking encryption and hash operations and initializes a hash tree for hierarchical data integrity.


2. Encryption and Decryption

HyphaCrypt uses symmetric encryption (Fernet) for data protection. The process includes:

Generating the Encryption Key

  • generate_encryption_key: Creates an encryption key based on an optional password. If no password is provided, a random key is generated using Fernet.generate_key().
  • Salt Management: If a password is supplied, the key is derived using a randomly generated salt, allowing reusability across sessions while ensuring data privacy.

Encrypting Data

  • encrypt_data: Encrypts the data using the generated Fernet key. This method outputs encrypted data bytes and logs the process.

Decrypting Data

  • decrypt_data: Decrypts data using the corresponding Fernet key. If decryption fails, the error is logged with high severity, and an exception is raised to handle the failure.

3. Hashing with HyphaCrypt

Primary and Secondary Hashes

HyphaCrypt generates hashes for Seigr Cells, supporting both security and modularity. The hypha_hash function enables:

  • Algorithm Selection: Users can select from supported algorithms (e.g., SHA-256, SHA-512).
  • Salting: Optionally applies a salt to the data before hashing to enhance security.
  • Encoding: Supports hex or senary-encoded output, with senary encoding being a sustainable option for Seigr.

Hierarchical Hash Tree (Multi-Depth Hashing)

The hierarchical hash tree enables adaptive data integrity, allowing verification at multiple levels.

  • compute_primary_hash: Computes the primary hash for the segment, which serves as the root of the hash tree.
  • compute_layered_hashes: Generates a multi-depth hash tree, where each level’s hash is computed based on the previous layer. Hash depth determines the security level and integrity coverage of the tree.
  • Tree Storage: Each layer’s hash is stored in a dictionary (self.tree) for flexible retrieval and verification.

Integrity Verification

HyphaCrypt includes integrity verification by comparing the generated hash tree to a reference tree. This process ensures that data has not been altered and supports Seigr’s self-healing properties.

Verification Process

  • verify_integrity: This function accepts a reference hash tree and compares it against the generated tree up to a specified depth.
  • Partial Depth Option: Allows verification at multiple levels, optimizing for faster checks when full-depth verification is unnecessary.
  • Failure Handling: If discrepancies are detected, the verification status is marked as “failed,” and each failed layer is recorded for further action.

Error Handling and Logging

HyphaCrypt’s error handling system logs issues through protocol buffers, providing detailed, actionable error reports:

  • _log_error: Records an error event with information including error ID, severity, component, and resolution strategy.
  • Severity Levels: High-severity errors trigger alerts and may pause operations to prevent further issues.

Each error entry is managed using ErrorLogEntry protocol buffers, capturing rich metadata for debugging and providing resolution strategies based on severity.

Layer Event Logging

HyphaCrypt logs each hash layer’s generation:

  • _log_layer_event: Captures each layer’s metadata, including operation ID, type, and timestamp, using OperationLog entries for traceable event tracking.

Example Usage

Here is a high-level example of using HyphaCrypt:

from src.crypto.hypha_crypt import HyphaCrypt

# Initialize HyphaCrypt with data and segment ID
data = b"example data to encrypt"
segment_id = "seigr_segment_001"
hypha_crypt = HyphaCrypt(data=data, segment_id=segment_id, hash_depth=3, use_senary=True)

# Generate an encryption key
key = hypha_crypt.generate_encryption_key(password="secure_password")

# Encrypt and decrypt data
encrypted_data = hypha_crypt.encrypt_data(key)
decrypted_data = hypha_crypt.decrypt_data(encrypted_data, key)

# Compute and verify hashes
primary_hash = hypha_crypt.compute_primary_hash()
hash_tree = hypha_crypt.compute_layered_hashes()

# Integrity verification against a reference tree
reference_tree = {...}  # Predefined or previously saved hash tree
verification_status = hypha_crypt.verify_integrity(reference_tree)

This example demonstrates the core workflow of encryption, hashing, and integrity verification using HyphaCrypt. Each step is logged and validated, ensuring both transparency and robust error handling.


Future Enhancements

Planned improvements to HyphaCrypt include:

  • Additional Hashing Algorithms: Expand supported hashing algorithms to include more lightweight and energy-efficient options.
  • Advanced Encryption Modes: Introduce asymmetric encryption for specific use cases, enhancing data security.
  • Enhanced Error Resilience: Implement multi-level redundancy within the hash tree, allowing partial recovery from corrupted data segments.

Conclusion

HyphaCrypt Encryption provides Seigr’s ecosystem with an advanced, adaptable encryption and hashing framework. Its hierarchical hash tree, secure encryption, and detailed logging support Seigr’s goals for resilience, transparency, and modularity. Designed to evolve with the ecosystem, HyphaCrypt remains at the core of Seigr’s secure and sustainable data architecture.