]> Cypherpunks.ru repositories - govpn.git/blob - identity.go
Raise copyright years
[govpn.git] / identity.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package govpn
19
20 import (
21         "crypto/subtle"
22         "encoding/base64"
23         "encoding/binary"
24         "hash"
25         "log"
26         "sync"
27         "time"
28
29         "golang.org/x/crypto/blake2b"
30 )
31
32 const (
33         IDSize = 128 / 8
34 )
35
36 type PeerID [IDSize]byte
37
38 func (id PeerID) String() string {
39         return base64.RawStdEncoding.EncodeToString(id[:])
40 }
41
42 func (id PeerID) MarshalJSON() ([]byte, error) {
43         return []byte(`"` + id.String() + `"`), nil
44 }
45
46 type MACAndTimeSync struct {
47         mac hash.Hash
48         ts  int
49         l   sync.Mutex
50 }
51
52 type MACCache struct {
53         cache map[PeerID]*MACAndTimeSync
54         l     sync.RWMutex
55 }
56
57 func NewMACCache() *MACCache {
58         return &MACCache{cache: make(map[PeerID]*MACAndTimeSync)}
59 }
60
61 // Remove disappeared keys, add missing ones with initialized MACs.
62 func (mc *MACCache) Update(peers *map[PeerID]*PeerConf) {
63         mc.l.Lock()
64         for pid := range mc.cache {
65                 if _, exists := (*peers)[pid]; !exists {
66                         log.Println("Cleaning key:", pid)
67                         delete(mc.cache, pid)
68                 }
69         }
70         for pid, pc := range *peers {
71                 if _, exists := mc.cache[pid]; exists {
72                         mc.cache[pid].ts = pc.TimeSync
73                 } else {
74                         log.Println("Adding key", pid)
75                         mac, err := blake2b.New256(pid[:])
76                         if err != nil {
77                                 panic(err)
78                         }
79                         mc.cache[pid] = &MACAndTimeSync{
80                                 mac: mac,
81                                 ts:  pc.TimeSync,
82                         }
83                 }
84         }
85         mc.l.Unlock()
86 }
87
88 // If timeSync > 0, then XOR timestamp with the data.
89 func AddTimeSync(ts int, data []byte) {
90         if ts == 0 {
91                 return
92         }
93         buf := make([]byte, 8)
94         binary.BigEndian.PutUint64(buf, uint64(time.Now().Unix()/int64(ts)*int64(ts)))
95         for i := 0; i < 8; i++ {
96                 data[i] ^= buf[i]
97         }
98 }
99
100 // Try to find peer's identity (that equals to MAC)
101 // by taking first blocksize sized bytes from data at the beginning
102 // as plaintext and last bytes as cyphertext.
103 func (mc *MACCache) Find(data []byte) *PeerID {
104         if len(data) < 8*2 {
105                 return nil
106         }
107         buf := make([]byte, 8)
108         sum := make([]byte, 32)
109         mc.l.RLock()
110         for pid, mt := range mc.cache {
111                 copy(buf, data)
112                 AddTimeSync(mt.ts, buf)
113                 mt.l.Lock()
114                 mt.mac.Reset()
115                 mt.mac.Write(buf)
116                 mt.mac.Sum(sum[:0])
117                 mt.l.Unlock()
118                 if subtle.ConstantTimeCompare(sum[len(sum)-8:], data[len(data)-8:]) == 1 {
119                         ppid := PeerID(pid)
120                         mc.l.RUnlock()
121                         return &ppid
122                 }
123         }
124         mc.l.RUnlock()
125         return nil
126 }