Debian Codegraph @

legacy/tpm2/kdf.go

raw
1 // Copyright (c) 2018, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tpm2
16
17 import (
18 "crypto"
19 "crypto/hmac"
20 "encoding/binary"
21 "hash"
22 )
23
24 // KDFa implements TPM 2.0's default key derivation function, as defined in
25 // section 11.4.9.2 of the TPM revision 2 specification part 1.
26 // See: https://trustedcomputinggroup.org/resource/tpm-library-specification/
27 // The key & label parameters must not be zero length.
28 // The label parameter is a non-null-terminated string.
29 // The contextU & contextV parameters are optional.
30 // Deprecated: Use KDFaHash.
31 func KDFa(hashAlg Algorithm, key []byte, label string, contextU, contextV []byte, bits int) ([]byte, error) {
32 h, err := hashAlg.Hash()
33 if err != nil {
34 return nil, err
35 }
36 return KDFaHash(h, key, label, contextU, contextV, bits), nil
37 }
38
39 // KDFe implements TPM 2.0's ECDH key derivation function, as defined in
40 // section 11.4.9.3 of the TPM revision 2 specification part 1.
41 // See: https://trustedcomputinggroup.org/resource/tpm-library-specification/
42 // The z parameter is the x coordinate of one party's private ECC key multiplied
43 // by the other party's public ECC point.
44 // The use parameter is a non-null-terminated string.
45 // The partyUInfo and partyVInfo are the x coordinates of the initiator's and
46 // Deprecated: Use KDFeHash.
47 func KDFe(hashAlg Algorithm, z []byte, use string, partyUInfo, partyVInfo []byte, bits int) ([]byte, error) {
48 h, err := hashAlg.Hash()
49 if err != nil {
50 return nil, err
51 }
52 return KDFeHash(h, z, use, partyUInfo, partyVInfo, bits), nil
53 }
54
55 // KDFaHash implements TPM 2.0's default key derivation function, as defined in
56 // section 11.4.9.2 of the TPM revision 2 specification part 1.
57 // See: https://trustedcomputinggroup.org/resource/tpm-library-specification/
58 // The key & label parameters must not be zero length.
59 // The label parameter is a non-null-terminated string.
60 // The contextU & contextV parameters are optional.
61 func KDFaHash(h crypto.Hash, key []byte, label string, contextU, contextV []byte, bits int) []byte {
62 mac := hmac.New(h.New, key)
63
64 out := kdf(mac, bits, func() {
65 mac.Write([]byte(label))
66 mac.Write([]byte{0}) // Terminating null character for C-string.
67 mac.Write(contextU)
68 mac.Write(contextV)
69 binary.Write(mac, binary.BigEndian, uint32(bits))
70 })
71 return out
72 }
73
74 // KDFeHash implements TPM 2.0's ECDH key derivation function, as defined in
75 // section 11.4.9.3 of the TPM revision 2 specification part 1.
76 // See: https://trustedcomputinggroup.org/resource/tpm-library-specification/
77 // The z parameter is the x coordinate of one party's private ECC key multiplied
78 // by the other party's public ECC point.
79 // The use parameter is a non-null-terminated string.
80 // The partyUInfo and partyVInfo are the x coordinates of the initiator's and
81 // the responder's ECC points, respectively.
82 func KDFeHash(h crypto.Hash, z []byte, use string, partyUInfo, partyVInfo []byte, bits int) []byte {
83 hash := h.New()
84
85 out := kdf(hash, bits, func() {
86 hash.Write(z)
87 hash.Write([]byte(use))
88 hash.Write([]byte{0}) // Terminating null character for C-string.
89 hash.Write(partyUInfo)
90 hash.Write(partyVInfo)
91 })
92 return out
93 }
94
95 func kdf(h hash.Hash, bits int, update func()) []byte {
96 bytes := (bits + 7) / 8
97 out := []byte{}
98
99 for counter := 1; len(out) < bytes; counter++ {
100 h.Reset()
101 binary.Write(h, binary.BigEndian, uint32(counter))
102 update()
103
104 out = h.Sum(out)
105 }
106 // out's length is a multiple of hash size, so there will be excess
107 // bytes if bytes isn't a multiple of hash size.
108 out = out[:bytes]
109
110 // As mentioned in the KDFa and KDFe specs mentioned above,
111 // the unused bits of the most significant octet are masked off.
112 if maskBits := uint8(bits % 8); maskBits > 0 {
113 out[0] &= (1 << maskBits) - 1
114 }
115 return out
116 }