This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
encrypter.go
172 lines (140 loc) · 4.19 KB
/
encrypter.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
package fte
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/binary"
"errors"
"io"
"math"
"github.com/ooni/psiphon/oopsi/github.com/redjack/marionette/ecb"
)
// _MAC_LENGTH = AES.block_size
// _IV_LENGTH = 7
// _MSG_COUNTER_LENGTH = 8
// _CTXT_EXPANSION = 1 + _IV_LENGTH + _MSG_COUNTER_LENGTH + _MAC_LENGTH
var (
ErrShortCiphertext = errors.New("fte: short ciphertext")
ErrInvalidMessageLength = errors.New("fte: invalid message length")
ErrHMACVerificationFailed = errors.New("fte: hmac verification failed")
)
var (
K1 = []byte("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff")
K2 = []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
)
const _IV_LENGTH = 7
type Encrypter struct {
block cipher.Block
blockMode cipher.BlockMode
IV []byte
}
func NewEncrypter() (*Encrypter, error) {
blk, err := aes.NewCipher(K1)
if err != nil {
return nil, err
}
return &Encrypter{
block: blk,
blockMode: ecb.NewEncrypter(blk),
}, nil
}
func (enc *Encrypter) Encrypt(plaintext []byte) ([]byte, error) {
plaintextN := len(plaintext)
// Read random bytes for initialization vector.
iv := make([]byte, _IV_LENGTH)
if len(enc.IV) == _IV_LENGTH {
copy(iv, enc.IV)
} else {
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
}
// Encrypt length as AES ECB.
iv1 := []byte{'\x01'}
iv1 = append(iv1, iv...)
iv1 = append(iv1, u64tob(uint64(plaintextN))...)
W1 := make([]byte, len(iv1))
enc.blockMode.CryptBlocks(W1, iv1)
// Encrypt plaintext with AES CTR.
iv2 := []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x02")
iv2 = append(iv2, iv...)
stream := cipher.NewCTR(enc.block, iv2)
padN := aes.BlockSize - (plaintextN % aes.BlockSize)
plaintext = append(plaintext, make([]byte, padN)...)
W2 := make([]byte, len(plaintext))
stream.XORKeyStream(W2, plaintext)
W2, plaintext = W2[:plaintextN], plaintext[:plaintextN]
// Concatenate both sections.
ciphertext := append(W1[:len(W1):len(W1)], W2...)
// Sign the message & limit size to AES block size.
mac := hmac.New(sha512.New, K2)
mac.Write(ciphertext)
T := mac.Sum(nil)
T = T[:aes.BlockSize]
return append(ciphertext, T...), nil
}
type Decrypter struct {
block cipher.Block
blockMode cipher.BlockMode
}
func NewDecrypter() (*Decrypter, error) {
blk, err := aes.NewCipher(K1)
if err != nil {
return nil, err
}
return &Decrypter{
block: blk,
blockMode: ecb.NewDecrypter(blk),
}, nil
}
func (dec *Decrypter) Decrypt(ciphertext []byte) ([]byte, error) {
if len(ciphertext) < 16 {
return nil, ErrShortCiphertext
}
// Decrypt header.
L := make([]byte, 16)
dec.block.Decrypt(L, ciphertext[:16])
plaintext_length := binary.BigEndian.Uint64(L[8:16])
if plaintext_length > math.MaxUint32 {
return nil, ErrInvalidMessageLength
}
ciphertext_length := plaintext_length + CTXT_EXPANSION
if len(ciphertext) < int(ciphertext_length) {
return nil, ErrShortCiphertext
}
ciphertext = ciphertext[:ciphertext_length:ciphertext_length]
W1 := ciphertext[0:aes.BlockSize:aes.BlockSize]
W2 := ciphertext[aes.BlockSize : aes.BlockSize+plaintext_length : aes.BlockSize+plaintext_length]
T_start := aes.BlockSize + plaintext_length
T_end := aes.BlockSize + plaintext_length + aes.BlockSize
T_expected := ciphertext[T_start:T_end:T_end]
// Sign the message & limit size to AES block size.
mac := hmac.New(sha512.New, K2)
mac.Write(append(W1, W2...))
if !hmac.Equal(mac.Sum(nil)[:aes.BlockSize], T_expected) {
return nil, ErrHMACVerificationFailed
}
// Decrypt ciphertext with AES CTR.
iv := make([]byte, aes.BlockSize)
dec.block.Decrypt(iv, W1)
iv2 := make([]byte, aes.BlockSize)
iv2[8] = '\x02'
copy(iv2[9:], iv[1:8])
stream := cipher.NewCTR(dec.block, iv2)
plaintext := make([]byte, plaintext_length)
stream.XORKeyStream(plaintext, W2)
return plaintext, nil
}
func (dec *Decrypter) CiphertextLen(ciphertext []byte) int {
L := make([]byte, 16)
dec.block.Decrypt(L, ciphertext[:16])
return int(binary.BigEndian.Uint32(L[12:16])) + CTXT_EXPANSION
}
// u64tob returns the big endian representation of a uint64 value.
func u64tob(i uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, i)
return b
}