| 1 | package keyfilescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.22.0 bytes/" |
| 5 | "cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/" |
| 6 | "crypto/aesscip-go gomod github.com/golang/go/src go1.22.0 `crypto/aes`/" |
| 7 | "crypto/cipherscip-go gomod github.com/golang/go/src go1.22.0 `crypto/cipher`/" |
| 8 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/" |
| 9 | "crypto/ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/" |
| 10 | "crypto/hmacscip-go gomod github.com/golang/go/src go1.22.0 `crypto/hmac`/" |
| 11 | "crypto/randscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rand`/" |
| 12 | "crypto/rsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/rsa`/" |
| 13 | "fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/" |
| 14 | "hashscip-go gomod github.com/golang/go/src go1.22.0 hash/" |
| 15 | "math/bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/" |
| 16 | |
| 17 | "github.com/foxboron/go-tpm-keyfiles/templatescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles/template`/" |
| 18 | "github.com/google/go-tpm/tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/" |
| 19 | "github.com/google/go-tpm/tpm2/transportscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2/transport`/" |
| 20 | ) |
| 21 | |
| 22 | // ECC coordinates need to maintain a specific size based on the curve, so we pad the front with zeros. |
| 23 | // This is particularly an issue for NIST-P521 coordinates, as they are frequently missing their first byte. |
| 24 | func eccIntToBytesscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/eccIntToBytes().(curve ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/.Curvescip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/Curve#, i *bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#) []byte { |
| 25 | bytes := i.Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().() |
| 26 | curveBytes := (curve.Paramsscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/Curve#Params().().BitSizescip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/CurveParams#BitSize. + 7) / 8 |
| 27 | return append(make([]byte, curveBytes-len(bytes)), bytes...) |
| 28 | } |
| 29 | |
| 30 | func createECCSeedscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/createECCSeed().(pub *tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#) (seed, encryptedSeed []byte, err error) { |
| 31 | curve := ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/.P256scip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/P256().() |
| 32 | |
| 33 | // We need access to the values so we don't use ecdh to generate the key |
| 34 | priv, x, y, err := ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/GenerateKey().(curve, randscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rand`/Reader.) |
| 35 | if err != nil { |
| 36 | return nil, nil, err |
| 37 | } |
| 38 | privKey := &ecdsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#{ |
| 39 | PublicKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#PublicKey.: ecdsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#{ |
| 40 | Curvescip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#Curve.: ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/.P256scip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/P256().(), |
| 41 | Xscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#X.: new(bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#SetBytes().(x.Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().()), |
| 42 | Yscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#Y.: new(bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#SetBytes().(y.Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().()), |
| 43 | }, |
| 44 | Dscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#D.: new(bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#SetBytes().(priv), |
| 45 | } |
| 46 | privKeyECDH, err := privKey.ECDHscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#ECDH().() |
| 47 | if err != nil { |
| 48 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("failed creating ecdh key: %v", err) |
| 49 | } |
| 50 | |
| 51 | ecc, err := pub.Uniquescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#Unique..ECCscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMUPublicID#ECC().() |
| 52 | if err != nil { |
| 53 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("failed getting ECC values from public: %v", err) |
| 54 | } |
| 55 | |
| 56 | if len(ecc.Xscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#X..Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.) == 0 || len(ecc.Yscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#Y..Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.) == 0 { |
| 57 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("public TPM2TPublic does not have a valid ECC public key") |
| 58 | } |
| 59 | |
| 60 | pubKey := &ecdsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#{ |
| 61 | Curvescip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#Curve.: ellipticscip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/.P256scip-go gomod github.com/golang/go/src go1.22.0 `crypto/elliptic`/P256().(), |
| 62 | Xscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#X.: new(bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#SetBytes().(ecc.Xscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#X..Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.), |
| 63 | Yscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#Y.: new(bigscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#SetBytes().(ecc.Yscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#Y..Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.), |
| 64 | } |
| 65 | |
| 66 | pubkeyECDH, err := pubKey.ECDHscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#ECDH().() |
| 67 | if err != nil { |
| 68 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("failed getting ECDH from produced public key: %v", err) |
| 69 | } |
| 70 | |
| 71 | z, _ := privKeyECDH.ECDHscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdh`/PrivateKey#ECDH().(pubkeyECDH) |
| 72 | xBytes := eccIntToBytesscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/eccIntToBytes().(curve, x) |
| 73 | seed = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.KDFescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/KDFe().( |
| 74 | cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256., |
| 75 | z, |
| 76 | "DUPLICATE", |
| 77 | xBytes, |
| 78 | eccIntToBytesscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/eccIntToBytes().(curve, pubKey.Xscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PublicKey#X.), |
| 79 | cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256..Sizescip-go gomod github.com/golang/go/src go1.22.0 crypto/Hash#Size().()*8) |
| 80 | |
| 81 | encryptedSeed = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.Marshalscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Marshal().(tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMSECCPointscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#{ |
| 82 | Xscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#X.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BECCParameterscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BECCParameter#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: x.FillBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#FillBytes().(make([]byte, len(x.Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().())))}, |
| 83 | Yscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMSECCPoint#Y.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BECCParameterscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BECCParameter#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: y.FillBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#FillBytes().(make([]byte, len(y.Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().())))}, |
| 84 | }) |
| 85 | |
| 86 | return seed, encryptedSeed, err |
| 87 | } |
| 88 | |
| 89 | func createWrapscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/createWrap().(pub *tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#, pk any, userauth []byte) (tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#, []byte, []byte, error) { |
| 90 | var err error |
| 91 | var seed []byte |
| 92 | var encryptedSeed []byte |
| 93 | |
| 94 | switch pub.Typescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#Type. { |
| 95 | case tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgECCscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgECC.: |
| 96 | seed, encryptedSeed, err = createECCSeedscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/createECCSeed().(pub) |
| 97 | if err != nil { |
| 98 | return tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#{}, nil, nil, err |
| 99 | } |
| 100 | default: |
| 101 | return tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#{}, nil, nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("only support ECC parents for import wrapping: %v", pub.Typescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#Type.) |
| 102 | } |
| 103 | |
| 104 | var public *tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic# |
| 105 | var sensitive tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTSensitivescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive# |
| 106 | |
| 107 | switch pk := pk.(type) { |
| 108 | case ecdsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#: |
| 109 | public = templatescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles/template`/.EcdsaToTPMTPublicscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles/template`/EcdsaToTPMTPublic().(&pk.PublicKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#PublicKey., tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgSHA256scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgSHA256.) |
| 110 | sensitive = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTSensitivescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#{ |
| 111 | SensitiveTypescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#SensitiveType.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgECCscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgECC., |
| 112 | Sensitivescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#Sensitive.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.NewTPMUSensitiveCompositescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/NewTPMUSensitiveComposite().( |
| 113 | tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgECCscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgECC., |
| 114 | &tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BECCParameterscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BECCParameter#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: pk.Dscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#D..FillBytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#FillBytes().(make([]byte, len(pk.Dscip-go gomod github.com/golang/go/src go1.22.0 `crypto/ecdsa`/PrivateKey#D..Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().())))}, |
| 115 | ), |
| 116 | } |
| 117 | case rsascip-go gomod github.com/golang/go/src go1.22.0 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rsa`/PrivateKey#: |
| 118 | // TODO: We only really support 2048 bits |
| 119 | public = templatescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles/template`/.RSAToTPMTPublicscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles/template`/RSAToTPMTPublic().(&pk.PublicKeyscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rsa`/PrivateKey#PublicKey., 2048) |
| 120 | sensitive = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTSensitivescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#{ |
| 121 | SensitiveTypescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#SensitiveType.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgRSAscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgRSA., |
| 122 | Sensitivescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#Sensitive.: tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.NewTPMUSensitiveCompositescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/NewTPMUSensitiveComposite().( |
| 123 | tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMAlgRSAscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMAlgRSA., |
| 124 | &tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPrivateKeyRSAscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPrivateKeyRSA#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: pk.Primesscip-go gomod github.com/golang/go/src go1.22.0 `crypto/rsa`/PrivateKey#Primes.[0].Bytesscip-go gomod github.com/golang/go/src go1.22.0 `math/big`/Int#Bytes().()}, |
| 125 | ), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Add user auth |
| 130 | if !bytesscip-go gomod github.com/golang/go/src go1.22.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.22.0 bytes/Equal().(userauth, []byte("")) { |
| 131 | sensitive.AuthValuescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTSensitive#AuthValue. = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BAuthscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#{ |
| 132 | Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: userauth, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | sens2B := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.Marshalscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Marshal().(sensitive) |
| 137 | sens2B = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.Marshalscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Marshal().(tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPrivatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPrivate#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: sens2B}) |
| 138 | |
| 139 | b2name, err := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.ObjectNamescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/ObjectName().(public) |
| 140 | if err != nil { |
| 141 | return tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#{}, nil, nil, err |
| 142 | } |
| 143 | |
| 144 | // AES symm encryption key |
| 145 | symmetricKey := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.KDFascip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/KDFa().( |
| 146 | cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256., |
| 147 | seed, |
| 148 | "STORAGE", |
| 149 | b2name.Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer., |
| 150 | /*contextV=*/ nil, |
| 151 | 128) |
| 152 | |
| 153 | c, err := aesscip-go gomod github.com/golang/go/src go1.22.0 `crypto/aes`/.NewCipherscip-go gomod github.com/golang/go/src go1.22.0 `crypto/aes`/NewCipher().(symmetricKey) |
| 154 | if err != nil { |
| 155 | return tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#{}, nil, nil, err |
| 156 | } |
| 157 | encryptedSecret := make([]byte, len(sens2B)) |
| 158 | // The TPM spec requires an all-zero IV. |
| 159 | iv := make([]byte, len(symmetricKey)) |
| 160 | cipherscip-go gomod github.com/golang/go/src go1.22.0 `crypto/cipher`/.NewCFBEncrypterscip-go gomod github.com/golang/go/src go1.22.0 `crypto/cipher`/NewCFBEncrypter().(c, iv).XORKeyStreamscip-go gomod github.com/golang/go/src go1.22.0 `crypto/cipher`/Stream#XORKeyStream().(encryptedSecret, sens2B) |
| 161 | |
| 162 | macKey := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.KDFascip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/KDFa().( |
| 163 | cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256., |
| 164 | seed, |
| 165 | "INTEGRITY", |
| 166 | /*contextU=*/ nil, |
| 167 | /*contextV=*/ nil, |
| 168 | cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256..Sizescip-go gomod github.com/golang/go/src go1.22.0 crypto/Hash#Size().()*8) |
| 169 | |
| 170 | mac := hmacscip-go gomod github.com/golang/go/src go1.22.0 `crypto/hmac`/.Newscip-go gomod github.com/golang/go/src go1.22.0 `crypto/hmac`/New().(func() hashscip-go gomod github.com/golang/go/src go1.22.0 hash/.Hashscip-go gomod github.com/golang/go/src go1.22.0 hash/Hash# { return cryptoscip-go gomod github.com/golang/go/src go1.22.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.22.0 crypto/SHA256..Newscip-go gomod github.com/golang/go/src go1.22.0 crypto/Hash#New().() }, macKey) |
| 171 | mac.Writescip-go gomod github.com/golang/go/src go1.22.0 io/Writer#Write().(encryptedSecret) |
| 172 | mac.Writescip-go gomod github.com/golang/go/src go1.22.0 io/Writer#Write().(b2name.Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.) |
| 173 | |
| 174 | hmacSum := mac.Sumscip-go gomod github.com/golang/go/src go1.22.0 hash/Hash#Sum().(nil) |
| 175 | |
| 176 | // The duplicate structure is a sized TPM2BPrivate for the Digest |
| 177 | // and a encrypted secret which is read based off on the size |
| 178 | // of the encrypted metadata. |
| 179 | dup := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.Marshalscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Marshal().(tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPrivatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPrivate#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: hmacSum}) |
| 180 | dup = append(dup, encryptedSecret...) |
| 181 | |
| 182 | return tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.New2Bscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/New2B().(*public), dup, encryptedSeed, nil |
| 183 | } |
| 184 | |
| 185 | func NewImportablekeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/NewImportablekey().(rempub *tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPMTPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPMTPublic#, pk any, fn ...TPMKeyOptionscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKeyOption#) (*TPMKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#, error) { |
| 186 | key := NewTPMKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/NewTPMKey().(OIDImportableKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/OIDImportableKey., tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPublic#{}, tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPrivatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPrivate#{}, fn...) |
| 187 | pub, dup, encSeed, err := createWrapscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/createWrap().(rempub, pk, key.userAuthscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#userAuth.) |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | key.AddOptionsscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#AddOptions().( |
| 192 | WithPubkeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/WithPubkey().(pub), |
| 193 | WithPrivkeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/WithPrivkey().(tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BPrivatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BPrivate#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: dup}), |
| 194 | WithSecretscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/WithSecret().(tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BEncryptedSecretscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BEncryptedSecret#{Bufferscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BAuth#Buffer.: encSeed}), |
| 195 | ) |
| 196 | return key, nil |
| 197 | } |
| 198 | |
| 199 | // Returns a loadable key |
| 200 | func ImportTPMKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/ImportTPMKey().(tpm transportscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2/transport`/.TPMCloserscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2/transport`/TPMCloser#, key *TPMKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#, ownerauth []byte) (*TPMKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#, error) { |
| 201 | var sess TPMSessionscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMSession# |
| 202 | if !key.Keytypescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Keytype..Equalscip-go gomod github.com/golang/go/src go1.22.0 `encoding/asn1`/ObjectIdentifier#Equal().(OIDImportableKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/OIDImportableKey.) && !key.Keytypescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Keytype..Equalscip-go gomod github.com/golang/go/src go1.22.0 `encoding/asn1`/ObjectIdentifier#Equal().(OIDSealedKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/OIDSealedKey.) { |
| 203 | return nil, fmtscip-go gomod github.com/golang/go/src go1.22.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.22.0 fmt/Errorf().("need importable key OID") |
| 204 | } |
| 205 | |
| 206 | sess.SetTPMscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMSession#SetTPM().(tpm) |
| 207 | parenthandle, err := GetParentHandlescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/GetParentHandle().(&sess, key.Parentscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Parent., ownerauth) |
| 208 | if err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | defer sess.FlushHandlescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMSession#FlushHandle().() |
| 212 | |
| 213 | importRsp, err := tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.Importscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#{ |
| 214 | ParentHandlescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#ParentHandle.: parenthandle, |
| 215 | ObjectPublicscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#ObjectPublic.: key.Pubkeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Pubkey., |
| 216 | Duplicatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#Duplicate.: key.Privkeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Privkey., |
| 217 | InSymSeedscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#InSymSeed.: key.Secretscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Secret., |
| 218 | }.Executescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/Import#Execute().(tpm, sess.GetHMACscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMSession#GetHMAC().()) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | |
| 223 | // copy key |
| 224 | lkey := *key |
| 225 | |
| 226 | lkey.Secretscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#Secret. = tpm2scip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/.TPM2BEncryptedSecretscip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/TPM2BEncryptedSecret#{} |
| 227 | lkey.AddOptionsscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/TPMKey#AddOptions().( |
| 228 | WithKeytypescip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/WithKeytype().(OIDLoadableKeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/OIDLoadableKey.), |
| 229 | WithPrivkeyscip-go gomod github.com/foxboron/go-tpm-keyfiles . `github.com/foxboron/go-tpm-keyfiles`/WithPrivkey().(importRsp.OutPrivatescip-go gomod github.com/google/go-tpm 58e3e47cd434 `github.com/google/go-tpm/tpm2`/ImportResponse#OutPrivate.), |
| 230 | ) |
| 231 | |
| 232 | return &lkey, nil |
| 233 | } |