Skip to content

Commit

Permalink
cabal: Add a flag to enable/disable scrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jul 20, 2021
1 parent defbc26 commit f3d4d11
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/core/cardano-wallet-core.cabal
Expand Up @@ -17,6 +17,10 @@ flag release
default: False
manual: True

flag scrypt
description: Enable compatibility support for legacy wallet passwords.
default: True

library
default-language:
Haskell2010
Expand All @@ -29,6 +33,9 @@ library
-fwarn-redundant-constraints
if (flag(release))
ghc-options: -O2 -Werror
if (flag(scrypt))
cpp-options: -DHAVE_SCRYPT
build-depends: scrypt
build-depends:
aeson
, async
Expand Down Expand Up @@ -93,7 +100,6 @@ library
, retry
, safe
, scientific
, scrypt
, servant
, servant-client
, servant-server
Expand Down
6 changes: 6 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -3249,6 +3249,12 @@ instance IsServerError ErrWithRootKey where
, "to encrypt the root private key of the given wallet: "
, toText wid
]
ErrWithRootKeyWrongPassphrase wid ErrScryptUnsupported ->
apiError err501 WrongEncryptionPassphrase $ mconcat
[ "This build is not compiled with support for the "
, "legacy scrypt scheme used by the given wallet: "
, toText wid
]

instance IsServerError ErrListAssets where
toServerError = \case
Expand Down
18 changes: 14 additions & 4 deletions lib/core/src/Cardano/Wallet/Primitive/AddressDerivation.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
Expand Down Expand Up @@ -156,14 +157,17 @@ import Safe
( readMay, toEnumMay )

import qualified Cardano.Address.Script as CA
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.Scrypt as Scrypt
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Data.Text.Encoding as T

#ifdef HAVE_SCRYPT
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.Scrypt as Scrypt
#endif

{-------------------------------------------------------------------------------
HD Hierarchy
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -610,13 +614,17 @@ checkPassphrase scheme received stored = do
unless (constantTimeEq (encryptPassphrase prepared salt) stored) $
Left ErrWrongPassphrase
EncryptWithScrypt -> do
#ifdef HAVE_SCRYPT
let msg = Scrypt.Pass
$ CBOR.toStrictByteString
$ CBOR.encodeBytes
$ BA.convert prepared
if Scrypt.verifyPass' msg (Scrypt.EncryptedPass (getHash stored))
then Right ()
else Left ErrWrongPassphrase
#else
Left ErrScryptUnsupported
#endif
where
getSalt :: Hash purpose -> Either ErrWrongPassphrase (Passphrase "salt")
getSalt (Hash bytes) = do
Expand All @@ -629,7 +637,9 @@ checkPassphrase scheme received stored = do
BA.convert @_ @ScrubbedBytes a == BA.convert @_ @ScrubbedBytes b

-- | Indicate a failure when checking for a given 'Passphrase' match
data ErrWrongPassphrase = ErrWrongPassphrase
data ErrWrongPassphrase
= ErrWrongPassphrase
| ErrScryptUnsupported
deriving stock (Show, Eq)

-- | Little trick to be able to provide our own "random" salt in order to
Expand Down

0 comments on commit f3d4d11

Please sign in to comment.