@inco/lightning 0.3.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +2 -8
  2. package/manifest.yaml +24 -11
  3. package/package.json +2 -5
  4. package/src/DeployUtils.sol +113 -77
  5. package/src/IIncoLightning.sol +16 -9
  6. package/src/IncoLightning.sol +18 -9
  7. package/src/IncoVerifier.sol +47 -0
  8. package/src/Lib.alphanet.sol +14 -15
  9. package/src/Lib.demonet.sol +14 -15
  10. package/src/Lib.devnet.sol +14 -15
  11. package/src/Lib.sol +14 -15
  12. package/src/Lib.template.sol +15 -44
  13. package/src/Lib.testnet.sol +14 -15
  14. package/src/Types.sol +7 -0
  15. package/src/interfaces/IIncoLightning.sol +20 -0
  16. package/src/interfaces/IIncoVerifier.sol +24 -0
  17. package/src/interfaces/automata-interfaces/BELE.sol +20 -0
  18. package/src/interfaces/automata-interfaces/IAutomataEnclaveIdentityDao.sol +28 -0
  19. package/src/interfaces/automata-interfaces/IFmspcTcbDao.sol +10 -0
  20. package/src/interfaces/automata-interfaces/IPCCSRouter.sol +94 -0
  21. package/src/interfaces/automata-interfaces/IPCCSRouterExtended.sol +10 -0
  22. package/src/interfaces/automata-interfaces/IPcsDao.sol +18 -0
  23. package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +34 -0
  24. package/src/interfaces/automata-interfaces/Types.sol +193 -0
  25. package/src/libs/incoLightning_alphanet_v0_297966649.sol +14 -15
  26. package/src/libs/incoLightning_demonet_v0_863421733.sol +14 -15
  27. package/src/libs/incoLightning_devnet_v0_340846814.sol +14 -15
  28. package/src/libs/incoLightning_testnet_v0_183408998.sol +14 -15
  29. package/src/libs/incoLightning_testnet_v1_938327937.sol +451 -0
  30. package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +133 -0
  31. package/src/lightning-parts/AccessControl/AdvancedAccessControl.types.sol +18 -0
  32. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +42 -3
  33. package/src/lightning-parts/AccessControl/interfaces/IAdvancedAccessControl.sol +25 -0
  34. package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +24 -0
  35. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +234 -0
  36. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +16 -2
  37. package/src/lightning-parts/DecryptionAttester.sol +45 -0
  38. package/src/lightning-parts/DecryptionAttester.types.sol +7 -0
  39. package/src/lightning-parts/EncryptedInput.sol +10 -13
  40. package/src/lightning-parts/EncryptedOperations.sol +78 -28
  41. package/src/lightning-parts/TEELifecycle.sol +180 -88
  42. package/src/lightning-parts/TrivialEncryption.sol +9 -9
  43. package/src/lightning-parts/interfaces/IDecryptionAttester.sol +9 -0
  44. package/src/lightning-parts/interfaces/IEncryptedInput.sol +19 -0
  45. package/src/lightning-parts/interfaces/IEncryptedOperations.sol +31 -0
  46. package/src/lightning-parts/interfaces/ITEELifecycle.sol +26 -0
  47. package/src/lightning-parts/interfaces/ITrivialEncryption.sol +10 -0
  48. package/src/lightning-parts/primitives/EventCounter.sol +15 -3
  49. package/src/lightning-parts/primitives/HandleGeneration.sol +8 -3
  50. package/src/lightning-parts/primitives/HandleMetadata.sol +1 -1
  51. package/src/lightning-parts/primitives/LightningAddressGetter.sol +10 -0
  52. package/src/lightning-parts/primitives/SignatureVerifier.sol +2 -9
  53. package/src/lightning-parts/primitives/VerifierAddressGetter.sol +13 -0
  54. package/src/lightning-parts/primitives/{EventCounter.gen.sol → interfaces/IEventCounter.sol} +4 -2
  55. package/src/lightning-parts/primitives/interfaces/IHandleGeneration.sol +41 -0
  56. package/src/lightning-parts/primitives/interfaces/ISignatureVerifier.sol +9 -0
  57. package/src/lightning-parts/primitives/interfaces/IVerifierAddressGetter.sol +8 -0
  58. package/src/lightning-parts/test/HandleMetadata.t.sol +25 -6
  59. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +78 -0
  60. package/src/periphery/SessionVerifier.sol +63 -0
  61. package/src/test/AddTwo.sol +20 -24
  62. package/src/test/FakeIncoInfra/FakeDecryptionAttester.sol +198 -0
  63. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +0 -15
  64. package/src/test/FakeIncoInfra/FakeQuoteVerifier.sol +10 -6
  65. package/src/test/FakeIncoInfra/MockOpHandler.sol +0 -7
  66. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +19 -7
  67. package/src/test/FakeIncoInfra/getOpForSelector.sol +0 -3
  68. package/src/test/IncoTest.sol +12 -11
  69. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +98 -77
  70. package/src/test/TestAddTwo.t.sol +16 -9
  71. package/src/test/TestFakeInfra.t.sol +10 -27
  72. package/src/test/TestUpgrade.t.sol +11 -4
  73. package/src/test/TestVersion.t.sol +0 -7
  74. package/src/version/IncoLightningConfig.sol +4 -3
  75. package/src/version/SessionVerifierConfig.sol +8 -0
  76. package/src/version/Version.sol +7 -9
  77. package/src/version/interfaces/IVersion.sol +8 -0
  78. package/src/DeployTEE.sol +0 -153
  79. package/src/IncoLightning.gen.sol +0 -15
  80. package/src/lightning-parts/AccessControl/BaseAccessControlList.gen.sol +0 -19
  81. package/src/lightning-parts/DecryptionHandler.gen.sol +0 -54
  82. package/src/lightning-parts/DecryptionHandler.sol +0 -307
  83. package/src/lightning-parts/DecryptionHandler.types.sol +0 -34
  84. package/src/lightning-parts/EncryptedInput.gen.sol +0 -16
  85. package/src/lightning-parts/EncryptedOperations.gen.sol +0 -59
  86. package/src/lightning-parts/TEELifecycle.gen.sol +0 -58
  87. package/src/lightning-parts/TrivialEncryption.gen.sol +0 -15
  88. package/src/lightning-parts/primitives/HandleGeneration.gen.sol +0 -19
  89. package/src/lightning-parts/primitives/HandleMetadata.gen.sol +0 -4
  90. package/src/lightning-parts/primitives/SignatureVerifier.gen.sol +0 -16
  91. package/src/test/FibonacciDecrypt.sol +0 -49
  92. package/src/test/TEELifecycle/TEELifecycleHWTest.t.sol +0 -119
  93. package/src/test/TEELifecycle/addnode_data/eoa.txt +0 -1
  94. package/src/test/TEELifecycle/addnode_data/quote.bin +0 -0
  95. package/src/test/TEELifecycle/bootstrap_data/ecies_pubkey.bin +0 -1
  96. package/src/test/TEELifecycle/bootstrap_data/eip712_signature.bin +0 -1
  97. package/src/test/TEELifecycle/bootstrap_data/eoa.txt +0 -1
  98. package/src/test/TEELifecycle/bootstrap_data/qe_identity +0 -1
  99. package/src/test/TEELifecycle/bootstrap_data/qe_identity_signature.bin +0 -1
  100. package/src/test/TEELifecycle/bootstrap_data/quote.bin +0 -0
  101. package/src/test/TEELifecycle/bootstrap_data/tcb_info +0 -1
  102. package/src/test/TEELifecycle/bootstrap_data/tcb_info_signature.bin +0 -1
  103. package/src/test/TEELifecycle/test_cert/AttestationReportSigningCA.crl +0 -0
  104. package/src/test/TEELifecycle/test_cert/Intel_SGX_Attestation_RootCA.cer +0 -0
  105. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_CRL.crl +0 -0
  106. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_PlatformCA.cer +0 -0
  107. package/src/test/TEELifecycle/test_cert/Intel_SGX_TCB_Signing.cer +0 -0
  108. package/src/version/Version.gen.sol +0 -14
