This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
/
session.go
182 lines (137 loc) · 4.09 KB
/
session.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
173
174
175
176
177
178
179
180
181
182
package soft
import (
"crypto/rand"
"errors"
"github.com/pagarme/cryptokit"
"github.com/pagarme/cryptokit/soft/dukpt"
)
type Session struct {
db Database
}
func (s *Session) ListKeys() ([]string, error) {
return s.db.ListKeys()
}
func (s *Session) FindKey(id string) (cryptokit.Key, bool, error) {
attribs, found, err := s.db.FindKey(id)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
return loadKey(s, attribs), true, nil
}
func (s *Session) Encrypt(mech cryptokit.Mechanism, key cryptokit.Key, in []byte) ([]byte, error) {
if key.Attributes().Capabilities&cryptokit.Encrypt == 0 {
return nil, errors.New("Key can't be used for encryption")
}
return s.encryptionCore(mech, key, in, true)
}
func (s *Session) Decrypt(mech cryptokit.Mechanism, key cryptokit.Key, in []byte) ([]byte, error) {
if key.Attributes().Capabilities&cryptokit.Decrypt == 0 {
return nil, errors.New("Key can't be used for decryption")
}
return s.encryptionCore(mech, key, in, false)
}
func (s *Session) Translate(mech cryptokit.Mechanism, inKey cryptokit.Key, in []byte, outKey cryptokit.Key) ([]byte, error) {
data, err := s.Decrypt(mech, inKey, in)
if err != nil {
return nil, err
}
return s.Encrypt(mech, outKey, data)
}
func (s *Session) Wrap(mech cryptokit.Mechanism, kek, key cryptokit.Key) ([]byte, error) {
if kek.Attributes().Capabilities&cryptokit.Wrap == 0 {
return nil, errors.New("Key can't be used for wrapping")
}
return s.encryptionCore(mech, kek, key.(*Key).data, true)
}
func (s *Session) Unwrap(mech cryptokit.Mechanism, kek cryptokit.Key, key []byte, attributes cryptokit.KeyAttributes) (cryptokit.Key, error) {
if kek.Attributes().Capabilities&cryptokit.Unwrap == 0 {
return nil, errors.New("Key can't be used for unwrapping")
}
if err := s.checkConsistency(attributes); err != nil {
return nil, err
}
data, err := s.encryptionCore(mech, kek, key, false)
if err != nil {
return nil, err
}
return s.createKey(attributes, data)
}
func (s *Session) Generate(mech cryptokit.Mechanism, attributes cryptokit.KeyAttributes) (cryptokit.Key, error) {
if err := s.checkConsistency(attributes); err != nil {
return nil, err
}
data := make([]byte, attributes.Length)
switch v := mech.(type) {
case cryptokit.FixedKey:
copy(data, v.Key)
case cryptokit.Random:
if _, err := rand.Read(data); err != nil {
return nil, err
}
default:
return nil, errors.New("Unsupported mechanism")
}
return s.createKey(attributes, data)
}
func (s *Session) Derive(mech cryptokit.Mechanism, key cryptokit.Key, attributes cryptokit.KeyAttributes) (cryptokit.Key, error) {
var data []byte
if key.Attributes().Capabilities&cryptokit.Derive == 0 {
return nil, errors.New("Key can't be used for derivation")
}
if err := s.checkConsistency(attributes); err != nil {
return nil, err
}
skey := key.(*Key)
switch v := mech.(type) {
case cryptokit.Dukpt:
d, err := dukpt.DerivePekFromBdk(skey.data, v.Ksn)
if err != nil {
return nil, err
}
data = d
default:
return nil, errors.New("Unsupported mechanism")
}
return s.createKey(attributes, data)
}
func (s *Session) Hash(mech cryptokit.Mechanism, in []byte) ([]byte, error) {
hash, err := getHashImplementation(mech)
if err != nil {
return nil, err
}
h := hash.New()
h.Write(in)
return h.Sum(nil), nil
}
func (s *Session) Close() error {
return nil
}
func (s *Session) checkConsistency(a cryptokit.KeyAttributes) error {
if a.Length <= 0 {
return errors.New("Invalid key size")
}
return nil
}
func (s *Session) createKey(a cryptokit.KeyAttributes, data []byte) (*Key, error) {
k := newKey(s, a, data)
if k.permanent {
if err := k.save(); err != nil {
return nil, err
}
}
return k, nil
}
func (s *Session) encryptionCore(mech cryptokit.Mechanism, key cryptokit.Key, in []byte, encrypt bool) ([]byte, error) {
switch v := mech.(type) {
case cryptokit.BlockCipher:
return processBlockCipher(v, key, in, encrypt)
case cryptokit.Gcm:
return processAead(v, key, in, encrypt)
case cryptokit.Hmac:
return processHmac(v, key, in, encrypt)
}
return nil, errors.New("Unknown mechanism")
}