rc4-hmac.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package crypto
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "hash"
  7. "io"
  8. "github.com/jcmturner/gokrb5/v8/crypto/rfc3961"
  9. "github.com/jcmturner/gokrb5/v8/crypto/rfc4757"
  10. "github.com/jcmturner/gokrb5/v8/iana/chksumtype"
  11. "github.com/jcmturner/gokrb5/v8/iana/etypeID"
  12. "golang.org/x/crypto/md4"
  13. )
  14. // RC4HMAC implements Kerberos encryption type rc4-hmac
  15. type RC4HMAC struct {
  16. }
  17. // GetETypeID returns the EType ID number.
  18. func (e RC4HMAC) GetETypeID() int32 {
  19. return etypeID.RC4_HMAC
  20. }
  21. // GetHashID returns the checksum type ID number.
  22. func (e RC4HMAC) GetHashID() int32 {
  23. return chksumtype.KERB_CHECKSUM_HMAC_MD5
  24. }
  25. // GetKeyByteSize returns the number of bytes for key of this etype.
  26. func (e RC4HMAC) GetKeyByteSize() int {
  27. return 16
  28. }
  29. // GetKeySeedBitLength returns the number of bits for the seed for key generation.
  30. func (e RC4HMAC) GetKeySeedBitLength() int {
  31. return e.GetKeyByteSize() * 8
  32. }
  33. // GetHashFunc returns the hash function for this etype.
  34. func (e RC4HMAC) GetHashFunc() func() hash.Hash {
  35. return md5.New
  36. }
  37. // GetMessageBlockByteSize returns the block size for the etype's messages.
  38. func (e RC4HMAC) GetMessageBlockByteSize() int {
  39. return 1
  40. }
  41. // GetDefaultStringToKeyParams returns the default key derivation parameters in string form.
  42. func (e RC4HMAC) GetDefaultStringToKeyParams() string {
  43. return ""
  44. }
  45. // GetConfounderByteSize returns the byte count for confounder to be used during cryptographic operations.
  46. func (e RC4HMAC) GetConfounderByteSize() int {
  47. return 8
  48. }
  49. // GetHMACBitLength returns the bit count size of the integrity hash.
  50. func (e RC4HMAC) GetHMACBitLength() int {
  51. return md5.Size * 8
  52. }
  53. // GetCypherBlockBitLength returns the bit count size of the cypher block.
  54. func (e RC4HMAC) GetCypherBlockBitLength() int {
  55. return 8 // doesn't really apply
  56. }
  57. // StringToKey returns a key derived from the string provided.
  58. func (e RC4HMAC) StringToKey(secret string, salt string, s2kparams string) ([]byte, error) {
  59. return rfc4757.StringToKey(secret)
  60. }
  61. // RandomToKey returns a key from the bytes provided.
  62. func (e RC4HMAC) RandomToKey(b []byte) []byte {
  63. r := bytes.NewReader(b)
  64. h := md4.New()
  65. io.Copy(h, r)
  66. return h.Sum(nil)
  67. }
  68. // EncryptData encrypts the data provided.
  69. func (e RC4HMAC) EncryptData(key, data []byte) ([]byte, []byte, error) {
  70. b, err := rfc4757.EncryptData(key, data, e)
  71. return []byte{}, b, err
  72. }
  73. // EncryptMessage encrypts the message provided and concatenates it with the integrity hash to create an encrypted message.
  74. func (e RC4HMAC) EncryptMessage(key, message []byte, usage uint32) ([]byte, []byte, error) {
  75. b, err := rfc4757.EncryptMessage(key, message, usage, false, e)
  76. return []byte{}, b, err
  77. }
  78. // DecryptData decrypts the data provided.
  79. func (e RC4HMAC) DecryptData(key, data []byte) ([]byte, error) {
  80. return rfc4757.DecryptData(key, data, e)
  81. }
  82. // DecryptMessage decrypts the message provided and verifies the integrity of the message.
  83. func (e RC4HMAC) DecryptMessage(key, ciphertext []byte, usage uint32) ([]byte, error) {
  84. return rfc4757.DecryptMessage(key, ciphertext, usage, false, e)
  85. }
  86. // DeriveKey derives a key from the protocol key based on the usage value.
  87. func (e RC4HMAC) DeriveKey(protocolKey, usage []byte) ([]byte, error) {
  88. return rfc4757.HMAC(protocolKey, usage), nil
  89. }
  90. // DeriveRandom generates data needed for key generation.
  91. func (e RC4HMAC) DeriveRandom(protocolKey, usage []byte) ([]byte, error) {
  92. return rfc3961.DeriveRandom(protocolKey, usage, e)
  93. }
  94. // VerifyIntegrity checks the integrity of the plaintext message.
  95. func (e RC4HMAC) VerifyIntegrity(protocolKey, ct, pt []byte, usage uint32) bool {
  96. return rfc4757.VerifyIntegrity(protocolKey, pt, ct, e)
  97. }
  98. // GetChecksumHash returns a keyed checksum hash of the bytes provided.
  99. func (e RC4HMAC) GetChecksumHash(protocolKey, data []byte, usage uint32) ([]byte, error) {
  100. return rfc4757.Checksum(protocolKey, usage, data)
  101. }
  102. // VerifyChecksum compares the checksum of the message bytes is the same as the checksum provided.
  103. func (e RC4HMAC) VerifyChecksum(protocolKey, data, chksum []byte, usage uint32) bool {
  104. checksum, err := rfc4757.Checksum(protocolKey, usage, data)
  105. if err != nil {
  106. return false
  107. }
  108. return hmac.Equal(checksum, chksum)
  109. }