-
Notifications
You must be signed in to change notification settings - Fork 61
/
cipher.go
164 lines (142 loc) · 3.85 KB
/
cipher.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
package pkcs8
import (
"crypto/cipher"
"crypto/rand"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"github.com/emmansun/gmsm/padding"
)
func genRandom(len int) ([]byte, error) {
value := make([]byte, len)
_, err := rand.Read(value)
return value, err
}
type cipherWithBlock struct {
oid asn1.ObjectIdentifier
ivSize int
keySize int
newBlock func(key []byte) (cipher.Block, error)
}
func (c cipherWithBlock) KeySize() int {
return c.keySize
}
func (c cipherWithBlock) OID() asn1.ObjectIdentifier {
return c.oid
}
func (c cipherWithBlock) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) {
block, err := c.newBlock(key)
if err != nil {
return nil, nil, err
}
iv, err := genRandom(c.ivSize)
if err != nil {
return nil, nil, err
}
ciphertext, err := cbcEncrypt(block, key, iv, plaintext)
if err != nil {
return nil, nil, err
}
marshalledIV, err := asn1.Marshal(iv)
if err != nil {
return nil, nil, err
}
encryptionScheme := pkix.AlgorithmIdentifier{
Algorithm: c.oid,
Parameters: asn1.RawValue{FullBytes: marshalledIV},
}
return &encryptionScheme, ciphertext, nil
}
func (c cipherWithBlock) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) {
block, err := c.newBlock(key)
if err != nil {
return nil, err
}
var iv []byte
if _, err := asn1.Unmarshal(parameters.FullBytes, &iv); err != nil {
return nil, errors.New("pkcs8: invalid cipher parameters")
}
return cbcDecrypt(block, key, iv, encryptedKey)
}
func cbcEncrypt(block cipher.Block, key, iv, plaintext []byte) ([]byte, error) {
mode := cipher.NewCBCEncrypter(block, iv)
pkcs7 := padding.NewPKCS7Padding(uint(block.BlockSize()))
plainText := pkcs7.Pad(plaintext)
ciphertext := make([]byte, len(plainText))
mode.CryptBlocks(ciphertext, plainText)
return ciphertext, nil
}
func cbcDecrypt(block cipher.Block, key, iv, ciphertext []byte) ([]byte, error) {
mode := cipher.NewCBCDecrypter(block, iv)
pkcs7 := padding.NewPKCS7Padding(uint(block.BlockSize()))
plaintext := make([]byte, len(ciphertext))
mode.CryptBlocks(plaintext, ciphertext)
return pkcs7.Unpad(plaintext)
}
type cipherWithGCM struct {
oid asn1.ObjectIdentifier
nonceSize int
keySize int
newBlock func(key []byte) (cipher.Block, error)
}
// http://javadoc.iaik.tugraz.at/iaik_jce/current/index.html?iaik/security/cipher/GCMParameters.html
type gcmParameters struct {
Nonce []byte `asn1:"tag:4"`
ICVLen int
}
func (c cipherWithGCM) KeySize() int {
return c.keySize
}
func (c cipherWithGCM) OID() asn1.ObjectIdentifier {
return c.oid
}
func (c cipherWithGCM) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) {
block, err := c.newBlock(key)
if err != nil {
return nil, nil, err
}
nonce, err := genRandom(c.nonceSize)
if err != nil {
return nil, nil, err
}
aead, err := cipher.NewGCMWithNonceSize(block, c.nonceSize)
if err != nil {
return nil, nil, err
}
ciphertext := aead.Seal(nil, nonce, plaintext, nil)
paramSeq := gcmParameters{
Nonce: nonce,
ICVLen: aead.Overhead(),
}
paramBytes, err := asn1.Marshal(paramSeq)
if err != nil {
return nil, nil, err
}
encryptionAlgorithm := pkix.AlgorithmIdentifier{
Algorithm: c.oid,
Parameters: asn1.RawValue{
Tag: asn1.TagSequence,
Bytes: paramBytes,
},
}
return &encryptionAlgorithm, ciphertext, nil
}
func (c cipherWithGCM) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) {
block, err := c.newBlock(key)
if err != nil {
return nil, err
}
params := gcmParameters{}
_, err = asn1.Unmarshal(parameters.Bytes, ¶ms)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCMWithNonceSize(block, len(params.Nonce))
if err != nil {
return nil, err
}
if params.ICVLen != aead.Overhead() {
return nil, errors.New("pkcs8: invalid tag size")
}
return aead.Open(nil, params.Nonce, encryptedKey, nil)
}