|
| 1 | +%%%--------------------------------------------------------------------- |
| 2 | +%%% Module: sec |
| 3 | +%%%--------------------------------------------------------------------- |
| 4 | +%%% Purpose: |
| 5 | +%%% This module handles the generation and verification of attestation |
| 6 | +%%% reports using both TPM and SEV-SNP. It combines attestation reports |
| 7 | +%%% from both technologies into a single binary and provides functionality |
| 8 | +%%% to verify the combined reports. |
| 9 | +%%% |
| 10 | +%%% It uses the `sec_tpm` and `sec_tee` modules to interact with the TPM |
| 11 | +%%% hardware and SEV-SNP for generating and verifying attestation reports. |
| 12 | +%%%--------------------------------------------------------------------- |
| 13 | +%%% Exports |
| 14 | +%%%--------------------------------------------------------------------- |
| 15 | +%%% generate_attestation(Nonce) |
| 16 | +%%% Generates a combined attestation report using the provided nonce. |
| 17 | +%%% It generates attestation reports from both TPM and SEV-SNP, calculates |
| 18 | +%%% their sizes, creates a header containing the sizes, and combines them |
| 19 | +%%% into a single binary. |
| 20 | +%%% |
| 21 | +%%% verify_attestation(AttestationBinary) |
| 22 | +%%% Verifies the provided attestation binary by extracting the TPM and |
| 23 | +%%% SEV-SNP reports, verifying them using their respective verification |
| 24 | +%%% methods, and then combining the results into a single binary. |
| 25 | +%%%--------------------------------------------------------------------- |
| 26 | + |
| 27 | +-module(sec). |
| 28 | +-export([generate_attestation/1, verify_attestation/1]). |
| 29 | + |
| 30 | +-include("include/ao.hrl"). |
| 31 | + |
| 32 | +-ao_debug(print). |
| 33 | + |
| 34 | +%% Generate attestation based on the provided nonce (both TPM and SEV-SNP) |
| 35 | +generate_attestation(Nonce) -> |
| 36 | + ?c({"Generating TPM attestation..."}), |
| 37 | + |
| 38 | + case sec_tpm:generate_attestation(Nonce) of |
| 39 | + {ok, TPMAttestation} -> |
| 40 | + ?c({"TPM attestation generated, size:", byte_size(TPMAttestation)}), |
| 41 | + |
| 42 | + ?c({"Generating SEV-SNP attestation..."}), |
| 43 | + |
| 44 | + case sec_tee:generate_attestation(Nonce) of |
| 45 | + {ok, TEEAttestation} -> |
| 46 | + ?c({"SEV-SNP attestation generated, size:", byte_size(TEEAttestation)}), |
| 47 | + |
| 48 | + %% Calculate sizes of the two attestation binaries |
| 49 | + TPMSize = byte_size(TPMAttestation), |
| 50 | + TEESize = byte_size(TEEAttestation), |
| 51 | + |
| 52 | + %% Create the header containing the sizes |
| 53 | + Header = <<TPMSize:32/unit:8, TEESize:32/unit:8>>, |
| 54 | + ?c({"Header created, TPMSize:", TPMSize, "TEESize:", TEESize}), |
| 55 | + |
| 56 | + %% Combine the header with the two attestation binaries |
| 57 | + CombinedAttestation = <<Header/binary, TPMAttestation/binary, TEEAttestation/binary>>, |
| 58 | + ?c({"Combined attestation binary created, total size:", byte_size(CombinedAttestation)}), |
| 59 | + |
| 60 | + {ok, CombinedAttestation}; |
| 61 | + |
| 62 | + {error, Reason} -> |
| 63 | + ?c({"Error generating SEV-SNP attestation:", Reason}), |
| 64 | + {error, Reason} |
| 65 | + end; |
| 66 | + |
| 67 | + {error, Reason} -> |
| 68 | + ?c({"Error generating TPM attestation:", Reason}), |
| 69 | + {error, Reason} |
| 70 | + end. |
| 71 | + |
| 72 | +%% Verify attestation report based on the provided binary (both TPM and SEV-SNP) |
| 73 | +verify_attestation(AttestationBinary) -> |
| 74 | + ?c("Verifying attestation..."), |
| 75 | + |
| 76 | + %% Extract the header (size info) and the attestation binaries |
| 77 | + <<TPMSize:32/unit:8, TEESize:32/unit:8, Rest/binary>> = AttestationBinary, |
| 78 | + ?c({"Header extracted, TPMSize:", TPMSize, "TEESize:", TEESize}), |
| 79 | + |
| 80 | + %% Extract the TPM and SEV-SNP attestation binaries based on their sizes |
| 81 | + <<TPMAttestation:TPMSize/binary, TEEAttestation:TEESize/binary>> = Rest, |
| 82 | + ?c({"Extracted TPM and SEV-SNP attestation binaries"}), |
| 83 | + |
| 84 | + %% Verify TPM attestation |
| 85 | + case sec_tpm:verify_attestation(TPMAttestation) of |
| 86 | + {ok, _TPMVerification} -> |
| 87 | + ?c({"TPM attestation verification completed"}), |
| 88 | + |
| 89 | + %% Verify SEV-SNP attestation |
| 90 | + case sec_tee:verify_attestation(TEEAttestation) of |
| 91 | + {ok, _TEEVerification} -> |
| 92 | + ?c({"SEV-SNP attestation verification completed"}), |
| 93 | + |
| 94 | + %% Return success if both verifications succeeded |
| 95 | + {ok, "Verified"}; |
| 96 | + |
| 97 | + {error, Reason} -> |
| 98 | + ?c({"Error verifying SEV-SNP attestation:", Reason}), |
| 99 | + {error, Reason} |
| 100 | + end; |
| 101 | + |
| 102 | + {error, Reason} -> |
| 103 | + ?c({"Error verifying TPM attestation:", Reason}), |
| 104 | + {error, Reason} |
| 105 | + end. |
0 commit comments