blob: 12158bf0fea67cacaa33128a7d2d2e3d192a8c2e [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>
25
26__BEGIN_DECLS
27
28/**
29 * The id of this module
30 */
31#define KEYSTORE_HARDWARE_MODULE_ID "keystore"
32
33#define KEYSTORE_KEYMASTER "keymaster"
34
Kenny Root9271d042012-03-13 09:22:21 -070035/**
Kenny Rootc124b232013-09-04 22:17:56 -070036 * Settings for "module_api_version" and "hal_api_version"
37 * fields in the keymaster_module initialization.
Kenny Root9271d042012-03-13 09:22:21 -070038 */
Kenny Rootfea9aa62013-10-25 10:55:04 -070039#define KEYMASTER_HEADER_VERSION 3
Kenny Rootc124b232013-09-04 22:17:56 -070040
41#define KEYMASTER_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2)
42#define KEYMASTER_DEVICE_API_VERSION_0_2 HARDWARE_DEVICE_API_VERSION_2(0, 2, KEYMASTER_HEADER_VERSION)
Kenny Root9271d042012-03-13 09:22:21 -070043
Kenny Rootfea9aa62013-10-25 10:55:04 -070044#define KEYMASTER_MODULE_API_VERSION_0_3 HARDWARE_MODULE_API_VERSION(0, 3)
45#define KEYMASTER_DEVICE_API_VERSION_0_3 HARDWARE_DEVICE_API_VERSION_2(0, 3, KEYMASTER_HEADER_VERSION)
46
Kenny Root3c338f42012-03-26 13:47:48 -070047/**
48 * Flags for keymaster_device::flags
49 */
50enum {
51 /*
52 * Indicates this keymaster implementation does not have hardware that
53 * keeps private keys out of user space.
54 *
55 * This should not be implemented on anything other than the default
56 * implementation.
57 */
Kenny Rootfea9aa62013-10-25 10:55:04 -070058 KEYMASTER_SOFTWARE_ONLY = 1 << 0,
59
60 /*
61 * This indicates that the key blobs returned via all the primitives
62 * are sufficient to operate on their own without the trusted OS
63 * querying userspace to retrieve some other data. Key blobs of
64 * this type are normally returned encrypted with a
65 * Key Encryption Key (KEK).
66 *
67 * This is currently used by "vold" to know whether the whole disk
68 * encryption secret can be unwrapped without having some external
69 * service started up beforehand since the "/data" partition will
70 * be unavailable at that point.
71 */
72 KEYMASTER_BLOBS_ARE_STANDALONE = 1 << 1,
73
74 /*
75 * Indicates that the keymaster module supports DSA keys.
76 */
77 KEYMASTER_SUPPORTS_DSA = 1 << 2,
78
79 /*
80 * Indicates that the keymaster module supports EC keys.
81 */
82 KEYMASTER_SUPPORTS_EC = 1 << 3,
Kenny Root3c338f42012-03-26 13:47:48 -070083};
84
Kenny Root60d0e5f2012-02-15 10:54:24 -080085struct keystore_module {
Kenny Root9271d042012-03-13 09:22:21 -070086 hw_module_t common;
Kenny Root60d0e5f2012-02-15 10:54:24 -080087};
88
89/**
Kenny Root9271d042012-03-13 09:22:21 -070090 * Asymmetric key pair types.
Kenny Root60d0e5f2012-02-15 10:54:24 -080091 */
92typedef enum {
Kenny Root9271d042012-03-13 09:22:21 -070093 TYPE_RSA = 1,
Kenny Root2541a0a2013-08-19 09:51:35 -070094 TYPE_DSA = 2,
95 TYPE_EC = 3,
Kenny Root9271d042012-03-13 09:22:21 -070096} keymaster_keypair_t;
97
98/**
99 * Parameters needed to generate an RSA key.
100 */
101typedef struct {
102 uint32_t modulus_size;
103 uint64_t public_exponent;
104} keymaster_rsa_keygen_params_t;
105
106/**
Kenny Root2541a0a2013-08-19 09:51:35 -0700107 * Parameters needed to generate a DSA key.
108 */
109typedef struct {
110 uint32_t key_size;
111 uint32_t generator_len;
112 uint32_t prime_p_len;
113 uint32_t prime_q_len;
114 const uint8_t* generator;
115 const uint8_t* prime_p;
116 const uint8_t* prime_q;
117} keymaster_dsa_keygen_params_t;
118
119/**
120 * Parameters needed to generate an EC key.
121 *
122 * Field size is the only parameter in version 2. The sizes correspond to these required curves:
123 *
124 * 192 = NIST P-192
125 * 224 = NIST P-224
126 * 256 = NIST P-256
127 * 384 = NIST P-384
128 * 521 = NIST P-521
129 *
130 * The parameters for these curves are available at: http://www.nsa.gov/ia/_files/nist-routines.pdf
131 * in Chapter 4.
132 */
133typedef struct {
134 uint32_t field_size;
135} keymaster_ec_keygen_params_t;
136
137/**
138 * Digest type.
Kenny Root9271d042012-03-13 09:22:21 -0700139 */
140typedef enum {
141 DIGEST_NONE,
Kenny Root2541a0a2013-08-19 09:51:35 -0700142} keymaster_digest_t;
Kenny Root9271d042012-03-13 09:22:21 -0700143
144/**
145 * Type of padding used for RSA operations.
146 */
147typedef enum {
148 PADDING_NONE,
149} keymaster_rsa_padding_t;
150
Kenny Root2541a0a2013-08-19 09:51:35 -0700151
Kenny Root9271d042012-03-13 09:22:21 -0700152typedef struct {
Kenny Root2541a0a2013-08-19 09:51:35 -0700153 keymaster_digest_t digest_type;
154} keymaster_dsa_sign_params_t;
155
156typedef struct {
157 keymaster_digest_t digest_type;
158} keymaster_ec_sign_params_t;
159
160typedef struct {
161 keymaster_digest_t digest_type;
Kenny Root9271d042012-03-13 09:22:21 -0700162 keymaster_rsa_padding_t padding_type;
163} keymaster_rsa_sign_params_t;
Kenny Root60d0e5f2012-02-15 10:54:24 -0800164
165/**
166 * The parameters that can be set for a given keymaster implementation.
167 */
168struct keymaster_device {
169 struct hw_device_t common;
170
Kenny Rootc124b232013-09-04 22:17:56 -0700171 /**
172 * THIS IS DEPRECATED. Use the new "module_api_version" and "hal_api_version"
173 * fields in the keymaster_module initialization instead.
174 */
Kenny Root9271d042012-03-13 09:22:21 -0700175 uint32_t client_version;
176
Kenny Root3c338f42012-03-26 13:47:48 -0700177 /**
178 * See flags defined for keymaster_device::flags above.
179 */
180 uint32_t flags;
181
Kenny Root60d0e5f2012-02-15 10:54:24 -0800182 void* context;
183
184 /**
185 * Generates a public and private key. The key-blob returned is opaque
Kenny Root9271d042012-03-13 09:22:21 -0700186 * and must subsequently provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800187 *
188 * Returns: 0 on success or an error code less than 0.
189 */
Kenny Root9271d042012-03-13 09:22:21 -0700190 int (*generate_keypair)(const struct keymaster_device* dev,
191 const keymaster_keypair_t key_type, const void* key_params,
192 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800193
194 /**
Kenny Root9271d042012-03-13 09:22:21 -0700195 * Imports a public and private key pair. The imported keys will be in
196 * PKCS#8 format with DER encoding (Java standard). The key-blob
197 * returned is opaque and will be subsequently provided for signing
198 * and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800199 *
200 * Returns: 0 on success or an error code less than 0.
201 */
202 int (*import_keypair)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700203 const uint8_t* key, const size_t key_length,
204 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800205
206 /**
Kenny Root9271d042012-03-13 09:22:21 -0700207 * Gets the public key part of a key pair. The public key must be in
208 * X.509 format (Java standard) encoded byte array.
209 *
210 * Returns: 0 on success or an error code less than 0.
211 * On error, x509_data should not be allocated.
212 */
213 int (*get_keypair_public)(const struct keymaster_device* dev,
214 const uint8_t* key_blob, const size_t key_blob_length,
215 uint8_t** x509_data, size_t* x509_data_length);
216
217 /**
218 * Deletes the key pair associated with the key blob.
Kenny Root8ae65e72012-03-23 16:17:28 -0700219 *
220 * This function is optional and should be set to NULL if it is not
221 * implemented.
222 *
223 * Returns 0 on success or an error code less than 0.
Kenny Root9271d042012-03-13 09:22:21 -0700224 */
225 int (*delete_keypair)(const struct keymaster_device* dev,
226 const uint8_t* key_blob, const size_t key_blob_length);
227
228 /**
Kenny Root8ae65e72012-03-23 16:17:28 -0700229 * Deletes all keys in the hardware keystore. Used when keystore is
230 * reset completely.
231 *
232 * This function is optional and should be set to NULL if it is not
233 * implemented.
234 *
235 * Returns 0 on success or an error code less than 0.
236 */
237 int (*delete_all)(const struct keymaster_device* dev);
238
239 /**
Kenny Root9271d042012-03-13 09:22:21 -0700240 * Signs data using a key-blob generated before. This can use either
241 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800242 *
243 * Returns: 0 on success or an error code less than 0.
244 */
245 int (*sign_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700246 const void* signing_params,
247 const uint8_t* key_blob, const size_t key_blob_length,
248 const uint8_t* data, const size_t data_length,
249 uint8_t** signed_data, size_t* signed_data_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800250
251 /**
Kenny Root9271d042012-03-13 09:22:21 -0700252 * Verifies data signed with a key-blob. This can use either
253 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800254 *
255 * Returns: 0 on successful verification or an error code less than 0.
256 */
257 int (*verify_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700258 const void* signing_params,
259 const uint8_t* key_blob, const size_t key_blob_length,
260 const uint8_t* signed_data, const size_t signed_data_length,
261 const uint8_t* signature, const size_t signature_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800262};
263typedef struct keymaster_device keymaster_device_t;
264
Kenny Root9271d042012-03-13 09:22:21 -0700265
266/* Convenience API for opening and closing keymaster devices */
267
268static inline int keymaster_open(const struct hw_module_t* module,
269 keymaster_device_t** device)
270{
271 int rc = module->methods->open(module, KEYSTORE_KEYMASTER,
272 (struct hw_device_t**) device);
273
Kenny Root9271d042012-03-13 09:22:21 -0700274 return rc;
275}
276
277static inline int keymaster_close(keymaster_device_t* device)
278{
279 return device->common.close(&device->common);
280}
281
Kenny Root60d0e5f2012-02-15 10:54:24 -0800282__END_DECLS
283
284#endif // ANDROID_HARDWARE_KEYMASTER_H
285