-
Notifications
You must be signed in to change notification settings - Fork 20
/
deterministicguid.go
97 lines (88 loc) · 2.87 KB
/
deterministicguid.go
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
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved.
*/
package main
import (
"bytes"
"encoding/binary"
"sort"
"unsafe"
"golang.org/x/crypto/blake2s"
"golang.org/x/sys/windows"
"golang.org/x/text/unicode/norm"
"github.com/amnezia-vpn/amneziawg-windows/conf"
)
const deterministicGUIDLabel = "Deterministic WireGuard Windows GUID v1 [email protected]"
const fixedGUIDLabel = "Fixed WireGuard Windows GUID v1 [email protected]"
// Escape hatch for external consumers, not us.
var UseFixedGUIDInsteadOfDeterministic = false
/* All peer public keys and allowed ips are sorted. Length/number fields are
* little endian 32-bit. Hash input is:
*
* label || len(interface name) || interface name ||
* interface public key || number of peers ||
* peer public key || number of peer allowed ips ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* ...
* peer public key || number of peer allowed ips ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* len(allowed ip string) || allowed ip/cidr in canonical string notation ||
* ...
* ...
*/
func deterministicGUID(c *conf.Config) *windows.GUID {
b2, _ := blake2s.New256(nil)
if !UseFixedGUIDInsteadOfDeterministic {
b2.Write([]byte(deterministicGUIDLabel))
} else {
b2.Write([]byte(fixedGUIDLabel))
}
b2Number := func(i int) {
if uint(i) > uint(^uint32(0)) {
panic("length out of bounds")
}
var bytes [4]byte
binary.LittleEndian.PutUint32(bytes[:], uint32(i))
b2.Write(bytes[:])
}
b2String := func(s string) {
bytes := []byte(s)
bytes = norm.NFC.Bytes(bytes)
b2Number(len(bytes))
b2.Write(bytes)
}
b2Key := func(k *conf.Key) {
b2.Write(k[:])
}
b2String(c.Name)
if !UseFixedGUIDInsteadOfDeterministic {
b2Key(c.Interface.PrivateKey.Public())
b2Number(len(c.Peers))
sortedPeers := c.Peers
sort.Slice(sortedPeers, func(i, j int) bool {
return bytes.Compare(sortedPeers[i].PublicKey[:], sortedPeers[j].PublicKey[:]) < 0
})
for _, peer := range sortedPeers {
b2Key(&peer.PublicKey)
b2Number(len(peer.AllowedIPs))
sortedAllowedIPs := peer.AllowedIPs
sort.Slice(sortedAllowedIPs, func(i, j int) bool {
if bi, bj := sortedAllowedIPs[i].Bits(), sortedAllowedIPs[j].Bits(); bi != bj {
return bi < bj
}
if sortedAllowedIPs[i].Cidr != sortedAllowedIPs[j].Cidr {
return sortedAllowedIPs[i].Cidr < sortedAllowedIPs[j].Cidr
}
return bytes.Compare(sortedAllowedIPs[i].IP[:], sortedAllowedIPs[j].IP[:]) < 0
})
for _, allowedip := range sortedAllowedIPs {
b2String(allowedip.String())
}
}
}
return (*windows.GUID)(unsafe.Pointer(&b2.Sum(nil)[0]))
}