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