| 1 | go-tpm-keyfile |
| 2 | ============== |
| 3 | |
| 4 | Implements the ASN.1 Specification for TPM 2.0 Key Files. |
| 5 | |
| 6 | https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html |
| 7 | |
| 8 | |
| 9 | # Loadable Keys |
| 10 | |
| 11 | ## With NewLoadableKey |
| 12 | |
| 13 | ```go |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "os" |
| 18 | |
| 19 | keyfile "github.com/foxboron/go-tpm-keyfiles" |
| 20 | |
| 21 | "github.com/google/go-tpm/tpm2" |
| 22 | "github.com/google/go-tpm/tpm2/transport/simulator" |
| 23 | ) |
| 24 | |
| 25 | func main() { |
| 26 | tpm, _ := simulator.OpenSimulator() |
| 27 | defer tpm.Close() |
| 28 | k, _ := keyfile.NewLoadableKey(tpm, tpm2.TPMAlgECC, 256, []byte{}, |
| 29 | keyfile.WithDescription("TPM Key"), |
| 30 | ) |
| 31 | os.Writefile("key.pem", k.Bytes(), 0640) |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | ## With NewTPMKey |
| 36 | |
| 37 | ```go |
| 38 | package main |
| 39 | |
| 40 | import ( |
| 41 | "os" |
| 42 | |
| 43 | keyfile "github.com/foxboron/go-tpm-keyfiles" |
| 44 | |
| 45 | "github.com/google/go-tpm/tpm2" |
| 46 | "github.com/google/go-tpm/tpm2/transport/simulator" |
| 47 | ) |
| 48 | |
| 49 | func main(){ |
| 50 | tpm, _ := simulator.OpenSimulator() |
| 51 | defer tpm.Close() |
| 52 | |
| 53 | primary, _ := tpm2.CreatePrimary{ |
| 54 | PrimaryHandle: tpm2.TPMRHOwner, |
| 55 | InPublic: tpm2.New2B(tpm2.ECCSRKTemplate), |
| 56 | }.Execute(tpm) |
| 57 | |
| 58 | eccTemplate := tpm2.TPMTPublic{ |
| 59 | Type: tpm2.TPMAlgECC, |
| 60 | NameAlg: sha, |
| 61 | ObjectAttributes: tpm2.TPMAObject{ |
| 62 | SignEncrypt: true, |
| 63 | FixedTPM: true, |
| 64 | FixedParent: true, |
| 65 | SensitiveDataOrigin: true, |
| 66 | UserWithAuth: true, |
| 67 | }, |
| 68 | Parameters: tpm2.NewTPMUPublicParms( |
| 69 | tpm2.TPMAlgECC, |
| 70 | &tpm2.TPMSECCParms{ |
| 71 | CurveID: ecc, |
| 72 | Scheme: tpm2.TPMTECCScheme{ |
| 73 | Scheme: tpm2.TPMAlgNull, |
| 74 | }, |
| 75 | }, |
| 76 | ), |
| 77 | } |
| 78 | |
| 79 | eccKeyResponse, := tpm2.CreateLoaded{ |
| 80 | ParentHandle: tpm2.AuthHandle{ |
| 81 | Handle: primary.ObjectHandle, |
| 82 | Name: primary.Name, |
| 83 | Auth: tpm2.PasswordAuth([]byte(nil)), |
| 84 | }, |
| 85 | InPublic: tpm2.New2BTemplate(&eccTemplate), |
| 86 | }.Execute(tpm) |
| 87 | |
| 88 | k := keyfile.NewTPMKey( |
| 89 | keyfile.OIDLoadableKey |
| 90 | eccKeyResponse.OutPublic, |
| 91 | eccKeyResponse.OutPrivate, |
| 92 | keyfile.WithDescription("This is a TPM Key"), |
| 93 | ) |
| 94 | |
| 95 | os.Writefile("key.pem", k.Bytes(), 0640) |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | # Importable Key |
| 100 | |
| 101 | ```go |
| 102 | package main |
| 103 | |
| 104 | import ( |
| 105 | "os" |
| 106 | |
| 107 | keyfile "github.com/foxboron/go-tpm-keyfiles" |
| 108 | |
| 109 | "github.com/google/go-tpm/tpm2" |
| 110 | "github.com/google/go-tpm/tpm2/transport/simulator" |
| 111 | ) |
| 112 | |
| 113 | var ECCSRK_H2_Template = tpm2.TPMTPublic{ |
| 114 | Type: tpm2.TPMAlgECC, |
| 115 | NameAlg: tpm2.TPMAlgSHA256, |
| 116 | ObjectAttributes: tpm2.TPMAObject{ |
| 117 | FixedTPM: true, |
| 118 | FixedParent: true, |
| 119 | SensitiveDataOrigin: true, |
| 120 | UserWithAuth: true, |
| 121 | NoDA: true, |
| 122 | Restricted: true, |
| 123 | Decrypt: true, |
| 124 | }, |
| 125 | Parameters: tpm2.NewTPMUPublicParms( |
| 126 | tpm2.TPMAlgECC, |
| 127 | &tpm2.TPMSECCParms{ |
| 128 | Symmetric: tpm2.TPMTSymDefObject{ |
| 129 | Algorithm: tpm2.TPMAlgAES, |
| 130 | KeyBits: tpm2.NewTPMUSymKeyBits( |
| 131 | tpm2.TPMAlgAES, |
| 132 | tpm2.TPMKeyBits(128), |
| 133 | ), |
| 134 | Mode: tpm2.NewTPMUSymMode( |
| 135 | tpm2.TPMAlgAES, |
| 136 | tpm2.TPMAlgCFB, |
| 137 | ), |
| 138 | }, |
| 139 | CurveID: tpm2.TPMECCNistP256, |
| 140 | }, |
| 141 | ), |
| 142 | Unique: tpm2.NewTPMUPublicID( |
| 143 | tpm2.TPMAlgECC, |
| 144 | &tpm2.TPMSECCPoint{ |
| 145 | X: tpm2.TPM2BECCParameter{ |
| 146 | Buffer: make([]byte, 0), |
| 147 | }, |
| 148 | Y: tpm2.TPM2BECCParameter{ |
| 149 | Buffer: make([]byte, 0), |
| 150 | }, |
| 151 | }, |
| 152 | ), |
| 153 | } |
| 154 | ) |
| 155 | |
| 156 | func main() { |
| 157 | tpm, _ := simulator.OpenSimulator() |
| 158 | defer tpm.Close() |
| 159 | |
| 160 | pk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 161 | |
| 162 | srk := tpm2.CreatePrimary{ |
| 163 | PrimaryHandle: tpm2.AuthHandle{ |
| 164 | Handle: hier, |
| 165 | Auth: tpm2.PasswordAuth(ownerAuth), |
| 166 | }, |
| 167 | InSensitive: tpm2.TPM2BSensitiveCreate{ |
| 168 | Sensitive: &tpm2.TPMSSensitiveCreate{ |
| 169 | UserAuth: tpm2.TPM2BAuth{ |
| 170 | Buffer: []byte(nil), |
| 171 | }, |
| 172 | }, |
| 173 | }, |
| 174 | InPublic: tpm2.New2B(ECCSRK_H2_Template), |
| 175 | } |
| 176 | |
| 177 | var rsp *tpm2.CreatePrimaryResponse |
| 178 | rsp, _ := srk.Execute(tpm) |
| 179 | srkPublic, _ := rsp.OutPublic.Contents() |
| 180 | |
| 181 | key, _ := keyfile.NewImportablekey(srkpub, ecc) |
| 182 | |
| 183 | os.Writefile("key.pem", k.Bytes(), 0640) |
| 184 | |
| 185 | // To import the key |
| 186 | k, _ := keyfile.ImportTPMKey(tpm, key, []byte(nil)) |
| 187 | } |
| 188 | ``` |
| 189 | |
| 190 | # Sealed Data |
| 191 | |
| 192 | ```go |
| 193 | package main |
| 194 | |
| 195 | import ( |
| 196 | "os" |
| 197 | |
| 198 | keyfile "github.com/foxboron/go-tpm-keyfiles" |
| 199 | |
| 200 | "github.com/google/go-tpm/tpm2" |
| 201 | "github.com/google/go-tpm/tpm2/transport/simulator" |
| 202 | ) |
| 203 | |
| 204 | func main() { |
| 205 | tpm, _ := simulator.OpenSimulator() |
| 206 | defer tpm.Close() |
| 207 | |
| 208 | msg := []byte("message") |
| 209 | |
| 210 | k, _ := keyfile.NewSealedData(tpm, msg, []byte(nil)) |
| 211 | |
| 212 | data, _ := keyfile.UnsealData(tpm, k, []byte(nil)) |
| 213 | |
| 214 | if bytes.Equal(data, msg) { |
| 215 | fmt.Println("same message") |
| 216 | } |
| 217 | } |
| 218 | ``` |
| 219 | |
| 220 | # TPMSigner |
| 221 | |
| 222 | `go-tpm-keyfile` implements a `crypto.Signer` interface to be used with the keys |
| 223 | for easy signature creation and verification. |
| 224 | |
| 225 | The `TPMKeySigner` struct implements a callback-style approach for user auth, |
| 226 | owner auth and for TPM fetching for easier implementation towards things that |
| 227 | require user-input. |
| 228 | |
| 229 | ```go |
| 230 | package main |
| 231 | |
| 232 | import ( |
| 233 | "crypto" |
| 234 | "os" |
| 235 | |
| 236 | keyfile "github.com/foxboron/go-tpm-keyfiles" |
| 237 | |
| 238 | "github.com/google/go-tpm/tpm2" |
| 239 | "github.com/google/go-tpm/tpm2/transport/simulator" |
| 240 | ) |
| 241 | |
| 242 | func main() { |
| 243 | tpm, _ := simulator.OpenSimulator() |
| 244 | defer tpm.Close() |
| 245 | |
| 246 | k, _ := NewLoadableKey(tpm, tpm2.TPMAlgECC, 256, []byte("")) |
| 247 | |
| 248 | signer, _ := k.Signer(tpm, []byte(""), []byte("")) |
| 249 | |
| 250 | h := crypto.SHA256.New() |
| 251 | h.Write([]byte("message")) |
| 252 | b := h.Sum(nil) |
| 253 | |
| 254 | sig, _ := signer.Sign((io.Reader)(nil), b[:], crypto.SHA256) |
| 255 | |
| 256 | ok, err := k.Verify(crypto.SHA256, b[:], sig) |
| 257 | if !ok || err != nil { |
| 258 | log.Fatalf("invalid signature") |
| 259 | } |
| 260 | } |
| 261 | ``` |