blob: f6ee1d0d684c0a013604c5d382bf924258e31e24 [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
2 * Copyright (C) 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
Shawn Willden437fbd12014-08-20 11:59:49 -060017#include <string>
18#include <fstream>
19
Shawn Willden128ffe02014-08-06 12:31:33 -060020#include <gtest/gtest.h>
Shawn Willden76364712014-08-11 17:48:04 -060021
Shawn Willden128ffe02014-08-06 12:31:33 -060022#include <openssl/engine.h>
23
Shawn Willden98d9b922014-08-26 08:14:10 -060024#include <keymaster/google_keymaster_utils.h>
25#include <keymaster/keymaster_tags.h>
26
Shawn Willden76364712014-08-11 17:48:04 -060027#include "google_keymaster_test_utils.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060028#include "google_softkeymaster.h"
29
Shawn Willden437fbd12014-08-20 11:59:49 -060030using std::string;
31using std::ifstream;
32using std::istreambuf_iterator;
33
Shawn Willden128ffe02014-08-06 12:31:33 -060034int main(int argc, char** argv) {
35 ::testing::InitGoogleTest(&argc, argv);
36 int result = RUN_ALL_TESTS();
37 // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain.
38 CRYPTO_cleanup_all_ex_data();
Shawn Willden7c0a82b2014-09-17 12:57:32 -060039 ERR_remove_thread_state(NULL);
Shawn Willden128ffe02014-08-06 12:31:33 -060040 ERR_free_strings();
41 return result;
42}
43
44namespace keymaster {
45namespace test {
46
Shawn Willden6bbe6782014-09-18 11:26:15 -060047// Note that these DSA generator, p and q values must match the values from dsa_privkey_pk8.der.
48const uint8_t dsa_g[] = {
49 0x19, 0x1C, 0x71, 0xFD, 0xE0, 0x03, 0x0C, 0x43, 0xD9, 0x0B, 0xF6, 0xCD, 0xD6, 0xA9, 0x70, 0xE7,
50 0x37, 0x86, 0x3A, 0x78, 0xE9, 0xA7, 0x47, 0xA7, 0x47, 0x06, 0x88, 0xB1, 0xAF, 0xD7, 0xF3, 0xF1,
51 0xA1, 0xD7, 0x00, 0x61, 0x28, 0x88, 0x31, 0x48, 0x60, 0xD8, 0x11, 0xEF, 0xA5, 0x24, 0x1A, 0x81,
52 0xC4, 0x2A, 0xE2, 0xEA, 0x0E, 0x36, 0xD2, 0xD2, 0x05, 0x84, 0x37, 0xCF, 0x32, 0x7D, 0x09, 0xE6,
53 0x0F, 0x8B, 0x0C, 0xC8, 0xC2, 0xA4, 0xB1, 0xDC, 0x80, 0xCA, 0x68, 0xDF, 0xAF, 0xD2, 0x90, 0xC0,
54 0x37, 0x58, 0x54, 0x36, 0x8F, 0x49, 0xB8, 0x62, 0x75, 0x8B, 0x48, 0x47, 0xC0, 0xBE, 0xF7, 0x9A,
55 0x92, 0xA6, 0x68, 0x05, 0xDA, 0x9D, 0xAF, 0x72, 0x9A, 0x67, 0xB3, 0xB4, 0x14, 0x03, 0xAE, 0x4F,
56 0x4C, 0x76, 0xB9, 0xD8, 0x64, 0x0A, 0xBA, 0x3B, 0xA8, 0x00, 0x60, 0x4D, 0xAE, 0x81, 0xC3, 0xC5,
57};
58const uint8_t dsa_p[] = {
59 0xA3, 0xF3, 0xE9, 0xB6, 0x7E, 0x7D, 0x88, 0xF6, 0xB7, 0xE5, 0xF5, 0x1F, 0x3B, 0xEE, 0xAC, 0xD7,
60 0xAD, 0xBC, 0xC9, 0xD1, 0x5A, 0xF8, 0x88, 0xC4, 0xEF, 0x6E, 0x3D, 0x74, 0x19, 0x74, 0xE7, 0xD8,
61 0xE0, 0x26, 0x44, 0x19, 0x86, 0xAF, 0x19, 0xDB, 0x05, 0xE9, 0x3B, 0x8B, 0x58, 0x58, 0xDE, 0xE5,
62 0x4F, 0x48, 0x15, 0x01, 0xEA, 0xE6, 0x83, 0x52, 0xD7, 0xC1, 0x21, 0xDF, 0xB9, 0xB8, 0x07, 0x66,
63 0x50, 0xFB, 0x3A, 0x0C, 0xB3, 0x85, 0xEE, 0xBB, 0x04, 0x5F, 0xC2, 0x6D, 0x6D, 0x95, 0xFA, 0x11,
64 0x93, 0x1E, 0x59, 0x5B, 0xB1, 0x45, 0x8D, 0xE0, 0x3D, 0x73, 0xAA, 0xF2, 0x41, 0x14, 0x51, 0x07,
65 0x72, 0x3D, 0xA2, 0xF7, 0x58, 0xCD, 0x11, 0xA1, 0x32, 0xCF, 0xDA, 0x42, 0xB7, 0xCC, 0x32, 0x80,
66 0xDB, 0x87, 0x82, 0xEC, 0x42, 0xDB, 0x5A, 0x55, 0x24, 0x24, 0xA2, 0xD1, 0x55, 0x29, 0xAD, 0xEB,
67};
68const uint8_t dsa_q[] = {
69 0xEB, 0xEA, 0x17, 0xD2, 0x09, 0xB3, 0xD7, 0x21, 0x9A, 0x21,
70 0x07, 0x82, 0x8F, 0xAB, 0xFE, 0x88, 0x71, 0x68, 0xF7, 0xE3,
71};
72
Shawn Willden128ffe02014-08-06 12:31:33 -060073class KeymasterTest : public testing::Test {
74 protected:
Shawn Willden2f3be362014-08-25 11:31:39 -060075 KeymasterTest() : device(5, new StdoutLogger) { RAND_seed("foobar", 6); }
Shawn Willdenda8485e2014-08-17 08:00:01 -060076 ~KeymasterTest() {}
Shawn Willden128ffe02014-08-06 12:31:33 -060077
Shawn Willdend0772312014-09-18 12:27:57 -060078 template <typename T> void ExpectEmptyResponse(const SupportedResponse<T>& response) {
79 EXPECT_EQ(KM_ERROR_OK, response.error);
80 EXPECT_EQ(0U, response.results_length);
81 }
82
83 template <typename T> void ExpectResponseContains(T val, const SupportedResponse<T>& response) {
84 EXPECT_EQ(KM_ERROR_OK, response.error);
85 EXPECT_EQ(1U, response.results_length);
86 EXPECT_EQ(val, response.results[0]);
87 }
88
Shawn Willden128ffe02014-08-06 12:31:33 -060089 GoogleSoftKeymaster device;
90};
91
Shawn Willden128ffe02014-08-06 12:31:33 -060092typedef KeymasterTest CheckSupported;
93TEST_F(CheckSupported, SupportedAlgorithms) {
94 // Shouldn't blow up on NULL.
95 device.SupportedAlgorithms(NULL);
96
97 SupportedResponse<keymaster_algorithm_t> response;
98 device.SupportedAlgorithms(&response);
99 EXPECT_EQ(KM_ERROR_OK, response.error);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700100 EXPECT_EQ(4U, response.results_length);
Shawn Willden128ffe02014-08-06 12:31:33 -0600101 EXPECT_EQ(KM_ALGORITHM_RSA, response.results[0]);
Shawn Willden28e41472014-08-18 13:35:22 -0600102 EXPECT_EQ(KM_ALGORITHM_DSA, response.results[1]);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600103 EXPECT_EQ(KM_ALGORITHM_ECDSA, response.results[2]);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700104 EXPECT_EQ(KM_ALGORITHM_AES, response.results[3]);
Shawn Willden128ffe02014-08-06 12:31:33 -0600105}
106
107TEST_F(CheckSupported, SupportedBlockModes) {
108 // Shouldn't blow up on NULL.
Shawn Willden3809b932014-12-02 06:59:46 -0700109 device.SupportedBlockModes(KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, NULL);
Shawn Willden128ffe02014-08-06 12:31:33 -0600110
111 SupportedResponse<keymaster_block_mode_t> response;
Shawn Willden3809b932014-12-02 06:59:46 -0700112 device.SupportedBlockModes(KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, &response);
113 EXPECT_EQ(KM_ERROR_UNSUPPORTED_BLOCK_MODE, response.error);
Shawn Willden128ffe02014-08-06 12:31:33 -0600114
Shawn Willden3809b932014-12-02 06:59:46 -0700115 device.SupportedBlockModes(KM_ALGORITHM_DSA, KM_PURPOSE_ENCRYPT, &response);
116 EXPECT_EQ(KM_ERROR_UNSUPPORTED_BLOCK_MODE, response.error);
Shawn Willden28e41472014-08-18 13:35:22 -0600117
Shawn Willden3809b932014-12-02 06:59:46 -0700118 device.SupportedBlockModes(KM_ALGORITHM_ECDSA, KM_PURPOSE_ENCRYPT, &response);
119 EXPECT_EQ(KM_ERROR_UNSUPPORTED_BLOCK_MODE, response.error);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600120
Shawn Willden3809b932014-12-02 06:59:46 -0700121 device.SupportedBlockModes(KM_ALGORITHM_AES, KM_PURPOSE_ENCRYPT, &response);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700122 EXPECT_EQ(KM_ERROR_UNSUPPORTED_BLOCK_MODE, response.error);
Shawn Willden128ffe02014-08-06 12:31:33 -0600123}
124
125TEST_F(CheckSupported, SupportedPaddingModes) {
126 // Shouldn't blow up on NULL.
Shawn Willden3809b932014-12-02 06:59:46 -0700127 device.SupportedPaddingModes(KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, NULL);
Shawn Willden128ffe02014-08-06 12:31:33 -0600128
129 SupportedResponse<keymaster_padding_t> response;
Shawn Willden3809b932014-12-02 06:59:46 -0700130 device.SupportedPaddingModes(KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600131 ExpectResponseContains(KM_PAD_NONE, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600132
Shawn Willden3809b932014-12-02 06:59:46 -0700133 device.SupportedPaddingModes(KM_ALGORITHM_DSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600134 ExpectResponseContains(KM_PAD_NONE, response);
Shawn Willden28e41472014-08-18 13:35:22 -0600135
Shawn Willden3809b932014-12-02 06:59:46 -0700136 device.SupportedPaddingModes(KM_ALGORITHM_ECDSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600137 ExpectResponseContains(KM_PAD_NONE, response);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600138
Shawn Willden3809b932014-12-02 06:59:46 -0700139 device.SupportedPaddingModes(KM_ALGORITHM_AES, KM_PURPOSE_SIGN, &response);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700140 ExpectEmptyResponse(response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600141}
142
143TEST_F(CheckSupported, SupportedDigests) {
144 // Shouldn't blow up on NULL.
Shawn Willden3809b932014-12-02 06:59:46 -0700145 device.SupportedDigests(KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, NULL);
Shawn Willden128ffe02014-08-06 12:31:33 -0600146
147 SupportedResponse<keymaster_digest_t> response;
Shawn Willden3809b932014-12-02 06:59:46 -0700148 device.SupportedDigests(KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600149 ExpectResponseContains(KM_DIGEST_NONE, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600150
Shawn Willden3809b932014-12-02 06:59:46 -0700151 device.SupportedDigests(KM_ALGORITHM_DSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600152 ExpectResponseContains(KM_DIGEST_NONE, response);
Shawn Willden28e41472014-08-18 13:35:22 -0600153
Shawn Willden3809b932014-12-02 06:59:46 -0700154 device.SupportedDigests(KM_ALGORITHM_ECDSA, KM_PURPOSE_SIGN, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600155 ExpectResponseContains(KM_DIGEST_NONE, response);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600156
Shawn Willden3809b932014-12-02 06:59:46 -0700157 device.SupportedDigests(KM_ALGORITHM_AES, KM_PURPOSE_SIGN, &response);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700158 ExpectEmptyResponse(response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600159}
160
161TEST_F(CheckSupported, SupportedImportFormats) {
162 // Shouldn't blow up on NULL.
163 device.SupportedImportFormats(KM_ALGORITHM_RSA, NULL);
164
165 SupportedResponse<keymaster_key_format_t> response;
166 device.SupportedImportFormats(KM_ALGORITHM_RSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600167 ExpectResponseContains(KM_KEY_FORMAT_PKCS8, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600168
169 device.SupportedImportFormats(KM_ALGORITHM_DSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600170 ExpectResponseContains(KM_KEY_FORMAT_PKCS8, response);
Shawn Willden28e41472014-08-18 13:35:22 -0600171
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600172 device.SupportedImportFormats(KM_ALGORITHM_ECDSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600173 ExpectResponseContains(KM_KEY_FORMAT_PKCS8, response);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600174
Shawn Willden28e41472014-08-18 13:35:22 -0600175 device.SupportedImportFormats(KM_ALGORITHM_AES, &response);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700176 ExpectEmptyResponse(response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600177}
178
179TEST_F(CheckSupported, SupportedExportFormats) {
180 // Shouldn't blow up on NULL.
181 device.SupportedExportFormats(KM_ALGORITHM_RSA, NULL);
182
183 SupportedResponse<keymaster_key_format_t> response;
184 device.SupportedExportFormats(KM_ALGORITHM_RSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600185 ExpectResponseContains(KM_KEY_FORMAT_X509, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600186
187 device.SupportedExportFormats(KM_ALGORITHM_DSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600188 ExpectResponseContains(KM_KEY_FORMAT_X509, response);
Shawn Willden28e41472014-08-18 13:35:22 -0600189
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600190 device.SupportedExportFormats(KM_ALGORITHM_ECDSA, &response);
Shawn Willdend0772312014-09-18 12:27:57 -0600191 ExpectResponseContains(KM_KEY_FORMAT_X509, response);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600192
Shawn Willden28e41472014-08-18 13:35:22 -0600193 device.SupportedExportFormats(KM_ALGORITHM_AES, &response);
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700194 ExpectEmptyResponse(response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600195}
196
Shawn Willdend0772312014-09-18 12:27:57 -0600197keymaster_key_param_t key_generation_base_params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700198 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
199 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
Shawn Willdend0772312014-09-18 12:27:57 -0600200 Authorization(TAG_APPLICATION_ID, "app_id", 6),
Shawn Willden3809b932014-12-02 06:59:46 -0700201 Authorization(TAG_APPLICATION_DATA, "app_data", 8), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willdend0772312014-09-18 12:27:57 -0600202};
203
204class NewKeyGeneration : public KeymasterTest {
205 protected:
206 NewKeyGeneration() {
207 req_.key_description.Reinitialize(key_generation_base_params,
208 array_length(key_generation_base_params));
209 }
210
211 void CheckBaseParams(const GenerateKeyResponse& rsp) {
212 ASSERT_EQ(KM_ERROR_OK, rsp.error);
213 EXPECT_EQ(0U, rsp.enforced.size());
214 EXPECT_EQ(12U, rsp.enforced.SerializedSize());
215 EXPECT_GT(rsp.unenforced.SerializedSize(), 12U);
216
217 EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_SIGN));
218 EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_VERIFY));
219 EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_ID, 7));
220 EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_AUTH_ID, 8));
221 EXPECT_TRUE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 300));
222
223 // Verify that App ID, App data and ROT are NOT included.
224 EXPECT_FALSE(contains(rsp.unenforced, TAG_ROOT_OF_TRUST));
225 EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_ID));
226 EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_DATA));
227
228 // Just for giggles, check that some unexpected tags/values are NOT present.
229 EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_ENCRYPT));
230 EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_DECRYPT));
231 EXPECT_FALSE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 301));
232 EXPECT_FALSE(contains(rsp.unenforced, TAG_RESCOPE_AUTH_TIMEOUT));
233
234 // Now check that unspecified, defaulted tags are correct.
235 EXPECT_TRUE(contains(rsp.unenforced, TAG_ORIGIN, KM_ORIGIN_SOFTWARE));
236 EXPECT_TRUE(contains(rsp.unenforced, KM_TAG_CREATION_DATETIME));
237 }
238
239 GenerateKeyRequest req_;
240 GenerateKeyResponse rsp_;
241};
242
Shawn Willden128ffe02014-08-06 12:31:33 -0600243TEST_F(NewKeyGeneration, Rsa) {
Shawn Willdend0772312014-09-18 12:27:57 -0600244 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA));
245 req_.key_description.push_back(Authorization(TAG_KEY_SIZE, 256));
246 req_.key_description.push_back(Authorization(TAG_RSA_PUBLIC_EXPONENT, 3));
247 device.GenerateKey(req_, &rsp_);
Shawn Willden128ffe02014-08-06 12:31:33 -0600248
Shawn Willdend0772312014-09-18 12:27:57 -0600249 CheckBaseParams(rsp_);
Shawn Willden128ffe02014-08-06 12:31:33 -0600250
251 // Check specified tags are all present in unenforced characteristics
Shawn Willdend0772312014-09-18 12:27:57 -0600252 EXPECT_TRUE(contains(rsp_.unenforced, TAG_ALGORITHM, KM_ALGORITHM_RSA));
253 EXPECT_TRUE(contains(rsp_.unenforced, TAG_KEY_SIZE, 256));
254 EXPECT_TRUE(contains(rsp_.unenforced, TAG_RSA_PUBLIC_EXPONENT, 3));
Shawn Willden128ffe02014-08-06 12:31:33 -0600255}
256
Shawn Willden6bbe6782014-09-18 11:26:15 -0600257TEST_F(NewKeyGeneration, RsaDefaultSize) {
Shawn Willdend0772312014-09-18 12:27:57 -0600258 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA));
259 device.GenerateKey(req_, &rsp_);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600260
Shawn Willdend0772312014-09-18 12:27:57 -0600261 CheckBaseParams(rsp_);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600262
263 // Check specified tags are all present in unenforced characteristics
Shawn Willdend0772312014-09-18 12:27:57 -0600264 EXPECT_TRUE(contains(rsp_.unenforced, TAG_ALGORITHM, KM_ALGORITHM_RSA));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600265
266 // Now check that unspecified, defaulted tags are correct.
Shawn Willdend0772312014-09-18 12:27:57 -0600267 EXPECT_TRUE(contains(rsp_.unenforced, TAG_RSA_PUBLIC_EXPONENT, 65537));
268 EXPECT_TRUE(contains(rsp_.unenforced, TAG_KEY_SIZE, 2048));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600269}
270
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600271TEST_F(NewKeyGeneration, Ecdsa) {
Shawn Willdend0772312014-09-18 12:27:57 -0600272 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
Shawn Willden8c856c82014-09-26 09:34:36 -0600273 req_.key_description.push_back(Authorization(TAG_KEY_SIZE, 224));
Shawn Willdend0772312014-09-18 12:27:57 -0600274 device.GenerateKey(req_, &rsp_);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600275
Shawn Willdend0772312014-09-18 12:27:57 -0600276 CheckBaseParams(rsp_);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600277
278 // Check specified tags are all present in unenforced characteristics
Shawn Willdend0772312014-09-18 12:27:57 -0600279 EXPECT_TRUE(contains(rsp_.unenforced, TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
Shawn Willden8c856c82014-09-26 09:34:36 -0600280 EXPECT_TRUE(contains(rsp_.unenforced, TAG_KEY_SIZE, 224));
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600281}
282
Shawn Willden6bbe6782014-09-18 11:26:15 -0600283TEST_F(NewKeyGeneration, EcdsaDefaultSize) {
Shawn Willdend0772312014-09-18 12:27:57 -0600284 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
285 device.GenerateKey(req_, &rsp_);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600286
Shawn Willdend0772312014-09-18 12:27:57 -0600287 CheckBaseParams(rsp_);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600288
289 // Check specified tags are all present in unenforced characteristics
Shawn Willdend0772312014-09-18 12:27:57 -0600290 EXPECT_TRUE(contains(rsp_.unenforced, TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600291
292 // Now check that unspecified, defaulted tags are correct.
Shawn Willdend0772312014-09-18 12:27:57 -0600293 EXPECT_TRUE(contains(rsp_.unenforced, TAG_KEY_SIZE, 224));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600294}
295
296TEST_F(NewKeyGeneration, EcdsaInvalidSize) {
Shawn Willdend0772312014-09-18 12:27:57 -0600297 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
298 req_.key_description.push_back(Authorization(TAG_KEY_SIZE, 190));
299 device.GenerateKey(req_, &rsp_);
300 ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE, rsp_.error);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600301}
302
303TEST_F(NewKeyGeneration, EcdsaAllValidSizes) {
Shawn Willden8c856c82014-09-26 09:34:36 -0600304 size_t valid_sizes[] = {224, 256, 384, 521};
Shawn Willden6bbe6782014-09-18 11:26:15 -0600305 for (size_t size : valid_sizes) {
Shawn Willdena03c62b2014-12-03 15:33:39 -0700306 GenerateKeyResponse rsp;
Shawn Willdend0772312014-09-18 12:27:57 -0600307 req_.key_description.Reinitialize(key_generation_base_params,
308 array_length(key_generation_base_params));
309 req_.key_description.push_back(Authorization(TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
310 req_.key_description.push_back(Authorization(TAG_KEY_SIZE, size));
Shawn Willdena03c62b2014-12-03 15:33:39 -0700311 device.GenerateKey(req_, &rsp);
312 EXPECT_EQ(KM_ERROR_OK, rsp.error) << "Failed to generate size: " << size;
Shawn Willden6bbe6782014-09-18 11:26:15 -0600313 }
314}
315
Shawn Willdenb949a3f2014-12-04 13:11:07 -0700316TEST_F(NewKeyGeneration, AesOcb) {
317 keymaster_key_param_t params[] = {
318 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
319 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
320 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES), Authorization(TAG_KEY_SIZE, 128),
321 Authorization(TAG_BLOCK_MODE, KM_MODE_OCB), Authorization(TAG_CHUNK_LENGTH, 4096),
322 Authorization(TAG_PADDING, KM_PAD_NONE),
323 };
324 req_.key_description.Reinitialize(params, array_length(params));
325 device.GenerateKey(req_, &rsp_);
326 EXPECT_EQ(KM_ERROR_OK, rsp_.error);
327}
328
329TEST_F(NewKeyGeneration, AesOcbInvalidKeySize) {
330 keymaster_key_param_t params[] = {
331 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
332 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
333 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES), Authorization(TAG_KEY_SIZE, 129),
334 Authorization(TAG_BLOCK_MODE, KM_MODE_OCB), Authorization(TAG_CHUNK_LENGTH, 4096),
335 Authorization(TAG_PADDING, KM_PAD_NONE),
336 };
337 req_.key_description.Reinitialize(params, array_length(params));
338 device.GenerateKey(req_, &rsp_);
339 EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE, rsp_.error);
340}
341
342TEST_F(NewKeyGeneration, AesOcbAllValidSizes) {
343 keymaster_key_param_t params[] = {
344 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
345 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
346 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES),
347 Authorization(TAG_BLOCK_MODE, KM_MODE_OCB),
348 Authorization(TAG_CHUNK_LENGTH, 4096),
349 Authorization(TAG_PADDING, KM_PAD_NONE),
350 };
351
352 size_t valid_sizes[] = {128, 192, 256};
353 for (size_t size : valid_sizes) {
354 GenerateKeyResponse rsp;
355 req_.key_description.Reinitialize(params, array_length(params));
356 req_.key_description.push_back(Authorization(TAG_KEY_SIZE, size));
357 device.GenerateKey(req_, &rsp);
358 EXPECT_EQ(KM_ERROR_OK, rsp.error) << "Failed to generate size: " << size;
359 }
360}
361
362TEST_F(NewKeyGeneration, AesOcbNoChunkLength) {
363 keymaster_key_param_t params[] = {
364 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
365 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
366 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES), Authorization(TAG_KEY_SIZE, 128),
367 Authorization(TAG_BLOCK_MODE, KM_MODE_OCB), Authorization(TAG_PADDING, KM_PAD_NONE),
368 };
369 req_.key_description.Reinitialize(params, array_length(params));
370 device.GenerateKey(req_, &rsp_);
371 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, rsp_.error);
372}
373
374TEST_F(NewKeyGeneration, AesEcbUnsupported) {
375 keymaster_key_param_t params[] = {
376 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
377 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
378 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES), Authorization(TAG_KEY_SIZE, 128),
379 Authorization(TAG_BLOCK_MODE, KM_MODE_ECB), Authorization(TAG_PADDING, KM_PAD_NONE),
380 };
381 req_.key_description.Reinitialize(params, array_length(params));
382 device.GenerateKey(req_, &rsp_);
383 EXPECT_EQ(KM_ERROR_UNSUPPORTED_BLOCK_MODE, rsp_.error);
384}
385
386TEST_F(NewKeyGeneration, AesOcbPaddingUnsupported) {
387 keymaster_key_param_t params[] = {
388 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
389 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT),
390 Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES), Authorization(TAG_KEY_SIZE, 128),
391 Authorization(TAG_BLOCK_MODE, KM_MODE_OCB), Authorization(TAG_CHUNK_LENGTH, 4096),
392 Authorization(TAG_PADDING, KM_PAD_ZERO),
393 };
394 req_.key_description.Reinitialize(params, array_length(params));
395 device.GenerateKey(req_, &rsp_);
396 EXPECT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, rsp_.error);
397}
398
Shawn Willden76364712014-08-11 17:48:04 -0600399typedef KeymasterTest GetKeyCharacteristics;
400TEST_F(GetKeyCharacteristics, SimpleRsa) {
401 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700402 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
403 Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_KEY_SIZE, 256),
404 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
405 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden76364712014-08-11 17:48:04 -0600406 };
407
408 GenerateKeyRequest gen_req;
409 gen_req.key_description.Reinitialize(params, array_length(params));
410 GenerateKeyResponse gen_rsp;
411
412 device.GenerateKey(gen_req, &gen_rsp);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600413 ASSERT_EQ(KM_ERROR_OK, gen_rsp.error);
Shawn Willden76364712014-08-11 17:48:04 -0600414
415 GetKeyCharacteristicsRequest req;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600416 req.SetKeyMaterial(gen_rsp.key_blob);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600417 req.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6);
Shawn Willden76364712014-08-11 17:48:04 -0600418
419 GetKeyCharacteristicsResponse rsp;
420 device.GetKeyCharacteristics(req, &rsp);
421 ASSERT_EQ(KM_ERROR_OK, rsp.error);
422
423 EXPECT_EQ(gen_rsp.enforced, rsp.enforced);
424 EXPECT_EQ(gen_rsp.unenforced, rsp.unenforced);
425}
426
Shawn Willden61644f32014-08-18 13:43:14 -0600427/**
428 * Test class that provides some infrastructure for generating keys and signing messages.
429 */
Shawn Willden1615f2e2014-08-13 10:37:40 -0600430class SigningOperationsTest : public KeymasterTest {
431 protected:
Shawn Willden61644f32014-08-18 13:43:14 -0600432 void GenerateKey(keymaster_algorithm_t algorithm, keymaster_digest_t digest,
433 keymaster_padding_t padding, uint32_t key_size) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600434 keymaster_key_param_t params[] = {
435 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
Shawn Willden3809b932014-12-02 06:59:46 -0700436 Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), Authorization(TAG_ALGORITHM, algorithm),
437 Authorization(TAG_KEY_SIZE, key_size), Authorization(TAG_USER_ID, 7),
438 Authorization(TAG_USER_AUTH_ID, 8), Authorization(TAG_APPLICATION_ID, "app_id", 6),
Shawn Willden1615f2e2014-08-13 10:37:40 -0600439 Authorization(TAG_AUTH_TIMEOUT, 300),
440 };
441 GenerateKeyRequest generate_request;
442 generate_request.key_description.Reinitialize(params, array_length(params));
Shawn Willden43e999e2014-08-13 13:29:50 -0600443 if (static_cast<int>(digest) != -1)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600444 generate_request.key_description.push_back(TAG_DIGEST, digest);
Shawn Willden43e999e2014-08-13 13:29:50 -0600445 if (static_cast<int>(padding) != -1)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600446 generate_request.key_description.push_back(TAG_PADDING, padding);
447 device.GenerateKey(generate_request, &generate_response_);
448 EXPECT_EQ(KM_ERROR_OK, generate_response_.error);
Shawn Willden61644f32014-08-18 13:43:14 -0600449 }
Shawn Willden1615f2e2014-08-13 10:37:40 -0600450
Shawn Willden61644f32014-08-18 13:43:14 -0600451 void SignMessage(const void* message, size_t size) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600452 SignMessage(generate_response_.key_blob, message, size);
453 }
454
455 void SignMessage(const keymaster_key_blob_t& key_blob, const void* message, size_t size) {
Shawn Willden61644f32014-08-18 13:43:14 -0600456 BeginOperationRequest begin_request;
457 BeginOperationResponse begin_response;
Shawn Willden437fbd12014-08-20 11:59:49 -0600458 begin_request.SetKeyMaterial(key_blob);
Shawn Willden61644f32014-08-18 13:43:14 -0600459 begin_request.purpose = KM_PURPOSE_SIGN;
460 AddClientParams(&begin_request.additional_params);
461
462 device.BeginOperation(begin_request, &begin_response);
463 ASSERT_EQ(KM_ERROR_OK, begin_response.error);
464
465 UpdateOperationRequest update_request;
466 UpdateOperationResponse update_response;
467 update_request.op_handle = begin_response.op_handle;
468 update_request.input.Reinitialize(message, size);
469 EXPECT_EQ(size, update_request.input.available_read());
470
471 device.UpdateOperation(update_request, &update_response);
472 ASSERT_EQ(KM_ERROR_OK, update_response.error);
473 EXPECT_EQ(0U, update_response.output.available_read());
474
475 FinishOperationRequest finish_request;
476 finish_request.op_handle = begin_response.op_handle;
477 device.FinishOperation(finish_request, &finish_response_);
478 ASSERT_EQ(KM_ERROR_OK, finish_response_.error);
479 EXPECT_GT(finish_response_.output.available_read(), 0U);
480 }
481
482 void AddClientParams(AuthorizationSet* set) { set->push_back(TAG_APPLICATION_ID, "app_id", 6); }
483
484 const keymaster_key_blob_t& key_blob() { return generate_response_.key_blob; }
Shawn Willdenf268d742014-08-19 15:36:26 -0600485
486 const keymaster_key_blob_t& corrupt_key_blob() {
Shawn Willden2241bf02014-08-28 09:59:53 -0600487 uint8_t* tmp = const_cast<uint8_t*>(generate_response_.key_blob.key_material);
488 ++tmp[generate_response_.key_blob.key_material_size / 2];
Shawn Willdenf268d742014-08-19 15:36:26 -0600489 return generate_response_.key_blob;
490 }
491
Shawn Willden61644f32014-08-18 13:43:14 -0600492 Buffer* signature() {
493 if (finish_response_.error == KM_ERROR_OK)
494 return &finish_response_.output;
495 return NULL;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600496 }
497
Shawn Willden1615f2e2014-08-13 10:37:40 -0600498 GenerateKeyResponse generate_response_;
Shawn Willden61644f32014-08-18 13:43:14 -0600499 FinishOperationResponse finish_response_;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600500};
501
502TEST_F(SigningOperationsTest, RsaSuccess) {
Shawn Willden61644f32014-08-18 13:43:14 -0600503 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600504 const char message[] = "12345678901234567890123456789012";
Shawn Willdend05cba52014-09-26 09:58:12 -0600505 SignMessage(message, array_size(message) - 1);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600506}
507
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600508TEST_F(SigningOperationsTest, EcdsaSuccess) {
Shawn Willden8c856c82014-09-26 09:34:36 -0600509 GenerateKey(KM_ALGORITHM_ECDSA, KM_DIGEST_NONE, KM_PAD_NONE, 224 /* key size */);
Shawn Willdend05cba52014-09-26 09:58:12 -0600510 const char message[] = "123456789012345678901234567890123456789012345678";
511 SignMessage(message, array_size(message) - 1);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600512}
513
Shawn Willden1615f2e2014-08-13 10:37:40 -0600514TEST_F(SigningOperationsTest, RsaAbort) {
Shawn Willden61644f32014-08-18 13:43:14 -0600515 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600516
517 BeginOperationRequest begin_request;
518 BeginOperationResponse begin_response;
Shawn Willden61644f32014-08-18 13:43:14 -0600519 begin_request.SetKeyMaterial(key_blob());
Shawn Willden1615f2e2014-08-13 10:37:40 -0600520 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600521 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600522
523 device.BeginOperation(begin_request, &begin_response);
524 ASSERT_EQ(KM_ERROR_OK, begin_response.error);
525
526 EXPECT_EQ(KM_ERROR_OK, device.AbortOperation(begin_response.op_handle));
527
528 // Another abort should fail
529 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
530}
531
532TEST_F(SigningOperationsTest, RsaUnsupportedDigest) {
Shawn Willden61644f32014-08-18 13:43:14 -0600533 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_SHA_2_256, KM_PAD_NONE, 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600534
535 BeginOperationRequest begin_request;
536 BeginOperationResponse begin_response;
537 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600538 begin_request.SetKeyMaterial(key_blob());
539 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600540
541 device.BeginOperation(begin_request, &begin_response);
542 ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, begin_response.error);
543
544 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
545}
546
547TEST_F(SigningOperationsTest, RsaUnsupportedPadding) {
Shawn Willden61644f32014-08-18 13:43:14 -0600548 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_RSA_OAEP, 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600549
550 BeginOperationRequest begin_request;
551 BeginOperationResponse begin_response;
552 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600553 begin_request.SetKeyMaterial(key_blob());
554 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600555
556 device.BeginOperation(begin_request, &begin_response);
557 ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, begin_response.error);
558
559 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
560}
561
562TEST_F(SigningOperationsTest, RsaNoDigest) {
Shawn Willden61644f32014-08-18 13:43:14 -0600563 GenerateKey(KM_ALGORITHM_RSA, static_cast<keymaster_digest_t>(-1), KM_PAD_NONE,
564 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600565
566 BeginOperationRequest begin_request;
567 BeginOperationResponse begin_response;
568 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600569 begin_request.SetKeyMaterial(key_blob());
570 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600571
572 device.BeginOperation(begin_request, &begin_response);
573 ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, begin_response.error);
574
575 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
576}
577
578TEST_F(SigningOperationsTest, RsaNoPadding) {
Shawn Willden61644f32014-08-18 13:43:14 -0600579 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, static_cast<keymaster_padding_t>(-1),
580 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600581
582 BeginOperationRequest begin_request;
583 BeginOperationResponse begin_response;
584 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600585 begin_request.SetKeyMaterial(key_blob());
586 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600587
588 device.BeginOperation(begin_request, &begin_response);
589 ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, begin_response.error);
590
591 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
592}
593
594TEST_F(SigningOperationsTest, RsaTooShortMessage) {
Shawn Willden61644f32014-08-18 13:43:14 -0600595 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600596
597 BeginOperationRequest begin_request;
598 BeginOperationResponse begin_response;
Shawn Willden61644f32014-08-18 13:43:14 -0600599 begin_request.SetKeyMaterial(key_blob());
Shawn Willden1615f2e2014-08-13 10:37:40 -0600600 begin_request.purpose = KM_PURPOSE_SIGN;
Shawn Willden61644f32014-08-18 13:43:14 -0600601 AddClientParams(&begin_request.additional_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600602
603 device.BeginOperation(begin_request, &begin_response);
604 ASSERT_EQ(KM_ERROR_OK, begin_response.error);
605
606 UpdateOperationRequest update_request;
607 UpdateOperationResponse update_response;
608 update_request.op_handle = begin_response.op_handle;
609 update_request.input.Reinitialize("01234567890123456789012345678901", 31);
610 EXPECT_EQ(31U, update_request.input.available_read());
611
612 device.UpdateOperation(update_request, &update_response);
613 ASSERT_EQ(KM_ERROR_OK, update_response.error);
614 EXPECT_EQ(0U, update_response.output.available_read());
615
Shawn Willden43e999e2014-08-13 13:29:50 -0600616 FinishOperationRequest finish_request;
617 finish_request.op_handle = begin_response.op_handle;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600618 FinishOperationResponse finish_response;
Shawn Willden43e999e2014-08-13 13:29:50 -0600619 device.FinishOperation(finish_request, &finish_response);
Shawn Willden00aa7942014-09-10 07:57:43 -0600620 ASSERT_EQ(KM_ERROR_UNKNOWN_ERROR, finish_response.error);
Shawn Willden43e999e2014-08-13 13:29:50 -0600621 EXPECT_EQ(0U, finish_response.output.available_read());
622
623 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle));
624}
625
Shawn Willdend05cba52014-09-26 09:58:12 -0600626class VerificationOperationsTest : public SigningOperationsTest {
627 protected:
628 void VerifyMessage(const void* message, size_t message_len) {
629 VerifyMessage(generate_response_.key_blob, message, message_len);
630 }
631
632 void VerifyMessage(const keymaster_key_blob_t& key_blob, const void* message,
633 size_t message_len) {
634 ASSERT_TRUE(signature() != NULL);
635
636 BeginOperationRequest begin_request;
637 BeginOperationResponse begin_response;
638 begin_request.SetKeyMaterial(key_blob);
639 begin_request.purpose = KM_PURPOSE_VERIFY;
640 AddClientParams(&begin_request.additional_params);
641
642 device.BeginOperation(begin_request, &begin_response);
643 ASSERT_EQ(KM_ERROR_OK, begin_response.error);
644
645 UpdateOperationRequest update_request;
646 UpdateOperationResponse update_response;
647 update_request.op_handle = begin_response.op_handle;
648 update_request.input.Reinitialize(message, message_len);
649 EXPECT_EQ(message_len, update_request.input.available_read());
650
651 device.UpdateOperation(update_request, &update_response);
652 ASSERT_EQ(KM_ERROR_OK, update_response.error);
653 EXPECT_EQ(0U, update_response.output.available_read());
654
655 FinishOperationRequest finish_request;
656 finish_request.op_handle = begin_response.op_handle;
657 finish_request.signature.Reinitialize(*signature());
658 FinishOperationResponse finish_response;
659 device.FinishOperation(finish_request, &finish_response);
660 ASSERT_EQ(KM_ERROR_OK, finish_response.error);
661 EXPECT_EQ(0U, finish_response.output.available_read());
662
663 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE,
664 device.AbortOperation(begin_response.op_handle));
665 }
666};
667
Shawn Willden43e999e2014-08-13 13:29:50 -0600668TEST_F(VerificationOperationsTest, RsaSuccess) {
Shawn Willden61644f32014-08-18 13:43:14 -0600669 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */);
670 const char message[] = "12345678901234567890123456789012";
671 SignMessage(message, array_size(message) - 1);
Shawn Willdend05cba52014-09-26 09:58:12 -0600672 VerifyMessage(message, array_size(message) - 1);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600673}
674
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600675TEST_F(VerificationOperationsTest, EcdsaSuccess) {
Shawn Willden8c856c82014-09-26 09:34:36 -0600676 GenerateKey(KM_ALGORITHM_ECDSA, KM_DIGEST_NONE, KM_PAD_NONE, 224 /* key size */);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600677 const char message[] = "123456789012345678901234567890123456789012345678";
678 SignMessage(message, array_size(message) - 1);
Shawn Willdend05cba52014-09-26 09:58:12 -0600679 VerifyMessage(message, array_size(message) - 1);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600680}
681
Shawn Willdenffd790c2014-08-18 21:20:06 -0600682typedef SigningOperationsTest ExportKeyTest;
683TEST_F(ExportKeyTest, RsaSuccess) {
684 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600685
686 ExportKeyRequest request;
687 ExportKeyResponse response;
688 AddClientParams(&request.additional_params);
689 request.key_format = KM_KEY_FORMAT_X509;
690 request.SetKeyMaterial(key_blob());
691
692 device.ExportKey(request, &response);
693 ASSERT_EQ(KM_ERROR_OK, response.error);
694 EXPECT_TRUE(response.key_data != NULL);
Shawn Willdene46a43f2014-08-27 10:35:36 -0600695
696 // TODO(swillden): Verify that the exported key is actually usable to verify signatures.
Shawn Willdenffd790c2014-08-18 21:20:06 -0600697}
698
Shawn Willdenf268d742014-08-19 15:36:26 -0600699TEST_F(ExportKeyTest, EcdsaSuccess) {
Shawn Willden8c856c82014-09-26 09:34:36 -0600700 GenerateKey(KM_ALGORITHM_ECDSA, KM_DIGEST_NONE, KM_PAD_NONE, 224 /* key size */);
Shawn Willdenf268d742014-08-19 15:36:26 -0600701
702 ExportKeyRequest request;
703 ExportKeyResponse response;
704 AddClientParams(&request.additional_params);
705 request.key_format = KM_KEY_FORMAT_X509;
706 request.SetKeyMaterial(key_blob());
707
708 device.ExportKey(request, &response);
709 ASSERT_EQ(KM_ERROR_OK, response.error);
710 EXPECT_TRUE(response.key_data != NULL);
Shawn Willdene46a43f2014-08-27 10:35:36 -0600711
712 // TODO(swillden): Verify that the exported key is actually usable to verify signatures.
Shawn Willdenf268d742014-08-19 15:36:26 -0600713}
714
715TEST_F(ExportKeyTest, RsaUnsupportedKeyFormat) {
716 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256);
717
718 ExportKeyRequest request;
719 ExportKeyResponse response;
720 AddClientParams(&request.additional_params);
721
722 /* We have no other defined export formats defined. */
723 request.key_format = KM_KEY_FORMAT_PKCS8;
724 request.SetKeyMaterial(key_blob());
725
726 device.ExportKey(request, &response);
727 ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, response.error);
728 EXPECT_TRUE(response.key_data == NULL);
729}
730
731TEST_F(ExportKeyTest, RsaCorruptedKeyBlob) {
732 GenerateKey(KM_ALGORITHM_RSA, KM_DIGEST_NONE, KM_PAD_NONE, 256);
733
734 ExportKeyRequest request;
735 ExportKeyResponse response;
736 AddClientParams(&request.additional_params);
737 request.key_format = KM_KEY_FORMAT_X509;
738 request.SetKeyMaterial(corrupt_key_blob());
739
740 device.ExportKey(request, &response);
741 ASSERT_EQ(KM_ERROR_INVALID_KEY_BLOB, response.error);
742 ASSERT_TRUE(response.key_data == NULL);
743}
744
Shawn Willden437fbd12014-08-20 11:59:49 -0600745static string read_file(const string& file_name) {
746 ifstream file_stream(file_name, std::ios::binary);
747 istreambuf_iterator<char> file_begin(file_stream);
748 istreambuf_iterator<char> file_end;
749 return string(file_begin, file_end);
750}
751
Shawn Willdend05cba52014-09-26 09:58:12 -0600752typedef VerificationOperationsTest ImportKeyTest;
Shawn Willden437fbd12014-08-20 11:59:49 -0600753TEST_F(ImportKeyTest, RsaSuccess) {
754 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700755 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
756 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
757 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
758 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden437fbd12014-08-20 11:59:49 -0600759 };
760
Shawn Willden81effc62014-08-27 10:08:46 -0600761 string pk8_key = read_file("rsa_privkey_pk8.der");
Shawn Willden437fbd12014-08-20 11:59:49 -0600762 ASSERT_EQ(633U, pk8_key.size());
763
764 ImportKeyRequest import_request;
765 import_request.key_description.Reinitialize(params, array_length(params));
766 import_request.key_format = KM_KEY_FORMAT_PKCS8;
767 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
768
769 ImportKeyResponse import_response;
770 device.ImportKey(import_request, &import_response);
771 ASSERT_EQ(KM_ERROR_OK, import_response.error);
772 EXPECT_EQ(0U, import_response.enforced.size());
773 EXPECT_GT(import_response.unenforced.size(), 0U);
774
775 // Check values derived from the key.
776 EXPECT_TRUE(contains(import_response.unenforced, TAG_ALGORITHM, KM_ALGORITHM_RSA));
777 EXPECT_TRUE(contains(import_response.unenforced, TAG_KEY_SIZE, 1024));
778 EXPECT_TRUE(contains(import_response.unenforced, TAG_RSA_PUBLIC_EXPONENT, 65537U));
779
780 // And values provided by GoogleKeymaster
781 EXPECT_TRUE(contains(import_response.unenforced, TAG_ORIGIN, KM_ORIGIN_IMPORTED));
782 EXPECT_TRUE(contains(import_response.unenforced, KM_TAG_CREATION_DATETIME));
783
784 size_t message_len = 1024 / 8;
785 UniquePtr<uint8_t[]> message(new uint8_t[message_len]);
786 std::fill(message.get(), message.get() + message_len, 'a');
787 SignMessage(import_response.key_blob, message.get(), message_len);
788 ASSERT_TRUE(signature() != NULL);
Shawn Willdend05cba52014-09-26 09:58:12 -0600789 VerifyMessage(import_response.key_blob, message.get(), message_len);
Shawn Willden437fbd12014-08-20 11:59:49 -0600790}
791
Shawn Willden6bbe6782014-09-18 11:26:15 -0600792TEST_F(ImportKeyTest, RsaKeySizeMismatch) {
793 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700794 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
795 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
Shawn Willden6bbe6782014-09-18 11:26:15 -0600796 Authorization(TAG_KEY_SIZE, 2048), // Doesn't match key
Shawn Willden3809b932014-12-02 06:59:46 -0700797 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
798 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden6bbe6782014-09-18 11:26:15 -0600799 };
800
801 string pk8_key = read_file("rsa_privkey_pk8.der");
802 ASSERT_EQ(633U, pk8_key.size());
803
804 ImportKeyRequest import_request;
805 import_request.key_description.Reinitialize(params, array_length(params));
806 import_request.key_format = KM_KEY_FORMAT_PKCS8;
807 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
808
809 ImportKeyResponse import_response;
810 device.ImportKey(import_request, &import_response);
811 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, import_response.error);
812}
813
814TEST_F(ImportKeyTest, RsaPublicExponenMismatch) {
815 keymaster_key_param_t params[] = {
816 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
Shawn Willden3809b932014-12-02 06:59:46 -0700817 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
818 Authorization(TAG_RSA_PUBLIC_EXPONENT, 3), Authorization(TAG_USER_ID, 7),
819 Authorization(TAG_USER_AUTH_ID, 8), Authorization(TAG_APPLICATION_ID, "app_id", 6),
Shawn Willden6bbe6782014-09-18 11:26:15 -0600820 Authorization(TAG_AUTH_TIMEOUT, 300),
821 };
822
823 string pk8_key = read_file("rsa_privkey_pk8.der");
824 ASSERT_EQ(633U, pk8_key.size());
825
826 ImportKeyRequest import_request;
827 import_request.key_description.Reinitialize(params, array_length(params));
828 import_request.key_format = KM_KEY_FORMAT_PKCS8;
829 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
830
831 ImportKeyResponse import_response;
832 device.ImportKey(import_request, &import_response);
833 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, import_response.error);
834}
835
Shawn Willden81effc62014-08-27 10:08:46 -0600836TEST_F(ImportKeyTest, EcdsaSuccess) {
837 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700838 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
839 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
840 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
841 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden81effc62014-08-27 10:08:46 -0600842 };
843
844 string pk8_key = read_file("ec_privkey_pk8.der");
845 ASSERT_EQ(138U, pk8_key.size());
846
847 ImportKeyRequest import_request;
848 import_request.key_description.Reinitialize(params, array_length(params));
849 import_request.key_format = KM_KEY_FORMAT_PKCS8;
850 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
851
852 ImportKeyResponse import_response;
853 device.ImportKey(import_request, &import_response);
854 ASSERT_EQ(KM_ERROR_OK, import_response.error);
855 EXPECT_EQ(0U, import_response.enforced.size());
856 EXPECT_GT(import_response.unenforced.size(), 0U);
857
858 // Check values derived from the key.
859 EXPECT_TRUE(contains(import_response.unenforced, TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
860 EXPECT_TRUE(contains(import_response.unenforced, TAG_KEY_SIZE, 256));
861
862 // And values provided by GoogleKeymaster
863 EXPECT_TRUE(contains(import_response.unenforced, TAG_ORIGIN, KM_ORIGIN_IMPORTED));
864 EXPECT_TRUE(contains(import_response.unenforced, KM_TAG_CREATION_DATETIME));
865
866 size_t message_len = 1024 / 8;
867 UniquePtr<uint8_t[]> message(new uint8_t[message_len]);
868 std::fill(message.get(), message.get() + message_len, 'a');
869 SignMessage(import_response.key_blob, message.get(), message_len);
870 ASSERT_TRUE(signature() != NULL);
Shawn Willdend05cba52014-09-26 09:58:12 -0600871 VerifyMessage(import_response.key_blob, message.get(), message_len);
Shawn Willden81effc62014-08-27 10:08:46 -0600872}
873
Shawn Willden6bbe6782014-09-18 11:26:15 -0600874TEST_F(ImportKeyTest, EcdsaSizeSpecified) {
875 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700876 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
877 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
878 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
879 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden6bbe6782014-09-18 11:26:15 -0600880 Authorization(TAG_KEY_SIZE, 256),
881 };
882
883 string pk8_key = read_file("ec_privkey_pk8.der");
884 ASSERT_EQ(138U, pk8_key.size());
885
886 ImportKeyRequest import_request;
887 import_request.key_description.Reinitialize(params, array_length(params));
888 import_request.key_format = KM_KEY_FORMAT_PKCS8;
889 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
890
891 ImportKeyResponse import_response;
892 device.ImportKey(import_request, &import_response);
893 ASSERT_EQ(KM_ERROR_OK, import_response.error);
894 EXPECT_EQ(0U, import_response.enforced.size());
895 EXPECT_GT(import_response.unenforced.size(), 0U);
896
897 // Check values derived from the key.
898 EXPECT_TRUE(contains(import_response.unenforced, TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
899 EXPECT_TRUE(contains(import_response.unenforced, TAG_KEY_SIZE, 256));
900
901 // And values provided by GoogleKeymaster
902 EXPECT_TRUE(contains(import_response.unenforced, TAG_ORIGIN, KM_ORIGIN_IMPORTED));
903 EXPECT_TRUE(contains(import_response.unenforced, KM_TAG_CREATION_DATETIME));
904
905 size_t message_len = 1024 / 8;
906 UniquePtr<uint8_t[]> message(new uint8_t[message_len]);
907 std::fill(message.get(), message.get() + message_len, 'a');
908 SignMessage(import_response.key_blob, message.get(), message_len);
909 ASSERT_TRUE(signature() != NULL);
Shawn Willdend05cba52014-09-26 09:58:12 -0600910 VerifyMessage(import_response.key_blob, message.get(), message_len);
Shawn Willden6bbe6782014-09-18 11:26:15 -0600911}
912
913TEST_F(ImportKeyTest, EcdsaSizeMismatch) {
914 keymaster_key_param_t params[] = {
Shawn Willden3809b932014-12-02 06:59:46 -0700915 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
916 Authorization(TAG_DIGEST, KM_DIGEST_NONE), Authorization(TAG_PADDING, KM_PAD_NONE),
917 Authorization(TAG_USER_ID, 7), Authorization(TAG_USER_AUTH_ID, 8),
918 Authorization(TAG_APPLICATION_ID, "app_id", 6), Authorization(TAG_AUTH_TIMEOUT, 300),
Shawn Willden8c856c82014-09-26 09:34:36 -0600919 Authorization(TAG_KEY_SIZE, 224),
Shawn Willden6bbe6782014-09-18 11:26:15 -0600920 };
921
922 string pk8_key = read_file("ec_privkey_pk8.der");
923 ASSERT_EQ(138U, pk8_key.size());
924
925 ImportKeyRequest import_request;
926 import_request.key_description.Reinitialize(params, array_length(params));
927 import_request.key_format = KM_KEY_FORMAT_PKCS8;
928 import_request.SetKeyMaterial(pk8_key.data(), pk8_key.size());
929
930 ImportKeyResponse import_response;
931 device.ImportKey(import_request, &import_response);
932 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, import_response.error);
933}
934
Shawn Willden46a420d2014-12-02 07:01:21 -0700935/**
936 * Test class that provides some infrastructure for generating keys and encrypting messages.
937 */
938class EncryptionOperationsTest : public KeymasterTest {
939 protected:
940 void GenerateKey(keymaster_algorithm_t algorithm, keymaster_padding_t padding,
941 uint32_t key_size) {
942 keymaster_key_param_t params[] = {
943 Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT),
944 Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT), Authorization(TAG_ALGORITHM, algorithm),
945 Authorization(TAG_KEY_SIZE, key_size), Authorization(TAG_USER_ID, 7),
946 Authorization(TAG_USER_AUTH_ID, 8), Authorization(TAG_APPLICATION_ID, "app_id", 6),
947 Authorization(TAG_AUTH_TIMEOUT, 300),
948 };
949 GenerateKeyRequest generate_request;
950 generate_request.key_description.Reinitialize(params, array_length(params));
951 if (static_cast<int>(padding) != -1)
952 generate_request.key_description.push_back(TAG_PADDING, padding);
953 device.GenerateKey(generate_request, &generate_response_);
954 EXPECT_EQ(KM_ERROR_OK, generate_response_.error);
955 }
956
957 keymaster_error_t BeginOperation(keymaster_purpose_t purpose,
958 const keymaster_key_blob_t& key_blob, uint64_t* op_handle) {
959 BeginOperationRequest begin_request;
960 begin_request.SetKeyMaterial(key_blob);
961 begin_request.purpose = purpose;
962 AddClientParams(&begin_request.additional_params);
963
964 BeginOperationResponse begin_response;
965 device.BeginOperation(begin_request, &begin_response);
966 *op_handle = begin_response.op_handle;
967 return begin_response.error;
968 }
969
970 keymaster_error_t UpdateOperation(uint64_t op_handle, const void* message, size_t size,
971 string* output) {
972 UpdateOperationRequest update_request;
973 update_request.op_handle = op_handle;
974 update_request.input.Reinitialize(message, size);
975
976 UpdateOperationResponse update_response;
977 device.UpdateOperation(update_request, &update_response);
978 if (update_response.error == KM_ERROR_OK)
979 output->append(reinterpret_cast<const char*>(update_response.output.peek_read()),
980 update_response.output.available_read());
981 return update_response.error;
982 }
983
984 keymaster_error_t FinishOperation(uint64_t op_handle, string* output) {
985 FinishOperationRequest finish_request;
986 finish_request.op_handle = op_handle;
987 FinishOperationResponse finish_response;
988 device.FinishOperation(finish_request, &finish_response);
989 if (finish_response.error == KM_ERROR_OK)
990 output->append(reinterpret_cast<const char*>(finish_response.output.peek_read()),
991 finish_response.output.available_read());
992 return finish_response.error;
993 }
994
995 string ProcessMessage(keymaster_purpose_t purpose, const keymaster_key_blob_t& key_blob,
996 const void* message, size_t size) {
997 uint64_t op_handle;
998 EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, key_blob, &op_handle));
999
1000 string result;
1001 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, size, &result));
1002 EXPECT_EQ(KM_ERROR_OK, FinishOperation(op_handle, &result));
1003 return result;
1004 }
1005
1006 string EncryptMessage(const void* message, size_t size) {
1007 return ProcessMessage(KM_PURPOSE_ENCRYPT, generate_response_.key_blob, message, size);
1008 }
1009
1010 string DecryptMessage(const void* ciphertext, size_t size) {
1011 return ProcessMessage(KM_PURPOSE_DECRYPT, generate_response_.key_blob, ciphertext, size);
1012 }
1013
1014 void AddClientParams(AuthorizationSet* set) { set->push_back(TAG_APPLICATION_ID, "app_id", 6); }
1015
1016 const keymaster_key_blob_t& key_blob() { return generate_response_.key_blob; }
1017
1018 const keymaster_key_blob_t& corrupt_key_blob() {
1019 uint8_t* tmp = const_cast<uint8_t*>(generate_response_.key_blob.key_material);
1020 ++tmp[generate_response_.key_blob.key_material_size / 2];
1021 return generate_response_.key_blob;
1022 }
1023
1024 protected:
1025 GenerateKeyResponse generate_response_;
1026};
1027
1028TEST_F(EncryptionOperationsTest, RsaOaepSuccess) {
1029 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_OAEP, 512);
1030 const char message[] = "Hello World!";
1031 string ciphertext1 = EncryptMessage(message, strlen(message));
1032 EXPECT_EQ(512 / 8, ciphertext1.size());
1033
1034 string ciphertext2 = EncryptMessage(message, strlen(message));
1035 EXPECT_EQ(512 / 8, ciphertext2.size());
1036
1037 // OAEP randomizes padding so every result should be different.
1038 EXPECT_NE(ciphertext1, ciphertext2);
1039}
1040
1041TEST_F(EncryptionOperationsTest, RsaOaepRoundTrip) {
1042 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_OAEP, 512);
1043 const char message[] = "Hello World!";
1044 string ciphertext = EncryptMessage(message, strlen(message));
1045 EXPECT_EQ(512 / 8, ciphertext.size());
1046
1047 string plaintext = DecryptMessage(ciphertext.data(), ciphertext.size());
1048 EXPECT_EQ(message, plaintext);
1049}
1050
1051TEST_F(EncryptionOperationsTest, RsaOaepTooLarge) {
1052 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_OAEP, 512);
1053 const char message[] = "12345678901234567890123";
1054 uint64_t op_handle;
1055 string result;
1056
1057 EXPECT_EQ(KM_ERROR_OK,
1058 BeginOperation(KM_PURPOSE_ENCRYPT, generate_response_.key_blob, &op_handle));
1059 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, array_size(message), &result));
1060 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(op_handle, &result));
1061 EXPECT_EQ(0, result.size());
1062}
1063
1064TEST_F(EncryptionOperationsTest, RsaOaepCorruptedDecrypt) {
1065 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_OAEP, 512);
1066 const char message[] = "Hello World!";
1067 string ciphertext = EncryptMessage(message, strlen(message));
1068 EXPECT_EQ(512 / 8, ciphertext.size());
1069
1070 // Corrupt the ciphertext
1071 ciphertext[512 / 8 / 2]++;
1072
1073 uint64_t op_handle;
1074 string result;
1075 EXPECT_EQ(KM_ERROR_OK,
1076 BeginOperation(KM_PURPOSE_DECRYPT, generate_response_.key_blob, &op_handle));
1077 EXPECT_EQ(KM_ERROR_OK,
1078 UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result));
1079 EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(op_handle, &result));
1080 EXPECT_EQ(0, result.size());
1081}
1082
1083TEST_F(EncryptionOperationsTest, RsaPkcs1Success) {
1084 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_PKCS1_1_5_ENCRYPT, 512);
1085 const char message[] = "Hello World!";
1086 string ciphertext1 = EncryptMessage(message, strlen(message));
1087 EXPECT_EQ(512 / 8, ciphertext1.size());
1088
1089 string ciphertext2 = EncryptMessage(message, strlen(message));
1090 EXPECT_EQ(512 / 8, ciphertext2.size());
1091
1092 // PKCS1 v1.5 randomizes padding so every result should be different.
1093 EXPECT_NE(ciphertext1, ciphertext2);
1094}
1095
1096TEST_F(EncryptionOperationsTest, RsaPkcs1RoundTrip) {
1097 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_PKCS1_1_5_ENCRYPT, 512);
1098 const char message[] = "Hello World!";
1099 string ciphertext = EncryptMessage(message, strlen(message));
1100 EXPECT_EQ(512 / 8, ciphertext.size());
1101
1102 string plaintext = DecryptMessage(ciphertext.data(), ciphertext.size());
1103 EXPECT_EQ(message, plaintext);
1104}
1105
1106TEST_F(EncryptionOperationsTest, RsaPkcs1TooLarge) {
1107 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_PKCS1_1_5_ENCRYPT, 512);
1108 const char message[] = "1234567890123456789012345678901234567890123456789012";
1109 uint64_t op_handle;
1110 string result;
1111
1112 EXPECT_EQ(KM_ERROR_OK,
1113 BeginOperation(KM_PURPOSE_ENCRYPT, generate_response_.key_blob, &op_handle));
1114 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, array_size(message), &result));
1115 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(op_handle, &result));
1116 EXPECT_EQ(0, result.size());
1117}
1118
1119TEST_F(EncryptionOperationsTest, RsaPkcs1CorruptedDecrypt) {
1120 GenerateKey(KM_ALGORITHM_RSA, KM_PAD_RSA_PKCS1_1_5_ENCRYPT, 512);
1121 const char message[] = "Hello World!";
1122 string ciphertext = EncryptMessage(message, strlen(message));
1123 EXPECT_EQ(512 / 8, ciphertext.size());
1124
1125 // Corrupt the ciphertext
1126 ciphertext[512 / 8 / 2]++;
1127
1128 uint64_t op_handle;
1129 string result;
1130 EXPECT_EQ(KM_ERROR_OK,
1131 BeginOperation(KM_PURPOSE_DECRYPT, generate_response_.key_blob, &op_handle));
1132 EXPECT_EQ(KM_ERROR_OK,
1133 UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result));
1134 EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(op_handle, &result));
1135 EXPECT_EQ(0, result.size());
1136}
1137
Shawn Willdend7fe0432014-11-24 14:46:21 -07001138typedef KeymasterTest VersionTest;
1139TEST_F(VersionTest, GetVersion) {
1140 GetVersionRequest req;
1141 GetVersionResponse rsp;
1142 device.GetVersion(req, &rsp);
1143 EXPECT_EQ(KM_ERROR_OK, rsp.error);
1144 EXPECT_EQ(1, rsp.major_ver);
1145 EXPECT_EQ(0, rsp.minor_ver);
1146 EXPECT_EQ(0, rsp.subminor_ver);
1147}
1148
Shawn Willden128ffe02014-08-06 12:31:33 -06001149} // namespace test
1150} // namespace keymaster