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