blob: 6a54475930e0bde3b38e6de2d393186a7b57a92e [file] [log] [blame]
David K. Bainbridgebd6b2882021-08-26 13:31:02 +00001package crypto
2
3import (
4 "crypto/aes"
5 "crypto/hmac"
6 "crypto/sha512"
7 "hash"
8
9 "github.com/jcmturner/gokrb5/v8/crypto/common"
10 "github.com/jcmturner/gokrb5/v8/crypto/rfc8009"
11 "github.com/jcmturner/gokrb5/v8/iana/chksumtype"
12 "github.com/jcmturner/gokrb5/v8/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// As the hash is calculated over the iv concatenated with the AES cipher output not the plaintext the pt value to this
114// interface method is not use. Pass any []byte.
115func (e Aes256CtsHmacSha384192) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool {
116 // We don't need ib just there for the interface
117 return rfc8009.VerifyIntegrity(protocolKey, ct, usage, e)
118}
119
120// GetChecksumHash returns a keyed checksum hash of the bytes provided.
121func (e Aes256CtsHmacSha384192) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) {
122 return common.GetHash(data, protocolKey, common.GetUsageKc(usage), e)
123}
124
125// VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided.
126func (e Aes256CtsHmacSha384192) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool {
127 c, err := e.GetChecksumHash(protocolKey, data, usage)
128 if err != nil {
129 return false
130 }
131 return hmac.Equal(chksum, c)
132}