blob: 6acbb69a90590a0b513b97e08d75d90303cd1159 [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001// This file was extracted from the TCG Published
2// Trusted Platform Module Library
3// Part 4: Supporting Routines
4// Family "2.0"
5// Level 00 Revision 01.16
6// October 30, 2014
7
8#ifndef _CRYPT_PRI_H
9#define _CRYPT_PRI_H
10#include <stddef.h>
11#include "TpmBuildSwitches.h"
12#include "BaseTypes.h"
13#include "TpmError.h"
14#include "swap.h"
15#include "Implementation.h"
Vadim Bendeburyc4b77e02015-05-27 15:29:50 -070016#include "TPM_Types.h"
Vadim Bendebury56797522015-05-20 10:32:25 -070017//#include "TPMB.h"
18#include "bool.h"
19#include "Platform.h"
20#ifndef NULL
21#define NULL 0
22#endif
23typedef UINT16 NUMBYTES; // When a size is a number of bytes
24typedef UINT32 NUMDIGITS; // When a size is a number of "digits"
25// General Purpose Macros
26//
27#ifndef MAX
28# define MAX(a, b) ((a) > (b) ? (a) : b)
29#endif
30//
31// This is the definition of a bit array with one bit per algorithm
32//
33typedef BYTE ALGORITHM_VECTOR[(ALG_LAST_VALUE + 7) / 8];
34//
35//
36// Self-test
37//
38// This structure is used to contain self-test tracking information for the crypto engine. Each of the major
39// modules is given a 32-bit value in which it may maintain its own self test information. The convention for
40// this state is that when all of the bits in this structure are 0, all functions need to be tested.
41//
42typedef struct {
43 UINT32 rng;
44 UINT32 hash;
45 UINT32 sym;
46#ifdef TPM_ALG_RSA
47 UINT32 rsa;
48#endif
49#ifdef TPM_ALG_ECC
50 UINT32 ecc;
51#endif
52} CRYPTO_SELF_TEST_STATE;
53//
54//
55// Hash-related Structures
56//
57typedef struct {
58 const TPM_ALG_ID alg;
59 const NUMBYTES digestSize;
60 const NUMBYTES blockSize;
61 const NUMBYTES derSize;
62 const BYTE der[20];
63} HASH_INFO;
64//
65// This value will change with each implementation. The value of 16 is used to account for any slop in the
66// context values. The overall size needs to be as large as any of the hash contexts. The structure needs to
67// start on an alignment boundary and be an even multiple of the alignment
68//
69#define ALIGNED_SIZE(x, b) ((((x) + (b) - 1) / (b)) * (b))
70#define MAX_HASH_STATE_SIZE ((2 * MAX_HASH_BLOCK_SIZE) + 16)
71#define MAX_HASH_STATE_SIZE_ALIGNED \
72 ALIGNED_SIZE(MAX_HASH_STATE_SIZE, CRYPTO_ALIGNMENT)
73//
74// This is an byte array that will hold any of the hash contexts.
75//
76typedef CRYPTO_ALIGNED BYTE ALIGNED_HASH_STATE[MAX_HASH_STATE_SIZE_ALIGNED];
77//
78// Macro to align an address to the next higher size
79//
80#define AlignPointer(address, align) \
81 ((((intptr_t)&(address)) + (align - 1)) & ~(align - 1))
82//
83// Macro to test alignment
84//
85#define IsAddressAligned(address, align) \
86 (((intptr_t)(address) & (align - 1)) == 0)
87//
88// This is the structure that is used for passing a context into the hashing functions. It should be the same
89// size as the function context used within the hashing functions. This is checked when the hash function is
90// initialized. This version uses a new layout for the contexts and a different definition. The state buffer is an
91// array of HASH_UNIT values so that a decent compiler will put the structure on a HASH_UNIT boundary.
92// If the structure is not properly aligned, the code that manipulates the structure will copy to a properly
93// aligned structure before it is used and copy the result back. This just makes things slower.
94//
95typedef struct _HASH_STATE
96{
97 ALIGNED_HASH_STATE state;
98 TPM_ALG_ID hashAlg;
99} CPRI_HASH_STATE, *PCPRI_HASH_STATE;
100extern const HASH_INFO g_hashData[HASH_COUNT + 1];
101//
102// This is for the external hash state. This implementation assumes that the size of the exported hash state
103// is no larger than the internal hash state. There is a compile-time check to make sure that this is true.
104//
105typedef struct {
106 ALIGNED_HASH_STATE buffer;
107 TPM_ALG_ID hashAlg;
108} EXPORT_HASH_STATE;
109typedef enum {
110 IMPORT_STATE, // Converts externally formatted state to internal
111 EXPORT_STATE // Converts internal formatted state to external
112} IMPORT_EXPORT;
113//
114// Values and structures for the random number generator. These values are defined in this header file so
115// that the size of the RNG state can be known to TPM.lib. This allows the allocation of some space in NV
116// memory for the state to be stored on an orderly shutdown. The GET_PUT enum is used by
117// _cpri__DrbgGetPutState() to indicate the direction of data flow.
118//
119typedef enum {
120 GET_STATE, // Get the state to save to NV
121 PUT_STATE // Restore the state from NV
122} GET_PUT;
123//
124// The DRBG based on a symmetric block cipher is defined by three values,
125// a) the key size
126// b) the block size (the IV size)
127// c) the symmetric algorithm
128//
129#define DRBG_KEY_SIZE_BITS MAX_AES_KEY_BITS
130#define DRBG_IV_SIZE_BITS (MAX_AES_BLOCK_SIZE_BYTES * 8)
131#define DRBG_ALGORITHM TPM_ALG_AES
132#if ((DRBG_KEY_SIZE_BITS % 8) != 0) || ((DRBG_IV_SIZE_BITS % 8) != 0)
133#error "Key size and IV for DRBG must be even multiples of 8"
134#endif
135#if (DRBG_KEY_SIZE_BITS % DRBG_IV_SIZE_BITS) != 0
136#error "Key size for DRBG must be even multiple of the cypher block size"
137#endif
138typedef UINT32 DRBG_SEED[(DRBG_KEY_SIZE_BITS + DRBG_IV_SIZE_BITS) / 32];
139typedef struct {
140 UINT64 reseedCounter;
141 UINT32 magic;
142 DRBG_SEED seed; // contains the key and IV for the counter mode DRBG
143 UINT32 lastValue[4]; // used when the TPM does continuous self-test
144 // for FIPS compliance of DRBG
145} DRBG_STATE, *pDRBG_STATE;
146//
147//
148// Asymmetric Structures and Values
149//
150#ifdef TPM_ALG_ECC
151//
152//
153// ECC-related Structures
154//
155// This structure replicates the structure definition in TPM_Types.h. It is duplicated to avoid inclusion of all of
156// TPM_Types.h This structure is similar to the RSA_KEY structure below. The purpose of these structures
157// is to reduce the overhead of a function call and to make the code less dependent on key types as much
158// as possible.
159//
160typedef struct {
161 UINT32 curveID; // The curve identifier
162 TPMS_ECC_POINT *publicPoint; // Pointer to the public point
163 TPM2B_ECC_PARAMETER *privateKey; // Pointer to the private key
164} ECC_KEY;
165#endif // TPM_ALG_ECC
166#ifdef TPM_ALG_RSA
167//
168//
169// RSA-related Structures
170//
171// This structure is a succinct representation of the cryptographic components of an RSA key.
172//
173typedef struct {
174 UINT32 exponent; // The public exponent pointer
175 TPM2B *publicKey; // Pointer to the public modulus
176 TPM2B *privateKey; // The private exponent (not a prime)
177} RSA_KEY;
178#endif // TPM_ALG_RSA
179//
180//
181// Miscelaneous
182//
183#ifdef TPM_ALG_RSA
184# ifdef TPM_ALG_ECC
185# if MAX_RSA_KEY_BYTES > MAX_ECC_KEY_BYTES
186# define MAX_NUMBER_SIZE MAX_RSA_KEY_BYTES
187# else
188# define MAX_NUMBER_SIZE MAX_ECC_KEY_BYTES
189# endif
190# else // RSA but no ECC
191# define MAX_NUMBER_SIZE MAX_RSA_KEY_BYTES
192# endif
193#elif defined TPM_ALG_ECC
194# define MAX_NUMBER_SIZE MAX_ECC_KEY_BYTES
195#else
196# error No assymmetric algorithm implemented.
197#endif
198typedef INT16 CRYPT_RESULT;
199#define CRYPT_RESULT_MIN INT16_MIN
200#define CRYPT_RESULT_MAX INT16_MAX
201//
202//
203// <0 recoverable error
204//
205// 0 success
206// >0 command specific return value (generally a digest size)
207//
208#define CRYPT_FAIL ((CRYPT_RESULT) 1)
209#define CRYPT_SUCCESS ((CRYPT_RESULT) 0)
210#define CRYPT_NO_RESULT ((CRYPT_RESULT) -1)
211//
212#define CRYPT_SCHEME ((CRYPT_RESULT) -2)
213#define CRYPT_PARAMETER ((CRYPT_RESULT) -3)
214#define CRYPT_UNDERFLOW ((CRYPT_RESULT) -4)
215#define CRYPT_POINT ((CRYPT_RESULT) -5)
216#define CRYPT_CANCEL ((CRYPT_RESULT) -6)
217typedef UINT64 HASH_CONTEXT[MAX_HASH_STATE_SIZE/sizeof(UINT64)];
218#include "CpriCryptPri_fp.h"
219#ifdef TPM_ALG_ECC
220# include "CpriDataEcc.h"
221# include "CpriECC_fp.h"
222#endif
223#include "MathFunctions_fp.h"
224#include "CpriRNG_fp.h"
225#include "CpriHash_fp.h"
226#include "CpriSym_fp.h"
227#ifdef TPM_ALG_RSA
228# include "CpriRSA_fp.h"
229#endif
230#endif // !_CRYPT_PRI_H