/
attachments.go
155 lines (135 loc) · 3.95 KB
/
attachments.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
// Copyright (c) 2020 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package attachment
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
)
var (
HashMismatch = errors.New("mismatching SHA-256 digest")
UnsupportedVersion = errors.New("unsupported Matrix file encryption version")
UnsupportedAlgorithm = errors.New("unsupported JWK encryption algorithm")
InvalidKey = errors.New("failed to decode key")
InvalidInitVector = errors.New("failed to decode initialization vector")
)
const (
keyLength = 32
hashLength = 32
ivLength = 16
)
var (
keyBase64Length = base64.RawURLEncoding.EncodedLen(keyLength)
hashBase64Length = base64.RawStdEncoding.EncodedLen(hashLength)
ivBase64Length = base64.RawStdEncoding.EncodedLen(ivLength)
)
type JSONWebKey struct {
Key string `json:"k"`
Algorithm string `json:"alg"`
Extractable bool `json:"ext"`
KeyType string `json:"kty"`
KeyOps []string `json:"key_ops"`
}
type EncryptedFileHashes struct {
SHA256 string `json:"sha256"`
}
type decodedKeys struct {
key [keyLength]byte
iv [ivLength]byte
}
type EncryptedFile struct {
Key JSONWebKey `json:"key"`
InitVector string `json:"iv"`
Hashes EncryptedFileHashes `json:"hashes"`
Version string `json:"v"`
decoded *decodedKeys `json:"-"`
}
func NewEncryptedFile() *EncryptedFile {
key, iv := genA256CTR()
return &EncryptedFile{
Key: JSONWebKey{
Key: base64.RawURLEncoding.EncodeToString(key[:]),
Algorithm: "A256CTR",
Extractable: true,
KeyType: "oct",
KeyOps: []string{"encrypt", "decrypt"},
},
InitVector: base64.RawStdEncoding.EncodeToString(iv[:]),
Version: "v2",
decoded: &decodedKeys{key, iv},
}
}
func (ef *EncryptedFile) decodeKeys() error {
if ef.decoded != nil {
return nil
} else if len(ef.Key.Key) != keyBase64Length {
return InvalidKey
} else if len(ef.InitVector) != ivBase64Length {
return InvalidInitVector
}
ef.decoded = &decodedKeys{}
_, err := base64.RawURLEncoding.Decode(ef.decoded.key[:], []byte(ef.Key.Key))
if err != nil {
return InvalidKey
}
_, err = base64.RawStdEncoding.Decode(ef.decoded.iv[:], []byte(ef.InitVector))
if err != nil {
return InvalidInitVector
}
return nil
}
func (ef *EncryptedFile) Encrypt(plaintext []byte) []byte {
ef.decodeKeys()
ciphertext := xorA256CTR(plaintext, ef.decoded.key, ef.decoded.iv)
hash := sha256.Sum256(ciphertext)
ef.Hashes.SHA256 = base64.RawStdEncoding.EncodeToString(hash[:])
return ciphertext
}
func (ef *EncryptedFile) checkHash(ciphertext []byte) bool {
if len(ef.Hashes.SHA256) != hashBase64Length {
return false
}
var hash [hashLength]byte
_, err := base64.RawStdEncoding.Decode(hash[:], []byte(ef.Hashes.SHA256))
if err != nil {
return false
}
return hash == sha256.Sum256(ciphertext)
}
func (ef *EncryptedFile) Decrypt(ciphertext []byte) ([]byte, error) {
if ef.Version != "v2" {
return nil, UnsupportedVersion
} else if ef.Key.Algorithm != "A256CTR" {
return nil, UnsupportedAlgorithm
} else if !ef.checkHash(ciphertext) {
return nil, HashMismatch
} else if err := ef.decodeKeys(); err != nil {
return nil, err
} else {
return xorA256CTR(ciphertext, ef.decoded.key, ef.decoded.iv), nil
}
}
func xorA256CTR(source []byte, key [keyLength]byte, iv [ivLength]byte) []byte {
block, _ := aes.NewCipher(key[:])
result := make([]byte, len(source))
cipher.NewCTR(block, iv[:]).XORKeyStream(result, source)
return result
}
func genA256CTR() (key [keyLength]byte, iv [ivLength]byte) {
_, err := rand.Read(key[:])
if err != nil {
panic(err)
}
// For some reason we leave the 8 last bytes empty even though AES256-CTR has a 16-byte block size.
_, err = rand.Read(iv[:8])
if err != nil {
panic(err)
}
return
}