Run clang-format over ext4crypt related code
The formatting here is inconsistent with Android house style; use
clang-format to bring it back into line.
Change-Id: Id1fe6ff54e9b668ca88c3fc021ae0a5bdd1327eb
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 429411d..f2b16e3 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -42,20 +42,19 @@
extern "C" {
#include "crypto_scrypt.h"
-
}
namespace android {
namespace vold {
-const KeyAuthentication kEmptyAuthentication { "", "" };
+const KeyAuthentication kEmptyAuthentication{"", ""};
static constexpr size_t AES_KEY_BYTES = 32;
static constexpr size_t GCM_NONCE_BYTES = 12;
static constexpr size_t GCM_MAC_BYTES = 16;
-static constexpr size_t SALT_BYTES = 1<<4;
-static constexpr size_t SECDISCARDABLE_BYTES = 1<<14;
-static constexpr size_t STRETCHED_BYTES = 1<<6;
+static constexpr size_t SALT_BYTES = 1 << 4;
+static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14;
+static constexpr size_t STRETCHED_BYTES = 1 << 6;
static const char* kCurrentVersion = "1";
static const char* kRmPath = "/system/bin/rm";
@@ -72,14 +71,14 @@
static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
if (actual != expected) {
- LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected
- << " got " << actual;
+ LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got "
+ << actual;
return false;
}
return true;
}
-static std::string hashSecdiscardable(const std::string &secdiscardable) {
+static std::string hashSecdiscardable(const std::string& secdiscardable) {
SHA512_CTX c;
SHA512_Init(&c);
@@ -91,18 +90,17 @@
SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
std::string res(SHA512_DIGEST_LENGTH, '\0');
- SHA512_Final(reinterpret_cast<uint8_t *>(&res[0]), &c);
+ SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c);
return res;
}
-static bool generateKeymasterKey(Keymaster &keymaster,
- const KeyAuthentication &auth, const std::string &appId,
- std::string *key) {
+static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth,
+ const std::string& appId, std::string* key) {
auto paramBuilder = keymaster::AuthorizationSetBuilder()
- .AesEncryptionKey(AES_KEY_BYTES * 8)
- .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
- .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
- .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
+ .AesEncryptionKey(AES_KEY_BYTES * 8)
+ .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
+ .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
+ .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
addStringParam(¶mBuilder, keymaster::TAG_APPLICATION_ID, appId);
if (auth.token.empty()) {
LOG(DEBUG) << "Creating key that doesn't need auth token";
@@ -111,10 +109,10 @@
LOG(DEBUG) << "Auth token required for key";
if (auth.token.size() != sizeof(hw_auth_token_t)) {
LOG(ERROR) << "Auth token should be " << sizeof(hw_auth_token_t) << " bytes, was "
- << auth.token.size() << " bytes";
+ << auth.token.size() << " bytes";
return false;
}
- const hw_auth_token_t *at = reinterpret_cast<const hw_auth_token_t *>(auth.token.data());
+ const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data());
paramBuilder.Authorization(keymaster::TAG_USER_SECURE_ID, at->user_id);
paramBuilder.Authorization(keymaster::TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD);
paramBuilder.Authorization(keymaster::TAG_AUTH_TIMEOUT, 5);
@@ -122,12 +120,12 @@
return keymaster.generateKey(paramBuilder.build(), key);
}
-static keymaster::AuthorizationSetBuilder beginParams(
- const KeyAuthentication &auth, const std::string &appId) {
+static keymaster::AuthorizationSetBuilder beginParams(const KeyAuthentication& auth,
+ const std::string& appId) {
auto paramBuilder = keymaster::AuthorizationSetBuilder()
- .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
- .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
- .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
+ .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
+ .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
+ .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
addStringParam(¶mBuilder, keymaster::TAG_APPLICATION_ID, appId);
if (!auth.token.empty()) {
LOG(DEBUG) << "Supplying auth token to Keymaster";
@@ -136,13 +134,9 @@
return paramBuilder;
}
-static bool encryptWithKeymasterKey(
- Keymaster &keymaster,
- const std::string &key,
- const KeyAuthentication &auth,
- const std::string &appId,
- const std::string &message,
- std::string *ciphertext) {
+static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
+ const KeyAuthentication& auth, const std::string& appId,
+ const std::string& message, std::string* ciphertext) {
auto params = beginParams(auth, appId).build();
keymaster::AuthorizationSet outParams;
auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, &outParams);
@@ -153,7 +147,7 @@
return false;
}
// nonceBlob here is just a pointer into existing data, must not be freed
- std::string nonce(reinterpret_cast<const char *>(nonceBlob.data), nonceBlob.data_length);
+ std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
std::string body;
if (!opHandle.updateCompletely(message, &body)) return false;
@@ -165,13 +159,9 @@
return true;
}
-static bool decryptWithKeymasterKey(
- Keymaster &keymaster,
- const std::string &key,
- const KeyAuthentication &auth,
- const std::string &appId,
- const std::string &ciphertext,
- std::string *message) {
+static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
+ const KeyAuthentication& auth, const std::string& appId,
+ const std::string& ciphertext, std::string* message) {
auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
auto params = addStringParam(beginParams(auth, appId), keymaster::TAG_NONCE, nonce).build();
@@ -182,18 +172,18 @@
return true;
}
-static bool readFileToString(const std::string &filename, std::string *result) {
+static bool readFileToString(const std::string& filename, std::string* result) {
if (!android::base::ReadFileToString(filename, result)) {
- PLOG(ERROR) << "Failed to read from " << filename;
- return false;
+ PLOG(ERROR) << "Failed to read from " << filename;
+ return false;
}
return true;
}
-static bool writeStringToFile(const std::string &payload, const std::string &filename) {
+static bool writeStringToFile(const std::string& payload, const std::string& filename) {
if (!android::base::WriteStringToFile(payload, filename)) {
- PLOG(ERROR) << "Failed to write to " << filename;
- return false;
+ PLOG(ERROR) << "Failed to write to " << filename;
+ return false;
}
return true;
}
@@ -205,12 +195,12 @@
return std::string() + kStretchPrefix_scrypt + paramstr;
}
-static bool stretchingNeedsSalt(const std::string &stretching) {
+static bool stretchingNeedsSalt(const std::string& stretching) {
return stretching != kStretch_nopassword && stretching != kStretch_none;
}
-static bool stretchSecret(const std::string &stretching, const std::string &secret,
- const std::string &salt, std::string *stretched) {
+static bool stretchSecret(const std::string& stretching, const std::string& secret,
+ const std::string& salt, std::string* stretched) {
if (stretching == kStretch_nopassword) {
if (!secret.empty()) {
LOG(WARNING) << "Password present but stretching is nopassword";
@@ -219,20 +209,19 @@
stretched->clear();
} else if (stretching == kStretch_none) {
*stretched = secret;
- } else if (std::equal(kStretchPrefix_scrypt.begin(),
- kStretchPrefix_scrypt.end(), stretching.begin())) {
+ } else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(),
+ stretching.begin())) {
int Nf, rf, pf;
- if (!parse_scrypt_parameters(
- stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf, &rf, &pf)) {
+ if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf,
+ &rf, &pf)) {
LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching;
return false;
}
stretched->assign(STRETCHED_BYTES, '\0');
- if (crypto_scrypt(
- reinterpret_cast<const uint8_t *>(secret.data()), secret.size(),
- reinterpret_cast<const uint8_t *>(salt.data()), salt.size(),
- 1 << Nf, 1 << rf, 1 << pf,
- reinterpret_cast<uint8_t *>(&(*stretched)[0]), stretched->size()) != 0) {
+ if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(),
+ reinterpret_cast<const uint8_t*>(salt.data()), salt.size(),
+ 1 << Nf, 1 << rf, 1 << pf,
+ reinterpret_cast<uint8_t*>(&(*stretched)[0]), stretched->size()) != 0) {
LOG(ERROR) << "scrypt failed with params: " << stretching;
return false;
}
@@ -243,21 +232,21 @@
return true;
}
-static bool generateAppId(const KeyAuthentication &auth, const std::string &stretching,
- const std::string &salt, const std::string &secdiscardable,
- std::string* appId) {
+static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching,
+ const std::string& salt, const std::string& secdiscardable,
+ std::string* appId) {
std::string stretched;
if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false;
*appId = hashSecdiscardable(secdiscardable) + stretched;
return true;
}
-bool storeKey(const std::string &dir, const KeyAuthentication &auth, const std::string &key) {
+bool storeKey(const std::string& dir, const KeyAuthentication& auth, const std::string& key) {
if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
PLOG(ERROR) << "key mkdir " << dir;
return false;
}
- if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
+ if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
std::string secdiscardable;
if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
// TODO status_t plays badly with PLOG, fix it.
@@ -266,14 +255,14 @@
}
if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching();
- if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
+ if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
std::string salt;
if (stretchingNeedsSalt(stretching)) {
if (ReadRandomBytes(SALT_BYTES, salt) != OK) {
LOG(ERROR) << "Random read failed";
return false;
}
- if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false;
+ if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false;
}
std::string appId;
if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
@@ -288,7 +277,7 @@
return true;
}
-bool retrieveKey(const std::string &dir, const KeyAuthentication &auth, std::string *key) {
+bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, std::string* key) {
std::string version;
if (!readFileToString(dir + "/" + kFn_version, &version)) return false;
if (version != kCurrentVersion) {
@@ -301,7 +290,7 @@
if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false;
std::string salt;
if (stretchingNeedsSalt(stretching)) {
- if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false;
+ if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false;
}
std::string appId;
if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
@@ -314,7 +303,7 @@
return decryptWithKeymasterKey(keymaster, kmKey, auth, appId, encryptedMessage, key);
}
-static bool deleteKey(const std::string &dir) {
+static bool deleteKey(const std::string& dir) {
std::string kmKey;
if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
Keymaster keymaster;
@@ -323,25 +312,24 @@
return true;
}
-static bool secdiscardSecdiscardable(const std::string &dir) {
- if (ForkExecvp(std::vector<std::string> {
- kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
+static bool secdiscardSecdiscardable(const std::string& dir) {
+ if (ForkExecvp(
+ std::vector<std::string>{kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
LOG(ERROR) << "secdiscard failed";
return false;
}
return true;
}
-static bool recursiveDeleteKey(const std::string &dir) {
- if (ForkExecvp(std::vector<std::string> {
- kRmPath, "-rf", dir}) != 0) {
+static bool recursiveDeleteKey(const std::string& dir) {
+ if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) {
LOG(ERROR) << "recursive delete failed";
return false;
}
return true;
}
-bool destroyKey(const std::string &dir) {
+bool destroyKey(const std::string& dir) {
bool success = true;
// Try each thing, even if previous things failed.
success &= deleteKey(dir);