Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1643eab
add first version of streamed encryption
Baptouuuu Nov 7, 2026
10a61e7
use sequence position instead of own tag
Baptouuuu Nov 8, 2026
7f35ec1
zero out the salt
Baptouuuu Nov 8, 2026
da0840b
discard error
Baptouuuu Nov 12, 2026
2635006
add streamed decryption
Baptouuuu Nov 12, 2026
90ea9c5
prove encryption/decryption identity
Baptouuuu Nov 12, 2026
5c3faca
prove the plain content does not appear in the encrypted version
Baptouuuu Nov 12, 2026
4e91ff9
fix blackbox configuration
Baptouuuu Nov 12, 2026
d6a4293
reduce probability that a short chunk may appear in the encrypted file
Baptouuuu Nov 12, 2026
d5fab8f
make sure there is always at least one chunk to encrypt
Baptouuuu Nov 12, 2026
95a4ac9
make sure no one big chunk can go undecrypted
Baptouuuu Nov 12, 2026
4275a69
fail when the last encrypted chunk is not the last one in the sequence
Baptouuuu Nov 12, 2026
8769d1a
exclude empty chunks from encryption
Baptouuuu Nov 12, 2026
1324655
make sure truncated files fail decryption
Baptouuuu Nov 12, 2026
76c42d2
force the efforts to be the same as encryption
Baptouuuu Nov 12, 2026
bdc0df4
remove useless call as the info is publicly available
Baptouuuu Nov 12, 2026
fc4466a
remove empty last chunk if there is another previous chunk
Baptouuuu Nov 12, 2026
2dbe267
filter out empty chunks before decrypting
Baptouuuu Nov 12, 2026
d6648f3
make the most likely branch to run first
Baptouuuu Nov 12, 2026
9788788
fix truncating to the exact encrypted size
Baptouuuu Nov 12, 2026
ee1c68b
bump dependencies
Baptouuuu Nov 13, 2026
dbf340f
make sure decrypting with the wrong password fails
Baptouuuu Nov 13, 2026
341390b
use an object to keep a signle reference to the derivation key
Baptouuuu Nov 13, 2026
8c16a30
use a value object to hold passwords
Baptouuuu Nov 13, 2026
e778975
add Version enum
Baptouuuu Nov 13, 2026
5096425
use an enum to specify the sensitivity of the encryption
Baptouuuu Nov 13, 2026
7590778
derive the sensitivity from the version
Baptouuuu Nov 13, 2026
2cd1bd8
make sure to use the same version
Baptouuuu Nov 13, 2026
d563608
add Algorithm enum to centralize derivation key logic
Baptouuuu Nov 13, 2026
f14ff87
build the sensitivity frame from the enum
Baptouuuu Nov 13, 2026
88a4327
use single DerivationKey object for both encryption and decryption
Baptouuuu Nov 13, 2026
64ef24a
use the version enum to build the kdf header
Baptouuuu Nov 13, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions blackbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
__DIR__.'/src/',
__DIR__.'/proofs/',
)
->dumpTo('coverage.clover')
->enableWhen(true),
->dumpTo('coverage.clover'),
),
default => $app,
})
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"issues": "http://github.com/innmind/encryption/issues"
},
"require": {
"php": "~8.5"
"php": "~8.5",
"innmind/immutable": "~6.5",
"innmind/frame": "^1.0.2"
},
"autoload": {
"psr-4": {
Expand Down
Empty file removed proofs/.gitkeep
Empty file.
137 changes: 137 additions & 0 deletions proofs/encryption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types = 1);

use Innmind\Encryption\{
Encrypt,
Decrypt,
Password,
};
use Innmind\Immutable\{
Sequence,
Str,
Monoid\Concat,
};
use Innmind\BlackBox\{
Set,
Prove,
Runner\Assert,
};

return static function(Prove $prove) {
yield $prove
->proof('Encryption/decryption identity')
->given(
Set::sequence(
Set::strings(),
),
Set::strings()->between(1, 4096),
)
->test(static function(Assert $assert, $chunks, $password) {
$encrypt = Encrypt::of(Password::of($password));
$decrypt = Decrypt::of(Password::of($password));

$assert->same(
\implode('', $chunks),
Sequence::of(...$chunks)
->map(Str::of(...))
->via($encrypt)
->via($decrypt)
->fold(Concat::monoid)
->toString(),
);
});

yield $prove
->proof('Encryption does not expose chunks in plain text')
->given(
Set::sequence(
Set::strings()->atLeast(4), // statistically probable that a short chunk can be found as an encrypted value
),
Set::strings()->between(1, 4096),
)
->test(static function(Assert $assert, $chunks, $password) {
$encrypt = Encrypt::of(Password::of($password));

$encrypted = Sequence::of(...$chunks)
->map(Str::of(...))
->via($encrypt)
->fold(Concat::monoid)
->toString();

$assert
->string($encrypted)
->not()
->empty();

foreach ($chunks as $chunk) {
$assert
->string($encrypted)
->not()
->contains($chunk)
->contains(\strrev($chunk));
}
});

yield $prove
->proof('Decryption of truncated file fails')
->given(
Set::strings()
->between(1, 4096)
->flatMap(
static fn($password) => Set::sequence(Set::strings())
->map(static fn($chunks) => Sequence::of(...$chunks)->map(Str::of(...)))
->map(Encrypt::of(Password::of($password->unwrap())))
->flatMap(
static fn($encrypted) => Set::integers()
->between(
0,
$encrypted
->unwrap()
->fold(Concat::monoid)
->toEncoding(Str\Encoding::ascii)
->length() - 1,
)
->map(static fn($truntaed) => [
$encrypted->unwrap(),
$password->unwrap(),
$truntaed,
]),
),
),
)
->test(static function(Assert $assert, $config) {
[$encrypted, $password, $truncated] = $config;

$assert->throws(
static fn() => $encrypted
->flatMap(static fn($chunk) => $chunk->chunk())
->take($truncated)
->via(Decrypt::of(Password::of($password)))
->fold(Concat::monoid)
->toString(),
);
});

yield $prove
->proof('Decryption with wrong password fails')
->given(
Set::sequence(
Set::strings(),
),
Set::strings()->between(1, 4096),
Set::strings()->between(1, 4096),
)
->exclude(static fn($_, $a, $b) => $a === $b)
->test(static function(Assert $assert, $chunks, $password, $wrongPassword) {
$encrypt = Encrypt::of(Password::of($password));
$decrypt = Decrypt::of(Password::of($wrongPassword));
$encrypted = Sequence::of(...$chunks)
->map(Str::of(...))
->via($encrypt);

$assert->throws(
static fn() => $decrypt($encrypted)->memoize(),
);
});
};
Empty file removed src/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions src/Algorithm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

namespace Innmind\Encryption;

/**
* @psalm-immutable
*/
enum Algorithm
{
case xchacha20poly1305;

public function hash(): Algorithm\Hash
{
return match ($this) {
self::xchacha20poly1305 => Algorithm\Hash::xchacha20poly1305,
};
}

public function salt(): Algorithm\Salt
{
return match ($this) {
self::xchacha20poly1305 => Algorithm\Salt::xchacha20poly1305,
};
}
}
43 changes: 43 additions & 0 deletions src/Algorithm/Hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types = 1);

