blob: e2fdd7678b218bb10d132f9a5514b91dd015be2d [file] [log] [blame]
Vadim Bendeburyc4b77e02015-05-27 15:29:50 -07001/*
2 * Copyright 2015 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#ifndef __SOURCE_CRYPTUTIL_FP_H
8#define __SOURCE_CRYPTUTIL_FP_H
9
Vadim Bendeburyc00055c2015-05-29 22:50:18 -070010BOOL CryptAreKeySizesConsistent(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070011 TPMT_PUBLIC *publicArea // IN: the public area to check
12 );
Vadim Bendeburybbd79002015-05-31 14:44:07 -070013TPMI_YES_NO CryptCapGetECCCurve(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070014 TPM_ECC_CURVE curveID, // IN: the starting ECC curve
15 UINT32 maxCount, // IN: count of returned curve
16 TPML_ECC_CURVE *curveList // OUT: ECC curve list
17 );
18UINT32 CryptCapGetEccCurveNumber(void);
Vadim Bendebury6bb41ff2015-05-29 22:43:25 -070019UINT16 CryptCommit(void);
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070020LIB_EXPORT int CryptCompare(const UINT32 aSize, // IN: size of a
21 const BYTE *a, // IN: a buffer
22 const UINT32 bSize, // IN: size of b
23 const BYTE *b // IN: b buffer
24 );
Vadim Bendebury6bb41ff2015-05-29 22:43:25 -070025TPM_RC CryptCommitCompute(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070026 TPMS_ECC_POINT *K, // OUT: [d]B
27 TPMS_ECC_POINT *L, // OUT: [r]B
28 TPMS_ECC_POINT *E, // OUT: [r]M
29 TPM_ECC_CURVE curveID, // IN: The curve for the computation
30 TPMS_ECC_POINT *M, // IN: M (P1)
31 TPMS_ECC_POINT *B, // IN: B (x2, y2)
32 TPM2B_ECC_PARAMETER *d, // IN: the private scalar
33 TPM2B_ECC_PARAMETER *r // IN: the computed r value
34 );
35int CryptCompareSigned(UINT32 aSize, // IN: size of a
36 BYTE *a, // IN: a buffer
37 UINT32 bSize, // IN: size of b
38 BYTE *b // IN: b buffer
39 );
Vadim Bendeburyc00055c2015-05-29 22:50:18 -070040void CryptComputeSymmetricUnique(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070041 TPMI_ALG_HASH nameAlg, // IN: object name algorithm
42 TPMT_SENSITIVE *sensitive, // IN: sensitive area
43 TPM2B_DIGEST *unique // OUT: unique buffer
44 );
45LIB_EXPORT UINT16
46CryptCompleteHMAC2B(HMAC_STATE *hmacState, // IN: the state of HMAC stack
47 TPM2B *digest // OUT: HMAC
48 );
49LIB_EXPORT UINT16
50CryptCompleteHash(void *state, // IN: the state of hash stack
51 UINT16 digestSize, // IN: size of digest buffer
52 BYTE *digest // OUT: hash digest
53 );
Vadim Bendeburyb86371b2015-05-27 18:16:19 -070054UINT16 CryptCompleteHash2B(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070055 void *state, // IN: the state of hash stack
56 TPM2B *digest // IN: the size of the buffer Out: requested number of byte
57 );
Vadim Bendeburybde479a2015-05-29 22:48:02 -070058TPM_RC CryptCreateObject(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070059 TPM_HANDLE parentHandle, // IN/OUT: indication of the seed source
60 TPMT_PUBLIC *publicArea, // IN/OUT: public area
61 TPMS_SENSITIVE_CREATE *sensitiveCreate, // IN: sensitive creation
62 TPMT_SENSITIVE *sensitive // OUT: sensitive area
63 );
64void CryptDrbgGetPutState(GET_PUT direction // IN: Get from or put to DRBG
65 );
Vadim Bendebury3743ac12015-05-29 22:44:14 -070066TPM_RC CryptDecryptRSA(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070067 UINT16 *dataOutSize, // OUT: size of plain text in byte
68 BYTE *dataOut, // OUT: plain text
69 OBJECT *rsaKey, // IN: internal RSA key
70 TPMT_RSA_DECRYPT *scheme, // IN: selects the padding scheme
71 UINT16 cipherInSize, // IN: size of cipher text in byte
72 BYTE *cipherIn, // IN: cipher text
73 const char *label // IN: a label, when needed
74 );
Vadim Bendebury6bb41ff2015-05-29 22:43:25 -070075TPM_RC CryptDivide(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070076 TPM2B *numerator, // IN: numerator
77 TPM2B *denominator, // IN: denominator
78 TPM2B *quotient, // OUT: quotient = numerator / denominator.
79 TPM2B *remainder // OUT: numerator mod denominator.
80 );
81void CryptDrbgGetPutState(GET_PUT direction // IN: Get from or put to DRBG
82 );
Vadim Bendeburyc00055c2015-05-29 22:50:18 -070083//
84//
85// 10.2.6.3 CryptEccGetKeySizeBytes()
86//
87// This macro returns the size of the ECC key in bytes. It uses
88// CryptEccGetKeySizeInBits().
89//
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070090#define CryptEccGetKeySizeInBytes(curve) \
91 ((CryptEccGetKeySizeInBits(curve) + 7) / 8)
Vadim Bendeburyc00055c2015-05-29 22:50:18 -070092
Vadim Bendebury8ef10bb2015-06-01 19:15:01 -070093TPM_RC CryptEcc2PhaseKeyExchange(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -070094 TPMS_ECC_POINT *outZ1, // OUT: the computed point
95 TPMS_ECC_POINT *outZ2, // OUT: optional second point
96 TPM_ALG_ID scheme, // IN: the key exchange scheme
97 TPM_ECC_CURVE curveId, // IN: the curve for the computation
98 TPM2B_ECC_PARAMETER *dsA, // IN: static private TPM key
99 TPM2B_ECC_PARAMETER *deA, // IN: ephemeral private TPM key
100 TPMS_ECC_POINT *QsB, // IN: static public party B key
101 TPMS_ECC_POINT *QeB // IN: ephemeral public party B key
102 );
Vadim Bendebury9c193562015-06-01 17:31:24 -0700103TPM_RC CryptEncryptRSA(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700104 UINT16 *cipherOutSize, // OUT: size of cipher text in byte
105 BYTE *cipherOut, // OUT: cipher text
106 OBJECT *rsaKey, // IN: internal RSA key
107 TPMT_RSA_DECRYPT *scheme, // IN: selects the padding scheme
108 UINT16 dataInSize, // IN: size of plain text in byte
109 BYTE *dataIn, // IN: plain text
110 const char *label // IN: an optional label
111 );
112TPM_ALG_ID CryptGetContextAlg(void *state // IN: the context to check
113 );
114LIB_EXPORT TPM_ALG_ID CryptGetHashAlgByIndex(UINT32 index // IN: the index
Vadim Bendeburyb0d692f2015-05-29 22:54:52 -0700115 );
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700116LIB_EXPORT UINT16
117CryptGetHashDigestSize(TPM_ALG_ID hashAlg // IN: hash algorithm
118 );
119LIB_EXPORT const TPM2B *CryptEccGetParameter(
120 char p, // IN: the parameter selector
121 TPM_ECC_CURVE curveId // IN: the curve id
122 );
Vadim Bendebury6edc84a2015-05-29 22:51:56 -0700123BOOL CryptEccGetParameters(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700124 TPM_ECC_CURVE curveId, // IN: ECC curve ID
125 TPMS_ALGORITHM_DETAIL_ECC *parameters // OUT: ECC parameter
126 );
127TPM_RC CryptEccPointMultiply(TPMS_ECC_POINT *pOut, // OUT: output point
128 TPM_ECC_CURVE curveId, // IN: curve selector
129 TPM2B_ECC_PARAMETER *dIn, // IN: public scalar
130 TPMS_ECC_POINT *pIn // IN: optional point
Vadim Bendebury7ef58d92015-05-29 22:52:44 -0700131 );
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700132BOOL CryptEccIsPointOnCurve(TPM_ECC_CURVE curveID, // IN: ECC curve ID
133 TPMS_ECC_POINT *Q // IN: ECC point
Vadim Bendebury6bb41ff2015-05-29 22:43:25 -0700134 );
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700135void CryptEndCommit(UINT16 c // IN: the counter value of the commitment
136 );
Vadim Bendebury6bb41ff2015-05-29 22:43:25 -0700137BOOL CryptGenerateR(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700138 TPM2B_ECC_PARAMETER *r, // OUT: the generated random value
139 UINT16 *c, // IN/OUT: count value.
140 TPMI_ECC_CURVE curveID, // IN: the curve for the value
141 TPM2B_NAME *name // IN: optional name of a key to associate with 'r'
142 );
143UINT16 CryptGenerateRandom(UINT16 randomSize, // IN: size of random number
144 BYTE *buffer // OUT: buffer of random number
145 );
Vadim Bendeburyc00055c2015-05-29 22:50:18 -0700146void CryptGenerateNewSymmetric(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700147 TPMS_SENSITIVE_CREATE *sensitiveCreate, // IN: sensitive creation data
148 TPMT_SENSITIVE *sensitive, // OUT: sensitive area
149 TPM_ALG_ID hashAlg, // IN: hash algorithm for the KDF
150 TPM2B_SEED *seed, // IN: seed used in creation
151 TPM2B_NAME *name // IN: name of the object
152 );
153const TPMT_ECC_SCHEME *CryptGetCurveSignScheme(
154 TPM_ECC_CURVE curveId // IN: The curve selector
155 );
156LIB_EXPORT UINT16
157CryptGetHashDigestSize(TPM_ALG_ID hashAlg // IN: hash algorithm
158 );
159TPMI_ALG_HASH CryptGetSignHashAlg(TPMT_SIGNATURE *auth // IN: signature
160 );
Vadim Bendeburybde479a2015-05-29 22:48:02 -0700161INT16 CryptGetSymmetricBlockSize(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700162 TPMI_ALG_SYM algorithm, // IN: symmetric algorithm
163 UINT16 keySize // IN: key size in bit
164 );
165TPM_RC CryptGetTestResult(TPM2B_MAX_BUFFER *outData // OUT: test result data
166 );
167LIB_EXPORT UINT16
168CryptHashBlock(TPM_ALG_ID algId, // IN: the hash algorithm to use
169 UINT16 blockSize, // IN: size of the data block
170 BYTE *block, // IN: address of the block to hash
171 UINT16 retSize, // IN: size of the return buffer
172 BYTE *ret // OUT: address of the buffer
173 );
Vadim Bendeburyc00055c2015-05-29 22:50:18 -0700174//
175//
176//
177// 10.2.4.23 CryptKDFa()
178//
179// This function generates a key using the KDFa() formulation in Part 1 of the
180// TPM specification. In this implementation, this is a macro invocation of
181// _cpri__KDFa() in the hash module of the CryptoEngine(). This macro sets
182// once to FALSE so that KDFa() will iterate as many times as necessary to
183// generate sizeInBits number of bits.
184//
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700185#define CryptKDFa(hashAlg, key, label, contextU, contextV, sizeInBits, \
186 keyStream, counterInOut) \
187 TEST_HASH(hashAlg); \
188 _cpri__KDFa(((TPM_ALG_ID)hashAlg), ((TPM2B *)key), ((const char *)label), \
189 ((TPM2B *)contextU), ((TPM2B *)contextV), ((UINT32)sizeInBits), \
190 ((BYTE *)keyStream), ((UINT32 *)counterInOut), ((BOOL)FALSE))
Vadim Bendeburyc00055c2015-05-29 22:50:18 -0700191
192//
193//
194// 10.2.4.24 CryptKDFaOnce()
195//
196// This function generates a key using the KDFa() formulation in Part 1 of the
197// TPM specification. In this implementation, this is a macro invocation of
198// _cpri__KDFa() in the hash module of the CryptoEngine(). This macro will
199// call _cpri__KDFa() with once TRUE so that only one iteration is performed,
200// regardless of sizeInBits.
201//
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700202#define CryptKDFaOnce(hashAlg, key, label, contextU, contextV, sizeInBits, \
203 keyStream, counterInOut) \
204 TEST_HASH(hashAlg); \
205 _cpri__KDFa(((TPM_ALG_ID)hashAlg), ((TPM2B *)key), ((const char *)label), \
206 ((TPM2B *)contextU), ((TPM2B *)contextV), ((UINT32)sizeInBits), \
207 ((BYTE *)keyStream), ((UINT32 *)counterInOut), ((BOOL)TRUE))
Vadim Bendeburyc00055c2015-05-29 22:50:18 -0700208
209//
210//
211// 10.2.4.26 CryptKDFe()
212//
213// This function generates a key using the KDFa() formulation in Part 1 of
214// the TPM specification. In this implementation, this is a macro invocation
215// of _cpri__KDFe() in the hash module of the CryptoEngine().
216//
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700217#define CryptKDFe(hashAlg, Z, label, partyUInfo, partyVInfo, sizeInBits, \
218 keyStream) \
219 TEST_HASH(hashAlg); \
220 _cpri__KDFe(((TPM_ALG_ID)hashAlg), ((TPM2B *)Z), ((const char *)label), \
221 ((TPM2B *)partyUInfo), ((TPM2B *)partyVInfo), \
222 ((UINT32)sizeInBits), ((BYTE *)keyStream))
Vadim Bendeburyc00055c2015-05-29 22:50:18 -0700223
Vadim Bendebury8c027af2015-05-29 22:45:58 -0700224void CryptHashStateImportExport(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700225 HASH_STATE *internalFmt, // IN: state to LIB_EXPORT
226 HASH_STATE *externalFmt, // OUT: exported state
227 IMPORT_EXPORT direction);
228void CryptInitUnits(void);
229BOOL CryptIsAsymAlgorithm(TPM_ALG_ID algID // IN: algorithm ID
Vadim Bendebury258ca732015-05-28 16:35:41 -0700230 );
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700231BOOL CryptIsDecryptScheme(TPMI_ALG_ASYM_SCHEME scheme);
232BOOL CryptIsSchemeAnonymous(
233 TPM_ALG_ID scheme // IN: the scheme algorithm to test
234 );
235BOOL CryptIsSignScheme(TPMI_ALG_ASYM_SCHEME scheme);
236BOOL CryptIsSplitSign(TPM_ALG_ID scheme // IN: the algorithm selector
237 );
238TPM_RC CryptNewEccKey(TPM_ECC_CURVE curveID, // IN: ECC curve
239 TPMS_ECC_POINT *publicPoint, // OUT: public point
240 TPM2B_ECC_PARAMETER *sensitive // OUT: private area
241 );
242BOOL CryptObjectIsPublicConsistent(TPMT_PUBLIC *publicArea // IN: public area
243 );
244TPM_RC CryptObjectPublicPrivateMatch(OBJECT *object // IN: the object to check
245 );
246TPM_RC CryptParameterDecryption(
247 TPM_HANDLE handle, // IN: encrypted session handle
248 TPM2B *nonceCaller, // IN: nonce caller
249 UINT32 bufferSize, // IN: size of parameter buffer
250 UINT16
251 leadingSizeInByte, // IN: the size of the leading size field in byte
252 TPM2B_AUTH *extraKey, // IN: the authValue
253 BYTE *buffer // IN/OUT: parameter buffer to be decrypted
254 );
255void CryptParameterEncryption(
256 TPM_HANDLE handle, // IN: encrypt session handle
257 TPM2B *nonceCaller, // IN: nonce caller
258 UINT16 leadingSizeInByte, // IN: the size of the leading size field in byte
259 TPM2B_AUTH *
260 extraKey, // IN: additional key material other than session auth
261 BYTE *buffer // IN/OUT: parameter buffer to be encrypted
262 );
263TPM_RC CryptSecretDecrypt(
264 TPM_HANDLE tpmKey, // IN: decrypt key
265 TPM2B_NONCE *nonceCaller, // IN: nonceCaller. It is needed for symmetric
266 // decryption. For asymmetric decryption, this
267 // parameter is NULL
268 const char *label, // IN: a null-terminated string as L
269 TPM2B_ENCRYPTED_SECRET *secret, // IN: input secret
270 TPM2B_DATA *data // OUT: decrypted secret value
271 );
272TPM_RC CryptSecretEncrypt(
273 TPMI_DH_OBJECT keyHandle, // IN: encryption key handle
274 const char *label, // IN: a null-terminated string as L
275 TPM2B_DATA *data, // OUT: secret value
276 TPM2B_ENCRYPTED_SECRET *secret // OUT: secret structure
277 );
278TPMT_RSA_DECRYPT *CryptSelectRSAScheme(
279 TPMI_DH_OBJECT rsaHandle, // IN: handle of sign key
280 TPMT_RSA_DECRYPT *scheme // IN: a sign or decrypt scheme
281 );
282TPM_RC CryptSelectSignScheme(
283 TPMI_DH_OBJECT signHandle, // IN: handle of signing key
284 TPMT_SIG_SCHEME *scheme // IN/OUT: signing scheme
285 );
286TPM_RC CryptSign(TPMI_DH_OBJECT signHandle, // IN: The handle of sign key
287 TPMT_SIG_SCHEME *signScheme, // IN: sign scheme.
288 TPM2B_DIGEST *digest, // IN: The digest being signed
289 TPMT_SIGNATURE *signature // OUT: signature
290 );
291LIB_EXPORT UINT16
292CryptStartHMAC2B(TPMI_ALG_HASH hashAlg, // IN: hash algorithm
293 TPM2B *key, // IN: HMAC key
294 HMAC_STATE *hmacState // OUT: the state of HMAC stack. It will
295 // be used in HMAC update and completion
296 );
297UINT16 CryptStartHMACSequence2B(TPMI_ALG_HASH hashAlg, // IN: hash algorithm
298 TPM2B *key, // IN: HMAC key
299 HMAC_STATE *hmacState // OUT: the state of HMAC
300 // stack. It will be used
301 // in HMAC update and
302 // completion
303 );
304UINT16 CryptStartHashSequence(TPMI_ALG_HASH hashAlg, // IN: hash algorithm
305 HASH_STATE *hashState // OUT: the state of hash
306 // stack. It will be used
307 // in hash update and
308 // completion
309 );
310void CryptStirRandom(UINT32 entropySize, // IN: size of entropy buffer
311 BYTE *buffer // IN: entropy buffer
312 );
313void CryptStopUnits(void);
314void CryptSymmetricDecrypt(
315 BYTE *decrypted,
316 TPM_ALG_ID algorithm, // IN: algorithm for encryption
317 UINT16 keySizeInBits, // IN: key size in bit
318 TPMI_ALG_SYM_MODE mode, // IN: symmetric encryption mode
319 BYTE *key, // IN: encryption key
320 TPM2B_IV *ivIn, // IN/OUT: IV for next block
321 UINT32 dataSize, // IN: data size in byte
322 BYTE *data // IN/OUT: data buffer
323 );
324void CryptSymmetricEncrypt(
325 BYTE *encrypted, // OUT: the encrypted data
326 TPM_ALG_ID algorithm, // IN: algorithm for encryption
327 UINT16 keySizeInBits, // IN: key size in bit
328 TPMI_ALG_SYM_MODE mode, // IN: symmetric encryption mode
329 BYTE *key, // IN: encryption key
330 TPM2B_IV *ivIn, // IN/OUT: Input IV and output chaining value for the
331 // next block
332 UINT32 dataSize, // IN: data size in byte
333 BYTE *data // IN/OUT: data buffer
334 );
335UINT16 CryptStartHash(TPMI_ALG_HASH hashAlg, // IN: hash algorithm
336 HASH_STATE *hashState // OUT: the state of hash stack. It
337 // will be used in hash update and
338 // completion
339 );
340void CryptUpdateDigest(void *digestState, // IN: the state of hash stack
341 UINT32 dataSize, // IN: the size of data
342 BYTE *data // IN: data to be hashed
343 );
344LIB_EXPORT void CryptUpdateDigest2B(void *digestState, // IN: the digest state
345 TPM2B *bIn // IN: 2B containing the data
346 );
347void CryptUpdateDigestInt(void *state, // IN: the state of hash stack
348 UINT32 intSize, // IN: the size of 'intValue' in byte
349 void *intValue // IN: integer value to be hashed
350 );
351BOOL CryptUtilStartup(STARTUP_TYPE type // IN: the startup type
352 );
Vadim Bendeburydeb59d22015-06-01 16:37:29 -0700353TPM_RC CryptVerifySignature(
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700354 TPMI_DH_OBJECT keyHandle, // IN: The handle of sign key
355 TPM2B_DIGEST *digest, // IN: The digest being validated
356 TPMT_SIGNATURE *signature // IN: signature
357 );
358void KDFa(TPM_ALG_ID hash, // IN: hash algorithm used in HMAC
359 TPM2B *key, // IN: HMAC key
360 const char *label, // IN: a null-terminated label for KDF
361 TPM2B *contextU, // IN: context U
362 TPM2B *contextV, // IN: context V
363 UINT32 sizeInBits, // IN: size of generated key in bit
364 BYTE *keyStream, // OUT: key buffer
365 UINT32 *counterInOut // IN/OUT: caller may provide the iteration
366 // counter for incremental operations to avoid
367 // large intermediate buffers.
368 );
Vadim Bendeburyc4b77e02015-05-27 15:29:50 -0700369
Vadim Bendeburyf788ffb2015-06-05 12:57:27 -0700370#endif // __SOURCE_CRYPTUTIL_FP_H