android_keymaster_test_utils.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * Copyright 2014 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 "android_keymaster_test_utils.h"
  17. #include <algorithm>
  18. #include <openssl/rand.h>
  19. #include <keymaster/android_keymaster_messages.h>
  20. #include <keymaster/android_keymaster_utils.h>
  21. using std::copy_if;
  22. using std::find_if;
  23. using std::is_permutation;
  24. using std::ostream;
  25. using std::string;
  26. using std::vector;
  27. #ifndef KEYMASTER_NAME_TAGS
  28. #error Keymaster test code requires that KEYMASTER_NAME_TAGS is defined
  29. #endif
  30. std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param) {
  31. os << "Tag: " << keymaster::StringifyTag(param.tag);
  32. switch (keymaster_tag_get_type(param.tag)) {
  33. case KM_INVALID:
  34. os << " Invalid";
  35. break;
  36. case KM_UINT_REP:
  37. os << " (Rep)";
  38. /* Falls through */
  39. case KM_UINT:
  40. os << " Int: " << param.integer;
  41. break;
  42. case KM_ENUM_REP:
  43. os << " (Rep)";
  44. /* Falls through */
  45. case KM_ENUM:
  46. os << " Enum: " << param.enumerated;
  47. break;
  48. case KM_ULONG_REP:
  49. os << " (Rep)";
  50. /* Falls through */
  51. case KM_ULONG:
  52. os << " Long: " << param.long_integer;
  53. break;
  54. case KM_DATE:
  55. os << " Date: " << param.date_time;
  56. break;
  57. case KM_BOOL:
  58. os << " Bool: " << param.boolean;
  59. break;
  60. case KM_BIGNUM:
  61. os << " Bignum: ";
  62. if (!param.blob.data)
  63. os << "(null)";
  64. else
  65. for (size_t i = 0; i < param.blob.data_length; ++i)
  66. os << std::hex << std::setw(2) << static_cast<int>(param.blob.data[i]) << std::dec;
  67. break;
  68. case KM_BYTES:
  69. os << " Bytes: ";
  70. if (!param.blob.data)
  71. os << "(null)";
  72. else
  73. for (size_t i = 0; i < param.blob.data_length; ++i)
  74. os << std::hex << std::setw(2) << static_cast<int>(param.blob.data[i]) << std::dec;
  75. break;
  76. }
  77. return os;
  78. }
  79. bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b) {
  80. if (a.tag != b.tag) {
  81. return false;
  82. }
  83. switch (keymaster_tag_get_type(a.tag)) {
  84. case KM_INVALID:
  85. return true;
  86. case KM_UINT_REP:
  87. case KM_UINT:
  88. return a.integer == b.integer;
  89. case KM_ENUM_REP:
  90. case KM_ENUM:
  91. return a.enumerated == b.enumerated;
  92. case KM_ULONG:
  93. case KM_ULONG_REP:
  94. return a.long_integer == b.long_integer;
  95. case KM_DATE:
  96. return a.date_time == b.date_time;
  97. case KM_BOOL:
  98. return a.boolean == b.boolean;
  99. case KM_BIGNUM:
  100. case KM_BYTES:
  101. if ((a.blob.data == nullptr || b.blob.data == nullptr) && a.blob.data != b.blob.data)
  102. return false;
  103. return a.blob.data_length == b.blob.data_length &&
  104. (memcmp(a.blob.data, b.blob.data, a.blob.data_length) == 0);
  105. }
  106. return false;
  107. }
  108. static char hex_value[256] = {
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9'
  112. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F'
  113. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0,
  114. 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f'
  115. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  119. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  120. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  121. string hex2str(string a) {
  122. string b;
  123. size_t num = a.size() / 2;
  124. b.resize(num);
  125. for (size_t i = 0; i < num; i++) {
  126. b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]);
  127. }
  128. return b;
  129. }
  130. namespace keymaster {
  131. bool operator==(const AuthorizationSet& a, const AuthorizationSet& b) {
  132. if (a.size() != b.size())
  133. return false;
  134. for (size_t i = 0; i < a.size(); ++i)
  135. if (!(a[i] == b[i]))
  136. return false;
  137. return true;
  138. }
  139. bool operator!=(const AuthorizationSet& a, const AuthorizationSet& b) {
  140. return !(a == b);
  141. }
  142. std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set) {
  143. if (set.size() == 0)
  144. os << "(Empty)" << std::endl;
  145. else {
  146. os << "\n";
  147. for (size_t i = 0; i < set.size(); ++i)
  148. os << set[i] << std::endl;
  149. }
  150. return os;
  151. }
  152. namespace test {
  153. std::ostream& operator<<(std::ostream& os, const InstanceCreatorPtr& instance_creator) {
  154. return os << instance_creator->name();
  155. }
  156. Keymaster2Test::Keymaster2Test() : op_handle_(OP_HANDLE_SENTINEL) {
  157. memset(&characteristics_, 0, sizeof(characteristics_));
  158. blob_.key_material = nullptr;
  159. RAND_seed("foobar", 6);
  160. blob_.key_material = 0;
  161. device_ = GetParam()->CreateDevice();
  162. }
  163. Keymaster2Test::~Keymaster2Test() {
  164. FreeCharacteristics();
  165. FreeKeyBlob();
  166. device_->common.close(reinterpret_cast<hw_device_t*>(device_));
  167. }
  168. keymaster2_device_t* Keymaster2Test::device() {
  169. return device_;
  170. }
  171. keymaster_error_t Keymaster2Test::GenerateKey(const AuthorizationSetBuilder& builder) {
  172. AuthorizationSet params(builder.build());
  173. params.push_back(UserAuthParams());
  174. params.push_back(ClientParams());
  175. FreeKeyBlob();
  176. FreeCharacteristics();
  177. return device()->generate_key(device(), &params, &blob_, &characteristics_);
  178. }
  179. keymaster_error_t Keymaster2Test::DeleteKey() {
  180. return device()->delete_key(device(), &blob_);
  181. }
  182. keymaster_error_t Keymaster2Test::ImportKey(const AuthorizationSetBuilder& builder,
  183. keymaster_key_format_t format,
  184. const string& key_material) {
  185. AuthorizationSet params(builder.build());
  186. params.push_back(UserAuthParams());
  187. params.push_back(ClientParams());
  188. FreeKeyBlob();
  189. FreeCharacteristics();
  190. keymaster_blob_t key = {reinterpret_cast<const uint8_t*>(key_material.c_str()),
  191. key_material.length()};
  192. return device()->import_key(device(), &params, format, &key, &blob_, &characteristics_);
  193. }
  194. AuthorizationSet Keymaster2Test::UserAuthParams() {
  195. AuthorizationSet set;
  196. set.push_back(TAG_USER_ID, 7);
  197. set.push_back(TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD);
  198. set.push_back(TAG_AUTH_TIMEOUT, 300);
  199. return set;
  200. }
  201. AuthorizationSet Keymaster2Test::ClientParams() {
  202. AuthorizationSet set;
  203. set.push_back(TAG_APPLICATION_ID, "app_id", 6);
  204. return set;
  205. }
  206. keymaster_error_t Keymaster2Test::BeginOperation(keymaster_purpose_t purpose) {
  207. AuthorizationSet in_params(client_params());
  208. keymaster_key_param_set_t out_params;
  209. keymaster_error_t error =
  210. device()->begin(device(), purpose, &blob_, &in_params, &out_params, &op_handle_);
  211. EXPECT_EQ(0U, out_params.length);
  212. EXPECT_TRUE(out_params.params == nullptr);
  213. return error;
  214. }
  215. keymaster_error_t Keymaster2Test::BeginOperation(keymaster_purpose_t purpose,
  216. const AuthorizationSet& input_set,
  217. AuthorizationSet* output_set) {
  218. keymaster_key_param_set_t out_params;
  219. keymaster_error_t error =
  220. device()->begin(device(), purpose, &blob_, &input_set, &out_params, &op_handle_);
  221. if (error == KM_ERROR_OK) {
  222. if (output_set) {
  223. output_set->Reinitialize(out_params);
  224. } else {
  225. EXPECT_EQ(0U, out_params.length);
  226. EXPECT_TRUE(out_params.params == nullptr);
  227. }
  228. keymaster_free_param_set(&out_params);
  229. }
  230. return error;
  231. }
  232. keymaster_error_t Keymaster2Test::UpdateOperation(const string& message, string* output,
  233. size_t* input_consumed) {
  234. EXPECT_NE(op_handle_, OP_HANDLE_SENTINEL);
  235. keymaster_blob_t input = {reinterpret_cast<const uint8_t*>(message.c_str()), message.length()};
  236. keymaster_blob_t out_tmp;
  237. keymaster_key_param_set_t out_params;
  238. keymaster_error_t error = device()->update(device(), op_handle_, nullptr /* params */, &input,
  239. input_consumed, &out_params, &out_tmp);
  240. if (error == KM_ERROR_OK && out_tmp.data)
  241. output->append(reinterpret_cast<const char*>(out_tmp.data), out_tmp.data_length);
  242. free(const_cast<uint8_t*>(out_tmp.data));
  243. return error;
  244. }
  245. keymaster_error_t Keymaster2Test::UpdateOperation(const AuthorizationSet& additional_params,
  246. const string& message,
  247. AuthorizationSet* output_params, string* output,
  248. size_t* input_consumed) {
  249. EXPECT_NE(op_handle_, OP_HANDLE_SENTINEL);
  250. keymaster_blob_t input = {reinterpret_cast<const uint8_t*>(message.c_str()), message.length()};
  251. keymaster_blob_t out_tmp;
  252. keymaster_key_param_set_t out_params;
  253. keymaster_error_t error = device()->update(device(), op_handle_, &additional_params, &input,
  254. input_consumed, &out_params, &out_tmp);
  255. if (error == KM_ERROR_OK && out_tmp.data)
  256. output->append(reinterpret_cast<const char*>(out_tmp.data), out_tmp.data_length);
  257. free((void*)out_tmp.data);
  258. if (output_params)
  259. output_params->Reinitialize(out_params);
  260. keymaster_free_param_set(&out_params);
  261. return error;
  262. }
  263. keymaster_error_t Keymaster2Test::FinishOperation(string* output) {
  264. return FinishOperation("", "", output);
  265. }
  266. keymaster_error_t Keymaster2Test::FinishOperation(const string& input, const string& signature,
  267. string* output) {
  268. AuthorizationSet additional_params;
  269. AuthorizationSet output_params;
  270. return FinishOperation(additional_params, input, signature, &output_params, output);
  271. }
  272. keymaster_error_t Keymaster2Test::FinishOperation(const AuthorizationSet& additional_params,
  273. const string& input, const string& signature,
  274. AuthorizationSet* output_params, string* output) {
  275. keymaster_blob_t inp = {reinterpret_cast<const uint8_t*>(input.c_str()), input.length()};
  276. keymaster_blob_t sig = {reinterpret_cast<const uint8_t*>(signature.c_str()),
  277. signature.length()};
  278. keymaster_blob_t out_tmp;
  279. keymaster_key_param_set_t out_params;
  280. keymaster_error_t error = device()->finish(device(), op_handle_, &additional_params, &inp, &sig,
  281. &out_params, &out_tmp);
  282. if (error != KM_ERROR_OK) {
  283. EXPECT_TRUE(out_tmp.data == nullptr);
  284. EXPECT_TRUE(out_params.params == nullptr);
  285. return error;
  286. }
  287. if (out_tmp.data)
  288. output->append(reinterpret_cast<const char*>(out_tmp.data), out_tmp.data_length);
  289. free((void*)out_tmp.data);
  290. if (output_params)
  291. output_params->Reinitialize(out_params);
  292. keymaster_free_param_set(&out_params);
  293. return error;
  294. }
  295. keymaster_error_t Keymaster2Test::AbortOperation() {
  296. return device()->abort(device(), op_handle_);
  297. }
  298. keymaster_error_t Keymaster2Test::AttestKey(const string& attest_challenge,
  299. const string& attest_app_id,
  300. keymaster_cert_chain_t* cert_chain) {
  301. AuthorizationSet attest_params;
  302. attest_params.push_back(UserAuthParams());
  303. attest_params.push_back(ClientParams());
  304. attest_params.push_back(TAG_ATTESTATION_CHALLENGE, attest_challenge.data(),
  305. attest_challenge.length());
  306. attest_params.push_back(TAG_ATTESTATION_APPLICATION_ID, attest_app_id.data(),
  307. attest_app_id.length());
  308. return device()->attest_key(device(), &blob_, &attest_params, cert_chain);
  309. }
  310. keymaster_error_t Keymaster2Test::UpgradeKey(const AuthorizationSet& upgrade_params) {
  311. keymaster_key_blob_t upgraded_blob;
  312. keymaster_error_t error =
  313. device()->upgrade_key(device(), &blob_, &upgrade_params, &upgraded_blob);
  314. if (error == KM_ERROR_OK) {
  315. FreeKeyBlob();
  316. blob_ = upgraded_blob;
  317. }
  318. return error;
  319. }
  320. string Keymaster2Test::ProcessMessage(keymaster_purpose_t purpose, const string& message) {
  321. EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, client_params(), nullptr /* output_params */));
  322. string result;
  323. EXPECT_EQ(KM_ERROR_OK, FinishOperation(message, "" /* signature */, &result));
  324. return result;
  325. }
  326. string Keymaster2Test::ProcessMessage(keymaster_purpose_t purpose, const string& message,
  327. const AuthorizationSet& begin_params,
  328. const AuthorizationSet& update_params,
  329. AuthorizationSet* begin_out_params) {
  330. EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, begin_params, begin_out_params));
  331. string result;
  332. EXPECT_EQ(KM_ERROR_OK, FinishOperation(update_params, message, "" /* signature */, &result));
  333. return result;
  334. }
  335. string Keymaster2Test::ProcessMessage(keymaster_purpose_t purpose, const string& message,
  336. const string& signature, const AuthorizationSet& begin_params,
  337. const AuthorizationSet& update_params,
  338. AuthorizationSet* output_params) {
  339. EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, begin_params, output_params));
  340. string result;
  341. EXPECT_EQ(KM_ERROR_OK, FinishOperation(update_params, message, signature, &result));
  342. return result;
  343. }
  344. string Keymaster2Test::ProcessMessage(keymaster_purpose_t purpose, const string& message,
  345. const string& signature) {
  346. EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, client_params(), nullptr /* output_params */));
  347. string result;
  348. EXPECT_EQ(KM_ERROR_OK, FinishOperation(message, signature, &result));
  349. return result;
  350. }
  351. void Keymaster2Test::SignMessage(const string& message, string* signature,
  352. keymaster_digest_t digest) {
  353. SCOPED_TRACE("SignMessage");
  354. AuthorizationSet input_params(AuthorizationSet(client_params_, array_length(client_params_)));
  355. input_params.push_back(TAG_DIGEST, digest);
  356. AuthorizationSet update_params;
  357. AuthorizationSet output_params;
  358. *signature =
  359. ProcessMessage(KM_PURPOSE_SIGN, message, input_params, update_params, &output_params);
  360. EXPECT_GT(signature->size(), 0U);
  361. }
  362. void Keymaster2Test::SignMessage(const string& message, string* signature,
  363. keymaster_digest_t digest, keymaster_padding_t padding) {
  364. SCOPED_TRACE("SignMessage");
  365. AuthorizationSet input_params(AuthorizationSet(client_params_, array_length(client_params_)));
  366. input_params.push_back(TAG_DIGEST, digest);
  367. input_params.push_back(TAG_PADDING, padding);
  368. AuthorizationSet update_params;
  369. AuthorizationSet output_params;
  370. *signature =
  371. ProcessMessage(KM_PURPOSE_SIGN, message, input_params, update_params, &output_params);
  372. EXPECT_GT(signature->size(), 0U);
  373. }
  374. void Keymaster2Test::MacMessage(const string& message, string* signature, size_t mac_length) {
  375. SCOPED_TRACE("SignMessage");
  376. AuthorizationSet input_params(AuthorizationSet(client_params_, array_length(client_params_)));
  377. input_params.push_back(TAG_MAC_LENGTH, mac_length);
  378. AuthorizationSet update_params;
  379. AuthorizationSet output_params;
  380. *signature =
  381. ProcessMessage(KM_PURPOSE_SIGN, message, input_params, update_params, &output_params);
  382. EXPECT_GT(signature->size(), 0U);
  383. }
  384. void Keymaster2Test::VerifyMessage(const string& message, const string& signature,
  385. keymaster_digest_t digest) {
  386. SCOPED_TRACE("VerifyMessage");
  387. AuthorizationSet input_params(client_params());
  388. input_params.push_back(TAG_DIGEST, digest);
  389. AuthorizationSet update_params;
  390. AuthorizationSet output_params;
  391. ProcessMessage(KM_PURPOSE_VERIFY, message, signature, input_params, update_params,
  392. &output_params);
  393. }
  394. void Keymaster2Test::VerifyMessage(const string& message, const string& signature,
  395. keymaster_digest_t digest, keymaster_padding_t padding) {
  396. SCOPED_TRACE("VerifyMessage");
  397. AuthorizationSet input_params(client_params());
  398. input_params.push_back(TAG_DIGEST, digest);
  399. input_params.push_back(TAG_PADDING, padding);
  400. AuthorizationSet update_params;
  401. AuthorizationSet output_params;
  402. ProcessMessage(KM_PURPOSE_VERIFY, message, signature, input_params, update_params,
  403. &output_params);
  404. }
  405. void Keymaster2Test::VerifyMac(const string& message, const string& signature) {
  406. SCOPED_TRACE("VerifyMac");
  407. ProcessMessage(KM_PURPOSE_VERIFY, message, signature);
  408. }
  409. string Keymaster2Test::EncryptMessage(const string& message, keymaster_padding_t padding,
  410. string* generated_nonce) {
  411. SCOPED_TRACE("EncryptMessage");
  412. AuthorizationSet begin_params(client_params()), output_params;
  413. begin_params.push_back(TAG_PADDING, padding);
  414. AuthorizationSet update_params;
  415. string ciphertext =
  416. ProcessMessage(KM_PURPOSE_ENCRYPT, message, begin_params, update_params, &output_params);
  417. if (generated_nonce) {
  418. keymaster_blob_t nonce_blob;
  419. EXPECT_TRUE(output_params.GetTagValue(TAG_NONCE, &nonce_blob));
  420. *generated_nonce = make_string(nonce_blob.data, nonce_blob.data_length);
  421. } else {
  422. EXPECT_EQ(-1, output_params.find(TAG_NONCE));
  423. }
  424. return ciphertext;
  425. }
  426. string Keymaster2Test::EncryptMessage(const string& message, keymaster_digest_t digest,
  427. keymaster_padding_t padding, string* generated_nonce) {
  428. AuthorizationSet update_params;
  429. return EncryptMessage(update_params, message, digest, padding, generated_nonce);
  430. }
  431. string Keymaster2Test::EncryptMessage(const string& message, keymaster_block_mode_t block_mode,
  432. keymaster_padding_t padding, string* generated_nonce) {
  433. AuthorizationSet update_params;
  434. return EncryptMessage(update_params, message, block_mode, padding, generated_nonce);
  435. }
  436. string Keymaster2Test::EncryptMessage(const AuthorizationSet& update_params, const string& message,
  437. keymaster_digest_t digest, keymaster_padding_t padding,
  438. string* generated_nonce) {
  439. SCOPED_TRACE("EncryptMessage");
  440. AuthorizationSet begin_params(client_params()), output_params;
  441. begin_params.push_back(TAG_PADDING, padding);
  442. begin_params.push_back(TAG_DIGEST, digest);
  443. string ciphertext =
  444. ProcessMessage(KM_PURPOSE_ENCRYPT, message, begin_params, update_params, &output_params);
  445. if (generated_nonce) {
  446. keymaster_blob_t nonce_blob;
  447. EXPECT_TRUE(output_params.GetTagValue(TAG_NONCE, &nonce_blob));
  448. *generated_nonce = make_string(nonce_blob.data, nonce_blob.data_length);
  449. } else {
  450. EXPECT_EQ(-1, output_params.find(TAG_NONCE));
  451. }
  452. return ciphertext;
  453. }
  454. string Keymaster2Test::EncryptMessage(const AuthorizationSet& update_params, const string& message,
  455. keymaster_block_mode_t block_mode,
  456. keymaster_padding_t padding, string* generated_nonce) {
  457. SCOPED_TRACE("EncryptMessage");
  458. AuthorizationSet begin_params(client_params()), output_params;
  459. begin_params.push_back(TAG_PADDING, padding);
  460. begin_params.push_back(TAG_BLOCK_MODE, block_mode);
  461. string ciphertext =
  462. ProcessMessage(KM_PURPOSE_ENCRYPT, message, begin_params, update_params, &output_params);
  463. if (generated_nonce) {
  464. keymaster_blob_t nonce_blob;
  465. EXPECT_TRUE(output_params.GetTagValue(TAG_NONCE, &nonce_blob));
  466. *generated_nonce = make_string(nonce_blob.data, nonce_blob.data_length);
  467. } else {
  468. EXPECT_EQ(-1, output_params.find(TAG_NONCE));
  469. }
  470. return ciphertext;
  471. }
  472. string Keymaster2Test::EncryptMessageWithParams(const string& message,
  473. const AuthorizationSet& begin_params,
  474. const AuthorizationSet& update_params,
  475. AuthorizationSet* output_params) {
  476. SCOPED_TRACE("EncryptMessageWithParams");
  477. return ProcessMessage(KM_PURPOSE_ENCRYPT, message, begin_params, update_params, output_params);
  478. }
  479. string Keymaster2Test::DecryptMessage(const string& ciphertext, keymaster_padding_t padding) {
  480. SCOPED_TRACE("DecryptMessage");
  481. AuthorizationSet begin_params(client_params());
  482. begin_params.push_back(TAG_PADDING, padding);
  483. AuthorizationSet update_params;
  484. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  485. }
  486. string Keymaster2Test::DecryptMessage(const string& ciphertext, keymaster_digest_t digest,
  487. keymaster_padding_t padding) {
  488. SCOPED_TRACE("DecryptMessage");
  489. AuthorizationSet begin_params(client_params());
  490. begin_params.push_back(TAG_PADDING, padding);
  491. begin_params.push_back(TAG_DIGEST, digest);
  492. AuthorizationSet update_params;
  493. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  494. }
  495. string Keymaster2Test::DecryptMessage(const string& ciphertext, keymaster_block_mode_t block_mode,
  496. keymaster_padding_t padding) {
  497. SCOPED_TRACE("DecryptMessage");
  498. AuthorizationSet begin_params(client_params());
  499. begin_params.push_back(TAG_PADDING, padding);
  500. begin_params.push_back(TAG_BLOCK_MODE, block_mode);
  501. AuthorizationSet update_params;
  502. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  503. }
  504. string Keymaster2Test::DecryptMessage(const string& ciphertext, keymaster_digest_t digest,
  505. keymaster_padding_t padding, const string& nonce) {
  506. SCOPED_TRACE("DecryptMessage");
  507. AuthorizationSet begin_params(client_params());
  508. begin_params.push_back(TAG_PADDING, padding);
  509. begin_params.push_back(TAG_DIGEST, digest);
  510. begin_params.push_back(TAG_NONCE, nonce.data(), nonce.size());
  511. AuthorizationSet update_params;
  512. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  513. }
  514. string Keymaster2Test::DecryptMessage(const string& ciphertext, keymaster_block_mode_t block_mode,
  515. keymaster_padding_t padding, const string& nonce) {
  516. SCOPED_TRACE("DecryptMessage");
  517. AuthorizationSet begin_params(client_params());
  518. begin_params.push_back(TAG_PADDING, padding);
  519. begin_params.push_back(TAG_BLOCK_MODE, block_mode);
  520. begin_params.push_back(TAG_NONCE, nonce.data(), nonce.size());
  521. AuthorizationSet update_params;
  522. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  523. }
  524. string Keymaster2Test::DecryptMessage(const AuthorizationSet& update_params,
  525. const string& ciphertext, keymaster_digest_t digest,
  526. keymaster_padding_t padding, const string& nonce) {
  527. SCOPED_TRACE("DecryptMessage");
  528. AuthorizationSet begin_params(client_params());
  529. begin_params.push_back(TAG_PADDING, padding);
  530. begin_params.push_back(TAG_DIGEST, digest);
  531. begin_params.push_back(TAG_NONCE, nonce.data(), nonce.size());
  532. return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext, begin_params, update_params);
  533. }
  534. string Keymaster2Test::DecryptMessageWithParams(const string& message,
  535. const AuthorizationSet& begin_params,
  536. const AuthorizationSet& update_params,
  537. AuthorizationSet* output_params) {
  538. SCOPED_TRACE("DecryptMessageWithParams");
  539. return ProcessMessage(KM_PURPOSE_DECRYPT, message, begin_params, update_params, output_params);
  540. }
  541. keymaster_error_t Keymaster2Test::GetCharacteristics() {
  542. FreeCharacteristics();
  543. return device()->get_key_characteristics(device(), &blob_, &client_id_, nullptr /* app_data */,
  544. &characteristics_);
  545. }
  546. keymaster_error_t Keymaster2Test::ExportKey(keymaster_key_format_t format, string* export_data) {
  547. keymaster_blob_t export_tmp;
  548. keymaster_error_t error = device()->export_key(device(), format, &blob_, &client_id_,
  549. nullptr /* app_data */, &export_tmp);
  550. if (error != KM_ERROR_OK)
  551. return error;
  552. *export_data = string(reinterpret_cast<const char*>(export_tmp.data), export_tmp.data_length);
  553. free((void*)export_tmp.data);
  554. return error;
  555. }
  556. void Keymaster2Test::CheckHmacTestVector(const string& key, const string& message,
  557. keymaster_digest_t digest, string expected_mac) {
  558. ASSERT_EQ(KM_ERROR_OK, ImportKey(AuthorizationSetBuilder()
  559. .HmacKey(key.size() * 8)
  560. .Authorization(TAG_MIN_MAC_LENGTH, expected_mac.size() * 8)
  561. .Digest(digest),
  562. KM_KEY_FORMAT_RAW, key));
  563. string signature;
  564. MacMessage(message, &signature, expected_mac.size() * 8);
  565. EXPECT_EQ(expected_mac, signature) << "Test vector didn't match for digest " << (int)digest;
  566. }
  567. void Keymaster2Test::CheckAesCtrTestVector(const string& key, const string& nonce,
  568. const string& message,
  569. const string& expected_ciphertext) {
  570. ASSERT_EQ(KM_ERROR_OK, ImportKey(AuthorizationSetBuilder()
  571. .AesEncryptionKey(key.size() * 8)
  572. .Authorization(TAG_BLOCK_MODE, KM_MODE_CTR)
  573. .Authorization(TAG_CALLER_NONCE)
  574. .Padding(KM_PAD_NONE),
  575. KM_KEY_FORMAT_RAW, key));
  576. AuthorizationSet begin_params(client_params()), update_params, output_params;
  577. begin_params.push_back(TAG_NONCE, nonce.data(), nonce.size());
  578. begin_params.push_back(TAG_BLOCK_MODE, KM_MODE_CTR);
  579. begin_params.push_back(TAG_PADDING, KM_PAD_NONE);
  580. string ciphertext =
  581. EncryptMessageWithParams(message, begin_params, update_params, &output_params);
  582. EXPECT_EQ(expected_ciphertext, ciphertext);
  583. }
  584. void Keymaster2Test::CheckTripleDesTestVector(keymaster_purpose_t purpose,
  585. keymaster_block_mode_t block_mode,
  586. keymaster_padding_t padding_mode, const string& key,
  587. const string& iv, const string& input,
  588. const string& expected_output) {
  589. auto authset = AuthorizationSetBuilder()
  590. .TripleDesEncryptionKey(key.size() * 7)
  591. .Authorization(TAG_BLOCK_MODE, block_mode)
  592. .Padding(padding_mode);
  593. if (iv.size()) authset.Authorization(TAG_CALLER_NONCE);
  594. ASSERT_EQ(KM_ERROR_OK, ImportKey(authset, KM_KEY_FORMAT_RAW, key));
  595. AuthorizationSet begin_params(client_params()), update_params, output_params;
  596. if (iv.size()) begin_params.push_back(TAG_NONCE, iv.data(), iv.size());
  597. begin_params.push_back(TAG_BLOCK_MODE, block_mode);
  598. begin_params.push_back(TAG_PADDING, padding_mode);
  599. string output = ProcessMessage(purpose, input, begin_params, update_params, &output_params);
  600. EXPECT_EQ(expected_output, output);
  601. }
  602. AuthorizationSet Keymaster2Test::hw_enforced() {
  603. return AuthorizationSet(characteristics_.hw_enforced);
  604. }
  605. AuthorizationSet Keymaster2Test::sw_enforced() {
  606. return AuthorizationSet(characteristics_.sw_enforced);
  607. }
  608. void Keymaster2Test::FreeCharacteristics() {
  609. keymaster_free_characteristics(&characteristics_);
  610. }
  611. void Keymaster2Test::FreeKeyBlob() {
  612. free(const_cast<uint8_t*>(blob_.key_material));
  613. blob_.key_material = nullptr;
  614. }
  615. void Keymaster2Test::corrupt_key_blob() {
  616. assert(blob_.key_material);
  617. uint8_t* tmp = const_cast<uint8_t*>(blob_.key_material);
  618. ++tmp[blob_.key_material_size / 2];
  619. }
  620. class Sha256OnlyWrapper {
  621. public:
  622. explicit Sha256OnlyWrapper(const keymaster1_device_t* wrapped_device)
  623. : wrapped_device_(wrapped_device) {
  624. new_module = *wrapped_device_->common.module;
  625. new_module_name = std::string("SHA 256-only ") + wrapped_device_->common.module->name;
  626. new_module.name = new_module_name.c_str();
  627. memset(&device_, 0, sizeof(device_));
  628. device_.common.module = &new_module;
  629. device_.common.close = close_device;
  630. device_.get_supported_algorithms = get_supported_algorithms;
  631. device_.get_supported_block_modes = get_supported_block_modes;
  632. device_.get_supported_padding_modes = get_supported_padding_modes;
  633. device_.get_supported_digests = get_supported_digests;
  634. device_.get_supported_import_formats = get_supported_import_formats;
  635. device_.get_supported_export_formats = get_supported_export_formats;
  636. device_.add_rng_entropy = add_rng_entropy;
  637. device_.generate_key = generate_key;
  638. device_.get_key_characteristics = get_key_characteristics;
  639. device_.import_key = import_key;
  640. device_.export_key = export_key;
  641. device_.begin = begin;
  642. device_.update = update;
  643. device_.finish = finish;
  644. device_.abort = abort;
  645. }
  646. keymaster1_device_t* keymaster_device() { return &device_; }
  647. static bool is_supported(keymaster_digest_t digest) {
  648. return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
  649. }
  650. static bool all_digests_supported(const keymaster_key_param_set_t* params) {
  651. for (size_t i = 0; i < params->length; ++i)
  652. if (params->params[i].tag == TAG_DIGEST)
  653. if (!is_supported(static_cast<keymaster_digest_t>(params->params[i].enumerated)))
  654. return false;
  655. return true;
  656. }
  657. static const keymaster_key_param_t*
  658. get_algorithm_param(const keymaster_key_param_set_t* params) {
  659. keymaster_key_param_t* end = params->params + params->length;
  660. auto alg_ptr = std::find_if(params->params, end, [](keymaster_key_param_t& p) {
  661. return p.tag == KM_TAG_ALGORITHM;
  662. });
  663. if (alg_ptr == end)
  664. return nullptr;
  665. return alg_ptr;
  666. }
  667. static int close_device(hw_device_t* dev) {
  668. Sha256OnlyWrapper* wrapper = reinterpret_cast<Sha256OnlyWrapper*>(dev);
  669. const keymaster1_device_t* wrapped_device = wrapper->wrapped_device_;
  670. delete wrapper;
  671. return wrapped_device->common.close(const_cast<hw_device_t*>(&wrapped_device->common));
  672. }
  673. static const keymaster1_device_t* unwrap(const keymaster1_device_t* dev) {
  674. return reinterpret_cast<const Sha256OnlyWrapper*>(dev)->wrapped_device_;
  675. }
  676. static keymaster_error_t get_supported_algorithms(const struct keymaster1_device* dev,
  677. keymaster_algorithm_t** algorithms,
  678. size_t* algorithms_length) {
  679. return unwrap(dev)->get_supported_algorithms(unwrap(dev), algorithms, algorithms_length);
  680. }
  681. static keymaster_error_t get_supported_block_modes(const struct keymaster1_device* dev,
  682. keymaster_algorithm_t algorithm,
  683. keymaster_purpose_t purpose,
  684. keymaster_block_mode_t** modes,
  685. size_t* modes_length) {
  686. return unwrap(dev)->get_supported_block_modes(unwrap(dev), algorithm, purpose, modes,
  687. modes_length);
  688. }
  689. static keymaster_error_t get_supported_padding_modes(const struct keymaster1_device* dev,
  690. keymaster_algorithm_t algorithm,
  691. keymaster_purpose_t purpose,
  692. keymaster_padding_t** modes,
  693. size_t* modes_length) {
  694. return unwrap(dev)->get_supported_padding_modes(unwrap(dev), algorithm, purpose, modes,
  695. modes_length);
  696. }
  697. static keymaster_error_t get_supported_digests(const keymaster1_device_t* dev,
  698. keymaster_algorithm_t algorithm,
  699. keymaster_purpose_t purpose,
  700. keymaster_digest_t** digests,
  701. size_t* digests_length) {
  702. keymaster_error_t error = unwrap(dev)->get_supported_digests(
  703. unwrap(dev), algorithm, purpose, digests, digests_length);
  704. if (error != KM_ERROR_OK)
  705. return error;
  706. std::vector<keymaster_digest_t> filtered_digests;
  707. std::copy_if(*digests, *digests + *digests_length, std::back_inserter(filtered_digests),
  708. [](keymaster_digest_t digest) { return is_supported(digest); });
  709. free(*digests);
  710. *digests_length = filtered_digests.size();
  711. *digests = reinterpret_cast<keymaster_digest_t*>(
  712. malloc(*digests_length * sizeof(keymaster_digest_t)));
  713. std::copy(filtered_digests.begin(), filtered_digests.end(), *digests);
  714. return KM_ERROR_OK;
  715. }
  716. static keymaster_error_t get_supported_import_formats(const struct keymaster1_device* dev,
  717. keymaster_algorithm_t algorithm,
  718. keymaster_key_format_t** formats,
  719. size_t* formats_length) {
  720. return unwrap(dev)->get_supported_import_formats(unwrap(dev), algorithm, formats,
  721. formats_length);
  722. }
  723. static keymaster_error_t get_supported_export_formats(const struct keymaster1_device* dev,
  724. keymaster_algorithm_t algorithm,
  725. keymaster_key_format_t** formats,
  726. size_t* formats_length) {
  727. return unwrap(dev)->get_supported_export_formats(unwrap(dev), algorithm, formats,
  728. formats_length);
  729. }
  730. static keymaster_error_t add_rng_entropy(const struct keymaster1_device* dev,
  731. const uint8_t* data, size_t data_length) {
  732. return unwrap(dev)->add_rng_entropy(unwrap(dev), data, data_length);
  733. }
  734. static keymaster_error_t generate_key(const keymaster1_device_t* dev,
  735. const keymaster_key_param_set_t* params,
  736. keymaster_key_blob_t* key_blob,
  737. keymaster_key_characteristics_t** characteristics) {
  738. auto alg_ptr = get_algorithm_param(params);
  739. if (!alg_ptr)
  740. return KM_ERROR_UNSUPPORTED_ALGORITHM;
  741. if (alg_ptr->enumerated == KM_ALGORITHM_HMAC && !all_digests_supported(params))
  742. return KM_ERROR_UNSUPPORTED_DIGEST;
  743. return unwrap(dev)->generate_key(unwrap(dev), params, key_blob, characteristics);
  744. }
  745. static keymaster_error_t
  746. get_key_characteristics(const struct keymaster1_device* dev,
  747. const keymaster_key_blob_t* key_blob, const keymaster_blob_t* client_id,
  748. const keymaster_blob_t* app_data,
  749. keymaster_key_characteristics_t** characteristics) {
  750. return unwrap(dev)->get_key_characteristics(unwrap(dev), key_blob, client_id, app_data,
  751. characteristics);
  752. }
  753. static keymaster_error_t
  754. import_key(const keymaster1_device_t* dev, const keymaster_key_param_set_t* params,
  755. keymaster_key_format_t key_format, const keymaster_blob_t* key_data,
  756. keymaster_key_blob_t* key_blob, keymaster_key_characteristics_t** characteristics) {
  757. auto alg_ptr = get_algorithm_param(params);
  758. if (!alg_ptr)
  759. return KM_ERROR_UNSUPPORTED_ALGORITHM;
  760. if (alg_ptr->enumerated == KM_ALGORITHM_HMAC && !all_digests_supported(params))
  761. return KM_ERROR_UNSUPPORTED_DIGEST;
  762. return unwrap(dev)->import_key(unwrap(dev), params, key_format, key_data, key_blob,
  763. characteristics);
  764. }
  765. static keymaster_error_t export_key(const struct keymaster1_device* dev, //
  766. keymaster_key_format_t export_format,
  767. const keymaster_key_blob_t* key_to_export,
  768. const keymaster_blob_t* client_id,
  769. const keymaster_blob_t* app_data,
  770. keymaster_blob_t* export_data) {
  771. return unwrap(dev)->export_key(unwrap(dev), export_format, key_to_export, client_id,
  772. app_data, export_data);
  773. }
  774. static keymaster_error_t begin(const keymaster1_device_t* dev, //
  775. keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
  776. const keymaster_key_param_set_t* in_params,
  777. keymaster_key_param_set_t* out_params,
  778. keymaster_operation_handle_t* operation_handle) {
  779. if (!all_digests_supported(in_params))
  780. return KM_ERROR_UNSUPPORTED_DIGEST;
  781. return unwrap(dev)->begin(unwrap(dev), purpose, key, in_params, out_params,
  782. operation_handle);
  783. }
  784. static keymaster_error_t update(const keymaster1_device_t* dev,
  785. keymaster_operation_handle_t operation_handle,
  786. const keymaster_key_param_set_t* in_params,
  787. const keymaster_blob_t* input, size_t* input_consumed,
  788. keymaster_key_param_set_t* out_params,
  789. keymaster_blob_t* output) {
  790. return unwrap(dev)->update(unwrap(dev), operation_handle, in_params, input, input_consumed,
  791. out_params, output);
  792. }
  793. static keymaster_error_t finish(const struct keymaster1_device* dev, //
  794. keymaster_operation_handle_t operation_handle,
  795. const keymaster_key_param_set_t* in_params,
  796. const keymaster_blob_t* signature,
  797. keymaster_key_param_set_t* out_params,
  798. keymaster_blob_t* output) {
  799. return unwrap(dev)->finish(unwrap(dev), operation_handle, in_params, signature, out_params,
  800. output);
  801. }
  802. static keymaster_error_t abort(const struct keymaster1_device* dev,
  803. keymaster_operation_handle_t operation_handle) {
  804. return unwrap(dev)->abort(unwrap(dev), operation_handle);
  805. }
  806. private:
  807. keymaster1_device_t device_;
  808. const keymaster1_device_t* wrapped_device_;
  809. hw_module_t new_module;
  810. string new_module_name;
  811. };
  812. keymaster1_device_t* make_device_sha256_only(keymaster1_device_t* device) {
  813. return (new Sha256OnlyWrapper(device))->keymaster_device();
  814. }
  815. } // namespace test
  816. } // namespace keymaster