-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencrypt.go
157 lines (138 loc) · 3.54 KB
/
encrypt.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
/*
* Copyright (c) 2019 Zenichi Amano
*
* This file is part of http-ece, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
package httpece
import (
"crypto/cipher"
"errors"
"fmt"
)
// Encrypt encrypts plaintext data.
func Encrypt(plaintext []byte, opts ...Option) ([]byte, error) {
var opt *options
var err error
// Options
if opt, err = parseOptions(encrypt, opts); err != nil {
return nil, err
}
// Check Record Size
if opt.rs < sizeRecordMin || opt.rs > sizeRecordMax {
return nil, fmt.Errorf("invalid record size: %d", opt.rs)
}
debug.dumpBinary("receiver public key", opt.dh)
debug.dumpBinary("sender private key", opt.private)
// Generate salt
saltLen := len(opt.salt)
if saltLen == 0 {
if opt.salt, err = randomSalt(); err != nil {
return nil, err
}
} else if saltLen != keyLen {
return nil, fmt.Errorf("the salt parameter must be %d bytes", keyLen)
}
// Save the DH public key in the header unless keyId is set.
if opt.encoding == AES128GCM && len(opt.keyID) == 0 {
opt.keyID = opt.public
}
// Derive key and nonce.
key, baseNonce, err := deriveKeyAndNonce(opt)
if err != nil {
return nil, err
}
gcm, err := createCipher(key)
if err != nil {
return nil, err
}
// Check Record Size
overhead := opt.encoding.Padding()
if opt.encoding == AES128GCM {
overhead += gcm.Overhead()
}
if opt.rs <= overhead {
return nil, fmt.Errorf("the rs parameter has to be greater than %d", overhead)
}
// Calculate chunkSize.
chunkSize := opt.rs
start := 0
counter := 0
plaintextLen := len(plaintext)
chunkSize -= opt.encoding.Padding()
if opt.encoding == AES128GCM {
chunkSize -= gcm.Overhead()
}
results := make([][]byte, 1+(plaintextLen+chunkSize-1)/chunkSize)
// Create header.
results, err = writeHeader(opt, results)
if err != nil {
return nil, err
}
// Encrypt records.
last := false
for !last {
end := start + chunkSize
switch opt.encoding {
case AES128GCM:
last = end >= plaintextLen
break
case AESGCM:
last = end > plaintextLen
break
}
if end > plaintextLen {
end = plaintextLen
}
// Generate nonce.
nonce := generateNonce(baseNonce, counter)
debug.dumpBinary("nonce", nonce)
r := encryptRecord(opt, gcm, nonce, plaintext[start:end], last)
results = append(results, r)
debug.dumpBinary("result", r)
start = end
counter++
}
return resultsJoin(results), nil
}
func encryptRecord(opt *options, gcm cipher.AEAD, nonce []byte, plaintext []byte, last bool) []byte {
plaintextWithPadding := appendPad(plaintext, opt.encoding, last)
return gcm.Seal(nil, nonce, plaintextWithPadding, nil)
}
func appendPad(plaintext []byte, encoding ContentEncoding, last bool) []byte {
plaintextLen := len(plaintext)
result := make([]byte, plaintextLen+encoding.Padding())
switch encoding {
case AESGCM:
copy(result, []byte{0x00, 0x00})
copy(result[2:], plaintext)
default:
copy(result, plaintext)
if last {
result[plaintextLen] = 0x02
} else {
result[plaintextLen] = 0x01
}
}
return result
}
func writeHeader(opt *options, results [][]byte) ([][]byte, error) {
switch opt.encoding {
case AES128GCM:
keyIDLen := len(opt.keyID)
if keyIDLen > keyIDLenMax {
return nil, errors.New("keyId is too large")
}
saltLen := len(opt.salt)
buffer := make([]byte, saltLen+4+1+keyIDLen)
copy(buffer, opt.salt)
copy(buffer[saltLen:], uint32ToBytes(opt.rs))
buffer[saltLen+4] = uint8(keyIDLen)
copy(buffer[saltLen+5:], opt.keyID)
results = append(results, buffer)
return results, nil
default:
// No header on other versions
return results, nil
}
}