Skip to content

Commit 5b3eade

Browse files
committed
Fix for wolfTPM2_SetKeyAuthPassword that was truncating password to 2 chars. Added test to catch this and made sure there are no others. Bug introduced in PR #427 and release v3.9.2.
1 parent a88d7ba commit 5b3eade

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/tpm2_wrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ int wolfTPM2_SetKeyAuthPassword(WOLFTPM2_KEY *key, const byte* auth,
528528
}
529529

530530
/* specify auth password for storage key */
531-
if (authSz > (int)sizeof(key->handle.auth.size)) {
532-
authSz = (int)sizeof(key->handle.auth.size); /* truncate */
531+
if (authSz > (int)sizeof(key->handle.auth.buffer)) {
532+
authSz = (int)sizeof(key->handle.auth.buffer); /* truncate */
533533
}
534534
key->handle.auth.size = (UINT16)authSz;
535535
if (auth != NULL) {

wrapper/CSharp/wolfTPM-tests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ private void GenerateKey(string algorithm)
127127
Template template = new Template();
128128
byte[] blob_buffer = new byte[Device.MAX_KEYBLOB_BYTES];
129129

130+
Console.WriteLine("Generating {0} key", algorithm);
131+
130132
if (algorithm == "RSA")
131133
{
132134
rc = template.GetKeyTemplate_RSA((ulong)(
@@ -158,6 +160,7 @@ private void GenerateKey(string algorithm)
158160
rc = blob.GetKeyBlobAsBuffer(blob_buffer);
159161
if (rc > 0)
160162
{
163+
Console.WriteLine("Key Blob Size: {0} bytes", rc);
161164
Array.Resize(ref blob_buffer, rc);
162165
if (algorithm == "RSA")
163166
{
@@ -190,6 +193,8 @@ private void LoadGeneratedKey(string algorithm)
190193
KeyBlob blob = new KeyBlob();
191194
byte[] blob_buffer;
192195

196+
Console.WriteLine("Loading {0} key", algorithm);
197+
193198
if (algorithm == "RSA")
194199
{
195200
blob_buffer = generatedRSA;
@@ -213,6 +218,28 @@ private void LoadGeneratedKey(string algorithm)
213218
rc = blob.SetKeyAuthPassword("ThisIsMyKeyAuth");
214219
Assert.AreEqual((int)Status.TPM_RC_SUCCESS, rc);
215220

221+
/* Use key to make sure authentication works */
222+
if (algorithm == "RSA") {
223+
const int RsaKeySz = 256;
224+
const int HashDigestSz = 32;
225+
byte[] sig = new byte[RsaKeySz];
226+
byte[] digest = new byte[HashDigestSz];
227+
228+
/* Perform RSA sign / verify - PKCSv1.5 (SSA) padding */
229+
for (int i=0; i<digest.Length; i++) {
230+
digest[i] = 0x11;
231+
}
232+
rc = device.SignHashScheme(blob, digest, sig,
233+
TPM2_Alg.RSASSA, TPM2_Alg.SHA256);
234+
Assert.AreEqual(RsaKeySz, rc);
235+
236+
rc = device.VerifyHashScheme(blob, sig, digest,
237+
TPM2_Alg.RSASSA, TPM2_Alg.SHA256);
238+
Assert.AreEqual((int)Status.TPM_RC_SUCCESS, rc);
239+
240+
Console.WriteLine("RSA Sign/Verify Success");
241+
}
242+
216243
rc = device.UnloadHandle(blob);
217244
Assert.AreEqual((int)Status.TPM_RC_SUCCESS, rc);
218245
}

wrapper/CSharp/wolfTPM.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,13 @@ public class Device : IDisposable
903903

904904
const string DLLNAME = "wolftpm";
905905

906-
public const int MAX_KEYBLOB_BYTES = 1024;
906+
/* These "max" buffer sizes are used for testing only and may be larger
907+
* depending on actual platform. */
908+
/* Temporary buffer large enough for key blob public+private parts */
909+
public const int MAX_KEYBLOB_BYTES = 2048; /* MAX_CONTEXT_SIZE */
910+
/* Temporary buffer large enough for test CSR's */
907911
public const int MAX_TPM_BUFFER = 2048;
912+
908913
public const int INVALID_DEVID = -2;
909914
private IntPtr device = IntPtr.Zero;
910915

0 commit comments

Comments
 (0)