Home | History | Annotate | Download | only in keymaster
      1 /*
      2  * Copyright 2014 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 #ifndef SYSTEM_KEYMASTER_KEYMASTER_TAGS_H_
     18 #define SYSTEM_KEYMASTER_KEYMASTER_TAGS_H_
     19 
     20 /**
     21  * This header contains various definitions that make working with keymaster tags safer and easier.
     22  * It makes use of a fair amount of template metaprogramming, which is genarally a bad idea for
     23  * maintainability, but in this case all of the metaprogramming serves the purpose of making it
     24  * impossible to make certain classes of mistakes when operating on keymaster authorizations.  For
     25  * example, it's an error to create a keymaster_param_t with tag == KM_TAG_PURPOSE and then to
     26  * assign KM_ALGORITHM_RSA to the enumerated element of its union, but because "enumerated" is a
     27  * uint32_t, there's no way for the compiler, ordinarily, to diagnose it.  Also, generic functions
     28  * to manipulate authorizations of multiple types can't be written, because they need to know which
     29  * union parameter to modify.
     30  *
     31  * The machinery in this header solves these problems.  The core elements are two templated classes,
     32  * TypedTag and TypedEnumTag.  These classes are templated on a tag type and a tag value, and in the
     33  * case of TypedEnumTag, an enumeration type as well.  Specializations are created for each
     34  * keymaster tag, associating the tag type with the tag, and an instance of each specialization is
     35  * created, and named the same as the keymaster tag, but with the KM_ prefix omitted.  Because the
     36  * classes include a conversion operator to keymaster_tag_t, they can be used anywhere a
     37  * keymaster_tag_t is expected.
     38  *
     39  * They also define a "value_type" typedef, which specifies the type of values associated with that
     40  * particular tag.  This enables template functions to be written that check that the correct
     41  * parameter type is used for a given tag, and that use the correct union entry for the tag type.  A
     42  * very useful example is the overloaded "Authorization" function defined below, which takes tag and
     43  * value arguments and correctly constructs a keyamster_param_t struct.
     44  *
     45  * Because the classes have no data members and all of their methods are inline, they have ZERO
     46  * run-time cost in and of themselves.  The one way in which they can create code bloat is when
     47  * template functions using them are expanded multiple times.  The standard method of creating
     48  * trivial, inlined template functions which call non-templated functions which are compact but not
     49  * type-safe, allows the program to have both the type-safety of the templates and the compactness
     50  * of the non-templated functions, at the same time.
     51  *
     52  * For debugging purposes, one additional element of TypedTag and TypedEnumTag can be conditionally
     53  * compiled in.  If the "KEYMASTER_NAME_TAGS" macro symbol is defined, both classes will have a
     54  * name() method which returns a string equal to the tame of the tag (e.g. TAG_PURPOSE).  Activating
     55  * this option means the classes _do_ contain a data member, a pointer to the string, and also
     56  * causes static data space to be allocated for the strings.  So the run-time cost of these classes
     57  * is no longer zero.  Note that it can cause problems if KEYMASTER_NAME_TAGS is defined for some
     58  * compilation units and not others.
     59  */
     60 
     61 #include <hardware/hw_auth_token.h>
     62 #include <hardware/keymaster_defs.h>
     63 
     64 namespace keymaster {
     65 
     66 // The following create the numeric values that KM_TAG_PADDING and KM_TAG_DIGEST used to have.  We
     67 // need these old values to be able to support old keys that use them.
     68 static const keymaster_tag_t KM_TAG_DIGEST_OLD = static_cast<keymaster_tag_t>(KM_ENUM | 5);
     69 static const keymaster_tag_t KM_TAG_PADDING_OLD = static_cast<keymaster_tag_t>(KM_ENUM | 7);
     70 
     71 // Until we have C++11, fake std::static_assert.
     72 template <bool b> struct StaticAssert {};
     73 template <> struct StaticAssert<true> {
     74     static void check() {}
     75 };
     76 
     77 // An unusable type that we can associate with tag types that don't have a simple value type.
     78 // That will prevent the associated type from being used inadvertently.
     79 class Void {
     80     Void();
     81     ~Void();
     82 };
     83 
     84 /**
     85  * A template that defines the association between non-enumerated tag types and their value
     86  * types.  For each tag type we define a specialized struct that contains a typedef "value_type".
     87  */
     88 template <keymaster_tag_type_t tag_type> struct TagValueType {};
     89 template <> struct TagValueType<KM_ULONG> { typedef uint64_t value_type; };
     90 template <> struct TagValueType<KM_ULONG_REP> { typedef uint64_t value_type; };
     91 template <> struct TagValueType<KM_DATE> { typedef uint64_t value_type; };
     92 template <> struct TagValueType<KM_UINT> { typedef uint32_t value_type; };
     93 template <> struct TagValueType<KM_UINT_REP> { typedef uint32_t value_type; };
     94 template <> struct TagValueType<KM_INVALID> { typedef Void value_type; };
     95 template <> struct TagValueType<KM_BOOL> { typedef bool value_type; };
     96 template <> struct TagValueType<KM_BYTES> { typedef keymaster_blob_t value_type; };
     97 template <> struct TagValueType<KM_BIGNUM> { typedef keymaster_blob_t value_type; };
     98 
     99 /**
    100  * TypedTag is a templatized version of keymaster_tag_t, which provides compile-time checking of
    101  * keymaster tag types. Instances are convertible to keymaster_tag_t, so they can be used wherever
    102  * keymaster_tag_t is expected, and because they encode the tag type it's possible to create
    103  * function overloadings that only operate on tags with a particular type.
    104  */
    105 template <keymaster_tag_type_t tag_type, keymaster_tag_t tag> class TypedTag {
    106   public:
    107     typedef typename TagValueType<tag_type>::value_type value_type;
    108 
    109 #ifdef KEYMASTER_NAME_TAGS
    110     inline TypedTag(const char* name) : name_(name) {
    111 #else
    112     inline TypedTag() {
    113 #endif
    114         // Ensure that it's impossible to create a TypedTag instance whose 'tag' doesn't have type
    115         // 'tag_type'.  Attempting to instantiate a tag with the wrong type will result in a compile
    116         // error (no match for template specialization StaticAssert<false>), with no run-time cost.
    117         StaticAssert<(tag & tag_type) == tag_type>::check();
    118         StaticAssert<(tag_type != KM_ENUM) && (tag_type != KM_ENUM_REP)>::check();
    119     }
    120     inline operator keymaster_tag_t() { return tag; }
    121 #ifdef KEYMASTER_NAME_TAGS
    122     const char* name() { return name_; }
    123 
    124   private:
    125     const char* name_;
    126 #endif
    127 };
    128 
    129 template <keymaster_tag_type_t tag_type, keymaster_tag_t tag, typename KeymasterEnum>
    130 class TypedEnumTag {
    131   public:
    132     typedef KeymasterEnum value_type;
    133 
    134 #ifdef KEYMASTER_NAME_TAGS
    135     inline TypedEnumTag(const char* name) : name_(name) {
    136 #else
    137     inline TypedEnumTag() {
    138 #endif
    139         // Ensure that it's impossible to create a TypedTag instance whose 'tag' doesn't have type
    140         // 'tag_type'.  Attempting to instantiate a tag with the wrong type will result in a compile
    141         // error (no match for template specialization StaticAssert<false>), with no run-time cost.
    142         StaticAssert<(tag & tag_type) == tag_type>::check();
    143         StaticAssert<(tag_type == KM_ENUM) || (tag_type == KM_ENUM_REP)>::check();
    144     }
    145     inline operator keymaster_tag_t() { return tag; }
    146 #ifdef KEYMASTER_NAME_TAGS
    147     const char* name() { return name_; }
    148 
    149   private:
    150     const char* name_;
    151 #endif
    152 };
    153 
    154 // DEFINE_KEYMASTER_TAG is used to create TypedTag instances for each non-enum keymaster tag.
    155 #ifdef KEYMASTER_NAME_TAGS
    156 #define DEFINE_KEYMASTER_TAG(type, name) static TypedTag<type, KM_##name> name(#name)
    157 #else
    158 #define DEFINE_KEYMASTER_TAG(type, name) static TypedTag<type, KM_##name> name
    159 #endif
    160 
    161 DEFINE_KEYMASTER_TAG(KM_INVALID, TAG_INVALID);
    162 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_KEY_SIZE);
    163 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MAC_LENGTH);
    164 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_CALLER_NONCE);
    165 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MIN_MAC_LENGTH);
    166 DEFINE_KEYMASTER_TAG(KM_ULONG, TAG_RSA_PUBLIC_EXPONENT);
    167 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_ACTIVE_DATETIME);
    168 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_ORIGINATION_EXPIRE_DATETIME);
    169 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_USAGE_EXPIRE_DATETIME);
    170 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MIN_SECONDS_BETWEEN_OPS);
    171 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MAX_USES_PER_BOOT);
    172 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ALL_USERS);
    173 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_USER_ID);
    174 DEFINE_KEYMASTER_TAG(KM_ULONG_REP, TAG_USER_SECURE_ID);
    175 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_NO_AUTH_REQUIRED);
    176 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_AUTH_TIMEOUT);
    177 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ALL_APPLICATIONS);
    178 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_APPLICATION_ID);
    179 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_APPLICATION_DATA);
    180 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_CREATION_DATETIME);
    181 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ROLLBACK_RESISTANT);
    182 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_ROOT_OF_TRUST);
    183 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_ASSOCIATED_DATA);
    184 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_NONCE);
    185 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_AUTH_TOKEN);
    186 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_BOOTLOADER_ONLY);
    187 
    188 #ifdef KEYMASTER_NAME_TAGS
    189 #define DEFINE_KEYMASTER_ENUM_TAG(type, name, enumtype)                                            \
    190     static TypedEnumTag<type, KM_##name, enumtype> name(#name)
    191 #else
    192 #define DEFINE_KEYMASTER_ENUM_TAG(type, name, enumtype)                                            \
    193     static TypedEnumTag<type, KM_##name, enumtype> name
    194 #endif
    195 
    196 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_PURPOSE, keymaster_purpose_t);
    197 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_ALGORITHM, keymaster_algorithm_t);
    198 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_BLOCK_MODE, keymaster_block_mode_t);
    199 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_DIGEST, keymaster_digest_t);
    200 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_DIGEST_OLD, keymaster_digest_t);
    201 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_PADDING, keymaster_padding_t);
    202 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_PADDING_OLD, keymaster_padding_t);
    203 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_BLOB_USAGE_REQUIREMENTS,
    204                           keymaster_key_blob_usage_requirements_t);
    205 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_ORIGIN, keymaster_key_origin_t);
    206 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_USER_AUTH_TYPE, hw_authenticator_type_t);
    207 
    208 //
    209 // Overloaded function "Authorization" to create keymaster_key_param_t objects for all of tags.
    210 //
    211 
    212 template <keymaster_tag_t Tag>
    213 inline keymaster_key_param_t Authorization(TypedTag<KM_BOOL, Tag> tag) {
    214     return keymaster_param_bool(tag);
    215 }
    216 
    217 template <keymaster_tag_t Tag>
    218 inline keymaster_key_param_t Authorization(TypedTag<KM_UINT, Tag> tag, uint32_t value) {
    219     return keymaster_param_int(tag, value);
    220 }
    221 
    222 template <keymaster_tag_t Tag>
    223 inline keymaster_key_param_t Authorization(TypedTag<KM_UINT_REP, Tag> tag, uint32_t value) {
    224     return keymaster_param_int(tag, value);
    225 }
    226 
    227 template <keymaster_tag_t Tag>
    228 inline keymaster_key_param_t Authorization(TypedTag<KM_ULONG, Tag> tag, uint64_t value) {
    229     return keymaster_param_long(tag, value);
    230 }
    231 
    232 template <keymaster_tag_t Tag>
    233 inline keymaster_key_param_t Authorization(TypedTag<KM_ULONG_REP, Tag> tag, uint64_t value) {
    234     return keymaster_param_long(tag, value);
    235 }
    236 
    237 template <keymaster_tag_t Tag>
    238 inline keymaster_key_param_t Authorization(TypedTag<KM_DATE, Tag> tag, uint64_t value) {
    239     return keymaster_param_date(tag, value);
    240 }
    241 
    242 template <keymaster_tag_t Tag>
    243 inline keymaster_key_param_t Authorization(TypedTag<KM_BYTES, Tag> tag, const void* bytes,
    244                                            size_t bytes_len) {
    245     return keymaster_param_blob(tag, reinterpret_cast<const uint8_t*>(bytes), bytes_len);
    246 }
    247 
    248 template <keymaster_tag_t Tag>
    249 inline keymaster_key_param_t Authorization(TypedTag<KM_BYTES, Tag> tag,
    250                                            const keymaster_blob_t& blob) {
    251     return keymaster_param_blob(tag, blob.data, blob.data_length);
    252 }
    253 
    254 template <keymaster_tag_t Tag>
    255 inline keymaster_key_param_t Authorization(TypedTag<KM_BIGNUM, Tag> tag, const void* bytes,
    256                                            size_t bytes_len) {
    257     return keymaster_param_blob(tag, reinterpret_cast<const uint8_t*>(bytes), bytes_len);
    258 }
    259 
    260 template <keymaster_tag_t Tag>
    261 inline keymaster_key_param_t Authorization(TypedTag<KM_BIGNUM, Tag> tag,
    262                                            const keymaster_blob_t& blob) {
    263     return keymaster_param_blob(tag, blob.data, blob.data_length);
    264 }
    265 
    266 template <keymaster_tag_t Tag, typename KeymasterEnum>
    267 inline keymaster_key_param_t Authorization(TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag,
    268                                            KeymasterEnum value) {
    269     return keymaster_param_enum(tag, value);
    270 }
    271 
    272 template <keymaster_tag_t Tag, typename KeymasterEnum>
    273 inline keymaster_key_param_t Authorization(TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag,
    274                                            KeymasterEnum value) {
    275     return keymaster_param_enum(tag, value);
    276 }
    277 
    278 }  // namespace keymaster
    279 
    280 #endif  // SYSTEM_KEYMASTER_KEYMASTER_TAGS_H_
    281