~haskell-guy/ssb-haskell

ssb-haskell/src/Ssb/Peer/SecretHandshake.hs -rw-r--r-- 27.6 KiB
759d7bc4 — Haskell Guy README: Note where to post feedback 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
-- | This module implements Scuttlebutt's Secret Handshake.
--
-- For more information kindly refer the to protocol guide
-- https://ssbc.github.io/scuttlebutt-protocol-guide

-- | TODO: Take care of possible import loop
-- | TODO: Optimize handling of PublicKey (extractPublicKey)

module Ssb.Peer.SecretHandshake where

import           Protolude               hiding ( Identity )
import qualified Data.ByteString               as BS
import           Data.Default
import qualified Crypto.Hash.SHA256            as SHA256
import qualified Crypto.Saltine.Core.ScalarMult
                                               as ScalarMult
import qualified Crypto.Saltine.Class          as Nacl
import qualified Crypto.Saltine.Core.Auth      as Auth
import qualified Crypto.Saltine.Core.Box       as Box
import qualified Crypto.Saltine.Core.SecretBox as SecretBox
import qualified Crypto.Saltine.Core.Sign      as Sign

import           Ssb.Network
import           Ssb.Identity
import qualified Sodium

-- | ChallengeLength is the length of a challenge message in bytes
challengeLength :: Int
challengeLength = 64

-- | ClientAuthLength is the length of a clientAuth message in bytes
clientAuthLength :: Int
clientAuthLength = 16 + 32 + 64

-- | ServerAcceptLength is the length of a serverAccept message in bytes
serverAcceptLength :: Int
serverAcceptLength = 16 + 64

-- | MACLength is the length of a MAC in bytes
macLength :: Int
macLength = 16

-- | NetworkIdentifier defines which of the possible networks is being used.
-- Most traffic is on MainNet, and others may be used for testing purposes.
type NetworkIdentifier = ByteString

type SharedSecret = ScalarMult.GroupElement

-- | SharedSecrets are the result of Scuttlebutt's handshake
-- TODO: make shared secrets readable and showable
data SharedSecrets = SharedSecrets
    { network               :: NetworkIdentifier
    , secreta               :: Maybe PublicKey
    , secretA               :: Maybe PublicKey
    , secretb               :: Maybe PublicKey
    , secretB               :: Maybe PublicKey
    , secretab              :: SharedSecret
    , secretaB              :: SharedSecret
    , secretAb              :: SharedSecret
    , serverHMAC            :: Auth.Authenticator
    }

-- | ConnStatus defines the progress of the handshake.
data ConnStatus =
      StartingHandshake
    | AwaitingClientHello
    | AwaitingServerHello
    | AwaitingClientAuthentication
    | AwaitingServerAccept
    | HandshakeComplete
  deriving Show

-- | Message sent between Scuttlebutt peers.
-- TODO: Add encoding and processing of remaining messages
data Message =
      ClientHello       Auth.Authenticator -- | Client's HMAC
                        PublicKey          -- | Client's Ephemeral Public Key
                        NetworkIdentifier
    | ServerHello       Auth.Authenticator -- | Server's HMAC
                        PublicKey          -- | Server's Ephemeral Public Key
                        NetworkIdentifier
    -- TODO: Can this be renamed?
    | ClientAuthMessage ByteString         -- | Detached Signature A
                        PublicKey          -- | Client long term Public Key
    | ServerAccept      ByteString         -- | Detached Signature B

-- | ConnState holds important details during the connection process.
--
-- TODO: define a getter method for fields.  Is it possible to get the field
-- name for the error message?
data ConnState = ConnState
    { connState              :: ConnStatus
    , networkID              :: NetworkIdentifier
    , clientPrivateKey       :: Maybe PrivateKey
    , clientPublicKey        :: Maybe PublicKey
    , clientEphemeralPrivKey :: Maybe PrivateKey
    , clientEphemeralPubKey  :: Maybe PublicKey
    , clientHMAC             :: Maybe Auth.Authenticator
    , serverPrivateKey       :: Maybe PrivateKey
    , serverPublicKey        :: Maybe PublicKey
    , serverEphemeralPrivKey :: Maybe PrivateKey
    , serverEphemeralPubKey  :: Maybe PublicKey
    , serverHMAC             :: Maybe Auth.Authenticator
    , sharedSecretab         :: Maybe SharedSecret
    , sharedSecretaB         :: Maybe SharedSecret
    , sharedSecretAb         :: Maybe SharedSecret
    , detachedSignatureA     :: Maybe ByteString
    , detachedSignatureB     :: Maybe ByteString
    }

