xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/CryptRand.h (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 // This file contains constant definition shared by CryptUtil and the parts
37*5c591343SA. Cody Schuffelen // of the Crypto Engine.
38*5c591343SA. Cody Schuffelen //
39*5c591343SA. Cody Schuffelen 
40*5c591343SA. Cody Schuffelen #ifndef _CRYPT_RAND_H
41*5c591343SA. Cody Schuffelen #define _CRYPT_RAND_H
42*5c591343SA. Cody Schuffelen 
43*5c591343SA. Cody Schuffelen 
44*5c591343SA. Cody Schuffelen //** DRBG Structures and Defines
45*5c591343SA. Cody Schuffelen 
46*5c591343SA. Cody Schuffelen // Values and structures for the random number generator. These values are defined
47*5c591343SA. Cody Schuffelen // in this header file so that the size of the RNG state can be known to TPM.lib.
48*5c591343SA. Cody Schuffelen // This allows the allocation of some space in NV memory for the state to
49*5c591343SA. Cody Schuffelen // be stored on an orderly shutdown.
50*5c591343SA. Cody Schuffelen 
51*5c591343SA. Cody Schuffelen // The DRBG based on a symmetric block cipher is defined by three values,
52*5c591343SA. Cody Schuffelen // 1) the key size
53*5c591343SA. Cody Schuffelen // 2) the block size (the IV size)
54*5c591343SA. Cody Schuffelen // 3) the symmetric algorithm
55*5c591343SA. Cody Schuffelen 
56*5c591343SA. Cody Schuffelen #define DRBG_KEY_SIZE_BITS      AES_MAX_KEY_SIZE_BITS
57*5c591343SA. Cody Schuffelen #define DRBG_IV_SIZE_BITS       (AES_MAX_BLOCK_SIZE * 8)
58*5c591343SA. Cody Schuffelen #define DRBG_ALGORITHM          TPM_ALG_AES
59*5c591343SA. Cody Schuffelen 
60*5c591343SA. Cody Schuffelen 
61*5c591343SA. Cody Schuffelen typedef tpmKeyScheduleAES     DRBG_KEY_SCHEDULE;
62*5c591343SA. Cody Schuffelen #define DRBG_ENCRYPT_SETUP(key, keySizeInBits, schedule)        \
63*5c591343SA. Cody Schuffelen             TpmCryptSetEncryptKeyAES(key, keySizeInBits, schedule)
64*5c591343SA. Cody Schuffelen #define DRBG_ENCRYPT(keySchedule, in, out)                      \
65*5c591343SA. Cody Schuffelen             TpmCryptEncryptAES(SWIZZLE(keySchedule, in, out))
66*5c591343SA. Cody Schuffelen 
67*5c591343SA. Cody Schuffelen #if     ((DRBG_KEY_SIZE_BITS % RADIX_BITS) != 0) \
68*5c591343SA. Cody Schuffelen     || ((DRBG_IV_SIZE_BITS % RADIX_BITS) != 0)
69*5c591343SA. Cody Schuffelen #error "Key size and IV for DRBG must be even multiples of the radix"
70*5c591343SA. Cody Schuffelen #endif
71*5c591343SA. Cody Schuffelen #if (DRBG_KEY_SIZE_BITS % DRBG_IV_SIZE_BITS) != 0
72*5c591343SA. Cody Schuffelen #error "Key size for DRBG must be even multiple of the cypher block size"
73*5c591343SA. Cody Schuffelen #endif
74*5c591343SA. Cody Schuffelen 
75*5c591343SA. Cody Schuffelen // Derived values
76*5c591343SA. Cody Schuffelen #define DRBG_MAX_REQUESTS_PER_RESEED (1 << 48)
77*5c591343SA. Cody Schuffelen #define DRBG_MAX_REQEST_SIZE (1 << 32)
78*5c591343SA. Cody Schuffelen 
79*5c591343SA. Cody Schuffelen #define pDRBG_KEY(seed)    ((DRBG_KEY *)&(((BYTE *)(seed))[0]))
80*5c591343SA. Cody Schuffelen #define pDRBG_IV(seed)     ((DRBG_IV *)&(((BYTE *)(seed))[DRBG_KEY_SIZE_BYTES]))
81*5c591343SA. Cody Schuffelen 
82*5c591343SA. Cody Schuffelen #define DRBG_KEY_SIZE_WORDS     (BITS_TO_CRYPT_WORDS(DRBG_KEY_SIZE_BITS))
83*5c591343SA. Cody Schuffelen #define DRBG_KEY_SIZE_BYTES     (DRBG_KEY_SIZE_WORDS * RADIX_BYTES)
84*5c591343SA. Cody Schuffelen 
85*5c591343SA. Cody Schuffelen #define DRBG_IV_SIZE_WORDS      (BITS_TO_CRYPT_WORDS(DRBG_IV_SIZE_BITS))
86*5c591343SA. Cody Schuffelen #define DRBG_IV_SIZE_BYTES      (DRBG_IV_SIZE_WORDS * RADIX_BYTES)
87*5c591343SA. Cody Schuffelen 
88*5c591343SA. Cody Schuffelen #define DRBG_SEED_SIZE_WORDS    (DRBG_KEY_SIZE_WORDS + DRBG_IV_SIZE_WORDS)
89*5c591343SA. Cody Schuffelen #define DRBG_SEED_SIZE_BYTES    (DRBG_KEY_SIZE_BYTES + DRBG_IV_SIZE_BYTES)
90*5c591343SA. Cody Schuffelen 
91*5c591343SA. Cody Schuffelen 
92*5c591343SA. Cody Schuffelen typedef union
93*5c591343SA. Cody Schuffelen {
94*5c591343SA. Cody Schuffelen     BYTE            bytes[DRBG_KEY_SIZE_BYTES];
95*5c591343SA. Cody Schuffelen     crypt_uword_t   words[DRBG_KEY_SIZE_WORDS];
96*5c591343SA. Cody Schuffelen } DRBG_KEY;
97*5c591343SA. Cody Schuffelen 
98*5c591343SA. Cody Schuffelen typedef union
99*5c591343SA. Cody Schuffelen {
100*5c591343SA. Cody Schuffelen     BYTE            bytes[DRBG_IV_SIZE_BYTES];
101*5c591343SA. Cody Schuffelen     crypt_uword_t   words[DRBG_IV_SIZE_WORDS];
102*5c591343SA. Cody Schuffelen } DRBG_IV;
103*5c591343SA. Cody Schuffelen 
104*5c591343SA. Cody Schuffelen typedef union
105*5c591343SA. Cody Schuffelen {
106*5c591343SA. Cody Schuffelen     BYTE            bytes[DRBG_SEED_SIZE_BYTES];
107*5c591343SA. Cody Schuffelen     crypt_uword_t   words[DRBG_SEED_SIZE_WORDS];
108*5c591343SA. Cody Schuffelen } DRBG_SEED;
109*5c591343SA. Cody Schuffelen 
110*5c591343SA. Cody Schuffelen #define CTR_DRBG_MAX_REQUESTS_PER_RESEED        ((UINT64)1 << 20)
111*5c591343SA. Cody Schuffelen #define CTR_DRBG_MAX_BYTES_PER_REQUEST          (1 << 16)
112*5c591343SA. Cody Schuffelen 
113*5c591343SA. Cody Schuffelen #   define CTR_DRBG_MIN_ENTROPY_INPUT_LENGTH    DRBG_SEED_SIZE_BYTES
114*5c591343SA. Cody Schuffelen #   define CTR_DRBG_MAX_ENTROPY_INPUT_LENGTH    DRBG_SEED_SIZE_BYTES
115*5c591343SA. Cody Schuffelen #   define CTR_DRBG_MAX_ADDITIONAL_INPUT_LENGTH DRBG_SEED_SIZE_BYTES
116*5c591343SA. Cody Schuffelen 
117*5c591343SA. Cody Schuffelen #define     TESTING         (1 << 0)
118*5c591343SA. Cody Schuffelen #define     ENTROPY         (1 << 1)
119*5c591343SA. Cody Schuffelen #define     TESTED          (1 << 2)
120*5c591343SA. Cody Schuffelen 
121*5c591343SA. Cody Schuffelen #define     IsTestStateSet(BIT)    ((g_cryptoSelfTestState.rng & BIT) != 0)
122*5c591343SA. Cody Schuffelen #define     SetTestStateBit(BIT)   (g_cryptoSelfTestState.rng |= BIT)
123*5c591343SA. Cody Schuffelen #define     ClearTestStateBit(BIT) (g_cryptoSelfTestState.rng &= ~BIT)
124*5c591343SA. Cody Schuffelen 
125*5c591343SA. Cody Schuffelen #define     IsSelfTest()    IsTestStateSet(TESTING)
126*5c591343SA. Cody Schuffelen #define     SetSelfTest()   SetTestStateBit(TESTING)
127*5c591343SA. Cody Schuffelen #define     ClearSelfTest() ClearTestStateBit(TESTING)
128*5c591343SA. Cody Schuffelen 
129*5c591343SA. Cody Schuffelen #define     IsEntropyBad()      IsTestStateSet(ENTROPY)
130*5c591343SA. Cody Schuffelen #define     SetEntropyBad()     SetTestStateBit(ENTROPY)
131*5c591343SA. Cody Schuffelen #define     ClearEntropyBad()   ClearTestStateBit(ENTROPY)
132*5c591343SA. Cody Schuffelen 
133*5c591343SA. Cody Schuffelen #define     IsDrbgTested()      IsTestStateSet(TESTED)
134*5c591343SA. Cody Schuffelen #define     SetDrbgTested()     SetTestStateBit(TESTED)
135*5c591343SA. Cody Schuffelen #define     ClearDrbgTested()   ClearTestStateBit(TESTED)
136*5c591343SA. Cody Schuffelen 
137*5c591343SA. Cody Schuffelen typedef struct
138*5c591343SA. Cody Schuffelen {
139*5c591343SA. Cody Schuffelen     UINT64      reseedCounter;
140*5c591343SA. Cody Schuffelen     UINT32      magic;
141*5c591343SA. Cody Schuffelen     DRBG_SEED   seed; // contains the key and IV for the counter mode DRBG
142*5c591343SA. Cody Schuffelen     UINT32      lastValue[4];   // used when the TPM does continuous self-test
143*5c591343SA. Cody Schuffelen                                 // for FIPS compliance of DRBG
144*5c591343SA. Cody Schuffelen } DRBG_STATE, *pDRBG_STATE;
145*5c591343SA. Cody Schuffelen #define DRBG_MAGIC   ((UINT32) 0x47425244) // "DRBG" backwards so that it displays
146*5c591343SA. Cody Schuffelen 
147*5c591343SA. Cody Schuffelen typedef struct KDF_STATE {
148*5c591343SA. Cody Schuffelen     UINT64               counter;
149*5c591343SA. Cody Schuffelen     UINT32               magic;
150*5c591343SA. Cody Schuffelen     UINT32               limit;
151*5c591343SA. Cody Schuffelen     TPM2B               *seed;
152*5c591343SA. Cody Schuffelen     const TPM2B         *label;
153*5c591343SA. Cody Schuffelen     TPM2B               *context;
154*5c591343SA. Cody Schuffelen     TPM_ALG_ID           hash;
155*5c591343SA. Cody Schuffelen     TPM_ALG_ID           kdf;
156*5c591343SA. Cody Schuffelen     UINT16               digestSize;
157*5c591343SA. Cody Schuffelen     TPM2B_DIGEST         residual;
158*5c591343SA. Cody Schuffelen } KDF_STATE, *pKDR_STATE;
159*5c591343SA. Cody Schuffelen #define KDF_MAGIC    ((UINT32) 0x4048444a) // "KDF " backwards
160*5c591343SA. Cody Schuffelen 
161*5c591343SA. Cody Schuffelen // Make sure that any other structures added to this union start with a 64-bit
162*5c591343SA. Cody Schuffelen // counter and a 32-bit magic number
163*5c591343SA. Cody Schuffelen typedef union
164*5c591343SA. Cody Schuffelen {
165*5c591343SA. Cody Schuffelen     DRBG_STATE      drbg;
166*5c591343SA. Cody Schuffelen     KDF_STATE       kdf;
167*5c591343SA. Cody Schuffelen } RAND_STATE;
168*5c591343SA. Cody Schuffelen 
169*5c591343SA. Cody Schuffelen // This is the state used when the library uses a random number generator.
170*5c591343SA. Cody Schuffelen // A special function is installed for the library to call. That function
171*5c591343SA. Cody Schuffelen // picks up the state from this location and uses it for the generation
172*5c591343SA. Cody Schuffelen // of the random number.
173*5c591343SA. Cody Schuffelen extern RAND_STATE           *s_random;
174*5c591343SA. Cody Schuffelen 
175*5c591343SA. Cody Schuffelen // When instrumenting RSA key sieve
176*5c591343SA. Cody Schuffelen #if  RSA_INSTRUMENT
177*5c591343SA. Cody Schuffelen #define PRIME_INDEX(x)  ((x) == 512 ? 0 : (x) == 1024 ? 1 : 2)
178*5c591343SA. Cody Schuffelen #   define INSTRUMENT_SET(a, b) ((a) = (b))
179*5c591343SA. Cody Schuffelen #   define INSTRUMENT_ADD(a, b) (a) = (a) + (b)
180*5c591343SA. Cody Schuffelen #   define INSTRUMENT_INC(a)    (a) = (a) + 1
181*5c591343SA. Cody Schuffelen 
182*5c591343SA. Cody Schuffelen extern UINT32  PrimeIndex;
183*5c591343SA. Cody Schuffelen extern UINT32  failedAtIteration[10];
184*5c591343SA. Cody Schuffelen extern UINT32  PrimeCounts[3];
185*5c591343SA. Cody Schuffelen extern UINT32  MillerRabinTrials[3];
186*5c591343SA. Cody Schuffelen extern UINT32  totalFieldsSieved[3];
187*5c591343SA. Cody Schuffelen extern UINT32  bitsInFieldAfterSieve[3];
188*5c591343SA. Cody Schuffelen extern UINT32  emptyFieldsSieved[3];
189*5c591343SA. Cody Schuffelen extern UINT32  noPrimeFields[3];
190*5c591343SA. Cody Schuffelen extern UINT32  primesChecked[3];
191*5c591343SA. Cody Schuffelen extern UINT16  lastSievePrime;
192*5c591343SA. Cody Schuffelen #else
193*5c591343SA. Cody Schuffelen #   define INSTRUMENT_SET(a, b)
194*5c591343SA. Cody Schuffelen #   define INSTRUMENT_ADD(a, b)
195*5c591343SA. Cody Schuffelen #   define INSTRUMENT_INC(a)
196*5c591343SA. Cody Schuffelen #endif
197*5c591343SA. Cody Schuffelen 
198*5c591343SA. Cody Schuffelen #endif // _CRYPT_RAND_H
199