Android-cuttlefish cvd tool
sign_utils.h
Go to the documentation of this file.
1/*
2 * Copyright 2021, 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#pragma once
18
19#include <stdint.h>
20
21#include <array>
22
23namespace cuttlefish {
24namespace confui {
25namespace support {
26using auth_token_key_t = std::array<uint8_t, 32>;
28
29template <typename T>
30auto bytes_cast(const T& v) -> const uint8_t (&)[sizeof(T)] {
31 return *reinterpret_cast<const uint8_t * [sizeof(T)]>(&v);
32}
33template <typename T>
34auto bytes_cast(T& v) -> uint8_t (&)[sizeof(T)] {
35 return *reinterpret_cast<uint8_t * [sizeof(T)]>(&v);
36}
37
38template <typename IntType, uint32_t byteOrder>
40
41template <typename IntType>
42struct choose_hton<IntType, __ORDER_LITTLE_ENDIAN__> {
43 inline static IntType hton(const IntType& value) {
44 IntType result = {};
45 const unsigned char* inbytes = reinterpret_cast<const unsigned char*>(&value);
46 unsigned char* outbytes = reinterpret_cast<unsigned char*>(&result);
47 for (int i = sizeof(IntType) - 1; i >= 0; --i) {
48 *(outbytes++) = inbytes[i];
49 }
50 return result;
51 }
52};
53
54template <typename IntType>
55struct choose_hton<IntType, __ORDER_BIG_ENDIAN__> {
56 inline static IntType hton(const IntType& value) { return value; }
57};
58
59template <typename IntType>
60inline IntType hton(const IntType& value) {
62}
63
65 template <typename T>
66 struct has_data {
67 template <typename U>
68 static int f(const U*, const void*) {
69 return 0;
70 }
71 template <typename U>
72 static int* f(const U* u, decltype(u->data())) {
73 return nullptr;
74 }
75 static constexpr bool value = std::is_pointer<decltype(f((T*)nullptr, ""))>::value;
76 };
77
78 public:
79 template <typename T>
80 ByteBufferProxy(const T& buffer, decltype(buffer.data()) = nullptr)
81 : data_(reinterpret_cast<const uint8_t*>(buffer.data())), size_(buffer.size()) {
82 static_assert(sizeof(decltype(*buffer.data())) == 1, "elements to large");
83 }
84
85 // this overload kicks in for types that have .c_str() but not .data(), such
86 // as hidl_string. std::string has both so we need to explicitly disable this
87 // overload if .data() is present.
88 template <typename T>
89 ByteBufferProxy(const T& buffer,
90 std::enable_if_t<!has_data<T>::value, decltype(buffer.c_str())> = nullptr)
91 : data_(reinterpret_cast<const uint8_t*>(buffer.c_str())), size_(buffer.size()) {
92 static_assert(sizeof(decltype(*buffer.c_str())) == 1, "elements to large");
93 }
94
95 template <size_t size>
96 ByteBufferProxy(const char (&buffer)[size])
97 : data_(reinterpret_cast<const uint8_t*>(buffer)), size_(size - 1) {
98 static_assert(size > 0, "even an empty string must be 0-terminated");
99 }
100
101 template <size_t size>
102 ByteBufferProxy(const uint8_t (&buffer)[size]) : data_(buffer), size_(size) {}
103
104 ByteBufferProxy() : data_(nullptr), size_(0) {}
105
106 const uint8_t* data() const { return data_; }
107 size_t size() const { return size_; }
108
109 const uint8_t* begin() const { return data_; }
110 const uint8_t* end() const { return data_ + size_; }
111
112 private:
113 const uint8_t* data_;
114 size_t size_;
115};
116
117// copied from:
118// hardware/interface/confirmationui/support/include/android/hardware/confirmationui/support/confirmationui_utils.h
119
120} // end of namespace support
121} // end of namespace confui
122} // end of namespace cuttlefish
ByteBufferProxy()
Definition: sign_utils.h:104
ByteBufferProxy(const char(&buffer)[size])
Definition: sign_utils.h:96
const uint8_t * data() const
Definition: sign_utils.h:106
ByteBufferProxy(const uint8_t(&buffer)[size])
Definition: sign_utils.h:102
const uint8_t * end() const
Definition: sign_utils.h:110
ByteBufferProxy(const T &buffer, decltype(buffer.data())=nullptr)
Definition: sign_utils.h:80
const uint8_t * begin() const
Definition: sign_utils.h:109
size_t size_
Definition: sign_utils.h:114
ByteBufferProxy(const T &buffer, std::enable_if_t<!has_data< T >::value, decltype(buffer.c_str())>=nullptr)
Definition: sign_utils.h:89
size_t size() const
Definition: sign_utils.h:107
const uint8_t * data_
Definition: sign_utils.h:113
auto bytes_cast(const T &v) -> const uint8_t(&)[sizeof(T)]
Definition: sign_utils.h:30
auth_token_key_t hmac_t
Definition: sign_utils.h:27
std::array< uint8_t, 32 > auth_token_key_t
Definition: sign_utils.h:26
IntType hton(const IntType &value)
Definition: sign_utils.h:60
Definition: alloc_driver.h:20
static int f(const U *, const void *)
Definition: sign_utils.h:68
static constexpr bool value
Definition: sign_utils.h:75
static int * f(const U *u, decltype(u->data()))
Definition: sign_utils.h:72
static IntType hton(const IntType &value)
Definition: sign_utils.h:56
static IntType hton(const IntType &value)
Definition: sign_utils.h:43
Definition: sign_utils.h:39