-- | TODO: confirm use of default
instance Default ConnState  where
  def = ConnState { connState              = StartingHandshake
                  , networkID              = ""
                  , clientPrivateKey       = def
                  , clientPublicKey        = def
                  , clientEphemeralPrivKey = def
                  , clientEphemeralPubKey  = def
                  , clientHMAC             = def
                  , serverPrivateKey       = def
                  , serverPublicKey        = def
                  , serverEphemeralPrivKey = def
                  , serverEphemeralPubKey  = def
                  , serverHMAC             = def
                  , sharedSecretab         = def
                  , sharedSecretaB         = def
                  , sharedSecretAb         = def
                  , detachedSignatureA     = def
                  , detachedSignatureB     = def
                  }

must :: Text -> Maybe a -> Either Text a
must field = maybeToEither ("missing " <> field)

-- | Create the state for initiating a Handshake given the Scuttlebutt User's key pair.
newClientConnState
    :: NetworkIdentifier
    -> Identity
    -> PublicKey
    -> IO ConnState
newClientConnState network clientID serverPubKey = do
  let clientPrivKey = Ssb.Identity.privateKey clientID
  let clientPubKey  = Ssb.Identity.publicKey clientID

  (ephPrivKey, ephPubKey) <- Box.newKeypair
  return $ def { connState              = StartingHandshake
               , networkID              = network
               , clientPrivateKey       = clientPrivKey
               , clientPublicKey        = Just clientPubKey
               , clientEphemeralPrivKey = Just $ PrivateKey (Nacl.encode ephPrivKey)
               , clientEphemeralPubKey  = Just $ PublicKey (Nacl.encode ephPubKey)
               , serverPublicKey        = Just serverPubKey
               }
-- | Create the state for initiating a Handshake given the Scuttlebutt User's key pair.
newServerConnState
    :: NetworkIdentifier
    -> Identity
    -> IO ConnState
newServerConnState network serverID = do
  let serverPrivKey = Ssb.Identity.privateKey serverID
  let serverPubKey  = Ssb.Identity.publicKey serverID

  (ephPrivKey, ephPubKey) <- Box.newKeypair
  return $ def { connState              = AwaitingClientHello
               , networkID              = network
               , serverEphemeralPrivKey = Just $ PrivateKey (Nacl.encode ephPrivKey)
               , serverEphemeralPubKey  = Just $ PublicKey (Nacl.encode ephPubKey)
               , serverPrivateKey       = serverPrivKey
               , serverPublicKey        = Just serverPubKey
               }

-- | Create shared secrets given the Handshake's final connection state.
newSharedSecrets :: ConnState -> Either Text SharedSecrets
newSharedSecrets state = do
  ssab <- must "secret key ab" $ sharedSecretab state
  ssaB <- must "secret key aB" $ sharedSecretaB state
  ssAb <- must "secret key Ab" $ sharedSecretAb state
  serverHMAC' <- must "secret HMAC" $ serverHMAC (state :: ConnState)
  return $ SharedSecrets { network    = networkID state
                         , secreta    = clientEphemeralPubKey state
                         , secretA    = clientPublicKey       state
                         , secretb    = serverEphemeralPubKey state
                         , secretB    = serverPublicKey       state
                         , secretab   = ssab
                         , secretaB   = ssaB
                         , secretAb   = ssAb
                         , serverHMAC = serverHMAC'
                         }

newClientAuthMessage :: ConnState -> Either Text Message
newClientAuthMessage state = do
  let network = networkID state
  cliLTPrivKey  <- must "client Private Key" $ clientPrivateKey state
  cliLTPubKey   <- must "client Public Key" $ clientPublicKey state
  srvLTPubKey   <- must "server Public Key" $ serverPublicKey state

  cliEphPrivKey <- must "client Private Key" $ clientEphemeralPrivKey state
  srvEphPubKey  <- must "server Ephemeral Public Key"
    $ serverEphemeralPubKey state

  ssab               <- must "shared secret ab" $ sharedSecretab state

  detachedSignatureA <- newDetachedSignatureA network
                                              srvLTPubKey
                                              ssab
                                              cliLTPrivKey

  return $ ClientAuthMessage detachedSignatureA
                             cliLTPubKey

