-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathencryption.go
54 lines (41 loc) · 1.16 KB
/
encryption.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
package main
import (
"errors"
)
// PacketEncrypter represents wrapper for encryption alg
type PacketEncrypter interface {
Encrypt(input []byte, output []byte, iv []byte) int
Decrypt(input []byte, output []byte) (int, error)
CheckSize(size int) bool
AdjustInputSize(size int) int
// OutputAdd returns number of bytes will be added after encryption
OutputAdd() int
// IVLen returns bytes needed to store IV or other state
IVLen() int
}
type newEncrypterFunc func(string) (PacketEncrypter, error)
var (
registeredEncrypters = make(map[string]newEncrypterFunc)
// predefined errors
ePacketSmall = errors.New("Packet too small")
ePacketNonIPv4 = errors.New("Non IPv4 packet")
ePacketInvalidSize = errors.New("Stored packet size bigger then packet itself")
)
func DecryptV4Chk(e PacketEncrypter, src []byte, dst []byte) (int, error) {
num, err := e.Decrypt(src, dst)
if nil != err {
return 0, err
}
// 2 bytes size + 20 bytes ip header
if num < 22 {
return 0, ePacketSmall
}
if (*IPPacket)(&dst).IPver() != 4 {
return 0, ePacketNonIPv4
}
size := (*IPPacket)(&dst).GetSize()
if size > num {
return 0, ePacketInvalidSize
}
return size, nil
}