namespace Innmind\Encryption\Algorithm;

use Innmind\Encryption\{
DerivationKey,
Password,
Sensitivity,
};

/**
* @psalm-immutable
*/
enum Hash
{
case xchacha20poly1305;

public function size(): int
{
return match ($this) {
self::xchacha20poly1305 => \SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
};
}

public function key(
Password $password,
string $salt,
Sensitivity $sensitivity,
): DerivationKey {
return new DerivationKey(match ($this) {
self::xchacha20poly1305 => \sodium_crypto_pwhash(
$this->size(),
$password->unwrap(),
$salt,
$sensitivity->operations(),
$sensitivity->memory(),
\SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13,
),
});
}
}
38 changes: 38 additions & 0 deletions src/Algorithm/Salt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace Innmind\Encryption\Algorithm;

use Innmind\Frame\Frame;
use Innmind\Immutable\Str;

enum Salt
{
case xchacha20poly1305;

public function new(): string
{
return \random_bytes($this->size());
}

/**
* @return Frame<Str>
*/
public function frame(): Frame
{
return match ($this) {
self::xchacha20poly1305 => Frame::chunk($this->size()),
};
}

/**
* @return int<1, max>
*/
private function size(): int
{
return match ($this) {
self::xchacha20poly1305 => \SODIUM_CRYPTO_PWHASH_SALTBYTES,
};
}
}
125 changes: 125 additions & 0 deletions src/Decrypt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types = 1);

