This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
crypto.go
585 lines (485 loc) · 11.8 KB
/
crypto.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package kerb
import (
"bytes"
"code.google.com/p/go.crypto/md4"
"crypto/cipher"
"crypto/des"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rc4"
"crypto/subtle"
"encoding/binary"
"hash"
"io"
"unicode/utf16"
)
type key interface {
// If algo is -1 then use the default
Sign(algo, usage int, data ...[]byte) ([]byte, error)
SignAlgo(usage int) int
Encrypt(salt []byte, usage int, data ...[]byte) []byte
Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error)
EncryptAlgo(usage int) int
Key() []byte
}
func mustSign(key key, algo, usage int, data ...[]byte) []byte {
sign, err := key.Sign(algo, usage, data...)
if err != nil {
panic(err)
}
return sign
}
func mustDecrypt(key key, salt []byte, algo, usage int, data []byte) []byte {
dec, err := key.Decrypt(salt, algo, usage, data)
if err != nil {
panic(err)
}
return dec
}
type rc4hmac struct {
key []byte
}
// rc4HmacKey converts a UTF8 password into a key suitable for use with the
// rc4hmac.
func rc4HmacKey(password string) []byte {
// Convert password from UTF8 to UTF16-LE
s := make([]byte, 0)
for _, r := range password {
if r > 0x10000 {
a, b := utf16.EncodeRune(r)
s = append(s, byte(a), byte(a>>8), byte(b), byte(b>>8))
} else {
s = append(s, byte(r), byte(r>>8))
}
}
h := md4.New()
h.Write(s)
return h.Sum(nil)
}
// RC4-HMAC has a few slight differences in the key usage values
func rc4HmacUsage(usage int) uint32 {
switch usage {
case asReplyClientKey:
return 8
case gssWrapSign:
return 13
}
return uint32(usage)
}
func (c *rc4hmac) EncryptAlgo(usage int) int {
switch usage {
case gssWrapSeal, gssSequenceNumber:
return cryptGssRc4Hmac
}
return cryptRc4Hmac
}
func (c *rc4hmac) Key() []byte {
return c.key
}
func (c *rc4hmac) SignAlgo(usage int) int {
switch usage {
case gssWrapSign:
return signGssRc4Hmac
}
// TODO: replace with RC4-HMAC checksum algorithm. For now we are
// using the unkeyed RSA-MD5 checksum algorithm
return signMd5
}
func unkeyedSign(algo, usage int, data ...[]byte) ([]byte, error) {
var h hash.Hash
switch algo {
case signMd5:
h = md5.New()
case signMd4:
h = md4.New()
default:
return nil, ErrProtocol
}
for _, d := range data {
h.Write(d)
}
return h.Sum(nil), nil
}
var signaturekey = []byte("signaturekey\x00")
func (c *rc4hmac) Sign(algo, usage int, data ...[]byte) ([]byte, error) {
if algo != signGssRc4Hmac && algo != signRc4Hmac {
return unkeyedSign(algo, usage, data...)
}
h := hmac.New(md5.New, c.key)
h.Write(signaturekey)
ksign := h.Sum(nil)
chk := md5.New()
binary.Write(chk, binary.LittleEndian, rc4HmacUsage(usage))
for _, d := range data {
chk.Write(d)
}
h = hmac.New(md5.New, ksign)
h.Write(chk.Sum(nil))
return h.Sum(nil), nil
}
func (c *rc4hmac) Encrypt(salt []byte, usage int, data ...[]byte) []byte {
switch usage {
case gssSequenceNumber:
// salt is the checksum
h := hmac.New(md5.New, c.key)
binary.Write(h, binary.LittleEndian, uint32(0))
h = hmac.New(md5.New, h.Sum(nil))
h.Write(salt)
r, _ := rc4.NewCipher(h.Sum(nil))
for _, d := range data {
r.XORKeyStream(d, d)
}
return bytes.Join(data, nil)
case gssWrapSeal:
// salt is the sequence number in big endian
seqnum := binary.BigEndian.Uint32(salt)
kcrypt := make([]byte, len(c.key))
for i, b := range c.key {
kcrypt[i] = b ^ 0xF0
}
h := hmac.New(md5.New, kcrypt)
binary.Write(h, binary.LittleEndian, seqnum)
r, _ := rc4.NewCipher(h.Sum(nil))
for _, d := range data {
r.XORKeyStream(d, d)
}
return bytes.Join(data, nil)
}
// Create the output vector, layout is 0-15 checksum, 16-23 random data, 24- actual data
outsz := 24
for _, d := range data {
outsz += len(d)
}
out := make([]byte, outsz)
io.ReadFull(rand.Reader, out[16:24])
// Hash the key and usage together to get the HMAC-MD5 key
h1 := hmac.New(md5.New, c.key)
binary.Write(h1, binary.LittleEndian, rc4HmacUsage(usage))
K1 := h1.Sum(nil)
// Fill in out[:16] with the checksum
ch := hmac.New(md5.New, K1)
ch.Write(out[16:24])
for _, d := range data {
ch.Write(d)
}
ch.Sum(out[:0])
// Calculate the RC4 key using the checksum
h3 := hmac.New(md5.New, K1)
h3.Write(out[:16])
K3 := h3.Sum(nil)
// Encrypt out[16:] with 16:24 being random data and 24: being the
// encrypted data
r, _ := rc4.NewCipher(K3)
r.XORKeyStream(out[16:24], out[16:24])
dst := out[24:]
for _, d := range data {
r.XORKeyStream(dst[:len(d)], d)
dst = dst[len(d):]
}
return out
}
func (c *rc4hmac) Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error) {
switch usage {
case gssSequenceNumber:
if algo != cryptGssRc4Hmac && algo != cryptGssNone {
return nil, ErrProtocol
}
return c.Encrypt(salt, usage, data), nil
case gssWrapSeal:
// GSS sealing uses an external checksum for integrity and
// since RC4 is symettric we can just reencrypt the data
if algo != cryptGssRc4Hmac {
return nil, ErrProtocol
}
return c.Encrypt(salt, usage, data), nil
}
if algo != cryptRc4Hmac || len(data) < 24 {
return nil, ErrProtocol
}
// Hash the key and usage together to get the HMAC-MD5 key
h1 := hmac.New(md5.New, c.key)
binary.Write(h1, binary.LittleEndian, rc4HmacUsage(usage))
K1 := h1.Sum(nil)
// Calculate the RC4 key using the checksum
h3 := hmac.New(md5.New, K1)
h3.Write(data[:16])
K3 := h3.Sum(nil)
// Decrypt d.Data[16:] in place with 16:24 being random data and 24:
// being the encrypted data
r, _ := rc4.NewCipher(K3)
r.XORKeyStream(data[16:], data[16:])
// Recalculate the checksum using the decrypted data
ch := hmac.New(md5.New, K1)
ch.Write(data[16:])
chk := ch.Sum(nil)
// Check the input checksum
if subtle.ConstantTimeCompare(chk, data[:16]) != 1 {
return nil, ErrProtocol
}
return data[24:], nil
}
func fixparity(u uint64, expand bool) uint64 {
for i := 7; i >= 0; i-- {
// pull out this byte
var b uint64
if expand {
b = (u >> uint(i*7)) & 0x7F
} else {
b = (u >> (uint(i*8) + 1)) & 0x7F
}
// compute parity
p := b ^ (b >> 4)
p &= 0x0F
p = 0x9669 >> p
// add in parity as lsb
b = (b << 1) | (p & 1)
// set that byte in output
u &^= 0xFF << uint(i*8)
u |= b << uint(i*8)
}
return u
}
func fixweak(u uint64) uint64 {
switch u {
case 0x0101010101010101, 0xFEFEFEFEFEFEFEFE,
0xE0E0E0E0F1F1F1F1, 0x1F1F1F1F0E0E0E0E,
0x011F011F010E010E, 0x1F011F010E010E01,
0x01E001E001F101F1, 0xE001E001F101F101,
0x01FE01FE01FE01FE, 0xFE01FE01FE01FE01,
0x1FE01FE00EF10EF1, 0xE01FE01FF10EF10E,
0x1FFE1FFE0EFE0EFE, 0xFE1FFE1FFE0EFE0E,
0xE0FEE0FEF1FEF1FE, 0xFEE0FEE0FEF1FEF1:
u ^= 0xF0
}
return u
}
func desStringKey(password, salt string) []byte {
blk := make([]byte, (len(password)+len(salt)+7)&^7)
copy(blk, password)
copy(blk[len(password):], salt)
var u uint64
for i := 0; i < len(blk); i += 8 {
a := binary.BigEndian.Uint64(blk[i:])
a = (a & 0x7F) |
((a & 0x7F00) >> 1) |
((a & 0x7F0000) >> 2) |
((a & 0x7F000000) >> 3) |
((a & 0x7F00000000) >> 4) |
((a & 0x7F0000000000) >> 5) |
((a & 0x7F000000000000) >> 6) |
((a & 0x7F00000000000000) >> 7)
if (i & 8) != 0 {
a = ((a >> 1) & 0x5555555555555555) | ((a & 0x5555555555555555) << 1)
a = ((a >> 2) & 0x3333333333333333) | ((a & 0x3333333333333333) << 2)
a = ((a >> 4) & 0x0F0F0F0F0F0F0F0F) | ((a & 0x0F0F0F0F0F0F0F0F) << 4)
a = ((a >> 8) & 0x00FF00FF00FF00FF) | ((a & 0x00FF00FF00FF00FF) << 8)
a = ((a >> 16) & 0x0000FFFF0000FFFF) | ((a & 0x0000FFFF0000FFFF) << 16)
a = (a >> 32) | (a << 32)
a >>= 8
}
u ^= a
}
u = fixweak(fixparity(u, true))
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, u)
b, _ := des.NewCipher(k)
c := cipher.NewCBCEncrypter(b, k)
c.CryptBlocks(blk, blk)
u = binary.BigEndian.Uint64(blk[len(blk)-8:])
u = fixweak(fixparity(u, false))
binary.BigEndian.PutUint64(k, u)
return k
}
type descbc struct {
key []byte
etype int
}
func (s *descbc) Sign(algo, usage int, data ...[]byte) ([]byte, error) {
var h hash.Hash
switch algo {
case signGssDes:
sz := 0
for _, d := range data {
sz += len(d)
}
sz = (sz + 7) &^ 7
u := make([]byte, sz)
v := u[:0]
for _, d := range data {
v = append(v, d...)
}
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCEncrypter(b, iv[:])
c.CryptBlocks(u, u)
return u[len(u)-8:], nil
case signGssMd5Des:
h = md5.New()
for _, d := range data {
h.Write(d)
}
return s.Sign(signGssDes, usage, h.Sum(nil))
case signMd5Des:
h = md5.New()
case signMd4Des:
h = md4.New()
default:
return unkeyedSign(algo, usage, data...)
}
var key [8]byte
for i := 0; i < 8; i++ {
key[i] = s.key[i] ^ 0xF0
}
chk := make([]byte, 24)
io.ReadFull(rand.Reader, chk[:8])
h.Write(chk[:8])
for _, d := range data {
h.Write(d)
}
h.Sum(chk[8:])
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCEncrypter(b, iv[:])
c.CryptBlocks(chk, chk)
return chk, nil
}
func (s *descbc) SignAlgo(usage int) int {
switch usage {
case gssWrapSign:
return signGssMd5Des
}
return signMd5Des
}
func (s *descbc) Encrypt(salt []byte, usage int, data ...[]byte) []byte {
var h hash.Hash
switch s.etype {
case cryptDesCbcMd5:
h = md5.New()
case cryptDesCbcMd4:
h = md4.New()
default:
panic("")
}
outsz := 8 + h.Size()
for _, d := range data {
outsz += len(d)
}
outsz = (outsz + 7) &^ 7
out := make([]byte, outsz)
io.ReadFull(rand.Reader, out[:8])
v := out[8+h.Size():]
for _, d := range data {
n := copy(v, d)
v = v[n:]
}
h.Write(out)
h.Sum(out[:8])
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCEncrypter(b, iv[:])
c.CryptBlocks(out, out)
return out
}
func (s *descbc) Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error) {
var h hash.Hash
switch algo {
case cryptDesCbcMd5:
h = md5.New()
case cryptDesCbcMd4:
h = md4.New()
default:
return nil, ErrProtocol
}
if (len(data) & 7) != 0 {
return nil, ErrProtocol
}
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCDecrypter(b, iv[:])
c.CryptBlocks(data, data)
chk := make([]byte, h.Size())
h.Write(data[:8])
h.Write(chk) // Just need h.Size() zero bytes instead of the checksum
h.Write(data[8+len(chk):])
h.Sum(chk[:0])
if subtle.ConstantTimeCompare(chk, data[8:8+len(chk)]) != 1 {
return nil, ErrProtocol
}
return data[8+len(chk):], nil
}
func (s *descbc) EncryptAlgo(usage int) int {
switch usage {
case gssWrapSeal, gssSequenceNumber:
return cryptGssDes
}
return s.etype
}
func (s *descbc) Key() []byte {
return s.key
}
func generateKey(algo int, rand io.Reader) (key, error) {
switch algo {
case cryptRc4Hmac:
data := [16]byte{}
if _, err := io.ReadFull(rand, data[:]); err != nil {
return nil, err
}
return loadKey(cryptRc4Hmac, data[:])
case cryptDesCbcMd4, cryptDesCbcMd5:
k := make([]byte, 8)
if _, err := io.ReadFull(rand, k[1:]); err != nil {
return nil, err
}
u := binary.BigEndian.Uint64(k)
u = fixweak(fixparity(u, true))
binary.BigEndian.PutUint64(k, u)
return loadKey(algo, k)
}
return nil, ErrProtocol
}
func loadKey(algo int, key []byte) (key, error) {
switch algo {
case cryptRc4Hmac:
return &rc4hmac{key}, nil
case cryptDesCbcMd4, cryptDesCbcMd5:
return &descbc{key, algo}, nil
}
return nil, ErrProtocol
}
func loadStringKey(algo int, pass, salt string) (key, error) {
if len(pass) == 0 {
return nil, ErrProtocol
}
switch algo {
case cryptRc4Hmac:
if len(salt) > 0 {
return nil, ErrProtocol
}
return &rc4hmac{rc4HmacKey(pass)}, nil
case cryptDesCbcMd4, cryptDesCbcMd5:
return &descbc{desStringKey(pass, salt), algo}, nil
}
return nil, ErrProtocol
}
func mustGenerateKey(algo int, rand io.Reader) key {
k, err := generateKey(algo, rand)
if err != nil {
panic(err)
}
return k
}
func mustLoadKey(algo int, key []byte) key {
k, err := loadKey(algo, key)
if err != nil {
panic(err)
}
return k
}
func mustLoadStringKey(algo int, pass, salt string) key {
k, err := loadStringKey(algo, pass, salt)
if err != nil {
panic(err)
}
return k
}