blob: 49a1b077405679f90440d4789f10a1597783889f [file] [log] [blame]
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +00001package crypto
2
3import (
4 "crypto/aes"
5 "crypto/hmac"
6 "crypto/sha256"
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// Aes128CtsHmacSha256128 implements Kerberos encryption type aes128-cts-hmac-sha256-128
18type Aes128CtsHmacSha256128 struct {
19}
20
21// GetETypeID returns the EType ID number.
22func (e Aes128CtsHmacSha256128) GetETypeID() int32 {
23 return etypeID.AES128_CTS_HMAC_SHA256_128
24}
25
26// GetHashID returns the checksum type ID number.
27func (e Aes128CtsHmacSha256128) GetHashID() int32 {
28 return chksumtype.HMAC_SHA256_128_AES128
29}
30
31// GetKeyByteSize returns the number of bytes for key of this etype.
32func (e Aes128CtsHmacSha256128) GetKeyByteSize() int {
33 return 128 / 8
34}
35
36// GetKeySeedBitLength returns the number of bits for the seed for key generation.
37func (e Aes128CtsHmacSha256128) GetKeySeedBitLength() int {
38 return e.GetKeyByteSize() * 8
39}
40
41// GetHashFunc returns the hash function for this etype.
42func (e Aes128CtsHmacSha256128) GetHashFunc() func() hash.Hash {
43 return sha256.New
44}
45
46// GetMessageBlockByteSize returns the block size for the etype's messages.
47func (e Aes128CtsHmacSha256128) GetMessageBlockByteSize() int {
48 return 1
49}
50
51// GetDefaultStringToKeyParams returns the default key derivation parameters in string form.
52func (e Aes128CtsHmacSha256128) GetDefaultStringToKeyParams() string {
53 return "00008000"
54}
55
56// GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations.
57func (e Aes128CtsHmacSha256128) GetConfounderByteSize() int {
58 return aes.BlockSize
59}
60
61// GetHMACBitLength returns the bit count size of the integrity hash.
62func (e Aes128CtsHmacSha256128) GetHMACBitLength() int {
63 return 128
64}
65
66// GetCypherBlockBitLength returns the bit count size of the cypher block.
67func (e Aes128CtsHmacSha256128) GetCypherBlockBitLength() int {
68 return aes.BlockSize * 8
69}
70
71// StringToKey returns a key derived from the string provided.
72func (e Aes128CtsHmacSha256128) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) {
73 saltp := rfc8009.GetSaltP(salt, "aes128-cts-hmac-sha256-128")
74 return rfc8009.StringToKey(secret, saltp, s2kparams, e)
75}
76
77// RandomToKey returns a key from the bytes provided.
78func (e Aes128CtsHmacSha256128) RandomToKey(b []byte) []byte {
79 return rfc8009.RandomToKey(b)
80}
81
82// EncryptData encrypts the data provided.
83func (e Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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 Aes128CtsHmacSha256128) 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}