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