blob: 3f409b4d838e17ea223fb0879605764d8d4c110b [file] [log] [blame]
Shawn Willden9d645a02014-06-12 13:48:46 -06001/*
2 * Copyright (C) 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 ANDROID_HARDWARE_KEYMASTER_DEFS_H
18#define ANDROID_HARDWARE_KEYMASTER_DEFS_H
19
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23
24#if defined(__cplusplus)
25extern "C" {
26#endif // defined(__cplusplus)
27
28/*!
29 * \deprecated Flags for keymaster_device::flags
30 *
31 * keymaster_device::flags is deprecated and will be removed in the
32 * next version of the API in favor of the more detailed information
33 * available from TODO:
34 */
35enum {
36 /*
37 * Indicates this keymaster implementation does not have hardware that
38 * keeps private keys out of user space.
39 *
40 * This should not be implemented on anything other than the default
41 * implementation.
42 */
43 KEYMASTER_SOFTWARE_ONLY = 1 << 0,
44
45 /*
46 * This indicates that the key blobs returned via all the primitives
47 * are sufficient to operate on their own without the trusted OS
48 * querying userspace to retrieve some other data. Key blobs of
49 * this type are normally returned encrypted with a
50 * Key Encryption Key (KEK).
51 *
52 * This is currently used by "vold" to know whether the whole disk
53 * encryption secret can be unwrapped without having some external
54 * service started up beforehand since the "/data" partition will
55 * be unavailable at that point.
56 */
57 KEYMASTER_BLOBS_ARE_STANDALONE = 1 << 1,
58
59 /*
60 * Indicates that the keymaster module supports DSA keys.
61 */
62 KEYMASTER_SUPPORTS_DSA = 1 << 2,
63
64 /*
65 * Indicates that the keymaster module supports EC keys.
66 */
67 KEYMASTER_SUPPORTS_EC = 1 << 3,
68};
69
70/**
71 * \deprecated Asymmetric key pair types.
72 */
73typedef enum {
74 TYPE_RSA = 1,
75 TYPE_DSA = 2,
76 TYPE_EC = 3,
77} keymaster_keypair_t;
78
79/**
80 * Authorization tags each have an associated type. This enumeration facilitates tagging each with
81 * a type, by using the high four bits (of an implied 32-bit unsigned enum value) to specify up to
82 * 16 data types. These values are ORed with tag IDs to generate the final tag ID values.
83 */
84typedef enum {
85 KM_INVALID = 0 << 28, /* Invalid type, used to designate a tag as uninitialized */
86 KM_ENUM = 1 << 28,
87 KM_ENUM_REP = 2 << 28, /* Repeatable enumeration value. */
88 KM_INT = 3 << 28,
89 KM_INT_REP = 4 << 28, /* Repeatable integer value */
90 KM_LONG = 5 << 28,
91 KM_DATE = 6 << 28,
92 KM_BOOL = 7 << 28,
93 KM_BIGNUM = 8 << 28,
94 KM_BYTES = 9 << 28,
95} keymaster_tag_type_t;
96
97typedef enum {
98 KM_TAG_INVALID = KM_INVALID | 0,
99
100 /*
101 * Tags that must be semantically enforced by hardware and software implementations.
102 */
103
104 /* Crypto parameters */
105 KM_TAG_PURPOSE = KM_ENUM_REP | 1, /* keymaster_purpose_t. */
106 KM_TAG_ALGORITHM = KM_ENUM | 2, /* keymaster_algorithm_t. */
107 KM_TAG_KEY_SIZE = KM_INT | 3, /* Key size in bits. */
108 KM_TAG_BLOCK_MODE = KM_ENUM | 4, /* keymaster_block_mode_t. */
109 KM_TAG_DIGEST = KM_ENUM | 5, /* keymaster_digest_t. */
110 KM_TAG_MAC_LENGTH = KM_INT | 6, /* MAC length in bits. */
111 KM_TAG_PADDING = KM_ENUM | 7, /* keymaster_padding_t. */
112 KM_TAG_CHUNK_LENGTH = KM_INT | 8, /* AEAD mode minimum decryption chunk size, in bytes. */
113 KM_TAG_NONCE = KM_BYTES | 9, /* Nonce or Initialization Vector */
114
115 /* Other hardware-enforced. */
116 KM_TAG_RESCOPING_ADD = KM_ENUM_REP | 101, /* Tags authorized for addition via rescoping. */
117 KM_TAG_RESCOPING_DEL = KM_ENUM_REP | 102, /* Tags authorized for removal via rescoping. */
118 KM_TAG_BLOB_USAGE_REQUIREMENTS = KM_ENUM | 705, /* keymaster_key_blob_usage_requirements_t */
119
120 /* Algorithm-specific. */
121 KM_TAG_RSA_PUBLIC_EXPONENT = KM_LONG | 200, /* Defaults to 2^16+1 */
122 KM_TAG_DSA_GENERATOR = KM_BIGNUM | 201,
123 KM_TAG_DSA_P = KM_BIGNUM | 202,
124 KM_TAG_DSA_Q = KM_BIGNUM | 203,
125 /* Note there are no EC-specific params. Field size is defined by KM_TAG_KEY_SIZE, and the
126 curve is chosen from NIST recommendations for field size */
127
128 /*
129 * Tags that should be semantically enforced by hardware if possible and will otherwise be
130 * enforced by software (keystore).
131 */
132
133 /* Key validity period */
134 KM_TAG_ACTIVE_DATETIME = KM_DATE | 400, /* Start of validity */
135 KM_TAG_ORIGINATION_EXPIRE_DATETIME = KM_DATE | 401, /* Date when new "messages" should no
136 longer be created. */
137 KM_TAG_USAGE_EXPIRE_DATETIME = KM_DATE | 402, /* Date when existing "messages" should no
138 longer be trusted. */
139 KM_TAG_MIN_SECONDS_BETWEEN_OPS = KM_INT | 403, /* Minimum elapsed time between
140 cryptographic operations with the key. */
Shawn Willdendc0007b2015-01-23 01:01:21 -0700141 KM_TAG_MAX_USES_PER_BOOT = KM_INT | 404, /* Number of times the key can be used per
142 boot. */
Shawn Willden9d645a02014-06-12 13:48:46 -0600143
144 /* User authentication */
Shawn Willdendc0007b2015-01-23 01:01:21 -0700145 KM_TAG_ALL_USERS = KM_BOOL | 500, /* If key is usable by all users. */
146 KM_TAG_USER_ID = KM_INT | 501, /* ID of authorized user. Disallowed if
147 KM_TAG_ALL_USERS is present. */
Shawn Willden9d645a02014-06-12 13:48:46 -0600148 KM_TAG_NO_AUTH_REQUIRED = KM_BOOL | 502, /* If key is usable without authentication. */
149 KM_TAG_USER_AUTH_ID = KM_INT_REP | 503, /* ID of the authenticator to use (e.g. password,
150 fingerprint, etc.). Repeatable to support
151 multi-factor auth. Disallowed if
152 KM_TAG_NO_AUTH_REQUIRED is present. */
153 KM_TAG_AUTH_TIMEOUT = KM_INT | 504, /* Required freshness of user authentication for
154 private/secret key operations, in seconds.
155 Public key operations require no authentication.
156 If absent, authentication is required for every
157 use. Authentication state is lost when the
158 device is powered off. */
Shawn Willden9d645a02014-06-12 13:48:46 -0600159
160 /* Application access control */
161 KM_TAG_ALL_APPLICATIONS = KM_BOOL | 600, /* If key is usable by all applications. */
162 KM_TAG_APPLICATION_ID = KM_BYTES | 601, /* ID of authorized application. Disallowed if
163 KM_TAG_ALL_APPLICATIONS is present. */
164
165 /*
166 * Semantically unenforceable tags, either because they have no specific meaning or because
167 * they're informational only.
168 */
169 KM_TAG_APPLICATION_DATA = KM_BYTES | 700, /* Data provided by authorized application. */
170 KM_TAG_CREATION_DATETIME = KM_DATE | 701, /* Key creation time */
171 KM_TAG_ORIGIN = KM_ENUM | 702, /* keymaster_key_origin_t. */
172 KM_TAG_ROLLBACK_RESISTANT = KM_BOOL | 703, /* Whether key is rollback-resistant. */
173 KM_TAG_ROOT_OF_TRUST = KM_BYTES | 704, /* Root of trust ID. Empty array means usable by all
174 roots. */
175
176 /* Tags used only to provide data to operations */
177 KM_TAG_ADDITIONAL_DATA = KM_BYTES | 1000, /* Used to provide additional data for AEAD modes. */
178} keymaster_tag_t;
179
180/**
181 * Algorithms that may be provided by keymaster implementations. Those that must be provided by all
182 * implementations are tagged as "required". Note that where the values in this enumeration overlap
183 * with the values for the deprecated keymaster_keypair_t, the same algorithm must be
184 * specified. This type is new in 0_4 and replaces the deprecated keymaster_keypair_t.
185 */
186typedef enum {
187 /* Asymmetric algorithms. */
188 KM_ALGORITHM_RSA = 1, /* required */
189 KM_ALGORITHM_DSA = 2, /* required */
190 KM_ALGORITHM_ECDSA = 3, /* required */
191 KM_ALGORITHM_ECIES = 4,
192 /* FIPS Approved Ciphers */
193 KM_ALGORITHM_AES = 32, /* required */
194 KM_ALGORITHM_3DES = 33,
195 KM_ALGORITHM_SKIPJACK = 34,
196 /* AES Finalists */
197 KM_ALGORITHM_MARS = 48,
198 KM_ALGORITHM_RC6 = 49,
199 KM_ALGORITHM_SERPENT = 50,
200 KM_ALGORITHM_TWOFISH = 51,
201 /* Other common block ciphers */
202 KM_ALGORITHM_IDEA = 52,
203 KM_ALGORITHM_RC5 = 53,
204 KM_ALGORITHM_CAST5 = 54,
205 KM_ALGORITHM_BLOWFISH = 55,
206 /* Common stream ciphers */
207 KM_ALGORITHM_RC4 = 64,
208 KM_ALGORITHM_CHACHA20 = 65,
209 /* MAC algorithms */
210 KM_ALGORITHM_HMAC = 128, /* required */
211} keymaster_algorithm_t;
212
213/**
214 * Symmetric block cipher modes that may be provided by keymaster implementations. Those that must
215 * be provided by all implementations are tagged as "required". This type is new in 0_4.
216 *
217 * KM_MODE_FIRST_UNAUTHENTICATED, KM_MODE_FIRST_AUTHENTICATED and KM_MODE_FIRST_MAC are not modes,
218 * but markers used to separate the available modes into classes.
219 */
220typedef enum {
221 /* Unauthenticated modes, usable only for encryption/decryption and not generally recommended
222 * except for compatibility with existing other protocols. */
223 KM_MODE_FIRST_UNAUTHENTICATED = 1,
224 KM_MODE_ECB = KM_MODE_FIRST_UNAUTHENTICATED, /* required */
225 KM_MODE_CBC = 2, /* required */
226 KM_MODE_CBC_CTS = 3, /* recommended */
227 KM_MODE_CTR = 4, /* recommended */
228 KM_MODE_OFB = 5,
229 KM_MODE_CFB = 6,
230 KM_MODE_XTS = 7, /* Note: requires double-length keys */
231 /* Authenticated modes, usable for encryption/decryption and signing/verification. Recommended
232 * over unauthenticated modes for all purposes. One of KM_MODE_GCM and KM_MODE_OCB is
233 * required. */
234 KM_MODE_FIRST_AUTHENTICATED = 32,
235 KM_MODE_GCM = KM_MODE_FIRST_AUTHENTICATED,
236 KM_MODE_OCB = 33,
237 KM_MODE_CCM = 34,
238 /* MAC modes -- only for signing/verification */
239 KM_MODE_FIRST_MAC = 128,
240 KM_MODE_CMAC = KM_MODE_FIRST_MAC,
241 KM_MODE_POLY1305 = 129,
242} keymaster_block_mode_t;
243
244/**
245 * Padding modes that may be applied to plaintext for encryption operations. This list includes
246 * padding modes for both symmetric and asymmetric algorithms. Note that implementations should not
247 * provide all possible combinations of algorithm and padding, only the
248 * cryptographically-appropriate pairs.
249 */
250typedef enum {
251 KM_PAD_NONE = 1, /* required, deprecated */
252 KM_PAD_RSA_OAEP = 2, /* required */
253 KM_PAD_RSA_PSS = 3, /* required */
254 KM_PAD_RSA_PKCS1_1_5_ENCRYPT = 4,
255 KM_PAD_RSA_PKCS1_1_5_SIGN = 5,
256 KM_PAD_ANSI_X923 = 32,
257 KM_PAD_ISO_10126 = 33,
258 KM_PAD_ZERO = 64, /* required */
259 KM_PAD_PKCS7 = 65, /* required */
260 KM_PAD_ISO_7816_4 = 66,
261} keymaster_padding_t;
262
263/**
264 * Digests that may be provided by keymaster implementations. Those that must be provided by all
265 * implementations are tagged as "required". Those that have been added since version 0_2 of the
266 * API are tagged as "new".
267 */
268typedef enum {
269 KM_DIGEST_NONE = 0, /* new, required */
270 DIGEST_NONE = KM_DIGEST_NONE, /* For 0_2 compatibility */
271 KM_DIGEST_MD5 = 1, /* new, for compatibility with old protocols only */
272 KM_DIGEST_SHA1 = 2, /* new */
273 KM_DIGEST_SHA_2_224 = 3, /* new */
274 KM_DIGEST_SHA_2_256 = 4, /* new, required */
275 KM_DIGEST_SHA_2_384 = 5, /* new, recommended */
276 KM_DIGEST_SHA_2_512 = 6, /* new, recommended */
277 KM_DIGEST_SHA_3_256 = 7, /* new */
278 KM_DIGEST_SHA_3_384 = 8, /* new */
279 KM_DIGEST_SHA_3_512 = 9, /* new */
280} keymaster_digest_t;
281
282/**
283 * The origin of a key (or pair), i.e. where it was generated. Origin and can be used together to
284 * determine whether a key may have existed outside of secure hardware. This type is new in 0_4.
285 */
286typedef enum {
287 KM_ORIGIN_HARDWARE = 0, /* Generated in secure hardware */
288 KM_ORIGIN_SOFTWARE = 1, /* Generated in non-secure software */
289 KM_ORIGIN_IMPORTED = 2, /* Imported, origin unknown */
290} keymaster_key_origin_t;
291
292/**
293 * Usability requirements of key blobs. This defines what system functionality must be available
294 * for the key to function. For example, key "blobs" which are actually handles referencing
295 * encrypted key material stored in the file system cannot be used until the file system is
296 * available, and should have BLOB_REQUIRES_FILE_SYSTEM. Other requirements entries will be added
297 * as needed for implementations. This type is new in 0_4.
298 */
299typedef enum {
300 KM_BLOB_STANDALONE = 0,
301 KM_BLOB_REQUIRES_FILE_SYSTEM = 1,
302} keymaster_key_blob_usage_requirements_t;
303
304/**
305 * Possible purposes of a key (or pair). This type is new in 0_4.
306 */
307typedef enum {
308 KM_PURPOSE_ENCRYPT = 0,
309 KM_PURPOSE_DECRYPT = 1,
310 KM_PURPOSE_SIGN = 2,
311 KM_PURPOSE_VERIFY = 3,
312} keymaster_purpose_t;
313
314typedef struct {
315 const uint8_t* data;
316 size_t data_length;
317} keymaster_blob_t;
318
319typedef struct {
320 keymaster_tag_t tag;
321 union {
322 uint32_t enumerated; /* KM_ENUM and KM_ENUM_REP */
323 bool boolean; /* KM_BOOL */
324 uint32_t integer; /* KM_INT and KM_INT_REP */
325 uint64_t long_integer; /* KM_LONG */
326 uint64_t date_time; /* KM_DATE */
327 keymaster_blob_t blob; /* KM_BIGNUM and KM_BYTES*/
328 };
329} keymaster_key_param_t;
330
331typedef struct {
332 keymaster_key_param_t* params; /* may be NULL if length == 0 */
333 size_t length;
334} keymaster_key_param_set_t;
335
336/**
337 * Parameters that define a key's characteristics, including authorized modes of usage and access
338 * control restrictions. The parameters are divided into two categories, those that are enforced by
339 * secure hardware, and those that are not. For a software-only keymaster implementation the
340 * enforced array must NULL. Hardware implementations must enforce everything in the enforced
341 * array.
342 */
343typedef struct {
344 keymaster_key_param_set_t hw_enforced;
345 keymaster_key_param_set_t sw_enforced;
346} keymaster_key_characteristics_t;
347
348typedef struct {
349 const uint8_t* key_material;
350 size_t key_material_size;
351} keymaster_key_blob_t;
352
353/**
354 * Formats for key import and export. At present, only asymmetric key import/export is supported.
355 * In the future this list will expand greatly to accommodate asymmetric key import/export.
356 */
357typedef enum {
358 KM_KEY_FORMAT_X509, /* for public key export, required */
359 KM_KEY_FORMAT_PKCS8, /* for asymmetric key pair import, required */
360 KM_KEY_FORMAT_PKCS12, /* for asymmetric key pair import, not required */
361} keymaster_key_format_t;
362
363/**
364 * The keymaster operation API consists of begin, update, finish and abort. This is the type of the
365 * handle used to tie the sequence of calls together. A 64-bit value is used because it's important
366 * that handles not be predictable. Implementations must use strong random numbers for handle
367 * values.
368 */
369typedef uint64_t keymaster_operation_handle_t;
370
371typedef enum {
372 KM_ERROR_OK = 0,
373 KM_ERROR_ROOT_OF_TRUST_ALREADY_SET = -1,
374 KM_ERROR_UNSUPPORTED_PURPOSE = -2,
375 KM_ERROR_INCOMPATIBLE_PURPOSE = -3,
376 KM_ERROR_UNSUPPORTED_ALGORITHM = -4,
377 KM_ERROR_INCOMPATIBLE_ALGORITHM = -5,
378 KM_ERROR_UNSUPPORTED_KEY_SIZE = -6,
379 KM_ERROR_UNSUPPORTED_BLOCK_MODE = -7,
380 KM_ERROR_INCOMPATIBLE_BLOCK_MODE = -8,
Shawn Willden6b424be2015-01-26 13:04:28 -0700381 KM_ERROR_UNSUPPORTED_MAC_LENGTH = -9,
Shawn Willden9d645a02014-06-12 13:48:46 -0600382 KM_ERROR_UNSUPPORTED_PADDING_MODE = -10,
383 KM_ERROR_INCOMPATIBLE_PADDING_MODE = -11,
384 KM_ERROR_UNSUPPORTED_DIGEST = -12,
385 KM_ERROR_INCOMPATIBLE_DIGEST = -13,
386 KM_ERROR_INVALID_EXPIRATION_TIME = -14,
387 KM_ERROR_INVALID_USER_ID = -15,
388 KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT = -16,
389 KM_ERROR_UNSUPPORTED_KEY_FORMAT = -17,
390 KM_ERROR_INCOMPATIBLE_KEY_FORMAT = -18,
391 KM_ERROR_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM = -19, /* For PKCS8 & PKCS12 */
392 KM_ERROR_UNSUPPORTED_KEY_VERIFICATION_ALGORITHM = -20, /* For PKCS8 & PKCS12 */
393 KM_ERROR_INVALID_INPUT_LENGTH = -21,
394 KM_ERROR_KEY_EXPORT_OPTIONS_INVALID = -22,
395 KM_ERROR_DELEGATION_NOT_ALLOWED = -23,
396 KM_ERROR_KEY_NOT_YET_VALID = -24,
397 KM_ERROR_KEY_EXPIRED = -25,
398 KM_ERROR_KEY_USER_NOT_AUTHENTICATED = -26,
399 KM_ERROR_OUTPUT_PARAMETER_NULL = -27,
400 KM_ERROR_INVALID_OPERATION_HANDLE = -28,
401 KM_ERROR_INSUFFICIENT_BUFFER_SPACE = -29,
402 KM_ERROR_VERIFICATION_FAILED = -30,
403 KM_ERROR_TOO_MANY_OPERATIONS = -31,
404 KM_ERROR_UNEXPECTED_NULL_POINTER = -32,
405 KM_ERROR_INVALID_KEY_BLOB = -33,
406 KM_ERROR_IMPORTED_KEY_NOT_ENCRYPTED = -34,
407 KM_ERROR_IMPORTED_KEY_DECRYPTION_FAILED = -35,
408 KM_ERROR_IMPORTED_KEY_NOT_SIGNED = -36,
409 KM_ERROR_IMPORTED_KEY_VERIFICATION_FAILED = -37,
410 KM_ERROR_INVALID_ARGUMENT = -38,
411 KM_ERROR_UNSUPPORTED_TAG = -39,
412 KM_ERROR_INVALID_TAG = -40,
413 KM_ERROR_MEMORY_ALLOCATION_FAILED = -41,
414 KM_ERROR_INVALID_RESCOPING = -42,
415 KM_ERROR_INVALID_DSA_PARAMS = -43,
416 KM_ERROR_IMPORT_PARAMETER_MISMATCH = -44,
417 KM_ERROR_SECURE_HW_ACCESS_DENIED = -45,
418 KM_ERROR_OPERATION_CANCELLED = -46,
419 KM_ERROR_CONCURRENT_ACCESS_CONFLICT = -47,
420 KM_ERROR_SECURE_HW_BUSY = -48,
421 KM_ERROR_SECURE_HW_COMMUNICATION_FAILED = -49,
422 KM_ERROR_UNSUPPORTED_EC_FIELD = -50,
423 KM_ERROR_UNIMPLEMENTED = -100,
424 KM_ERROR_VERSION_MISMATCH = -101,
425
426 /* Additional error codes may be added by implementations, but implementers should coordinate
427 * with Google to avoid code collision. */
428 KM_ERROR_UNKNOWN_ERROR = -1000,
429} keymaster_error_t;
430
431/**
432 * \deprecated Parameters needed to generate an RSA key.
433 */
434typedef struct {
435 uint32_t modulus_size; /* bits */
436 uint64_t public_exponent;
437} keymaster_rsa_keygen_params_t;
438
439/**
440 * \deprecated Parameters needed to generate a DSA key.
441 */
442typedef struct {
443 uint32_t key_size; /* bits */
444 uint32_t generator_len;
445 uint32_t prime_p_len;
446 uint32_t prime_q_len;
447 const uint8_t* generator;
448 const uint8_t* prime_p;
449 const uint8_t* prime_q;
450} keymaster_dsa_keygen_params_t;
451
452/**
453 * \deprecated Parameters needed to generate an EC key.
454 *
455 * Field size is the only parameter in version 4. The sizes correspond to these required curves:
456 *
457 * 192 = NIST P-192
458 * 224 = NIST P-224
459 * 256 = NIST P-256
460 * 384 = NIST P-384
461 * 521 = NIST P-521
462 *
463 * The parameters for these curves are available at: http://www.nsa.gov/ia/_files/nist-routines.pdf
464 * in Chapter 4.
465 */
466typedef struct { uint32_t field_size; /* bits */ } keymaster_ec_keygen_params_t;
467
468/**
469 * \deprecated Type of padding used for RSA operations.
470 */
471typedef enum {
472 PADDING_NONE,
473} keymaster_rsa_padding_t;
474
475/**
476 * \deprecated
477 */
478typedef struct { keymaster_digest_t digest_type; } keymaster_dsa_sign_params_t;
479
480/**
481 * \deprecated
482 */
483typedef struct { keymaster_digest_t digest_type; } keymaster_ec_sign_params_t;
484
485/**
486 *\deprecated
487 */
488typedef struct {
489 keymaster_digest_t digest_type;
490 keymaster_rsa_padding_t padding_type;
491} keymaster_rsa_sign_params_t;
492
493/* Convenience functions for manipulating keymaster tag types */
494
495static inline keymaster_tag_type_t keymaster_tag_get_type(keymaster_tag_t tag) {
496 return (keymaster_tag_type_t)(tag & (0xF << 28));
497}
498
499static inline uint32_t keymaster_tag_mask_type(keymaster_tag_t tag) {
500 return tag & 0x0FFFFFFF;
501}
502
503static inline bool keymaster_tag_type_repeatable(keymaster_tag_type_t type) {
504 switch (type) {
505 case KM_INT_REP:
506 case KM_ENUM_REP:
507 return true;
508 default:
509 return false;
510 }
511}
512
513static inline bool keymaster_tag_repeatable(keymaster_tag_t tag) {
514 return keymaster_tag_type_repeatable(keymaster_tag_get_type(tag));
515}
516
517/* Convenience functions for manipulating keymaster_key_param_t structs */
518
519inline keymaster_key_param_t keymaster_param_enum(keymaster_tag_t tag, uint32_t value) {
520 // assert(keymaster_tag_get_type(tag) == KM_ENUM || keymaster_tag_get_type(tag) == KM_ENUM_REP);
521 keymaster_key_param_t param;
522 memset(&param, 0, sizeof(param));
523 param.tag = tag;
524 param.enumerated = value;
525 return param;
526}
527
528inline keymaster_key_param_t keymaster_param_int(keymaster_tag_t tag, uint32_t value) {
529 // assert(keymaster_tag_get_type(tag) == KM_INT || keymaster_tag_get_type(tag) == KM_INT_REP);
530 keymaster_key_param_t param;
531 memset(&param, 0, sizeof(param));
532 param.tag = tag;
533 param.integer = value;
534 return param;
535}
536
537inline keymaster_key_param_t keymaster_param_long(keymaster_tag_t tag, uint64_t value) {
538 // assert(keymaster_tag_get_type(tag) == KM_LONG);
539 keymaster_key_param_t param;
540 memset(&param, 0, sizeof(param));
541 param.tag = tag;
542 param.long_integer = value;
543 return param;
544}
545
546inline keymaster_key_param_t keymaster_param_blob(keymaster_tag_t tag, const uint8_t* bytes,
547 size_t bytes_len) {
548 // assert(keymaster_tag_get_type(tag) == KM_BYTES || keymaster_tag_get_type(tag) == KM_BIGNUM);
549 keymaster_key_param_t param;
550 memset(&param, 0, sizeof(param));
551 param.tag = tag;
552 param.blob.data = (uint8_t*)bytes;
553 param.blob.data_length = bytes_len;
554 return param;
555}
556
557inline keymaster_key_param_t keymaster_param_bool(keymaster_tag_t tag) {
558 // assert(keymaster_tag_get_type(tag) == KM_BOOL);
559 keymaster_key_param_t param;
560 memset(&param, 0, sizeof(param));
561 param.tag = tag;
562 param.boolean = true;
563 return param;
564}
565
566inline keymaster_key_param_t keymaster_param_date(keymaster_tag_t tag, uint64_t value) {
567 // assert(keymaster_tag_get_type(tag) == KM_DATE);
568 keymaster_key_param_t param;
569 memset(&param, 0, sizeof(param));
570 param.tag = tag;
571 param.date_time = value;
572 return param;
573}
574
575inline void keymaster_free_param_values(keymaster_key_param_t* param, size_t param_count) {
576 while (param_count-- > 0) {
577 switch (keymaster_tag_get_type(param->tag)) {
578 case KM_BIGNUM:
579 case KM_BYTES:
580 free((void*)param->blob.data);
581 param->blob.data = NULL;
582 break;
583 default:
584 // NOP
585 break;
586 }
587 ++param;
588 }
589}
590
591inline void keymaster_free_param_set(keymaster_key_param_set_t* set) {
592 if (set) {
593 keymaster_free_param_values(set->params, set->length);
594 free(set->params);
595 set->params = NULL;
596 }
597}
598
599inline void keymaster_free_characteristics(keymaster_key_characteristics_t* characteristics) {
600 if (characteristics) {
601 keymaster_free_param_set(&characteristics->hw_enforced);
602 keymaster_free_param_set(&characteristics->sw_enforced);
603 }
604}
605
606#if defined(__cplusplus)
607} // extern "C"
608#endif // defined(__cplusplus)
609
610#endif // ANDROID_HARDWARE_KEYMASTER_DEFS_H