i want to encrypt and decrypt my messages with TPM. I use Microsofts TSS-Library. The documentation (including examples) can be found here: https://github.com/microsoft/TSS.MSR/tree/master/TSS.NET
Here is my CodeSnippet:
public byte[] encryptData(byte[] message)
{
TpmHandle handle = new TpmHandle();
byte[] keyAuth = new byte[] { 1, 2, 3 };
SigSchemeRsassa scheme = new SigSchemeRsassa(TpmAlgId.Rsa);
byte[] label = Encoding.Unicode.GetBytes("Label");
return Tpm[keyAuth].RsaEncrypt(handle, message, scheme,label);
}
The error is:
Error {Value} was returned for command RsaEncrypt. Details: [Code=TpmRc.Value],[RawCode=0x184,388] [ErrorEntity=Handle], [ParmNum=1]
I modified my Code from my signature Method (found in Samples). I just added my own SignatureData-class which stores the output information from CreatePrimary(): (I have had the same error in this Method as well with a wrong authValue, but now it works)
public void Sign(string message)
{
AuthValue ownerAuth = new AuthValue();
signatureData = new TpmSignatureData();
//Transform Message in byte-form
signatureData.ByteMessage = Encoding.Unicode.GetBytes(message);
//create keyTepmplate
TpmPublic keyTemplate = new TpmPublic(TpmAlgId.Sha1, ObjectAttr.UserWithAuth | ObjectAttr.Sign |
ObjectAttr.FixedParent | ObjectAttr.FixedTPM | ObjectAttr.SensitiveDataOrigin, null, // No policy
new RsaParms(new SymDefObject(), new SchemeRsassa(TpmAlgId.Sha1), 2048, 0), new Tpm2bPublicKeyRsa());
byte[] keyAuth = new byte[] { 1, 2, 3 };
#region Temporary Variables
TpmPublic tpmPublic;
CreationData creationData;
byte[] creationHash;
TkCreation creationTicket;
#endregion
//Create keyHandle based on keyTemplate
TpmHandle keyHandl = Tpm[ownerAuth].CreatePrimary(
TpmRh.Owner, // In the owner-hierarchy
new SensitiveCreate(keyAuth, null), // With this auth-value
keyTemplate, // Describes key
null, // Extra data for creation ticket
new PcrSelection[0], // Non-PCR-bound
out tpmPublic, // PubKey and attributes
out creationData, out creationHash, out creationTicket); // Not used here
//create Hash to be signed
TpmHash digestToSign = TpmHash.FromData(TpmAlgId.Sha1, signatureData.ByteMessage);
//Sign the hash-Value
signatureData.signature = Tpm[keyAuth].Sign(keyHandl, digestToSign, null, TpmHashCheck.Null()) as SignatureRsassa;
}
There is also another Method to Encryp it. You can also use the EncryptDecrypt-Method, but did not work for me as well. Am I missing something simple or do I need to change my Code completely. I guess my main problem is the TpmHandle. I am not sure how to handle with it. Any help would be great. Thanks in Advance.