/
crypto.go
188 lines (137 loc) · 6.46 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright ArxanFintech Technology Ltd. 2017 All Rights Reserved.
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 primitives
import (
"errors"
"io"
)
var (
// ErrEncryption Error during encryption
ErrEncryption = errors.New("Error during encryption.")
// ErrDecryption Error during decryption
ErrDecryption = errors.New("Error during decryption.")
// ErrInvalidSecretKeyType Invalid Secret Key type
ErrInvalidSecretKeyType = errors.New("Invalid Secret Key type.")
// ErrInvalidPublicKeyType Invalid Public Key type
ErrInvalidPublicKeyType = errors.New("Invalid Public Key type.")
// ErrInvalidKeyParameter Invalid Key Parameter
ErrInvalidKeyParameter = errors.New("Invalid Key Parameter.")
// ErrInvalidNilKeyParameter Invalid Nil Key Parameter
ErrInvalidNilKeyParameter = errors.New("Invalid Nil Key Parameter.")
// ErrInvalidKeyGeneratorParameter Invalid Key Generator Parameter
ErrInvalidKeyGeneratorParameter = errors.New("Invalid Key Generator Parameter.")
)
// Parameters is common interface for all the parameters
type Parameters interface {
// GetRand returns the random generated associated to this parameters
GetRand() io.Reader
}
// CipherParameters is common interface to represent cipher parameters
type CipherParameters interface {
Parameters
}
// AsymmetricCipherParameters is common interface to represent asymmetric cipher parameters
type AsymmetricCipherParameters interface {
CipherParameters
// IsPublic returns true if the parameters are public, false otherwise.
IsPublic() bool
}
// PublicKey is common interface to represent public asymmetric cipher parameters
type PublicKey interface {
AsymmetricCipherParameters
}
// PrivateKey is common interface to represent private asymmetric cipher parameters
type PrivateKey interface {
AsymmetricCipherParameters
// GetPublicKey returns the associated public key
GetPublicKey() PublicKey
}
// KeyGeneratorParameters is common interface to represent key generation parameters
type KeyGeneratorParameters interface {
Parameters
}
// KeyGenerator defines a key generator
type KeyGenerator interface {
// Init initializes this generated using the passed parameters
Init(params KeyGeneratorParameters) error
// GenerateKey generates a new private key
GenerateKey() (PrivateKey, error)
}
// AsymmetricCipher defines an asymmetric cipher
type AsymmetricCipher interface {
// Init initializes this cipher with the passed parameters
Init(params AsymmetricCipherParameters) error
// Process processes the byte array given in input
Process(msg []byte) ([]byte, error)
}
// SecretKey defines a symmetric key
type SecretKey interface {
CipherParameters
}
// StreamCipher defines a stream cipher
type StreamCipher interface {
// Init initializes this cipher with the passed parameters
Init(forEncryption bool, params CipherParameters) error
// Process processes the byte array given in input
Process(msg []byte) ([]byte, error)
}
// KeySerializer defines a key serializer/deserializer
type KeySerializer interface {
// ToBytes converts a key to bytes
ToBytes(key interface{}) ([]byte, error)
// ToBytes converts bytes to a key
FromBytes([]byte) (interface{}, error)
}
// AsymmetricCipherSPI is a Service Provider Interface for AsymmetricCipher
type AsymmetricCipherSPI interface {
// NewAsymmetricCipherFromPrivateKey creates a new AsymmetricCipher for decryption from a secret key
NewAsymmetricCipherFromPrivateKey(priv PrivateKey) (AsymmetricCipher, error)
// NewAsymmetricCipherFromPublicKey creates a new AsymmetricCipher for encryption from a public key
NewAsymmetricCipherFromPublicKey(pub PublicKey) (AsymmetricCipher, error)
// NewAsymmetricCipherFromPublicKey creates a new AsymmetricCipher for encryption from a serialized public key
NewAsymmetricCipherFromSerializedPublicKey(pub []byte) (AsymmetricCipher, error)
// NewAsymmetricCipherFromPublicKey creates a new AsymmetricCipher for encryption from a serialized public key
NewAsymmetricCipherFromSerializedPrivateKey(priv []byte) (AsymmetricCipher, error)
// NewPrivateKey creates a new private key rand and default parameters
NewDefaultPrivateKey(rand io.Reader) (PrivateKey, error)
// NewPrivateKey creates a new private key from (rand, params)
NewPrivateKey(rand io.Reader, params interface{}) (PrivateKey, error)
// NewPublicKey creates a new public key from (rand, params)
NewPublicKey(rand io.Reader, params interface{}) (PublicKey, error)
// SerializePrivateKey serializes a private key
SerializePrivateKey(priv PrivateKey) ([]byte, error)
// DeserializePrivateKey deserializes to a private key
DeserializePrivateKey(bytes []byte) (PrivateKey, error)
// SerializePrivateKey serializes a private key
SerializePublicKey(pub PublicKey) ([]byte, error)
// DeserializePrivateKey deserializes to a private key
DeserializePublicKey(bytes []byte) (PublicKey, error)
}
// StreamCipherSPI is a Service Provider Interface for StreamCipher
type StreamCipherSPI interface {
GenerateKey() (SecretKey, error)
GenerateKeyAndSerialize() (SecretKey, []byte, error)
NewSecretKey(rand io.Reader, params interface{}) (SecretKey, error)
// NewStreamCipherForEncryptionFromKey creates a new StreamCipher for encryption from a secret key
NewStreamCipherForEncryptionFromKey(secret SecretKey) (StreamCipher, error)
// NewStreamCipherForEncryptionFromSerializedKey creates a new StreamCipher for encryption from a serialized key
NewStreamCipherForEncryptionFromSerializedKey(secret []byte) (StreamCipher, error)
// NewStreamCipherForDecryptionFromKey creates a new StreamCipher for decryption from a secret key
NewStreamCipherForDecryptionFromKey(secret SecretKey) (StreamCipher, error)
// NewStreamCipherForDecryptionFromKey creates a new StreamCipher for decryption from a serialized key
NewStreamCipherForDecryptionFromSerializedKey(secret []byte) (StreamCipher, error)
// SerializePrivateKey serializes a private key
SerializeSecretKey(secret SecretKey) ([]byte, error)
// DeserializePrivateKey deserializes to a private key
DeserializeSecretKey(bytes []byte) (SecretKey, error)
}