Skip to content

Commit

Permalink
Merge pull request #3 from dajiaji/fix-key-wrapping
Browse files Browse the repository at this point in the history
Fix bug on V2/V4 key wrapping.
  • Loading branch information
paragonie-security committed Sep 20, 2021
2 parents 7181b76 + 0a620cd commit 38c1b0a
Showing 1 changed file with 4 additions and 4 deletions.
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

0 comments on commit 38c1b0a

Please sign in to comment.