Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame^] | 1 | package rfc4757 |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/hex" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | |
| 10 | "golang.org/x/crypto/md4" |
| 11 | ) |
| 12 | |
| 13 | // StringToKey returns a key derived from the string provided according to the definition in RFC 4757. |
| 14 | func StringToKey(secret string) ([]byte, error) { |
| 15 | b := make([]byte, len(secret)*2, len(secret)*2) |
| 16 | for i, r := range secret { |
| 17 | u := fmt.Sprintf("%04x", r) |
| 18 | c, err := hex.DecodeString(u) |
| 19 | if err != nil { |
| 20 | return []byte{}, errors.New("character could not be encoded") |
| 21 | } |
| 22 | // Swap round the two bytes to make little endian as we put into byte slice |
| 23 | b[2*i] = c[1] |
| 24 | b[2*i+1] = c[0] |
| 25 | } |
| 26 | r := bytes.NewReader(b) |
| 27 | h := md4.New() |
| 28 | _, err := io.Copy(h, r) |
| 29 | if err != nil { |
| 30 | return []byte{}, err |
| 31 | } |
| 32 | return h.Sum(nil), nil |
| 33 | } |
| 34 | |
| 35 | func deriveKeys(key, checksum []byte, usage uint32, export bool) (k1, k2, k3 []byte) { |
| 36 | //if export { |
| 37 | // L40 := make([]byte, 14, 14) |
| 38 | // copy(L40, []byte(`fortybits`)) |
| 39 | // k1 = HMAC(key, L40) |
| 40 | //} else { |
| 41 | // tb := MessageTypeBytes(usage) |
| 42 | // k1 = HMAC(key, tb) |
| 43 | //} |
| 44 | //k2 = k1[:16] |
| 45 | //if export { |
| 46 | // mask := []byte{0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB} |
| 47 | // copy(k1[7:16], mask) |
| 48 | //} |
| 49 | //k3 = HMAC(k1, checksum) |
| 50 | //return |
| 51 | k1 = key |
| 52 | k2 = HMAC(k1, UsageToMSMsgType(usage)) |
| 53 | k3 = HMAC(k2, checksum) |
| 54 | return |
| 55 | } |