-
Notifications
You must be signed in to change notification settings - Fork 3
/
encryption.go
164 lines (123 loc) · 3.67 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package core
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
"github.com/awnumar/memguard"
"github.com/denisbrodbeck/machineid"
"github.com/rs/zerolog/log"
)
func tryContainerMachineID() (machineID string, err error) {
// Ref: https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container
cgroupFile, err := os.Open("/proc/self/cgroup")
if err != nil {
return machineID, fmt.Errorf("cannot open cgroup: %w", err)
}
defer cgroupFile.Close()
var buff bytes.Buffer
_, err = buff.ReadFrom(cgroupFile)
if err != nil {
return machineID, fmt.Errorf("cannot read cgroup: %w", err)
}
for _, line := range strings.Split(buff.String(), "\n") {
if strings.Contains(line, "/docker/") {
parts := strings.Split(line, "/docker/")
if len(parts) != 2 {
return machineID, fmt.Errorf("not a valid docker container id: %w", err)
}
machineID = parts[1]
break
}
}
if machineID == "" {
return machineID, fmt.Errorf("docker container id not found: %w", err)
}
return machineID, nil
}
func CreateHash(key string) string {
log.Debug().Msg("create hash")
id, errID := machineid.ProtectedID("sts-wire")
if errID != nil {
if strings.Contains(errID.Error(), "open /etc/machine-id: no such file or directory") {
// TODO: get a unique uuid for containers:
// Ref: https://github.com/denisbrodbeck/machineid/issues/5
// Ref: https://github.com/panta/machineid/blob/master/id_linux.go
id, errID = tryContainerMachineID()
if errID != nil {
id = "notAMachine"
log.Debug().Str("machineID", id).Msg("Cannot find a docker container id")
} else {
log.Debug().Str("machineID", id).Msg("Found docker container id")
}
} else {
panic(errID)
}
}
hasher := hmac.New(md5.New, []byte(id))
_, errWrite := hasher.Write([]byte(key))
if errWrite != nil {
panic(errWrite)
}
return hex.EncodeToString(hasher.Sum(nil))
}
func Encrypt(data []byte, password *memguard.Enclave) []byte {
log.Debug().Msg("encryption - open enclave")
passphrase, errOpenEnclave := password.Open()
if errOpenEnclave != nil {
memguard.SafePanic(errOpenEnclave)
}
defer passphrase.Destroy() // Destroy the copy when we return
log.Debug().Msg("encryption - create cipher")
block, errNewCiper := aes.NewCipher([]byte(CreateHash(string(passphrase.Bytes()))))
if errNewCiper != nil {
panic(errNewCiper)
}
log.Debug().Msg("encryption - create block")
gcm, errNewGCM := cipher.NewGCM(block)
if errNewGCM != nil {
panic(errNewGCM.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
log.Debug().Msg("encryption - encode")
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext
}
func Decrypt(data []byte, password *memguard.Enclave) []byte {
log.Debug().Msg("decryption - open enclave")
passphrase, errOpenEnclave := password.Open()
if errOpenEnclave != nil {
memguard.SafePanic(errOpenEnclave)
}
defer passphrase.Destroy() // Destroy the copy when we return
log.Debug().Msg("decryption - create key")
key := []byte(CreateHash(string(passphrase.Bytes())))
log.Debug().Msg("decryption - create cipher")
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
log.Debug().Msg("decryption - create block")
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
log.Debug().Msg("decryption - decode")
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err.Error())
}
return plaintext
}