Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/crypt/AlgorithmTests.c

raw
1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 //** Introduction
36 // This file contains the code to perform the various self-test functions.
37 //
38 // NOTE: In this implementation, large local variables are made static to minimize
39 // stack usage, which is critical for stack-constrained platforms.
40
41 //** Includes and Defines
42 #include "Tpm.h"
43
44 #define SELF_TEST_DATA
45
46 #if SELF_TEST
47
48 // These includes pull in the data structures. They contain data definitions for the
49 // various tests.
50 #include "SelfTest.h"
51 #include "SymmetricTest.h"
52 #include "RsaTestData.h"
53 #include "EccTestData.h"
54 #include "HashTestData.h"
55 #include "KdfTestData.h"
56
57 #define TEST_DEFAULT_TEST_HASH(vector) \
58 if(TEST_BIT(DEFAULT_TEST_HASH, g_toTest)) \
59 TestHash(DEFAULT_TEST_HASH, vector);
60
61 // Make sure that the algorithm has been tested
62 #define CLEAR_BOTH(alg) { CLEAR_BIT(alg, *toTest); \
63 if(toTest != &g_toTest) \
64 CLEAR_BIT(alg, g_toTest); }
65
66 #define SET_BOTH(alg) { SET_BIT(alg, *toTest); \
67 if(toTest != &g_toTest) \
68 SET_BIT(alg, g_toTest); }
69
70 #define TEST_BOTH(alg) ((toTest != &g_toTest) \
71 ? TEST_BIT(alg, *toTest) || TEST_BIT(alg, g_toTest) \
72 : TEST_BIT(alg, *toTest))
73
74 // Can only cancel if doing a list.
75 #define CHECK_CANCELED \
76 if(_plat__IsCanceled() && toTest != &g_toTest) \
77 return TPM_RC_CANCELED;
78
79 //** Hash Tests
80
81 //*** Description
82 // The hash test does a known-value HMAC using the specified hash algorithm.
83
84 //*** TestHash()
85 // The hash test function.
86 static TPM_RC
87 TestHash(
88 TPM_ALG_ID hashAlg,
89 ALGORITHM_VECTOR *toTest
90 )
91 {
92 static TPM2B_DIGEST computed; // value computed
93 static HMAC_STATE state;
94 UINT16 digestSize;
95 const TPM2B *testDigest = NULL;
96 // TPM2B_TYPE(HMAC_BLOCK, DEFAULT_TEST_HASH_BLOCK_SIZE);
97
98 pAssert(hashAlg != ALG_NULL_VALUE);
99 switch(hashAlg)
100 {
101 #if ALG_SHA1
102 case ALG_SHA1_VALUE:
103 testDigest = &c_SHA1_digest.b;
104 break;
105 #endif
106 #if ALG_SHA256
107 case ALG_SHA256_VALUE:
108 testDigest = &c_SHA256_digest.b;
109 break;
110 #endif
111 #if ALG_SHA384
112 case ALG_SHA384_VALUE:
113 testDigest = &c_SHA384_digest.b;
114 break;
115 #endif
116 #if ALG_SHA512
117 case ALG_SHA512_VALUE:
118 testDigest = &c_SHA512_digest.b;
119 break;
120 #endif
121 #if ALG_SM3_256
122 case ALG_SM3_256_VALUE:
123 testDigest = &c_SM3_256_digest.b;
124 break;
125 #endif
126 default:
127 FAIL(FATAL_ERROR_INTERNAL);
128 }
129 // Clear the to-test bits
130 CLEAR_BOTH(hashAlg);
131
132 // Set the HMAC key to twice the digest size
133 digestSize = CryptHashGetDigestSize(hashAlg);
134 CryptHmacStart(&state, hashAlg, digestSize * 2,
135 (BYTE *)c_hashTestKey.t.buffer);
136 CryptDigestUpdate(&state.hashState, 2 * CryptHashGetBlockSize(hashAlg),
137 (BYTE *)c_hashTestData.t.buffer);
138 computed.t.size = digestSize;
139 CryptHmacEnd(&state, digestSize, computed.t.buffer);
140 if((testDigest->size != computed.t.size)
141 || (memcmp(testDigest->buffer, computed.t.buffer, computed.b.size) != 0))
142 SELF_TEST_FAILURE;
143 return TPM_RC_SUCCESS;
144 }
145
146 //** Symmetric Test Functions
147
148 //*** MakeIv()
149 // Internal function to make the appropriate IV depending on the mode.
150 static UINT32
151 MakeIv(
152 TPM_ALG_ID mode, // IN: symmetric mode
153 UINT32 size, // IN: block size of the algorithm
154 BYTE *iv // OUT: IV to fill in
155 )
156 {
157 BYTE i;
158
159 if(mode == ALG_ECB_VALUE)
160 return 0;
161 if(mode == ALG_CTR_VALUE)
162 {
163 // The test uses an IV that has 0xff in the last byte
164 for(i = 1; i <= size; i++)
165 *iv++ = 0xff - (BYTE)(size - i);
166 }
167 else
168 {
169 for(i = 0; i < size; i++)
170 *iv++ = i;
171 }
172 return size;
173 }
174
175 //*** TestSymmetricAlgorithm()
176 // Function to test a specific algorithm, key size, and mode.
177 static void
178 TestSymmetricAlgorithm(
179 const SYMMETRIC_TEST_VECTOR *test, //
180 TPM_ALG_ID mode //
181 )
182 {
183 static BYTE encrypted[MAX_SYM_BLOCK_SIZE * 2];
184 static BYTE decrypted[MAX_SYM_BLOCK_SIZE * 2];
185 static TPM2B_IV iv;
186 //
187 // Get the appropriate IV
188 iv.t.size = (UINT16)MakeIv(mode, test->ivSize, iv.t.buffer);
189
190 // Encrypt known data
191 CryptSymmetricEncrypt(encrypted, test->alg, test->keyBits, test->key, &iv,
192 mode, test->dataInOutSize, test->dataIn);
193 // Check that it matches the expected value
194 if(!MemoryEqual(encrypted, test->dataOut[mode - ALG_CTR_VALUE],
195 test->dataInOutSize))
196 SELF_TEST_FAILURE;
197 // Reinitialize the iv for decryption
198 MakeIv(mode, test->ivSize, iv.t.buffer);
199 CryptSymmetricDecrypt(decrypted, test->alg, test->keyBits, test->key, &iv,
200 mode, test->dataInOutSize,
201 test->dataOut[mode - ALG_CTR_VALUE]);
202 // Make sure that it matches what we started with
203 if(!MemoryEqual(decrypted, test->dataIn, test->dataInOutSize))
204 SELF_TEST_FAILURE;
205 }
206
207 //*** AllSymsAreDone()
208 // Checks if both symmetric algorithms have been tested. This is put here
209 // so that addition of a symmetric algorithm will be relatively easy to handle
210 // Return Type: BOOL
211 // TRUE(1) all symmetric algorithms tested
212 // FALSE(0) not all symmetric algorithms tested
213 static BOOL
214 AllSymsAreDone(
215 ALGORITHM_VECTOR *toTest
216 )
217 {
218 return (!TEST_BOTH(ALG_AES_VALUE) && !TEST_BOTH(ALG_SM4_VALUE));
219 }
220
221 //*** AllModesAreDone()
222 // Checks if all the modes have been tested
223 // Return Type: BOOL
224 // TRUE(1) all modes tested
225 // FALSE(0) all modes not tested
226 static BOOL
227 AllModesAreDone(
228 ALGORITHM_VECTOR *toTest
229 )
230 {
231 TPM_ALG_ID alg;
232 for(alg = TPM_SYM_MODE_FIRST; alg <= TPM_SYM_MODE_LAST; alg++)
233 if(TEST_BOTH(alg))
234 return FALSE;
235 return TRUE;
236 }
237
238 //*** TestSymmetric()
239 // If 'alg' is a symmetric block cipher, then all of the modes that are selected are
240 // tested. If 'alg' is a mode, then all algorithms of that mode are tested.
241 static TPM_RC
242 TestSymmetric(
243 TPM_ALG_ID alg,
244 ALGORITHM_VECTOR *toTest
245 )
246 {
247 SYM_INDEX index;
248 TPM_ALG_ID mode;
249 //
250 if(!TEST_BIT(alg, *toTest))
251 return TPM_RC_SUCCESS;
252 if(alg == ALG_AES_VALUE || alg == ALG_SM4_VALUE || alg == ALG_CAMELLIA_VALUE)
253 {
254 // Will test the algorithm for all modes and key sizes
255 CLEAR_BOTH(alg);
256
257 // A test this algorithm for all modes
258 for(index = 0; index < NUM_SYMS; index++)
259 {
260 if(c_symTestValues[index].alg == alg)
261 {
262 for(mode = TPM_SYM_MODE_FIRST;
263 mode <= TPM_SYM_MODE_LAST;
264 mode++)
265 {
266 if(TEST_BIT(mode, *toTest))
267 TestSymmetricAlgorithm(&c_symTestValues[index], mode);
268 }
269 }
270 }
271 // if all the symmetric tests are done
272 if(AllSymsAreDone(toTest))
273 {
274 // all symmetric algorithms tested so no modes should be set
275 for(alg = TPM_SYM_MODE_FIRST; alg <= TPM_SYM_MODE_LAST; alg++)
276 CLEAR_BOTH(alg);
277 }
278 }
279 else if(TPM_SYM_MODE_FIRST <= alg && alg <= TPM_SYM_MODE_LAST)
280 {
281 // Test this mode for all key sizes and algorithms
282 for(index = 0; index < NUM_SYMS; index++)
283 {
284 // The mode testing only comes into play when doing self tests
285 // by command. When doing self tests by command, the block ciphers are
286 // tested first. That means that all of their modes would have been
287 // tested for all key sizes. If there is no block cipher left to
288 // test, then clear this mode bit.
289 if(!TEST_BIT(ALG_AES_VALUE, *toTest)
290 && !TEST_BIT(ALG_SM4_VALUE, *toTest))
291 {
292 CLEAR_BOTH(alg);
293 }
294 else
295 {
296 for(index = 0; index < NUM_SYMS; index++)
297 {
298 if(TEST_BIT(c_symTestValues[index].alg, *toTest))
299 TestSymmetricAlgorithm(&c_symTestValues[index], alg);
300 }
301 // have tested this mode for all algorithms
302 CLEAR_BOTH(alg);
303 }
304 }
305 if(AllModesAreDone(toTest))
306 {
307 CLEAR_BOTH(ALG_AES_VALUE);
308 CLEAR_BOTH(ALG_SM4_VALUE);
309 }
310 }
311 else
312 pAssert(alg == 0 && alg != 0);
313 return TPM_RC_SUCCESS;
314 }
315
316 //** RSA Tests
317 #if ALG_RSA
318
319 //*** Introduction
320 // The tests are for public key only operations and for private key operations.
321 // Signature verification and encryption are public key operations. They are tested
322 // by using a KVT. For signature verification, this means that a known good
323 // signature is checked by CryptRsaValidateSignature(). If it fails, then the
324 // TPM enters failure mode. For encryption, the TPM encrypts known values using
325 // the selected scheme and checks that the returned value matches the expected
326 // value.
327 //
328 // For private key operations, a full scheme check is used. For a signing key, a
329 // known key is used to sign a known message. Then that signature is verified.
330 // since the signature may involve use of random values, the signature will be
331 // different each time and we can't always check that the signature matches a
332 // known value. The same technique is used for decryption (RSADP/RSAEP).
333 //
334 // When an operation uses the public key and the verification has not been
335 // tested, the TPM will do a KVT.
336 //
337 // The test for the signing algorithm is built into the call for the algorithm
338
339 //*** RsaKeyInitialize()
340 // The test key is defined by a public modulus and a private prime. The TPM's RSA
341 // code computes the second prime and the private exponent.
342 static void
343 RsaKeyInitialize(
344 OBJECT *testObject
345 )
346 {
347 MemoryCopy2B(&testObject->publicArea.unique.rsa.b, (P2B)&c_rsaPublicModulus,
348 sizeof(c_rsaPublicModulus));
349 MemoryCopy2B(&testObject->sensitive.sensitive.rsa.b, (P2B)&c_rsaPrivatePrime,
350 sizeof(testObject->sensitive.sensitive.rsa.t.buffer));
351 testObject->publicArea.parameters.rsaDetail.keyBits = RSA_TEST_KEY_SIZE * 8;
352 // Use the default exponent
353 testObject->publicArea.parameters.rsaDetail.exponent = 0;
354 }
355
356 //*** TestRsaEncryptDecrypt()
357 // These tests are for a public key encryption that uses a random value.
358 static TPM_RC
359 TestRsaEncryptDecrypt(
360 TPM_ALG_ID scheme, // IN: the scheme
361 ALGORITHM_VECTOR *toTest //
362 )
363 {
364 static TPM2B_PUBLIC_KEY_RSA testInput;
365 static TPM2B_PUBLIC_KEY_RSA testOutput;
366 static OBJECT testObject;
367 const TPM2B_RSA_TEST_KEY *kvtValue = NULL;
368 TPM_RC result = TPM_RC_SUCCESS;
369 const TPM2B *testLabel = NULL;
370 TPMT_RSA_DECRYPT rsaScheme;
371 //
372 // Don't need to initialize much of the test object
373 RsaKeyInitialize(&testObject);
374 rsaScheme.scheme = scheme;
375 rsaScheme.details.anySig.hashAlg = DEFAULT_TEST_HASH;
376 CLEAR_BOTH(scheme);
377 CLEAR_BOTH(ALG_NULL_VALUE);
378 if(scheme == ALG_NULL_VALUE)
379 {
380 // This is an encryption scheme using the private key without any encoding.
381 memcpy(testInput.t.buffer, c_RsaTestValue, sizeof(c_RsaTestValue));
382 testInput.t.size = sizeof(c_RsaTestValue);
383 if(TPM_RC_SUCCESS != CryptRsaEncrypt(&testOutput, &testInput.b,
384 &testObject, &rsaScheme, NULL, NULL))
385 SELF_TEST_FAILURE;
386 if(!MemoryEqual(testOutput.t.buffer, c_RsaepKvt.buffer, c_RsaepKvt.size))
387 SELF_TEST_FAILURE;
388 MemoryCopy2B(&testInput.b, &testOutput.b, sizeof(testInput.t.buffer));
389 if(TPM_RC_SUCCESS != CryptRsaDecrypt(&testOutput.b, &testInput.b,
390 &testObject, &rsaScheme, NULL))
391 SELF_TEST_FAILURE;
392 if(!MemoryEqual(testOutput.t.buffer, c_RsaTestValue,
393 sizeof(c_RsaTestValue)))
394 SELF_TEST_FAILURE;
395 }
396 else
397 {
398 // ALG_RSAES_VALUE:
399 // This is an decryption scheme using padding according to
400 // PKCS#1v2.1, 7.2. This padding uses random bits. To test a public
401 // key encryption that uses random data, encrypt a value and then
402 // decrypt the value and see that we get the encrypted data back.
403 // The hash is not used by this encryption so it can be TMP_ALG_NULL
404
405 // ALG_OAEP_VALUE:
406 // This is also an decryption scheme and it also uses a
407 // pseudo-random
408 // value. However, this also uses a hash algorithm. So, we may need
409 // to test that algorithm before use.
410 if(scheme == ALG_OAEP_VALUE)
411 {
412 TEST_DEFAULT_TEST_HASH(toTest);
413 kvtValue = &c_OaepKvt;
414 testLabel = OAEP_TEST_STRING;
415 }
416 else if(scheme == ALG_RSAES_VALUE)
417 {
418 kvtValue = &c_RsaesKvt;
419 testLabel = NULL;
420 }
421 else
422 SELF_TEST_FAILURE;
423 // Only use a digest-size portion of the test value
424 memcpy(testInput.t.buffer, c_RsaTestValue, DEFAULT_TEST_DIGEST_SIZE);
425 testInput.t.size = DEFAULT_TEST_DIGEST_SIZE;
426
427 // See if the encryption works
428 if(TPM_RC_SUCCESS != CryptRsaEncrypt(&testOutput, &testInput.b,
429 &testObject, &rsaScheme, testLabel,
430 NULL))
431 SELF_TEST_FAILURE;
432 MemoryCopy2B(&testInput.b, &testOutput.b, sizeof(testInput.t.buffer));
433 // see if we can decrypt this value and get the original data back
434 if(TPM_RC_SUCCESS != CryptRsaDecrypt(&testOutput.b, &testInput.b,
435 &testObject, &rsaScheme, testLabel))
436 SELF_TEST_FAILURE;
437 // See if the results compare
438 if(testOutput.t.size != DEFAULT_TEST_DIGEST_SIZE
439 || !MemoryEqual(testOutput.t.buffer, c_RsaTestValue,
440 DEFAULT_TEST_DIGEST_SIZE))
441 SELF_TEST_FAILURE;
442 // Now check that the decryption works on a known value
443 MemoryCopy2B(&testInput.b, (P2B)kvtValue,
444 sizeof(testInput.t.buffer));
445 if(TPM_RC_SUCCESS != CryptRsaDecrypt(&testOutput.b, &testInput.b,
446 &testObject, &rsaScheme, testLabel))
447 SELF_TEST_FAILURE;
448 if(testOutput.t.size != DEFAULT_TEST_DIGEST_SIZE
449 || !MemoryEqual(testOutput.t.buffer, c_RsaTestValue,
450 DEFAULT_TEST_DIGEST_SIZE))
451 SELF_TEST_FAILURE;
452 }
453 return result;
454 }
455
456 //*** TestRsaSignAndVerify()
457 // This function does the testing of the RSA sign and verification functions. This
458 // test does a KVT.
459 static TPM_RC
460 TestRsaSignAndVerify(
461 TPM_ALG_ID scheme,
462 ALGORITHM_VECTOR *toTest
463 )
464 {
465 TPM_RC result = TPM_RC_SUCCESS;
466 static OBJECT testObject;
467 static TPM2B_DIGEST testDigest;
468 static TPMT_SIGNATURE testSig;
469
470 // Do a sign and signature verification.
471 // RSASSA:
472 // This is a signing scheme according to PKCS#1-v2.1 8.2. It does not
473 // use random data so there is a KVT for the signing operation. On
474 // first use of the scheme for signing, use the TPM's RSA key to
475 // sign a portion of c_RsaTestData and compare the results to c_RsassaKvt. Then
476 // decrypt the data to see that it matches the starting value. This verifies
477 // the signature with a KVT
478
479 // Clear the bits indicating that the function has not been checked. This is to
480 // prevent looping
481 CLEAR_BOTH(scheme);
482 CLEAR_BOTH(ALG_NULL_VALUE);
483 CLEAR_BOTH(ALG_RSA_VALUE);
484
485 RsaKeyInitialize(&testObject);
486 memcpy(testDigest.t.buffer, (BYTE *)c_RsaTestValue, DEFAULT_TEST_DIGEST_SIZE);
487 testDigest.t.size = DEFAULT_TEST_DIGEST_SIZE;
488 testSig.sigAlg = scheme;
489 testSig.signature.rsapss.hash = DEFAULT_TEST_HASH;
490
491 // RSAPSS:
492 // This is a signing scheme a according to PKCS#1-v2.2 8.1 it uses
493 // random data in the signature so there is no KVT for the signing
494 // operation. To test signing, the TPM will use the TPM's RSA key
495 // to sign a portion of c_RsaTestValue and then it will verify the
496 // signature. For verification, c_RsapssKvt is verified before the
497 // user signature blob is verified. The worst case for testing of this
498 // algorithm is two private and one public key operation.
499
500 // The process is to sign known data. If RSASSA is being done, verify that the
501 // signature matches the precomputed value. For both, use the signed value and
502 // see that the verification says that it is a good signature. Then
503 // if testing RSAPSS, do a verify of a known good signature. This ensures that
504 // the validation function works.
505
506 if(TPM_RC_SUCCESS != CryptRsaSign(&testSig, &testObject, &testDigest, NULL))
507 SELF_TEST_FAILURE;
508 // For RSASSA, make sure the results is what we are looking for
509 if(testSig.sigAlg == ALG_RSASSA_VALUE)
510 {
511 if(testSig.signature.rsassa.sig.t.size != RSA_TEST_KEY_SIZE
512 || !MemoryEqual(c_RsassaKvt.buffer,
513 testSig.signature.rsassa.sig.t.buffer,
514 RSA_TEST_KEY_SIZE))
515 SELF_TEST_FAILURE;
516 }
517 // See if the TPM will validate its own signatures
518 if(TPM_RC_SUCCESS != CryptRsaValidateSignature(&testSig, &testObject,
519 &testDigest))
520 SELF_TEST_FAILURE;
521 // If this is RSAPSS, check the verification with known signature
522 // Have to copy because CrytpRsaValidateSignature() eats the signature
523 if(ALG_RSAPSS_VALUE == scheme)
524 {
525 MemoryCopy2B(&testSig.signature.rsapss.sig.b, (P2B)&c_RsapssKvt,
526 sizeof(testSig.signature.rsapss.sig.t.buffer));
527 if(TPM_RC_SUCCESS != CryptRsaValidateSignature(&testSig, &testObject,
528 &testDigest))
529 SELF_TEST_FAILURE;
530 }
531 return result;
532 }
533
534 //*** TestRSA()
535 // Function uses the provided vector to indicate which tests to run. It will clear
536 // the vector after each test is run and also clear g_toTest
537 static TPM_RC
538 TestRsa(
539 TPM_ALG_ID alg,
540 ALGORITHM_VECTOR *toTest
541 )
542 {
543 TPM_RC result = TPM_RC_SUCCESS;
544 //
545 switch(alg)
546 {
547 case ALG_NULL_VALUE:
548 // This is the RSAEP/RSADP function. If we are processing a list, don't
549 // need to test these now because any other test will validate
550 // RSAEP/RSADP. Can tell this is list of test by checking to see if
551 // 'toTest' is pointing at g_toTest. If so, this is an isolated test
552 // an need to go ahead and do the test;
553 if((toTest == &g_toTest)
554 || (!TEST_BIT(ALG_RSASSA_VALUE, *toTest)
555 && !TEST_BIT(ALG_RSAES_VALUE, *toTest)
556 && !TEST_BIT(ALG_RSAPSS_VALUE, *toTest)
557 && !TEST_BIT(ALG_OAEP_VALUE, *toTest)))
558 // Not running a list of tests or no other tests on the list
559 // so run the test now
560 result = TestRsaEncryptDecrypt(alg, toTest);
561 // if not running the test now, leave the bit on, just in case things
562 // get interrupted
563 break;
564 case ALG_OAEP_VALUE:
565 case ALG_RSAES_VALUE:
566 result = TestRsaEncryptDecrypt(alg, toTest);
567 break;
568 case ALG_RSAPSS_VALUE:
569 case ALG_RSASSA_VALUE:
570 result = TestRsaSignAndVerify(alg, toTest);
571 break;
572 default:
573 SELF_TEST_FAILURE;
574 }
575 return result;
576 }
577
578 #endif // ALG_RSA
579
580 //** ECC Tests
581
582 #if ALG_ECC
583
584 //*** LoadEccParameter()
585 // This function is mostly for readability and type checking
586 static void
587 LoadEccParameter(
588 TPM2B_ECC_PARAMETER *to, // target
589 const TPM2B_EC_TEST *from // source
590 )
591 {
592 MemoryCopy2B(&to->b, &from->b, sizeof(to->t.buffer));
593 }
594
595 //*** LoadEccPoint()
596 static void
597 LoadEccPoint(
598 TPMS_ECC_POINT *point, // target
599 const TPM2B_EC_TEST *x, // source
600 const TPM2B_EC_TEST *y
601 )
602 {
603 MemoryCopy2B(&point->x.b, (TPM2B *)x, sizeof(point->x.t.buffer));
604 MemoryCopy2B(&point->y.b, (TPM2B *)y, sizeof(point->y.t.buffer));
605 }
606
607 //*** TestECDH()
608 // This test does a KVT on a point multiply.
609 static TPM_RC
610 TestECDH(
611 TPM_ALG_ID scheme, // IN: for consistency
612 ALGORITHM_VECTOR *toTest // IN/OUT: modified after test is run
613 )
614 {
615 static TPMS_ECC_POINT Z;
616 static TPMS_ECC_POINT Qe;
617 static TPM2B_ECC_PARAMETER ds;
618 TPM_RC result = TPM_RC_SUCCESS;
619 //
620 NOT_REFERENCED(scheme);
621 CLEAR_BOTH(ALG_ECDH_VALUE);
622 LoadEccParameter(&ds, &c_ecTestKey_ds);
623 LoadEccPoint(&Qe, &c_ecTestKey_QeX, &c_ecTestKey_QeY);
624 if(TPM_RC_SUCCESS != CryptEccPointMultiply(&Z, c_testCurve, &Qe, &ds,
625 NULL, NULL))
626 SELF_TEST_FAILURE;
627 if(!MemoryEqual2B(&c_ecTestEcdh_X.b, &Z.x.b)
628 || !MemoryEqual2B(&c_ecTestEcdh_Y.b, &Z.y.b))
629 SELF_TEST_FAILURE;
630 return result;
631 }
632
633 //*** TestEccSignAndVerify()
634 static TPM_RC
635 TestEccSignAndVerify(
636 TPM_ALG_ID scheme,
637 ALGORITHM_VECTOR *toTest
638 )
639 {
640 static OBJECT testObject;
641 static TPMT_SIGNATURE testSig;
642 static TPMT_ECC_SCHEME eccScheme;
643
644 testSig.sigAlg = scheme;
645 testSig.signature.ecdsa.hash = DEFAULT_TEST_HASH;
646
647 eccScheme.scheme = scheme;
648 eccScheme.details.anySig.hashAlg = DEFAULT_TEST_HASH;
649
650 CLEAR_BOTH(scheme);
651 CLEAR_BOTH(ALG_ECDH_VALUE);
652
653 // ECC signature verification testing uses a KVT.
654 switch(scheme)
655 {
656 case ALG_ECDSA_VALUE:
657 LoadEccParameter(&testSig.signature.ecdsa.signatureR, &c_TestEcDsa_r);
658 LoadEccParameter(&testSig.signature.ecdsa.signatureS, &c_TestEcDsa_s);
659 break;
660 case ALG_ECSCHNORR_VALUE:
661 LoadEccParameter(&testSig.signature.ecschnorr.signatureR,
662 &c_TestEcSchnorr_r);
663 LoadEccParameter(&testSig.signature.ecschnorr.signatureS,
664 &c_TestEcSchnorr_s);
665 break;
666 case ALG_SM2_VALUE:
667 // don't have a test for SM2
668 return TPM_RC_SUCCESS;
669 default:
670 SELF_TEST_FAILURE;
671 break;
672 }
673 TEST_DEFAULT_TEST_HASH(toTest);
674
675 // Have to copy the key. This is because the size used in the test vectors
676 // is the size of the ECC parameter for the test key while the size of a point
677 // is TPM dependent
678 MemoryCopy2B(&testObject.sensitive.sensitive.ecc.b, &c_ecTestKey_ds.b,
679 sizeof(testObject.sensitive.sensitive.ecc.t.buffer));
680 LoadEccPoint(&testObject.publicArea.unique.ecc, &c_ecTestKey_QsX,
681 &c_ecTestKey_QsY);
682 testObject.publicArea.parameters.eccDetail.curveID = c_testCurve;
683
684 if(TPM_RC_SUCCESS != CryptEccValidateSignature(&testSig, &testObject,
685 (TPM2B_DIGEST *)&c_ecTestValue.b))
686 {
687 SELF_TEST_FAILURE;
688 }
689 CHECK_CANCELED;
690
691 // Now sign and verify some data
692 if(TPM_RC_SUCCESS != CryptEccSign(&testSig, &testObject,
693 (TPM2B_DIGEST *)&c_ecTestValue,
694 &eccScheme, NULL))
695 SELF_TEST_FAILURE;
696
697 CHECK_CANCELED;
698
699 if(TPM_RC_SUCCESS != CryptEccValidateSignature(&testSig, &testObject,
700 (TPM2B_DIGEST *)&c_ecTestValue))
701 SELF_TEST_FAILURE;
702
703 CHECK_CANCELED;
704
705 return TPM_RC_SUCCESS;
706 }
707
708 //*** TestKDFa()
709 static TPM_RC
710 TestKDFa(
711 ALGORITHM_VECTOR *toTest
712 )
713 {
714 static TPM2B_KDF_TEST_KEY keyOut;
715 UINT32 counter = 0;
716 //
717 CLEAR_BOTH(ALG_KDF1_SP800_108_VALUE);
718
719 keyOut.t.size = CryptKDFa(KDF_TEST_ALG, &c_kdfTestKeyIn.b, &c_kdfTestLabel.b,
720 &c_kdfTestContextU.b, &c_kdfTestContextV.b,
721 TEST_KDF_KEY_SIZE * 8, keyOut.t.buffer,
722 &counter, FALSE);
723 if ( keyOut.t.size != TEST_KDF_KEY_SIZE
724 || !MemoryEqual(keyOut.t.buffer, c_kdfTestKeyOut.t.buffer,
725 TEST_KDF_KEY_SIZE))
726 SELF_TEST_FAILURE;
727
728 return TPM_RC_SUCCESS;
729 }
730
731 //*** TestEcc()
732 static TPM_RC
733 TestEcc(
734 TPM_ALG_ID alg,
735 ALGORITHM_VECTOR *toTest
736 )
737 {
738 TPM_RC result = TPM_RC_SUCCESS;
739 NOT_REFERENCED(toTest);
740 switch(alg)
741 {
742 case ALG_ECC_VALUE:
743 case ALG_ECDH_VALUE:
744 // If this is in a loop then see if another test is going to deal with
745 // this.
746 // If toTest is not a self-test list
747 if((toTest == &g_toTest)
748 // or this is the only ECC test in the list
749 || !(TEST_BIT(ALG_ECDSA_VALUE, *toTest)
750 || TEST_BIT(ALG_ECSCHNORR, *toTest)
751 || TEST_BIT(ALG_SM2_VALUE, *toTest)))
752 {
753 result = TestECDH(alg, toTest);
754 }
755 break;
756 case ALG_ECDSA_VALUE:
757 case ALG_ECSCHNORR_VALUE:
758 case ALG_SM2_VALUE:
759 result = TestEccSignAndVerify(alg, toTest);
760 break;
761 default:
762 SELF_TEST_FAILURE;
763 break;
764 }
765 return result;
766 }
767
768 #endif // ALG_ECC
769
770 //*** TestAlgorithm()
771 // Dispatches to the correct test function for the algorithm or gets a list of
772 // testable algorithms.
773 //
774 // If 'toTest' is not NULL, then the test decisions are based on the algorithm
775 // selections in 'toTest'. Otherwise, 'g_toTest' is used. When bits are clear in
776 // 'g_toTest' they will also be cleared 'toTest'.
777 //
778 // If there doesn't happen to be a test for the algorithm, its associated bit is
779 // quietly cleared.
780 //
781 // If 'alg' is zero (TPM_ALG_ERROR), then the toTest vector is cleared of any bits
782 // for which there is no test (i.e. no tests are actually run but the vector is
783 // cleared).
784 //
785 // Note: 'toTest' will only ever have bits set for implemented algorithms but 'alg'
786 // can be anything.
787 // Return Type: TPM_RC
788 // TPM_RC_CANCELED test was canceled
789 LIB_EXPORT
790 TPM_RC
791 TestAlgorithm(
792 TPM_ALG_ID alg,
793 ALGORITHM_VECTOR *toTest
794 )
795 {
796 TPM_ALG_ID first = (alg == ALG_ERROR_VALUE) ? ALG_FIRST_VALUE : alg;
797 TPM_ALG_ID last = (alg == ALG_ERROR_VALUE) ? ALG_LAST_VALUE : alg;
798 BOOL doTest = (alg != ALG_ERROR_VALUE);
799 TPM_RC result = TPM_RC_SUCCESS;
800
801 if(toTest == NULL)
802 toTest = &g_toTest;
803
804 // This is kind of strange. This function will either run a test of the selected
805 // algorithm or just clear a bit if there is no test for the algorithm. So,
806 // either this loop will be executed once for the selected algorithm or once for
807 // each of the possible algorithms. If it is executed more than once ('alg' ==
808 // ALG_ERROR), then no test will be run but bits will be cleared for
809 // unimplemented algorithms. This was done this way so that there is only one
810 // case statement with all of the algorithms. It was easier to have one case
811 // statement than to have multiple ones to manage whenever an algorithm ID is
812 // added.
813 for(alg = first; (alg <= last); alg++)
814 {
815 // if 'alg' was TPM_ALG_ERROR, then we will be cycling through
816 // values, some of which may not be implemented. If the bit in toTest
817 // happens to be set, then we could either generated an assert, or just
818 // silently CLEAR it. Decided to just clear.
819 if(!TEST_BIT(alg, g_implementedAlgorithms))
820 {
821 CLEAR_BIT(alg, *toTest);
822 continue;
823 }
824 // Process whatever is left.
825 // NOTE: since this switch will only be called if the algorithm is
826 // implemented, it is not necessary to modify this list except to comment
827 // out the algorithms for which there is no test
828 switch(alg)
829 {
830 // Symmetric block ciphers
831 #if ALG_AES
832 case ALG_AES_VALUE:
833 #endif // ALG_AES
834 #if ALG_SM4
835 // if SM4 is implemented, its test is like other block ciphers but there
836 // aren't any test vectors for it yet
837 // case ALG_SM4_VALUE:
838 #endif // ALG_SM4
839 #if ALG_CAMELLIA
840 // no test vectors for camellia
841 // case ALG_CAMELLIA_VALUE:
842 #endif
843 // Symmetric modes
844 #if !ALG_CFB
845 # error CFB is required in all TPM implementations
846 #endif // !ALG_CFB
847 case ALG_CFB_VALUE:
848 if(doTest)
849 result = TestSymmetric(alg, toTest);
850 break;
851 #if ALG_CTR
852 case ALG_CTR_VALUE:
853 #endif // ALG_CRT
854 #if ALG_OFB
855 case ALG_OFB_VALUE:
856 #endif // ALG_OFB
857 #if ALG_CBC
858 case ALG_CBC_VALUE:
859 #endif // ALG_CBC
860 #if ALG_ECB
861 case ALG_ECB_VALUE:
862 #endif
863 if(doTest)
864 result = TestSymmetric(alg, toTest);
865 else
866 // If doing the initialization of g_toTest vector, only need
867 // to test one of the modes for the symmetric algorithms. If
868 // initializing for a SelfTest(FULL_TEST), allow all the modes.
869 if(toTest == &g_toTest)
870 CLEAR_BIT(alg, *toTest);
871 break;
872 #if !ALG_HMAC
873 # error HMAC is required in all TPM implementations
874 #endif
875 case ALG_HMAC_VALUE:
876 // Clear the bit that indicates that HMAC is required because
877 // HMAC is used as the basic test for all hash algorithms.
878 CLEAR_BOTH(alg);
879 // Testing HMAC means test the default hash
880 if(doTest)
881 TestHash(DEFAULT_TEST_HASH, toTest);
882 else
883 // If not testing, then indicate that the hash needs to be
884 // tested because this uses HMAC
885 SET_BOTH(DEFAULT_TEST_HASH);
886 break;
887 #if ALG_SHA1
888 case ALG_SHA1_VALUE:
889 #endif // ALG_SHA1
890 #if ALG_SHA256
891 case ALG_SHA256_VALUE:
892 #endif // ALG_SHA256
893 #if ALG_SHA384
894 case ALG_SHA384_VALUE:
895 #endif // ALG_SHA384
896 #if ALG_SHA512
897 case ALG_SHA512_VALUE:
898 #endif // ALG_SHA512
899 // if SM3 is implemented its test is like any other hash, but there
900 // aren't any test vectors yet.
901 #if ALG_SM3_256
902 // case ALG_SM3_256_VALUE:
903 #endif // ALG_SM3_256
904 if(doTest)
905 result = TestHash(alg, toTest);
906 break;
907 // RSA-dependent
908 #if ALG_RSA
909 case ALG_RSA_VALUE:
910 CLEAR_BOTH(alg);
911 if(doTest)
912 result = TestRsa(ALG_NULL_VALUE, toTest);
913 else
914 SET_BOTH(ALG_NULL_VALUE);
915 break;
916 case ALG_RSASSA_VALUE:
917 case ALG_RSAES_VALUE:
918 case ALG_RSAPSS_VALUE:
919 case ALG_OAEP_VALUE:
920 case ALG_NULL_VALUE: // used or RSADP
921 if(doTest)
922 result = TestRsa(alg, toTest);
923 break;
924 #endif // ALG_RSA
925 #if ALG_KDF1_SP800_108
926 case ALG_KDF1_SP800_108_VALUE:
927 if(doTest)
928 result = TestKDFa(toTest);
929 break;
930 #endif // ALG_KDF1_SP800_108
931 #if ALG_ECC
932 // ECC dependent but no tests
933 // case ALG_ECDAA_VALUE:
934 // case ALG_ECMQV_VALUE:
935 // case ALG_KDF1_SP800_56a_VALUE:
936 // case ALG_KDF2_VALUE:
937 // case ALG_MGF1_VALUE:
938 case ALG_ECC_VALUE:
939 CLEAR_BOTH(alg);
940 if(doTest)
941 result = TestEcc(ALG_ECDH_VALUE, toTest);
942 else
943 SET_BOTH(ALG_ECDH_VALUE);
944 break;
945 case ALG_ECDSA_VALUE:
946 case ALG_ECDH_VALUE:
947 case ALG_ECSCHNORR_VALUE:
948 // case ALG_SM2_VALUE:
949 if(doTest)
950 result = TestEcc(alg, toTest);
951 break;
952 #endif // ALG_ECC
953 default:
954 CLEAR_BIT(alg, *toTest);
955 break;
956 }
957 if(result != TPM_RC_SUCCESS)
958 break;
959 }
960 return result;
961 }
962
963 #endif // SELF_TESTS