Exemple #1
0
        } // SimpleDuplicateImportRsaSample

        TssObject ImportExternalRsaKey(Tpm2 tpm, TpmHandle hParent,
                                       int keySizeInBits, IAsymSchemeUnion scheme,
                                       byte[] publicPart, byte[] privatePart,
                                       ObjectAttr keyAttrs,
                                       byte[] authVal = null, byte[] policyDigest = null)
        {
            TpmAlgId sigHashAlg = TpmHelper.GetSchemeHash(scheme),
                     nameAlg    = sigHashAlg;

            // TPM signing key template with the actual public key bits
            var inPub = new TpmPublic(nameAlg,
                                      keyAttrs,
                                      policyDigest,
                                      new RsaParms(new SymDefObject(), scheme as IAsymSchemeUnion,
                                                   (ushort)keySizeInBits, 0),
                                      new Tpm2bPublicKeyRsa(publicPart));

            // Wrap the key in a TSS helper class
            TssObject swKey = TssObject.Create(inPub, authVal, privatePart);

            // Get a key duplication blob in TPM 2.0 compatibale format
            TpmPrivate dupBlob = swKey.GetPlaintextDuplicationBlob();

            // Importing a duplication blob creates a new TPM private key blob protected
            // with its new parent key.
            swKey.Private = tpm.Import(hParent, null, swKey.Public, dupBlob, null, new SymDefObject());

            return(swKey);
        } // ImportExternalRsaKey
Exemple #2
0
        public static TpmAlgId GetSchemeHash(IAsymSchemeUnion scheme)
        {
            if (scheme == null || scheme is NullUnion || scheme is Empty)
            {
                return(TpmAlgId.Null);
            }
            var daaScheme = scheme as SchemeEcdaa;

            return(daaScheme != null ? daaScheme.hashAlg
                                     : (scheme as SchemeHash).hashAlg);
        }
            } // class PrivateKeyBlob


            // Trailing parameters are used to populate TpmPublic generated for the key from the blob.
            public static TpmPrivate CspToTpm(byte[] cspPrivateBlob, out TpmPublic tpmPub,
                                              TpmAlgId nameAlg        = TpmAlgId.Sha1,
                                              ObjectAttr keyAttrs     = ObjectAttr.Decrypt | ObjectAttr.UserWithAuth,
                                              IAsymSchemeUnion scheme = null,
                                              SymDefObject symDef     = null)
            {
                if (scheme == null)
                {
                    scheme = new NullAsymScheme();
                }
                if (symDef == null)
                {
                    symDef = new SymDefObject();
                }

                var m          = new Marshaller(cspPrivateBlob, DataRepresentation.LittleEndian);
                var cspPrivate = m.Get <Csp.PrivateKeyBlob>();
                var keyAlg     = cspPrivate.publicKeyStruc.aiKeyAlg;

                if (keyAlg != Csp.AlgId.CAlgRsaKeyX && keyAlg != Csp.AlgId.CAlgRsaSign)
                {
                    Globs.Throw <NotSupportedException>("CSP blobs for keys of type " + keyAlg.ToString("X") + " are not supported");
                    tpmPub = new TpmPublic();
                    return(new TpmPrivate());
                }

                var rsaPriv = new Tpm2bPrivateKeyRsa(Globs.ReverseByteOrder(cspPrivate.prime1));
                var sens    = new Sensitive(new byte[0], new byte[0], rsaPriv);

                tpmPub = new TpmPublic(nameAlg, keyAttrs, new byte[0],
                                       new RsaParms(symDef,
                                                    scheme,
                                                    (ushort)cspPrivate.rsaPubKey.bitlen,
                                                    cspPrivate.rsaPubKey.pubexp),
                                       new Tpm2bPublicKeyRsa(Globs.ReverseByteOrder(cspPrivate.modulus)));

                return(new TpmPrivate(sens.GetTpm2BRepresentation()));
            }
