Repository navigation
First draft #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Baptouuuu
wants to merge
32
commits into
main
Choose a base branch
from
first-draft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
First draft #1
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 10a61e7
use sequence position instead of own tag
Baptouuuu 7f35ec1
zero out the salt
Baptouuuu da0840b
discard error
Baptouuuu 2635006
add streamed decryption
Baptouuuu 90ea9c5
prove encryption/decryption identity
Baptouuuu 5c3faca
prove the plain content does not appear in the encrypted version
Baptouuuu 4e91ff9
fix blackbox configuration
Baptouuuu d6a4293
reduce probability that a short chunk may appear in the encrypted file
Baptouuuu d5fab8f
make sure there is always at least one chunk to encrypt
Baptouuuu 95a4ac9
make sure no one big chunk can go undecrypted
Baptouuuu 4275a69
fail when the last encrypted chunk is not the last one in the sequence
Baptouuuu 8769d1a
exclude empty chunks from encryption
Baptouuuu 1324655
make sure truncated files fail decryption
Baptouuuu 76c42d2
force the efforts to be the same as encryption
Baptouuuu bdc0df4
remove useless call as the info is publicly available
Baptouuuu fc4466a
remove empty last chunk if there is another previous chunk
Baptouuuu 2dbe267
filter out empty chunks before decrypting
Baptouuuu d6648f3
make the most likely branch to run first
Baptouuuu 9788788
fix truncating to the exact encrypted size
Baptouuuu ee1c68b
bump dependencies
Baptouuuu dbf340f
make sure decrypting with the wrong password fails
Baptouuuu 341390b
use an object to keep a signle reference to the derivation key
Baptouuuu 8c16a30
use a value object to hold passwords
Baptouuuu e778975
add Version enum
Baptouuuu 5096425
use an enum to specify the sensitivity of the encryption
Baptouuuu 7590778
derive the sensitivity from the version
Baptouuuu 2cd1bd8
make sure to use the same version
Baptouuuu d563608
add Algorithm enum to centralize derivation key logic
Baptouuuu f14ff87
build the sensitivity frame from the enum
Baptouuuu 88a4327
use single DerivationKey object for both encryption and decryption
Baptouuuu 64ef24a
use the version enum to build the kdf header
Baptouuuu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ), | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.