This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
/
utils.go
127 lines (97 loc) · 2.55 KB
/
utils.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
package soft
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/hmac"
"errors"
"github.com/pagarme/cryptokit"
)
func processAead(mech cryptokit.Gcm, key cryptokit.Key, in []byte, encrypt bool) ([]byte, error) {
impl, err := getImplementation(mech.Underlying, key)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(impl)
if err != nil {
return nil, err
}
if encrypt {
return aead.Seal(nil, mech.Nonce, in, mech.AdditionalData), nil
} else {
return aead.Open(nil, mech.Nonce, in, mech.AdditionalData)
}
}
func processBlockCipher(mech cryptokit.BlockCipher, key cryptokit.Key, in []byte, encrypt bool) ([]byte, error) {
impl, err := getImplementation(mech.BlockCipherUnderlying(), key)
if err != nil {
return nil, err
}
c, err := getBlockImplementation(mech, impl, encrypt)
if err != nil {
return nil, err
}
if len(in)%c.BlockSize() != 0 {
return nil, errors.New("Input must be a multiple of block size")
}
out := make([]byte, len(in))
c.CryptBlocks(out, in)
return out, nil
}
func processHmac(mech cryptokit.Hmac, key cryptokit.Key, in []byte, encrypt bool) ([]byte, error) {
skey := key.(*Key)
if !encrypt {
return nil, errors.New("This mechanism is encrypt only")
}
impl, err := getHashImplementation(mech.Underlying)
if err != nil {
return nil, err
}
h := hmac.New(impl.New, skey.data)
h.Write(in)
return h.Sum(nil), nil
}
func getHashImplementation(mech cryptokit.Mechanism) (crypto.Hash, error) {
switch mech.(type) {
case cryptokit.Sha1:
return crypto.SHA1, nil
case cryptokit.Sha256:
return crypto.SHA256, nil
case cryptokit.Sha512:
return crypto.SHA512, nil
}
return 0, errors.New("Unknown mechanism")
}
func getImplementation(mech cryptokit.Mechanism, key cryptokit.Key) (cipher.Block, error) {
skey := key.(*Key)
switch mech.(type) {
case cryptokit.Aes:
return aes.NewCipher(skey.data)
case cryptokit.Des:
return des.NewCipher(skey.data)
case cryptokit.Tdes:
return des.NewTripleDESCipher(skey.data)
}
return nil, errors.New("Unknown mechanism")
}
func getBlockImplementation(mech cryptokit.BlockCipher, impl cipher.Block, encrypt bool) (cipher.BlockMode, error) {
var c cipher.BlockMode
iv := mech.BlockCipherIV()
if iv == nil {
iv = make([]byte, impl.BlockSize())
}
switch mech.(type) {
case cryptokit.Cbc:
if encrypt {
c = cipher.NewCBCEncrypter(impl, iv)
} else {
c = cipher.NewCBCDecrypter(impl, iv)
}
case cryptokit.Ecb:
c = &ecbBlockMode{impl, encrypt}
default:
return nil, errors.New("Unknown block cipher mode")
}
return c, nil
}