0

devices/fido: use base::as_string_view()

The bespoke fido_parsing_utils::ConvertToStringView() is not needed.

Bug: None
Change-Id: Id4c944e6ba474997726cd3f485e8f3d8e42c3239
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6442538
Reviewed-by: Martin Kreichgauer <martinkr@google.com>
Commit-Queue: Martin Kreichgauer <martinkr@google.com>
Commit-Queue: Elly FJ <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1444462}
This commit is contained in:
Elly
2025-04-08 16:55:02 -07:00
committed by Chromium LUCI CQ
parent c3173b6238
commit 50e7e97931
6 changed files with 15 additions and 32 deletions

@ -100,7 +100,7 @@ class FakeCableAuthenticator {
std::string ciphertext; std::string ciphertext;
aead.Seal( aead.Seal(
message, fido_parsing_utils::ConvertToStringView(encryption_nonce), message, base::as_string_view(encryption_nonce),
std::string(1, base::strict_cast<uint8_t>(FidoBleDeviceCommand::kMsg)), std::string(1, base::strict_cast<uint8_t>(FidoBleDeviceCommand::kMsg)),
&ciphertext); &ciphertext);
authenticator_counter_++; authenticator_counter_++;
@ -121,8 +121,7 @@ class FakeCableAuthenticator {
std::string ciphertext; std::string ciphertext;
aead.Open( aead.Open(
fido_parsing_utils::ConvertToStringView(message), base::as_string_view(message), base::as_string_view(encryption_nonce),
fido_parsing_utils::ConvertToStringView(encryption_nonce),
std::string(1, base::strict_cast<uint8_t>(FidoBleDeviceCommand::kMsg)), std::string(1, base::strict_cast<uint8_t>(FidoBleDeviceCommand::kMsg)),
&ciphertext); &ciphertext);
expected_client_counter_++; expected_client_counter_++;

@ -64,8 +64,8 @@ ConstructHandshakeMessage(std::string_view handshake_key,
return std::nullopt; return std::nullopt;
std::array<uint8_t, kCableHandshakeMacMessageSize> client_hello_mac; std::array<uint8_t, kCableHandshakeMacMessageSize> client_hello_mac;
if (!hmac.Sign(fido_parsing_utils::ConvertToStringView(*client_hello), if (!hmac.Sign(base::as_string_view(*client_hello), client_hello_mac.data(),
client_hello_mac.data(), client_hello_mac.size())) { client_hello_mac.size())) {
return std::nullopt; return std::nullopt;
} }
@ -90,11 +90,10 @@ FidoCableV1HandshakeHandler::FidoCableV1HandshakeHandler(
: cable_device_(cable_device), : cable_device_(cable_device),
nonce_(fido_parsing_utils::Materialize(nonce)), nonce_(fido_parsing_utils::Materialize(nonce)),
session_pre_key_(fido_parsing_utils::Materialize(session_pre_key)), session_pre_key_(fido_parsing_utils::Materialize(session_pre_key)),
handshake_key_(crypto::HkdfSha256( handshake_key_(crypto::HkdfSha256(base::as_string_view(session_pre_key_),
fido_parsing_utils::ConvertToStringView(session_pre_key_), base::as_string_view(nonce_),
fido_parsing_utils::ConvertToStringView(nonce_), kCableHandshakeKeyInfo,
kCableHandshakeKeyInfo, /*derived_key_size=*/32)) {
/*derived_key_size=*/32)) {
crypto::RandBytes(client_session_random_); crypto::RandBytes(client_session_random_);
} }
@ -128,9 +127,8 @@ bool FidoCableV1HandshakeHandler::ValidateAuthenticatorHandshakeMessage(
const auto authenticator_hello = response.first( const auto authenticator_hello = response.first(
kCableAuthenticatorHandshakeMessageSize - kCableHandshakeMacMessageSize); kCableAuthenticatorHandshakeMessageSize - kCableHandshakeMacMessageSize);
if (!hmac.VerifyTruncated( if (!hmac.VerifyTruncated(
fido_parsing_utils::ConvertToStringView(authenticator_hello), base::as_string_view(authenticator_hello),
fido_parsing_utils::ConvertToStringView( base::as_string_view(response.subspan(authenticator_hello.size())))) {
response.subspan(authenticator_hello.size())))) {
return false; return false;
} }

@ -148,7 +148,7 @@ std::vector<uint8_t> ConstructAuthenticatorHelloReply(
return std::vector<uint8_t>(); return std::vector<uint8_t>();
std::array<uint8_t, 32> authenticator_hello_mac; std::array<uint8_t, 32> authenticator_hello_mac;
if (!hmac.Sign(fido_parsing_utils::ConvertToStringView(hello_msg), if (!hmac.Sign(base::as_string_view(hello_msg),
authenticator_hello_mac.data(), authenticator_hello_mac.data(),
authenticator_hello_mac.size())) { authenticator_hello_mac.size())) {
return std::vector<uint8_t>(); return std::vector<uint8_t>();
@ -184,9 +184,8 @@ class FakeCableAuthenticator {
public: public:
FakeCableAuthenticator() { FakeCableAuthenticator() {
handshake_key_ = crypto::HkdfSha256( handshake_key_ = crypto::HkdfSha256(
fido_parsing_utils::ConvertToStringView(kTestSessionPreKey), base::as_string_view(kTestSessionPreKey),
fido_parsing_utils::ConvertToStringView(kTestNonce), base::as_string_view(kTestNonce), kCableHandshakeKeyInfo, 32);
kCableHandshakeKeyInfo, 32);
} }
// Receives handshake message from the client, check its validity and if the // Receives handshake message from the client, check its validity and if the
@ -208,9 +207,8 @@ class FakeCableAuthenticator {
const auto client_hello = handshake_message.first(42u); const auto client_hello = handshake_message.first(42u);
if (!hmac.VerifyTruncated( if (!hmac.VerifyTruncated(
fido_parsing_utils::ConvertToStringView(client_hello), base::as_string_view(client_hello),
fido_parsing_utils::ConvertToStringView( base::as_string_view(handshake_message.subspan<42>()))) {
handshake_message.subspan<42>()))) {
return false; return false;
} }

@ -89,10 +89,6 @@ std::array<uint8_t, crypto::kSHA256Length> CreateSHA256Hash(
return hashed_data; return hashed_data;
} }
std::string_view ConvertToStringView(base::span<const uint8_t> data) {
return {reinterpret_cast<const char*>(data.data()), data.size()};
}
std::string ConvertBytesToUuid(base::span<const uint8_t, 16> bytes) { std::string ConvertBytesToUuid(base::span<const uint8_t, 16> bytes) {
uint64_t most_significant_bytes = 0; uint64_t most_significant_bytes = 0;
for (size_t i = 0; i < sizeof(uint64_t); i++) { for (size_t i = 0; i < sizeof(uint64_t); i++) {

@ -115,9 +115,6 @@ COMPONENT_EXPORT(DEVICE_FIDO)
std::array<uint8_t, crypto::kSHA256Length> CreateSHA256Hash( std::array<uint8_t, crypto::kSHA256Length> CreateSHA256Hash(
std::string_view data); std::string_view data);
COMPONENT_EXPORT(DEVICE_FIDO)
std::string_view ConvertToStringView(base::span<const uint8_t> data);
// Convert byte array into GUID formatted string as defined by RFC 4122. // Convert byte array into GUID formatted string as defined by RFC 4122.
// As we are converting 128 bit UUID, |bytes| must be have length of 16. // As we are converting 128 bit UUID, |bytes| must be have length of 16.
// https://tools.ietf.org/html/rfc4122 // https://tools.ietf.org/html/rfc4122

@ -275,10 +275,5 @@ TEST(U2fParsingUtils, CreateSHA256Hash) {
::testing::ElementsAreArray(test_data::kApplicationParameter)); ::testing::ElementsAreArray(test_data::kApplicationParameter));
} }
TEST(U2fParsingUtils, ConvertSpanToStringView) {
constexpr uint8_t kTestAsciiAbcd[] = {'a', 'b', 'c', 'd'};
EXPECT_EQ("abcd", ConvertToStringView(kTestAsciiAbcd));
}
} // namespace fido_parsing_utils } // namespace fido_parsing_utils
} // namespace device } // namespace device