/
kdf_mac.go
266 lines (229 loc) · 6.74 KB
/
kdf_mac.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package pkcs12
import (
"hash"
"errors"
"crypto/rand"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/x509/pkix"
"encoding/asn1"
"golang.org/x/crypto/md4"
"github.com/deatil/go-cryptobin/kdf/pbkdf"
"github.com/deatil/go-cryptobin/kdf/gost_pbkdf2"
"github.com/deatil/go-cryptobin/hash/md2"
"github.com/deatil/go-cryptobin/hash/sm3"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012256"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012512"
)
// 可使用的 hash 方式
type Hash uint
const (
MD2 Hash = 1 + iota
MD4
MD5
SHA1
SHA224
SHA256
SHA384
SHA512
SHA512_224
SHA512_256
SM3
GOST34112012256
GOST34112012512
)
var (
// 默认 hash
DefaultHash = SHA1
)
var (
oidMD2 = asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 2}
oidMD4 = asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 4}
oidMD5 = asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 5}
oidSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
oidSHA224 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 4}
oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
oidSHA512_224 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 5}
oidSHA512_256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 6}
oidSM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 401}
oidGOST34112012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 2}
oidGOST34112012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 3}
)
// 返回使用的 Hash 方式
func hashByOID(oid asn1.ObjectIdentifier) (func() hash.Hash, error) {
switch {
case oid.Equal(oidMD2):
return md2.New, nil
case oid.Equal(oidMD4):
return md4.New, nil
case oid.Equal(oidMD5):
return md5.New, nil
case oid.Equal(oidSHA1):
return sha1.New, nil
case oid.Equal(oidSHA224):
return sha256.New224, nil
case oid.Equal(oidSHA256):
return sha256.New, nil
case oid.Equal(oidSHA384):
return sha512.New384, nil
case oid.Equal(oidSHA512):
return sha512.New, nil
case oid.Equal(oidSHA512_224):
return sha512.New512_224, nil
case oid.Equal(oidSHA512_256):
return sha512.New512_256, nil
case oid.Equal(oidSM3):
return sm3.New, nil
case oid.Equal(oidGOST34112012256):
return gost34112012256.New, nil
case oid.Equal(oidGOST34112012512):
return gost34112012512.New, nil
}
return nil, errors.New("pkcs12: unsupported hash function")
}
// 返回使用的 Hash 对应的 asn1
func oidByHash(h Hash) (asn1.ObjectIdentifier, error) {
switch h {
case MD2:
return oidMD2, nil
case MD4:
return oidMD4, nil
case MD5:
return oidMD5, nil
case SHA1:
return oidSHA1, nil
case SHA224:
return oidSHA224, nil
case SHA256:
return oidSHA256, nil
case SHA384:
return oidSHA384, nil
case SHA512:
return oidSHA512, nil
case SHA512_224:
return oidSHA512_224, nil
case SHA512_256:
return oidSHA512_256, nil
case SM3:
return oidSM3, nil
case GOST34112012256:
return oidGOST34112012256, nil
case GOST34112012512:
return oidGOST34112012512, nil
}
return nil, errors.New("pkcs12: unsupported hash function")
}
// from PKCS#7:
type DigestInfo struct {
Algorithm pkix.AlgorithmIdentifier
Digest []byte
}
type MacData struct {
Mac DigestInfo
MacSalt []byte
Iterations int `asn1:"optional,default:1"`
}
func (this MacData) Verify(message []byte, password []byte) (err error) {
var alg asn1.ObjectIdentifier
var h func() hash.Hash
if this.Mac.Algorithm.Algorithm.String() != "" {
h, err = hashByOID(this.Mac.Algorithm.Algorithm)
if err != nil {
return err
}
} else {
alg, err = oidByHash(DefaultHash)
if err != nil {
return err
}
h, err = hashByOID(alg)
if err != nil {
return err
}
}
oid := this.Mac.Algorithm.Algorithm
var key []byte
switch {
case oid.Equal(oidGOST34112012256),
oid.Equal(oidGOST34112012512):
pass, err := decodeBMPString(password)
if err != nil {
return err
}
key = gost_pbkdf2.Key(h, []byte(pass), this.MacSalt, this.Iterations, 96)
key = key[len(key)-32:]
default:
hashSize := h().Size()
key = pbkdf.Key(h, hashSize, 64, this.MacSalt, password, this.Iterations, 3, hashSize)
}
mac := hmac.New(h, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
if !hmac.Equal(this.Mac.Digest, expectedMAC) {
return ErrIncorrectPassword
}
return
}
// mac 配置
type MacOpts struct {
SaltSize int // 8
IterationCount int // 1
HMACHash Hash
}
func (this MacOpts) Compute(message []byte, password []byte) (data MacKDFParameters, err error) {
var alg asn1.ObjectIdentifier
var prfParam pkix.AlgorithmIdentifier
if this.HMACHash != 0 {
alg, err = oidByHash(this.HMACHash)
} else {
alg, err = oidByHash(DefaultHash)
}
if err != nil {
return nil, err
}
prfParam = pkix.AlgorithmIdentifier{
Algorithm: alg,
Parameters: asn1.RawValue{
Tag: asn1.TagNull,
},
}
h, err := hashByOID(alg)
if err != nil {
return nil, err
}
macSalt := make([]byte, this.SaltSize)
if _, err = rand.Read(macSalt); err != nil {
return nil, err
}
var key []byte
switch {
case alg.Equal(oidGOST34112012256),
alg.Equal(oidGOST34112012512):
pass, err := decodeBMPString(password)
if err != nil {
return nil, err
}
key = gost_pbkdf2.Key(h, []byte(pass), macSalt, this.IterationCount, 96)
key = key[len(key)-32:]
default:
hashSize := h().Size()
key = pbkdf.Key(h, hashSize, 64, macSalt, password, this.IterationCount, 3, hashSize)
}
mac := hmac.New(h, key)
mac.Write(message)
digest := mac.Sum(nil)
data = MacData{
DigestInfo{
prfParam,
digest,
},
macSalt,
this.IterationCount,
}
return
}