blob: 526dc57bf32e8a392c09c7aff74a73ed08f69043 [file] [log] [blame]
Darren Krahn69a3dbc2015-09-22 16:21:04 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <cstdio>
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "base/command_line.h"
Darren Krahn251cb282015-09-28 08:51:18 -070021#include "base/files/file_util.h"
Darren Krahn69a3dbc2015-09-22 16:21:04 -070022#include "keymaster/authorization_set.h"
23#include "keystore/keystore_client_impl.h"
24
25using base::CommandLine;
26using keymaster::AuthorizationSet;
27using keymaster::AuthorizationSetBuilder;
28using keystore::KeystoreClient;
29
30namespace {
31
Darren Krahna9474ab2015-11-03 14:38:53 -080032struct TestCase {
33 std::string name;
34 bool required_for_brillo_pts;
35 AuthorizationSet parameters;
36};
37
Darren Krahn69a3dbc2015-09-22 16:21:04 -070038void PrintUsageAndExit() {
39 printf("Usage: keystore_client_v2 <command> [options]\n");
Darren Krahna9474ab2015-11-03 14:38:53 -080040 printf("Commands: brillo-platform-test [--prefix=<test_name_prefix>]\n"
41 " list-brillo-tests\n"
42 " add-entropy --input=<entropy>\n"
Darren Krahn69a3dbc2015-09-22 16:21:04 -070043 " generate --name=<key_name>\n"
44 " get-chars --name=<key_name>\n"
45 " export --name=<key_name>\n"
46 " delete --name=<key_name>\n"
47 " delete-all\n"
48 " exists --name=<key_name>\n"
49 " list [--prefix=<key_name_prefix>]\n"
Darren Krahn251cb282015-09-28 08:51:18 -070050 " sign-verify --name=<key_name>\n"
51 " [en|de]crypt --name=<key_name> --in=<file> --out=<file>\n");
Darren Krahn69a3dbc2015-09-22 16:21:04 -070052 exit(1);
53}
54
55std::unique_ptr<KeystoreClient> CreateKeystoreInstance() {
56 return std::unique_ptr<KeystoreClient>(new keystore::KeystoreClientImpl);
57}
58
Darren Krahna9474ab2015-11-03 14:38:53 -080059const char* StringifyTag(keymaster_tag_t tag) {
60 switch (tag) {
61 case KM_TAG_INVALID:
62 return "KM_TAG_INVALID";
63 case KM_TAG_PURPOSE:
64 return "KM_TAG_PURPOSE";
65 case KM_TAG_ALGORITHM:
66 return "KM_TAG_ALGORITHM";
67 case KM_TAG_KEY_SIZE:
68 return "KM_TAG_KEY_SIZE";
69 case KM_TAG_BLOCK_MODE:
70 return "KM_TAG_BLOCK_MODE";
71 case KM_TAG_DIGEST:
72 return "KM_TAG_DIGEST";
73 case KM_TAG_PADDING:
74 return "KM_TAG_PADDING";
75 case KM_TAG_CALLER_NONCE:
76 return "KM_TAG_CALLER_NONCE";
77 case KM_TAG_MIN_MAC_LENGTH:
78 return "KM_TAG_MIN_MAC_LENGTH";
79 case KM_TAG_RSA_PUBLIC_EXPONENT:
80 return "KM_TAG_RSA_PUBLIC_EXPONENT";
81 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
82 return "KM_TAG_BLOB_USAGE_REQUIREMENTS";
83 case KM_TAG_BOOTLOADER_ONLY:
84 return "KM_TAG_BOOTLOADER_ONLY";
85 case KM_TAG_ACTIVE_DATETIME:
86 return "KM_TAG_ACTIVE_DATETIME";
87 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
88 return "KM_TAG_ORIGINATION_EXPIRE_DATETIME";
89 case KM_TAG_USAGE_EXPIRE_DATETIME:
90 return "KM_TAG_USAGE_EXPIRE_DATETIME";
91 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
92 return "KM_TAG_MIN_SECONDS_BETWEEN_OPS";
93 case KM_TAG_MAX_USES_PER_BOOT:
94 return "KM_TAG_MAX_USES_PER_BOOT";
95 case KM_TAG_ALL_USERS:
96 return "KM_TAG_ALL_USERS";
97 case KM_TAG_USER_ID:
98 return "KM_TAG_USER_ID";
99 case KM_TAG_USER_SECURE_ID:
100 return "KM_TAG_USER_SECURE_ID";
101 case KM_TAG_NO_AUTH_REQUIRED:
102 return "KM_TAG_NO_AUTH_REQUIRED";
103 case KM_TAG_USER_AUTH_TYPE:
104 return "KM_TAG_USER_AUTH_TYPE";
105 case KM_TAG_AUTH_TIMEOUT:
106 return "KM_TAG_AUTH_TIMEOUT";
107 case KM_TAG_ALL_APPLICATIONS:
108 return "KM_TAG_ALL_APPLICATIONS";
109 case KM_TAG_APPLICATION_ID:
110 return "KM_TAG_APPLICATION_ID";
111 case KM_TAG_APPLICATION_DATA:
112 return "KM_TAG_APPLICATION_DATA";
113 case KM_TAG_CREATION_DATETIME:
114 return "KM_TAG_CREATION_DATETIME";
115 case KM_TAG_ORIGIN:
116 return "KM_TAG_ORIGIN";
117 case KM_TAG_ROLLBACK_RESISTANT:
118 return "KM_TAG_ROLLBACK_RESISTANT";
119 case KM_TAG_ROOT_OF_TRUST:
120 return "KM_TAG_ROOT_OF_TRUST";
121 case KM_TAG_ASSOCIATED_DATA:
122 return "KM_TAG_ASSOCIATED_DATA";
123 case KM_TAG_NONCE:
124 return "KM_TAG_NONCE";
125 case KM_TAG_AUTH_TOKEN:
126 return "KM_TAG_AUTH_TOKEN";
127 case KM_TAG_MAC_LENGTH:
128 return "KM_TAG_MAC_LENGTH";
Shawn Willden0ba9f6e2015-11-23 08:56:33 -0700129 case KM_TAG_KDF:
130 return "KM_TAG_KDF";
131 case KM_TAG_EC_CURVE:
132 return "KM_TAG_EC_CURVE";
133 case KM_TAG_ECIES_SINGLE_HASH_MODE:
134 return "KM_TAG_ECIES_SINGLE_HASH_MODE";
Darren Krahna9474ab2015-11-03 14:38:53 -0800135 }
136 return "<Unknown>";
137}
138
139void PrintTags(const AuthorizationSet& parameters) {
140 const keymaster_key_param_t* iter = nullptr;
141 for (iter = parameters.begin(); iter != parameters.end(); ++iter) {
142 printf(" %s\n", StringifyTag(iter->tag));
143 }
144}
145
146void PrintKeyCharacteristics(const AuthorizationSet& hardware_enforced_characteristics,
147 const AuthorizationSet& software_enforced_characteristics) {
148 printf("Hardware:\n");
149 PrintTags(hardware_enforced_characteristics);
150 printf("Software:\n");
151 PrintTags(software_enforced_characteristics);
152}
153
154bool TestKey(const std::string& name, bool required, const AuthorizationSet& parameters) {
155 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
156 AuthorizationSet hardware_enforced_characteristics;
157 AuthorizationSet software_enforced_characteristics;
158 int32_t result = keystore->generateKey("tmp", parameters, &hardware_enforced_characteristics,
159 &software_enforced_characteristics);
160 if (result != KM_ERROR_OK) {
161 LOG(ERROR) << "Failed to generate key: " << result;
162 printf("%s Result: ABORT\n", name.c_str());
163 return false;
164 }
165 result = keystore->deleteKey("tmp");
166 if (result != KM_ERROR_OK) {
167 LOG(ERROR) << "Failed to delete key: " << result;
168 printf("%s Result: ABORT\n", name.c_str());
169 return false;
170 }
171 printf("===============================================================\n");
172 printf("%s Key Characteristics:\n", name.c_str());
173 PrintKeyCharacteristics(hardware_enforced_characteristics, software_enforced_characteristics);
174 bool hardware_backed = (hardware_enforced_characteristics.size() > 0);
175 if (software_enforced_characteristics.GetTagCount(KM_TAG_PURPOSE) > 0 ||
176 software_enforced_characteristics.GetTagCount(KM_TAG_ALGORITHM) > 0 ||
177 software_enforced_characteristics.GetTagCount(KM_TAG_KEY_SIZE) > 0 ||
178 software_enforced_characteristics.GetTagCount(KM_TAG_RSA_PUBLIC_EXPONENT) > 0 ||
179 software_enforced_characteristics.GetTagCount(KM_TAG_DIGEST) > 0 ||
180 software_enforced_characteristics.GetTagCount(KM_TAG_PADDING) > 0 ||
181 software_enforced_characteristics.GetTagCount(KM_TAG_BLOCK_MODE) > 0) {
182 VLOG(1) << "Hardware-backed key but required characteristics enforced in software.";
183 hardware_backed = false;
184 }
185 const char kBoldRedFail[] = "\033[1;31mFAIL\033[0m";
186 const char kBoldGreenPass[] = "\033[1;32mPASS\033[0m";
187 const char kBoldYellowWarn[] = "\033[1;33mWARN\033[0m";
188 printf("[%s] %s\n",
189 hardware_backed ? kBoldGreenPass : (required ? kBoldRedFail : kBoldYellowWarn),
190 name.c_str());
191
192 return (hardware_backed || !required);
193}
194
195AuthorizationSet GetRSASignParameters(uint32_t key_size, bool sha256_only) {
196 AuthorizationSetBuilder parameters;
197 parameters.RsaSigningKey(key_size, 65537)
198 .Digest(KM_DIGEST_SHA_2_256)
199 .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN)
200 .Padding(KM_PAD_RSA_PSS)
201 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
202 if (!sha256_only) {
203 parameters.Digest(KM_DIGEST_SHA_2_224)
204 .Digest(KM_DIGEST_SHA_2_384)
205 .Digest(KM_DIGEST_SHA_2_512);
206 }
207 return parameters.build();
208}
209
210AuthorizationSet GetRSAEncryptParameters(uint32_t key_size) {
211 AuthorizationSetBuilder parameters;
212 parameters.RsaEncryptionKey(key_size, 65537)
213 .Padding(KM_PAD_RSA_PKCS1_1_5_ENCRYPT)
214 .Padding(KM_PAD_RSA_OAEP)
215 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
216 return parameters.build();
217}
218
219AuthorizationSet GetECDSAParameters(uint32_t key_size, bool sha256_only) {
220 AuthorizationSetBuilder parameters;
221 parameters.EcdsaSigningKey(key_size)
222 .Digest(KM_DIGEST_SHA_2_256)
223 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
224 if (!sha256_only) {
225 parameters.Digest(KM_DIGEST_SHA_2_224)
226 .Digest(KM_DIGEST_SHA_2_384)
227 .Digest(KM_DIGEST_SHA_2_512);
228 }
229 return parameters.build();
230}
231
232AuthorizationSet GetAESParameters(uint32_t key_size, bool with_gcm_mode) {
233 AuthorizationSetBuilder parameters;
234 parameters.AesEncryptionKey(key_size).Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
235 if (with_gcm_mode) {
236 parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
237 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, 128);
238 } else {
239 parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_ECB);
240 parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_CBC);
241 parameters.Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_CTR);
242 }
243 return parameters.build();
244}
245
246AuthorizationSet GetHMACParameters(uint32_t key_size, keymaster_digest_t digest) {
247 AuthorizationSetBuilder parameters;
248 parameters.HmacKey(key_size)
249 .Digest(digest)
250 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, 224)
251 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
252 return parameters.build();
253}
254
255std::vector<TestCase> GetTestCases() {
256 TestCase test_cases[] = {
257 {"RSA-2048 Sign", true, GetRSASignParameters(2048, true)},
258 {"RSA-2048 Sign (more digests)", false, GetRSASignParameters(2048, false)},
259 {"RSA-3072 Sign", false, GetRSASignParameters(3072, false)},
260 {"RSA-4096 Sign", false, GetRSASignParameters(4096, false)},
261 {"RSA-2048 Encrypt", true, GetRSAEncryptParameters(2048)},
262 {"RSA-3072 Encrypt", false, GetRSAEncryptParameters(3072)},
263 {"RSA-4096 Encrypt", false, GetRSAEncryptParameters(4096)},
264 {"ECDSA-P256 Sign", true, GetECDSAParameters(256, true)},
265 {"ECDSA-P256 Sign (more digests)", false, GetECDSAParameters(256, false)},
266 {"ECDSA-P224 Sign", false, GetECDSAParameters(224, false)},
267 {"ECDSA-P384 Sign", false, GetECDSAParameters(384, false)},
268 {"ECDSA-P521 Sign", false, GetECDSAParameters(521, false)},
269 {"AES-128", true, GetAESParameters(128, false)},
270 {"AES-256", true, GetAESParameters(256, false)},
271 {"AES-128-GCM", false, GetAESParameters(128, true)},
272 {"AES-256-GCM", false, GetAESParameters(256, true)},
273 {"HMAC-SHA256-16", true, GetHMACParameters(16, KM_DIGEST_SHA_2_256)},
274 {"HMAC-SHA256-32", true, GetHMACParameters(32, KM_DIGEST_SHA_2_256)},
275 {"HMAC-SHA256-64", false, GetHMACParameters(64, KM_DIGEST_SHA_2_256)},
276 {"HMAC-SHA224-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_224)},
277 {"HMAC-SHA384-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_384)},
278 {"HMAC-SHA512-32", false, GetHMACParameters(32, KM_DIGEST_SHA_2_512)},
279 };
280 return std::vector<TestCase>(&test_cases[0], &test_cases[arraysize(test_cases)]);
281}
282
283int BrilloPlatformTest(const std::string& prefix) {
284 int test_count = 0;
285 int fail_count = 0;
286 std::vector<TestCase> test_cases = GetTestCases();
287 for (const auto& test_case : test_cases) {
288 if (!prefix.empty() && test_case.name.find(prefix) != 0) {
289 continue;
290 }
291 ++test_count;
292 if (!TestKey(test_case.name, test_case.required_for_brillo_pts, test_case.parameters)) {
293 VLOG(1) << "Test failed: " << test_case.name;
294 ++fail_count;
295 }
296 }
297 return fail_count;
298}
299
300int ListTestCases() {
301 const char kBoldGreenRequired[] = "\033[1;32mREQUIRED\033[0m";
302 const char kBoldYellowRecommended[] = "\033[1;33mRECOMMENDED\033[0m";
303 std::vector<TestCase> test_cases = GetTestCases();
304 for (const auto& test_case : test_cases) {
305 printf("%s : %s\n", test_case.name.c_str(),
306 test_case.required_for_brillo_pts ? kBoldGreenRequired : kBoldYellowRecommended);
307 }
308 return 0;
309}
310
Darren Krahn251cb282015-09-28 08:51:18 -0700311std::string ReadFile(const std::string& filename) {
312 std::string content;
313 base::FilePath path(filename);
314 if (!base::ReadFileToString(path, &content)) {
315 printf("Failed to read file: %s\n", filename.c_str());
316 exit(1);
317 }
318 return content;
319}
320
321void WriteFile(const std::string& filename, const std::string& content) {
322 base::FilePath path(filename);
323 int size = content.size();
324 if (base::WriteFile(path, content.data(), size) != size) {
325 printf("Failed to write file: %s\n", filename.c_str());
326 exit(1);
327 }
328}
329
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700330int AddEntropy(const std::string& input) {
331 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
332 int32_t result = keystore->addRandomNumberGeneratorEntropy(input);
333 printf("AddEntropy: %d\n", result);
334 return result;
335}
336
337int GenerateKey(const std::string& name) {
338 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
339 AuthorizationSetBuilder params;
340 params.RsaSigningKey(2048, 65537)
341 .Digest(KM_DIGEST_SHA_2_224)
342 .Digest(KM_DIGEST_SHA_2_256)
343 .Digest(KM_DIGEST_SHA_2_384)
344 .Digest(KM_DIGEST_SHA_2_512)
345 .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN)
346 .Padding(KM_PAD_RSA_PSS)
347 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
348 AuthorizationSet hardware_enforced_characteristics;
349 AuthorizationSet software_enforced_characteristics;
350 int32_t result = keystore->generateKey(name, params.build(), &hardware_enforced_characteristics,
351 &software_enforced_characteristics);
Darren Krahna9474ab2015-11-03 14:38:53 -0800352 printf("GenerateKey: %d\n", result);
353 if (result == KM_ERROR_OK) {
354 PrintKeyCharacteristics(hardware_enforced_characteristics,
355 software_enforced_characteristics);
356 }
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700357 return result;
358}
359
360int GetCharacteristics(const std::string& name) {
361 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
362 AuthorizationSet hardware_enforced_characteristics;
363 AuthorizationSet software_enforced_characteristics;
364 int32_t result = keystore->getKeyCharacteristics(name, &hardware_enforced_characteristics,
365 &software_enforced_characteristics);
Darren Krahna9474ab2015-11-03 14:38:53 -0800366 printf("GetCharacteristics: %d\n", result);
367 if (result == KM_ERROR_OK) {
368 PrintKeyCharacteristics(hardware_enforced_characteristics,
369 software_enforced_characteristics);
370 }
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700371 return result;
372}
373
374int ExportKey(const std::string& name) {
375 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
376 std::string data;
377 int32_t result = keystore->exportKey(KM_KEY_FORMAT_X509, name, &data);
378 printf("ExportKey: %d (%zu)\n", result, data.size());
379 return result;
380}
381
382int DeleteKey(const std::string& name) {
383 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
384 int32_t result = keystore->deleteKey(name);
385 printf("DeleteKey: %d\n", result);
386 return result;
387}
388
389int DeleteAllKeys() {
390 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
391 int32_t result = keystore->deleteAllKeys();
392 printf("DeleteAllKeys: %d\n", result);
393 return result;
394}
395
396int DoesKeyExist(const std::string& name) {
397 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
398 printf("DoesKeyExist: %s\n", keystore->doesKeyExist(name) ? "yes" : "no");
399 return 0;
400}
401
402int List(const std::string& prefix) {
403 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
404 std::vector<std::string> key_list;
405 if (!keystore->listKeys(prefix, &key_list)) {
406 printf("ListKeys failed.\n");
407 return 1;
408 }
409 printf("Keys:\n");
410 for (const auto& key_name : key_list) {
411 printf(" %s\n", key_name.c_str());
412 }
413 return 0;
414}
415
416int SignAndVerify(const std::string& name) {
417 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
418 AuthorizationSetBuilder sign_params;
419 sign_params.Padding(KM_PAD_RSA_PKCS1_1_5_SIGN);
420 sign_params.Digest(KM_DIGEST_SHA_2_256);
421 AuthorizationSet output_params;
422 keymaster_operation_handle_t handle;
423 int32_t result = keystore->beginOperation(KM_PURPOSE_SIGN, name, sign_params.build(),
424 &output_params, &handle);
425 if (result != KM_ERROR_OK) {
426 printf("Sign: BeginOperation failed: %d\n", result);
427 return result;
428 }
429 AuthorizationSet empty_params;
430 size_t num_input_bytes_consumed;
431 std::string output_data;
432 result = keystore->updateOperation(handle, empty_params, "data_to_sign",
433 &num_input_bytes_consumed, &output_params, &output_data);
434 if (result != KM_ERROR_OK) {
435 printf("Sign: UpdateOperation failed: %d\n", result);
436 return result;
437 }
438 result = keystore->finishOperation(handle, empty_params, std::string() /*signature_to_verify*/,
439 &output_params, &output_data);
440 if (result != KM_ERROR_OK) {
441 printf("Sign: FinishOperation failed: %d\n", result);
442 return result;
443 }
444 printf("Sign: %zu bytes.\n", output_data.size());
445 // We have a signature, now verify it.
446 std::string signature_to_verify = output_data;
Darren Krahn251cb282015-09-28 08:51:18 -0700447 output_data.clear();
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700448 result = keystore->beginOperation(KM_PURPOSE_VERIFY, name, sign_params.build(), &output_params,
449 &handle);
450 if (result != KM_ERROR_OK) {
451 printf("Verify: BeginOperation failed: %d\n", result);
452 return result;
453 }
454 result = keystore->updateOperation(handle, empty_params, "data_to_sign",
455 &num_input_bytes_consumed, &output_params, &output_data);
456 if (result != KM_ERROR_OK) {
457 printf("Verify: UpdateOperation failed: %d\n", result);
458 return result;
459 }
460 result = keystore->finishOperation(handle, empty_params, signature_to_verify, &output_params,
461 &output_data);
462 if (result == KM_ERROR_VERIFICATION_FAILED) {
463 printf("Verify: Failed to verify signature.\n");
464 return result;
465 }
466 if (result != KM_ERROR_OK) {
467 printf("Verify: FinishOperation failed: %d\n", result);
468 return result;
469 }
470 printf("Verify: OK\n");
471 return 0;
472}
473
Darren Krahn251cb282015-09-28 08:51:18 -0700474int Encrypt(const std::string& key_name, const std::string& input_filename,
475 const std::string& output_filename) {
476 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
477 std::string input = ReadFile(input_filename);
478 std::string output;
479 if (!keystore->encryptWithAuthentication(key_name, input, &output)) {
480 printf("EncryptWithAuthentication failed.\n");
481 return 1;
482 }
483 WriteFile(output_filename, output);
484 return 0;
485}
486
487int Decrypt(const std::string& key_name, const std::string& input_filename,
488 const std::string& output_filename) {
489 std::unique_ptr<KeystoreClient> keystore = CreateKeystoreInstance();
490 std::string input = ReadFile(input_filename);
491 std::string output;
492 if (!keystore->decryptWithAuthentication(key_name, input, &output)) {
493 printf("DecryptWithAuthentication failed.\n");
494 return 1;
495 }
496 WriteFile(output_filename, output);
497 return 0;
498}
499
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700500} // namespace
501
502int main(int argc, char** argv) {
503 CommandLine::Init(argc, argv);
504 CommandLine* command_line = CommandLine::ForCurrentProcess();
505 CommandLine::StringVector args = command_line->GetArgs();
506 if (args.empty()) {
507 PrintUsageAndExit();
508 }
Darren Krahna9474ab2015-11-03 14:38:53 -0800509 if (args[0] == "brillo-platform-test") {
510 return BrilloPlatformTest(command_line->GetSwitchValueASCII("prefix"));
511 } else if (args[0] == "list-brillo-tests") {
512 return ListTestCases();
513 } else if (args[0] == "add-entropy") {
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700514 return AddEntropy(command_line->GetSwitchValueASCII("input"));
515 } else if (args[0] == "generate") {
516 return GenerateKey(command_line->GetSwitchValueASCII("name"));
517 } else if (args[0] == "get-chars") {
518 return GetCharacteristics(command_line->GetSwitchValueASCII("name"));
519 } else if (args[0] == "export") {
520 return ExportKey(command_line->GetSwitchValueASCII("name"));
521 } else if (args[0] == "delete") {
522 return DeleteKey(command_line->GetSwitchValueASCII("name"));
523 } else if (args[0] == "delete-all") {
524 return DeleteAllKeys();
525 } else if (args[0] == "exists") {
526 return DoesKeyExist(command_line->GetSwitchValueASCII("name"));
527 } else if (args[0] == "list") {
528 return List(command_line->GetSwitchValueASCII("prefix"));
529 } else if (args[0] == "sign-verify") {
530 return SignAndVerify(command_line->GetSwitchValueASCII("name"));
Darren Krahn251cb282015-09-28 08:51:18 -0700531 } else if (args[0] == "encrypt") {
532 return Encrypt(command_line->GetSwitchValueASCII("name"),
533 command_line->GetSwitchValueASCII("in"),
534 command_line->GetSwitchValueASCII("out"));
535 } else if (args[0] == "decrypt") {
536 return Decrypt(command_line->GetSwitchValueASCII("name"),
537 command_line->GetSwitchValueASCII("in"),
538 command_line->GetSwitchValueASCII("out"));
Darren Krahn69a3dbc2015-09-22 16:21:04 -0700539 } else {
540 PrintUsageAndExit();
541 }
542 return 0;
543}