newClientHello :: ConnState -> Either Text Message
newClientHello state = do
    cliEphPubKey <- maybeToEither noKeyMsg $ clientEphemeralPubKey state
    key          <- maybeToEither badNetMsg $ Nacl.decode (networkID state)
    let auth = Auth.auth key (extractPublicKey cliEphPubKey)
    return $ ClientHello auth cliEphPubKey (networkID state)
   where
    badNetMsg = "badly formatted Network Identifier"
    noKeyMsg  = "clientEphemeralKey required"

decodeClientHello :: ConnState -> ByteString -> Either Text Message
decodeClientHello state buf = do
  let network                 = networkID state
  let (hmacbuf, cliEphPubKey) = BS.splitAt 32 buf

  key  <- maybeToEither badNetMsg $ Nacl.decode network
  auth <- maybeToEither badHMACMsg $ Nacl.decode hmacbuf
  let msg = cliEphPubKey

  if Auth.verify key auth msg
    then Right $ ClientHello auth (PublicKey cliEphPubKey) network
    else Left badVerificationMsg
 where
  badNetMsg          = "badly formatted Network Identifier"
  badHMACMsg         = "badly formatted server HMAC"
  badPubKeyMsg       = "badly formatted server Public Key"
  badVerificationMsg = "verification failed"

-- TODO: check if its possible to change the function depending on the return type.

newServerHello :: ConnState -> Either Text Message
newServerHello state = do
    srvEphPubKey <- maybeToEither noKeyMsg $ serverEphemeralPubKey state
    key          <- maybeToEither badNetMsg $ Nacl.decode (networkID state)
    let auth = Auth.auth key (extractPublicKey srvEphPubKey)
    return $ ServerHello auth srvEphPubKey (networkID state)
   where
    badNetMsg = "badly formatted Network Identifier"
    noKeyMsg  = "clientEphemeralKey required"

decodeServerHello :: ConnState -> ByteString -> Either Text Message
decodeServerHello state buf = do
  let network                 = networkID state
  let (hmacbuf, srvEphPubKey) = BS.splitAt 32 buf

  key  <- maybeToEither badNetMsg $ Nacl.decode network
  auth <- maybeToEither badHMACMsg $ Nacl.decode hmacbuf
  let msg = srvEphPubKey

  if Auth.verify key auth msg
    then Right $ ServerHello auth (PublicKey srvEphPubKey) network
    else Left badVerificationMsg
 where
  badNetMsg          = "badly formatted Network Identifier"
  badHMACMsg         = "badly formatted server HMAC"
  badPubKeyMsg       = "badly formatted server Public Key"
  badVerificationMsg = "verification failed"

decodeClientAuthMessage :: ConnState -> ByteString -> Either Text Message
decodeClientAuthMessage state buf = do
    let network = networkID state
    serverPublicKey <- must "serverPublicKey" $ serverPublicKey state
    sharedSecretab  <- must "sharedSecretab" $ sharedSecretab state
    sharedSecretaB  <- must "sharedSecretaB" $ sharedSecretaB state

    key <-
        naclDecode "key"
        $  SHA256.hash
        $  network
        <> Nacl.encode sharedSecretab
        <> Nacl.encode sharedSecretaB
    let nonce = Nacl.zero
    msg3 <- maybeToEither "could not open secret box"
        $ SecretBox.secretboxOpen key nonce buf

    -- TODO: Make the client auth message length a constant
    msg3 <- if (BS.length msg3 == 96)
        then (return msg3)
        else (Left badMessageLength)
    let detachedSignatureA = BS.take 64 msg3
    clientLongTermPubKey   <- naclDecode "client Long Term Public Key"
        $ BS.drop 64 msg3

    let msg =
          (network :: ByteString)
          <> (extractPublicKey serverPublicKey)
          <> SHA256.hash (Nacl.encode sharedSecretab)
    if Sign.signVerifyDetached
        clientLongTermPubKey
        detachedSignatureA
        msg
      then Right state {connState = HandshakeComplete}
      else Left "client verification failed"

    return $ ClientAuthMessage
        detachedSignatureA
        (PublicKey $ Nacl.encode clientLongTermPubKey)
  where
    badMessageLength = "unexpected length of Client Authentication Message"
    naclDecode msg = maybeToEither msg . Nacl.decode

