CryptfsScryptHidlizationEquivalence_test.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. **
  3. ** Copyright 2017, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #define LOG_TAG "scrypt_test"
  18. #include <log/log.h>
  19. #include <gtest/gtest.h>
  20. #include <hardware/keymaster0.h>
  21. #include <hardware/keymaster1.h>
  22. #include <cstring>
  23. #include "../Keymaster.h"
  24. #include "../cryptfs.h"
  25. #ifdef CONFIG_HW_DISK_ENCRYPTION
  26. #include "cryptfs_hw.h"
  27. #endif
  28. #define min(a, b) ((a) < (b) ? (a) : (b))
  29. /* Maximum allowed keymaster blob size. */
  30. #define KEYMASTER_BLOB_SIZE 2048
  31. /* Key Derivation Function algorithms */
  32. #define KDF_PBKDF2 1
  33. #define KDF_SCRYPT 2
  34. /* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */
  35. #define KDF_SCRYPT_KEYMASTER 5
  36. #define KEY_LEN_BYTES 16
  37. #define DEFAULT_PASSWORD "default_password"
  38. #define RSA_KEY_SIZE 2048
  39. #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
  40. #define RSA_EXPONENT 0x10001
  41. #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
  42. static int keymaster_init(keymaster0_device_t** keymaster0_dev,
  43. keymaster1_device_t** keymaster1_dev) {
  44. int rc;
  45. const hw_module_t* mod;
  46. rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
  47. if (rc) {
  48. ALOGE("could not find any keystore module");
  49. goto err;
  50. }
  51. SLOGI("keymaster module name is %s", mod->name);
  52. SLOGI("keymaster version is %d", mod->module_api_version);
  53. *keymaster0_dev = NULL;
  54. *keymaster1_dev = NULL;
  55. if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
  56. SLOGI("Found keymaster1 module, using keymaster1 API.");
  57. rc = keymaster1_open(mod, keymaster1_dev);
  58. } else {
  59. SLOGI("Found keymaster0 module, using keymaster0 API.");
  60. rc = keymaster0_open(mod, keymaster0_dev);
  61. }
  62. if (rc) {
  63. ALOGE("could not open keymaster device in %s (%s)", KEYSTORE_HARDWARE_MODULE_ID,
  64. strerror(-rc));
  65. goto err;
  66. }
  67. return 0;
  68. err:
  69. *keymaster0_dev = NULL;
  70. *keymaster1_dev = NULL;
  71. return rc;
  72. }
  73. /* Should we use keymaster? */
  74. static int keymaster_check_compatibility_old() {
  75. keymaster0_device_t* keymaster0_dev = 0;
  76. keymaster1_device_t* keymaster1_dev = 0;
  77. int rc = 0;
  78. if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
  79. SLOGE("Failed to init keymaster");
  80. rc = -1;
  81. goto out;
  82. }
  83. if (keymaster1_dev) {
  84. rc = 1;
  85. goto out;
  86. }
  87. if (!keymaster0_dev || !keymaster0_dev->common.module) {
  88. rc = -1;
  89. goto out;
  90. }
  91. // TODO(swillden): Check to see if there's any reason to require v0.3. I think v0.1 and v0.2
  92. // should work.
  93. if (keymaster0_dev->common.module->module_api_version < KEYMASTER_MODULE_API_VERSION_0_3) {
  94. rc = 0;
  95. goto out;
  96. }
  97. if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
  98. (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
  99. rc = 1;
  100. }
  101. out:
  102. if (keymaster1_dev) {
  103. keymaster1_close(keymaster1_dev);
  104. }
  105. if (keymaster0_dev) {
  106. keymaster0_close(keymaster0_dev);
  107. }
  108. return rc;
  109. }
  110. /* Create a new keymaster key and store it in this footer */
  111. static int keymaster_create_key_old(struct crypt_mnt_ftr* ftr) {
  112. uint8_t* key = 0;
  113. keymaster0_device_t* keymaster0_dev = 0;
  114. keymaster1_device_t* keymaster1_dev = 0;
  115. if (ftr->keymaster_blob_size) {
  116. SLOGI("Already have key");
  117. return 0;
  118. }
  119. if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
  120. SLOGE("Failed to init keymaster");
  121. return -1;
  122. }
  123. int rc = 0;
  124. size_t key_size = 0;
  125. if (keymaster1_dev) {
  126. keymaster_key_param_t params[] = {
  127. /* Algorithm & size specifications. Stick with RSA for now. Switch to AES later. */
  128. keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA),
  129. keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE),
  130. keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT),
  131. /* The only allowed purpose for this key is signing. */
  132. keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
  133. /* Padding & digest specifications. */
  134. keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
  135. keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
  136. /* Require that the key be usable in standalone mode. File system isn't available. */
  137. keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE),
  138. /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */
  139. keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
  140. /* Rate-limit key usage attempts, to rate-limit brute force */
  141. keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT),
  142. };
  143. keymaster_key_param_set_t param_set = {params, sizeof(params) / sizeof(*params)};
  144. keymaster_key_blob_t key_blob;
  145. keymaster_error_t error = keymaster1_dev->generate_key(
  146. keymaster1_dev, &param_set, &key_blob, NULL /* characteristics */);
  147. if (error != KM_ERROR_OK) {
  148. SLOGE("Failed to generate keymaster1 key, error %d", error);
  149. rc = -1;
  150. goto out;
  151. }
  152. key = (uint8_t*)key_blob.key_material;
  153. key_size = key_blob.key_material_size;
  154. } else if (keymaster0_dev) {
  155. keymaster_rsa_keygen_params_t params;
  156. memset(&params, '\0', sizeof(params));
  157. params.public_exponent = RSA_EXPONENT;
  158. params.modulus_size = RSA_KEY_SIZE;
  159. if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, &params, &key, &key_size)) {
  160. SLOGE("Failed to generate keypair");
  161. rc = -1;
  162. goto out;
  163. }
  164. } else {
  165. SLOGE("Cryptfs bug: keymaster_init succeeded but didn't initialize a device");
  166. rc = -1;
  167. goto out;
  168. }
  169. if (key_size > KEYMASTER_BLOB_SIZE) {
  170. SLOGE("Keymaster key too large for crypto footer");
  171. rc = -1;
  172. goto out;
  173. }
  174. memcpy(ftr->keymaster_blob, key, key_size);
  175. ftr->keymaster_blob_size = key_size;
  176. out:
  177. if (keymaster0_dev) keymaster0_close(keymaster0_dev);
  178. if (keymaster1_dev) keymaster1_close(keymaster1_dev);
  179. free(key);
  180. return rc;
  181. }
  182. /* This signs the given object using the keymaster key. */
  183. static int keymaster_sign_object_old(struct crypt_mnt_ftr* ftr, const unsigned char* object,
  184. const size_t object_size, unsigned char** signature,
  185. size_t* signature_size) {
  186. int rc = 0;
  187. keymaster0_device_t* keymaster0_dev = 0;
  188. keymaster1_device_t* keymaster1_dev = 0;
  189. unsigned char to_sign[RSA_KEY_SIZE_BYTES];
  190. size_t to_sign_size = sizeof(to_sign);
  191. memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
  192. if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
  193. SLOGE("Failed to init keymaster");
  194. rc = -1;
  195. goto out;
  196. }
  197. // To sign a message with RSA, the message must satisfy two
  198. // constraints:
  199. //
  200. // 1. The message, when interpreted as a big-endian numeric value, must
  201. // be strictly less than the public modulus of the RSA key. Note
  202. // that because the most significant bit of the public modulus is
  203. // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
  204. // key), an n-bit message with most significant bit 0 always
  205. // satisfies this requirement.
  206. //
  207. // 2. The message must have the same length in bits as the public
  208. // modulus of the RSA key. This requirement isn't mathematically
  209. // necessary, but is necessary to ensure consistency in
  210. // implementations.
  211. switch (ftr->kdf_type) {
  212. case KDF_SCRYPT_KEYMASTER:
  213. // This ensures the most significant byte of the signed message
  214. // is zero. We could have zero-padded to the left instead, but
  215. // this approach is slightly more robust against changes in
  216. // object size. However, it's still broken (but not unusably
  217. // so) because we really should be using a proper deterministic
  218. // RSA padding function, such as PKCS1.
  219. memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
  220. SLOGI("Signing safely-padded object");
  221. break;
  222. default:
  223. SLOGE("Unknown KDF type %d", ftr->kdf_type);
  224. rc = -1;
  225. goto out;
  226. }
  227. if (keymaster0_dev) {
  228. keymaster_rsa_sign_params_t params;
  229. params.digest_type = DIGEST_NONE;
  230. params.padding_type = PADDING_NONE;
  231. rc = keymaster0_dev->sign_data(keymaster0_dev, &params, ftr->keymaster_blob,
  232. ftr->keymaster_blob_size, to_sign, to_sign_size, signature,
  233. signature_size);
  234. goto out;
  235. } else if (keymaster1_dev) {
  236. keymaster_key_blob_t key = {ftr->keymaster_blob, ftr->keymaster_blob_size};
  237. keymaster_key_param_t params[] = {
  238. keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
  239. keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
  240. };
  241. keymaster_key_param_set_t param_set = {params, sizeof(params) / sizeof(*params)};
  242. keymaster_operation_handle_t op_handle;
  243. keymaster_error_t error = keymaster1_dev->begin(
  244. keymaster1_dev, KM_PURPOSE_SIGN, &key, &param_set, NULL /* out_params */, &op_handle);
  245. if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) {
  246. // Key usage has been rate-limited. Wait a bit and try again.
  247. sleep(KEYMASTER_CRYPTFS_RATE_LIMIT);
  248. error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, &param_set,
  249. NULL /* out_params */, &op_handle);
  250. }
  251. if (error != KM_ERROR_OK) {
  252. SLOGE("Error starting keymaster signature transaction: %d", error);
  253. rc = -1;
  254. goto out;
  255. }
  256. keymaster_blob_t input = {to_sign, to_sign_size};
  257. size_t input_consumed;
  258. error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */, &input,
  259. &input_consumed, NULL /* out_params */, NULL /* output */);
  260. if (error != KM_ERROR_OK) {
  261. SLOGE("Error sending data to keymaster signature transaction: %d", error);
  262. rc = -1;
  263. goto out;
  264. }
  265. if (input_consumed != to_sign_size) {
  266. // This should never happen. If it does, it's a bug in the keymaster implementation.
  267. SLOGE("Keymaster update() did not consume all data.");
  268. keymaster1_dev->abort(keymaster1_dev, op_handle);
  269. rc = -1;
  270. goto out;
  271. }
  272. keymaster_blob_t tmp_sig;
  273. error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */,
  274. NULL /* verify signature */, NULL /* out_params */, &tmp_sig);
  275. if (error != KM_ERROR_OK) {
  276. SLOGE("Error finishing keymaster signature transaction: %d", error);
  277. rc = -1;
  278. goto out;
  279. }
  280. *signature = (uint8_t*)tmp_sig.data;
  281. *signature_size = tmp_sig.data_length;
  282. } else {
  283. SLOGE("Cryptfs bug: keymaster_init succeded but didn't initialize a device.");
  284. rc = -1;
  285. goto out;
  286. }
  287. out:
  288. if (keymaster1_dev) keymaster1_close(keymaster1_dev);
  289. if (keymaster0_dev) keymaster0_close(keymaster0_dev);
  290. return rc;
  291. }
  292. /* Should we use keymaster? */
  293. static int keymaster_check_compatibility_new() {
  294. return keymaster_compatibility_cryptfs_scrypt();
  295. }
  296. #if 0
  297. /* Create a new keymaster key and store it in this footer */
  298. static int keymaster_create_key_new(struct crypt_mnt_ftr *ftr)
  299. {
  300. if (ftr->keymaster_blob_size) {
  301. SLOGI("Already have key");
  302. return 0;
  303. }
  304. int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
  305. KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
  306. &ftr->keymaster_blob_size);
  307. if (rc) {
  308. if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
  309. SLOGE("Keymaster key blob to large)");
  310. ftr->keymaster_blob_size = 0;
  311. }
  312. SLOGE("Failed to generate keypair");
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. #endif
  318. /* This signs the given object using the keymaster key. */
  319. static int keymaster_sign_object_new(struct crypt_mnt_ftr* ftr, const unsigned char* object,
  320. const size_t object_size, unsigned char** signature,
  321. size_t* signature_size) {
  322. unsigned char to_sign[RSA_KEY_SIZE_BYTES];
  323. size_t to_sign_size = sizeof(to_sign);
  324. memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
  325. // To sign a message with RSA, the message must satisfy two
  326. // constraints:
  327. //
  328. // 1. The message, when interpreted as a big-endian numeric value, must
  329. // be strictly less than the public modulus of the RSA key. Note
  330. // that because the most significant bit of the public modulus is
  331. // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
  332. // key), an n-bit message with most significant bit 0 always
  333. // satisfies this requirement.
  334. //
  335. // 2. The message must have the same length in bits as the public
  336. // modulus of the RSA key. This requirement isn't mathematically
  337. // necessary, but is necessary to ensure consistency in
  338. // implementations.
  339. switch (ftr->kdf_type) {
  340. case KDF_SCRYPT_KEYMASTER:
  341. // This ensures the most significant byte of the signed message
  342. // is zero. We could have zero-padded to the left instead, but
  343. // this approach is slightly more robust against changes in
  344. // object size. However, it's still broken (but not unusably
  345. // so) because we really should be using a proper deterministic
  346. // RSA padding function, such as PKCS1.
  347. memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
  348. SLOGI("Signing safely-padded object");
  349. break;
  350. default:
  351. SLOGE("Unknown KDF type %d", ftr->kdf_type);
  352. return -1;
  353. }
  354. if (keymaster_sign_object_for_cryptfs_scrypt(
  355. ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
  356. to_sign_size, signature, signature_size) != KeymasterSignResult::ok)
  357. return -1;
  358. return 0;
  359. }
  360. namespace android {
  361. class CryptFsTest : public testing::Test {
  362. protected:
  363. virtual void SetUp() {}
  364. virtual void TearDown() {}
  365. };
  366. TEST_F(CryptFsTest, ScryptHidlizationEquivalenceTest) {
  367. crypt_mnt_ftr ftr;
  368. ftr.kdf_type = KDF_SCRYPT_KEYMASTER;
  369. ftr.keymaster_blob_size = 0;
  370. ASSERT_EQ(0, keymaster_create_key_old(&ftr));
  371. uint8_t* sig1 = nullptr;
  372. uint8_t* sig2 = nullptr;
  373. size_t sig_size1 = 123456789;
  374. size_t sig_size2 = 123456789;
  375. uint8_t object[] = "the object";
  376. ASSERT_EQ(1, keymaster_check_compatibility_old());
  377. ASSERT_EQ(1, keymaster_check_compatibility_new());
  378. ASSERT_EQ(0, keymaster_sign_object_old(&ftr, object, 10, &sig1, &sig_size1));
  379. ASSERT_EQ(0, keymaster_sign_object_new(&ftr, object, 10, &sig2, &sig_size2));
  380. ASSERT_EQ(sig_size1, sig_size2);
  381. ASSERT_NE(nullptr, sig1);
  382. ASSERT_NE(nullptr, sig2);
  383. EXPECT_EQ(0, memcmp(sig1, sig2, sig_size1));
  384. free(sig1);
  385. free(sig2);
  386. }
  387. } // namespace android