Exemple #4
0
 ///<param name = "the_symmetric">for a restricted decryption key, shall be set to a supported symmetric algorithm, key size. and mode. if the key is not a restricted decryption key, this field shall be set to TPM_ALG_NULL.</param>
 ///<param name = "the_scheme">If the sign attribute of the key is SET, then this shall be a valid signing scheme. NOTE	If the sign parameter in curveID indicates a mandatory scheme, then this field shall have the same value. If the decrypt attribute of the key is SET, then this shall be a valid key exchange scheme or TPM_ALG_NULL. If the key is a Storage Key, then this field shall be TPM_ALG_NULL.(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 ///<param name = "the_curveID">ECC curve ID</param>
 ///<param name = "the_kdf">an optional key derivation scheme for generating a symmetric key from a Z value If the kdf parameter associated with curveID is not TPM_ALG_NULL then this is required to be NULL. NOTE	There are currently no commands where this parameter has effect and, in the reference code, this field needs to be set to TPM_ALG_NULL.(One of SchemeMgf1, SchemeKdf1Sp80056a, SchemeKdf2, SchemeKdf1Sp800108, NullKdfScheme)</param>
 public EccParms(
 SymDefObject the_symmetric,
 IAsymSchemeUnion the_scheme,
 EccCurve the_curveID,
 IKdfSchemeUnion the_kdf
 )
 {
     this.symmetric = the_symmetric;
     this.scheme = the_scheme;
     this.curveID = the_curveID;
     this.kdf = the_kdf;
 }
Exemple #5
0
 ///<param name = "the_symmetric">for a restricted decryption key, shall be set to a supported symmetric algorithm, key size, and mode. if the key is not a restricted decryption key, this field shall be set to TPM_ALG_NULL.</param>
 ///<param name = "the_scheme">for an unrestricted signing key, shall be either TPM_ALG_RSAPSS TPM_ALG_RSASSA or TPM_ALG_NULL for a restricted signing key, shall be either TPM_ALG_RSAPSS or TPM_ALG_RSASSA for an unrestricted decryption key, shall be TPM_ALG_RSAES, TPM_ALG_OAEP, or TPM_ALG_NULL unless the object also has the sign attribute for a restricted decryption key, this field shall be TPM_ALG_NULL NOTE	When both sign and decrypt are SET, restricted shall be CLEAR and scheme shall be TPM_ALG_NULL.(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 ///<param name = "the_keyBits">number of bits in the public modulus</param>
 ///<param name = "the_exponent">the public exponent A prime number greater than 2. When zero, indicates that the exponent is the default of 216 + 1</param>
 public RsaParms(
 SymDefObject the_symmetric,
 IAsymSchemeUnion the_scheme,
 ushort the_keyBits,
 uint the_exponent
 )
 {
     this.symmetric = the_symmetric;
     this.scheme = the_scheme;
     this.keyBits = the_keyBits;
     this.exponent = the_exponent;
 }
Exemple #6
0
 ///<param name = "the_symmetric">the companion symmetric algorithm for a restricted decryption key and shall be set to a supported symmetric algorithm This field is optional for keys that are not decryption keys and shall be set to TPM_ALG_NULL if not used.</param>
 ///<param name = "the_scheme">for a key with the sign attribute SET, a valid signing scheme for the key type for a key with the decrypt attribute SET, a valid key exchange protocol for a key with sign and decrypt attributes, shall be TPM_ALG_NULL(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 public AsymParms(
 SymDefObject the_symmetric,
 IAsymSchemeUnion the_scheme
 )
 {
     this.symmetric = the_symmetric;
     this.scheme = the_scheme;
 }
