/* * Copyright (C) 2018 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. */ #include #include namespace android { namespace hardware { inline static bool operator<(const hidl_vec& a, const hidl_vec& b) { return memcmp(a.data(), b.data(), std::min(a.size(), b.size())) == -1; } template inline static bool operator<(const hidl_array& a, const hidl_array& b) { return memcmp(a.data(), b.data(), SIZE) == -1; } namespace keymaster { namespace V4_0 { bool operator<(const HmacSharingParameters& a, const HmacSharingParameters& b) { return std::tie(a.seed, a.nonce) < std::tie(b.seed, b.nonce); } namespace support { template inline static InIter copy_bytes_from_iterator(T* value, InIter src) { uint8_t* value_ptr = reinterpret_cast(value); std::copy(src, src + sizeof(T), value_ptr); return src + sizeof(T); } template inline static OutIter copy_bytes_to_iterator(const T& value, OutIter dest) { const uint8_t* value_ptr = reinterpret_cast(&value); return std::copy(value_ptr, value_ptr + sizeof(value), dest); } constexpr size_t kHmacSize = 32; hidl_vec authToken2HidlVec(const HardwareAuthToken& token) { static_assert(1 /* version size */ + sizeof(token.challenge) + sizeof(token.userId) + sizeof(token.authenticatorId) + sizeof(token.authenticatorType) + sizeof(token.timestamp) + kHmacSize == sizeof(hw_auth_token_t), "HardwareAuthToken content size does not match hw_auth_token_t size"); hidl_vec result; result.resize(sizeof(hw_auth_token_t)); auto pos = result.begin(); *pos++ = 0; // Version byte pos = copy_bytes_to_iterator(token.challenge, pos); pos = copy_bytes_to_iterator(token.userId, pos); pos = copy_bytes_to_iterator(token.authenticatorId, pos); auto auth_type = htonl(static_cast(token.authenticatorType)); pos = copy_bytes_to_iterator(auth_type, pos); auto timestamp = htonq(token.timestamp); pos = copy_bytes_to_iterator(timestamp, pos); if (token.mac.size() != kHmacSize) { std::fill(pos, pos + kHmacSize, 0); } else { std::copy(token.mac.begin(), token.mac.end(), pos); } return result; } HardwareAuthToken hidlVec2AuthToken(const hidl_vec& buffer) { HardwareAuthToken token; static_assert(1 /* version size */ + sizeof(token.challenge) + sizeof(token.userId) + sizeof(token.authenticatorId) + sizeof(token.authenticatorType) + sizeof(token.timestamp) + kHmacSize == sizeof(hw_auth_token_t), "HardwareAuthToken content size does not match hw_auth_token_t size"); if (buffer.size() != sizeof(hw_auth_token_t)) return {}; auto pos = buffer.begin(); ++pos; // skip first byte pos = copy_bytes_from_iterator(&token.challenge, pos); pos = copy_bytes_from_iterator(&token.userId, pos); pos = copy_bytes_from_iterator(&token.authenticatorId, pos); pos = copy_bytes_from_iterator(&token.authenticatorType, pos); token.authenticatorType = static_cast( ntohl(static_cast(token.authenticatorType))); pos = copy_bytes_from_iterator(&token.timestamp, pos); token.timestamp = ntohq(token.timestamp); token.mac.resize(kHmacSize); std::copy(pos, pos + kHmacSize, token.mac.data()); return token; } } // namespace support } // namespace V4_0 } // namespace keymaster } // namespace hardware } // namespace android