-
Notifications
You must be signed in to change notification settings - Fork 0
/
xipher_crypto.go
164 lines (152 loc) · 4.94 KB
/
xipher_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
package xipher
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"dev.shib.me/xipher/crypto/asx"
"dev.shib.me/xipher/crypto/xcp"
)
func newVariableKeySymmCipher(key []byte) (*xcp.SymmetricCipher, error) {
if len(key) == secretKeyBaseLength {
keySum := sha256.Sum256(key)
key = keySum[:]
}
return xcp.New(key)
}
func (secretKey *SecretKey) NewEncryptingWriter(dst io.Writer, compress bool) (writer io.WriteCloser, err error) {
if isPwdBased(secretKey.keyType) {
if _, err := dst.Write([]byte{ctPwdSymmetric}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write ciphertext type", "xipher")
}
if _, err := dst.Write(secretKey.spec.bytes()); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write kdf spec", "xipher")
}
} else {
if _, err := dst.Write([]byte{ctKeySymmetric}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write ciphertext type", "xipher")
}
}
if secretKey.symmCipher == nil {
if secretKey.symmCipher, err = newVariableKeySymmCipher(secretKey.key); err != nil {
return nil, err
}
}
return secretKey.symmCipher.NewEncryptingWriter(dst, compress)
}
// EncryptStream encrypts src with the secret key treating it as a symmetric key and writes to dst.
func (secretKey *SecretKey) EncryptStream(dst io.Writer, src io.Reader, compress bool) (err error) {
encryptedWriter, err := secretKey.NewEncryptingWriter(dst, compress)
if err != nil {
return err
}
if _, err = io.Copy(encryptedWriter, src); err != nil {
return err
}
return encryptedWriter.Close()
}
// Encrypt encrypts data with the secret key treating it as a symmetric key.
func (secretKey *SecretKey) Encrypt(data []byte, compress bool) (ciphertext []byte, err error) {
var buf bytes.Buffer
if err = secretKey.EncryptStream(&buf, bytes.NewReader(data), compress); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compress bool) (writer io.WriteCloser, err error) {
if isPwdBased(publicKey.keyType) {
if _, err := dst.Write([]byte{ctPwdAsymmetric}); err != nil {
return nil, err
}
if _, err := dst.Write(publicKey.spec.bytes()); err != nil {
return nil, err
}
} else {
if _, err := dst.Write([]byte{ctKeyAsymmetric}); err != nil {
return nil, err
}
}
return publicKey.publicKey.NewEncryptingWriter(dst, compress)
}
// EncryptStream encrypts src with the public key and writes to dst.
func (publicKey *PublicKey) EncryptStream(dst io.Writer, src io.Reader, compress bool) (err error) {
encryptedWriter, err := publicKey.NewEncryptingWriter(dst, compress)
if err != nil {
return err
}
if _, err = io.Copy(encryptedWriter, src); err != nil {
return err
}
return encryptedWriter.Close()
}
// Encrypt encrypts data with the public key.
func (publicKey *PublicKey) Encrypt(data []byte, compress bool) (ciphertext []byte, err error) {
var buf bytes.Buffer
if err = publicKey.EncryptStream(&buf, bytes.NewReader(data), compress); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (secretKey *SecretKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
ctTypeBytes := make([]byte, 1)
if _, err := io.ReadFull(src, ctTypeBytes); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read ciphertext type", "xipher")
}
var ctType uint8 = ctTypeBytes[0]
key := secretKey.key
switch ctType {
case ctKeyAsymmetric, ctKeySymmetric:
if isPwdBased(secretKey.keyType) {
return nil, errDecryptionFailedKeyRequired
}
case ctPwdAsymmetric, ctPwdSymmetric:
if !isPwdBased(secretKey.keyType) {
return nil, errDecryptionFailedPwdRequired
}
specBytes := make([]byte, kdfSpecLength)
if _, err := io.ReadFull(src, specBytes); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read kdf spec", "xipher")
}
spec, err := parseKdfSpec(specBytes)
if err != nil {
return nil, err
}
key = secretKey.getKeyForPwdSpec(*spec)
default:
return nil, errInvalidCiphertext
}
switch ctType {
case ctKeyAsymmetric, ctPwdAsymmetric:
asxPrivKey, err := asx.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return asxPrivKey.NewDecryptingReader(src)
case ctKeySymmetric, ctPwdSymmetric:
symmCipher, err := newVariableKeySymmCipher(key)
if err != nil {
return nil, err
}
return symmCipher.NewDecryptingReader(src)
}
return nil, errInvalidCiphertext
}
// DecryptStream decrypts src and writes to dst.
func (secretKey *SecretKey) DecryptStream(dst io.Writer, src io.Reader) (err error) {
decryptedReader, err := secretKey.NewDecryptingReader(src)
if err != nil {
return err
}
if _, err = io.Copy(dst, decryptedReader); err != nil {
return err
}
return decryptedReader.Close()
}
// Decrypt decrypts the given ciphertext and returns the decrypted data.
func (secretKey *SecretKey) Decrypt(ciphertext []byte) (data []byte, err error) {
var buf bytes.Buffer
if err = secretKey.DecryptStream(&buf, bytes.NewReader(ciphertext)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}