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 #include <keymaster/keymaster_tags.h>
18 
19 namespace keymaster {
20 
21 #ifdef KEYMASTER_NAME_TAGS
StringifyTag(keymaster_tag_t tag)22 const char* StringifyTag(keymaster_tag_t tag) {
23     switch (tag) {
24     case KM_TAG_INVALID:
25         return "KM_TAG_INVALID";
26     case KM_TAG_PURPOSE:
27         return "KM_TAG_PURPOSE";
28     case KM_TAG_ALGORITHM:
29         return "KM_TAG_ALGORITHM";
30     case KM_TAG_KEY_SIZE:
31         return "KM_TAG_KEY_SIZE";
32     case KM_TAG_BLOCK_MODE:
33         return "KM_TAG_BLOCK_MODE";
34     case KM_TAG_DIGEST:
35         return "KM_TAG_DIGEST";
36     case KM_TAG_PADDING:
37         return "KM_TAG_PADDING";
38     case KM_TAG_CALLER_NONCE:
39         return "KM_TAG_CALLER_NONCE";
40     case KM_TAG_MIN_MAC_LENGTH:
41         return "KM_TAG_MIN_MAC_LENGTH";
42     case KM_TAG_RSA_PUBLIC_EXPONENT:
43         return "KM_TAG_RSA_PUBLIC_EXPONENT";
44     case KM_TAG_BLOB_USAGE_REQUIREMENTS:
45         return "KM_TAG_BLOB_USAGE_REQUIREMENTS";
46     case KM_TAG_BOOTLOADER_ONLY:
47         return "KM_TAG_BOOTLOADER_ONLY";
48     case KM_TAG_ACTIVE_DATETIME:
49         return "KM_TAG_ACTIVE_DATETIME";
50     case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
51         return "KM_TAG_ORIGINATION_EXPIRE_DATETIME";
52     case KM_TAG_USAGE_EXPIRE_DATETIME:
53         return "KM_TAG_USAGE_EXPIRE_DATETIME";
54     case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
55         return "KM_TAG_MIN_SECONDS_BETWEEN_OPS";
56     case KM_TAG_MAX_USES_PER_BOOT:
57         return "KM_TAG_MAX_USES_PER_BOOT";
58     case KM_TAG_ALL_USERS:
59         return "KM_TAG_ALL_USERS";
60     case KM_TAG_USER_ID:
61         return "KM_TAG_USER_ID";
62     case KM_TAG_USER_SECURE_ID:
63         return "KM_TAG_USER_SECURE_ID";
64     case KM_TAG_NO_AUTH_REQUIRED:
65         return "KM_TAG_NO_AUTH_REQUIRED";
66     case KM_TAG_USER_AUTH_TYPE:
67         return "KM_TAG_USER_AUTH_TYPE";
68     case KM_TAG_AUTH_TIMEOUT:
69         return "KM_TAG_AUTH_TIMEOUT";
70     case KM_TAG_ALL_APPLICATIONS:
71         return "KM_TAG_ALL_APPLICATIONS";
72     case KM_TAG_APPLICATION_ID:
73         return "KM_TAG_APPLICATION_ID";
74     case KM_TAG_APPLICATION_DATA:
75         return "KM_TAG_APPLICATION_DATA";
76     case KM_TAG_CREATION_DATETIME:
77         return "KM_TAG_CREATION_DATETIME";
78     case KM_TAG_ORIGIN:
79         return "KM_TAG_ORIGIN";
80     case KM_TAG_ROLLBACK_RESISTANT:
81         return "KM_TAG_ROLLBACK_RESISTANT";
82     case KM_TAG_ROOT_OF_TRUST:
83         return "KM_TAG_ROOT_OF_TRUST";
84     case KM_TAG_ASSOCIATED_DATA:
85         return "KM_TAG_ASSOCIATED_DATA";
86     case KM_TAG_NONCE:
87         return "KM_TAG_NONCE";
88     case KM_TAG_AUTH_TOKEN:
89         return "KM_TAG_AUTH_TOKEN";
90     case KM_TAG_MAC_LENGTH:
91         return "KM_TAG_MAC_LENGTH";
92     case KM_TAG_KDF:
93         return "KM_TAG_KDF";
94     case KM_TAG_EC_CURVE:
95         return "KM_TAG_EC_CURVE";
96     case KM_TAG_ECIES_SINGLE_HASH_MODE:
97         return "KM_TAG_ECIES_SINGLE_HASH_MODE";
98     case KM_TAG_OS_VERSION:
99         return "KM_TAG_OS_VERSION";
100     case KM_TAG_OS_PATCHLEVEL:
101         return "KM_TAG_OS_PATCHLEVEL";
102     case KM_TAG_EXPORTABLE:
103         return "KM_TAG_EXPORTABLE";
104     case KM_TAG_UNIQUE_ID:
105         return "KM_TAG_UNIQUE_ID";
106     case KM_TAG_INCLUDE_UNIQUE_ID:
107         return "KM_TAG_INCLUDE_UNIQUE_ID";
108     case KM_TAG_RESET_SINCE_ID_ROTATION:
109         return "KM_TAG_RESET_SINCE_ID_ROTATION";
110     case KM_TAG_ALLOW_WHILE_ON_BODY:
111         return "KM_TAG_ALLOW_WHILE_ON_BODY";
112     case KM_TAG_ATTESTATION_CHALLENGE:
113         return "KM_TAG_ATTESTATION_CHALLENGE";
114     }
115     return "<Unknown>";
116 }
117 #endif  // KEYMASTER_NAME_TAGS
118 
119 // DEFINE_KEYMASTER_TAG is used to create TypedTag instances for each non-enum keymaster tag.
120 #define DEFINE_KEYMASTER_TAG(type, name) TypedTag<type, KM_##name> name
121 
122 DEFINE_KEYMASTER_TAG(KM_INVALID, TAG_INVALID);
123 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_KEY_SIZE);
124 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MAC_LENGTH);
125 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_CALLER_NONCE);
126 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MIN_MAC_LENGTH);
127 DEFINE_KEYMASTER_TAG(KM_ULONG, TAG_RSA_PUBLIC_EXPONENT);
128 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ECIES_SINGLE_HASH_MODE);
129 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_INCLUDE_UNIQUE_ID);
130 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_ACTIVE_DATETIME);
131 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_ORIGINATION_EXPIRE_DATETIME);
132 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_USAGE_EXPIRE_DATETIME);
133 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MIN_SECONDS_BETWEEN_OPS);
134 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_MAX_USES_PER_BOOT);
135 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ALL_USERS);
136 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_USER_ID);
137 DEFINE_KEYMASTER_TAG(KM_ULONG_REP, TAG_USER_SECURE_ID);
138 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_NO_AUTH_REQUIRED);
139 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_AUTH_TIMEOUT);
140 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ALLOW_WHILE_ON_BODY);
141 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ALL_APPLICATIONS);
142 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_APPLICATION_ID);
143 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_APPLICATION_DATA);
144 DEFINE_KEYMASTER_TAG(KM_DATE, TAG_CREATION_DATETIME);
145 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_ROLLBACK_RESISTANT);
146 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_ROOT_OF_TRUST);
147 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_ASSOCIATED_DATA);
148 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_NONCE);
149 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_AUTH_TOKEN);
150 DEFINE_KEYMASTER_TAG(KM_BOOL, TAG_BOOTLOADER_ONLY);
151 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_OS_VERSION);
152 DEFINE_KEYMASTER_TAG(KM_UINT, TAG_OS_PATCHLEVEL);
153 DEFINE_KEYMASTER_TAG(KM_BYTES, TAG_UNIQUE_ID);
154 
155 // DEFINE_KEYMASTER_ENUM_TAG is used to create TypedEnumTag instances for each enum keymaster tag.
156 
157 #define DEFINE_KEYMASTER_ENUM_TAG(type, name, enumtype) TypedEnumTag<type, KM_##name, enumtype> name
158 
159 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_PURPOSE, keymaster_purpose_t);
160 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_ALGORITHM, keymaster_algorithm_t);
161 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_BLOCK_MODE, keymaster_block_mode_t);
162 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_DIGEST, keymaster_digest_t);
163 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_DIGEST_OLD, keymaster_digest_t);
164 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_PADDING, keymaster_padding_t);
165 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_PADDING_OLD, keymaster_padding_t);
166 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_BLOB_USAGE_REQUIREMENTS,
167                           keymaster_key_blob_usage_requirements_t);
168 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_ORIGIN, keymaster_key_origin_t);
169 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_USER_AUTH_TYPE, hw_authenticator_type_t);
170 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM_REP, TAG_KDF, keymaster_kdf_t);
171 DEFINE_KEYMASTER_ENUM_TAG(KM_ENUM, TAG_EC_CURVE, keymaster_ec_curve_t);
172 
173 }  // namespace keymaster
174