xbin25

package module
v0.0.0-...-5066df0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

README

Secure Data Serialization for Go

XBin25

License

XBin25 is a Go package designed for secure data serialization, combining state-of-the-art encryption, digital signatures, and multi-layer compression. It ensures confidentiality, integrity, and authenticity for sensitive data in transit or at rest.

Features

  • Military-Grade Encryption: AES-256-GCM encryption with unique per-message keys
  • Secure Key Exchange: RSA-OAEP for AES key encryption (3072-bit or stronger)
  • Tamper Evidence: RSA-PSS digital signatures for data authenticity
  • Compression Layers: Parallelized zstd (inner) and pgzip (outer) compression
  • Replay Protection: Configurable timestamp validity windows
  • Memory Hardening: Sensitive keys guarded by memguard against memory leaks
  • Modern Serialization: Efficient MessagePack encoding for structured data

Installation

go get github.com/nthnn/xbin25

Dependencies

  • Go 1.20+
  • memguard (secure memory)
  • msgpack/v5 (serialization)
  • pgzip/zstd (compression)
  • rsa/aes (crypto primitives)

Usage

Basic Usage

import "github.com/nthnn/xbin25"

func main() {
    // Initialize configuration
    config := xbin25.NewConfig(
        "encrypt-cert.pem",  // RSA public key for encryption
        "encrypt-key.pem",   // RSA private key for decryption
        "sign-cert.pem",     // RSA public key for signature verification
        "sign-key.pem",      // RSA private key for signing
        "user-auth-system",  // Context label
        30*time.Minute,      // Max message age
        1024*1024,           // 1MB compression blocks
    )

    // Marshall sensitive data
    data := map[string]interface{}{
        "session_id": "7a4e3b1c-89f2-4d65-9128-cc9a4b1d0e7f",
        "permissions": []string{"read:logs", "write:config"},
    }

    encryptedData, err := config.Marshall(data)
    if err != nil {
        panic(err)
    }

    // Unmarshall securely
    decrypted, err := config.Unmarshall(encryptedData)
    if err != nil {
        panic(err)
    }

    restored := decrypted.(map[string]interface{})
}

Configuration Guide

XBin25Config Parameters
Parameter Description
EncryptCertFile Path to PEM-encoded X.509 certificate with RSA public key for encryption
EncryptKeyFile Path to PEM-encoded RSA private key for decryption
SignCertFile Path to PEM-encoded X.509 certificate for signature verification
SignKeyFile Path to PEM-encoded RSA private key for signing
BlockSize Compression block size (typically 1MB-4MB)
Label Auto-derived from label string (SHA-256 hash of provided label)
Duration Maximum allowed message age (e.g., 30*time.Minute)

Security Architecture

Marshalling Process
  1. MessagePack serialization
  2. AES-256-GCM encryption with random key
  3. RSA-OAEP encryption of AES key
  4. zstd compression
  5. RSA-PSS signing
  6. Timestamp embedding
  7. pgzip outer compression
Unmarshalling Process
  1. pgzip decompression
  2. Timestamp validation
  3. RSA-PSS signature verification
  4. zstd decompression
  5. RSA-OAEP decryption
  6. AES-GCM decryption
  7. MessagePack deserialization

Best Practices

  1. Key Management

    • Use 4096-bit RSA keys minimum
    • Store private keys in hardware security modules (HSMs) where possible
    • Rotate signing keys quarterly
  2. Operational Security

    • Keep system clocks synchronized (NTP)
    • Use unique labels for different data contexts
    • Set conservative duration windows (15-60 minutes)
  3. Performance Tuning

    • Adjust BlockSize based on payload characteristics
    • Balance between zstd compression level and CPU usage
    • Utilize hardware-accelerated AES (AES-NI)

License

Apache 2.0 - See LICENSE for details.

Copyright 2025 Nathanne Isip

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type XBin25Config

type XBin25Config struct {
	// EncryptCertFile is the path to the certificate file containing the public key used for encryption.
	EncryptCertFile string

	// EncryptKeyFile is the path to the private key file used for decryption.
	EncryptKeyFile string

	// SignCertFile is the path to the certificate file containing the public key used for signature verification.
	SignCertFile string

	// SignKeyFile is the path to the private key file used for signing.
	SignKeyFile string

	// BlockSize specifies the block size for parallel compression algorithms.
	// Larger values can improve compression speed on multi-core systems at the cost of memory usage.
	BlockSize int

	// Label is a context parameter for RSA-OAEP encryption.
	// This is automatically derived from the labelStr parameter in NewConfig.
	Label string

	// Duration specifies the maximum allowed age for messages.
	// Messages older than this duration will be rejected, protecting against replay attacks.
	Duration time.Duration
}

XBin25Config holds all configuration parameters for encryption and decryption. A single configuration can be used for multiple Marshall/Unmarshall operations.

func NewConfig

func NewConfig(
	encryptCertFile,
	encryptKeyFile,
	signCertFile,
	signKeyFile,
	labelStr string,
	duration time.Duration,
	blockSize int,
) *XBin25Config

NewConfig creates a new XBin25Config with the provided parameters.

Parameters:

  • encryptCertFile: Path to the certificate file containing the public key used for encryption
  • encryptKeyFile: Path to the private key file used for decryption
  • signCertFile: Path to the certificate file containing the public key used for signature verification
  • signKeyFile: Path to the private key file used for signing
  • labelStr: A string label that is hashed and used as context for RSA-OAEP encryption
  • duration: Maximum allowed age for messages (for replay protection)
  • blockSize: Block size for parallel compression algorithms

The function initializes memguard to protect sensitive cryptographic material in memory.

func (*XBin25Config) Marshall

func (config *XBin25Config) Marshall(data interface{}) ([]byte, error)

Marshall converts any serializable Go data structure into a secure binary format.

The process involves:

  1. MessagePack encoding of the input data
  2. AES-256-GCM encryption with a random key
  3. RSA-OAEP encryption of the AES key
  4. zstd compression of the encrypted package
  5. RSA-PSS signature of the compressed data
  6. Timestamping for replay protection
  7. Final compression with parallel gzip

Parameters:

  • data: Any Go value that can be serialized by MessagePack

Returns:

  • []byte: The marshalled, encrypted, signed, and compressed data
  • error: An error if any step in the process fails

func (*XBin25Config) Unmarshall

func (config *XBin25Config) Unmarshall(data []byte) (interface{}, error)

Unmarshall decrypts, verifies, and deserializes binary data produced by the Marshall function.

The process involves:

  1. Decompression of the outer pgzip layer
  2. Deserialization of the envelope structure
  3. Timestamp verification for replay protection
  4. RSA-PSS signature verification
  5. Decompression of the zstd layer
  6. RSA-OAEP decryption of the AES key
  7. AES-GCM decryption of the data
  8. MessagePack deserialization of the plaintext

Parameters:

  • data: Binary data previously produced by Marshall

Returns:

  • interface{}: The unmarshalled Go value
  • error: An error if any step in the process fails

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL