BouncyCastle C#: How to Implement EllipticCurve Encryption/Decryption - c#

BouncyCastle includes many symmetric encryption engines, as well as RSA and ElGamal encryption engines (asymmetric engines). It also has a lot of online resources about how to use these engines to perform encryption/decryption processes. However, a bouncy castle provides no Elliptic Curve engine (check github/bc). After reviewing the code, all asymmetric engines implement the AsymmetricBlockCipher interface and none of them is an EC engine.
This is somehow confusing since BouncyCastle provides the ability to generate EC key pairs with different key strength based on predefined and well-known curves, like the example below:
public static AsymmetricCipherKeyPair GenerateKeys(int keySize)
{
DerObjectIdentifier oid;
switch (keySize)
{
case 192:
oid = X9ObjectIdentifiers.Prime192v1;
break;
case 224:
oid = SecObjectIdentifiers.SecP224r1;
break;
case 128:
oid = SecObjectIdentifiers.SecP128r1;
break;
case 239:
oid = X9ObjectIdentifiers.Prime239v1;
break;
case 256:
oid = X9ObjectIdentifiers.Prime256v1;
break;
case 384:
oid = SecObjectIdentifiers.SecP384r1;
break;
case 521:
oid = SecObjectIdentifiers.SecP521r1;
break;
default:
throw new InvalidParameterException("unknown key size.");
}
ECKeyPairGenerator gen = new ECKeyPairGenerator();
SecureRandom secureRandom = new SecureRandom();
X9ECParameters ecps = CustomNamedCurves.GetByOid(oid);
ECDomainParameters ecDomainParameters = new ECDomainParameters(ecps.Curve, ecps.G, ecps.N, ecps.H, ecps.GetSeed());
ECKeyGenerationParameters ecKeyGenerationParameters = new ECKeyGenerationParameters(ecDomainParameters, secureRandom);
gen.Init(ecKeyGenerationParameters);
return gen.GenerateKeyPair();
}
There are some engines, like IESEngine, that provides a public/private EC agreement on top of the encryption/decryption process (e.g. ECDHBasicAgreement), however, it doesn't use the public/private keys directly, instead, it calculates a new symmetric key from both keys that are then used to encrypt the plaintext message using a predefined symmetric cipher.
My question:
Is BC really not providing an easy to use EC Engine like
ElGamalEngine and RSAEngine?
If yes, how to implement a safe EC encryption/decryption process using directly the ECKeyParameters generated using the above function (if possible)?
Thanks in advance.

Is BC really not providing an easy to use EC Engine like ElGamalEngine and RSAEngine?
Correct, because there aren't any. In principle you could use ElGamal encryption with ECC, but that has such serious input limitations (requiring a point rather than normal plaintext) that it is hardly useful to do so. Furthermore, using it directly will lead to an insecure scheme. That's not specific to Bouncy Castle, by the way.
If yes, how to implement a safe EC encryption/decryption process using directly the ECKeyParameters generated using the above function (if possible)?
Unless you are a cryptographer / mathematician, you don't. You use ECIES.

After some research, I found that BouncyCastle has its own SM2Engine that implements the SM2 Digital Signature Algorithm and uses the ECKeyParameters (Elliptic curve key parameters) to provide encryption/decryption abilities.
Edit: please note that SM2 is not yet verified to be totally secure, where there has been relatively little analysis of the SM2 signature scheme that I can find in the anglophone cryptography literature beyond some side channel attacks. So use it upon your own responsibility.
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(true, new ParametersWithRandom((ECKeyParameters) publicKey, new SecureRandom()));
byte[] enc1 = sm2Engine.processBlock(plainText, 0, plainText.length);
System.out.println("Cipher Text (SM2Engine): " + Hex.toHexString(enc1));
sm2Engine = new SM2Engine();
sm2Engine.init(false, (ECKeyParameters) privateKey);
byte[] dec1 = sm2Engine.processBlock(enc1, 0, enc1.length);
System.out.println("Plain Text (SM2Engine): " + Hex.toHexString(dec1));

Related

Is there a way to generate a DSA certificate using pure .NET Framework and if not, why?

Starting from .NET 4.7.2 it is possible to generate RSA and EC certificates using .NET CertificateRequest. However I can't find anything that would allow me to generate DSA certs. Here is how I'd do it for RSA and EC:
private static X509Certificate2 GenerateRsaCertificate()
{
var hashAlgorithm = HashAlgorithmName.SHA256;
var rsaKey = RSA.Create(2048);
var subject = new X500DistinguishedName("CN=mycert");
var request = new CertificateRequest(subject, rsaKey, hashAlgorithm, RSASignaturePadding.Pkcs1);
var certificate = request.CreateSelfSigned(DateTime.Now - TimeSpan.FromDays(5), DateTime.Now + TimeSpan.FromDays(365));
return certificate;
}
private static X509Certificate2 GenerateEcDsaCertificate()
{
var hashAlgorithm = HashAlgorithmName.SHA256;
var curve = ECCurve.NamedCurves.nistP256;
var ecDsaKey = ECDsa.Create(curve);
var subject = new X500DistinguishedName("CN=mycert");
var request = new CertificateRequest(subject, ecDsaKey, hashAlgorithm);
var certificate = request.CreateSelfSigned(DateTime.Now - TimeSpan.FromDays(5), DateTime.Now + TimeSpan.FromDays(365));
return certificate;
}
Previously I used Bouncy Castle to generate all three types of certs, but with migration to .NET I'm able to use only RSA and ECDsa in CertificateRequest calls. Are there any reasons why DSA is not included? I can still generate a key with DSA.Create(keySize) however. Also .NET Framework includes other classes that work with DSA: DSA, DSACng, DSACryptoServiceProvider, DSACertificateExtensions, but I don't see anything for certificate generation. Are there any problems with the algorithm itself (maybe I shouldn't use it at all)? Or am I missing something in the API?
Are there any problems with the algorithm itself (maybe I shouldn't use it at all)?
Non-EC DSA is dying.
I'll speculate that the thing that really did it in is the original specification (FIPS 186-1) limited the keys to 1024-bit and the algorithm to SHA-1. In 2009 the algorithm got updated in FIPS 186-3 to support slightly larger keys and the SHA-2 hashes. FIPS 186-1 (and FIPS 186-2) DSA signatures only required data and a private key (verification only required data, signature, and a public key), FIPS 186-3 signatures also require the hash algorithm as an input... so the API isn't exactly compatible.
Windows CAPI (the older of the two Windows cryptography platforms) ignored the FIPS 186-3 update, as did Apple's Security.framework. Windows CNG and OpenSSL both support "new DSA". Apple can't process certificates signed with "new DSA" (and maybe not even with "DSA classic", I forget), and Windows doesn't support "new DSA" in cert chains, only "DSA classic".
So DSA certificates are generally limited to FIPS 186-1/186-2 restrictions, which means SHA-1 (not on anyone's good side these days) and 1024-bit keys (which are too small by today's reckoning). If you know you're being validated by OpenSSL you can use better DSA keys.
DSA is also generally much slower at signature verification than RSA.
At the 80 bits of security level, using OpenSSL's speed tool on a random VM of mine (output slightly modified for presentation purposes, sorted by verify/s descending):
sign verify sign/s verify/s
rsa 1024 bits 0.000301s 0.000018s 3326.3 56419.7
dsa 1024 bits 0.000309s 0.000236s 3236.2 4240.5
ecdsa 160 bits (secp160r1) 0.0005s 0.0004s 1984.6 2385.7
112 bits of security
sign verify sign/s verify/s
rsa 2048 bits 0.002030s 0.000062s 492.6 16062.4
ecdsa 224 bits (nistp224) 0.0001s 0.0002s 9020.6 4252.2
dsa 2048 bits 0.000885s 0.000802s 1129.4 1247.3
128 bits of security
sign verify sign/s verify/s
rsa 3072 bits 0.006935s 0.000135s 144.2 7401.6
ecdsa 256 bits (nistp256) 0.0001s 0.0002s 16901.5 5344.7
ecdsa 256 bits (brainpoolP256t1) 0.0010s 0.0008s 980.1 1262.5
ecdsa 256 bits (brainpoolP256r1) 0.0010s 0.0008s 1012.9 1209.5
dsa 3072 bits (not in test suite)
192 bits of security
sign verify sign/s verify/s
rsa 7680 bits 0.122805s 0.000820s 8.1 1220.2
ecdsa 384 bits (nistp384) 0.0024s 0.0018s 416.1 571.2
ecdsa 384 bits (brainpoolP384t1) 0.0024s 0.0018s 410.0 545.1
ecdsa 384 bits (brainpoolP384r1) 0.0025s 0.0019s 407.4 540.1
dsa 7680 bits (beyond FIPS 186-3 DSA maximum of 3072 bits)
256 bits of security
sign verify sign/s verify/s
ecdsa 521 bits (nistp521) 0.0006s 0.0012s 1563.1 841.3
ecdsa 512 bits (brainpoolP512t1) 0.0038s 0.0027s 265.2 369.1
ecdsa 512 bits (brainpoolP512r1) 0.0038s 0.0028s 262.4 360.5
rsa 15360 bits 0.783846s 0.003190s 1.3 313.5
dsa 15360 bits (beyond FIPS 186-3 DSA maximum of 3072 bits)
I'm able to use only RSA and ECDsa in CertificateRequest calls. Are there any reasons why DSA is not included?
From the thread with the original feature proposal:
Based on new data from Windows (and their lack of support for FIPS 186-3 DSA certificates) I'm going to pull the DSA typed constructor and leave DSA as a "power user" scenario (custom X509SignatureGenerator class, etc)
So, it was removed mainly because DSA is dying.
Or am I missing something in the API?
The API allows for custom signature generators to be provided. In the tests for CertificateRequest it proves this out with a DSAX509SignatureGenerator
X509SignatureGenerator dsaGen = new DSAX509SignatureGenerator(dsaCsp);
// Use SHA-1 because that's all DSACryptoServiceProvider understands.
HashAlgorithmName hashAlgorithm = HashAlgorithmName.SHA1;
CertificateRequest request = new CertificateRequest(
new X500DistinguishedName($"CN={KeyName}-{provType}"),
dsaGen.PublicKey,
hashAlgorithm);
DateTimeOffset now = DateTimeOffset.UtcNow;
using (X509Certificate2 cert = request.Create(request.SubjectName, dsaGen, now, now.AddDays(1), new byte[1]))
using (X509Certificate2 certWithPrivateKey = cert.CopyWithPrivateKey(dsaCsp))
using (DSA dsa = certWithPrivateKey.GetDSAPrivateKey())
{
byte[] signature = dsa.SignData(Array.Empty<byte>(), hashAlgorithm);
Assert.True(dsaCsp.VerifyData(Array.Empty<byte>(), signature, hashAlgorithm));
}
(snippet from https://github.com/dotnet/runtime/blob/4f9ae42d861fcb4be2fcd5d3d55d5f227d30e723/src/libraries/System.Security.Cryptography.X509Certificates/tests/CertificateCreation/PrivateKeyAssociationTests.cs#L276-L295)

How can I encrypt data using a public key from ECC X509 certificate in .net framework on windows?

I am using:
Windows 10 (Version 1709, OS Build 17025.1000)
.net framework 4.7
VS 2017 (version: 15.3.5)
Here is what I did:
Got a self signed ECC certificate using OpenSSL and steps outlined in the script at https://gist.github.com/sidshetye/4759690 with modifications:
a) Used NIST/P-256 curve over a 256 bit prime field
b) Used SHA-256
Load the certificate from file (generated in previous step) into X509Certificate2 object
Imported the PFX file into windows trust store (for testing). This is successful.
Inspection of the imported certificate shows Public Key field as 'ECC (256 Bits)' and Public key parameters as 'ECDSA_P256'.
Next tried to figure out how to encrypt with this certificate.
I am stuck at the last step because all the examples that use X509Certificate2 object predominantly use only RSA and I am using ECC certificate. For RSA certificate, there is a GetRSAPublicKey extention method on X509Certificate2 and RSA class has Encrypt method. However there is no such method for ECC certificates.
Next, I stumbled on this post (Load a Certificate Using X509Certificate2 with ECC Public Key) and tried following (even though it appeared bizarre as to why ECC cert public key is being coerced into RSA type):
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key
I got following exception: The certificate key algorithm is not supported.
Next I stumbled on this post (Importing ECC-based certificate from the Windows Certificate Store into CngKey) which basically tried to create CNGKey type and instantiate ECDsaCng with it. However even if I can do it with ECDiffieHellmanCng, there is no Encrypt method on it.
So I am not really sure how can I proceed further to use ECC X509 certificate's public key to encrypt data.
###Background
Asymmetric algorithms have three different purposes (that I know of)
Encryption
RSA is the only "standard" algorithm that can do this directly.
Signature
RSA
DSA
ECDSA
ElGamal Signature
Key Agreement
Diffie-Hellman (DH)
ECDH
ElGamal encryption (the asymmetric startup phase)
MQV
ECMQV
Because RSA encryption is space limited, and was hard for computers in the '90s, RSA encryption's primary use was in "Key Transfer", which is to say that the "encrypted message" was just the symmetric encryption key for DES/3DES (AES not yet having been invented) - https://www.rfc-editor.org/rfc/rfc2313#section-8.
Key agreement (or transfer) schemes always have to be combined with a protocol/scheme to result in an encryption operation. Such schemes include
TLS (nee SSL)
CMS or S/MIME encrypted-data
IES (Integrated Encryption Scheme)
ECIES (Elliptic Curve Integrated Encryption Scheme)
ElGamal encryption (holistically)
PGP encryption
So what you probably want is ECIES.
ECIES.Net
Currently (.NET Framework 4.7.1, .NET Core 2.0) there's no support to get an ECDiffieHellman object from a certificate in .NET.
Game over, right? Well, probably not. Unless a certificate carrying an ECDH key explicitly uses the id-ecDH algorithm identifier (vs the more standard id-ecc one) it can be opened as ECDSA. Then, you can coerce that object into being ECDH:
using (ECDsa ecdsa = cert.GetECDsaPublicKey())
{
return ECDiffieHellman.Create(ecdsa.ExportParameters(false));
}
(a similar thing can be done for a private key, if the key is exportable, otherwise complex things are required, but you shouldn't need it)
Let's go ahead and carve off the recipient public object:
ECDiffieHellmanPublicKey recipientPublic = GetECDHFromCertificate(cert).PublicKey;
ECCurve curve = recipientPublic.ExportParameters().Curve;
So now we turn to http://www.secg.org/sec1-v2.pdf section 5.1 (Elliptic Curve Integrated Encryption Scheme)
###Setup
Choose ANSI-X9.63-KDF with SHA-2-256 as the hash function.
Choose HMAC–SHA-256–256.
Choose AES–256 in CBC mode.
Choose Elliptic Curve Diffie-Hellman Primitive.
You already chose secp256r1.
Hard-coded. Done.
Point compression's annoying, choose not to use it.
I'm omitting SharedInfo. That probably makes me a bad person.
Not using XOR, N/A.
###Encrypt
Make an ephemeral key on the right curve.
ECDiffieHellman ephem = ECDiffieHellman.Create(curve);
We decided no.
ECParameters ephemPublicParams = ephem.ExportParameters(false);
int pointLen = ephemPublicParams.Q.X.Length;
byte[] rBar = new byte[pointLen * 2 + 1];
rBar[0] = 0x04;
Buffer.BlockCopy(ephemPublicParams.Q.X, 0, rBar, 1, pointLen);
Buffer.BlockCopy(ephemPublicParams.Q.Y, 0, rBar, 1 + pointLen, pointLen);
Can't directly do this, moving on.
Can't directly do this, moving on.
Since we're in control here, we'll just do 3, 4, 5, and 6 as one thing.
KDF time.
// This is why we picked AES 256, HMAC-SHA-2-256(-256) and SHA-2-256,
// the KDF is dead simple.
byte[] ek = ephem.DeriveKeyFromHash(
recipientPublic,
HashAlgorithmName.SHA256,
null,
new byte[] { 0, 0, 0, 1 });
byte[] mk = ephem.DeriveKeyFromHash(
recipientPublic,
HashAlgorithmName.SHA256,
null,
new byte[] { 0, 0, 0, 2 });
Encrypt stuff.
byte[] em;
// ECIES uses AES with the all zero IV. Since the key is never reused,
// there's not risk in that.
using (Aes aes = Aes.Create())
using (ICryptoTransform encryptor = aes.CreateEncryptor(ek, new byte[16]))
{
if (!encryptor.CanTransformMultipleBlocks)
{
throw new InvalidOperationException();
}
em = encryptor.TransformFinalBlock(message, 0, message.Length);
}
MAC it
byte[] d;
using (HMAC hmac = new HMACSHA256(mk))
{
d = hmac.ComputeHash(em);
}
Finish
// Either
return Tuple.Create(rBar, em, d);
// Or
return rBar.Concat(em).Concat(d).ToArray();
###Decrypt
Left as an exercise to the reader.
For getting ECDiffieHellman private key from certificate, use the following method:
Install NuGet package Security.Cryptography (CLR Security). (The package is under MIT license.)
Use the following extension method to get the CngKey instance:
CngKey cngKey = certificate.GetCngPrivateKey();
(Note: The extension method certificate.GetECDsaPrivateKey(), natively supported in .NET, returns an ECDsaCng instance; there is no extension method to return ECDiffieHellmanCng.)
The cngKey instance can be used to create either an ECDsaCng or an ECDiffieHellmanCng instance:
var sa = new ECDsaCng(cngKey);
var sa = new ECDiffieHellmanCng(cngKey);

How to sign public PGP key with Bouncy Castle in C#

I want to create web of trust support in my application, allowing my users to use their private keys, to sign other user's public keys - Using C# and Bouncy Castle.
I've got most things figured out, such as creating PGP keys, submitting them to key servers using HTTP REST, encrypting MIME messages and cryptographically signing them (using MimeKit) - But the one remaining hurdle, is to figure out some piece of code that can use my private key, to sign for another person's public key, using Bouncy Castle.
Since the documentation for BC is horrendous, figuring out these parts, have previously proven close to impossible ...
For the record, I'm using GnuPG as my storage for keys.
If anybody wants to look at my code so far for what I have done, feel free to check it out here.
I am probably not supposed to ask this here, but I'd also love it if some BC gurus out there could have a general look at my code so far, and check if I've made a fool of myself with the stuff I've done so far ...
Found the answer after a lot of trial and error, here it is ...
private static byte[] SignPublicKey(
PgpSecretKey secretKey,
string password,
PgpPublicKey keyToBeSigned,
bool isCertain)
{
// Extracting private key, and getting ready to create a signature.
PgpPrivateKey pgpPrivKey = secretKey.ExtractPrivateKey (password.ToCharArray());
PgpSignatureGenerator sGen = new PgpSignatureGenerator (secretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1);
sGen.InitSign (isCertain ? PgpSignature.PositiveCertification : PgpSignature.CasualCertification, pgpPrivKey);
// Creating a stream to wrap the results of operation.
Stream os = new MemoryStream();
BcpgOutputStream bOut = new BcpgOutputStream (os);
sGen.GenerateOnePassVersion (false).Encode (bOut);
// Creating a generator.
PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator();
PgpSignatureSubpacketVector packetVector = spGen.Generate();
sGen.SetHashedSubpackets (packetVector);
bOut.Flush();
// Returning the signed public key.
return PgpPublicKey.AddCertification (keyToBeSigned, sGen.Generate()).GetEncoded();
}

Self signed certificate: Private key questions

To implement TLS encryption via SslStream i am using a self signed certificate.
I am experiencing strange "no common algorithm" errors for clients connecting from an older Win2003 machine.
After reading this thread i discovered the following:
Those errors disappear if i change my certificate generation procedure (more specifically: the private key generation part):
Old:
var privateKey = new CX509PrivateKey();
privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0";
privateKey.MachineContext = true;
privateKey.Length = 2048;
privateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE
privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
privateKey.Create();
New:
var privateKey = new CX509PrivateKey();
privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0";
privateKey.MachineContext = true;
privateKey.Length = 1024;
privateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE;
privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
privateKey.Create();
My questions (may sound stupid, sorry for that; i'm fairly new to TLS & co):
Which algorithms are relaying on a private key with this keySpec value? Can I see somewhere which algo has been taken by SslStream?
Why do I have to reduce the key length to 1024? Any value above will cause an exception to occur when calling Create().
Am I taking security risks with these changes?
Any suggestions refering to fixing the Win2K03 machine are also welcome...
Microsoft Base Cryptographic Provider v1.0 is the most limited of the cryptographic providers. For AT_EXCHANGE it is limited to 1024-bit RSA, per https://msdn.microsoft.com/en-us/library/windows/desktop/bb931357(v=vs.85).aspx.
Your TLS error probably comes from the SChannel library wanting to use the RSA key in AT_EXCHANGE mode even on ciphersuites where RSA signature is used, but not RSA encryption, since your two files differ in both keyspec and value.
Microsoft Enhanced RSA and AES Cryptographic Provider is the newest (added in XP SP3) CSP, if you change to that you should be able to make RSA AT_EXCHANGE keys up to length 16384 (though it'll take hours to do so, so you might want to stick to your 2048).

How to encrypt bytes using the TPM (Trusted Platform Module)

How can I encrypt bytes using a machine's TPM module?
CryptProtectData
Windows provides a (relatively) simple API to encrypt a blob using the CryptProtectData API, which we can wrap an easy to use function:
public Byte[] ProtectBytes(Byte[] plaintext)
{
//...
}
The details of ProtectBytes are less important than the idea that you can use it quite easily:
here are the bytes I want encrypted by a secret key held in the System
give me back the encrypted blob
The returned blob is an undocumented documentation structure that contains everything needed to decrypt and return the original data (hash algorithm, cipher algorithm, salt, HMAC signature, etc).
For completeness, here's the sample pseudocode implementation of ProtectBytes that uses the Crypt API to protect bytes:
public Byte[] ProtectBytes(Byte[] plaintext)
{
//Setup our n-byte plaintext blob
DATA_BLOB dataIn;
dataIn.cbData = plaintext.Length;
dataIn.pbData = Addr(plaintext[0]);
DATA_BLOB dataOut;
//dataOut = EncryptedFormOf(dataIn)
BOOL bRes = CryptProtectData(
dataIn,
null, //data description (optional PWideChar)
null, //optional entropy (PDATA_BLOB)
null, //reserved
null, //prompt struct
CRYPTPROTECT_UI_FORBIDDEN || CRYPTPROTECT_LOCAL_MACHINE,
ref dataOut);
if (!bRes) then
{
DWORD le = GetLastError();
throw new Win32Error(le, "Error calling CryptProtectData");
}
//Copy ciphertext from dataOut blob into an actual array
bytes[] result;
SetLength(result, dataOut.cbData);
CopyMemory(dataOut.pbData, Addr(result[0]), dataOut.cbData);
//When you have finished using the DATA_BLOB structure, free its pbData member by calling the LocalFree function
LocalFree(HANDLE(dataOut.pbData)); //LocalFree takes a handle, not a pointer. But that's what the SDK says.
}
How to do the same with the TPM?
The above code is useful for encrypting data for the local machine only. The data is encrypted using the System account as the key generator (details, while interesting, are unimportant). The end result is that I can encrypt data (e.g. a hard drive encryption master key) that can only be decrypted by the local machine.
Now it's time to take this one step further. I want to encrypt some data (e.g. a hard drive encryption master key) that can only be decrypted by the local TPM. In other words, I want to replace the Qualcomm Trusted Execution Environment (TEE) in the block diagram below for Android, with the TPM in Windows:
Note: I realize that the TPM doesn't do data-signing (or if it does, it does not guarantee that signing the same data will give the same binary output every time). Which is why I'd be willing to replace "RSA signing" with "encrypting a 256-bit blob with a hardware bound key".
So where's the code?
The problem is that TPM programming is completely undocumented on MSDN. There is no API available to perform any operations. Instead you have to find yourself a copy of the Trusted Computing Group's Software Stack (aka TSS), figure out what commands to send to the TPM, with payloads, in what order, and call Window's Tbsip_Submit_Command function to submit commands directly:
TBS_RESULT Tbsip_Submit_Command(
_In_ TBS_HCONTEXT hContext,
_In_ TBS_COMMAND_LOCALITY Locality,
_In_ TBS_COMMAND_PRIORITY Priority,
_In_ const PCBYTE *pabCommand,
_In_ UINT32 cbCommand,
_Out_ PBYTE *pabResult,
_Inout_ UINT32 *pcbOutput
);
Windows has no higher level API to perform actions.
It's the moral equivalent of trying to create a text file by issuing SATA I/O commands to your hard drive.
Why not just use Trousers
The Trusted Computing Group (TCG) did define their own API: TCB Software Stack (TSS). An implementation of this API was created by some people, and is called TrouSerS. A guy then ported that project to Windows.
The problem with that code is that it is not portable into the Windows world. For example, you can't use it from Delphi, you cannot use it from C#. It requires:
OpenSSL
pThread
I just want the code to encrypt something with my TPM.
The above CryptProtectData requires nothing other than what's in the function body.
What is the equivalent code to encrypt data using the TPM? As others have noted, you probably have to consult the three TPM manuals, and construct the blobs yourself. It probably involves the TPM_seal command. Although I think I don't want to seal data, I think I want to bind it:
Binding – encrypts data using TPM bind key, a unique RSA key descended from a storage key.
Sealing – encrypts data in a similar manner to binding, but in addition specifies a state in which TPM must be in order for the data to be decrypted (unsealed)
I try to read the three required volumes in order to find the 20 lines of code I need:
Part 1 - Design Principles
Part 2 - Structures of the TPM
Part 3 - Commands
But I have no idea what I'm reading. If there was any kind of tutorial or examples, I might have a shot. But I'm completely lost.
So we ask Stackoverflow
In the same way I was able to provide:
Byte[] ProtectBytes_Crypt(Byte[] plaintext)
{
//...
CryptProtectData(...);
//...
}
can someone provide the corresponding equivalent:
Byte[] ProtectBytes_TPM(Byte[] plaintext)
{
//...
Tbsip_Submit_Command(...);
Tbsip_Submit_Command(...);
Tbsip_Submit_Command(...);
//...snip...
Tbsip_Submit_Command(...);
//...
}
that does the same thing, except rather than a key locked away in System LSA, is locked away in the TPM?
Start of Research
I don't know exactly what bind means. But looking at TPM Main - Part 3 Commands - Specification Version 1.2, there is a mention of bind:
10.3 TPM_UnBind
TPM_UnBind takes the data blob that is the result of a Tspi_Data_Bind command and decrypts it for export to the User. The caller must authorize the use of the key that will decrypt the incoming blob.
TPM_UnBind operates on a block-by-block basis, and has no notion of any relation between one block and another.
What's confusing is there is no Tspi_Data_Bind command.
Research Effort
It is horrifying how nobody has ever bothered to document the TPM or its operation. It's as if they spent all their time coming up with this cool thing to play with, but didn't want to deal with the painful step of making it usable for something.
Starting with the (now) free book A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security:
Chapter 3 - Quick Tutorial on TPM 2.0
The TPM has access to a self-generated private key, so it can encrypt keys with a public key and then store the resulting blob on the hard disk. This way, the TPM can keep a virtually unlimited number of keys available for use but not waste valuable internal storage. Keys stored on the hard disk can be erased, but they can also be backed up, which seemed to the designers like an acceptable trade-off.
How can I encrypt a key with the TPM's public key?
Chapter 4 - Existing Applications That Use TPMs
Applications That Should Use the TPM but Don’t
In the past few years, the number of web-based applications has increased. Among them are web-based backup and storage. A large number of companies now offer such services, but as far as we are aware, none of the clients for these services let the user lock the key for the backup service to a TPM. If this were done, it would certainly be nice if the TPM key itself were backed up by duplicating it on multiple machines. This appears to be an opportunity for developers.
How does a developer lock a key to the TPM?
Chapter 9 - Heirarchies
USE CASE: STORING LOGIN PASSWORDS
A typical password file stores salted hashes of passwords. Verification consists of salting and hashing a supplied password and comparing it to the stored value. Because the calculation doesn’t include a secret, it’s subject to an offline attack on the password file.
This use case uses a TPM-generated HMAC key. The password file stores an HMAC of the salted password. Verification consists of salting and HMACing the supplied password and comparing it to the stored value. Because an offline attacker doesn’t have the HMAC key, the attacker can’t mount an attack by performing the calculation.
This could work. If the TPM has a secret HMAC key, and only my TPM knows the HMAC key, then I could replace "Sign (aka TPM encrypt with it's private key)" with "HMAC". But then in the very next line he reverses himself completely:
TPM2_Create, specifying an HMAC key
It's not a TPM secret if I have to specify the HMAC key. The fact that the HMAC key isn't secret makes sense when you realize this is the chapter about cryptographic utilities that the TPM provides. Rather than you having to write SHA2, AES, HMAC, or RSA yourself, you can re-use what the TPM already has laying around.
Chapter 10 - Keys
As a security device, the ability of an application to use keys while keeping them safe in a hardware device is the TPM’s greatest strength. The TPM can both generate and import externally generated keys. It supports both asymmetric and symmetric keys.
Excellent! How do you do it!?
Key Generator
Arguably, the TPM’s greatest strength is its ability to generate a cryptographic key and protect its secret within a hardware boundary. The key generator is based on the TPM’s own random number generator and doesn’t rely on external sources of randomness. It thus eliminates weaknesses based on weak softwaresoftware with an insufficient source of entropy.
Does the TPM have the ability to generate cryptographic keys and protect its secrets within a hardware boundary? Is so, how?
Chapter 12 - Platform Configuration Registers
PCRs for Authorization
USE CASE: SEALING A HARD DISK ENCRYPTION KEY TO PLATFORM STATE
Full-disk encryption applications are far more secure if a TPM protects theencryption key than if it’s stored on the same disk, protected only by a password.
First, the TPM hardware has anti-hammering protection (see Chapter 8 for a detailed description of TPM dictionary attack protection), making a brute-force attack on the password impractical. A key protected only by software is far more vulnerable to a weak password. Second, a software key stored on disk is far easier to steal. Take the disk (or a backup of the disk), and you get the key. When a TPM holds the key,
the entire platform, or at least the disk and the motherboard, must be stolen.
Sealing permits the key to be protected not only by a password but by a policy. A typical policy locks the key to PCR values (the software state) current at the time of sealing. This assumes that the state at first boot isn’t compromised. Any preinstalled malware present at first boot would be measured into the PCRs, and thus the key would be sealed to a compromised software state. A less trusting enterprise might have a standard disk image and seal to PCRs representing that image. These PCR values would be precalculated on a presumably more trusted platform. An even more sophisticated enterprise would use TPM2_PolicyAuthorize, and provide several tickets authorizing a set of trusted PCR values. See Chapter 14 for a detailed description of policy authorize and its application to solve the PCRbrittleness problem.
Although a password could also protect the key, there is a security gain even without a TPM key password. An attacker could boot the platform without supplying a TPMkey password but could not log in without the OS username and password. The OSsecurity protects the data. The attacker could boot an alternative OS, say from a live DVD or USB stick rather that from the hard drive, to bypass the OS login security. However, this different boot configuration and software would change the PCRvalues. Because these new PCRs would not match the sealed values, the TPM would not release the decryption key, and the hard drive could not be decrypted.
Excellent! This is exactly the use case I happen to want. It's also the use case the Microsoft uses the TPM for. How do I do it!?
So I read that entire book, and it provided nothing useful. Which is quite impressive because it's 375 pages. You wonder what the book contained - and looking back on it, I have no idea.
So we give up on the definitive guide to programming the TPM, and turn instead to some documentation from Microsoft:
From the Microsoft TPM Platform Crypto-Provider Toolkit. It mentions exactly what I want to do:
The Endorsement Key or EK
The EK is designed to provide a reliable cryptographic identifier for the platform. An enterprise might maintain a database of the Endorsement Keys belonging to the TPMs of all of the PCs in their enterprise, or a data center fabric controller might have a database of the TPMs in all of the blades. On Windows you can use the NCrypt provider described in the section “Platform Crypto Provider in Windows 8” to read the public part of the EK.
Somewhere inside the TPM is an RSA private key. That key is locked away in there - never to be seen by the outside world. I want the TPM to sign something with it's private key (i.e. encrypt it with it's private key).
So I want the most basic operation that can possibly exist:
Encrypt something with your private key. I'm not even (yet) asking for the more complicated stuff:
"sealing" it based on PCR state
creating a key and storing it in volatile or non-volatile memroy
creating a symmetric key and trying to load it into the TPM
I am asking for the most basic operation a TPM can do. Why is it impossible to get any information about how to do it?
I can get random data
I suppose I was being glib when I said RSA signing was the most basic thing the TPM can do. The most basic thing the TPM can be asked to do is give me random bytes. That I have figured out how to do:
public Byte[] GetRandomBytesTPM(int desiredBytes)
{
//The maximum random number size is limited to 4,096 bytes per call
Byte[] result = new Byte[desiredBytes];
BCRYPT_ALG_HANDLE hAlgorithm;
BCryptOpenAlgorithmProvider(
out hAlgorithm,
BCRYPT_RNG_ALGORITHM, //AlgorithmID: "RNG"
MS_PLATFORM_CRYPTO_PROVIDER, //Implementation: "Microsoft Platform Crypto Provider" i.e. the TPM
0 //Flags
);
try
{
BCryptGenRandom(hAlgorithm, #result[0], desiredBytes, 0);
}
finally
{
BCryptCloseAlgorithmProvider(hAlgorithm);
}
return result;
}
The Fancy Thing
I realize the volume of people using the TPM is very low. That is why nobody on Stackoverflow has an answer. So I can't really get too greedy in getting a solution to my common problem. But the thing I'd really want to do is to "seal" some data:
present the TPM some data (e.g. 32 bytes of key material)
have the TPM encrypt the data, returning some opaque blob structure
later ask the TPM to decrypt the blob
the decryption will only work if the TPM's PCR registers are the same as they were during encryption.
In other words:
Byte[] ProtectBytes_TPM(Byte[] plaintext, Boolean sealToPcr)
{
//...
}
Byte[] UnprotectBytes_TPM(Byte[] protectedBlob)
{
//...
}
Cryptography Next Gen (Cng, aka BCrypt) supports TPM
The original Cryptography API in Windows was knows as the Crypto API.
Starting with Windows Vista, the Crypto API has been replaced with Cryptography API: Next Generation (internally known as BestCrypt, abbreviated as BCrypt, not to be confused with the password hashing algorithm).
Windows ships with two BCrypt providers:
Microsoft Primitive Provider (MS_PRIMITIVE_PROVIDER) default: Default software implementation of all the primitives (hashing, symmetric encryption, digital signatures, etc)
Microsoft Platform Crypto Provider (MS_PLATFORM_CRYPTO_PROVIDER): Provider that provides TPM access
The Platform Crypto provider is not documented on MSDN, but does have documentation from a 2012 Microsoft Research site:
TPM Platform Crypto-Provider Toolkit
The TPM Platform Crypto Provider and Toolkit contains sample code, utilities and documentation for using TPM-related functionality in Windows 8. Subsystems described include the TPM-backed Crypto-Next-Gen (CNG) platform crypto-provider, and how attestation-service providers can use the new Windows features. Both TPM1.2 and TPM2.0-based systems are supported.
It seems that Microsoft's intent is to surface TPM crypto functionality with the Microsoft Platform Crypto Provider of the Cryptography NG API.
Public key encryption using Microsoft BCrypt
Given that:
i want to perform RSA asymmetric encryption (using the TPM)
Microsoft BestCrypt supports RSA asymmetric encryption
Microsoft BestCrypt has a TPM Provider
a way forward might be to figure out how to do digital signing using the Microsoft Cryptography Next Gen API.
My next step will be to come up with the code to do encryption in BCrypt, with an RSA public key, using the standard provider (MS_PRIMITIVE_PROVIDER). E.g.:
modulus: 0xDC 67 FA F4 9E F2 72 1D 45 2C B4 80 79 06 A0 94 27 50 8209 DD 67 CE 57 B8 6C 4A 4F 40 9F D2 D1 69 FB 995D 85 0C 07 A1 F9 47 1B 56 16 6E F6 7F B9 CF 2A 58 36 37 99 29 AA 4F A8 12 E8 4F C7 82 2B 9D 72 2A 9C DE 6F C2 EE 12 6D CF F0 F2 B8 C4 DD 7C 5C 1A C8 17 51 A9 AC DF 08 22 04 9D 2B D7 F9 4B 09 DE 9A EB 5C 51 1A D8 F8 F9 56 9E F8 FB 37 9B 3F D3 74 65 24 0D FF 34 75 57 A4 F5 BF 55
publicExponent: 65537
With that code functioning, i may be able to switch to using the TPM Provider (MS_PLATFORM_CRYPTO_PROVIDER).
2/22/2016: And with Apple being compelled to help decrypt user data, there is renewed interest in how to make the TPM perform the most simplest task that it was invented for - encrypting something.
It's roughly equivalent to everyone owning a car, but nobody knows how to start one. It can do really useful and cool things, if only we could get past Step 1.
Microsoft Key Storage API
Microsoft's TPM Base Servicesarchive documentation homepage says we probably want to use the Key Storage API instead:
Note
The TPM can be used for key storage operations. However, developers are encouraged to use the Key Storage APIs for these scenarios instead. The Key Storage APIs provide the functionality to create, sign or encrypt with, and persist cryptographic keys, and they are higher-level and easier to use than the TBS for these targeted scenarios.
The introduction to the Key Storage APIarchive says:
Key Storage Architecture
CNG provides a model for private key storage
that allows adapting to the current and future demands of creating
applications that use cryptography features such as public or private
key encryption, as well as the demands of the storage of key material.
The key storage router is the central routine in this model and is
implemented in Ncrypt.dll. An application accesses the key storage
providers (KSPs) on the system through the key storage router, which
conceals details, such as key isolation, from both the application and
the storage provider itself. The following illustration shows the
design and function of the CNG key isolation architecture.
And they note that hardware security modules (presumably the term for a TPM) are supported:
As described above, a wide range of hardware storage devices can be supported. In each case, the interface to all of these storage devices is identical. It includes functions to perform various private key operations as well as functions that pertain to key storage and management.
Only thing I don't know is if you have to ask to use a HSM, or does it happens automatically when available (and how to know when it isn't available - so you don't try to proceed anyway).
Bonus Reading
Android - Encryption - Storing the encrypted key
Android Explorations - Revisiting Android disk encryption
DPAPI Secrets. Security analysis and data recovery in DPAPI (Part 1)
CryptoNextGeneration : Storing a key in the TPM
How to export RSA private key from TPM through CNG KSP API
Primer
All that follows is about TPM 1.2. Keep in mind that Microsoft requires a TPM 2.0 for all future Windows versions. The 2.0 generation is fundamentally different to the 1.2
There is no one-line solution because of TPM design principles. Think of the TPM as a microcontroller with limited resources. It main design goal was to be cheap, while still secure. So the TPM was ripped of all logic which was not necessary for a secure operation. Thus a TPM is only working when you have at least some more or less fat software, issuing a lot of commands in the correct order. And those sequences of commands may get very complex. That's why TCG specified the TSS with a well defined API. If you would like to go the Java way, there is even an high level Java API. I'm not aware of an similar project for C# / .net
Development
In your case I'd suggest you look at IBM's software TPM.
Project page
Donwload the whole package
In the package you will find 3 very usefull components:
a software TPM emulator
a lightweight tpm lib
some basic command line utilities
You don't necessarily need the software TPM emulator, you can also connect to the machine's HW TPM. However, you can intercept the issued commands and look at the responses, thus learning how they are assembled and how they correspond to the command specification.
High level
Prerequisites:
TPM is activated
TPM driver is loaded
you have taken ownership of the TPM
In order to seal a blob, you need to do the following:
create a key
store the key-blob somewhere
ensure that the key is loaded in the TPM
seal the blob
To unseal you need to:
obtain the key-blob
load the key to the TPM
unseal the sealed blob
You can store the key-blob in your data structure you use to store the protected bytes.
Most of the TPM commands you need are authorized ones. Therefore you need to establish authorization sessions where needed. AFAIR those are mostly OSAP sessions.
TPM commands
Currently I can't run a debug version, so I can't provide you with the exact sequence. So consider this an unordered list of commands you will have to use:
TPM_OSAP
TPM_CreateWrapKey
TPM_LoadKey2
TPM_Seal
If you want to read the current PCR values, too:
TPM_PCRRead
How can I encrypt bytes using a machine's TPM module?
Depends on your intent and circumstances:
What kind of a TPM do you have (1-family or 2-family)?
What state is the TPM in? Has it been owned? Has it been provisioned?
What is your programming language?
Do you want to encrypt or sign? (that's vague from the rest of the question)
How big is the data you want to encrypt?
Do you want to use a symmetric key or an asymmetric key?
Do you want to use a key that already exists on the TPM, or do you want to have it create the key first?
By "encrypt" do you perhaps mean "wrap a key"?
Do you want to lock the encrypted data to the system configuration, so that it can only be decrypted when the system is back in the same configuration?
Do you want to require authorization for decrypting?
Perhaps you don't need to encrypt at all, but rather store the data within the TPM?
If you are storing the data within the TPM, do you want to require authorization, or for the system to be in a particular configuration, for retrieval?
Each of these use cases (and there are more) -- or a combination thereof -- presents a different implementation path. Think of the TPM as a Swiss Army knife of cryptographic devices: there isn't much you can't do with it, but ease of use suffers for that versatility. The question keeps bouncing between encrypting, signing and locking to system configuration, but the main part of this answer will consider the Seal command to cover most of the needs described in the question.
Now it's time to take this one step further. I want to encrypt some
data (e.g. a hard drive encryption master key) that can only be
decrypted by the local TPM.
This is what the Bind command is for (superseded by the Create command for TPM 2). You load a key that derives from a TPM-bound key and encrypt with it (or directly with a hardware-bound key). This way the data can only be decrypted with access to the same TPM.
In other words, I want to replace the Qualcomm Trusted Execution
Environment (TEE) in the block diagram below for Android, with the TPM
in Windows:
Not sure if replicating this whole process is a good idea. For one, there is no need to use a signing operation anywhere in the process. It would appear that, at the time when Android 5 was being developed, the Keystore API was limited to signing and verification operations. My best guess is that the disk encryption team did their best to work with what they had and devised an algorithm whereby one of the intermediate keys was derived with a signing operation, using a stored TEE key, thereby tying the whole process to a hardware-bound key only available on the platform -- as signing was the only way to do that at the time. However, there is no need to constrain yourself in such ways if have access to a TPM, which gives you more capabilities than you knew you needed!
I realize that the TPM doesn't do data-signing
This is false, both versions of TPM support signing.
(or if it does, it does not guarantee that signing the same data will
give the same binary output every time)
This makes no sense. Signing the same data with the same key will produce the same signature. You may be confusing the signing operation with the quoting operation, which will mix in a nonce.
Which is why I'd be willing to replace "RSA signing" with "encrypting
a 256-bit blob with a hardware bound key".
This should actually be the preferred option, although both are possible with a TPM. See above.
The problem is that TPM programming is completely undocumented on
MSDN. There is no API available to perform any operations.
Unfortunately there isn't much to document. The Win API is limited to a couple of TBS functions which are one level removed from the driver.
Instead you have to find yourself a copy of the Trusted Computing
Group's Software Stack (aka TSS), figure out what commands to send to
the TPM, with payloads, in what order, and call Window's
Tbsip_Submit_Command function to submit commands directly:
Actually, no, if you had a TSS you wouldn't have to use Tbsip_submit_Command(). That's the whole point of having a TSS -- the low-level details are abstracted away.
Windows has no higher level API to perform actions.
Still true for TPM 1, but for TPM 2 there is TSS.MSR.
It's the moral equivalent of trying to create a text file by issuing
SATA I/O commands to your hard drive.
Correct.
Why not just use Trousers ... The problem with that code is that it is
not portable into the Windows world. For example, you can't use it
from Delphi, you cannot use it from C#. It requires:
OpenSSL,
pThread
It's not clear that this is an insurmountable challenge. Accessing TrouSerS through an interop should be preferable to rewriting all the data structuring code. Also, there was doTSS at the time of writing the question.
What is the equivalent code to encrypt data using the TPM? It probably
involves the TPM_seal command. Although I think I don't want to seal
data, I think I want to bind it:
The question contains a quote describing the difference between the two commands, so there shouldn't be much confusion. Sealing is similar to binding, with the added constraint that the system state must be the same for the data to be unsealed.
In the same way I was able to provide:
Byte[] ProtectBytes_Crypt(Byte[] plaintext)
{
//...
CryptProtectData(...);
//...
}
can someone provide the corresponding equivalent:
Byte[] ProtectBytes_TPM(Byte[] plaintext)
{
//...
Tbsip_Submit_Command(...);
Tbsip_Submit_Command(...);
Tbsip_Submit_Command(...);
//...snip...
Tbsip_Submit_Command(...);
//...
}
that does the same thing, except rather than a key locked away in
System LSA, is locked away in the TPM?
First, it's worth pointing out that there are two major versions of TPM, which are totally incompatible between each other. So virtually no code you may have written for TPM 1 will work for TPM 2. The TBS API is the only common code between the two and, to be fair to Microsoft, this may have been one of the reasons why that API never grew. The main part of the answer will present the code for TPM 1 for two reasons:
The question is loaded with TPM 1 specific concepts, so people using TPM 1 are more likely to land here searching for them
There is a Microsoft implementation of TSS for TPM 2.
Second, let's make the question more specific. I'm reinterpreting it as follows:
How do I write code in C#, using only the TBS API, to interface with
an already owned and provisioned TPM to, without user interaction,
encrypt no more than 128 bytes of arbitrary data with an asymmetric
key already resident in the TPM and bound to it, but not protected
with a password, so that in order to decrypt the data the system may
need to be in the same state it was in at encryption time based on an
easily configurable variable?
The Seal command is best suited for this, as it performs the same function as the Bind command when the PCR selection size is set to zero, but the PCR selection can easily be changed to include any PCRs you may want. It makes one wonder why the Bind command was included in the spec at all, and as noted it was removed in the TPM 2 spec and the two were combined in one Create command.
Here is the C# code for using the TPM 1.2 Seal command to encrypt data with only TBS functions (note: this code is untested and not likely to work without debugging):
[DllImport ("tbs.dll")]
unsafe static extern UInt32 Tbsi_Context_Create (UInt32 * version, IntPtr * hContext);
[DllImport ("tbs.dll")]
unsafe static extern UInt32 Tbsip_Context_Close (IntPtr hContext);
[DllImport ("tbs.dll")]
unsafe static extern UInt32 Tbsip_Submit_Command (
IntPtr hContext, UInt32 Locality,
UInt32 Priority,
byte * pCommandBuf,
UInt32 CommandBufLen,
byte * pResultBuf,
UInt32 * pResultBufLen);
byte[] ProtectBytes_TPM (byte[] plaintext) {
void AddUInt32Reversed (byte[] a, System.UInt32 o, ref int i) {
byte[] bytes = System.BitConverter.GetBytes (o);
Array.Reverse (bytes);
Array.Copy (bytes, 0, a, i, bytes.Length);
i += bytes.Length;
}
void AddUInt16Reversed (byte[] a, System.UInt16 o, ref int i) {
byte[] bytes = System.BitConverter.GetBytes (o);
Array.Reverse (bytes);
Array.Copy (bytes, 0, a, i, bytes.Length);
i += bytes.Length;
}
void AddBool (byte[] a, byte b, ref int i) {
a[i] = b;
i += 1;
}
void AddBlob (byte[] a, byte[] b, ref int i) {
Array.Copy (b, 0, a, i, b.Length);
i += b.Length;
}
byte[] Xor (byte[] text, byte[] key) {
byte[] xor = new byte[text.Length];
for (int i = 0; i < text.Length; i++) {
xor[i] = (byte) (text[i] ^ key[i % key.Length]);
}
return xor;
}
int offset;
Random rnd = new Random ();
IntPtr hContext = IntPtr.Zero;
unsafe {
UInt32 version = 1;
IntPtr handle = hContext;
UInt32 result = Tbsi_Context_Create ( & version, & handle);
if (result == 0) {
hContext = handle;
}
}
byte[] cmdBuf = new byte[768];
//OSAP
System.UInt32 outSize;
byte[] oddOsap = new byte[20];
byte[] evenOsap = new byte[20];
byte[] nonceEven = new byte[20];
byte[] nonceOdd = new byte[20];
System.UInt32 hAuth = 0;
offset = 0;
AddUInt16Reversed (cmdBuf, 0x00C1, ref offset);
offset = 6;
AddUInt32Reversed (cmdBuf, 0x0000000B, ref offset);
offset = 2 + 4 + 4; //2 for tag, 4 for size and 4 for command code
AddUInt16Reversed (cmdBuf, 0x0004, ref offset); //Entity Type SRK = 0x0004
AddUInt32Reversed (cmdBuf, 0x40000000, ref offset); //Entity Value SRK = 0x40000000
rnd.NextBytes (oddOsap);
AddBlob (cmdBuf, oddOsap, ref offset);
uint cmdSize = (System.UInt32) offset;
offset = 2;
AddUInt32Reversed (cmdBuf, cmdSize, ref offset);
outSize = (System.UInt32) (Marshal.SizeOf (hAuth) + nonceEven.Length + evenOsap.Length);
byte[] response = new byte[outSize];
unsafe {
UInt32 result = 0;
//uint cmdSize = (uint)offset;
uint resSize = outSize;
fixed (byte * pCmd = cmdBuf, pRes = response) {
result = Tbsip_Submit_Command (hContext, 0, 200, pCmd, cmdSize, pRes, & resSize);
}
}
byte contSession = 0;
System.UInt32 hKey = 0x40000000; //TPM_KH_SRK;
System.UInt32 pcrInfoSize = 0;
byte[] srkAuthdata = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
uint inDataSize = (uint) plaintext.Length;
offset = 2 + 4 + 4; //2 for tag, 4 for size and 4 for return code
byte[] hauthbytes = new byte[Marshal.SizeOf (hAuth)];
Array.Copy (response, offset, hauthbytes, 0, hauthbytes.Length);
Array.Reverse (hauthbytes);
hAuth = System.BitConverter.ToUInt32 (hauthbytes, 0);
offset += Marshal.SizeOf (hAuth);
Array.Copy (response, offset, nonceEven, 0, nonceEven.Length);
offset += nonceEven.Length;
Array.Copy (response, offset, evenOsap, 0, evenOsap.Length);
//shared-secret = HMAC(srk_auth, even_osap || odd_osap)
byte[] sharedSecretBuf = new byte[evenOsap.Length + oddOsap.Length];
Array.Copy (evenOsap, 0, sharedSecretBuf, 0, evenOsap.Length);
Array.Copy (oddOsap, 0, sharedSecretBuf, evenOsap.Length, oddOsap.Length);
System.Security.Cryptography.HMACSHA1 sharedSecretHmac = new System.Security.Cryptography.HMACSHA1 (srkAuthdata);
byte[] sharedSecret = sharedSecretHmac.ComputeHash (sharedSecretBuf);
byte[] authSha1InBuf = new byte[sharedSecret.Length + nonceEven.Length];
Array.Copy (sharedSecret, 0, authSha1InBuf, 0, sharedSecret.Length);
Array.Copy (nonceEven, 0, authSha1InBuf, sharedSecret.Length, nonceEven.Length);
System.Security.Cryptography.SHA1Managed sha1 = new System.Security.Cryptography.SHA1Managed ();
byte[] authSha1 = sha1.ComputeHash (authSha1InBuf);
byte[] encAuth = Xor (srkAuthdata, authSha1);
//inParamDigest = sha1(1S ~ 6S)
int paramInDigestInBufSize =
sizeof (System.UInt32) +
encAuth.Length +
Marshal.SizeOf (pcrInfoSize) +
Marshal.SizeOf (inDataSize) +
(int) inDataSize;
byte[] paramInDigestInBuf = new byte[paramInDigestInBufSize];
offset = 0;
AddUInt32Reversed (paramInDigestInBuf, 0x00000017, ref offset);
AddBlob (paramInDigestInBuf, encAuth, ref offset);
AddUInt32Reversed (paramInDigestInBuf, 0x0, ref offset); //PCR info size
AddUInt32Reversed (paramInDigestInBuf, inDataSize, ref offset);
AddBlob (paramInDigestInBuf, plaintext, ref offset);
byte[] paramInDigest = sha1.ComputeHash (paramInDigestInBuf);
int pubAuthInBufSize = paramInDigest.Length + nonceEven.Length + nonceOdd.Length + Marshal.SizeOf (contSession);
byte[] pubAuthInBuf = new byte[pubAuthInBufSize];
offset = 0;
AddBlob (pubAuthInBuf, paramInDigest, ref offset);
AddBlob (pubAuthInBuf, nonceEven, ref offset);
AddBlob (pubAuthInBuf, nonceOdd, ref offset);
AddBool (pubAuthInBuf, contSession, ref offset);
System.Security.Cryptography.HMACSHA1 pubAuthHmac = new System.Security.Cryptography.HMACSHA1 (sharedSecret);
byte[] pubAuth = pubAuthHmac.ComputeHash (pubAuthInBuf);
//Seal
offset = 0;
AddUInt16Reversed (cmdBuf, 0x00C2, ref offset); // TPM_TAG_RQU_AUTH1_COMMAND;
offset = 6;
AddUInt32Reversed (cmdBuf, 0x00000017, ref offset); // TPM_ORD_SEAL;
offset = 2 + 4 + 4; //2 for tag, 4 for size and 4 for command code
AddUInt32Reversed (cmdBuf, hKey, ref offset);
AddBlob (cmdBuf, encAuth, ref offset);
AddUInt32Reversed (cmdBuf, pcrInfoSize, ref offset);
AddUInt32Reversed (cmdBuf, inDataSize, ref offset);
AddBlob (cmdBuf, plaintext, ref offset);
AddUInt32Reversed (cmdBuf, hAuth, ref offset);
AddBlob (cmdBuf, nonceOdd, ref offset);
AddBool (cmdBuf, contSession, ref offset);
AddBlob (cmdBuf, pubAuth, ref offset);
cmdSize = (System.UInt32) offset;
offset = 2;
AddUInt32Reversed (cmdBuf, cmdSize, ref offset);
outSize = 768;
uint responseSize = 0;
response = new byte[outSize];
unsafe {
UInt32 result = 0;
uint resSize = outSize;
fixed (byte * pCmd = cmdBuf, pRes = response) {
result = Tbsip_Submit_Command (hContext, 0, 200, pCmd, cmdSize, pRes, & resSize);
}
responseSize = resSize;
}
byte[] retBuffer = new byte[responseSize - 10];
Array.Copy (response, 10, retBuffer, 0, retBuffer.Length);
Tbsip_Context_Close (hContext);
return retBuffer;
}
Code Analysis:
[DllImport ("tbs.dll")]
...
These are some of the few functions available in Tbs.h and the only ones we'll use here. They basically allow you to open a handle to the device and communicate with it by sending and receiving raw bytes.
void AddUInt32Reversed (byte[] a, System.UInt32 o, ref int i) { ... }
void AddUInt16Reversed (byte[] a, System.UInt16 o, ref int i) { ... }
void AddBool (byte[] a, byte b, ref int i) { ... }
void AddBlob (byte[] a, byte[] b, ref int i) { ... }
TPM is big endian, Windows is little endian. So the byte order will have to be reversed for any data we're sending over. We only need to worry about reversing 32-bit and 16-bit unsigned ints here.
...
UInt32 result = Tbsi_Context_Create ( & version, & handle);
...
Here we use Tbsi_Context_Create() to open a handle to talk to the TPM. The TBS_CONTEXT_PARAMS parameter is just a C struct with one unsigned 32-bit int field that must be set to 1 to talk to a TPM 1.2 instance, and that what we set it to.
byte[] cmdBuf = new byte[768];
This is specified as the minimum buffer size in the TPM PC Client Spec. It will be more than enough for our needs here.
TPM 1.2 Spec Part 3 says the following:
TPM_Seal requires the encryption of one parameter (“Secret”). For the
sake of uniformity with other commands that require the encryption of
more than one parameter, the string used for XOR encryption is
generated by concatenating a nonce (created during the OSAP session)
with the session shared secret and then hashing the result.
We need to XOR-encrypt this "secret" parameter using a nonce generated during an OSAP session. One of the Seal command input handles is also an OSAP handle:
The authorization session handle used for keyHandle authorization.
Must be an OSAP session for this command.
So we need to establish this OSAP session first. OSAP is described in TPM 1.2 Spec Part 1. OSAP, or Object-Specific Authorization Protocol, was invented to handle the use case where you want to use a TPM object that requires authorization multiple times, but don't want to provide authorization each time: an OSAP session is used instead, which relies on the concept of "shared secret", which is an HMAC which mixes in the object authorization data with nonces generated on each side to prevent reply attacks. Therefore the "shared secret" is only known to the two sides in this session: the side that initiated the session (user) and the side that accepted it (TPM); also, both sides must have the same object authorization data for the "shared secret" to be the same; additionally, "shared secret" used in one session will be invalid in another. This diagram from the spec describes the process:
We will not be using multiple sessions in this particular case (in fact, that parameter is ignored with the Seal command!) and the key we will be using does not require authorization, but unfortunately we are still bound by the spec to establish an OSAP session.
offset = 0;
AddUInt16Reversed (cmdBuf, 0x00C1, ref offset);
offset = 6;
AddUInt32Reversed (cmdBuf, 0x0000000B, ref offset);
offset = 2 + 4 + 4; //2 for tag, 4 for size and 4 for command code
AddUInt16Reversed (cmdBuf, 0x0004, ref offset); //Entity Type SRK = 0x0004
AddUInt32Reversed (cmdBuf, 0x40000000, ref offset); //Entity Value SRK = 0x40000000
rnd.NextBytes (oddOsap);
AddBlob (cmdBuf, oddOsap, ref offset);
uint cmdSize = (System.UInt32) offset;
TPM_OSAP command operands are:
Each TPM 1.2 command is laid out like this:
2 bytes 4 bytes 4 bytes
+---------+------------------+------------------+---------------------------
| Tag | Size | Command code | Command body ....
+---------+------------------+------------------+---------------------------
The tag is a two-byte value that indicates whether what follows is either input or output, and whether there are any auth data values following command parameters. For TPM_OSAP, the tag must be TPM_TAG_RQU_COMMAND (0x00C1) as per the spec, which means "a command with no authorization".
Size is a four-byte value that specifies the size of the command in bytes, including the tag and size itself. We will set this value later, once we have computed it.
Command code is a four-byte value that servers as a command ID: it tells the TPM how to interpret the rest of the command. Our command code here is TPM_OSAP (0x0000000B).
The next two things to set are entity type and entity value. Since we want to use a key that already exists in the TPM, we will use entity type "SRK" (0x0004), and since we are working under the assumption that the TPM has already been owned, it is safe to assume that it has an SRK loaded under the permanent handle 0x40000000 as per the spec, so we will use this permanent handle value for our entity value. (SRK stands for "Storage Root Key" and is the root key from which most other TPM-owned keys derive)
result = Tbsip_Submit_Command (hContext, 0, 200, pCmd, cmdSize, pRes, & resSize);
Finally we compute the command size and set it, and send the command.
offset = 2 + 4 + 4; //2 for tag, 4 for size and 4 for return code
byte[] hauthbytes = new byte[Marshal.SizeOf (hAuth)];
Array.Copy (response, offset, hauthbytes, 0, hauthbytes.Length);
Array.Reverse (hauthbytes);
hAuth = System.BitConverter.ToUInt32 (hauthbytes, 0);
offset += Marshal.SizeOf (hAuth);
Array.Copy (response, offset, nonceEven, 0, nonceEven.Length);
offset += nonceEven.Length;
Array.Copy (response, offset, evenOsap, 0, evenOsap.Length);
The data we're supposed to get back from the TPM on TPM_OSAP is:
So we get back:
The authorization handle to use with our main command (Seal)
nonceEven: the nonce generated by the TPM to use with the main command
nonceEvenOSAP: the OSAP nonce that is the counter-nonce to the nonce we generated on our side before sending the TPM_OSAP command. These two nonces will be used in generating the "shared secret".
We extract those values and store them in variables.
byte[] sharedSecretBuf = new byte[evenOsap.Length + oddOsap.Length];
Array.Copy (evenOsap, 0, sharedSecretBuf, 0, evenOsap.Length);
Array.Copy (oddOsap, 0, sharedSecretBuf, evenOsap.Length, oddOsap.Length);
System.Security.Cryptography.HMACSHA1 sharedSecretHmac = new System.Security.Cryptography.HMACSHA1 (srkAuthdata);
byte[] sharedSecret = sharedSecretHmac.ComputeHash (sharedSecretBuf);
Then we calculate the "shared secret". As per the spec, the values that go into the calculation are the two OSAP nonces (one generated by the user and one generated by the TPM) and the authorization value for the key we want to use -- the SRK. By convention, the SRK auth value is the "well-known auth": a zeroed out 20-byte buffer. Technically, one could change this value to something else when taking ownership of the TPM, but this is not done in practice, so we can safely assume the "well-known auth" value is good.
Next let's take a look at what goes into the TPM_Seal command:
Most of these parameters are trivial to build, except for two of them: encAuth and pubAuth. Let's look at them one by one.
encAuth is "The encrypted AuthData for the sealed data." Our AuthData here is the "well-known auth" from before, but yes we still have to encrypt it. Because we are using an OSAP session it is encrypted following ADIP, or Authorization-Data Insertion Protocol. From the spec: "The ADIP allows for the creation of new entities and the secure insertion of the new entity AuthData. The transmission of the new AuthData uses encryption with the key based on the shared secret of an OSAP session." Additionally: "For the mandatory XOR encryption algorithm, the creator builds an encryption key using a SHA-1 hash of the OSAP shared secret and a session nonce. The creator XOR encrypts the new AuthData using the encryption key as a one-time pad and sends this encrypted data along with the creation request to the TPM." So we have to build an XOR key from the session nonce and the "shared secret" and then XOR-encrypt our "well-known auth" with that key.
The following diagram explains how ADIP operates:
pubAuth is "The authorization session digest for inputs and keyHandle." Part 1 of the spec, in "Parameter Declarations for OIAP and OSAP Examples" explains how to interpret the TPM_Seal parameter table above: "The HMAC # column details the parameters used in the HMAC calculation. Parameters 1S, 2S, etc. are concatenated and hashed to inParamDigest or outParamDigest, implicitly called 1H1 and possibly 1H2 if there are two authorization sessions. For the first session, 1H1, 2H1, 3H1, and 4H1 are concatenated and HMAC’ed. For the second session, 1H2, 2H2, 3H2, and 4H2 are concatenated and HMAC’ed." So we will have to hash the plaintext, its size, PCR info size, encAuth from above and the TPM_Seal ordinal, and then HMAC that with the two nonces and the "continue session" boolean using the OSAP "shared secret" as the HMAC key.
Putting it all together in a diagram:
Notice how we set "PCR info size" to zero in this code, as we just want to encrypt the data without locking it to a system state. However it is trivial to provide a "PCR info" structure if needed.
offset = 0;
AddUInt16Reversed (cmdBuf, 0x00C2, ref offset);
offset = 6;
AddUInt32Reversed (cmdBuf, 0x00000017, ref offset); // TPM_ORD_SEAL;
...
result = Tbsip_Submit_Command (hContext, 0, 200, pCmd, cmdSize, pRes, & resSize);
Finally we construct the command and send it.
byte[] retBuffer = new byte[responseSize - 10];
Array.Copy (response, 10, retBuffer, 0, retBuffer.Length);
Tbsip_Context_Close (hContext);
return retBuffer;
We use the Tbsip_Context_Close() function to close our communication handle.
We return the response as-is here. Ideally you would want to reverse the bytes again and validate it by recomputing the resAuth value to prevent man-in-the-middle attacks.
What's confusing is there is no Tspi_Data_Bind command.
This is because Tspi_Data_Bind is a TSS command, not a TPM command. The reason why is because it requires no secrets (only the public key is used) so it can be done without involving the TPM. This caused confusion, however, and even the commands that require no secrets are now included in the TPM 2 spec.
How can I encrypt a key with the TPM's public key?
Depends on the TPM version. With the TPM_CreateWrapKey command for TPM 1.2. With the TPM2_Create command for TPM 2.
How does a developer lock a key to the TPM?
Either create it in the TPM, or wrap it, or use any other of the available methods.
TPM2_Create, specifying an HMAC key
The text in the book is confusing. You don't specify the HMAC key, you specify that you want an HMAC key.
The fact that the HMAC key isn't secret makes sense
No it does not make sense. The key is secret.
...use keys while keeping them safe in a hardware device... Excellent!
How do you do it!?
There are commands to either create keys or import them for both version of TPM. For TPM 1 there was only one root key -- the SRK -- from which you could establish a key hierarchy by creating wrapped keys. With TPM 2 you can have multiple primary, or root, keys.
Does the TPM have the ability to generate cryptographic keys and
protect its secrets within a hardware boundary? Is so, how?
See above.
Excellent! This is exactly the use case I happen to want. It's also
the use case the Microsoft uses the TPM for. How do I do it!?
Probably depends on the type of the drive. In the case of non-SED drives, the drive encryption key is probably wrapped with a TPM key. In the case of SED drives, the Admin1 password (or such) is sealed with the TPM.
The Endorsement Key or EK... Somewhere inside the TPM is an RSA
private key. That key is locked away in there - never to be seen by
the outside world. I want the TPM to sign something with its private
key (i.e. encrypt it with it's private key).
The EK is not a signing key -- it's an encryption key. However, it's not a general-purpose encryption key: it can only be used in certain contexts.
But the thing I'd really want to do is to "seal" some data
See above.
Trusted and Encrypted Keys
Trusted and Encrypted Keys are two new key types added to the existing kernel
key ring service. Both of these new types are variable length symmetric keys,
and in both cases all keys are created in the kernel, and user space sees,
stores, and loads only encrypted blobs. Trusted Keys require the availability
of a Trusted Platform Module (TPM) chip for greater security, while Encrypted
Keys can be used on any system. All user level blobs, are displayed and loaded
in hex ascii for convenience, and are integrity verified.
Trusted Keys use a TPM both to generate and to seal the keys. Keys are sealed
under a 2048 bit RSA key in the TPM, and optionally sealed to specified PCR
(integrity measurement) values, and only unsealed by the TPM, if PCRs and blob
integrity verifications match. A loaded Trusted Key can be updated with new
(future) PCR values, so keys are easily migrated to new pcr values, such as
when the kernel and initramfs are updated. The same key can have many saved
blobs under different PCR values, so multiple boots are easily supported.
By default, trusted keys are sealed under the SRK, which has the default
authorization value (20 zeros). This can be set at takeownership time with the
trouser's utility: tpm_takeownership -u -z.
Usage:
keyctl add trusted name "new keylen [options]" ring
keyctl add trusted name "load hex_blob [pcrlock=pcrnum]" ring
keyctl update key "update [options]"
keyctl print keyid
options:
keyhandle= ascii hex value of sealing key default 0x40000000 (SRK)
keyauth= ascii hex auth for sealing key default 0x00...i
(40 ascii zeros)
blobauth= ascii hex auth for sealed data default 0x00...
(40 ascii zeros)
blobauth= ascii hex auth for sealed data default 0x00...
(40 ascii zeros)
pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no default)
pcrlock= pcr number to be extended to "lock" blob
migratable= 0|1 indicating permission to reseal to new PCR values,
default 1 (resealing allowed)
keyctl print returns an ascii hex copy of the sealed key, which is in standard
TPM_STORED_DATA format. The key length for new keys are always in bytes.
Trusted Keys can be 32 - 128 bytes (256 - 1024 bits), the upper limit is to fit
within the 2048 bit SRK (RSA) keylength, with all necessary structure/padding.
Encrypted keys do not depend on a TPM, and are faster, as they use AES for
encryption/decryption. New keys are created from kernel generated random
numbers, and are encrypted/decrypted using a specified 'master' key. The
'master' key can either be a trusted-key or user-key type. The main
disadvantage of encrypted keys is that if they are not rooted in a trusted key,
they are only as secure as the user key encrypting them. The master user key
should therefore be loaded in as secure a way as possible, preferably early in
boot.
The decrypted portion of encrypted keys can contain either a simple symmetric
key or a more complex structure. The format of the more complex structure is
application specific, which is identified by 'format'.
Usage:
keyctl add encrypted name "new [format] key-type:master-key-name keylen"
ring
keyctl add encrypted name "load hex_blob" ring
keyctl update keyid "update key-type:master-key-name"
format:= 'default | ecryptfs'
key-type:= 'trusted' | 'user'
Examples of trusted and encrypted key usage
Create and save a trusted key named "kmk" of length 32 bytes:
$ keyctl add trusted kmk "new 32" #u
440502848
$ keyctl show
Session Keyring
-3 --alswrv 500 500 keyring: _ses
97833714 --alswrv 500 -1 \_ keyring: _uid.500
440502848 --alswrv 500 500 \_ trusted: kmk
$ keyctl print 440502848
0101000000000000000001005d01b7e3f4a6be5709930f3b70a743cbb42e0cc95e18e915
3f60da455bbf1144ad12e4f92b452f966929f6105fd29ca28e4d4d5a031d068478bacb0b
27351119f822911b0a11ba3d3498ba6a32e50dac7f32894dd890eb9ad578e4e292c83722
a52e56a097e6a68b3f56f7a52ece0cdccba1eb62cad7d817f6dc58898b3ac15f36026fec
d568bd4a706cb60bb37be6d8f1240661199d640b66fb0fe3b079f97f450b9ef9c22c6d5d
dd379f0facd1cd020281dfa3c70ba21a3fa6fc2471dc6d13ecf8298b946f65345faa5ef0
f1f8fff03ad0acb083725535636addb08d73dedb9832da198081e5deae84bfaf0409c22b
e4a8aea2b607ec96931e6f4d4fe563ba
$ keyctl pipe 440502848 > kmk.blob
Load a trusted key from the saved blob:
$ keyctl add trusted kmk "load `cat kmk.blob`" #u
268728824
$ keyctl print 268728824
0101000000000000000001005d01b7e3f4a6be5709930f3b70a743cbb42e0cc95e18e915
3f60da455bbf1144ad12e4f92b452f966929f6105fd29ca28e4d4d5a031d068478bacb0b
27351119f822911b0a11ba3d3498ba6a32e50dac7f32894dd890eb9ad578e4e292c83722
a52e56a097e6a68b3f56f7a52ece0cdccba1eb62cad7d817f6dc58898b3ac15f36026fec
d568bd4a706cb60bb37be6d8f1240661199d640b66fb0fe3b079f97f450b9ef9c22c6d5d
dd379f0facd1cd020281dfa3c70ba21a3fa6fc2471dc6d13ecf8298b946f65345faa5ef0
f1f8fff03ad0acb083725535636addb08d73dedb9832da198081e5deae84bfaf0409c22b
e4a8aea2b607ec96931e6f4d4fe563ba
Reseal a trusted key under new pcr values:
$ keyctl update 268728824 "update pcrinfo=`cat pcr.blob`"
$ keyctl print 268728824
010100000000002c0002800093c35a09b70fff26e7a98ae786c641e678ec6ffb6b46d805
77c8a6377aed9d3219c6dfec4b23ffe3000001005d37d472ac8a44023fbb3d18583a4f73
d3a076c0858f6f1dcaa39ea0f119911ff03f5406df4f7f27f41da8d7194f45c9f4e00f2e
df449f266253aa3f52e55c53de147773e00f0f9aca86c64d94c95382265968c354c5eab4
9638c5ae99c89de1e0997242edfb0b501744e11ff9762dfd951cffd93227cc513384e7e6
e782c29435c7ec2edafaa2f4c1fe6e7a781b59549ff5296371b42133777dcc5b8b971610
94bc67ede19e43ddb9dc2baacad374a36feaf0314d700af0a65c164b7082401740e489c9
7ef6a24defe4846104209bf0c3eced7fa1a672ed5b125fc9d8cd88b476a658a4434644ef
df8ae9a178e9f83ba9f08d10fa47e4226b98b0702f06b3b8
The initial consumer of trusted keys is EVM, which at boot time needs a high
quality symmetric key for HMAC protection of file metadata. The use of a
trusted key provides strong guarantees that the EVM key has not been
compromised by a user level problem, and when sealed to specific boot PCR
values, protects against boot and offline attacks. Create and save an
encrypted key "evm" using the above trusted key "kmk":
option 1: omitting 'format'
$ keyctl add encrypted evm "new trusted:kmk 32" #u
159771175
option 2: explicitly defining 'format' as 'default'
$ keyctl add encrypted evm "new default trusted:kmk 32" #u
159771175
$ keyctl print 159771175
default trusted:kmk 32 2375725ad57798846a9bbd240de8906f006e66c03af53b1b3
82dbbc55be2a44616e4959430436dc4f2a7a9659aa60bb4652aeb2120f149ed197c564e0
24717c64 5972dcb82ab2dde83376d82b2e3c09ffc
$ keyctl pipe 159771175 > evm.blob
Load an encrypted key "evm" from saved blob:
$ keyctl add encrypted evm "load `cat evm.blob`" #u
831684262
$ keyctl print 831684262
default trusted:kmk 32 2375725ad57798846a9bbd240de8906f006e66c03af53b1b3
82dbbc55be2a44616e4959430436dc4f2a7a9659aa60bb4652aeb2120f149ed197c564e0
24717c64 5972dcb82ab2dde83376d82b2e3c09ffc
Other uses for trusted and encrypted keys, such as for disk and file encryption
are anticipated. In particular the new format 'ecryptfs' has been defined in
in order to use encrypted keys to mount an eCryptfs filesystem. More details
about the usage can be found in the file
'Documentation/security/keys-ecryptfs.txt'.
When it says
specifying the HMAC key
it does NOT mean provide the HMAC key - it means to "point to the HMAC key that you want to use".
TPMs can use a virtually unlimited number of HMAC keys, as is pointed out in the book. You have to tell the TPM which one to use.

Categories

Resources