serialize objects in NVMEM

Reference implementation stores OBJECT structures in NVRAM
unmarshaled, even though this structure layout is such that most of
its 1540 bytes remain unused by the object stored in the structure.

Marshaling the structure before storing it in NVMEM allows to save a
lot of room there.

To make sure that marshaling is not processing junk data, clear the
entire structure before allocating a new OBJECT.

This change is meant to be backwards compatible. When data is read
from NVMEM, in case its size is equal the size of OBJECT structure,
data is considered stored unmarshaled and is copied to the output
directly. If the stored size is smaller - unmarshaling function is
invoked.

BUG=chrome-os-partner:60502
TEST=tcg test suite passes (not that it exercises this a lot, just
     five instances of storing/retrieving objects for the entire
     suite). Will test on real tpm to verify NVMEM storage format
     backwards compatibility.

     Also tried taking a chrome os device through enterprise
     enrollment. With the old code after enrollment there is room for
     just two eviction objects left:

   # command to retrieve number of objects in nvmem(is in the last
   #  byte of the response)
   localhost ~ #  trunks_send --raw 80 01 00 00 00 16 00 00 01 7a 00\
        00 00 06 00 00 02 08 00 00 00 01
   80010000001B000000000100000006000000010000020800000003

   # command to retrieve how many objects the tpm estimates it is
   #  still possible to store in nvram (is in the last byte of the
   #  response)
   localhost ~ # trunks_send --raw 80 01 00 00 00 16 00 00 01 7a \
      00 00 00 06 00 00 02 09 00 00 00 01
   80010000001B000000000100000006000000010000020900000002

     with the new code after enrollment the responses the above commands
     are:

   80010000001B000000000100000006000000010000020800000003
   80010000001B000000000100000006000000010000020900000004

     That is with three objects stored there is room for 4 more
     objects.

    Also verified that the device enrolled with the old version of the
    cr50 firmware remains enrolled after firmware update, which
    demonstrates backward compatibility.

