blob: bfb1dd53365cba46958136a33bbceec95ba0c2f6 [file] [log] [blame]
Kenny Rootb4d2e022013-09-04 13:56:03 -07001/*
2 * Copyright (C) 2012 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#include <errno.h>
17#include <string.h>
18#include <stdint.h>
19
20#include <keymaster/softkeymaster.h>
21
22#include <keystore/keystore.h>
23
24#include <hardware/hardware.h>
25#include <hardware/keymaster.h>
26
27#include <openssl/err.h>
28
Kenny Root26cfc082013-09-11 14:38:56 -070029#include <UniquePtr.h>
Kenny Rootb4d2e022013-09-04 13:56:03 -070030
31// For debugging
Shawn Willden1406b8a2014-06-12 11:39:48 -060032// #define LOG_NDEBUG 0
Kenny Rootb4d2e022013-09-04 13:56:03 -070033
34#define LOG_TAG "OpenSSLKeyMaster"
35#include <cutils/log.h>
36
37typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
38
39/* Close an opened OpenSSL instance */
Shawn Willden1406b8a2014-06-12 11:39:48 -060040static int openssl_close(hw_device_t* dev) {
Kenny Rootb4d2e022013-09-04 13:56:03 -070041 delete dev;
42 return 0;
43}
44
45/*
46 * Generic device handling
47 */
Shawn Willden1406b8a2014-06-12 11:39:48 -060048static int openssl_open(const hw_module_t* module, const char* name, hw_device_t** device) {
Kenny Rootb4d2e022013-09-04 13:56:03 -070049 if (strcmp(name, KEYSTORE_KEYMASTER) != 0)
50 return -EINVAL;
51
52 Unique_keymaster_device_t dev(new keymaster_device_t);
53 if (dev.get() == NULL)
54 return -ENOMEM;
55
56 dev->common.tag = HARDWARE_DEVICE_TAG;
57 dev->common.version = 1;
Shawn Willden1406b8a2014-06-12 11:39:48 -060058 dev->common.module = (struct hw_module_t*)module;
Kenny Rootb4d2e022013-09-04 13:56:03 -070059 dev->common.close = openssl_close;
60
61 dev->flags = KEYMASTER_SOFTWARE_ONLY;
62
63 dev->generate_keypair = openssl_generate_keypair;
64 dev->import_keypair = openssl_import_keypair;
65 dev->get_keypair_public = openssl_get_keypair_public;
66 dev->delete_keypair = NULL;
67 dev->delete_all = NULL;
68 dev->sign_data = openssl_sign_data;
69 dev->verify_data = openssl_verify_data;
70
71 ERR_load_crypto_strings();
72 ERR_load_BIO_strings();
73
74 *device = reinterpret_cast<hw_device_t*>(dev.release());
75
76 return 0;
77}
78
79static struct hw_module_methods_t keystore_module_methods = {
synergydev2d73d552013-10-18 18:38:40 -070080 .open = openssl_open,
Kenny Rootb4d2e022013-09-04 13:56:03 -070081};
82
Shawn Willden1406b8a2014-06-12 11:39:48 -060083struct keystore_module HAL_MODULE_INFO_SYM __attribute__((visibility("default"))) = {
synergydev2d73d552013-10-18 18:38:40 -070084 .common = {
85 .tag = HARDWARE_MODULE_TAG,
86 .module_api_version = KEYMASTER_MODULE_API_VERSION_0_2,
87 .hal_api_version = HARDWARE_HAL_API_VERSION,
88 .id = KEYSTORE_HARDWARE_MODULE_ID,
89 .name = "Keymaster OpenSSL HAL",
90 .author = "The Android Open Source Project",
91 .methods = &keystore_module_methods,
92 .dso = 0,
93 .reserved = {},
Kenny Rootb4d2e022013-09-04 13:56:03 -070094 },
95};