Search by

adachsoft / data-backup-encryption

Arkadiusz Adach

Encrypts and decrypts backup archives through a PSR-7 stream-based storage decorator.

Package info

gitlab.com/a.adach/data-backup-encryption

Issues

pkg:composer/adachsoft/data-backup-encryption

Statistics

Installs: 11

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.1 2026-12-01 08:11 UTC

This package is auto-updated.

Last update: 2026-12-01 07:11:23 UTC


README

adachsoft/data-backup-encryption decorates an ArchiveStorageInterface implementation, encrypting archive bytes when they are saved and authenticating and decrypting them when they are opened. Archive manifests are delegated unchanged so listing and retention metadata remain available.

The library uses libsodium secretstream for authenticated streaming encryption, supports key rotation for decryption, and leaves archive manifests unchanged.

Requirements

  • PHP 8.3 or newer.
  • The Sodium extension.
  • A PSR-7 stream factory and an implementation of ArchiveStorageInterface, provided by the integrating application.

Installation

Install the package with Composer:

composer require adachsoft/data-backup-encryption

The runtime requires PHP 8.3 or newer and the Sodium extension. A PSR-7 stream factory and an ArchiveStorageInterface implementation must be provided by the integrating application.

Usage

Wrap a configured FilesystemArchiveStorage with EncryptedArchiveStorage:

use AdachSoft\DataBackupEncryption\EncryptedArchiveStorage;
use AdachSoft\DataBackupEncryption\ValueObject\EncryptionKey;
use AdachSoft\DataBackupFilesystem\FilesystemArchiveStorage;
use Nyholm\Psr7\Factory\Psr17Factory;

$streamFactory = new Psr17Factory();
$filesystemStorage = $container->get(FilesystemArchiveStorage::class);
$encryptionKey = EncryptionKey::fromBase64($_ENV['BACKUP_ENCRYPTION_KEY']);

$storage = new EncryptedArchiveStorage(
    $filesystemStorage,
    $encryptionKey,
    $streamFactory,
);

The inner storage must implement ArchiveStorageInterface. Older keys can be supplied in EncryptionKeyCollection for decryption during key rotation; new archives are always encrypted with the primary key.

Generate a key once and store its base64 value in a secure secret manager:

php -r 'require "vendor/autoload.php"; echo \AdachSoft\DataBackupEncryption\ValueObject\EncryptionKey::generate()->toBase64(), PHP_EOL;'

Keep a protected copy of the encryption key outside the backup server. Without the key, encrypted backups cannot be restored. toBase64() deliberately returns the secret and should only be used when provisioning secure key storage.

Encrypted archive format

Version 1 archives contain:

  1. The six-byte ADBENC magic value and one-byte format version.
  2. An eight-byte key identifier and the 24-byte libsodium secretstream header.
  3. Repeated records containing a four-byte big-endian ciphertext length followed by the secretstream ciphertext.

Each chunk is authenticated with AAD containing the magic value, version, key identifier, and archive identifier. The final chunk carries the secretstream TAG_FINAL tag, including for an empty archive. Truncation, modification, an incorrect archive identifier, and trailing data after the final chunk are rejected.

The manifest is not encrypted: archive dates, labels, source counts, and other manifest metadata remain visible to the storage backend.