EncryptionEngine.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package crypto
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. "github.com/jcmturner/gokrb5/types"
  9. "hash"
  10. "encoding/hex"
  11. )
  12. type EType interface {
  13. GetETypeID() int
  14. GetKeyByteSize() int // See "protocol key format" for defined values
  15. GetKeySeedBitLength() int // key-generation seed length, k
  16. GetDefaultStringToKeyParams() string // default string-to-key parameters (s2kparams)
  17. StringToKey(string, salt, s2kparams string) ([]byte, error) // string-to-key (UTF-8 string, UTF-8 string, opaque)->(protocol-key)
  18. RandomToKey(b []byte) []byte // random-to-key (bitstring[K])->(protocol-key)
  19. GetHMACBitLength() int // HMAC output size, h
  20. GetMessageBlockByteSize() int // message block size, m
  21. Encrypt(key, message []byte) ([]byte, []byte, error) // E function - encrypt (specific-key, state, octet string)->(state, octet string)
  22. Decrypt(key, ciphertext []byte) ([]byte, error) // D function
  23. GetCypherBlockBitLength() int // cipher block size, c
  24. GetConfounderByteSize() int // This is the same as the cipher block size but in bytes.
  25. DeriveKey(protocolKey, usage []byte) ([]byte, error) // DK key-derivation (protocol-key, integer)->(specific-key)
  26. DeriveRandom(protocolKey, usage []byte) ([]byte, error) // DR pseudo-random (protocol-key, octet-string)->(octet-string)
  27. VerifyChecksum(protocolKey, ct, pt []byte, usage int) bool
  28. GetHash() hash.Hash
  29. }
  30. func GetEtype(id int) (EType, error) {
  31. switch id {
  32. case 17:
  33. var et Aes128CtsHmacSha96
  34. return et, nil
  35. case 18:
  36. var et Aes256CtsHmacSha96
  37. return et, nil
  38. default:
  39. return nil, fmt.Errorf("Unknown or unsupported EType: %d", id)
  40. }
  41. }
  42. // RFC3961: DR(Key, Constant) = k-truncate(E(Key, Constant, initial-cipher-state))
  43. // key - base key or protocol key. Likely to be a key from a keytab file
  44. // TODO usage - a constant
  45. // n - block size in bits (not bytes) - note if you use something like aes.BlockSize this is in bytes.
  46. // k - key length / key seed length in bits. Eg. for AES256 this value is 256
  47. // encrypt - the encryption function to use
  48. func deriveRandom(key, usage []byte, n, k int, e EType) ([]byte, error) {
  49. //Ensure the usage constant is at least the size of the cypher block size. Pass it through the nfold algorithm that will "stretch" it if needs be.
  50. nFoldUsage := Nfold(usage, n)
  51. //k-truncate implemented by creating a byte array the size of k (k is in bits hence /8)
  52. out := make([]byte, k/8)
  53. /*If the output of E is shorter than k bits, it is fed back into the encryption as many times as necessary.
  54. The construct is as follows (where | indicates concatentation):
  55. K1 = E(Key, n-fold(Constant), initial-cipher-state)
  56. K2 = E(Key, K1, initial-cipher-state)
  57. K3 = E(Key, K2, initial-cipher-state)
  58. K4 = ...
  59. DR(Key, Constant) = k-truncate(K1 | K2 | K3 | K4 ...)*/
  60. _, K, err := e.Encrypt(key, nFoldUsage)
  61. if err != nil {
  62. return out, err
  63. }
  64. for i := copy(out, K); i < len(out); {
  65. _, K, _ = e.Encrypt(key, K)
  66. i = i + copy(out[i:], K)
  67. }
  68. return out, nil
  69. }
  70. func zeroPad(b []byte, m int) ([]byte, error) {
  71. if m <= 0 {
  72. return nil, errors.New("Invalid message block size when padding")
  73. }
  74. if b == nil || len(b) == 0 {
  75. return nil, errors.New("Data not valid to pad: Zero size")
  76. }
  77. if l := len(b) % m; l != 0 {
  78. n := m - l
  79. z := make([]byte, n)
  80. b = append(b, z...)
  81. }
  82. return b, nil
  83. }
  84. func pkcs7Pad(b []byte, m int) ([]byte, error) {
  85. if m <= 0 {
  86. return nil, errors.New("Invalid message block size when padding")
  87. }
  88. if b == nil || len(b) == 0 {
  89. return nil, errors.New("Data not valid to pad: Zero size")
  90. }
  91. n := m - (len(b) % m)
  92. pb := make([]byte, len(b)+n)
  93. copy(pb, b)
  94. copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n))
  95. return pb, nil
  96. }
  97. func pkcs7Unpad(b []byte, m int) ([]byte, error) {
  98. if m <= 0 {
  99. return nil, errors.New("Invalid message block size when unpadding")
  100. }
  101. if b == nil || len(b) == 0 {
  102. return nil, errors.New("Padded data not valid: Zero size")
  103. }
  104. if len(b)%m != 0 {
  105. return nil, errors.New("Padded data not valid: Not multiple of message block size")
  106. }
  107. c := b[len(b)-1]
  108. n := int(c)
  109. if n == 0 || n > len(b) {
  110. return nil, errors.New("Padded data not valid: Data may not have been padded")
  111. }
  112. for i := 0; i < n; i++ {
  113. if b[len(b)-n+i] != c {
  114. return nil, errors.New("Padded data not valid")
  115. }
  116. }
  117. return b[:len(b)-n], nil
  118. }
  119. func DecryptEncPart(key []byte, pe types.EncryptedData, etype EType, usage uint32) ([]byte, error) {
  120. //Derive the key
  121. //TODO need to consider PAdata for deriving key
  122. k, err := etype.DeriveKey(key, GetUsageKe(usage))
  123. if err != nil {
  124. return nil, fmt.Errorf("Error deriving key: %v", err)
  125. }
  126. // Strip off the checksum from the end
  127. b, err := etype.Decrypt(k, pe.Cipher[:len(pe.Cipher)-etype.GetHMACBitLength()/8])
  128. if err != nil {
  129. return nil, fmt.Errorf("Error decrypting: %v", err)
  130. }
  131. //Verify checksum
  132. if !etype.VerifyChecksum(key, pe.Cipher, b, int(usage)) {
  133. return nil, errors.New("Error decrypting encrypted part: checksum verification failed")
  134. }
  135. //Remove the confounder bytes
  136. b = b[etype.GetConfounderByteSize():]
  137. if err != nil {
  138. return nil, fmt.Errorf("Error decrypting encrypted part: %v", err)
  139. }
  140. return b, nil
  141. }
  142. func GetKeyFromPassword(passwd string, cn types.PrincipalName, realm string, etypeId int, pas types.PADataSequence) ([]byte, EType, error) {
  143. var key []byte
  144. var etype EType
  145. for _, pa := range pas {
  146. if pa.PADataType == 19 {
  147. var et2 types.ETypeInfo2
  148. err := et2.Unmarshal(pa.PADataValue)
  149. if err != nil {
  150. return key, etype, fmt.Errorf("Error unmashalling PA Data to PA-ETYPE-INFO2: %v", err)
  151. }
  152. etype, err := GetEtype(et2[0].EType)
  153. if err != nil {
  154. return key, etype, fmt.Errorf("Error getting encryption type: %v", err)
  155. }
  156. sk2p := etype.GetDefaultStringToKeyParams()
  157. if len(et2[0].S2KParams) == 8 {
  158. sk2p = hex.EncodeToString(et2[0].S2KParams)
  159. }
  160. key, err := etype.StringToKey(passwd, et2[0].Salt, sk2p)
  161. if err != nil {
  162. return key, etype, fmt.Errorf("Error deriving key from string: %+v", err)
  163. }
  164. return key, etype, nil
  165. }
  166. }
  167. etype, err := GetEtype(etypeId)
  168. if err != nil {
  169. return key, etype, fmt.Errorf("Error getting encryption type: %v", err)
  170. }
  171. sk2p := etype.GetDefaultStringToKeyParams()
  172. key, err = etype.StringToKey(passwd, cn.GetSalt(realm), sk2p)
  173. if err != nil {
  174. return key, etype, fmt.Errorf("Error deriving key from string: %+v", err)
  175. }
  176. return key, etype, nil
  177. }
  178. func GetChecksum(pt, key []byte, usage int, etype EType) ([]byte, error) {
  179. k, err := etype.DeriveKey(key, GetUsageKi(uint32(usage)))
  180. if err != nil {
  181. return nil, fmt.Errorf("Unable to derive key for checksum: %v", err)
  182. }
  183. mac := hmac.New(etype.GetHash, k)
  184. //TODO do I need to append the ivz before taking the hash?
  185. //ivz := make([]byte, etype.GetConfounderByteSize())
  186. //pt = append(ivz, pt...)
  187. //if r := len(pt)%etype.GetMessageBlockByteSize(); r != 0 {
  188. // t := make([]byte, etype.GetMessageBlockByteSize() - r)
  189. // pt = append(pt, t...)
  190. //}
  191. mac.Write(pt)
  192. return mac.Sum(nil), nil
  193. }
  194. func VerifyChecksum(key, ct, pt []byte, usage int, etype EType) bool {
  195. //The ciphertext output is the concatenation of the output of the basic
  196. //encryption function E and a (possibly truncated) HMAC using the
  197. //specified hash function H, both applied to the plaintext with a
  198. //random confounder prefix and sufficient padding to bring it to a
  199. //multiple of the message block size. When the HMAC is computed, the
  200. //key is used in the protocol key form.
  201. h := ct[len(ct)-etype.GetHMACBitLength()/8+1:]
  202. expectedMAC, _ := GetChecksum(pt, key, usage, etype)
  203. return hmac.Equal(h, expectedMAC[1:etype.GetHMACBitLength()/8])
  204. }
  205. /*
  206. Key Usage Numbers
  207. RFC 3961: The "well-known constant" used for the DK function is the key usage number, expressed as four octets in big-endian order, followed by one octet indicated below.
  208. Kc = DK(base-key, usage | 0x99);
  209. Ke = DK(base-key, usage | 0xAA);
  210. Ki = DK(base-key, usage | 0x55);
  211. */
  212. // un - usage number
  213. func GetUsageKc(un uint32) []byte {
  214. return getUsage(un, 0x99)
  215. }
  216. // un - usage number
  217. func GetUsageKe(un uint32) []byte {
  218. return getUsage(un, 0xAA)
  219. }
  220. // un - usage number
  221. func GetUsageKi(un uint32) []byte {
  222. return getUsage(un, 0x55)
  223. }
  224. func getUsage(un uint32, o byte) []byte {
  225. var buf bytes.Buffer
  226. binary.Write(&buf, binary.BigEndian, un)
  227. return append(buf.Bytes(), o)
  228. }