forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
178 lines (143 loc) · 4.69 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package net
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"golang.org/x/crypto/hkdf"
libp2p "gx/ipfs/QmUWER4r4qMvaCnX5zREcfyiWN7cXN9g3a7fkRqNz8qWPP/go-libp2p-crypto"
"io"
)
const (
// The version of the encryption algorithm used. Currently only 1 is supported
CiphertextVersion = 1
// Length of the serialized version in bytes
CiphertextVersionBytes = 4
// Length of the secret key used to generate the AES and MAC keys in bytes
SecretKeyBytes = 32
// Length of the AES key in bytes
AESKeyBytes = 32
// Length of the MAC key in bytes
MacKeyBytes = 32
// Length of the RSA encrypted secret key ciphertext in bytes
EncryptedSecretKeyBytes = 512
// Length of the MAC in bytes
MacBytes = 32
)
var (
// The ciphertext cannot be shorter than CiphertextVersionBytes + EncryptedSecretKeyBytes + aes.BlockSize + MacKeyBytes
ErrShortCiphertext = errors.New("Ciphertext is too short")
// The HMAC included in the ciphertext is invalid
ErrInvalidHmac = errors.New("Invalid Hmac")
// Satic salt used in the hdkf
Salt = []byte("OpenBazaar Encryption Algorithm")
)
func Encrypt(pubKey libp2p.PubKey, plaintext []byte) ([]byte, error) {
// Encrypt random secret key with RSA pubkey
secretKey := make([]byte, SecretKeyBytes)
rand.Read(secretKey)
encKey, err := pubKey.Encrypt(secretKey)
if err != nil {
return nil, err
}
// Derive MAC and AES keys from the secret key using hkdf
hash := sha256.New
hkdf := hkdf.New(hash, secretKey, Salt, nil)
aesKey := make([]byte, AESKeyBytes)
_, err = io.ReadFull(hkdf, aesKey)
if err != nil {
return nil, err
}
macKey := make([]byte, MacKeyBytes)
_, err = io.ReadFull(hkdf, macKey)
if err != nil {
return nil, err
}
// Encrypt message with the AES key
block, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
/* The IV needs to be unique, but not secure. Therefore it is common to
include it at the beginning of the ciphertext. */
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// Create the HMAC
mac := hmac.New(sha256.New, macKey)
mac.Write(ciphertext)
messageMac := mac.Sum(nil)
// Prepend the ciphertext with the encrypted secret key
ciphertext = append(encKey, ciphertext...)
// Prepend version
version := make([]byte, CiphertextVersionBytes)
binary.BigEndian.PutUint32(version, uint32(CiphertextVersion))
ciphertext = append(version, ciphertext...)
// Append the MAC
ciphertext = append(ciphertext, messageMac...)
return ciphertext, nil
}
func Decrypt(privKey libp2p.PrivKey, ciphertext []byte) ([]byte, error) {
version := getCipherTextVersion(ciphertext)
if version == CiphertextVersion {
return decryptV1(privKey, ciphertext)
} else {
return nil, errors.New("Unknown ciphertext version")
}
}
func decryptV1(privKey libp2p.PrivKey, ciphertext []byte) ([]byte, error) {
if len(ciphertext) < CiphertextVersionBytes+EncryptedSecretKeyBytes+aes.BlockSize+MacKeyBytes {
return nil, ErrShortCiphertext
}
// Decrypt the secret key using the RSA private key
secretKey, err := privKey.Decrypt(ciphertext[CiphertextVersionBytes : CiphertextVersionBytes+EncryptedSecretKeyBytes])
if err != nil {
return nil, err
}
// Derive the AES and MAC keys from the secret key using hdkf
hash := sha256.New
hkdf := hkdf.New(hash, secretKey, Salt, nil)
aesKey := make([]byte, AESKeyBytes)
_, err = io.ReadFull(hkdf, aesKey)
if err != nil {
return nil, err
}
macKey := make([]byte, MacKeyBytes)
_, err = io.ReadFull(hkdf, macKey)
if err != nil {
return nil, err
}
// Calculate the HMAC and verify it is correct
mac := hmac.New(sha256.New, macKey)
mac.Write(ciphertext[CiphertextVersionBytes+EncryptedSecretKeyBytes : len(ciphertext)-MacBytes])
messageMac := mac.Sum(nil)
if !hmac.Equal(messageMac, ciphertext[len(ciphertext)-MacBytes:]) {
return nil, ErrInvalidHmac
}
// Decrypt the AES ciphertext
block, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
ciphertext = ciphertext[CiphertextVersionBytes+EncryptedSecretKeyBytes : len(ciphertext)-MacBytes]
if len(ciphertext) < aes.BlockSize {
return nil, err
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same
stream.XORKeyStream(ciphertext, ciphertext)
plaintext := ciphertext
return plaintext, nil
}
func getCipherTextVersion(ciphertext []byte) uint32 {
return binary.BigEndian.Uint32(ciphertext[:CiphertextVersionBytes])
}