Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Tpm2Lib;
- namespace EncryptionDecryption
- {
- class Program
- {
- /// <summary>
- /// Defines the argument to use to have this program use a TCP connection
- /// to communicate with a TPM 2.0 simulator.
- /// </summary>
- private const string DeviceSimulator = "-tcp";
- /// <summary>
- /// Defines the argument to use to have this program use the Windows TBS
- /// API to communicate with a TPM 2.0 device.
- /// </summary>
- private const string DeviceWinTbs = "-tbs";
- /// <summary>
- /// The default connection to use for communication with the TPM.
- /// </summary>
- private const string DefaultDevice = DeviceSimulator;
- /// <summary>
- /// If using a TCP connection, the default DNS name/IP address for the
- /// simulator.
- /// </summary>
- private const string DefaultSimulatorName = "127.0.0.1";
- /// <summary>
- /// If using a TCP connection, the default TCP port of the simulator.
- /// </summary>
- private const int DefaultSimulatorPort = 2321;
- static void Main(string[] args)
- {
- string tpmDeviceName = "-tcp";
- try
- {
- Tpm2Device tpmDevice;
- switch (tpmDeviceName)
- {
- case DeviceSimulator:
- tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);
- break;
- case DeviceWinTbs:
- tpmDevice = new TbsDevice();
- break;
- default:
- throw new Exception("Unknown device selected.");
- }
- tpmDevice.Connect();
- var tpm = new Tpm2(tpmDevice);
- if (tpmDevice is TcpTpmDevice)
- {
- //
- // If we are using the simulator, we have to do a few things the
- // firmware would usually do. These actions have to occur after
- // the connection has been established.
- //
- tpmDevice.PowerCycle();
- tpm.Startup(Su.Clear);
- }
- var sensCreate = new SensitiveCreate(new byte[] { 0xa, 0xb, 0xc }, null);
- TpmPublic parms = new TpmPublic(
- TpmAlgId.Sha1,
- ObjectAttr.Decrypt | ObjectAttr.UserWithAuth | ObjectAttr.SensitiveDataOrigin,
- null,
- new RsaParms(
- null, // new SymDefObject(TpmAlgId.Aes, 128, TpmAlgId.Cfb),
- new SchemeOaep(TpmAlgId.Sha1),
- 2048,
- 65537),
- new Tpm2bPublicKeyRsa());
- byte[] outsideInfo = Globs.GetRandomBytes(8);
- var creationPcr = new PcrSelection(TpmAlgId.Sha1, new uint[] { 0, 1, 2 });
- TpmPublic pubCreated;
- CreationData creationData;
- TkCreation creationTicket;
- byte[] creationHash;
- TpmHandle h = tpm.CreatePrimary(TpmRh.Null,
- sensCreate, // null,
- parms,
- null, //outsideInfo,
- null, //new PcrSelection[] { creationPcr },
- out pubCreated,
- out creationData,
- out creationHash,
- out creationTicket);
- Console.WriteLine("Primary RSA storage key created.");
- System.Text.ASCIIEncoding encASCII = new System.Text.ASCIIEncoding();
- byte[] secret = encASCII.GetBytes("secret");
- var encrypted = pubCreated.EncryptOaep(secret, null);
- var decrypted = tpm.RsaDecrypt(h, encrypted, new NullAsymScheme(), null);
- Console.WriteLine("secret: '{0}'", secret);
- Console.WriteLine("encrypted: '{0}'", encrypted);
- Console.WriteLine("decrypted: '{0}'", decrypted);
- tpm.FlushContext(h);
- tpm.Dispose();
- }
- catch (TpmException e)
- {
- //
- // If a command fails because an unexpected return code is in the response,
- // i.e., TPM returns an error code where success is expected or success
- // where an error code is expected. Or if the response is malformed, then
- // the unmarshaling code will throw a TPM exception.
- // The Error string will contain a description of the return code. Usually the
- // return code will be a known TPM return code. However, if using the TPM through
- // TBS, TBS might encode internal error codes into the response code. For instance
- // a return code of 0x80280400 indicates that a command is blocked by TBS. This
- // error code is also returned if the command is not implemented by the TPM.
- //
- // You can see the information included in the TPM exception by removing the
- // checks for available TPM commands above and running the sample on a TPM
- // without the required commands.
- //
- Console.WriteLine("TPM exception occurred: {0}", e.ToString());
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception occurred: {0}", e.ToString());
- }
- Console.WriteLine("Press Any Key to continue.");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement