blob: dcde30ee22c1746c9bddcfb80f5f2da846c59f04 [file] [log] [blame]
Shawn Willdencf30fe12015-12-21 16:29:10 -07001/*
2 * Copyright (C) 2015 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_KEYMASTER2_H
18#define ANDROID_HARDWARE_KEYMASTER2_H
19
20#include <hardware/keymaster_common.h>
21#include <hardware/keymaster_defs.h>
22
23__BEGIN_DECLS
24
25/**
26 * Keymaster2 device definition
27 */
28struct keymaster2_device {
29 /**
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 */
35 struct hw_device_t common;
36
37 void* context;
38
39 /**
40 * See flags defined for keymaster0_devices::flags in keymaster_common.h. Used only for
41 * backward compatibility; keymaster2 hardware devices must set this to zero.
42 */
43 uint32_t flags;
44
45 /**
46 * Adds entropy to the RNG used by keymaster. Entropy added through this method is guaranteed
47 * not to be the only source of entropy used, and the mixing function is required to be secure,
48 * in the sense that if the RNG is seeded (from any source) with any data the attacker cannot
49 * predict (or control), then the RNG output is indistinguishable from random. Thus, if the
50 * entropy from any source is good, the output will be good.
51 *
52 * \param[in] dev The keymaster device structure.
53 *
54 * \param[in] data Random data to be mixed in.
55 *
56 * \param[in] data_length Length of \p data.
57 */
58 keymaster_error_t (*add_rng_entropy)(const struct keymaster2_device* dev, const uint8_t* data,
59 size_t data_length);
60
61 /**
62 * Generates a key, or key pair, returning a key blob and/or a description of the key.
63 *
64 * Key generation parameters are defined as keymaster tag/value pairs, provided in \p params.
65 * See keymaster_tag_t for the full list. Some values that are always required for generation
66 * of useful keys are:
67 *
68 * - KM_TAG_ALGORITHM;
69 * - KM_TAG_PURPOSE; and
70 * - (KM_TAG_USER_SECURE_ID and KM_TAG_USER_AUTH_TYPE) or KM_TAG_NO_AUTH_REQUIRED.
71 *
72 * KM_TAG_AUTH_TIMEOUT should generally be specified unless KM_TAG_NO_AUTH_REQUIRED is present,
73 * or the user will have to authenticate for every use.
74 *
75 * KM_TAG_BLOCK_MODE, KM_TAG_PADDING, KM_TAG_MAC_LENGTH and KM_TAG_DIGEST must be specified for
76 * algorithms that require them.
77 *
78 * The following tags may not be specified; their values will be provided by the implementation.
79 *
80 * - KM_TAG_ORIGIN,
81 * - KM_TAG_ROLLBACK_RESISTANT,
82 * - KM_TAG_CREATION_DATETIME
83 *
84 * \param[in] dev The keymaster device structure.
85 *
86 * \param[in] params Array of key generation param
87 *
88 * \param[out] key_blob returns the generated key. \p key_blob must not be NULL. The caller
89 * assumes ownership key_blob->key_material and must free() it.
90 *
91 * \param[out] characteristics returns the characteristics of the key that was, generated, if
92 * non-NULL. If non-NULL, the caller assumes ownership and must deallocate with
93 * keymaster_free_characteristics(). Note that KM_TAG_ROOT_OF_TRUST, KM_TAG_APPLICATION_ID and
94 * KM_TAG_APPLICATION_DATA are never returned.
95 */
96 keymaster_error_t (*generate_key)(const struct keymaster2_device* dev,
97 const keymaster_key_param_set_t* params,
98 keymaster_key_blob_t* key_blob,
99 keymaster_key_characteristics_t* characteristics);
100
101 /**
102 * Returns the characteristics of the specified key, or KM_ERROR_INVALID_KEY_BLOB if the
103 * key_blob is invalid (implementations must fully validate the integrity of the key).
104 * client_id and app_data must be the ID and data provided when the key was generated or
105 * imported, or empty if KM_TAG_APPLICATION_ID and/or KM_TAG_APPLICATION_DATA were not provided
106 * during generation. Those values are not included in the returned characteristics. The
107 * caller assumes ownership of the allocated characteristics object, which must be deallocated
108 * with keymaster_free_characteristics().
109 *
110 * Note that KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA are never returned.
111 *
112 * \param[in] dev The keymaster device structure.
113 *
114 * \param[in] key_blob The key to retreive characteristics from.
115 *
116 * \param[in] client_id The client ID data, or NULL if none associated.
117 *
118 * \param[in] app_id The app data, or NULL if none associated.
119 *
120 * \param[out] characteristics The key characteristics. Must not be NULL. The caller assumes
121 * ownership of the contents and must deallocate with keymaster_free_characteristics().
122 */
123 keymaster_error_t (*get_key_characteristics)(const struct keymaster2_device* dev,
124 const keymaster_key_blob_t* key_blob,
125 const keymaster_blob_t* client_id,
126 const keymaster_blob_t* app_data,
127 keymaster_key_characteristics_t* characteristics);
128
129 /**
130 * Imports a key, or key pair, returning a key blob and/or a description of the key.
131 *
132 * Most key import parameters are defined as keymaster tag/value pairs, provided in "params".
133 * See keymaster_tag_t for the full list. Values that are always required for import of useful
134 * keys are:
135 *
136 * - KM_TAG_ALGORITHM;
137 * - KM_TAG_PURPOSE; and
138 * - (KM_TAG_USER_SECURE_ID and KM_TAG_USER_AUTH_TYPE) or KM_TAG_NO_AUTH_REQUIRED.
139 *
140 * KM_TAG_AUTH_TIMEOUT should generally be specified. If unspecified, the user will have to
141 * authenticate for every use.
142 *
143 * The following tags will take default values if unspecified:
144 *
145 * - KM_TAG_KEY_SIZE will default to the size of the key provided.
146 * - KM_TAG_RSA_PUBLIC_EXPONENT will default to the value in the key provided (for RSA keys)
147 *
148 * The following tags may not be specified; their values will be provided by the implementation.
149 *
150 * - KM_TAG_ORIGIN,
151 * - KM_TAG_ROLLBACK_RESISTANT,
152 * - KM_TAG_CREATION_DATETIME
153 *
154 * \param[in] dev The keymaster device structure.
155 *
156 * \param[in] params Parameters defining the imported key.
157 *
158 * \param[in] params_count The number of entries in \p params.
159 *
160 * \param[in] key_format specifies the format of the key data in key_data.
161 *
162 * \param[out] key_blob Used to return the opaque key blob. Must be non-NULL. The caller
163 * assumes ownership of the contained key_material.
164 *
165 * \param[out] characteristics Used to return the characteristics of the imported key. May be
166 * NULL, in which case no characteristics will be returned. If non-NULL, the caller assumes
167 * ownership of the contents and must deallocate with keymaster_free_characteristics(). Note
168 * that KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA are never returned.
169 */
170 keymaster_error_t (*import_key)(const struct keymaster2_device* dev,
171 const keymaster_key_param_set_t* params,
172 keymaster_key_format_t key_format,
173 const keymaster_blob_t* key_data,
174 keymaster_key_blob_t* key_blob,
175 keymaster_key_characteristics_t* characteristics);
176
177 /**
178 * Exports a public or symmetric key, returning a byte array in the specified format.
179 *
180 * Note that symmetric key export is allowed only if the key was created with KM_TAG_EXPORTABLE,
181 * and only if all of the requirements for key usage (e.g. authentication) are met.
182 *
183 * \param[in] dev The keymaster device structure.
184 *
185 * \param[in] export_format The format to be used for exporting the key.
186 *
187 * \param[in] key_to_export The key to export.
188 *
189 * \param[in] client_id Client ID blob, which must match the blob provided in
190 * KM_TAG_APPLICATION_ID during key generation (if any).
191 *
192 * \param[in] app_data Appliation data blob, which must match the blob provided in
193 * KM_TAG_APPLICATION_DATA during key generation (if any).
194 *
195 * \param[out] export_data The exported key material. The caller assumes ownership.
196 */
197 keymaster_error_t (*export_key)(const struct keymaster2_device* dev,
198 keymaster_key_format_t export_format,
199 const keymaster_key_blob_t* key_to_export,
200 const keymaster_blob_t* client_id,
201 const keymaster_blob_t* app_data,
202 keymaster_blob_t* export_data);
203
204 /**
205 * Derives a shared secret key from \p key, which must be an EC key, and the public key found in
206 * /p other_key_certificate, an X.509 certificate containing a compatible EC public key. The
207 * derived key's characteristics are described in \p new_key_params, which must include
208 * algorithm (KM_TAG_ALGORITHM), key size (KM_TAG_KEY_SIZE) and KDF (KM_TAG_KDF) as well as
209 * other desired key characteristics. The resulting key material is not returned directly, but
210 * instead a new keymaster key is created and the associated blob returned in \p key_blob. If
211 * \p characteristics is non-NULL, the new key's characteristics are placed there.
212 *
213 * \param[in] dev The keymaster device structure.
214 *
215 * \param[in] key The keymaster key to use for key agreement. This must be an EC key with the
216 * KM_PURPOSE_DERIVE_KEY purpose.
217 *
218 * \param[in] other_key_certificate An X.509 certificate or certificate fragment containing a
219 * SubjectPublicKey field containing an EC public key on the same curve as \p key.
220 *
221 * \param[in] new_key_params A set of parameters to define/describe the newly-derived symmetric
222 * key. The parameters will define how the key may be used. The set must include KM_TAG_KDF to
223 * specify how the raw agreed key bytes will be transformed to produce the key material.
224 *
225 * \param[out] key_blob The key blob containing the newly-derived key. The caller takes
226 * ownership of the returned blob.
227 *
228 * \param[out] characteristics. If non-null, will be used to return the characteristics of the
229 * new key blob, which will have KM_TAG_ORIGIN set to KM_ORIGIN_DERIVED. The caller takes
230 * ownership of the returned characteristics and must deallocate with
231 * keymaster_free_characteristics().
232 */
233 keymaster_error_t (*agree_key)(const struct keymaster2_device* dev,
234 const keymaster_key_blob_t* key,
235 const keymaster_blob_t* other_key_certificate,
236 const keymaster_key_param_set_t* new_key_params,
237 keymaster_key_blob_t* new_key_blob,
238 keymaster_key_characteristics_t* characteristics);
239
240 /**
241 * Generates a signed X.509 certificate chain attesting to the presence of \p key_to_attest in
242 * keymaster (TODO(swillden): Describe certificate contents in more detail). The certificate
243 * will contain an extension with OID 1.3.6.1.4.1.11129.2.1.17 and value defined in
244 * <TODO:swillden -- insert link here> which contains the key description.
245 *
246 * \param[in] dev The keymaster device structure.
247 *
248 * \param[in] key_to_attest The keymaster key for which the attestation certificate will be
249 * generated.
250 *
251 * \param[in] attest_params Parameters defining how to do the attestation. At present the only
252 * parameter is KM_TAG_ALGORITHM, which must be either KM_ALGORITHM_EC or KM_ALGORITHM_RSA.
253 * This selects which of the provisioned attestation keys will be used to sign the certificate.
254 *
255 * \param[out] cert_chain An array of DER-encoded X.509 certificates. The first will be the
256 * certificate for \p key_to_attest. The remaining entries will chain back to the root. The
257 * caller takes ownership and must deallocate with keymaster_free_cert_chain.
258 */
259 keymaster_error_t (*attest_key)(const struct keymaster2_device* dev,
260 const keymaster_key_blob_t* key_to_attest,
261 const keymaster_key_param_set_t* attest_params,
262 keymaster_cert_chain_t* cert_chain);
263
264 /**
265 * Upgrades an old key. Keys can become "old" in two ways: Keymaster can be upgraded to a new
266 * version, or the system can be updated to invalidate the OS version and/or patch level. In
267 * either case, attempts to use an old key will result in keymaster returning
268 * KM_ERROR_KEY_REQUIRES_UPGRADE. This method should then be called to upgrade the key.
269 *
270 * \param[in] dev The keymaster device structure.
271 *
272 * \param[in] key_to_upgrade The keymaster key to upgrade.
273 *
274 * \param[in] upgrade_params Parameters needed to complete the upgrade. In particular,
275 * KM_TAG_APPLICATION_ID and KM_TAG_APPLICATION_DATA will be required if they were defined for
276 * the key.
277 *
278 * \param[out] upgraded_key The upgraded key blob.
279 */
280 keymaster_error_t (*upgrade_key)(const struct keymaster2_device* dev,
281 const keymaster_key_blob_t* key_to_upgrade,
282 const keymaster_key_param_set_t* upgrade_params,
283 keymaster_key_blob_t* upgraded_key);
284
285 /**
286 * Deletes the key, or key pair, associated with the key blob. After calling this function it
287 * will be impossible to use the key for any other operations. May be applied to keys from
288 * foreign roots of trust (keys not usable under the current root of trust).
289 *
290 * This function is optional and should be set to NULL if it is not implemented.
291 *
292 * \param[in] dev The keymaster device structure.
293 *
294 * \param[in] key The key to be deleted.
295 */
296 keymaster_error_t (*delete_key)(const struct keymaster2_device* dev,
297 const keymaster_key_blob_t* key);
298
299 /**
300 * Deletes all keys in the hardware keystore. Used when keystore is reset completely. After
301 * calling this function it will be impossible to use any previously generated or imported key
302 * blobs for any operations.
303 *
304 * This function is optional and should be set to NULL if it is not implemented.
305 *
306 * \param[in] dev The keymaster device structure.
307 */
308 keymaster_error_t (*delete_all_keys)(const struct keymaster2_device* dev);
309
310 /**
311 * Begins a cryptographic operation using the specified key. If all is well, begin() will
312 * return KM_ERROR_OK and create an operation handle which must be passed to subsequent calls to
313 * update(), finish() or abort().
314 *
315 * It is critical that each call to begin() be paired with a subsequent call to finish() or
316 * abort(), to allow the keymaster implementation to clean up any internal operation state.
317 * Failure to do this may leak internal state space or other internal resources and may
318 * eventually cause begin() to return KM_ERROR_TOO_MANY_OPERATIONS when it runs out of space for
319 * operations. Any result other than KM_ERROR_OK from begin(), update() or finish() implicitly
320 * aborts the operation, in which case abort() need not be called (and will return
321 * KM_ERROR_INVALID_OPERATION_HANDLE if called).
322 *
323 * \param[in] dev The keymaster device structure.
324 *
325 * \param[in] purpose The purpose of the operation, one of KM_PURPOSE_ENCRYPT,
326 * KM_PURPOSE_DECRYPT, KM_PURPOSE_SIGN or KM_PURPOSE_VERIFY. Note that for AEAD modes,
327 * encryption and decryption imply signing and verification, respectively, but should be
328 * specified as KM_PURPOSE_ENCRYPT and KM_PURPOSE_DECRYPT.
329 *
330 * \param[in] key The key to be used for the operation. \p key must have a purpose compatible
331 * with \p purpose and all of its usage requirements must be satisfied, or begin() will return
332 * an appropriate error code.
333 *
334 * \param[in] in_params Additional parameters for the operation. This is typically used to
335 * provide authentication data, with KM_TAG_AUTH_TOKEN. If KM_TAG_APPLICATION_ID or
336 * KM_TAG_APPLICATION_DATA were provided during generation, they must be provided here, or the
337 * operation will fail with KM_ERROR_INVALID_KEY_BLOB. For operations that require a nonce or
338 * IV, on keys that were generated with KM_TAG_CALLER_NONCE, in_params may contain a tag
339 * KM_TAG_NONCE.
340 *
341 * \param[out] out_params Output parameters. Used to return additional data from the operation
342 * initialization, notably to return the IV or nonce from operations that generate an IV or
343 * nonce. The caller takes ownership of the output parameters array and must free it with
344 * keymaster_free_param_set(). out_params may be set to NULL if no output parameters are
345 * expected. If out_params is NULL, and output paramaters are generated, begin() will return
346 * KM_ERROR_OUTPUT_PARAMETER_NULL.
347 *
348 * \param[out] operation_handle The newly-created operation handle which must be passed to
349 * update(), finish() or abort(). If operation_handle is NULL, begin() will return
350 * KM_ERROR_OUTPUT_PARAMETER_NULL.
351 */
352 keymaster_error_t (*begin)(const struct keymaster2_device* dev, keymaster_purpose_t purpose,
353 const keymaster_key_blob_t* key,
354 const keymaster_key_param_set_t* in_params,
355 keymaster_key_param_set_t* out_params,
356 keymaster_operation_handle_t* operation_handle);
357
358 /**
359 * Provides data to, and possibly receives output from, an ongoing cryptographic operation begun
360 * with begin().
361 *
362 * If operation_handle is invalid, update() will return KM_ERROR_INVALID_OPERATION_HANDLE.
363 *
364 * update() may not consume all of the data provided in the data buffer. update() will return
365 * the amount consumed in *data_consumed. The caller should provide the unconsumed data in a
366 * subsequent call.
367 *
368 * \param[in] dev The keymaster device structure.
369 *
370 * \param[in] operation_handle The operation handle returned by begin().
371 *
372 * \param[in] in_params Additional parameters for the operation. For AEAD modes, this is used
373 * to specify KM_TAG_ADDITIONAL_DATA. Note that additional data may be provided in multiple
374 * calls to update(), but only until input data has been provided.
375 *
376 * \param[in] input Data to be processed, per the parameters established in the call to begin().
377 * Note that update() may or may not consume all of the data provided. See \p input_consumed.
378 *
379 * \param[out] input_consumed Amount of data that was consumed by update(). If this is less
380 * than the amount provided, the caller should provide the remainder in a subsequent call to
381 * update().
382 *
383 * \param[out] out_params Output parameters. Used to return additional data from the operation
384 * The caller takes ownership of the output parameters array and must free it with
385 * keymaster_free_param_set(). out_params may be set to NULL if no output parameters are
386 * expected. If out_params is NULL, and output paramaters are generated, begin() will return
387 * KM_ERROR_OUTPUT_PARAMETER_NULL.
388 *
389 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
390 * buffer. output must not be NULL.
391 *
392 * Note that update() may not provide any output, in which case output->data_length will be
393 * zero, and output->data may be either NULL or zero-length (so the caller should always free()
394 * it).
395 */
396 keymaster_error_t (*update)(const struct keymaster2_device* dev,
397 keymaster_operation_handle_t operation_handle,
398 const keymaster_key_param_set_t* in_params,
399 const keymaster_blob_t* input, size_t* input_consumed,
400 keymaster_key_param_set_t* out_params, keymaster_blob_t* output);
401
402 /**
403 * Finalizes a cryptographic operation begun with begin() and invalidates \p operation_handle.
404 *
405 * \param[in] dev The keymaster device structure.
406 *
407 * \param[in] operation_handle The operation handle returned by begin(). This handle will be
408 * invalidated.
409 *
410 * \param[in] in_params Additional parameters for the operation. For AEAD modes, this is used
411 * to specify KM_TAG_ADDITIONAL_DATA, but only if no input data was provided to update().
412 *
413 * \param[in] input Data to be processed, per the parameters established in the call to
414 * begin(). finish() must consume all provided data or return KM_ERROR_INVALID_INPUT_LENGTH.
415 *
416 * \param[in] signature The signature to be verified if the purpose specified in the begin()
417 * call was KM_PURPOSE_VERIFY.
418 *
419 * \param[out] output The output data, if any. The caller assumes ownership of the allocated
420 * buffer.
421 *
422 * If the operation being finished is a signature verification or an AEAD-mode decryption and
423 * verification fails then finish() will return KM_ERROR_VERIFICATION_FAILED.
424 */
425 keymaster_error_t (*finish)(const struct keymaster2_device* dev,
426 keymaster_operation_handle_t operation_handle,
427 const keymaster_key_param_set_t* in_params,
428 const keymaster_blob_t* input, const keymaster_blob_t* signature,
429 keymaster_key_param_set_t* out_params, keymaster_blob_t* output);
430
431 /**
432 * Aborts a cryptographic operation begun with begin(), freeing all internal resources and
433 * invalidating \p operation_handle.
434 */
435 keymaster_error_t (*abort)(const struct keymaster2_device* dev,
436 keymaster_operation_handle_t operation_handle);
437};
438typedef struct keymaster2_device keymaster2_device_t;
439
440/* Convenience API for opening and closing keymaster devices */
441
442static inline int keymaster2_open(const struct hw_module_t* module, keymaster2_device_t** device) {
443 return module->methods->open(module, KEYSTORE_KEYMASTER, (struct hw_device_t**)device);
444}
445
446static inline int keymaster2_close(keymaster2_device_t* device) {
447 return device->common.close(&device->common);
448}
449
450__END_DECLS
451
452#endif // ANDROID_HARDWARE_KEYMASTER2_H