@@ -5,10 +5,10 @@ import {euint256, ebool, EOps, SenderNotAllowedForHandle, ETypes, isTypeSupporte
5
5
  import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
6
6
  import {EventCounter} from "./primitives/EventCounter.sol";
7
7
  import {HandleGeneration} from "./primitives/HandleGeneration.sol";
8
- import {IEncryptedOperationsGen} from "./EncryptedOperations.gen.sol";
8
+ import {IEncryptedOperations} from "./interfaces/IEncryptedOperations.sol";
9
9
 
10
10
  abstract contract EncryptedOperations is
11
- IEncryptedOperationsGen,
11
+ IEncryptedOperations,
12
12
  BaseAccessControlList,
13
13
  EventCounter,
14
14
  HandleGeneration
@@ -248,7 +248,9 @@ abstract contract EncryptedOperations is
248
248
  euint256.unwrap(rhs)
249
249
  )
250
250
  );
251
- emit EAdd(lhs, rhs, result, getNewEventId());
251
+ uint256 id = getNextEventId();
252
+ emit EAdd(lhs, rhs, result, id);
253
+ setDigest(abi.encodePacked(result, id));
252
254
  }
253
255
 
254
256
  function eSub(
@@ -263,7 +265,9 @@ abstract contract EncryptedOperations is
263
265
  euint256.unwrap(rhs)
264
266
  )
265
267
  );
266
- emit ESub(lhs, rhs, result, getNewEventId());
268
+ uint256 id = getNextEventId();
269
+ emit ESub(lhs, rhs, result, id);
270
+ setDigest(abi.encodePacked(result, id));
267
271
  }
268
272
 
269
273
  function eMul(
@@ -278,8 +282,10 @@ abstract contract EncryptedOperations is
278
282
  euint256.unwrap(rhs)
279
283
  )
280
284
  );
281
- emit EMul(lhs, rhs, result, getNewEventId());
282
- }
285
+ uint256 id = getNextEventId();
286
+ emit EMul(lhs, rhs, result, id);
287
+ setDigest(abi.encodePacked(result, id));
288
+ }
283
289
 
284
290
  function eDiv(
285
291
  euint256 lhs,
@@ -293,7 +299,9 @@ abstract contract EncryptedOperations is
293
299
  euint256.unwrap(rhs)
294
300
  )
295
301
  );
296
- emit EDiv(lhs, rhs, result, getNewEventId());
302
+ uint256 id = getNextEventId();
303
+ emit EDiv(lhs, rhs, result, id);
304
+ setDigest(abi.encodePacked(result, id));
297
305
  }
298
306
 
299
307
  function eRem(
@@ -308,7 +316,9 @@ abstract contract EncryptedOperations is
308
316
  euint256.unwrap(rhs)
309
317
  )
310
318
  );
