/
crypto.go
166 lines (145 loc) · 4.28 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
package crypto
import (
"errors"
"github.com/HyperspaceApp/fastrand"
)
var (
// TypeDefaultRenter is the default CipherType that is used for
// encrypting pieces of uploaded data.
TypeDefaultRenter = TypeThreefish
// TypeDefaultWallet is the default CipherType that is used for
// wallet operations like encrypting the wallet files.
TypeDefaultWallet = TypeTwofish
// TypePlain means no encryption is used.
TypePlain = CipherType{0, 0, 0, 0, 0, 0, 0, 1}
// TypeTwofish is the type for the Twofish-GCM encryption.
TypeTwofish = CipherType{0, 0, 0, 0, 0, 0, 0, 2}
// TypeThreefish is the type for the Threefish encryption.
TypeThreefish = CipherType{0, 0, 0, 0, 0, 0, 0, 3}
)
var (
// ErrInvalidCipherType is returned upon encountering an unknown cipher
// type.
ErrInvalidCipherType = errors.New("provided cipher type is invalid")
)
type (
// CipherType is an identifier for the individual ciphers provided by this
// package.
CipherType [8]byte
// Ciphertext is an encrypted []byte.
Ciphertext []byte
// CipherKey is a key with Sia specific encryption/decryption methods.
CipherKey interface {
// Key returns the underlying key.
Key() []byte
// Type returns the type of the key.
Type() CipherType
// EncryptBytes encrypts the given plaintext and returns the
// ciphertext.
EncryptBytes([]byte) Ciphertext
// DecryptBytes decrypts the given ciphertext and returns the
// plaintext.
DecryptBytes(Ciphertext) ([]byte, error)
// DecryptBytesInPlace decrypts the given ciphertext and returns the
// plaintext. It will reuse the memory of the ciphertext which means
// that it's not save to use it after calling DecryptBytesInPlace.
DecryptBytesInPlace(Ciphertext) ([]byte, error)
// Derive derives a child cipherkey given a provided chunk index and
// piece index.
Derive(chunkIndex, pieceIndex uint64) CipherKey
}
)
// String creates a string representation of a CipherType that can be converted
// into a type with FromString.
func (ct CipherType) String() string {
switch ct {
case TypePlain:
return "plaintext"
case TypeTwofish:
return "twofish-gcm"
case TypeThreefish:
return "threefish512"
default:
panic(ErrInvalidCipherType)
}
}
// FromString reads a CipherType from a string.
func (ct *CipherType) FromString(s string) error {
switch s {
case "plaintext":
*ct = TypePlain
case "twofish-gcm":
*ct = TypeTwofish
case "threefish512":
*ct = TypeThreefish
default:
return ErrInvalidCipherType
}
return nil
}
// Overhead reports the overhead produced by a CipherType in bytes.
func (ct CipherType) Overhead() uint64 {
switch ct {
case TypePlain, TypeThreefish:
return 0
case TypeTwofish:
return twofishOverhead
default:
panic(ErrInvalidCipherType)
}
}
// NewWalletKey is a helper method which is meant to be used only if the type
// and entropy are guaranteed to be valid. In the wallet this is always the
// case since we always use hashes as the entropy and we don't read the key
// from file.
func NewWalletKey(entropy Hash) CipherKey {
sk, err := NewSiaKey(TypeDefaultWallet, entropy[:])
if err != nil {
panic(err)
}
return sk
}
// NewSiaKey creates a new SiaKey from the provided type and entropy.
func NewSiaKey(ct CipherType, entropy []byte) (CipherKey, error) {
switch ct {
case TypePlain:
return plainTextCipherKey{}, nil
case TypeTwofish:
return newTwofishKey(entropy)
case TypeThreefish:
return newThreefishKey(entropy)
default:
return nil, ErrInvalidCipherType
}
}
// GenerateSiaKey creates a new SiaKey from the provided type and
// entropy.
func GenerateSiaKey(ct CipherType) CipherKey {
switch ct {
case TypePlain:
return plainTextCipherKey{}
case TypeTwofish:
return generateTwofishKey()
case TypeThreefish:
return generateThreefishKey()
default:
panic(ErrInvalidCipherType)
}
}
// IsValidCipherType returns true if ct is a known CipherType and false
// otherwise.
func IsValidCipherType(ct CipherType) bool {
switch ct {
case TypePlain, TypeTwofish, TypeThreefish:
return true
default:
return false
}
}
// RandomCipherType is a helper function for testing. It's located in the
// crypto package to centralize all the types within one file to make future
// changes to them easy.
func RandomCipherType() CipherType {
types := []CipherType{TypePlain, TypeTwofish}
return types[fastrand.Intn(len(types))]
}