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