-
Notifications
You must be signed in to change notification settings - Fork 82
/
crypto.go
113 lines (97 loc) · 3.54 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
// Copyright 2018 Shift Devices AG
//
// 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 crypto
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"io"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)
// pad pads the given bytes to the AES block size.
func pad(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
// unpad unpads the given bytes from the AES block size.
func unpad(src []byte) ([]byte, error) {
length := len(src)
unpadding := int(src[length-1])
if unpadding > length {
return nil, errp.New("Unpad error, which can happen when incorrect encryption key is used.")
}
return src[:(length - unpadding)], nil
}
// Encrypt encrypts the given bytes with the given encryption key.
func Encrypt(bytes []byte, encryptionKey []byte) ([]byte, error) {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, errp.WithStack(err)
}
paddedBytes := pad(bytes)
encryptedBytes := make([]byte, aes.BlockSize+len(paddedBytes))
iv := encryptedBytes[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, errp.WithStack(err)
}
cipher.NewCBCEncrypter(block, iv).CryptBlocks(encryptedBytes[aes.BlockSize:], paddedBytes)
return encryptedBytes, nil
}
// EncryptThenMAC encrypts the given bytes with the given encryption key and appends an HMAC of the
// encrypted bytes with the given authentication key.
func EncryptThenMAC(bytes []byte, encryptionKey []byte, authenticationKey []byte) ([]byte, error) {
encryptedBytes, err := Encrypt(bytes, encryptionKey)
if err != nil {
return nil, err
}
mac := hmac.New(sha256.New, authenticationKey)
_, err = mac.Write(encryptedBytes)
if err != nil {
return nil, errp.WithStack(err)
}
return mac.Sum(encryptedBytes), nil
}
// Decrypt decrypts the given bytes with the given encryption key.
func Decrypt(bytes []byte, encryptionKey []byte) ([]byte, error) {
if (len(bytes) % aes.BlockSize) != 0 {
return nil, errp.New("The length of the encrypted bytes has to match the AES block size.")
}
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, errp.WithStack(err)
}
iv := bytes[:aes.BlockSize]
decryptedBytes := bytes[aes.BlockSize:]
cipher.NewCBCDecrypter(block, iv).CryptBlocks(decryptedBytes, decryptedBytes)
return unpad(decryptedBytes)
}
// MACThenDecrypt authenticates the HMAC of the given bytes with the given authentication key and
// then decrypts the encrypted bytes with the given encryption key.
func MACThenDecrypt(bytes []byte, encryptionKey []byte, authenticationKey []byte) ([]byte, error) {
mac := hmac.New(sha256.New, authenticationKey)
encryptedBytes := bytes[:len(bytes)-sha256.Size]
appendedHMAC := bytes[len(bytes)-sha256.Size:]
_, err := mac.Write(encryptedBytes)
if err != nil {
return nil, errp.WithStack(err)
}
if !hmac.Equal(appendedHMAC, mac.Sum(nil)) {
return nil, errp.New("The appended HMAC is wrong.")
}
return Decrypt(encryptedBytes, encryptionKey)
}