blob: 968beb9692120a2edd746fee08f850b1d55c0c18 [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/**
36 * The API level of this version of the header. The allows the implementing
37 * module to recognize which API level of the client it is dealing with in
38 * the case of pre-compiled binary clients.
39 */
Kenny Root2541a0a2013-08-19 09:51:35 -070040#define KEYMASTER_API_VERSION 2
Kenny Root9271d042012-03-13 09:22:21 -070041
Kenny Root3c338f42012-03-26 13:47:48 -070042/**
43 * Flags for keymaster_device::flags
44 */
45enum {
46 /*
47 * Indicates this keymaster implementation does not have hardware that
48 * keeps private keys out of user space.
49 *
50 * This should not be implemented on anything other than the default
51 * implementation.
52 */
53 KEYMASTER_SOFTWARE_ONLY = 0x00000001,
54};
55
Kenny Root60d0e5f2012-02-15 10:54:24 -080056struct keystore_module {
Kenny Root9271d042012-03-13 09:22:21 -070057 hw_module_t common;
Kenny Root60d0e5f2012-02-15 10:54:24 -080058};
59
60/**
Kenny Root9271d042012-03-13 09:22:21 -070061 * Asymmetric key pair types.
Kenny Root60d0e5f2012-02-15 10:54:24 -080062 */
63typedef enum {
Kenny Root9271d042012-03-13 09:22:21 -070064 TYPE_RSA = 1,
Kenny Root2541a0a2013-08-19 09:51:35 -070065 TYPE_DSA = 2,
66 TYPE_EC = 3,
Kenny Root9271d042012-03-13 09:22:21 -070067} keymaster_keypair_t;
68
69/**
70 * Parameters needed to generate an RSA key.
71 */
72typedef struct {
73 uint32_t modulus_size;
74 uint64_t public_exponent;
75} keymaster_rsa_keygen_params_t;
76
77/**
Kenny Root2541a0a2013-08-19 09:51:35 -070078 * Parameters needed to generate a DSA key.
79 */
80typedef struct {
81 uint32_t key_size;
82 uint32_t generator_len;
83 uint32_t prime_p_len;
84 uint32_t prime_q_len;
85 const uint8_t* generator;
86 const uint8_t* prime_p;
87 const uint8_t* prime_q;
88} keymaster_dsa_keygen_params_t;
89
90/**
91 * Parameters needed to generate an EC key.
92 *
93 * Field size is the only parameter in version 2. The sizes correspond to these required curves:
94 *
95 * 192 = NIST P-192
96 * 224 = NIST P-224
97 * 256 = NIST P-256
98 * 384 = NIST P-384
99 * 521 = NIST P-521
100 *
101 * The parameters for these curves are available at: http://www.nsa.gov/ia/_files/nist-routines.pdf
102 * in Chapter 4.
103 */
104typedef struct {
105 uint32_t field_size;
106} keymaster_ec_keygen_params_t;
107
108/**
109 * Digest type.
Kenny Root9271d042012-03-13 09:22:21 -0700110 */
111typedef enum {
112 DIGEST_NONE,
Kenny Root2541a0a2013-08-19 09:51:35 -0700113} keymaster_digest_t;
Kenny Root9271d042012-03-13 09:22:21 -0700114
115/**
116 * Type of padding used for RSA operations.
117 */
118typedef enum {
119 PADDING_NONE,
120} keymaster_rsa_padding_t;
121
Kenny Root2541a0a2013-08-19 09:51:35 -0700122
Kenny Root9271d042012-03-13 09:22:21 -0700123typedef struct {
Kenny Root2541a0a2013-08-19 09:51:35 -0700124 keymaster_digest_t digest_type;
125} keymaster_dsa_sign_params_t;
126
127typedef struct {
128 keymaster_digest_t digest_type;
129} keymaster_ec_sign_params_t;
130
131typedef struct {
132 keymaster_digest_t digest_type;
Kenny Root9271d042012-03-13 09:22:21 -0700133 keymaster_rsa_padding_t padding_type;
134} keymaster_rsa_sign_params_t;
Kenny Root60d0e5f2012-02-15 10:54:24 -0800135
136/**
137 * The parameters that can be set for a given keymaster implementation.
138 */
139struct keymaster_device {
140 struct hw_device_t common;
141
Kenny Root9271d042012-03-13 09:22:21 -0700142 uint32_t client_version;
143
Kenny Root3c338f42012-03-26 13:47:48 -0700144 /**
145 * See flags defined for keymaster_device::flags above.
146 */
147 uint32_t flags;
148
Kenny Root60d0e5f2012-02-15 10:54:24 -0800149 void* context;
150
151 /**
152 * Generates a public and private key. The key-blob returned is opaque
Kenny Root9271d042012-03-13 09:22:21 -0700153 * and must subsequently provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800154 *
155 * Returns: 0 on success or an error code less than 0.
156 */
Kenny Root9271d042012-03-13 09:22:21 -0700157 int (*generate_keypair)(const struct keymaster_device* dev,
158 const keymaster_keypair_t key_type, const void* key_params,
159 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800160
161 /**
Kenny Root9271d042012-03-13 09:22:21 -0700162 * Imports a public and private key pair. The imported keys will be in
163 * PKCS#8 format with DER encoding (Java standard). The key-blob
164 * returned is opaque and will be subsequently provided for signing
165 * and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800166 *
167 * Returns: 0 on success or an error code less than 0.
168 */
169 int (*import_keypair)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700170 const uint8_t* key, const size_t key_length,
171 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800172
173 /**
Kenny Root9271d042012-03-13 09:22:21 -0700174 * Gets the public key part of a key pair. The public key must be in
175 * X.509 format (Java standard) encoded byte array.
176 *
177 * Returns: 0 on success or an error code less than 0.
178 * On error, x509_data should not be allocated.
179 */
180 int (*get_keypair_public)(const struct keymaster_device* dev,
181 const uint8_t* key_blob, const size_t key_blob_length,
182 uint8_t** x509_data, size_t* x509_data_length);
183
184 /**
185 * Deletes the key pair associated with the key blob.
Kenny Root8ae65e72012-03-23 16:17:28 -0700186 *
187 * This function is optional and should be set to NULL if it is not
188 * implemented.
189 *
190 * Returns 0 on success or an error code less than 0.
Kenny Root9271d042012-03-13 09:22:21 -0700191 */
192 int (*delete_keypair)(const struct keymaster_device* dev,
193 const uint8_t* key_blob, const size_t key_blob_length);
194
195 /**
Kenny Root8ae65e72012-03-23 16:17:28 -0700196 * Deletes all keys in the hardware keystore. Used when keystore is
197 * reset completely.
198 *
199 * This function is optional and should be set to NULL if it is not
200 * implemented.
201 *
202 * Returns 0 on success or an error code less than 0.
203 */
204 int (*delete_all)(const struct keymaster_device* dev);
205
206 /**
Kenny Root9271d042012-03-13 09:22:21 -0700207 * Signs data using a key-blob generated before. This can use either
208 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800209 *
210 * Returns: 0 on success or an error code less than 0.
211 */
212 int (*sign_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700213 const void* signing_params,
214 const uint8_t* key_blob, const size_t key_blob_length,
215 const uint8_t* data, const size_t data_length,
216 uint8_t** signed_data, size_t* signed_data_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800217
218 /**
Kenny Root9271d042012-03-13 09:22:21 -0700219 * Verifies data signed with a key-blob. This can use either
220 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800221 *
222 * Returns: 0 on successful verification or an error code less than 0.
223 */
224 int (*verify_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700225 const void* signing_params,
226 const uint8_t* key_blob, const size_t key_blob_length,
227 const uint8_t* signed_data, const size_t signed_data_length,
228 const uint8_t* signature, const size_t signature_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800229};
230typedef struct keymaster_device keymaster_device_t;
231
Kenny Root9271d042012-03-13 09:22:21 -0700232
233/* Convenience API for opening and closing keymaster devices */
234
235static inline int keymaster_open(const struct hw_module_t* module,
236 keymaster_device_t** device)
237{
238 int rc = module->methods->open(module, KEYSTORE_KEYMASTER,
239 (struct hw_device_t**) device);
240
241 if (!rc) {
242 (*device)->client_version = KEYMASTER_API_VERSION;
243 }
244
245 return rc;
246}
247
248static inline int keymaster_close(keymaster_device_t* device)
249{
250 return device->common.close(&device->common);
251}
252
Kenny Root60d0e5f2012-02-15 10:54:24 -0800253__END_DECLS
254
255#endif // ANDROID_HARDWARE_KEYMASTER_H
256