| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/des" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/sha1" |
| 7 |
"errors" |
| 8 |
"hash" |
| 9 |
|
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/crypto/common" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/crypto/rfc3961" |
| 12 |
"gopkg.in/jcmturner/gokrb5.v5/iana/chksumtype" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/iana/etypeID" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
type Des3CbcSha1Kd struct { |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
func (e Des3CbcSha1Kd) GetETypeID() int32 { |
| 24 |
return etypeID.DES3_CBC_SHA1_KD |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
func (e Des3CbcSha1Kd) GetHashID() int32 { |
| 29 |
return chksumtype.HMAC_SHA1_DES3_KD |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func (e Des3CbcSha1Kd) GetKeyByteSize() int { |
| 34 |
return 24 |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func (e Des3CbcSha1Kd) GetKeySeedBitLength() int { |
| 39 |
return 21 * 8 |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (e Des3CbcSha1Kd) GetHashFunc() func() hash.Hash { |
| 44 |
return sha1.New |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (e Des3CbcSha1Kd) GetMessageBlockByteSize() int { |
| 49 |
return des.BlockSize |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (e Des3CbcSha1Kd) GetDefaultStringToKeyParams() string { |
| 54 |
var s string |
| 55 |
return s |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func (e Des3CbcSha1Kd) GetConfounderByteSize() int { |
| 60 |
return des.BlockSize |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (e Des3CbcSha1Kd) GetHMACBitLength() int { |
| 65 |
return e.GetHashFunc()().Size() * 8 |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (e Des3CbcSha1Kd) GetCypherBlockBitLength() int { |
| 70 |
return des.BlockSize * 8 |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (e Des3CbcSha1Kd) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) { |
| 75 |
if s2kparams != "" { |
| 76 |
return []byte{}, errors.New("s2kparams must be an empty string") |
| 77 |
} |
| 78 |
return rfc3961.DES3StringToKey(secret, salt, e) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (e Des3CbcSha1Kd) RandomToKey(b []byte) []byte { |
| 83 |
return rfc3961.DES3RandomToKey(b) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (e Des3CbcSha1Kd) DeriveRandom(protocolKey, usage []byte) ([]byte, error) { |
| 88 |
r, err := rfc3961.DeriveRandom(protocolKey, usage, e) |
| 89 |
return r, err |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func (e Des3CbcSha1Kd) DeriveKey(protocolKey, usage []byte) ([]byte, error) { |
| 94 |
r, err := e.DeriveRandom(protocolKey, usage) |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
return e.RandomToKey(r), nil |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func (e Des3CbcSha1Kd) EncryptData(key, data []byte) ([]byte, []byte, error) { |
| 103 |
return rfc3961.DES3EncryptData(key, data, e) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
func (e Des3CbcSha1Kd) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) { |
| 108 |
return rfc3961.DES3EncryptMessage(key, message, usage, e) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func (e Des3CbcSha1Kd) DecryptData(key, data []byte) ([]byte, error) { |
| 113 |
return rfc3961.DES3DecryptData(key, data, e) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func (e Des3CbcSha1Kd) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) { |
| 118 |
return rfc3961.DES3DecryptMessage(key, ciphertext, usage, e) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
func (e Des3CbcSha1Kd) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool { |
| 123 |
return rfc3961.VerifyIntegrity(protocolKey, ct, pt, usage, e) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func (e Des3CbcSha1Kd) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) { |
| 128 |
return common.GetHash(data, protocolKey, common.GetUsageKc(usage), e) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (e Des3CbcSha1Kd) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool { |
| 133 |
c, err := e.GetChecksumHash(protocolKey, data, usage) |
| 134 |
if err != nil { |
| 135 |
return false |
| 136 |
} |
| 137 |
return hmac.Equal(chksum, c) |
| 138 |
} |