-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc_stream.go
More file actions
105 lines (83 loc) · 2.35 KB
/
Copy pathenc_stream.go
File metadata and controls
105 lines (83 loc) · 2.35 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
package crypka
import "io"
// Note: this type might change in future, when we run out of values on uint8
type cpkControlValue uint8
func (v *cpkControlValue) decode(encoded uint64) (ok bool) {
if encoded != uint64(streamEndCpkControlByte) {
ok = false
} else {
*v = cpkControlValue(encoded)
ok = true
}
return
}
func (v cpkControlValue) toEncodable() uint64 {
return uint64(v)
}
const (
streamEndCpkControlByte cpkControlValue = 0
)
// Implements algorithm, which handles streamming encryption in crypka's format.
type CPKStreamSymmEncAlgo struct {
EncSymmAlgo
}
func (algo *CPKStreamSymmEncAlgo) GetInfo() EncAlgoInfo {
info := algo.EncSymmAlgo.GetInfo()
info.EncType = EncTypeStream
// Note: basic authentication is sufficient to prevent truncation, since
// the only thing we require to be trunc-authenticated is guarantee of valid finalization chunk
// which is provided either during finalization or eagerly during decryption
// in both cases, we are trunc authenticated.
if info.AuthMode.IsFinalizeAuthetnicated() || info.AuthMode.IsEagerAuthenticated() {
info.AuthMode.SetTruncAuthenticated(true)
}
info.EncInfo.RequiresFinalization = true
return info
}
func (algo *CPKStreamSymmEncAlgo) GenerateKey(ctx KeyGenerationContext, rng RNG) (key EncSymmKey, err error) {
inner, err := algo.EncSymmAlgo.GenerateKey(ctx, rng)
if err != nil {
return
}
key = &cpkStreamEncSymmKey{
wrapped: inner,
}
return
}
func (algo *CPKStreamSymmEncAlgo) ParseSymmEncKey(ctx KeyParseContext, data []byte) (key EncSymmKey, err error) {
inner, err := algo.EncSymmAlgo.ParseSymmEncKey(ctx, data)
if err != nil {
return
}
key = &cpkStreamEncSymmKey{
wrapped: inner,
}
return
}
type cpkStreamEncSymmKey struct {
wrapped EncSymmKey
}
func (ek *cpkStreamEncSymmKey) MakeEncryptor(ctx KeyContext) (enc Encryptor, err error) {
inner, err := ek.wrapped.MakeEncryptor(ctx)
if err != nil {
return
}
enc = newCPKStreamEncryptor(inner, 256)
return
}
func (ek *cpkStreamEncSymmKey) MakeDecryptor(ctx KeyContext) (dec Decryptor, err error) {
inner, err := ek.wrapped.MakeDecryptor(ctx)
if err != nil {
return
}
dec = newCPKStreamDecryptor(inner, 1024)
return
}
func (ek *cpkStreamEncSymmKey) MarshalToWriter(w io.Writer) (err error) {
mk, ok := ek.wrapped.(MarshalableKey)
if !ok {
err = ErrKeyNotMarshalable
return
}
return mk.MarshalToWriter(w)
}