diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-26 13:14:02 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-26 13:14:02 +0200 |
commit | 9d806d3853c926df75e83966d2c4f832708a1b08 (patch) | |
tree | 97dd50a56751d5cb48b60353697aa9ccad9a3e14 /src/cookie.go | |
parent | cf3a5130d3aa53fc56c7c3194ee326d5a1d21970 (diff) |
Begin work on outbound packet flow
Diffstat (limited to 'src/cookie.go')
-rw-r--r-- | src/cookie.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/cookie.go b/src/cookie.go new file mode 100644 index 0000000..a6987a2 --- /dev/null +++ b/src/cookie.go @@ -0,0 +1,39 @@ +package main + +import ( + "errors" + "golang.org/x/crypto/blake2s" +) + +func CalculateCookie(peer *Peer, msg []byte) { + size := len(msg) + + if size < blake2s.Size128*2 { + panic(errors.New("bug: message too short")) + } + + startMac1 := size - (blake2s.Size128 * 2) + startMac2 := size - blake2s.Size128 + + mac1 := msg[startMac1 : startMac1+blake2s.Size128] + mac2 := msg[startMac2 : startMac2+blake2s.Size128] + + peer.mutex.RLock() + defer peer.mutex.RUnlock() + + // set mac1 + + func() { + mac, _ := blake2s.New128(peer.macKey[:]) + mac.Write(msg[:startMac1]) + mac.Sum(mac1[:0]) + }() + + // set mac2 + + if peer.cookie != nil { + mac, _ := blake2s.New128(peer.cookie) + mac.Write(msg[:startMac2]) + mac.Sum(mac2[:0]) + } +} |