engine.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package engine
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. "github.com/jcmturner/gokrb5/crypto/etype"
  9. )
  10. // RFC 3961: DR(Key, Constant) = k-truncate(E(Key, Constant, initial-cipher-state)).
  11. //
  12. // key: base key or protocol key. Likely to be a key from a keytab file.
  13. //
  14. // usage: a constant.
  15. //
  16. // n: block size in bits (not bytes) - note if you use something like aes.BlockSize this is in bytes.
  17. //
  18. // k: key length / key seed length in bits. Eg. for AES256 this value is 256.
  19. //
  20. // e: the encryption etype function to use.
  21. func DeriveRandom(key, usage []byte, n, k int, e etype.EType) ([]byte, error) {
  22. //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.
  23. nFoldUsage := Nfold(usage, n)
  24. //k-truncate implemented by creating a byte array the size of k (k is in bits hence /8)
  25. out := make([]byte, k/8)
  26. /*If the output of E is shorter than k bits, it is fed back into the encryption as many times as necessary.
  27. The construct is as follows (where | indicates concatentation):
  28. K1 = E(Key, n-fold(Constant), initial-cipher-state)
  29. K2 = E(Key, K1, initial-cipher-state)
  30. K3 = E(Key, K2, initial-cipher-state)
  31. K4 = ...
  32. DR(Key, Constant) = k-truncate(K1 | K2 | K3 | K4 ...)*/
  33. _, K, err := e.Encrypt(key, nFoldUsage)
  34. if err != nil {
  35. return out, err
  36. }
  37. for i := copy(out, K); i < len(out); {
  38. _, K, _ = e.Encrypt(key, K)
  39. i = i + copy(out[i:], K)
  40. }
  41. return out, nil
  42. }
  43. // Pad bytes b with zeros to nearest multiple of message size m.
  44. func ZeroPad(b []byte, m int) ([]byte, error) {
  45. if m <= 0 {
  46. return nil, errors.New("Invalid message block size when padding")
  47. }
  48. if b == nil || len(b) == 0 {
  49. return nil, errors.New("Data not valid to pad: Zero size")
  50. }
  51. if l := len(b) % m; l != 0 {
  52. n := m - l
  53. z := make([]byte, n)
  54. b = append(b, z...)
  55. }
  56. return b, nil
  57. }
  58. // Pad bytes b according to RFC 2315 to nearest multiple of message size m.
  59. func PKCS7Pad(b []byte, m int) ([]byte, error) {
  60. if m <= 0 {
  61. return nil, errors.New("Invalid message block size when padding")
  62. }
  63. if b == nil || len(b) == 0 {
  64. return nil, errors.New("Data not valid to pad: Zero size")
  65. }
  66. n := m - (len(b) % m)
  67. pb := make([]byte, len(b)+n)
  68. copy(pb, b)
  69. copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n))
  70. return pb, nil
  71. }
  72. // Remove RFC 2315 padding from byes b where message size is m.
  73. func PKCS7Unpad(b []byte, m int) ([]byte, error) {
  74. if m <= 0 {
  75. return nil, errors.New("Invalid message block size when unpadding")
  76. }
  77. if b == nil || len(b) == 0 {
  78. return nil, errors.New("Padded data not valid: Zero size")
  79. }
  80. if len(b)%m != 0 {
  81. return nil, errors.New("Padded data not valid: Not multiple of message block size")
  82. }
  83. c := b[len(b)-1]
  84. n := int(c)
  85. if n == 0 || n > len(b) {
  86. return nil, errors.New("Padded data not valid: Data may not have been padded")
  87. }
  88. for i := 0; i < n; i++ {
  89. if b[len(b)-n+i] != c {
  90. return nil, errors.New("Padded data not valid")
  91. }
  92. }
  93. return b[:len(b)-n], nil
  94. }
  95. func getHash(pt, key []byte, usage []byte, etype etype.EType) ([]byte, error) {
  96. k, err := etype.DeriveKey(key, usage)
  97. if err != nil {
  98. return nil, fmt.Errorf("Unable to derive key for checksum: %v", err)
  99. }
  100. mac := hmac.New(etype.GetHash, k)
  101. p := make([]byte, len(pt))
  102. copy(p, pt)
  103. mac.Write(p)
  104. return mac.Sum(nil)[:etype.GetHMACBitLength()/8], nil
  105. }
  106. // Get a keyed checksum hash of bytes b.
  107. func GetChecksumHash(b, key []byte, usage uint32, etype etype.EType) ([]byte, error) {
  108. return getHash(b, key, GetUsageKc(usage), etype)
  109. }
  110. // Get a keyed integrity hash of bytes b.
  111. func GetIntegrityHash(b, key []byte, usage uint32, etype etype.EType) ([]byte, error) {
  112. return getHash(b, key, GetUsageKi(usage), etype)
  113. }
  114. // Verify the integrity of cipertext bytes ct.
  115. func VerifyIntegrity(key, ct, pt []byte, usage uint32, etype etype.EType) bool {
  116. //The ciphertext output is the concatenation of the output of the basic
  117. //encryption function E and a (possibly truncated) HMAC using the
  118. //specified hash function H, both applied to the plaintext with a
  119. //random confounder prefix and sufficient padding to bring it to a
  120. //multiple of the message block size. When the HMAC is computed, the
  121. //key is used in the protocol key form.
  122. h := make([]byte, etype.GetHMACBitLength()/8)
  123. copy(h, ct[len(ct)-etype.GetHMACBitLength()/8:])
  124. expectedMAC, _ := GetIntegrityHash(pt, key, usage, etype)
  125. return hmac.Equal(h, expectedMAC)
  126. }
  127. // Verify the checksum of the msg bytes is the same as the checksum provided.
  128. func VerifyChecksum(key, chksum, msg []byte, usage uint32, etype etype.EType) bool {
  129. //The ciphertext output is the concatenation of the output of the basic
  130. //encryption function E and a (possibly truncated) HMAC using the
  131. //specified hash function H, both applied to the plaintext with a
  132. //random confounder prefix and sufficient padding to bring it to a
  133. //multiple of the message block size. When the HMAC is computed, the
  134. //key is used in the protocol key form.
  135. expectedMAC, _ := GetChecksumHash(msg, key, usage, etype)
  136. return hmac.Equal(chksum, expectedMAC)
  137. }
  138. // Get the checksum key usage value for the usage number un.
  139. //
  140. // 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.
  141. //
  142. // Kc = DK(base-key, usage | 0x99);
  143. func GetUsageKc(un uint32) []byte {
  144. return getUsage(un, 0x99)
  145. }
  146. // Get the encryption key usage value for the usage number un
  147. //
  148. // 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.
  149. //
  150. // Ke = DK(base-key, usage | 0xAA);
  151. func GetUsageKe(un uint32) []byte {
  152. return getUsage(un, 0xAA)
  153. }
  154. // Get the integrity key usage value for the usage number un
  155. //
  156. // 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.
  157. //
  158. // Ki = DK(base-key, usage | 0x55);
  159. func GetUsageKi(un uint32) []byte {
  160. return getUsage(un, 0x55)
  161. }
  162. func getUsage(un uint32, o byte) []byte {
  163. var buf bytes.Buffer
  164. binary.Write(&buf, binary.BigEndian, un)
  165. return append(buf.Bytes(), o)
  166. }