Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include <gtest/gtest.h> |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 18 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 19 | #include <openssl/engine.h> |
| 20 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 21 | #include "google_keymaster_test_utils.h" |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 22 | #include "google_keymaster_utils.h" |
| 23 | #include "google_softkeymaster.h" |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 24 | #include "keymaster_tags.h" |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 25 | |
| 26 | int main(int argc, char** argv) { |
| 27 | ::testing::InitGoogleTest(&argc, argv); |
| 28 | int result = RUN_ALL_TESTS(); |
| 29 | // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain. |
| 30 | CRYPTO_cleanup_all_ex_data(); |
| 31 | ERR_free_strings(); |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | namespace keymaster { |
| 36 | namespace test { |
| 37 | |
| 38 | class KeymasterTest : public testing::Test { |
| 39 | protected: |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 40 | KeymasterTest() : device(5) { RAND_seed("foobar", 6); } |
| 41 | ~KeymasterTest() {} |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 42 | |
| 43 | GoogleSoftKeymaster device; |
| 44 | }; |
| 45 | |
| 46 | template <keymaster_tag_t Tag, typename KeymasterEnum> |
| 47 | bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag, |
| 48 | KeymasterEnum val) { |
| 49 | int pos = set.find(tag); |
| 50 | return pos != -1 && set[pos].enumerated == val; |
| 51 | } |
| 52 | |
| 53 | template <keymaster_tag_t Tag, typename KeymasterEnum> |
| 54 | bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag, |
| 55 | KeymasterEnum val) { |
| 56 | int pos = -1; |
| 57 | while ((pos = set.find(tag, pos)) != -1) |
| 58 | if (set[pos].enumerated == val) |
| 59 | return true; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | template <keymaster_tag_t Tag> |
| 64 | bool contains(const AuthorizationSet& set, TypedTag<KM_INT, Tag> tag, uint32_t val) { |
| 65 | int pos = set.find(tag); |
| 66 | return pos != -1 && set[pos].integer == val; |
| 67 | } |
| 68 | |
| 69 | template <keymaster_tag_t Tag> |
| 70 | bool contains(const AuthorizationSet& set, TypedTag<KM_INT_REP, Tag> tag, uint32_t val) { |
| 71 | int pos = -1; |
| 72 | while ((pos = set.find(tag, pos)) != -1) |
| 73 | if (set[pos].integer == val) |
| 74 | return true; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | template <keymaster_tag_t Tag> |
| 79 | bool contains(const AuthorizationSet& set, TypedTag<KM_LONG, Tag> tag, uint64_t val) { |
| 80 | int pos = set.find(tag); |
| 81 | return pos != -1 && set[pos].long_integer == val; |
| 82 | } |
| 83 | |
| 84 | template <keymaster_tag_t Tag> |
| 85 | bool contains(const AuthorizationSet& set, TypedTag<KM_BYTES, Tag> tag, const std::string& val) { |
| 86 | int pos = set.find(tag); |
| 87 | return pos != -1 && |
| 88 | std::string(reinterpret_cast<const char*>(set[pos].blob.data), |
| 89 | set[pos].blob.data_length) == val; |
| 90 | } |
| 91 | |
| 92 | inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) { |
| 93 | return set.find(tag) != -1; |
| 94 | } |
| 95 | |
| 96 | typedef KeymasterTest CheckSupported; |
| 97 | TEST_F(CheckSupported, SupportedAlgorithms) { |
| 98 | // Shouldn't blow up on NULL. |
| 99 | device.SupportedAlgorithms(NULL); |
| 100 | |
| 101 | SupportedResponse<keymaster_algorithm_t> response; |
| 102 | device.SupportedAlgorithms(&response); |
| 103 | EXPECT_EQ(KM_ERROR_OK, response.error); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 104 | EXPECT_EQ(2U, response.results_length); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 105 | EXPECT_EQ(KM_ALGORITHM_RSA, response.results[0]); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 106 | EXPECT_EQ(KM_ALGORITHM_DSA, response.results[1]); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(CheckSupported, SupportedBlockModes) { |
| 110 | // Shouldn't blow up on NULL. |
| 111 | device.SupportedBlockModes(KM_ALGORITHM_RSA, NULL); |
| 112 | |
| 113 | SupportedResponse<keymaster_block_mode_t> response; |
| 114 | device.SupportedBlockModes(KM_ALGORITHM_RSA, &response); |
| 115 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 116 | EXPECT_EQ(0U, response.results_length); |
| 117 | |
| 118 | device.SupportedBlockModes(KM_ALGORITHM_DSA, &response); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 119 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 120 | EXPECT_EQ(0U, response.results_length); |
| 121 | |
| 122 | device.SupportedBlockModes(KM_ALGORITHM_AES, &response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 123 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, response.error); |
| 124 | } |
| 125 | |
| 126 | TEST_F(CheckSupported, SupportedPaddingModes) { |
| 127 | // Shouldn't blow up on NULL. |
| 128 | device.SupportedPaddingModes(KM_ALGORITHM_RSA, NULL); |
| 129 | |
| 130 | SupportedResponse<keymaster_padding_t> response; |
| 131 | device.SupportedPaddingModes(KM_ALGORITHM_RSA, &response); |
| 132 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 133 | EXPECT_EQ(1U, response.results_length); |
| 134 | EXPECT_EQ(KM_PAD_NONE, response.results[0]); |
| 135 | |
| 136 | device.SupportedPaddingModes(KM_ALGORITHM_DSA, &response); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 137 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 138 | EXPECT_EQ(1U, response.results_length); |
| 139 | EXPECT_EQ(KM_PAD_NONE, response.results[0]); |
| 140 | |
| 141 | device.SupportedPaddingModes(KM_ALGORITHM_AES, &response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 142 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, response.error); |
| 143 | } |
| 144 | |
| 145 | TEST_F(CheckSupported, SupportedDigests) { |
| 146 | // Shouldn't blow up on NULL. |
| 147 | device.SupportedDigests(KM_ALGORITHM_RSA, NULL); |
| 148 | |
| 149 | SupportedResponse<keymaster_digest_t> response; |
| 150 | device.SupportedDigests(KM_ALGORITHM_RSA, &response); |
| 151 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 152 | EXPECT_EQ(1U, response.results_length); |
| 153 | EXPECT_EQ(KM_DIGEST_NONE, response.results[0]); |
| 154 | |
| 155 | device.SupportedDigests(KM_ALGORITHM_DSA, &response); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 156 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 157 | EXPECT_EQ(1U, response.results_length); |
| 158 | EXPECT_EQ(KM_DIGEST_NONE, response.results[0]); |
| 159 | |
| 160 | device.SupportedDigests(KM_ALGORITHM_AES, &response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 161 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, response.error); |
| 162 | } |
| 163 | |
| 164 | TEST_F(CheckSupported, SupportedImportFormats) { |
| 165 | // Shouldn't blow up on NULL. |
| 166 | device.SupportedImportFormats(KM_ALGORITHM_RSA, NULL); |
| 167 | |
| 168 | SupportedResponse<keymaster_key_format_t> response; |
| 169 | device.SupportedImportFormats(KM_ALGORITHM_RSA, &response); |
| 170 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 171 | EXPECT_EQ(1U, response.results_length); |
| 172 | EXPECT_EQ(KM_KEY_FORMAT_PKCS8, response.results[0]); |
| 173 | |
| 174 | device.SupportedImportFormats(KM_ALGORITHM_DSA, &response); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 175 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 176 | EXPECT_EQ(1U, response.results_length); |
| 177 | EXPECT_EQ(KM_KEY_FORMAT_PKCS8, response.results[0]); |
| 178 | |
| 179 | device.SupportedImportFormats(KM_ALGORITHM_AES, &response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 180 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, response.error); |
| 181 | } |
| 182 | |
| 183 | TEST_F(CheckSupported, SupportedExportFormats) { |
| 184 | // Shouldn't blow up on NULL. |
| 185 | device.SupportedExportFormats(KM_ALGORITHM_RSA, NULL); |
| 186 | |
| 187 | SupportedResponse<keymaster_key_format_t> response; |
| 188 | device.SupportedExportFormats(KM_ALGORITHM_RSA, &response); |
| 189 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 190 | EXPECT_EQ(1U, response.results_length); |
| 191 | EXPECT_EQ(KM_KEY_FORMAT_X509, response.results[0]); |
| 192 | |
| 193 | device.SupportedExportFormats(KM_ALGORITHM_DSA, &response); |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 194 | EXPECT_EQ(KM_ERROR_OK, response.error); |
| 195 | EXPECT_EQ(1U, response.results_length); |
| 196 | EXPECT_EQ(KM_KEY_FORMAT_X509, response.results[0]); |
| 197 | |
| 198 | device.SupportedExportFormats(KM_ALGORITHM_AES, &response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 199 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, response.error); |
| 200 | } |
| 201 | |
| 202 | typedef KeymasterTest NewKeyGeneration; |
| 203 | TEST_F(NewKeyGeneration, Rsa) { |
| 204 | keymaster_key_param_t params[] = { |
| 205 | Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 206 | Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), |
| 207 | Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 208 | Authorization(TAG_KEY_SIZE, 256), |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 209 | Authorization(TAG_USER_ID, 7), |
| 210 | Authorization(TAG_USER_AUTH_ID, 8), |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 211 | Authorization(TAG_APPLICATION_ID, "app_id", 6), |
| 212 | Authorization(TAG_APPLICATION_DATA, "app_data", 8), |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 213 | Authorization(TAG_AUTH_TIMEOUT, 300), |
| 214 | }; |
| 215 | GenerateKeyRequest req; |
| 216 | req.key_description.Reinitialize(params, array_length(params)); |
| 217 | GenerateKeyResponse rsp; |
| 218 | |
| 219 | device.GenerateKey(req, &rsp); |
| 220 | |
| 221 | ASSERT_EQ(KM_ERROR_OK, rsp.error); |
| 222 | EXPECT_EQ(0U, rsp.enforced.size()); |
Shawn Willden | 8d336ae | 2014-08-09 15:47:05 -0600 | [diff] [blame] | 223 | EXPECT_EQ(12U, rsp.enforced.SerializedSize()); |
| 224 | EXPECT_GT(rsp.unenforced.SerializedSize(), 12U); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 225 | |
| 226 | // Check specified tags are all present in unenforced characteristics |
| 227 | EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 228 | EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_VERIFY)); |
| 229 | |
| 230 | EXPECT_TRUE(contains(rsp.unenforced, TAG_ALGORITHM, KM_ALGORITHM_RSA)); |
| 231 | |
| 232 | EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_ID, 7)); |
| 233 | EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_AUTH_ID, 8)); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 234 | EXPECT_TRUE(contains(rsp.unenforced, TAG_KEY_SIZE, 256)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 235 | EXPECT_TRUE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 300)); |
| 236 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 237 | // Verify that App ID, App data and ROT are NOT included. |
| 238 | EXPECT_FALSE(contains(rsp.unenforced, TAG_ROOT_OF_TRUST)); |
| 239 | EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_ID)); |
| 240 | EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_DATA)); |
| 241 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 242 | // Just for giggles, check that some unexpected tags/values are NOT present. |
| 243 | EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
| 244 | EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_DECRYPT)); |
| 245 | EXPECT_FALSE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 301)); |
| 246 | EXPECT_FALSE(contains(rsp.unenforced, TAG_RESCOPE_AUTH_TIMEOUT)); |
| 247 | |
| 248 | // Now check that unspecified, defaulted tags are correct. |
| 249 | EXPECT_TRUE(contains(rsp.unenforced, TAG_RSA_PUBLIC_EXPONENT, 65537)); |
| 250 | EXPECT_TRUE(contains(rsp.unenforced, TAG_ORIGIN, KM_ORIGIN_SOFTWARE)); |
| 251 | EXPECT_TRUE(contains(rsp.unenforced, KM_TAG_CREATION_DATETIME)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame^] | 254 | TEST_F(NewKeyGeneration, Dsa) { |
| 255 | keymaster_key_param_t params[] = { |
| 256 | Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 257 | Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), |
| 258 | Authorization(TAG_ALGORITHM, KM_ALGORITHM_DSA), |
| 259 | Authorization(TAG_KEY_SIZE, 256), |
| 260 | Authorization(TAG_USER_ID, 7), |
| 261 | Authorization(TAG_USER_AUTH_ID, 8), |
| 262 | Authorization(TAG_APPLICATION_ID, "app_id", 6), |
| 263 | Authorization(TAG_APPLICATION_DATA, "app_data", 8), |
| 264 | Authorization(TAG_AUTH_TIMEOUT, 300), |
| 265 | }; |
| 266 | GenerateKeyRequest req; |
| 267 | req.key_description.Reinitialize(params, array_length(params)); |
| 268 | GenerateKeyResponse rsp; |
| 269 | |
| 270 | device.GenerateKey(req, &rsp); |
| 271 | |
| 272 | ASSERT_EQ(KM_ERROR_OK, rsp.error); |
| 273 | EXPECT_EQ(0U, rsp.enforced.size()); |
| 274 | EXPECT_EQ(12U, rsp.enforced.SerializedSize()); |
| 275 | EXPECT_GT(rsp.unenforced.SerializedSize(), 12U); |
| 276 | |
| 277 | // Check specified tags are all present in unenforced characteristics |
| 278 | EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 279 | EXPECT_TRUE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_VERIFY)); |
| 280 | |
| 281 | EXPECT_TRUE(contains(rsp.unenforced, TAG_ALGORITHM, KM_ALGORITHM_DSA)); |
| 282 | |
| 283 | EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_ID, 7)); |
| 284 | EXPECT_TRUE(contains(rsp.unenforced, TAG_USER_AUTH_ID, 8)); |
| 285 | EXPECT_TRUE(contains(rsp.unenforced, TAG_KEY_SIZE, 256)); |
| 286 | EXPECT_TRUE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 300)); |
| 287 | |
| 288 | // Verify that App ID, App data and ROT are NOT included. |
| 289 | EXPECT_FALSE(contains(rsp.unenforced, TAG_ROOT_OF_TRUST)); |
| 290 | EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_ID)); |
| 291 | EXPECT_FALSE(contains(rsp.unenforced, TAG_APPLICATION_DATA)); |
| 292 | |
| 293 | // Just for giggles, check that some unexpected tags/values are NOT present. |
| 294 | EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
| 295 | EXPECT_FALSE(contains(rsp.unenforced, TAG_PURPOSE, KM_PURPOSE_DECRYPT)); |
| 296 | EXPECT_FALSE(contains(rsp.unenforced, TAG_AUTH_TIMEOUT, 301)); |
| 297 | EXPECT_FALSE(contains(rsp.unenforced, TAG_RESCOPE_AUTH_TIMEOUT)); |
| 298 | |
| 299 | // Now check that unspecified, defaulted tags are correct. |
| 300 | EXPECT_TRUE(contains(rsp.unenforced, TAG_ORIGIN, KM_ORIGIN_SOFTWARE)); |
| 301 | EXPECT_TRUE(contains(rsp.unenforced, KM_TAG_CREATION_DATETIME)); |
| 302 | |
| 303 | // Generator should have created DSA params. |
| 304 | keymaster_blob_t g, p, q; |
| 305 | EXPECT_TRUE(rsp.unenforced.GetTagValue(TAG_DSA_GENERATOR, &g)); |
| 306 | EXPECT_TRUE(rsp.unenforced.GetTagValue(TAG_DSA_P, &p)); |
| 307 | EXPECT_TRUE(rsp.unenforced.GetTagValue(TAG_DSA_Q, &q)); |
| 308 | EXPECT_EQ(64U, g.data_length); |
| 309 | EXPECT_EQ(64U, p.data_length); |
| 310 | EXPECT_EQ(20U, q.data_length); |
| 311 | } |
| 312 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 313 | typedef KeymasterTest GetKeyCharacteristics; |
| 314 | TEST_F(GetKeyCharacteristics, SimpleRsa) { |
| 315 | keymaster_key_param_t params[] = { |
| 316 | Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 317 | Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), |
| 318 | Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 319 | Authorization(TAG_KEY_SIZE, 256), |
| 320 | Authorization(TAG_USER_ID, 7), |
| 321 | Authorization(TAG_USER_AUTH_ID, 8), |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 322 | Authorization(TAG_APPLICATION_ID, "app_id", 6), |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 323 | Authorization(TAG_AUTH_TIMEOUT, 300), |
| 324 | }; |
| 325 | |
| 326 | GenerateKeyRequest gen_req; |
| 327 | gen_req.key_description.Reinitialize(params, array_length(params)); |
| 328 | GenerateKeyResponse gen_rsp; |
| 329 | |
| 330 | device.GenerateKey(gen_req, &gen_rsp); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 331 | ASSERT_EQ(KM_ERROR_OK, gen_rsp.error); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 332 | |
| 333 | GetKeyCharacteristicsRequest req; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 334 | req.SetKeyMaterial(gen_rsp.key_blob); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 335 | req.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 336 | |
| 337 | GetKeyCharacteristicsResponse rsp; |
| 338 | device.GetKeyCharacteristics(req, &rsp); |
| 339 | ASSERT_EQ(KM_ERROR_OK, rsp.error); |
| 340 | |
| 341 | EXPECT_EQ(gen_rsp.enforced, rsp.enforced); |
| 342 | EXPECT_EQ(gen_rsp.unenforced, rsp.unenforced); |
| 343 | } |
| 344 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 345 | class SigningOperationsTest : public KeymasterTest { |
| 346 | protected: |
| 347 | keymaster_key_blob_t* GenerateKey(keymaster_digest_t digest, keymaster_padding_t padding, |
| 348 | uint32_t key_size) { |
| 349 | keymaster_key_param_t params[] = { |
| 350 | Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 351 | Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), |
| 352 | Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 353 | Authorization(TAG_KEY_SIZE, key_size), |
| 354 | Authorization(TAG_USER_ID, 7), |
| 355 | Authorization(TAG_USER_AUTH_ID, 8), |
| 356 | Authorization(TAG_APPLICATION_ID, "app_id", 6), |
| 357 | Authorization(TAG_AUTH_TIMEOUT, 300), |
| 358 | }; |
| 359 | GenerateKeyRequest generate_request; |
| 360 | generate_request.key_description.Reinitialize(params, array_length(params)); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 361 | if (static_cast<int>(digest) != -1) |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 362 | generate_request.key_description.push_back(TAG_DIGEST, digest); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 363 | if (static_cast<int>(padding) != -1) |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 364 | generate_request.key_description.push_back(TAG_PADDING, padding); |
| 365 | device.GenerateKey(generate_request, &generate_response_); |
| 366 | EXPECT_EQ(KM_ERROR_OK, generate_response_.error); |
| 367 | |
| 368 | // This is safe because generate_response_ lives as long as the test and will keep the key |
| 369 | // blob around. |
| 370 | return &generate_response_.key_blob; |
| 371 | } |
| 372 | |
| 373 | private: |
| 374 | GenerateKeyResponse generate_response_; |
| 375 | }; |
| 376 | |
| 377 | TEST_F(SigningOperationsTest, RsaSuccess) { |
| 378 | keymaster_key_blob_t* key = GenerateKey(KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */); |
| 379 | ASSERT_TRUE(key != NULL); |
| 380 | |
| 381 | BeginOperationRequest begin_request; |
| 382 | BeginOperationResponse begin_response; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 383 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 384 | begin_request.purpose = KM_PURPOSE_SIGN; |
| 385 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 386 | |
| 387 | device.BeginOperation(begin_request, &begin_response); |
| 388 | ASSERT_EQ(KM_ERROR_OK, begin_response.error); |
| 389 | |
| 390 | UpdateOperationRequest update_request; |
| 391 | UpdateOperationResponse update_response; |
| 392 | update_request.op_handle = begin_response.op_handle; |
| 393 | update_request.input.Reinitialize("012345678901234567890123456789012", 32); |
| 394 | EXPECT_EQ(32U, update_request.input.available_read()); |
| 395 | |
| 396 | device.UpdateOperation(update_request, &update_response); |
| 397 | ASSERT_EQ(KM_ERROR_OK, update_response.error); |
| 398 | EXPECT_EQ(0U, update_response.output.available_read()); |
| 399 | |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 400 | FinishOperationRequest finish_request; |
| 401 | finish_request.op_handle = begin_response.op_handle; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 402 | FinishOperationResponse finish_response; |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 403 | device.FinishOperation(finish_request, &finish_response); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 404 | ASSERT_EQ(KM_ERROR_OK, finish_response.error); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 405 | EXPECT_GT(finish_response.output.available_read(), 0U); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 406 | |
| 407 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 408 | } |
| 409 | |
| 410 | TEST_F(SigningOperationsTest, RsaAbort) { |
| 411 | keymaster_key_blob_t* key = GenerateKey(KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */); |
| 412 | ASSERT_TRUE(key != NULL); |
| 413 | |
| 414 | BeginOperationRequest begin_request; |
| 415 | BeginOperationResponse begin_response; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 416 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 417 | begin_request.purpose = KM_PURPOSE_SIGN; |
| 418 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 419 | |
| 420 | device.BeginOperation(begin_request, &begin_response); |
| 421 | ASSERT_EQ(KM_ERROR_OK, begin_response.error); |
| 422 | |
| 423 | EXPECT_EQ(KM_ERROR_OK, device.AbortOperation(begin_response.op_handle)); |
| 424 | |
| 425 | // Another abort should fail |
| 426 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 427 | } |
| 428 | |
| 429 | TEST_F(SigningOperationsTest, RsaUnsupportedDigest) { |
| 430 | keymaster_key_blob_t* key = GenerateKey(KM_DIGEST_SHA_2_256, KM_PAD_NONE, 256 /* key size */); |
| 431 | ASSERT_TRUE(key != NULL); |
| 432 | |
| 433 | BeginOperationRequest begin_request; |
| 434 | BeginOperationResponse begin_response; |
| 435 | begin_request.purpose = KM_PURPOSE_SIGN; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 436 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 437 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 438 | |
| 439 | device.BeginOperation(begin_request, &begin_response); |
| 440 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, begin_response.error); |
| 441 | |
| 442 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 443 | } |
| 444 | |
| 445 | TEST_F(SigningOperationsTest, RsaUnsupportedPadding) { |
| 446 | keymaster_key_blob_t* key = GenerateKey(KM_DIGEST_NONE, KM_PAD_RSA_OAEP, 256 /* key size */); |
| 447 | ASSERT_TRUE(key != NULL); |
| 448 | |
| 449 | BeginOperationRequest begin_request; |
| 450 | BeginOperationResponse begin_response; |
| 451 | begin_request.purpose = KM_PURPOSE_SIGN; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 452 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 453 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 454 | |
| 455 | device.BeginOperation(begin_request, &begin_response); |
| 456 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, begin_response.error); |
| 457 | |
| 458 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 459 | } |
| 460 | |
| 461 | TEST_F(SigningOperationsTest, RsaNoDigest) { |
| 462 | keymaster_key_blob_t* key = |
| 463 | GenerateKey(static_cast<keymaster_digest_t>(-1), KM_PAD_NONE, 256 /* key size */); |
| 464 | ASSERT_TRUE(key != NULL); |
| 465 | |
| 466 | BeginOperationRequest begin_request; |
| 467 | BeginOperationResponse begin_response; |
| 468 | begin_request.purpose = KM_PURPOSE_SIGN; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 469 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 470 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 471 | |
| 472 | device.BeginOperation(begin_request, &begin_response); |
| 473 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, begin_response.error); |
| 474 | |
| 475 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 476 | } |
| 477 | |
| 478 | TEST_F(SigningOperationsTest, RsaNoPadding) { |
| 479 | keymaster_key_blob_t* key = |
| 480 | GenerateKey(KM_DIGEST_NONE, static_cast<keymaster_padding_t>(-1), 256 /* key size */); |
| 481 | ASSERT_TRUE(key != NULL); |
| 482 | |
| 483 | BeginOperationRequest begin_request; |
| 484 | BeginOperationResponse begin_response; |
| 485 | begin_request.purpose = KM_PURPOSE_SIGN; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 486 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 487 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 488 | |
| 489 | device.BeginOperation(begin_request, &begin_response); |
| 490 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, begin_response.error); |
| 491 | |
| 492 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 493 | } |
| 494 | |
| 495 | TEST_F(SigningOperationsTest, RsaTooShortMessage) { |
| 496 | keymaster_key_blob_t* key = GenerateKey(KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */); |
| 497 | ASSERT_TRUE(key != NULL); |
| 498 | |
| 499 | BeginOperationRequest begin_request; |
| 500 | BeginOperationResponse begin_response; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 501 | begin_request.SetKeyMaterial(*key); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 502 | begin_request.purpose = KM_PURPOSE_SIGN; |
| 503 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 504 | |
| 505 | device.BeginOperation(begin_request, &begin_response); |
| 506 | ASSERT_EQ(KM_ERROR_OK, begin_response.error); |
| 507 | |
| 508 | UpdateOperationRequest update_request; |
| 509 | UpdateOperationResponse update_response; |
| 510 | update_request.op_handle = begin_response.op_handle; |
| 511 | update_request.input.Reinitialize("01234567890123456789012345678901", 31); |
| 512 | EXPECT_EQ(31U, update_request.input.available_read()); |
| 513 | |
| 514 | device.UpdateOperation(update_request, &update_response); |
| 515 | ASSERT_EQ(KM_ERROR_OK, update_response.error); |
| 516 | EXPECT_EQ(0U, update_response.output.available_read()); |
| 517 | |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 518 | FinishOperationRequest finish_request; |
| 519 | finish_request.op_handle = begin_response.op_handle; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 520 | FinishOperationResponse finish_response; |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 521 | device.FinishOperation(finish_request, &finish_response); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 522 | ASSERT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, finish_response.error); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 523 | EXPECT_EQ(0U, finish_response.output.available_read()); |
| 524 | |
| 525 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 526 | } |
| 527 | |
| 528 | class VerificationOperationsTest : public KeymasterTest { |
| 529 | protected: |
| 530 | VerificationOperationsTest() { |
| 531 | generate_response_.error = KM_ERROR_UNKNOWN_ERROR; |
| 532 | finish_response_.error = KM_ERROR_UNKNOWN_ERROR; |
| 533 | } |
| 534 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 535 | void GenerateKey(keymaster_digest_t digest, keymaster_padding_t padding, uint32_t key_size) { |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 536 | keymaster_key_param_t params[] = { |
| 537 | Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 538 | Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY), |
| 539 | Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 540 | Authorization(TAG_KEY_SIZE, key_size), |
| 541 | Authorization(TAG_USER_ID, 7), |
| 542 | Authorization(TAG_USER_AUTH_ID, 8), |
| 543 | Authorization(TAG_APPLICATION_ID, "app_id", 6), |
| 544 | Authorization(TAG_AUTH_TIMEOUT, 300), |
| 545 | }; |
| 546 | GenerateKeyRequest generate_request; |
| 547 | generate_request.key_description.Reinitialize(params, array_length(params)); |
| 548 | if (static_cast<int>(digest) != -1) |
| 549 | generate_request.key_description.push_back(TAG_DIGEST, digest); |
| 550 | if (static_cast<int>(padding) != -1) |
| 551 | generate_request.key_description.push_back(TAG_PADDING, padding); |
| 552 | device.GenerateKey(generate_request, &generate_response_); |
| 553 | EXPECT_EQ(KM_ERROR_OK, generate_response_.error); |
| 554 | |
| 555 | BeginOperationRequest begin_request; |
| 556 | BeginOperationResponse begin_response; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 557 | begin_request.SetKeyMaterial(generate_response_.key_blob); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 558 | begin_request.purpose = KM_PURPOSE_SIGN; |
| 559 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 560 | |
| 561 | device.BeginOperation(begin_request, &begin_response); |
| 562 | ASSERT_EQ(KM_ERROR_OK, begin_response.error); |
| 563 | |
| 564 | UpdateOperationRequest update_request; |
| 565 | UpdateOperationResponse update_response; |
| 566 | update_request.op_handle = begin_response.op_handle; |
| 567 | update_request.input.Reinitialize("012345678901234567890123456789012", 32); |
| 568 | EXPECT_EQ(32U, update_request.input.available_read()); |
| 569 | |
| 570 | device.UpdateOperation(update_request, &update_response); |
| 571 | ASSERT_EQ(KM_ERROR_OK, update_response.error); |
| 572 | EXPECT_EQ(0U, update_response.output.available_read()); |
| 573 | |
| 574 | FinishOperationRequest finish_request; |
| 575 | finish_request.op_handle = begin_response.op_handle; |
| 576 | device.FinishOperation(finish_request, &finish_response_); |
| 577 | ASSERT_EQ(KM_ERROR_OK, finish_response_.error); |
| 578 | EXPECT_GT(finish_response_.output.available_read(), 0U); |
| 579 | } |
| 580 | |
| 581 | keymaster_key_blob_t* key_blob() { |
| 582 | if (generate_response_.error == KM_ERROR_OK) |
| 583 | return &generate_response_.key_blob; |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | Buffer* signature() { |
| 588 | if (finish_response_.error == KM_ERROR_OK) |
| 589 | return &finish_response_.output; |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | private: |
| 594 | GenerateKeyResponse generate_response_; |
| 595 | FinishOperationResponse finish_response_; |
| 596 | }; |
| 597 | |
| 598 | TEST_F(VerificationOperationsTest, RsaSuccess) { |
| 599 | GenerateKey(KM_DIGEST_NONE, KM_PAD_NONE, 256 /* key size */); |
| 600 | ASSERT_TRUE(key_blob() != NULL); |
| 601 | ASSERT_TRUE(signature() != NULL); |
| 602 | |
| 603 | BeginOperationRequest begin_request; |
| 604 | BeginOperationResponse begin_response; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 605 | begin_request.SetKeyMaterial(*key_blob()); |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 606 | begin_request.purpose = KM_PURPOSE_VERIFY; |
| 607 | begin_request.additional_params.push_back(TAG_APPLICATION_ID, "app_id", 6); |
| 608 | |
| 609 | device.BeginOperation(begin_request, &begin_response); |
| 610 | ASSERT_EQ(KM_ERROR_OK, begin_response.error); |
| 611 | |
| 612 | UpdateOperationRequest update_request; |
| 613 | UpdateOperationResponse update_response; |
| 614 | update_request.op_handle = begin_response.op_handle; |
| 615 | update_request.input.Reinitialize("012345678901234567890123456789012", 32); |
| 616 | EXPECT_EQ(32U, update_request.input.available_read()); |
| 617 | |
| 618 | device.UpdateOperation(update_request, &update_response); |
| 619 | ASSERT_EQ(KM_ERROR_OK, update_response.error); |
| 620 | EXPECT_EQ(0U, update_response.output.available_read()); |
| 621 | |
| 622 | FinishOperationRequest finish_request; |
| 623 | finish_request.op_handle = begin_response.op_handle; |
| 624 | finish_request.signature.Reinitialize(*signature()); |
| 625 | FinishOperationResponse finish_response; |
| 626 | device.FinishOperation(finish_request, &finish_response); |
| 627 | ASSERT_EQ(KM_ERROR_OK, finish_response.error); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 628 | EXPECT_EQ(0U, finish_response.output.available_read()); |
| 629 | |
| 630 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, device.AbortOperation(begin_response.op_handle)); |
| 631 | } |
| 632 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 633 | } // namespace test |
| 634 | } // namespace keymaster |