Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 1 | package crypto |
| 2 | |
| 3 | import ( |
| 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 |
| 18 | type Aes256CtsHmacSha384192 struct { |
| 19 | } |
| 20 | |
| 21 | // GetETypeID returns the EType ID number. |
| 22 | func (e Aes256CtsHmacSha384192) GetETypeID() int32 { |
| 23 | return etypeID.AES256_CTS_HMAC_SHA384_192 |
| 24 | } |
| 25 | |
| 26 | // GetHashID returns the checksum type ID number. |
| 27 | func (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. |
| 32 | func (e Aes256CtsHmacSha384192) GetKeyByteSize() int { |
| 33 | return 192 / 8 |
| 34 | } |
| 35 | |
| 36 | // GetKeySeedBitLength returns the number of bits for the seed for key generation. |
| 37 | func (e Aes256CtsHmacSha384192) GetKeySeedBitLength() int { |
| 38 | return e.GetKeyByteSize() * 8 |
| 39 | } |
| 40 | |
| 41 | // GetHashFunc returns the hash function for this etype. |
| 42 | func (e Aes256CtsHmacSha384192) GetHashFunc() func() hash.Hash { |
| 43 | return sha512.New384 |
| 44 | } |
| 45 | |
| 46 | // GetMessageBlockByteSize returns the block size for the etype's messages. |
| 47 | func (e Aes256CtsHmacSha384192) GetMessageBlockByteSize() int { |
| 48 | return 1 |
| 49 | } |
| 50 | |
| 51 | // GetDefaultStringToKeyParams returns the default key derivation parameters in string form. |
| 52 | func (e Aes256CtsHmacSha384192) GetDefaultStringToKeyParams() string { |
| 53 | return "00008000" |
| 54 | } |
| 55 | |
| 56 | // GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations. |
| 57 | func (e Aes256CtsHmacSha384192) GetConfounderByteSize() int { |
| 58 | return aes.BlockSize |
| 59 | } |
| 60 | |
| 61 | // GetHMACBitLength returns the bit count size of the integrity hash. |
| 62 | func (e Aes256CtsHmacSha384192) GetHMACBitLength() int { |
| 63 | return 192 |
| 64 | } |
| 65 | |
| 66 | // GetCypherBlockBitLength returns the bit count size of the cypher block. |
| 67 | func (e Aes256CtsHmacSha384192) GetCypherBlockBitLength() int { |
| 68 | return aes.BlockSize * 8 |
| 69 | } |
| 70 | |
| 71 | // StringToKey returns a key derived from the string provided. |
| 72 | func (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. |
| 78 | func (e Aes256CtsHmacSha384192) RandomToKey(b []byte) []byte { |
| 79 | return rfc8009.RandomToKey(b) |
| 80 | } |
| 81 | |
| 82 | // EncryptData encrypts the data provided. |
| 83 | func (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. |
| 88 | func (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. |
| 93 | func (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. |
| 98 | func (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. |
| 103 | func (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. |
| 108 | func (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. |
| 115 | func (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. |
| 121 | func (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. |
| 126 | func (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 | } |