blob: 562b078654e5736cf3709b932ac1bc9c3c1684d0 [file] [log] [blame]
Scott Bakered4efab2020-01-13 19:12:25 -08001package crypto
2
3import (
4 "crypto/aes"
5 "crypto/hmac"
6 "crypto/sha512"
7 "hash"
8
9 "gopkg.in/jcmturner/gokrb5.v7/crypto/common"
10 "gopkg.in/jcmturner/gokrb5.v7/crypto/rfc8009"
11 "gopkg.in/jcmturner/gokrb5.v7/iana/chksumtype"
12 "gopkg.in/jcmturner/gokrb5.v7/iana/etypeID"
13)
14
15// RFC https://tools.ietf.org/html/rfc8009
16
17// Aes256CtsHmacSha384192 implements Kerberos encryption type aes256-cts-hmac-sha384-192
18type Aes256CtsHmacSha384192 struct {
19}
20
21// GetETypeID returns the EType ID number.
22func (e Aes256CtsHmacSha384192) GetETypeID() int32 {
23 return etypeID.AES256_CTS_HMAC_SHA384_192
24}
25
26// GetHashID returns the checksum type ID number.
27func (e Aes256CtsHmacSha384192) GetHashID() int32 {
28 return chksumtype.HMAC_SHA384_192_AES256
29}
30
31// GetKeyByteSize returns the number of bytes for key of this etype.
32func (e Aes256CtsHmacSha384192) GetKeyByteSize() int {
33 return 192 / 8
34}
35
36// GetKeySeedBitLength returns the number of bits for the seed for key generation.
37func (e Aes256CtsHmacSha384192) GetKeySeedBitLength() int {
38 return e.GetKeyByteSize() * 8
39}
40
41// GetHashFunc returns the hash function for this etype.
42func (e Aes256CtsHmacSha384192) GetHashFunc() func() hash.Hash {
43 return sha512.New384
44}
45
46// GetMessageBlockByteSize returns the block size for the etype's messages.
47func (e Aes256CtsHmacSha384192) GetMessageBlockByteSize() int {
48 return 1
49}
50
51// GetDefaultStringToKeyParams returns the default key derivation parameters in string form.
52func (e Aes256CtsHmacSha384192) GetDefaultStringToKeyParams() string {
53 return "00008000"
54}
55
56// GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations.
57func (e Aes256CtsHmacSha384192) GetConfounderByteSize() int {
58 return aes.BlockSize
59}
60
61// GetHMACBitLength returns the bit count size of the integrity hash.
62func (e Aes256CtsHmacSha384192) GetHMACBitLength() int {
63 return 192
64}
65
66// GetCypherBlockBitLength returns the bit count size of the cypher block.
67func (e Aes256CtsHmacSha384192) GetCypherBlockBitLength() int {
68 return aes.BlockSize * 8
69}
70
71// StringToKey returns a key derived from the string provided.
72func (e Aes256CtsHmacSha384192) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) {
73 saltp := rfc8009.GetSaltP(salt, "aes256-cts-hmac-sha384-192")
74 return rfc8009.StringToKey(secret, saltp, s2kparams, e)
75}
76
77// RandomToKey returns a key from the bytes provided.
78func (e Aes256CtsHmacSha384192) RandomToKey(b []byte) []byte {
79 return rfc8009.RandomToKey(b)
80}
81
82// EncryptData encrypts the data provided.
83func (e Aes256CtsHmacSha384192) EncryptData(key, data []byte) ([]byte, []byte, error) {
84 return rfc8009.EncryptData(key, data, e)
85}
86
87// EncryptMessage encrypts the message provided and concatenates it with the integrity hash to create an encrypted message.
88func (e Aes256CtsHmacSha384192) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) {
89 return rfc8009.EncryptMessage(key, message, usage, e)
90}
91
92// DecryptData decrypts the data provided.
93func (e Aes256CtsHmacSha384192) DecryptData(key, data []byte) ([]byte, error) {
94 return rfc8009.DecryptData(key, data, e)
95}
96
97// DecryptMessage decrypts the message provided and verifies the integrity of the message.
98func (e Aes256CtsHmacSha384192) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) {
99 return rfc8009.DecryptMessage(key, ciphertext, usage, e)
100}
101
102// DeriveKey derives a key from the protocol key based on the usage value.
103func (e Aes256CtsHmacSha384192) DeriveKey(protocolKey, usage []byte) ([]byte, error) {
104 return rfc8009.DeriveKey(protocolKey, usage, e), nil
105}
106
107// DeriveRandom generates data needed for key generation.
108func (e Aes256CtsHmacSha384192) DeriveRandom(protocolKey, usage []byte) ([]byte, error) {
109 return rfc8009.DeriveRandom(protocolKey, usage, e)
110}
111
112// VerifyIntegrity checks the integrity of the ciphertext message.
113// The HMAC is calculated over the cipher state concatenated with the
114// AES output, instead of being calculated over the confounder and
115// plaintext. This allows the message receiver to verify the
116// integrity of the message before decrypting the message.
117// Therefore the pt value to this interface method is not use. Pass any []byte.
118func (e Aes256CtsHmacSha384192) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool {
119 // We don't need ib just there for the interface
120 return rfc8009.VerifyIntegrity(protocolKey, ct, usage, e)
121}
122
123// GetChecksumHash returns a keyed checksum hash of the bytes provided.
124func (e Aes256CtsHmacSha384192) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) {
125 return common.GetHash(data, protocolKey, common.GetUsageKc(usage), e)
126}
127
128// VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided.
129func (e Aes256CtsHmacSha384192) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool {
130 c, err := e.GetChecksumHash(protocolKey, data, usage)
131 if err != nil {
132 return false
133 }
134 return hmac.Equal(chksum, c)
135}