/
acrablock.go
377 lines (329 loc) · 14.3 KB
/
acrablock.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
Copyright 2020, Cossack Labs Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package acrablock
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"github.com/cossacklabs/acra/acrastruct"
"github.com/cossacklabs/acra/utils"
"github.com/cossacklabs/themis/gothemis/cell"
"github.com/cossacklabs/themis/gothemis/keys"
)
// SymmetricBackend interface abstract backend for key and data encryption
type SymmetricBackend interface {
Encrypt(key []byte, data []byte, context []byte) ([]byte, error)
Decrypt(key []byte, data []byte, context []byte) ([]byte, error)
}
// SymmetricDataEncryptionKeyLength size for each new random symmetric key for new AcraBlock
const SymmetricDataEncryptionKeyLength = 32
// Set of constants with sizes of each part of AcraBlock
const (
TagBeginSize = 4
KeyEncryptionKeyTypeSize = 1
KeyEncryptionKeyIDSize = 2
DataEncryptionKeyLengthSize = 2
DataEncryptionTypeSize = 1
RestAcraBlockLengthSize = 8
AcraBlockMinSize = TagBeginSize + KeyEncryptionKeyTypeSize + KeyEncryptionKeyIDSize + DataEncryptionKeyLengthSize + DataEncryptionTypeSize + RestAcraBlockLengthSize
)
// ErrInvalidAcraBlock defines invalid AcraBlock error
var ErrInvalidAcraBlock = errors.New("invalid AcraBlock")
// SecureCellSymmetricBackend implement SymmetricBackend with SecureCell backend
type SecureCellSymmetricBackend struct{}
// Encrypt SecureCellSymmetricBackend implementation of SymmetricBackend interface for key and data encryption
func (s SecureCellSymmetricBackend) Encrypt(key []byte, data []byte, context []byte) (out []byte, err error) {
seal, err := cell.SealWithKey(&keys.SymmetricKey{Value: key})
if err != nil {
return nil, err
}
return seal.Encrypt(data, context)
}
// Decrypt SecureCellSymmetricBackend implementation of SymmetricBackend interface for key and data decryption
func (s SecureCellSymmetricBackend) Decrypt(key []byte, data []byte, context []byte) (out []byte, err error) {
seal, err := cell.SealWithKey(&keys.SymmetricKey{Value: key})
if err != nil {
return nil, err
}
return seal.Decrypt(data, context)
}
// KeyEncryptionBackendType used as storage for known backends to encrypt symmetric keys in AcraBLock
type KeyEncryptionBackendType uint8
// MarshalBinary encode backend type to bytes
func (k KeyEncryptionBackendType) MarshalBinary() (data []byte, err error) {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(k))
return b[:KeyEncryptionKeyTypeSize], nil
}
// Set of known backends for key encryption
const (
KeyEncryptionBackendTypeSecureCell KeyEncryptionBackendType = iota
)
const defaultKeyEncryptionBackendType = KeyEncryptionBackendTypeSecureCell
// map backend type value to implementation
var keyEncryptionBackendTypeMap = map[KeyEncryptionBackendType]SymmetricBackend{
KeyEncryptionBackendTypeSecureCell: SecureCellSymmetricBackend{},
}
// DataEncryptionBackendType used as storage for known backends to encrypt data in AcraBlock
type DataEncryptionBackendType uint8
// MarshalBinary encode backend type to bytes
func (t DataEncryptionBackendType) MarshalBinary() (data []byte, err error) {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(t))
return b[:DataEncryptionTypeSize], nil
}
// Set of known backends for data encryption in AcraBlock
const (
DataEncryptionBackendTypeSecureCell DataEncryptionBackendType = iota
)
const defaultDataEncryptionBackendType = DataEncryptionBackendTypeSecureCell
// map backend type value to implementation
var dataEncryptionBackendTypeMap = map[DataEncryptionBackendType]SymmetricBackend{
DataEncryptionBackendTypeSecureCell: SecureCellSymmetricBackend{},
}
// ErrDataEncryptionKeyGeneration used when can't generate random key with crypto.Rand
var ErrDataEncryptionKeyGeneration = errors.New("can't generate random data encryption key")
// KeyIDGenerator abstract logic to generate ID for symmetric key which will be placed in AcraBlock
type KeyIDGenerator interface {
GenerateKeyID(key, context []byte) ([]byte, error)
}
// Sha256KeyIDGenerator generate ID for key using sha256 hash function for key value and context
type Sha256KeyIDGenerator struct{}
// GenerateKeyID generate sha256 hash by provided key and context
func (s Sha256KeyIDGenerator) GenerateKeyID(key, context []byte) ([]byte, error) {
h := sha256.New()
h.Write(key)
h.Write(context)
return h.Sum(nil)[:KeyEncryptionKeyIDSize], nil
}
var defaultKeyIDGenerator KeyIDGenerator = Sha256KeyIDGenerator{}
// AcraBlock array of several parts: TagBegin[4] + LengthOfRestData[4] + KeyEncryptionKeyType[1] + KeyEncryptionKeyID[2] + DataEncryptionType[1] + DataEncryptionKeyLength[2] + EncryptedDataEncryptionKey[*] + EncryptedData[*]
type AcraBlock []byte
var tagBegin = acrastruct.TagBegin[:TagBeginSize]
// NewEmptyAcraBlock create empty block for desired length and filled TagBegin
func NewEmptyAcraBlock(length int) AcraBlock {
b := make([]byte, length)
copy(b[:len(tagBegin)], tagBegin)
return b
}
// SetKeyEncryptionKeyType place marshalled type into AcraBlock
func (b AcraBlock) SetKeyEncryptionKeyType(t KeyEncryptionBackendType) error {
kekEncryptionType, err := t.MarshalBinary()
// our MarshalBinary never return any error but we check it here for a future case if we change
// serialization algorithm that may return error
if err != nil {
return err
}
copy(b[KeyEncryptionKeyTypePosition:KeyEncryptionKeyTypePosition+KeyEncryptionKeyTypeSize], kekEncryptionType[:KeyEncryptionKeyTypeSize])
return nil
}
// SetKeyEncryptionKeyID place generated key id into AcraBlock
func (b AcraBlock) SetKeyEncryptionKeyID(key, context []byte, idGenerator KeyIDGenerator) error {
keyID, err := idGenerator.GenerateKeyID(key, context)
if err != nil {
return err
}
copy(b[KeyEncryptionKeyIDPosition:KeyEncryptionKeyIDPosition+KeyEncryptionKeyIDSize], keyID[:KeyEncryptionKeyIDSize])
return nil
}
func (b AcraBlock) getKeyEncryptionKeyID() ([]byte, error) {
if len(b) < KeyEncryptionKeyIDPosition+KeyEncryptionKeyIDSize {
return nil, ErrInvalidAcraBlock
}
return b[KeyEncryptionKeyIDPosition : KeyEncryptionKeyIDPosition+KeyEncryptionKeyIDSize], nil
}
// setEncryptedDataEncryptionKey place key length and key into AcraBlock
func (b AcraBlock) setEncryptedDataEncryptionKey(key []byte) error {
if len(b) < EncryptedDataEncryptionKeyPosition+len(key) {
return ErrInvalidAcraBlock
}
buf := [8]byte{}
binary.LittleEndian.PutUint32(buf[:], uint32(len(key)))
copy(b[DataEncryptionKeyLengthPosition:DataEncryptionKeyLengthPosition+DataEncryptionKeyLengthSize], buf[:DataEncryptionKeyLengthSize])
copy(b[EncryptedDataEncryptionKeyPosition:EncryptedDataEncryptionKeyPosition+len(key)], key)
return nil
}
// SetDataEncryptionType place marshalled type into AcraBlock
func (b AcraBlock) SetDataEncryptionType(t DataEncryptionBackendType) error {
dataEncryptionType, err := t.MarshalBinary()
if err != nil {
return err
}
copy(b[DataEncryptionTypePosition:DataEncryptionTypePosition+DataEncryptionTypeSize], dataEncryptionType[:DataEncryptionTypeSize])
return nil
}
// setEncryptedData place data into AcraBlock. Note: should be used only after setEncryptedDataEncryptionKey call
// because need to know key size before place data by AcraBlock.EncryptedDataEncryptionKeyLength call
func (b AcraBlock) setEncryptedData(data []byte) error {
if len(b) < EncryptedDataEncryptionKeyPosition {
return ErrInvalidAcraBlock
}
keySize := b.EncryptedDataEncryptionKeyLength()
if len(b) < EncryptedDataEncryptionKeyPosition+keySize {
return ErrInvalidAcraBlock
}
if n := copy(b[EncryptedDataEncryptionKeyPosition+keySize:], data); n != len(data) {
return ErrInvalidAcraBlock
}
return nil
}
// Build create final acraBlock by encryptedKey and encryptedData
func (b AcraBlock) Build(encryptedKey, encryptedData []byte) ([]byte, error) {
if err := b.setEncryptedDataEncryptionKey(encryptedKey); err != nil {
return nil, err
}
if err := b.setEncryptedData(encryptedData); err != nil {
return nil, err
}
sumLength := len(b) - TagBeginSize
sumLengthBuf := [8]byte{}
binary.LittleEndian.PutUint64(sumLengthBuf[:], uint64(sumLength))
copy(b[TagBeginSize:TagBeginSize+RestAcraBlockLengthSize], sumLengthBuf[:RestAcraBlockLengthSize])
return b, nil
}
// AcraBlock length parts constants
const (
RestAcraBlockLengthPosition = TagBeginSize
KeyEncryptionKeyTypePosition = RestAcraBlockLengthPosition + RestAcraBlockLengthSize
KeyEncryptionKeyIDPosition = KeyEncryptionKeyTypePosition + KeyEncryptionKeyTypeSize
DataEncryptionTypePosition = KeyEncryptionKeyIDPosition + KeyEncryptionKeyIDSize
DataEncryptionKeyLengthPosition = DataEncryptionTypePosition + DataEncryptionTypeSize
EncryptedDataEncryptionKeyPosition = DataEncryptionKeyLengthPosition + DataEncryptionKeyLengthSize
)
// KeyEncryptionBackend read SymmetricBackend by KeyEncryptionKeyTypePosition
func (b AcraBlock) KeyEncryptionBackend() SymmetricBackend {
return keyEncryptionBackendTypeMap[KeyEncryptionBackendType(b[KeyEncryptionKeyTypePosition])]
}
// DataEncryptionBackend read SymmetricBackend by DataEncryptionTypePosition
func (b AcraBlock) DataEncryptionBackend() SymmetricBackend {
return dataEncryptionBackendTypeMap[DataEncryptionBackendType(b[DataEncryptionTypePosition])]
}
// EncryptedDataEncryptionKeyLength return encryption key length of encrypted data
func (b AcraBlock) EncryptedDataEncryptionKeyLength() int {
return int(binary.LittleEndian.Uint16(b[DataEncryptionKeyLengthPosition : DataEncryptionKeyLengthPosition+DataEncryptionKeyLengthSize]))
}
// Decrypt AcraBlock using all keys sequentially until successful decryption and context
func (b AcraBlock) Decrypt(keys [][]byte, context []byte) ([]byte, error) {
keySize := b.EncryptedDataEncryptionKeyLength()
encryptedKey := b[EncryptedDataEncryptionKeyPosition : EncryptedDataEncryptionKeyPosition+keySize]
encryptedData := b[AcraBlockMinSize+keySize:]
keyEncryptionKeyBackend := b.KeyEncryptionBackend()
dataEncryptionBackend := b.DataEncryptionBackend()
blockKeyID, err := b.getKeyEncryptionKeyID()
if err != nil {
return nil, err
}
var dataEncryptionKey []byte
for _, key := range keys {
keyID, err := Sha256KeyIDGenerator{}.GenerateKeyID(key, context)
if err != nil {
return nil, err
}
if bytes.Equal(keyID, blockKeyID) {
decryptedKey, err := keyEncryptionKeyBackend.Decrypt(key, encryptedKey, context)
if err == nil {
dataEncryptionKey = decryptedKey
break
}
}
}
if dataEncryptionKey == nil {
return nil, ErrInvalidAcraBlock
}
decryptedData, err := dataEncryptionBackend.Decrypt(dataEncryptionKey, encryptedData, context)
utils.ZeroizeSymmetricKey(dataEncryptionKey)
if err != nil {
return nil, ErrInvalidAcraBlock
}
return decryptedData, nil
}
const (
// AcraBlock valid if correct TagBegin (1) + correct length of rest part (2) + known key encryption backend (3)
// + known data encryption backend (4)
validAcraBlockMask = 1 << 4
)
// ExtractAcraBlockFromData return AcraBlock that stored at start of data and return size in bytes of parsed AcraBlockLength
func ExtractAcraBlockFromData(data []byte) (int, AcraBlock, error) {
if len(data) < AcraBlockMinSize {
return 0, nil, ErrInvalidAcraBlock
}
validMask := 1
if bytes.Equal(data[:TagBeginSize], acrastruct.TagBegin[:TagBeginSize]) {
validMask <<= 1
}
restLength := binary.LittleEndian.Uint64(data[RestAcraBlockLengthPosition : RestAcraBlockLengthPosition+RestAcraBlockLengthSize])
if len(data) >= int(restLength+TagBeginSize) {
validMask <<= 1
}
_, ok := keyEncryptionBackendTypeMap[KeyEncryptionBackendType(data[KeyEncryptionKeyTypePosition])]
if ok {
validMask <<= 1
}
_, ok = dataEncryptionBackendTypeMap[DataEncryptionBackendType(data[DataEncryptionTypePosition])]
if ok {
validMask <<= 1
}
if validMask != validAcraBlockMask {
return 0, nil, ErrInvalidAcraBlock
}
length := TagBeginSize + restLength
return int(length), AcraBlock(data[:length]), nil
}
// NewAcraBlockFromData expects that whole data is one AcraBlock, validate and return, otherwise error
func NewAcraBlockFromData(data []byte) (AcraBlock, error) {
_, block, err := ExtractAcraBlockFromData(data)
if err != nil {
return nil, err
}
return block, nil
}
// CreateAcraBlock construct AcraBlock like
// tag_begin[4] + rest_sum_length[*] + kek_encryption_type[1] + kek_id[2] + data_encryption_type[1] + dek_length[2] + dek + encrypted_data
func CreateAcraBlock(data []byte, key []byte, context []byte) ([]byte, error) {
return CreateAcraBlockWithBackends(data, key, context, defaultKeyEncryptionBackendType, defaultDataEncryptionBackendType)
}
// CreateAcraBlockWithBackends create AcraBlock using specified encryption backends
func CreateAcraBlockWithBackends(data []byte, key []byte, context []byte, keyEncryptionBackend KeyEncryptionBackendType, dataEncryptionBackend DataEncryptionBackendType) ([]byte, error) {
dataEncryptionKey := make([]byte, 32)
n, err := rand.Read(dataEncryptionKey)
if err != nil {
return nil, err
}
if n != SymmetricDataEncryptionKeyLength {
return nil, ErrDataEncryptionKeyGeneration
}
dataEncryptor := dataEncryptionBackendTypeMap[dataEncryptionBackend]
encryptedData, err := dataEncryptor.Encrypt(dataEncryptionKey, data, context)
if err != nil {
return nil, err
}
keyEncryptionKeyEncryptor := keyEncryptionBackendTypeMap[keyEncryptionBackend]
encryptedDataEncryptionKey, err := keyEncryptionKeyEncryptor.Encrypt(key, dataEncryptionKey, context)
if err != nil {
return nil, err
}
utils.ZeroizeSymmetricKey(dataEncryptionKey)
acraBlock := NewEmptyAcraBlock(AcraBlockMinSize + len(encryptedData) + len(encryptedDataEncryptionKey))
if err := acraBlock.SetKeyEncryptionKeyType(keyEncryptionBackend); err != nil {
return nil, err
}
if err := acraBlock.SetKeyEncryptionKeyID(key, context, defaultKeyIDGenerator); err != nil {
return nil, err
}
if err := acraBlock.SetDataEncryptionType(dataEncryptionBackend); err != nil {
return nil, err
}
return acraBlock.Build(encryptedDataEncryptionKey, encryptedData)
}