|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +mod structure; |
| 4 | + |
| 5 | +use crate::{tss2_esys::TPM2_CC, Error, Result, WrapperErrorKind}; |
| 6 | +use log::error; |
| 7 | +use num_derive::{FromPrimitive, ToPrimitive}; |
| 8 | +use num_traits::{FromPrimitive, ToPrimitive}; |
| 9 | +use std::convert::TryFrom; |
| 10 | +use structure::CommandCodeStructure; |
| 11 | + |
| 12 | +#[derive(FromPrimitive, ToPrimitive, Copy, Clone, Debug, PartialEq, Eq, Hash)] |
| 13 | +#[repr(u32)] |
| 14 | +pub enum CommandCode { |
| 15 | + NvUndefineSpaceSpecial = CommandCodeStructure::NV_UNDEFINE_SPACE_SPECIAL.0, |
| 16 | + EvictControl = CommandCodeStructure::EVICT_CONTROL.0, |
| 17 | + HierarchyControl = CommandCodeStructure::HIERARCHY_CONTROL.0, |
| 18 | + NvUndefineSpace = CommandCodeStructure::NV_UNDEFINE_SPACE.0, |
| 19 | + ChangeEps = CommandCodeStructure::CHANGE_EPS.0, |
| 20 | + ChangePps = CommandCodeStructure::CHANGE_PPS.0, |
| 21 | + Clear = CommandCodeStructure::CLEAR.0, |
| 22 | + ClearControl = CommandCodeStructure::CLEAR_CONTROL.0, |
| 23 | + ClockSet = CommandCodeStructure::CLOCK_SET.0, |
| 24 | + HierarchyChangeAuth = CommandCodeStructure::HIERARCHY_CHANGE_AUTH.0, |
| 25 | + NvDefineSpace = CommandCodeStructure::NV_DEFINE_SPACE.0, |
| 26 | + PcrAllocate = CommandCodeStructure::PCR_ALLOCATE.0, |
| 27 | + PcrSetAuthPolicy = CommandCodeStructure::PCR_SET_AUTH_POLICY.0, |
| 28 | + PpCommands = CommandCodeStructure::PP_COMMANDS.0, |
| 29 | + SetPrimaryPolicy = CommandCodeStructure::SET_PRIMARY_POLICY.0, |
| 30 | + FieldUpgradeStart = CommandCodeStructure::FIELD_UPGRADE_START.0, |
| 31 | + ClockRateAdjust = CommandCodeStructure::CLOCK_RATE_ADJUST.0, |
| 32 | + CreatePrimary = CommandCodeStructure::CREATE_PRIMARY.0, |
| 33 | + NvGlobalWriteLock = CommandCodeStructure::NV_GLOBAL_WRITE_LOCK.0, |
| 34 | + GetCommandAuditDigest = CommandCodeStructure::GET_COMMAND_AUDIT_DIGEST.0, |
| 35 | + NvIncrement = CommandCodeStructure::NV_INCREMENT.0, |
| 36 | + NvSetBits = CommandCodeStructure::NV_SET_BITS.0, |
| 37 | + NvExtend = CommandCodeStructure::NV_EXTEND.0, |
| 38 | + NvWrite = CommandCodeStructure::NV_WRITE.0, |
| 39 | + NvWriteLock = CommandCodeStructure::NV_WRITE_LOCK.0, |
| 40 | + DictionaryAttackLockReset = CommandCodeStructure::DICTIONARY_ATTACK_LOCK_RESET.0, |
| 41 | + DictionaryAttackParameters = CommandCodeStructure::DICTIONARY_ATTACK_PARAMETERS.0, |
| 42 | + NvChangeAuth = CommandCodeStructure::NV_CHANGE_AUTH.0, |
| 43 | + PcrEvent = CommandCodeStructure::PCR_EVENT.0, |
| 44 | + PcrReset = CommandCodeStructure::PCR_RESET.0, |
| 45 | + SequenceComplete = CommandCodeStructure::SEQUENCE_COMPLETE.0, |
| 46 | + SetAlgorithmSet = CommandCodeStructure::SET_ALGORITHM_SET.0, |
| 47 | + SetCommandCodeAuditStatus = CommandCodeStructure::SET_COMMAND_CODE_AUDIT_STATUS.0, |
| 48 | + FieldUpgradeData = CommandCodeStructure::FIELD_UPGRADE_DATA.0, |
| 49 | + IncrementalSelfTest = CommandCodeStructure::INCREMENTAL_SELF_TEST.0, |
| 50 | + SelfTest = CommandCodeStructure::SELF_TEST.0, |
| 51 | + Startup = CommandCodeStructure::STARTUP.0, |
| 52 | + Shutdown = CommandCodeStructure::SHUTDOWN.0, |
| 53 | + StirRandom = CommandCodeStructure::STIR_RANDOM.0, |
| 54 | + ActivateCredential = CommandCodeStructure::ACTIVATE_CREDENTIAL.0, |
| 55 | + Certify = CommandCodeStructure::CERTIFY.0, |
| 56 | + PolicyNv = CommandCodeStructure::POLICY_NV.0, |
| 57 | + CertifyCreation = CommandCodeStructure::CERTIFY_CREATION.0, |
| 58 | + Duplicate = CommandCodeStructure::DUPLICATE.0, |
| 59 | + GetTime = CommandCodeStructure::GET_TIME.0, |
| 60 | + GetSessionAuditDigest = CommandCodeStructure::GET_SESSION_AUDIT_DIGEST.0, |
| 61 | + NvRead = CommandCodeStructure::NV_READ.0, |
| 62 | + NvReadLock = CommandCodeStructure::NV_READ_LOCK.0, |
| 63 | + ObjectChangeAuth = CommandCodeStructure::OBJECT_CHANGE_AUTH.0, |
| 64 | + PolicySecret = CommandCodeStructure::POLICY_SECRET.0, |
| 65 | + Rewrap = CommandCodeStructure::REWRAP.0, |
| 66 | + Create = CommandCodeStructure::CREATE.0, |
| 67 | + EcdhZGen = CommandCodeStructure::ECDH_Z_GEN.0, |
| 68 | + Hmac = CommandCodeStructure::HMAC.0, |
| 69 | + Import = CommandCodeStructure::IMPORT.0, |
| 70 | + Load = CommandCodeStructure::LOAD.0, |
| 71 | + Quote = CommandCodeStructure::QUOTE.0, |
| 72 | + RsaDecrypt = CommandCodeStructure::RSA_DECRYPT.0, |
| 73 | + HmacStart = CommandCodeStructure::HMAC_START.0, |
| 74 | + SequenceUpdate = CommandCodeStructure::SEQUENCE_UPDATE.0, |
| 75 | + Sign = CommandCodeStructure::SIGN.0, |
| 76 | + Unseal = CommandCodeStructure::UNSEAL.0, |
| 77 | + PolicySigned = CommandCodeStructure::POLICY_SIGNED.0, |
| 78 | + ContextLoad = CommandCodeStructure::CONTEXT_LOAD.0, |
| 79 | + ContextSave = CommandCodeStructure::CONTEXT_SAVE.0, |
| 80 | + EcdhKeyGen = CommandCodeStructure::ECDH_KEY_GEN.0, |
| 81 | + EncryptDecrypt = CommandCodeStructure::ENCRYPT_DECRYPT.0, |
| 82 | + FlushContext = CommandCodeStructure::FLUSH_CONTEXT.0, |
| 83 | + LoadExternal = CommandCodeStructure::LOAD_EXTERNAL.0, |
| 84 | + MakeCredential = CommandCodeStructure::MAKE_CREDENTIAL.0, |
| 85 | + NvReadPublic = CommandCodeStructure::NV_READ_PUBLIC.0, |
| 86 | + PolicyAuthorize = CommandCodeStructure::POLICY_AUTHORIZE.0, |
| 87 | + PolicyAuthValue = CommandCodeStructure::POLICY_AUTH_VALUE.0, |
| 88 | + PolicyCommandCode = CommandCodeStructure::POLICY_COMMAND_CODE.0, |
| 89 | + PolicyCounterTimer = CommandCodeStructure::POLICY_COUNTER_TIMER.0, |
| 90 | + PolicyCpHash = CommandCodeStructure::POLICY_CP_HASH.0, |
| 91 | + PolicyLocality = CommandCodeStructure::POLICY_LOCALITY.0, |
| 92 | + PolicyNameHash = CommandCodeStructure::POLICY_NAME_HASH.0, |
| 93 | + PolicyOr = CommandCodeStructure::POLICY_OR.0, |
| 94 | + PolicyTicket = CommandCodeStructure::POLICY_TICKET.0, |
| 95 | + ReadPublic = CommandCodeStructure::READ_PUBLIC.0, |
| 96 | + RsaEncrypt = CommandCodeStructure::RSA_ENCRYPT.0, |
| 97 | + StartAuthSession = CommandCodeStructure::START_AUTH_SESSION.0, |
| 98 | + VerifySignature = CommandCodeStructure::VERIFY_SIGNATURE.0, |
| 99 | + EccParameters = CommandCodeStructure::ECC_PARAMETERS.0, |
| 100 | + FirmwareRead = CommandCodeStructure::FIRMWARE_READ.0, |
| 101 | + GetCapability = CommandCodeStructure::GET_CAPABILITY.0, |
| 102 | + GetRandom = CommandCodeStructure::GET_RANDOM.0, |
| 103 | + GetTestResult = CommandCodeStructure::GET_TEST_RESULT.0, |
| 104 | + Hash = CommandCodeStructure::HASH.0, |
| 105 | + PcrRead = CommandCodeStructure::PCR_READ.0, |
| 106 | + PolicyPcr = CommandCodeStructure::POLICY_PCR.0, |
| 107 | + PolicyRestart = CommandCodeStructure::POLICY_RESTART.0, |
| 108 | + ReadClock = CommandCodeStructure::READ_CLOCK.0, |
| 109 | + PcrExtend = CommandCodeStructure::PCR_EXTEND.0, |
| 110 | + PcrSetAuthValue = CommandCodeStructure::PCR_SET_AUTH_VALUE.0, |
| 111 | + NvCertify = CommandCodeStructure::NV_CERTIFY.0, |
| 112 | + EventSequenceComplete = CommandCodeStructure::EVENT_SEQUENCE_COMPLETE.0, |
| 113 | + HashSequenceStart = CommandCodeStructure::HASH_SEQUENCE_START.0, |
| 114 | + PolicyPhysicalPresence = CommandCodeStructure::POLICY_PHYSICAL_PRESENCE.0, |
| 115 | + PolicyDuplicationSelect = CommandCodeStructure::POLICY_DUPLICATION_SELECT.0, |
| 116 | + PolicyGetDigest = CommandCodeStructure::POLICY_GET_DIGEST.0, |
| 117 | + TestParms = CommandCodeStructure::TEST_PARMS.0, |
| 118 | + Commit = CommandCodeStructure::COMMIT.0, |
| 119 | + PolicyPassword = CommandCodeStructure::POLICY_PASSWORD.0, |
| 120 | + ZGen2Phase = CommandCodeStructure::Z_GEN_2_PHASE.0, |
| 121 | + EcEphemeral = CommandCodeStructure::EC_EPHEMERAL.0, |
| 122 | + PolicyNvWritten = CommandCodeStructure::POLICY_NV_WRITTEN.0, |
| 123 | + PolicyTemplate = CommandCodeStructure::POLICY_TEMPLATE.0, |
| 124 | + CreateLoaded = CommandCodeStructure::CREATE_LOADED.0, |
| 125 | + PolicyAuthorizeNv = CommandCodeStructure::POLICY_AUTHORIZE_NV.0, |
| 126 | + EncryptDecrypt2 = CommandCodeStructure::ENCRYPT_DECRYPT_2.0, |
| 127 | + AcGetCapability = CommandCodeStructure::AC_GET_CAPABILITY.0, |
| 128 | + AcSend = CommandCodeStructure::AC_SEND.0, |
| 129 | + PolicyAcSendSelect = CommandCodeStructure::POLICY_AC_SEND_SELECT.0, |
| 130 | +} |
| 131 | + |
| 132 | +impl TryFrom<TPM2_CC> for CommandCode { |
| 133 | + type Error = Error; |
| 134 | + |
| 135 | + fn try_from(tpm2_cc: TPM2_CC) -> Result<Self> { |
| 136 | + CommandCode::from_u32(CommandCodeStructure::try_from(tpm2_cc)?.0).ok_or_else(|| { |
| 137 | + error!("Value = {} did not match any Command Code", tpm2_cc); |
| 138 | + Error::local_error(WrapperErrorKind::InvalidParam) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl From<CommandCode> for TPM2_CC { |
| 144 | + fn from(command_code: CommandCode) -> Self { |
| 145 | + // The values are well defined so this cannot fail. |
| 146 | + command_code.to_u32().unwrap() |
| 147 | + } |
| 148 | +} |
0 commit comments