newServerAccept :: ConnState -> Either Text Message
newServerAccept state = do
      detachedSignatureB' <- maybeToEither noSigB (detachedSignatureB state)
      return $ ServerAccept detachedSignatureB'
   where
      noSigB  = "detachedSignatureB required"

decodeServerAccept :: ConnState -> ByteString -> Either Text Message
decodeServerAccept state buf = do
  let network = networkID state
  sharedSecretab <- must "sharedSecretab" $ sharedSecretab state
  sharedSecretaB <- must "sharedSecretaB" $ sharedSecretaB state
  sharedSecretAb <- must "sharedSecretAb" $ sharedSecretAb state

  key            <-
    naclDecode "key"
    $  SHA256.hash
    $  network
    <> Nacl.encode sharedSecretab
    <> Nacl.encode sharedSecretaB
    <> Nacl.encode sharedSecretAb
  let nonce = Nacl.zero

  detachedSignatureB <- secretBoxOpen key nonce buf
  return $ ServerAccept detachedSignatureB
 where
  naclDecode msg =
    maybeToEither ("could not decode " <> msg :: Text) . Nacl.decode
  secretBoxOpen key nonce msg =
    maybeToEither "could not open secret box"
      $ SecretBox.secretboxOpen key nonce msg

-- | generate a signature used in the Client Authentication
newDetachedSignatureA
  :: NetworkIdentifier
  -> Ssb.Identity.PublicKey
  -> SharedSecret
  -> PrivateKey
  -> Either Text ByteString
newDetachedSignatureA network serverLongTermPubKey sharedSecretab clientLongTermPrivKey
  = do
    clientLongTermPrivKey' <- maybeToEither badCliKeyMsg
      $  Nacl.decode $ extractPrivateKey clientLongTermPrivKey
    let secretChecksum = SHA256.hash $ Nacl.encode sharedSecretab
    let msg =
          (network :: ByteString)
            <> extractPublicKey serverLongTermPubKey
            <> (secretChecksum :: ByteString)
    return $ Sign.signDetached clientLongTermPrivKey' msg
 where
  badSrvKeyMsg = "badly encoded long term server public key"
  badCliKeyMsg = "badly encoded long term client private key"

calcSharedSecretab :: PrivateKey -> PublicKey -> Either Text SharedSecret
calcSharedSecretab cliEphPrivKey srvEphPubKey = do
  cliEphPrivKey' <- maybeToEither "badly formatted client ephemeral private key"
      $ Nacl.decode $ extractPrivateKey cliEphPrivKey
  srvEphPubKey' <- maybeToEither "badly formatted server ephemeral public key"
      $ Nacl.decode $ extractPublicKey srvEphPubKey
  return $ ScalarMult.mult cliEphPrivKey' srvEphPubKey'

-- | generate a signature used in the Server acknowledgement
newDetachedSignatureB
  :: NetworkIdentifier
  -> ByteString
  -> PublicKey
  -> SharedSecret
  -> PrivateKey
  -> Either Text ByteString
newDetachedSignatureB network detachedSignatureA clientPublicKey sharedSecretab serverPrivateKey = do
    key <- naclDecode badPrivkey $ extractPrivateKey serverPrivateKey
    let msg =
          (network :: ByteString)
          <> detachedSignatureA
          <> (extractPublicKey clientPublicKey)
          <> SHA256.hash (Nacl.encode sharedSecretab)
    return $ Sign.signDetached key msg
  where
    badPrivkey     = "badly formatted private key"
    naclDecode msg = maybeToEither msg . Nacl.decode


