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