blob: 985eb66237eca80cf0262b5c1ed546ca9b482a60 [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 Willden76076ab2014-12-18 08:36:35 -070017#include <algorithm>
Shawn Willden437fbd12014-08-20 11:59:49 -060018#include <fstream>
Shawn Willden76076ab2014-12-18 08:36:35 -070019#include <string>
20#include <vector>
Shawn Willden437fbd12014-08-20 11:59:49 -060021
Shawn Willden128ffe02014-08-06 12:31:33 -060022#include <gtest/gtest.h>
Shawn Willden76364712014-08-11 17:48:04 -060023
Shawn Willden128ffe02014-08-06 12:31:33 -060024#include <openssl/engine.h>
25
Shawn Willden98d9b922014-08-26 08:14:10 -060026#include <keymaster/google_keymaster_utils.h>
27#include <keymaster/keymaster_tags.h>
Shawn Willdenb7510332015-02-06 19:58:29 -070028#include <keymaster/soft_keymaster_device.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060029
Shawn Willden76364712014-08-11 17:48:04 -060030#include "google_keymaster_test_utils.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060031
Shawn Willden437fbd12014-08-20 11:59:49 -060032using std::ifstream;
33using std::istreambuf_iterator;
Shawn Willden76076ab2014-12-18 08:36:35 -070034using std::string;
35using std::vector;
Shawn Willden437fbd12014-08-20 11:59:49 -060036
Shawn Willden128ffe02014-08-06 12:31:33 -060037int main(int argc, char** argv) {
Shawn Willdenf0f68b92014-12-30 16:03:28 -070038 ERR_load_crypto_strings();
Shawn Willden128ffe02014-08-06 12:31:33 -060039 ::testing::InitGoogleTest(&argc, argv);
40 int result = RUN_ALL_TESTS();
41 // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain.
42 CRYPTO_cleanup_all_ex_data();
Shawn Willden7c0a82b2014-09-17 12:57:32 -060043 ERR_remove_thread_state(NULL);
Shawn Willden128ffe02014-08-06 12:31:33 -060044 ERR_free_strings();
45 return result;
46}
47
Shawn Willden63ac0432014-12-29 14:07:08 -070048template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
49 os << "{ ";
50 bool first = true;
51 for (T t : vec) {
52 os << (first ? "" : ", ") << t;
53 if (first)
54 first = false;
55 }
56 os << " }";
57 return os;
58}
59
Shawn Willden128ffe02014-08-06 12:31:33 -060060namespace keymaster {
61namespace test {
62
Shawn Willdenb15d77e2014-12-17 16:44:29 -070063/**
64 * Utility class to make construction of AuthorizationSets easy, and readable. Use like:
65 *
66 * ParamBuilder()
67 * .Option(TAG_ALGORITHM, KM_ALGORITHM_RSA)
68 * .Option(TAG_KEY_SIZE, 512)
69 * .Option(TAG_DIGEST, KM_DIGEST_NONE)
70 * .Option(TAG_PADDING, KM_PAD_NONE)
71 * .Option(TAG_SINGLE_USE_PER_BOOT, true)
72 * .build();
73 *
74 * In addition there are methods that add common sets of parameters, like RsaSigningKey().
75 */
76class ParamBuilder {
77 public:
78 template <typename TagType, typename ValueType>
79 ParamBuilder& Option(TagType tag, ValueType value) {
80 set.push_back(tag, value);
81 return *this;
82 }
83
84 ParamBuilder& RsaKey(uint32_t key_size = 0, uint64_t public_exponent = 0) {
85 Option(TAG_ALGORITHM, KM_ALGORITHM_RSA);
86 if (key_size != 0)
87 Option(TAG_KEY_SIZE, key_size);
88 if (public_exponent != 0)
89 Option(TAG_RSA_PUBLIC_EXPONENT, public_exponent);
90 return *this;
91 }
92
93 ParamBuilder& EcdsaKey(uint32_t key_size = 0) {
94 Option(TAG_ALGORITHM, KM_ALGORITHM_ECDSA);
95 if (key_size != 0)
96 Option(TAG_KEY_SIZE, key_size);
97 return *this;
98 }
99
100 ParamBuilder& AesKey(uint32_t key_size) {
101 Option(TAG_ALGORITHM, KM_ALGORITHM_AES);
102 return Option(TAG_KEY_SIZE, key_size);
103 }
104
105 ParamBuilder& HmacKey(uint32_t key_size, keymaster_digest_t digest, uint32_t mac_length) {
106 Option(TAG_ALGORITHM, KM_ALGORITHM_HMAC);
107 Option(TAG_KEY_SIZE, key_size);
108 SigningKey();
109 Option(TAG_DIGEST, digest);
110 return Option(TAG_MAC_LENGTH, mac_length);
111 }
112
113 ParamBuilder& RsaSigningKey(uint32_t key_size = 0, keymaster_digest_t digest = KM_DIGEST_NONE,
114 keymaster_padding_t padding = KM_PAD_NONE,
115 uint64_t public_exponent = 0) {
116 RsaKey(key_size, public_exponent);
117 SigningKey();
118 Option(TAG_DIGEST, digest);
119 return Option(TAG_PADDING, padding);
120 }
121
122 ParamBuilder& RsaEncryptionKey(uint32_t key_size = 0,
123 keymaster_padding_t padding = KM_PAD_RSA_OAEP,
124 uint64_t public_exponent = 0) {
125 RsaKey(key_size, public_exponent);
126 EncryptionKey();
127 return Option(TAG_PADDING, padding);
128 }
129
130 ParamBuilder& EcdsaSigningKey(uint32_t key_size = 0) {
131 EcdsaKey(key_size);
132 return SigningKey();
133 }
134
135 ParamBuilder& AesEncryptionKey(uint32_t key_size) {
136 AesKey(key_size);
137 return EncryptionKey();
138 }
139
140 ParamBuilder& SigningKey() {
141 Option(TAG_PURPOSE, KM_PURPOSE_SIGN);
142 return Option(TAG_PURPOSE, KM_PURPOSE_VERIFY);
143 }
144
145 ParamBuilder& EncryptionKey() {
146 Option(TAG_PURPOSE, KM_PURPOSE_ENCRYPT);
147 return Option(TAG_PURPOSE, KM_PURPOSE_DECRYPT);
148 }
149
150 ParamBuilder& NoDigestOrPadding() {
151 Option(TAG_DIGEST, KM_DIGEST_NONE);
152 return Option(TAG_PADDING, KM_PAD_NONE);
153 }
154
155 ParamBuilder& OcbMode(uint32_t chunk_length, uint32_t mac_length) {
156 Option(TAG_BLOCK_MODE, KM_MODE_OCB);
157 Option(TAG_CHUNK_LENGTH, chunk_length);
158 return Option(TAG_MAC_LENGTH, mac_length);
159 }
160
161 AuthorizationSet build() const { return set; }
162
163 private:
164 AuthorizationSet set;
Shawn Willden6bbe6782014-09-18 11:26:15 -0600165};
166
Shawn Willden567a4a02014-12-31 12:14:46 -0700167StdoutLogger logger;
168
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700169const uint64_t OP_HANDLE_SENTINEL = 0xFFFFFFFFFFFFFFFF;
Shawn Willden128ffe02014-08-06 12:31:33 -0600170class KeymasterTest : public testing::Test {
171 protected:
Shawn Willden567a4a02014-12-31 12:14:46 -0700172 KeymasterTest() : out_params_(NULL), op_handle_(OP_HANDLE_SENTINEL), characteristics_(NULL) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700173 blob_.key_material = NULL;
174 RAND_seed("foobar", 6);
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700175 blob_.key_material = 0;
Shawn Willden5b53c992015-02-02 08:05:25 -0700176 }
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700177
Shawn Willden5b53c992015-02-02 08:05:25 -0700178 ~KeymasterTest() {
179 FreeCharacteristics();
180 FreeKeyBlob();
Shawn Willdend0772312014-09-18 12:27:57 -0600181 }
182
Shawn Willden30255022015-02-24 14:00:21 -0700183 keymaster1_device_t* device() {
184 return reinterpret_cast<keymaster1_device_t*>(device_.hw_device());
185 }
Shawn Willden5b53c992015-02-02 08:05:25 -0700186
Shawn Willdenc24c1102014-12-22 15:30:09 -0700187 keymaster_error_t GenerateKey(const ParamBuilder& builder) {
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700188 AuthorizationSet params(builder.build());
189 params.push_back(UserAuthParams());
190 params.push_back(ClientParams());
191
Shawn Willden0d560bf2014-12-15 17:44:02 -0700192 FreeKeyBlob();
193 FreeCharacteristics();
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700194 return device()->generate_key(device(), params.data(), params.size(), &blob_,
195 &characteristics_);
Shawn Willden0d560bf2014-12-15 17:44:02 -0700196 }
197
Shawn Willdenc24c1102014-12-22 15:30:09 -0700198 keymaster_error_t ImportKey(const ParamBuilder& builder, keymaster_key_format_t format,
199 const string& key_material) {
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700200 AuthorizationSet params(builder.build());
201 params.push_back(UserAuthParams());
202 params.push_back(ClientParams());
203
204 FreeKeyBlob();
205 FreeCharacteristics();
206 return device()->import_key(device(), params.data(), params.size(), format,
207 reinterpret_cast<const uint8_t*>(key_material.c_str()),
208 key_material.length(), &blob_, &characteristics_);
209 }
210
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700211 AuthorizationSet UserAuthParams() {
212 AuthorizationSet set;
213 set.push_back(TAG_USER_ID, 7);
214 set.push_back(TAG_USER_AUTH_ID, 8);
215 set.push_back(TAG_AUTH_TIMEOUT, 300);
216 return set;
217 }
218
219 AuthorizationSet ClientParams() {
220 AuthorizationSet set;
221 set.push_back(TAG_APPLICATION_ID, "app_id", 6);
222 return set;
223 }
224
225 keymaster_error_t BeginOperation(keymaster_purpose_t purpose) {
226 return device()->begin(device(), purpose, &blob_, client_params_,
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700227 array_length(client_params_), &out_params_, &out_params_count_,
228 &op_handle_);
229 }
230
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700231 keymaster_error_t UpdateOperation(const string& message, string* output,
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700232 size_t* input_consumed) {
233 uint8_t* out_tmp = NULL;
234 size_t out_length;
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700235 EXPECT_NE(op_handle_, OP_HANDLE_SENTINEL);
236 keymaster_error_t error = device()->update(
Shawn Willden6bfbff02015-02-06 19:48:24 -0700237 device(), op_handle_, NULL /* additional_params */, 0 /* additional_params_count */,
238 reinterpret_cast<const uint8_t*>(message.c_str()), message.length(), input_consumed,
239 &out_tmp, &out_length);
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700240 if (out_tmp)
241 output->append(reinterpret_cast<char*>(out_tmp), out_length);
242 free(out_tmp);
243 return error;
244 }
245
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700246 keymaster_error_t FinishOperation(string* output) { return FinishOperation("", output); }
247
248 keymaster_error_t FinishOperation(const string& signature, string* output) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700249 uint8_t* out_tmp = NULL;
250 size_t out_length;
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700251 keymaster_error_t error = device()->finish(
Shawn Willden6bfbff02015-02-06 19:48:24 -0700252 device(), op_handle_, NULL /* additional_params */, 0 /* additional_params_count */,
253 reinterpret_cast<const uint8_t*>(signature.c_str()), signature.length(), &out_tmp,
254 &out_length);
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700255 if (out_tmp)
256 output->append(reinterpret_cast<char*>(out_tmp), out_length);
257 free(out_tmp);
258 return error;
259 }
260
Shawn Willden76076ab2014-12-18 08:36:35 -0700261 template <typename T>
262 bool ResponseContains(const vector<T>& expected, const T* values, size_t len) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700263 return expected.size() == len &&
264 std::is_permutation(values, values + len, expected.begin());
Shawn Willden76076ab2014-12-18 08:36:35 -0700265 }
266
267 template <typename T> bool ResponseContains(T expected, const T* values, size_t len) {
268 return (len == 1 && *values == expected);
Shawn Willdend0772312014-09-18 12:27:57 -0600269 }
270
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700271 keymaster_error_t AbortOperation() { return device()->abort(device(), op_handle_); }
272
273 string ProcessMessage(keymaster_purpose_t purpose, const string& message) {
274 EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose));
275
276 string result;
277 size_t input_consumed;
278 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
279 EXPECT_EQ(message.size(), input_consumed);
280 EXPECT_EQ(KM_ERROR_OK, FinishOperation(&result));
281 return result;
282 }
283
284 string ProcessMessage(keymaster_purpose_t purpose, const string& message,
285 const string& signature) {
286 EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose));
287
288 string result;
289 size_t input_consumed;
290 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
291 EXPECT_EQ(message.size(), input_consumed);
292 EXPECT_EQ(KM_ERROR_OK, FinishOperation(signature, &result));
293 return result;
294 }
295
296 void SignMessage(const string& message, string* signature) {
Shawn Willden63ac0432014-12-29 14:07:08 -0700297 SCOPED_TRACE("SignMessage");
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700298 *signature = ProcessMessage(KM_PURPOSE_SIGN, message);
299 EXPECT_GT(signature->size(), 0);
300 }
301
302 void VerifyMessage(const string& message, const string& signature) {
Shawn Willden63ac0432014-12-29 14:07:08 -0700303 SCOPED_TRACE("VerifyMessage");
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700304 ProcessMessage(KM_PURPOSE_VERIFY, message, signature);
305 }
306
307 string EncryptMessage(const string& message) {
Shawn Willden63ac0432014-12-29 14:07:08 -0700308 SCOPED_TRACE("EncryptMessage");
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700309 return ProcessMessage(KM_PURPOSE_ENCRYPT, message);
310 }
311
312 string DecryptMessage(const string& ciphertext) {
Shawn Willden63ac0432014-12-29 14:07:08 -0700313 SCOPED_TRACE("DecryptMessage");
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700314 return ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext);
315 }
316
317 keymaster_error_t GetCharacteristics() {
318 FreeCharacteristics();
319 return device()->get_key_characteristics(device(), &blob_, &client_id_, NULL /* app_data */,
320 &characteristics_);
321 }
322
323 keymaster_error_t ExportKey(keymaster_key_format_t format, string* export_data) {
324 uint8_t* export_data_tmp;
325 size_t export_data_length;
326
327 keymaster_error_t error =
328 device()->export_key(device(), format, &blob_, &client_id_, NULL /* app_data */,
329 &export_data_tmp, &export_data_length);
330
331 if (error != KM_ERROR_OK)
332 return error;
333
334 *export_data = string(reinterpret_cast<char*>(export_data_tmp), export_data_length);
335 free(export_data_tmp);
336 return error;
337 }
338
339 keymaster_error_t GetVersion(uint8_t* major, uint8_t* minor, uint8_t* subminor) {
340 GetVersionRequest request;
341 GetVersionResponse response;
342 device_.GetVersion(request, &response);
343 if (response.error != KM_ERROR_OK)
344 return response.error;
345 *major = response.major_ver;
346 *minor = response.minor_ver;
347 *subminor = response.subminor_ver;
348 return response.error;
349 }
350
351 AuthorizationSet hw_enforced() {
352 EXPECT_TRUE(characteristics_ != NULL);
353 return AuthorizationSet(characteristics_->hw_enforced);
354 }
355
356 AuthorizationSet sw_enforced() {
357 EXPECT_TRUE(characteristics_ != NULL);
358 return AuthorizationSet(characteristics_->sw_enforced);
359 }
360
Shawn Willden5b53c992015-02-02 08:05:25 -0700361 void FreeCharacteristics() {
362 keymaster_free_characteristics(characteristics_);
363 free(characteristics_);
364 characteristics_ = NULL;
365 }
366
367 void FreeKeyBlob() {
368 free(const_cast<uint8_t*>(blob_.key_material));
369 blob_.key_material = NULL;
370 }
371
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700372 void corrupt_key_blob() {
373 assert(blob_.key_material);
374 uint8_t* tmp = const_cast<uint8_t*>(blob_.key_material);
375 ++tmp[blob_.key_material_size / 2];
376 }
Shawn Willden5b53c992015-02-02 08:05:25 -0700377
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700378 private:
379 SoftKeymasterDevice device_;
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700380 keymaster_blob_t client_id_ = {.data = reinterpret_cast<const uint8_t*>("app_id"),
381 .data_length = 6};
382 keymaster_key_param_t client_params_[1] = {
383 Authorization(TAG_APPLICATION_ID, client_id_.data, client_id_.data_length)};
384
385 keymaster_key_param_t* out_params_;
386 size_t out_params_count_;
387 uint64_t op_handle_;
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700388
Shawn Willden5b53c992015-02-02 08:05:25 -0700389 keymaster_key_blob_t blob_;
390 keymaster_key_characteristics_t* characteristics_;
Shawn Willden128ffe02014-08-06 12:31:33 -0600391};
392
Shawn Willden128ffe02014-08-06 12:31:33 -0600393typedef KeymasterTest CheckSupported;
394TEST_F(CheckSupported, SupportedAlgorithms) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700395 EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL,
396 device()->get_supported_algorithms(device(), NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600397
Shawn Willden5b53c992015-02-02 08:05:25 -0700398 size_t len;
399 keymaster_algorithm_t* algorithms;
400 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_algorithms(device(), &algorithms, &len));
Shawn Willdena278f612014-12-23 11:22:21 -0700401 EXPECT_TRUE(ResponseContains(
402 {KM_ALGORITHM_RSA, KM_ALGORITHM_ECDSA, KM_ALGORITHM_AES, KM_ALGORITHM_HMAC}, algorithms,
403 len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700404 free(algorithms);
Shawn Willden128ffe02014-08-06 12:31:33 -0600405}
406
407TEST_F(CheckSupported, SupportedBlockModes) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700408 EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL,
409 device()->get_supported_block_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT,
410 NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600411
Shawn Willden5b53c992015-02-02 08:05:25 -0700412 size_t len;
413 keymaster_block_mode_t* modes;
Shawn Willden63ac0432014-12-29 14:07:08 -0700414 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_block_modes(device(), KM_ALGORITHM_RSA,
415 KM_PURPOSE_ENCRYPT, &modes, &len));
416 EXPECT_EQ(0, len);
417 free(modes);
Shawn Willden128ffe02014-08-06 12:31:33 -0600418
Shawn Willden76076ab2014-12-18 08:36:35 -0700419 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM,
Shawn Willden5b53c992015-02-02 08:05:25 -0700420 device()->get_supported_block_modes(device(), KM_ALGORITHM_DSA, KM_PURPOSE_ENCRYPT,
421 &modes, &len));
Shawn Willden28e41472014-08-18 13:35:22 -0600422
Shawn Willden63ac0432014-12-29 14:07:08 -0700423 EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE,
Shawn Willden5b53c992015-02-02 08:05:25 -0700424 device()->get_supported_block_modes(device(), KM_ALGORITHM_ECDSA, KM_PURPOSE_ENCRYPT,
425 &modes, &len));
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600426
Shawn Willden63ac0432014-12-29 14:07:08 -0700427 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_block_modes(device(), KM_ALGORITHM_AES,
428 KM_PURPOSE_ENCRYPT, &modes, &len));
Shawn Willden498e0aa2015-03-04 15:35:45 -0700429 EXPECT_TRUE(ResponseContains({KM_MODE_OCB, KM_MODE_ECB, KM_MODE_CBC}, modes, len));
Shawn Willden63ac0432014-12-29 14:07:08 -0700430 free(modes);
Shawn Willden128ffe02014-08-06 12:31:33 -0600431}
432
433TEST_F(CheckSupported, SupportedPaddingModes) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700434 EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL,
435 device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT,
436 NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600437
Shawn Willden5b53c992015-02-02 08:05:25 -0700438 size_t len;
439 keymaster_padding_t* modes;
440 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA,
441 KM_PURPOSE_SIGN, &modes, &len));
Shawn Willdenf90f2352014-12-18 23:01:15 -0700442 EXPECT_TRUE(
443 ResponseContains({KM_PAD_NONE, KM_PAD_RSA_PKCS1_1_5_SIGN, KM_PAD_RSA_PSS}, modes, len));
Shawn Willden63ac0432014-12-29 14:07:08 -0700444 free(modes);
445
446 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA,
447 KM_PURPOSE_ENCRYPT, &modes, &len));
448 EXPECT_TRUE(ResponseContains({KM_PAD_RSA_OAEP, KM_PAD_RSA_PKCS1_1_5_ENCRYPT}, modes, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700449 free(modes);
Shawn Willden128ffe02014-08-06 12:31:33 -0600450
Shawn Willden76076ab2014-12-18 08:36:35 -0700451 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM,
452 device()->get_supported_padding_modes(device(), KM_ALGORITHM_DSA, KM_PURPOSE_SIGN,
453 &modes, &len));
Shawn Willden28e41472014-08-18 13:35:22 -0600454
Shawn Willden5b53c992015-02-02 08:05:25 -0700455 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_ECDSA,
456 KM_PURPOSE_SIGN, &modes, &len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700457 EXPECT_EQ(0, len);
458 free(modes);
Shawn Willden63ac0432014-12-29 14:07:08 -0700459
460 EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE,
461 device()->get_supported_padding_modes(device(), KM_ALGORITHM_AES, KM_PURPOSE_SIGN,
462 &modes, &len));
Shawn Willden128ffe02014-08-06 12:31:33 -0600463}
464
465TEST_F(CheckSupported, SupportedDigests) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700466 EXPECT_EQ(
467 KM_ERROR_OUTPUT_PARAMETER_NULL,
468 device()->get_supported_digests(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600469
Shawn Willden5b53c992015-02-02 08:05:25 -0700470 size_t len;
471 keymaster_digest_t* digests;
472 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_RSA,
473 KM_PURPOSE_SIGN, &digests, &len));
Shawn Willden61902362014-12-18 10:33:24 -0700474 EXPECT_TRUE(ResponseContains({KM_DIGEST_NONE, KM_DIGEST_SHA_2_256}, digests, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700475 free(digests);
Shawn Willden128ffe02014-08-06 12:31:33 -0600476
Shawn Willden76076ab2014-12-18 08:36:35 -0700477 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM,
478 device()->get_supported_digests(device(), KM_ALGORITHM_DSA, KM_PURPOSE_SIGN, &digests,
479 &len));
Shawn Willden28e41472014-08-18 13:35:22 -0600480
Shawn Willden5b53c992015-02-02 08:05:25 -0700481 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_ECDSA,
482 KM_PURPOSE_SIGN, &digests, &len));
Shawn Willden63ac0432014-12-29 14:07:08 -0700483 EXPECT_EQ(0, len);
Shawn Willden5b53c992015-02-02 08:05:25 -0700484 free(digests);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600485
Shawn Willden63ac0432014-12-29 14:07:08 -0700486 EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE,
487 device()->get_supported_digests(device(), KM_ALGORITHM_AES, KM_PURPOSE_SIGN, &digests,
488 &len));
489
490 EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_HMAC,
Shawn Willden5b53c992015-02-02 08:05:25 -0700491 KM_PURPOSE_SIGN, &digests, &len));
Shawn Willden63ac0432014-12-29 14:07:08 -0700492 EXPECT_TRUE(ResponseContains({KM_DIGEST_SHA_2_224, KM_DIGEST_SHA_2_256, KM_DIGEST_SHA_2_384,
493 KM_DIGEST_SHA_2_512, KM_DIGEST_SHA1},
494 digests, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700495 free(digests);
Shawn Willden128ffe02014-08-06 12:31:33 -0600496}
497
498TEST_F(CheckSupported, SupportedImportFormats) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700499 EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL,
500 device()->get_supported_import_formats(device(), KM_ALGORITHM_RSA, NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600501
Shawn Willden5b53c992015-02-02 08:05:25 -0700502 size_t len;
503 keymaster_key_format_t* formats;
504 EXPECT_EQ(KM_ERROR_OK,
505 device()->get_supported_import_formats(device(), KM_ALGORITHM_RSA, &formats, &len));
Shawn Willden76076ab2014-12-18 08:36:35 -0700506 EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_PKCS8, formats, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700507 free(formats);
Shawn Willden7dad93b2015-02-05 10:20:47 -0700508
509 EXPECT_EQ(KM_ERROR_OK,
510 device()->get_supported_import_formats(device(), KM_ALGORITHM_AES, &formats, &len));
511 EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_RAW, formats, len));
512 free(formats);
513
514 EXPECT_EQ(KM_ERROR_OK,
515 device()->get_supported_import_formats(device(), KM_ALGORITHM_HMAC, &formats, &len));
516 EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_RAW, formats, len));
517 free(formats);
Shawn Willden128ffe02014-08-06 12:31:33 -0600518}
519
520TEST_F(CheckSupported, SupportedExportFormats) {
Shawn Willden5b53c992015-02-02 08:05:25 -0700521 EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL,
522 device()->get_supported_export_formats(device(), KM_ALGORITHM_RSA, NULL, NULL));
Shawn Willden128ffe02014-08-06 12:31:33 -0600523
Shawn Willden5b53c992015-02-02 08:05:25 -0700524 size_t len;
525 keymaster_key_format_t* formats;
526 EXPECT_EQ(KM_ERROR_OK,
527 device()->get_supported_export_formats(device(), KM_ALGORITHM_RSA, &formats, &len));
Shawn Willden76076ab2014-12-18 08:36:35 -0700528 EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_X509, formats, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700529 free(formats);
Shawn Willden128ffe02014-08-06 12:31:33 -0600530
Shawn Willden76076ab2014-12-18 08:36:35 -0700531 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM,
Shawn Willden5b53c992015-02-02 08:05:25 -0700532 device()->get_supported_export_formats(device(), KM_ALGORITHM_DSA, &formats, &len));
Shawn Willden28e41472014-08-18 13:35:22 -0600533
Shawn Willden5b53c992015-02-02 08:05:25 -0700534 EXPECT_EQ(KM_ERROR_OK,
535 device()->get_supported_export_formats(device(), KM_ALGORITHM_ECDSA, &formats, &len));
Shawn Willden76076ab2014-12-18 08:36:35 -0700536 EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_X509, formats, len));
Shawn Willden5b53c992015-02-02 08:05:25 -0700537 free(formats);
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600538
Shawn Willden5b53c992015-02-02 08:05:25 -0700539 EXPECT_EQ(KM_ERROR_OK,
540 device()->get_supported_export_formats(device(), KM_ALGORITHM_AES, &formats, &len));
541 EXPECT_EQ(0, len);
542 free(formats);
Shawn Willden7dad93b2015-02-05 10:20:47 -0700543
544 EXPECT_EQ(KM_ERROR_OK,
545 device()->get_supported_export_formats(device(), KM_ALGORITHM_AES, &formats, &len));
546 EXPECT_EQ(0, len);
547 free(formats);
548
549 EXPECT_EQ(KM_ERROR_OK,
550 device()->get_supported_export_formats(device(), KM_ALGORITHM_HMAC, &formats, &len));
551 EXPECT_EQ(0, len);
552 free(formats);
Shawn Willden128ffe02014-08-06 12:31:33 -0600553}
554
Shawn Willdend0772312014-09-18 12:27:57 -0600555class NewKeyGeneration : public KeymasterTest {
556 protected:
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700557 void CheckBaseParams() {
558 EXPECT_EQ(0U, hw_enforced().size());
559 EXPECT_EQ(12U, hw_enforced().SerializedSize());
Shawn Willdend0772312014-09-18 12:27:57 -0600560
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700561 AuthorizationSet auths = sw_enforced();
562 EXPECT_GT(auths.SerializedSize(), 12U);
563
Shawn Willden5b53c992015-02-02 08:05:25 -0700564 EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_SIGN));
565 EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_VERIFY));
566 EXPECT_TRUE(contains(auths, TAG_USER_ID, 7));
567 EXPECT_TRUE(contains(auths, TAG_USER_AUTH_ID, 8));
568 EXPECT_TRUE(contains(auths, TAG_AUTH_TIMEOUT, 300));
Shawn Willdend0772312014-09-18 12:27:57 -0600569
570 // Verify that App ID, App data and ROT are NOT included.
Shawn Willden5b53c992015-02-02 08:05:25 -0700571 EXPECT_FALSE(contains(auths, TAG_ROOT_OF_TRUST));
572 EXPECT_FALSE(contains(auths, TAG_APPLICATION_ID));
573 EXPECT_FALSE(contains(auths, TAG_APPLICATION_DATA));
Shawn Willdend0772312014-09-18 12:27:57 -0600574
575 // Just for giggles, check that some unexpected tags/values are NOT present.
Shawn Willden5b53c992015-02-02 08:05:25 -0700576 EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_ENCRYPT));
577 EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_DECRYPT));
578 EXPECT_FALSE(contains(auths, TAG_AUTH_TIMEOUT, 301));
Shawn Willdend0772312014-09-18 12:27:57 -0600579
580 // Now check that unspecified, defaulted tags are correct.
Shawn Willden5b53c992015-02-02 08:05:25 -0700581 EXPECT_TRUE(contains(auths, TAG_ORIGIN, KM_ORIGIN_SOFTWARE));
582 EXPECT_TRUE(contains(auths, KM_TAG_CREATION_DATETIME));
Shawn Willdend0772312014-09-18 12:27:57 -0600583 }
Shawn Willden2079ae82015-01-22 13:42:31 -0700584};
585
Shawn Willden128ffe02014-08-06 12:31:33 -0600586TEST_F(NewKeyGeneration, Rsa) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700587 ASSERT_EQ(KM_ERROR_OK,
588 GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_NONE, KM_PAD_NONE, 3)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700589 CheckBaseParams();
Shawn Willden128ffe02014-08-06 12:31:33 -0600590
Shawn Willden5b53c992015-02-02 08:05:25 -0700591 // Check specified tags are all present in auths
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700592 AuthorizationSet auths(sw_enforced());
Shawn Willden5b53c992015-02-02 08:05:25 -0700593 EXPECT_TRUE(contains(auths, TAG_ALGORITHM, KM_ALGORITHM_RSA));
594 EXPECT_TRUE(contains(auths, TAG_KEY_SIZE, 256));
595 EXPECT_TRUE(contains(auths, TAG_RSA_PUBLIC_EXPONENT, 3));
Shawn Willden128ffe02014-08-06 12:31:33 -0600596}
597
Shawn Willden6bbe6782014-09-18 11:26:15 -0600598TEST_F(NewKeyGeneration, RsaDefaultSize) {
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700599 // TODO(swillden): Remove support for defaulting RSA parameter size and pub exponent.
Shawn Willdenc24c1102014-12-22 15:30:09 -0700600 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey()));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700601 CheckBaseParams();
Shawn Willden6bbe6782014-09-18 11:26:15 -0600602
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700603 // Check specified tags are all present in unenforced characteristics
604 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_RSA));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600605
606 // Now check that unspecified, defaulted tags are correct.
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700607 EXPECT_TRUE(contains(sw_enforced(), TAG_RSA_PUBLIC_EXPONENT, 65537));
608 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 2048));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600609}
610
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600611TEST_F(NewKeyGeneration, Ecdsa) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700612 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey(224)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700613 CheckBaseParams();
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600614
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700615 // Check specified tags are all present in unenforced characteristics
616 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
617 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 224));
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600618}
619
Shawn Willden6bbe6782014-09-18 11:26:15 -0600620TEST_F(NewKeyGeneration, EcdsaDefaultSize) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700621 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey()));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700622 CheckBaseParams();
Shawn Willden6bbe6782014-09-18 11:26:15 -0600623
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700624 // Check specified tags are all present in unenforced characteristics
625 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600626
627 // Now check that unspecified, defaulted tags are correct.
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700628 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 224));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600629}
630
631TEST_F(NewKeyGeneration, EcdsaInvalidSize) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700632 ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE, GenerateKey(ParamBuilder().EcdsaSigningKey(190)));
Shawn Willden6bbe6782014-09-18 11:26:15 -0600633}
634
635TEST_F(NewKeyGeneration, EcdsaAllValidSizes) {
Shawn Willden8c856c82014-09-26 09:34:36 -0600636 size_t valid_sizes[] = {224, 256, 384, 521};
Shawn Willden6bbe6782014-09-18 11:26:15 -0600637 for (size_t size : valid_sizes) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700638 EXPECT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey(size)))
Shawn Willden5b53c992015-02-02 08:05:25 -0700639 << "Failed to generate size: " << size;
Shawn Willden6bbe6782014-09-18 11:26:15 -0600640 }
641}
642
Shawn Willden19fca882015-01-22 16:35:30 -0700643TEST_F(NewKeyGeneration, AesOcb) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700644 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willden19fca882015-01-22 16:35:30 -0700645}
646
647TEST_F(NewKeyGeneration, AesOcbInvalidKeySize) {
Shawn Willden7dad93b2015-02-05 10:20:47 -0700648 ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE,
649 GenerateKey(ParamBuilder().AesEncryptionKey(136).OcbMode(4096, 16)));
Shawn Willden19fca882015-01-22 16:35:30 -0700650}
651
652TEST_F(NewKeyGeneration, AesOcbAllValidSizes) {
Shawn Willden19fca882015-01-22 16:35:30 -0700653 size_t valid_sizes[] = {128, 192, 256};
654 for (size_t size : valid_sizes) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700655 EXPECT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(size)))
Shawn Willden5b53c992015-02-02 08:05:25 -0700656 << "Failed to generate size: " << size;
Shawn Willden19fca882015-01-22 16:35:30 -0700657 }
658}
659
Shawn Willden0d560bf2014-12-15 17:44:02 -0700660TEST_F(NewKeyGeneration, HmacSha256) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700661 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_256, 16)));
Shawn Willden0d560bf2014-12-15 17:44:02 -0700662}
663
Shawn Willden76364712014-08-11 17:48:04 -0600664typedef KeymasterTest GetKeyCharacteristics;
665TEST_F(GetKeyCharacteristics, SimpleRsa) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700666 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700667 AuthorizationSet original(sw_enforced());
Shawn Willden76364712014-08-11 17:48:04 -0600668
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700669 ASSERT_EQ(KM_ERROR_OK, GetCharacteristics());
670 EXPECT_EQ(original, sw_enforced());
Shawn Willden76364712014-08-11 17:48:04 -0600671}
672
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700673typedef KeymasterTest SigningOperationsTest;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600674TEST_F(SigningOperationsTest, RsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700675 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700676 string message = "12345678901234567890123456789012";
677 string signature;
678 SignMessage(message, &signature);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600679}
680
Shawn Willden61902362014-12-18 10:33:24 -0700681TEST_F(SigningOperationsTest, RsaSha256DigestSuccess) {
682 // Note that without padding, key size must exactly match digest size.
Shawn Willdenf90f2352014-12-18 23:01:15 -0700683 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_SHA_2_256)));
684 string message(1024, 'a');
685 string signature;
686 SignMessage(message, &signature);
687}
688
689TEST_F(SigningOperationsTest, RsaPssSha256Success) {
690 ASSERT_EQ(KM_ERROR_OK,
691 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PSS)));
Shawn Willden61902362014-12-18 10:33:24 -0700692 // Use large message, which won't work without digesting.
693 string message(1024, 'a');
694 string signature;
695 SignMessage(message, &signature);
696}
697
Shawn Willdenf90f2352014-12-18 23:01:15 -0700698TEST_F(SigningOperationsTest, RsaPkcs1Sha256Success) {
699 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256,
700 KM_PAD_RSA_PKCS1_1_5_SIGN)));
701 string message(1024, 'a');
702 string signature;
703 SignMessage(message, &signature);
704}
705
706TEST_F(SigningOperationsTest, RsaPssSha256TooSmallKey) {
707 // Key must be at least 10 bytes larger than hash, to provide minimal random salt, so verify
708 // that 9 bytes larger than hash won't work.
709 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(
710 256 + 9 * 8, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PSS)));
711 string message(1024, 'a');
712 string signature;
713
714 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN));
715
716 string result;
717 size_t input_consumed;
718 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
719 EXPECT_EQ(message.size(), input_consumed);
720 EXPECT_EQ(KM_ERROR_INCOMPATIBLE_DIGEST, FinishOperation(signature, &result));
721}
722
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600723TEST_F(SigningOperationsTest, EcdsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700724 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey(224)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700725 string message = "123456789012345678901234567890123456789012345678";
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700726 string signature;
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700727 SignMessage(message, &signature);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600728}
729
Shawn Willden1615f2e2014-08-13 10:37:40 -0600730TEST_F(SigningOperationsTest, RsaAbort) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700731 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700732 ASSERT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN));
733 EXPECT_EQ(KM_ERROR_OK, AbortOperation());
734 // Another abort should fail
735 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, AbortOperation());
Shawn Willden1615f2e2014-08-13 10:37:40 -0600736}
737
738TEST_F(SigningOperationsTest, RsaUnsupportedDigest) {
Shawn Willdenf90f2352014-12-18 23:01:15 -0700739 GenerateKey(
740 ParamBuilder().RsaSigningKey(256, KM_DIGEST_MD5, KM_PAD_RSA_PSS /* supported padding */));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700741 ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600742}
743
744TEST_F(SigningOperationsTest, RsaUnsupportedPadding) {
Shawn Willdenf90f2352014-12-18 23:01:15 -0700745 GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_SHA_2_256 /* supported digest */,
746 KM_PAD_PKCS7));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700747 ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600748}
749
750TEST_F(SigningOperationsTest, RsaNoDigest) {
Shawn Willdenf90f2352014-12-18 23:01:15 -0700751 // Digest must be specified.
Shawn Willdenc24c1102014-12-22 15:30:09 -0700752 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaKey(256).SigningKey().Option(
753 TAG_PADDING, KM_PAD_NONE)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700754 ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willdenf90f2352014-12-18 23:01:15 -0700755 // PSS requires a digest.
756 GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_NONE, KM_PAD_RSA_PSS));
757 ASSERT_EQ(KM_ERROR_INCOMPATIBLE_DIGEST, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600758}
759
760TEST_F(SigningOperationsTest, RsaNoPadding) {
Shawn Willdenf90f2352014-12-18 23:01:15 -0700761 // Padding must be specified
Shawn Willdenc24c1102014-12-22 15:30:09 -0700762 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaKey(256).SigningKey().Option(
763 TAG_DIGEST, KM_DIGEST_NONE)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700764 ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600765}
766
Shawn Willden51d5e0e2014-12-18 10:44:03 -0700767TEST_F(SigningOperationsTest, HmacSha1Success) {
768 GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA1, 20));
769 string message = "12345678901234567890123456789012";
770 string signature;
771 SignMessage(message, &signature);
772 ASSERT_EQ(20, signature.size());
773}
774
Shawn Willden62c22862014-12-17 08:36:20 -0700775TEST_F(SigningOperationsTest, HmacSha224Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700776 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_224, 28)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700777 string message = "12345678901234567890123456789012";
778 string signature;
779 SignMessage(message, &signature);
780 ASSERT_EQ(28, signature.size());
Shawn Willden62c22862014-12-17 08:36:20 -0700781}
782
Shawn Willden0d560bf2014-12-15 17:44:02 -0700783TEST_F(SigningOperationsTest, HmacSha256Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700784 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_256, 32)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700785 string message = "12345678901234567890123456789012";
786 string signature;
787 SignMessage(message, &signature);
788 ASSERT_EQ(32, signature.size());
Shawn Willden0d560bf2014-12-15 17:44:02 -0700789}
790
Shawn Willden62c22862014-12-17 08:36:20 -0700791TEST_F(SigningOperationsTest, HmacSha384Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700792 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_384, 48)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700793 string message = "12345678901234567890123456789012";
794 string signature;
795 SignMessage(message, &signature);
796 ASSERT_EQ(48, signature.size());
Shawn Willden62c22862014-12-17 08:36:20 -0700797}
798
799TEST_F(SigningOperationsTest, HmacSha512Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700800 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_512, 64)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700801 string message = "12345678901234567890123456789012";
Shawn Willden62c22862014-12-17 08:36:20 -0700802 string signature;
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700803 SignMessage(message, &signature);
804 ASSERT_EQ(64, signature.size());
Shawn Willden62c22862014-12-17 08:36:20 -0700805}
806
807// TODO(swillden): Add HMACSHA{224|256|384|512} tests that validates against the test vectors from
808// RFC4231. Doing that requires being able to import keys, rather than just
809// generate them randomly.
Shawn Willden0d560bf2014-12-15 17:44:02 -0700810
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700811TEST_F(SigningOperationsTest, HmacSha256NoMacLength) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700812 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder()
813 .Option(TAG_ALGORITHM, KM_ALGORITHM_HMAC)
814 .Option(TAG_KEY_SIZE, 128)
815 .SigningKey()
816 .Option(TAG_DIGEST, KM_DIGEST_SHA_2_256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700817 EXPECT_EQ(KM_ERROR_UNSUPPORTED_MAC_LENGTH, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden0d560bf2014-12-15 17:44:02 -0700818}
819
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700820TEST_F(SigningOperationsTest, HmacSha256TooLargeMacLength) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700821 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_256, 33)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700822 ASSERT_EQ(KM_ERROR_UNSUPPORTED_MAC_LENGTH, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willden0d560bf2014-12-15 17:44:02 -0700823}
824
Shawn Willden1615f2e2014-08-13 10:37:40 -0600825TEST_F(SigningOperationsTest, RsaTooShortMessage) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700826 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700827 ASSERT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN));
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700828
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700829 string message = "1234567890123456789012345678901";
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700830 string result;
831 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700832 ASSERT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700833 EXPECT_EQ(0U, result.size());
834 EXPECT_EQ(31U, input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600835
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700836 string signature;
837 ASSERT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&signature));
838 EXPECT_EQ(0U, signature.length());
Shawn Willden43e999e2014-08-13 13:29:50 -0600839}
840
Shawn Willden61902362014-12-18 10:33:24 -0700841// TODO(swillden): Add more verification failure tests.
842
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700843typedef KeymasterTest VerificationOperationsTest;
Shawn Willden43e999e2014-08-13 13:29:50 -0600844TEST_F(VerificationOperationsTest, RsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -0700845 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -0700846 string message = "12345678901234567890123456789012";
847 string signature;
848 SignMessage(message, &signature);
849 VerifyMessage(message, signature);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600850}
851
Shawn Willden61902362014-12-18 10:33:24 -0700852TEST_F(VerificationOperationsTest, RsaSha256DigestSuccess) {
853 // Note that without padding, key size must exactly match digest size.
854 GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_SHA_2_256));
Shawn Willden61902362014-12-18 10:33:24 -0700855 string message(1024, 'a');
856 string signature;
857 SignMessage(message, &signature);
858 VerifyMessage(message, signature);
859}
860
Shawn Willdenf90f2352014-12-18 23:01:15 -0700861TEST_F(VerificationOperationsTest, RsaSha256CorruptSignature) {
Shawn Willden61902362014-12-18 10:33:24 -0700862 GenerateKey(ParamBuilder().RsaSigningKey(256, KM_DIGEST_SHA_2_256));
Shawn Willden61902362014-12-18 10:33:24 -0700863 string message(1024, 'a');
864 string signature;
865 SignMessage(message, &signature);
866 ++signature[signature.size() / 2];
867
868 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY));
869
870 string result;
871 size_t input_consumed;
872 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
873 EXPECT_EQ(message.size(), input_consumed);
874 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result));
875}
876
Shawn Willdenf90f2352014-12-18 23:01:15 -0700877TEST_F(VerificationOperationsTest, RsaPssSha256Success) {
878 ASSERT_EQ(KM_ERROR_OK,
879 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PSS)));
880 // Use large message, which won't work without digesting.
881 string message(1024, 'a');
882 string signature;
883 SignMessage(message, &signature);
884 VerifyMessage(message, signature);
885}
886
887TEST_F(VerificationOperationsTest, RsaPssSha256CorruptSignature) {
888 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PSS));
889 string message(1024, 'a');
890 string signature;
891 SignMessage(message, &signature);
892 ++signature[signature.size() / 2];
893
894 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY));
895
896 string result;
897 size_t input_consumed;
898 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
899 EXPECT_EQ(message.size(), input_consumed);
900 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result));
901}
902
903TEST_F(VerificationOperationsTest, RsaPssSha256CorruptInput) {
904 ASSERT_EQ(KM_ERROR_OK,
905 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PSS)));
Shawn Willden61902362014-12-18 10:33:24 -0700906 // Use large message, which won't work without digesting.
907 string message(1024, 'a');
908 string signature;
909 SignMessage(message, &signature);
910 ++message[message.size() / 2];
911
912 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY));
913
914 string result;
915 size_t input_consumed;
916 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
917 EXPECT_EQ(message.size(), input_consumed);
918 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result));
919}
920
Shawn Willdenf90f2352014-12-18 23:01:15 -0700921TEST_F(VerificationOperationsTest, RsaPkcs1Sha256Success) {
922 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PKCS1_1_5_SIGN));
923 string message(1024, 'a');
924 string signature;
925 SignMessage(message, &signature);
926 VerifyMessage(message, signature);
927}
928
929TEST_F(VerificationOperationsTest, RsaPkcs1Sha256CorruptSignature) {
930 GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256, KM_PAD_RSA_PKCS1_1_5_SIGN));
931 string message(1024, 'a');
932 string signature;
933 SignMessage(message, &signature);
934 ++signature[signature.size() / 2];
935
936 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY));
937
938 string result;
939 size_t input_consumed;
940 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
941 EXPECT_EQ(message.size(), input_consumed);
942 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result));
943}
944
945TEST_F(VerificationOperationsTest, RsaPkcs1Sha256CorruptInput) {
946 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(512, KM_DIGEST_SHA_2_256,
947 KM_PAD_RSA_PKCS1_1_5_SIGN)));
948 // Use large message, which won't work without digesting.
949 string message(1024, 'a');
950 string signature;
951 SignMessage(message, &signature);
952 ++message[message.size() / 2];
953
954 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY));
955
956 string result;
957 size_t input_consumed;
958 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
959 EXPECT_EQ(message.size(), input_consumed);
960 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result));
961}
962
963template <typename T> vector<T> make_vector(const T* array, size_t len) {
964 return vector<T>(array, array + len);
965}
966
967TEST_F(VerificationOperationsTest, RsaAllDigestAndPadCombinations) {
968 // Get all supported digests and padding modes.
969 size_t digests_len;
970 keymaster_digest_t* digests;
971 EXPECT_EQ(KM_ERROR_OK,
972 device()->get_supported_digests(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, &digests,
973 &digests_len));
974
975 size_t padding_modes_len;
976 keymaster_padding_t* padding_modes;
977 EXPECT_EQ(KM_ERROR_OK,
978 device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN,
979 &padding_modes, &padding_modes_len));
980
981 // Try them.
982 for (keymaster_padding_t padding_mode : make_vector(padding_modes, padding_modes_len)) {
983 for (keymaster_digest_t digest : make_vector(digests, digests_len)) {
984 // Compute key & message size that will work.
985 size_t key_bits = 256;
986 size_t message_len = 1000;
987 switch (digest) {
988 case KM_DIGEST_NONE:
989 switch (padding_mode) {
990 case KM_PAD_NONE:
991 // Match key size.
992 message_len = key_bits / 8;
993 break;
994 case KM_PAD_RSA_PKCS1_1_5_SIGN:
995 message_len = key_bits / 8 - 11;
996 break;
997 case KM_PAD_RSA_PSS:
998 // PSS requires a digest.
999 continue;
1000 default:
1001 FAIL() << "Missing padding";
1002 break;
1003 }
1004 break;
1005
1006 case KM_DIGEST_SHA_2_256:
1007 switch (padding_mode) {
1008 case KM_PAD_NONE:
1009 // Key size matches digest size
1010 break;
1011 case KM_PAD_RSA_PKCS1_1_5_SIGN:
1012 key_bits += 8 * 11;
1013 break;
1014 case KM_PAD_RSA_PSS:
1015 key_bits += 8 * 10;
1016 break;
1017 default:
1018 FAIL() << "Missing padding";
1019 break;
1020 }
1021 break;
1022 default:
1023 FAIL() << "Missing digest";
1024 }
1025
1026 GenerateKey(ParamBuilder().RsaSigningKey(key_bits, digest, padding_mode));
1027 string message(message_len, 'a');
1028 string signature;
1029 SignMessage(message, &signature);
1030 VerifyMessage(message, signature);
1031 }
1032 }
1033
1034 free(padding_modes);
1035 free(digests);
1036}
1037
Shawn Willden5ac2f8f2014-08-18 15:33:10 -06001038TEST_F(VerificationOperationsTest, EcdsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001039 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001040 string message = "123456789012345678901234567890123456789012345678";
1041 string signature;
1042 SignMessage(message, &signature);
1043 VerifyMessage(message, signature);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -06001044}
1045
Shawn Willden51d5e0e2014-12-18 10:44:03 -07001046TEST_F(VerificationOperationsTest, HmacSha1Success) {
1047 GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA1, 16));
1048 string message = "123456789012345678901234567890123456789012345678";
1049 string signature;
1050 SignMessage(message, &signature);
1051 VerifyMessage(message, signature);
1052}
1053
1054TEST_F(VerificationOperationsTest, HmacSha224Success) {
1055 GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_224, 16));
1056 string message = "123456789012345678901234567890123456789012345678";
1057 string signature;
1058 SignMessage(message, &signature);
1059 VerifyMessage(message, signature);
1060}
1061
Shawn Willden0d560bf2014-12-15 17:44:02 -07001062TEST_F(VerificationOperationsTest, HmacSha256Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001063 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_256, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001064 string message = "123456789012345678901234567890123456789012345678";
Shawn Willden0d560bf2014-12-15 17:44:02 -07001065 string signature;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001066 SignMessage(message, &signature);
1067 VerifyMessage(message, signature);
Shawn Willden0d560bf2014-12-15 17:44:02 -07001068}
1069
Shawn Willden51d5e0e2014-12-18 10:44:03 -07001070TEST_F(VerificationOperationsTest, HmacSha384Success) {
1071 GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_384, 16));
1072 string message = "123456789012345678901234567890123456789012345678";
1073 string signature;
1074 SignMessage(message, &signature);
1075 VerifyMessage(message, signature);
1076}
1077
1078TEST_F(VerificationOperationsTest, HmacSha512Success) {
1079 GenerateKey(ParamBuilder().HmacKey(128, KM_DIGEST_SHA_2_512, 16));
1080 string message = "123456789012345678901234567890123456789012345678";
1081 string signature;
1082 SignMessage(message, &signature);
1083 VerifyMessage(message, signature);
1084}
1085
Shawn Willden5b53c992015-02-02 08:05:25 -07001086typedef VerificationOperationsTest ExportKeyTest;
Shawn Willdenffd790c2014-08-18 21:20:06 -06001087TEST_F(ExportKeyTest, RsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001088 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001089 string export_data;
1090 ASSERT_EQ(KM_ERROR_OK, ExportKey(KM_KEY_FORMAT_X509, &export_data));
1091 EXPECT_GT(export_data.length(), 0);
Shawn Willdene46a43f2014-08-27 10:35:36 -06001092
1093 // TODO(swillden): Verify that the exported key is actually usable to verify signatures.
Shawn Willdenffd790c2014-08-18 21:20:06 -06001094}
1095
Shawn Willdenf268d742014-08-19 15:36:26 -06001096TEST_F(ExportKeyTest, EcdsaSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001097 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().EcdsaSigningKey(224)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001098 string export_data;
1099 ASSERT_EQ(KM_ERROR_OK, ExportKey(KM_KEY_FORMAT_X509, &export_data));
1100 EXPECT_GT(export_data.length(), 0);
Shawn Willdene46a43f2014-08-27 10:35:36 -06001101
1102 // TODO(swillden): Verify that the exported key is actually usable to verify signatures.
Shawn Willdenf268d742014-08-19 15:36:26 -06001103}
1104
1105TEST_F(ExportKeyTest, RsaUnsupportedKeyFormat) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001106 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001107 string export_data;
1108 ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_PKCS8, &export_data));
Shawn Willdenf268d742014-08-19 15:36:26 -06001109}
1110
1111TEST_F(ExportKeyTest, RsaCorruptedKeyBlob) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001112 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaSigningKey(256)));
Shawn Willden5b53c992015-02-02 08:05:25 -07001113 corrupt_key_blob();
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001114 string export_data;
1115 ASSERT_EQ(KM_ERROR_INVALID_KEY_BLOB, ExportKey(KM_KEY_FORMAT_X509, &export_data));
Shawn Willdenf268d742014-08-19 15:36:26 -06001116}
1117
Shawn Willden7dad93b2015-02-05 10:20:47 -07001118TEST_F(ExportKeyTest, AesKeyExportFails) {
1119 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128)));
1120 string export_data;
1121
1122 EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_X509, &export_data));
1123 EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_PKCS8, &export_data));
1124 EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_RAW, &export_data));
1125}
1126
Shawn Willden437fbd12014-08-20 11:59:49 -06001127static string read_file(const string& file_name) {
1128 ifstream file_stream(file_name, std::ios::binary);
1129 istreambuf_iterator<char> file_begin(file_stream);
1130 istreambuf_iterator<char> file_end;
1131 return string(file_begin, file_end);
1132}
1133
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001134typedef VerificationOperationsTest ImportKeyTest;
Shawn Willden5b53c992015-02-02 08:05:25 -07001135TEST_F(ImportKeyTest, RsaSuccess) {
Shawn Willden81effc62014-08-27 10:08:46 -06001136 string pk8_key = read_file("rsa_privkey_pk8.der");
Shawn Willden437fbd12014-08-20 11:59:49 -06001137 ASSERT_EQ(633U, pk8_key.size());
1138
Shawn Willdena278f612014-12-23 11:22:21 -07001139 ASSERT_EQ(KM_ERROR_OK, ImportKey(ParamBuilder().RsaSigningKey().NoDigestOrPadding(),
Shawn Willdenc24c1102014-12-22 15:30:09 -07001140 KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden437fbd12014-08-20 11:59:49 -06001141
1142 // Check values derived from the key.
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001143 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_RSA));
1144 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 1024));
1145 EXPECT_TRUE(contains(sw_enforced(), TAG_RSA_PUBLIC_EXPONENT, 65537U));
Shawn Willden437fbd12014-08-20 11:59:49 -06001146
1147 // And values provided by GoogleKeymaster
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001148 EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED));
1149 EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME));
Shawn Willden437fbd12014-08-20 11:59:49 -06001150
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001151 string message(1024 / 8, 'a');
1152 string signature;
1153 SignMessage(message, &signature);
1154 VerifyMessage(message, signature);
Shawn Willden437fbd12014-08-20 11:59:49 -06001155}
1156
Shawn Willden6bbe6782014-09-18 11:26:15 -06001157TEST_F(ImportKeyTest, RsaKeySizeMismatch) {
Shawn Willden6bbe6782014-09-18 11:26:15 -06001158 string pk8_key = read_file("rsa_privkey_pk8.der");
1159 ASSERT_EQ(633U, pk8_key.size());
Shawn Willden5b53c992015-02-02 08:05:25 -07001160 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH,
Shawn Willdenc24c1102014-12-22 15:30:09 -07001161 ImportKey(ParamBuilder()
Shawn Willdena278f612014-12-23 11:22:21 -07001162 .RsaSigningKey(2048) // Size doesn't match key
Shawn Willdenc24c1102014-12-22 15:30:09 -07001163 .NoDigestOrPadding(),
1164 KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden6bbe6782014-09-18 11:26:15 -06001165}
1166
1167TEST_F(ImportKeyTest, RsaPublicExponenMismatch) {
Shawn Willden6bbe6782014-09-18 11:26:15 -06001168 string pk8_key = read_file("rsa_privkey_pk8.der");
1169 ASSERT_EQ(633U, pk8_key.size());
Shawn Willden5b53c992015-02-02 08:05:25 -07001170 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH,
Shawn Willdenc24c1102014-12-22 15:30:09 -07001171 ImportKey(ParamBuilder()
Shawn Willdena278f612014-12-23 11:22:21 -07001172 .RsaSigningKey()
Shawn Willdenc24c1102014-12-22 15:30:09 -07001173 .Option(TAG_RSA_PUBLIC_EXPONENT, 3) // Doesn't match key
1174 .NoDigestOrPadding(),
1175 KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden6bbe6782014-09-18 11:26:15 -06001176}
1177
Shawn Willden81effc62014-08-27 10:08:46 -06001178TEST_F(ImportKeyTest, EcdsaSuccess) {
Shawn Willden81effc62014-08-27 10:08:46 -06001179 string pk8_key = read_file("ec_privkey_pk8.der");
1180 ASSERT_EQ(138U, pk8_key.size());
1181
Shawn Willdena278f612014-12-23 11:22:21 -07001182 ASSERT_EQ(KM_ERROR_OK,
1183 ImportKey(ParamBuilder().EcdsaSigningKey(), KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden81effc62014-08-27 10:08:46 -06001184
1185 // Check values derived from the key.
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001186 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
1187 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 256));
Shawn Willden81effc62014-08-27 10:08:46 -06001188
1189 // And values provided by GoogleKeymaster
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001190 EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED));
1191 EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME));
Shawn Willden81effc62014-08-27 10:08:46 -06001192
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001193 string message(1024 / 8, 'a');
1194 string signature;
1195 SignMessage(message, &signature);
1196 VerifyMessage(message, signature);
Shawn Willden81effc62014-08-27 10:08:46 -06001197}
1198
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001199TEST_F(ImportKeyTest, EcdsaSizeSpecified) {
1200 string pk8_key = read_file("ec_privkey_pk8.der");
1201 ASSERT_EQ(138U, pk8_key.size());
Shawn Willden6bbe6782014-09-18 11:26:15 -06001202
Shawn Willdena278f612014-12-23 11:22:21 -07001203 ASSERT_EQ(KM_ERROR_OK,
1204 ImportKey(ParamBuilder().EcdsaSigningKey(256), KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden6bbe6782014-09-18 11:26:15 -06001205
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001206 // Check values derived from the key.
1207 EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_ECDSA));
1208 EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 256));
1209
1210 // And values provided by GoogleKeymaster
1211 EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED));
1212 EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME));
1213
1214 string message(1024 / 8, 'a');
1215 string signature;
1216 SignMessage(message, &signature);
1217 VerifyMessage(message, signature);
1218}
1219
1220TEST_F(ImportKeyTest, EcdsaSizeMismatch) {
1221 string pk8_key = read_file("ec_privkey_pk8.der");
1222 ASSERT_EQ(138U, pk8_key.size());
Shawn Willden5b53c992015-02-02 08:05:25 -07001223 ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH,
Shawn Willdena278f612014-12-23 11:22:21 -07001224 ImportKey(ParamBuilder().EcdsaSigningKey(224), // Size does not match key
1225 KM_KEY_FORMAT_PKCS8, pk8_key));
Shawn Willden6bbe6782014-09-18 11:26:15 -06001226}
1227
Shawn Willden2665e862014-11-24 14:46:21 -07001228typedef KeymasterTest VersionTest;
1229TEST_F(VersionTest, GetVersion) {
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001230 uint8_t major, minor, subminor;
1231 ASSERT_EQ(KM_ERROR_OK, GetVersion(&major, &minor, &subminor));
1232 EXPECT_EQ(1, major);
1233 EXPECT_EQ(0, minor);
1234 EXPECT_EQ(0, subminor);
Shawn Willden2665e862014-11-24 14:46:21 -07001235}
1236
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001237typedef KeymasterTest EncryptionOperationsTest;
Shawn Willden4200f212014-12-02 07:01:21 -07001238TEST_F(EncryptionOperationsTest, RsaOaepSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001239 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_OAEP)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001240
1241 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001242 string ciphertext1 = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001243 EXPECT_EQ(512 / 8, ciphertext1.size());
1244
Shawn Willden6dde87c2014-12-11 14:08:48 -07001245 string ciphertext2 = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001246 EXPECT_EQ(512 / 8, ciphertext2.size());
1247
1248 // OAEP randomizes padding so every result should be different.
1249 EXPECT_NE(ciphertext1, ciphertext2);
1250}
1251
1252TEST_F(EncryptionOperationsTest, RsaOaepRoundTrip) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001253 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_OAEP)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001254 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001255 string ciphertext = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001256 EXPECT_EQ(512 / 8, ciphertext.size());
1257
Shawn Willden6dde87c2014-12-11 14:08:48 -07001258 string plaintext = DecryptMessage(ciphertext);
Shawn Willden4200f212014-12-02 07:01:21 -07001259 EXPECT_EQ(message, plaintext);
1260}
1261
1262TEST_F(EncryptionOperationsTest, RsaOaepTooLarge) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001263 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_OAEP)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001264 string message = "12345678901234567890123";
Shawn Willden4200f212014-12-02 07:01:21 -07001265 string result;
Shawn Willdenb7361132014-12-08 08:15:14 -07001266 size_t input_consumed;
Shawn Willden4200f212014-12-02 07:01:21 -07001267
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001268 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT));
1269 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001270 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&result));
Shawn Willden4200f212014-12-02 07:01:21 -07001271 EXPECT_EQ(0, result.size());
1272}
1273
1274TEST_F(EncryptionOperationsTest, RsaOaepCorruptedDecrypt) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001275 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_OAEP)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001276 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001277 string ciphertext = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001278 EXPECT_EQ(512 / 8, ciphertext.size());
1279
1280 // Corrupt the ciphertext
1281 ciphertext[512 / 8 / 2]++;
1282
Shawn Willden4200f212014-12-02 07:01:21 -07001283 string result;
Shawn Willdenb7361132014-12-08 08:15:14 -07001284 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001285 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
1286 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001287 EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&result));
Shawn Willden4200f212014-12-02 07:01:21 -07001288 EXPECT_EQ(0, result.size());
1289}
1290
1291TEST_F(EncryptionOperationsTest, RsaPkcs1Success) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001292 ASSERT_EQ(KM_ERROR_OK,
1293 GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_PKCS1_1_5_ENCRYPT)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001294 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001295 string ciphertext1 = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001296 EXPECT_EQ(512 / 8, ciphertext1.size());
1297
Shawn Willden6dde87c2014-12-11 14:08:48 -07001298 string ciphertext2 = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001299 EXPECT_EQ(512 / 8, ciphertext2.size());
1300
1301 // PKCS1 v1.5 randomizes padding so every result should be different.
1302 EXPECT_NE(ciphertext1, ciphertext2);
1303}
1304
1305TEST_F(EncryptionOperationsTest, RsaPkcs1RoundTrip) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001306 ASSERT_EQ(KM_ERROR_OK,
1307 GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_PKCS1_1_5_ENCRYPT)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001308 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001309 string ciphertext = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001310 EXPECT_EQ(512 / 8, ciphertext.size());
1311
Shawn Willden6dde87c2014-12-11 14:08:48 -07001312 string plaintext = DecryptMessage(ciphertext);
Shawn Willden4200f212014-12-02 07:01:21 -07001313 EXPECT_EQ(message, plaintext);
1314}
1315
1316TEST_F(EncryptionOperationsTest, RsaPkcs1TooLarge) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001317 ASSERT_EQ(KM_ERROR_OK,
1318 GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_PKCS1_1_5_ENCRYPT)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001319 string message = "12345678901234567890123456789012345678901234567890123";
Shawn Willden4200f212014-12-02 07:01:21 -07001320 string result;
Shawn Willdenb7361132014-12-08 08:15:14 -07001321 size_t input_consumed;
Shawn Willden4200f212014-12-02 07:01:21 -07001322
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001323 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT));
1324 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001325 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&result));
Shawn Willden4200f212014-12-02 07:01:21 -07001326 EXPECT_EQ(0, result.size());
1327}
1328
1329TEST_F(EncryptionOperationsTest, RsaPkcs1CorruptedDecrypt) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001330 ASSERT_EQ(KM_ERROR_OK,
1331 GenerateKey(ParamBuilder().RsaEncryptionKey(512, KM_PAD_RSA_PKCS1_1_5_ENCRYPT)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001332 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001333 string ciphertext = EncryptMessage(string(message));
Shawn Willden4200f212014-12-02 07:01:21 -07001334 EXPECT_EQ(512 / 8, ciphertext.size());
1335
1336 // Corrupt the ciphertext
1337 ciphertext[512 / 8 / 2]++;
1338
Shawn Willden4200f212014-12-02 07:01:21 -07001339 string result;
Shawn Willdenb7361132014-12-08 08:15:14 -07001340 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001341 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
1342 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001343 EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&result));
Shawn Willden4200f212014-12-02 07:01:21 -07001344 EXPECT_EQ(0, result.size());
1345}
1346
Shawn Willden907c3012014-12-08 15:51:55 -07001347TEST_F(EncryptionOperationsTest, AesOcbSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001348 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001349 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001350 string ciphertext1 = EncryptMessage(string(message));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001351 EXPECT_EQ(12 /* nonce */ + message.size() + 16 /* tag */, ciphertext1.size());
Shawn Willden907c3012014-12-08 15:51:55 -07001352
Shawn Willden6dde87c2014-12-11 14:08:48 -07001353 string ciphertext2 = EncryptMessage(string(message));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001354 EXPECT_EQ(12 /* nonce */ + message.size() + 16 /* tag */, ciphertext2.size());
Shawn Willden907c3012014-12-08 15:51:55 -07001355
1356 // OCB uses a random nonce, so every output should be different
1357 EXPECT_NE(ciphertext1, ciphertext2);
1358}
1359
Shawn Willden6dde87c2014-12-11 14:08:48 -07001360TEST_F(EncryptionOperationsTest, AesOcbRoundTripSuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001361 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001362 string message = "Hello World!";
1363 string ciphertext = EncryptMessage(message);
1364 EXPECT_EQ(12 /* nonce */ + message.length() + 16 /* tag */, ciphertext.size());
1365
1366 string plaintext = DecryptMessage(ciphertext);
1367 EXPECT_EQ(message, plaintext);
1368}
1369
1370TEST_F(EncryptionOperationsTest, AesOcbRoundTripCorrupted) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001371 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001372 string message = "Hello World!";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001373 string ciphertext = EncryptMessage(string(message));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001374 EXPECT_EQ(12 /* nonce */ + message.size() + 16 /* tag */, ciphertext.size());
Shawn Willden6dde87c2014-12-11 14:08:48 -07001375
1376 ciphertext[ciphertext.size() / 2]++;
1377
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001378 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001379
1380 string result;
1381 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001382 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001383 EXPECT_EQ(ciphertext.length(), input_consumed);
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001384 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(&result));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001385}
1386
1387TEST_F(EncryptionOperationsTest, AesDecryptGarbage) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001388 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001389 string ciphertext(128, 'a');
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001390 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001391
1392 string result;
1393 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001394 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001395 EXPECT_EQ(ciphertext.length(), input_consumed);
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001396 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(&result));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001397}
1398
1399TEST_F(EncryptionOperationsTest, AesDecryptTooShort) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001400 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001401
Shawn Willden6dde87c2014-12-11 14:08:48 -07001402 // Try decrypting garbage ciphertext that is too short to be valid (< nonce + tag).
Shawn Willden6dde87c2014-12-11 14:08:48 -07001403 string ciphertext(12 + 15, 'a');
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001404 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001405
1406 string result;
1407 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001408 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001409 EXPECT_EQ(ciphertext.length(), input_consumed);
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001410 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&result));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001411}
1412
1413TEST_F(EncryptionOperationsTest, AesOcbRoundTripEmptySuccess) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001414 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001415 string message = "";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001416 string ciphertext = EncryptMessage(string(message));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001417 EXPECT_EQ(12 /* nonce */ + message.size() + 16 /* tag */, ciphertext.size());
Shawn Willden6dde87c2014-12-11 14:08:48 -07001418
1419 string plaintext = DecryptMessage(ciphertext);
1420 EXPECT_EQ(message, plaintext);
1421}
1422
1423TEST_F(EncryptionOperationsTest, AesOcbRoundTripEmptyCorrupted) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001424 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001425 string message = "";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001426 string ciphertext = EncryptMessage(string(message));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001427 EXPECT_EQ(12 /* nonce */ + message.size() + 16 /* tag */, ciphertext.size());
Shawn Willden6dde87c2014-12-11 14:08:48 -07001428
1429 ciphertext[ciphertext.size() / 2]++;
1430
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001431 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001432
1433 string result;
1434 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001435 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001436 EXPECT_EQ(ciphertext.length(), input_consumed);
Shawn Willdend6cd7e32014-12-17 08:01:26 -07001437 EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(&result));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001438}
1439
1440TEST_F(EncryptionOperationsTest, AesOcbFullChunk) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001441 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001442 string message(4096, 'a');
1443 string ciphertext = EncryptMessage(message);
1444 EXPECT_EQ(12 /* nonce */ + message.length() + 16 /* tag */, ciphertext.size());
1445
1446 string plaintext = DecryptMessage(ciphertext);
1447 EXPECT_EQ(message, plaintext);
1448}
1449
1450TEST_F(EncryptionOperationsTest, AesOcbVariousChunkLengths) {
1451 for (unsigned chunk_length = 1; chunk_length <= 128; ++chunk_length) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001452 ASSERT_EQ(KM_ERROR_OK,
1453 GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(chunk_length, 16)));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001454 string message(128, 'a');
1455 string ciphertext = EncryptMessage(message);
1456 int expected_tag_count = (message.length() + chunk_length - 1) / chunk_length;
1457 EXPECT_EQ(12 /* nonce */ + message.length() + 16 * expected_tag_count, ciphertext.size())
1458 << "Unexpected ciphertext size for chunk length " << chunk_length
1459 << " expected tag count was " << expected_tag_count
1460 << " but actual tag count was probably "
1461 << (ciphertext.size() - message.length() - 12) / 16;
1462
1463 string plaintext = DecryptMessage(ciphertext);
1464 EXPECT_EQ(message, plaintext);
1465 }
1466}
1467
1468TEST_F(EncryptionOperationsTest, AesOcbAbort) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001469 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001470 string message = "Hello";
Shawn Willden6dde87c2014-12-11 14:08:48 -07001471
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001472 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT));
Shawn Willden6dde87c2014-12-11 14:08:48 -07001473
1474 string result;
1475 size_t input_consumed;
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001476 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed));
1477 EXPECT_EQ(message.length(), input_consumed);
1478 EXPECT_EQ(KM_ERROR_OK, AbortOperation());
Shawn Willden6dde87c2014-12-11 14:08:48 -07001479}
1480
Shawn Willdenbe4a2a32014-12-15 14:51:10 -07001481TEST_F(EncryptionOperationsTest, AesOcbNoChunkLength) {
Shawn Willdenc24c1102014-12-22 15:30:09 -07001482 EXPECT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder()
1483 .AesEncryptionKey(128)
1484 .Option(TAG_BLOCK_MODE, KM_MODE_OCB)
1485 .Option(TAG_MAC_LENGTH, 16)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001486 EXPECT_EQ(KM_ERROR_INVALID_ARGUMENT, BeginOperation(KM_PURPOSE_ENCRYPT));
Shawn Willdenbe4a2a32014-12-15 14:51:10 -07001487}
1488
Shawn Willdenbe4a2a32014-12-15 14:51:10 -07001489TEST_F(EncryptionOperationsTest, AesOcbPaddingUnsupported) {
Shawn Willdenf0f68b92014-12-30 16:03:28 -07001490 ASSERT_EQ(KM_ERROR_OK,
1491 GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 16).Option(
1492 TAG_PADDING, KM_PAD_ZERO)));
Shawn Willdenb15d77e2014-12-17 16:44:29 -07001493 EXPECT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_ENCRYPT));
Shawn Willdenbe4a2a32014-12-15 14:51:10 -07001494}
1495
1496TEST_F(EncryptionOperationsTest, AesOcbInvalidMacLength) {
Shawn Willden63ac0432014-12-29 14:07:08 -07001497 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).OcbMode(4096, 17)));
1498 EXPECT_EQ(KM_ERROR_INVALID_ARGUMENT, BeginOperation(KM_PURPOSE_ENCRYPT));
Shawn Willdenbe4a2a32014-12-15 14:51:10 -07001499}
1500
Shawn Willdenf0f68b92014-12-30 16:03:28 -07001501TEST_F(EncryptionOperationsTest, AesEcbRoundTripSuccess) {
1502 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).Option(TAG_BLOCK_MODE,
1503 KM_MODE_ECB)));
1504 // Two-block message.
1505 string message = "12345678901234567890123456789012";
1506 string ciphertext1 = EncryptMessage(message);
1507 EXPECT_EQ(message.size(), ciphertext1.size());
1508
1509 string ciphertext2 = EncryptMessage(string(message));
1510 EXPECT_EQ(message.size(), ciphertext2.size());
1511
1512 // ECB is deterministic.
1513 EXPECT_EQ(ciphertext1, ciphertext2);
1514
1515 string plaintext = DecryptMessage(ciphertext1);
1516 EXPECT_EQ(message, plaintext);
1517}
1518
1519TEST_F(EncryptionOperationsTest, AesEcbNoPaddingWrongInputSize) {
1520 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).Option(TAG_BLOCK_MODE,
1521 KM_MODE_ECB)));
1522 // Message is slightly shorter than two blocks.
1523 string message = "1234567890123456789012345678901";
1524
1525 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT));
1526 string ciphertext;
1527 size_t input_consumed;
1528 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &ciphertext, &input_consumed));
1529 EXPECT_EQ(message.size(), input_consumed);
1530 EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&ciphertext));
1531}
1532
1533TEST_F(EncryptionOperationsTest, AesEcbPkcs7Padding) {
1534 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder()
1535 .AesEncryptionKey(128)
1536 .Option(TAG_BLOCK_MODE, KM_MODE_ECB)
1537 .Option(TAG_PADDING, KM_PAD_PKCS7)));
1538
1539 // Try various message lengths; all should work.
1540 for (int i = 0; i < 32; ++i) {
1541 string message(i, 'a');
1542 string ciphertext = EncryptMessage(message);
1543 EXPECT_EQ(i + 16 - (i % 16), ciphertext.size());
1544 string plaintext = DecryptMessage(ciphertext);
1545 EXPECT_EQ(message, plaintext);
1546 }
1547}
1548
1549TEST_F(EncryptionOperationsTest, AesEcbPkcs7PaddingCorrupted) {
1550 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder()
1551 .AesEncryptionKey(128)
1552 .Option(TAG_BLOCK_MODE, KM_MODE_ECB)
1553 .Option(TAG_PADDING, KM_PAD_PKCS7)));
1554
1555 string message = "a";
1556 string ciphertext = EncryptMessage(message);
1557 EXPECT_EQ(16, ciphertext.size());
1558 EXPECT_NE(ciphertext, message);
1559 ++ciphertext[ciphertext.size() / 2];
1560
1561 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
1562 string plaintext;
1563 size_t input_consumed;
1564 EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &plaintext, &input_consumed));
1565 EXPECT_EQ(ciphertext.size(), input_consumed);
1566 EXPECT_EQ(KM_ERROR_INVALID_ARGUMENT, FinishOperation(&plaintext));
1567}
1568
1569TEST_F(EncryptionOperationsTest, AesCbcRoundTripSuccess) {
1570 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).Option(TAG_BLOCK_MODE,
1571 KM_MODE_CBC)));
1572 // Two-block message.
1573 string message = "12345678901234567890123456789012";
1574 string ciphertext1 = EncryptMessage(message);
1575 EXPECT_EQ(message.size() + 16, ciphertext1.size());
1576
1577 string ciphertext2 = EncryptMessage(string(message));
1578 EXPECT_EQ(message.size() + 16, ciphertext2.size());
1579
1580 // CBC uses random IVs, so ciphertexts shouldn't match.
1581 EXPECT_NE(ciphertext1, ciphertext2);
1582
1583 string plaintext = DecryptMessage(ciphertext1);
1584 EXPECT_EQ(message, plaintext);
1585}
1586
1587TEST_F(EncryptionOperationsTest, AesCbcIncrementalNoPadding) {
1588 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder().AesEncryptionKey(128).Option(TAG_BLOCK_MODE,
1589 KM_MODE_CBC)));
1590
1591 int increment = 15;
1592 string message(240, 'a');
1593 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT));
1594 string ciphertext;
1595 size_t input_consumed;
1596 for (size_t i = 0; i < message.size(); i += increment)
1597 EXPECT_EQ(KM_ERROR_OK,
1598 UpdateOperation(message.substr(i, increment), &ciphertext, &input_consumed));
1599 EXPECT_EQ(KM_ERROR_OK, FinishOperation(&ciphertext));
1600 EXPECT_EQ(message.size() + 16, ciphertext.size());
1601
1602 EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT));
1603 string plaintext;
1604 for (size_t i = 0; i < ciphertext.size(); i += increment)
1605 EXPECT_EQ(KM_ERROR_OK,
1606 UpdateOperation(ciphertext.substr(i, increment), &plaintext, &input_consumed));
1607 EXPECT_EQ(KM_ERROR_OK, FinishOperation(&plaintext));
1608 EXPECT_EQ(ciphertext.size() - 16, plaintext.size());
1609 EXPECT_EQ(message, plaintext);
1610}
1611
1612TEST_F(EncryptionOperationsTest, AesCbcPkcs7Padding) {
1613 ASSERT_EQ(KM_ERROR_OK, GenerateKey(ParamBuilder()
1614 .AesEncryptionKey(128)
1615 .Option(TAG_BLOCK_MODE, KM_MODE_CBC)
1616 .Option(TAG_PADDING, KM_PAD_PKCS7)));
1617
1618 // Try various message lengths; all should work.
1619 for (int i = 0; i < 32; ++i) {
1620 string message(i, 'a');
1621 string ciphertext = EncryptMessage(message);
1622 EXPECT_EQ(i + 32 - (i % 16), ciphertext.size());
1623 string plaintext = DecryptMessage(ciphertext);
1624 EXPECT_EQ(message, plaintext);
1625 }
1626}
1627
Shawn Willden128ffe02014-08-06 12:31:33 -06001628} // namespace test
1629} // namespace keymaster