forked from phayes/cryptoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
262 lines (249 loc) · 5.41 KB
/
lookup.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
package cryptoid
import (
"crypto"
"crypto/x509"
"errors"
)
// Errors
var UnableToFind = errors.New("cryptoid: Unable to find cryptosystem with that identifier")
func PublicKeyAlgorithmByOID(oid string) (PublicKeyAlgorithm, error) {
item := LookupByOID(oid)
algo, ok := item.(PublicKeyAlgorithm)
if !ok {
return PublicKeyAlgorithm{}, UnableToFind
}
return algo, nil
}
func HashAlgorithmByOID(oid string) (HashAlgorithm, error) {
item := LookupByOID(oid)
algo, ok := item.(HashAlgorithm)
if !ok {
return HashAlgorithm{}, UnableToFind
}
return algo, nil
}
func SignatureAlgorithmByOID(oid string) (SignatureAlgorithm, error) {
item := LookupByOID(oid)
algo, ok := item.(SignatureAlgorithm)
if !ok {
return SignatureAlgorithm{}, UnableToFind
}
return algo, nil
}
func LookupByOID(oid string) interface{} {
if len(oid) == 0 {
return nil
}
switch oid {
case "1.2.840.113549.1.1.1":
return RSA
case "1.2.840.10040.4.1":
return DSA
case "1.2.840.10045.2.1":
return ECDSA
case "1.2.840.113549.2.2":
return MD2
case "1.2.840.113549.2.4":
return MD4
case "1.2.840.113549.2.5":
return MD5
case "1.3.14.3.2.26":
return SHA1
case "2.16.840.1.101.3.4.2.4":
return SHA224
case "2.16.840.1.101.3.4.2.1":
return SHA256
case "2.16.840.1.101.3.4.2.2":
return SHA384
case "2.16.840.1.101.3.4.2.3":
return SHA512
case "2.16.840.1.101.3.4.2.7":
return SHA3_224
case "2.16.840.1.101.3.4.2.8":
return SHA3_256
case "2.16.840.1.101.3.4.2.9":
return SHA3_384
case "2.16.840.1.101.3.4.2.10":
return SHA3_512
case "2.16.840.1.101.3.4.2.11":
return SHAKE128
case "2.16.840.1.101.3.4.2.12":
return SHAKE256
case "1.2.840.113549.1.1.2":
return MD2WithRSA
case "1.2.840.113549.1.1.3":
return MD4WithRSA
case "1.2.840.113549.1.1.4":
return MD5WithRSA
case "1.2.840.113549.1.1.5":
return SHA1WithRSA
case "1.2.840.113549.1.1.11":
return SHA256WithRSA
case "1.2.840.113549.1.1.12":
return SHA384WithRSA
case "1.2.840.113549.1.1.13":
return SHA512WithRSA
case "1.2.840.10040.4.3":
return DSAWithSHA1
case "2.16.840.1.101.3.4.3.2":
return DSAWithSHA256
case "1.2.840.10045.4.1":
return ECDSAWithSHA1
case "2.16.840.1.101.4.3.2":
return ECDSAWithSHA256
case "2.16.840.1.101.4.3.3":
return ECDSAWithSHA384
case "2.16.840.1.101.4.3.4":
return ECDSAWithSHA512
default:
return nil
}
}
func PublicKeyAlgorithmByName(name string) (PublicKeyAlgorithm, error) {
item := LookupByName(name)
algo, ok := item.(PublicKeyAlgorithm)
if !ok {
return PublicKeyAlgorithm{}, UnableToFind
}
return algo, nil
}
func HashAlgorithmByName(name string) (HashAlgorithm, error) {
item := LookupByName(name)
algo, ok := item.(HashAlgorithm)
if !ok {
return HashAlgorithm{}, UnableToFind
}
return algo, nil
}
func SignatureAlgorithmByName(name string) (SignatureAlgorithm, error) {
item := LookupByName(name)
algo, ok := item.(SignatureAlgorithm)
if !ok {
return SignatureAlgorithm{}, UnableToFind
}
return algo, nil
}
func LookupByName(name string) interface{} {
switch name {
case "RSA":
return RSA
case "DSA":
return DSA
case "ECDSA":
return ECDSA
case "MD2":
return MD2
case "MD4":
return MD4
case "MD5":
return MD5
case "SHA1":
return SHA1
case "SHA224":
return SHA224
case "SHA256":
return SHA256
case "SHA384":
return SHA384
case "SHA512":
return SHA512
case "SHA3-224":
return SHA3_224
case "SHA3-256":
return SHA3_256
case "SHA3-384":
return SHA3_384
case "SHA3-512":
return SHA3_512
case "SHAKE128":
return SHAKE128
case "SHAKE256":
return SHAKE256
case "MD2WithRSA":
return MD2WithRSA
case "MD4WithRSA":
return MD4WithRSA
case "MD5WithRSA":
return MD5WithRSA
case "SHA1WithRSA":
return SHA1WithRSA
case "SHA256WithRSA":
return SHA256WithRSA
case "SHA384WithRSA":
return SHA384WithRSA
case "SHA512WithRSA":
return SHA512WithRSA
case "DSAWithSHA1":
return DSAWithSHA1
case "DSAWithSHA256":
return DSAWithSHA256
case "ECDSAWithSHA1":
return ECDSAWithSHA1
case "ECDSAWithSHA256":
return ECDSAWithSHA256
case "ECDSAWithSHA384":
return ECDSAWithSHA384
case "ECDSAWithSHA512":
return ECDSAWithSHA512
default:
return nil
}
}
func HashAlgorithmByCrypto(hash crypto.Hash) HashAlgorithm {
switch hash {
case crypto.MD4:
return MD4
case crypto.MD5:
return MD5
case crypto.SHA1:
return SHA1
case crypto.SHA224:
return SHA224
case crypto.SHA256:
return SHA256
case crypto.SHA384:
return SHA384
case crypto.SHA512:
return SHA512
case crypto.SHA3_224:
return SHA3_224
case crypto.SHA3_256:
return SHA3_256
case crypto.SHA3_384:
return SHA3_384
case crypto.SHA3_512:
return SHA3_512
default:
panic("Invalid crypto.Hash") // This shouldn't be possible
}
}
func SignatureAlgorithmByX509(sig x509.SignatureAlgorithm) SignatureAlgorithm {
switch sig {
case x509.MD2WithRSA:
return MD2WithRSA
case x509.MD5WithRSA:
return MD5WithRSA
case x509.SHA1WithRSA:
return SHA1WithRSA
case x509.SHA256WithRSA:
return SHA256WithRSA
case x509.SHA384WithRSA:
return SHA384WithRSA
case x509.SHA512WithRSA:
return SHA512WithRSA
case x509.DSAWithSHA1:
return DSAWithSHA1
case x509.DSAWithSHA256:
return DSAWithSHA256
case x509.ECDSAWithSHA1:
return ECDSAWithSHA1
case x509.ECDSAWithSHA256:
return ECDSAWithSHA256
case x509.ECDSAWithSHA384:
return ECDSAWithSHA384
case x509.ECDSAWithSHA512:
return ECDSAWithSHA512
default:
panic("Invalid x509.SignatureAlgorithm") // This shouldn't be possible
}
}