Exemple #7
0
 ///<param name = "the_curveID">identifier for the curve</param>
 ///<param name = "the_keySize">Size in bits of the key</param>
 ///<param name = "the_kdf">the default KDF and hash algorithm used in secret sharing operations(One of SchemeMgf1, SchemeKdf1Sp80056a, SchemeKdf2, SchemeKdf1Sp800108, NullKdfScheme)</param>
 ///<param name = "the_sign">If not TPM_ALG_NULL, this is the mandatory signature scheme that is required to be used with this curve.(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 ///<param name = "the_p">Fp (the modulus)</param>
 ///<param name = "the_a">coefficient of the linear term in the curve equation</param>
 ///<param name = "the_b">constant term for curve equation</param>
 ///<param name = "the_gX">x coordinate of base point G</param>
 ///<param name = "the_gY">y coordinate of base point G</param>
 ///<param name = "the_n">order of G</param>
 ///<param name = "the_h">cofactor (a size of zero indicates a cofactor of 1)</param>
 public AlgorithmDetailEcc(
 EccCurve the_curveID,
 ushort the_keySize,
 IKdfSchemeUnion the_kdf,
 IAsymSchemeUnion the_sign,
 byte[] the_p,
 byte[] the_a,
 byte[] the_b,
 byte[] the_gX,
 byte[] the_gY,
 byte[] the_n,
 byte[] the_h
 )
 {
     this.curveID = the_curveID;
     this.keySize = the_keySize;
     this.kdf = the_kdf;
     this.sign = the_sign;
     this.p = the_p;
     this.a = the_a;
     this.b = the_b;
     this.gX = the_gX;
     this.gY = the_gY;
     this.n = the_n;
     this.h = the_h;
 }
Exemple #8
0
 ///<param name = "the_details">scheme parameters(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 public EccScheme(
 IAsymSchemeUnion the_details
 )
 {
     this.details = the_details;
 }
Exemple #9
0
 ///<param name = "the_details">scheme parameters(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 public RsaDecrypt(
 IAsymSchemeUnion the_details
 )
 {
     this.details = the_details;
 }
Exemple #10
0
 public byte[] RsaDecrypt(
     TpmHandle keyHandle,
     byte[] cipherText,
     IAsymSchemeUnion inScheme,
     byte[] label
 )
 {
     Tpm2RsaDecryptRequest inS = new Tpm2RsaDecryptRequest();
     inS.keyHandle = keyHandle;
     inS.cipherText = cipherText;
     inS.inScheme = inScheme;
     inS.label = label;
     TpmStructureBase outSBase;
     DispatchMethod(TpmCc.RsaDecrypt, (TpmStructureBase) inS, typeof(Tpm2RsaDecryptResponse), out outSBase, 1, 0);
     Tpm2RsaDecryptResponse outS = (Tpm2RsaDecryptResponse) outSBase;
     return outS.message;
 }
Exemple #11
0
 ///<param name = "the_keyHandle">RSA key to use for decryption Auth Index: 1 Auth Role: USER</param>
 ///<param name = "the_cipherText">cipher text to be decrypted NOTE	An encrypted RSA data block is the size of the public modulus.</param>
 ///<param name = "the_inScheme">the padding scheme to use if scheme associated with keyHandle is TPM_ALG_NULL(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 ///<param name = "the_label">label whose association with the message is to be verified</param>
 public Tpm2RsaDecryptRequest(
 TpmHandle the_keyHandle,
 byte[] the_cipherText,
 IAsymSchemeUnion the_inScheme,
 byte[] the_label
 )
 {
     this.keyHandle = the_keyHandle;
     this.cipherText = the_cipherText;
     this.inScheme = the_inScheme;
     this.label = the_label;
 }
Exemple #12
0
 ///<param name = "the_keyHandle">reference to public portion of RSA key to use for encryption Auth Index: None</param>
 ///<param name = "the_message">message to be encrypted NOTE 1	The data type was chosen because it limits the overall size of the input to no greater than the size of the largest RSA public key. This may be larger than allowed for keyHandle.</param>
 ///<param name = "the_inScheme">the padding scheme to use if scheme associated with keyHandle is TPM_ALG_NULL(One of KeySchemeEcdh, KeySchemeEcmqv, SigSchemeRsassa, SigSchemeRsapss, SigSchemeEcdsa, SigSchemeEcdaa, SigSchemeSm2, SigSchemeEcschnorr, EncSchemeRsaes, EncSchemeOaep, SchemeHash, NullAsymScheme)</param>
 ///<param name = "the_label">optional label L to be associated with the message Size of the buffer is zero if no label is present NOTE 2	See description of label above.</param>
 public Tpm2RsaEncryptRequest(
 TpmHandle the_keyHandle,
 byte[] the_message,
 IAsymSchemeUnion the_inScheme,
 byte[] the_label
 )
 {
     this.keyHandle = the_keyHandle;
     this.message = the_message;
     this.inScheme = the_inScheme;
     this.label = the_label;
 }