xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/src/crypt/CryptSelfTest.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Introduction
36*5c591343SA. Cody Schuffelen // The functions in this file are designed to support self-test of cryptographic
37*5c591343SA. Cody Schuffelen // functions in the TPM. The TPM allows the user to decide whether to run self-test
38*5c591343SA. Cody Schuffelen // on a demand basis or to run all the self-tests before proceeding.
39*5c591343SA. Cody Schuffelen //
40*5c591343SA. Cody Schuffelen // The self-tests are controlled by a set of bit vectors. The
41*5c591343SA. Cody Schuffelen // 'g_untestedDecryptionAlgorithms' vector has a bit for each decryption algorithm
42*5c591343SA. Cody Schuffelen // that needs to be tested and 'g_untestedEncryptionAlgorithms' has a bit for
43*5c591343SA. Cody Schuffelen // each encryption algorithm that needs to be tested. Before an algorithm
44*5c591343SA. Cody Schuffelen // is used, the appropriate vector is checked (indexed using the algorithm ID).
45*5c591343SA. Cody Schuffelen // If the bit is 1, then the test function should be called.
46*5c591343SA. Cody Schuffelen //
47*5c591343SA. Cody Schuffelen // For more information, see TpmSelfTests.txt
48*5c591343SA. Cody Schuffelen 
49*5c591343SA. Cody Schuffelen #include "Tpm.h"
50*5c591343SA. Cody Schuffelen 
51*5c591343SA. Cody Schuffelen //** Functions
52*5c591343SA. Cody Schuffelen 
53*5c591343SA. Cody Schuffelen //*** RunSelfTest()
54*5c591343SA. Cody Schuffelen // Local function to run self-test
55*5c591343SA. Cody Schuffelen static TPM_RC
CryptRunSelfTests(ALGORITHM_VECTOR * toTest)56*5c591343SA. Cody Schuffelen CryptRunSelfTests(
57*5c591343SA. Cody Schuffelen     ALGORITHM_VECTOR    *toTest         // IN: the vector of the algorithms to test
58*5c591343SA. Cody Schuffelen     )
59*5c591343SA. Cody Schuffelen {
60*5c591343SA. Cody Schuffelen     TPM_ALG_ID           alg;
61*5c591343SA. Cody Schuffelen 
62*5c591343SA. Cody Schuffelen     // For each of the algorithms that are in the toTestVecor, need to run a
63*5c591343SA. Cody Schuffelen     // test
64*5c591343SA. Cody Schuffelen     for(alg = TPM_ALG_FIRST; alg <= TPM_ALG_LAST; alg++)
65*5c591343SA. Cody Schuffelen     {
66*5c591343SA. Cody Schuffelen         if(TEST_BIT(alg, *toTest))
67*5c591343SA. Cody Schuffelen         {
68*5c591343SA. Cody Schuffelen             TPM_RC          result = CryptTestAlgorithm(alg, toTest);
69*5c591343SA. Cody Schuffelen             if(result != TPM_RC_SUCCESS)
70*5c591343SA. Cody Schuffelen                 return result;
71*5c591343SA. Cody Schuffelen         }
72*5c591343SA. Cody Schuffelen     }
73*5c591343SA. Cody Schuffelen     return TPM_RC_SUCCESS;
74*5c591343SA. Cody Schuffelen }
75*5c591343SA. Cody Schuffelen 
76*5c591343SA. Cody Schuffelen //*** CryptSelfTest()
77*5c591343SA. Cody Schuffelen // This function is called to start/complete a full self-test.
78*5c591343SA. Cody Schuffelen // If 'fullTest' is NO, then only the untested algorithms will be run. If
79*5c591343SA. Cody Schuffelen // 'fullTest' is YES, then 'g_untestedDecryptionAlgorithms' is reinitialized and then
80*5c591343SA. Cody Schuffelen // all tests are run.
81*5c591343SA. Cody Schuffelen // This implementation of the reference design does not support processing outside
82*5c591343SA. Cody Schuffelen // the framework of a TPM command. As a consequence, this command does not
83*5c591343SA. Cody Schuffelen // complete until all tests are done. Since this can take a long time, the TPM
84*5c591343SA. Cody Schuffelen // will check after each test to see if the command is canceled. If so, then the
85*5c591343SA. Cody Schuffelen // TPM will returned TPM_RC_CANCELLED. To continue with the self-tests, call
86*5c591343SA. Cody Schuffelen // TPM2_SelfTest(fullTest == No) and the TPM will complete the testing.
87*5c591343SA. Cody Schuffelen //  Return Type: TPM_RC
88*5c591343SA. Cody Schuffelen //      TPM_RC_CANCELED        if the command is canceled
89*5c591343SA. Cody Schuffelen LIB_EXPORT
90*5c591343SA. Cody Schuffelen TPM_RC
CryptSelfTest(TPMI_YES_NO fullTest)91*5c591343SA. Cody Schuffelen CryptSelfTest(
92*5c591343SA. Cody Schuffelen     TPMI_YES_NO      fullTest       // IN: if full test is required
93*5c591343SA. Cody Schuffelen     )
94*5c591343SA. Cody Schuffelen {
95*5c591343SA. Cody Schuffelen #if SIMULATION
96*5c591343SA. Cody Schuffelen     if(g_forceFailureMode)
97*5c591343SA. Cody Schuffelen         FAIL(FATAL_ERROR_FORCED);
98*5c591343SA. Cody Schuffelen #endif
99*5c591343SA. Cody Schuffelen 
100*5c591343SA. Cody Schuffelen     // If the caller requested a full test, then reset the to test vector so that
101*5c591343SA. Cody Schuffelen     // all the tests will be run
102*5c591343SA. Cody Schuffelen     if(fullTest == YES)
103*5c591343SA. Cody Schuffelen     {
104*5c591343SA. Cody Schuffelen         MemoryCopy(g_toTest,
105*5c591343SA. Cody Schuffelen                    g_implementedAlgorithms,
106*5c591343SA. Cody Schuffelen                    sizeof(g_toTest));
107*5c591343SA. Cody Schuffelen     }
108*5c591343SA. Cody Schuffelen     return CryptRunSelfTests(&g_toTest);
109*5c591343SA. Cody Schuffelen }
110*5c591343SA. Cody Schuffelen 
111*5c591343SA. Cody Schuffelen //*** CryptIncrementalSelfTest()
112*5c591343SA. Cody Schuffelen // This function is used to perform an incremental self-test. This implementation
113*5c591343SA. Cody Schuffelen // will perform the toTest values before returning. That is, it assumes that the
114*5c591343SA. Cody Schuffelen // TPM cannot perform background tasks between commands.
115*5c591343SA. Cody Schuffelen //
116*5c591343SA. Cody Schuffelen // This command may be canceled. If it is, then there is no return result.
117*5c591343SA. Cody Schuffelen // However, this command can be run again and the incremental progress will not
118*5c591343SA. Cody Schuffelen // be lost.
119*5c591343SA. Cody Schuffelen //  Return Type: TPM_RC
120*5c591343SA. Cody Schuffelen //      TPM_RC_CANCELED         processing of this command was canceled
121*5c591343SA. Cody Schuffelen //      TPM_RC_TESTING          if toTest list is not empty
122*5c591343SA. Cody Schuffelen //      TPM_RC_VALUE            an algorithm in the toTest list is not implemented
123*5c591343SA. Cody Schuffelen TPM_RC
CryptIncrementalSelfTest(TPML_ALG * toTest,TPML_ALG * toDoList)124*5c591343SA. Cody Schuffelen CryptIncrementalSelfTest(
125*5c591343SA. Cody Schuffelen     TPML_ALG            *toTest,        // IN: list of algorithms to be tested
126*5c591343SA. Cody Schuffelen     TPML_ALG            *toDoList       // OUT: list of algorithms needing test
127*5c591343SA. Cody Schuffelen     )
128*5c591343SA. Cody Schuffelen {
129*5c591343SA. Cody Schuffelen     ALGORITHM_VECTOR     toTestVector = {0};
130*5c591343SA. Cody Schuffelen     TPM_ALG_ID           alg;
131*5c591343SA. Cody Schuffelen     UINT32               i;
132*5c591343SA. Cody Schuffelen 
133*5c591343SA. Cody Schuffelen     pAssert(toTest != NULL && toDoList != NULL);
134*5c591343SA. Cody Schuffelen     if(toTest->count > 0)
135*5c591343SA. Cody Schuffelen     {
136*5c591343SA. Cody Schuffelen         // Transcribe the toTest list into the toTestVector
137*5c591343SA. Cody Schuffelen         for(i = 0; i < toTest->count; i++)
138*5c591343SA. Cody Schuffelen         {
139*5c591343SA. Cody Schuffelen             alg = toTest->algorithms[i];
140*5c591343SA. Cody Schuffelen 
141*5c591343SA. Cody Schuffelen             // make sure that the algorithm value is not out of range
142*5c591343SA. Cody Schuffelen             if((alg > TPM_ALG_LAST) || !TEST_BIT(alg, g_implementedAlgorithms))
143*5c591343SA. Cody Schuffelen                 return TPM_RC_VALUE;
144*5c591343SA. Cody Schuffelen             SET_BIT(alg, toTestVector);
145*5c591343SA. Cody Schuffelen         }
146*5c591343SA. Cody Schuffelen         // Run the test
147*5c591343SA. Cody Schuffelen         if(CryptRunSelfTests(&toTestVector) == TPM_RC_CANCELED)
148*5c591343SA. Cody Schuffelen             return TPM_RC_CANCELED;
149*5c591343SA. Cody Schuffelen     }
150*5c591343SA. Cody Schuffelen     // Fill in the toDoList with the algorithms that are still untested
151*5c591343SA. Cody Schuffelen     toDoList->count = 0;
152*5c591343SA. Cody Schuffelen 
153*5c591343SA. Cody Schuffelen     for(alg = TPM_ALG_FIRST;
154*5c591343SA. Cody Schuffelen     toDoList->count < MAX_ALG_LIST_SIZE && alg <= TPM_ALG_LAST;
155*5c591343SA. Cody Schuffelen         alg++)
156*5c591343SA. Cody Schuffelen     {
157*5c591343SA. Cody Schuffelen         if(TEST_BIT(alg, g_toTest))
158*5c591343SA. Cody Schuffelen             toDoList->algorithms[toDoList->count++] = alg;
159*5c591343SA. Cody Schuffelen     }
160*5c591343SA. Cody Schuffelen     return TPM_RC_SUCCESS;
161*5c591343SA. Cody Schuffelen }
162*5c591343SA. Cody Schuffelen 
163*5c591343SA. Cody Schuffelen //*** CryptInitializeToTest()
164*5c591343SA. Cody Schuffelen // This function will initialize the data structures for testing all the
165*5c591343SA. Cody Schuffelen // algorithms. This should not be called unless CryptAlgsSetImplemented() has
166*5c591343SA. Cody Schuffelen // been called
167*5c591343SA. Cody Schuffelen void
CryptInitializeToTest(void)168*5c591343SA. Cody Schuffelen CryptInitializeToTest(
169*5c591343SA. Cody Schuffelen     void
170*5c591343SA. Cody Schuffelen     )
171*5c591343SA. Cody Schuffelen {
172*5c591343SA. Cody Schuffelen     // Indicate that nothing has been tested
173*5c591343SA. Cody Schuffelen     memset(&g_cryptoSelfTestState, 0, sizeof(g_cryptoSelfTestState));
174*5c591343SA. Cody Schuffelen 
175*5c591343SA. Cody Schuffelen     // Copy the implemented algorithm vector
176*5c591343SA. Cody Schuffelen     MemoryCopy(g_toTest, g_implementedAlgorithms, sizeof(g_toTest));
177*5c591343SA. Cody Schuffelen 
178*5c591343SA. Cody Schuffelen     // Setting the algorithm to null causes the test function to just clear
179*5c591343SA. Cody Schuffelen     // out any algorithms for which there is no test.
180*5c591343SA. Cody Schuffelen     CryptTestAlgorithm(TPM_ALG_ERROR, &g_toTest);
181*5c591343SA. Cody Schuffelen 
182*5c591343SA. Cody Schuffelen     return;
183*5c591343SA. Cody Schuffelen }
184*5c591343SA. Cody Schuffelen 
185*5c591343SA. Cody Schuffelen //*** CryptTestAlgorithm()
186*5c591343SA. Cody Schuffelen // Only point of contact with the actual self tests. If a self-test fails, there
187*5c591343SA. Cody Schuffelen // is no return and the TPM goes into failure mode.
188*5c591343SA. Cody Schuffelen // The call to TestAlgorithm uses an algorithm selector and a bit vector. When the
189*5c591343SA. Cody Schuffelen // test is run, the corresponding bit in 'toTest' and in 'g_toTest' is CLEAR. If
190*5c591343SA. Cody Schuffelen // 'toTest' is NULL, then only the bit in 'g_toTest' is CLEAR.
191*5c591343SA. Cody Schuffelen // There is a special case for the call to TestAlgorithm(). When 'alg' is
192*5c591343SA. Cody Schuffelen // ALG_ERROR, TestAlgorithm() will CLEAR any bit in 'toTest' for which it has
193*5c591343SA. Cody Schuffelen // no test. This allows the knowledge about which algorithms have test to be
194*5c591343SA. Cody Schuffelen // accessed through the interface that provides the test.
195*5c591343SA. Cody Schuffelen //  Return Type: TPM_RC
196*5c591343SA. Cody Schuffelen //      TPM_RC_CANCELED     test was canceled
197*5c591343SA. Cody Schuffelen LIB_EXPORT
198*5c591343SA. Cody Schuffelen TPM_RC
CryptTestAlgorithm(TPM_ALG_ID alg,ALGORITHM_VECTOR * toTest)199*5c591343SA. Cody Schuffelen CryptTestAlgorithm(
200*5c591343SA. Cody Schuffelen     TPM_ALG_ID           alg,
201*5c591343SA. Cody Schuffelen     ALGORITHM_VECTOR    *toTest
202*5c591343SA. Cody Schuffelen     )
203*5c591343SA. Cody Schuffelen {
204*5c591343SA. Cody Schuffelen     TPM_RC                   result;
205*5c591343SA. Cody Schuffelen #if SELF_TEST
206*5c591343SA. Cody Schuffelen     result = TestAlgorithm(alg, toTest);
207*5c591343SA. Cody Schuffelen #else
208*5c591343SA. Cody Schuffelen     // If this is an attempt to determine the algorithms for which there is a
209*5c591343SA. Cody Schuffelen     // self test, pretend that all of them do. We do that by not clearing any
210*5c591343SA. Cody Schuffelen     // of the algorithm bits. When/if this function is called to run tests, it
211*5c591343SA. Cody Schuffelen     // will over report. This can be changed so that any call to check on which
212*5c591343SA. Cody Schuffelen     // algorithms have tests, 'toTest' can be cleared.
213*5c591343SA. Cody Schuffelen     if(alg != TPM_ALG_ERROR)
214*5c591343SA. Cody Schuffelen     {
215*5c591343SA. Cody Schuffelen         CLEAR_BIT(alg, g_toTest);
216*5c591343SA. Cody Schuffelen         if(toTest != NULL)
217*5c591343SA. Cody Schuffelen             CLEAR_BIT(alg, *toTest);
218*5c591343SA. Cody Schuffelen     }
219*5c591343SA. Cody Schuffelen     result = TPM_RC_SUCCESS;
220*5c591343SA. Cody Schuffelen #endif
221*5c591343SA. Cody Schuffelen     return result;
222*5c591343SA. Cody Schuffelen }