-- | Server Longterm PK should be converted to curve25519
-- Does not look like a problem given the Golang code
--  TODO: Implement type conversion here
clientCalcSharedSecretaB :: PrivateKey -> PublicKey -> Either Text SharedSecret
clientCalcSharedSecretaB clientEphemeralSK serverLongtermPK = do
  cliEphPrivKey' <- maybeToEither "badly formatted client ephemeral private key"
      $ Nacl.decode $ extractPrivateKey clientEphemeralSK
  srvLTPubKey' <- maybeToEither "badly formatted server long term public key"
      $ Nacl.decode $ extractPublicKey serverLongtermPK
  curvePublicKey <-
      maybeToEither "badly formatted curve25519"
      $ Nacl.decode . Nacl.encode $ Sodium.publicKeyToCurve25519 srvLTPubKey'
  return $ ScalarMult.mult cliEphPrivKey' curvePublicKey

serverCalcSharedSecretaB :: PrivateKey -> PublicKey -> Either Text SharedSecret
serverCalcSharedSecretaB serverLongtermSK clientEphemeralPK = do
  srvLTPrivKey' <- maybeToEither "badly formatted server long term private key"
      $ Nacl.decode $ extractPrivateKey serverLongtermSK
  cliEphPubKey' <- maybeToEither "badly formatted client ephemeral public key"
      $ Nacl.decode $ extractPublicKey clientEphemeralPK
  curvePrivKey <-
      maybeToEither "badly formatted curve25519"
      $ Nacl.decode . Nacl.encode $ Sodium.secretKeyToCurve25519 srvLTPrivKey'
  return $ ScalarMult.mult curvePrivKey cliEphPubKey'

calcSharedSecretAb :: PrivateKey -> PublicKey -> Either Text SharedSecret
calcSharedSecretAb clientLongTermPrivKey serverEphemeralPubKey = do
  cliLTPrivKey' <- naclDecode "badly formatted client long term private key"
                              $ extractPrivateKey clientLongTermPrivKey
  curveSecretKey <-
    naclDecode "badly formatted curve25519"
    . Nacl.encode
    $ Sodium.secretKeyToCurve25519 cliLTPrivKey'
  srvEphPubKey' <- naclDecode "badly formatted server ephemeral public key"
                              $ extractPublicKey serverEphemeralPubKey
  return $ ScalarMult.mult curveSecretKey srvEphPubKey'
  where naclDecode msg = maybeToEither msg . Nacl.decode

serverCalcSharedSecretAb
    :: PrivateKey
    -> PublicKey
    -> Either Text SharedSecret
serverCalcSharedSecretAb serverEphemeralPrivKey clientLongTermPubKey = do
    srvEphPrivKey' <- naclDecode "here bad formatted server long term private key"
                                $ extractPrivateKey serverEphemeralPrivKey
    cliLTPubKey' <- naclDecode "badly formatted client long term public key"
                              $ extractPublicKey clientLongTermPubKey
    curvePublicKey <-
      naclDecode "badly formatted curve25519"
        . Nacl.encode
        $ Sodium.publicKeyToCurve25519 cliLTPubKey'
    return $ ScalarMult.mult srvEphPrivKey' curvePublicKey
  where
    naclDecode msg = maybeToEither msg . Nacl.decode


-- | encode and serialize the message in preparation to send to peer.
encode :: ConnState -> Message -> Either Text ByteString
encode state msg = case msg of
  ClientHello auth pubKey network -> do
    cliEphPubKey <- maybeToEither noKeyMsg $ clientEphemeralPubKey state
    return $ Nacl.encode auth <> extractPublicKey cliEphPubKey
   where
    noKeyMsg  = "clientEphemeralKey required"
  ServerHello auth pubKey network -> do
    return $ Nacl.encode auth <> extractPublicKey pubKey
   where
    noKeyMsg  = "clientEphemeralKey required"
  ClientAuthMessage dSigA cliLTPubKey -> do
    let network        = networkID state
    ssab               <- must "shared secret ab" $ sharedSecretab state
    ssaB               <- must "shared secret aB" $ sharedSecretaB state

    key <-
      maybeToEither badKeyMsg
      $  Nacl.decode
      $  SHA256.hash
      $  network
      <> Nacl.encode ssab
      <> Nacl.encode ssaB
    let nonce = Nacl.zero
    let msg   = dSigA <> extractPublicKey cliLTPubKey
    return $ SecretBox.secretbox key nonce msg
    where badKeyMsg = "clientEphemeralKey required"
  ServerAccept detachedSignatureB -> do
      let network        = networkID state
      ssab               <- must "shared secret ab" $ sharedSecretab state
      ssaB               <- must "shared secret aB" $ sharedSecretaB state
      ssAb               <- must "shared secret Ab" $ sharedSecretAb state

      key <-
          maybeToEither badKeyMsg
          $ Nacl.decode
          $ SHA256.hash
          $ ((network :: ByteString)
          <> Nacl.encode ssab
          <> Nacl.encode ssaB
          <> Nacl.encode ssAb)
      let nonce = Nacl.zero
      let msg   = detachedSignatureB
      return $ SecretBox.secretbox key nonce msg
    where badKeyMsg = "clientEphemeralKey required"

