blob: d4cf124e3de220f1a02e61c08da7f9298f7ff46a [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 */
40#define KEYMASTER_API_VERSION 1
41
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,
65} keymaster_keypair_t;
66
67/**
68 * Parameters needed to generate an RSA key.
69 */
70typedef struct {
71 uint32_t modulus_size;
72 uint64_t public_exponent;
73} keymaster_rsa_keygen_params_t;
74
75/**
76 * Digest type used for RSA operations.
77 */
78typedef enum {
79 DIGEST_NONE,
80} keymaster_rsa_digest_t;
81
82/**
83 * Type of padding used for RSA operations.
84 */
85typedef enum {
86 PADDING_NONE,
87} keymaster_rsa_padding_t;
88
89typedef struct {
90 keymaster_rsa_digest_t digest_type;
91 keymaster_rsa_padding_t padding_type;
92} keymaster_rsa_sign_params_t;
Kenny Root60d0e5f2012-02-15 10:54:24 -080093
94/**
95 * The parameters that can be set for a given keymaster implementation.
96 */
97struct keymaster_device {
98 struct hw_device_t common;
99
Kenny Root9271d042012-03-13 09:22:21 -0700100 uint32_t client_version;
101
Kenny Root3c338f42012-03-26 13:47:48 -0700102 /**
103 * See flags defined for keymaster_device::flags above.
104 */
105 uint32_t flags;
106
Kenny Root60d0e5f2012-02-15 10:54:24 -0800107 void* context;
108
109 /**
110 * Generates a public and private key. The key-blob returned is opaque
Kenny Root9271d042012-03-13 09:22:21 -0700111 * and must subsequently provided for signing and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800112 *
113 * Returns: 0 on success or an error code less than 0.
114 */
Kenny Root9271d042012-03-13 09:22:21 -0700115 int (*generate_keypair)(const struct keymaster_device* dev,
116 const keymaster_keypair_t key_type, const void* key_params,
117 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800118
119 /**
Kenny Root9271d042012-03-13 09:22:21 -0700120 * Imports a public and private key pair. The imported keys will be in
121 * PKCS#8 format with DER encoding (Java standard). The key-blob
122 * returned is opaque and will be subsequently provided for signing
123 * and verification.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800124 *
125 * Returns: 0 on success or an error code less than 0.
126 */
127 int (*import_keypair)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700128 const uint8_t* key, const size_t key_length,
129 uint8_t** key_blob, size_t* key_blob_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800130
131 /**
Kenny Root9271d042012-03-13 09:22:21 -0700132 * Gets the public key part of a key pair. The public key must be in
133 * X.509 format (Java standard) encoded byte array.
134 *
135 * Returns: 0 on success or an error code less than 0.
136 * On error, x509_data should not be allocated.
137 */
138 int (*get_keypair_public)(const struct keymaster_device* dev,
139 const uint8_t* key_blob, const size_t key_blob_length,
140 uint8_t** x509_data, size_t* x509_data_length);
141
142 /**
143 * Deletes the key pair associated with the key blob.
144 */
145 int (*delete_keypair)(const struct keymaster_device* dev,
146 const uint8_t* key_blob, const size_t key_blob_length);
147
148 /**
149 * Signs data using a key-blob generated before. This can use either
150 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800151 *
152 * Returns: 0 on success or an error code less than 0.
153 */
154 int (*sign_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700155 const void* signing_params,
156 const uint8_t* key_blob, const size_t key_blob_length,
157 const uint8_t* data, const size_t data_length,
158 uint8_t** signed_data, size_t* signed_data_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800159
160 /**
Kenny Root9271d042012-03-13 09:22:21 -0700161 * Verifies data signed with a key-blob. This can use either
162 * an asymmetric key or a secret key.
Kenny Root60d0e5f2012-02-15 10:54:24 -0800163 *
164 * Returns: 0 on successful verification or an error code less than 0.
165 */
166 int (*verify_data)(const struct keymaster_device* dev,
Kenny Root9271d042012-03-13 09:22:21 -0700167 const void* signing_params,
168 const uint8_t* key_blob, const size_t key_blob_length,
169 const uint8_t* signed_data, const size_t signed_data_length,
170 const uint8_t* signature, const size_t signature_length);
Kenny Root60d0e5f2012-02-15 10:54:24 -0800171};
172typedef struct keymaster_device keymaster_device_t;
173
Kenny Root9271d042012-03-13 09:22:21 -0700174
175/* Convenience API for opening and closing keymaster devices */
176
177static inline int keymaster_open(const struct hw_module_t* module,
178 keymaster_device_t** device)
179{
180 int rc = module->methods->open(module, KEYSTORE_KEYMASTER,
181 (struct hw_device_t**) device);
182
183 if (!rc) {
184 (*device)->client_version = KEYMASTER_API_VERSION;
185 }
186
187 return rc;
188}
189
190static inline int keymaster_close(keymaster_device_t* device)
191{
192 return device->common.close(&device->common);
193}
194
Kenny Root60d0e5f2012-02-15 10:54:24 -0800195__END_DECLS
196
197#endif // ANDROID_HARDWARE_KEYMASTER_H
198