Change-Id: Ic2d5f902220b451523b740b57edb7867441d1faa
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/424171
Reviewed-by: Andrey Pronin <apronin@chromium.org>
3 files changed
tree: e8c14f3634be389b148314425c3ea23000ddfcab
  1. fuzz/
  2. generator/
  3. .gitignore
  4. _TPM_Hash_Data.c
  5. _TPM_Hash_Data_fp.h
  6. _TPM_Hash_End.c
  7. _TPM_Hash_End_fp.h
  8. _TPM_Hash_Start.c
  9. _TPM_Hash_Start_fp.h
  10. _TPM_Init.c
  11. _TPM_Init_fp.h
  12. ActivateCredential.c
  13. ActivateCredential_fp.h
  14. AlgorithmCap.c
  15. AlgorithmCap_fp.h
  16. Attest_spt.c
  17. Attest_spt_fp.h
  18. BaseTypes.h
  19. Bits.c
  20. bits.h
  21. Bits_fp.h
  22. bool.h
  23. Cancel.c
  24. Capabilities.h
  25. Certify.c
  26. Certify_fp.h
  27. CertifyCreation.c
  28. CertifyCreation_fp.h
  29. ChangeEPS.c
  30. ChangeEPS_fp.h
  31. ChangePPS.c
  32. ChangePPS_fp.h
  33. Clear.c
  34. Clear_fp.h
  35. ClearControl.c
  36. ClearControl_fp.h
  37. Clock.c
  38. ClockRateAdjust.c
  39. ClockRateAdjust_fp.h
  40. ClockSet.c
  41. ClockSet_fp.h
  42. CommandAttributeData.c
  43. CommandAudit.c
  44. CommandAudit_fp.h
  45. CommandCodeAttributes.c
  46. CommandCodeAttributes_fp.h
  47. CommandDispatcher.c
  48. CommandDispatcher_fp.h
  49. Commands_fp.h
  50. COMMIT-QUEUE.ini
  51. Commit.c
  52. Commit_fp.h
  53. Context_spt.c
  54. Context_spt_fp.h
  55. ContextLoad.c
  56. ContextLoad_fp.h
  57. ContextSave.c
  58. ContextSave_fp.h
  59. CpriCryptPri.c
  60. CpriCryptPri_fp.h
  61. CpriDataEcc.c
  62. CpriDataEcc.h
  63. CpriECC.c
  64. CpriECC_fp.h
  65. CpriHash.c
  66. CpriHash_fp.h
  67. CpriHashData.c
  68. CpriMisc.c
  69. CpriMisc_fp.h
  70. CpriRNG.c
  71. CpriRNG_fp.h
  72. CpriRSA.c
  73. CpriRSA_fp.h
  74. CpriSym.c
  75. CpriSym_fp.h
  76. Create.c
  77. Create_fp.h
  78. CreatePrimary.c
  79. CreatePrimary_fp.h
  80. CryptoEngine.h
  81. CryptSelfTest.c
  82. CryptSelfTest_fp.h
  83. CryptUtil.c
  84. CryptUtil_fp.h
  85. DA.c
  86. DA_fp.h
  87. DictionaryAttackLockReset.c
  88. DictionaryAttackLockReset_fp.h
  89. DictionaryAttackParameters.c
  90. DictionaryAttackParameters_fp.h
  91. DRTM.c
  92. Duplicate.c
  93. Duplicate_fp.h
  94. EC_Ephemeral.c
  95. EC_Ephemeral_fp.h
  96. ECC_Parameters.c
  97. ECC_Parameters_fp.h
  98. ECDH_KeyGen.c
  99. ECDH_KeyGen_fp.h
  100. ECDH_ZGen.c
  101. ECDH_ZGen_fp.h
  102. EncryptDecrypt.c
  103. EncryptDecrypt_fp.h
  104. Entity.c
  105. Entity_fp.h
  106. Entropy.c
  107. EventSequenceComplete.c
  108. EventSequenceComplete_fp.h
  109. EvictControl.c
  110. EvictControl_fp.h
  111. ExecCommand.c
  112. ExecCommand_fp.h
  113. FieldUpgradeData.c
  114. FieldUpgradeData_fp.h
  115. FieldUpgradeStart.c
  116. FieldUpgradeStart_fp.h
  117. FirmwareRead.c
  118. FirmwareRead_fp.h
  119. FlushContext.c
  120. FlushContext_fp.h
  121. GetCapability.c
  122. GetCapability_fp.h
  123. GetCommandAuditDigest.c
  124. GetCommandAuditDigest_fp.h
  125. GetCommandCodeString.c
  126. GetCommandCodeString_fp.h
  127. GetRandom.c
  128. GetRandom_fp.h
  129. GetSessionAuditDigest.c
  130. GetSessionAuditDigest_fp.h
  131. GetTestResult.c
  132. GetTestResult_fp.h
  133. GetTime.c
  134. GetTime_fp.h
  135. Global.c
  136. Global.h
  137. Handle.c
  138. Handle_fp.h
  139. HandleProcess.c
  140. HandleProcess_fp.h
  141. Hash.c
  142. Hash_fp.h
  143. HashSequenceStart.c
  144. HashSequenceStart_fp.h
  145. Hierarchy.c
  146. Hierarchy_fp.h
  147. HierarchyChangeAuth.c
  148. HierarchyChangeAuth_fp.h
  149. HierarchyControl.c
  150. HierarchyControl_fp.h
  151. HMAC.c
  152. HMAC_fp.h
  153. HMAC_Start.c
  154. HMAC_Start_fp.h
  155. Implementation.h
  156. Import.c
  157. Import_fp.h
  158. IncrementalSelfTest.c
  159. IncrementalSelfTest_fp.h
  160. InternalRoutines.h
  161. LICENSE
  162. Load.c
  163. Load_fp.h
  164. LoadExternal.c
  165. LoadExternal_fp.h
  166. Locality.c
  167. Locality_fp.h
  168. LocalityPlat.c
  169. MakeCredential.c
  170. MakeCredential_fp.h
  171. Makefile
  172. Manufacture.c
  173. Manufacture_fp.h
  174. Marshal_ActivateCredential.c
  175. Marshal_Certify.c
  176. Marshal_CertifyCreation.c
  177. Marshal_ChangeEPS.c
  178. Marshal_ChangePPS.c
  179. Marshal_Clear.c
  180. Marshal_ClearControl.c
  181. Marshal_ClockRateAdjust.c
  182. Marshal_ClockSet.c
  183. Marshal_Commit.c
  184. Marshal_ContextLoad.c
  185. Marshal_ContextSave.c
  186. Marshal_Create.c
  187. Marshal_CreatePrimary.c
  188. Marshal_DictionaryAttackLockReset.c
  189. Marshal_DictionaryAttackParameters.c
  190. Marshal_Duplicate.c
  191. Marshal_EC_Ephemeral.c
  192. Marshal_ECC_Parameters.c
  193. Marshal_ECDH_KeyGen.c
  194. Marshal_ECDH_ZGen.c
  195. Marshal_EncryptDecrypt.c
  196. Marshal_EventSequenceComplete.c
  197. Marshal_EvictControl.c
  198. Marshal_FieldUpgradeData.c
  199. Marshal_FieldUpgradeStart.c
  200. Marshal_FirmwareRead.c
  201. Marshal_FlushContext.c
  202. marshal_fp.h
  203. Marshal_GetCapability.c
  204. Marshal_GetCommandAuditDigest.c
  205. Marshal_GetRandom.c
  206. Marshal_GetSessionAuditDigest.c
  207. Marshal_GetTestResult.c
  208. Marshal_GetTime.c
  209. Marshal_Hash.c
  210. Marshal_HashSequenceStart.c
  211. Marshal_HierarchyChangeAuth.c
  212. Marshal_HierarchyControl.c
  213. Marshal_HMAC.c
  214. Marshal_HMAC_Start.c
  215. Marshal_Import.c
  216. Marshal_IncrementalSelfTest.c
  217. Marshal_Load.c
  218. Marshal_LoadExternal.c
  219. Marshal_MakeCredential.c
  220. Marshal_NV_Certify.c
  221. Marshal_NV_ChangeAuth.c
  222. Marshal_NV_DefineSpace.c
  223. Marshal_NV_Extend.c
  224. Marshal_NV_GlobalWriteLock.c
  225. Marshal_NV_Increment.c
  226. Marshal_NV_Read.c
  227. Marshal_NV_ReadLock.c
  228. Marshal_NV_ReadPublic.c
  229. Marshal_NV_SetBits.c
  230. Marshal_NV_UndefineSpace.c
  231. Marshal_NV_UndefineSpaceSpecial.c
  232. Marshal_NV_Write.c
  233. Marshal_NV_WriteLock.c
  234. Marshal_ObjectChangeAuth.c
  235. Marshal_PCR_Allocate.c
  236. Marshal_PCR_Event.c
  237. Marshal_PCR_Extend.c
  238. Marshal_PCR_Read.c
  239. Marshal_PCR_Reset.c
  240. Marshal_PCR_SetAuthPolicy.c
  241. Marshal_PCR_SetAuthValue.c
  242. Marshal_PolicyAuthorize.c
  243. Marshal_PolicyAuthValue.c
  244. Marshal_PolicyCommandCode.c
  245. Marshal_PolicyCounterTimer.c
  246. Marshal_PolicyCpHash.c
  247. Marshal_PolicyDuplicationSelect.c
  248. Marshal_PolicyGetDigest.c
  249. Marshal_PolicyLocality.c
  250. Marshal_PolicyNameHash.c
  251. Marshal_PolicyNV.c
  252. Marshal_PolicyNvWritten.c
  253. Marshal_PolicyOR.c
  254. Marshal_PolicyPassword.c
  255. Marshal_PolicyPCR.c
  256. Marshal_PolicyPhysicalPresence.c
  257. Marshal_PolicyRestart.c
  258. Marshal_PolicySecret.c
  259. Marshal_PolicySigned.c
  260. Marshal_PolicyTicket.c
  261. Marshal_PP_Commands.c
  262. Marshal_Quote.c
  263. Marshal_ReadClock.c
  264. Marshal_ReadPublic.c
  265. Marshal_Rewrap.c
  266. Marshal_RSA_Decrypt.c
  267. Marshal_RSA_Encrypt.c
  268. Marshal_SelfTest.c
  269. Marshal_SequenceComplete.c
  270. Marshal_SequenceUpdate.c
  271. Marshal_SetAlgorithmSet.c
  272. Marshal_SetCommandCodeAuditStatus.c
  273. Marshal_SetPrimaryPolicy.c
  274. Marshal_Shutdown.c
  275. Marshal_Sign.c
  276. Marshal_StartAuthSession.c
  277. Marshal_Startup.c
  278. Marshal_StirRandom.c
  279. marshal_test.c
  280. Marshal_TestParms.c
  281. Marshal_Unseal.c
  282. Marshal_VerifySignature.c
  283. Marshal_ZGen_2Phase.c
  284. MathFunctions.c
  285. MathFunctions_fp.h
  286. MemoryLib.c
  287. MemoryLib_fp.h
  288. NV.c
  289. NV_Certify.c
  290. NV_Certify_fp.h
  291. NV_ChangeAuth.c
  292. NV_ChangeAuth_fp.h
  293. NV_DefineSpace.c
  294. NV_DefineSpace_fp.h
  295. NV_Extend.c
  296. NV_Extend_fp.h
  297. NV_fp.h
  298. NV_GlobalWriteLock.c
  299. NV_GlobalWriteLock_fp.h
  300. NV_Increment.c
  301. NV_Increment_fp.h
  302. NV_Read.c
  303. NV_Read_fp.h
  304. NV_ReadLock.c
  305. NV_ReadLock_fp.h
  306. NV_ReadPublic.c
  307. NV_ReadPublic_fp.h
  308. NV_SetBits.c
  309. NV_SetBits_fp.h
  310. NV_spt.c
  311. NV_spt_fp.h
  312. NV_UndefineSpace.c
  313. NV_UndefineSpace_fp.h
  314. NV_UndefineSpaceSpecial.c
  315. NV_UndefineSpaceSpecial_fp.h
  316. NV_Write.c
  317. NV_Write_fp.h
  318. NV_WriteLock.c
  319. NV_WriteLock_fp.h
  320. NVMem.c
  321. Object.c
  322. Object_fp.h
  323. Object_spt.c
  324. Object_spt_fp.h
  325. ObjectChangeAuth.c
  326. ObjectChangeAuth_fp.h
  327. OsslCryptoEngine.h
  328. parsep3
  329. parsep4
  330. PCR.c
  331. PCR_Allocate.c
  332. PCR_Allocate_fp.h
  333. PCR_Event.c
  334. PCR_Event_fp.h
  335. PCR_Extend.c
  336. PCR_Extend_fp.h
  337. PCR_fp.h
  338. PCR_Read.c
  339. PCR_Read_fp.h
  340. PCR_Reset.c
  341. PCR_Reset_fp.h
  342. PCR_SetAuthPolicy.c
  343. PCR_SetAuthPolicy_fp.h
  344. PCR_SetAuthValue.c
  345. PCR_SetAuthValue_fp.h
  346. Platform.h
  347. PlatformData.c
  348. PlatformData.h
  349. Policy_spt.c
  350. Policy_spt_fp.h
  351. PolicyAuthorize.c
  352. PolicyAuthorize_fp.h
  353. PolicyAuthValue.c
  354. PolicyAuthValue_fp.h
  355. PolicyCommandCode.c
  356. PolicyCommandCode_fp.h
  357. PolicyCounterTimer.c
  358. PolicyCounterTimer_fp.h
  359. PolicyCpHash.c
  360. PolicyCpHash_fp.h
  361. PolicyDuplicationSelect.c
  362. PolicyDuplicationSelect_fp.h
  363. PolicyGetDigest.c
  364. PolicyGetDigest_fp.h
  365. PolicyLocality.c
  366. PolicyLocality_fp.h
  367. PolicyNameHash.c
  368. PolicyNameHash_fp.h
  369. PolicyNV.c
  370. PolicyNV_fp.h
  371. PolicyNvWritten.c
  372. PolicyNvWritten_fp.h
  373. PolicyOR.c
  374. PolicyOR_fp.h
  375. PolicyPassword.c
  376. PolicyPassword_fp.h
  377. PolicyPCR.c
  378. PolicyPCR_fp.h
  379. PolicyPhysicalPresence.c
  380. PolicyPhysicalPresence_fp.h
  381. PolicyRestart.c
  382. PolicyRestart_fp.h
  383. PolicySecret.c
  384. PolicySecret_fp.h
  385. PolicySigned.c
  386. PolicySigned_fp.h
  387. PolicyTicket.c
  388. PolicyTicket_fp.h
  389. Power.c
  390. Power_fp.h
  391. PowerPlat.c
  392. PP.c
  393. PP_Commands.c
  394. PP_Commands_fp.h
  395. PP_fp.h
  396. PPPlat.c
  397. PRESUBMIT.cfg
  398. PropertyCap.c
  399. PropertyCap_fp.h
  400. Quote.c
  401. Quote_fp.h
  402. ReadClock.c
  403. ReadClock_fp.h
  404. README
  405. ReadPublic.c
  406. ReadPublic_fp.h
  407. Rewrap.c
  408. Rewrap_fp.h
  409. RSA_Decrypt.c
  410. RSA_Decrypt_fp.h
  411. RSA_Encrypt.c
  412. RSA_Encrypt_fp.h
  413. RSAData.c
  414. RSAKeySieve.c
  415. RSAKeySieve.h
  416. RSAKeySieve_fp.h
  417. SelfTest.c
  418. SelfTest_fp.h
  419. SequenceComplete.c
  420. SequenceComplete_fp.h
  421. SequenceUpdate.c
  422. SequenceUpdate_fp.h
  423. Session.c
  424. Session_fp.h
  425. SessionProcess.c
  426. SessionProcess_fp.h
  427. SetAlgorithmSet.c
  428. SetAlgorithmSet_fp.h
  429. SetCommandCodeAuditStatus.c
  430. SetCommandCodeAuditStatus_fp.h
  431. SetPrimaryPolicy.c
  432. SetPrimaryPolicy_fp.h
  433. Shutdown.c
  434. Shutdown_fp.h
  435. Sign.c
  436. Sign_fp.h
  437. StartAuthSession.c
  438. StartAuthSession_fp.h
  439. Startup.c
  440. Startup_fp.h
  441. StirRandom.c
  442. StirRandom_fp.h
  443. stubs_ecc.c
  444. stubs_hash.c
  445. stubs_sym.c
  446. swap.h
  447. TcpServer.c
  448. TestParms.c
  449. TestParms_fp.h
  450. Ticket.c
  451. Ticket_fp.h
  452. Time.c
  453. Time_fp.h
  454. Tpm.h
  455. tpm_generated.c
  456. tpm_generated.h
  457. TPM_Types.h
  458. tpm_types.h
  459. TPMB.h
  460. TpmBuildSwitches.h
  461. TPMCmdp.c
  462. TPMCmds.c
  463. TpmError.h
  464. TpmFail.c
  465. TpmFail_fp.h
  466. TpmTcpProtocol.h
  467. Unique.c
  468. Unique_fp.h
  469. Unseal.c
  470. Unseal_fp.h
  471. VendorString.h
  472. VerifySignature.c
  473. VerifySignature_fp.h
  474. ZGen_2Phase.c
  475. ZGen_2Phase_fp.h