Shawn Willden | 98b998b | 2018-01-20 11:48:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <hardware/hw_auth_token.h> |
| 18 | #include <keymasterV4_0/keymaster_utils.h> |
| 19 | |
| 20 | namespace android { |
| 21 | namespace hardware { |
| 22 | namespace keymaster { |
| 23 | namespace V4_0 { |
| 24 | namespace support { |
| 25 | |
| 26 | template <typename T, typename InIter> |
| 27 | inline static InIter copy_bytes_from_iterator(T* value, InIter src) { |
| 28 | uint8_t* value_ptr = reinterpret_cast<uint8_t*>(value); |
| 29 | std::copy(src, src + sizeof(T), value_ptr); |
| 30 | return src + sizeof(T); |
| 31 | } |
| 32 | |
| 33 | template <typename T, typename OutIter> |
| 34 | inline static OutIter copy_bytes_to_iterator(const T& value, OutIter dest) { |
| 35 | const uint8_t* value_ptr = reinterpret_cast<const uint8_t*>(&value); |
| 36 | return std::copy(value_ptr, value_ptr + sizeof(value), dest); |
| 37 | } |
| 38 | |
| 39 | constexpr size_t kHmacSize = 32; |
| 40 | |
| 41 | hidl_vec<uint8_t> authToken2HidlVec(const HardwareAuthToken& token) { |
| 42 | static_assert(1 /* version size */ + sizeof(token.challenge) + sizeof(token.userId) + |
| 43 | sizeof(token.authenticatorId) + sizeof(token.authenticatorType) + |
| 44 | sizeof(token.timestamp) + kHmacSize == |
| 45 | sizeof(hw_auth_token_t), |
| 46 | "HardwareAuthToken content size does not match hw_auth_token_t size"); |
| 47 | |
| 48 | hidl_vec<uint8_t> result; |
| 49 | result.resize(sizeof(hw_auth_token_t)); |
| 50 | auto pos = result.begin(); |
| 51 | *pos++ = 0; // Version byte |
| 52 | pos = copy_bytes_to_iterator(token.challenge, pos); |
| 53 | pos = copy_bytes_to_iterator(token.userId, pos); |
| 54 | pos = copy_bytes_to_iterator(token.authenticatorId, pos); |
| 55 | auto auth_type = htonl(static_cast<uint32_t>(token.authenticatorType)); |
| 56 | pos = copy_bytes_to_iterator(auth_type, pos); |
| 57 | auto timestamp = htonq(token.timestamp); |
| 58 | pos = copy_bytes_to_iterator(timestamp, pos); |
| 59 | if (token.mac.size() != kHmacSize) { |
| 60 | std::fill(pos, pos + kHmacSize, 0); |
| 61 | } else { |
| 62 | std::copy(token.mac.begin(), token.mac.end(), pos); |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | HardwareAuthToken hidlVec2AuthToken(const hidl_vec<uint8_t>& buffer) { |
| 69 | HardwareAuthToken token; |
| 70 | static_assert(1 /* version size */ + sizeof(token.challenge) + sizeof(token.userId) + |
| 71 | sizeof(token.authenticatorId) + sizeof(token.authenticatorType) + |
| 72 | sizeof(token.timestamp) + kHmacSize == |
| 73 | sizeof(hw_auth_token_t), |
| 74 | "HardwareAuthToken content size does not match hw_auth_token_t size"); |
| 75 | |
| 76 | if (buffer.size() != sizeof(hw_auth_token_t)) return {}; |
| 77 | |
| 78 | auto pos = buffer.begin(); |
| 79 | ++pos; // skip first byte |
| 80 | pos = copy_bytes_from_iterator(&token.challenge, pos); |
| 81 | pos = copy_bytes_from_iterator(&token.userId, pos); |
| 82 | pos = copy_bytes_from_iterator(&token.authenticatorId, pos); |
| 83 | pos = copy_bytes_from_iterator(&token.authenticatorType, pos); |
| 84 | token.authenticatorType = static_cast<HardwareAuthenticatorType>( |
| 85 | ntohl(static_cast<uint32_t>(token.authenticatorType))); |
| 86 | pos = copy_bytes_from_iterator(&token.timestamp, pos); |
| 87 | token.timestamp = ntohq(token.timestamp); |
| 88 | token.mac.resize(kHmacSize); |
| 89 | std::copy(pos, pos + kHmacSize, token.mac.data()); |
| 90 | |
| 91 | return token; |
| 92 | } |
| 93 | |
| 94 | } // namespace support |
| 95 | } // namespace V4_0 |
| 96 | } // namespace keymaster |
| 97 | } // namespace hardware |
| 98 | } // namespace android |