Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 1 | /* |
| 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 Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 35 | /** |
| 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 Root | 3c338f4 | 2012-03-26 13:47:48 -0700 | [diff] [blame^] | 42 | /** |
| 43 | * Flags for keymaster_device::flags |
| 44 | */ |
| 45 | enum { |
| 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 56 | struct keystore_module { |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 57 | hw_module_t common; |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /** |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 61 | * Asymmetric key pair types. |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 62 | */ |
| 63 | typedef enum { |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 64 | TYPE_RSA = 1, |
| 65 | } keymaster_keypair_t; |
| 66 | |
| 67 | /** |
| 68 | * Parameters needed to generate an RSA key. |
| 69 | */ |
| 70 | typedef 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 | */ |
| 78 | typedef enum { |
| 79 | DIGEST_NONE, |
| 80 | } keymaster_rsa_digest_t; |
| 81 | |
| 82 | /** |
| 83 | * Type of padding used for RSA operations. |
| 84 | */ |
| 85 | typedef enum { |
| 86 | PADDING_NONE, |
| 87 | } keymaster_rsa_padding_t; |
| 88 | |
| 89 | typedef struct { |
| 90 | keymaster_rsa_digest_t digest_type; |
| 91 | keymaster_rsa_padding_t padding_type; |
| 92 | } keymaster_rsa_sign_params_t; |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * The parameters that can be set for a given keymaster implementation. |
| 96 | */ |
| 97 | struct keymaster_device { |
| 98 | struct hw_device_t common; |
| 99 | |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 100 | uint32_t client_version; |
| 101 | |
Kenny Root | 3c338f4 | 2012-03-26 13:47:48 -0700 | [diff] [blame^] | 102 | /** |
| 103 | * See flags defined for keymaster_device::flags above. |
| 104 | */ |
| 105 | uint32_t flags; |
| 106 | |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 107 | void* context; |
| 108 | |
| 109 | /** |
| 110 | * Generates a public and private key. The key-blob returned is opaque |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 111 | * and must subsequently provided for signing and verification. |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 112 | * |
| 113 | * Returns: 0 on success or an error code less than 0. |
| 114 | */ |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 115 | 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 120 | * 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 124 | * |
| 125 | * Returns: 0 on success or an error code less than 0. |
| 126 | */ |
| 127 | int (*import_keypair)(const struct keymaster_device* dev, |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 128 | const uint8_t* key, const size_t key_length, |
| 129 | uint8_t** key_blob, size_t* key_blob_length); |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 132 | * 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 151 | * |
| 152 | * Returns: 0 on success or an error code less than 0. |
| 153 | */ |
| 154 | int (*sign_data)(const struct keymaster_device* dev, |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 155 | 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 161 | * Verifies data signed with a key-blob. This can use either |
| 162 | * an asymmetric key or a secret key. |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 163 | * |
| 164 | * Returns: 0 on successful verification or an error code less than 0. |
| 165 | */ |
| 166 | int (*verify_data)(const struct keymaster_device* dev, |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 167 | 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 Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 171 | }; |
| 172 | typedef struct keymaster_device keymaster_device_t; |
| 173 | |
Kenny Root | 9271d04 | 2012-03-13 09:22:21 -0700 | [diff] [blame] | 174 | |
| 175 | /* Convenience API for opening and closing keymaster devices */ |
| 176 | |
| 177 | static 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 | |
| 190 | static inline int keymaster_close(keymaster_device_t* device) |
| 191 | { |
| 192 | return device->common.close(&device->common); |
| 193 | } |
| 194 | |
Kenny Root | 60d0e5f | 2012-02-15 10:54:24 -0800 | [diff] [blame] | 195 | __END_DECLS |
| 196 | |
| 197 | #endif // ANDROID_HARDWARE_KEYMASTER_H |
| 198 | |