forked from packetflinger/q2admin-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
277 lines (233 loc) · 5.96 KB
/
crypto.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
)
/**
* Get a SHA256 hash of an input byte slice
*/
func DigestSHA256(input []byte) []byte {
hash := sha256.New()
_, _ = hash.Write(input)
checksum := hash.Sum(nil)
return checksum
}
/**
* Generate a private/public key pair of a certain bit length
*/
func GenerateKeys(bitlength int) bool {
// make the actual keys
privatekey, err := rsa.GenerateKey(rand.Reader, bitlength)
if err != nil {
fmt.Printf("Cannot generate RSA key\n")
return false
}
publickey := &privatekey.PublicKey
// dump to disk
var privBytes []byte = x509.MarshalPKCS1PrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privBytes,
}
privatePem, err := os.Create("private.pem")
if err != nil {
fmt.Printf("error when create private.pem: %s \n", err)
return false
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
fmt.Printf("error when encode private pem: %s \n", err)
return false
}
// dump public key to disk
pubBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
fmt.Printf("error when dumping publickey: %s \n", err)
return false
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubBytes,
}
publicPem, err := os.Create("public.pem")
if err != nil {
fmt.Printf("error when create public.pem: %s \n", err)
return false
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
fmt.Printf("error when encode public pem: %s \n", err)
return false
}
return true
}
/**
* Read an RSA private key into memory from the filesystem
*/
func LoadPrivateKey(keyfile string) (*rsa.PrivateKey, error) {
priv, err := os.ReadFile(keyfile)
if err != nil {
panic(err)
}
privPem, _ := pem.Decode(priv)
if privPem.Type != "RSA PRIVATE KEY" {
return nil, errors.New("not a private key file")
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(privPem.Bytes); err != nil {
if parsedKey, err = x509.ParsePKCS8PrivateKey(privPem.Bytes); err != nil { // note this returns type `interface{}`
return nil, errors.New("unable to parse private key file")
}
}
var privateKey *rsa.PrivateKey
var ok bool
privateKey, ok = parsedKey.(*rsa.PrivateKey)
if ok {
return privateKey, nil
}
return nil, errors.New("something went wrong")
}
/**
* Read an RSA public key from the filesystem and get
* it ready to use
*/
func LoadPublicKey(keyfile string) (*rsa.PublicKey, error) {
pub, err := os.ReadFile(keyfile)
if err != nil {
return nil, err
}
pubPem, _ := pem.Decode(pub)
if pubPem.Type == "PUBLIC KEY" {
public, _ := x509.ParsePKIXPublicKey(pubPem.Bytes)
return public.(*rsa.PublicKey), nil
}
if pubPem.Type == "RSA PUBLIC KEY" {
public, _ := x509.ParsePKCS1PublicKey(pubPem.Bytes)
return public, nil
}
return nil, errors.New("not a public key file")
}
/**
* Manually add padding to a slice to get it to a specific
* block size. The number of bytes required is the byte used
* for the actual padding
*/
func PKCS5Padding(input []byte, blockSize int) []byte {
padding := (blockSize - len(input)%blockSize)
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(input, padtext...)
}
func PrivateDecrypt(key *rsa.PrivateKey, ciphertext []byte) []byte {
plaintext, err := key.Decrypt(
nil,
ciphertext,
&rsa.OAEPOptions{Hash: 5}) // sha256 (https://pkg.go.dev/crypto#DecrypterOpts)
if err != nil {
fmt.Println(err)
return nil
}
return plaintext
}
func PublicEncrypt(key *rsa.PublicKey, plaintext []byte) []byte {
encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, key, plaintext)
if err != nil {
panic(err)
}
return encryptedBytes
}
/**
* Get a byte slice of random data (for generating keys)
*/
func RandomBytes(length int) []byte {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
return nil
}
return b
}
/**
* Change our AES key and IV. This should be called
* periodically.
*/
func RotateKeys(cl *Client) {
if !cl.Encrypted {
return
}
key := RandomBytes(AESBlockLength)
iv := RandomBytes(AESIVLength)
blob := append(key, iv...)
// Send immediately so old keys used for this message
WriteByte(SCMDKey, &cl.MessageOut)
WriteData(blob, &cl.MessageOut)
cl.SendMessages()
cl.AESKey = key
cl.AESIV = iv
}
/**
* Hash the plaintext, encrypt the resulting digest with our private key.
* Only our public key can decrypt, proving it's really us
*/
func Sign(key *rsa.PrivateKey, plaintext []byte) []byte {
hash := sha256.New()
_, _ = hash.Write(plaintext)
checksum := hash.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, key, 5, checksum)
if err != nil {
fmt.Println(err)
}
return signature
}
/**
* decrypt incoming messages using AES
*/
func SymmetricDecrypt(key []byte, nonce []byte, ciphertext []byte) ([]byte, int) {
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println("newcipher:", err)
}
plaintext := make([]byte, len(ciphertext))
cbc := cipher.NewCBCDecrypter(block, nonce)
cbc.CryptBlocks(plaintext, ciphertext)
padding := int(plaintext[len(plaintext)-1])
unpadded := plaintext[:len(plaintext)-padding]
return unpadded, len(unpadded)
}
/**
* Encrypt outgoing messages using AES
*/
func SymmetricEncrypt(key []byte, nonce []byte, plaintext []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println(err)
}
plaintext = PKCS5Padding(plaintext, AESBlockLength)
ciphertext := make([]byte, len(plaintext))
cbc := cipher.NewCBCEncrypter(block, nonce)
cbc.CryptBlocks(ciphertext, plaintext)
return ciphertext
}
/**
* Use a public key to decrypt a signature and compare it to hash of the content
*/
func VerifySignature(key *rsa.PublicKey, plaintext []byte, sig []byte) bool {
hash := sha256.New()
temp := plaintext
_, _ = hash.Write(temp)
checksum := hash.Sum(nil)
err := rsa.VerifyPKCS1v15(key, 5, checksum, sig)
if err == nil {
return true
}
fmt.Println(err)
return false
}