blob: a6200a545d8c811ef296eadf4c213a94c359005f [file] [log] [blame]
Kenny Root60d0e5f2012-02-15 10:54:24 -08001/*
2 * Copyright (C) 2011 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_H
18#define ANDROID_HARDWARE_KEYMASTER_H
19
20#include <stdint.h>
21#include <sys/cdefs.h>
22#include <sys/types.h>
23
24#include <hardware/hardware.h>
Shawn Willden9d645a02014-06-12 13:48:46 -060025#include <hardware/keymaster_defs.h>
Kenny Root60d0e5f2012-02-15 10:54:24 -080026
27__BEGIN_DECLS
28
29/**
30 * The id of this module
31 */
32#define KEYSTORE_HARDWARE_MODULE_ID "keystore"
33
34#define KEYSTORE_KEYMASTER "keymaster"
35
Kenny Root9271d042012-03-13 09:22:21 -070036/**
Kenny Rootc124b232013-09-04 22:17:56 -070037 * Settings for "module_api_version" and "hal_api_version"
38 * fields in the keymaster_module initialization.
Kenny Root9271d042012-03-13 09:22:21 -070039 */
Shawn Willden9d645a02014-06-12 13:48:46 -060040#define KEYMASTER_HEADER_VERSION 4
Kenny Rootc124b232013-09-04 22:17:56 -070041
Shawn Willden9d645a02014-06-12 13:48:46 -060042#define KEYMASTER_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2)
43#define KEYMASTER_DEVICE_API_VERSION_0_2 \
44 HARDWARE_DEVICE_API_VERSION_2(0, 2, KEYMASTER_HEADER_VERSION)
Kenny Root9271d042012-03-13 09:22:21 -070045
Shawn Willden9d645a02014-06-12 13:48:46 -060046#define KEYMASTER_MODULE_API_VERSION_0_3 HARDWARE_MODULE_API_VERSION(0, 3)
47#define KEYMASTER_DEVICE_API_VERSION_0_3 \
48 HARDWARE_DEVICE_API_VERSION_2(0, 3, KEYMASTER_HEADER_VERSION)
Kenny Rootfea9aa62013-10-25 10:55:04 -070049
Shawn Willden9d645a02014-06-12 13:48:46 -060050#define KEYMASTER_MODULE_API_VERSION_0_4 HARDWARE_MODULE_API_VERSION(0, 4)
51#define KEYMASTER_DEVICE_API_VERSION_0_4 \
52 HARDWARE_DEVICE_API_VERSION_2(0, 4, KEYMASTER_HEADER_VERSION)
Kenny Root3c338f42012-03-26 13:47:48 -070053
Kenny Root60d0e5f2012-02-15 10:54:24 -080054struct keystore_module {
Stewart Miles84d35492014-05-01 09:03:27 -070055 /**
56 * Common methods of the keystore module. This *must* be the first member of
57 * keystore_module as users of this structure will cast a hw_module_t to
58 * keystore_module pointer in contexts where it's known the hw_module_t references a
59 * keystore_module.
60 */
Kenny Root9271d042012-03-13 09:22:21 -070061 hw_module_t common;
Kenny Root60d0e5f2012-02-15 10:54:24 -080062};
63
64/**
Kenny Root60d0e5f2012-02-15 10:54:24 -080065 * The parameters that can be set for a given keymaster implementation.
66 */
67struct keymaster_device {
Stewart Miles84d35492014-05-01 09:03:27 -070068 /**
69 * Common methods of the keymaster device. This *must* be the first member of
70 * keymaster_device as users of this structure will cast a hw_device_t to
71 * keymaster_device pointer in contexts where it's known the hw_device_t references a
72 * keymaster_device.
73 */
Kenny Root60d0e5f2012-02-15 10:54:24 -080074 struct hw_device_t common;
75
Kenny Rootc124b232013-09-04 22:17:56 -070076 /**
77 * THIS IS DEPRECATED. Use the new "module_api_version" and "hal_api_version"
78 * fields in the keymaster_module initialization instead.
79 */
Kenny Root9271d042012-03-13 09:22:21 -070080 uint32_t client_version;
81
Kenny Root3c338f42012-03-26 13:47:48 -070082 /**
83 * See flags defined for keymaster_device::flags above.
84 */
85 uint32_t flags;
86
Kenny Root60d0e5f2012-02-15 10:54:24 -080087 void* context;
88
89 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060090 * \deprecated Generates a public and private key. The key-blob returned is opaque and must
91 * subsequently provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -080092 *
93 * Returns: 0 on success or an error code less than 0.
94 */
Shawn Willden9d645a02014-06-12 13:48:46 -060095 int (*generate_keypair)(const struct keymaster_device* dev, const keymaster_keypair_t key_type,
96 const void* key_params, uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -080097
98 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060099 * \deprecated Imports a public and private key pair. The imported keys will be in PKCS#8 format
100 * with DER encoding (Java standard). The key-blob returned is opaque and will be subsequently
101 * provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800102 *
103 * Returns: 0 on success or an error code less than 0.
104 */
Shawn Willden9d645a02014-06-12 13:48:46 -0600105 int (*import_keypair)(const struct keymaster_device* dev, const uint8_t* key,
106 const size_t key_length, uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800107
108 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600109 * \deprecated Gets the public key part of a key pair. The public key must be in X.509 format
110 * (Java standard) encoded byte array.
Kenny Root9271d042012-03-13 09:22:21 -0700111 *
Shawn Willden9d645a02014-06-12 13:48:46 -0600112 * Returns: 0 on success or an error code less than 0. On error, x509_data
113 * should not be allocated.
Kenny Root9271d042012-03-13 09:22:21 -0700114 */
Shawn Willden9d645a02014-06-12 13:48:46 -0600115 int (*get_keypair_public)(const struct keymaster_device* dev, const uint8_t* key_blob,
116 const size_t key_blob_length, uint8_t** x509_data,
117 size_t* x509_data_length);
Kenny Root9271d042012-03-13 09:22:21 -0700118
119 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600120 * \deprecated Deletes the key pair associated with the key blob.
Kenny Root8ae65e72012-03-23 16:17:28 -0700121 *
122 * This function is optional and should be set to NULL if it is not
123 * implemented.
124 *
125 * Returns 0 on success or an error code less than 0.
Kenny Root9271d042012-03-13 09:22:21 -0700126 */
Shawn Willden9d645a02014-06-12 13:48:46 -0600127 int (*delete_keypair)(const struct keymaster_device* dev, const uint8_t* key_blob,
128 const size_t key_blob_length);
Kenny Root9271d042012-03-13 09:22:21 -0700129
130 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600131 * \deprecated Deletes all keys in the hardware keystore. Used when keystore is reset
132 * completely.
Kenny Root8ae65e72012-03-23 16:17:28 -0700133 *
134 * This function is optional and should be set to NULL if it is not
135 * implemented.
136 *
137 * Returns 0 on success or an error code less than 0.
138 */
139 int (*delete_all)(const struct keymaster_device* dev);
140
141 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600142 * \deprecated Signs data using a key-blob generated before. This can use either an asymmetric
143 * key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800144 *
145 * Returns: 0 on success or an error code less than 0.
146 */
Shawn Willden9d645a02014-06-12 13:48:46 -0600147 int (*sign_data)(const struct keymaster_device* dev, const void* signing_params,
148 const uint8_t* key_blob, const size_t key_blob_length, const uint8_t* data,
149 const size_t data_length, uint8_t** signed_data, size_t* signed_data_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800150
151 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600152 * \deprecated Verifies data signed with a key-blob. This can use either an asymmetric key or a
153 * secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800154 *
155 * Returns: 0 on successful verification or an error code less than 0.
156 */
Shawn Willden9d645a02014-06-12 13:48:46 -0600157 int (*verify_data)(const struct keymaster_device* dev, const void* signing_params,
158 const uint8_t* key_blob, const size_t key_blob_length,
159 const uint8_t* signed_data, const size_t signed_data_length,
160 const uint8_t* signature, const size_t signature_length);
161
162 /**
163 * Gets algorithms supported.
164 *
165 * \param[in] dev The keymaster device structure.
166 *
167 * \param[out] algorithms Array of algorithms supported. The caller takes ownership of the
168 * array and must free() it.
169 *
170 * \param[out] algorithms_length Length of \p algorithms.
171 */
172 keymaster_error_t (*get_supported_algorithms)(const struct keymaster_device* dev,
173 keymaster_algorithm_t** algorithms,
174 size_t* algorithms_length);
175
176 /**
177 * Gets the block modes supported for the specified algorithm.
178 *
179 * \param[in] dev The keymaster device structure.
180 *
181 * \param[in] algorithm The algorithm for which supported modes will be returned.
182 *
183 * \param[out] modes Array of modes supported. The caller takes ownership of the array and must
184 * free() it.
185 *
186 * \param[out] modes_length Length of \p modes.
187 */
188 keymaster_error_t (*get_supported_block_modes)(const struct keymaster_device* dev,
189 keymaster_algorithm_t algorithm,
190 keymaster_purpose_t purpose,
191 keymaster_block_mode_t** modes,
192 size_t* modes_length);
193
194 /**
195 * Gets the padding modes supported for the specified algorithm. Caller assumes ownership of
196 * the allocated array.
197 *
198 * \param[in] dev The keymaster device structure.
199 *
200 * \param[in] algorithm The algorithm for which supported padding modes will be returned.
201 *
202 * \param[out] modes Array of padding modes supported. The caller takes ownership of the array
203 * and must free() it.
204 *
205 * \param[out] modes_length Length of \p modes.
206 */
207 keymaster_error_t (*get_supported_padding_modes)(const struct keymaster_device* dev,
208 keymaster_algorithm_t algorithm,
209 keymaster_purpose_t purpose,
210 keymaster_padding_t** modes,
211 size_t* modes_length);
212
213 /**
214 * Gets the digests supported for the specified algorithm. Caller assumes ownership of the
215 * allocated array.
216 *
217 * \param[in] dev The keymaster device structure.
218 *
219 * \param[in] algorithm The algorithm for which supported digests will be returned.
220 *
221 * \param[out] digests Array of digests supported. The caller takes ownership of the array and
222 * must free() it.
223 *
224 * \param[out] digests_length Length of \p digests.
225 */
226 keymaster_error_t (*get_supported_digests)(const struct keymaster_device* dev,
227 keymaster_algorithm_t algorithm,
228 keymaster_purpose_t purpose,
229 keymaster_digest_t** digests,
230 size_t* digests_length);
231
232 /**
233 * Gets the key import formats supported for keys of the specified algorithm. Caller assumes
234 * ownership of the allocated array.
235 *
236 * \param[in] dev The keymaster device structure.
237 *
238 * \param[in] algorithm The algorithm for which supported formats will be returned.
239 *
240 * \param[out] formats Array of formats supported. The caller takes ownership of the array and
241 * must free() it.
242 *
243 * \param[out] formats_length Length of \p formats.
244 */
245 keymaster_error_t (*get_supported_import_formats)(const struct keymaster_device* dev,
246 keymaster_algorithm_t algorithm,
247 keymaster_key_format_t** formats,
248 size_t* formats_length);
249
250 /**
251 * Gets the key export formats supported for keys of the specified algorithm. Caller assumes
252 * ownership of the allocated array.
253 *
254 * \param[in] dev The keymaster device structure.
255 *
256 * \param[in] algorithm The algorithm for which supported formats will be returned.
257 *
258 * \param[out] formats Array of formats supported. The caller takes ownership of the array and
259 * must free() it.
260 *
261 * \param[out] formats_length Length of \p formats.
262 */
263 keymaster_error_t (*get_supported_export_formats)(const struct keymaster_device* dev,
264 keymaster_algorithm_t algorithm,
265 keymaster_key_format_t** formats,
266 size_t* formats_length);
267
268 /**
269 * Adds entropy to the RNG used by keymaster. Entropy added through this method is guaranteed
270 * not to be the only source of entropy used, and the mixing function is required to be secure,
271 * in the sense that if the RNG is seeded (from any source) with any data the attacker cannot
272 * predict (or control), then the RNG output is indistinguishable from random. Thus, if the
273 * entropy from any source is good, the output will be good.
274 *
275 * \param[in] dev The keymaster device structure.
276 *
277 * \param[in] data Random data to be mixed in.
278 *
279 * \param[in] data_length Length of \p data.
280 */
281 keymaster_error_t (*add_rng_entropy)(const struct keymaster_device* dev, uint8_t* data,
282 size_t data_length);
283
284 /**
285 * Generates a key, or key pair, returning a key blob and/or a description of the key.
286 *
287 * Key generation parameters are defined as keymaster tag/value pairs, provided in \p params.
288 * See keymaster_tag_t for the full list. Some values that are always required for generation
289 * of useful keys are:
290 *
291 * - KM_TAG_ALGORITHM;
292 * - KM_TAG_PURPOSE;
293 * - KM_TAG_USER_ID or KM_TAG_ALL_USERS;
294 * - KM_TAG_USER_AUTH_ID or KM_TAG_NO_AUTH_REQUIRED;
295 * - KM_TAG_APPLICATION_ID or KM_TAG_ALL_APPLICATIONS; and
296 * - KM_TAG_ORIGINATION_EXPIRE_DATETIME
297 *
298 * KM_TAG_AUTH_TIMEOUT should generally be specified unless KM_TAG_NO_AUTH_REQUIRED is present,
299 * or the user will have to authenticate for every use.
300 *
301 * KM_TAG_BLOCK_MODE, KM_TAG_PADDING, KM_TAG_MAC_LENGTH and KM_TAG_DIGEST must be specified for
302 * algorithms that require them.
303 *
304 * The following tags will take default values if unspecified:
305 *
306 * - KM_TAG_KEY_SIZE defaults to a recommended key size for the specified algorithm.
307 *
308 * - KM_TAG_USAGE_EXPIRE_DATETIME defaults to the value of KM_TAG_ORIGINATION_EXPIRE_DATETIME.
309 *
310 * - KM_TAG_ACTIVE_DATETIME will default to the value of KM_TAG_CREATION_DATETIME
311 *
312 * - KM_TAG_ROOT_OF_TRUST will default to the current root of trust.
313 *
314 * - KM_TAG_{RSA|DSA|DH}_* will default to values appropriate for the specified key size.
315 *
316 * The following tags may not be specified; their values will be provided by the implementation.
317 *
318 * - KM_TAG_ORIGIN,
319 *
320 * - KM_TAG_ROLLBACK_RESISTANT,
321 *
322 * - KM_TAG_CREATION_DATETIME,
323 *
324 * \param[in] dev The keymaster device structure.
325 *
326 * \param[in] params Array of key generation parameters.
327 *
328 * \param[in] params_count Length of \p params.
329 *
330 * \param[out] key_blob returns the generated key. If \p key_blob is NULL, no key is generated,
331 * but the characteristics of the key that would be generated are returned. The caller assumes
332 * ownership key_blob->key_material and must free() it.
333 *
334 * \param[out] characteristics returns the characteristics of the key that was, or would be,
335 * generated, if non-NULL. The caller assumes ownership, and the object must be freed with
336 * keymaster_free_characteristics(). Note that KM_TAG_ROOT_OF_TRUST, KM_TAG_APPLICATION_ID and
337 * KM_TAG_APPLICATION_DATA are never returned.
338 */
339 keymaster_error_t (*generate_key)(const struct keymaster_device* dev,
340 const keymaster_key_param_t* params, size_t params_count,
341 keymaster_key_blob_t* key_blob,
342 keymaster_key_characteristics_t** characteristics);
343
344 /**
345 * Returns the characteristics of the specified key, or NULL if the key_blob is invalid
346 * (implementations must fully validate the integrity of the key). client_id and app_data must
347 * be the ID and data provided when the key was generated or imported. Those values are not
348 * included in the returned characteristics. Caller assumes ownership of the allocated
349 * characteristics object, which must be deallocated with keymaster_free_characteristics().
350 *
351 * Note that KM_TAG_ROOT_OF_TRUST, KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA are never
352 * returned.
353 *
354 * \param[in] dev The keymaster device structure.
355 *
356 * \param[in] key_blob The key to retreive characteristics from.
357 *
358 * \param[in] client_id The client ID data, or NULL if none associated.
359 *
360 * \param[in] app_id The app data, or NULL if none associated.
361 *
362 * \param[out] characteristics The key characteristics.
363 */
364 keymaster_error_t (*get_key_characteristics)(const struct keymaster_device* dev,
365 const keymaster_key_blob_t* key_blob,
366 const keymaster_blob_t* client_id,
367 const keymaster_blob_t* app_data,
368 keymaster_key_characteristics_t** characteristics);
369
370 /**
371 * Change a key's authorizations.
372 *
373 * Update the authorizations associated with key_blob to the list specified in new_params, which
374 * must contain the complete set of authorizations desired (hw_enforced and sw_enforced). Tags
375 * will be added, removed and/or updated only if the appropriate KM_TAG_RESCOPING_ADD and
376 * KM_TAG_RESCOPING_DEL tags exist in the key's authorizations, otherwise
377 * KM_ERROR_INVALID_RESCOPING will be returned and no changes will be made.
378 *
379 * \param[in] dev The keymaster device structure.
380 *
381 * \param[in] new_params The new authorization list to be associated with the key.
382 *
383 * \param[in] new_params_count The number of entries in \p new_params.
384 *
385 * \param[in] key_blob The key to update.
386 *
387 * \param[in] client_id The client ID associated with the key, or NULL if none is associated.
388 *
389 * \param[in] app_data The application data associated with the key, or NULL if none is
390 * associated.
391 *
392 * \param[out] rescoped_key_blob The key blob with the updated authorizations, if successful.
393 * The caller assumes ownership of rescoped_key_blob->key_material and must free() it.
394 *
395 * \param[out] characteristics If not null will contain the new key authorizations, divided into
396 * hw_enforced and sw_enforced lists. The caller takes ownership and must call
397 * keymaster_free_characteristics() to free.
398 */
399 keymaster_error_t (*rescope)(const struct keymaster_device* dev,
400 const keymaster_key_param_t* new_params, size_t new_params_count,
401 const keymaster_key_blob_t* key_blob,
402 const keymaster_blob_t* client_id,
403 const keymaster_blob_t* app_data,
404 const keymaster_key_blob_t* rescoped_key_blob,
405 keymaster_key_characteristics_t** characteristics);
406
407 /**
408 * Imports a key, or key pair, returning a key blob and/or a description of the key.
409 *
410 * Most key import parameters are defined as keymaster tag/value pairs, provided in "params".
411 * See keymaster_tag_t for the full list. Some values that are always required for import of
412 * useful keys are:
413 *
414 * - KM_TAG_PURPOSE;
415 *
416 * - KM_TAG_USER_ID
417 *
418 * - KM_TAG_USER_AUTH_ID;
419 *
420 * - KM_TAG_APPLICATION_ID or KM_TAG_ALL_APPLICATIONS;
421 *
422 * - KM_TAG_PRIVKEY_EXPIRE_DATETIME.
423 *
424 * KM_TAG_AUTH_TIMEOUT should generally be specified. If unspecified, the user will have to
425 * authenticate for every use, unless KM_TAG_USER_AUTH_ID is set to
426 * KM_NO_AUTHENTICATION_REQUIRED.
427 *
428 * The following tags will take default values if unspecified:
429 *
430 * - KM_TAG_PUBKEY_EXPIRE_DATETIME will default to the value for KM_TAG_PRIVKEY_EXPIRE_DATETIME.
431 *
432 * - KM_TAG_ACTIVE_DATETIME will default to the value of KM_TAG_CREATION_DATETIME
433 *
434 * - KM_TAG_ROOT_OF_TRUST will default to the current root of trust.
435 *
436 * The following tags may not be specified; their values will be provided by the implementation.
437 *
438 * - KM_TAG_ORIGIN,
439 *
440 * - KM_TAG_ROLLBACK_RESISTANT,
441 *
442 * - KM_TAG_CREATION_DATETIME,
443 *
444 * \param[in] dev The keymaster device structure.
445 *
446 * \param[in] params Parameters defining the imported key.
447 *
448 * \param[in] params_count The number of entries in \p params.
449 *
450 * \param[in] key_format specifies the format of the key data in key_data.
451 *
452 * \param[out] key_blob Used to return the opaque key blob. Must be non-NULL. The caller
453 * assumes ownership of the contained key_material.
454 *
455 * \param[out] characteristics Used to return the characteristics of the imported key. May be
456 * NULL, in which case no characteristics will be returned. If non-NULL, the caller assumes
457 * ownership and must deallocate with keymaster_free_characteristics().
458 */
459 keymaster_error_t (*import_key)(const struct keymaster_device* dev,
460 const keymaster_key_param_t* params, size_t params_count,
461 keymaster_key_format_t key_format, const uint8_t* key_data,
462 size_t key_data_length, keymaster_key_blob_t* key_blob,
463 keymaster_key_characteristics_t** characteristics);
464
465 /**
466 * Exports a public key, returning a byte array in the specified format.
467 *
468 * \param[in] dev The keymaster device structure.
469 *
470 * \param[in] export_format The format to be used for exporting the key.
471 *
472 * \param[in] key_to_export The key to export.
473 *
474 * \param[out] export_data The exported key material. The caller assumes ownership.
475 *
476 * \param[out] export_data_length The length of \p export_data.
477 */
478 keymaster_error_t (*export_key)(const struct keymaster_device* dev,
479 keymaster_key_format_t export_format,
480 const keymaster_key_blob_t* key_to_export,
481 const keymaster_blob_t* client_id,
482 const keymaster_blob_t* app_data, uint8_t** export_data,
483 size_t* export_data_length);
484
485 /**
486 * Deletes the key, or key pair, associated with the key blob. After calling this function it
487 * will be impossible to use the key for any other operations (though rescoped versions may
488 * exist, and if so will be usable). May be applied to keys from foreign roots of trust (keys
489 * not usable under the current root of trust).
490 *
491 * This function is optional and should be set to NULL if it is not implemented.
492 *
493 * \param[in] dev The keymaster device structure.
494 *
495 * \param[in] key The key to be deleted.
496 */
497 keymaster_error_t (*delete_key)(const struct keymaster_device* dev,
498 const keymaster_key_blob_t* key);
499
500 /**
501 * Deletes all keys in the hardware keystore. Used when keystore is reset completely. After
502 * calling this function it will be impossible to use any previously generated or imported key
503 * blobs for any operations.
504 *
505 * This function is optional and should be set to NULL if it is not implemented.
506 *
507 * \param[in] dev The keymaster device structure.
508 *
509 * Returns 0 on success or an error code less than 0.
510 */
511 int (*delete_all_keys)(const struct keymaster_device* dev);
512
513 /**
514 * Begins a cryptographic operation using the specified key. If all is well, begin() will
515 * return KM_ERROR_OK and create an operation handle which must be passed to subsequent calls to
516 * update(), finish() or abort().
517 *
518 * It is critical that each call to begin() be paired with a subsequent call to finish() or
519 * abort(), to allow the keymaster implementation to clean up any internal operation state.
520 * Failure to do this will leak internal state space or other internal resources and will
521 * eventually cause begin() to return KM_ERROR_TOO_MANY_OPERATIONS when it runs out of space for
522 * operations.
523 *
524 * \param[in] dev The keymaster device structure.
525 *
526 * \param[in] purpose The purpose of the operation, one of KM_PURPOSE_ENCRYPT,
527 * KM_PURPOSE_DECRYPT, KM_PURPOSE_SIGN or KM_PURPOSE_VERIFY. Note that for AEAD modes,
528 * encryption and decryption imply signing and verification, respectively.
529 *
530 * \param[in] key The key to be used for the operation. \p key must have a purpose compatible
531 * with \p purpose and all of its usage requirements must be satisfied, or begin() will return
532 * an appropriate error code.
533 *
534 * \param[in] params Additional parameters for the operation. This is typically used to provide
535 * client ID information, with tags KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA. If the
536 * client information associated with the key is not provided, begin() will fail and return
537 * KM_ERROR_INVALID_KEY_BLOB. Less commonly, \params can be used to provide AEAD additional
538 * data and chunk size with KM_TAG_ADDITIONAL_DATA or KM_TAG_CHUNK_SIZE respectively.
539 *
540 * \param[in] params_count The number of entries in \p params.
541 *
542 * \param[out] out_params Array of output parameters. The caller takes ownership of the output
543 * parametes array and must free it. out_params may be set to NULL if no output parameters are
544 * expected. If NULL, and output paramaters are generated, begin() will return
545 * KM_ERROR_OUTPUT_PARAMETER_NULL.
546 *
547 * \param[out] out_params_count The length of out_params.
548 *
549 * \param[out] operation_handle The newly-created operation handle which must be passed to
550 * update(), finish() or abort().
551 */
552 keymaster_error_t (*begin)(const struct keymaster_device* dev, keymaster_purpose_t purpose,
553 const keymaster_key_blob_t* key, const keymaster_key_param_t* params,
554 size_t params_count, keymaster_key_param_t** out_params,
555 size_t* out_params_count,
556 keymaster_operation_handle_t* operation_handle);
557
558 /**
559 * Get an estimate of the output that will be generated by calling update() with the specified
560 * number of input bytes, followed by finish(). The estimate may not be exact, but is
561 * guaranteed not to be smaller than sum of the output lengths from update() and finish(). The
562 * estimate takes into account input data already provided.
563 *
564 * \param[in] input_length The number of additional input bytes to be processed.
565 *
566 * \param[out] output_estimate The length of the output that will be produced.
567 */
568 keymaster_error_t (*get_output_size)(size_t input_length, size_t* output_estimate);
569
570 /**
571 * Provides data to, and possibly receives output from, an ongoing cryptographic operation begun
572 * with begin().
573 *
574 * If operation_handle is invalid, update() will return KM_ERROR_INVALID_OPERATION_HANDLE.
575 *
576 * Not all of the data provided in the data buffer may be consumed. update() will return the
577 * amount consumed in *data_consumed. The caller should provide the unconsumed data in a
578 * subsequent call.
579 *
580 * \param[in] dev The keymaster device structure.
581 *
582 * \param[in] operation_handle The operation handle returned by begin().
583 *
584 * \param[in] input Data to be processed, per the parameters established in the call to begin().
585 * Note that update() may or may not consume all of the data provided. See \p data_consumed.
586 *
587 * \param[in] input_length Length of \p input.
588 *
589 * \param[out] input_consumed Amount of data that was consumed by update(). If this is less
590 * than the amount provided, the caller should provide the remainder in a subsequent call to
591 * update().
592 *
593 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
594 * buffer. If output is NULL then NO input data is consumed and no output is produced, but
595 * *output_length is set to an estimate of the size that would have been produced by this
596 * update() and a subsequent finish().
597 *
598 * \param[out] output_length The length of the output buffer.
599 *
600 * Note that update() may not provide any output, in which case *output_length will be zero, and
601 * *output may be either NULL or zero-length (so the caller should always free() it).
602 */
603 keymaster_error_t (*update)(const struct keymaster_device* dev,
604 keymaster_operation_handle_t operation_handle, const uint8_t* input,
605 size_t input_length, size_t* input_consumed, uint8_t** output,
606 size_t* output_length);
607
608 /**
609 * Finalizes a cryptographic operation begun with begin() and invalidates operation_handle
610 * (except in the insufficient buffer case, detailed below).
611 *
612 * \param[in] dev The keymaster device structure.
613 *
614 * \param[in] operation_handle The operation handle returned by begin(). This handle will be
615 * invalidated.
616 *
617 * \param[in] signature The signature to be verified if the purpose specified in the begin()
618 * call was KM_PURPOSE_VERIFY.
619 *
620 * \param[in] signature_length The length of \p signature.
621 *
622 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
623 * buffer.
624 *
625 * \param[out] output_length The length of the output buffer.
626 *
627 * If the operation being finished is a signature verification or an AEAD-mode decryption and
628 * verification fails then finish() will return KM_ERROR_VERIFICATION_FAILED.
629 */
630 keymaster_error_t (*finish)(const struct keymaster_device* dev,
631 keymaster_operation_handle_t operation_handle,
632 const uint8_t* signature, size_t signature_length, uint8_t** output,
633 size_t* output_length);
634
635 /**
636 * Aborts a cryptographic operation begun with begin(), freeing all internal resources and
637 * invalidating operation_handle.
638 */
639 keymaster_error_t (*abort)(const struct keymaster_device* dev,
640 keymaster_operation_handle_t operation_handle);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800641};
642typedef struct keymaster_device keymaster_device_t;
643
Kenny Root9271d042012-03-13 09:22:21 -0700644/* Convenience API for opening and closing keymaster devices */
645
Shawn Willden9d645a02014-06-12 13:48:46 -0600646static inline int keymaster_open(const struct hw_module_t* module, keymaster_device_t** device) {
647 return module->methods->open(module, KEYSTORE_KEYMASTER, (struct hw_device_t**)device);
Kenny Root9271d042012-03-13 09:22:21 -0700648}
649
Shawn Willden9d645a02014-06-12 13:48:46 -0600650static inline int keymaster_close(keymaster_device_t* device) {
Kenny Root9271d042012-03-13 09:22:21 -0700651 return device->common.close(&device->common);
652}
653
Kenny Root60d0e5f2012-02-15 10:54:24 -0800654__END_DECLS
655
656#endif // ANDROID_HARDWARE_KEYMASTER_H