From 686dcc8b172b77e3e612ba4badbb88879d0f5599 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 27 Apr 2017 15:26:50 -0400 Subject: Leave the prevMessage out of Activity serialization to save BW. Do include it in the data that gets signed, so it can be recovered by trying each likely (recently seen) Activity as the prevMessage, and checking the signature. The UserState and DeveloperState already had the necessary state about recently seen hashes, so this does not impact data use. One tricky bit is that relayFromSocket needs to wait for the TMChan to be empty before calling restorePrevActivityHash. Otherwise, the hashes of items in the channel that have not been processed yet won't be tried. The TMChan is not really being used as a channel since only 1 item can be in it. It could be converted to a TMVar, but closeTMChan is used so I left it as a channel. Note that the server does not restore hashes of messages that pass through it; it's just a dumb relay. Sending a single key press now only needs 94 bytes of data to be sent, down from 169! --- Also switched to SHA512, since hashes are no longer being sent over the wire and so the larger size does not matter. SHA512 is slightly faster and more secure. This commit was sponsored by Ewen McNeill. --- ProtocolBuffers.hs | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) (limited to 'ProtocolBuffers.hs') diff --git a/ProtocolBuffers.hs b/ProtocolBuffers.hs index 6d02096..53dfca0 100644 --- a/ProtocolBuffers.hs +++ b/ProtocolBuffers.hs @@ -40,7 +40,9 @@ data MessageP a data ActivityP a = ActivityP { activityP :: Required 6 (Message a) - , prevAtivityP :: Optional 7 (Message HashP) + -- This is not included, because the hash is never actually sent + -- over the wire! + -- , prevAtivityP :: Optional 7 (Message HashP) , elapsedTimeP :: Required 8 (Message ElapsedTimeP) , activitySignatureP :: Required 9 (Message SignatureP) } @@ -63,12 +65,6 @@ data ControlActionP { sessionKeyRejectedP :: Required 15 (Message PublicKeyP) } deriving (Generic) -data HashP = HashP - { hashMethodP :: Required 16 (Value B.ByteString) - , hashValueP :: Required 17 (Value B.ByteString) - } - deriving (Generic) - data SignatureP = Ed25519SignatureP { ed25519SignatureP :: Required 18 (Value B.ByteString) } @@ -141,13 +137,12 @@ instance ProtocolBuffer (ActivityP p) (T.Activity t) => ProtocolBuffer (MessageP instance ProtocolBuffer p t => ProtocolBuffer (ActivityP p) (T.Activity t) where toProtocolBuffer t = ActivityP { activityP = putField $ toProtocolBuffer $ T.activity t - , prevAtivityP = putField $ fmap toProtocolBuffer $ T.prevActivity t , elapsedTimeP = putField $ toProtocolBuffer $ T.elapsedTime t , activitySignatureP = putField $ toProtocolBuffer $ T.activitySignature t } fromProtocolBuffer p = T.Activity { T.activity = fromProtocolBuffer $ getField $ activityP p - , T.prevActivity = fmap fromProtocolBuffer $ getField $ prevAtivityP p + , T.prevActivity = Nothing -- not sent over the wire , T.elapsedTime = fromProtocolBuffer $ getField $ elapsedTimeP p , T.activitySignature = fromProtocolBuffer $ getField $ activitySignatureP p } @@ -180,21 +175,6 @@ instance ProtocolBuffer ControlActionP T.ControlAction where fromProtocolBuffer p@(SessionKeyRejectedP {}) = T.SessionKeyRejected $ fromProtocolBuffer $ getField $ sessionKeyRejectedP p -instance ProtocolBuffer HashP T.Hash where - toProtocolBuffer t = HashP - { hashMethodP = putField $ case T.hashMethod t of - T.SHA256 -> "2" - T.SHA3 -> "3" - , hashValueP = putField $ val $ T.hashValue t - } - fromProtocolBuffer p = T.Hash - { T.hashMethod = case getField (hashMethodP p) of - "2" -> T.SHA256 - "3" -> T.SHA3 - _ -> T.SHA256 - , T.hashValue = Val $ getField $ hashValueP p - } - instance ProtocolBuffer SignatureP T.Signature where toProtocolBuffer (T.Ed25519Signature t) = Ed25519SignatureP { ed25519SignatureP = putField $ val t } @@ -252,8 +232,6 @@ instance Encode ControlP instance Decode ControlP instance Encode ControlActionP instance Decode ControlActionP -instance Encode HashP -instance Decode HashP instance Encode SignatureP instance Decode SignatureP instance Encode PublicKeyP -- cgit v1.2.3