Debian Codegraph @

crypto/rc4-hmac.go

raw symbols SCIP
1 package crypto
2
3 import (
4 "bytes"
5 "crypto/md5"
6 "hash"
7 "io"
8
9 "golang.org/x/crypto/md4"
10 "gopkg.in/jcmturner/gokrb5.v5/crypto/rfc3961"
11 "gopkg.in/jcmturner/gokrb5.v5/crypto/rfc4757"
12 "gopkg.in/jcmturner/gokrb5.v5/iana/chksumtype"
13 "gopkg.in/jcmturner/gokrb5.v5/iana/etypeID"
14 )
15
16 //http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/security/krb5/internal/crypto/dk/ArcFourCrypto.java#ArcFourCrypto.encrypt%28byte%5B%5D%2Cint%2Cbyte%5B%5D%2Cbyte%5B%5D%2Cbyte%5B%5D%2Cint%2Cint%29
17
18 // RC4HMAC implements Kerberos encryption type aes256-cts-hmac-sha1-96
19 type RC4HMAC struct {
20 }
21
22 // GetETypeID returns the EType ID number.
23 func (e RC4HMAC) GetETypeID() int32 {
24 return etypeID.RC4_HMAC
25 }
26
27 // GetHashID returns the checksum type ID number.
28 func (e RC4HMAC) GetHashID() int32 {
29 return chksumtype.KERB_CHECKSUM_HMAC_MD5
30 }
31
32 // GetKeyByteSize returns the number of bytes for key of this etype.
33 func (e RC4HMAC) GetKeyByteSize() int {
34 return 16
35 }
36
37 // GetKeySeedBitLength returns the number of bits for the seed for key generation.
38 func (e RC4HMAC) GetKeySeedBitLength() int {
39 return e.GetKeyByteSize() * 8
40 }
41
42 // GetHashFunc returns the hash function for this etype.
43 func (e RC4HMAC) GetHashFunc() func() hash.Hash {
44 return md5.New
45 }
46
47 // GetMessageBlockByteSize returns the block size for the etype's messages.
48 func (e RC4HMAC) GetMessageBlockByteSize() int {
49 return 1
50 }
51
52 // GetDefaultStringToKeyParams returns the default key derivation parameters in string form.
53 func (e RC4HMAC) GetDefaultStringToKeyParams() string {
54 return ""
55 }
56
57 // GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations.
58 func (e RC4HMAC) GetConfounderByteSize() int {
59 return 8
60 }
61
62 // GetHMACBitLength returns the bit count size of the integrity hash.
63 func (e RC4HMAC) GetHMACBitLength() int {
64 return md5.Size * 8
65 }
66
67 // GetCypherBlockBitLength returns the bit count size of the cypher block.
68 func (e RC4HMAC) GetCypherBlockBitLength() int {
69 return 8 // doesn't really apply
70 }
71
72 // StringToKey returns a key derived from the string provided.
73 func (e RC4HMAC) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) {
74 return rfc4757.StringToKey(secret)
75 }
76
77 // RandomToKey returns a key from the bytes provided.
78 func (e RC4HMAC) RandomToKey(b []byte) []byte {
79 r := bytes.NewReader(b)
80 h := md4.New()
81 io.Copy(h, r)
82 return h.Sum(nil)
83 }
84
85 // EncryptData encrypts the data provided.
86 func (e RC4HMAC) EncryptData(key, data []byte) ([]byte, []byte, error) {
87 b, err := rfc4757.EncryptData(key, data, e)
88 return []byte{}, b, err
89 }
90
91 // EncryptMessage encrypts the message provided and concatenates it with the integrity hash to create an encrypted message.
92 func (e RC4HMAC) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) {
93 b, err := rfc4757.EncryptMessage(key, message, usage, false, e)
94 return []byte{}, b, err
95 }
96
97 // DecryptData decrypts the data provided.
98 func (e RC4HMAC) DecryptData(key, data []byte) ([]byte, error) {
99 return rfc4757.DecryptData(key, data, e)
100 }
101
102 // DecryptMessage decrypts the message provided and verifies the integrity of the message.
103 func (e RC4HMAC) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) {
104 return rfc4757.DecryptMessage(key, ciphertext, usage, false, e)
105 }
106
107 // DeriveKey derives a key from the protocol key based on the usage value.
108 func (e RC4HMAC) DeriveKey(protocolKey, usage []byte) ([]byte, error) {
109 return rfc4757.HMAC(protocolKey, usage), nil
110 }
111
112 // DeriveRandom generates data needed for key generation.
113 func (e RC4HMAC) DeriveRandom(protocolKey, usage []byte) ([]byte, error) {
114 return rfc3961.DeriveRandom(protocolKey, usage, e)
115 }
116
117 // VerifyIntegrity checks the integrity of the plaintext message.
118 func (e RC4HMAC) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool {
119 return rfc4757.VerifyIntegrity(protocolKey, pt, ct, e)
120 }
121
122 // GetChecksumHash returns a keyed checksum hash of the bytes provided.
123 func (e RC4HMAC) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) {
124 return rfc4757.Checksum(protocolKey, usage, data)
125 }
126
127 // VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided.
128 func (e RC4HMAC) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool {
129 checksum, err := rfc4757.Checksum(protocolKey, usage, data)
130 if err != nil {
131 return false
132 }
133 if !bytes.Equal(checksum, chksum) {
134 return false
135 }
136 return true
137 }