-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
178 lines (135 loc) · 3.88 KB
/
encode.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
package qrsecrets
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"io"
"github.com/go-compile/rome"
"golang.org/x/crypto/argon2"
)
// Encode takes a public key to encrypt the metadata section
func (c *Container) Encode(w io.Writer, pub rome.PublicKey, masterKey []byte) error {
curve := CurveToID(pub.Name())
if curve == 0 {
return ErrCurveSupport
}
if curve != c.Curve {
return ErrCurveMissmatch
}
// Write magic number
if _, err := w.Write(MagicNumber); err != nil {
return err
}
// Format version
if _, err := w.Write([]byte{c.version}); err != nil {
return err
}
// Curve ID
if _, err := w.Write([]byte{byte(curve)}); err != nil {
return err
}
// Hash ID
if _, err := w.Write([]byte{byte(c.HashID)}); err != nil {
return err
}
if err := c.MetaData.Encode(w, c, pub); err != nil {
return err
}
return c.CipherText.Encode(w, c.MetaData, masterKey)
}
// Encode will write the metadata and encrypt it with ECIES
func (c *SectionMetaData) Encode(w io.Writer, container *Container, pub rome.PublicKey) error {
if len(c.Salt) != 32 {
return ErrSaltInvalid
}
// Create new buffer to write metadata section into
// later we will encrypt this data
buf := bytes.NewBuffer(nil)
// Write the salt to the buffer
if _, err := buf.Write(c.Salt); err != nil {
return err
}
memory := make([]byte, 4)
binary.BigEndian.PutUint32(memory, c.ArgonMemory)
if _, err := buf.Write(memory); err != nil {
return err
}
iterations := make([]byte, 4)
binary.BigEndian.PutUint32(iterations, c.ArgonIterations)
if _, err := buf.Write(iterations); err != nil {
return err
}
if _, err := buf.Write([]byte{c.ArgonParallelism}); err != nil {
return err
}
keyLen := make([]byte, 4)
binary.BigEndian.PutUint32(keyLen, c.ArgonKeyLen)
if _, err := buf.Write(keyLen); err != nil {
return err
}
padding := make([]byte, 4)
binary.BigEndian.PutUint32(padding, c.PaddingSize)
if _, err := buf.Write(padding); err != nil {
return err
}
hash := HashIDToFunc(container.HashID)
if hash == nil {
return ErrHashUnsupported
}
// TODO: add cipher option for encrypt
// Encrypt the metadata section using ECIES-AES256-SHA256 (or other specified hash function)
ciphertext, err := pub.Encrypt(buf.Bytes(), rome.CipherAES_GCM, hash(), rome.NewHKDF(hash, 32, nil))
if err != nil {
return err
}
cipherTextLen := make([]byte, 2)
binary.BigEndian.PutUint16(cipherTextLen, uint16(len(ciphertext)))
// Write the length of ciphertext
if _, err := w.Write(cipherTextLen); err != nil {
return err
}
// Write ciphertext to underlying writer
if _, err := w.Write(ciphertext); err != nil {
return err
}
return nil
}
// Encode encrypts and marshales the plaintext
func (c *SectionCipherText) Encode(w io.Writer, m *SectionMetaData, masterKey []byte) error {
key := argon2.Key(masterKey, m.Salt, m.ArgonIterations, m.ArgonMemory, m.ArgonParallelism, m.ArgonKeyLen)
block, err := aes.NewCipher(key)
if err != nil {
return err
}
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return err
}
// Append padding to the end of ciphertext and encrypt
ciphertext := aesgcm.Seal(nil, nonce, append(c.Plaintext, c.Padding...), nil)
ciphertext = append(nonce, ciphertext...)
cipherTextLen := make([]byte, 8)
binary.BigEndian.PutUint64(cipherTextLen, uint64(len(ciphertext)))
// Write the length of ciphertext
if _, err := w.Write(cipherTextLen); err != nil {
return err
}
// Write ciphertext to underlying writer
if _, err := w.Write(ciphertext); err != nil {
return err
}
return nil
}
// Marshal encodes the container and returns it in bytes
func (c *Container) Marshal(pub rome.PublicKey, masterKey []byte) (data []byte, err error) {
buf := bytes.NewBuffer(nil)
err = c.Encode(buf, pub, masterKey)
data = buf.Bytes()
return data, err
}