-- | update the connection state and return any reponse message for the peer.
-- TODO: Process secretAb
process :: ConnState -> Message -> IO (Either Text (ConnState, Maybe Message))
process state (ClientHello hmac cliEphPubKey network) = do
  stateUpdate <- return $ do
    srvLTPrivKey  <- must "server Private Key"
        $ serverPrivateKey state
    srvEphPrivKey <- must "server ephemeral Private Key"
        $ serverEphemeralPrivKey state

    ssab        <- calcSharedSecretab srvEphPrivKey cliEphPubKey
    -- TODO: srvLTPubKey should be curved Process sk_to_curve25519
    ssaB        <- serverCalcSharedSecretaB srvLTPrivKey cliEphPubKey

    return $ state { connState             = AwaitingClientAuthentication
                   , clientEphemeralPubKey = Just cliEphPubKey
                   , serverHMAC            = Just hmac
                   , sharedSecretab        = Just ssab
                   , sharedSecretaB        = Just ssaB
                   }
  return $ stateUpdate >>= \state' -> case newServerHello state' of
    Right msg' -> return $ (state', Just msg')
    Left  err  -> Left err

process state (ServerHello hmac ephPubKey network) = do
  stateUpdate <- return $ do
    cliLTPrivKey  <- must "client Private Key" $ clientPrivateKey state
    cliEphPrivKey <- must "ephemeral client Private Key"
      $ clientEphemeralPrivKey state
    srvLTPubKey <- must "server Public Key" $ serverPublicKey state

    ssab        <- calcSharedSecretab cliEphPrivKey ephPubKey
    ssaB        <- clientCalcSharedSecretaB cliEphPrivKey srvLTPubKey
    ssAb        <- calcSharedSecretAb cliLTPrivKey ephPubKey

    return $ state { connState             = AwaitingServerAccept
                   , serverHMAC            = Just hmac
                   , serverEphemeralPubKey = Just ephPubKey
                   , sharedSecretab        = Just ssab
                   , sharedSecretaB        = Just ssaB
                   , sharedSecretAb        = Just ssAb
                   }
  return $ stateUpdate >>= \state' -> case newClientAuthMessage state' of
    Right msg' -> return $ (state', Just msg')
    Left  err  -> Left err

process state (ClientAuthMessage detachedSignatureA clientLongTermPubKey) = do
    stateUpdate <- return $ do
        let network = networkID state
        srvPrivKey <- must "server private key"
            $ serverPrivateKey state
        srvEphPrivKey <- must "server Long Term ephemeral private key"
            $ serverEphemeralPrivKey state
        sharedSecretAb <- serverCalcSharedSecretAb srvEphPrivKey clientLongTermPubKey
        sharedSecretab <- must "shared secret ab" $ sharedSecretab state

        detachedSignatureB <- newDetachedSignatureB
            network
            detachedSignatureA
            clientLongTermPubKey
            sharedSecretab
            srvPrivKey

        return $ state { connState          = HandshakeComplete
                       , detachedSignatureA = Just detachedSignatureA
                       , detachedSignatureB = Just detachedSignatureB
                       , clientPublicKey    = Just clientLongTermPubKey
                       , sharedSecretAb     = Just sharedSecretAb
                       }
    return $ stateUpdate >>= \state' -> case newServerAccept state' of
      Right msg' -> return $ (state', Just msg')
      Left  err  -> Left err

