| 1 |
package rfc8009 |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/hmac" |
| 5 |
"encoding/binary" |
| 6 |
"encoding/hex" |
| 7 |
"errors" |
| 8 |
|
| 9 |
"golang.org/x/crypto/pbkdf2" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v7/crypto/etype" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v7/iana/etypeID" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
s2kParamsZero = 32768 |
| 16 |
) |
| 17 |
|
| 18 |
// DeriveRandom for key derivation as defined in RFC 8009 |
| 19 |
func DeriveRandom(protocolKey, usage []byte, e etype.EType) ([]byte, error) { |
| 20 |
h := e.GetHashFunc()() |
| 21 |
return KDF_HMAC_SHA2(protocolKey, []byte("prf"), usage, h.Size(), e), nil |
| 22 |
} |
| 23 |
|
| 24 |
// DeriveKey derives a key from the protocol key based on the usage and the etype's specific methods. |
| 25 |
// |
| 26 |
// https://tools.ietf.org/html/rfc8009#section-5 |
| 27 |
func DeriveKey(protocolKey, label []byte, e etype.EType) []byte { |
| 28 |
var context []byte |
| 29 |
var kl int |
| 30 |
// Key length is longer for aes256-cts-hmac-sha384-192 is it is a Ke or from StringToKey (where label is "kerberos") |
| 31 |
if e.GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { |
| 32 |
switch label[len(label)-1] { |
| 33 |
case 0x73: |
| 34 |
// 0x73 is "s" so label could be kerberos meaning StringToKey so now check if the label is "kerberos" |
| 35 |
kerblabel := []byte("kerberos") |
| 36 |
if len(label) != len(kerblabel) { |
| 37 |
break |
| 38 |
} |
| 39 |
for i, b := range label { |
| 40 |
if b != kerblabel[i] { |
| 41 |
kl = e.GetKeySeedBitLength() |
| 42 |
break |
| 43 |
} |
| 44 |
} |
| 45 |
if kl == 0 { |
| 46 |
// This is StringToKey |
| 47 |
kl = 256 |
| 48 |
} |
| 49 |
case 0xAA: |
| 50 |
// This is a Ke |
| 51 |
kl = 256 |
| 52 |
} |
| 53 |
} |
| 54 |
if kl == 0 { |
| 55 |
kl = e.GetKeySeedBitLength() |
| 56 |
} |
| 57 |
return e.RandomToKey(KDF_HMAC_SHA2(protocolKey, label, context, kl, e)) |
| 58 |
} |
| 59 |
|
| 60 |
// RandomToKey returns a key from the bytes provided according to the definition in RFC 8009. |
| 61 |
func RandomToKey(b []byte) []byte { |
| 62 |
return b |
| 63 |
} |
| 64 |
|
| 65 |
// StringToKey returns a key derived from the string provided according to the definition in RFC 8009. |
| 66 |
func StringToKey(secret, salt, s2kparams string, e etype.EType) ([]byte, error) { |
| 67 |
i, err := S2KparamsToItertions(s2kparams) |
| 68 |
if err != nil { |
| 69 |
return nil, err |
| 70 |
} |
| 71 |
return StringToKeyIter(secret, salt, i, e) |
| 72 |
} |
| 73 |
|
| 74 |
// StringToKeyIter returns a key derived from the string provided according to the definition in RFC 8009. |
| 75 |
func StringToKeyIter(secret, salt string, iterations int, e etype.EType) ([]byte, error) { |
| 76 |
tkey := e.RandomToKey(StringToPBKDF2(secret, salt, iterations, e)) |
| 77 |
return e.DeriveKey(tkey, []byte("kerberos")) |
| 78 |
} |
| 79 |
|
| 80 |
// StringToPBKDF2 generates an encryption key from a pass phrase and salt string using the PBKDF2 function from PKCS #5 v2.0 |
| 81 |
func StringToPBKDF2(secret, salt string, iterations int, e etype.EType) []byte { |
| 82 |
kl := e.GetKeyByteSize() |
| 83 |
if e.GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { |
| 84 |
kl = 32 |
| 85 |
} |
| 86 |
return pbkdf2.Key([]byte(secret), []byte(salt), iterations, kl, e.GetHashFunc()) |
| 87 |
} |
| 88 |
|
| 89 |
// KDF_HMAC_SHA2 key derivation: https://tools.ietf.org/html/rfc8009#section-3 |
| 90 |
func KDF_HMAC_SHA2(protocolKey, label, context []byte, kl int, e etype.EType) []byte { |
| 91 |
//k: Length in bits of the key to be outputted, expressed in big-endian binary representation in 4 bytes. |
| 92 |
k := make([]byte, 4, 4) |
| 93 |
binary.BigEndian.PutUint32(k, uint32(kl)) |
| 94 |
|
| 95 |
c := make([]byte, 4, 4) |
| 96 |
binary.BigEndian.PutUint32(c, uint32(1)) |
| 97 |
c = append(c, label...) |
| 98 |
c = append(c, byte(0)) |
| 99 |
if len(context) > 0 { |
| 100 |
c = append(c, context...) |
| 101 |
} |
| 102 |
c = append(c, k...) |
| 103 |
|
| 104 |
mac := hmac.New(e.GetHashFunc(), protocolKey) |
| 105 |
mac.Write(c) |
| 106 |
return mac.Sum(nil)[:(kl / 8)] |
| 107 |
} |
| 108 |
|
| 109 |
// GetSaltP returns the salt value based on the etype name: https://tools.ietf.org/html/rfc8009#section-4 |
| 110 |
func GetSaltP(salt, ename string) string { |
| 111 |
b := []byte(ename) |
| 112 |
b = append(b, byte(0)) |
| 113 |
b = append(b, []byte(salt)...) |
| 114 |
return string(b) |
| 115 |
} |
| 116 |
|
| 117 |
// S2KparamsToItertions converts the string representation of iterations to an integer for RFC 8009. |
| 118 |
func S2KparamsToItertions(s2kparams string) (int, error) { |
| 119 |
var i uint32 |
| 120 |
if len(s2kparams) != 8 { |
| 121 |
return s2kParamsZero, errors.New("Invalid s2kparams length") |
| 122 |
} |
| 123 |
b, err := hex.DecodeString(s2kparams) |
| 124 |
if err != nil { |
| 125 |
return s2kParamsZero, errors.New("Invalid s2kparams, cannot decode string to bytes") |
| 126 |
} |
| 127 |
i = binary.BigEndian.Uint32(b) |
| 128 |
//buf := bytes.NewBuffer(b) |
| 129 |
//err = binary.Read(buf, binary.BigEndian, &i) |
| 130 |
if err != nil { |
| 131 |
return s2kParamsZero, errors.New("Invalid s2kparams, cannot convert to big endian int32") |
| 132 |
} |
| 133 |
return int(i), nil |
| 134 |
} |