// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CRYPTO_UNEXPORTABLE_KEY_METRICS_H_
#define CRYPTO_UNEXPORTABLE_KEY_METRICS_H_

#include <string>

#include "crypto/crypto_export.h"
#include "crypto/unexportable_key.h"

namespace crypto {

// LINT.IfChange(TPMOperation)
enum class TPMOperation {
  // An operation to sign data with a TPM key.
  kMessageSigning,
  // An operation to verify a TPM signature.
  kMessageVerify,
  // An operation to create a TPM key from a wrapped key or a similar
  // representation identifying a TPM key.
  kWrappedKeyCreation,
  // An operation to create a new TPM-protected key.
  kNewKeyCreation,
  // An operation to export a wrapped key (or a similar representation
  // identifying a TPM key) from an existing TPM key.
  kWrappedKeyExport,
  // An operation to select a signature algorithm supported by the TPM.
  kSelectAlgorithm,
  // An operation to delete a TPM-protected key.
  kKeyDeletion,
  // An operation to certify a key with an attestation key.
  kKeyCertification,
  // An operation to create a new TPM-protected attestation key.
  kNewAttestationKeyCreation,
  // An operation to create an attestation key from a wrapped key.
  kWrappedAttestationKeyCreation,
  // An operation to export a wrapped attestation key.
  kWrappedAttestationKeyExport,
  // An operation to hash data using the TPM.
  kMessageHashing,
  // An operation to sign data with a restricted signing key (such as a TPM 2.0
  // Attestation Identity Key) using restricted credentials.
  kRestrictedMessageSigning,
  // An operation to verify the signature of a restricted signing key.
  kRestrictedMessageVerify,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/net/histograms.xml:TpmOperation)

// Converts the given `operation` to a string representation.
CRYPTO_EXPORT std::string OperationToString(TPMOperation operation);

// Converts the given `algorithm` to a string representation.
CRYPTO_EXPORT std::string AlgorithmToString(
    SignatureVerifier::SignatureAlgorithm algorithm);

// Records UMA metrics of TPM availability, latency and successful usage.
// Does the work on a new background task.
CRYPTO_EXPORT void MaybeMeasureTpmOperations(
    UnexportableKeyProvider::Config config);

// internal namespace to be used by tests only
namespace internal {

// Note that values here are used in a recorded histogram. Don't change
// the values of existing members.
enum class TPMSupport {
  kNone = 0,
  kRSA = 1,
  kECDSA = 2,
  kMaxValue = 2,
};

// Note that values here are used in a recorded histogram. Don't change
// the values of existing members.
enum class TPMType {
  kNone = 0,
  kHW = 1,
  kVirtual = 2,
  kBoth = 3,
  kMaxValue = 3,
};

// Exported for testing
CRYPTO_EXPORT void MeasureTpmOperationsInternalForTesting();
}  // namespace internal

}  // namespace crypto

#endif  // CRYPTO_UNEXPORTABLE_KEY_METRICS_H_
