rc4-hmac.go 4.3 KB

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