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 | |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 17 | #include <fstream> |
Shawn Willden | 76076ab | 2014-12-18 08:36:35 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 20 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 21 | #include <openssl/engine.h> |
| 22 | |
Shawn Willden | d7a5c71 | 2015-04-09 16:33:52 -0600 | [diff] [blame] | 23 | #include <hardware/keymaster0.h> |
| 24 | |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 25 | #include <keymaster/google_keymaster_utils.h> |
| 26 | #include <keymaster/keymaster_tags.h> |
Shawn Willden | b751033 | 2015-02-06 19:58:29 -0700 | [diff] [blame] | 27 | #include <keymaster/soft_keymaster_device.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 28 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 29 | #include "google_keymaster_test_utils.h" |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 30 | |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 31 | using std::ifstream; |
| 32 | using std::istreambuf_iterator; |
Shawn Willden | 76076ab | 2014-12-18 08:36:35 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | using std::vector; |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 35 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 36 | template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { |
| 37 | os << "{ "; |
| 38 | bool first = true; |
| 39 | for (T t : vec) { |
| 40 | os << (first ? "" : ", ") << t; |
| 41 | if (first) |
| 42 | first = false; |
| 43 | } |
| 44 | os << " }"; |
| 45 | return os; |
| 46 | } |
| 47 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 48 | namespace keymaster { |
| 49 | namespace test { |
| 50 | |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 51 | StdoutLogger logger; |
| 52 | |
Shawn Willden | 95dda36 | 2015-02-27 10:58:37 -0700 | [diff] [blame] | 53 | class KeymasterTest : public Keymaster1Test { |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 54 | protected: |
Shawn Willden | 95dda36 | 2015-02-27 10:58:37 -0700 | [diff] [blame] | 55 | KeymasterTest() { |
| 56 | SoftKeymasterDevice* device = new SoftKeymasterDevice; |
| 57 | init(device->keymaster_device()); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 58 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 59 | }; |
| 60 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 61 | typedef KeymasterTest CheckSupported; |
| 62 | TEST_F(CheckSupported, SupportedAlgorithms) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 63 | EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 64 | device()->get_supported_algorithms(device(), NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 65 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 66 | size_t len; |
| 67 | keymaster_algorithm_t* algorithms; |
| 68 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_algorithms(device(), &algorithms, &len)); |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(ResponseContains( |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 70 | {KM_ALGORITHM_RSA, KM_ALGORITHM_EC, KM_ALGORITHM_AES, KM_ALGORITHM_HMAC}, algorithms, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 71 | free(algorithms); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_F(CheckSupported, SupportedBlockModes) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 75 | EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 76 | device()->get_supported_block_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, |
| 77 | NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 78 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 79 | size_t len; |
| 80 | keymaster_block_mode_t* modes; |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 81 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_block_modes(device(), KM_ALGORITHM_RSA, |
| 82 | KM_PURPOSE_ENCRYPT, &modes, &len)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 83 | EXPECT_EQ(0U, len); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 84 | free(modes); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 85 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 86 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE, |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 87 | device()->get_supported_block_modes(device(), KM_ALGORITHM_EC, KM_PURPOSE_ENCRYPT, |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 88 | &modes, &len)); |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 89 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 90 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_block_modes(device(), KM_ALGORITHM_AES, |
| 91 | KM_PURPOSE_ENCRYPT, &modes, &len)); |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 92 | EXPECT_TRUE(ResponseContains({KM_MODE_ECB, KM_MODE_CBC, KM_MODE_CTR}, modes, len)); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 93 | free(modes); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST_F(CheckSupported, SupportedPaddingModes) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 97 | EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 98 | device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, |
| 99 | NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 100 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 101 | size_t len; |
| 102 | keymaster_padding_t* modes; |
| 103 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, |
| 104 | KM_PURPOSE_SIGN, &modes, &len)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 105 | EXPECT_TRUE( |
| 106 | ResponseContains({KM_PAD_NONE, KM_PAD_RSA_PKCS1_1_5_SIGN, KM_PAD_RSA_PSS}, modes, len)); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 107 | free(modes); |
| 108 | |
| 109 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, |
| 110 | KM_PURPOSE_ENCRYPT, &modes, &len)); |
| 111 | EXPECT_TRUE(ResponseContains({KM_PAD_RSA_OAEP, KM_PAD_RSA_PKCS1_1_5_ENCRYPT}, modes, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 112 | free(modes); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 113 | |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 114 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_padding_modes(device(), KM_ALGORITHM_EC, |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 115 | KM_PURPOSE_SIGN, &modes, &len)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 116 | EXPECT_EQ(0U, len); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 117 | free(modes); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 118 | |
| 119 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE, |
| 120 | device()->get_supported_padding_modes(device(), KM_ALGORITHM_AES, KM_PURPOSE_SIGN, |
| 121 | &modes, &len)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(CheckSupported, SupportedDigests) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 125 | EXPECT_EQ( |
| 126 | KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 127 | device()->get_supported_digests(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 128 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 129 | size_t len; |
| 130 | keymaster_digest_t* digests; |
| 131 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_RSA, |
| 132 | KM_PURPOSE_SIGN, &digests, &len)); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(ResponseContains({KM_DIGEST_NONE, KM_DIGEST_SHA_2_256}, digests, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 134 | free(digests); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 135 | |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 136 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_EC, |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 137 | KM_PURPOSE_SIGN, &digests, &len)); |
Shawn Willden | e998c13 | 2015-04-14 14:41:46 -0600 | [diff] [blame] | 138 | EXPECT_TRUE(ResponseContains(KM_DIGEST_NONE, digests, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 139 | free(digests); |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 140 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 141 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE, |
| 142 | device()->get_supported_digests(device(), KM_ALGORITHM_AES, KM_PURPOSE_SIGN, &digests, |
| 143 | &len)); |
| 144 | |
| 145 | EXPECT_EQ(KM_ERROR_OK, device()->get_supported_digests(device(), KM_ALGORITHM_HMAC, |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 146 | KM_PURPOSE_SIGN, &digests, &len)); |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 147 | EXPECT_TRUE(ResponseContains({KM_DIGEST_SHA_2_224, KM_DIGEST_SHA_2_256, KM_DIGEST_SHA_2_384, |
| 148 | KM_DIGEST_SHA_2_512, KM_DIGEST_SHA1}, |
| 149 | digests, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 150 | free(digests); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(CheckSupported, SupportedImportFormats) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 154 | EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 155 | device()->get_supported_import_formats(device(), KM_ALGORITHM_RSA, NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 156 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 157 | size_t len; |
| 158 | keymaster_key_format_t* formats; |
| 159 | EXPECT_EQ(KM_ERROR_OK, |
| 160 | device()->get_supported_import_formats(device(), KM_ALGORITHM_RSA, &formats, &len)); |
Shawn Willden | 76076ab | 2014-12-18 08:36:35 -0700 | [diff] [blame] | 161 | EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_PKCS8, formats, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 162 | free(formats); |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 163 | |
| 164 | EXPECT_EQ(KM_ERROR_OK, |
| 165 | device()->get_supported_import_formats(device(), KM_ALGORITHM_AES, &formats, &len)); |
| 166 | EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_RAW, formats, len)); |
| 167 | free(formats); |
| 168 | |
| 169 | EXPECT_EQ(KM_ERROR_OK, |
| 170 | device()->get_supported_import_formats(device(), KM_ALGORITHM_HMAC, &formats, &len)); |
| 171 | EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_RAW, formats, len)); |
| 172 | free(formats); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST_F(CheckSupported, SupportedExportFormats) { |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 176 | EXPECT_EQ(KM_ERROR_OUTPUT_PARAMETER_NULL, |
| 177 | device()->get_supported_export_formats(device(), KM_ALGORITHM_RSA, NULL, NULL)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 178 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 179 | size_t len; |
| 180 | keymaster_key_format_t* formats; |
| 181 | EXPECT_EQ(KM_ERROR_OK, |
| 182 | device()->get_supported_export_formats(device(), KM_ALGORITHM_RSA, &formats, &len)); |
Shawn Willden | 76076ab | 2014-12-18 08:36:35 -0700 | [diff] [blame] | 183 | EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_X509, formats, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 184 | free(formats); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 185 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 186 | EXPECT_EQ(KM_ERROR_OK, |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 187 | device()->get_supported_export_formats(device(), KM_ALGORITHM_EC, &formats, &len)); |
Shawn Willden | 76076ab | 2014-12-18 08:36:35 -0700 | [diff] [blame] | 188 | EXPECT_TRUE(ResponseContains(KM_KEY_FORMAT_X509, formats, len)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 189 | free(formats); |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 190 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 191 | EXPECT_EQ(KM_ERROR_OK, |
| 192 | device()->get_supported_export_formats(device(), KM_ALGORITHM_AES, &formats, &len)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 193 | EXPECT_EQ(0U, len); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 194 | free(formats); |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 195 | |
| 196 | EXPECT_EQ(KM_ERROR_OK, |
| 197 | device()->get_supported_export_formats(device(), KM_ALGORITHM_AES, &formats, &len)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 198 | EXPECT_EQ(0U, len); |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 199 | free(formats); |
| 200 | |
| 201 | EXPECT_EQ(KM_ERROR_OK, |
| 202 | device()->get_supported_export_formats(device(), KM_ALGORITHM_HMAC, &formats, &len)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 203 | EXPECT_EQ(0U, len); |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 204 | free(formats); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 207 | class NewKeyGeneration : public KeymasterTest { |
| 208 | protected: |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 209 | void CheckBaseParams() { |
| 210 | EXPECT_EQ(0U, hw_enforced().size()); |
| 211 | EXPECT_EQ(12U, hw_enforced().SerializedSize()); |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 212 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 213 | AuthorizationSet auths = sw_enforced(); |
| 214 | EXPECT_GT(auths.SerializedSize(), 12U); |
| 215 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 216 | EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 217 | EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_VERIFY)); |
| 218 | EXPECT_TRUE(contains(auths, TAG_USER_ID, 7)); |
Shawn Willden | eb63b97 | 2015-03-14 08:01:12 -0600 | [diff] [blame] | 219 | EXPECT_TRUE(contains(auths, TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(contains(auths, TAG_AUTH_TIMEOUT, 300)); |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 221 | |
| 222 | // Verify that App ID, App data and ROT are NOT included. |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 223 | EXPECT_FALSE(contains(auths, TAG_ROOT_OF_TRUST)); |
| 224 | EXPECT_FALSE(contains(auths, TAG_APPLICATION_ID)); |
| 225 | EXPECT_FALSE(contains(auths, TAG_APPLICATION_DATA)); |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 226 | |
| 227 | // Just for giggles, check that some unexpected tags/values are NOT present. |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 228 | EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
| 229 | EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_DECRYPT)); |
| 230 | EXPECT_FALSE(contains(auths, TAG_AUTH_TIMEOUT, 301)); |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 231 | |
| 232 | // Now check that unspecified, defaulted tags are correct. |
Shawn Willden | 0b2d333 | 2015-04-07 17:46:18 -0600 | [diff] [blame] | 233 | EXPECT_TRUE(contains(auths, TAG_ORIGIN, KM_ORIGIN_GENERATED)); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 234 | EXPECT_TRUE(contains(auths, KM_TAG_CREATION_DATETIME)); |
Shawn Willden | d077231 | 2014-09-18 12:27:57 -0600 | [diff] [blame] | 235 | } |
Shawn Willden | 2079ae8 | 2015-01-22 13:42:31 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 238 | TEST_F(NewKeyGeneration, Rsa) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 239 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 240 | .RsaSigningKey(256, 3) |
| 241 | .Digest(KM_DIGEST_NONE) |
| 242 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 243 | CheckBaseParams(); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 244 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 245 | // Check specified tags are all present in auths |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 246 | AuthorizationSet auths(sw_enforced()); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 247 | EXPECT_TRUE(contains(auths, TAG_ALGORITHM, KM_ALGORITHM_RSA)); |
| 248 | EXPECT_TRUE(contains(auths, TAG_KEY_SIZE, 256)); |
| 249 | EXPECT_TRUE(contains(auths, TAG_RSA_PUBLIC_EXPONENT, 3)); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 252 | TEST_F(NewKeyGeneration, RsaDefaultSize) { |
Shawn Willden | 3b4e165 | 2015-02-27 13:33:01 -0700 | [diff] [blame] | 253 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE, |
| 254 | GenerateKey(AuthorizationSetBuilder() |
| 255 | .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA) |
| 256 | .Authorization(TAG_RSA_PUBLIC_EXPONENT, 3) |
| 257 | .SigningKey())); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 258 | } |
| 259 | |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 260 | TEST_F(NewKeyGeneration, Ecdsa) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 261 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 262 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(224).Digest(KM_DIGEST_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 263 | CheckBaseParams(); |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 264 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 265 | // Check specified tags are all present in unenforced characteristics |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 266 | EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_EC)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 224)); |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 268 | } |
| 269 | |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 270 | TEST_F(NewKeyGeneration, EcdsaDefaultSize) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 271 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 272 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(224).Digest(KM_DIGEST_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 273 | CheckBaseParams(); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 274 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 275 | // Check specified tags are all present in unenforced characteristics |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 276 | EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_EC)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 277 | |
| 278 | // Now check that unspecified, defaulted tags are correct. |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 279 | EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 224)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | TEST_F(NewKeyGeneration, EcdsaInvalidSize) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 283 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_SIZE, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 284 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(190).Digest(KM_DIGEST_NONE))); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(NewKeyGeneration, EcdsaAllValidSizes) { |
Shawn Willden | 8c856c8 | 2014-09-26 09:34:36 -0600 | [diff] [blame] | 288 | size_t valid_sizes[] = {224, 256, 384, 521}; |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 289 | for (size_t size : valid_sizes) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 290 | EXPECT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(size).Digest( |
| 291 | KM_DIGEST_NONE))) |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 292 | << "Failed to generate size: " << size; |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 296 | TEST_F(NewKeyGeneration, HmacSha256) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 297 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 298 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_256))); |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 301 | typedef KeymasterTest GetKeyCharacteristics; |
| 302 | TEST_F(GetKeyCharacteristics, SimpleRsa) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 303 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 304 | .RsaSigningKey(256, 3) |
| 305 | .Digest(KM_DIGEST_NONE) |
| 306 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 307 | AuthorizationSet original(sw_enforced()); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 308 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 309 | ASSERT_EQ(KM_ERROR_OK, GetCharacteristics()); |
| 310 | EXPECT_EQ(original, sw_enforced()); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 313 | typedef KeymasterTest SigningOperationsTest; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 314 | TEST_F(SigningOperationsTest, RsaSuccess) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 315 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 316 | .RsaSigningKey(256, 3) |
| 317 | .Digest(KM_DIGEST_NONE) |
| 318 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 319 | string message = "12345678901234567890123456789012"; |
| 320 | string signature; |
| 321 | SignMessage(message, &signature); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 322 | } |
| 323 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 324 | TEST_F(SigningOperationsTest, RsaSha256DigestSuccess) { |
| 325 | // Note that without padding, key size must exactly match digest size. |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 326 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 327 | .RsaSigningKey(256, 3) |
| 328 | .Digest(KM_DIGEST_SHA_2_256) |
| 329 | .Padding(KM_PAD_NONE))); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 330 | string message(1024, 'a'); |
| 331 | string signature; |
| 332 | SignMessage(message, &signature); |
| 333 | } |
| 334 | |
| 335 | TEST_F(SigningOperationsTest, RsaPssSha256Success) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 336 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 337 | .RsaSigningKey(512, 3) |
| 338 | .Digest(KM_DIGEST_SHA_2_256) |
| 339 | .Padding(KM_PAD_RSA_PSS))); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 340 | // Use large message, which won't work without digesting. |
| 341 | string message(1024, 'a'); |
| 342 | string signature; |
| 343 | SignMessage(message, &signature); |
| 344 | } |
| 345 | |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 346 | TEST_F(SigningOperationsTest, RsaPkcs1Sha256Success) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 347 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 348 | .RsaSigningKey(512, 3) |
| 349 | .Digest(KM_DIGEST_SHA_2_256) |
| 350 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN))); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 351 | string message(1024, 'a'); |
| 352 | string signature; |
| 353 | SignMessage(message, &signature); |
| 354 | } |
| 355 | |
| 356 | TEST_F(SigningOperationsTest, RsaPssSha256TooSmallKey) { |
| 357 | // Key must be at least 10 bytes larger than hash, to provide minimal random salt, so verify |
| 358 | // that 9 bytes larger than hash won't work. |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 359 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 360 | .RsaSigningKey(256 + 9 * 8, 3) |
| 361 | .Digest(KM_DIGEST_SHA_2_256) |
| 362 | .Padding(KM_PAD_RSA_PSS))); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 363 | string message(1024, 'a'); |
| 364 | string signature; |
| 365 | |
| 366 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN)); |
| 367 | |
| 368 | string result; |
| 369 | size_t input_consumed; |
| 370 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 371 | EXPECT_EQ(message.size(), input_consumed); |
| 372 | EXPECT_EQ(KM_ERROR_INCOMPATIBLE_DIGEST, FinishOperation(signature, &result)); |
| 373 | } |
| 374 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 375 | TEST_F(SigningOperationsTest, RsaAbort) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 376 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 377 | .RsaSigningKey(256, 3) |
| 378 | .Digest(KM_DIGEST_NONE) |
| 379 | .Padding(KM_PAD_NONE))); |
Shawn Willden | dfa1c03 | 2015-02-07 00:39:01 -0700 | [diff] [blame] | 380 | AuthorizationSet input_params, output_params; |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 381 | ASSERT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN)); |
| 382 | EXPECT_EQ(KM_ERROR_OK, AbortOperation()); |
| 383 | // Another abort should fail |
| 384 | EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, AbortOperation()); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | TEST_F(SigningOperationsTest, RsaUnsupportedDigest) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 388 | GenerateKey(AuthorizationSetBuilder() |
| 389 | .RsaSigningKey(256, 3) |
| 390 | .Digest(KM_DIGEST_MD5) |
| 391 | .Padding(KM_PAD_RSA_PSS /* supported padding */)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 392 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, BeginOperation(KM_PURPOSE_SIGN)); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | TEST_F(SigningOperationsTest, RsaUnsupportedPadding) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 396 | GenerateKey(AuthorizationSetBuilder() |
| 397 | .RsaSigningKey(256, 3) |
| 398 | .Digest(KM_DIGEST_SHA_2_256 /* supported digest */) |
| 399 | .Padding(KM_PAD_PKCS7)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 400 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_SIGN)); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | TEST_F(SigningOperationsTest, RsaNoDigest) { |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 404 | // Digest must be specified. |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 405 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaKey(256, 3).SigningKey())); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 406 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_DIGEST, BeginOperation(KM_PURPOSE_SIGN)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 407 | // PSS requires a digest. |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 408 | GenerateKey(AuthorizationSetBuilder() |
| 409 | .RsaSigningKey(256, 3) |
| 410 | .Digest(KM_DIGEST_NONE) |
| 411 | .Padding(KM_PAD_RSA_PSS)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 412 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_DIGEST, BeginOperation(KM_PURPOSE_SIGN)); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST_F(SigningOperationsTest, RsaNoPadding) { |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 416 | // Padding must be specified |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 417 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaKey(256, 3).SigningKey().Digest( |
| 418 | KM_DIGEST_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 419 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_SIGN)); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 420 | } |
| 421 | |
Shawn Willden | edb7994 | 2015-05-08 06:46:44 -0600 | [diff] [blame^] | 422 | TEST_F(SigningOperationsTest, RsaTooShortMessage) { |
| 423 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 424 | .RsaSigningKey(256, 3) |
| 425 | .Digest(KM_DIGEST_NONE) |
| 426 | .Padding(KM_PAD_NONE))); |
| 427 | ASSERT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_SIGN)); |
| 428 | |
| 429 | string message = "1234567890123456789012345678901"; |
| 430 | string result; |
| 431 | size_t input_consumed; |
| 432 | ASSERT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 433 | EXPECT_EQ(0U, result.size()); |
| 434 | EXPECT_EQ(31U, input_consumed); |
| 435 | |
| 436 | string signature; |
| 437 | ASSERT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&signature)); |
| 438 | EXPECT_EQ(0U, signature.length()); |
| 439 | } |
| 440 | |
| 441 | TEST_F(SigningOperationsTest, RsaSignWithEncryptionKey) { |
| 442 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 443 | .RsaEncryptionKey(256, 3) |
| 444 | .Digest(KM_DIGEST_NONE) |
| 445 | .Padding(KM_PAD_NONE))); |
| 446 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_SIGN)); |
| 447 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_VERIFY)); |
| 448 | } |
| 449 | |
| 450 | TEST_F(SigningOperationsTest, EcdsaSuccess) { |
| 451 | ASSERT_EQ(KM_ERROR_OK, |
| 452 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(224).Digest(KM_DIGEST_NONE))); |
| 453 | string message = "123456789012345678901234567890123456789012345678"; |
| 454 | string signature; |
| 455 | SignMessage(message, &signature); |
| 456 | } |
| 457 | |
| 458 | TEST_F(SigningOperationsTest, AesEcbSign) { |
| 459 | ASSERT_EQ(KM_ERROR_OK, |
| 460 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 461 | TAG_BLOCK_MODE, KM_MODE_ECB))); |
| 462 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_SIGN)); |
| 463 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_VERIFY)); |
| 464 | } |
| 465 | |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 466 | TEST_F(SigningOperationsTest, HmacSha1Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 467 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA1)); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 468 | string message = "12345678901234567890123456789012"; |
| 469 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 470 | MacMessage(message, &signature, 160); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 471 | ASSERT_EQ(20U, signature.size()); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 474 | TEST_F(SigningOperationsTest, HmacSha224Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 475 | ASSERT_EQ(KM_ERROR_OK, |
| 476 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_224))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 477 | string message = "12345678901234567890123456789012"; |
| 478 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 479 | MacMessage(message, &signature, 224); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 480 | ASSERT_EQ(28U, signature.size()); |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 483 | TEST_F(SigningOperationsTest, HmacSha256Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 484 | ASSERT_EQ(KM_ERROR_OK, |
| 485 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_256))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 486 | string message = "12345678901234567890123456789012"; |
| 487 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 488 | MacMessage(message, &signature, 256); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 489 | ASSERT_EQ(32U, signature.size()); |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 492 | TEST_F(SigningOperationsTest, HmacSha384Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 493 | ASSERT_EQ(KM_ERROR_OK, |
| 494 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_384))); |
| 495 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 496 | string message = "12345678901234567890123456789012"; |
| 497 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 498 | MacMessage(message, &signature, 384); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 499 | ASSERT_EQ(48U, signature.size()); |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | TEST_F(SigningOperationsTest, HmacSha512Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 503 | ASSERT_EQ(KM_ERROR_OK, |
| 504 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_512))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 505 | string message = "12345678901234567890123456789012"; |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 506 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 507 | MacMessage(message, &signature, 512); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 508 | ASSERT_EQ(64U, signature.size()); |
Shawn Willden | 62c2286 | 2014-12-17 08:36:20 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 511 | TEST_F(SigningOperationsTest, HmacLengthInKey) { |
| 512 | // TODO(swillden): unified API should generate an error on key generation. |
| 513 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 514 | .HmacKey(128) |
| 515 | .Digest(KM_DIGEST_SHA_2_256) |
| 516 | .Authorization(TAG_MAC_LENGTH, 20))); |
| 517 | string message = "12345678901234567890123456789012"; |
| 518 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 519 | MacMessage(message, &signature, 240); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 520 | // Size in key was ignored. |
| 521 | ASSERT_EQ(30U, signature.size()); |
| 522 | } |
| 523 | |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 524 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase1) { |
| 525 | uint8_t key_data[] = { |
| 526 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 527 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 528 | }; |
| 529 | string message = "Hi There"; |
| 530 | uint8_t sha_224_expected[] = { |
| 531 | 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19, 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, |
| 532 | 0xf3, 0x3f, 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f, 0x53, 0x68, 0x4b, 0x22, |
| 533 | }; |
| 534 | uint8_t sha_256_expected[] = { |
| 535 | 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, |
| 536 | 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, |
| 537 | 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7, |
| 538 | }; |
| 539 | uint8_t sha_384_expected[] = { |
| 540 | 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08, 0x25, 0xf4, |
| 541 | 0xab, 0x46, 0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6, |
| 542 | 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, |
| 543 | 0x07, 0x6e, 0xde, 0x7f, 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6, |
| 544 | }; |
| 545 | uint8_t sha_512_expected[] = { |
| 546 | 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0, 0xb4, 0x24, 0x1a, |
| 547 | 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, |
| 548 | 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, |
| 549 | 0x02, 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d, 0x91, 0x4e, |
| 550 | 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54, |
| 551 | }; |
| 552 | |
| 553 | string key = make_string(key_data); |
| 554 | |
| 555 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 556 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 557 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 558 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 559 | } |
| 560 | |
| 561 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase2) { |
| 562 | string key = "Jefe"; |
| 563 | string message = "what do ya want for nothing?"; |
| 564 | uint8_t sha_224_expected[] = { |
| 565 | 0xa3, 0x0e, 0x01, 0x09, 0x8b, 0xc6, 0xdb, 0xbf, 0x45, 0x69, 0x0f, 0x3a, 0x7e, 0x9e, |
| 566 | 0x6d, 0x0f, 0x8b, 0xbe, 0xa2, 0xa3, 0x9e, 0x61, 0x48, 0x00, 0x8f, 0xd0, 0x5e, 0x44, |
| 567 | }; |
| 568 | uint8_t sha_256_expected[] = { |
| 569 | 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, |
| 570 | 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, |
| 571 | 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43, |
| 572 | }; |
| 573 | uint8_t sha_384_expected[] = { |
| 574 | 0xaf, 0x45, 0xd2, 0xe3, 0x76, 0x48, 0x40, 0x31, 0x61, 0x7f, 0x78, 0xd2, |
| 575 | 0xb5, 0x8a, 0x6b, 0x1b, 0x9c, 0x7e, 0xf4, 0x64, 0xf5, 0xa0, 0x1b, 0x47, |
| 576 | 0xe4, 0x2e, 0xc3, 0x73, 0x63, 0x22, 0x44, 0x5e, 0x8e, 0x22, 0x40, 0xca, |
| 577 | 0x5e, 0x69, 0xe2, 0xc7, 0x8b, 0x32, 0x39, 0xec, 0xfa, 0xb2, 0x16, 0x49, |
| 578 | }; |
| 579 | uint8_t sha_512_expected[] = { |
| 580 | 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2, 0xe3, 0x95, 0xfb, 0xe7, 0x3b, |
| 581 | 0x56, 0xe0, 0xa3, 0x87, 0xbd, 0x64, 0x22, 0x2e, 0x83, 0x1f, 0xd6, 0x10, 0x27, |
| 582 | 0x0c, 0xd7, 0xea, 0x25, 0x05, 0x54, 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, |
| 583 | 0x4a, 0x6d, 0x03, 0x4f, 0x65, 0xf8, 0xf0, 0xe6, 0xfd, 0xca, 0xea, 0xb1, 0xa3, |
| 584 | 0x4d, 0x4a, 0x6b, 0x4b, 0x63, 0x6e, 0x07, 0x0a, 0x38, 0xbc, 0xe7, 0x37, |
| 585 | }; |
| 586 | |
| 587 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 588 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 589 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 590 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 591 | } |
| 592 | |
| 593 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase3) { |
| 594 | string key(20, 0xaa); |
| 595 | string message(50, 0xdd); |
| 596 | uint8_t sha_224_expected[] = { |
| 597 | 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9, 0x69, 0x4d, 0x7d, 0x6a, |
| 598 | 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1, 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea, |
| 599 | }; |
| 600 | uint8_t sha_256_expected[] = { |
| 601 | 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, |
| 602 | 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, |
| 603 | 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe, |
| 604 | }; |
| 605 | uint8_t sha_384_expected[] = { |
| 606 | 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2, 0xac, 0xe0, |
| 607 | 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9, 0x47, 0xac, 0x9f, 0xeb, |
| 608 | 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, |
| 609 | 0xc1, 0x38, 0x14, 0xb9, 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27, |
| 610 | }; |
| 611 | uint8_t sha_512_expected[] = { |
| 612 | 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0, 0xf0, 0x75, 0x6c, |
| 613 | 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd, 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, |
| 614 | 0x3e, 0x33, 0xb2, 0x27, 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, |
| 615 | 0xc8, 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46, 0xa3, 0x37, |
| 616 | 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59, 0xe1, 0x32, 0x92, 0xfb, |
| 617 | }; |
| 618 | |
| 619 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 620 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 621 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 622 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 623 | } |
| 624 | |
| 625 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase4) { |
| 626 | uint8_t key_data[25] = { |
| 627 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, |
| 628 | 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, |
| 629 | }; |
| 630 | string key = make_string(key_data); |
| 631 | string message(50, 0xcd); |
| 632 | uint8_t sha_224_expected[] = { |
| 633 | 0x6c, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3c, 0xac, 0x6a, 0x2a, 0xbc, 0x1b, 0xb3, 0x82, |
| 634 | 0x62, 0x7c, 0xec, 0x6a, 0x90, 0xd8, 0x6e, 0xfc, 0x01, 0x2d, 0xe7, 0xaf, 0xec, 0x5a, |
| 635 | }; |
| 636 | uint8_t sha_256_expected[] = { |
| 637 | 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, |
| 638 | 0x98, 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, |
| 639 | 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b, |
| 640 | }; |
| 641 | uint8_t sha_384_expected[] = { |
| 642 | 0x3e, 0x8a, 0x69, 0xb7, 0x78, 0x3c, 0x25, 0x85, 0x19, 0x33, 0xab, 0x62, |
| 643 | 0x90, 0xaf, 0x6c, 0xa7, 0x7a, 0x99, 0x81, 0x48, 0x08, 0x50, 0x00, 0x9c, |
| 644 | 0xc5, 0x57, 0x7c, 0x6e, 0x1f, 0x57, 0x3b, 0x4e, 0x68, 0x01, 0xdd, 0x23, |
| 645 | 0xc4, 0xa7, 0xd6, 0x79, 0xcc, 0xf8, 0xa3, 0x86, 0xc6, 0x74, 0xcf, 0xfb, |
| 646 | }; |
| 647 | uint8_t sha_512_expected[] = { |
| 648 | 0xb0, 0xba, 0x46, 0x56, 0x37, 0x45, 0x8c, 0x69, 0x90, 0xe5, 0xa8, 0xc5, 0xf6, |
| 649 | 0x1d, 0x4a, 0xf7, 0xe5, 0x76, 0xd9, 0x7f, 0xf9, 0x4b, 0x87, 0x2d, 0xe7, 0x6f, |
| 650 | 0x80, 0x50, 0x36, 0x1e, 0xe3, 0xdb, 0xa9, 0x1c, 0xa5, 0xc1, 0x1a, 0xa2, 0x5e, |
| 651 | 0xb4, 0xd6, 0x79, 0x27, 0x5c, 0xc5, 0x78, 0x80, 0x63, 0xa5, 0xf1, 0x97, 0x41, |
| 652 | 0x12, 0x0c, 0x4f, 0x2d, 0xe2, 0xad, 0xeb, 0xeb, 0x10, 0xa2, 0x98, 0xdd, |
| 653 | }; |
| 654 | |
| 655 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 656 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 657 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 658 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 659 | } |
| 660 | |
| 661 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase5) { |
| 662 | string key(20, 0x0c); |
| 663 | string message = "Test With Truncation"; |
| 664 | |
| 665 | uint8_t sha_224_expected[] = { |
| 666 | 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, |
| 667 | 0xc9, 0x88, 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8, |
| 668 | }; |
| 669 | uint8_t sha_256_expected[] = { |
| 670 | 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, |
| 671 | 0x6e, 0x0c, 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b, |
| 672 | }; |
| 673 | uint8_t sha_384_expected[] = { |
| 674 | 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, |
| 675 | 0xa4, 0x6e, 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97, |
| 676 | }; |
| 677 | uint8_t sha_512_expected[] = { |
| 678 | 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, |
| 679 | 0x1d, 0x41, 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6, |
| 680 | }; |
| 681 | |
| 682 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 683 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 684 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 685 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 686 | } |
| 687 | |
| 688 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase6) { |
| 689 | string key(131, 0xaa); |
| 690 | string message = "Test Using Larger Than Block-Size Key - Hash Key First"; |
| 691 | |
| 692 | uint8_t sha_224_expected[] = { |
| 693 | 0x95, 0xe9, 0xa0, 0xdb, 0x96, 0x20, 0x95, 0xad, 0xae, 0xbe, 0x9b, 0x2d, 0x6f, 0x0d, |
| 694 | 0xbc, 0xe2, 0xd4, 0x99, 0xf1, 0x12, 0xf2, 0xd2, 0xb7, 0x27, 0x3f, 0xa6, 0x87, 0x0e, |
| 695 | }; |
| 696 | uint8_t sha_256_expected[] = { |
| 697 | 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, |
| 698 | 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, |
| 699 | 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54, |
| 700 | }; |
| 701 | uint8_t sha_384_expected[] = { |
| 702 | 0x4e, 0xce, 0x08, 0x44, 0x85, 0x81, 0x3e, 0x90, 0x88, 0xd2, 0xc6, 0x3a, |
| 703 | 0x04, 0x1b, 0xc5, 0xb4, 0x4f, 0x9e, 0xf1, 0x01, 0x2a, 0x2b, 0x58, 0x8f, |
| 704 | 0x3c, 0xd1, 0x1f, 0x05, 0x03, 0x3a, 0xc4, 0xc6, 0x0c, 0x2e, 0xf6, 0xab, |
| 705 | 0x40, 0x30, 0xfe, 0x82, 0x96, 0x24, 0x8d, 0xf1, 0x63, 0xf4, 0x49, 0x52, |
| 706 | }; |
| 707 | uint8_t sha_512_expected[] = { |
| 708 | 0x80, 0xb2, 0x42, 0x63, 0xc7, 0xc1, 0xa3, 0xeb, 0xb7, 0x14, 0x93, 0xc1, 0xdd, |
| 709 | 0x7b, 0xe8, 0xb4, 0x9b, 0x46, 0xd1, 0xf4, 0x1b, 0x4a, 0xee, 0xc1, 0x12, 0x1b, |
| 710 | 0x01, 0x37, 0x83, 0xf8, 0xf3, 0x52, 0x6b, 0x56, 0xd0, 0x37, 0xe0, 0x5f, 0x25, |
| 711 | 0x98, 0xbd, 0x0f, 0xd2, 0x21, 0x5d, 0x6a, 0x1e, 0x52, 0x95, 0xe6, 0x4f, 0x73, |
| 712 | 0xf6, 0x3f, 0x0a, 0xec, 0x8b, 0x91, 0x5a, 0x98, 0x5d, 0x78, 0x65, 0x98, |
| 713 | }; |
| 714 | |
| 715 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 716 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 717 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 718 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 719 | } |
| 720 | |
| 721 | TEST_F(SigningOperationsTest, HmacRfc4231TestCase7) { |
| 722 | string key(131, 0xaa); |
| 723 | string message = "This is a test using a larger than block-size key and a larger than " |
| 724 | "block-size data. The key needs to be hashed before being used by the HMAC " |
| 725 | "algorithm."; |
| 726 | |
| 727 | uint8_t sha_224_expected[] = { |
| 728 | 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02, 0x3f, 0x54, 0xd5, 0x17, 0xd0, 0xb3, |
| 729 | 0x9d, 0xbd, 0x94, 0x67, 0x70, 0xdb, 0x9c, 0x2b, 0x95, 0xc9, 0xf6, 0xf5, 0x65, 0xd1, |
| 730 | }; |
| 731 | uint8_t sha_256_expected[] = { |
| 732 | 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, |
| 733 | 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, |
| 734 | 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2, |
| 735 | }; |
| 736 | uint8_t sha_384_expected[] = { |
| 737 | 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, 0x35, 0x1e, 0x2f, 0x25, |
| 738 | 0x4e, 0x8f, 0xd3, 0x2c, 0x60, 0x24, 0x20, 0xfe, 0xb0, 0xb8, 0xfb, 0x9a, |
| 739 | 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e, 0x99, 0xc5, 0xa6, 0x78, 0xcc, 0x31, |
| 740 | 0xe7, 0x99, 0x17, 0x6d, 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e, |
| 741 | }; |
| 742 | uint8_t sha_512_expected[] = { |
| 743 | 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, 0xa4, 0xdf, 0xa9, 0xf9, 0x6e, |
| 744 | 0x5e, 0x3f, 0xfd, 0xde, 0xbd, 0x71, 0xf8, 0x86, 0x72, 0x89, 0x86, 0x5d, 0xf5, |
| 745 | 0xa3, 0x2d, 0x20, 0xcd, 0xc9, 0x44, 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, |
| 746 | 0xb1, 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, 0x13, 0x46, 0x76, 0xfb, |
| 747 | 0x6d, 0xe0, 0x44, 0x60, 0x65, 0xc9, 0x74, 0x40, 0xfa, 0x8c, 0x6a, 0x58, |
| 748 | }; |
| 749 | |
| 750 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_224, make_string(sha_224_expected)); |
| 751 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_256, make_string(sha_256_expected)); |
| 752 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_384, make_string(sha_384_expected)); |
| 753 | CheckHmacTestVector(key, message, KM_DIGEST_SHA_2_512, make_string(sha_512_expected)); |
| 754 | } |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 755 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 756 | TEST_F(SigningOperationsTest, HmacSha256TooLargeMacLength) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 757 | ASSERT_EQ(KM_ERROR_OK, |
| 758 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_256))); |
| 759 | AuthorizationSet begin_params(client_params()); |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 760 | begin_params.push_back(TAG_MAC_LENGTH, 264); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 761 | ASSERT_EQ(KM_ERROR_OK, |
| 762 | BeginOperation(KM_PURPOSE_SIGN, begin_params, nullptr /* output_params */)); |
| 763 | string message = "1234567890123456789012345678901"; |
| 764 | string result; |
| 765 | size_t input_consumed; |
| 766 | ASSERT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 767 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_MAC_LENGTH, FinishOperation(&result)); |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 770 | // TODO(swillden): Add more verification failure tests. |
| 771 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 772 | typedef KeymasterTest VerificationOperationsTest; |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 773 | TEST_F(VerificationOperationsTest, RsaSuccess) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 774 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 775 | .RsaSigningKey(256, 3) |
| 776 | .Digest(KM_DIGEST_NONE) |
| 777 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 778 | string message = "12345678901234567890123456789012"; |
| 779 | string signature; |
| 780 | SignMessage(message, &signature); |
| 781 | VerifyMessage(message, signature); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 782 | } |
| 783 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 784 | TEST_F(VerificationOperationsTest, RsaSha256DigestSuccess) { |
| 785 | // Note that without padding, key size must exactly match digest size. |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 786 | GenerateKey(AuthorizationSetBuilder() |
| 787 | .RsaSigningKey(256, 3) |
| 788 | .Digest(KM_DIGEST_SHA_2_256) |
| 789 | .Padding(KM_PAD_NONE)); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 790 | string message(1024, 'a'); |
| 791 | string signature; |
| 792 | SignMessage(message, &signature); |
| 793 | VerifyMessage(message, signature); |
| 794 | } |
| 795 | |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 796 | TEST_F(VerificationOperationsTest, RsaSha256CorruptSignature) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 797 | GenerateKey(AuthorizationSetBuilder() |
| 798 | .RsaSigningKey(256, 3) |
| 799 | .Digest(KM_DIGEST_SHA_2_256) |
| 800 | .Padding(KM_PAD_NONE)); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 801 | string message(1024, 'a'); |
| 802 | string signature; |
| 803 | SignMessage(message, &signature); |
| 804 | ++signature[signature.size() / 2]; |
| 805 | |
| 806 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY)); |
| 807 | |
| 808 | string result; |
| 809 | size_t input_consumed; |
| 810 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 811 | EXPECT_EQ(message.size(), input_consumed); |
| 812 | EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result)); |
| 813 | } |
| 814 | |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 815 | TEST_F(VerificationOperationsTest, RsaPssSha256Success) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 816 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 817 | .RsaSigningKey(512, 3) |
| 818 | .Digest(KM_DIGEST_SHA_2_256) |
| 819 | .Padding(KM_PAD_RSA_PSS))); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 820 | // Use large message, which won't work without digesting. |
| 821 | string message(1024, 'a'); |
| 822 | string signature; |
| 823 | SignMessage(message, &signature); |
| 824 | VerifyMessage(message, signature); |
| 825 | } |
| 826 | |
| 827 | TEST_F(VerificationOperationsTest, RsaPssSha256CorruptSignature) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 828 | GenerateKey(AuthorizationSetBuilder() |
| 829 | .RsaSigningKey(512, 3) |
| 830 | .Digest(KM_DIGEST_SHA_2_256) |
| 831 | .Padding(KM_PAD_RSA_PSS)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 832 | string message(1024, 'a'); |
| 833 | string signature; |
| 834 | SignMessage(message, &signature); |
| 835 | ++signature[signature.size() / 2]; |
| 836 | |
| 837 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY)); |
| 838 | |
| 839 | string result; |
| 840 | size_t input_consumed; |
| 841 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 842 | EXPECT_EQ(message.size(), input_consumed); |
| 843 | EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result)); |
| 844 | } |
| 845 | |
| 846 | TEST_F(VerificationOperationsTest, RsaPssSha256CorruptInput) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 847 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 848 | .RsaSigningKey(512, 3) |
| 849 | .Digest(KM_DIGEST_SHA_2_256) |
| 850 | .Padding(KM_PAD_RSA_PSS))); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 851 | // Use large message, which won't work without digesting. |
| 852 | string message(1024, 'a'); |
| 853 | string signature; |
| 854 | SignMessage(message, &signature); |
| 855 | ++message[message.size() / 2]; |
| 856 | |
| 857 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY)); |
| 858 | |
| 859 | string result; |
| 860 | size_t input_consumed; |
| 861 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 862 | EXPECT_EQ(message.size(), input_consumed); |
| 863 | EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result)); |
| 864 | } |
| 865 | |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 866 | TEST_F(VerificationOperationsTest, RsaPkcs1Sha256Success) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 867 | GenerateKey(AuthorizationSetBuilder() |
| 868 | .RsaSigningKey(512, 3) |
| 869 | .Digest(KM_DIGEST_SHA_2_256) |
| 870 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 871 | string message(1024, 'a'); |
| 872 | string signature; |
| 873 | SignMessage(message, &signature); |
| 874 | VerifyMessage(message, signature); |
| 875 | } |
| 876 | |
| 877 | TEST_F(VerificationOperationsTest, RsaPkcs1Sha256CorruptSignature) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 878 | GenerateKey(AuthorizationSetBuilder() |
| 879 | .RsaSigningKey(512, 3) |
| 880 | .Digest(KM_DIGEST_SHA_2_256) |
| 881 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 882 | string message(1024, 'a'); |
| 883 | string signature; |
| 884 | SignMessage(message, &signature); |
| 885 | ++signature[signature.size() / 2]; |
| 886 | |
| 887 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY)); |
| 888 | |
| 889 | string result; |
| 890 | size_t input_consumed; |
| 891 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 892 | EXPECT_EQ(message.size(), input_consumed); |
| 893 | EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result)); |
| 894 | } |
| 895 | |
| 896 | TEST_F(VerificationOperationsTest, RsaPkcs1Sha256CorruptInput) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 897 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 898 | .RsaSigningKey(512, 3) |
| 899 | .Digest(KM_DIGEST_SHA_2_256) |
| 900 | .Padding(KM_PAD_RSA_PKCS1_1_5_SIGN))); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 901 | // Use large message, which won't work without digesting. |
| 902 | string message(1024, 'a'); |
| 903 | string signature; |
| 904 | SignMessage(message, &signature); |
| 905 | ++message[message.size() / 2]; |
| 906 | |
| 907 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_VERIFY)); |
| 908 | |
| 909 | string result; |
| 910 | size_t input_consumed; |
| 911 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
| 912 | EXPECT_EQ(message.size(), input_consumed); |
| 913 | EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED, FinishOperation(signature, &result)); |
| 914 | } |
| 915 | |
| 916 | template <typename T> vector<T> make_vector(const T* array, size_t len) { |
| 917 | return vector<T>(array, array + len); |
| 918 | } |
| 919 | |
| 920 | TEST_F(VerificationOperationsTest, RsaAllDigestAndPadCombinations) { |
| 921 | // Get all supported digests and padding modes. |
| 922 | size_t digests_len; |
| 923 | keymaster_digest_t* digests; |
| 924 | EXPECT_EQ(KM_ERROR_OK, |
| 925 | device()->get_supported_digests(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, &digests, |
| 926 | &digests_len)); |
| 927 | |
| 928 | size_t padding_modes_len; |
| 929 | keymaster_padding_t* padding_modes; |
| 930 | EXPECT_EQ(KM_ERROR_OK, |
| 931 | device()->get_supported_padding_modes(device(), KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, |
| 932 | &padding_modes, &padding_modes_len)); |
| 933 | |
| 934 | // Try them. |
| 935 | for (keymaster_padding_t padding_mode : make_vector(padding_modes, padding_modes_len)) { |
| 936 | for (keymaster_digest_t digest : make_vector(digests, digests_len)) { |
| 937 | // Compute key & message size that will work. |
| 938 | size_t key_bits = 256; |
| 939 | size_t message_len = 1000; |
| 940 | switch (digest) { |
| 941 | case KM_DIGEST_NONE: |
| 942 | switch (padding_mode) { |
| 943 | case KM_PAD_NONE: |
| 944 | // Match key size. |
| 945 | message_len = key_bits / 8; |
| 946 | break; |
| 947 | case KM_PAD_RSA_PKCS1_1_5_SIGN: |
| 948 | message_len = key_bits / 8 - 11; |
| 949 | break; |
| 950 | case KM_PAD_RSA_PSS: |
| 951 | // PSS requires a digest. |
| 952 | continue; |
| 953 | default: |
| 954 | FAIL() << "Missing padding"; |
| 955 | break; |
| 956 | } |
| 957 | break; |
| 958 | |
| 959 | case KM_DIGEST_SHA_2_256: |
| 960 | switch (padding_mode) { |
| 961 | case KM_PAD_NONE: |
| 962 | // Key size matches digest size |
| 963 | break; |
| 964 | case KM_PAD_RSA_PKCS1_1_5_SIGN: |
| 965 | key_bits += 8 * 11; |
| 966 | break; |
| 967 | case KM_PAD_RSA_PSS: |
| 968 | key_bits += 8 * 10; |
| 969 | break; |
| 970 | default: |
| 971 | FAIL() << "Missing padding"; |
| 972 | break; |
| 973 | } |
| 974 | break; |
| 975 | default: |
| 976 | FAIL() << "Missing digest"; |
| 977 | } |
| 978 | |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 979 | GenerateKey(AuthorizationSetBuilder() |
| 980 | .RsaSigningKey(key_bits, 3) |
| 981 | .Digest(digest) |
| 982 | .Padding(padding_mode)); |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 983 | string message(message_len, 'a'); |
| 984 | string signature; |
| 985 | SignMessage(message, &signature); |
| 986 | VerifyMessage(message, signature); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | free(padding_modes); |
| 991 | free(digests); |
| 992 | } |
| 993 | |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 994 | TEST_F(VerificationOperationsTest, EcdsaSuccess) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 995 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 996 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(256).Digest(KM_DIGEST_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 997 | string message = "123456789012345678901234567890123456789012345678"; |
| 998 | string signature; |
| 999 | SignMessage(message, &signature); |
| 1000 | VerifyMessage(message, signature); |
Shawn Willden | 5ac2f8f | 2014-08-18 15:33:10 -0600 | [diff] [blame] | 1001 | } |
| 1002 | |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1003 | TEST_F(VerificationOperationsTest, HmacSha1Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1004 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA1)); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1005 | string message = "123456789012345678901234567890123456789012345678"; |
| 1006 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 1007 | MacMessage(message, &signature, 160); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1008 | VerifyMessage(message, signature); |
| 1009 | } |
| 1010 | |
| 1011 | TEST_F(VerificationOperationsTest, HmacSha224Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1012 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_224)); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1013 | string message = "123456789012345678901234567890123456789012345678"; |
| 1014 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 1015 | MacMessage(message, &signature, 224); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1016 | VerifyMessage(message, signature); |
| 1017 | } |
| 1018 | |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 1019 | TEST_F(VerificationOperationsTest, HmacSha256Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1020 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_256)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1021 | string message = "123456789012345678901234567890123456789012345678"; |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 1022 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 1023 | MacMessage(message, &signature, 256); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1024 | VerifyMessage(message, signature); |
Shawn Willden | 0d560bf | 2014-12-15 17:44:02 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1027 | TEST_F(VerificationOperationsTest, HmacSha384Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1028 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_384)); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1029 | string message = "123456789012345678901234567890123456789012345678"; |
| 1030 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 1031 | MacMessage(message, &signature, 384); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1032 | VerifyMessage(message, signature); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(VerificationOperationsTest, HmacSha512Success) { |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1036 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_SHA_2_512)); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1037 | string message = "123456789012345678901234567890123456789012345678"; |
| 1038 | string signature; |
Shawn Willden | 0c60f6f | 2015-04-27 23:40:10 -0600 | [diff] [blame] | 1039 | MacMessage(message, &signature, 512); |
Shawn Willden | 51d5e0e | 2014-12-18 10:44:03 -0700 | [diff] [blame] | 1040 | VerifyMessage(message, signature); |
| 1041 | } |
| 1042 | |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1043 | typedef VerificationOperationsTest ExportKeyTest; |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 1044 | TEST_F(ExportKeyTest, RsaSuccess) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1045 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1046 | .RsaSigningKey(256, 3) |
| 1047 | .Digest(KM_DIGEST_NONE) |
| 1048 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1049 | string export_data; |
| 1050 | ASSERT_EQ(KM_ERROR_OK, ExportKey(KM_KEY_FORMAT_X509, &export_data)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1051 | EXPECT_GT(export_data.length(), 0U); |
Shawn Willden | e46a43f | 2014-08-27 10:35:36 -0600 | [diff] [blame] | 1052 | |
| 1053 | // TODO(swillden): Verify that the exported key is actually usable to verify signatures. |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 1054 | } |
| 1055 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 1056 | TEST_F(ExportKeyTest, EcdsaSuccess) { |
Shawn Willden | 84b8da5 | 2015-03-11 07:21:32 -0600 | [diff] [blame] | 1057 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1058 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(224).Digest(KM_DIGEST_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1059 | string export_data; |
| 1060 | ASSERT_EQ(KM_ERROR_OK, ExportKey(KM_KEY_FORMAT_X509, &export_data)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1061 | EXPECT_GT(export_data.length(), 0U); |
Shawn Willden | e46a43f | 2014-08-27 10:35:36 -0600 | [diff] [blame] | 1062 | |
| 1063 | // TODO(swillden): Verify that the exported key is actually usable to verify signatures. |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | TEST_F(ExportKeyTest, RsaUnsupportedKeyFormat) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1067 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1068 | .RsaSigningKey(256, 3) |
| 1069 | .Digest(KM_DIGEST_NONE) |
| 1070 | .Padding(KM_PAD_NONE))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1071 | string export_data; |
| 1072 | ASSERT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_PKCS8, &export_data)); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | TEST_F(ExportKeyTest, RsaCorruptedKeyBlob) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1076 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1077 | .RsaSigningKey(256, 3) |
| 1078 | .Digest(KM_DIGEST_NONE) |
| 1079 | .Padding(KM_PAD_NONE))); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1080 | corrupt_key_blob(); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1081 | string export_data; |
| 1082 | ASSERT_EQ(KM_ERROR_INVALID_KEY_BLOB, ExportKey(KM_KEY_FORMAT_X509, &export_data)); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 1083 | } |
| 1084 | |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 1085 | TEST_F(ExportKeyTest, AesKeyExportFails) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1086 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128))); |
Shawn Willden | 7dad93b | 2015-02-05 10:20:47 -0700 | [diff] [blame] | 1087 | string export_data; |
| 1088 | |
| 1089 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_X509, &export_data)); |
| 1090 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_PKCS8, &export_data)); |
| 1091 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_KEY_FORMAT, ExportKey(KM_KEY_FORMAT_RAW, &export_data)); |
| 1092 | } |
| 1093 | |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1094 | static string read_file(const string& file_name) { |
| 1095 | ifstream file_stream(file_name, std::ios::binary); |
| 1096 | istreambuf_iterator<char> file_begin(file_stream); |
| 1097 | istreambuf_iterator<char> file_end; |
| 1098 | return string(file_begin, file_end); |
| 1099 | } |
| 1100 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1101 | typedef VerificationOperationsTest ImportKeyTest; |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1102 | TEST_F(ImportKeyTest, RsaSuccess) { |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1103 | string pk8_key = read_file("rsa_privkey_pk8.der"); |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1104 | ASSERT_EQ(633U, pk8_key.size()); |
| 1105 | |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1106 | ASSERT_EQ(KM_ERROR_OK, ImportKey(AuthorizationSetBuilder() |
| 1107 | .RsaSigningKey(1024, 65537) |
| 1108 | .Digest(KM_DIGEST_NONE) |
| 1109 | .Padding(KM_PAD_NONE), |
Shawn Willden | c24c110 | 2014-12-22 15:30:09 -0700 | [diff] [blame] | 1110 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1111 | |
| 1112 | // Check values derived from the key. |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1113 | EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_RSA)); |
| 1114 | EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 1024)); |
| 1115 | EXPECT_TRUE(contains(sw_enforced(), TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1116 | |
| 1117 | // And values provided by GoogleKeymaster |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1118 | EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED)); |
| 1119 | EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME)); |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1120 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1121 | string message(1024 / 8, 'a'); |
| 1122 | string signature; |
| 1123 | SignMessage(message, &signature); |
| 1124 | VerifyMessage(message, signature); |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 1125 | } |
| 1126 | |
Shawn Willden | d7a5c71 | 2015-04-09 16:33:52 -0600 | [diff] [blame] | 1127 | TEST_F(ImportKeyTest, OldApiRsaSuccess) { |
| 1128 | string pk8_key = read_file("rsa_privkey_pk8.der"); |
| 1129 | ASSERT_EQ(633U, pk8_key.size()); |
| 1130 | |
| 1131 | // NOTE: This will break when the keymaster0 APIs are removed from keymaster1. But at that |
| 1132 | // point softkeymaster will no longer support keymaster0 APIs anyway. |
| 1133 | uint8_t* key_blob; |
| 1134 | size_t key_blob_length; |
| 1135 | ASSERT_EQ(0, |
| 1136 | device()->import_keypair(device(), reinterpret_cast<const uint8_t*>(pk8_key.data()), |
| 1137 | pk8_key.size(), &key_blob, &key_blob_length)); |
| 1138 | set_key_blob(key_blob, key_blob_length); |
| 1139 | |
| 1140 | string message(1024 / 8, 'a'); |
| 1141 | string signature; |
| 1142 | SignMessage(message, &signature, false /* use_client_params */); |
| 1143 | VerifyMessage(message, signature, false /* use_client_params */); |
| 1144 | } |
| 1145 | |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1146 | TEST_F(ImportKeyTest, RsaKeySizeMismatch) { |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1147 | string pk8_key = read_file("rsa_privkey_pk8.der"); |
| 1148 | ASSERT_EQ(633U, pk8_key.size()); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1149 | ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1150 | ImportKey(AuthorizationSetBuilder() |
| 1151 | .RsaSigningKey(2048 /* Doesn't match key */, 3) |
| 1152 | .Digest(KM_DIGEST_NONE) |
| 1153 | .Padding(KM_PAD_NONE), |
Shawn Willden | c24c110 | 2014-12-22 15:30:09 -0700 | [diff] [blame] | 1154 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | TEST_F(ImportKeyTest, RsaPublicExponenMismatch) { |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1158 | string pk8_key = read_file("rsa_privkey_pk8.der"); |
| 1159 | ASSERT_EQ(633U, pk8_key.size()); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1160 | ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1161 | ImportKey(AuthorizationSetBuilder() |
| 1162 | .RsaSigningKey(256, 3 /* Doesnt' match key */) |
| 1163 | .Digest(KM_DIGEST_NONE) |
| 1164 | .Padding(KM_PAD_NONE), |
Shawn Willden | c24c110 | 2014-12-22 15:30:09 -0700 | [diff] [blame] | 1165 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1166 | } |
| 1167 | |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1168 | TEST_F(ImportKeyTest, EcdsaSuccess) { |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1169 | string pk8_key = read_file("ec_privkey_pk8.der"); |
| 1170 | ASSERT_EQ(138U, pk8_key.size()); |
| 1171 | |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1172 | ASSERT_EQ(KM_ERROR_OK, |
| 1173 | ImportKey(AuthorizationSetBuilder().EcdsaSigningKey(256).Digest(KM_DIGEST_NONE), |
| 1174 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1175 | |
| 1176 | // Check values derived from the key. |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 1177 | EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_EC)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1178 | EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 256)); |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1179 | |
| 1180 | // And values provided by GoogleKeymaster |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1181 | EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED)); |
| 1182 | EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME)); |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1183 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1184 | string message(1024 / 8, 'a'); |
| 1185 | string signature; |
| 1186 | SignMessage(message, &signature); |
| 1187 | VerifyMessage(message, signature); |
Shawn Willden | 81effc6 | 2014-08-27 10:08:46 -0600 | [diff] [blame] | 1188 | } |
| 1189 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1190 | TEST_F(ImportKeyTest, EcdsaSizeSpecified) { |
| 1191 | string pk8_key = read_file("ec_privkey_pk8.der"); |
| 1192 | ASSERT_EQ(138U, pk8_key.size()); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1193 | |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1194 | ASSERT_EQ(KM_ERROR_OK, |
| 1195 | ImportKey(AuthorizationSetBuilder().EcdsaSigningKey(256).Digest(KM_DIGEST_NONE), |
| 1196 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1197 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1198 | // Check values derived from the key. |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame] | 1199 | EXPECT_TRUE(contains(sw_enforced(), TAG_ALGORITHM, KM_ALGORITHM_EC)); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1200 | EXPECT_TRUE(contains(sw_enforced(), TAG_KEY_SIZE, 256)); |
| 1201 | |
| 1202 | // And values provided by GoogleKeymaster |
| 1203 | EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED)); |
| 1204 | EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME)); |
| 1205 | |
| 1206 | string message(1024 / 8, 'a'); |
| 1207 | string signature; |
| 1208 | SignMessage(message, &signature); |
| 1209 | VerifyMessage(message, signature); |
| 1210 | } |
| 1211 | |
| 1212 | TEST_F(ImportKeyTest, EcdsaSizeMismatch) { |
| 1213 | string pk8_key = read_file("ec_privkey_pk8.der"); |
| 1214 | ASSERT_EQ(138U, pk8_key.size()); |
Shawn Willden | 5b53c99 | 2015-02-02 08:05:25 -0700 | [diff] [blame] | 1215 | ASSERT_EQ(KM_ERROR_IMPORT_PARAMETER_MISMATCH, |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1216 | ImportKey(AuthorizationSetBuilder() |
| 1217 | .EcdsaSigningKey(224 /* Doesn't match key */) |
| 1218 | .Digest(KM_DIGEST_NONE), |
| 1219 | KM_KEY_FORMAT_PKCS8, pk8_key)); |
Shawn Willden | 6bbe678 | 2014-09-18 11:26:15 -0600 | [diff] [blame] | 1220 | } |
| 1221 | |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 1222 | TEST_F(ImportKeyTest, AesKeySuccess) { |
| 1223 | char key_data[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1224 | string key(key_data, sizeof(key_data)); |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1225 | ASSERT_EQ(KM_ERROR_OK, |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1226 | ImportKey(AuthorizationSetBuilder().AesEncryptionKey(128).EcbMode().Authorization( |
| 1227 | TAG_PADDING, KM_PAD_PKCS7), |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1228 | KM_KEY_FORMAT_RAW, key)); |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 1229 | |
| 1230 | EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED)); |
| 1231 | EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME)); |
| 1232 | |
| 1233 | string message = "Hello World!"; |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1234 | string ciphertext = EncryptMessage(message); |
| 1235 | string plaintext = DecryptMessage(ciphertext); |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 1236 | EXPECT_EQ(message, plaintext); |
| 1237 | } |
| 1238 | |
| 1239 | TEST_F(ImportKeyTest, HmacSha256KeySuccess) { |
| 1240 | char key_data[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1241 | string key(key_data, sizeof(key_data)); |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1242 | ASSERT_EQ(KM_ERROR_OK, ImportKey(AuthorizationSetBuilder() |
| 1243 | .HmacKey(sizeof(key_data) * 8) |
| 1244 | .Digest(KM_DIGEST_SHA_2_256) |
| 1245 | .Authorization(TAG_MAC_LENGTH, 32), |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1246 | KM_KEY_FORMAT_RAW, key)); |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 1247 | |
| 1248 | EXPECT_TRUE(contains(sw_enforced(), TAG_ORIGIN, KM_ORIGIN_IMPORTED)); |
| 1249 | EXPECT_TRUE(contains(sw_enforced(), KM_TAG_CREATION_DATETIME)); |
| 1250 | |
| 1251 | string message = "Hello World!"; |
| 1252 | string signature; |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1253 | MacMessage(message, &signature, 32); |
Shawn Willden | 3b702e2 | 2015-02-05 10:26:47 -0700 | [diff] [blame] | 1254 | VerifyMessage(message, signature); |
| 1255 | } |
| 1256 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1257 | typedef KeymasterTest EncryptionOperationsTest; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1258 | TEST_F(EncryptionOperationsTest, RsaOaepSuccess) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1259 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1260 | KM_PAD_RSA_OAEP))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1261 | |
| 1262 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1263 | string ciphertext1 = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1264 | EXPECT_EQ(512U / 8, ciphertext1.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1265 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1266 | string ciphertext2 = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1267 | EXPECT_EQ(512U / 8, ciphertext2.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1268 | |
| 1269 | // OAEP randomizes padding so every result should be different. |
| 1270 | EXPECT_NE(ciphertext1, ciphertext2); |
| 1271 | } |
| 1272 | |
| 1273 | TEST_F(EncryptionOperationsTest, RsaOaepRoundTrip) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1274 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1275 | KM_PAD_RSA_OAEP))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1276 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1277 | string ciphertext = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1278 | EXPECT_EQ(512U / 8, ciphertext.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1279 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1280 | string plaintext = DecryptMessage(ciphertext); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1281 | EXPECT_EQ(message, plaintext); |
| 1282 | } |
| 1283 | |
| 1284 | TEST_F(EncryptionOperationsTest, RsaOaepTooLarge) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1285 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1286 | KM_PAD_RSA_OAEP))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1287 | string message = "12345678901234567890123"; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1288 | string result; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 1289 | size_t input_consumed; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1290 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1291 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1292 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
Shawn Willden | d6cd7e3 | 2014-12-17 08:01:26 -0700 | [diff] [blame] | 1293 | EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&result)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1294 | EXPECT_EQ(0U, result.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | TEST_F(EncryptionOperationsTest, RsaOaepCorruptedDecrypt) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1298 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1299 | KM_PAD_RSA_OAEP))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1300 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1301 | string ciphertext = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1302 | EXPECT_EQ(512U / 8, ciphertext.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1303 | |
| 1304 | // Corrupt the ciphertext |
| 1305 | ciphertext[512 / 8 / 2]++; |
| 1306 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1307 | string result; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 1308 | size_t input_consumed; |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1309 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1310 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed)); |
Shawn Willden | d6cd7e3 | 2014-12-17 08:01:26 -0700 | [diff] [blame] | 1311 | EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&result)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1312 | EXPECT_EQ(0U, result.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | TEST_F(EncryptionOperationsTest, RsaPkcs1Success) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1316 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1317 | KM_PAD_RSA_PKCS1_1_5_ENCRYPT))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1318 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1319 | string ciphertext1 = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1320 | EXPECT_EQ(512U / 8, ciphertext1.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1321 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1322 | string ciphertext2 = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1323 | EXPECT_EQ(512U / 8, ciphertext2.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1324 | |
| 1325 | // PKCS1 v1.5 randomizes padding so every result should be different. |
| 1326 | EXPECT_NE(ciphertext1, ciphertext2); |
| 1327 | } |
| 1328 | |
| 1329 | TEST_F(EncryptionOperationsTest, RsaPkcs1RoundTrip) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1330 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1331 | KM_PAD_RSA_PKCS1_1_5_ENCRYPT))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1332 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1333 | string ciphertext = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1334 | EXPECT_EQ(512U / 8, ciphertext.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1335 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1336 | string plaintext = DecryptMessage(ciphertext); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1337 | EXPECT_EQ(message, plaintext); |
| 1338 | } |
| 1339 | |
| 1340 | TEST_F(EncryptionOperationsTest, RsaPkcs1TooLarge) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1341 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1342 | KM_PAD_RSA_PKCS1_1_5_ENCRYPT))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1343 | string message = "12345678901234567890123456789012345678901234567890123"; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1344 | string result; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 1345 | size_t input_consumed; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1346 | |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1347 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1348 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &result, &input_consumed)); |
Shawn Willden | d6cd7e3 | 2014-12-17 08:01:26 -0700 | [diff] [blame] | 1349 | EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&result)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1350 | EXPECT_EQ(0U, result.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | TEST_F(EncryptionOperationsTest, RsaPkcs1CorruptedDecrypt) { |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1354 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder().RsaEncryptionKey(512, 3).Padding( |
| 1355 | KM_PAD_RSA_PKCS1_1_5_ENCRYPT))); |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1356 | string message = "Hello World!"; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame] | 1357 | string ciphertext = EncryptMessage(string(message)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1358 | EXPECT_EQ(512U / 8, ciphertext.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1359 | |
| 1360 | // Corrupt the ciphertext |
| 1361 | ciphertext[512 / 8 / 2]++; |
| 1362 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1363 | string result; |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 1364 | size_t input_consumed; |
Shawn Willden | b15d77e | 2014-12-17 16:44:29 -0700 | [diff] [blame] | 1365 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1366 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &result, &input_consumed)); |
Shawn Willden | d6cd7e3 | 2014-12-17 08:01:26 -0700 | [diff] [blame] | 1367 | EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(&result)); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1368 | EXPECT_EQ(0U, result.size()); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Shawn Willden | edb7994 | 2015-05-08 06:46:44 -0600 | [diff] [blame^] | 1371 | TEST_F(EncryptionOperationsTest, RsaEncryptWithSigningKey) { |
| 1372 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1373 | .RsaSigningKey(256, 3) |
| 1374 | .Digest(KM_DIGEST_NONE) |
| 1375 | .Padding(KM_PAD_NONE))); |
| 1376 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1377 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1378 | } |
| 1379 | |
| 1380 | TEST_F(EncryptionOperationsTest, EcdsaEncrypt) { |
| 1381 | ASSERT_EQ(KM_ERROR_OK, |
| 1382 | GenerateKey(AuthorizationSetBuilder().EcdsaSigningKey(224).Digest(KM_DIGEST_NONE))); |
| 1383 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1384 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1385 | } |
| 1386 | |
| 1387 | TEST_F(EncryptionOperationsTest, HmacEncrypt) { |
| 1388 | ASSERT_EQ( |
| 1389 | KM_ERROR_OK, |
| 1390 | GenerateKey( |
| 1391 | AuthorizationSetBuilder().HmacKey(128).Digest(KM_DIGEST_NONE).Padding(KM_PAD_NONE))); |
| 1392 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1393 | ASSERT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1394 | } |
| 1395 | |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1396 | TEST_F(EncryptionOperationsTest, AesEcbRoundTripSuccess) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1397 | ASSERT_EQ(KM_ERROR_OK, |
| 1398 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1399 | TAG_BLOCK_MODE, KM_MODE_ECB))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1400 | // Two-block message. |
| 1401 | string message = "12345678901234567890123456789012"; |
| 1402 | string ciphertext1 = EncryptMessage(message); |
| 1403 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 1404 | |
| 1405 | string ciphertext2 = EncryptMessage(string(message)); |
| 1406 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 1407 | |
| 1408 | // ECB is deterministic. |
| 1409 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 1410 | |
| 1411 | string plaintext = DecryptMessage(ciphertext1); |
| 1412 | EXPECT_EQ(message, plaintext); |
| 1413 | } |
| 1414 | |
| 1415 | TEST_F(EncryptionOperationsTest, AesEcbNoPaddingWrongInputSize) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1416 | ASSERT_EQ(KM_ERROR_OK, |
| 1417 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1418 | TAG_BLOCK_MODE, KM_MODE_ECB))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1419 | // Message is slightly shorter than two blocks. |
| 1420 | string message = "1234567890123456789012345678901"; |
| 1421 | |
| 1422 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1423 | string ciphertext; |
| 1424 | size_t input_consumed; |
| 1425 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(message, &ciphertext, &input_consumed)); |
| 1426 | EXPECT_EQ(message.size(), input_consumed); |
| 1427 | EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(&ciphertext)); |
| 1428 | } |
| 1429 | |
| 1430 | TEST_F(EncryptionOperationsTest, AesEcbPkcs7Padding) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1431 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1432 | .AesEncryptionKey(128) |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1433 | .Authorization(TAG_BLOCK_MODE, KM_MODE_ECB) |
| 1434 | .Authorization(TAG_PADDING, KM_PAD_PKCS7))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1435 | |
| 1436 | // Try various message lengths; all should work. |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1437 | for (size_t i = 0; i < 32; ++i) { |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1438 | string message(i, 'a'); |
| 1439 | string ciphertext = EncryptMessage(message); |
| 1440 | EXPECT_EQ(i + 16 - (i % 16), ciphertext.size()); |
| 1441 | string plaintext = DecryptMessage(ciphertext); |
| 1442 | EXPECT_EQ(message, plaintext); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | TEST_F(EncryptionOperationsTest, AesEcbPkcs7PaddingCorrupted) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1447 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1448 | .AesEncryptionKey(128) |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1449 | .Authorization(TAG_BLOCK_MODE, KM_MODE_ECB) |
| 1450 | .Authorization(TAG_PADDING, KM_PAD_PKCS7))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1451 | |
| 1452 | string message = "a"; |
| 1453 | string ciphertext = EncryptMessage(message); |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1454 | EXPECT_EQ(16U, ciphertext.size()); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1455 | EXPECT_NE(ciphertext, message); |
| 1456 | ++ciphertext[ciphertext.size() / 2]; |
| 1457 | |
| 1458 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT)); |
| 1459 | string plaintext; |
| 1460 | size_t input_consumed; |
| 1461 | EXPECT_EQ(KM_ERROR_OK, UpdateOperation(ciphertext, &plaintext, &input_consumed)); |
| 1462 | EXPECT_EQ(ciphertext.size(), input_consumed); |
| 1463 | EXPECT_EQ(KM_ERROR_INVALID_ARGUMENT, FinishOperation(&plaintext)); |
| 1464 | } |
| 1465 | |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1466 | TEST_F(EncryptionOperationsTest, AesCtrRoundTripSuccess) { |
| 1467 | ASSERT_EQ(KM_ERROR_OK, |
| 1468 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1469 | TAG_BLOCK_MODE, KM_MODE_CTR))); |
| 1470 | string message = "123"; |
| 1471 | string iv1; |
| 1472 | string ciphertext1 = EncryptMessage(message, &iv1); |
| 1473 | EXPECT_EQ(message.size(), ciphertext1.size()); |
Shawn Willden | 72a5fdd | 2015-03-17 20:04:33 -0600 | [diff] [blame] | 1474 | EXPECT_EQ(16U, iv1.size()); |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1475 | |
| 1476 | string iv2; |
| 1477 | string ciphertext2 = EncryptMessage(message, &iv2); |
| 1478 | EXPECT_EQ(message.size(), ciphertext2.size()); |
Shawn Willden | 72a5fdd | 2015-03-17 20:04:33 -0600 | [diff] [blame] | 1479 | EXPECT_EQ(16U, iv2.size()); |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1480 | |
| 1481 | // IVs should be random, so ciphertexts should differ. |
| 1482 | EXPECT_NE(iv1, iv2); |
| 1483 | EXPECT_NE(ciphertext1, ciphertext2); |
| 1484 | |
| 1485 | string plaintext = DecryptMessage(ciphertext1, iv1); |
| 1486 | EXPECT_EQ(message, plaintext); |
| 1487 | } |
| 1488 | |
| 1489 | TEST_F(EncryptionOperationsTest, AesCtrIncremental) { |
| 1490 | ASSERT_EQ(KM_ERROR_OK, |
| 1491 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1492 | TAG_BLOCK_MODE, KM_MODE_CTR))); |
| 1493 | |
| 1494 | int increment = 15; |
| 1495 | string message(239, 'a'); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1496 | AuthorizationSet input_params(client_params()); |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1497 | AuthorizationSet output_params; |
| 1498 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT, input_params, &output_params)); |
| 1499 | |
| 1500 | string ciphertext; |
| 1501 | size_t input_consumed; |
| 1502 | for (size_t i = 0; i < message.size(); i += increment) |
| 1503 | EXPECT_EQ(KM_ERROR_OK, |
| 1504 | UpdateOperation(message.substr(i, increment), &ciphertext, &input_consumed)); |
| 1505 | EXPECT_EQ(KM_ERROR_OK, FinishOperation(&ciphertext)); |
| 1506 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 1507 | |
| 1508 | // Move TAG_NONCE into input_params |
| 1509 | input_params.Reinitialize(output_params); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1510 | input_params.push_back(client_params()); |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1511 | output_params.Clear(); |
| 1512 | |
| 1513 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT, input_params, &output_params)); |
| 1514 | string plaintext; |
| 1515 | for (size_t i = 0; i < ciphertext.size(); i += increment) |
| 1516 | EXPECT_EQ(KM_ERROR_OK, |
| 1517 | UpdateOperation(ciphertext.substr(i, increment), &plaintext, &input_consumed)); |
| 1518 | EXPECT_EQ(KM_ERROR_OK, FinishOperation(&plaintext)); |
| 1519 | EXPECT_EQ(ciphertext.size(), plaintext.size()); |
| 1520 | EXPECT_EQ(message, plaintext); |
| 1521 | } |
| 1522 | |
| 1523 | struct AesCtrSp80038aTestVector { |
| 1524 | const char* key; |
| 1525 | const char* nonce; |
| 1526 | const char* plaintext; |
| 1527 | const char* ciphertext; |
| 1528 | }; |
| 1529 | |
| 1530 | // These test vectors are taken from |
| 1531 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section F.5. |
| 1532 | static const AesCtrSp80038aTestVector kAesCtrSp80038aTestVectors[] = { |
| 1533 | // AES-128 |
| 1534 | { |
| 1535 | "2b7e151628aed2a6abf7158809cf4f3c", "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 1536 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 1537 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 1538 | "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff" |
| 1539 | "5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee", |
| 1540 | }, |
| 1541 | // AES-192 |
| 1542 | { |
| 1543 | "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 1544 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 1545 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 1546 | "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94" |
| 1547 | "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050", |
| 1548 | }, |
| 1549 | // AES-256 |
| 1550 | { |
| 1551 | "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", |
| 1552 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 1553 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 1554 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 1555 | "601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5" |
| 1556 | "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6", |
| 1557 | }, |
| 1558 | }; |
| 1559 | |
| 1560 | TEST_F(EncryptionOperationsTest, AesCtrSp80038aTestVector) { |
| 1561 | for (size_t i = 0; i < 3; i++) { |
| 1562 | const AesCtrSp80038aTestVector& test(kAesCtrSp80038aTestVectors[i]); |
| 1563 | const string key = hex2str(test.key); |
| 1564 | const string nonce = hex2str(test.nonce); |
| 1565 | const string plaintext = hex2str(test.plaintext); |
| 1566 | const string ciphertext = hex2str(test.ciphertext); |
| 1567 | CheckAesCtrTestVector(key, nonce, plaintext, ciphertext); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | TEST_F(EncryptionOperationsTest, AesCtrInvalidPaddingMode) { |
| 1572 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1573 | .AesEncryptionKey(128) |
| 1574 | .Authorization(TAG_BLOCK_MODE, KM_MODE_CTR) |
| 1575 | .Authorization(TAG_PADDING, KM_PAD_PKCS7))); |
| 1576 | |
| 1577 | EXPECT_EQ(KM_ERROR_UNSUPPORTED_PADDING_MODE, BeginOperation(KM_PURPOSE_ENCRYPT)); |
| 1578 | } |
| 1579 | |
| 1580 | TEST_F(EncryptionOperationsTest, AesCtrInvalidCallerNonce) { |
| 1581 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1582 | .AesEncryptionKey(128) |
| 1583 | .Authorization(TAG_BLOCK_MODE, KM_MODE_CTR) |
| 1584 | .Authorization(TAG_CALLER_NONCE))); |
| 1585 | |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1586 | AuthorizationSet input_params(client_params()); |
Thai Duong | 20d725d | 2015-03-24 17:49:58 -0700 | [diff] [blame] | 1587 | input_params.push_back(TAG_NONCE, "123", 3); |
| 1588 | EXPECT_EQ(KM_ERROR_INVALID_NONCE, BeginOperation(KM_PURPOSE_ENCRYPT, input_params)); |
| 1589 | } |
| 1590 | |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1591 | TEST_F(EncryptionOperationsTest, AesCbcRoundTripSuccess) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1592 | ASSERT_EQ(KM_ERROR_OK, |
| 1593 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1594 | TAG_BLOCK_MODE, KM_MODE_CBC))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1595 | // Two-block message. |
| 1596 | string message = "12345678901234567890123456789012"; |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1597 | string iv1; |
| 1598 | string ciphertext1 = EncryptMessage(message, &iv1); |
| 1599 | EXPECT_EQ(message.size(), ciphertext1.size()); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1600 | |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1601 | string iv2; |
| 1602 | string ciphertext2 = EncryptMessage(message, &iv2); |
| 1603 | EXPECT_EQ(message.size(), ciphertext2.size()); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1604 | |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1605 | // IVs should be random, so ciphertexts should differ. |
| 1606 | EXPECT_NE(iv1, iv2); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1607 | EXPECT_NE(ciphertext1, ciphertext2); |
| 1608 | |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1609 | string plaintext = DecryptMessage(ciphertext1, iv1); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1610 | EXPECT_EQ(message, plaintext); |
| 1611 | } |
| 1612 | |
Shawn Willden | 969aa38 | 2015-04-15 17:05:53 -0700 | [diff] [blame] | 1613 | TEST_F(EncryptionOperationsTest, AesCallerNonce) { |
| 1614 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
| 1615 | .AesEncryptionKey(128) |
| 1616 | .Authorization(TAG_BLOCK_MODE, KM_MODE_CBC) |
| 1617 | .Authorization(TAG_CALLER_NONCE))); |
| 1618 | string message = "12345678901234567890123456789012"; |
| 1619 | string iv1; |
| 1620 | // Don't specify nonce, should get a random one. |
| 1621 | string ciphertext1 = EncryptMessage(message, &iv1); |
| 1622 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 1623 | EXPECT_EQ(16U, iv1.size()); |
| 1624 | |
| 1625 | string plaintext = DecryptMessage(ciphertext1, iv1); |
| 1626 | EXPECT_EQ(message, plaintext); |
| 1627 | |
| 1628 | // Now specify a nonce, should also work. |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1629 | AuthorizationSet input_params(client_params()); |
Shawn Willden | 969aa38 | 2015-04-15 17:05:53 -0700 | [diff] [blame] | 1630 | AuthorizationSet update_params; |
| 1631 | AuthorizationSet output_params; |
| 1632 | input_params.push_back(TAG_NONCE, "abcdefghijklmnop", 16); |
| 1633 | string ciphertext2 = |
| 1634 | ProcessMessage(KM_PURPOSE_ENCRYPT, message, input_params, update_params, &output_params); |
| 1635 | |
| 1636 | // Decrypt with correct nonce. |
| 1637 | plaintext = ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext2, input_params, update_params, |
| 1638 | &output_params); |
| 1639 | EXPECT_EQ(message, plaintext); |
| 1640 | |
| 1641 | // Now try with wrong nonce. |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1642 | input_params.Reinitialize(client_params()); |
Shawn Willden | 969aa38 | 2015-04-15 17:05:53 -0700 | [diff] [blame] | 1643 | input_params.push_back(TAG_NONCE, "aaaaaaaaaaaaaaaa", 16); |
| 1644 | plaintext = ProcessMessage(KM_PURPOSE_DECRYPT, ciphertext2, input_params, update_params, |
| 1645 | &output_params); |
| 1646 | EXPECT_NE(message, plaintext); |
| 1647 | } |
| 1648 | |
Shawn Willden | 6770635 | 2015-04-28 00:43:19 -0600 | [diff] [blame] | 1649 | TEST_F(EncryptionOperationsTest, AesCallerNonceProhibited) { |
| 1650 | ASSERT_EQ(KM_ERROR_OK, |
| 1651 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1652 | TAG_BLOCK_MODE, KM_MODE_CBC))); |
| 1653 | |
| 1654 | string message = "12345678901234567890123456789012"; |
| 1655 | string iv1; |
| 1656 | // Don't specify nonce, should get a random one. |
| 1657 | string ciphertext1 = EncryptMessage(message, &iv1); |
| 1658 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 1659 | EXPECT_EQ(16U, iv1.size()); |
| 1660 | |
| 1661 | string plaintext = DecryptMessage(ciphertext1, iv1); |
| 1662 | EXPECT_EQ(message, plaintext); |
| 1663 | |
| 1664 | // Now specify a nonce, should fail. |
| 1665 | AuthorizationSet input_params(client_params()); |
| 1666 | AuthorizationSet update_params; |
| 1667 | AuthorizationSet output_params; |
| 1668 | input_params.push_back(TAG_NONCE, "abcdefghijklmnop", 16); |
| 1669 | |
| 1670 | EXPECT_EQ(KM_ERROR_CALLER_NONCE_PROHIBITED, |
| 1671 | BeginOperation(KM_PURPOSE_ENCRYPT, input_params, &output_params)); |
| 1672 | } |
| 1673 | |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1674 | TEST_F(EncryptionOperationsTest, AesCbcIncrementalNoPadding) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1675 | ASSERT_EQ(KM_ERROR_OK, |
| 1676 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).Authorization( |
| 1677 | TAG_BLOCK_MODE, KM_MODE_CBC))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1678 | |
| 1679 | int increment = 15; |
| 1680 | string message(240, 'a'); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1681 | AuthorizationSet input_params(client_params()); |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1682 | AuthorizationSet output_params; |
| 1683 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_ENCRYPT, input_params, &output_params)); |
| 1684 | |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1685 | string ciphertext; |
| 1686 | size_t input_consumed; |
| 1687 | for (size_t i = 0; i < message.size(); i += increment) |
| 1688 | EXPECT_EQ(KM_ERROR_OK, |
| 1689 | UpdateOperation(message.substr(i, increment), &ciphertext, &input_consumed)); |
| 1690 | EXPECT_EQ(KM_ERROR_OK, FinishOperation(&ciphertext)); |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1691 | EXPECT_EQ(message.size(), ciphertext.size()); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1692 | |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1693 | // Move TAG_NONCE into input_params |
| 1694 | input_params.Reinitialize(output_params); |
Shawn Willden | 09f2527 | 2015-04-15 13:49:49 -0600 | [diff] [blame] | 1695 | input_params.push_back(client_params()); |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1696 | output_params.Clear(); |
| 1697 | |
| 1698 | EXPECT_EQ(KM_ERROR_OK, BeginOperation(KM_PURPOSE_DECRYPT, input_params, &output_params)); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1699 | string plaintext; |
| 1700 | for (size_t i = 0; i < ciphertext.size(); i += increment) |
| 1701 | EXPECT_EQ(KM_ERROR_OK, |
| 1702 | UpdateOperation(ciphertext.substr(i, increment), &plaintext, &input_consumed)); |
| 1703 | EXPECT_EQ(KM_ERROR_OK, FinishOperation(&plaintext)); |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1704 | EXPECT_EQ(ciphertext.size(), plaintext.size()); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1705 | EXPECT_EQ(message, plaintext); |
| 1706 | } |
| 1707 | |
| 1708 | TEST_F(EncryptionOperationsTest, AesCbcPkcs7Padding) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1709 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1710 | .AesEncryptionKey(128) |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1711 | .Authorization(TAG_BLOCK_MODE, KM_MODE_CBC) |
| 1712 | .Authorization(TAG_PADDING, KM_PAD_PKCS7))); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1713 | |
| 1714 | // Try various message lengths; all should work. |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1715 | for (size_t i = 0; i < 32; ++i) { |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1716 | string message(i, 'a'); |
Shawn Willden | 7a62f5e | 2015-03-10 12:59:20 -0600 | [diff] [blame] | 1717 | string iv; |
| 1718 | string ciphertext = EncryptMessage(message, &iv); |
| 1719 | EXPECT_EQ(i + 16 - (i % 16), ciphertext.size()); |
| 1720 | string plaintext = DecryptMessage(ciphertext, iv); |
Shawn Willden | f0f68b9 | 2014-12-30 16:03:28 -0700 | [diff] [blame] | 1721 | EXPECT_EQ(message, plaintext); |
| 1722 | } |
| 1723 | } |
| 1724 | |
Shawn Willden | cd69582 | 2015-01-26 14:06:32 -0700 | [diff] [blame] | 1725 | typedef KeymasterTest AddEntropyTest; |
| 1726 | TEST_F(AddEntropyTest, AddEntropy) { |
| 1727 | // There's no obvious way to test that entropy is actually added, but we can test that the API |
| 1728 | // doesn't blow up or return an error. |
| 1729 | EXPECT_EQ(KM_ERROR_OK, |
| 1730 | device()->add_rng_entropy(device(), reinterpret_cast<const uint8_t*>("foo"), 3)); |
| 1731 | } |
| 1732 | |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1733 | typedef KeymasterTest RescopingTest; |
| 1734 | TEST_F(RescopingTest, KeyWithRescopingNotUsable) { |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1735 | ASSERT_EQ(KM_ERROR_OK, |
| 1736 | GenerateKey(AuthorizationSetBuilder().AesEncryptionKey(128).EcbMode().Authorization( |
| 1737 | TAG_RESCOPING_ADD, KM_TAG_PURPOSE))); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1738 | // TODO(swillden): Add a better error code for this. |
Shawn Willden | f01329d | 2015-03-11 21:51:38 -0600 | [diff] [blame] | 1739 | EXPECT_EQ(KM_ERROR_RESCOPABLE_KEY_NOT_USABLE, BeginOperation(KM_PURPOSE_ENCRYPT)); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | TEST_F(RescopingTest, RescopeSymmetric) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1743 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1744 | .AesEncryptionKey(128) |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1745 | .EcbMode() |
| 1746 | .Authorization(TAG_RESCOPING_ADD, KM_TAG_PURPOSE) |
| 1747 | .Authorization(TAG_RESCOPING_DEL, KM_TAG_PURPOSE))); |
| 1748 | EXPECT_FALSE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 1749 | EXPECT_TRUE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1750 | |
| 1751 | keymaster_key_blob_t rescoped_blob; |
| 1752 | keymaster_key_characteristics_t* rescoped_characteristics; |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1753 | AuthorizationSet new_params = |
| 1754 | AuthorizationSetBuilder().AesKey(128).Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN).build(); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1755 | |
| 1756 | ASSERT_EQ(KM_ERROR_OK, Rescope(new_params, &rescoped_blob, &rescoped_characteristics)); |
| 1757 | ASSERT_TRUE(rescoped_characteristics != NULL); |
| 1758 | |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1759 | EXPECT_EQ(0U, rescoped_characteristics->hw_enforced.length); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1760 | AuthorizationSet auths(rescoped_characteristics->sw_enforced); |
| 1761 | keymaster_free_characteristics(rescoped_characteristics); |
| 1762 | free(rescoped_characteristics); |
| 1763 | free(const_cast<uint8_t*>(rescoped_blob.key_material)); |
| 1764 | |
| 1765 | EXPECT_TRUE(contains(auths, TAG_ALGORITHM, KM_ALGORITHM_AES)); |
Shawn Willden | c47c88f | 2015-04-07 17:23:27 -0600 | [diff] [blame] | 1766 | EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 1767 | EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | TEST_F(RescopingTest, RescopeRsa) { |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1771 | ASSERT_EQ(KM_ERROR_OK, GenerateKey(AuthorizationSetBuilder() |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1772 | .RsaEncryptionKey(256, 3) |
| 1773 | .Padding(KM_PAD_RSA_OAEP) |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1774 | .Authorization(TAG_RESCOPING_ADD, KM_TAG_PURPOSE) |
| 1775 | .Authorization(TAG_RESCOPING_DEL, KM_TAG_PURPOSE))); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1776 | EXPECT_TRUE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
| 1777 | EXPECT_TRUE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_DECRYPT)); |
| 1778 | EXPECT_FALSE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 1779 | EXPECT_FALSE(contains(sw_enforced(), TAG_PURPOSE, KM_PURPOSE_VERIFY)); |
| 1780 | |
| 1781 | keymaster_key_blob_t rescoped_blob; |
| 1782 | keymaster_key_characteristics_t* rescoped_characteristics; |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1783 | AuthorizationSet new_params = AuthorizationSetBuilder() |
Shawn Willden | af53399 | 2015-04-15 13:48:28 -0600 | [diff] [blame] | 1784 | .RsaSigningKey(256, 3) |
| 1785 | .Digest(KM_DIGEST_SHA_2_256) |
| 1786 | .Padding(KM_PAD_RSA_PSS) |
Shawn Willden | 2c24200 | 2015-02-27 07:01:02 -0700 | [diff] [blame] | 1787 | .build(); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1788 | |
| 1789 | ASSERT_EQ(KM_ERROR_OK, Rescope(new_params, &rescoped_blob, &rescoped_characteristics)); |
| 1790 | ASSERT_TRUE(rescoped_characteristics != NULL); |
| 1791 | |
Shawn Willden | c609659 | 2015-03-17 15:53:14 -0600 | [diff] [blame] | 1792 | EXPECT_EQ(0U, rescoped_characteristics->hw_enforced.length); |
Shawn Willden | 5fad785 | 2015-01-26 16:10:56 -0700 | [diff] [blame] | 1793 | AuthorizationSet auths(rescoped_characteristics->sw_enforced); |
| 1794 | keymaster_free_characteristics(rescoped_characteristics); |
| 1795 | free(rescoped_characteristics); |
| 1796 | free(const_cast<uint8_t*>(rescoped_blob.key_material)); |
| 1797 | |
| 1798 | EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_ENCRYPT)); |
| 1799 | EXPECT_FALSE(contains(auths, TAG_PURPOSE, KM_PURPOSE_DECRYPT)); |
| 1800 | EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_SIGN)); |
| 1801 | EXPECT_TRUE(contains(auths, TAG_PURPOSE, KM_PURPOSE_VERIFY)); |
| 1802 | } |
| 1803 | |
| 1804 | // TODO(swillden): When adding rescoping enforcement, include tests that verify that tags |
| 1805 | // corresponding to intrinsic attributes of keys, like RSA public exponent, or symmetric key size, |
| 1806 | // may not be changed. |
| 1807 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 1808 | } // namespace test |
| 1809 | } // namespace keymaster |