Skip to content

Commit

Permalink
add EncryptMetadataMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Nov 24, 2023
1 parent 27fbebc commit 936a644
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3119,7 +3119,7 @@ toMetadataEncryptedKey
toMetadataEncryptedKey apiEncrypt =
fst $ generateKeyForMetadata (BA.convert $ unPassphrase passphrase) Nothing
where
(ApiEncryptMetadata (ApiT passphrase)) = apiEncrypt
(ApiEncryptMetadata (ApiT passphrase) _) = apiEncrypt

-- When encryption is enabled we do the following:
-- (a) recreate encrypted payload from chunks
Expand Down
28 changes: 26 additions & 2 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ module Cardano.Wallet.Api.Types
, DRep (..)
, DRepKeyHash (..)
, DRepScriptHash (..)
, EncryptMetadataMethod (..)

-- * API Types (Byron)
, ApiByronWallet (..)
Expand Down Expand Up @@ -1216,8 +1217,13 @@ data ApiMultiDelegationAction
deriving (Eq, Generic, Show)
deriving anyclass NFData

newtype ApiEncryptMetadata = ApiEncryptMetadata
{ passphrase :: ApiT (Passphrase "lenient") }
data EncryptMetadataMethod = AES256CBC | ChaChaPoly1305
deriving (Eq, Generic, Show)
deriving anyclass NFData

data ApiEncryptMetadata = ApiEncryptMetadata
{ passphrase :: ApiT (Passphrase "lenient")
, enc :: Maybe EncryptMetadataMethod }
deriving (Eq, Generic, Show)
deriving (FromJSON, ToJSON) via DefaultRecord ApiEncryptMetadata
deriving anyclass NFData
Expand Down Expand Up @@ -2258,6 +2264,24 @@ instance ToJSON ApiStakeKeyIndex where
instance FromJSON ApiStakeKeyIndex where
parseJSON val = ApiStakeKeyIndex <$> parseJSON val

instance ToJSON EncryptMetadataMethod where
toJSON AES256CBC = "base"
toJSON ChaChaPoly1305 = "chachapoly1305"
instance FromJSON EncryptMetadataMethod where
parseJSON t =
parseBase t <|> parseChaChaPoly1305 t
where
parseBase = withText "base" $ \txt ->
if txt == "base" then
pure AES256CBC
else
fail "'base' is expected."
parseChaChaPoly1305 = withText "ChaChaPoly1305" $ \txt ->
if txt == "chachapoly1305" then
pure ChaChaPoly1305
else
fail "'chachapoly1305' is expected."

instance ToJSON ApiVoteAction where
toJSON Abstain = "abstain"
toJSON NoConfidence = "no_confidence"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3392,7 +3392,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let metadataToBeEncrypted =
TxMetadataWithSchema TxMetadataNoSchema metadataRaw
let encryptMetadata =
ApiEncryptMetadata $ ApiT $ Passphrase "metadata-secret"
ApiEncryptMetadata (ApiT $ Passphrase "metadata-secret") Nothing
let payloadMetadata = Json [json|{
"encrypt_metadata": #{toJSON encryptMetadata},
"metadata": #{toJSON metadataToBeEncrypted}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5248,7 +5248,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let metadataToBeEncrypted =
TxMetadataWithSchema TxMetadataNoSchema metadataRaw
let encryptMetadata =
ApiEncryptMetadata $ ApiT $ Passphrase "metadata-secret"
ApiEncryptMetadata (ApiT $ Passphrase "metadata-secret") Nothing
let payload = Json [json|{
"encrypt_metadata": #{toJSON encryptMetadata},
"metadata": #{toJSON metadataToBeEncrypted}
Expand Down
8 changes: 7 additions & 1 deletion lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ import Cardano.Wallet.Api.Types
, DRep (..)
, DRepKeyHash (..)
, DRepScriptHash (..)
, EncryptMetadataMethod (..)
, Iso8601Time (..)
, KeyFormat (..)
, NtpSyncingStatus (..)
Expand Down Expand Up @@ -1988,8 +1989,13 @@ instance Arbitrary TxMetadataWithSchema where
<$> elements [TxMetadataNoSchema, TxMetadataDetailedSchema]
<*> arbitrary

instance Arbitrary EncryptMetadataMethod where
arbitrary = elements [AES256CBC, ChaChaPoly1305]

instance Arbitrary ApiEncryptMetadata where
arbitrary = ApiEncryptMetadata <$> arbitrary
arbitrary = ApiEncryptMetadata
<$> arbitrary
<*> arbitrary

instance Arbitrary DRep where
arbitrary = do
Expand Down
37 changes: 33 additions & 4 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1751,17 +1751,28 @@ x-transactionMetadata: &transactionMetadata
# propertyNames:
# pattern: '^[0-9]+$'

x-encryptionMethod: &encryptionMethod
type: string
enum:
- base
- chachapoly1305

x-encryptMetadata: &encryptMetadata
description: |
If used then metadata in transaction is going to be encrypted
via AEAD scheme using ChaCha20 and Poly1305 (see RFC 7539).
PBKDF2 password stretching is used to get a 32-byte symmetric key.
PBKDF2 encryption using HMAC with the hash algorithm SHA512 is employed here.
If used then metadata in transaction is going to be encrypted by either
(a) base which is AES 256 using CBC mode or
(b) AEAD scheme using ChaCha20 and Poly1305 (see RFC 7539).
PBKDF2 password stretching is used to get a 32-byte secret key and 16-byte iv
needed in (a) scheme.
PBKDF2 encryption algorithm using HMAC with the hash algorithm SHA256 is employed
using 10000 iterations. Nosalt is used at this moment. Randomized version with
salt is to be added in the future.
The encrypted metadata is going to be stored in blockchain as a consequence.
type: object
required:
- passphrase
properties:
enc: *encryptionMethod
passphrase: *lenientPassphrase

x-transactionTTL: &transactionTTL
Expand Down Expand Up @@ -4788,6 +4799,17 @@ x-errCreatedInvalidTransaction: &errCreatedInvalidTransaction
type: string
enum: ['created_invalid_transaction']

x-errInvalidMetadataDecryption: &errInvalidMetadataDecryption
<<: *responsesErr
title: invalid_metadata_decryption
properties:
message:
type: string
description: The encrypted metadata has either wrong structure, or cannot be decrypted or decoded.
code:
type: string
enum: ['invalid_metadata_decryption']

x-errForeignTransaction: &errForeignTransaction
<<: *responsesErr
title: foreign_transaction
Expand Down Expand Up @@ -5904,6 +5926,13 @@ x-responsesDecodedTransaction: &responsesDecodedTransaction
content:
application/json:
schema: *ApiDecodedTransaction
403:
description: Forbidden
content:
application/json:
schema:
oneOf:
- <<: *errInvalidMetadataDecryption

x-responsesSubmitTransaction: &responsesSubmitTransaction
<<: *responsesErr400
Expand Down

0 comments on commit 936a644

Please sign in to comment.