311
- emit ERem(lhs, rhs, result, getNewEventId());
319
+ uint256 id = getNextEventId();
320
+ emit ERem(lhs, rhs, result, id);
321
+ setDigest(abi.encodePacked(result, id));
312
322
  }
313
323
 
314
324
  function eBitAnd(
@@ -321,7 +331,9 @@ abstract contract EncryptedOperations is
321
331
  checkInput(rhs, typeToBitMask(rhsType));
322
332
  require(lhsType == rhsType, UnexpectedType(lhsType, typeToBitMask(rhsType)));
323
333
  result = createResultHandle(EOps.BitAnd, lhsType, lhs, rhs);
324
- emit EBitAnd(lhs, rhs, result, getNewEventId());
334
+ uint256 id = getNextEventId();
335
+ emit EBitAnd(lhs, rhs, result, id);
336
+ setDigest(abi.encodePacked(result, id));
325
337
  }
326
338
 
327
339
  function eBitOr(
@@ -334,7 +346,9 @@ abstract contract EncryptedOperations is
334
346
  checkInput(rhs, typeToBitMask(rhsType));
335
347
  require(lhsType == rhsType, UnexpectedType(lhsType, typeToBitMask(rhsType)));
336
348
  result = createResultHandle(EOps.BitOr, lhsType, lhs, rhs);
337
- emit EBitOr(lhs, rhs, result, getNewEventId());
349
+ uint256 id = getNextEventId();
350
+ emit EBitOr(lhs, rhs, result, id);
351
+ setDigest(abi.encodePacked(result, id));
338
352
  }
339
353
 
340
354
  function eBitXor(
@@ -347,7 +361,9 @@ abstract contract EncryptedOperations is
347
361
  checkInput(rhs, typeToBitMask(rhsType));
348
362
  require(lhsType == rhsType, UnexpectedType(lhsType, typeToBitMask(rhsType)));
349
363
  result = createResultHandle(EOps.BitXor, lhsType, lhs, rhs);
350
- emit EBitXor(lhs, rhs, result, getNewEventId());
364
+ uint256 id = getNextEventId();
365
+ emit EBitXor(lhs, rhs, result, id);
366
+ setDigest(abi.encodePacked(result, id));
351
367
  }
352
368
 
353
369
  function eShl(
@@ -362,7 +378,9 @@ abstract contract EncryptedOperations is
362
378
  euint256.unwrap(rhs)
363
379
  )
364
380
  );
365
- emit EShl(lhs, rhs, result, getNewEventId());
381
+ uint256 id = getNextEventId();
382
+ emit EShl(lhs, rhs, result, id);
383
+ setDigest(abi.encodePacked(result, id));
366
384
  }
367
385
 
368
386
  function eShr(
@@ -377,7 +395,9 @@ abstract contract EncryptedOperations is
377
395
  euint256.unwrap(rhs)
378
396
  )
379
397
  );
380
- emit EShr(lhs, rhs, result, getNewEventId());
398
+ uint256 id = getNextEventId();
399
+ emit EShr(lhs, rhs, result, id);
400
+ setDigest(abi.encodePacked(result, id));
381
401
  }
382
402
 
383
403
  function eRotl(
@@ -392,7 +412,9 @@ abstract contract EncryptedOperations is
392
412
  euint256.unwrap(rhs)
393
413
  )
394
414
  );
395
- emit ERotl(lhs, rhs, result, getNewEventId());
415
+ uint256 id = getNextEventId();
416
+ emit ERotl(lhs, rhs, result, id);
417
+ setDigest(abi.encodePacked(result, id));
396
418
  }
397
419
 
398
420
  function eRotr(
@@ -407,7 +429,9 @@ abstract contract EncryptedOperations is
407
429
  euint256.unwrap(rhs)
408
430
  )
409
431
  );
410
- emit ERotr(lhs, rhs, result, getNewEventId());
432
+ uint256 id = getNextEventId();
433
+ emit ERotr(lhs, rhs, result, id);
434
+ setDigest(abi.encodePacked(result, id));
411
435
  }
412
436
 
413
437
  function eEq(
@@ -425,7 +449,9 @@ abstract contract EncryptedOperations is
425
449
  rhs
426
450
  )
427
451
  );
428
- emit EEq(lhs, rhs, result, getNewEventId());
452
+ uint256 id = getNextEventId();
453
+ emit EEq(lhs, rhs, result, id);
454
+ setDigest(abi.encodePacked(result, id));
429
455
  }
430
456
 
431
457
  function eNe(
@@ -443,7 +469,9 @@ abstract contract EncryptedOperations is
443
469
  rhs
444
470
  )
445
471
  );
446
- emit ENe(lhs, rhs, result, getNewEventId());
472
+ uint256 id = getNextEventId();
473
+ emit ENe(lhs, rhs, result, id);
474
+ setDigest(abi.encodePacked(result, id));
447
475
  }
448
476
 
449
477
  function eGe(
@@ -458,7 +486,9 @@ abstract contract EncryptedOperations is
458
486
  euint256.unwrap(rhs)
459
487
  )
460
488
  );
461
- emit EGe(lhs, rhs, result, getNewEventId());
489
+ uint256 id = getNextEventId();
490
+ emit EGe(lhs, rhs, result, id);
491
+ setDigest(abi.encodePacked(result, id));
462
492
  }
463
493
 
464
494
  function eGt(
@@ -473,7 +503,9 @@ abstract contract EncryptedOperations is
473
503
  euint256.unwrap(rhs)
474
504
  )
