forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecipher_aes.go
67 lines (54 loc) · 1.66 KB
/
decipher_aes.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
package provider
import (
"context"
"crypto/aes"
"crypto/cipher"
"errors"
"github.com/grafana/grafana/pkg/services/encryption"
)
type aesDecipher struct {
algorithm string
}
func (d aesDecipher) Decrypt(_ context.Context, payload []byte, secret string) ([]byte, error) {
if len(payload) < encryption.SaltLength {
return nil, errors.New("unable to compute salt")
}
salt := payload[:encryption.SaltLength]
key, err := encryption.KeyToBytes(secret, string(salt))
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
switch d.algorithm {
case encryption.AesGcm:
return decryptGCM(block, payload)
default:
return decryptCFB(block, payload)
}
}
func decryptGCM(block cipher.Block, payload []byte) ([]byte, error) {
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := payload[encryption.SaltLength : encryption.SaltLength+gcm.NonceSize()]
ciphertext := payload[encryption.SaltLength+gcm.NonceSize():]
return gcm.Open(nil, nonce, ciphertext, nil)
}
func decryptCFB(block cipher.Block, payload []byte) ([]byte, error) {
// The IV needs to be unique, but not secure. Therefore, it's common to
// include it at the beginning of the ciphertext.
if len(payload) < aes.BlockSize {
return nil, errors.New("payload too short")
}
iv := payload[encryption.SaltLength : encryption.SaltLength+aes.BlockSize]
payload = payload[encryption.SaltLength+aes.BlockSize:]
payloadDst := make([]byte, len(payload))
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(payloadDst, payload)
return payloadDst, nil
}