khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | package crypto |
| 2 | |
| 3 | import ( |
| 4 | "crypto/aes" |
| 5 | "crypto/hmac" |
| 6 | "crypto/sha1" |
| 7 | "hash" |
| 8 | |
| 9 | "github.com/jcmturner/gokrb5/v8/crypto/common" |
| 10 | "github.com/jcmturner/gokrb5/v8/crypto/rfc3961" |
| 11 | "github.com/jcmturner/gokrb5/v8/crypto/rfc3962" |
| 12 | "github.com/jcmturner/gokrb5/v8/iana/chksumtype" |
| 13 | "github.com/jcmturner/gokrb5/v8/iana/etypeID" |
| 14 | ) |
| 15 | |
| 16 | // RFC 3962 |
| 17 | |
| 18 | // Aes256CtsHmacSha96 implements Kerberos encryption type aes256-cts-hmac-sha1-96 |
| 19 | type Aes256CtsHmacSha96 struct { |
| 20 | } |
| 21 | |
| 22 | // GetETypeID returns the EType ID number. |
| 23 | func (e Aes256CtsHmacSha96) GetETypeID() int32 { |
| 24 | return etypeID.AES256_CTS_HMAC_SHA1_96 |
| 25 | } |
| 26 | |
| 27 | // GetHashID returns the checksum type ID number. |
| 28 | func (e Aes256CtsHmacSha96) GetHashID() int32 { |
| 29 | return chksumtype.HMAC_SHA1_96_AES256 |
| 30 | } |
| 31 | |
| 32 | // GetKeyByteSize returns the number of bytes for key of this etype. |
| 33 | func (e Aes256CtsHmacSha96) GetKeyByteSize() int { |
| 34 | return 256 / 8 |
| 35 | } |
| 36 | |
| 37 | // GetKeySeedBitLength returns the number of bits for the seed for key generation. |
| 38 | func (e Aes256CtsHmacSha96) GetKeySeedBitLength() int { |
| 39 | return e.GetKeyByteSize() * 8 |
| 40 | } |
| 41 | |
| 42 | // GetHashFunc returns the hash function for this etype. |
| 43 | func (e Aes256CtsHmacSha96) GetHashFunc() func() hash.Hash { |
| 44 | return sha1.New |
| 45 | } |
| 46 | |
| 47 | // GetMessageBlockByteSize returns the block size for the etype's messages. |
| 48 | func (e Aes256CtsHmacSha96) GetMessageBlockByteSize() int { |
| 49 | return 1 |
| 50 | } |
| 51 | |
| 52 | // GetDefaultStringToKeyParams returns the default key derivation parameters in string form. |
| 53 | func (e Aes256CtsHmacSha96) GetDefaultStringToKeyParams() string { |
| 54 | return "00001000" |
| 55 | } |
| 56 | |
| 57 | // GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations. |
| 58 | func (e Aes256CtsHmacSha96) GetConfounderByteSize() int { |
| 59 | return aes.BlockSize |
| 60 | } |
| 61 | |
| 62 | // GetHMACBitLength returns the bit count size of the integrity hash. |
| 63 | func (e Aes256CtsHmacSha96) GetHMACBitLength() int { |
| 64 | return 96 |
| 65 | } |
| 66 | |
| 67 | // GetCypherBlockBitLength returns the bit count size of the cypher block. |
| 68 | func (e Aes256CtsHmacSha96) GetCypherBlockBitLength() int { |
| 69 | return aes.BlockSize * 8 |
| 70 | } |
| 71 | |
| 72 | // StringToKey returns a key derived from the string provided. |
| 73 | func (e Aes256CtsHmacSha96) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) { |
| 74 | return rfc3962.StringToKey(secret, salt, s2kparams, e) |
| 75 | } |
| 76 | |
| 77 | // RandomToKey returns a key from the bytes provided. |
| 78 | func (e Aes256CtsHmacSha96) RandomToKey(b []byte) []byte { |
| 79 | return rfc3961.RandomToKey(b) |
| 80 | } |
| 81 | |
| 82 | // EncryptData encrypts the data provided. |
| 83 | func (e Aes256CtsHmacSha96) EncryptData(key, data []byte) ([]byte, []byte, error) { |
| 84 | return rfc3962.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 Aes256CtsHmacSha96) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) { |
| 89 | return rfc3962.EncryptMessage(key, message, usage, e) |
| 90 | } |
| 91 | |
| 92 | // DecryptData decrypts the data provided. |
| 93 | func (e Aes256CtsHmacSha96) DecryptData(key, data []byte) ([]byte, error) { |
| 94 | return rfc3962.DecryptData(key, data, e) |
| 95 | } |
| 96 | |
| 97 | // DecryptMessage decrypts the message provided and verifies the integrity of the message. |
| 98 | func (e Aes256CtsHmacSha96) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) { |
| 99 | return rfc3962.DecryptMessage(key, ciphertext, usage, e) |
| 100 | } |
| 101 | |
| 102 | // DeriveKey derives a key from the protocol key based on the usage value. |
| 103 | func (e Aes256CtsHmacSha96) DeriveKey(protocolKey, usage []byte) ([]byte, error) { |
| 104 | return rfc3961.DeriveKey(protocolKey, usage, e) |
| 105 | } |
| 106 | |
| 107 | // DeriveRandom generates data needed for key generation. |
| 108 | func (e Aes256CtsHmacSha96) DeriveRandom(protocolKey, usage []byte) ([]byte, error) { |
| 109 | return rfc3961.DeriveRandom(protocolKey, usage, e) |
| 110 | } |
| 111 | |
| 112 | // VerifyIntegrity checks the integrity of the plaintext message. |
| 113 | func (e Aes256CtsHmacSha96) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool { |
| 114 | return rfc3961.VerifyIntegrity(protocolKey, ct, pt, usage, e) |
| 115 | } |
| 116 | |
| 117 | // GetChecksumHash returns a keyed checksum hash of the bytes provided. |
| 118 | func (e Aes256CtsHmacSha96) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) { |
| 119 | return common.GetHash(data, protocolKey, common.GetUsageKc(usage), e) |
| 120 | } |
| 121 | |
| 122 | // VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided. |
| 123 | func (e Aes256CtsHmacSha96) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool { |
| 124 | c, err := e.GetChecksumHash(protocolKey, data, usage) |
| 125 | if err != nil { |
| 126 | return false |
| 127 | } |
| 128 | return hmac.Equal(chksum, c) |
| 129 | } |