-
Notifications
You must be signed in to change notification settings - Fork 231
Metadata encryption bug #5037
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
Merged
paweljakubas
merged 3 commits into
master
from
paweljakubas/5017/metadata-encryption-bug
Mar 11, 2025
Merged
Metadata encryption bug #5037
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
| {-# LANGUAGE RecordWildCards #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| module Cryptography.KDF.PBKDF2Spec | ||
| ( spec | ||
|
|
@@ -35,7 +37,17 @@ import Test.Hspec | |
| , it | ||
| , shouldBe | ||
| ) | ||
| import Test.QuickCheck | ||
| ( Arbitrary (..) | ||
| , Property | ||
| , chooseInt | ||
| , oneof | ||
| , property | ||
| , vectorOf | ||
| , (===) | ||
| ) | ||
|
|
||
| import qualified Data.ByteString as BS | ||
| import qualified Data.Text as T | ||
| import qualified Data.Text.Encoding as T | ||
|
|
||
|
|
@@ -313,6 +325,17 @@ spec = do | |
| { key = "6BBCD65DA84D7BB1D214908270352A56FF25B375E53B7D3F237330666CCBF0DF" | ||
| , iv = "FD40C29618D438E3658388C52EB7974E" | ||
| } | ||
|
|
||
| describe "password and password padded with \NUL produce the same (key, iv) pair" $ | ||
| it "generateKey conf passwd salt == generateKey conf (passwd <> \NUL <> ...) salt" $ | ||
| property @(TestCaseSHA256 -> Property) $ | ||
| \(TestCase algo' iters' keyL' ivL' passwd' salt') -> do | ||
| let nulsToAdd = 64 - BS.length passwd' | ||
| let passwdPadded = passwd' <> BS.replicate nulsToAdd 0 | ||
| generateKey (PBKDF2Config algo' iters' keyL' ivL') passwd' salt' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| === | ||
| generateKey (PBKDF2Config algo' iters' keyL' ivL') passwdPadded salt' | ||
|
|
||
| where | ||
| toKeyIV TestCase {..} = | ||
| tohex (generateKey (PBKDF2Config algo iters keyL ivL) passwd salt) | ||
|
|
@@ -335,3 +358,25 @@ data OpenSSLOutput = OpenSSLOutput | |
| { key :: Text | ||
| , iv :: Text | ||
| } deriving (Show, Eq) | ||
|
|
||
| type TestCaseSHA256 = TestCase SHA256 | ||
|
|
||
| instance Arbitrary TestCaseSHA256 where | ||
| arbitrary = do | ||
| passwd' <- | ||
| BS.pack <$> do | ||
| len <- chooseInt (1, 63) | ||
| vectorOf len arbitrary | ||
| saltLen <- chooseInt (1,32) | ||
| salt' <- oneof | ||
| [ pure Nothing | ||
| , Just . BS.pack <$> vectorOf saltLen arbitrary | ||
| ] | ||
| pure $ TestCase | ||
| { algo = SHA256 | ||
| , iters = 10000 | ||
| , keyL = 32 | ||
| , ivL = 16 | ||
| , passwd = passwd' | ||
| , salt = salt' | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ import Data.ByteArray.Encoding | |
| import Data.ByteString | ||
| ( ByteString | ||
| ) | ||
| import Data.Char | ||
| ( isAlphaNum | ||
| ) | ||
| import Data.Either | ||
| ( isLeft | ||
| ) | ||
|
|
@@ -57,6 +60,8 @@ import Test.QuickCheck | |
|
|
||
| import qualified Cardano.Api as Cardano | ||
| import qualified Data.ByteString as BS | ||
| import qualified Data.ByteString.Char8 as B8 | ||
| import qualified Data.List as L | ||
| import qualified Data.Map.Strict as Map | ||
| import qualified Data.Text as T | ||
| import qualified Data.Text.Encoding as T | ||
|
|
@@ -433,10 +438,19 @@ spec = do | |
| fromHexToM :: Text -> Maybe ByteString | ||
| fromHexToM = rightToMaybe . convertFromBase Base16 . T.encodeUtf8 | ||
|
|
||
| newtype MetadataPassword = MetadataPassword {unPassword :: ByteString} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just use a generator and avoid creating types ? Just 2c |
||
| deriving (Eq, Show) | ||
|
|
||
| instance Arbitrary MetadataPassword where | ||
| arbitrary = do | ||
| pwdLen <- chooseInt (5,10) | ||
| chars <- vectorOf pwdLen arbitrary `suchThat` L.all isAlphaNum | ||
| pure $ MetadataPassword $ B8.pack chars | ||
|
|
||
| data TestingSetup = TestingSetup | ||
| { payload :: Cardano.TxMetadata | ||
| , password :: ByteString | ||
| , passwordOther :: ByteString | ||
| , password :: MetadataPassword | ||
| , passwordOther :: MetadataPassword | ||
| , salt :: ByteString | ||
| } deriving (Eq, Show) | ||
|
|
||
|
|
@@ -452,10 +466,8 @@ instance Arbitrary TestingSetup where | |
| arbitrary = do | ||
| msgNum <- chooseInt (1,10) | ||
| txts <- vectorOf msgNum (getMsg <$> arbitrary) | ||
| pwdLen1 <- chooseInt (5,10) | ||
| pwdLen2 <- chooseInt (5,10) | ||
| pwd1 <- BS.pack <$> vectorOf pwdLen1 arbitrary | ||
| pwd2 <- (BS.pack <$> vectorOf pwdLen2 arbitrary) `suchThat` (/= pwd1) | ||
| pwd1 <- arbitrary | ||
| pwd2 <- arbitrary `suchThat` (/= pwd1) | ||
| salt' <- BS.pack <$> vectorOf 8 arbitrary | ||
| let metadata toEncrypt = | ||
| Cardano.TxMetadata $ Map.fromList | ||
|
|
@@ -478,23 +490,23 @@ instance Arbitrary TestingSetup where | |
| } | ||
|
|
||
| prop_roundtrip :: TestingSetup -> Property | ||
| prop_roundtrip (TestingSetup payload' pwd' _ salt') = do | ||
| prop_roundtrip (TestingSetup payload' (MetadataPassword pwd') _ salt') = do | ||
| (mapLeft | ||
| (const ErrMissingValidEncryptionPayload) | ||
| (toMetadataEncrypted pwd' payload' (Just salt')) | ||
| >>= fromMetadataEncrypted pwd') | ||
| === Right payload' | ||
|
|
||
| prop_passphrase :: TestingSetup -> Expectation | ||
| prop_passphrase (TestingSetup payload' pwd1 pwd2 salt') = do | ||
| prop_passphrase (TestingSetup payload' (MetadataPassword pwd1) (MetadataPassword pwd2) salt') = do | ||
| (mapLeft | ||
| (const ErrMissingValidEncryptionPayload) | ||
| (toMetadataEncrypted pwd1 payload' (Just salt')) | ||
| >>= fromMetadataEncrypted pwd2) | ||
| `shouldSatisfy` isLeft | ||
|
|
||
| prop_structure_after_enc :: TestingSetup -> Expectation | ||
| prop_structure_after_enc (TestingSetup payload' pwd' _ salt') = do | ||
| prop_structure_after_enc (TestingSetup payload' (MetadataPassword pwd') _ salt') = do | ||
| let hasMsgWithList (Cardano.TxMetaText k, Cardano.TxMetaList _) = | ||
| k == cip83EncryptPayloadKey | ||
| hasMsgWithList _ = False | ||
|
|
||
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.
👍