475
505
  );
476
- emit EGt(lhs, rhs, result, getNewEventId());
506
+ uint256 id = getNextEventId();
507
+ emit EGt(lhs, rhs, result, id);
508
+ setDigest(abi.encodePacked(result, id));
477
509
  }
478
510
 
479
511
  function eLe(
@@ -488,7 +520,9 @@ abstract contract EncryptedOperations is
488
520
  euint256.unwrap(rhs)
489
521
  )
490
522
  );
491
- emit ELe(lhs, rhs, result, getNewEventId());
523
+ uint256 id = getNextEventId();
524
+ emit ELe(lhs, rhs, result, id);
525
+ setDigest(abi.encodePacked(result, id));
492
526
  }
493
527
 
494
528
  function eLt(
@@ -503,7 +537,9 @@ abstract contract EncryptedOperations is
503
537
  euint256.unwrap(rhs)
504
538
  )
505
539
  );
506
- emit ELt(lhs, rhs, result, getNewEventId());
540
+ uint256 id = getNextEventId();
541
+ emit ELt(lhs, rhs, result, id);
542
+ setDigest(abi.encodePacked(result, id));
507
543
  }
508
544
 
509
545
  function eMin(
@@ -518,7 +554,9 @@ abstract contract EncryptedOperations is
518
554
  euint256.unwrap(rhs)
519
555
  )
520
556
  );
521
- emit EMin(lhs, rhs, result, getNewEventId());
557
+ uint256 id = getNextEventId();
558
+ emit EMin(lhs, rhs, result, id);
559
+ setDigest(abi.encodePacked(result, id));
522
560
  }
523
561
 
524
562
  function eMax(
@@ -533,7 +571,9 @@ abstract contract EncryptedOperations is
533
571
  euint256.unwrap(rhs)
534
572
  )
535
573
  );
536
- emit EMax(lhs, rhs, result, getNewEventId());
574
+ uint256 id = getNextEventId();
575
+ emit EMax(lhs, rhs, result, id);
576
+ setDigest(abi.encodePacked(result, id));
537
577
  }
538
578
 
539
579
  function eNot(ebool operand) external returns (ebool result) {
@@ -541,7 +581,9 @@ abstract contract EncryptedOperations is
541
581
  result = ebool.wrap(
542
582
  createResultHandle(EOps.Not, ETypes.Bool, ebool.unwrap(operand))
543
583
  );
544
- emit ENot(operand, result, getNewEventId());
584
+ uint256 id = getNextEventId();
585
+ emit ENot(operand, result, id);
586
+ setDigest(abi.encodePacked(result, id));
545
587
  }
546
588
 
547
589
  function eCast(
@@ -551,7 +593,9 @@ abstract contract EncryptedOperations is
551
593
  bytes32 baseHandle = keccak256(abi.encodePacked(EOps.Cast, ct, toType));
552
594
  result = embedTypeVersion(baseHandle, toType);
553
595
  allowTransientInternal(result, msg.sender);
554
- emit ECast(ct, uint8(toType), result, getNewEventId());
596
+ uint256 id = getNextEventId();
597
+ emit ECast(ct, uint8(toType), result, id);
598
+ setDigest(abi.encodePacked(result, id));
555
599
  }
556
600
 
557
601
  function eRand(
@@ -568,7 +612,9 @@ abstract contract EncryptedOperations is
568
612
  //NOTE: We pass the incremented randCounter which is incremented using postfix increment above.
569
613
  // Due to postfix returning the value before incrementing, the emitted randCounter will be larger by one than the number used to build the handle.
570
614
  // So for security and replayability reasons, we always use the incremented randCounter when seeding on the covalidator side, which is fine for as long as we're consistent.
571
- emit ERand(randCounter, randType, result, getNewEventId());
615
+ uint256 id = getNextEventId();
616
+ emit ERand(randCounter, randType, result, id);
617
+ setDigest(abi.encodePacked(result, id));
572
618
  }
573
619
 
574
620
  function eRandBounded(
@@ -588,7 +634,9 @@ abstract contract EncryptedOperations is
588
634
  //NOTE: We pass the incremented randCounter which is incremented using postfix increment above.
589
635
  // Due to postfix returning the value before incrementing, the emitted randCounter will be larger by one than the number used to build the handle.
590
636
  // So for security and replayability reasons, we always use the incremented randCounter when seeding on the covalidator side, which is fine for as long as we're consistent.
591
- emit ERandBounded(randCounter, randType, upperBound, result, getNewEventId());
637
+ uint256 id = getNextEventId();
638
+ emit ERandBounded(randCounter, randType, upperBound, result, id);
639
+ setDigest(abi.encodePacked(result, id));
592
640
  }
593
641
 
594
642
  // todo add support in testing framework
@@ -608,7 +656,9 @@ abstract contract EncryptedOperations is
608
656
  );
609
657
  result = embedTypeVersion(baseHandle, returnType);
610
658
  allowTransientInternal(result, msg.sender);
611
- emit EIfThenElse(control, ifTrue, ifFalse, result, getNewEventId());
659
+ uint256 id = getNextEventId();
660
+ emit EIfThenElse(control, ifTrue, ifFalse, result, id);
661
+ setDigest(abi.encodePacked(result, id));
612
662
  }
613
663
 
