Debian Codegraph @

crypto/des3-cbc-sha1-kd.go

raw symbols SCIP
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 //RFC: 3961 Section 6.3
17
18 // Des3CbcSha1Kd implements Kerberos encryption type des3-cbc-hmac-sha1-kd
19 type Des3CbcSha1Kd struct {
20 }
21
22 // GetETypeID returns the EType ID number.
23 func (e Des3CbcSha1Kd) GetETypeID() int32 {
24 return etypeID.DES3_CBC_SHA1_KD
25 }
26
27 // GetHashID returns the checksum type ID number.
28 func (e Des3CbcSha1Kd) GetHashID() int32 {
29 return chksumtype.HMAC_SHA1_DES3_KD
30 }
31
32 // GetKeyByteSize returns the number of bytes for key of this etype.
33 func (e Des3CbcSha1Kd) GetKeyByteSize() int {
34 return 24
35 }
36
37 // GetKeySeedBitLength returns the number of bits for the seed for key generation.
38 func (e Des3CbcSha1Kd) GetKeySeedBitLength() int {
39 return 21 * 8
40 }
41
42 // GetHashFunc returns the hash function for this etype.
43 func (e Des3CbcSha1Kd) GetHashFunc() func() hash.Hash {
44 return sha1.New
45 }
46
47 // GetMessageBlockByteSize returns the block size for the etype's messages.
48 func (e Des3CbcSha1Kd) GetMessageBlockByteSize() int {
49 return des.BlockSize
50 }
51
52 // GetDefaultStringToKeyParams returns the default key derivation parameters in string form.
53 func (e Des3CbcSha1Kd) GetDefaultStringToKeyParams() string {
54 var s string
55 return s
56 }
57
58 // GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations.
59 func (e Des3CbcSha1Kd) GetConfounderByteSize() int {
60 return des.BlockSize
61 }
62
63 // GetHMACBitLength returns the bit count size of the integrity hash.
64 func (e Des3CbcSha1Kd) GetHMACBitLength() int {
65 return e.GetHashFunc()().Size() * 8
66 }
67
68 // GetCypherBlockBitLength returns the bit count size of the cypher block.
69 func (e Des3CbcSha1Kd) GetCypherBlockBitLength() int {
70 return des.BlockSize * 8
71 }
72
73 // StringToKey returns a key derived from the string provided.
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 // RandomToKey returns a key from the bytes provided.
82 func (e Des3CbcSha1Kd) RandomToKey(b []byte) []byte {
83 return rfc3961.DES3RandomToKey(b)
84 }
85
86 // DeriveRandom generates data needed for key generation.
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 // DeriveKey derives a key from the protocol key based on the usage value.
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 // EncryptData encrypts the data provided.
102 func (e Des3CbcSha1Kd) EncryptData(key, data []byte) ([]byte, []byte, error) {
103 return rfc3961.DES3EncryptData(key, data, e)
104 }
105
106 // EncryptMessage encrypts the message provided and concatenates it with the integrity hash to create an encrypted message.
107 func (e Des3CbcSha1Kd) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) {
108 return rfc3961.DES3EncryptMessage(key, message, usage, e)
109 }
110
111 // DecryptData decrypts the data provided.
112 func (e Des3CbcSha1Kd) DecryptData(key, data []byte) ([]byte, error) {
113 return rfc3961.DES3DecryptData(key, data, e)
114 }
115
116 // DecryptMessage decrypts the message provided and verifies the integrity of the message.
117 func (e Des3CbcSha1Kd) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) {
118 return rfc3961.DES3DecryptMessage(key, ciphertext, usage, e)
119 }
120
121 // VerifyIntegrity checks the integrity of the plaintext message.
122 func (e Des3CbcSha1Kd) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool {
123 return rfc3961.VerifyIntegrity(protocolKey, ct, pt, usage, e)
124 }
125
126 // GetChecksumHash returns a keyed checksum hash of the bytes provided.
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 // VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided.
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 }