-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryption.go
More file actions
215 lines (192 loc) · 5.99 KB
/
encryption.go
File metadata and controls
215 lines (192 loc) · 5.99 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// encryption.go
package sofia
import (
"crypto/cipher"
"errors"
)
// --- PSSH ---
type PsshBox struct {
Header *BoxHeader
Version byte
Flags [3]byte
SystemID [16]byte
KIDs [][16]byte
Data []byte
}
func DecodePsshBox(data []byte) (*PsshBox, error) {
b := &PsshBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
if len(data) < 28 { // 8 byte header + 4 byte version/flags + 16 byte systemID
return nil, errors.New("pssh too short")
}
p := parser{data: data, offset: 8}
versionAndFlags := p.Bytes(4)
b.Version = versionAndFlags[0]
copy(b.Flags[:], versionAndFlags[1:])
copy(b.SystemID[:], p.Bytes(16))
if b.Version > 0 {
if len(data) < p.offset+4 {
return nil, errors.New("pssh too short for KID count")
}
kidCount := p.Uint32()
if len(data) < p.offset+int(kidCount*16) {
return nil, errors.New("pssh too short for KIDs")
}
b.KIDs = make([][16]byte, kidCount)
for i := 0; i < int(kidCount); i++ {
copy(b.KIDs[i][:], p.Bytes(16))
}
}
if len(data) < p.offset+4 {
return nil, errors.New("pssh too short for data size")
}
dataSize := p.Uint32()
if len(data) < p.offset+int(dataSize) {
return nil, errors.New("pssh size mismatch")
}
b.Data = p.Bytes(int(dataSize))
return b, nil
}
// --- TENC ---
// TencBox defines the Track Encryption Box ('tenc'), which contains
// default encryption parameters for a track.
// Specification: ISO/IEC 23001-7
type TencBox struct {
Header *BoxHeader
Version byte
Flags uint32
DefaultIsProtected byte
DefaultPerSampleIVSize byte
DefaultKID [16]byte
DefaultConstantIVSize byte // Present if DefaultIsProtected=1 and DefaultPerSampleIVSize=0
DefaultConstantIV []byte // Present if DefaultIsProtected=1 and DefaultPerSampleIVSize=0
}
func DecodeTencBox(data []byte) (*TencBox, error) {
b := &TencBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
p := parser{data: data, offset: 8}
if len(data) < p.offset+4 {
return nil, errors.New("tenc box too short for version/flags")
}
versionAndFlags := p.Uint32()
b.Version = byte(versionAndFlags >> 24)
b.Flags = versionAndFlags & 0x00FFFFFF
if b.Version == 0 {
// Based on the error, the reserved field for this file is 2 bytes.
// Payload: reserved(2) + isProtected(1) + perSampleIVSize(1) + KID(16) = 20 bytes.
const requiredV0PayloadSize = 20
if len(data) < p.offset+requiredV0PayloadSize {
return nil, errors.New("tenc v0 box too short for required fields")
}
// Correctly skip the 2 reserved bytes.
_ = p.Bytes(2)
b.DefaultIsProtected = p.Byte()
b.DefaultPerSampleIVSize = p.Byte()
copy(b.DefaultKID[:], p.Bytes(16))
if b.DefaultIsProtected == 1 && b.DefaultPerSampleIVSize == 0 {
if p.offset < int(b.Header.Size) {
if len(data) < p.offset+1 {
return nil, errors.New("tenc box truncated before constant IV size")
}
b.DefaultConstantIVSize = p.Byte()
if len(data) < p.offset+int(b.DefaultConstantIVSize) {
return nil, errors.New("tenc box truncated, not enough data for constant IV")
}
b.DefaultConstantIV = p.Bytes(int(b.DefaultConstantIVSize))
}
}
}
// For other versions, we do nothing and leave the fields as their zero-value.
return b, nil
}
// --- SENC ---
type Subsample struct {
BytesOfClearData uint16
BytesOfProtectedData uint32
}
type SencSample struct {
IV []byte
Subsamples []Subsample
}
type SencBox struct {
Header *BoxHeader
Flags uint32
Samples []SencSample
}
func DecodeSencBox(data []byte) (*SencBox, error) {
b := &SencBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
if len(data) < 16 { // 8 byte header, 4 byte flags, 4 byte sample count
return nil, errors.New("senc too short")
}
p := parser{data: data, offset: 8}
b.Flags = p.Uint32() & 0x00FFFFFF
sampleCount := p.Uint32()
b.Samples = make([]SencSample, sampleCount)
const ivSize = 8
subsamplesPresent := b.Flags&0x000002 != 0
for i := uint32(0); i < sampleCount; i++ {
if len(data) < p.offset+ivSize {
return nil, errors.New("senc truncated while reading IV")
}
b.Samples[i].IV = p.Bytes(ivSize)
if subsamplesPresent {
if len(data) < p.offset+2 {
return nil, errors.New("senc truncated while reading subsample count")
}
subsampleCount := p.Uint16()
b.Samples[i].Subsamples = make([]Subsample, subsampleCount)
for j := uint16(0); j < subsampleCount; j++ {
if len(data) < p.offset+6 {
return nil, errors.New("senc truncated while reading subsample")
}
clear := p.Uint16()
prot := p.Uint32()
b.Samples[i].Subsamples[j] = Subsample{clear, prot}
}
}
}
return b, nil
}
// --- Logic ---
func Decrypt(data []byte, sample *SencSample, block cipher.Block) {
if sample == nil || len(sample.IV) == 0 {
return
}
iv := sample.IV
if len(iv) == 8 {
paddedIV := make([]byte, 16)
copy(paddedIV, iv)
iv = paddedIV
}
stream := cipher.NewCTR(block, iv)
if len(sample.Subsamples) == 0 {
stream.XORKeyStream(data, data)
} else {
offset := 0
for _, subsample := range sample.Subsamples {
offset += int(subsample.BytesOfClearData)
if subsample.BytesOfProtectedData > 0 {
end := offset + int(subsample.BytesOfProtectedData)
if end > len(data) {
end = len(data)
}
chunk := data[offset:end]
stream.XORKeyStream(chunk, chunk)
offset = end
}
}
}
}