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