Neeraj Soni | 6b99d61 | 2019-07-09 11:16:15 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2014, 2017, 2019 The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
Neeraj Soni | 6b99d61 | 2019-07-09 11:16:15 +0530 | [diff] [blame] | 35 | #include "cutils/log.h" |
| 36 | #include "cutils/properties.h" |
| 37 | #include "cryptfs_hw.h" |
Paul Keith | 398b58f | 2019-09-22 01:09:47 +0300 | [diff] [blame] | 38 | #include <vendor/qti/hardware/cryptfshw/1.0/ICryptfsHw.h> |
Neeraj Soni | 6b99d61 | 2019-07-09 11:16:15 +0530 | [diff] [blame] | 39 | |
| 40 | using android::sp; |
| 41 | using vendor::qti::hardware::cryptfshw::V1_0::ICryptfsHw; |
| 42 | using ::android::hardware::Return; |
| 43 | using ::android::hardware::Void; |
| 44 | |
| 45 | #define QTI_ICE_STORAGE_UFS 1 |
| 46 | #define QTI_ICE_STORAGE_SDCC 2 |
| 47 | |
Neeraj Soni | 6b99d61 | 2019-07-09 11:16:15 +0530 | [diff] [blame] | 48 | int set_ice_param(int flag) |
| 49 | { |
| 50 | int rc = -1; |
| 51 | sp<ICryptfsHw> cryptfshwService = ICryptfsHw::getService(); |
| 52 | if (cryptfshwService.get() == nullptr) { |
| 53 | ALOGE("Failed to get Cryptfshw service"); |
| 54 | return rc; |
| 55 | } |
| 56 | rc = cryptfshwService->setIceParam(flag); |
| 57 | return rc; |
| 58 | } |
Neeraj Soni | 6b99d61 | 2019-07-09 11:16:15 +0530 | [diff] [blame] | 59 | |
| 60 | int set_hw_device_encryption_key(const char* passwd, const char* enc_mode) |
| 61 | { |
| 62 | int rc = -1; |
| 63 | sp<ICryptfsHw> cryptfshwService = ICryptfsHw::getService(); |
| 64 | if (cryptfshwService.get() == nullptr) { |
| 65 | ALOGE("Failed to get Cryptfshw service"); |
| 66 | return rc; |
| 67 | } |
| 68 | rc = cryptfshwService->setKey(passwd, enc_mode); |
| 69 | return rc; |
| 70 | } |
| 71 | |
| 72 | int update_hw_device_encryption_key(const char* oldpw, const char* newpw, const char* enc_mode) |
| 73 | { |
| 74 | int rc = -1; |
| 75 | sp<ICryptfsHw> cryptfshwService = ICryptfsHw::getService(); |
| 76 | if (cryptfshwService.get() == nullptr) { |
| 77 | ALOGE("Failed to get Cryptfshw service"); |
| 78 | return rc; |
| 79 | } |
| 80 | rc = cryptfshwService->updateKey(oldpw, newpw, enc_mode); |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | unsigned int is_hw_disk_encryption(const char* encryption_mode) |
| 85 | { |
| 86 | int ret = 0; |
| 87 | if(encryption_mode) { |
| 88 | if (!strcmp(encryption_mode, "aes-xts")) { |
| 89 | SLOGD("HW based disk encryption is enabled \n"); |
| 90 | ret = 1; |
| 91 | } |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | int is_ice_enabled(void) |
| 97 | { |
| 98 | char prop_storage[PATH_MAX]; |
| 99 | int storage_type = 0; |
| 100 | |
| 101 | if (property_get("ro.boot.bootdevice", prop_storage, "")) { |
| 102 | if (strstr(prop_storage, "ufs")) { |
| 103 | /* All UFS based devices has ICE in it. So we dont need |
| 104 | * to check if corresponding device exists or not |
| 105 | */ |
| 106 | storage_type = QTI_ICE_STORAGE_UFS; |
| 107 | } else if (strstr(prop_storage, "sdhc")) { |
| 108 | if (access("/dev/icesdcc", F_OK) != -1) |
| 109 | storage_type = QTI_ICE_STORAGE_SDCC; |
| 110 | } |
| 111 | } |
| 112 | return storage_type; |
| 113 | } |
| 114 | |
| 115 | int clear_hw_device_encryption_key() |
| 116 | { |
| 117 | int rc = -1; |
| 118 | sp<ICryptfsHw> cryptfshwService = ICryptfsHw::getService(); |
| 119 | if (cryptfshwService.get() == nullptr) { |
| 120 | ALOGE("Failed to get Cryptfshw service"); |
| 121 | return rc; |
| 122 | } |
| 123 | rc = cryptfshwService->clearKey(); |
| 124 | return rc; |
| 125 | } |
| 126 | |