adachsoft / data-backup-encryption
Encrypts and decrypts backup archives through a PSR-7 stream-based storage decorator.
Package info
gitlab.com/a.adach/data-backup-encryption
pkg:composer/adachsoft/data-backup-encryption
Requires
- php: ^8.3
- ext-sodium: *
- adachsoft/collection: ^3.0
- adachsoft/data-backup-contracts: ^0.2
- psr/http-message: ^2.0
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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:
- The six-byte
ADBENCmagic value and one-byte format version. - An eight-byte key identifier and the 24-byte libsodium secretstream header.
- 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.