2010-12-03 12:47:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
//
|
|
|
|
// This file contains the implementation of the dm-crypt volume metadata
|
|
|
|
// encryption method, which is deprecated. Devices that launched with Android
|
|
|
|
// 11 or higher use a different method instead. For details, see
|
|
|
|
// https://source.android.com/security/encryption/metadata#configuration-on-adoptable-storage
|
|
|
|
//
|
|
|
|
|
2018-05-02 05:36:45 +02:00
|
|
|
#define LOG_TAG "Cryptfs"
|
|
|
|
|
|
|
|
#include "cryptfs.h"
|
|
|
|
|
2020-02-07 21:45:20 +01:00
|
|
|
#include "CryptoType.h"
|
2018-10-30 23:59:24 +01:00
|
|
|
#include "Utils.h"
|
2018-05-02 05:36:45 +02:00
|
|
|
|
2019-01-25 19:47:55 +01:00
|
|
|
#include <android-base/parseint.h>
|
2018-05-02 05:39:03 +02:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <cutils/properties.h>
|
2019-05-13 22:02:54 +02:00
|
|
|
#include <libdm/dm.h>
|
2018-04-23 07:37:39 +02:00
|
|
|
#include <log/log.h>
|
2018-05-02 05:36:45 +02:00
|
|
|
|
2020-02-13 16:20:52 +01:00
|
|
|
#include <chrono>
|
2014-01-21 22:27:04 +01:00
|
|
|
|
2019-01-25 19:47:55 +01:00
|
|
|
using android::base::ParseUint;
|
2020-02-07 21:45:20 +01:00
|
|
|
using android::vold::CryptoType;
|
2020-02-07 20:49:09 +01:00
|
|
|
using android::vold::KeyBuffer;
|
2020-02-12 20:04:05 +01:00
|
|
|
using android::vold::KeyGeneration;
|
2019-05-13 22:02:54 +02:00
|
|
|
using namespace android::dm;
|
2021-11-09 01:45:58 +01:00
|
|
|
using namespace android::vold;
|
2018-10-30 23:59:24 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2020-02-03 21:22:03 +01:00
|
|
|
#define MAX_KEY_LEN 48
|
2011-05-19 02:20:07 +02:00
|
|
|
|
2012-09-30 02:07:41 +02:00
|
|
|
#define TABLE_LOAD_RETRIES 10
|
|
|
|
|
2020-02-07 21:45:20 +01:00
|
|
|
constexpr CryptoType aes_128_cbc = CryptoType()
|
|
|
|
.set_config_name("AES-128-CBC")
|
|
|
|
.set_kernel_name("aes-cbc-essiv:sha256")
|
|
|
|
.set_keysize(16);
|
|
|
|
|
|
|
|
constexpr CryptoType supported_crypto_types[] = {aes_128_cbc, android::vold::adiantum};
|
|
|
|
|
|
|
|
static_assert(validateSupportedCryptoTypes(MAX_KEY_LEN, supported_crypto_types,
|
|
|
|
array_length(supported_crypto_types)),
|
|
|
|
"We have a CryptoType with keysize > MAX_KEY_LEN or which was "
|
|
|
|
"incompletely constructed.");
|
|
|
|
|
|
|
|
static const CryptoType& get_crypto_type() {
|
|
|
|
// We only want to parse this read-only property once. But we need to wait
|
|
|
|
// until the system is initialized before we can read it. So we use a static
|
|
|
|
// scoped within this function to get it only once.
|
|
|
|
static CryptoType crypto_type =
|
|
|
|
lookup_crypto_algorithm(supported_crypto_types, array_length(supported_crypto_types),
|
|
|
|
aes_128_cbc, "ro.crypto.fde_algorithm");
|
|
|
|
return crypto_type;
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:04:05 +01:00
|
|
|
const KeyGeneration cryptfs_get_keygen() {
|
2020-02-07 21:51:56 +01:00
|
|
|
return KeyGeneration{get_crypto_type().get_keysize(), true, false};
|
2020-02-12 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
/* Convert a binary key of specified length into an ascii hex string equivalent,
|
|
|
|
* without the leading 0x and with null termination
|
2014-02-07 21:07:22 +01:00
|
|
|
*/
|
2021-11-09 01:45:58 +01:00
|
|
|
static void convert_key_to_hex_ascii(const KeyBuffer& key, char* key_ascii) {
|
|
|
|
unsigned int i, a;
|
|
|
|
unsigned char nibble;
|
2014-02-20 21:21:31 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
for (i = 0, a = 0; i < key.size(); i++, a += 2) {
|
|
|
|
/* For each byte, write out two ascii hex digits */
|
|
|
|
nibble = (key[i] >> 4) & 0xf;
|
|
|
|
key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
|
2014-02-20 21:21:31 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
nibble = key[i] & 0xf;
|
|
|
|
key_ascii[a + 1] = nibble + (nibble > 9 ? 0x37 : 0x30);
|
2013-06-05 01:37:52 +02:00
|
|
|
}
|
2014-02-20 21:21:31 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
/* Add the null termination */
|
|
|
|
key_ascii[a] = '\0';
|
2013-06-05 01:37:52 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
/*
|
|
|
|
* If the ro.crypto.fde_sector_size system property is set, append the
|
|
|
|
* parameters to make dm-crypt use the specified crypto sector size and round
|
|
|
|
* the crypto device size down to a crypto sector boundary.
|
2013-06-14 21:11:38 +02:00
|
|
|
*/
|
2021-11-09 01:45:58 +01:00
|
|
|
static int add_sector_size_param(DmTargetCrypt* target, uint64_t* nr_sec) {
|
|
|
|
constexpr char DM_CRYPT_SECTOR_SIZE[] = "ro.crypto.fde_sector_size";
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
2011-01-29 08:31:16 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
if (property_get(DM_CRYPT_SECTOR_SIZE, value, "") > 0) {
|
|
|
|
unsigned int sector_size;
|
2011-01-29 08:31:16 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
if (!ParseUint(value, §or_size) || sector_size < 512 || sector_size > 4096 ||
|
|
|
|
(sector_size & (sector_size - 1)) != 0) {
|
|
|
|
SLOGE("Invalid value for %s: %s. Must be >= 512, <= 4096, and a power of 2\n",
|
|
|
|
DM_CRYPT_SECTOR_SIZE, value);
|
|
|
|
return -1;
|
2019-01-29 23:34:01 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
target->SetSectorSize(sector_size);
|
2010-12-03 12:47:09 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
// With this option, IVs will match the sector numbering, instead
|
|
|
|
// of being hard-coded to being based on 512-byte sectors.
|
|
|
|
target->SetIvLargeSectors();
|
2010-12-03 12:47:09 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
// Round the crypto device size down to a crypto sector boundary.
|
|
|
|
*nr_sec &= ~((sector_size / 512) - 1);
|
2011-05-19 02:20:07 +02:00
|
|
|
}
|
2021-11-09 01:45:58 +01:00
|
|
|
return 0;
|
2015-11-20 16:07:19 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
/*
|
|
|
|
* Called by vold when it's asked to mount an encrypted external
|
|
|
|
* storage volume. The incoming partition has no crypto header/footer,
|
|
|
|
* as any metadata is been stored in a separate, small partition. We
|
|
|
|
* assume it must be using our same crypt type and keysize.
|
2013-04-22 21:15:39 +02:00
|
|
|
*/
|
2021-11-09 01:45:58 +01:00
|
|
|
int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, const KeyBuffer& key,
|
|
|
|
std::string* out_crypto_blkdev) {
|
|
|
|
auto crypto_type = get_crypto_type();
|
|
|
|
if (key.size() != crypto_type.get_keysize()) {
|
|
|
|
SLOGE("Raw keysize %zu does not match crypt keysize %zu", key.size(),
|
|
|
|
crypto_type.get_keysize());
|
2018-09-18 22:30:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2021-11-09 01:45:58 +01:00
|
|
|
uint64_t nr_sec = 0;
|
|
|
|
if (android::vold::GetBlockDev512Sectors(real_blkdev, &nr_sec) != android::OK) {
|
|
|
|
SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
|
2018-09-18 22:30:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-19 02:20:07 +02:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
2013-06-14 20:29:53 +02:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
// We need two ASCII characters to represent each byte, and need space for
|
|
|
|
// the '\0' terminator.
|
|
|
|
char key_ascii[MAX_KEY_LEN * 2 + 1];
|
|
|
|
convert_key_to_hex_ascii(key, key_ascii);
|
2013-06-14 20:29:53 +02:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
auto target = std::make_unique<DmTargetCrypt>(0, nr_sec, crypto_type.get_kernel_name(),
|
|
|
|
key_ascii, 0, real_blkdev, 0);
|
|
|
|
target->AllowDiscards();
|
2013-06-14 20:29:53 +02:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
if (fscrypt_is_native() &&
|
|
|
|
android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false)) {
|
|
|
|
target->AllowEncryptOverride();
|
2011-05-19 02:20:07 +02:00
|
|
|
}
|
2021-11-09 01:45:58 +01:00
|
|
|
if (add_sector_size_param(target.get(), &nr_sec)) {
|
|
|
|
SLOGE("Error processing dm-crypt sector size param\n");
|
|
|
|
return -1;
|
2013-06-14 21:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
DmTable table;
|
|
|
|
table.AddTarget(std::move(target));
|
2014-01-29 22:31:03 +01:00
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
int load_count = 1;
|
|
|
|
while (load_count < TABLE_LOAD_RETRIES) {
|
|
|
|
if (dm.CreateDevice(label, table)) {
|
|
|
|
break;
|
2013-06-14 20:29:53 +02:00
|
|
|
}
|
2021-11-09 01:45:58 +01:00
|
|
|
load_count++;
|
2013-04-22 21:15:39 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
if (load_count >= TABLE_LOAD_RETRIES) {
|
|
|
|
SLOGE("Cannot load dm-crypt mapping table.\n");
|
2018-09-18 22:30:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2021-11-09 01:45:58 +01:00
|
|
|
if (load_count > 1) {
|
|
|
|
SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
|
2013-04-22 21:15:39 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
if (!dm.GetDmDevicePathByName(label, out_crypto_blkdev)) {
|
|
|
|
SLOGE("Cannot determine dm-crypt path for %s.\n", label);
|
2013-04-22 21:15:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:45:58 +01:00
|
|
|
/* Ensure the dm device has been created before returning. */
|
|
|
|
if (android::vold::WaitForFile(out_crypto_blkdev->c_str(), 1s) < 0) {
|
|
|
|
// WaitForFile generates a suitable log message
|
2013-04-22 21:15:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|