aboutsummaryrefslogtreecommitdiffstats
path: root/src/Network/WireGuard/Internal/Types.hs
blob: 3409e2af270d628c209f65eee2a87c04f83a9e71 (plain) (blame)
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
module Network.WireGuard.Internal.Types
  ( Index
  , Counter
  , PeerId
  , PublicKey
  , PrivateKey
  , KeyPair
  , PresharedKey
  , Time
  , UdpPacket
  , TunPacket
  , EncryptedPayload
  , AuthTag
  , TAI64n
  , SessionKey(..)
  , WireGuardError(..)
  , getPeerId
  , farFuture
  ) where

import           Control.Exception          (Exception, SomeException)
import qualified Crypto.Noise.DH            as DH
import           Crypto.Noise.DH.Curve25519 (Curve25519)
import           Data.ByteArray             (ScrubbedBytes)
import qualified Data.ByteArray             as BA
import qualified Data.ByteString            as BS
import           Foreign.C.Types            (CTime (..))
import           Network.Socket             (SockAddr)
import           System.Posix.Types         (EpochTime)

import           Data.Word

type Index        = Word32
type Counter      = Word64
type PeerId       = BS.ByteString

type PublicKey    = DH.PublicKey Curve25519
type PrivateKey   = DH.SecretKey Curve25519
type KeyPair      = DH.KeyPair Curve25519
type PresharedKey = ScrubbedBytes

type Time         = EpochTime

type UdpPacket    = (BS.ByteString, SockAddr)
type TunPacket    = ScrubbedBytes

type EncryptedPayload = BS.ByteString
type AuthTag          = BS.ByteString
type TAI64n           = BS.ByteString

data SessionKey = SessionKey
                { sendKey :: !ScrubbedBytes
                , recvKey :: !ScrubbedBytes
                }

data WireGuardError
    = DecryptFailureError
    | DestinationNotReachableError
    | DeviceNotReadyError
    | EndPointUnknownError
    | HandshakeInitiationReplayError
    | InvalidIPPacketError
    | InvalidWGPacketError String
    | NoiseError SomeException
    | NonceReuseError
    | OutdatedPacketError
    | RemotePeerNotFoundError
    | SourceAddrBlockedError
    | UnknownIndexError
  deriving (Show)

instance Exception WireGuardError

getPeerId :: PublicKey -> PeerId
getPeerId = BA.convert . DH.dhPubToBytes

farFuture :: Time
farFuture = CTime maxBound