namespace Innmind\Encryption;

use Innmind\Frame\Frame;
use Innmind\Immutable\{
Sequence,
Str,
};

final class Decrypt
{
private const int CHUNK = 8192 + \SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;

/** @var Frame<array{string, DerivationKey}> */
private Frame $decode;

private function __construct(
Password $password,
) {
$this->decode = Frame::chunk(4)
->map(Version::of(...))
->flatMap(
static fn($version) => $version
->algorithm()
->salt()
->frame()
->map(static fn($salt) => [$version, $salt]),
)
->flatMap(
static fn($pair) => $pair[0]
->sensitivity()
->frame()
->map(static fn($limits) => [
$pair[0]->kdfHeader($pair[1]->toString(), $limits),
$pair[1]->toString(),
$pair[0],
]),
)
->map(static function($tuple) use ($password) {
[$kdfHeader, $salt, $version] = $tuple;
$key = $version->algorithm()->hash()->key(
$password,
$salt,
$version->sensitivity(),
);

return [
$kdfHeader,
$key,
];
});
}

/**
* @param Sequence<Str> $chunks
*
* @return Sequence<Str>
*/
public function __invoke(Sequence $chunks): Sequence
{
$decode = $this->decode;

return Sequence::lazy(static function() use ($decode, $chunks) {
$state = '';
$decode = $decode
->flatMap(
static function($pair) use (&$state) {
return Frame::chunk(\SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES)->map(
static function($header) use (&$pair, &$state) {
$state = \sodium_crypto_secretstream_xchacha20poly1305_init_pull(
$header->toString(),
$pair[1]->unwrap(),
);
$pair[1]->destroy();

return $pair[0];
},
);
},
)
->map(Str::of(...))
->map(Decrypt\Header::of(...))
->passthrough();

/** @psalm-suppress MixedArgumentTypeCoercion */
yield $chunks
->via($decode)
->filter(static fn($chunk) => $chunk instanceof Decrypt\Header || !$chunk->empty())
->flatMap(static fn($chunk) => match (true) {
$chunk instanceof Str => $chunk->chunk(self::CHUNK),
default => Sequence::of($chunk),
})
->aggregate(static fn(Decrypt\Header|Str $a, Str $b) => match (true) {
$a instanceof Str => $a->append($b)->chunk(self::CHUNK),
default => Sequence::of($a, $b),
})
->map(Decrypt\Chunk::of(...))
->prepend(Sequence::of(Decrypt\Chunk::none()))
->aggregate(static fn(Decrypt\Chunk|Decrypt\Pull $a, Decrypt\Chunk $b) => $a->and($b))
->map(static function($pull) use (&$state) {
[$plain, $state] = $pull($state);

return $plain;
})
->via(Sequence\Position::tag(...))
->map(
static fn($value) => $value
->unwrap()
->validate($value->position()),
);

\sodium_memzero($state);
})
->flatMap(static fn($chunks) => $chunks)
->map(static fn($chunk) => $chunk->toEncoding(Str\Encoding::ascii));
}

public static function of(Password $password): self
{
return new self($password);
}
}
Loading
Loading