des3-cbc-sha1-kd.go 4.5 KB

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