Debian Codegraph @

ntlm/crypto.go

raw symbols SCIP
1 //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
2
3 package ntlm
4
5 import (
6 desP "crypto/des"
7 hmacP "crypto/hmac"
8 md5P "crypto/md5"
9 "crypto/rand"
10 rc4P "crypto/rc4"
11 crc32P "hash/crc32"
12
13 md4P "github.com/ThomsonReutersEikon/go-ntlm/ntlm/md4"
14 )
15
16 func md4(data []byte) []byte {
17 md4 := md4P.New()
18 md4.Write(data)
19 return md4.Sum(nil)
20 }
21
22 func md5(data []byte) []byte {
23 md5 := md5P.New()
24 md5.Write(data)
25 return md5.Sum(nil)
26 }
27
28 // Indicates the computation of a 16-byte HMAC-keyed MD5 message digest of the byte string M using the key K.
29 func hmacMd5(key []byte, data []byte) []byte {
30 mac := hmacP.New(md5P.New, key)
31 mac.Write(data)
32 return mac.Sum(nil)
33 }
34
35 // Indicates the computation of an N-byte cryptographic- strength random number.
36 func nonce(length int) []byte {
37 result := make([]byte, length)
38 rand.Read(result)
39 return result
40 }
41
42 func crc32(bytes []byte) uint32 {
43 crc := crc32P.New(crc32P.IEEETable)
44 crc.Write(bytes)
45 return crc.Sum32()
46 }
47
48 // Indicates the encryption of data item D with the key K using the RC4 algorithm.
49 func rc4K(key []byte, ciphertext []byte) ([]byte, error) {
50 cipher, err := rc4P.NewCipher(key)
51 if err != nil {
52 return nil, err
53 }
54 result := make([]byte, len(ciphertext))
55 cipher.XORKeyStream(result, ciphertext)
56 return result, nil
57 }
58
59 func rc4Init(key []byte) (cipher *rc4P.Cipher, err error) {
60 cipher, err = rc4P.NewCipher(key)
61 if err != nil {
62 return nil, err
63 }
64 return cipher, nil
65 }
66
67 func rc4(cipher *rc4P.Cipher, ciphertext []byte) []byte {
68 result := make([]byte, len(ciphertext))
69 cipher.XORKeyStream(result, ciphertext)
70 return result
71 }
72
73 // Indicates the encryption of an 8-byte data item D with the 7-byte key K using the Data Encryption Standard (DES)
74 // algorithm in Electronic Codebook (ECB) mode. The result is 8 bytes in length ([FIPS46-2]).
75 func des(key []byte, ciphertext []byte) ([]byte, error) {
76 calcKey := createDesKey(key)
77 cipher, err := desP.NewCipher(calcKey)
78 if err != nil {
79 return nil, err
80 }
81
82 result := make([]byte, len(ciphertext))
83 cipher.Encrypt(result, ciphertext)
84
85 return result, nil
86 }
87
88 // Indicates the encryption of an 8-byte data item D with the 16-byte key K using the Data Encryption Standard Long (DESL) algorithm.
89 // The result is 24 bytes in length. DESL(K, D) is computed as follows.
90 // Note K[] implies a key represented as a character array.
91 func desL(key []byte, cipherText []byte) ([]byte, error) {
92 out1, err := des(zeroPaddedBytes(key, 0, 7), cipherText)
93 if err != nil {
94 return nil, err
95 }
96
97 out2, err := des(zeroPaddedBytes(key, 7, 7), cipherText)
98 if err != nil {
99 return nil, err
100 }
101
102 out3, err := des(zeroPaddedBytes(key, 14, 7), cipherText)
103 if err != nil {
104 return nil, err
105 }
106
107 return concat(out1, out2, out3), nil
108 }
109
110 // Creates a DES encryption key from the given 7 byte key material.
111 func createDesKey(keyBytes []byte) []byte {
112 material := zeroBytes(8)
113 material[0] = keyBytes[0]
114 material[1] = (byte)(keyBytes[0]<<7 | (keyBytes[1]&0xff)>>1)
115 material[2] = (byte)(keyBytes[1]<<6 | (keyBytes[2]&0xff)>>2)
116 material[3] = (byte)(keyBytes[2]<<5 | (keyBytes[3]&0xff)>>3)
117 material[4] = (byte)(keyBytes[3]<<4 | (keyBytes[4]&0xff)>>4)
118 material[5] = (byte)(keyBytes[4]<<3 | (keyBytes[5]&0xff)>>5)
119 material[6] = (byte)(keyBytes[5]<<2 | (keyBytes[6]&0xff)>>6)
120 material[7] = (byte)(keyBytes[6] << 1)
121 oddParity(material)
122 return material
123 }
124
125 // Applies odd parity to the given byte array.
126 func oddParity(bytes []byte) {
127 for i := 0; i < len(bytes); i++ {
128 b := bytes[i]
129 needsParity := (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 4) ^ (b >> 3) ^ (b >> 2) ^ (b >> 1)) & 0x01) == 0
130 if needsParity {
131 bytes[i] = bytes[i] | byte(0x01)
132 } else {
133 bytes[i] = bytes[i] & byte(0xfe)
134 }
135 }
136 }