blob: c4a746cca2132facf7ba615836e83a1ab1d77785 [file] [log] [blame]
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <base/logging.h>
#include <base/sys_byteorder.h>
#include "tpm2-simulator/tpm_command_utils.h"
namespace {
constexpr size_t kHeaderSize = 10;
static_assert(kHeaderSize == sizeof(tpm2_simulator::CommandHeader));
struct CommandPartialHeader {
uint16_t tag;
uint32_t size;
} __attribute__((packed));
constexpr size_t kPartialHeaderSize = 6;
static_assert(kPartialHeaderSize == sizeof(CommandPartialHeader));
constexpr uint32_t kTpmNoSessionTag = 0x8001;
} // namespace
namespace tpm2_simulator {
bool ExtractCommandSize(const std::string& command, uint32_t* size) {
if (command.size() < kPartialHeaderSize) {
LOG(ERROR) << "Partial command too small.";
return false;
}
const CommandPartialHeader* input_header =
reinterpret_cast<const CommandPartialHeader*>(command.data());
*size = base::NetToHost32(input_header->size);
return true;
}
bool ExtractCommandHeader(const std::string& command, CommandHeader* header) {
if (command.size() < kHeaderSize) {
LOG(ERROR) << "Command too small.";
return false;
}
const CommandHeader* input_header =
reinterpret_cast<const CommandHeader*>(command.data());
header->tag = base::NetToHost16(input_header->tag);
header->size = base::NetToHost32(input_header->size);
header->code = base::NetToHost32(input_header->code);
return true;
}
std::string CreateCommandWithCode(uint32_t code) {
std::string response;
response.resize(kHeaderSize);
CommandHeader* header = reinterpret_cast<CommandHeader*>(response.data());
header->tag = base::HostToNet16(kTpmNoSessionTag);
header->size = base::HostToNet32(kHeaderSize);
header->code = base::HostToNet32(code);
return response;
}
} // namespace tpm2_simulator