process state (ServerAccept dSigB) = do
  stateUpdate <- return $ do
    let network = networkID state
    cliLTPrivKey <- must "client private key" $ clientPrivateKey state
    cliLTPubKey <- must "client public key" $ clientPublicKey state
    srvLTPubKey <- must "server public key" $ serverPublicKey state
    ssab <- must "shared secret ab" $ sharedSecretab state

    detachedSignatureA <- newDetachedSignatureA network
                                                srvLTPubKey
                                                ssab
                                                cliLTPrivKey

    keyBuf <- must "server Public Key" $ serverPublicKey state
    key <- maybeToEither "badly formatted public key" $ Nacl.decode $ extractPublicKey keyBuf
    let msg = network <> detachedSignatureA <> extractPublicKey cliLTPubKey <> SHA256.hash (Nacl.encode ssab)

    if Sign.signVerifyDetached key dSigB msg
      then Right state {connState = HandshakeComplete}
      else Left "server verification failed"

  return $ case stateUpdate of
    Right state' -> return (state', Nothing)
    Left  err  -> Left err

-- TODO: Investigate a better way to separate network from handshake logic
type ReadFn = Int -> IO (Maybe ByteString)
type SendFn = ByteString -> IO ()

-- | readMsg decodes the next expected message from the byte stream.
readMsg :: ConnState -> ReadFn -> IO (Either Text Message)
readMsg state read = case connState state of
  AwaitingClientHello -> do
    mbuf <- read' challengeLength
    return $ do
      buf <- mbuf
      decodeClientHello state buf
  AwaitingServerHello -> do
    mbuf <- read' challengeLength
    return $ do
      buf <- mbuf
      decodeServerHello state buf
  AwaitingClientAuthentication -> do
    mbuf <- read' 112
    return $ do
      buf <- mbuf
      decodeClientAuthMessage state buf
  AwaitingServerAccept -> do
    mbuf <- read' serverAcceptLength
    return $ do
      buf <- mbuf
      decodeServerAccept state buf
  _ -> return $ Left "unknown state"
  where read' len = maybeToEither "connection broken" <$> read len

-- TODO: use Either instead
sendMsg :: SendFn -> ConnState -> Message -> IO ()
sendMsg send state msg = do
  case encode state msg of
    Left  err -> die err
    Right buf -> send buf

-- | startHandshake initializes the connection with the Scuttlebutt peer
-- returning the new shared secrets upon completion.
startHandshake
  :: SendFn
  -> ReadFn
  -> NetworkIdentifier
  -> Identity
  -> PublicKey
  -> IO (Either Text SharedSecrets)
startHandshake send recv network clientID srvPubKey = do
  state <- newClientConnState network clientID srvPubKey
  let clientHello = fromRight undefined (newClientHello state)
  let state'      = state { connState = AwaitingServerHello }
  finalState <- loop state' recv (Just clientHello)
  return $ finalState >>= newSharedSecrets
 where
  loop :: ConnState -> ReadFn -> Maybe Message -> IO (Either Text ConnState)
  loop state _    Nothing    = return . return $ state
  loop state recv (Just msg) = do
    sendMsg send state msg
    case connState state of
      HandshakeComplete -> return . return $ state
      _                 -> do
        resp <- readMsg state recv
        case resp of
          Left  err -> return $ Left err
          Right msg -> do
            res <- process state msg
            case res of
                Left err -> return $ Left ("handshake error while connecting to peer: " <> err)
                Right (state', msg') ->
                  loop state' recv msg'

welcomeHandshake
  :: SendFn
  -> ReadFn
  -> NetworkIdentifier
  -> Identity
  -> IO (Either Text SharedSecrets)
welcomeHandshake send recv network serverID = do
    state <- newServerConnState network serverID
    finalState <- loop state
    return $ finalState >>= newSharedSecrets
  where
    loop :: ConnState -> IO (Either Text ConnState)
    loop state = do
      msg <- readMsg state recv
      case msg of
          Left  err -> return $ Left err
          Right msg -> do
            res <-  process state msg
            case res of
                Left err -> return $ Left $ "handshake failed: " <> err
                Right (state', msg') ->
                    case msg' of
                        Nothing -> return $ Right state'
                        Just msg'' -> do

                          sendMsg send state' msg''
                          case connState state' of
                            HandshakeComplete -> return $ Right state'
                            _                 -> loop state'