Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/command/Object/Object_spt.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 //** Includes
36 #include "Tpm.h"
37 #include "Object_spt_fp.h"
38
39 //** Local Functions
40
41 //*** GetIV2BSize()
42 // Get the size of TPM2B_IV in canonical form that will be append to the start of
43 // the sensitive data. It includes both size of size field and size of iv data
44 static UINT16
45 GetIV2BSize(
46 OBJECT *protector // IN: the protector handle
47 )
48 {
49 TPM_ALG_ID symAlg;
50 UINT16 keyBits;
51
52 // Determine the symmetric algorithm and size of key
53 if(protector == NULL)
54 {
55 // Use the context encryption algorithm and key size
56 symAlg = CONTEXT_ENCRYPT_ALG;
57 keyBits = CONTEXT_ENCRYPT_KEY_BITS;
58 }
59 else
60 {
61 symAlg = protector->publicArea.parameters.asymDetail.symmetric.algorithm;
62 keyBits = protector->publicArea.parameters.asymDetail.symmetric.keyBits.sym;
63 }
64
65 // The IV size is a UINT16 size field plus the block size of the symmetric
66 // algorithm
67 return sizeof(UINT16) + CryptGetSymmetricBlockSize(symAlg, keyBits);
68 }
69
70 //*** ComputeProtectionKeyParms()
71 // This function retrieves the symmetric protection key parameters for
72 // the sensitive data
73 // The parameters retrieved from this function include encryption algorithm,
74 // key size in bit, and a TPM2B_SYM_KEY containing the key material as well as
75 // the key size in bytes
76 // This function is used for any action that requires encrypting or decrypting of
77 // the sensitive area of an object or a credential blob
78 //
79 /*(See part 1 specification)
80 KDF for generating the protection key material:
81 KDFa(hashAlg, seed, "STORAGE", Name, NULL , bits)
82 where
83 hashAlg for a Primary Object, an algorithm chosen by the TPM vendor
84 for derivations from Primary Seeds. For all other objects,
85 the nameAlg of the object's parent.
86 seed for a Primary Object in the Platform Hierarchy, the PPS.
87 For Primary Objects in either Storage or Endorsement Hierarchy,
88 the SPS. For Temporary Objects, the context encryption seed.
89 For all other objects, the symmetric seed value in the
90 sensitive area of the object's parent.
91 STORAGE label to differentiate use of KDFa() (see 4.7)
92 Name the Name of the object being encrypted
93 bits the number of bits required for a symmetric key and IV
94 */
95 // Return Type: void
96 static void
97 ComputeProtectionKeyParms(
98 OBJECT *protector, // IN: the protector object
99 TPM_ALG_ID hashAlg, // IN: hash algorithm for KDFa
100 TPM2B *name, // IN: name of the object
101 TPM2B *seedIn, // IN: optional seed for duplication blob.
102 // For non duplication blob, this
103 // parameter should be NULL
104 TPM_ALG_ID *symAlg, // OUT: the symmetric algorithm
105 UINT16 *keyBits, // OUT: the symmetric key size in bits
106 TPM2B_SYM_KEY *symKey // OUT: the symmetric key
107 )
108 {
109 const TPM2B *seed = seedIn;
110
111 // Determine the algorithms for the KDF and the encryption/decryption
112 // For TPM_RH_NULL, using context settings
113 if(protector == NULL)
114 {
115 // Use the context encryption algorithm and key size
116 *symAlg = CONTEXT_ENCRYPT_ALG;
117 symKey->t.size = CONTEXT_ENCRYPT_KEY_BYTES;
118 *keyBits = CONTEXT_ENCRYPT_KEY_BITS;
119 }
120 else
121 {
122 TPMT_SYM_DEF_OBJECT *symDef;
123 symDef = &protector->publicArea.parameters.asymDetail.symmetric;
124 *symAlg = symDef->algorithm;
125 *keyBits = symDef->keyBits.sym;
126 symKey->t.size = (*keyBits + 7) / 8;
127 }
128 // Get seed for KDF
129 if(seed == NULL)
130 seed = GetSeedForKDF(protector);
131 // KDFa to generate symmetric key and IV value
132 CryptKDFa(hashAlg, seed, STORAGE_KEY, name, NULL,
133 symKey->t.size * 8, symKey->t.buffer, NULL, FALSE);
134 return;
135 }
136
137 //*** ComputeOuterIntegrity()
138 // The sensitive area parameter is a buffer that holds a space for
139 // the integrity value and the marshaled sensitive area. The caller should
140 // skip over the area set aside for the integrity value
141 // and compute the hash of the remainder of the object.
142 // The size field of sensitive is in unmarshaled form and the
143 // sensitive area contents is an array of bytes.
144 /*(See part 1 specification)
145 KDFa(hashAlg, seed, "INTEGRITY", NULL, NULL , bits) (38)
146 where
147 hashAlg for a Primary Object, the nameAlg of the object. For all other
148 objects the nameAlg of the object's parent.
149 seed for a Primary Object in the Platform Hierarchy, the PPS. For
150 Primary Objects in either Storage or Endorsement Hierarchy,
151 the SPS. For a Temporary Object, the context encryption key.
152 For all other objects, the symmetric seed value in the sensitive
153 area of the object's parent.
154 "INTEGRITY" a value used to differentiate the uses of the KDF.
155 bits the number of bits in the digest produced by hashAlg.
156 Key is then used in the integrity computation.
157 HMACnameAlg(HMACkey, encSensitive || Name )
158 where
159 HMACnameAlg() the HMAC function using nameAlg of the object's parent
160 HMACkey value derived from the parent symmetric protection value
161 encSensitive symmetrically encrypted sensitive area
162 Name the Name of the object being protected
163 */
164 // Return Type: void
165 static void
166 ComputeOuterIntegrity(
167 TPM2B *name, // IN: the name of the object
168 OBJECT *protector, // IN: the object that
169 // provides protection. For an object,
170 // it is a parent. For a credential, it
171 // is the encrypt object. For
172 // a Temporary Object, it is NULL
173 TPMI_ALG_HASH hashAlg, // IN: algorithm to use for integrity
174 TPM2B *seedIn, // IN: an external seed may be provided for
175 // duplication blob. For non duplication
176 // blob, this parameter should be NULL
177 UINT32 sensitiveSize, // IN: size of the marshaled sensitive data
178 BYTE *sensitiveData, // IN: sensitive area
179 TPM2B_DIGEST *integrity // OUT: integrity
180 )
181 {
182 HMAC_STATE hmacState;
183 TPM2B_DIGEST hmacKey;
184 const TPM2B *seed = seedIn;
185 //
186 // Get seed for KDF
187 if(seed == NULL)
188 seed = GetSeedForKDF(protector);
189 // Determine the HMAC key bits
190 hmacKey.t.size = CryptHashGetDigestSize(hashAlg);
191
192 // KDFa to generate HMAC key
193 CryptKDFa(hashAlg, seed, INTEGRITY_KEY, NULL, NULL,
194 hmacKey.t.size * 8, hmacKey.t.buffer, NULL, FALSE);
195 // Start HMAC and get the size of the digest which will become the integrity
196 integrity->t.size = CryptHmacStart2B(&hmacState, hashAlg, &hmacKey.b);
197
198 // Adding the marshaled sensitive area to the integrity value
199 CryptDigestUpdate(&hmacState.hashState, sensitiveSize, sensitiveData);
200
201 // Adding name
202 CryptDigestUpdate2B(&hmacState.hashState, name);
203
204 // Compute HMAC
205 CryptHmacEnd2B(&hmacState, &integrity->b);
206
207 return;
208 }
209
210 //*** ComputeInnerIntegrity()
211 // This function computes the integrity of an inner wrap
212 static void
213 ComputeInnerIntegrity(
214 TPM_ALG_ID hashAlg, // IN: hash algorithm for inner wrap
215 TPM2B *name, // IN: the name of the object
216 UINT16 dataSize, // IN: the size of sensitive data
217 BYTE *sensitiveData, // IN: sensitive data
218 TPM2B_DIGEST *integrity // OUT: inner integrity
219 )
220 {
221 HASH_STATE hashState;
222 //
223 // Start hash and get the size of the digest which will become the integrity
224 integrity->t.size = CryptHashStart(&hashState, hashAlg);
225
226 // Adding the marshaled sensitive area to the integrity value
227 CryptDigestUpdate(&hashState, dataSize, sensitiveData);
228
229 // Adding name
230 CryptDigestUpdate2B(&hashState, name);
231
232 // Compute hash
233 CryptHashEnd2B(&hashState, &integrity->b);
234
235 return;
236 }
237
238 //*** ProduceInnerIntegrity()
239 // This function produces an inner integrity for regular private, credential or
240 // duplication blob
241 // It requires the sensitive data being marshaled to the innerBuffer, with the
242 // leading bytes reserved for integrity hash. It assume the sensitive data
243 // starts at address (innerBuffer + integrity size).
244 // This function integrity at the beginning of the inner buffer
245 // It returns the total size of buffer with the inner wrap
246 static UINT16
247 ProduceInnerIntegrity(
248 TPM2B *name, // IN: the name of the object
249 TPM_ALG_ID hashAlg, // IN: hash algorithm for inner wrap
250 UINT16 dataSize, // IN: the size of sensitive data, excluding the
251 // leading integrity buffer size
252 BYTE *innerBuffer // IN/OUT: inner buffer with sensitive data in
253 // it. At input, the leading bytes of this
254 // buffer is reserved for integrity
255 )
256 {
257 BYTE *sensitiveData; // pointer to the sensitive data
258 TPM2B_DIGEST integrity;
259 UINT16 integritySize;
260 BYTE *buffer; // Auxiliary buffer pointer
261 //
262 // sensitiveData points to the beginning of sensitive data in innerBuffer
263 integritySize = sizeof(UINT16) + CryptHashGetDigestSize(hashAlg);
264 sensitiveData = innerBuffer + integritySize;
265
266 ComputeInnerIntegrity(hashAlg, name, dataSize, sensitiveData, &integrity);
267
268 // Add integrity at the beginning of inner buffer
269 buffer = innerBuffer;
270 TPM2B_DIGEST_Marshal(&integrity, &buffer, NULL);
271
272 return dataSize + integritySize;
273 }
274
275 //*** CheckInnerIntegrity()
276 // This function check integrity of inner blob
277 // Return Type: TPM_RC
278 // TPM_RC_INTEGRITY if the outer blob integrity is bad
279 // unmarshal errors unmarshal errors while unmarshaling integrity
280 static TPM_RC
281 CheckInnerIntegrity(
282 TPM2B *name, // IN: the name of the object
283 TPM_ALG_ID hashAlg, // IN: hash algorithm for inner wrap
284 UINT16 dataSize, // IN: the size of sensitive data, including the
285 // leading integrity buffer size
286 BYTE *innerBuffer // IN/OUT: inner buffer with sensitive data in
287 // it
288 )
289 {
290 TPM_RC result;
291 TPM2B_DIGEST integrity;
292 TPM2B_DIGEST integrityToCompare;
293 BYTE *buffer; // Auxiliary buffer pointer
294 INT32 size;
295 //
296 // Unmarshal integrity
297 buffer = innerBuffer;
298 size = (INT32)dataSize;
299 result = TPM2B_DIGEST_Unmarshal(&integrity, &buffer, &size);
300 if(result == TPM_RC_SUCCESS)
301 {
302 // Compute integrity to compare
303 ComputeInnerIntegrity(hashAlg, name, (UINT16)size, buffer,
304 &integrityToCompare);
305 // Compare outer blob integrity
306 if(!MemoryEqual2B(&integrity.b, &integrityToCompare.b))
307 result = TPM_RC_INTEGRITY;
308 }
309 return result;
310 }
311
312 //** Public Functions
313
314 //*** AdjustAuthSize()
315 // This function will validate that the input authValue is no larger than the
316 // digestSize for the nameAlg. It will then pad with zeros to the size of the
317 // digest.
318 BOOL
319 AdjustAuthSize(
320 TPM2B_AUTH *auth, // IN/OUT: value to adjust
321 TPMI_ALG_HASH nameAlg // IN:
322 )
323 {
324 UINT16 digestSize;
325 //
326 // If there is no nameAlg, then this is a LoadExternal and the authVale can
327 // be any size up to the maximum allowed by the
328 digestSize = (nameAlg == TPM_ALG_NULL) ? sizeof(TPMU_HA)
329 : CryptHashGetDigestSize(nameAlg);
330 if(digestSize < MemoryRemoveTrailingZeros(auth))
331 return FALSE;
332 else if(digestSize > auth->t.size)
333 MemoryPad2B(&auth->b, digestSize);
334 auth->t.size = digestSize;
335
336 return TRUE;
337 }
338
339 //*** AreAttributesForParent()
340 // This function is called by create, load, and import functions.
341 // Note: The 'isParent' attribute is SET when an object is loaded and it has
342 // attributes that are suitable for a parent object.
343 // Return Type: BOOL
344 // TRUE(1) properties are those of a parent
345 // FALSE(0) properties are not those of a parent
346 BOOL
347 ObjectIsParent(
348 OBJECT *parentObject // IN: parent handle
349 )
350 {
351 return parentObject->attributes.isParent;
352 }
353
354 //*** CreateChecks()
355 // Attribute checks that are unique to creation.
356 // Return Type: TPM_RC
357 // TPM_RC_ATTRIBUTES sensitiveDataOrigin is not consistent with the
358 // object type
359 // other returns from PublicAttributesValidation()
360 TPM_RC
361 CreateChecks(
362 OBJECT *parentObject,
363 TPMT_PUBLIC *publicArea,
364 UINT16 sensitiveDataSize
365 )
366 {
367 TPMA_OBJECT attributes = publicArea->objectAttributes;
368 TPM_RC result = TPM_RC_SUCCESS;
369 //
370 // If the caller indicates that they have provided the data, then make sure that
371 // they have provided some data.
372 if((!IS_ATTRIBUTE(attributes, TPMA_OBJECT, sensitiveDataOrigin))
373 && (sensitiveDataSize == 0))
374 return TPM_RCS_ATTRIBUTES;
375 // For an ordinary object, data can only be provided when sensitiveDataOrigin
376 // is CLEAR
377 if((parentObject != NULL)
378 && (IS_ATTRIBUTE(attributes, TPMA_OBJECT, sensitiveDataOrigin))
379 && (sensitiveDataSize != 0))
380 return TPM_RCS_ATTRIBUTES;
381 switch(publicArea->type)
382 {
383 case ALG_KEYEDHASH_VALUE:
384 // if this is a data object (sign == decrypt == CLEAR) then the
385 // TPM cannot be the data source.
386 if(!IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)
387 && !IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt)
388 && IS_ATTRIBUTE(attributes, TPMA_OBJECT, sensitiveDataOrigin))
389 result = TPM_RC_ATTRIBUTES;
390 // comment out the next line in order to prevent a fixedTPM derivation
391 // parent
392 // break;
393 case ALG_SYMCIPHER_VALUE:
394 // A restricted key symmetric key (SYMCIPHER and KEYEDHASH)
395 // must have sensitiveDataOrigin SET unless it has fixedParent and
396 // fixedTPM CLEAR.
397 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted))
398 if(!IS_ATTRIBUTE(attributes, TPMA_OBJECT, sensitiveDataOrigin))
399 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedParent)
400 || IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM))
401 result = TPM_RCS_ATTRIBUTES;
402 break;
403 default: // Asymmetric keys cannot have the sensitive portion provided
404 if(!IS_ATTRIBUTE(attributes, TPMA_OBJECT, sensitiveDataOrigin))
405 result = TPM_RCS_ATTRIBUTES;
406 break;
407 }
408 if(TPM_RC_SUCCESS == result)
409 {
410 result = PublicAttributesValidation(parentObject, publicArea);
411 }
412 return result;
413 }
414 //*** SchemeChecks
415 // This function is called by TPM2_LoadExternal() and PublicAttributesValidation().
416 // This function validates the schemes in the public area of an object.
417 // Return Type: TPM_RC
418 // TPM_RC_HASH non-duplicable storage key and its parent have different
419 // name algorithm
420 // TPM_RC_KDF incorrect KDF specified for decrypting keyed hash object
421 // TPM_RC_KEY invalid key size values in an asymmetric key public area
422 // TPM_RCS_SCHEME inconsistent attributes 'decrypt', 'sign', 'restricted'
423 // and key's scheme ID; or hash algorithm is inconsistent
424 // with the scheme ID for keyed hash object
425 // TPM_RC_SYMMETRIC a storage key with no symmetric algorithm specified; or
426 // non-storage key with symmetric algorithm different from
427 // ALG_NULL
428 TPM_RC
429 SchemeChecks(
430 OBJECT *parentObject, // IN: parent (null if primary seed)
431 TPMT_PUBLIC *publicArea // IN: public area of the object
432 )
433 {
434 TPMT_SYM_DEF_OBJECT *symAlgs = NULL;
435 TPM_ALG_ID scheme = TPM_ALG_NULL;
436 TPMA_OBJECT attributes = publicArea->objectAttributes;
437 TPMU_PUBLIC_PARMS *parms = &publicArea->parameters;
438 //
439 switch(publicArea->type)
440 {
441 case ALG_SYMCIPHER_VALUE:
442 symAlgs = &parms->symDetail.sym;
443 // If this is a decrypt key, then only the block cipher modes (not
444 // SMAC) are valid. TPM_ALG_NULL is OK too. If this is a 'sign' key,
445 // then any mode that got through the unmarshaling is OK.
446 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt)
447 && !CryptSymModeIsValid(symAlgs->mode.sym, TRUE))
448 return TPM_RCS_SCHEME;
449 break;
450 case ALG_KEYEDHASH_VALUE:
451 scheme = parms->keyedHashDetail.scheme.scheme;
452 // if both sign and decrypt
453 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)
454 == IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
455 {
456 // if both sign and decrypt are set or clear, then need
457 // ALG_NULL as scheme
458 if(scheme != TPM_ALG_NULL)
459 return TPM_RCS_SCHEME;
460 }
461 else if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)
462 && scheme != TPM_ALG_HMAC)
463 return TPM_RCS_SCHEME;
464 else if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
465 {
466 if(scheme != TPM_ALG_XOR)
467 return TPM_RCS_SCHEME;
468 // If this is a derivation parent, then the KDF needs to be
469 // SP800-108 for this implementation. This is the only derivation
470 // supported by this implementation. Other implementations could
471 // support additional schemes. There is no default.
472 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted))
473 {
474 if(parms->keyedHashDetail.scheme.details.xor.kdf
475 != TPM_ALG_KDF1_SP800_108)
476 return TPM_RCS_SCHEME;
477 // Must select a digest.
478 if(CryptHashGetDigestSize(
479 parms->keyedHashDetail.scheme.details.xor.hashAlg) == 0)
480 return TPM_RCS_HASH;
481 }
482 }
483 break;
484 default: // handling for asymmetric
485 scheme = parms->asymDetail.scheme.scheme;
486 symAlgs = &parms->asymDetail.symmetric;
487 // if the key is both sign and decrypt, then the scheme must be
488 // ALG_NULL because there is no way to specify both a sign and a
489 // decrypt scheme in the key.
490 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)
491 == IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
492 {
493 // scheme must be TPM_ALG_NULL
494 if(scheme != TPM_ALG_NULL)
495 return TPM_RCS_SCHEME;
496 }
497 else if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign))
498 {
499 // If this is a signing key, see if it has a signing scheme
500 if(CryptIsAsymSignScheme(publicArea->type, scheme))
501 {
502 // if proper signing scheme then it needs a proper hash
503 if(parms->asymDetail.scheme.details.anySig.hashAlg
504 == TPM_ALG_NULL)
505 return TPM_RCS_SCHEME;
506 }
507 else
508 {
509 // signing key that does not have a proper signing scheme.
510 // This is OK if the key is not restricted and its scheme
511 // is TPM_ALG_NULL
512 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted)
513 || scheme != TPM_ALG_NULL)
514 return TPM_RCS_SCHEME;
515 }
516 }
517 else if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
518 {
519 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted))
520 {
521 // for a restricted decryption key (a parent), scheme
522 // is required to be TPM_ALG_NULL
523 if(scheme != TPM_ALG_NULL)
524 return TPM_RCS_SCHEME;
525 }
526 else
527 {
528 // For an unrestricted decryption key, the scheme has to
529 // be a valid scheme or TPM_ALG_NULL
530 if(scheme != TPM_ALG_NULL &&
531 !CryptIsAsymDecryptScheme(publicArea->type, scheme))
532 return TPM_RCS_SCHEME;
533 }
534 }
535 if(!IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted)
536 || !IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
537 {
538 // For an asymmetric key that is not a parent, the symmetric
539 // algorithms must be TPM_ALG_NULL
540 if(symAlgs->algorithm != TPM_ALG_NULL)
541 return TPM_RCS_SYMMETRIC;
542 }
543 // Special checks for an ECC key
544 #if ALG_ECC
545 if(publicArea->type == TPM_ALG_ECC)
546 {
547 TPM_ECC_CURVE curveID;
548 const TPMT_ECC_SCHEME *curveScheme;
549
550 curveID = publicArea->parameters.eccDetail.curveID;
551 curveScheme = CryptGetCurveSignScheme(curveID);
552 // The curveId must be valid or the unmarshaling is busted.
553 pAssert(curveScheme != NULL);
554
555 // If the curveID requires a specific scheme, then the key must
556 // select the same scheme
557 if(curveScheme->scheme != TPM_ALG_NULL)
558 {
559 TPMS_ECC_PARMS *ecc = &publicArea->parameters.eccDetail;
560 if(scheme != curveScheme->scheme)
561 return TPM_RCS_SCHEME;
562 // The scheme can allow any hash, or not...
563 if(curveScheme->details.anySig.hashAlg != TPM_ALG_NULL
564 && (ecc->scheme.details.anySig.hashAlg
565 != curveScheme->details.anySig.hashAlg))
566 return TPM_RCS_SCHEME;
567 }
568 // For now, the KDF must be TPM_ALG_NULL
569 if(publicArea->parameters.eccDetail.kdf.scheme != TPM_ALG_NULL)
570 return TPM_RCS_KDF;
571 }
572 #endif
573 break;
574 }
575 // If this is a restricted decryption key with symmetric algorithms, then it
576 // is an ordinary parent (not a derivation parent). It needs to specific
577 // symmetric algorithms other than TPM_ALG_NULL
578 if(symAlgs != NULL
579 && IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted)
580 && IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
581 {
582 if(symAlgs->algorithm == TPM_ALG_NULL)
583 return TPM_RCS_SYMMETRIC;
584 #if 0 //??
585 // This next check is under investigation. Need to see if it will break Windows
586 // before it is enabled. If it does not, then it should be default because a
587 // the mode used with a parent is always CFB and Part 2 indicates as much.
588 if(symAlgs->mode.sym != TPM_ALG_CFB)
589 return TPM_RCS_MODE;
590 #endif
591 // If this parent is not duplicable, then the symmetric algorithms
592 // (encryption and hash) must match those of its parent
593 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedParent)
594 && (parentObject != NULL))
595 {
596 if(publicArea->nameAlg != parentObject->publicArea.nameAlg)
597 return TPM_RCS_HASH;
598 if(!MemoryEqual(symAlgs, &parentObject->publicArea.parameters,
599 sizeof(TPMT_SYM_DEF_OBJECT)))
600 return TPM_RCS_SYMMETRIC;
601 }
602 }
603 return TPM_RC_SUCCESS;
604 }
605
606 //*** PublicAttributesValidation()
607 // This function validates the values in the public area of an object.
608 // This function is used in the processing of TPM2_Create, TPM2_CreatePrimary,
609 // TPM2_CreateLoaded(), TPM2_Load(), TPM2_Import(), and TPM2_LoadExternal().
610 // For TPM2_Import() this is only used if the new parent has fixedTPM SET. For
611 // TPM2_LoadExternal(), this is not used for a public-only key
612 // Return Type: TPM_RC
613 // TPM_RC_ATTRIBUTES 'fixedTPM', 'fixedParent', or 'encryptedDuplication'
614 // attributes are inconsistent between themselves or with
615 // those of the parent object;
616 // inconsistent 'restricted', 'decrypt' and 'sign'
617 // attributes;
618 // attempt to inject sensitive data for an asymmetric key;
619 // attempt to create a symmetric cipher key that is not
620 // a decryption key
621 // TPM_RC_HASH nameAlg is TPM_ALG_NULL
622 // TPM_RC_SIZE 'authPolicy' size does not match digest size of the name
623 // algorithm in 'publicArea'
624 // other returns from SchemeChecks()
625 TPM_RC
626 PublicAttributesValidation(
627 OBJECT *parentObject, // IN: input parent object
628 TPMT_PUBLIC *publicArea // IN: public area of the object
629 )
630 {
631 TPMA_OBJECT attributes = publicArea->objectAttributes;
632 TPMA_OBJECT parentAttributes = TPMA_ZERO_INITIALIZER();
633 //
634 if(parentObject != NULL)
635 parentAttributes = parentObject->publicArea.objectAttributes;
636 if(publicArea->nameAlg == TPM_ALG_NULL)
637 return TPM_RCS_HASH;
638 // If there is an authPolicy, it needs to be the size of the digest produced
639 // by the nameAlg of the object
640 if((publicArea->authPolicy.t.size != 0
641 && (publicArea->authPolicy.t.size
642 != CryptHashGetDigestSize(publicArea->nameAlg))))
643 return TPM_RCS_SIZE;
644 // If the parent is fixedTPM (including a Primary Object) the object must have
645 // the same value for fixedTPM and fixedParent
646 if(parentObject == NULL
647 || IS_ATTRIBUTE(parentAttributes, TPMA_OBJECT, fixedTPM))
648 {
649 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedParent)
650 != IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM))
651 return TPM_RCS_ATTRIBUTES;
652 }
653 else
654 {
655 // The parent is not fixedTPM so the object can't be fixedTPM
656 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM))
657 return TPM_RCS_ATTRIBUTES;
658 }
659 // See if sign and decrypt are the same
660 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)
661 == IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt))
662 {
663 // a restricted key cannot have both SET or both CLEAR
664 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted))
665 return TPM_RC_ATTRIBUTES;
666 // only a data object may have both sign and decrypt CLEAR
667 // BTW, since we know that decrypt==sign, no need to check both
668 if(publicArea->type != TPM_ALG_KEYEDHASH
669 && !IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign))
670 return TPM_RC_ATTRIBUTES;
671 }
672 // If the object can't be duplicated (directly or indirectly) then there
673 // is no justification for having encryptedDuplication SET
674 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM)
675 && IS_ATTRIBUTE(attributes, TPMA_OBJECT, encryptedDuplication))
676 return TPM_RCS_ATTRIBUTES;
677 // If a parent object has fixedTPM CLEAR, the child must have the
678 // same encryptedDuplication value as its parent.
679 // Primary objects are considered to have a fixedTPM parent (the seeds).
680 if(parentObject != NULL
681 && !IS_ATTRIBUTE(parentAttributes, TPMA_OBJECT, fixedTPM))
682 {
683 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, encryptedDuplication)
684 != IS_ATTRIBUTE(parentAttributes, TPMA_OBJECT, encryptedDuplication))
685 return TPM_RCS_ATTRIBUTES;
686 }
687 // Special checks for derived objects
688 if((parentObject != NULL) && (parentObject->attributes.derivation == SET))
689 {
690 // A derived object has the same settings for fixedTPM as its parent
691 if(IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM)
692 != IS_ATTRIBUTE(parentAttributes, TPMA_OBJECT, fixedTPM))
693 return TPM_RCS_ATTRIBUTES;
694 // A derived object is required to be fixedParent
695 if(!IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedParent))
696 return TPM_RCS_ATTRIBUTES;
697 }
698 return SchemeChecks(parentObject, publicArea);
699 }
700
701 //*** FillInCreationData()
702 // Fill in creation data for an object.
703 // Return Type: void
704 void
705 FillInCreationData(
706 TPMI_DH_OBJECT parentHandle, // IN: handle of parent
707 TPMI_ALG_HASH nameHashAlg, // IN: name hash algorithm
708 TPML_PCR_SELECTION *creationPCR, // IN: PCR selection
709 TPM2B_DATA *outsideData, // IN: outside data
710 TPM2B_CREATION_DATA *outCreation, // OUT: creation data for output
711 TPM2B_DIGEST *creationDigest // OUT: creation digest
712 )
713 {
714 BYTE creationBuffer[sizeof(TPMS_CREATION_DATA)];
715 BYTE *buffer;
716 HASH_STATE hashState;
717 //
718 // Fill in TPMS_CREATION_DATA in outCreation
719
720 // Compute PCR digest
721 PCRComputeCurrentDigest(nameHashAlg, creationPCR,
722 &outCreation->creationData.pcrDigest);
723
724 // Put back PCR selection list
725 outCreation->creationData.pcrSelect = *creationPCR;
726
727 // Get locality
728 outCreation->creationData.locality
729 = LocalityGetAttributes(_plat__LocalityGet());
730 outCreation->creationData.parentNameAlg = TPM_ALG_NULL;
731
732 // If the parent is either a primary seed or TPM_ALG_NULL, then the Name
733 // and QN of the parent are the parent's handle.
734 if(HandleGetType(parentHandle) == TPM_HT_PERMANENT)
735 {
736 buffer = &outCreation->creationData.parentName.t.name[0];
737 outCreation->creationData.parentName.t.size =
738 TPM_HANDLE_Marshal(&parentHandle, &buffer, NULL);
739 // For a primary or temporary object, the parent name (a handle) and the
740 // parent's QN are the same
741 outCreation->creationData.parentQualifiedName
742 = outCreation->creationData.parentName;
743 }
744 else // Regular object
745 {
746 OBJECT *parentObject = HandleToObject(parentHandle);
747 //
748 // Set name algorithm
749 outCreation->creationData.parentNameAlg = parentObject->publicArea.nameAlg;
750
751 // Copy parent name
752 outCreation->creationData.parentName = parentObject->name;
753
754 // Copy parent qualified name
755 outCreation->creationData.parentQualifiedName = parentObject->qualifiedName;
756 }
757 // Copy outside information
758 outCreation->creationData.outsideInfo = *outsideData;
759
760 // Marshal creation data to canonical form
761 buffer = creationBuffer;
762 outCreation->size = TPMS_CREATION_DATA_Marshal(&outCreation->creationData,
763 &buffer, NULL);
764 // Compute hash for creation field in public template
765 creationDigest->t.size = CryptHashStart(&hashState, nameHashAlg);
766 CryptDigestUpdate(&hashState, outCreation->size, creationBuffer);
767 CryptHashEnd2B(&hashState, &creationDigest->b);
768
769 return;
770 }
771
772 //*** GetSeedForKDF()
773 // Get a seed for KDF. The KDF for encryption and HMAC key use the same seed.
774 const TPM2B *
775 GetSeedForKDF(
776 OBJECT *protector // IN: the protector handle
777 )
778 {
779 // Get seed for encryption key. Use input seed if provided.
780 // Otherwise, using protector object's seedValue. TPM_RH_NULL is the only
781 // exception that we may not have a loaded object as protector. In such a
782 // case, use nullProof as seed.
783 if(protector == NULL)
784 return &gr.nullProof.b;
785 else
786 return &protector->sensitive.seedValue.b;
787 }
788
789 //*** ProduceOuterWrap()
790 // This function produce outer wrap for a buffer containing the sensitive data.
791 // It requires the sensitive data being marshaled to the outerBuffer, with the
792 // leading bytes reserved for integrity hash. If iv is used, iv space should
793 // be reserved at the beginning of the buffer. It assumes the sensitive data
794 // starts at address (outerBuffer + integrity size {+ iv size}).
795 // This function performs:
796 // 1. Add IV before sensitive area if required
797 // 2. encrypt sensitive data, if iv is required, encrypt by iv. otherwise,
798 // encrypted by a NULL iv
799 // 3. add HMAC integrity at the beginning of the buffer
800 // It returns the total size of blob with outer wrap
801 UINT16
802 ProduceOuterWrap(
803 OBJECT *protector, // IN: The handle of the object that provides
804 // protection. For object, it is parent
805 // handle. For credential, it is the handle
806 // of encrypt object.
807 TPM2B *name, // IN: the name of the object
808 TPM_ALG_ID hashAlg, // IN: hash algorithm for outer wrap
809 TPM2B *seed, // IN: an external seed may be provided for
810 // duplication blob. For non duplication
811 // blob, this parameter should be NULL
812 BOOL useIV, // IN: indicate if an IV is used
813 UINT16 dataSize, // IN: the size of sensitive data, excluding the
814 // leading integrity buffer size or the
815 // optional iv size
816 BYTE *outerBuffer // IN/OUT: outer buffer with sensitive data in
817 // it
818 )
819 {
820 TPM_ALG_ID symAlg;
821 UINT16 keyBits;
822 TPM2B_SYM_KEY symKey;
823 TPM2B_IV ivRNG; // IV from RNG
824 TPM2B_IV *iv = NULL;
825 UINT16 ivSize = 0; // size of iv area, including the size field
826 BYTE *sensitiveData; // pointer to the sensitive data
827 TPM2B_DIGEST integrity;
828 UINT16 integritySize;
829 BYTE *buffer; // Auxiliary buffer pointer
830 //
831 // Compute the beginning of sensitive data. The outer integrity should
832 // always exist if this function is called to make an outer wrap
833 integritySize = sizeof(UINT16) + CryptHashGetDigestSize(hashAlg);
834 sensitiveData = outerBuffer + integritySize;
835
836 // If iv is used, adjust the pointer of sensitive data and add iv before it
837 if(useIV)
838 {
839 ivSize = GetIV2BSize(protector);
840
841 // Generate IV from RNG. The iv data size should be the total IV area
842 // size minus the size of size field
843 ivRNG.t.size = ivSize - sizeof(UINT16);
844 CryptRandomGenerate(ivRNG.t.size, ivRNG.t.buffer);
845
846 // Marshal IV to buffer
847 buffer = sensitiveData;
848 TPM2B_IV_Marshal(&ivRNG, &buffer, NULL);
849
850 // adjust sensitive data starting after IV area
851 sensitiveData += ivSize;
852
853 // Use iv for encryption
854 iv = &ivRNG;
855 }
856 // Compute symmetric key parameters for outer buffer encryption
857 ComputeProtectionKeyParms(protector, hashAlg, name, seed,
858 &symAlg, &keyBits, &symKey);
859 // Encrypt inner buffer in place
860 CryptSymmetricEncrypt(sensitiveData, symAlg, keyBits,
861 symKey.t.buffer, iv, TPM_ALG_CFB, dataSize,
862 sensitiveData);
863 // Compute outer integrity. Integrity computation includes the optional IV
864 // area
865 ComputeOuterIntegrity(name, protector, hashAlg, seed, dataSize + ivSize,
866 outerBuffer + integritySize, &integrity);
867 // Add integrity at the beginning of outer buffer
868 buffer = outerBuffer;
869 TPM2B_DIGEST_Marshal(&integrity, &buffer, NULL);
870
871 // return the total size in outer wrap
872 return dataSize + integritySize + ivSize;
873 }
874
875 //*** UnwrapOuter()
876 // This function remove the outer wrap of a blob containing sensitive data
877 // This function performs:
878 // 1. check integrity of outer blob
879 // 2. decrypt outer blob
880 //
881 // Return Type: TPM_RC
882 // TPM_RCS_INSUFFICIENT error during sensitive data unmarshaling
883 // TPM_RCS_INTEGRITY sensitive data integrity is broken
884 // TPM_RCS_SIZE error during sensitive data unmarshaling
885 // TPM_RCS_VALUE IV size for CFB does not match the encryption
886 // algorithm block size
887 TPM_RC
888 UnwrapOuter(
889 OBJECT *protector, // IN: The object that provides
890 // protection. For object, it is parent
891 // handle. For credential, it is the
892 // encrypt object.
893 TPM2B *name, // IN: the name of the object
894 TPM_ALG_ID hashAlg, // IN: hash algorithm for outer wrap
895 TPM2B *seed, // IN: an external seed may be provided for
896 // duplication blob. For non duplication
897 // blob, this parameter should be NULL.
898 BOOL useIV, // IN: indicates if an IV is used
899 UINT16 dataSize, // IN: size of sensitive data in outerBuffer,
900 // including the leading integrity buffer
901 // size, and an optional iv area
902 BYTE *outerBuffer // IN/OUT: sensitive data
903 )
904 {
905 TPM_RC result;
906 TPM_ALG_ID symAlg = TPM_ALG_NULL;
907 TPM2B_SYM_KEY symKey;
908 UINT16 keyBits = 0;
909 TPM2B_IV ivIn; // input IV retrieved from input buffer
910 TPM2B_IV *iv = NULL;
911 BYTE *sensitiveData; // pointer to the sensitive data
912 TPM2B_DIGEST integrityToCompare;
913 TPM2B_DIGEST integrity;
914 INT32 size;
915 //
916 // Unmarshal integrity
917 sensitiveData = outerBuffer;
918 size = (INT32)dataSize;
919 result = TPM2B_DIGEST_Unmarshal(&integrity, &sensitiveData, &size);
920 if(result == TPM_RC_SUCCESS)
921 {
922 // Compute integrity to compare
923 ComputeOuterIntegrity(name, protector, hashAlg, seed,
924 (UINT16)size, sensitiveData,
925 &integrityToCompare);
926 // Compare outer blob integrity
927 if(!MemoryEqual2B(&integrity.b, &integrityToCompare.b))
928 return TPM_RCS_INTEGRITY;
929 // Get the symmetric algorithm parameters used for encryption
930 ComputeProtectionKeyParms(protector, hashAlg, name, seed,
931 &symAlg, &keyBits, &symKey);
932 // Retrieve IV if it is used
933 if(useIV)
934 {
935 result = TPM2B_IV_Unmarshal(&ivIn, &sensitiveData, &size);
936 if(result == TPM_RC_SUCCESS)
937 {
938 // The input iv size for CFB must match the encryption algorithm
939 // block size
940 if(ivIn.t.size != CryptGetSymmetricBlockSize(symAlg, keyBits))
941 result = TPM_RC_VALUE;
942 else
943 iv = &ivIn;
944 }
945 }
946 }
947 // If no errors, decrypt private in place. Since this function uses CFB,
948 // CryptSymmetricDecrypt() will not return any errors. It may fail but it will
949 // not return an error.
950 if(result == TPM_RC_SUCCESS)
951 CryptSymmetricDecrypt(sensitiveData, symAlg, keyBits,
952 symKey.t.buffer, iv, TPM_ALG_CFB,
953 (UINT16)size, sensitiveData);
954 return result;
955 }
956
957 //*** MarshalSensitive()
958 // This function is used to marshal a sensitive area. Among other things, it
959 // adjusts the size of the authValue to be no smaller than the digest of
960 // 'nameAlg'. It will also make sure that the RSA sensitive contains the right number
961 // of values.
962 // Returns the size of the marshaled area.
963 static UINT16
964 MarshalSensitive(
965 OBJECT *parent, // IN: the object parent (optional)
966 BYTE *buffer, // OUT: receiving buffer
967 TPMT_SENSITIVE *sensitive, // IN: the sensitive area to marshal
968 TPMI_ALG_HASH nameAlg // IN:
969 )
970 {
971 BYTE *sizeField = buffer; // saved so that size can be
972 // marshaled after it is known
973 UINT16 retVal;
974 //
975 // Pad the authValue if needed
976 MemoryPad2B(&sensitive->authValue.b, CryptHashGetDigestSize(nameAlg));
977 buffer += 2;
978
979 // Marshal the structure
980 #if ALG_RSA
981 // If the sensitive size is the special case for a prime in the type
982 if((sensitive->sensitive.rsa.t.size & RSA_prime_flag) > 0)
983 {
984 UINT16 sizeSave = sensitive->sensitive.rsa.t.size;
985 //
986 // Turn off the flag that indicates that the sensitive->sensitive contains
987 // the CRT form of the exponent.
988 sensitive->sensitive.rsa.t.size &= ~(RSA_prime_flag);
989 // If the parent isn't fixedTPM, then truncate the sensitive data to be
990 // the size of the prime. Otherwise, leave it at the current size which
991 // is the full CRT size.
992 if(parent == NULL
993 || !IS_ATTRIBUTE(parent->publicArea.objectAttributes,
994 TPMA_OBJECT, fixedTPM))
995 sensitive->sensitive.rsa.t.size /= 5;
996 retVal = TPMT_SENSITIVE_Marshal(sensitive, &buffer, NULL);
997 // Restore the flag and the size.
998 sensitive->sensitive.rsa.t.size = sizeSave;
999 }
1000 else
1001 #endif
1002 retVal = TPMT_SENSITIVE_Marshal(sensitive, &buffer, NULL);
1003
1004 // Marshal the size
1005 retVal = (UINT16)(retVal + UINT16_Marshal(&retVal, &sizeField, NULL));
1006
1007 return retVal;
1008 }
1009
1010 //*** SensitiveToPrivate()
1011 // This function prepare the private blob for off the chip storage
1012 // The operations in this function:
1013 // 1. marshal TPM2B_SENSITIVE structure into the buffer of TPM2B_PRIVATE
1014 // 2. apply encryption to the sensitive area.
1015 // 3. apply outer integrity computation.
1016 void
1017 SensitiveToPrivate(
1018 TPMT_SENSITIVE *sensitive, // IN: sensitive structure
1019 TPM2B_NAME *name, // IN: the name of the object
1020 OBJECT *parent, // IN: The parent object
1021 TPM_ALG_ID nameAlg, // IN: hash algorithm in public area. This
1022 // parameter is used when parentHandle is
1023 // NULL, in which case the object is
1024 // temporary.
1025 TPM2B_PRIVATE *outPrivate // OUT: output private structure
1026 )
1027 {
1028 BYTE *sensitiveData; // pointer to the sensitive data
1029 UINT16 dataSize; // data blob size
1030 TPMI_ALG_HASH hashAlg; // hash algorithm for integrity
1031 UINT16 integritySize;
1032 UINT16 ivSize;
1033 //
1034 pAssert(name != NULL && name->t.size != 0);
1035
1036 // Find the hash algorithm for integrity computation
1037 if(parent == NULL)
1038 {
1039 // For Temporary Object, using self name algorithm
1040 hashAlg = nameAlg;
1041 }
1042 else
1043 {
1044 // Otherwise, using parent's name algorithm
1045 hashAlg = parent->publicArea.nameAlg;
1046 }
1047 // Starting of sensitive data without wrappers
1048 sensitiveData = outPrivate->t.buffer;
1049
1050 // Compute the integrity size
1051 integritySize = sizeof(UINT16) + CryptHashGetDigestSize(hashAlg);
1052
1053 // Reserve space for integrity
1054 sensitiveData += integritySize;
1055
1056 // Get iv size
1057 ivSize = GetIV2BSize(parent);
1058
1059 // Reserve space for iv
1060 sensitiveData += ivSize;
1061
1062 // Marshal the sensitive area including authValue size adjustments.
1063 dataSize = MarshalSensitive(parent, sensitiveData, sensitive, nameAlg);
1064
1065 //Produce outer wrap, including encryption and HMAC
1066 outPrivate->t.size = ProduceOuterWrap(parent, &name->b, hashAlg, NULL,
1067 TRUE, dataSize, outPrivate->t.buffer);
1068 return;
1069 }
1070
1071 //*** PrivateToSensitive()
1072 // Unwrap a input private area. Check the integrity, decrypt and retrieve data
1073 // to a sensitive structure.
1074 // The operations in this function:
1075 // 1. check the integrity HMAC of the input private area
1076 // 2. decrypt the private buffer
1077 // 3. unmarshal TPMT_SENSITIVE structure into the buffer of TPMT_SENSITIVE
1078 // Return Type: TPM_RC
1079 // TPM_RCS_INTEGRITY if the private area integrity is bad
1080 // TPM_RC_SENSITIVE unmarshal errors while unmarshaling TPMS_ENCRYPT
1081 // from input private
1082 // TPM_RCS_SIZE error during sensitive data unmarshaling
1083 // TPM_RCS_VALUE outer wrapper does not have an iV of the correct
1084 // size
1085 TPM_RC
1086 PrivateToSensitive(
1087 TPM2B *inPrivate, // IN: input private structure
1088 TPM2B *name, // IN: the name of the object
1089 OBJECT *parent, // IN: parent object
1090 TPM_ALG_ID nameAlg, // IN: hash algorithm in public area. It is
1091 // passed separately because we only pass
1092 // name, rather than the whole public area
1093 // of the object. This parameter is used in
1094 // the following two cases: 1. primary
1095 // objects. 2. duplication blob with inner
1096 // wrap. In other cases, this parameter
1097 // will be ignored
1098 TPMT_SENSITIVE *sensitive // OUT: sensitive structure
1099 )
1100 {
1101 TPM_RC result;
1102 BYTE *buffer;
1103 INT32 size;
1104 BYTE *sensitiveData; // pointer to the sensitive data
1105 UINT16 dataSize;
1106 UINT16 dataSizeInput;
1107 TPMI_ALG_HASH hashAlg; // hash algorithm for integrity
1108 UINT16 integritySize;
1109 UINT16 ivSize;
1110 //
1111 // Make sure that name is provided
1112 pAssert(name != NULL && name->size != 0);
1113
1114 // Find the hash algorithm for integrity computation
1115 // For Temporary Object (parent == NULL) use self name algorithm;
1116 // Otherwise, using parent's name algorithm
1117 hashAlg = (parent == NULL) ? nameAlg : parent->publicArea.nameAlg;
1118
1119 // unwrap outer
1120 result = UnwrapOuter(parent, name, hashAlg, NULL, TRUE,
1121 inPrivate->size, inPrivate->buffer);
1122 if(result != TPM_RC_SUCCESS)
1123 return result;
1124 // Compute the inner integrity size.
1125 integritySize = sizeof(UINT16) + CryptHashGetDigestSize(hashAlg);
1126
1127 // Get iv size
1128 ivSize = GetIV2BSize(parent);
1129
1130 // The starting of sensitive data and data size without outer wrapper
1131 sensitiveData = inPrivate->buffer + integritySize + ivSize;
1132 dataSize = inPrivate->size - integritySize - ivSize;
1133
1134 // Unmarshal input data size
1135 buffer = sensitiveData;
1136 size = (INT32)dataSize;
1137 result = UINT16_Unmarshal(&dataSizeInput, &buffer, &size);
1138 if(result == TPM_RC_SUCCESS)
1139 {
1140 if((dataSizeInput + sizeof(UINT16)) != dataSize)
1141 result = TPM_RC_SENSITIVE;
1142 else
1143 {
1144 // Unmarshal sensitive buffer to sensitive structure
1145 result = TPMT_SENSITIVE_Unmarshal(sensitive, &buffer, &size);
1146 if(result != TPM_RC_SUCCESS || size != 0)
1147 {
1148 result = TPM_RC_SENSITIVE;
1149 }
1150 }
1151 }
1152 return result;
1153 }
1154
1155 //*** SensitiveToDuplicate()
1156 // This function prepare the duplication blob from the sensitive area.
1157 // The operations in this function:
1158 // 1. marshal TPMT_SENSITIVE structure into the buffer of TPM2B_PRIVATE
1159 // 2. apply inner wrap to the sensitive area if required
1160 // 3. apply outer wrap if required
1161 void
1162 SensitiveToDuplicate(
1163 TPMT_SENSITIVE *sensitive, // IN: sensitive structure
1164 TPM2B *name, // IN: the name of the object
1165 OBJECT *parent, // IN: The new parent object
1166 TPM_ALG_ID nameAlg, // IN: hash algorithm in public area. It
1167 // is passed separately because we
1168 // only pass name, rather than the
1169 // whole public area of the object.
1170 TPM2B *seed, // IN: the external seed. If external
1171 // seed is provided with size of 0,
1172 // no outer wrap should be applied
1173 // to duplication blob.
1174 TPMT_SYM_DEF_OBJECT *symDef, // IN: Symmetric key definition. If the
1175 // symmetric key algorithm is NULL,
1176 // no inner wrap should be applied.
1177 TPM2B_DATA *innerSymKey, // IN/OUT: a symmetric key may be
1178 // provided to encrypt the inner
1179 // wrap of a duplication blob. May
1180 // be generated here if needed.
1181 TPM2B_PRIVATE *outPrivate // OUT: output private structure
1182 )
1183 {
1184 BYTE *sensitiveData; // pointer to the sensitive data
1185 TPMI_ALG_HASH outerHash = TPM_ALG_NULL;// The hash algorithm for outer wrap
1186 TPMI_ALG_HASH innerHash = TPM_ALG_NULL;// The hash algorithm for inner wrap
1187 UINT16 dataSize; // data blob size
1188 BOOL doInnerWrap = FALSE;
1189 BOOL doOuterWrap = FALSE;
1190 //
1191 // Make sure that name is provided
1192 pAssert(name != NULL && name->size != 0);
1193
1194 // Make sure symDef and innerSymKey are not NULL
1195 pAssert(symDef != NULL && innerSymKey != NULL);
1196
1197 // Starting of sensitive data without wrappers
1198 sensitiveData = outPrivate->t.buffer;
1199
1200 // Find out if inner wrap is required
1201 if(symDef->algorithm != TPM_ALG_NULL)
1202 {
1203 doInnerWrap = TRUE;
1204
1205 // Use self nameAlg as inner hash algorithm
1206 innerHash = nameAlg;
1207
1208 // Adjust sensitive data pointer
1209 sensitiveData += sizeof(UINT16) + CryptHashGetDigestSize(innerHash);
1210 }
1211 // Find out if outer wrap is required
1212 if(seed->size != 0)
1213 {
1214 doOuterWrap = TRUE;
1215
1216 // Use parent nameAlg as outer hash algorithm
1217 outerHash = parent->publicArea.nameAlg;
1218
1219 // Adjust sensitive data pointer
1220 sensitiveData += sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1221 }
1222 // Marshal sensitive area
1223 dataSize = MarshalSensitive(NULL, sensitiveData, sensitive, nameAlg);
1224
1225 // Apply inner wrap for duplication blob. It includes both integrity and
1226 // encryption
1227 if(doInnerWrap)
1228 {
1229 BYTE *innerBuffer = NULL;
1230 BOOL symKeyInput = TRUE;
1231 innerBuffer = outPrivate->t.buffer;
1232 // Skip outer integrity space
1233 if(doOuterWrap)
1234 innerBuffer += sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1235 dataSize = ProduceInnerIntegrity(name, innerHash, dataSize,
1236 innerBuffer);
1237 // Generate inner encryption key if needed
1238 if(innerSymKey->t.size == 0)
1239 {
1240 innerSymKey->t.size = (symDef->keyBits.sym + 7) / 8;
1241 CryptRandomGenerate(innerSymKey->t.size, innerSymKey->t.buffer);
1242
1243 // TPM generates symmetric encryption. Set the flag to FALSE
1244 symKeyInput = FALSE;
1245 }
1246 else
1247 {
1248 // assume the input key size should matches the symmetric definition
1249 pAssert(innerSymKey->t.size == (symDef->keyBits.sym + 7) / 8);
1250 }
1251
1252 // Encrypt inner buffer in place
1253 CryptSymmetricEncrypt(innerBuffer, symDef->algorithm,
1254 symDef->keyBits.sym, innerSymKey->t.buffer, NULL,
1255 TPM_ALG_CFB, dataSize, innerBuffer);
1256
1257 // If the symmetric encryption key is imported, clear the buffer for
1258 // output
1259 if(symKeyInput)
1260 innerSymKey->t.size = 0;
1261 }
1262 // Apply outer wrap for duplication blob. It includes both integrity and
1263 // encryption
1264 if(doOuterWrap)
1265 {
1266 dataSize = ProduceOuterWrap(parent, name, outerHash, seed, FALSE,
1267 dataSize, outPrivate->t.buffer);
1268 }
1269 // Data size for output
1270 outPrivate->t.size = dataSize;
1271
1272 return;
1273 }
1274
1275 //*** DuplicateToSensitive()
1276 // Unwrap a duplication blob. Check the integrity, decrypt and retrieve data
1277 // to a sensitive structure.
1278 // The operations in this function:
1279 // 1. check the integrity HMAC of the input private area
1280 // 2. decrypt the private buffer
1281 // 3. unmarshal TPMT_SENSITIVE structure into the buffer of TPMT_SENSITIVE
1282 //
1283 // Return Type: TPM_RC
1284 // TPM_RC_INSUFFICIENT unmarshaling sensitive data from 'inPrivate' failed
1285 // TPM_RC_INTEGRITY 'inPrivate' data integrity is broken
1286 // TPM_RC_SIZE unmarshaling sensitive data from 'inPrivate' failed
1287 TPM_RC
1288 DuplicateToSensitive(
1289 TPM2B *inPrivate, // IN: input private structure
1290 TPM2B *name, // IN: the name of the object
1291 OBJECT *parent, // IN: the parent
1292 TPM_ALG_ID nameAlg, // IN: hash algorithm in public area.
1293 TPM2B *seed, // IN: an external seed may be provided.
1294 // If external seed is provided with
1295 // size of 0, no outer wrap is
1296 // applied
1297 TPMT_SYM_DEF_OBJECT *symDef, // IN: Symmetric key definition. If the
1298 // symmetric key algorithm is NULL,
1299 // no inner wrap is applied
1300 TPM2B *innerSymKey, // IN: a symmetric key may be provided
1301 // to decrypt the inner wrap of a
1302 // duplication blob.
1303 TPMT_SENSITIVE *sensitive // OUT: sensitive structure
1304 )
1305 {
1306 TPM_RC result;
1307 BYTE *buffer;
1308 INT32 size;
1309 BYTE *sensitiveData; // pointer to the sensitive data
1310 UINT16 dataSize;
1311 UINT16 dataSizeInput;
1312 //
1313 // Make sure that name is provided
1314 pAssert(name != NULL && name->size != 0);
1315
1316 // Make sure symDef and innerSymKey are not NULL
1317 pAssert(symDef != NULL && innerSymKey != NULL);
1318
1319 // Starting of sensitive data
1320 sensitiveData = inPrivate->buffer;
1321 dataSize = inPrivate->size;
1322
1323 // Find out if outer wrap is applied
1324 if(seed->size != 0)
1325 {
1326 // Use parent nameAlg as outer hash algorithm
1327 TPMI_ALG_HASH outerHash = parent->publicArea.nameAlg;
1328
1329 result = UnwrapOuter(parent, name, outerHash, seed, FALSE,
1330 dataSize, sensitiveData);
1331 if(result != TPM_RC_SUCCESS)
1332 return result;
1333 // Adjust sensitive data pointer and size
1334 sensitiveData += sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1335 dataSize -= sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1336 }
1337 // Find out if inner wrap is applied
1338 if(symDef->algorithm != TPM_ALG_NULL)
1339 {
1340 // assume the input key size matches the symmetric definition
1341 pAssert(innerSymKey->size == (symDef->keyBits.sym + 7) / 8);
1342
1343 // Decrypt inner buffer in place
1344 CryptSymmetricDecrypt(sensitiveData, symDef->algorithm,
1345 symDef->keyBits.sym, innerSymKey->buffer, NULL,
1346 TPM_ALG_CFB, dataSize, sensitiveData);
1347 // Check inner integrity
1348 result = CheckInnerIntegrity(name, nameAlg, dataSize, sensitiveData);
1349 if(result != TPM_RC_SUCCESS)
1350 return result;
1351 // Adjust sensitive data pointer and size
1352 sensitiveData += sizeof(UINT16) + CryptHashGetDigestSize(nameAlg);
1353 dataSize -= sizeof(UINT16) + CryptHashGetDigestSize(nameAlg);
1354 }
1355 // Unmarshal input data size
1356 buffer = sensitiveData;
1357 size = (INT32)dataSize;
1358 result = UINT16_Unmarshal(&dataSizeInput, &buffer, &size);
1359 if(result == TPM_RC_SUCCESS)
1360 {
1361 if((dataSizeInput + sizeof(UINT16)) != dataSize)
1362 result = TPM_RC_SIZE;
1363 else
1364 {
1365 // Unmarshal sensitive buffer to sensitive structure
1366 result = TPMT_SENSITIVE_Unmarshal(sensitive, &buffer, &size);
1367
1368 // if the results is OK make sure that all the data was unmarshaled
1369 if(result == TPM_RC_SUCCESS && size != 0)
1370 result = TPM_RC_SIZE;
1371 }
1372 }
1373 return result;
1374 }
1375
1376 //*** SecretToCredential()
1377 // This function prepare the credential blob from a secret (a TPM2B_DIGEST)
1378 // The operations in this function:
1379 // 1. marshal TPM2B_DIGEST structure into the buffer of TPM2B_ID_OBJECT
1380 // 2. encrypt the private buffer, excluding the leading integrity HMAC area
1381 // 3. compute integrity HMAC and append to the beginning of the buffer.
1382 // 4. Set the total size of TPM2B_ID_OBJECT buffer
1383 void
1384 SecretToCredential(
1385 TPM2B_DIGEST *secret, // IN: secret information
1386 TPM2B *name, // IN: the name of the object
1387 TPM2B *seed, // IN: an external seed.
1388 OBJECT *protector, // IN: the protector
1389 TPM2B_ID_OBJECT *outIDObject // OUT: output credential
1390 )
1391 {
1392 BYTE *buffer; // Auxiliary buffer pointer
1393 BYTE *sensitiveData; // pointer to the sensitive data
1394 TPMI_ALG_HASH outerHash; // The hash algorithm for outer wrap
1395 UINT16 dataSize; // data blob size
1396 //
1397 pAssert(secret != NULL && outIDObject != NULL);
1398
1399 // use protector's name algorithm as outer hash ????
1400 outerHash = protector->publicArea.nameAlg;
1401
1402 // Marshal secret area to credential buffer, leave space for integrity
1403 sensitiveData = outIDObject->t.credential
1404 + sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1405 // Marshal secret area
1406 buffer = sensitiveData;
1407 dataSize = TPM2B_DIGEST_Marshal(secret, &buffer, NULL);
1408
1409 // Apply outer wrap
1410 outIDObject->t.size = ProduceOuterWrap(protector, name, outerHash, seed, FALSE,
1411 dataSize, outIDObject->t.credential);
1412 return;
1413 }
1414
1415 //*** CredentialToSecret()
1416 // Unwrap a credential. Check the integrity, decrypt and retrieve data
1417 // to a TPM2B_DIGEST structure.
1418 // The operations in this function:
1419 // 1. check the integrity HMAC of the input credential area
1420 // 2. decrypt the credential buffer
1421 // 3. unmarshal TPM2B_DIGEST structure into the buffer of TPM2B_DIGEST
1422 //
1423 // Return Type: TPM_RC
1424 // TPM_RC_INSUFFICIENT error during credential unmarshaling
1425 // TPM_RC_INTEGRITY credential integrity is broken
1426 // TPM_RC_SIZE error during credential unmarshaling
1427 // TPM_RC_VALUE IV size does not match the encryption algorithm
1428 // block size
1429 TPM_RC
1430 CredentialToSecret(
1431 TPM2B *inIDObject, // IN: input credential blob
1432 TPM2B *name, // IN: the name of the object
1433 TPM2B *seed, // IN: an external seed.
1434 OBJECT *protector, // IN: the protector
1435 TPM2B_DIGEST *secret // OUT: secret information
1436 )
1437 {
1438 TPM_RC result;
1439 BYTE *buffer;
1440 INT32 size;
1441 TPMI_ALG_HASH outerHash; // The hash algorithm for outer wrap
1442 BYTE *sensitiveData; // pointer to the sensitive data
1443 UINT16 dataSize;
1444 //
1445 // use protector's name algorithm as outer hash
1446 outerHash = protector->publicArea.nameAlg;
1447
1448 // Unwrap outer, a TPM_RC_INTEGRITY error may be returned at this point
1449 result = UnwrapOuter(protector, name, outerHash, seed, FALSE,
1450 inIDObject->size, inIDObject->buffer);
1451 if(result == TPM_RC_SUCCESS)
1452 {
1453 // Compute the beginning of sensitive data
1454 sensitiveData = inIDObject->buffer
1455 + sizeof(UINT16) + CryptHashGetDigestSize(outerHash);
1456 dataSize = inIDObject->size
1457 - (sizeof(UINT16) + CryptHashGetDigestSize(outerHash));
1458 // Unmarshal secret buffer to TPM2B_DIGEST structure
1459 buffer = sensitiveData;
1460 size = (INT32)dataSize;
1461 result = TPM2B_DIGEST_Unmarshal(secret, &buffer, &size);
1462
1463 // If there were no other unmarshaling errors, make sure that the
1464 // expected amount of data was recovered
1465 if(result == TPM_RC_SUCCESS && size != 0)
1466 return TPM_RC_SIZE;
1467 }
1468 return result;
1469 }
1470
1471 //*** MemoryRemoveTrailingZeros()
1472 // This function is used to adjust the length of an authorization value.
1473 // It adjusts the size of the TPM2B so that it does not include octets
1474 // at the end of the buffer that contain zero.
1475 // The function returns the number of non-zero octets in the buffer.
1476 UINT16
1477 MemoryRemoveTrailingZeros(
1478 TPM2B_AUTH *auth // IN/OUT: value to adjust
1479 )
1480 {
1481 while((auth->t.size > 0) && (auth->t.buffer[auth->t.size - 1] == 0))
1482 auth->t.size--;
1483 return auth->t.size;
1484 }
1485
1486 //*** SetLabelAndContext()
1487 // This function sets the label and context for a derived key. It is possible
1488 // that 'label' or 'context' can end up being an Empty Buffer.
1489 TPM_RC
1490 SetLabelAndContext(
1491 TPMS_DERIVE *labelContext, // IN/OUT: the recovered label and
1492 // context
1493 TPM2B_SENSITIVE_DATA *sensitive // IN: the sensitive data
1494 )
1495 {
1496 TPMS_DERIVE sensitiveValue;
1497 TPM_RC result;
1498 INT32 size;
1499 BYTE *buff;
1500 //
1501 // Unmarshal a TPMS_DERIVE from the TPM2B_SENSITIVE_DATA buffer
1502 // If there is something to unmarshal...
1503 if(sensitive->t.size != 0)
1504 {
1505 size = sensitive->t.size;
1506 buff = sensitive->t.buffer;
1507 result = TPMS_DERIVE_Unmarshal(&sensitiveValue, &buff, &size);
1508 if(result != TPM_RC_SUCCESS)
1509 return result;
1510 // If there was a label in the public area leave it there, otherwise, copy
1511 // the new value
1512 if(labelContext->label.t.size == 0)
1513 MemoryCopy2B(&labelContext->label.b, &sensitiveValue.label.b,
1514 sizeof(labelContext->label.t.buffer));
1515 // if there was a context string in publicArea, it overrides
1516 if(labelContext->context.t.size == 0)
1517 MemoryCopy2B(&labelContext->context.b, &sensitiveValue.context.b,
1518 sizeof(labelContext->label.t.buffer));
1519 }
1520 return TPM_RC_SUCCESS;
1521 }
1522
1523 //*** UnmarshalToPublic()
1524 // Support function to unmarshal the template. This is used because the
1525 // Input may be a TPMT_TEMPLATE and that structure does not have the same
1526 // size as a TPMT_PUBLIC because of the difference between the 'unique' and
1527 // 'seed' fields.
1528 // If 'derive' is not NULL, then the 'seed' field is assumed to contain
1529 // a 'label' and 'context' that are unmarshaled into 'derive'.
1530 TPM_RC
1531 UnmarshalToPublic(
1532 TPMT_PUBLIC *tOut, // OUT: output
1533 TPM2B_TEMPLATE *tIn, // IN:
1534 BOOL derivation, // IN: indicates if this is for a derivation
1535 TPMS_DERIVE *labelContext// OUT: label and context if derivation
1536 )
1537 {
1538 BYTE *buffer = tIn->t.buffer;
1539 INT32 size = tIn->t.size;
1540 TPM_RC result;
1541 //
1542 // make sure that tOut is zeroed so that there are no remnants from previous
1543 // uses
1544 MemorySet(tOut, 0, sizeof(TPMT_PUBLIC));
1545 // Unmarshal the components of the TPMT_PUBLIC up to the unique field
1546 result = TPMI_ALG_PUBLIC_Unmarshal(&tOut->type, &buffer, &size);
1547 if(result != TPM_RC_SUCCESS)
1548 return result;
1549 result = TPMI_ALG_HASH_Unmarshal(&tOut->nameAlg, &buffer, &size, FALSE);
1550 if(result != TPM_RC_SUCCESS)
1551 return result;
1552 result = TPMA_OBJECT_Unmarshal(&tOut->objectAttributes, &buffer, &size);
1553 if(result != TPM_RC_SUCCESS)
1554 return result;
1555 result = TPM2B_DIGEST_Unmarshal(&tOut->authPolicy, &buffer, &size);
1556 if(result != TPM_RC_SUCCESS)
1557 return result;
1558 result = TPMU_PUBLIC_PARMS_Unmarshal(&tOut->parameters, &buffer, &size,
1559 tOut->type);
1560 if(result != TPM_RC_SUCCESS)
1561 return result;
1562 // Now unmarshal a TPMS_DERIVE if this is for derivation
1563 if(derivation)
1564 result = TPMS_DERIVE_Unmarshal(labelContext, &buffer, &size);
1565 else
1566 // otherwise, unmarshal a TPMU_PUBLIC_ID
1567 result = TPMU_PUBLIC_ID_Unmarshal(&tOut->unique, &buffer, &size,
1568 tOut->type);
1569 // Make sure the template was used up
1570 if((result == TPM_RC_SUCCESS) && (size != 0))
1571 result = TPM_RC_SIZE;
1572 return result;
1573 }
1574
1575
1576 //*** ObjectSetExternal()
1577 // Set the external attributes for an object.
1578 void
1579 ObjectSetExternal(
1580 OBJECT *object
1581 )
1582 {
1583 object->attributes.external = SET;
1584 }