614
664
  function checkEIfThenElseInputs(
@@ -1,60 +1,82 @@
1
- /// SPDX-License-Identifier: No License
2
1
  pragma solidity ^0.8.19;
3
2
 
4
- import "./TEELifecycle.types.sol";
5
-
3
+ import {
4
+ BootstrapResult,
5
+ TEEVersion,
6
+ TEEVersionStatus
7
+ } from "./TEELifecycle.types.sol";
6
8
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
7
- import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
8
9
  import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
9
- import {IQuoteVerifier} from "automata-dcap-attestation/interfaces/IQuoteVerifier.sol";
10
- import {BELE} from "automata-dcap-attestation/utils/BELE.sol";
11
- import {HEADER_LENGTH} from "automata-dcap-attestation/types/Constants.sol";
12
- import {TD10ReportBody, Header} from "automata-dcap-attestation/types/V4Structs.sol";
10
+ import {IQuoteVerifier} from "../interfaces/automata-interfaces/IQuoteVerifier.sol";
11
+ import {BELE} from "../interfaces/automata-interfaces/BELE.sol";
13
12
  import {EIP712Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
14
- import {EnclaveIdentityJsonObj, IdentityObj} from "@automata-network/on-chain-pccs/helpers/EnclaveIdentityHelper.sol";
15
- import {TcbInfoJsonObj} from "@automata-network/on-chain-pccs/helpers/FmspcTcbHelper.sol";
16
- import {AutomataFmspcTcbDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataFmspcTcbDao.sol";
17
- import {AutomataEnclaveIdentityDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataEnclaveIdentityDao.sol";
18
-
19
- // @todo: Make this contract UUPSUpgradeable: https://github.com/Inco-fhevm/inco-monorepo/issues/875
20
- contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
13
+ import {ITEELifecycle} from "./interfaces/ITEELifecycle.sol";
14
+ import {
15
+ TcbInfoJsonObj,
16
+ EnclaveIdentityJsonObj,
17
+ IdentityObj,
18
+ TD10ReportBody,
19
+ Header,
20
+ HEADER_LENGTH
21
+ } from "../interfaces/automata-interfaces/Types.sol";
22
+ import {IFmspcTcbDao} from "../interfaces/automata-interfaces/IFmspcTcbDao.sol";
23
+ import {IAutomataEnclaveIdentityDao} from "../interfaces/automata-interfaces/IAutomataEnclaveIdentityDao.sol";
24
+
25
+ // todo #1031 make TEELifecycle storage upgrade compatible
26
+ abstract contract TEELifecycle is
27
+ ITEELifecycle,
28
+ OwnableUpgradeable,
29
+ EIP712Upgradeable
30
+ {
31
+ error InvalidQuoteVerifierVersion(uint16 actual, uint16 expected);
32
+ error EmptyTcbInfo();
33
+ error EmptyIdentity();
34
+ error BootstrapNotComplete();
35
+ error BootstrapAlreadyCompleted();
36
+ /// @notice TEEVersionHistory must have exactly one version, please call approveNewTEEVersion first
37
+ error TEEVersionHistoryInconsistent();
38
+ error TEEVersionHistoryStatusIsNotPending();
39
+ error InvalidReportMRTD();
40
+ error InvalidBootstrapDataSignature();
41
+ /// @notice MRTD must be exactly 48 bytes
42
+ error MrtdInvalidLength();
43
+ error EOASignerAlreadyInitialized();
44
+ error InvalidMrtdReport();
45
+ error InvalidReportDataSigner();
21
46
 
22
47
  event QuoteVerifierUpdated(uint16 indexed version);
23
-
48
+ event TEEVersionUpdated(TEEVersion teeVersion);
49
+ event NewCovalidatorAdded(address covalidatorAddress, bytes quote);
24
50
  event BootstrapStageComplete(
25
51
  address indexed newEOASigner,
26
52
  BootstrapResult bootstrapResult
27
53
  );
28
54
 
29
- event TEEVersionUpdated(
30
- TEEVersion teeVersion
31
- );
32
-
33
- event NewCovalidatorAdded(
34
- address covalidatorAddress,
35
- bytes quote
36
- );
37
-
38
- bytes32 public constant BootstrapResultStructHash =
39
- keccak256(bytes(
40
- "BootstrapResult(bytes ecies_pubkey)"
41
- ));
55
+ bytes32 public constant BootstrapResultStructHash =
56
+ keccak256(bytes("BootstrapResult(bytes ecies_pubkey)"));
42
57
 
43
58
  uint16 public constant QUOTE_VERIFIER_VERSION = 4;
44
59
 
45
- IQuoteVerifier quoteVerifier;
46
-
60
+ IQuoteVerifier public quoteVerifier;
47
61
  BootstrapResult public VerifiedBootstrapResult;
62
+ bool public BootstrapComplete;
48
63
 
49
64
  TEEVersion[] public TEEVersionHistory;
50
65
  bytes public ECIESPubkey;
51
66
  mapping(address => bool) public EOASigners;
52
67
 
53
- function initialize(address owner, string memory eip712Name, string memory eip712Version, address quoteVerifierAddress) public initializer {
54
- __Ownable_init(owner);
55
- __EIP712_init(eip712Name, eip712Version);
56
- quoteVerifier = IQuoteVerifier(quoteVerifierAddress);
57
- require(quoteVerifier.quoteVersion() == QUOTE_VERIFIER_VERSION, "Invalid quote verifier version");
68
+ function __TEELifecycle_init(
69
+ IQuoteVerifier _quoteVerifier
70
+ ) internal onlyInitializing {
71
+ quoteVerifier = _quoteVerifier;
72
+ uint16 actualQuoteVerifierVersion = quoteVerifier.quoteVersion();
73
+ require(
74
+ actualQuoteVerifierVersion == QUOTE_VERIFIER_VERSION,
75
+ InvalidQuoteVerifierVersion(
76
+ actualQuoteVerifierVersion,
77
+ QUOTE_VERIFIER_VERSION
78
+ )
79
+ );
58
80
  }
59
81
 
60
82
  /**
@@ -62,15 +84,28 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
62
84
  * @param tcbInfo - The TCB info to upload
63
85
  * @param identity - The identity to upload
64
86
  */
65
- function uploadCollateral(TcbInfoJsonObj memory tcbInfo, EnclaveIdentityJsonObj memory identity) public onlyOwner {
66
- require(bytes(tcbInfo.tcbInfoStr).length != 0, "tcbInfo.tcbInfoStr must not be empty");
67
- require(bytes(identity.identityStr).length != 0, "identity.identityStr must not be empty");
68
-
69
- AutomataFmspcTcbDao fmspcTcbDao = AutomataFmspcTcbDao(quoteVerifier.pccsRouter().fmspcTcbDaoAddr());
87
+ function uploadCollateral(
88
+ TcbInfoJsonObj memory tcbInfo,
89
+ EnclaveIdentityJsonObj memory identity
90
+ ) public onlyOwner {
91
+ require(bytes(tcbInfo.tcbInfoStr).length != 0, EmptyTcbInfo());
92
+ require(bytes(identity.identityStr).length != 0, EmptyIdentity());
93
+
94
+ IFmspcTcbDao fmspcTcbDao = IFmspcTcbDao(
95
+ quoteVerifier.pccsRouter().fmspcTcbDaoAddr()
96
+ );
70
97
  fmspcTcbDao.upsertFmspcTcb(tcbInfo);
71
- AutomataEnclaveIdentityDao enclaveIdDao = AutomataEnclaveIdentityDao(quoteVerifier.pccsRouter().qeIdDaoAddr());
72
- (IdentityObj memory identityObj,) = enclaveIdDao.EnclaveIdentityLib().parseIdentityString(identity.identityStr);
73
- enclaveIdDao.upsertEnclaveIdentity(uint256(identityObj.id), 4, identity);
98
+ IAutomataEnclaveIdentityDao enclaveIdDao = IAutomataEnclaveIdentityDao(
99
+ quoteVerifier.pccsRouter().qeIdDaoAddr()
100
+ );
101
+ (IdentityObj memory identityObj, ) = enclaveIdDao
102
+ .EnclaveIdentityLib()
103
+ .parseIdentityString(identity.identityStr);
104
+ enclaveIdDao.upsertEnclaveIdentity(
105
+ uint256(identityObj.id),
106
+ 4,
107
+ identity
108
+ );
74
109
  }
75
110
 
76
111
  /**
@@ -79,12 +114,19 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
79
114
  * @param quote - The quote to verify against
80
115
  * @param signature - The signature to verify against
81
116
  */
82
- function verifyBootstrapResult(BootstrapResult calldata bootstrapResult, bytes calldata quote, bytes calldata signature) public onlyOwner {
117
+ function verifyBootstrapResult(
118
+ BootstrapResult calldata bootstrapResult,
119
+ bytes calldata quote,
120
+ bytes calldata signature
121
+ ) public onlyOwner {
83
122
  // Make sure the bootstrap is not already complete, and that the contract owner
84
123
  // has already submitted the pending TEE MRTD.
85
- require(!isBootstrapComplete(), "Bootstrap already completed");
86
- require(TEEVersionHistory.length == 1, "TEEVersionHistory must have exactly one version, please call approveNewTEEVersion first");
87
- require(TEEVersionHistory[0].status == TEEVersionStatus.PENDING, "TEEVersionHistory must still be pending");
124
+ require(!isBootstrapComplete(), BootstrapAlreadyCompleted());
125
+ require(TEEVersionHistory.length == 1, TEEVersionHistoryInconsistent());
126
+ require(
127
+ TEEVersionHistory[0].status == TEEVersionStatus.PENDING,
128
+ TEEVersionHistoryStatusIsNotPending()
129
+ );
88
130
 
89
131
  bytes32 _bootstrapResultDigest = bootstrapResultDigest(bootstrapResult);
90
132
  (bool success, bytes memory output) = _verifyAndAttestOnChain(quote);
@@ -93,10 +135,21 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
93
135
  bytes memory v0MRTD = TEEVersionHistory[0].mrtd;
94
136
 
95
137
  TD10ReportBody memory tdReport = parseTD10ReportBody(quote);
96
- (address reportDataSigner, bytes memory reportMRTD) = parseReport(tdReport);
97
- require(keccak256(reportMRTD) == keccak256(v0MRTD), "Invalid report MRTD");
98
- address recoveredAddress = ECDSA.recover(_bootstrapResultDigest, signature);
99
- require(recoveredAddress == reportDataSigner, "Invalid signature for bootstrap data");
138
+ (address reportDataSigner, bytes memory reportMRTD) = parseReport(
139
+ tdReport
140
+ );
141
+ require(
142
+ keccak256(reportMRTD) == keccak256(v0MRTD),
143
+ InvalidReportMRTD()
144
+ );
145
+ address recoveredAddress = ECDSA.recover(
146
+ _bootstrapResultDigest,
147
+ signature
148
+ );
149
+ require(
150
+ recoveredAddress == reportDataSigner,
151
+ InvalidBootstrapDataSignature()
152
+ );
100
153
 
101
154
  VerifiedBootstrapResult = bootstrapResult;
102
155
  TEEVersionHistory[0].status = TEEVersionStatus.ACTIVE;
@@ -112,13 +165,12 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
112
165
  * @dev This function increments the version number automatically based on the current history
113
166
  */
114
167
  function approveNewTEEVersion(bytes calldata newMRTD) public onlyOwner {
115
- require(newMRTD.length == 48, "MRTD must be 48 bytes");
116
-
117
- TEEVersionHistory.push(TEEVersion({
118
- mrtd: newMRTD,
119
- status: TEEVersionStatus.PENDING
120
- }));
121
-
168
+ require(newMRTD.length == 48, MrtdInvalidLength());
169
+
170
+ TEEVersionHistory.push(
171
+ TEEVersion({mrtd: newMRTD, status: TEEVersionStatus.PENDING})
172
+ );
173
+
122
174
  emit TEEVersionUpdated(TEEVersionHistory[TEEVersionHistory.length - 1]);
123
175
  }
124
176
 
@@ -127,16 +179,22 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
127
179
  * @param quote - The quote from the new covalidator that contains the current MRTD and the eoa address of the new party in the report data
128
180
  */
129
181
  function addNewCovalidator(bytes calldata quote) public onlyOwner {
130
- require(isBootstrapComplete(), "Bootstrap not complete");
182
+ require(isBootstrapComplete(), BootstrapNotComplete());
131
183
 
132
184
  (bool success, bytes memory output) = _verifyAndAttestOnChain(quote);
133
185
  require(success, string(output));
134
186
  TD10ReportBody memory tdReport = parseTD10ReportBody(quote);
135
- (address reportDataSigner, bytes memory reportMRTD) = parseReport(tdReport);
136
- require(!EOASigners[reportDataSigner], "EOA signer already initialized");
137
-
138
- require(keccak256(reportMRTD) == keccak256(TEEVersionHistory[TEEVersionHistory.length - 1].mrtd), "Invalid report MRTD");
139
- require(reportDataSigner != address(0), "Invalid report data signer");
187
+ (address reportDataSigner, bytes memory reportMRTD) = parseReport(
188
+ tdReport
189
+ );
190
+ require(!EOASigners[reportDataSigner], EOASignerAlreadyInitialized());
191
+
192
+ require(
193
+ keccak256(reportMRTD) ==
194
+ keccak256(TEEVersionHistory[TEEVersionHistory.length - 1].mrtd),
195
+ InvalidMrtdReport()
196
+ );
197
+ require(reportDataSigner != address(0), InvalidReportDataSigner());
140
198
  emit NewCovalidatorAdded(reportDataSigner, quote);
141
199
  EOASigners[reportDataSigner] = true;
142
200
  //TODO: Add the new covalidator signers to the Signers contract state
@@ -147,7 +205,9 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
147
205
  * @return true if the bootstrap is complete, false otherwise
148
206
  */
149
207
  function isBootstrapComplete() public view returns (bool) {
150
- return TEEVersionHistory.length >= 1 && TEEVersionHistory[0].status == TEEVersionStatus.ACTIVE;
208
+ return
209
+ TEEVersionHistory.length >= 1 &&
210
+ TEEVersionHistory[0].status == TEEVersionStatus.ACTIVE;
151
211
  }
152
212
 
153
213
  /**
@@ -159,12 +219,14 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
159
219
  * For verification failures, the output is simply a UTF-8 encoded string, describing the reason for failure.
160
220
  * @dev can directly type-cast the failed output as a string
161
221
  */
162
- function _verifyAndAttestOnChain(bytes calldata rawQuote) internal view returns (bool success, bytes memory output) {
222
+ function _verifyAndAttestOnChain(
223
+ bytes calldata rawQuote
224
+ ) internal view returns (bool success, bytes memory output) {
163
225
  // Parse the header
164
226
  Header memory header;
165
227
  (success, header) = _parseQuoteHeader(rawQuote);
166
228
  if (!success) {
167
- return (false, bytes("Could not parse quote header"));
229
+ return (false, bytes("Could not parse quote header")); // todo #949 switch the two returned strings for bytes4 identifiers
168
230
  }
169
231
 
170
232
  if (QUOTE_VERIFIER_VERSION != header.version) {
@@ -180,7 +242,9 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
180
242
  * @notice From https://github.com/automata-network/automata-dcap-attestation/blob/evm-v1.0.0/evm/contracts/AttestationEntrypointBase.sol#L168
181
243
  * @notice Parses the header to get basic information about the quote, such as the version, TEE types etc.
182
244
  */
183
- function _parseQuoteHeader(bytes calldata rawQuote) private pure returns (bool success, Header memory header) {
245
+ function _parseQuoteHeader(
246
+ bytes calldata rawQuote
247
+ ) private pure returns (bool success, Header memory header) {
184
248
  success = rawQuote.length >= HEADER_LENGTH;
185
249
  if (success) {
186
250
  uint16 version = uint16(BELE.leBytesToBeUint(rawQuote[0:2]));
@@ -209,23 +273,49 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
209
273
  * @param rawQuote - The raw quote bytes
210
274
  * @return report - The parsed TD10 report body
211
275
  */
212
- function parseTD10ReportBody(bytes calldata rawQuote) public pure returns (TD10ReportBody memory report) {
276
+ function parseTD10ReportBody(
277
+ bytes calldata rawQuote
278
+ ) public pure returns (TD10ReportBody memory report) {
213
279
  report = TD10ReportBody({
214
- teeTcbSvn: bytes16(rawQuote[HEADER_LENGTH:HEADER_LENGTH+16]),
215
- mrSeam: bytes(rawQuote[HEADER_LENGTH+16:HEADER_LENGTH+64]),
216
- mrsignerSeam: bytes(rawQuote[HEADER_LENGTH+64:HEADER_LENGTH+112]),
217
- seamAttributes: bytes8(uint64(BELE.leBytesToBeUint(rawQuote[HEADER_LENGTH+112:HEADER_LENGTH+120]))),
218
- tdAttributes: bytes8(uint64(BELE.leBytesToBeUint(rawQuote[HEADER_LENGTH+120:HEADER_LENGTH+128]))),
219
- xFAM: bytes8(uint64(BELE.leBytesToBeUint(rawQuote[HEADER_LENGTH+128:HEADER_LENGTH+136]))),
220
- mrTd: bytes(rawQuote[HEADER_LENGTH+136:HEADER_LENGTH+184]),
221
- mrConfigId: bytes(rawQuote[HEADER_LENGTH+184:HEADER_LENGTH+232]),
222
- mrOwner: bytes(rawQuote[HEADER_LENGTH+232:HEADER_LENGTH+280]),
223
- mrOwnerConfig: bytes(rawQuote[HEADER_LENGTH+280:HEADER_LENGTH+328]),
224
- rtMr0: bytes(rawQuote[HEADER_LENGTH+328:HEADER_LENGTH+376]),
225
- rtMr1: bytes(rawQuote[HEADER_LENGTH+376:HEADER_LENGTH+424]),
226
- rtMr2: bytes(rawQuote[HEADER_LENGTH+424:HEADER_LENGTH+472]),
227
- rtMr3: bytes(rawQuote[HEADER_LENGTH+472:HEADER_LENGTH+520]),
228
- reportData: bytes(rawQuote[HEADER_LENGTH+520:HEADER_LENGTH+584])
280
+ teeTcbSvn: bytes16(rawQuote[HEADER_LENGTH:HEADER_LENGTH + 16]),
281
+ mrSeam: bytes(rawQuote[HEADER_LENGTH + 16:HEADER_LENGTH + 64]),
282
+ mrsignerSeam: bytes(
283
+ rawQuote[HEADER_LENGTH + 64:HEADER_LENGTH + 112]
284
+ ),
285
+ seamAttributes: bytes8(
286
+ uint64(
287
+ BELE.leBytesToBeUint(
288
+ rawQuote[HEADER_LENGTH + 112:HEADER_LENGTH + 120]
289
+ )
290
+ )
291
+ ),
292
+ tdAttributes: bytes8(
293
+ uint64(
294
+ BELE.leBytesToBeUint(
295
+ rawQuote[HEADER_LENGTH + 120:HEADER_LENGTH + 128]
296
+ )
297
+ )
298
+ ),
299
+ xFAM: bytes8(
300
+ uint64(
301
+ BELE.leBytesToBeUint(
302
+ rawQuote[HEADER_LENGTH + 128:HEADER_LENGTH + 136]
303
+ )
304
+ )
305
+ ),
306
+ mrTd: bytes(rawQuote[HEADER_LENGTH + 136:HEADER_LENGTH + 184]),
307
+ mrConfigId: bytes(
308
+ rawQuote[HEADER_LENGTH + 184:HEADER_LENGTH + 232]
309
+ ),
310
+ mrOwner: bytes(rawQuote[HEADER_LENGTH + 232:HEADER_LENGTH + 280]),
311
+ mrOwnerConfig: bytes(
312
+ rawQuote[HEADER_LENGTH + 280:HEADER_LENGTH + 328]
313
+ ),
314
+ rtMr0: bytes(rawQuote[HEADER_LENGTH + 328:HEADER_LENGTH + 376]),
315
+ rtMr1: bytes(rawQuote[HEADER_LENGTH + 376:HEADER_LENGTH + 424]),
316
+ rtMr2: bytes(rawQuote[HEADER_LENGTH + 424:HEADER_LENGTH + 472]),
317
+ rtMr3: bytes(rawQuote[HEADER_LENGTH + 472:HEADER_LENGTH + 520]),
318
+ reportData: bytes(rawQuote[HEADER_LENGTH + 520:HEADER_LENGTH + 584])
229
319
  });
230
320
  }
231
321
 
@@ -235,14 +325,16 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
235
325
  * @return reportDataSigner - The signing address of the report data signer
236
326
  * @return reportMRTD - The MRTD bytes from the report
237
327
  */
238
- function parseReport(TD10ReportBody memory tdReport) public pure returns (address, bytes memory) {
328
+ function parseReport(
329
+ TD10ReportBody memory tdReport
330
+ ) public pure returns (address, bytes memory) {
239
331
  return (address(bytes20(tdReport.reportData)), tdReport.mrTd);
240
332
  }
241
333
 
242
334
  function bootstrapResultDigest(
243
335
  BootstrapResult memory bootstrapResult
244
336
  ) public view returns (bytes32) {
245
- return
337
+ return
246
338
  _hashTypedDataV4(
247
339
  keccak256(
248
340
  abi.encode(
@@ -252,4 +344,4 @@ contract TEELifecycle is OwnableUpgradeable, EIP712Upgradeable {
252
344
  )
253
345
  );
254
346
  }
255
- }
347
+ }