//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <memory>
#include <string>

#include <tss2/tss2_esys.h>

#include "host/commands/secure_env/tpm_auth.h"

namespace cuttlefish {

class TpmResourceManager;

struct EsysDeleter {
  void operator()(void* data) { Esys_Free(data); }
};

template<typename T>
using UniqueEsysPtr = std::unique_ptr<T, EsysDeleter>;

/**
 * Returns a HMAC signature for `data` with the key loaded into the TPM at
 * `key_handle`.
 *
 * The signature is a byte string that certifies a process that can make TPM
 * API calls has signed off on using another byte string (`data`) for some
 * purpose, which is implicitly tied to the signing key. In this case, the
 * secure_env process is the only process that should have TPM access.
 * secure_env can then transmit some data together with a signature over that
 * data, an external system (Android) can hold onto this data and the signature,
 * and then the secure_env process can receive the data back. The signature
 * is used to check that the data has not been tampered with.
 */
UniqueEsysPtr<TPM2B_DIGEST> TpmHmac(
    TpmResourceManager& resource_manager,
    ESYS_TR key_handle,
    TpmAuth auth,
    const uint8_t* data,
    size_t data_size);

/**
 * Returns a HMAC signature for `data` with a key created in the TPM with
 * context / unique-data `context`.
 */
UniqueEsysPtr<TPM2B_DIGEST> TpmHmacWithContext(
    TpmResourceManager& resource_manager, const std::string& context,
    const uint8_t* data, size_t data_size);

}  // namespace cuttlefish
