Skip to content
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

Fix bug on V2/V4 key wrapping. #3

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Operations/Wrap/Pie.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ protected function wrapKeyV2V4(string $header, KeyInterface $key): string
// Step 4:
$c = sodium_crypto_stream_xchacha20_xor($key->raw(), $n2, $Ek);

// Step 5:
$t = sodium_crypto_generichash($header . $n . $c, $Ak);

// Wipe keys from memory after use:
try {
sodium_memzero($Ek);
Expand All @@ -158,9 +161,6 @@ protected function wrapKeyV2V4(string $header, KeyInterface $key): string
$Ak ^= $Ak;
}

// Step 5:
$t = sodium_crypto_generichash($header . $n . $c);

return Base64UrlSafe::encodeUnpadded($t . $n . $c);
}

Expand Down Expand Up @@ -283,7 +283,7 @@ protected function unwrapKeyV2V4(string $header, string $encoded): string
$Ak = sodium_crypto_generichash("\x81" . $n, $this->wrappingKey->raw());

// Step 3:
$t2 = sodium_crypto_generichash($header . $n . $c);
$t2 = sodium_crypto_generichash($header . $n . $c, $Ak);

// Step 4:
if (!hash_equals($t2, $t)) {
Expand Down