blob: dae3b8e77678f89b7f4a1b9791123ac0ce139312 [file] [log] [blame]
Kenny Root60d0e5f2012-02-15 10:54:24 -08001/*
Shawn Willdenfd4b4d52015-02-24 09:17:38 -07002 * Copyright (C) 2015 The Android Open Source Project
Kenny Root60d0e5f2012-02-15 10:54:24 -08003 *
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
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070017#ifndef ANDROID_HARDWARE_KEYMASTER1_H
18#define ANDROID_HARDWARE_KEYMASTER1_H
Kenny Root60d0e5f2012-02-15 10:54:24 -080019
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070020#include <hardware/keymaster_common.h>
Shawn Willden9d645a02014-06-12 13:48:46 -060021#include <hardware/keymaster_defs.h>
Kenny Root60d0e5f2012-02-15 10:54:24 -080022
23__BEGIN_DECLS
24
25/**
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070026 * Keymaster1 device definition
Kenny Root60d0e5f2012-02-15 10:54:24 -080027 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070028struct keymaster1_device {
Stewart Miles84d35492014-05-01 09:03:27 -070029 /**
30 * Common methods of the keymaster device. This *must* be the first member of
31 * keymaster_device as users of this structure will cast a hw_device_t to
32 * keymaster_device pointer in contexts where it's known the hw_device_t references a
33 * keymaster_device.
34 */
Kenny Root60d0e5f2012-02-15 10:54:24 -080035 struct hw_device_t common;
36
Kenny Rootc124b232013-09-04 22:17:56 -070037 /**
38 * THIS IS DEPRECATED. Use the new "module_api_version" and "hal_api_version"
39 * fields in the keymaster_module initialization instead.
40 */
Kenny Root9271d042012-03-13 09:22:21 -070041 uint32_t client_version;
42
Kenny Root3c338f42012-03-26 13:47:48 -070043 /**
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070044 * See flags defined for keymaster0_devices::flags in keymaster_common.h
Kenny Root3c338f42012-03-26 13:47:48 -070045 */
46 uint32_t flags;
47
Kenny Root60d0e5f2012-02-15 10:54:24 -080048 void* context;
49
50 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060051 * \deprecated Generates a public and private key. The key-blob returned is opaque and must
52 * subsequently provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -080053 *
54 * Returns: 0 on success or an error code less than 0.
55 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070056 int (*generate_keypair)(const struct keymaster1_device* dev, const keymaster_keypair_t key_type,
Shawn Willden9d645a02014-06-12 13:48:46 -060057 const void* key_params, uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -080058
59 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060060 * \deprecated Imports a public and private key pair. The imported keys will be in PKCS#8 format
61 * with DER encoding (Java standard). The key-blob returned is opaque and will be subsequently
62 * provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -080063 *
64 * Returns: 0 on success or an error code less than 0.
65 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070066 int (*import_keypair)(const struct keymaster1_device* dev, const uint8_t* key,
Shawn Willden9d645a02014-06-12 13:48:46 -060067 const size_t key_length, uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -080068
69 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060070 * \deprecated Gets the public key part of a key pair. The public key must be in X.509 format
71 * (Java standard) encoded byte array.
Kenny Root9271d042012-03-13 09:22:21 -070072 *
Shawn Willden9d645a02014-06-12 13:48:46 -060073 * Returns: 0 on success or an error code less than 0. On error, x509_data
74 * should not be allocated.
Kenny Root9271d042012-03-13 09:22:21 -070075 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070076 int (*get_keypair_public)(const struct keymaster1_device* dev, const uint8_t* key_blob,
Shawn Willden9d645a02014-06-12 13:48:46 -060077 const size_t key_blob_length, uint8_t** x509_data,
78 size_t* x509_data_length);
Kenny Root9271d042012-03-13 09:22:21 -070079
80 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060081 * \deprecated Deletes the key pair associated with the key blob.
Kenny Root8ae65e72012-03-23 16:17:28 -070082 *
83 * This function is optional and should be set to NULL if it is not
84 * implemented.
85 *
86 * Returns 0 on success or an error code less than 0.
Kenny Root9271d042012-03-13 09:22:21 -070087 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -070088 int (*delete_keypair)(const struct keymaster1_device* dev, const uint8_t* key_blob,
Shawn Willden9d645a02014-06-12 13:48:46 -060089 const size_t key_blob_length);
Kenny Root9271d042012-03-13 09:22:21 -070090
91 /**
Shawn Willden9d645a02014-06-12 13:48:46 -060092 * \deprecated Deletes all keys in the hardware keystore. Used when keystore is reset
93 * completely.
Kenny Root8ae65e72012-03-23 16:17:28 -070094 *
95 * This function is optional and should be set to NULL if it is not
96 * implemented.
97 *
98 * Returns 0 on success or an error code less than 0.
99 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700100 int (*delete_all)(const struct keymaster1_device* dev);
Kenny Root8ae65e72012-03-23 16:17:28 -0700101
102 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600103 * \deprecated Signs data using a key-blob generated before. This can use either an asymmetric
104 * key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800105 *
106 * Returns: 0 on success or an error code less than 0.
107 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700108 int (*sign_data)(const struct keymaster1_device* dev, const void* signing_params,
Shawn Willden9d645a02014-06-12 13:48:46 -0600109 const uint8_t* key_blob, const size_t key_blob_length, const uint8_t* data,
110 const size_t data_length, uint8_t** signed_data, size_t* signed_data_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800111
112 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600113 * \deprecated Verifies data signed with a key-blob. This can use either an asymmetric key or a
114 * secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800115 *
116 * Returns: 0 on successful verification or an error code less than 0.
117 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700118 int (*verify_data)(const struct keymaster1_device* dev, const void* signing_params,
Shawn Willden9d645a02014-06-12 13:48:46 -0600119 const uint8_t* key_blob, const size_t key_blob_length,
120 const uint8_t* signed_data, const size_t signed_data_length,
121 const uint8_t* signature, const size_t signature_length);
122
123 /**
124 * Gets algorithms supported.
125 *
126 * \param[in] dev The keymaster device structure.
127 *
128 * \param[out] algorithms Array of algorithms supported. The caller takes ownership of the
129 * array and must free() it.
130 *
131 * \param[out] algorithms_length Length of \p algorithms.
132 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700133 keymaster_error_t (*get_supported_algorithms)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600134 keymaster_algorithm_t** algorithms,
135 size_t* algorithms_length);
136
137 /**
138 * Gets the block modes supported for the specified algorithm.
139 *
140 * \param[in] dev The keymaster device structure.
141 *
142 * \param[in] algorithm The algorithm for which supported modes will be returned.
143 *
144 * \param[out] modes Array of modes supported. The caller takes ownership of the array and must
145 * free() it.
146 *
147 * \param[out] modes_length Length of \p modes.
148 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700149 keymaster_error_t (*get_supported_block_modes)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600150 keymaster_algorithm_t algorithm,
151 keymaster_purpose_t purpose,
152 keymaster_block_mode_t** modes,
153 size_t* modes_length);
154
155 /**
156 * Gets the padding modes supported for the specified algorithm. Caller assumes ownership of
157 * the allocated array.
158 *
159 * \param[in] dev The keymaster device structure.
160 *
161 * \param[in] algorithm The algorithm for which supported padding modes will be returned.
162 *
163 * \param[out] modes Array of padding modes supported. The caller takes ownership of the array
164 * and must free() it.
165 *
166 * \param[out] modes_length Length of \p modes.
167 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700168 keymaster_error_t (*get_supported_padding_modes)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600169 keymaster_algorithm_t algorithm,
170 keymaster_purpose_t purpose,
171 keymaster_padding_t** modes,
172 size_t* modes_length);
173
174 /**
175 * Gets the digests supported for the specified algorithm. Caller assumes ownership of the
176 * allocated array.
177 *
178 * \param[in] dev The keymaster device structure.
179 *
180 * \param[in] algorithm The algorithm for which supported digests will be returned.
181 *
182 * \param[out] digests Array of digests supported. The caller takes ownership of the array and
183 * must free() it.
184 *
185 * \param[out] digests_length Length of \p digests.
186 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700187 keymaster_error_t (*get_supported_digests)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600188 keymaster_algorithm_t algorithm,
189 keymaster_purpose_t purpose,
190 keymaster_digest_t** digests,
191 size_t* digests_length);
192
193 /**
194 * Gets the key import formats supported for keys of the specified algorithm. Caller assumes
195 * ownership of the allocated array.
196 *
197 * \param[in] dev The keymaster device structure.
198 *
199 * \param[in] algorithm The algorithm for which supported formats will be returned.
200 *
201 * \param[out] formats Array of formats supported. The caller takes ownership of the array and
202 * must free() it.
203 *
204 * \param[out] formats_length Length of \p formats.
205 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700206 keymaster_error_t (*get_supported_import_formats)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600207 keymaster_algorithm_t algorithm,
208 keymaster_key_format_t** formats,
209 size_t* formats_length);
210
211 /**
212 * Gets the key export formats supported for keys of the specified algorithm. Caller assumes
213 * ownership of the allocated array.
214 *
215 * \param[in] dev The keymaster device structure.
216 *
217 * \param[in] algorithm The algorithm for which supported formats will be returned.
218 *
219 * \param[out] formats Array of formats supported. The caller takes ownership of the array and
220 * must free() it.
221 *
222 * \param[out] formats_length Length of \p formats.
223 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700224 keymaster_error_t (*get_supported_export_formats)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600225 keymaster_algorithm_t algorithm,
226 keymaster_key_format_t** formats,
227 size_t* formats_length);
228
229 /**
230 * Adds entropy to the RNG used by keymaster. Entropy added through this method is guaranteed
231 * not to be the only source of entropy used, and the mixing function is required to be secure,
232 * in the sense that if the RNG is seeded (from any source) with any data the attacker cannot
233 * predict (or control), then the RNG output is indistinguishable from random. Thus, if the
234 * entropy from any source is good, the output will be good.
235 *
236 * \param[in] dev The keymaster device structure.
237 *
238 * \param[in] data Random data to be mixed in.
239 *
240 * \param[in] data_length Length of \p data.
241 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700242 keymaster_error_t (*add_rng_entropy)(const struct keymaster1_device* dev, const uint8_t* data,
Shawn Willden9d645a02014-06-12 13:48:46 -0600243 size_t data_length);
244
245 /**
246 * Generates a key, or key pair, returning a key blob and/or a description of the key.
247 *
248 * Key generation parameters are defined as keymaster tag/value pairs, provided in \p params.
249 * See keymaster_tag_t for the full list. Some values that are always required for generation
250 * of useful keys are:
251 *
252 * - KM_TAG_ALGORITHM;
253 * - KM_TAG_PURPOSE;
254 * - KM_TAG_USER_ID or KM_TAG_ALL_USERS;
255 * - KM_TAG_USER_AUTH_ID or KM_TAG_NO_AUTH_REQUIRED;
256 * - KM_TAG_APPLICATION_ID or KM_TAG_ALL_APPLICATIONS; and
257 * - KM_TAG_ORIGINATION_EXPIRE_DATETIME
258 *
259 * KM_TAG_AUTH_TIMEOUT should generally be specified unless KM_TAG_NO_AUTH_REQUIRED is present,
260 * or the user will have to authenticate for every use.
261 *
262 * KM_TAG_BLOCK_MODE, KM_TAG_PADDING, KM_TAG_MAC_LENGTH and KM_TAG_DIGEST must be specified for
263 * algorithms that require them.
264 *
265 * The following tags will take default values if unspecified:
266 *
267 * - KM_TAG_KEY_SIZE defaults to a recommended key size for the specified algorithm.
268 *
269 * - KM_TAG_USAGE_EXPIRE_DATETIME defaults to the value of KM_TAG_ORIGINATION_EXPIRE_DATETIME.
270 *
271 * - KM_TAG_ACTIVE_DATETIME will default to the value of KM_TAG_CREATION_DATETIME
272 *
273 * - KM_TAG_ROOT_OF_TRUST will default to the current root of trust.
274 *
275 * - KM_TAG_{RSA|DSA|DH}_* will default to values appropriate for the specified key size.
276 *
277 * The following tags may not be specified; their values will be provided by the implementation.
278 *
279 * - KM_TAG_ORIGIN,
280 *
281 * - KM_TAG_ROLLBACK_RESISTANT,
282 *
283 * - KM_TAG_CREATION_DATETIME,
284 *
285 * \param[in] dev The keymaster device structure.
286 *
287 * \param[in] params Array of key generation parameters.
288 *
289 * \param[in] params_count Length of \p params.
290 *
291 * \param[out] key_blob returns the generated key. If \p key_blob is NULL, no key is generated,
292 * but the characteristics of the key that would be generated are returned. The caller assumes
293 * ownership key_blob->key_material and must free() it.
294 *
295 * \param[out] characteristics returns the characteristics of the key that was, or would be,
296 * generated, if non-NULL. The caller assumes ownership, and the object must be freed with
297 * keymaster_free_characteristics(). Note that KM_TAG_ROOT_OF_TRUST, KM_TAG_APPLICATION_ID and
298 * KM_TAG_APPLICATION_DATA are never returned.
299 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700300 keymaster_error_t (*generate_key)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600301 const keymaster_key_param_t* params, size_t params_count,
302 keymaster_key_blob_t* key_blob,
303 keymaster_key_characteristics_t** characteristics);
304
305 /**
306 * Returns the characteristics of the specified key, or NULL if the key_blob is invalid
307 * (implementations must fully validate the integrity of the key). client_id and app_data must
308 * be the ID and data provided when the key was generated or imported. Those values are not
309 * included in the returned characteristics. Caller assumes ownership of the allocated
310 * characteristics object, which must be deallocated with keymaster_free_characteristics().
311 *
312 * Note that KM_TAG_ROOT_OF_TRUST, KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA are never
313 * returned.
314 *
315 * \param[in] dev The keymaster device structure.
316 *
317 * \param[in] key_blob The key to retreive characteristics from.
318 *
319 * \param[in] client_id The client ID data, or NULL if none associated.
320 *
321 * \param[in] app_id The app data, or NULL if none associated.
322 *
323 * \param[out] characteristics The key characteristics.
324 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700325 keymaster_error_t (*get_key_characteristics)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600326 const keymaster_key_blob_t* key_blob,
327 const keymaster_blob_t* client_id,
328 const keymaster_blob_t* app_data,
329 keymaster_key_characteristics_t** characteristics);
330
331 /**
332 * Change a key's authorizations.
333 *
334 * Update the authorizations associated with key_blob to the list specified in new_params, which
335 * must contain the complete set of authorizations desired (hw_enforced and sw_enforced). Tags
336 * will be added, removed and/or updated only if the appropriate KM_TAG_RESCOPING_ADD and
337 * KM_TAG_RESCOPING_DEL tags exist in the key's authorizations, otherwise
338 * KM_ERROR_INVALID_RESCOPING will be returned and no changes will be made.
339 *
340 * \param[in] dev The keymaster device structure.
341 *
342 * \param[in] new_params The new authorization list to be associated with the key.
343 *
344 * \param[in] new_params_count The number of entries in \p new_params.
345 *
346 * \param[in] key_blob The key to update.
347 *
348 * \param[in] client_id The client ID associated with the key, or NULL if none is associated.
349 *
350 * \param[in] app_data The application data associated with the key, or NULL if none is
351 * associated.
352 *
353 * \param[out] rescoped_key_blob The key blob with the updated authorizations, if successful.
354 * The caller assumes ownership of rescoped_key_blob->key_material and must free() it.
355 *
356 * \param[out] characteristics If not null will contain the new key authorizations, divided into
357 * hw_enforced and sw_enforced lists. The caller takes ownership and must call
358 * keymaster_free_characteristics() to free.
359 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700360 keymaster_error_t (*rescope)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600361 const keymaster_key_param_t* new_params, size_t new_params_count,
362 const keymaster_key_blob_t* key_blob,
363 const keymaster_blob_t* client_id,
364 const keymaster_blob_t* app_data,
Shawn Willden611a6952015-01-26 16:19:49 -0700365 keymaster_key_blob_t* rescoped_key_blob,
Shawn Willden9d645a02014-06-12 13:48:46 -0600366 keymaster_key_characteristics_t** characteristics);
367
368 /**
369 * Imports a key, or key pair, returning a key blob and/or a description of the key.
370 *
371 * Most key import parameters are defined as keymaster tag/value pairs, provided in "params".
372 * See keymaster_tag_t for the full list. Some values that are always required for import of
373 * useful keys are:
374 *
375 * - KM_TAG_PURPOSE;
376 *
377 * - KM_TAG_USER_ID
378 *
379 * - KM_TAG_USER_AUTH_ID;
380 *
381 * - KM_TAG_APPLICATION_ID or KM_TAG_ALL_APPLICATIONS;
382 *
383 * - KM_TAG_PRIVKEY_EXPIRE_DATETIME.
384 *
385 * KM_TAG_AUTH_TIMEOUT should generally be specified. If unspecified, the user will have to
386 * authenticate for every use, unless KM_TAG_USER_AUTH_ID is set to
387 * KM_NO_AUTHENTICATION_REQUIRED.
388 *
389 * The following tags will take default values if unspecified:
390 *
391 * - KM_TAG_PUBKEY_EXPIRE_DATETIME will default to the value for KM_TAG_PRIVKEY_EXPIRE_DATETIME.
392 *
393 * - KM_TAG_ACTIVE_DATETIME will default to the value of KM_TAG_CREATION_DATETIME
394 *
395 * - KM_TAG_ROOT_OF_TRUST will default to the current root of trust.
396 *
397 * The following tags may not be specified; their values will be provided by the implementation.
398 *
399 * - KM_TAG_ORIGIN,
400 *
401 * - KM_TAG_ROLLBACK_RESISTANT,
402 *
403 * - KM_TAG_CREATION_DATETIME,
404 *
405 * \param[in] dev The keymaster device structure.
406 *
407 * \param[in] params Parameters defining the imported key.
408 *
409 * \param[in] params_count The number of entries in \p params.
410 *
411 * \param[in] key_format specifies the format of the key data in key_data.
412 *
413 * \param[out] key_blob Used to return the opaque key blob. Must be non-NULL. The caller
414 * assumes ownership of the contained key_material.
415 *
416 * \param[out] characteristics Used to return the characteristics of the imported key. May be
417 * NULL, in which case no characteristics will be returned. If non-NULL, the caller assumes
418 * ownership and must deallocate with keymaster_free_characteristics().
419 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700420 keymaster_error_t (*import_key)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600421 const keymaster_key_param_t* params, size_t params_count,
422 keymaster_key_format_t key_format, const uint8_t* key_data,
423 size_t key_data_length, keymaster_key_blob_t* key_blob,
424 keymaster_key_characteristics_t** characteristics);
425
426 /**
427 * Exports a public key, returning a byte array in the specified format.
428 *
429 * \param[in] dev The keymaster device structure.
430 *
431 * \param[in] export_format The format to be used for exporting the key.
432 *
433 * \param[in] key_to_export The key to export.
434 *
435 * \param[out] export_data The exported key material. The caller assumes ownership.
436 *
437 * \param[out] export_data_length The length of \p export_data.
438 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700439 keymaster_error_t (*export_key)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600440 keymaster_key_format_t export_format,
441 const keymaster_key_blob_t* key_to_export,
442 const keymaster_blob_t* client_id,
443 const keymaster_blob_t* app_data, uint8_t** export_data,
444 size_t* export_data_length);
445
446 /**
447 * Deletes the key, or key pair, associated with the key blob. After calling this function it
448 * will be impossible to use the key for any other operations (though rescoped versions may
449 * exist, and if so will be usable). May be applied to keys from foreign roots of trust (keys
450 * not usable under the current root of trust).
451 *
452 * This function is optional and should be set to NULL if it is not implemented.
453 *
454 * \param[in] dev The keymaster device structure.
455 *
456 * \param[in] key The key to be deleted.
457 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700458 keymaster_error_t (*delete_key)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600459 const keymaster_key_blob_t* key);
460
461 /**
462 * Deletes all keys in the hardware keystore. Used when keystore is reset completely. After
463 * calling this function it will be impossible to use any previously generated or imported key
464 * blobs for any operations.
465 *
466 * This function is optional and should be set to NULL if it is not implemented.
467 *
468 * \param[in] dev The keymaster device structure.
469 *
470 * Returns 0 on success or an error code less than 0.
471 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700472 int (*delete_all_keys)(const struct keymaster1_device* dev);
Shawn Willden9d645a02014-06-12 13:48:46 -0600473
474 /**
475 * Begins a cryptographic operation using the specified key. If all is well, begin() will
476 * return KM_ERROR_OK and create an operation handle which must be passed to subsequent calls to
477 * update(), finish() or abort().
478 *
479 * It is critical that each call to begin() be paired with a subsequent call to finish() or
480 * abort(), to allow the keymaster implementation to clean up any internal operation state.
481 * Failure to do this will leak internal state space or other internal resources and will
482 * eventually cause begin() to return KM_ERROR_TOO_MANY_OPERATIONS when it runs out of space for
483 * operations.
484 *
485 * \param[in] dev The keymaster device structure.
486 *
487 * \param[in] purpose The purpose of the operation, one of KM_PURPOSE_ENCRYPT,
488 * KM_PURPOSE_DECRYPT, KM_PURPOSE_SIGN or KM_PURPOSE_VERIFY. Note that for AEAD modes,
489 * encryption and decryption imply signing and verification, respectively.
490 *
491 * \param[in] key The key to be used for the operation. \p key must have a purpose compatible
492 * with \p purpose and all of its usage requirements must be satisfied, or begin() will return
493 * an appropriate error code.
494 *
495 * \param[in] params Additional parameters for the operation. This is typically used to provide
496 * client ID information, with tags KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA. If the
497 * client information associated with the key is not provided, begin() will fail and return
Shawn Willdene26e1362015-02-06 17:01:53 -0700498 * KM_ERROR_INVALID_KEY_BLOB. For operations that require a nonce or IV, this must contain a
Shawn Willden79d79342015-02-10 14:29:46 -0700499 * tag KM_TAG_NONCE. For AEAD operations KM_TAG_CHUNK_SIZE is specified here.
Shawn Willden9d645a02014-06-12 13:48:46 -0600500 *
501 * \param[in] params_count The number of entries in \p params.
502 *
503 * \param[out] out_params Array of output parameters. The caller takes ownership of the output
504 * parametes array and must free it. out_params may be set to NULL if no output parameters are
505 * expected. If NULL, and output paramaters are generated, begin() will return
506 * KM_ERROR_OUTPUT_PARAMETER_NULL.
507 *
508 * \param[out] out_params_count The length of out_params.
509 *
510 * \param[out] operation_handle The newly-created operation handle which must be passed to
511 * update(), finish() or abort().
512 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700513 keymaster_error_t (*begin)(const struct keymaster1_device* dev, keymaster_purpose_t purpose,
Shawn Willden9d645a02014-06-12 13:48:46 -0600514 const keymaster_key_blob_t* key, const keymaster_key_param_t* params,
515 size_t params_count, keymaster_key_param_t** out_params,
516 size_t* out_params_count,
517 keymaster_operation_handle_t* operation_handle);
518
519 /**
Shawn Willden9d645a02014-06-12 13:48:46 -0600520 * Provides data to, and possibly receives output from, an ongoing cryptographic operation begun
521 * with begin().
522 *
523 * If operation_handle is invalid, update() will return KM_ERROR_INVALID_OPERATION_HANDLE.
524 *
525 * Not all of the data provided in the data buffer may be consumed. update() will return the
526 * amount consumed in *data_consumed. The caller should provide the unconsumed data in a
527 * subsequent call.
528 *
529 * \param[in] dev The keymaster device structure.
530 *
531 * \param[in] operation_handle The operation handle returned by begin().
532 *
Shawn Willdene26e1362015-02-06 17:01:53 -0700533 * \param[in] params Additional parameters for the operation. For AEAD modes, this is used to
534 * specify KM_TAG_ADDITIONAL_DATA.
535 *
536 * \param[in] params_count Length of \p params.
537 *
Shawn Willden9d645a02014-06-12 13:48:46 -0600538 * \param[in] input Data to be processed, per the parameters established in the call to begin().
539 * Note that update() may or may not consume all of the data provided. See \p data_consumed.
540 *
541 * \param[in] input_length Length of \p input.
542 *
543 * \param[out] input_consumed Amount of data that was consumed by update(). If this is less
544 * than the amount provided, the caller should provide the remainder in a subsequent call to
545 * update().
546 *
547 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
548 * buffer. If output is NULL then NO input data is consumed and no output is produced, but
549 * *output_length is set to an estimate of the size that would have been produced by this
550 * update() and a subsequent finish().
551 *
552 * \param[out] output_length The length of the output buffer.
553 *
554 * Note that update() may not provide any output, in which case *output_length will be zero, and
555 * *output may be either NULL or zero-length (so the caller should always free() it).
556 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700557 keymaster_error_t (*update)(const struct keymaster1_device* dev,
Shawn Willdene26e1362015-02-06 17:01:53 -0700558 keymaster_operation_handle_t operation_handle,
559 const keymaster_key_param_t* params, size_t params_count,
560 const uint8_t* input, size_t input_length, size_t* input_consumed,
561 uint8_t** output, size_t* output_length);
Shawn Willden9d645a02014-06-12 13:48:46 -0600562
563 /**
564 * Finalizes a cryptographic operation begun with begin() and invalidates operation_handle
565 * (except in the insufficient buffer case, detailed below).
566 *
567 * \param[in] dev The keymaster device structure.
568 *
569 * \param[in] operation_handle The operation handle returned by begin(). This handle will be
570 * invalidated.
571 *
Shawn Willdene26e1362015-02-06 17:01:53 -0700572 * \param[in] params Additional parameters for the operation. For AEAD modes, this is used to
573 * specify KM_TAG_ADDITIONAL_DATA.
574 *
575 * \param[in] params_count Length of \p params.
576 *
Shawn Willden9d645a02014-06-12 13:48:46 -0600577 * \param[in] signature The signature to be verified if the purpose specified in the begin()
578 * call was KM_PURPOSE_VERIFY.
579 *
580 * \param[in] signature_length The length of \p signature.
581 *
582 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
583 * buffer.
584 *
585 * \param[out] output_length The length of the output buffer.
586 *
587 * If the operation being finished is a signature verification or an AEAD-mode decryption and
588 * verification fails then finish() will return KM_ERROR_VERIFICATION_FAILED.
589 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700590 keymaster_error_t (*finish)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600591 keymaster_operation_handle_t operation_handle,
Shawn Willdene26e1362015-02-06 17:01:53 -0700592 const keymaster_key_param_t* params, size_t params_count,
Shawn Willden9d645a02014-06-12 13:48:46 -0600593 const uint8_t* signature, size_t signature_length, uint8_t** output,
594 size_t* output_length);
595
596 /**
597 * Aborts a cryptographic operation begun with begin(), freeing all internal resources and
598 * invalidating operation_handle.
599 */
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700600 keymaster_error_t (*abort)(const struct keymaster1_device* dev,
Shawn Willden9d645a02014-06-12 13:48:46 -0600601 keymaster_operation_handle_t operation_handle);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800602};
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700603typedef struct keymaster1_device keymaster1_device_t;
Kenny Root60d0e5f2012-02-15 10:54:24 -0800604
Kenny Root9271d042012-03-13 09:22:21 -0700605/* Convenience API for opening and closing keymaster devices */
606
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700607static inline int keymaster1_open(const struct hw_module_t* module, keymaster1_device_t** device) {
Shawn Willden9d645a02014-06-12 13:48:46 -0600608 return module->methods->open(module, KEYSTORE_KEYMASTER, (struct hw_device_t**)device);
Kenny Root9271d042012-03-13 09:22:21 -0700609}
610
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700611static inline int keymaster1_close(keymaster1_device_t* device) {
Kenny Root9271d042012-03-13 09:22:21 -0700612 return device->common.close(&device->common);
613}
614
Kenny Root60d0e5f2012-02-15 10:54:24 -0800615__END_DECLS
616
Shawn Willdenfd4b4d52015-02-24 09:17:38 -0700617#endif // ANDROID_HARDWARE_KEYMASTER1_H