Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 17 | #define LOG_TAG "keymint_1_test" |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include <signal.h> |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 23 | #include <iostream> |
| 24 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 25 | #include <openssl/curve25519.h> |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 26 | #include <openssl/ec.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 27 | #include <openssl/evp.h> |
| 28 | #include <openssl/mem.h> |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 29 | #include <openssl/x509v3.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 30 | |
| 31 | #include <cutils/properties.h> |
| 32 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 33 | #include <android/binder_manager.h> |
| 34 | |
| 35 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 36 | #include <aidl/android/hardware/security/keymint/KeyFormat.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 37 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 38 | #include <keymint_support/key_param_output.h> |
| 39 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 40 | |
| 41 | #include "KeyMintAidlTestBase.h" |
| 42 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 43 | using aidl::android::hardware::security::keymint::AuthorizationSet; |
| 44 | using aidl::android::hardware::security::keymint::KeyCharacteristics; |
| 45 | using aidl::android::hardware::security::keymint::KeyFormat; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 46 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 47 | namespace std { |
| 48 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 49 | using namespace aidl::android::hardware::security::keymint; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 50 | |
| 51 | template <> |
| 52 | struct std::equal_to<KeyCharacteristics> { |
| 53 | bool operator()(const KeyCharacteristics& a, const KeyCharacteristics& b) const { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 54 | if (a.securityLevel != b.securityLevel) return false; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 55 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 56 | // this isn't very efficient. Oh, well. |
| 57 | AuthorizationSet a_auths(a.authorizations); |
| 58 | AuthorizationSet b_auths(b.authorizations); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 59 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 60 | a_auths.Sort(); |
| 61 | b_auths.Sort(); |
| 62 | |
| 63 | return a_auths == b_auths; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
| 67 | } // namespace std |
| 68 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 69 | namespace aidl::android::hardware::security::keymint::test { |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 70 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 71 | namespace { |
| 72 | |
David Drysdale | feab5d9 | 2022-01-06 15:46:23 +0000 | [diff] [blame] | 73 | // Maximum supported Ed25519 message size. |
| 74 | const size_t MAX_ED25519_MSG_SIZE = 16 * 1024; |
| 75 | |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 76 | // Whether to check that BOOT_PATCHLEVEL is populated. |
| 77 | bool check_boot_pl = true; |
| 78 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 79 | // The maximum number of times we'll attempt to verify that corruption |
David Drysdale | 4c1f6ac | 2021-11-25 16:08:29 +0000 | [diff] [blame] | 80 | // of an encrypted blob results in an error. Retries are necessary as there |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 81 | // is a small (roughly 1/256) chance that corrupting ciphertext still results |
| 82 | // in valid PKCS7 padding. |
| 83 | constexpr size_t kMaxPaddingCorruptionRetries = 8; |
| 84 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 85 | template <TagType tag_type, Tag tag, typename ValueT> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 86 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag> ttag, |
| 87 | ValueT expected_value) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 88 | auto it = std::find_if(set.begin(), set.end(), [&](const KeyParameter& param) { |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 89 | if (auto p = authorizationValue(ttag, param)) { |
| 90 | return *p == expected_value; |
| 91 | } |
| 92 | return false; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 93 | }); |
| 94 | return (it != set.end()); |
| 95 | } |
| 96 | |
| 97 | template <TagType tag_type, Tag tag> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 98 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag>) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 99 | auto it = std::find_if(set.begin(), set.end(), |
| 100 | [&](const KeyParameter& param) { return param.tag == tag; }); |
| 101 | return (it != set.end()); |
| 102 | } |
| 103 | |
| 104 | constexpr char hex_value[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 105 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 106 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 107 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9' |
| 108 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F' |
| 109 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 110 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f' |
| 111 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 112 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 113 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 114 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 115 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 116 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 117 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 118 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 119 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 120 | |
| 121 | string hex2str(string a) { |
| 122 | string b; |
| 123 | size_t num = a.size() / 2; |
| 124 | b.resize(num); |
| 125 | for (size_t i = 0; i < num; i++) { |
| 126 | b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]); |
| 127 | } |
| 128 | return b; |
| 129 | } |
| 130 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 131 | string rsa_key = hex2str( |
| 132 | // RFC 5208 s5 |
| 133 | "30820275" // SEQUENCE length 0x275 (PrivateKeyInfo) { |
| 134 | "020100" // INTEGER length 1 value 0x00 (version) |
| 135 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 136 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 137 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 138 | "0500" // NULL (parameters) |
| 139 | // } end SEQUENCE (AlgorithmIdentifier) |
| 140 | "0482025f" // OCTET STRING length 0x25f (privateKey) holding... |
| 141 | // RFC 8017 A.1.2 |
| 142 | "3082025b" // SEQUENCE length 0x25b (RSAPrivateKey) { |
| 143 | "020100" // INTEGER length 1 value 0x00 (version) |
| 144 | "028181" // INTEGER length 0x81 value (modulus) ... |
| 145 | "00c6095409047d8634812d5a218176e4" |
| 146 | "5c41d60a75b13901f234226cffe77652" |
| 147 | "1c5a77b9e389417b71c0b6a44d13afe4" |
| 148 | "e4a2805d46c9da2935adb1ff0c1f24ea" |
| 149 | "06e62b20d776430a4d435157233c6f91" |
| 150 | "6783c30e310fcbd89b85c2d567711697" |
| 151 | "85ac12bca244abda72bfb19fc44d27c8" |
| 152 | "1e1d92de284f4061edfd99280745ea6d" |
| 153 | "25" |
| 154 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 155 | "028180" // INTEGER length 0x80 (privateExponent) value... |
| 156 | "1be0f04d9cae3718691f035338308e91" |
| 157 | "564b55899ffb5084d2460e6630257e05" |
| 158 | "b3ceab02972dfabcd6ce5f6ee2589eb6" |
| 159 | "7911ed0fac16e43a444b8c861e544a05" |
| 160 | "93365772f8baf6b22fc9e3c5f1024b06" |
| 161 | "3ac080a7b2234cf8aee8f6c47bbf4fd3" |
| 162 | "ace7240290bef16c0b3f7f3cdd64ce3a" |
| 163 | "b5912cf6e32f39ab188358afcccd8081" |
| 164 | "0241" // INTEGER length 0x41 (prime1) |
| 165 | "00e4b49ef50f765d3b24dde01aceaaf1" |
| 166 | "30f2c76670a91a61ae08af497b4a82be" |
| 167 | "6dee8fcdd5e3f7ba1cfb1f0c926b88f8" |
| 168 | "8c92bfab137fba2285227b83c342ff7c" |
| 169 | "55" |
| 170 | "0241" // INTEGER length 0x41 (prime2) |
| 171 | "00ddabb5839c4c7f6bf3d4183231f005" |
| 172 | "b31aa58affdda5c79e4cce217f6bc930" |
| 173 | "dbe563d480706c24e9ebfcab28a6cdef" |
| 174 | "d324b77e1bf7251b709092c24ff501fd" |
| 175 | "91" |
| 176 | "0240" // INTEGER length 0x40 (exponent1) |
| 177 | "23d4340eda3445d8cd26c14411da6fdc" |
| 178 | "a63c1ccd4b80a98ad52b78cc8ad8beb2" |
| 179 | "842c1d280405bc2f6c1bea214a1d742a" |
| 180 | "b996b35b63a82a5e470fa88dbf823cdd" |
| 181 | "0240" // INTEGER length 0x40 (exponent2) |
| 182 | "1b7b57449ad30d1518249a5f56bb9829" |
| 183 | "4d4b6ac12ffc86940497a5a5837a6cf9" |
| 184 | "46262b494526d328c11e1126380fde04" |
| 185 | "c24f916dec250892db09a6d77cdba351" |
| 186 | "0240" // INTEGER length 0x40 (coefficient) |
| 187 | "7762cd8f4d050da56bd591adb515d24d" |
| 188 | "7ccd32cca0d05f866d583514bd7324d5" |
| 189 | "f33645e8ed8b4a1cb3cc4a1d67987399" |
| 190 | "f2a09f5b3fb68c88d5e5d90ac33492d6" |
| 191 | // } end SEQUENCE (PrivateKey) |
| 192 | // } end SEQUENCE (PrivateKeyInfo) |
| 193 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 194 | |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 195 | /* |
| 196 | * DER-encoded PKCS#8 format RSA key. Generated using: |
| 197 | * |
| 198 | * openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 199 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 200 | string rsa_2048_key = hex2str( |
| 201 | // RFC 5208 s5 |
| 202 | "308204BD" // SEQUENCE length 0x4bd (PrivateKeyInfo) { |
| 203 | "020100" // INTEGER length 1 value 0x00 (version) |
| 204 | "300D" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 205 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 206 | "2A864886F70D010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 207 | "0500" // NULL (parameters) |
| 208 | // } end SEQUENCE (AlgorithmIdentifier) |
| 209 | "048204A7" // OCTET STRING length 0x25f (privateKey) holding... |
| 210 | // RFC 8017 A.1.2 |
| 211 | "308204A3" // SEQUENCE length 0x4a3 (RSAPrivateKey) { |
| 212 | "020100" // INTEGER length 1 value 0x00 (version) |
| 213 | "02820101" // INTEGER length 0x101 value (modulus) ... |
| 214 | "00BEBC342B56D443B1299F9A6A7056E8" |
| 215 | "0A897E318476A5A18029E63B2ED739A6" |
| 216 | "1791D339F58DC763D9D14911F2EDEC38" |
| 217 | "3DEE11F6319B44510E7A3ECD9B79B973" |
| 218 | "82E49500ACF8117DC89CAF0E621F7775" |
| 219 | "6554A2FD4664BFE7AB8B59AB48340DBF" |
| 220 | "A27B93B5A81F6ECDEB02D0759307128D" |
| 221 | "F3E3BAD4055C8B840216DFAA5700670E" |
| 222 | "6C5126F0962FCB70FF308F25049164CC" |
| 223 | "F76CC2DA66A7DD9A81A714C2809D6918" |
| 224 | "6133D29D84568E892B6FFBF3199BDB14" |
| 225 | "383EE224407F190358F111A949552ABA" |
| 226 | "6714227D1BD7F6B20DD0CB88F9467B71" |
| 227 | "9339F33BFF35B3870B3F62204E4286B0" |
| 228 | "948EA348B524544B5F9838F29EE643B0" |
| 229 | "79EEF8A713B220D7806924CDF7295070" |
| 230 | "C5" |
| 231 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 232 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 233 | "69F377F35F2F584EF075353CCD1CA997" |
| 234 | "38DB3DBC7C7FF35F9366CE176DFD1B13" |
| 235 | "5AB10030344ABF5FBECF1D4659FDEF1C" |
| 236 | "0FC430834BE1BE3911951377BB3D563A" |
| 237 | "2EA9CA8F4AD9C48A8CE6FD516A735C66" |
| 238 | "2686C7B4B3C09A7B8354133E6F93F790" |
| 239 | "D59EAEB92E84C9A4339302CCE28FDF04" |
| 240 | "CCCAFA7DE3F3A827D4F6F7D38E68B0EC" |
| 241 | "6AB706645BF074A4E4090D06FB163124" |
| 242 | "365FD5EE7A20D350E9958CC30D91326E" |
| 243 | "1B292E9EF5DB408EC42DAF737D201497" |
| 244 | "04D0A678A0FB5B5446863B099228A352" |
| 245 | "D604BA8091A164D01D5AB05397C71EAD" |
| 246 | "20BE2A08FC528FE442817809C787FEE4" |
| 247 | "AB97F97B9130D022153EDC6EB6CBE7B0" |
| 248 | "F8E3473F2E901209B5DB10F93604DB01" |
| 249 | "028181" // INTEGER length 0x81 (prime1) |
| 250 | "00E83C0998214941EA4F9293F1B77E2E" |
| 251 | "99E6CF305FAF358238E126124FEAF2EB" |
| 252 | "9724B2EA7B78E6032343821A80E55D1D" |
| 253 | "88FB12D220C3F41A56142FEC85796D19" |
| 254 | "17F1E8C774F142B67D3D6E7B7E6B4383" |
| 255 | "E94DB5929089DBB346D5BDAB40CC2D96" |
| 256 | "EE0409475E175C63BF78CFD744136740" |
| 257 | "838127EA723FF3FE7FA368C1311B4A4E" |
| 258 | "05" |
| 259 | "028181" // INTEGER length 0x81 (prime2) |
| 260 | "00D240FCC0F5D7715CDE21CB2DC86EA1" |
| 261 | "46132EA3B06F61FF2AF54BF38473F59D" |
| 262 | "ADCCE32B5F4CC32DD0BA6F509347B4B5" |
| 263 | "B1B58C39F95E4798CCBB43E83D0119AC" |
| 264 | "F532F359CA743C85199F0286610E2009" |
| 265 | "97D7312917179AC9B67558773212EC96" |
| 266 | "1E8BCE7A3CC809BC5486A96E4B0E6AF3" |
| 267 | "94D94E066A0900B7B70E82A44FB30053" |
| 268 | "C1" |
| 269 | "028181" // INTEGER length 0x81 (exponent1) |
| 270 | "00AD15DA1CBD6A492B66851BA8C316D3" |
| 271 | "8AB700E2CFDDD926A658003513C54BAA" |
| 272 | "152B30021D667D20078F500F8AD3E7F3" |
| 273 | "945D74A891ED1A28EAD0FEEAEC8C14A8" |
| 274 | "E834CF46A13D1378C99D18940823CFDD" |
| 275 | "27EC5810D59339E0C34198AC638E09C8" |
| 276 | "7CBB1B634A9864AE9F4D5EB2D53514F6" |
| 277 | "7B4CAEC048C8AB849A02E397618F3271" |
| 278 | "35" |
| 279 | "028180" // INTEGER length 0x80 (exponent2) |
| 280 | "1FA2C1A5331880A92D8F3E281C617108" |
| 281 | "BF38244F16E352E69ED417C7153F9EC3" |
| 282 | "18F211839C643DCF8B4DD67CE2AC312E" |
| 283 | "95178D5D952F06B1BF779F4916924B70" |
| 284 | "F582A23F11304E02A5E7565AE22A35E7" |
| 285 | "4FECC8B6FDC93F92A1A37703E4CF0E63" |
| 286 | "783BD02EB716A7ECBBFA606B10B74D01" |
| 287 | "579522E7EF84D91FC522292108D902C1" |
| 288 | "028180" // INTEGER length 0x80 (coefficient) |
| 289 | "796FE3825F9DCC85DF22D58690065D93" |
| 290 | "898ACD65C087BEA8DA3A63BF4549B795" |
| 291 | "E2CD0E3BE08CDEBD9FCF1720D9CDC507" |
| 292 | "0D74F40DED8E1102C52152A31B6165F8" |
| 293 | "3A6722AECFCC35A493D7634664B888A0" |
| 294 | "8D3EB034F12EA28BFEE346E205D33482" |
| 295 | "7F778B16ED40872BD29FCB36536B6E93" |
| 296 | "FFB06778696B4A9D81BB0A9423E63DE5" |
| 297 | // } end SEQUENCE (PrivateKey) |
| 298 | // } end SEQUENCE (PrivateKeyInfo) |
| 299 | ); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 300 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 301 | string ec_256_key = hex2str( |
| 302 | // RFC 5208 s5 |
| 303 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 304 | "020100" // INTEGER length 1 value 0 (version) |
| 305 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 306 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 307 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 308 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 309 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 310 | // } end SEQUENCE (AlgorithmIdentifier) |
| 311 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 312 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 313 | "020101" // INTEGER length 1 value 1 (version) |
| 314 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 315 | "737c2ecd7b8d1940bf2930aa9b4ed3ff" |
| 316 | "941eed09366bc03299986481f3a4d859" |
| 317 | "a144" // TAG [1] len 0x44 (publicKey) { |
| 318 | "03420004bf85d7720d07c25461683bc6" |
| 319 | "48b4778a9a14dd8a024e3bdd8c7ddd9a" |
| 320 | "b2b528bbc7aa1b51f14ebbbb0bd0ce21" |
| 321 | "bcc41c6eb00083cf3376d11fd44949e0" |
| 322 | "b2183bfe" |
| 323 | // } end SEQUENCE (ECPrivateKey) |
| 324 | // } end SEQUENCE (PrivateKeyInfo) |
| 325 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 326 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 327 | string ec_521_key = hex2str( |
| 328 | // RFC 5208 s5 |
| 329 | "3081EE" // SEQUENCE length 0xee (PrivateKeyInfo) { |
| 330 | "020100" // INTEGER length 1 value 0 (version) |
| 331 | "3010" // SEQUENCE length 0x10 (AlgorithmIdentifier) { |
| 332 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 333 | "2A8648CE3D0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 334 | "0605" // OBJECT IDENTIFIER length 5 (param) |
| 335 | "2B81040023" // 1.3.132.0.35 (secp521r1) |
| 336 | // } end SEQUENCE (AlgorithmIdentifier) |
| 337 | "0481D6" // OCTET STRING length 0xd6 (privateKey) holding... |
| 338 | "3081D3" // SEQUENCE length 0xd3 (ECPrivateKey) |
| 339 | "020101" // INTEGER length 1 value 1 (version) |
| 340 | "0442" // OCTET STRING length 0x42 (privateKey) |
| 341 | "0011458C586DB5DAA92AFAB03F4FE46A" |
| 342 | "A9D9C3CE9A9B7A006A8384BEC4C78E8E" |
| 343 | "9D18D7D08B5BCFA0E53C75B064AD51C4" |
| 344 | "49BAE0258D54B94B1E885DED08ED4FB2" |
| 345 | "5CE9" |
| 346 | "A18189" // TAG [1] len 0x89 (publicKey) { |
| 347 | "03818600040149EC11C6DF0FA122C6A9" |
| 348 | "AFD9754A4FA9513A627CA329E349535A" |
| 349 | "5629875A8ADFBE27DCB932C051986377" |
| 350 | "108D054C28C6F39B6F2C9AF81802F9F3" |
| 351 | "26B842FF2E5F3C00AB7635CFB36157FC" |
| 352 | "0882D574A10D839C1A0C049DC5E0D775" |
| 353 | "E2EE50671A208431BB45E78E70BEFE93" |
| 354 | "0DB34818EE4D5C26259F5C6B8E28A652" |
| 355 | "950F9F88D7B4B2C9D9" |
| 356 | // } end SEQUENCE (ECPrivateKey) |
| 357 | // } end SEQUENCE (PrivateKeyInfo) |
| 358 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 359 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 360 | string ec_256_key_rfc5915 = hex2str( |
| 361 | // RFC 5208 s5 |
| 362 | "308193" // SEQUENCE length 0x93 (PrivateKeyInfo) { |
| 363 | "020100" // INTEGER length 1 value 0 (version) |
| 364 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 365 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 366 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 367 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 368 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 369 | // } end SEQUENCE (AlgorithmIdentifier) |
| 370 | "0479" // OCTET STRING length 0x79 (privateKey) holding... |
| 371 | // RFC 5915 s3 |
| 372 | "3077" // SEQUENCE length 0x77 (ECPrivateKey) |
| 373 | "020101" // INTEGER length 1 value 1 (version) |
| 374 | "0420" // OCTET STRING length 0x42 (privateKey) |
| 375 | "782370a8c8ce5537baadd04dcff079c8" |
| 376 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 377 | "a00a" // TAG [0] length 0xa (parameters) |
| 378 | "0608" // OBJECT IDENTIFIER length 8 |
| 379 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 380 | // } end TAG [0] |
| 381 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 382 | "0342" // BIT STRING length 0x42 |
| 383 | "00" // no pad bits |
| 384 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 385 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 386 | "8e68c905464666f98dad4f01573ba810" |
| 387 | "78b3428570a439ba3229fbc026c55068" |
| 388 | "2f" |
| 389 | // } end SEQUENCE (ECPrivateKey) |
| 390 | // } end SEQUENCE (PrivateKeyInfo) |
| 391 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 392 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 393 | string ec_256_key_sec1 = hex2str( |
| 394 | // RFC 5208 s5 |
| 395 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 396 | "020100" // INTEGER length 1 value 0 (version) |
| 397 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 398 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 399 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 400 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 401 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 402 | // } end SEQUENCE (AlgorithmIdentifier) |
| 403 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 404 | // SEC1-v2 C.4 |
| 405 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 406 | "020101" // INTEGER length 1 value 0x01 (version) |
| 407 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 408 | "782370a8c8ce5537baadd04dcff079c8" |
| 409 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 410 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 411 | "0342" // BIT STRING length 0x42 |
| 412 | "00" // no pad bits |
| 413 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 414 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 415 | "8e68c905464666f98dad4f01573ba810" |
| 416 | "78b3428570a439ba3229fbc026c55068" |
| 417 | "2f" |
| 418 | // } end TAG [1] (publicKey) |
| 419 | // } end SEQUENCE (PrivateKeyInfo) |
| 420 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 421 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 422 | /** |
| 423 | * Ed25519 key pair generated as follows: |
| 424 | * ``` |
| 425 | * % openssl req -x509 -newkey ED25519 -days 700 -nodes \ |
| 426 | * -keyout ed25519_priv.key -out ed25519.pem * -subj "/CN=fake.ed25519.com" |
| 427 | * Generating a ED25519 private key writing new private key to |
| 428 | * 'ed25519_priv.key' |
| 429 | * ----- |
| 430 | * % cat ed25519_priv.key |
| 431 | * -----BEGIN PRIVATE KEY----- |
| 432 | * MC4CAQAwBQYDK2VwBCIEIKl3A5quNywcj1P+0XI9SBalFPIvO52NxceMLRH6dVmR |
| 433 | * -----END PRIVATE KEY----- |
| 434 | * % der2ascii -pem -i ed25519_priv.key |
| 435 | * SEQUENCE { |
| 436 | * INTEGER { 0 } |
| 437 | * SEQUENCE { |
| 438 | * # ed25519 |
| 439 | * OBJECT_IDENTIFIER { 1.3.101.112 } |
| 440 | * } |
| 441 | * OCTET_STRING { |
| 442 | * OCTET_STRING { `a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991` } |
| 443 | * } |
| 444 | * } |
| 445 | * % cat ed25519.pem |
| 446 | * -----BEGIN CERTIFICATE----- |
| 447 | * MIIBSjCB/aADAgECAhR0Jron3eKcdgqyecv/eEfGWAzn8DAFBgMrZXAwGzEZMBcG |
| 448 | * A1UEAwwQZmFrZS5lZDI1NTE5LmNvbTAeFw0yMTEwMjAwODI3NDJaFw0yMzA5MjAw |
| 449 | * ODI3NDJaMBsxGTAXBgNVBAMMEGZha2UuZWQyNTUxOS5jb20wKjAFBgMrZXADIQDv |
| 450 | * uwHz+3TaQ69D2digxlz0fFfsZg0rPqgQae3jBPRWkaNTMFEwHQYDVR0OBBYEFN9O |
| 451 | * od30SY4JTs66ZR403UPya+iXMB8GA1UdIwQYMBaAFN9Ood30SY4JTs66ZR403UPy |
| 452 | * a+iXMA8GA1UdEwEB/wQFMAMBAf8wBQYDK2VwA0EAKjVrYQjuE/gEL2j/ABpDbFjV |
| 453 | * Ilg5tJ6MN/P3psAv3Cs7f0X1lFqdlt15nJ/6aj2cmGCwNRXt5wcyYDKNu+v2Dw== |
| 454 | * -----END CERTIFICATE----- |
| 455 | * % openssl x509 -in ed25519.pem -text -noout |
| 456 | * Certificate: |
| 457 | * Data: |
| 458 | * Version: 3 (0x2) |
| 459 | * Serial Number: |
| 460 | * 74:26:ba:27:dd:e2:9c:76:0a:b2:79:cb:ff:78:47:c6:58:0c:e7:f0 |
| 461 | * Signature Algorithm: ED25519 |
| 462 | * Issuer: CN = fake.ed25519.com |
| 463 | * Validity |
| 464 | * Not Before: Oct 20 08:27:42 2021 GMT |
| 465 | * Not After : Sep 20 08:27:42 2023 GMT |
| 466 | * Subject: CN = fake.ed25519.com |
| 467 | * Subject Public Key Info: |
| 468 | * Public Key Algorithm: ED25519 |
| 469 | * ED25519 Public-Key: |
| 470 | * pub: |
| 471 | * ef:bb:01:f3:fb:74:da:43:af:43:d9:d8:a0:c6:5c: |
| 472 | * f4:7c:57:ec:66:0d:2b:3e:a8:10:69:ed:e3:04:f4: |
| 473 | * 56:91 |
| 474 | * X509v3 extensions: |
| 475 | * X509v3 Subject Key Identifier: |
| 476 | * DF:4E:A1:DD:F4:49:8E:09:4E:CE:BA:65:1E:34:DD:43:F2:6B:E8:97 |
| 477 | * X509v3 Authority Key Identifier: |
| 478 | * keyid:DF:4E:A1:DD:F4:49:8E:09:4E:CE:BA:65:1E:34:DD:43:F2:6B:E8:97 |
| 479 | * |
| 480 | * X509v3 Basic Constraints: critical |
| 481 | * CA:TRUE |
| 482 | * Signature Algorithm: ED25519 |
| 483 | * 2a:35:6b:61:08:ee:13:f8:04:2f:68:ff:00:1a:43:6c:58:d5: |
| 484 | * 22:58:39:b4:9e:8c:37:f3:f7:a6:c0:2f:dc:2b:3b:7f:45:f5: |
| 485 | * 94:5a:9d:96:dd:79:9c:9f:fa:6a:3d:9c:98:60:b0:35:15:ed: |
| 486 | * e7:07:32:60:32:8d:bb:eb:f6:0f |
| 487 | * ``` |
| 488 | */ |
| 489 | string ed25519_key = hex2str("a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991"); |
| 490 | string ed25519_pkcs8_key = hex2str( |
| 491 | // RFC 5208 s5 |
| 492 | "302e" // SEQUENCE length 0x2e (PrivateKeyInfo) { |
| 493 | "0201" // INTEGER length 1 (Version) |
| 494 | "00" // version 0 |
| 495 | "3005" // SEQUENCE length 05 (AlgorithmIdentifier) { |
| 496 | "0603" // OBJECT IDENTIFIER length 3 (algorithm) |
| 497 | "2b6570" // 1.3.101.112 (id-Ed125519 RFC 8410 s3) |
| 498 | // } end SEQUENCE (AlgorithmIdentifier) |
| 499 | "0422" // OCTET STRING length 0x22 (PrivateKey) |
| 500 | "0420" // OCTET STRING length 0x20 (RFC 8410 s7) |
| 501 | "a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991" |
| 502 | // } end SEQUENCE (PrivateKeyInfo) |
| 503 | ); |
| 504 | string ed25519_pubkey = hex2str("efbb01f3fb74da43af43d9d8a0c65cf47c57ec660d2b3ea81069ede304f45691"); |
| 505 | |
| 506 | /** |
| 507 | * X25519 key pair generated as follows: |
| 508 | * ``` |
| 509 | * % openssl genpkey -algorithm X25519 > x25519_priv.key |
| 510 | * % cat x25519_priv.key |
| 511 | * -----BEGIN PRIVATE KEY----- |
| 512 | * MC4CAQAwBQYDK2VuBCIEIGgPwF3NLwQx/Sfwr2nfJvXitwlDNh3Skzh+TISN/y1C |
| 513 | * -----END PRIVATE KEY----- |
| 514 | * % der2ascii -pem -i x25519_priv.key |
| 515 | * SEQUENCE { |
| 516 | * INTEGER { 0 } |
| 517 | * SEQUENCE { |
| 518 | * # x25519 |
| 519 | * OBJECT_IDENTIFIER { 1.3.101.110 } |
| 520 | * } |
| 521 | * OCTET_STRING { |
| 522 | * OCTET_STRING { `680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42` } |
| 523 | * } |
| 524 | * } |
| 525 | * ``` |
| 526 | */ |
| 527 | |
| 528 | string x25519_key = hex2str("680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42"); |
| 529 | string x25519_pkcs8_key = hex2str( |
| 530 | // RFC 5208 s5 |
| 531 | "302e" // SEQUENCE length 0x2e (PrivateKeyInfo) { |
| 532 | "0201" // INTEGER length 1 (Version) |
| 533 | "00" // version 0 |
| 534 | "3005" // SEQUENCE length 05 (AlgorithmIdentifier) { |
| 535 | "0603" // OBJECT IDENTIFIER length 3 (algorithm) |
| 536 | "2b656e" // 1.3.101.110 (id-X125519 RFC 8410 s3) |
| 537 | "0422" // OCTET STRING length 0x22 (PrivateKey) |
| 538 | "0420" // OCTET STRING length 0x20 (RFC 8410 s7) |
| 539 | "680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42"); |
| 540 | string x25519_pubkey = hex2str("be46925a857f17831d6d454b9d3d36a4a30166edf80eb82b684661c3e258f768"); |
| 541 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 542 | struct RSA_Delete { |
| 543 | void operator()(RSA* p) { RSA_free(p); } |
| 544 | }; |
| 545 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 546 | std::string make_string(const uint8_t* data, size_t length) { |
| 547 | return std::string(reinterpret_cast<const char*>(data), length); |
| 548 | } |
| 549 | |
| 550 | template <size_t N> |
| 551 | std::string make_string(const uint8_t (&a)[N]) { |
| 552 | return make_string(a, N); |
| 553 | } |
| 554 | |
| 555 | class AidlBuf : public vector<uint8_t> { |
| 556 | typedef vector<uint8_t> super; |
| 557 | |
| 558 | public: |
| 559 | AidlBuf() {} |
| 560 | AidlBuf(const super& other) : super(other) {} |
| 561 | AidlBuf(super&& other) : super(std::move(other)) {} |
| 562 | explicit AidlBuf(const std::string& other) : AidlBuf() { *this = other; } |
| 563 | |
| 564 | AidlBuf& operator=(const super& other) { |
| 565 | super::operator=(other); |
| 566 | return *this; |
| 567 | } |
| 568 | |
| 569 | AidlBuf& operator=(super&& other) { |
| 570 | super::operator=(std::move(other)); |
| 571 | return *this; |
| 572 | } |
| 573 | |
| 574 | AidlBuf& operator=(const string& other) { |
| 575 | resize(other.size()); |
| 576 | for (size_t i = 0; i < other.size(); ++i) { |
| 577 | (*this)[i] = static_cast<uint8_t>(other[i]); |
| 578 | } |
| 579 | return *this; |
| 580 | } |
| 581 | |
| 582 | string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); } |
| 583 | }; |
| 584 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 585 | string device_suffix(const string& name) { |
| 586 | size_t pos = name.find('/'); |
| 587 | if (pos == string::npos) { |
| 588 | return name; |
| 589 | } |
| 590 | return name.substr(pos + 1); |
| 591 | } |
| 592 | |
| 593 | bool matching_rp_instance(const string& km_name, |
| 594 | std::shared_ptr<IRemotelyProvisionedComponent>* rp) { |
| 595 | string km_suffix = device_suffix(km_name); |
| 596 | |
| 597 | vector<string> rp_names = |
| 598 | ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor); |
| 599 | for (const string& rp_name : rp_names) { |
| 600 | // If the suffix of the RemotelyProvisionedComponent instance equals the suffix of the |
| 601 | // KeyMint instance, assume they match. |
| 602 | if (device_suffix(rp_name) == km_suffix && AServiceManager_isDeclared(rp_name.c_str())) { |
| 603 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(rp_name.c_str())); |
| 604 | *rp = IRemotelyProvisionedComponent::fromBinder(binder); |
| 605 | return true; |
| 606 | } |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 611 | } // namespace |
| 612 | |
| 613 | class NewKeyGenerationTest : public KeyMintAidlTestBase { |
| 614 | protected: |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 615 | void CheckBaseParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 616 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 617 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 618 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 619 | // Check that some unexpected tags/values are NOT present. |
| 620 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 621 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void CheckSymmetricParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 625 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
| 626 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 627 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 628 | |
| 629 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | AuthorizationSet CheckCommonParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 633 | // TODO(swillden): Distinguish which params should be in which auth list. |
| 634 | AuthorizationSet auths; |
| 635 | for (auto& entry : keyCharacteristics) { |
| 636 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 637 | } |
| 638 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 639 | |
| 640 | // Verify that App data, ROT and auth timeout are NOT included. |
| 641 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 642 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 643 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 644 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 645 | // None of the tests specify CREATION_DATETIME so check that the KeyMint implementation |
| 646 | // never adds it. |
| 647 | EXPECT_FALSE(auths.Contains(TAG_CREATION_DATETIME)); |
| 648 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 649 | // Check OS details match the original hardware info. |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 650 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 651 | EXPECT_TRUE(os_ver); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 652 | EXPECT_EQ(*os_ver, os_version()); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 653 | auto os_pl = auths.GetTagValue(TAG_OS_PATCHLEVEL); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 654 | EXPECT_TRUE(os_pl); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 655 | EXPECT_EQ(*os_pl, os_patch_level()); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 656 | |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 657 | // Should include vendor patchlevel. |
David Drysdale | f5bfa00 | 2021-09-27 17:30:41 +0100 | [diff] [blame] | 658 | auto vendor_pl = auths.GetTagValue(TAG_VENDOR_PATCHLEVEL); |
| 659 | EXPECT_TRUE(vendor_pl); |
| 660 | EXPECT_EQ(*vendor_pl, vendor_patch_level()); |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 661 | |
| 662 | // Should include boot patchlevel (but there are some test scenarios where this is not |
| 663 | // possible). |
| 664 | if (check_boot_pl) { |
| 665 | auto boot_pl = auths.GetTagValue(TAG_BOOT_PATCHLEVEL); |
| 666 | EXPECT_TRUE(boot_pl); |
| 667 | } |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 668 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 669 | return auths; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 670 | } |
| 671 | }; |
| 672 | |
| 673 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 674 | * NewKeyGenerationTest.Aes |
| 675 | * |
| 676 | * Verifies that keymint can generate all required AES key sizes, and that the resulting keys |
| 677 | * have correct characteristics. |
| 678 | */ |
| 679 | TEST_P(NewKeyGenerationTest, Aes) { |
| 680 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 681 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 682 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 683 | SCOPED_TRACE(testing::Message() |
| 684 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 685 | vector<uint8_t> key_blob; |
| 686 | vector<KeyCharacteristics> key_characteristics; |
| 687 | auto builder = AuthorizationSetBuilder() |
| 688 | .AesEncryptionKey(key_size) |
| 689 | .BlockMode(block_mode) |
| 690 | .Padding(padding_mode) |
| 691 | .SetDefaultValidity(); |
| 692 | if (block_mode == BlockMode::GCM) { |
| 693 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 694 | } |
| 695 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder, &key_blob, &key_characteristics)); |
| 696 | |
| 697 | EXPECT_GT(key_blob.size(), 0U); |
| 698 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 699 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 700 | |
| 701 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 702 | |
| 703 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::AES)); |
| 704 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 705 | << "Key size " << key_size << "missing"; |
| 706 | |
| 707 | CheckedDeleteKey(&key_blob); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * NewKeyGenerationTest.AesInvalidSize |
| 715 | * |
| 716 | * Verifies that specifying an invalid key size for AES key generation returns |
| 717 | * UNSUPPORTED_KEY_SIZE. |
| 718 | */ |
| 719 | TEST_P(NewKeyGenerationTest, AesInvalidSize) { |
| 720 | for (auto key_size : InvalidKeySizes(Algorithm::AES)) { |
| 721 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 722 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 723 | SCOPED_TRACE(testing::Message() |
| 724 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 725 | vector<uint8_t> key_blob; |
| 726 | vector<KeyCharacteristics> key_characteristics; |
| 727 | auto builder = AuthorizationSetBuilder() |
| 728 | .AesEncryptionKey(key_size) |
| 729 | .BlockMode(block_mode) |
| 730 | .Padding(padding_mode) |
| 731 | .SetDefaultValidity(); |
| 732 | if (block_mode == BlockMode::GCM) { |
| 733 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 734 | } |
| 735 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 736 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 742 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 743 | vector<uint8_t> key_blob; |
| 744 | vector<KeyCharacteristics> key_characteristics; |
| 745 | // No key size specified |
| 746 | auto builder = AuthorizationSetBuilder() |
| 747 | .Authorization(TAG_ALGORITHM, Algorithm::AES) |
| 748 | .BlockMode(block_mode) |
| 749 | .Padding(padding_mode) |
| 750 | .SetDefaultValidity(); |
| 751 | if (block_mode == BlockMode::GCM) { |
| 752 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 753 | } |
| 754 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 755 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * NewKeyGenerationTest.AesInvalidPadding |
| 762 | * |
| 763 | * Verifies that specifying an invalid padding on AES keys gives a failure |
| 764 | * somewhere along the way. |
| 765 | */ |
| 766 | TEST_P(NewKeyGenerationTest, AesInvalidPadding) { |
| 767 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 768 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 769 | for (auto padding_mode : InvalidPaddingModes(Algorithm::AES, block_mode)) { |
| 770 | SCOPED_TRACE(testing::Message() |
| 771 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 772 | auto builder = AuthorizationSetBuilder() |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 773 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 774 | .AesEncryptionKey(key_size) |
| 775 | .BlockMode(block_mode) |
| 776 | .Padding(padding_mode) |
| 777 | .SetDefaultValidity(); |
| 778 | if (block_mode == BlockMode::GCM) { |
| 779 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 780 | } |
| 781 | |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 782 | auto result = GenerateKey(builder); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 783 | if (result == ErrorCode::OK) { |
| 784 | // Key creation was OK but has generated a key that cannot be used. |
| 785 | auto params = |
| 786 | AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 787 | if (block_mode == BlockMode::GCM) { |
| 788 | params.Authorization(TAG_MAC_LENGTH, 128); |
| 789 | } |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 790 | auto result = Begin(KeyPurpose::ENCRYPT, params); |
| 791 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 792 | result == ErrorCode::INVALID_KEY_BLOB) |
| 793 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 794 | } else { |
| 795 | // The KeyMint implementation detected that the generated key |
| 796 | // is unusable. |
| 797 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, result); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * NewKeyGenerationTest.AesGcmMissingMinMac |
| 806 | * |
| 807 | * Verifies that specifying an invalid key size for AES key generation returns |
| 808 | * UNSUPPORTED_KEY_SIZE. |
| 809 | */ |
| 810 | TEST_P(NewKeyGenerationTest, AesGcmMissingMinMac) { |
| 811 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 812 | BlockMode block_mode = BlockMode::GCM; |
| 813 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 814 | SCOPED_TRACE(testing::Message() |
| 815 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 816 | vector<uint8_t> key_blob; |
| 817 | vector<KeyCharacteristics> key_characteristics; |
| 818 | // No MIN_MAC_LENGTH provided. |
| 819 | auto builder = AuthorizationSetBuilder() |
| 820 | .AesEncryptionKey(key_size) |
| 821 | .BlockMode(block_mode) |
| 822 | .Padding(padding_mode) |
| 823 | .SetDefaultValidity(); |
| 824 | EXPECT_EQ(ErrorCode::MISSING_MIN_MAC_LENGTH, |
| 825 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 831 | * NewKeyGenerationTest.AesGcmMinMacOutOfRange |
| 832 | * |
| 833 | * Verifies that specifying an invalid min MAC size for AES key generation returns |
| 834 | * UNSUPPORTED_MIN_MAC_LENGTH. |
| 835 | */ |
| 836 | TEST_P(NewKeyGenerationTest, AesGcmMinMacOutOfRange) { |
| 837 | for (size_t min_mac_len : {88, 136}) { |
| 838 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 839 | BlockMode block_mode = BlockMode::GCM; |
| 840 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 841 | SCOPED_TRACE(testing::Message() |
| 842 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 843 | vector<uint8_t> key_blob; |
| 844 | vector<KeyCharacteristics> key_characteristics; |
| 845 | auto builder = AuthorizationSetBuilder() |
| 846 | .AesEncryptionKey(key_size) |
| 847 | .BlockMode(block_mode) |
| 848 | .Padding(padding_mode) |
| 849 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_len) |
| 850 | .SetDefaultValidity(); |
| 851 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 852 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 859 | * NewKeyGenerationTest.TripleDes |
| 860 | * |
| 861 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 862 | * have correct characteristics. |
| 863 | */ |
| 864 | TEST_P(NewKeyGenerationTest, TripleDes) { |
| 865 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 866 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 867 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 868 | SCOPED_TRACE(testing::Message() |
| 869 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 870 | vector<uint8_t> key_blob; |
| 871 | vector<KeyCharacteristics> key_characteristics; |
| 872 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 873 | .TripleDesEncryptionKey(key_size) |
| 874 | .BlockMode(block_mode) |
| 875 | .Padding(padding_mode) |
| 876 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 877 | .SetDefaultValidity(), |
| 878 | &key_blob, &key_characteristics)); |
| 879 | |
| 880 | EXPECT_GT(key_blob.size(), 0U); |
| 881 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 882 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 883 | |
| 884 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 885 | |
| 886 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 887 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 888 | << "Key size " << key_size << "missing"; |
| 889 | |
| 890 | CheckedDeleteKey(&key_blob); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * NewKeyGenerationTest.TripleDesWithAttestation |
| 898 | * |
| 899 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 900 | * have correct characteristics. |
| 901 | * |
| 902 | * Request attestation, which doesn't help for symmetric keys (as there is no public key to |
| 903 | * put in a certificate) but which isn't an error. |
| 904 | */ |
| 905 | TEST_P(NewKeyGenerationTest, TripleDesWithAttestation) { |
| 906 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 907 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 908 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 909 | SCOPED_TRACE(testing::Message() |
| 910 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 911 | |
| 912 | auto challenge = "hello"; |
| 913 | auto app_id = "foo"; |
| 914 | |
| 915 | vector<uint8_t> key_blob; |
| 916 | vector<KeyCharacteristics> key_characteristics; |
| 917 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 918 | .TripleDesEncryptionKey(key_size) |
| 919 | .BlockMode(block_mode) |
| 920 | .Padding(padding_mode) |
| 921 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 922 | .AttestationChallenge(challenge) |
| 923 | .AttestationApplicationId(app_id) |
| 924 | .SetDefaultValidity(), |
| 925 | &key_blob, &key_characteristics)); |
| 926 | |
| 927 | EXPECT_GT(key_blob.size(), 0U); |
| 928 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 929 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 930 | |
| 931 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 932 | |
| 933 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 934 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 935 | << "Key size " << key_size << "missing"; |
| 936 | |
| 937 | CheckedDeleteKey(&key_blob); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * NewKeyGenerationTest.TripleDesInvalidSize |
| 945 | * |
| 946 | * Verifies that specifying an invalid key size for 3-DES key generation returns |
| 947 | * UNSUPPORTED_KEY_SIZE. |
| 948 | */ |
| 949 | TEST_P(NewKeyGenerationTest, TripleDesInvalidSize) { |
| 950 | for (auto key_size : InvalidKeySizes(Algorithm::TRIPLE_DES)) { |
| 951 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 952 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 953 | SCOPED_TRACE(testing::Message() |
| 954 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 955 | vector<uint8_t> key_blob; |
| 956 | vector<KeyCharacteristics> key_characteristics; |
| 957 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 958 | GenerateKey(AuthorizationSetBuilder() |
| 959 | .TripleDesEncryptionKey(key_size) |
| 960 | .BlockMode(block_mode) |
| 961 | .Padding(padding_mode) |
| 962 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 963 | .SetDefaultValidity(), |
| 964 | &key_blob, &key_characteristics)); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | // Omitting the key size fails. |
| 970 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 971 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 972 | SCOPED_TRACE(testing::Message() |
| 973 | << "3DES-default-" << block_mode << "-" << padding_mode); |
| 974 | vector<uint8_t> key_blob; |
| 975 | vector<KeyCharacteristics> key_characteristics; |
| 976 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 977 | GenerateKey(AuthorizationSetBuilder() |
| 978 | .Authorization(TAG_ALGORITHM, Algorithm::TRIPLE_DES) |
| 979 | .BlockMode(block_mode) |
| 980 | .Padding(padding_mode) |
| 981 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 982 | .SetDefaultValidity(), |
| 983 | &key_blob, &key_characteristics)); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 989 | * NewKeyGenerationTest.Rsa |
| 990 | * |
| 991 | * Verifies that keymint can generate all required RSA key sizes, and that the resulting keys |
| 992 | * have correct characteristics. |
| 993 | */ |
| 994 | TEST_P(NewKeyGenerationTest, Rsa) { |
| 995 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 996 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 997 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 998 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 999 | .RsaSigningKey(key_size, 65537) |
| 1000 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1001 | .Padding(PaddingMode::NONE) |
| 1002 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1003 | &key_blob, &key_characteristics)); |
| 1004 | |
| 1005 | ASSERT_GT(key_blob.size(), 0U); |
| 1006 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1007 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1008 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1009 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1010 | |
| 1011 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1012 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1013 | << "Key size " << key_size << "missing"; |
| 1014 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1015 | |
| 1016 | CheckedDeleteKey(&key_blob); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /* |
Prashant Patil | 6c1adf0 | 2021-11-22 06:21:21 +0000 | [diff] [blame] | 1021 | * NewKeyGenerationTest.RsaWithMissingValidity |
| 1022 | * |
| 1023 | * Verifies that keymint returns an error while generating asymmetric key |
| 1024 | * without providing NOT_BEFORE and NOT_AFTER parameters. |
| 1025 | */ |
| 1026 | TEST_P(NewKeyGenerationTest, RsaWithMissingValidity) { |
| 1027 | // Per RFC 5280 4.1.2.5, an undefined expiration (not-after) field should be set to |
| 1028 | // GeneralizedTime 999912312359559, which is 253402300799000 ms from Jan 1, 1970. |
| 1029 | constexpr uint64_t kUndefinedExpirationDateTime = 253402300799000; |
| 1030 | |
| 1031 | vector<uint8_t> key_blob; |
| 1032 | vector<KeyCharacteristics> key_characteristics; |
| 1033 | ASSERT_EQ(ErrorCode::MISSING_NOT_BEFORE, |
| 1034 | GenerateKey(AuthorizationSetBuilder() |
| 1035 | .RsaSigningKey(2048, 65537) |
| 1036 | .Digest(Digest::NONE) |
| 1037 | .Padding(PaddingMode::NONE) |
| 1038 | .Authorization(TAG_CERTIFICATE_NOT_AFTER, |
| 1039 | kUndefinedExpirationDateTime), |
| 1040 | &key_blob, &key_characteristics)); |
| 1041 | |
| 1042 | ASSERT_EQ(ErrorCode::MISSING_NOT_AFTER, |
| 1043 | GenerateKey(AuthorizationSetBuilder() |
| 1044 | .RsaSigningKey(2048, 65537) |
| 1045 | .Digest(Digest::NONE) |
| 1046 | .Padding(PaddingMode::NONE) |
| 1047 | .Authorization(TAG_CERTIFICATE_NOT_BEFORE, 0), |
| 1048 | &key_blob, &key_characteristics)); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1052 | * NewKeyGenerationTest.RsaWithAttestation |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1053 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1054 | * Verifies that keymint can generate all required RSA key sizes with attestation, and that the |
| 1055 | * resulting keys have correct characteristics. |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1056 | */ |
| 1057 | TEST_P(NewKeyGenerationTest, RsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1058 | auto challenge = "hello"; |
| 1059 | auto app_id = "foo"; |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1060 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1061 | auto subject = "cert subj 2"; |
| 1062 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1063 | |
| 1064 | uint64_t serial_int = 66; |
| 1065 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1066 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1067 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1068 | vector<uint8_t> key_blob; |
| 1069 | vector<KeyCharacteristics> key_characteristics; |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1070 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1071 | .RsaSigningKey(key_size, 65537) |
| 1072 | .Digest(Digest::NONE) |
| 1073 | .Padding(PaddingMode::NONE) |
| 1074 | .AttestationChallenge(challenge) |
| 1075 | .AttestationApplicationId(app_id) |
| 1076 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1077 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1078 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1079 | .SetDefaultValidity(), |
| 1080 | &key_blob, &key_characteristics); |
| 1081 | // Strongbox may not support factory provisioned attestation key. |
| 1082 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1083 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1084 | } |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1085 | |
| 1086 | ASSERT_GT(key_blob.size(), 0U); |
| 1087 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1088 | CheckCharacteristics(key_blob, key_characteristics); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1089 | |
| 1090 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1091 | |
| 1092 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1093 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1094 | << "Key size " << key_size << "missing"; |
| 1095 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1096 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1097 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1098 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1099 | ASSERT_GT(cert_chain_.size(), 0); |
| 1100 | |
| 1101 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1102 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1103 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1104 | sw_enforced, hw_enforced, SecLevel(), |
| 1105 | cert_chain_[0].encodedCertificate)); |
| 1106 | |
| 1107 | CheckedDeleteKey(&key_blob); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1112 | * NewKeyGenerationTest.RsaWithRpkAttestation |
| 1113 | * |
| 1114 | * Verifies that keymint can generate all required RSA key sizes, using an attestation key |
| 1115 | * that has been generated using an associate IRemotelyProvisionedComponent. |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 1116 | * |
| 1117 | * This test is disabled because the KeyMint specification does not require that implementations |
| 1118 | * of the first version of KeyMint have to also implement IRemotelyProvisionedComponent. |
| 1119 | * However, the test is kept in the code because KeyMint v2 will impose this requirement. |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1120 | */ |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 1121 | TEST_P(NewKeyGenerationTest, DISABLED_RsaWithRpkAttestation) { |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1122 | // There should be an IRemotelyProvisionedComponent instance associated with the KeyMint |
| 1123 | // instance. |
| 1124 | std::shared_ptr<IRemotelyProvisionedComponent> rp; |
| 1125 | ASSERT_TRUE(matching_rp_instance(GetParam(), &rp)) |
| 1126 | << "No IRemotelyProvisionedComponent found that matches KeyMint device " << GetParam(); |
| 1127 | |
| 1128 | // Generate a P-256 keypair to use as an attestation key. |
| 1129 | MacedPublicKey macedPubKey; |
| 1130 | std::vector<uint8_t> privateKeyBlob; |
| 1131 | auto status = |
| 1132 | rp->generateEcdsaP256KeyPair(/* testMode= */ false, &macedPubKey, &privateKeyBlob); |
| 1133 | ASSERT_TRUE(status.isOk()); |
| 1134 | vector<uint8_t> coseKeyData; |
| 1135 | check_maced_pubkey(macedPubKey, /* testMode= */ false, &coseKeyData); |
| 1136 | |
| 1137 | AttestationKey attestation_key; |
| 1138 | attestation_key.keyBlob = std::move(privateKeyBlob); |
| 1139 | attestation_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 1140 | |
| 1141 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1142 | auto challenge = "hello"; |
| 1143 | auto app_id = "foo"; |
| 1144 | |
| 1145 | vector<uint8_t> key_blob; |
| 1146 | vector<KeyCharacteristics> key_characteristics; |
| 1147 | ASSERT_EQ(ErrorCode::OK, |
| 1148 | GenerateKey(AuthorizationSetBuilder() |
| 1149 | .RsaSigningKey(key_size, 65537) |
| 1150 | .Digest(Digest::NONE) |
| 1151 | .Padding(PaddingMode::NONE) |
| 1152 | .AttestationChallenge(challenge) |
| 1153 | .AttestationApplicationId(app_id) |
| 1154 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1155 | .SetDefaultValidity(), |
| 1156 | attestation_key, &key_blob, &key_characteristics, &cert_chain_)); |
| 1157 | |
| 1158 | ASSERT_GT(key_blob.size(), 0U); |
| 1159 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1160 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1161 | |
| 1162 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1163 | |
| 1164 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1165 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1166 | << "Key size " << key_size << "missing"; |
| 1167 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1168 | |
| 1169 | // Attestation by itself is not valid (last entry is not self-signed). |
| 1170 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_)); |
| 1171 | |
| 1172 | // The signature over the attested key should correspond to the P256 public key. |
| 1173 | X509_Ptr key_cert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 1174 | ASSERT_TRUE(key_cert.get()); |
| 1175 | EVP_PKEY_Ptr signing_pubkey; |
| 1176 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 1177 | ASSERT_TRUE(signing_pubkey.get()); |
| 1178 | |
| 1179 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 1180 | << "Verification of attested certificate failed " |
| 1181 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
| 1182 | |
| 1183 | CheckedDeleteKey(&key_blob); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1188 | * NewKeyGenerationTest.RsaEncryptionWithAttestation |
| 1189 | * |
| 1190 | * Verifies that keymint attestation for RSA encryption keys with challenge and |
| 1191 | * app id is also successful. |
| 1192 | */ |
| 1193 | TEST_P(NewKeyGenerationTest, RsaEncryptionWithAttestation) { |
| 1194 | auto key_size = 2048; |
| 1195 | auto challenge = "hello"; |
| 1196 | auto app_id = "foo"; |
| 1197 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1198 | auto subject = "subj 2"; |
| 1199 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1200 | |
| 1201 | uint64_t serial_int = 111166; |
| 1202 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1203 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1204 | vector<uint8_t> key_blob; |
| 1205 | vector<KeyCharacteristics> key_characteristics; |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1206 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1207 | .RsaEncryptionKey(key_size, 65537) |
| 1208 | .Padding(PaddingMode::NONE) |
| 1209 | .AttestationChallenge(challenge) |
| 1210 | .AttestationApplicationId(app_id) |
| 1211 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1212 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1213 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1214 | .SetDefaultValidity(), |
| 1215 | &key_blob, &key_characteristics); |
| 1216 | // Strongbox may not support factory provisioned attestation key. |
| 1217 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1218 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1219 | } |
| 1220 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | ASSERT_GT(key_blob.size(), 0U); |
| 1223 | AuthorizationSet auths; |
| 1224 | for (auto& entry : key_characteristics) { |
| 1225 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1226 | } |
| 1227 | |
| 1228 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 1229 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 1230 | |
| 1231 | // Verify that App data and ROT are NOT included. |
| 1232 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 1233 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
| 1234 | |
| 1235 | // Check that some unexpected tags/values are NOT present. |
| 1236 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
| 1237 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::VERIFY)); |
| 1238 | |
| 1239 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 1240 | |
| 1241 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
| 1242 | ASSERT_TRUE(os_ver); |
| 1243 | EXPECT_EQ(*os_ver, os_version()); |
| 1244 | |
| 1245 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1246 | |
| 1247 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1248 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1249 | << "Key size " << key_size << "missing"; |
| 1250 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1251 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1252 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1253 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1254 | ASSERT_GT(cert_chain_.size(), 0); |
| 1255 | |
| 1256 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1257 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1258 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1259 | sw_enforced, hw_enforced, SecLevel(), |
| 1260 | cert_chain_[0].encodedCertificate)); |
| 1261 | |
| 1262 | CheckedDeleteKey(&key_blob); |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * NewKeyGenerationTest.RsaWithSelfSign |
| 1267 | * |
| 1268 | * Verifies that attesting to RSA key generation is successful, and returns |
| 1269 | * self signed certificate if no challenge is provided. And signing etc |
| 1270 | * works as expected. |
| 1271 | */ |
| 1272 | TEST_P(NewKeyGenerationTest, RsaWithSelfSign) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1273 | auto subject = "cert subj subj subj subj subj subj 22222222222222222222"; |
| 1274 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1275 | |
| 1276 | uint64_t serial_int = 0; |
| 1277 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1278 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1279 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1280 | vector<uint8_t> key_blob; |
| 1281 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1282 | ASSERT_EQ(ErrorCode::OK, |
| 1283 | GenerateKey(AuthorizationSetBuilder() |
| 1284 | .RsaSigningKey(key_size, 65537) |
| 1285 | .Digest(Digest::NONE) |
| 1286 | .Padding(PaddingMode::NONE) |
| 1287 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1288 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1289 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1290 | .SetDefaultValidity(), |
| 1291 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | ASSERT_GT(key_blob.size(), 0U); |
| 1294 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1295 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1298 | |
| 1299 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1300 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1301 | << "Key size " << key_size << "missing"; |
| 1302 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1303 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1304 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1305 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1306 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1307 | |
| 1308 | CheckedDeleteKey(&key_blob); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * NewKeyGenerationTest.RsaWithAttestationMissAppId |
| 1314 | * |
| 1315 | * Verifies that attesting to RSA checks for missing app ID. |
| 1316 | */ |
| 1317 | TEST_P(NewKeyGenerationTest, RsaWithAttestationMissAppId) { |
| 1318 | auto challenge = "hello"; |
| 1319 | vector<uint8_t> key_blob; |
| 1320 | vector<KeyCharacteristics> key_characteristics; |
| 1321 | |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1322 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1323 | .RsaSigningKey(2048, 65537) |
| 1324 | .Digest(Digest::NONE) |
| 1325 | .Padding(PaddingMode::NONE) |
| 1326 | .AttestationChallenge(challenge) |
| 1327 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1328 | .SetDefaultValidity(), |
| 1329 | &key_blob, &key_characteristics); |
| 1330 | // Strongbox may not support factory provisioned attestation key. |
| 1331 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1332 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1333 | } |
| 1334 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, result); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * NewKeyGenerationTest.RsaWithAttestationAppIdIgnored |
| 1339 | * |
| 1340 | * Verifies that attesting to RSA ignores app id if challenge is missing. |
| 1341 | */ |
| 1342 | TEST_P(NewKeyGenerationTest, RsaWithAttestationAppIdIgnored) { |
| 1343 | auto key_size = 2048; |
| 1344 | auto app_id = "foo"; |
| 1345 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1346 | auto subject = "cert subj 2"; |
| 1347 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1348 | |
| 1349 | uint64_t serial_int = 1; |
| 1350 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1351 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1352 | vector<uint8_t> key_blob; |
| 1353 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1354 | ASSERT_EQ(ErrorCode::OK, |
| 1355 | GenerateKey(AuthorizationSetBuilder() |
| 1356 | .RsaSigningKey(key_size, 65537) |
| 1357 | .Digest(Digest::NONE) |
| 1358 | .Padding(PaddingMode::NONE) |
| 1359 | .AttestationApplicationId(app_id) |
| 1360 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1361 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1362 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1363 | .SetDefaultValidity(), |
| 1364 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | ASSERT_GT(key_blob.size(), 0U); |
| 1367 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1368 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1369 | |
| 1370 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1371 | |
| 1372 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1373 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1374 | << "Key size " << key_size << "missing"; |
| 1375 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1376 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1377 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1378 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1379 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1380 | |
| 1381 | CheckedDeleteKey(&key_blob); |
| 1382 | } |
| 1383 | |
| 1384 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1385 | * NewKeyGenerationTest.LimitedUsageRsa |
| 1386 | * |
| 1387 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1388 | * resulting keys have correct characteristics. |
| 1389 | */ |
| 1390 | TEST_P(NewKeyGenerationTest, LimitedUsageRsa) { |
| 1391 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1392 | vector<uint8_t> key_blob; |
| 1393 | vector<KeyCharacteristics> key_characteristics; |
| 1394 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1395 | .RsaSigningKey(key_size, 65537) |
| 1396 | .Digest(Digest::NONE) |
| 1397 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1398 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1399 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1400 | &key_blob, &key_characteristics)); |
| 1401 | |
| 1402 | ASSERT_GT(key_blob.size(), 0U); |
| 1403 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1404 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1405 | |
| 1406 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1407 | |
| 1408 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1409 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1410 | << "Key size " << key_size << "missing"; |
| 1411 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1412 | |
| 1413 | // Check the usage count limit tag appears in the authorizations. |
| 1414 | AuthorizationSet auths; |
| 1415 | for (auto& entry : key_characteristics) { |
| 1416 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1417 | } |
| 1418 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1419 | << "key usage count limit " << 1U << " missing"; |
| 1420 | |
| 1421 | CheckedDeleteKey(&key_blob); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1426 | * NewKeyGenerationTest.LimitedUsageRsaWithAttestation |
| 1427 | * |
| 1428 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1429 | * resulting keys have correct characteristics and attestation. |
| 1430 | */ |
| 1431 | TEST_P(NewKeyGenerationTest, LimitedUsageRsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1432 | auto challenge = "hello"; |
| 1433 | auto app_id = "foo"; |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1434 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1435 | auto subject = "cert subj 2"; |
| 1436 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1437 | |
| 1438 | uint64_t serial_int = 66; |
| 1439 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1440 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1441 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1442 | vector<uint8_t> key_blob; |
| 1443 | vector<KeyCharacteristics> key_characteristics; |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1444 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1445 | .RsaSigningKey(key_size, 65537) |
| 1446 | .Digest(Digest::NONE) |
| 1447 | .Padding(PaddingMode::NONE) |
| 1448 | .AttestationChallenge(challenge) |
| 1449 | .AttestationApplicationId(app_id) |
| 1450 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1451 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1452 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1453 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1454 | .SetDefaultValidity(), |
| 1455 | &key_blob, &key_characteristics); |
| 1456 | // Strongbox may not support factory provisioned attestation key. |
| 1457 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1458 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1459 | } |
| 1460 | ASSERT_EQ(ErrorCode::OK, result); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1461 | |
| 1462 | ASSERT_GT(key_blob.size(), 0U); |
| 1463 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1464 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1465 | |
| 1466 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1467 | |
| 1468 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1469 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1470 | << "Key size " << key_size << "missing"; |
| 1471 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1472 | |
| 1473 | // Check the usage count limit tag appears in the authorizations. |
| 1474 | AuthorizationSet auths; |
| 1475 | for (auto& entry : key_characteristics) { |
| 1476 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1477 | } |
| 1478 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1479 | << "key usage count limit " << 1U << " missing"; |
| 1480 | |
| 1481 | // Check the usage count limit tag also appears in the attestation. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1482 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1483 | ASSERT_GT(cert_chain_.size(), 0); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1484 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1485 | |
| 1486 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1487 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1488 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1489 | sw_enforced, hw_enforced, SecLevel(), |
| 1490 | cert_chain_[0].encodedCertificate)); |
| 1491 | |
| 1492 | CheckedDeleteKey(&key_blob); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1497 | * NewKeyGenerationTest.NoInvalidRsaSizes |
| 1498 | * |
| 1499 | * Verifies that keymint cannot generate any RSA key sizes that are designated as invalid. |
| 1500 | */ |
| 1501 | TEST_P(NewKeyGenerationTest, NoInvalidRsaSizes) { |
| 1502 | for (auto key_size : InvalidKeySizes(Algorithm::RSA)) { |
| 1503 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1504 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1505 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1506 | GenerateKey(AuthorizationSetBuilder() |
| 1507 | .RsaSigningKey(key_size, 65537) |
| 1508 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1509 | .Padding(PaddingMode::NONE) |
| 1510 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1511 | &key_blob, &key_characteristics)); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * NewKeyGenerationTest.RsaNoDefaultSize |
| 1517 | * |
| 1518 | * Verifies that failing to specify a key size for RSA key generation returns |
| 1519 | * UNSUPPORTED_KEY_SIZE. |
| 1520 | */ |
| 1521 | TEST_P(NewKeyGenerationTest, RsaNoDefaultSize) { |
| 1522 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1523 | GenerateKey(AuthorizationSetBuilder() |
| 1524 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 1525 | .Authorization(TAG_RSA_PUBLIC_EXPONENT, 3U) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1526 | .SigningKey() |
| 1527 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1531 | * NewKeyGenerationTest.RsaMissingParams |
| 1532 | * |
| 1533 | * Verifies that omitting optional tags works. |
| 1534 | */ |
| 1535 | TEST_P(NewKeyGenerationTest, RsaMissingParams) { |
| 1536 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1537 | ASSERT_EQ(ErrorCode::OK, |
| 1538 | GenerateKey( |
| 1539 | AuthorizationSetBuilder().RsaKey(key_size, 65537).SetDefaultValidity())); |
| 1540 | CheckedDeleteKey(); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1545 | * NewKeyGenerationTest.Ecdsa |
| 1546 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1547 | * Verifies that keymint can generate all required EC curves, and that the resulting keys |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1548 | * have correct characteristics. |
| 1549 | */ |
| 1550 | TEST_P(NewKeyGenerationTest, Ecdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1551 | for (auto curve : ValidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1552 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1553 | vector<KeyCharacteristics> key_characteristics; |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1554 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1555 | .EcdsaSigningKey(curve) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1556 | .Digest(Digest::NONE) |
| 1557 | .SetDefaultValidity(), |
| 1558 | &key_blob, &key_characteristics)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1559 | ASSERT_GT(key_blob.size(), 0U); |
| 1560 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1561 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1562 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1563 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1564 | |
| 1565 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1566 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1567 | |
| 1568 | CheckedDeleteKey(&key_blob); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1573 | * NewKeyGenerationTest.EcdsaCurve25519 |
| 1574 | * |
| 1575 | * Verifies that keymint can generate a curve25519 key, and that the resulting key |
| 1576 | * has correct characteristics. |
| 1577 | */ |
| 1578 | TEST_P(NewKeyGenerationTest, EcdsaCurve25519) { |
| 1579 | if (!Curve25519Supported()) { |
| 1580 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1581 | } |
| 1582 | |
| 1583 | EcCurve curve = EcCurve::CURVE_25519; |
| 1584 | vector<uint8_t> key_blob; |
| 1585 | vector<KeyCharacteristics> key_characteristics; |
| 1586 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1587 | .EcdsaSigningKey(curve) |
| 1588 | .Digest(Digest::NONE) |
| 1589 | .SetDefaultValidity(), |
| 1590 | &key_blob, &key_characteristics); |
| 1591 | ASSERT_EQ(result, ErrorCode::OK); |
| 1592 | ASSERT_GT(key_blob.size(), 0U); |
| 1593 | |
| 1594 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1595 | ASSERT_GT(cert_chain_.size(), 0); |
| 1596 | |
| 1597 | CheckBaseParams(key_characteristics); |
| 1598 | CheckCharacteristics(key_blob, key_characteristics); |
| 1599 | |
| 1600 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1601 | |
| 1602 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
| 1603 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
| 1604 | |
| 1605 | CheckedDeleteKey(&key_blob); |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * NewKeyGenerationTest.EcCurve25519MultiPurposeFail |
| 1610 | * |
| 1611 | * Verifies that KeyMint rejects an attempt to generate a curve 25519 key for both |
| 1612 | * SIGN and AGREE_KEY. |
| 1613 | */ |
| 1614 | TEST_P(NewKeyGenerationTest, EcdsaCurve25519MultiPurposeFail) { |
| 1615 | if (!Curve25519Supported()) { |
| 1616 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1617 | } |
| 1618 | |
| 1619 | EcCurve curve = EcCurve::CURVE_25519; |
| 1620 | vector<uint8_t> key_blob; |
| 1621 | vector<KeyCharacteristics> key_characteristics; |
| 1622 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1623 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 1624 | .EcdsaSigningKey(curve) |
| 1625 | .Digest(Digest::NONE) |
| 1626 | .SetDefaultValidity(), |
| 1627 | &key_blob, &key_characteristics); |
| 1628 | ASSERT_EQ(result, ErrorCode::INCOMPATIBLE_PURPOSE); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
Prashant Patil | 6c1adf0 | 2021-11-22 06:21:21 +0000 | [diff] [blame] | 1632 | * NewKeyGenerationTest.EcdsaWithMissingValidity |
| 1633 | * |
| 1634 | * Verifies that keymint returns an error while generating asymmetric key |
| 1635 | * without providing NOT_BEFORE and NOT_AFTER parameters. |
| 1636 | */ |
| 1637 | TEST_P(NewKeyGenerationTest, EcdsaWithMissingValidity) { |
| 1638 | // Per RFC 5280 4.1.2.5, an undefined expiration (not-after) field should be set to |
| 1639 | // GeneralizedTime 999912312359559, which is 253402300799000 ms from Jan 1, 1970. |
| 1640 | constexpr uint64_t kUndefinedExpirationDateTime = 253402300799000; |
| 1641 | |
| 1642 | vector<uint8_t> key_blob; |
| 1643 | vector<KeyCharacteristics> key_characteristics; |
| 1644 | ASSERT_EQ(ErrorCode::MISSING_NOT_BEFORE, |
| 1645 | GenerateKey(AuthorizationSetBuilder() |
| 1646 | .EcdsaSigningKey(EcCurve::P_256) |
| 1647 | .Digest(Digest::NONE) |
| 1648 | .Authorization(TAG_CERTIFICATE_NOT_AFTER, |
| 1649 | kUndefinedExpirationDateTime), |
| 1650 | &key_blob, &key_characteristics)); |
| 1651 | |
| 1652 | ASSERT_EQ(ErrorCode::MISSING_NOT_AFTER, |
| 1653 | GenerateKey(AuthorizationSetBuilder() |
| 1654 | .EcdsaSigningKey(EcCurve::P_256) |
| 1655 | .Digest(Digest::NONE) |
| 1656 | .Authorization(TAG_CERTIFICATE_NOT_BEFORE, 0), |
| 1657 | &key_blob, &key_characteristics)); |
| 1658 | } |
| 1659 | |
| 1660 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1661 | * NewKeyGenerationTest.EcdsaAttestation |
| 1662 | * |
| 1663 | * Verifies that for all Ecdsa key sizes, if challenge and app id is provided, |
| 1664 | * an attestation will be generated. |
| 1665 | */ |
| 1666 | TEST_P(NewKeyGenerationTest, EcdsaAttestation) { |
| 1667 | auto challenge = "hello"; |
| 1668 | auto app_id = "foo"; |
| 1669 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1670 | auto subject = "cert subj 2"; |
| 1671 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1672 | |
| 1673 | uint64_t serial_int = 0xFFFFFFFFFFFFFFFF; |
| 1674 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1675 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1676 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1677 | vector<uint8_t> key_blob; |
| 1678 | vector<KeyCharacteristics> key_characteristics; |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1679 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1680 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1681 | .EcdsaSigningKey(curve) |
| 1682 | .Digest(Digest::NONE) |
| 1683 | .AttestationChallenge(challenge) |
| 1684 | .AttestationApplicationId(app_id) |
| 1685 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1686 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1687 | .SetDefaultValidity(), |
| 1688 | &key_blob, &key_characteristics); |
| 1689 | // Strongbox may not support factory provisioned attestation key. |
| 1690 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1691 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1692 | } |
| 1693 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1694 | ASSERT_GT(key_blob.size(), 0U); |
| 1695 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1696 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1697 | |
| 1698 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1699 | |
| 1700 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1701 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1702 | |
| 1703 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1704 | ASSERT_GT(cert_chain_.size(), 0); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1705 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1706 | |
| 1707 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1708 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1709 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1710 | sw_enforced, hw_enforced, SecLevel(), |
| 1711 | cert_chain_[0].encodedCertificate)); |
| 1712 | |
| 1713 | CheckedDeleteKey(&key_blob); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1718 | * NewKeyGenerationTest.EcdsaAttestationCurve25519 |
| 1719 | * |
| 1720 | * Verifies that for a curve 25519 key, if challenge and app id is provided, |
| 1721 | * an attestation will be generated. |
| 1722 | */ |
| 1723 | TEST_P(NewKeyGenerationTest, EcdsaAttestationCurve25519) { |
| 1724 | if (!Curve25519Supported()) { |
| 1725 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1726 | } |
| 1727 | |
| 1728 | EcCurve curve = EcCurve::CURVE_25519; |
| 1729 | auto challenge = "hello"; |
| 1730 | auto app_id = "foo"; |
| 1731 | |
| 1732 | auto subject = "cert subj 2"; |
| 1733 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1734 | |
| 1735 | uint64_t serial_int = 0xFFFFFFFFFFFFFFFF; |
| 1736 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1737 | |
| 1738 | vector<uint8_t> key_blob; |
| 1739 | vector<KeyCharacteristics> key_characteristics; |
| 1740 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1741 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1742 | .EcdsaSigningKey(curve) |
| 1743 | .Digest(Digest::NONE) |
| 1744 | .AttestationChallenge(challenge) |
| 1745 | .AttestationApplicationId(app_id) |
| 1746 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1747 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1748 | .SetDefaultValidity(), |
| 1749 | &key_blob, &key_characteristics); |
| 1750 | ASSERT_EQ(ErrorCode::OK, result); |
| 1751 | ASSERT_GT(key_blob.size(), 0U); |
| 1752 | CheckBaseParams(key_characteristics); |
| 1753 | CheckCharacteristics(key_blob, key_characteristics); |
| 1754 | |
| 1755 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1756 | |
| 1757 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
| 1758 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
| 1759 | |
| 1760 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1761 | ASSERT_GT(cert_chain_.size(), 0); |
| 1762 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
| 1763 | |
| 1764 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1765 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1766 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
| 1767 | sw_enforced, hw_enforced, SecLevel(), |
| 1768 | cert_chain_[0].encodedCertificate)); |
| 1769 | |
| 1770 | CheckedDeleteKey(&key_blob); |
| 1771 | } |
| 1772 | |
| 1773 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1774 | * NewKeyGenerationTest.EcdsaAttestationTags |
| 1775 | * |
| 1776 | * Verifies that creation of an attested ECDSA key includes various tags in the |
| 1777 | * attestation extension. |
| 1778 | */ |
| 1779 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTags) { |
| 1780 | auto challenge = "hello"; |
| 1781 | auto app_id = "foo"; |
| 1782 | auto subject = "cert subj 2"; |
| 1783 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1784 | uint64_t serial_int = 0x1010; |
| 1785 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1786 | const AuthorizationSetBuilder base_builder = |
| 1787 | AuthorizationSetBuilder() |
| 1788 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1789 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1790 | .Digest(Digest::NONE) |
| 1791 | .AttestationChallenge(challenge) |
| 1792 | .AttestationApplicationId(app_id) |
| 1793 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1794 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1795 | .SetDefaultValidity(); |
| 1796 | |
| 1797 | // Various tags that map to fields in the attestation extension ASN.1 schema. |
| 1798 | auto extra_tags = AuthorizationSetBuilder() |
| 1799 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 1800 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 1801 | .Authorization(TAG_ACTIVE_DATETIME, 1619621648000) |
| 1802 | .Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, 1619621648000) |
| 1803 | .Authorization(TAG_USAGE_EXPIRE_DATETIME, 1619621999000) |
| 1804 | .Authorization(TAG_USAGE_COUNT_LIMIT, 42) |
| 1805 | .Authorization(TAG_AUTH_TIMEOUT, 100000) |
| 1806 | .Authorization(TAG_ALLOW_WHILE_ON_BODY) |
| 1807 | .Authorization(TAG_TRUSTED_USER_PRESENCE_REQUIRED) |
| 1808 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 1809 | .Authorization(TAG_UNLOCKED_DEVICE_REQUIRED) |
| 1810 | .Authorization(TAG_CREATION_DATETIME, 1619621648000); |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1811 | |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1812 | for (const KeyParameter& tag : extra_tags) { |
| 1813 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1814 | vector<uint8_t> key_blob; |
| 1815 | vector<KeyCharacteristics> key_characteristics; |
| 1816 | AuthorizationSetBuilder builder = base_builder; |
| 1817 | builder.push_back(tag); |
| 1818 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
| 1819 | if (result == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE && |
| 1820 | tag.tag == TAG_ROLLBACK_RESISTANCE) { |
| 1821 | continue; |
| 1822 | } |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1823 | if (result == ErrorCode::UNSUPPORTED_TAG && tag.tag == TAG_TRUSTED_USER_PRESENCE_REQUIRED) { |
| 1824 | // Tag not required to be supported by all KeyMint implementations. |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1825 | continue; |
| 1826 | } |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1827 | // Strongbox may not support factory provisioned attestation key. |
| 1828 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1829 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1830 | } |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1831 | ASSERT_EQ(result, ErrorCode::OK); |
| 1832 | ASSERT_GT(key_blob.size(), 0U); |
| 1833 | |
| 1834 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1835 | ASSERT_GT(cert_chain_.size(), 0); |
| 1836 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1837 | |
| 1838 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1839 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1840 | // Some tags are optional, so don't require them to be in the enforcements. |
| 1841 | if (tag.tag != TAG_ATTESTATION_APPLICATION_ID && tag.tag != TAG_ALLOW_WHILE_ON_BODY) { |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1842 | EXPECT_TRUE(hw_enforced.Contains(tag.tag) || sw_enforced.Contains(tag.tag)) |
| 1843 | << tag << " not in hw:" << hw_enforced << " nor sw:" << sw_enforced; |
| 1844 | } |
| 1845 | |
| 1846 | // Verifying the attestation record will check for the specific tag because |
| 1847 | // it's included in the authorizations. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1848 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 1849 | hw_enforced, SecLevel(), |
| 1850 | cert_chain_[0].encodedCertificate)); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1851 | |
| 1852 | CheckedDeleteKey(&key_blob); |
| 1853 | } |
| 1854 | |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1855 | // Collection of invalid attestation ID tags. |
| 1856 | auto invalid_tags = |
| 1857 | AuthorizationSetBuilder() |
| 1858 | .Authorization(TAG_ATTESTATION_ID_BRAND, "bogus-brand") |
| 1859 | .Authorization(TAG_ATTESTATION_ID_DEVICE, "devious-device") |
| 1860 | .Authorization(TAG_ATTESTATION_ID_PRODUCT, "punctured-product") |
| 1861 | .Authorization(TAG_ATTESTATION_ID_SERIAL, "suspicious-serial") |
| 1862 | .Authorization(TAG_ATTESTATION_ID_IMEI, "invalid-imei") |
| 1863 | .Authorization(TAG_ATTESTATION_ID_MEID, "mismatching-meid") |
| 1864 | .Authorization(TAG_ATTESTATION_ID_MANUFACTURER, "malformed-manufacturer") |
| 1865 | .Authorization(TAG_ATTESTATION_ID_MODEL, "malicious-model"); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1866 | for (const KeyParameter& tag : invalid_tags) { |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1867 | SCOPED_TRACE(testing::Message() << "-incorrect-tag-" << tag); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1868 | vector<uint8_t> key_blob; |
| 1869 | vector<KeyCharacteristics> key_characteristics; |
| 1870 | AuthorizationSetBuilder builder = |
| 1871 | AuthorizationSetBuilder() |
| 1872 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1873 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1874 | .Digest(Digest::NONE) |
| 1875 | .AttestationChallenge(challenge) |
| 1876 | .AttestationApplicationId(app_id) |
| 1877 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1878 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1879 | .SetDefaultValidity(); |
| 1880 | builder.push_back(tag); |
| 1881 | ASSERT_EQ(ErrorCode::CANNOT_ATTEST_IDS, |
| 1882 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | /* |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1887 | * NewKeyGenerationTest.EcdsaAttestationIdTags |
| 1888 | * |
| 1889 | * Verifies that creation of an attested ECDSA key includes various ID tags in the |
| 1890 | * attestation extension. |
| 1891 | */ |
| 1892 | TEST_P(NewKeyGenerationTest, EcdsaAttestationIdTags) { |
| 1893 | auto challenge = "hello"; |
| 1894 | auto app_id = "foo"; |
| 1895 | auto subject = "cert subj 2"; |
| 1896 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1897 | uint64_t serial_int = 0x1010; |
| 1898 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1899 | const AuthorizationSetBuilder base_builder = |
| 1900 | AuthorizationSetBuilder() |
| 1901 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1902 | .EcdsaSigningKey(EcCurve::P_256) |
| 1903 | .Digest(Digest::NONE) |
| 1904 | .AttestationChallenge(challenge) |
| 1905 | .AttestationApplicationId(app_id) |
| 1906 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1907 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1908 | .SetDefaultValidity(); |
| 1909 | |
| 1910 | // Various ATTESTATION_ID_* tags that map to fields in the attestation extension ASN.1 schema. |
| 1911 | auto extra_tags = AuthorizationSetBuilder(); |
| 1912 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_BRAND, "ro.product.brand"); |
| 1913 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_DEVICE, "ro.product.device"); |
| 1914 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_PRODUCT, "ro.product.name"); |
| 1915 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_SERIAL, "ro.serial"); |
| 1916 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MANUFACTURER, "ro.product.manufacturer"); |
| 1917 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MODEL, "ro.product.model"); |
| 1918 | |
| 1919 | for (const KeyParameter& tag : extra_tags) { |
| 1920 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1921 | vector<uint8_t> key_blob; |
| 1922 | vector<KeyCharacteristics> key_characteristics; |
| 1923 | AuthorizationSetBuilder builder = base_builder; |
| 1924 | builder.push_back(tag); |
| 1925 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1926 | // Strongbox may not support factory provisioned attestation key. |
| 1927 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1928 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 1929 | } |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1930 | if (result == ErrorCode::CANNOT_ATTEST_IDS) { |
| 1931 | // Device ID attestation is optional; KeyMint may not support it at all. |
| 1932 | continue; |
| 1933 | } |
| 1934 | ASSERT_EQ(result, ErrorCode::OK); |
| 1935 | ASSERT_GT(key_blob.size(), 0U); |
| 1936 | |
| 1937 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1938 | ASSERT_GT(cert_chain_.size(), 0); |
| 1939 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1940 | |
| 1941 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1942 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1943 | |
| 1944 | // The attested key characteristics will not contain APPLICATION_ID_* fields (their |
| 1945 | // spec definitions all have "Must never appear in KeyCharacteristics"), but the |
| 1946 | // attestation extension should contain them, so make sure the extra tag is added. |
| 1947 | hw_enforced.push_back(tag); |
| 1948 | |
| 1949 | // Verifying the attestation record will check for the specific tag because |
| 1950 | // it's included in the authorizations. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1951 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 1952 | hw_enforced, SecLevel(), |
| 1953 | cert_chain_[0].encodedCertificate)); |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1954 | |
| 1955 | CheckedDeleteKey(&key_blob); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | /* |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1960 | * NewKeyGenerationTest.EcdsaAttestationUniqueId |
| 1961 | * |
| 1962 | * Verifies that creation of an attested ECDSA key with a UNIQUE_ID included. |
| 1963 | */ |
| 1964 | TEST_P(NewKeyGenerationTest, EcdsaAttestationUniqueId) { |
| 1965 | auto get_unique_id = [this](const std::string& app_id, uint64_t datetime, |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1966 | vector<uint8_t>* unique_id, bool reset = false) { |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1967 | auto challenge = "hello"; |
| 1968 | auto subject = "cert subj 2"; |
| 1969 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1970 | uint64_t serial_int = 0x1010; |
| 1971 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1972 | AuthorizationSetBuilder builder = |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1973 | AuthorizationSetBuilder() |
| 1974 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1975 | .Authorization(TAG_INCLUDE_UNIQUE_ID) |
| 1976 | .EcdsaSigningKey(EcCurve::P_256) |
| 1977 | .Digest(Digest::NONE) |
| 1978 | .AttestationChallenge(challenge) |
| 1979 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1980 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1981 | .AttestationApplicationId(app_id) |
| 1982 | .Authorization(TAG_CREATION_DATETIME, datetime) |
| 1983 | .SetDefaultValidity(); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1984 | if (reset) { |
| 1985 | builder.Authorization(TAG_RESET_SINCE_ID_ROTATION); |
| 1986 | } |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1987 | |
| 1988 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder)); |
| 1989 | ASSERT_GT(key_blob_.size(), 0U); |
| 1990 | |
| 1991 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1992 | ASSERT_GT(cert_chain_.size(), 0); |
| 1993 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1994 | |
| 1995 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics_); |
| 1996 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics_); |
| 1997 | |
| 1998 | // Check that the unique ID field in the extension is non-empty. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1999 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 2000 | hw_enforced, SecLevel(), |
| 2001 | cert_chain_[0].encodedCertificate, unique_id)); |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 2002 | EXPECT_GT(unique_id->size(), 0); |
| 2003 | CheckedDeleteKey(); |
| 2004 | }; |
| 2005 | |
| 2006 | // Generate unique ID |
| 2007 | auto app_id = "foo"; |
| 2008 | uint64_t cert_date = 1619621648000; // Wed Apr 28 14:54:08 2021 in ms since epoch |
| 2009 | vector<uint8_t> unique_id; |
| 2010 | get_unique_id(app_id, cert_date, &unique_id); |
| 2011 | |
| 2012 | // Generating a new key with the same parameters should give the same unique ID. |
| 2013 | vector<uint8_t> unique_id2; |
| 2014 | get_unique_id(app_id, cert_date, &unique_id2); |
| 2015 | EXPECT_EQ(unique_id, unique_id2); |
| 2016 | |
| 2017 | // Generating a new key with a slightly different date should give the same unique ID. |
| 2018 | uint64_t rounded_date = cert_date / 2592000000LLU; |
| 2019 | uint64_t min_date = rounded_date * 2592000000LLU; |
| 2020 | uint64_t max_date = ((rounded_date + 1) * 2592000000LLU) - 1; |
| 2021 | |
| 2022 | vector<uint8_t> unique_id3; |
| 2023 | get_unique_id(app_id, min_date, &unique_id3); |
| 2024 | EXPECT_EQ(unique_id, unique_id3); |
| 2025 | |
| 2026 | vector<uint8_t> unique_id4; |
| 2027 | get_unique_id(app_id, max_date, &unique_id4); |
| 2028 | EXPECT_EQ(unique_id, unique_id4); |
| 2029 | |
| 2030 | // A different attestation application ID should yield a different unique ID. |
| 2031 | auto app_id2 = "different_foo"; |
| 2032 | vector<uint8_t> unique_id5; |
| 2033 | get_unique_id(app_id2, cert_date, &unique_id5); |
| 2034 | EXPECT_NE(unique_id, unique_id5); |
| 2035 | |
| 2036 | // A radically different date should yield a different unique ID. |
| 2037 | vector<uint8_t> unique_id6; |
| 2038 | get_unique_id(app_id, 1611621648000, &unique_id6); |
| 2039 | EXPECT_NE(unique_id, unique_id6); |
| 2040 | |
| 2041 | vector<uint8_t> unique_id7; |
| 2042 | get_unique_id(app_id, max_date + 1, &unique_id7); |
| 2043 | EXPECT_NE(unique_id, unique_id7); |
| 2044 | |
| 2045 | vector<uint8_t> unique_id8; |
| 2046 | get_unique_id(app_id, min_date - 1, &unique_id8); |
| 2047 | EXPECT_NE(unique_id, unique_id8); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 2048 | |
| 2049 | // Marking RESET_SINCE_ID_ROTATION should give a different unique ID. |
| 2050 | vector<uint8_t> unique_id9; |
| 2051 | get_unique_id(app_id, cert_date, &unique_id9, /* reset_id = */ true); |
| 2052 | EXPECT_NE(unique_id, unique_id9); |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 2056 | * NewKeyGenerationTest.EcdsaAttestationTagNoApplicationId |
| 2057 | * |
| 2058 | * Verifies that creation of an attested ECDSA key does not include APPLICATION_ID. |
| 2059 | */ |
| 2060 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTagNoApplicationId) { |
| 2061 | auto challenge = "hello"; |
| 2062 | auto attest_app_id = "foo"; |
| 2063 | auto subject = "cert subj 2"; |
| 2064 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 2065 | uint64_t serial_int = 0x1010; |
| 2066 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 2067 | |
| 2068 | // Earlier versions of the attestation extension schema included a slot: |
| 2069 | // applicationId [601] EXPLICIT OCTET_STRING OPTIONAL, |
| 2070 | // This should never have been included, and should never be filled in. |
| 2071 | // Generate an attested key that include APPLICATION_ID and APPLICATION_DATA, |
| 2072 | // to confirm that this field never makes it into the attestation extension. |
| 2073 | vector<uint8_t> key_blob; |
| 2074 | vector<KeyCharacteristics> key_characteristics; |
| 2075 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 2076 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2077 | .EcdsaSigningKey(EcCurve::P_256) |
| 2078 | .Digest(Digest::NONE) |
| 2079 | .AttestationChallenge(challenge) |
| 2080 | .AttestationApplicationId(attest_app_id) |
| 2081 | .Authorization(TAG_APPLICATION_ID, "client_id") |
| 2082 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2083 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 2084 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 2085 | .SetDefaultValidity(), |
| 2086 | &key_blob, &key_characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 2087 | // Strongbox may not support factory provisioned attestation key. |
| 2088 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2089 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 2090 | } |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 2091 | ASSERT_EQ(result, ErrorCode::OK); |
| 2092 | ASSERT_GT(key_blob.size(), 0U); |
| 2093 | |
| 2094 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2095 | ASSERT_GT(cert_chain_.size(), 0); |
| 2096 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 2097 | |
| 2098 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2099 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 2100 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, attest_app_id, sw_enforced, |
| 2101 | hw_enforced, SecLevel(), |
| 2102 | cert_chain_[0].encodedCertificate)); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 2103 | |
| 2104 | // Check that the app id is not in the cert. |
| 2105 | string app_id = "clientid"; |
| 2106 | std::vector<uint8_t> needle(reinterpret_cast<const uint8_t*>(app_id.data()), |
| 2107 | reinterpret_cast<const uint8_t*>(app_id.data()) + app_id.size()); |
| 2108 | ASSERT_EQ(std::search(cert_chain_[0].encodedCertificate.begin(), |
| 2109 | cert_chain_[0].encodedCertificate.end(), needle.begin(), needle.end()), |
| 2110 | cert_chain_[0].encodedCertificate.end()); |
| 2111 | |
| 2112 | CheckedDeleteKey(&key_blob); |
| 2113 | } |
| 2114 | |
| 2115 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2116 | * NewKeyGenerationTest.EcdsaSelfSignAttestation |
| 2117 | * |
| 2118 | * Verifies that if no challenge is provided to an Ecdsa key generation, then |
| 2119 | * the key will generate a self signed attestation. |
| 2120 | */ |
| 2121 | TEST_P(NewKeyGenerationTest, EcdsaSelfSignAttestation) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2122 | auto subject = "cert subj 2"; |
| 2123 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 2124 | |
| 2125 | uint64_t serial_int = 0x123456FFF1234; |
| 2126 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 2127 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2128 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2129 | vector<uint8_t> key_blob; |
| 2130 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2131 | ASSERT_EQ(ErrorCode::OK, |
| 2132 | GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2133 | .EcdsaSigningKey(curve) |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2134 | .Digest(Digest::NONE) |
| 2135 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 2136 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 2137 | .SetDefaultValidity(), |
| 2138 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2139 | ASSERT_GT(key_blob.size(), 0U); |
| 2140 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2141 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2142 | |
| 2143 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2144 | |
| 2145 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2146 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2147 | |
| 2148 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2149 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2150 | ASSERT_EQ(cert_chain_.size(), 1); |
| 2151 | |
| 2152 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2153 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 2154 | |
| 2155 | CheckedDeleteKey(&key_blob); |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * NewKeyGenerationTest.EcdsaAttestationRequireAppId |
| 2161 | * |
| 2162 | * Verifies that if attestation challenge is provided to Ecdsa key generation, then |
| 2163 | * app id must also be provided or else it will fail. |
| 2164 | */ |
| 2165 | TEST_P(NewKeyGenerationTest, EcdsaAttestationRequireAppId) { |
| 2166 | auto challenge = "hello"; |
| 2167 | vector<uint8_t> key_blob; |
| 2168 | vector<KeyCharacteristics> key_characteristics; |
| 2169 | |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 2170 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 2171 | .EcdsaSigningKey(EcCurve::P_256) |
| 2172 | .Digest(Digest::NONE) |
| 2173 | .AttestationChallenge(challenge) |
| 2174 | .SetDefaultValidity(), |
| 2175 | &key_blob, &key_characteristics); |
| 2176 | // Strongbox may not support factory provisioned attestation key. |
| 2177 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2178 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 2179 | } |
| 2180 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, result); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * NewKeyGenerationTest.EcdsaIgnoreAppId |
| 2185 | * |
| 2186 | * Verifies that if no challenge is provided to the Ecdsa key generation, then |
| 2187 | * any appid will be ignored, and keymint will generate a self sign certificate. |
| 2188 | */ |
| 2189 | TEST_P(NewKeyGenerationTest, EcdsaIgnoreAppId) { |
| 2190 | auto app_id = "foo"; |
| 2191 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2192 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2193 | vector<uint8_t> key_blob; |
| 2194 | vector<KeyCharacteristics> key_characteristics; |
| 2195 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2196 | .EcdsaSigningKey(curve) |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2197 | .Digest(Digest::NONE) |
| 2198 | .AttestationApplicationId(app_id) |
| 2199 | .SetDefaultValidity(), |
| 2200 | &key_blob, &key_characteristics)); |
| 2201 | |
| 2202 | ASSERT_GT(key_blob.size(), 0U); |
| 2203 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2204 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2205 | |
| 2206 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2207 | |
| 2208 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2209 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2210 | |
| 2211 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2212 | ASSERT_EQ(cert_chain_.size(), 1); |
| 2213 | |
| 2214 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2215 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 2216 | |
| 2217 | CheckedDeleteKey(&key_blob); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | /* |
| 2222 | * NewKeyGenerationTest.AttestationApplicationIDLengthProperlyEncoded |
| 2223 | * |
| 2224 | * Verifies that the Attestation Application ID software enforced tag has a proper length encoding. |
| 2225 | * Some implementations break strict encoding rules by encoding a length between 127 and 256 in one |
| 2226 | * byte. Proper DER encoding specifies that for lengths greater than 127, one byte should be used |
| 2227 | * to specify how many following bytes will be used to encode the length. |
| 2228 | */ |
| 2229 | TEST_P(NewKeyGenerationTest, AttestationApplicationIDLengthProperlyEncoded) { |
| 2230 | auto challenge = "hello"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2231 | std::vector<uint32_t> app_id_lengths{143, 258}; |
| 2232 | |
| 2233 | for (uint32_t length : app_id_lengths) { |
| 2234 | const string app_id(length, 'a'); |
| 2235 | vector<uint8_t> key_blob; |
| 2236 | vector<KeyCharacteristics> key_characteristics; |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 2237 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 2238 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2239 | .EcdsaSigningKey(EcCurve::P_256) |
| 2240 | .Digest(Digest::NONE) |
| 2241 | .AttestationChallenge(challenge) |
| 2242 | .AttestationApplicationId(app_id) |
| 2243 | .SetDefaultValidity(), |
| 2244 | &key_blob, &key_characteristics); |
| 2245 | // Strongbox may not support factory provisioned attestation key. |
| 2246 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2247 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 2248 | } |
| 2249 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2250 | ASSERT_GT(key_blob.size(), 0U); |
| 2251 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2252 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2253 | |
| 2254 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2255 | |
| 2256 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2257 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, EcCurve::P_256)) << "Curve P256 missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2258 | |
| 2259 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2260 | ASSERT_GT(cert_chain_.size(), 0); |
| 2261 | |
| 2262 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2263 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 2264 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2265 | sw_enforced, hw_enforced, SecLevel(), |
| 2266 | cert_chain_[0].encodedCertificate)); |
| 2267 | |
| 2268 | CheckedDeleteKey(&key_blob); |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2273 | * NewKeyGenerationTest.LimitedUsageEcdsa |
| 2274 | * |
| 2275 | * Verifies that KeyMint can generate all required EC key sizes with limited usage, and that the |
| 2276 | * resulting keys have correct characteristics. |
| 2277 | */ |
| 2278 | TEST_P(NewKeyGenerationTest, LimitedUsageEcdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2279 | for (auto curve : ValidCurves()) { |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2280 | vector<uint8_t> key_blob; |
| 2281 | vector<KeyCharacteristics> key_characteristics; |
| 2282 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2283 | .EcdsaSigningKey(curve) |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2284 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2285 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 2286 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2287 | &key_blob, &key_characteristics)); |
| 2288 | |
| 2289 | ASSERT_GT(key_blob.size(), 0U); |
| 2290 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2291 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2292 | |
| 2293 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2294 | |
| 2295 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2296 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2297 | |
| 2298 | // Check the usage count limit tag appears in the authorizations. |
| 2299 | AuthorizationSet auths; |
| 2300 | for (auto& entry : key_characteristics) { |
| 2301 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 2302 | } |
| 2303 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 2304 | << "key usage count limit " << 1U << " missing"; |
| 2305 | |
| 2306 | CheckedDeleteKey(&key_blob); |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2311 | * NewKeyGenerationTest.EcdsaDefaultSize |
| 2312 | * |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2313 | * Verifies that failing to specify a curve for EC key generation returns |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2314 | * UNSUPPORTED_KEY_SIZE. |
| 2315 | */ |
| 2316 | TEST_P(NewKeyGenerationTest, EcdsaDefaultSize) { |
| 2317 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2318 | GenerateKey(AuthorizationSetBuilder() |
| 2319 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 2320 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2321 | .Digest(Digest::NONE) |
| 2322 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 2326 | * NewKeyGenerationTest.EcdsaInvalidCurve |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2327 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 2328 | * Verifies that specifying an invalid curve for EC key generation returns |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2329 | * UNSUPPORTED_KEY_SIZE. |
| 2330 | */ |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 2331 | TEST_P(NewKeyGenerationTest, EcdsaInvalidCurve) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2332 | for (auto curve : InvalidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2333 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2334 | vector<KeyCharacteristics> key_characteristics; |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 2335 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 2336 | .EcdsaSigningKey(curve) |
| 2337 | .Digest(Digest::NONE) |
| 2338 | .SetDefaultValidity(), |
| 2339 | &key_blob, &key_characteristics); |
| 2340 | ASSERT_TRUE(result == ErrorCode::UNSUPPORTED_KEY_SIZE || |
| 2341 | result == ErrorCode::UNSUPPORTED_EC_CURVE); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2344 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2345 | GenerateKey(AuthorizationSetBuilder() |
| 2346 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 2347 | .Authorization(TAG_KEY_SIZE, 190) |
| 2348 | .SigningKey() |
| 2349 | .Digest(Digest::NONE) |
| 2350 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* |
| 2354 | * NewKeyGenerationTest.EcdsaMismatchKeySize |
| 2355 | * |
| 2356 | * Verifies that specifying mismatched key size and curve for EC key generation returns |
| 2357 | * INVALID_ARGUMENT. |
| 2358 | */ |
| 2359 | TEST_P(NewKeyGenerationTest, EcdsaMismatchKeySize) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2360 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2361 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2362 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2363 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2364 | auto result = GenerateKey(AuthorizationSetBuilder() |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2365 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2366 | .Authorization(TAG_KEY_SIZE, 224) |
| 2367 | .Authorization(TAG_EC_CURVE, EcCurve::P_256) |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2368 | .SigningKey() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2369 | .Digest(Digest::NONE) |
| 2370 | .SetDefaultValidity()); |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2371 | ASSERT_TRUE(result == ErrorCode::INVALID_ARGUMENT); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2375 | * NewKeyGenerationTest.EcdsaAllValidCurves |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2376 | * |
| 2377 | * Verifies that keymint does not support any curve designated as unsupported. |
| 2378 | */ |
| 2379 | TEST_P(NewKeyGenerationTest, EcdsaAllValidCurves) { |
| 2380 | Digest digest; |
| 2381 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2382 | digest = Digest::SHA_2_256; |
| 2383 | } else { |
| 2384 | digest = Digest::SHA_2_512; |
| 2385 | } |
| 2386 | for (auto curve : ValidCurves()) { |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2387 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2388 | .EcdsaSigningKey(curve) |
| 2389 | .Digest(digest) |
| 2390 | .SetDefaultValidity())) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2391 | << "Failed to generate key on curve: " << curve; |
| 2392 | CheckedDeleteKey(); |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * NewKeyGenerationTest.Hmac |
| 2398 | * |
| 2399 | * Verifies that keymint supports all required digests, and that the resulting keys have correct |
| 2400 | * characteristics. |
| 2401 | */ |
| 2402 | TEST_P(NewKeyGenerationTest, Hmac) { |
| 2403 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2404 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2405 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2406 | constexpr size_t key_size = 128; |
| 2407 | ASSERT_EQ(ErrorCode::OK, |
| 2408 | GenerateKey( |
| 2409 | AuthorizationSetBuilder().HmacKey(key_size).Digest(digest).Authorization( |
| 2410 | TAG_MIN_MAC_LENGTH, 128), |
| 2411 | &key_blob, &key_characteristics)); |
| 2412 | |
| 2413 | ASSERT_GT(key_blob.size(), 0U); |
| 2414 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2415 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2416 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2417 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2418 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2419 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2420 | << "Key size " << key_size << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2421 | |
| 2422 | CheckedDeleteKey(&key_blob); |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2427 | * NewKeyGenerationTest.HmacNoAttestation |
| 2428 | * |
| 2429 | * Verifies that for Hmac key generation, no attestation will be generated even if challenge |
| 2430 | * and app id are provided. |
| 2431 | */ |
| 2432 | TEST_P(NewKeyGenerationTest, HmacNoAttestation) { |
| 2433 | auto challenge = "hello"; |
| 2434 | auto app_id = "foo"; |
| 2435 | |
| 2436 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2437 | vector<uint8_t> key_blob; |
| 2438 | vector<KeyCharacteristics> key_characteristics; |
| 2439 | constexpr size_t key_size = 128; |
| 2440 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2441 | .HmacKey(key_size) |
| 2442 | .Digest(digest) |
| 2443 | .AttestationChallenge(challenge) |
| 2444 | .AttestationApplicationId(app_id) |
| 2445 | .Authorization(TAG_MIN_MAC_LENGTH, 128), |
| 2446 | &key_blob, &key_characteristics)); |
| 2447 | |
| 2448 | ASSERT_GT(key_blob.size(), 0U); |
| 2449 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2450 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2451 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2452 | |
| 2453 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2454 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2455 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2456 | << "Key size " << key_size << "missing"; |
| 2457 | |
| 2458 | CheckedDeleteKey(&key_blob); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2463 | * NewKeyGenerationTest.LimitedUsageHmac |
| 2464 | * |
| 2465 | * Verifies that KeyMint supports all required digests with limited usage Hmac, and that the |
| 2466 | * resulting keys have correct characteristics. |
| 2467 | */ |
| 2468 | TEST_P(NewKeyGenerationTest, LimitedUsageHmac) { |
| 2469 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2470 | vector<uint8_t> key_blob; |
| 2471 | vector<KeyCharacteristics> key_characteristics; |
| 2472 | constexpr size_t key_size = 128; |
| 2473 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2474 | .HmacKey(key_size) |
| 2475 | .Digest(digest) |
| 2476 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 2477 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1), |
| 2478 | &key_blob, &key_characteristics)); |
| 2479 | |
| 2480 | ASSERT_GT(key_blob.size(), 0U); |
| 2481 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2482 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2483 | |
| 2484 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2485 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2486 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2487 | << "Key size " << key_size << "missing"; |
| 2488 | |
| 2489 | // Check the usage count limit tag appears in the authorizations. |
| 2490 | AuthorizationSet auths; |
| 2491 | for (auto& entry : key_characteristics) { |
| 2492 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 2493 | } |
| 2494 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 2495 | << "key usage count limit " << 1U << " missing"; |
| 2496 | |
| 2497 | CheckedDeleteKey(&key_blob); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2502 | * NewKeyGenerationTest.HmacCheckKeySizes |
| 2503 | * |
| 2504 | * Verifies that keymint supports all key sizes, and rejects all invalid key sizes. |
| 2505 | */ |
| 2506 | TEST_P(NewKeyGenerationTest, HmacCheckKeySizes) { |
| 2507 | for (size_t key_size = 0; key_size <= 512; ++key_size) { |
| 2508 | if (key_size < 64 || key_size % 8 != 0) { |
| 2509 | // To keep this test from being very slow, we only test a random fraction of |
| 2510 | // non-byte key sizes. We test only ~10% of such cases. Since there are 392 of |
| 2511 | // them, we expect to run ~40 of them in each run. |
| 2512 | if (key_size % 8 == 0 || random() % 10 == 0) { |
| 2513 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2514 | GenerateKey(AuthorizationSetBuilder() |
| 2515 | .HmacKey(key_size) |
| 2516 | .Digest(Digest::SHA_2_256) |
| 2517 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2518 | << "HMAC key size " << key_size << " invalid"; |
| 2519 | } |
| 2520 | } else { |
| 2521 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2522 | .HmacKey(key_size) |
| 2523 | .Digest(Digest::SHA_2_256) |
| 2524 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2525 | << "Failed to generate HMAC key of size " << key_size; |
| 2526 | CheckedDeleteKey(); |
| 2527 | } |
| 2528 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2529 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2530 | // STRONGBOX devices must not support keys larger than 512 bits. |
| 2531 | size_t key_size = 520; |
| 2532 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2533 | GenerateKey(AuthorizationSetBuilder() |
| 2534 | .HmacKey(key_size) |
| 2535 | .Digest(Digest::SHA_2_256) |
| 2536 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2537 | << "HMAC key size " << key_size << " unexpectedly valid"; |
| 2538 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * NewKeyGenerationTest.HmacCheckMinMacLengths |
| 2543 | * |
| 2544 | * Verifies that keymint supports all required MAC lengths and rejects all invalid lengths. This |
| 2545 | * test is probabilistic in order to keep the runtime down, but any failure prints out the |
| 2546 | * specific MAC length that failed, so reproducing a failed run will be easy. |
| 2547 | */ |
| 2548 | TEST_P(NewKeyGenerationTest, HmacCheckMinMacLengths) { |
| 2549 | for (size_t min_mac_length = 0; min_mac_length <= 256; ++min_mac_length) { |
| 2550 | if (min_mac_length < 64 || min_mac_length % 8 != 0) { |
| 2551 | // To keep this test from being very long, we only test a random fraction of |
| 2552 | // non-byte lengths. We test only ~10% of such cases. Since there are 172 of them, |
| 2553 | // we expect to run ~17 of them in each run. |
| 2554 | if (min_mac_length % 8 == 0 || random() % 10 == 0) { |
| 2555 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2556 | GenerateKey(AuthorizationSetBuilder() |
| 2557 | .HmacKey(128) |
| 2558 | .Digest(Digest::SHA_2_256) |
| 2559 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2560 | << "HMAC min mac length " << min_mac_length << " invalid."; |
| 2561 | } |
| 2562 | } else { |
| 2563 | EXPECT_EQ(ErrorCode::OK, |
| 2564 | GenerateKey(AuthorizationSetBuilder() |
| 2565 | .HmacKey(128) |
| 2566 | .Digest(Digest::SHA_2_256) |
| 2567 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2568 | << "Failed to generate HMAC key with min MAC length " << min_mac_length; |
| 2569 | CheckedDeleteKey(); |
| 2570 | } |
| 2571 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2572 | |
| 2573 | // Minimum MAC length must be no more than 512 bits. |
| 2574 | size_t min_mac_length = 520; |
| 2575 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2576 | GenerateKey(AuthorizationSetBuilder() |
| 2577 | .HmacKey(128) |
| 2578 | .Digest(Digest::SHA_2_256) |
| 2579 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2580 | << "HMAC min mac length " << min_mac_length << " invalid."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | /* |
| 2584 | * NewKeyGenerationTest.HmacMultipleDigests |
| 2585 | * |
| 2586 | * Verifies that keymint rejects HMAC key generation with multiple specified digest algorithms. |
| 2587 | */ |
| 2588 | TEST_P(NewKeyGenerationTest, HmacMultipleDigests) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2589 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2590 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2591 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2592 | |
| 2593 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2594 | GenerateKey(AuthorizationSetBuilder() |
| 2595 | .HmacKey(128) |
| 2596 | .Digest(Digest::SHA1) |
| 2597 | .Digest(Digest::SHA_2_256) |
| 2598 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * NewKeyGenerationTest.HmacDigestNone |
| 2603 | * |
| 2604 | * Verifies that keymint rejects HMAC key generation with no digest or Digest::NONE |
| 2605 | */ |
| 2606 | TEST_P(NewKeyGenerationTest, HmacDigestNone) { |
| 2607 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2608 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Authorization(TAG_MIN_MAC_LENGTH, |
| 2609 | 128))); |
| 2610 | |
| 2611 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2612 | GenerateKey(AuthorizationSetBuilder() |
| 2613 | .HmacKey(128) |
| 2614 | .Digest(Digest::NONE) |
| 2615 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2616 | } |
| 2617 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2618 | /* |
| 2619 | * NewKeyGenerationTest.AesNoAttestation |
| 2620 | * |
| 2621 | * Verifies that attestation parameters to AES keys are ignored and generateKey |
| 2622 | * will succeed. |
| 2623 | */ |
| 2624 | TEST_P(NewKeyGenerationTest, AesNoAttestation) { |
| 2625 | auto challenge = "hello"; |
| 2626 | auto app_id = "foo"; |
| 2627 | |
| 2628 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2629 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2630 | .AesEncryptionKey(128) |
| 2631 | .EcbMode() |
| 2632 | .Padding(PaddingMode::PKCS7) |
| 2633 | .AttestationChallenge(challenge) |
| 2634 | .AttestationApplicationId(app_id))); |
| 2635 | |
| 2636 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2637 | } |
| 2638 | |
| 2639 | /* |
| 2640 | * NewKeyGenerationTest.TripleDesNoAttestation |
| 2641 | * |
| 2642 | * Verifies that attesting parameters to 3DES keys are ignored and generate key |
| 2643 | * will be successful. No attestation should be generated. |
| 2644 | */ |
| 2645 | TEST_P(NewKeyGenerationTest, TripleDesNoAttestation) { |
| 2646 | auto challenge = "hello"; |
| 2647 | auto app_id = "foo"; |
| 2648 | |
| 2649 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2650 | .TripleDesEncryptionKey(168) |
| 2651 | .BlockMode(BlockMode::ECB) |
| 2652 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2653 | .Padding(PaddingMode::NONE) |
| 2654 | .AttestationChallenge(challenge) |
| 2655 | .AttestationApplicationId(app_id))); |
| 2656 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2657 | } |
| 2658 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2659 | INSTANTIATE_KEYMINT_AIDL_TEST(NewKeyGenerationTest); |
| 2660 | |
| 2661 | typedef KeyMintAidlTestBase SigningOperationsTest; |
| 2662 | |
| 2663 | /* |
| 2664 | * SigningOperationsTest.RsaSuccess |
| 2665 | * |
| 2666 | * Verifies that raw RSA signature operations succeed. |
| 2667 | */ |
| 2668 | TEST_P(SigningOperationsTest, RsaSuccess) { |
| 2669 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2670 | .RsaSigningKey(2048, 65537) |
| 2671 | .Digest(Digest::NONE) |
| 2672 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2673 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2674 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2675 | string message = "12345678901234567890123456789012"; |
| 2676 | string signature = SignMessage( |
| 2677 | message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 2678 | LocalVerifyMessage(message, signature, |
| 2679 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2680 | } |
| 2681 | |
| 2682 | /* |
| 2683 | * SigningOperationsTest.RsaAllPaddingsAndDigests |
| 2684 | * |
| 2685 | * Verifies RSA signature/verification for all padding modes and digests. |
| 2686 | */ |
| 2687 | TEST_P(SigningOperationsTest, RsaAllPaddingsAndDigests) { |
| 2688 | auto authorizations = AuthorizationSetBuilder() |
| 2689 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2690 | .RsaSigningKey(2048, 65537) |
| 2691 | .Digest(ValidDigests(true /* withNone */, true /* withMD5 */)) |
| 2692 | .Padding(PaddingMode::NONE) |
| 2693 | .Padding(PaddingMode::RSA_PSS) |
| 2694 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2695 | .SetDefaultValidity(); |
| 2696 | |
| 2697 | ASSERT_EQ(ErrorCode::OK, GenerateKey(authorizations)); |
| 2698 | |
| 2699 | string message(128, 'a'); |
| 2700 | string corrupt_message(message); |
| 2701 | ++corrupt_message[corrupt_message.size() / 2]; |
| 2702 | |
| 2703 | for (auto padding : |
| 2704 | {PaddingMode::NONE, PaddingMode::RSA_PSS, PaddingMode::RSA_PKCS1_1_5_SIGN}) { |
| 2705 | for (auto digest : ValidDigests(true /* withNone */, true /* withMD5 */)) { |
| 2706 | if (padding == PaddingMode::NONE && digest != Digest::NONE) { |
| 2707 | // Digesting only makes sense with padding. |
| 2708 | continue; |
| 2709 | } |
| 2710 | |
| 2711 | if (padding == PaddingMode::RSA_PSS && digest == Digest::NONE) { |
| 2712 | // PSS requires digesting. |
| 2713 | continue; |
| 2714 | } |
| 2715 | |
| 2716 | string signature = |
| 2717 | SignMessage(message, AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2718 | LocalVerifyMessage(message, signature, |
| 2719 | AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2720 | } |
| 2721 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * SigningOperationsTest.RsaUseRequiresCorrectAppIdAppData |
| 2726 | * |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2727 | * Verifies that using an RSA key requires the correct app data. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2728 | */ |
| 2729 | TEST_P(SigningOperationsTest, RsaUseRequiresCorrectAppIdAppData) { |
| 2730 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2731 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2732 | .RsaSigningKey(2048, 65537) |
| 2733 | .Digest(Digest::NONE) |
| 2734 | .Padding(PaddingMode::NONE) |
| 2735 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2736 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2737 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2738 | |
| 2739 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 2740 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2741 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2742 | Begin(KeyPurpose::SIGN, |
| 2743 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2744 | AbortIfNeeded(); |
| 2745 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2746 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2747 | .Digest(Digest::NONE) |
| 2748 | .Padding(PaddingMode::NONE) |
| 2749 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2750 | AbortIfNeeded(); |
| 2751 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2752 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2753 | .Digest(Digest::NONE) |
| 2754 | .Padding(PaddingMode::NONE) |
| 2755 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 2756 | AbortIfNeeded(); |
| 2757 | EXPECT_EQ(ErrorCode::OK, |
| 2758 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2759 | .Digest(Digest::NONE) |
| 2760 | .Padding(PaddingMode::NONE) |
| 2761 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2762 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2763 | AbortIfNeeded(); |
| 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * SigningOperationsTest.RsaPssSha256Success |
| 2768 | * |
| 2769 | * Verifies that RSA-PSS signature operations succeed. |
| 2770 | */ |
| 2771 | TEST_P(SigningOperationsTest, RsaPssSha256Success) { |
| 2772 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2773 | .RsaSigningKey(2048, 65537) |
| 2774 | .Digest(Digest::SHA_2_256) |
| 2775 | .Padding(PaddingMode::RSA_PSS) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2776 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2777 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2778 | // Use large message, which won't work without digesting. |
| 2779 | string message(1024, 'a'); |
| 2780 | string signature = SignMessage( |
| 2781 | message, |
| 2782 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS)); |
| 2783 | } |
| 2784 | |
| 2785 | /* |
| 2786 | * SigningOperationsTest.RsaPaddingNoneDoesNotAllowOther |
| 2787 | * |
| 2788 | * Verifies that keymint rejects signature operations that specify a padding mode when the key |
| 2789 | * supports only unpadded operations. |
| 2790 | */ |
| 2791 | TEST_P(SigningOperationsTest, RsaPaddingNoneDoesNotAllowOther) { |
| 2792 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2793 | .RsaSigningKey(2048, 65537) |
| 2794 | .Digest(Digest::NONE) |
| 2795 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2796 | .Padding(PaddingMode::NONE) |
| 2797 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2798 | string message = "12345678901234567890123456789012"; |
| 2799 | string signature; |
| 2800 | |
| 2801 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 2802 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2803 | .Digest(Digest::NONE) |
| 2804 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2805 | } |
| 2806 | |
| 2807 | /* |
| 2808 | * SigningOperationsTest.NoUserConfirmation |
| 2809 | * |
| 2810 | * Verifies that keymint rejects signing operations for keys with |
| 2811 | * TRUSTED_CONFIRMATION_REQUIRED and no valid confirmation token |
| 2812 | * presented. |
| 2813 | */ |
| 2814 | TEST_P(SigningOperationsTest, NoUserConfirmation) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2815 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2816 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2817 | } |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2818 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2819 | .RsaSigningKey(1024, 65537) |
| 2820 | .Digest(Digest::NONE) |
| 2821 | .Padding(PaddingMode::NONE) |
| 2822 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2823 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 2824 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2825 | |
| 2826 | const string message = "12345678901234567890123456789012"; |
| 2827 | EXPECT_EQ(ErrorCode::OK, |
| 2828 | Begin(KeyPurpose::SIGN, |
| 2829 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2830 | string signature; |
| 2831 | EXPECT_EQ(ErrorCode::NO_USER_CONFIRMATION, Finish(message, &signature)); |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * SigningOperationsTest.RsaPkcs1Sha256Success |
| 2836 | * |
| 2837 | * Verifies that digested RSA-PKCS1 signature operations succeed. |
| 2838 | */ |
| 2839 | TEST_P(SigningOperationsTest, RsaPkcs1Sha256Success) { |
| 2840 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2841 | .RsaSigningKey(2048, 65537) |
| 2842 | .Digest(Digest::SHA_2_256) |
| 2843 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2844 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2845 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2846 | string message(1024, 'a'); |
| 2847 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2848 | .Digest(Digest::SHA_2_256) |
| 2849 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2850 | } |
| 2851 | |
| 2852 | /* |
| 2853 | * SigningOperationsTest.RsaPkcs1NoDigestSuccess |
| 2854 | * |
| 2855 | * Verifies that undigested RSA-PKCS1 signature operations succeed. |
| 2856 | */ |
| 2857 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestSuccess) { |
| 2858 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2859 | .RsaSigningKey(2048, 65537) |
| 2860 | .Digest(Digest::NONE) |
| 2861 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2862 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2863 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2864 | string message(53, 'a'); |
| 2865 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2866 | .Digest(Digest::NONE) |
| 2867 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2868 | } |
| 2869 | |
| 2870 | /* |
| 2871 | * SigningOperationsTest.RsaPkcs1NoDigestTooLarge |
| 2872 | * |
| 2873 | * Verifies that undigested RSA-PKCS1 signature operations fail with the correct error code when |
| 2874 | * given a too-long message. |
| 2875 | */ |
| 2876 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestTooLong) { |
| 2877 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2878 | .RsaSigningKey(2048, 65537) |
| 2879 | .Digest(Digest::NONE) |
| 2880 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2881 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2882 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2883 | string message(257, 'a'); |
| 2884 | |
| 2885 | EXPECT_EQ(ErrorCode::OK, |
| 2886 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2887 | .Digest(Digest::NONE) |
| 2888 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2889 | string signature; |
| 2890 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &signature)); |
| 2891 | } |
| 2892 | |
| 2893 | /* |
| 2894 | * SigningOperationsTest.RsaPssSha512TooSmallKey |
| 2895 | * |
| 2896 | * Verifies that undigested RSA-PSS signature operations fail with the correct error code when |
| 2897 | * used with a key that is too small for the message. |
| 2898 | * |
| 2899 | * A PSS-padded message is of length salt_size + digest_size + 16 (sizes in bits), and the |
| 2900 | * keymint specification requires that salt_size == digest_size, so the message will be |
| 2901 | * digest_size * 2 + |
| 2902 | * 16. Such a message can only be signed by a given key if the key is at least that size. This |
| 2903 | * test uses SHA512, which has a digest_size == 512, so the message size is 1040 bits, too large |
| 2904 | * for a 1024-bit key. |
| 2905 | */ |
| 2906 | TEST_P(SigningOperationsTest, RsaPssSha512TooSmallKey) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2907 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2908 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2909 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2910 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2911 | .RsaSigningKey(1024, 65537) |
| 2912 | .Digest(Digest::SHA_2_512) |
| 2913 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2914 | .Padding(PaddingMode::RSA_PSS) |
| 2915 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2916 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2917 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2918 | .Digest(Digest::SHA_2_512) |
| 2919 | .Padding(PaddingMode::RSA_PSS))); |
| 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | * SigningOperationsTest.RsaNoPaddingTooLong |
| 2924 | * |
| 2925 | * Verifies that raw RSA signature operations fail with the correct error code when |
| 2926 | * given a too-long message. |
| 2927 | */ |
| 2928 | TEST_P(SigningOperationsTest, RsaNoPaddingTooLong) { |
| 2929 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2930 | .RsaSigningKey(2048, 65537) |
| 2931 | .Digest(Digest::NONE) |
| 2932 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2933 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2934 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2935 | // One byte too long |
| 2936 | string message(2048 / 8 + 1, 'a'); |
| 2937 | ASSERT_EQ(ErrorCode::OK, |
| 2938 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2939 | .Digest(Digest::NONE) |
| 2940 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2941 | string result; |
| 2942 | ErrorCode finish_error_code = Finish(message, &result); |
| 2943 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2944 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2945 | |
| 2946 | // Very large message that should exceed the transfer buffer size of any reasonable TEE. |
| 2947 | message = string(128 * 1024, 'a'); |
| 2948 | ASSERT_EQ(ErrorCode::OK, |
| 2949 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2950 | .Digest(Digest::NONE) |
| 2951 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2952 | finish_error_code = Finish(message, &result); |
| 2953 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2954 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2955 | } |
| 2956 | |
| 2957 | /* |
| 2958 | * SigningOperationsTest.RsaAbort |
| 2959 | * |
| 2960 | * Verifies that operations can be aborted correctly. Uses an RSA signing operation for the |
| 2961 | * test, but the behavior should be algorithm and purpose-independent. |
| 2962 | */ |
| 2963 | TEST_P(SigningOperationsTest, RsaAbort) { |
| 2964 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2965 | .RsaSigningKey(2048, 65537) |
| 2966 | .Digest(Digest::NONE) |
| 2967 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2968 | .Padding(PaddingMode::NONE) |
| 2969 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2970 | |
| 2971 | ASSERT_EQ(ErrorCode::OK, |
| 2972 | Begin(KeyPurpose::SIGN, |
| 2973 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2974 | EXPECT_EQ(ErrorCode::OK, Abort()); |
| 2975 | |
| 2976 | // Another abort should fail |
| 2977 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 2978 | |
| 2979 | // Set to sentinel, so TearDown() doesn't try to abort again. |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 2980 | op_.reset(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2984 | * SigningOperationsTest.RsaNonUniqueParams |
| 2985 | * |
| 2986 | * Verifies that an operation with multiple padding modes is rejected. |
| 2987 | */ |
| 2988 | TEST_P(SigningOperationsTest, RsaNonUniqueParams) { |
| 2989 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2990 | .RsaSigningKey(2048, 65537) |
| 2991 | .Digest(Digest::NONE) |
| 2992 | .Digest(Digest::SHA1) |
| 2993 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2994 | .Padding(PaddingMode::NONE) |
| 2995 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2996 | .SetDefaultValidity())); |
| 2997 | |
| 2998 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2999 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3000 | .Digest(Digest::NONE) |
| 3001 | .Padding(PaddingMode::NONE) |
| 3002 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 3003 | |
Tommy Chiu | c93c439 | 2021-05-11 18:36:50 +0800 | [diff] [blame] | 3004 | auto result = Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3005 | .Digest(Digest::NONE) |
| 3006 | .Digest(Digest::SHA1) |
| 3007 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 3008 | ASSERT_TRUE(result == ErrorCode::UNSUPPORTED_DIGEST || result == ErrorCode::INVALID_ARGUMENT); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3009 | |
| 3010 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 3011 | Begin(KeyPurpose::SIGN, |
| 3012 | AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 3013 | } |
| 3014 | |
| 3015 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3016 | * SigningOperationsTest.RsaUnsupportedPadding |
| 3017 | * |
| 3018 | * Verifies that RSA operations fail with the correct error (but key gen succeeds) when used |
| 3019 | * with a padding mode inappropriate for RSA. |
| 3020 | */ |
| 3021 | TEST_P(SigningOperationsTest, RsaUnsupportedPadding) { |
| 3022 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3023 | .RsaSigningKey(2048, 65537) |
| 3024 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3025 | .Digest(Digest::SHA_2_256 /* supported digest */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3026 | .Padding(PaddingMode::PKCS7) |
| 3027 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3028 | ASSERT_EQ( |
| 3029 | ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 3030 | Begin(KeyPurpose::SIGN, |
| 3031 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::PKCS7))); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3032 | CheckedDeleteKey(); |
| 3033 | |
| 3034 | ASSERT_EQ(ErrorCode::OK, |
| 3035 | GenerateKey( |
| 3036 | AuthorizationSetBuilder() |
| 3037 | .RsaSigningKey(2048, 65537) |
| 3038 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3039 | .Digest(Digest::SHA_2_256 /* supported digest */) |
| 3040 | .Padding(PaddingMode::RSA_OAEP) /* padding mode for encryption only */ |
| 3041 | .SetDefaultValidity())); |
| 3042 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 3043 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3044 | .Digest(Digest::SHA_2_256) |
| 3045 | .Padding(PaddingMode::RSA_OAEP))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | /* |
| 3049 | * SigningOperationsTest.RsaPssNoDigest |
| 3050 | * |
| 3051 | * Verifies that RSA PSS operations fail when no digest is used. PSS requires a digest. |
| 3052 | */ |
| 3053 | TEST_P(SigningOperationsTest, RsaNoDigest) { |
| 3054 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3055 | .RsaSigningKey(2048, 65537) |
| 3056 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3057 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3058 | .Padding(PaddingMode::RSA_PSS) |
| 3059 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3060 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 3061 | Begin(KeyPurpose::SIGN, |
| 3062 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::RSA_PSS))); |
| 3063 | |
| 3064 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 3065 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS))); |
| 3066 | } |
| 3067 | |
| 3068 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3069 | * SigningOperationsTest.RsaPssNoPadding |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3070 | * |
| 3071 | * Verifies that RSA operations fail when no padding mode is specified. PaddingMode::NONE is |
| 3072 | * supported in some cases (as validated in other tests), but a mode must be specified. |
| 3073 | */ |
| 3074 | TEST_P(SigningOperationsTest, RsaNoPadding) { |
| 3075 | // Padding must be specified |
| 3076 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3077 | .RsaKey(2048, 65537) |
| 3078 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3079 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3080 | .Digest(Digest::NONE) |
| 3081 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3082 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 3083 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 3084 | } |
| 3085 | |
| 3086 | /* |
| 3087 | * SigningOperationsTest.RsaShortMessage |
| 3088 | * |
| 3089 | * Verifies that raw RSA signatures succeed with a message shorter than the key size. |
| 3090 | */ |
| 3091 | TEST_P(SigningOperationsTest, RsaTooShortMessage) { |
| 3092 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3093 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3094 | .RsaSigningKey(2048, 65537) |
| 3095 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3096 | .Padding(PaddingMode::NONE) |
| 3097 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3098 | |
| 3099 | // Barely shorter |
| 3100 | string message(2048 / 8 - 1, 'a'); |
| 3101 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 3102 | |
| 3103 | // Much shorter |
| 3104 | message = "a"; |
| 3105 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * SigningOperationsTest.RsaSignWithEncryptionKey |
| 3110 | * |
| 3111 | * Verifies that RSA encryption keys cannot be used to sign. |
| 3112 | */ |
| 3113 | TEST_P(SigningOperationsTest, RsaSignWithEncryptionKey) { |
| 3114 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3115 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3116 | .RsaEncryptionKey(2048, 65537) |
| 3117 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3118 | .Padding(PaddingMode::NONE) |
| 3119 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3120 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3121 | Begin(KeyPurpose::SIGN, |
| 3122 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * SigningOperationsTest.RsaSignTooLargeMessage |
| 3127 | * |
| 3128 | * Verifies that attempting a raw signature of a message which is the same length as the key, |
| 3129 | * but numerically larger than the public modulus, fails with the correct error. |
| 3130 | */ |
| 3131 | TEST_P(SigningOperationsTest, RsaSignTooLargeMessage) { |
| 3132 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3133 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3134 | .RsaSigningKey(2048, 65537) |
| 3135 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3136 | .Padding(PaddingMode::NONE) |
| 3137 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3138 | |
| 3139 | // Largest possible message will always be larger than the public modulus. |
| 3140 | string message(2048 / 8, static_cast<char>(0xff)); |
| 3141 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3142 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3143 | .Digest(Digest::NONE) |
| 3144 | .Padding(PaddingMode::NONE))); |
| 3145 | string signature; |
| 3146 | ASSERT_EQ(ErrorCode::INVALID_ARGUMENT, Finish(message, &signature)); |
| 3147 | } |
| 3148 | |
| 3149 | /* |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3150 | * SigningOperationsTest.EcdsaAllDigestsAndCurves |
| 3151 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3152 | * Verifies ECDSA signature/verification for all digests and required curves. |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3153 | */ |
| 3154 | TEST_P(SigningOperationsTest, EcdsaAllDigestsAndCurves) { |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3155 | |
| 3156 | string message = "1234567890"; |
| 3157 | string corrupt_message = "2234567890"; |
| 3158 | for (auto curve : ValidCurves()) { |
| 3159 | SCOPED_TRACE(testing::Message() << "Curve::" << curve); |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3160 | // Ed25519 only allows Digest::NONE. |
| 3161 | auto digests = (curve == EcCurve::CURVE_25519) |
| 3162 | ? std::vector<Digest>(1, Digest::NONE) |
| 3163 | : ValidDigests(true /* withNone */, false /* withMD5 */); |
| 3164 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3165 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3166 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3167 | .EcdsaSigningKey(curve) |
| 3168 | .Digest(digests) |
| 3169 | .SetDefaultValidity()); |
| 3170 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate key for EC curve " << curve; |
| 3171 | if (error != ErrorCode::OK) { |
| 3172 | continue; |
| 3173 | } |
| 3174 | |
| 3175 | for (auto digest : digests) { |
| 3176 | SCOPED_TRACE(testing::Message() << "Digest::" << digest); |
| 3177 | string signature = SignMessage(message, AuthorizationSetBuilder().Digest(digest)); |
| 3178 | LocalVerifyMessage(message, signature, AuthorizationSetBuilder().Digest(digest)); |
| 3179 | } |
| 3180 | |
| 3181 | auto rc = DeleteKey(); |
| 3182 | ASSERT_TRUE(rc == ErrorCode::OK || rc == ErrorCode::UNIMPLEMENTED); |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3187 | * SigningOperationsTest.EcdsaAllCurves |
| 3188 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3189 | * Verifies that ECDSA operations succeed with all required curves. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3190 | */ |
| 3191 | TEST_P(SigningOperationsTest, EcdsaAllCurves) { |
| 3192 | for (auto curve : ValidCurves()) { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3193 | Digest digest = (curve == EcCurve::CURVE_25519 ? Digest::NONE : Digest::SHA_2_256); |
| 3194 | SCOPED_TRACE(testing::Message() << "Curve::" << curve); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3195 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3196 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3197 | .EcdsaSigningKey(curve) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3198 | .Digest(digest) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3199 | .SetDefaultValidity()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3200 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3201 | if (error != ErrorCode::OK) continue; |
| 3202 | |
| 3203 | string message(1024, 'a'); |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3204 | SignMessage(message, AuthorizationSetBuilder().Digest(digest)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3205 | CheckedDeleteKey(); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3210 | * SigningOperationsTest.EcdsaCurve25519 |
| 3211 | * |
| 3212 | * Verifies that ECDSA operations succeed with curve25519. |
| 3213 | */ |
| 3214 | TEST_P(SigningOperationsTest, EcdsaCurve25519) { |
| 3215 | if (!Curve25519Supported()) { |
| 3216 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3217 | } |
| 3218 | |
| 3219 | EcCurve curve = EcCurve::CURVE_25519; |
| 3220 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3221 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3222 | .EcdsaSigningKey(curve) |
| 3223 | .Digest(Digest::NONE) |
| 3224 | .SetDefaultValidity()); |
| 3225 | ASSERT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3226 | |
| 3227 | string message(1024, 'a'); |
| 3228 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE)); |
| 3229 | CheckedDeleteKey(); |
| 3230 | } |
| 3231 | |
| 3232 | /* |
David Drysdale | feab5d9 | 2022-01-06 15:46:23 +0000 | [diff] [blame] | 3233 | * SigningOperationsTest.EcdsaCurve25519MaxSize |
| 3234 | * |
| 3235 | * Verifies that EDDSA operations with curve25519 under the maximum message size succeed. |
| 3236 | */ |
| 3237 | TEST_P(SigningOperationsTest, EcdsaCurve25519MaxSize) { |
| 3238 | if (!Curve25519Supported()) { |
| 3239 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3240 | } |
| 3241 | |
| 3242 | EcCurve curve = EcCurve::CURVE_25519; |
| 3243 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3244 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3245 | .EcdsaSigningKey(curve) |
| 3246 | .Digest(Digest::NONE) |
| 3247 | .SetDefaultValidity()); |
| 3248 | ASSERT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3249 | |
| 3250 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 3251 | |
| 3252 | for (size_t msg_size : {MAX_ED25519_MSG_SIZE - 1, MAX_ED25519_MSG_SIZE}) { |
| 3253 | SCOPED_TRACE(testing::Message() << "-msg-size=" << msg_size); |
| 3254 | string message(msg_size, 'a'); |
| 3255 | |
| 3256 | // Attempt to sign via Begin+Finish. |
| 3257 | AuthorizationSet out_params; |
| 3258 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, key_blob_, params, &out_params)); |
| 3259 | EXPECT_TRUE(out_params.empty()); |
| 3260 | string signature; |
| 3261 | auto result = Finish(message, &signature); |
| 3262 | EXPECT_EQ(result, ErrorCode::OK); |
| 3263 | LocalVerifyMessage(message, signature, params); |
| 3264 | |
| 3265 | // Attempt to sign via Begin+Update+Finish |
| 3266 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, key_blob_, params, &out_params)); |
| 3267 | EXPECT_TRUE(out_params.empty()); |
| 3268 | string output; |
| 3269 | result = Update(message, &output); |
| 3270 | EXPECT_EQ(result, ErrorCode::OK); |
| 3271 | EXPECT_EQ(output.size(), 0); |
| 3272 | string signature2; |
| 3273 | EXPECT_EQ(ErrorCode::OK, Finish({}, &signature2)); |
| 3274 | LocalVerifyMessage(message, signature2, params); |
| 3275 | } |
| 3276 | |
| 3277 | CheckedDeleteKey(); |
| 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * SigningOperationsTest.EcdsaCurve25519MaxSizeFail |
| 3282 | * |
| 3283 | * Verifies that EDDSA operations with curve25519 fail when message size is too large. |
| 3284 | */ |
| 3285 | TEST_P(SigningOperationsTest, EcdsaCurve25519MaxSizeFail) { |
| 3286 | if (!Curve25519Supported()) { |
| 3287 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3288 | } |
| 3289 | |
| 3290 | EcCurve curve = EcCurve::CURVE_25519; |
| 3291 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3292 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3293 | .EcdsaSigningKey(curve) |
| 3294 | .Digest(Digest::NONE) |
| 3295 | .SetDefaultValidity()); |
| 3296 | ASSERT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3297 | |
| 3298 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 3299 | |
| 3300 | for (size_t msg_size : {MAX_ED25519_MSG_SIZE + 1, MAX_ED25519_MSG_SIZE * 2}) { |
| 3301 | SCOPED_TRACE(testing::Message() << "-msg-size=" << msg_size); |
| 3302 | string message(msg_size, 'a'); |
| 3303 | |
| 3304 | // Attempt to sign via Begin+Finish. |
| 3305 | AuthorizationSet out_params; |
| 3306 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, key_blob_, params, &out_params)); |
| 3307 | EXPECT_TRUE(out_params.empty()); |
| 3308 | string signature; |
| 3309 | auto result = Finish(message, &signature); |
| 3310 | EXPECT_EQ(result, ErrorCode::INVALID_INPUT_LENGTH); |
| 3311 | |
| 3312 | // Attempt to sign via Begin+Update (but never get to Finish) |
| 3313 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, key_blob_, params, &out_params)); |
| 3314 | EXPECT_TRUE(out_params.empty()); |
| 3315 | string output; |
| 3316 | result = Update(message, &output); |
| 3317 | EXPECT_EQ(result, ErrorCode::INVALID_INPUT_LENGTH); |
| 3318 | } |
| 3319 | |
| 3320 | CheckedDeleteKey(); |
| 3321 | } |
| 3322 | |
| 3323 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3324 | * SigningOperationsTest.EcdsaNoDigestHugeData |
| 3325 | * |
| 3326 | * Verifies that ECDSA operations support very large messages, even without digesting. This |
| 3327 | * should work because ECDSA actually only signs the leftmost L_n bits of the message, however |
| 3328 | * large it may be. Not using digesting is a bad idea, but in some cases digesting is done by |
| 3329 | * the framework. |
| 3330 | */ |
| 3331 | TEST_P(SigningOperationsTest, EcdsaNoDigestHugeData) { |
| 3332 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3333 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3334 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3335 | .Digest(Digest::NONE) |
| 3336 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3337 | string message(1 * 1024, 'a'); |
| 3338 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE)); |
| 3339 | } |
| 3340 | |
| 3341 | /* |
| 3342 | * SigningOperationsTest.EcUseRequiresCorrectAppIdAppData |
| 3343 | * |
| 3344 | * Verifies that using an EC key requires the correct app ID/data. |
| 3345 | */ |
| 3346 | TEST_P(SigningOperationsTest, EcUseRequiresCorrectAppIdAppData) { |
| 3347 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3348 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3349 | .EcdsaSigningKey(EcCurve::P_256) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3350 | .Digest(Digest::NONE) |
| 3351 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3352 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 3353 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 3354 | |
| 3355 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 3356 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3357 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3358 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 3359 | AbortIfNeeded(); |
| 3360 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3361 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3362 | .Digest(Digest::NONE) |
| 3363 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 3364 | AbortIfNeeded(); |
| 3365 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3366 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3367 | .Digest(Digest::NONE) |
| 3368 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 3369 | AbortIfNeeded(); |
| 3370 | EXPECT_EQ(ErrorCode::OK, |
| 3371 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3372 | .Digest(Digest::NONE) |
| 3373 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 3374 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 3375 | AbortIfNeeded(); |
| 3376 | } |
| 3377 | |
| 3378 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3379 | * SigningOperationsTest.EcdsaIncompatibleDigest |
| 3380 | * |
| 3381 | * Verifies that using an EC key requires compatible digest. |
| 3382 | */ |
| 3383 | TEST_P(SigningOperationsTest, EcdsaIncompatibleDigest) { |
| 3384 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3385 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3386 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3387 | .Digest(Digest::NONE) |
| 3388 | .Digest(Digest::SHA1) |
| 3389 | .SetDefaultValidity())); |
| 3390 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 3391 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::SHA_2_256))); |
| 3392 | AbortIfNeeded(); |
| 3393 | } |
| 3394 | |
| 3395 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3396 | * SigningOperationsTest.AesEcbSign |
| 3397 | * |
| 3398 | * Verifies that attempts to use AES keys to sign fail in the correct way. |
| 3399 | */ |
| 3400 | TEST_P(SigningOperationsTest, AesEcbSign) { |
| 3401 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3402 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3403 | .SigningKey() |
| 3404 | .AesEncryptionKey(128) |
| 3405 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB))); |
| 3406 | |
| 3407 | AuthorizationSet out_params; |
| 3408 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 3409 | Begin(KeyPurpose::SIGN, AuthorizationSet() /* in_params */, &out_params)); |
| 3410 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 3411 | Begin(KeyPurpose::VERIFY, AuthorizationSet() /* in_params */, &out_params)); |
| 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * SigningOperationsTest.HmacAllDigests |
| 3416 | * |
| 3417 | * Verifies that HMAC works with all digests. |
| 3418 | */ |
| 3419 | TEST_P(SigningOperationsTest, HmacAllDigests) { |
| 3420 | for (auto digest : ValidDigests(false /* withNone */, false /* withMD5 */)) { |
| 3421 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3422 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3423 | .HmacKey(128) |
| 3424 | .Digest(digest) |
| 3425 | .Authorization(TAG_MIN_MAC_LENGTH, 160))) |
| 3426 | << "Failed to create HMAC key with digest " << digest; |
| 3427 | string message = "12345678901234567890123456789012"; |
| 3428 | string signature = MacMessage(message, digest, 160); |
| 3429 | EXPECT_EQ(160U / 8U, signature.size()) |
| 3430 | << "Failed to sign with HMAC key with digest " << digest; |
| 3431 | CheckedDeleteKey(); |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | /* |
| 3436 | * SigningOperationsTest.HmacSha256TooLargeMacLength |
| 3437 | * |
| 3438 | * Verifies that HMAC fails in the correct way when asked to generate a MAC larger than the |
| 3439 | * digest size. |
| 3440 | */ |
| 3441 | TEST_P(SigningOperationsTest, HmacSha256TooLargeMacLength) { |
| 3442 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3443 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3444 | .HmacKey(128) |
| 3445 | .Digest(Digest::SHA_2_256) |
| 3446 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 3447 | AuthorizationSet output_params; |
| 3448 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3449 | AuthorizationSetBuilder() |
| 3450 | .Digest(Digest::SHA_2_256) |
| 3451 | .Authorization(TAG_MAC_LENGTH, 264), |
| 3452 | &output_params)); |
| 3453 | } |
| 3454 | |
| 3455 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3456 | * SigningOperationsTest.HmacSha256InvalidMacLength |
| 3457 | * |
| 3458 | * Verifies that HMAC fails in the correct way when asked to generate a MAC whose length is |
| 3459 | * not a multiple of 8. |
| 3460 | */ |
| 3461 | TEST_P(SigningOperationsTest, HmacSha256InvalidMacLength) { |
| 3462 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3463 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3464 | .HmacKey(128) |
| 3465 | .Digest(Digest::SHA_2_256) |
| 3466 | .Authorization(TAG_MIN_MAC_LENGTH, 160))); |
| 3467 | AuthorizationSet output_params; |
| 3468 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3469 | AuthorizationSetBuilder() |
| 3470 | .Digest(Digest::SHA_2_256) |
| 3471 | .Authorization(TAG_MAC_LENGTH, 161), |
| 3472 | &output_params)); |
| 3473 | } |
| 3474 | |
| 3475 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3476 | * SigningOperationsTest.HmacSha256TooSmallMacLength |
| 3477 | * |
| 3478 | * Verifies that HMAC fails in the correct way when asked to generate a MAC smaller than the |
| 3479 | * specified minimum MAC length. |
| 3480 | */ |
| 3481 | TEST_P(SigningOperationsTest, HmacSha256TooSmallMacLength) { |
| 3482 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3483 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3484 | .HmacKey(128) |
| 3485 | .Digest(Digest::SHA_2_256) |
| 3486 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 3487 | AuthorizationSet output_params; |
| 3488 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3489 | AuthorizationSetBuilder() |
| 3490 | .Digest(Digest::SHA_2_256) |
| 3491 | .Authorization(TAG_MAC_LENGTH, 120), |
| 3492 | &output_params)); |
| 3493 | } |
| 3494 | |
| 3495 | /* |
| 3496 | * SigningOperationsTest.HmacRfc4231TestCase3 |
| 3497 | * |
| 3498 | * Validates against the test vectors from RFC 4231 test case 3. |
| 3499 | */ |
| 3500 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase3) { |
| 3501 | string key(20, 0xaa); |
| 3502 | string message(50, 0xdd); |
| 3503 | uint8_t sha_224_expected[] = { |
| 3504 | 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9, 0x69, 0x4d, 0x7d, 0x6a, |
| 3505 | 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1, 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea, |
| 3506 | }; |
| 3507 | uint8_t sha_256_expected[] = { |
| 3508 | 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, |
| 3509 | 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, |
| 3510 | 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe, |
| 3511 | }; |
| 3512 | uint8_t sha_384_expected[] = { |
| 3513 | 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2, 0xac, 0xe0, |
| 3514 | 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9, 0x47, 0xac, 0x9f, 0xeb, |
| 3515 | 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, |
| 3516 | 0xc1, 0x38, 0x14, 0xb9, 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27, |
| 3517 | }; |
| 3518 | uint8_t sha_512_expected[] = { |
| 3519 | 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0, 0xf0, 0x75, 0x6c, |
| 3520 | 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd, 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, |
| 3521 | 0x3e, 0x33, 0xb2, 0x27, 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, |
| 3522 | 0xc8, 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46, 0xa3, 0x37, |
| 3523 | 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59, 0xe1, 0x32, 0x92, 0xfb, |
| 3524 | }; |
| 3525 | |
| 3526 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 3527 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 3528 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 3529 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 3530 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | /* |
| 3535 | * SigningOperationsTest.HmacRfc4231TestCase5 |
| 3536 | * |
| 3537 | * Validates against the test vectors from RFC 4231 test case 5. |
| 3538 | */ |
| 3539 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase5) { |
| 3540 | string key(20, 0x0c); |
| 3541 | string message = "Test With Truncation"; |
| 3542 | |
| 3543 | uint8_t sha_224_expected[] = { |
| 3544 | 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, |
| 3545 | 0xc9, 0x88, 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8, |
| 3546 | }; |
| 3547 | uint8_t sha_256_expected[] = { |
| 3548 | 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, |
| 3549 | 0x6e, 0x0c, 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b, |
| 3550 | }; |
| 3551 | uint8_t sha_384_expected[] = { |
| 3552 | 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, |
| 3553 | 0xa4, 0x6e, 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97, |
| 3554 | }; |
| 3555 | uint8_t sha_512_expected[] = { |
| 3556 | 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, |
| 3557 | 0x1d, 0x41, 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6, |
| 3558 | }; |
| 3559 | |
| 3560 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 3561 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 3562 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 3563 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 3564 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | INSTANTIATE_KEYMINT_AIDL_TEST(SigningOperationsTest); |
| 3569 | |
| 3570 | typedef KeyMintAidlTestBase VerificationOperationsTest; |
| 3571 | |
| 3572 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3573 | * VerificationOperationsTest.HmacSigningKeyCannotVerify |
| 3574 | * |
| 3575 | * Verifies HMAC signing and verification, but that a signing key cannot be used to verify. |
| 3576 | */ |
| 3577 | TEST_P(VerificationOperationsTest, HmacSigningKeyCannotVerify) { |
| 3578 | string key_material = "HelloThisIsAKey"; |
| 3579 | |
| 3580 | vector<uint8_t> signing_key, verification_key; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3581 | vector<KeyCharacteristics> signing_key_chars, verification_key_chars; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3582 | EXPECT_EQ(ErrorCode::OK, |
| 3583 | ImportKey(AuthorizationSetBuilder() |
| 3584 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3585 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3586 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 3587 | .Digest(Digest::SHA_2_256) |
| 3588 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3589 | KeyFormat::RAW, key_material, &signing_key, &signing_key_chars)); |
| 3590 | EXPECT_EQ(ErrorCode::OK, |
| 3591 | ImportKey(AuthorizationSetBuilder() |
| 3592 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3593 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3594 | .Authorization(TAG_PURPOSE, KeyPurpose::VERIFY) |
| 3595 | .Digest(Digest::SHA_2_256) |
| 3596 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3597 | KeyFormat::RAW, key_material, &verification_key, &verification_key_chars)); |
| 3598 | |
| 3599 | string message = "This is a message."; |
| 3600 | string signature = SignMessage( |
| 3601 | signing_key, message, |
| 3602 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Authorization(TAG_MAC_LENGTH, 160)); |
| 3603 | |
| 3604 | // Signing key should not work. |
| 3605 | AuthorizationSet out_params; |
| 3606 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3607 | Begin(KeyPurpose::VERIFY, signing_key, |
| 3608 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &out_params)); |
| 3609 | |
| 3610 | // Verification key should work. |
| 3611 | VerifyMessage(verification_key, message, signature, |
| 3612 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 3613 | |
| 3614 | CheckedDeleteKey(&signing_key); |
| 3615 | CheckedDeleteKey(&verification_key); |
| 3616 | } |
| 3617 | |
Prashant Patil | dec9fdc | 2021-12-08 15:25:47 +0000 | [diff] [blame] | 3618 | /* |
| 3619 | * VerificationOperationsTest.HmacVerificationFailsForCorruptSignature |
| 3620 | * |
| 3621 | * Verifies HMAC signature verification should fails if message or signature is corrupted. |
| 3622 | */ |
| 3623 | TEST_P(VerificationOperationsTest, HmacVerificationFailsForCorruptSignature) { |
| 3624 | string key_material = "HelloThisIsAKey"; |
| 3625 | |
| 3626 | vector<uint8_t> signing_key, verification_key; |
| 3627 | vector<KeyCharacteristics> signing_key_chars, verification_key_chars; |
| 3628 | EXPECT_EQ(ErrorCode::OK, |
| 3629 | ImportKey(AuthorizationSetBuilder() |
| 3630 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3631 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3632 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 3633 | .Digest(Digest::SHA_2_256) |
| 3634 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3635 | KeyFormat::RAW, key_material, &signing_key, &signing_key_chars)); |
| 3636 | EXPECT_EQ(ErrorCode::OK, |
| 3637 | ImportKey(AuthorizationSetBuilder() |
| 3638 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3639 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3640 | .Authorization(TAG_PURPOSE, KeyPurpose::VERIFY) |
| 3641 | .Digest(Digest::SHA_2_256) |
| 3642 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3643 | KeyFormat::RAW, key_material, &verification_key, &verification_key_chars)); |
| 3644 | |
| 3645 | string message = "This is a message."; |
| 3646 | string signature = SignMessage( |
| 3647 | signing_key, message, |
| 3648 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Authorization(TAG_MAC_LENGTH, 160)); |
| 3649 | |
| 3650 | AuthorizationSet begin_out_params; |
| 3651 | ASSERT_EQ(ErrorCode::OK, |
| 3652 | Begin(KeyPurpose::VERIFY, verification_key, |
| 3653 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &begin_out_params)); |
| 3654 | |
| 3655 | string corruptMessage = "This is b message."; // Corrupted message |
| 3656 | string output; |
| 3657 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(corruptMessage, signature, &output)); |
| 3658 | |
| 3659 | ASSERT_EQ(ErrorCode::OK, |
| 3660 | Begin(KeyPurpose::VERIFY, verification_key, |
| 3661 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &begin_out_params)); |
| 3662 | |
| 3663 | signature[0] += 1; // Corrupt a signature |
| 3664 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(message, signature, &output)); |
| 3665 | |
| 3666 | CheckedDeleteKey(&signing_key); |
| 3667 | CheckedDeleteKey(&verification_key); |
| 3668 | } |
| 3669 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3670 | INSTANTIATE_KEYMINT_AIDL_TEST(VerificationOperationsTest); |
| 3671 | |
| 3672 | typedef KeyMintAidlTestBase ExportKeyTest; |
| 3673 | |
| 3674 | /* |
| 3675 | * ExportKeyTest.RsaUnsupportedKeyFormat |
| 3676 | * |
| 3677 | * Verifies that attempting to export RSA keys in PKCS#8 format fails with the correct error. |
| 3678 | */ |
| 3679 | // TODO(seleneh) add ExportKey to GenerateKey |
| 3680 | // check result |
| 3681 | |
| 3682 | class ImportKeyTest : public KeyMintAidlTestBase { |
| 3683 | public: |
| 3684 | template <TagType tag_type, Tag tag, typename ValueT> |
| 3685 | void CheckCryptoParam(TypedTag<tag_type, tag> ttag, ValueT expected) { |
| 3686 | SCOPED_TRACE("CheckCryptoParam"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3687 | for (auto& entry : key_characteristics_) { |
| 3688 | if (entry.securityLevel == SecLevel()) { |
| 3689 | EXPECT_TRUE(contains(entry.authorizations, ttag, expected)) |
| 3690 | << "Tag " << tag << " with value " << expected |
| 3691 | << " not found at security level" << entry.securityLevel; |
| 3692 | } else { |
| 3693 | EXPECT_FALSE(contains(entry.authorizations, ttag, expected)) |
| 3694 | << "Tag " << tag << " found at security level " << entry.securityLevel; |
| 3695 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | void CheckOrigin() { |
| 3700 | SCOPED_TRACE("CheckOrigin"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3701 | // Origin isn't a crypto param, but it always lives with them. |
| 3702 | return CheckCryptoParam(TAG_ORIGIN, KeyOrigin::IMPORTED); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3703 | } |
| 3704 | }; |
| 3705 | |
| 3706 | /* |
| 3707 | * ImportKeyTest.RsaSuccess |
| 3708 | * |
| 3709 | * Verifies that importing and using an RSA key pair works correctly. |
| 3710 | */ |
| 3711 | TEST_P(ImportKeyTest, RsaSuccess) { |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3712 | uint32_t key_size; |
| 3713 | string key; |
| 3714 | |
| 3715 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3716 | key_size = 2048; |
| 3717 | key = rsa_2048_key; |
| 3718 | } else { |
| 3719 | key_size = 1024; |
| 3720 | key = rsa_key; |
| 3721 | } |
| 3722 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3723 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3724 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3725 | .RsaSigningKey(key_size, 65537) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3726 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3727 | .Padding(PaddingMode::RSA_PSS) |
| 3728 | .SetDefaultValidity(), |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3729 | KeyFormat::PKCS8, key)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3730 | |
| 3731 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3732 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3733 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3734 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3735 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3736 | CheckOrigin(); |
| 3737 | |
| 3738 | string message(1024 / 8, 'a'); |
| 3739 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3740 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3741 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3745 | * ImportKeyTest.RsaSuccessWithoutParams |
| 3746 | * |
| 3747 | * Verifies that importing and using an RSA key pair without specifying parameters |
| 3748 | * works correctly. |
| 3749 | */ |
| 3750 | TEST_P(ImportKeyTest, RsaSuccessWithoutParams) { |
| 3751 | uint32_t key_size; |
| 3752 | string key; |
| 3753 | |
| 3754 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3755 | key_size = 2048; |
| 3756 | key = rsa_2048_key; |
| 3757 | } else { |
| 3758 | key_size = 1024; |
| 3759 | key = rsa_key; |
| 3760 | } |
| 3761 | |
| 3762 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3763 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3764 | .SigningKey() |
| 3765 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 3766 | .Digest(Digest::SHA_2_256) |
| 3767 | .Padding(PaddingMode::RSA_PSS) |
| 3768 | .SetDefaultValidity(), |
| 3769 | KeyFormat::PKCS8, key)); |
| 3770 | |
| 3771 | // Key size and public exponent are determined from the imported key material. |
| 3772 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
| 3773 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3774 | |
| 3775 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
| 3776 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3777 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3778 | CheckOrigin(); |
| 3779 | |
| 3780 | string message(1024 / 8, 'a'); |
| 3781 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3782 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3783 | LocalVerifyMessage(message, signature, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3787 | * ImportKeyTest.RsaKeySizeMismatch |
| 3788 | * |
| 3789 | * Verifies that importing an RSA key pair with a size that doesn't match the key fails in the |
| 3790 | * correct way. |
| 3791 | */ |
| 3792 | TEST_P(ImportKeyTest, RsaKeySizeMismatch) { |
| 3793 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3794 | ImportKey(AuthorizationSetBuilder() |
| 3795 | .RsaSigningKey(2048 /* Doesn't match key */, 65537) |
| 3796 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3797 | .Padding(PaddingMode::NONE) |
| 3798 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3799 | KeyFormat::PKCS8, rsa_key)); |
| 3800 | } |
| 3801 | |
| 3802 | /* |
| 3803 | * ImportKeyTest.RsaPublicExponentMismatch |
| 3804 | * |
| 3805 | * Verifies that importing an RSA key pair with a public exponent that doesn't match the key |
| 3806 | * fails in the correct way. |
| 3807 | */ |
| 3808 | TEST_P(ImportKeyTest, RsaPublicExponentMismatch) { |
| 3809 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3810 | ImportKey(AuthorizationSetBuilder() |
| 3811 | .RsaSigningKey(1024, 3 /* Doesn't match key */) |
| 3812 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3813 | .Padding(PaddingMode::NONE) |
| 3814 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3815 | KeyFormat::PKCS8, rsa_key)); |
| 3816 | } |
| 3817 | |
| 3818 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3819 | * ImportKeyTest.RsaAttestMultiPurposeFail |
| 3820 | * |
| 3821 | * Verifies that importing an RSA key pair with purpose ATTEST_KEY+SIGN fails. |
| 3822 | */ |
| 3823 | TEST_P(ImportKeyTest, RsaAttestMultiPurposeFail) { |
David Drysdale | 50a66b8 | 2022-03-21 15:21:32 +0000 | [diff] [blame^] | 3824 | if (AidlVersion() < 2) { |
| 3825 | // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined |
| 3826 | // with other key purposes. However, this was not checked at the time |
| 3827 | // so we can only be strict about checking this for implementations of KeyMint |
| 3828 | // version 2 and above. |
| 3829 | GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2"; |
| 3830 | } |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3831 | uint32_t key_size = 2048; |
| 3832 | string key = rsa_2048_key; |
| 3833 | |
| 3834 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3835 | ImportKey(AuthorizationSetBuilder() |
| 3836 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3837 | .RsaSigningKey(key_size, 65537) |
| 3838 | .AttestKey() |
| 3839 | .Digest(Digest::SHA_2_256) |
| 3840 | .Padding(PaddingMode::RSA_PSS) |
| 3841 | .SetDefaultValidity(), |
| 3842 | KeyFormat::PKCS8, key)); |
| 3843 | } |
| 3844 | |
| 3845 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3846 | * ImportKeyTest.EcdsaSuccess |
| 3847 | * |
| 3848 | * Verifies that importing and using an ECDSA P-256 key pair works correctly. |
| 3849 | */ |
| 3850 | TEST_P(ImportKeyTest, EcdsaSuccess) { |
| 3851 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3852 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3853 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3854 | .Digest(Digest::SHA_2_256) |
| 3855 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3856 | KeyFormat::PKCS8, ec_256_key)); |
| 3857 | |
| 3858 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3859 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3860 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3861 | |
| 3862 | CheckOrigin(); |
| 3863 | |
| 3864 | string message(32, 'a'); |
| 3865 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3866 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3867 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* |
| 3871 | * ImportKeyTest.EcdsaP256RFC5915Success |
| 3872 | * |
| 3873 | * Verifies that importing and using an ECDSA P-256 key pair encoded using RFC5915 works |
| 3874 | * correctly. |
| 3875 | */ |
| 3876 | TEST_P(ImportKeyTest, EcdsaP256RFC5915Success) { |
| 3877 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3878 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3879 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3880 | .Digest(Digest::SHA_2_256) |
| 3881 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3882 | KeyFormat::PKCS8, ec_256_key_rfc5915)); |
| 3883 | |
| 3884 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3885 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3886 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3887 | |
| 3888 | CheckOrigin(); |
| 3889 | |
| 3890 | string message(32, 'a'); |
| 3891 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3892 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3893 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | /* |
| 3897 | * ImportKeyTest.EcdsaP256SEC1Success |
| 3898 | * |
| 3899 | * Verifies that importing and using an ECDSA P-256 key pair encoded using SEC1 works correctly. |
| 3900 | */ |
| 3901 | TEST_P(ImportKeyTest, EcdsaP256SEC1Success) { |
| 3902 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3903 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3904 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3905 | .Digest(Digest::SHA_2_256) |
| 3906 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3907 | KeyFormat::PKCS8, ec_256_key_sec1)); |
| 3908 | |
| 3909 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3910 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3911 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3912 | |
| 3913 | CheckOrigin(); |
| 3914 | |
| 3915 | string message(32, 'a'); |
| 3916 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3917 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3918 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | /* |
| 3922 | * ImportKeyTest.Ecdsa521Success |
| 3923 | * |
| 3924 | * Verifies that importing and using an ECDSA P-521 key pair works correctly. |
| 3925 | */ |
| 3926 | TEST_P(ImportKeyTest, Ecdsa521Success) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 3927 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3928 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 3929 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3930 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3931 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3932 | .EcdsaSigningKey(EcCurve::P_521) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3933 | .Digest(Digest::SHA_2_256) |
| 3934 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3935 | KeyFormat::PKCS8, ec_521_key)); |
| 3936 | |
| 3937 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3938 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3939 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_521); |
| 3940 | CheckOrigin(); |
| 3941 | |
| 3942 | string message(32, 'a'); |
| 3943 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3944 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3945 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3949 | * ImportKeyTest.EcdsaCurveMismatch |
| 3950 | * |
| 3951 | * Verifies that importing an ECDSA key pair with a curve that doesn't match the key fails in |
| 3952 | * the correct way. |
| 3953 | */ |
| 3954 | TEST_P(ImportKeyTest, EcdsaCurveMismatch) { |
| 3955 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3956 | ImportKey(AuthorizationSetBuilder() |
| 3957 | .EcdsaSigningKey(EcCurve::P_224 /* Doesn't match key */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3958 | .Digest(Digest::NONE) |
| 3959 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3960 | KeyFormat::PKCS8, ec_256_key)); |
| 3961 | } |
| 3962 | |
| 3963 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3964 | * ImportKeyTest.EcdsaAttestMultiPurposeFail |
| 3965 | * |
| 3966 | * Verifies that importing and using an ECDSA P-256 key pair with purpose ATTEST_KEY+SIGN fails. |
| 3967 | */ |
| 3968 | TEST_P(ImportKeyTest, EcdsaAttestMultiPurposeFail) { |
David Drysdale | 50a66b8 | 2022-03-21 15:21:32 +0000 | [diff] [blame^] | 3969 | if (AidlVersion() < 2) { |
| 3970 | // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined |
| 3971 | // with other key purposes. However, this was not checked at the time |
| 3972 | // so we can only be strict about checking this for implementations of KeyMint |
| 3973 | // version 2 and above. |
| 3974 | GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2"; |
| 3975 | } |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3976 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3977 | ImportKey(AuthorizationSetBuilder() |
| 3978 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3979 | .EcdsaSigningKey(EcCurve::P_256) |
| 3980 | .AttestKey() |
| 3981 | .Digest(Digest::SHA_2_256) |
| 3982 | .SetDefaultValidity(), |
| 3983 | KeyFormat::PKCS8, ec_256_key)); |
| 3984 | } |
| 3985 | |
| 3986 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 3987 | * ImportKeyTest.Ed25519RawSuccess |
| 3988 | * |
| 3989 | * Verifies that importing and using a raw Ed25519 private key works correctly. |
| 3990 | */ |
| 3991 | TEST_P(ImportKeyTest, Ed25519RawSuccess) { |
| 3992 | if (!Curve25519Supported()) { |
| 3993 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3994 | } |
| 3995 | |
| 3996 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3997 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3998 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3999 | .Digest(Digest::NONE) |
| 4000 | .SetDefaultValidity(), |
| 4001 | KeyFormat::RAW, ed25519_key)); |
| 4002 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 4003 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 4004 | CheckOrigin(); |
| 4005 | |
| 4006 | // The returned cert should hold the correct public key. |
| 4007 | ASSERT_GT(cert_chain_.size(), 0); |
| 4008 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 4009 | ASSERT_NE(kmKeyCert, nullptr); |
| 4010 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 4011 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 4012 | size_t kmPubKeySize = 32; |
| 4013 | uint8_t kmPubKeyData[32]; |
| 4014 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 4015 | ASSERT_EQ(kmPubKeySize, 32); |
| 4016 | EXPECT_EQ(string(kmPubKeyData, kmPubKeyData + 32), ed25519_pubkey); |
| 4017 | |
| 4018 | string message(32, 'a'); |
| 4019 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 4020 | string signature = SignMessage(message, params); |
| 4021 | LocalVerifyMessage(message, signature, params); |
| 4022 | } |
| 4023 | |
| 4024 | /* |
| 4025 | * ImportKeyTest.Ed25519Pkcs8Success |
| 4026 | * |
| 4027 | * Verifies that importing and using a PKCS#8-encoded Ed25519 private key works correctly. |
| 4028 | */ |
| 4029 | TEST_P(ImportKeyTest, Ed25519Pkcs8Success) { |
| 4030 | if (!Curve25519Supported()) { |
| 4031 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4032 | } |
| 4033 | |
| 4034 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4035 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4036 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4037 | .Digest(Digest::NONE) |
| 4038 | .SetDefaultValidity(), |
| 4039 | KeyFormat::PKCS8, ed25519_pkcs8_key)); |
| 4040 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 4041 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 4042 | CheckOrigin(); |
| 4043 | |
| 4044 | // The returned cert should hold the correct public key. |
| 4045 | ASSERT_GT(cert_chain_.size(), 0); |
| 4046 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 4047 | ASSERT_NE(kmKeyCert, nullptr); |
| 4048 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 4049 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 4050 | size_t kmPubKeySize = 32; |
| 4051 | uint8_t kmPubKeyData[32]; |
| 4052 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 4053 | ASSERT_EQ(kmPubKeySize, 32); |
| 4054 | EXPECT_EQ(string(kmPubKeyData, kmPubKeyData + 32), ed25519_pubkey); |
| 4055 | |
| 4056 | string message(32, 'a'); |
| 4057 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 4058 | string signature = SignMessage(message, params); |
| 4059 | LocalVerifyMessage(message, signature, params); |
| 4060 | } |
| 4061 | |
| 4062 | /* |
| 4063 | * ImportKeyTest.Ed25519CurveMismatch |
| 4064 | * |
| 4065 | * Verifies that importing an Ed25519 key pair with a curve that doesn't match the key fails in |
| 4066 | * the correct way. |
| 4067 | */ |
| 4068 | TEST_P(ImportKeyTest, Ed25519CurveMismatch) { |
| 4069 | if (!Curve25519Supported()) { |
| 4070 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4071 | } |
| 4072 | |
| 4073 | ASSERT_NE(ErrorCode::OK, |
| 4074 | ImportKey(AuthorizationSetBuilder() |
| 4075 | .EcdsaSigningKey(EcCurve::P_224 /* Doesn't match key */) |
| 4076 | .Digest(Digest::NONE) |
| 4077 | .SetDefaultValidity(), |
| 4078 | KeyFormat::RAW, ed25519_key)); |
| 4079 | } |
| 4080 | |
| 4081 | /* |
| 4082 | * ImportKeyTest.Ed25519FormatMismatch |
| 4083 | * |
| 4084 | * Verifies that importing an Ed25519 key pair with an invalid format fails. |
| 4085 | */ |
| 4086 | TEST_P(ImportKeyTest, Ed25519FormatMismatch) { |
| 4087 | if (!Curve25519Supported()) { |
| 4088 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4089 | } |
| 4090 | |
| 4091 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4092 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4093 | .Digest(Digest::NONE) |
| 4094 | .SetDefaultValidity(), |
| 4095 | KeyFormat::PKCS8, ed25519_key)); |
| 4096 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4097 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4098 | .Digest(Digest::NONE) |
| 4099 | .SetDefaultValidity(), |
| 4100 | KeyFormat::RAW, ed25519_pkcs8_key)); |
| 4101 | } |
| 4102 | |
| 4103 | /* |
| 4104 | * ImportKeyTest.Ed25519PurposeMismatch |
| 4105 | * |
| 4106 | * Verifies that importing an Ed25519 key pair with an invalid purpose fails. |
| 4107 | */ |
| 4108 | TEST_P(ImportKeyTest, Ed25519PurposeMismatch) { |
| 4109 | if (!Curve25519Supported()) { |
| 4110 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4111 | } |
| 4112 | |
| 4113 | // Can't have both SIGN and ATTEST_KEY |
| 4114 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4115 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4116 | .Authorization(TAG_PURPOSE, KeyPurpose::ATTEST_KEY) |
| 4117 | .Digest(Digest::NONE) |
| 4118 | .SetDefaultValidity(), |
| 4119 | KeyFormat::RAW, ed25519_key)); |
| 4120 | // AGREE_KEY is for X25519 (but can only tell the difference if the import key is in |
| 4121 | // PKCS#8 format and so includes an OID). |
| 4122 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4123 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4124 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4125 | .Digest(Digest::NONE) |
| 4126 | .SetDefaultValidity(), |
| 4127 | KeyFormat::PKCS8, ed25519_pkcs8_key)); |
| 4128 | } |
| 4129 | |
| 4130 | /* |
| 4131 | * ImportKeyTest.X25519RawSuccess |
| 4132 | * |
| 4133 | * Verifies that importing and using a raw X25519 private key works correctly. |
| 4134 | */ |
| 4135 | TEST_P(ImportKeyTest, X25519RawSuccess) { |
| 4136 | if (!Curve25519Supported()) { |
| 4137 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4138 | } |
| 4139 | |
| 4140 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4141 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4142 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4143 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4144 | .SetDefaultValidity(), |
| 4145 | KeyFormat::RAW, x25519_key)); |
| 4146 | |
| 4147 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 4148 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 4149 | CheckOrigin(); |
| 4150 | } |
| 4151 | |
| 4152 | /* |
| 4153 | * ImportKeyTest.X25519Pkcs8Success |
| 4154 | * |
| 4155 | * Verifies that importing and using a PKCS#8-encoded X25519 private key works correctly. |
| 4156 | */ |
| 4157 | TEST_P(ImportKeyTest, X25519Pkcs8Success) { |
| 4158 | if (!Curve25519Supported()) { |
| 4159 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4160 | } |
| 4161 | |
| 4162 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4163 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4164 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4165 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4166 | .SetDefaultValidity(), |
| 4167 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 4168 | |
| 4169 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 4170 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 4171 | CheckOrigin(); |
| 4172 | } |
| 4173 | |
| 4174 | /* |
| 4175 | * ImportKeyTest.X25519CurveMismatch |
| 4176 | * |
| 4177 | * Verifies that importing an X25519 key with a curve that doesn't match the key fails in |
| 4178 | * the correct way. |
| 4179 | */ |
| 4180 | TEST_P(ImportKeyTest, X25519CurveMismatch) { |
| 4181 | if (!Curve25519Supported()) { |
| 4182 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4183 | } |
| 4184 | |
| 4185 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4186 | .EcdsaKey(EcCurve::P_224 /* Doesn't match key */) |
| 4187 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4188 | .SetDefaultValidity(), |
| 4189 | KeyFormat::RAW, x25519_key)); |
| 4190 | } |
| 4191 | |
| 4192 | /* |
| 4193 | * ImportKeyTest.X25519FormatMismatch |
| 4194 | * |
| 4195 | * Verifies that importing an X25519 key with an invalid format fails. |
| 4196 | */ |
| 4197 | TEST_P(ImportKeyTest, X25519FormatMismatch) { |
| 4198 | if (!Curve25519Supported()) { |
| 4199 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4200 | } |
| 4201 | |
| 4202 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4203 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4204 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4205 | .SetDefaultValidity(), |
| 4206 | KeyFormat::PKCS8, x25519_key)); |
| 4207 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4208 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4209 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4210 | .SetDefaultValidity(), |
| 4211 | KeyFormat::RAW, x25519_pkcs8_key)); |
| 4212 | } |
| 4213 | |
| 4214 | /* |
| 4215 | * ImportKeyTest.X25519PurposeMismatch |
| 4216 | * |
| 4217 | * Verifies that importing an X25519 key pair with an invalid format fails. |
| 4218 | */ |
| 4219 | TEST_P(ImportKeyTest, X25519PurposeMismatch) { |
| 4220 | if (!Curve25519Supported()) { |
| 4221 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4222 | } |
| 4223 | |
| 4224 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4225 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4226 | .Authorization(TAG_PURPOSE, KeyPurpose::ATTEST_KEY) |
| 4227 | .SetDefaultValidity(), |
| 4228 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 4229 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4230 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4231 | .SetDefaultValidity(), |
| 4232 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 4233 | } |
| 4234 | |
| 4235 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4236 | * ImportKeyTest.AesSuccess |
| 4237 | * |
| 4238 | * Verifies that importing and using an AES key works. |
| 4239 | */ |
| 4240 | TEST_P(ImportKeyTest, AesSuccess) { |
| 4241 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4242 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4243 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4244 | .AesEncryptionKey(key.size() * 8) |
| 4245 | .EcbMode() |
| 4246 | .Padding(PaddingMode::PKCS7), |
| 4247 | KeyFormat::RAW, key)); |
| 4248 | |
| 4249 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::AES); |
| 4250 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 4251 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 4252 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 4253 | CheckOrigin(); |
| 4254 | |
| 4255 | string message = "Hello World!"; |
| 4256 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4257 | string ciphertext = EncryptMessage(message, params); |
| 4258 | string plaintext = DecryptMessage(ciphertext, params); |
| 4259 | EXPECT_EQ(message, plaintext); |
| 4260 | } |
| 4261 | |
| 4262 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4263 | * ImportKeyTest.AesFailure |
| 4264 | * |
| 4265 | * Verifies that importing an invalid AES key fails. |
| 4266 | */ |
| 4267 | TEST_P(ImportKeyTest, AesFailure) { |
| 4268 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4269 | uint32_t bitlen = key.size() * 8; |
| 4270 | for (uint32_t key_size : {bitlen - 1, bitlen + 1, bitlen - 8, bitlen + 8}) { |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4271 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4272 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 4273 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4274 | .AesEncryptionKey(key_size) |
| 4275 | .EcbMode() |
| 4276 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4277 | KeyFormat::RAW, key); |
| 4278 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4279 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 4280 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4281 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4282 | |
| 4283 | // Explicit key size matches that of the provided key, but it's not a valid size. |
| 4284 | string long_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4285 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4286 | ImportKey(AuthorizationSetBuilder() |
| 4287 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4288 | .AesEncryptionKey(long_key.size() * 8) |
| 4289 | .EcbMode() |
| 4290 | .Padding(PaddingMode::PKCS7), |
| 4291 | KeyFormat::RAW, long_key)); |
| 4292 | string short_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4293 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4294 | ImportKey(AuthorizationSetBuilder() |
| 4295 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4296 | .AesEncryptionKey(short_key.size() * 8) |
| 4297 | .EcbMode() |
| 4298 | .Padding(PaddingMode::PKCS7), |
| 4299 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | /* |
| 4303 | * ImportKeyTest.TripleDesSuccess |
| 4304 | * |
| 4305 | * Verifies that importing and using a 3DES key works. |
| 4306 | */ |
| 4307 | TEST_P(ImportKeyTest, TripleDesSuccess) { |
| 4308 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
| 4309 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4310 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4311 | .TripleDesEncryptionKey(168) |
| 4312 | .EcbMode() |
| 4313 | .Padding(PaddingMode::PKCS7), |
| 4314 | KeyFormat::RAW, key)); |
| 4315 | |
| 4316 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::TRIPLE_DES); |
| 4317 | CheckCryptoParam(TAG_KEY_SIZE, 168U); |
| 4318 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 4319 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 4320 | CheckOrigin(); |
| 4321 | |
| 4322 | string message = "Hello World!"; |
| 4323 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4324 | string ciphertext = EncryptMessage(message, params); |
| 4325 | string plaintext = DecryptMessage(ciphertext, params); |
| 4326 | EXPECT_EQ(message, plaintext); |
| 4327 | } |
| 4328 | |
| 4329 | /* |
| 4330 | * ImportKeyTest.TripleDesFailure |
| 4331 | * |
| 4332 | * Verifies that importing an invalid 3DES key fails. |
| 4333 | */ |
| 4334 | TEST_P(ImportKeyTest, TripleDesFailure) { |
| 4335 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4336 | uint32_t bitlen = key.size() * 7; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4337 | for (uint32_t key_size : {bitlen - 1, bitlen + 1, bitlen - 8, bitlen + 8}) { |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4338 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4339 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 4340 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4341 | .TripleDesEncryptionKey(key_size) |
| 4342 | .EcbMode() |
| 4343 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4344 | KeyFormat::RAW, key); |
| 4345 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4346 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 4347 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4348 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4349 | // Explicit key size matches that of the provided key, but it's not a valid size. |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4350 | string long_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f735800"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4351 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4352 | ImportKey(AuthorizationSetBuilder() |
| 4353 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4354 | .TripleDesEncryptionKey(long_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4355 | .EcbMode() |
| 4356 | .Padding(PaddingMode::PKCS7), |
| 4357 | KeyFormat::RAW, long_key)); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4358 | string short_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f73"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4359 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4360 | ImportKey(AuthorizationSetBuilder() |
| 4361 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4362 | .TripleDesEncryptionKey(short_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4363 | .EcbMode() |
| 4364 | .Padding(PaddingMode::PKCS7), |
| 4365 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | /* |
| 4369 | * ImportKeyTest.HmacKeySuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4370 | * |
| 4371 | * Verifies that importing and using an HMAC key works. |
| 4372 | */ |
| 4373 | TEST_P(ImportKeyTest, HmacKeySuccess) { |
| 4374 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4375 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4376 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4377 | .HmacKey(key.size() * 8) |
| 4378 | .Digest(Digest::SHA_2_256) |
| 4379 | .Authorization(TAG_MIN_MAC_LENGTH, 256), |
| 4380 | KeyFormat::RAW, key)); |
| 4381 | |
| 4382 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::HMAC); |
| 4383 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 4384 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 4385 | CheckOrigin(); |
| 4386 | |
| 4387 | string message = "Hello World!"; |
| 4388 | string signature = MacMessage(message, Digest::SHA_2_256, 256); |
| 4389 | VerifyMessage(message, signature, AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 4390 | } |
| 4391 | |
| 4392 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportKeyTest); |
| 4393 | |
| 4394 | auto wrapped_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4395 | // IKeyMintDevice.aidl |
| 4396 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 4397 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4398 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 4399 | "934bf94e2aa28a3f83c9f79297250262" |
| 4400 | "fbe3276b5a1c91159bbfa3ef8957aac8" |
| 4401 | "4b59b30b455a79c2973480823d8b3863" |
| 4402 | "c3deef4a8e243590268d80e18751a0e1" |
| 4403 | "30f67ce6a1ace9f79b95e097474febc9" |
| 4404 | "81195b1d13a69086c0863f66a7b7fdb4" |
| 4405 | "8792227b1ac5e2489febdf087ab54864" |
| 4406 | "83033a6f001ca5d1ec1e27f5c30f4cec" |
| 4407 | "2642074a39ae68aee552e196627a8e3d" |
| 4408 | "867e67a8c01b11e75f13cca0a97ab668" |
| 4409 | "b50cda07a8ecb7cd8e3dd7009c963653" |
| 4410 | "4f6f239cffe1fc8daa466f78b676c711" |
| 4411 | "9efb96bce4e69ca2a25d0b34ed9c3ff9" |
| 4412 | "99b801597d5220e307eaa5bee507fb94" |
| 4413 | "d1fa69f9e519b2de315bac92c36f2ea1" |
| 4414 | "fa1df4478c0ddedeae8c70e0233cd098" |
| 4415 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 4416 | "d796b02c370f1fa4cc0124f1" |
| 4417 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 4418 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 4419 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 4420 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 4421 | "3106" // SET length 0x06 |
| 4422 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 4423 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 4424 | // } end SET |
| 4425 | // } end [1] |
| 4426 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 4427 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 4428 | // } end [2] |
| 4429 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 4430 | "02020100" // INTEGER length 2 value 0x100 |
| 4431 | // } end [3] |
| 4432 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode) |
| 4433 | "3103" // SET length 0x03 { |
| 4434 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 4435 | // } end SET |
| 4436 | // } end [4] |
| 4437 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 4438 | "3103" // SET length 0x03 { |
| 4439 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 4440 | // } end SET |
| 4441 | // } end [5] |
| 4442 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 4443 | // (noAuthRequired) |
| 4444 | "0500" // NULL |
| 4445 | // } end [503] |
| 4446 | // } end SEQUENCE (AuthorizationList) |
| 4447 | // } end SEQUENCE (KeyDescription) |
| 4448 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 4449 | "ccd540855f833a5e1480bfd2d36faf3a" |
| 4450 | "eee15df5beabe2691bc82dde2a7aa910" |
| 4451 | "0410" // OCTET STRING length 0x10 (tag) |
| 4452 | "64c9f689c60ff6223ab6e6999e0eb6e5" |
| 4453 | // } SEQUENCE (SecureKeyWrapper) |
| 4454 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4455 | |
| 4456 | auto wrapped_key_masked = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4457 | // IKeyMintDevice.aidl |
| 4458 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 4459 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4460 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 4461 | "aad93ed5924f283b4bb5526fbe7a1412" |
| 4462 | "f9d9749ec30db9062b29e574a8546f33" |
| 4463 | "c88732452f5b8e6a391ee76c39ed1712" |
| 4464 | "c61d8df6213dec1cffbc17a8c6d04c7b" |
| 4465 | "30893d8daa9b2015213e219468215532" |
| 4466 | "07f8f9931c4caba23ed3bee28b36947e" |
| 4467 | "47f10e0a5c3dc51c988a628daad3e5e1" |
| 4468 | "f4005e79c2d5a96c284b4b8d7e4948f3" |
| 4469 | "31e5b85dd5a236f85579f3ea1d1b8484" |
| 4470 | "87470bdb0ab4f81a12bee42c99fe0df4" |
| 4471 | "bee3759453e69ad1d68a809ce06b949f" |
| 4472 | "7694a990429b2fe81e066ff43e56a216" |
| 4473 | "02db70757922a4bcc23ab89f1e35da77" |
| 4474 | "586775f423e519c2ea394caf48a28d0c" |
| 4475 | "8020f1dcf6b3a68ec246f615ae96dae9" |
| 4476 | "a079b1f6eb959033c1af5c125fd94168" |
| 4477 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 4478 | "6d9721d08589581ab49204a3" |
| 4479 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 4480 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 4481 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 4482 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 4483 | "3106" // SET length 0x06 |
| 4484 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 4485 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 4486 | // } end SET |
| 4487 | // } end [1] |
| 4488 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 4489 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 4490 | // } end [2] |
| 4491 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 4492 | "02020100" // INTEGER length 2 value 0x100 |
| 4493 | // } end [3] |
| 4494 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode |
| 4495 | "3103" // SET length 0x03 { |
| 4496 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 4497 | // } end SET |
| 4498 | // } end [4] |
| 4499 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 4500 | "3103" // SET length 0x03 { |
| 4501 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 4502 | // } end SET |
| 4503 | // } end [5] |
| 4504 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 4505 | // (noAuthRequired) |
| 4506 | "0500" // NULL |
| 4507 | // } end [503] |
| 4508 | // } end SEQUENCE (AuthorizationList) |
| 4509 | // } end SEQUENCE (KeyDescription) |
| 4510 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 4511 | "a61c6e247e25b3e6e69aa78eb03c2d4a" |
| 4512 | "c20d1f99a9a024a76f35c8e2cab9b68d" |
| 4513 | "0410" // OCTET STRING length 0x10 (tag) |
| 4514 | "2560c70109ae67c030f00b98b512a670" |
| 4515 | // } SEQUENCE (SecureKeyWrapper) |
| 4516 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4517 | |
| 4518 | auto wrapping_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4519 | // RFC 5208 s5 |
| 4520 | "308204be" // SEQUENCE length 0x4be (PrivateKeyInfo) { |
| 4521 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4522 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 4523 | "0609" // OBJECT IDENTIFIER length 0x09 (algorithm) |
| 4524 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (RSAES-PKCS1-v1_5 encryption scheme) |
| 4525 | "0500" // NULL (parameters) |
| 4526 | // } SEQUENCE (AlgorithmIdentifier) |
| 4527 | "048204a8" // OCTET STRING len 0x4a8 (privateKey), which contains... |
| 4528 | // RFC 8017 A.1.2 |
| 4529 | "308204a4" // SEQUENCE len 0x4a4 (RSAPrivateKey) { |
| 4530 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4531 | "02820101" // INTEGER length 0x0101 (modulus) value... |
| 4532 | "00aec367931d8900ce56b0067f7d70e1" // 0x10 |
| 4533 | "fc653f3f34d194c1fed50018fb43db93" // 0x20 |
| 4534 | "7b06e673a837313d56b1c725150a3fef" // 0x30 |
| 4535 | "86acbddc41bb759c2854eae32d35841e" // 0x40 |
| 4536 | "fb5c18d82bc90a1cb5c1d55adf245b02" // 0x50 |
| 4537 | "911f0b7cda88c421ff0ebafe7c0d23be" // 0x60 |
| 4538 | "312d7bd5921ffaea1347c157406fef71" // 0x70 |
| 4539 | "8f682643e4e5d33c6703d61c0cf7ac0b" // 0x80 |
| 4540 | "f4645c11f5c1374c3886427411c44979" // 0x90 |
| 4541 | "6792e0bef75dec858a2123c36753e02a" // 0xa0 |
| 4542 | "95a96d7c454b504de385a642e0dfc3e6" // 0xb0 |
| 4543 | "0ac3a7ee4991d0d48b0172a95f9536f0" // 0xc0 |
| 4544 | "2ba13cecccb92b727db5c27e5b2f5cec" // 0xd0 |
| 4545 | "09600b286af5cf14c42024c61ddfe71c" // 0xe0 |
| 4546 | "2a8d7458f185234cb00e01d282f10f8f" // 0xf0 |
| 4547 | "c6721d2aed3f4833cca2bd8fa62821dd" // 0x100 |
| 4548 | "55" // 0x101 |
| 4549 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 4550 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 4551 | "431447b6251908112b1ee76f99f3711a" // 0x10 |
| 4552 | "52b6630960046c2de70de188d833f8b8" // 0x20 |
| 4553 | "b91e4d785caeeeaf4f0f74414e2cda40" // 0x30 |
| 4554 | "641f7fe24f14c67a88959bdb27766df9" // 0x40 |
| 4555 | "e710b630a03adc683b5d2c43080e52be" // 0x50 |
| 4556 | "e71e9eaeb6de297a5fea1072070d181c" // 0x60 |
| 4557 | "822bccff087d63c940ba8a45f670feb2" // 0x70 |
| 4558 | "9fb4484d1c95e6d2579ba02aae0a0090" // 0x80 |
| 4559 | "0c3ebf490e3d2cd7ee8d0e20c536e4dc" // 0x90 |
| 4560 | "5a5097272888cddd7e91f228b1c4d747" // 0xa0 |
| 4561 | "4c55b8fcd618c4a957bbddd5ad7407cc" // 0xb0 |
| 4562 | "312d8d98a5caf7e08f4a0d6b45bb41c6" // 0xc0 |
| 4563 | "52659d5a5ba05b663737a8696281865b" // 0xd0 |
| 4564 | "a20fbdd7f851e6c56e8cbe0ddbbf24dc" // 0xe0 |
| 4565 | "03b2d2cb4c3d540fb0af52e034a2d066" // 0xf0 |
| 4566 | "98b128e5f101e3b51a34f8d8b4f86181" // 0x100 |
| 4567 | "028181" // INTEGER length 0x81 (prime1) value... |
| 4568 | "00de392e18d682c829266cc3454e1d61" // 0x10 |
| 4569 | "66242f32d9a1d10577753e904ea7d08b" // 0x20 |
| 4570 | "ff841be5bac82a164c5970007047b8c5" // 0x30 |
| 4571 | "17db8f8f84e37bd5988561bdf503d4dc" // 0x40 |
| 4572 | "2bdb38f885434ae42c355f725c9a60f9" // 0x50 |
| 4573 | "1f0788e1f1a97223b524b5357fdf72e2" // 0x60 |
| 4574 | "f696bab7d78e32bf92ba8e1864eab122" // 0x70 |
| 4575 | "9e91346130748a6e3c124f9149d71c74" // 0x80 |
| 4576 | "35" |
| 4577 | "028181" // INTEGER length 0x81 (prime2) value... |
| 4578 | "00c95387c0f9d35f137b57d0d65c397c" // 0x10 |
| 4579 | "5e21cc251e47008ed62a542409c8b6b6" // 0x20 |
| 4580 | "ac7f8967b3863ca645fcce49582a9aa1" // 0x30 |
| 4581 | "7349db6c4a95affdae0dae612e1afac9" // 0x40 |
| 4582 | "9ed39a2d934c880440aed8832f984316" // 0x50 |
| 4583 | "3a47f27f392199dc1202f9a0f9bd0830" // 0x60 |
| 4584 | "8007cb1e4e7f58309366a7de25f7c3c9" // 0x70 |
| 4585 | "b880677c068e1be936e81288815252a8" // 0x80 |
| 4586 | "a1" |
| 4587 | "028180" // INTEGER length 0x80 (exponent1) value... |
| 4588 | "57ff8ca1895080b2cae486ef0adfd791" // 0x10 |
| 4589 | "fb0235c0b8b36cd6c136e52e4085f4ea" // 0x20 |
| 4590 | "5a063212a4f105a3764743e53281988a" // 0x30 |
| 4591 | "ba073f6e0027298e1c4378556e0efca0" // 0x40 |
| 4592 | "e14ece1af76ad0b030f27af6f0ab35fb" // 0x50 |
| 4593 | "73a060d8b1a0e142fa2647e93b32e36d" // 0x60 |
| 4594 | "8282ae0a4de50ab7afe85500a16f43a6" // 0x70 |
| 4595 | "4719d6e2b9439823719cd08bcd031781" // 0x80 |
| 4596 | "028181" // INTEGER length 0x81 (exponent2) value... |
| 4597 | "00ba73b0bb28e3f81e9bd1c568713b10" // 0x10 |
| 4598 | "1241acc607976c4ddccc90e65b6556ca" // 0x20 |
| 4599 | "31516058f92b6e09f3b160ff0e374ec4" // 0x30 |
| 4600 | "0d78ae4d4979fde6ac06a1a400c61dd3" // 0x40 |
| 4601 | "1254186af30b22c10582a8a43e34fe94" // 0x50 |
| 4602 | "9c5f3b9755bae7baa7b7b7a6bd03b38c" // 0x60 |
| 4603 | "ef55c86885fc6c1978b9cee7ef33da50" // 0x70 |
| 4604 | "7c9df6b9277cff1e6aaa5d57aca52846" // 0x80 |
| 4605 | "61" |
| 4606 | "028181" // INTEGER length 0x81 (coefficient) value... |
| 4607 | "00c931617c77829dfb1270502be9195c" // 0x10 |
| 4608 | "8f2830885f57dba869536811e6864236" // 0x20 |
| 4609 | "d0c4736a0008a145af36b8357a7c3d13" // 0x30 |
| 4610 | "9966d04c4e00934ea1aede3bb6b8ec84" // 0x40 |
| 4611 | "1dc95e3f579751e2bfdfe27ae778983f" // 0x50 |
| 4612 | "959356210723287b0affcc9f727044d4" // 0x60 |
| 4613 | "8c373f1babde0724fa17a4fd4da0902c" // 0x70 |
| 4614 | "7c9b9bf27ba61be6ad02dfddda8f4e68" // 0x80 |
| 4615 | "22" |
| 4616 | // } SEQUENCE |
| 4617 | // } SEQUENCE () |
| 4618 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4619 | |
| 4620 | string zero_masking_key = |
| 4621 | hex2str("0000000000000000000000000000000000000000000000000000000000000000"); |
| 4622 | string masking_key = hex2str("D796B02C370F1FA4CC0124F14EC8CBEBE987E825246265050F399A51FD477DFC"); |
| 4623 | |
| 4624 | class ImportWrappedKeyTest : public KeyMintAidlTestBase {}; |
| 4625 | |
| 4626 | TEST_P(ImportWrappedKeyTest, Success) { |
| 4627 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4628 | .RsaEncryptionKey(2048, 65537) |
| 4629 | .Digest(Digest::SHA_2_256) |
| 4630 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4631 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4632 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4633 | |
| 4634 | ASSERT_EQ(ErrorCode::OK, |
| 4635 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4636 | AuthorizationSetBuilder() |
| 4637 | .Digest(Digest::SHA_2_256) |
| 4638 | .Padding(PaddingMode::RSA_OAEP))); |
| 4639 | |
| 4640 | string message = "Hello World!"; |
| 4641 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4642 | string ciphertext = EncryptMessage(message, params); |
| 4643 | string plaintext = DecryptMessage(ciphertext, params); |
| 4644 | EXPECT_EQ(message, plaintext); |
| 4645 | } |
| 4646 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4647 | /* |
| 4648 | * ImportWrappedKeyTest.SuccessSidsIgnored |
| 4649 | * |
| 4650 | * Verifies that password_sid and biometric_sid are ignored on import if the authorizations don't |
| 4651 | * include Tag:USER_SECURE_ID. |
| 4652 | */ |
| 4653 | TEST_P(ImportWrappedKeyTest, SuccessSidsIgnored) { |
| 4654 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4655 | .RsaEncryptionKey(2048, 65537) |
| 4656 | .Digest(Digest::SHA_2_256) |
| 4657 | .Padding(PaddingMode::RSA_OAEP) |
| 4658 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4659 | .SetDefaultValidity(); |
| 4660 | |
| 4661 | int64_t password_sid = 42; |
| 4662 | int64_t biometric_sid = 24; |
| 4663 | ASSERT_EQ(ErrorCode::OK, |
| 4664 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4665 | AuthorizationSetBuilder() |
| 4666 | .Digest(Digest::SHA_2_256) |
| 4667 | .Padding(PaddingMode::RSA_OAEP), |
| 4668 | password_sid, biometric_sid)); |
| 4669 | |
| 4670 | string message = "Hello World!"; |
| 4671 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4672 | string ciphertext = EncryptMessage(message, params); |
| 4673 | string plaintext = DecryptMessage(ciphertext, params); |
| 4674 | EXPECT_EQ(message, plaintext); |
| 4675 | } |
| 4676 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4677 | TEST_P(ImportWrappedKeyTest, SuccessMasked) { |
| 4678 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4679 | .RsaEncryptionKey(2048, 65537) |
| 4680 | .Digest(Digest::SHA_2_256) |
| 4681 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4682 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4683 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4684 | |
| 4685 | ASSERT_EQ(ErrorCode::OK, |
| 4686 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, masking_key, |
| 4687 | AuthorizationSetBuilder() |
| 4688 | .Digest(Digest::SHA_2_256) |
| 4689 | .Padding(PaddingMode::RSA_OAEP))); |
| 4690 | } |
| 4691 | |
| 4692 | TEST_P(ImportWrappedKeyTest, WrongMask) { |
| 4693 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4694 | .RsaEncryptionKey(2048, 65537) |
| 4695 | .Digest(Digest::SHA_2_256) |
| 4696 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4697 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4698 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4699 | |
| 4700 | ASSERT_EQ( |
| 4701 | ErrorCode::VERIFICATION_FAILED, |
| 4702 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4703 | AuthorizationSetBuilder() |
| 4704 | .Digest(Digest::SHA_2_256) |
| 4705 | .Padding(PaddingMode::RSA_OAEP))); |
| 4706 | } |
| 4707 | |
| 4708 | TEST_P(ImportWrappedKeyTest, WrongPurpose) { |
| 4709 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4710 | .RsaEncryptionKey(2048, 65537) |
| 4711 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4712 | .Padding(PaddingMode::RSA_OAEP) |
| 4713 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4714 | |
| 4715 | ASSERT_EQ( |
| 4716 | ErrorCode::INCOMPATIBLE_PURPOSE, |
| 4717 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4718 | AuthorizationSetBuilder() |
| 4719 | .Digest(Digest::SHA_2_256) |
| 4720 | .Padding(PaddingMode::RSA_OAEP))); |
| 4721 | } |
| 4722 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4723 | TEST_P(ImportWrappedKeyTest, WrongPaddingMode) { |
| 4724 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4725 | .RsaEncryptionKey(2048, 65537) |
| 4726 | .Digest(Digest::SHA_2_256) |
| 4727 | .Padding(PaddingMode::RSA_PSS) |
| 4728 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4729 | .SetDefaultValidity(); |
| 4730 | |
| 4731 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 4732 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4733 | AuthorizationSetBuilder() |
| 4734 | .Digest(Digest::SHA_2_256) |
| 4735 | .Padding(PaddingMode::RSA_OAEP))); |
| 4736 | } |
| 4737 | |
| 4738 | TEST_P(ImportWrappedKeyTest, WrongDigest) { |
| 4739 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4740 | .RsaEncryptionKey(2048, 65537) |
| 4741 | .Digest(Digest::SHA_2_512) |
| 4742 | .Padding(PaddingMode::RSA_OAEP) |
| 4743 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4744 | .SetDefaultValidity(); |
| 4745 | |
| 4746 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 4747 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4748 | AuthorizationSetBuilder() |
| 4749 | .Digest(Digest::SHA_2_256) |
| 4750 | .Padding(PaddingMode::RSA_OAEP))); |
| 4751 | } |
| 4752 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4753 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportWrappedKeyTest); |
| 4754 | |
| 4755 | typedef KeyMintAidlTestBase EncryptionOperationsTest; |
| 4756 | |
| 4757 | /* |
| 4758 | * EncryptionOperationsTest.RsaNoPaddingSuccess |
| 4759 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4760 | * Verifies that raw RSA decryption works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4761 | */ |
| 4762 | TEST_P(EncryptionOperationsTest, RsaNoPaddingSuccess) { |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 4763 | for (uint64_t exponent : ValidExponents()) { |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4764 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4765 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4766 | .RsaEncryptionKey(2048, exponent) |
| 4767 | .Padding(PaddingMode::NONE) |
| 4768 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4769 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4770 | string message = string(2048 / 8, 'a'); |
| 4771 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4772 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4773 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4774 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4775 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4776 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4777 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4778 | // Unpadded RSA is deterministic |
| 4779 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 4780 | |
| 4781 | CheckedDeleteKey(); |
| 4782 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | /* |
| 4786 | * EncryptionOperationsTest.RsaNoPaddingShortMessage |
| 4787 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4788 | * Verifies that raw RSA decryption of short messages works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4789 | */ |
| 4790 | TEST_P(EncryptionOperationsTest, RsaNoPaddingShortMessage) { |
| 4791 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4792 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4793 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4794 | .Padding(PaddingMode::NONE) |
| 4795 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4796 | |
| 4797 | string message = "1"; |
| 4798 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 4799 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4800 | string ciphertext = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4801 | EXPECT_EQ(2048U / 8, ciphertext.size()); |
| 4802 | |
| 4803 | string expected_plaintext = string(2048U / 8 - 1, 0) + message; |
| 4804 | string plaintext = DecryptMessage(ciphertext, params); |
| 4805 | |
| 4806 | EXPECT_EQ(expected_plaintext, plaintext); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4810 | * EncryptionOperationsTest.RsaOaepSuccess |
| 4811 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4812 | * Verifies that RSA-OAEP decryption operations work, with all digests. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4813 | */ |
| 4814 | TEST_P(EncryptionOperationsTest, RsaOaepSuccess) { |
| 4815 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 4816 | |
| 4817 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4818 | ASSERT_EQ(ErrorCode::OK, |
| 4819 | GenerateKey(AuthorizationSetBuilder() |
| 4820 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4821 | .RsaEncryptionKey(key_size, 65537) |
| 4822 | .Padding(PaddingMode::RSA_OAEP) |
| 4823 | .Digest(digests) |
| 4824 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1) |
| 4825 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4826 | |
| 4827 | string message = "Hello"; |
| 4828 | |
| 4829 | for (auto digest : digests) { |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4830 | SCOPED_TRACE(testing::Message() << "digest-" << digest); |
| 4831 | |
| 4832 | auto params = AuthorizationSetBuilder() |
| 4833 | .Digest(digest) |
| 4834 | .Padding(PaddingMode::RSA_OAEP) |
| 4835 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1); |
| 4836 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4837 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 4838 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 4839 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4840 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4841 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 4842 | |
| 4843 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 4844 | // probability). |
| 4845 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4846 | |
| 4847 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 4848 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 4849 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 4850 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 4851 | |
| 4852 | // Decrypting corrupted ciphertext should fail. |
| 4853 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4854 | char corrupt_byte; |
| 4855 | do { |
| 4856 | corrupt_byte = static_cast<char>(random() % 256); |
| 4857 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4858 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4859 | |
| 4860 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4861 | string result; |
| 4862 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4863 | EXPECT_EQ(0U, result.size()); |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | * EncryptionOperationsTest.RsaOaepInvalidDigest |
| 4869 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4870 | * Verifies that RSA-OAEP decryption operations fail in the correct way when asked to operate |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4871 | * without a digest. |
| 4872 | */ |
| 4873 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidDigest) { |
| 4874 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4875 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4876 | .RsaEncryptionKey(2048, 65537) |
| 4877 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4878 | .Digest(Digest::NONE) |
| 4879 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4880 | |
| 4881 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_OAEP).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4882 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4886 | * EncryptionOperationsTest.RsaOaepInvalidPadding |
| 4887 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4888 | * Verifies that RSA-OAEP decryption operations fail in the correct way when asked to operate |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4889 | * with a padding value that is only suitable for signing/verifying. |
| 4890 | */ |
| 4891 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidPadding) { |
| 4892 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4893 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4894 | .RsaEncryptionKey(2048, 65537) |
| 4895 | .Padding(PaddingMode::RSA_PSS) |
| 4896 | .Digest(Digest::NONE) |
| 4897 | .SetDefaultValidity())); |
| 4898 | |
| 4899 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4900 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, Begin(KeyPurpose::DECRYPT, params)); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4904 | * EncryptionOperationsTest.RsaOaepDecryptWithWrongDigest |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4905 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4906 | * Verifies that RSA-OAEP decryption operations fail in the correct way when asked to decrypt |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4907 | * with a different digest than was used to encrypt. |
| 4908 | */ |
| 4909 | TEST_P(EncryptionOperationsTest, RsaOaepDecryptWithWrongDigest) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 4910 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 4911 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 4912 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4913 | |
| 4914 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4915 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4916 | .RsaEncryptionKey(1024, 65537) |
| 4917 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4918 | .Digest(Digest::SHA_2_224, Digest::SHA_2_256) |
| 4919 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4920 | string message = "Hello World!"; |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4921 | string ciphertext = LocalRsaEncryptMessage( |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4922 | message, |
| 4923 | AuthorizationSetBuilder().Digest(Digest::SHA_2_224).Padding(PaddingMode::RSA_OAEP)); |
| 4924 | |
| 4925 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 4926 | .Digest(Digest::SHA_2_256) |
| 4927 | .Padding(PaddingMode::RSA_OAEP))); |
| 4928 | string result; |
| 4929 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext, &result)); |
| 4930 | EXPECT_EQ(0U, result.size()); |
| 4931 | } |
| 4932 | |
| 4933 | /* |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4934 | * EncryptionOperationsTest.RsaOaepWithMGFDigestSuccess |
| 4935 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4936 | * Verifies that RSA-OAEP decryption operations work, with all SHA 256 digests and all type of MGF1 |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4937 | * digests. |
| 4938 | */ |
| 4939 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFDigestSuccess) { |
| 4940 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 4941 | |
| 4942 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
| 4943 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4944 | .OaepMGFDigest(digests) |
| 4945 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4946 | .RsaEncryptionKey(key_size, 65537) |
| 4947 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4948 | .Digest(Digest::SHA_2_256) |
| 4949 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4950 | |
| 4951 | string message = "Hello"; |
| 4952 | |
| 4953 | for (auto digest : digests) { |
| 4954 | auto params = AuthorizationSetBuilder() |
| 4955 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, digest) |
| 4956 | .Digest(Digest::SHA_2_256) |
| 4957 | .Padding(PaddingMode::RSA_OAEP); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4958 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4959 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 4960 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 4961 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4962 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4963 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 4964 | |
| 4965 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 4966 | // probability). |
| 4967 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4968 | |
| 4969 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 4970 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 4971 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 4972 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 4973 | |
| 4974 | // Decrypting corrupted ciphertext should fail. |
| 4975 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4976 | char corrupt_byte; |
| 4977 | do { |
| 4978 | corrupt_byte = static_cast<char>(random() % 256); |
| 4979 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4980 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4981 | |
| 4982 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4983 | string result; |
| 4984 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4985 | EXPECT_EQ(0U, result.size()); |
| 4986 | } |
| 4987 | } |
| 4988 | |
| 4989 | /* |
| 4990 | * EncryptionOperationsTest.RsaOaepWithMGFIncompatibleDigest |
| 4991 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4992 | * Verifies that RSA-OAEP decryption operations fail in the correct way when asked to operate |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4993 | * with incompatible MGF digest. |
| 4994 | */ |
| 4995 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFIncompatibleDigest) { |
| 4996 | ASSERT_EQ(ErrorCode::OK, |
| 4997 | GenerateKey(AuthorizationSetBuilder() |
| 4998 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 4999 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5000 | .RsaEncryptionKey(2048, 65537) |
| 5001 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 5002 | .Digest(Digest::SHA_2_256) |
| 5003 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 5004 | string message = "Hello World!"; |
| 5005 | |
| 5006 | auto params = AuthorizationSetBuilder() |
| 5007 | .Padding(PaddingMode::RSA_OAEP) |
| 5008 | .Digest(Digest::SHA_2_256) |
| 5009 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_224); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 5010 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | /* |
| 5014 | * EncryptionOperationsTest.RsaOaepWithMGFUnsupportedDigest |
| 5015 | * |
| 5016 | * Verifies that RSA-OAEP encryption operations fail in the correct way when asked to operate |
| 5017 | * with unsupported MGF digest. |
| 5018 | */ |
| 5019 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFUnsupportedDigest) { |
| 5020 | ASSERT_EQ(ErrorCode::OK, |
| 5021 | GenerateKey(AuthorizationSetBuilder() |
| 5022 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 5023 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5024 | .RsaEncryptionKey(2048, 65537) |
| 5025 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 5026 | .Digest(Digest::SHA_2_256) |
| 5027 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 5028 | string message = "Hello World!"; |
| 5029 | |
| 5030 | auto params = AuthorizationSetBuilder() |
| 5031 | .Padding(PaddingMode::RSA_OAEP) |
| 5032 | .Digest(Digest::SHA_2_256) |
| 5033 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 5034 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5038 | * EncryptionOperationsTest.RsaPkcs1Success |
| 5039 | * |
| 5040 | * Verifies that RSA PKCS encryption/decrypts works. |
| 5041 | */ |
| 5042 | TEST_P(EncryptionOperationsTest, RsaPkcs1Success) { |
| 5043 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5044 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5045 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 5046 | .Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT) |
| 5047 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5048 | |
| 5049 | string message = "Hello World!"; |
| 5050 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 5051 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5052 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
| 5053 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 5054 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5055 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
| 5056 | |
| 5057 | // PKCS1 v1.5 randomizes padding so every result should be different. |
| 5058 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5059 | |
| 5060 | string plaintext = DecryptMessage(ciphertext1, params); |
| 5061 | EXPECT_EQ(message, plaintext); |
| 5062 | |
| 5063 | // Decrypting corrupted ciphertext should fail. |
| 5064 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 5065 | char corrupt_byte; |
| 5066 | do { |
| 5067 | corrupt_byte = static_cast<char>(random() % 256); |
| 5068 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 5069 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 5070 | |
| 5071 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5072 | string result; |
| 5073 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 5074 | EXPECT_EQ(0U, result.size()); |
| 5075 | } |
| 5076 | |
| 5077 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5078 | * EncryptionOperationsTest.EcdsaEncrypt |
| 5079 | * |
| 5080 | * Verifies that attempting to use ECDSA keys to encrypt fails in the correct way. |
| 5081 | */ |
| 5082 | TEST_P(EncryptionOperationsTest, EcdsaEncrypt) { |
| 5083 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5084 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 5085 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 5086 | .Digest(Digest::NONE) |
| 5087 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5088 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 5089 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5090 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 5091 | } |
| 5092 | |
| 5093 | /* |
| 5094 | * EncryptionOperationsTest.HmacEncrypt |
| 5095 | * |
| 5096 | * Verifies that attempting to use HMAC keys to encrypt fails in the correct way. |
| 5097 | */ |
| 5098 | TEST_P(EncryptionOperationsTest, HmacEncrypt) { |
| 5099 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5100 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5101 | .HmacKey(128) |
| 5102 | .Digest(Digest::SHA_2_256) |
| 5103 | .Padding(PaddingMode::NONE) |
| 5104 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5105 | auto params = AuthorizationSetBuilder() |
| 5106 | .Digest(Digest::SHA_2_256) |
| 5107 | .Padding(PaddingMode::NONE) |
| 5108 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5109 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5110 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 5111 | } |
| 5112 | |
| 5113 | /* |
| 5114 | * EncryptionOperationsTest.AesEcbRoundTripSuccess |
| 5115 | * |
| 5116 | * Verifies that AES ECB mode works. |
| 5117 | */ |
| 5118 | TEST_P(EncryptionOperationsTest, AesEcbRoundTripSuccess) { |
| 5119 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5120 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5121 | .AesEncryptionKey(128) |
| 5122 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5123 | .Padding(PaddingMode::NONE))); |
| 5124 | |
| 5125 | ASSERT_GT(key_blob_.size(), 0U); |
| 5126 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 5127 | |
| 5128 | // Two-block message. |
| 5129 | string message = "12345678901234567890123456789012"; |
| 5130 | string ciphertext1 = EncryptMessage(message, params); |
| 5131 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5132 | |
| 5133 | string ciphertext2 = EncryptMessage(string(message), params); |
| 5134 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 5135 | |
| 5136 | // ECB is deterministic. |
| 5137 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 5138 | |
| 5139 | string plaintext = DecryptMessage(ciphertext1, params); |
| 5140 | EXPECT_EQ(message, plaintext); |
| 5141 | } |
| 5142 | |
| 5143 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5144 | * EncryptionOperationsTest.AesEcbUnknownTag |
| 5145 | * |
| 5146 | * Verifies that AES ECB operations ignore unknown tags. |
| 5147 | */ |
| 5148 | TEST_P(EncryptionOperationsTest, AesEcbUnknownTag) { |
| 5149 | int32_t unknown_tag_value = ((7 << 28) /* TagType:BOOL */ | 150); |
| 5150 | Tag unknown_tag = static_cast<Tag>(unknown_tag_value); |
| 5151 | KeyParameter unknown_param; |
| 5152 | unknown_param.tag = unknown_tag; |
| 5153 | |
| 5154 | vector<KeyCharacteristics> key_characteristics; |
| 5155 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5156 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5157 | .AesEncryptionKey(128) |
| 5158 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5159 | .Padding(PaddingMode::NONE) |
| 5160 | .Authorization(unknown_param), |
| 5161 | &key_blob_, &key_characteristics)); |
| 5162 | ASSERT_GT(key_blob_.size(), 0U); |
| 5163 | |
| 5164 | // Unknown tags should not be returned in key characteristics. |
| 5165 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 5166 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 5167 | EXPECT_EQ(hw_enforced.find(unknown_tag), -1); |
| 5168 | EXPECT_EQ(sw_enforced.find(unknown_tag), -1); |
| 5169 | |
| 5170 | // Encrypt without mentioning the unknown parameter. |
| 5171 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 5172 | string message = "12345678901234567890123456789012"; |
| 5173 | string ciphertext = EncryptMessage(message, params); |
| 5174 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 5175 | |
| 5176 | // Decrypt including the unknown parameter. |
| 5177 | auto decrypt_params = AuthorizationSetBuilder() |
| 5178 | .BlockMode(BlockMode::ECB) |
| 5179 | .Padding(PaddingMode::NONE) |
| 5180 | .Authorization(unknown_param); |
| 5181 | string plaintext = DecryptMessage(ciphertext, decrypt_params); |
| 5182 | EXPECT_EQ(message, plaintext); |
| 5183 | } |
| 5184 | |
| 5185 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 5186 | * EncryptionOperationsTest.AesWrongMode |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5187 | * |
| 5188 | * Verifies that AES encryption fails in the correct way when an unauthorized mode is specified. |
| 5189 | */ |
| 5190 | TEST_P(EncryptionOperationsTest, AesWrongMode) { |
| 5191 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5192 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5193 | .AesEncryptionKey(128) |
| 5194 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5195 | .Padding(PaddingMode::NONE))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5196 | ASSERT_GT(key_blob_.size(), 0U); |
| 5197 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5198 | EXPECT_EQ( |
| 5199 | ErrorCode::INCOMPATIBLE_BLOCK_MODE, |
| 5200 | Begin(KeyPurpose::ENCRYPT, |
| 5201 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE))); |
| 5202 | } |
| 5203 | |
| 5204 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5205 | * EncryptionOperationsTest.AesWrongPadding |
| 5206 | * |
| 5207 | * Verifies that AES encryption fails in the correct way when an unauthorized padding is specified. |
| 5208 | */ |
| 5209 | TEST_P(EncryptionOperationsTest, AesWrongPadding) { |
| 5210 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5211 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5212 | .AesEncryptionKey(128) |
| 5213 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5214 | .Padding(PaddingMode::NONE))); |
| 5215 | ASSERT_GT(key_blob_.size(), 0U); |
| 5216 | |
| 5217 | EXPECT_EQ( |
| 5218 | ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 5219 | Begin(KeyPurpose::ENCRYPT, |
| 5220 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7))); |
| 5221 | } |
| 5222 | |
| 5223 | /* |
| 5224 | * EncryptionOperationsTest.AesInvalidParams |
| 5225 | * |
| 5226 | * Verifies that AES encryption fails in the correct way when an duplicate parameters are specified. |
| 5227 | */ |
| 5228 | TEST_P(EncryptionOperationsTest, AesInvalidParams) { |
| 5229 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5230 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5231 | .AesEncryptionKey(128) |
| 5232 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5233 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5234 | .Padding(PaddingMode::NONE) |
| 5235 | .Padding(PaddingMode::PKCS7))); |
| 5236 | ASSERT_GT(key_blob_.size(), 0U); |
| 5237 | |
| 5238 | auto result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5239 | .BlockMode(BlockMode::CBC) |
| 5240 | .BlockMode(BlockMode::ECB) |
| 5241 | .Padding(PaddingMode::NONE)); |
| 5242 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_BLOCK_MODE || |
| 5243 | result == ErrorCode::UNSUPPORTED_BLOCK_MODE); |
| 5244 | |
| 5245 | result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5246 | .BlockMode(BlockMode::ECB) |
| 5247 | .Padding(PaddingMode::NONE) |
| 5248 | .Padding(PaddingMode::PKCS7)); |
| 5249 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
| 5250 | result == ErrorCode::UNSUPPORTED_PADDING_MODE); |
| 5251 | } |
| 5252 | |
| 5253 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5254 | * EncryptionOperationsTest.AesWrongPurpose |
| 5255 | * |
| 5256 | * Verifies that AES encryption fails in the correct way when an unauthorized purpose is |
| 5257 | * specified. |
| 5258 | */ |
| 5259 | TEST_P(EncryptionOperationsTest, AesWrongPurpose) { |
| 5260 | auto err = GenerateKey(AuthorizationSetBuilder() |
| 5261 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5262 | .AesKey(128) |
| 5263 | .Authorization(TAG_PURPOSE, KeyPurpose::ENCRYPT) |
| 5264 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5265 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5266 | .Padding(PaddingMode::NONE)); |
| 5267 | ASSERT_EQ(ErrorCode::OK, err) << "Got " << err; |
| 5268 | ASSERT_GT(key_blob_.size(), 0U); |
| 5269 | |
| 5270 | err = Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 5271 | .BlockMode(BlockMode::GCM) |
| 5272 | .Padding(PaddingMode::NONE) |
| 5273 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 5274 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 5275 | |
| 5276 | CheckedDeleteKey(); |
| 5277 | |
| 5278 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5279 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5280 | .AesKey(128) |
| 5281 | .Authorization(TAG_PURPOSE, KeyPurpose::DECRYPT) |
| 5282 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5283 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5284 | .Padding(PaddingMode::NONE))); |
| 5285 | |
| 5286 | err = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5287 | .BlockMode(BlockMode::GCM) |
| 5288 | .Padding(PaddingMode::NONE) |
| 5289 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 5290 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 5291 | } |
| 5292 | |
| 5293 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5294 | * EncryptionOperationsTest.AesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5295 | * |
| 5296 | * Verifies that AES encryption fails in the correct way when provided an input that is not a |
| 5297 | * multiple of the block size and no padding is specified. |
| 5298 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5299 | TEST_P(EncryptionOperationsTest, AesEcbCbcNoPaddingWrongInputSize) { |
| 5300 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 5301 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5302 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5303 | .AesEncryptionKey(128) |
| 5304 | .Authorization(TAG_BLOCK_MODE, blockMode) |
| 5305 | .Padding(PaddingMode::NONE))); |
| 5306 | // Message is slightly shorter than two blocks. |
| 5307 | string message(16 * 2 - 1, 'a'); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5308 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5309 | auto params = AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 5310 | AuthorizationSet out_params; |
| 5311 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 5312 | string ciphertext; |
| 5313 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &ciphertext)); |
| 5314 | EXPECT_EQ(0U, ciphertext.size()); |
| 5315 | |
| 5316 | CheckedDeleteKey(); |
| 5317 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | /* |
| 5321 | * EncryptionOperationsTest.AesEcbPkcs7Padding |
| 5322 | * |
| 5323 | * Verifies that AES PKCS7 padding works for any message length. |
| 5324 | */ |
| 5325 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7Padding) { |
| 5326 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5327 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5328 | .AesEncryptionKey(128) |
| 5329 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5330 | .Padding(PaddingMode::PKCS7))); |
| 5331 | |
| 5332 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5333 | |
| 5334 | // Try various message lengths; all should work. |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 5335 | for (size_t i = 0; i <= 48; i++) { |
| 5336 | SCOPED_TRACE(testing::Message() << "i = " << i); |
| 5337 | // Edge case: '\t' (0x09) is also a valid PKCS7 padding character. |
| 5338 | string message(i, '\t'); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5339 | string ciphertext = EncryptMessage(message, params); |
| 5340 | EXPECT_EQ(i + 16 - (i % 16), ciphertext.size()); |
| 5341 | string plaintext = DecryptMessage(ciphertext, params); |
| 5342 | EXPECT_EQ(message, plaintext); |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | /* |
| 5347 | * EncryptionOperationsTest.AesEcbWrongPadding |
| 5348 | * |
| 5349 | * Verifies that AES enryption fails in the correct way when an unauthorized padding mode is |
| 5350 | * specified. |
| 5351 | */ |
| 5352 | TEST_P(EncryptionOperationsTest, AesEcbWrongPadding) { |
| 5353 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5354 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5355 | .AesEncryptionKey(128) |
| 5356 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5357 | .Padding(PaddingMode::NONE))); |
| 5358 | |
| 5359 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5360 | |
| 5361 | // Try various message lengths; all should fail |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 5362 | for (size_t i = 0; i <= 48; i++) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5363 | string message(i, 'a'); |
| 5364 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | /* |
| 5369 | * EncryptionOperationsTest.AesEcbPkcs7PaddingCorrupted |
| 5370 | * |
| 5371 | * Verifies that AES decryption fails in the correct way when the padding is corrupted. |
| 5372 | */ |
| 5373 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7PaddingCorrupted) { |
| 5374 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5375 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5376 | .AesEncryptionKey(128) |
| 5377 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5378 | .Padding(PaddingMode::PKCS7))); |
| 5379 | |
| 5380 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5381 | |
| 5382 | string message = "a"; |
| 5383 | string ciphertext = EncryptMessage(message, params); |
| 5384 | EXPECT_EQ(16U, ciphertext.size()); |
| 5385 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5386 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 5387 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 5388 | ++ciphertext[ciphertext.size() / 2]; |
| 5389 | |
| 5390 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5391 | string plaintext; |
| 5392 | ErrorCode error = Finish(message, &plaintext); |
| 5393 | if (error == ErrorCode::INVALID_INPUT_LENGTH) { |
| 5394 | // This is the expected error, we can exit the test now. |
| 5395 | return; |
| 5396 | } else { |
| 5397 | // Very small chance we got valid decryption, so try again. |
| 5398 | ASSERT_EQ(error, ErrorCode::OK); |
| 5399 | } |
| 5400 | } |
| 5401 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | vector<uint8_t> CopyIv(const AuthorizationSet& set) { |
| 5405 | auto iv = set.GetTagValue(TAG_NONCE); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5406 | EXPECT_TRUE(iv); |
| 5407 | return iv->get(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /* |
| 5411 | * EncryptionOperationsTest.AesCtrRoundTripSuccess |
| 5412 | * |
| 5413 | * Verifies that AES CTR mode works. |
| 5414 | */ |
| 5415 | TEST_P(EncryptionOperationsTest, AesCtrRoundTripSuccess) { |
| 5416 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5417 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5418 | .AesEncryptionKey(128) |
| 5419 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5420 | .Padding(PaddingMode::NONE))); |
| 5421 | |
| 5422 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 5423 | |
| 5424 | string message = "123"; |
| 5425 | AuthorizationSet out_params; |
| 5426 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 5427 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 5428 | EXPECT_EQ(16U, iv1.size()); |
| 5429 | |
| 5430 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5431 | |
| 5432 | out_params.Clear(); |
| 5433 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 5434 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 5435 | EXPECT_EQ(16U, iv2.size()); |
| 5436 | |
| 5437 | // IVs should be random, so ciphertexts should differ. |
| 5438 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5439 | |
| 5440 | auto params_iv1 = |
| 5441 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv1); |
| 5442 | auto params_iv2 = |
| 5443 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv2); |
| 5444 | |
| 5445 | string plaintext = DecryptMessage(ciphertext1, params_iv1); |
| 5446 | EXPECT_EQ(message, plaintext); |
| 5447 | plaintext = DecryptMessage(ciphertext2, params_iv2); |
| 5448 | EXPECT_EQ(message, plaintext); |
| 5449 | |
| 5450 | // Using the wrong IV will result in a "valid" decryption, but the data will be garbage. |
| 5451 | plaintext = DecryptMessage(ciphertext1, params_iv2); |
| 5452 | EXPECT_NE(message, plaintext); |
| 5453 | plaintext = DecryptMessage(ciphertext2, params_iv1); |
| 5454 | EXPECT_NE(message, plaintext); |
| 5455 | } |
| 5456 | |
| 5457 | /* |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5458 | * EncryptionOperationsTest.AesEcbIncremental |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5459 | * |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5460 | * Verifies that AES works for ECB block mode, when provided data in various size increments. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5461 | */ |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5462 | TEST_P(EncryptionOperationsTest, AesEcbIncremental) { |
| 5463 | CheckAesIncrementalEncryptOperation(BlockMode::ECB, 240); |
| 5464 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5465 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5466 | /* |
| 5467 | * EncryptionOperationsTest.AesCbcIncremental |
| 5468 | * |
| 5469 | * Verifies that AES works for CBC block mode, when provided data in various size increments. |
| 5470 | */ |
| 5471 | TEST_P(EncryptionOperationsTest, AesCbcIncremental) { |
| 5472 | CheckAesIncrementalEncryptOperation(BlockMode::CBC, 240); |
| 5473 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5474 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5475 | /* |
| 5476 | * EncryptionOperationsTest.AesCtrIncremental |
| 5477 | * |
| 5478 | * Verifies that AES works for CTR block mode, when provided data in various size increments. |
| 5479 | */ |
| 5480 | TEST_P(EncryptionOperationsTest, AesCtrIncremental) { |
| 5481 | CheckAesIncrementalEncryptOperation(BlockMode::CTR, 240); |
| 5482 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5483 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 5484 | /* |
| 5485 | * EncryptionOperationsTest.AesGcmIncremental |
| 5486 | * |
| 5487 | * Verifies that AES works for GCM block mode, when provided data in various size increments. |
| 5488 | */ |
| 5489 | TEST_P(EncryptionOperationsTest, AesGcmIncremental) { |
| 5490 | CheckAesIncrementalEncryptOperation(BlockMode::GCM, 240); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | struct AesCtrSp80038aTestVector { |
| 5494 | const char* key; |
| 5495 | const char* nonce; |
| 5496 | const char* plaintext; |
| 5497 | const char* ciphertext; |
| 5498 | }; |
| 5499 | |
| 5500 | // These test vectors are taken from |
| 5501 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section F.5. |
| 5502 | static const AesCtrSp80038aTestVector kAesCtrSp80038aTestVectors[] = { |
| 5503 | // AES-128 |
| 5504 | { |
| 5505 | "2b7e151628aed2a6abf7158809cf4f3c", |
| 5506 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5507 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5508 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5509 | "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff" |
| 5510 | "5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee", |
| 5511 | }, |
| 5512 | // AES-192 |
| 5513 | { |
| 5514 | "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", |
| 5515 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5516 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5517 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5518 | "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94" |
| 5519 | "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050", |
| 5520 | }, |
| 5521 | // AES-256 |
| 5522 | { |
| 5523 | "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", |
| 5524 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5525 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5526 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5527 | "601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5" |
| 5528 | "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6", |
| 5529 | }, |
| 5530 | }; |
| 5531 | |
| 5532 | /* |
| 5533 | * EncryptionOperationsTest.AesCtrSp80038aTestVector |
| 5534 | * |
| 5535 | * Verifies AES CTR implementation against SP800-38A test vectors. |
| 5536 | */ |
| 5537 | TEST_P(EncryptionOperationsTest, AesCtrSp80038aTestVector) { |
| 5538 | std::vector<uint32_t> InvalidSizes = InvalidKeySizes(Algorithm::AES); |
| 5539 | for (size_t i = 0; i < 3; i++) { |
| 5540 | const AesCtrSp80038aTestVector& test(kAesCtrSp80038aTestVectors[i]); |
| 5541 | const string key = hex2str(test.key); |
| 5542 | if (std::find(InvalidSizes.begin(), InvalidSizes.end(), (key.size() * 8)) != |
| 5543 | InvalidSizes.end()) |
| 5544 | continue; |
| 5545 | const string nonce = hex2str(test.nonce); |
| 5546 | const string plaintext = hex2str(test.plaintext); |
| 5547 | const string ciphertext = hex2str(test.ciphertext); |
| 5548 | CheckAesCtrTestVector(key, nonce, plaintext, ciphertext); |
| 5549 | } |
| 5550 | } |
| 5551 | |
| 5552 | /* |
| 5553 | * EncryptionOperationsTest.AesCtrIncompatiblePaddingMode |
| 5554 | * |
| 5555 | * Verifies that keymint rejects use of CTR mode with PKCS7 padding in the correct way. |
| 5556 | */ |
| 5557 | TEST_P(EncryptionOperationsTest, AesCtrIncompatiblePaddingMode) { |
| 5558 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5559 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5560 | .AesEncryptionKey(128) |
| 5561 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5562 | .Padding(PaddingMode::PKCS7))); |
| 5563 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 5564 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5565 | } |
| 5566 | |
| 5567 | /* |
| 5568 | * EncryptionOperationsTest.AesCtrInvalidCallerNonce |
| 5569 | * |
| 5570 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 5571 | */ |
| 5572 | TEST_P(EncryptionOperationsTest, AesCtrInvalidCallerNonce) { |
| 5573 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5574 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5575 | .AesEncryptionKey(128) |
| 5576 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5577 | .Authorization(TAG_CALLER_NONCE) |
| 5578 | .Padding(PaddingMode::NONE))); |
| 5579 | |
| 5580 | auto params = AuthorizationSetBuilder() |
| 5581 | .BlockMode(BlockMode::CTR) |
| 5582 | .Padding(PaddingMode::NONE) |
| 5583 | .Authorization(TAG_NONCE, AidlBuf(string(1, 'a'))); |
| 5584 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5585 | |
| 5586 | params = AuthorizationSetBuilder() |
| 5587 | .BlockMode(BlockMode::CTR) |
| 5588 | .Padding(PaddingMode::NONE) |
| 5589 | .Authorization(TAG_NONCE, AidlBuf(string(15, 'a'))); |
| 5590 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5591 | |
| 5592 | params = AuthorizationSetBuilder() |
| 5593 | .BlockMode(BlockMode::CTR) |
| 5594 | .Padding(PaddingMode::NONE) |
| 5595 | .Authorization(TAG_NONCE, AidlBuf(string(17, 'a'))); |
| 5596 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5597 | } |
| 5598 | |
| 5599 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 5600 | * EncryptionOperationsTest.AesCbcRoundTripSuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5601 | * |
| 5602 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 5603 | */ |
| 5604 | TEST_P(EncryptionOperationsTest, AesCbcRoundTripSuccess) { |
| 5605 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5606 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5607 | .AesEncryptionKey(128) |
| 5608 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5609 | .Padding(PaddingMode::NONE))); |
| 5610 | // Two-block message. |
| 5611 | string message = "12345678901234567890123456789012"; |
| 5612 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5613 | AuthorizationSet out_params; |
| 5614 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 5615 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 5616 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5617 | |
| 5618 | out_params.Clear(); |
| 5619 | |
| 5620 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 5621 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 5622 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 5623 | |
| 5624 | // IVs should be random, so ciphertexts should differ. |
| 5625 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5626 | |
| 5627 | params.push_back(TAG_NONCE, iv1); |
| 5628 | string plaintext = DecryptMessage(ciphertext1, params); |
| 5629 | EXPECT_EQ(message, plaintext); |
| 5630 | } |
| 5631 | |
| 5632 | /* |
| 5633 | * EncryptionOperationsTest.AesCallerNonce |
| 5634 | * |
| 5635 | * Verifies that AES caller-provided nonces work correctly. |
| 5636 | */ |
| 5637 | TEST_P(EncryptionOperationsTest, AesCallerNonce) { |
| 5638 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5639 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5640 | .AesEncryptionKey(128) |
| 5641 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5642 | .Authorization(TAG_CALLER_NONCE) |
| 5643 | .Padding(PaddingMode::NONE))); |
| 5644 | |
| 5645 | string message = "12345678901234567890123456789012"; |
| 5646 | |
| 5647 | // Don't specify nonce, should get a random one. |
| 5648 | AuthorizationSetBuilder params = |
| 5649 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5650 | AuthorizationSet out_params; |
| 5651 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 5652 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5653 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5654 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5655 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5656 | string plaintext = DecryptMessage(ciphertext, params); |
| 5657 | EXPECT_EQ(message, plaintext); |
| 5658 | |
| 5659 | // Now specify a nonce, should also work. |
| 5660 | params = AuthorizationSetBuilder() |
| 5661 | .BlockMode(BlockMode::CBC) |
| 5662 | .Padding(PaddingMode::NONE) |
| 5663 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 5664 | out_params.Clear(); |
| 5665 | ciphertext = EncryptMessage(message, params, &out_params); |
| 5666 | |
| 5667 | // Decrypt with correct nonce. |
| 5668 | plaintext = DecryptMessage(ciphertext, params); |
| 5669 | EXPECT_EQ(message, plaintext); |
| 5670 | |
| 5671 | // Try with wrong nonce. |
| 5672 | params = AuthorizationSetBuilder() |
| 5673 | .BlockMode(BlockMode::CBC) |
| 5674 | .Padding(PaddingMode::NONE) |
| 5675 | .Authorization(TAG_NONCE, AidlBuf("aaaaaaaaaaaaaaaa")); |
| 5676 | plaintext = DecryptMessage(ciphertext, params); |
| 5677 | EXPECT_NE(message, plaintext); |
| 5678 | } |
| 5679 | |
| 5680 | /* |
| 5681 | * EncryptionOperationsTest.AesCallerNonceProhibited |
| 5682 | * |
| 5683 | * Verifies that caller-provided nonces are not permitted when not specified in the key |
| 5684 | * authorizations. |
| 5685 | */ |
| 5686 | TEST_P(EncryptionOperationsTest, AesCallerNonceProhibited) { |
| 5687 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5688 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5689 | .AesEncryptionKey(128) |
| 5690 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5691 | .Padding(PaddingMode::NONE))); |
| 5692 | |
| 5693 | string message = "12345678901234567890123456789012"; |
| 5694 | |
| 5695 | // Don't specify nonce, should get a random one. |
| 5696 | AuthorizationSetBuilder params = |
| 5697 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5698 | AuthorizationSet out_params; |
| 5699 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 5700 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5701 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5702 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5703 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5704 | string plaintext = DecryptMessage(ciphertext, params); |
| 5705 | EXPECT_EQ(message, plaintext); |
| 5706 | |
| 5707 | // Now specify a nonce, should fail |
| 5708 | params = AuthorizationSetBuilder() |
| 5709 | .BlockMode(BlockMode::CBC) |
| 5710 | .Padding(PaddingMode::NONE) |
| 5711 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 5712 | out_params.Clear(); |
| 5713 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 5714 | } |
| 5715 | |
| 5716 | /* |
| 5717 | * EncryptionOperationsTest.AesGcmRoundTripSuccess |
| 5718 | * |
| 5719 | * Verifies that AES GCM mode works. |
| 5720 | */ |
| 5721 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripSuccess) { |
| 5722 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5723 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5724 | .AesEncryptionKey(128) |
| 5725 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5726 | .Padding(PaddingMode::NONE) |
| 5727 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5728 | |
| 5729 | string aad = "foobar"; |
| 5730 | string message = "123456789012345678901234567890123456"; |
| 5731 | |
| 5732 | auto begin_params = AuthorizationSetBuilder() |
| 5733 | .BlockMode(BlockMode::GCM) |
| 5734 | .Padding(PaddingMode::NONE) |
| 5735 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5736 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5737 | // Encrypt |
| 5738 | AuthorizationSet begin_out_params; |
| 5739 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 5740 | << "Begin encrypt"; |
| 5741 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5742 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5743 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5744 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 5745 | |
| 5746 | // Grab nonce |
| 5747 | begin_params.push_back(begin_out_params); |
| 5748 | |
| 5749 | // Decrypt. |
| 5750 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5751 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5752 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5753 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5754 | EXPECT_EQ(message.length(), plaintext.length()); |
| 5755 | EXPECT_EQ(message, plaintext); |
| 5756 | } |
| 5757 | |
| 5758 | /* |
| 5759 | * EncryptionOperationsTest.AesGcmRoundTripWithDelaySuccess |
| 5760 | * |
| 5761 | * Verifies that AES GCM mode works, even when there's a long delay |
| 5762 | * between operations. |
| 5763 | */ |
| 5764 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripWithDelaySuccess) { |
| 5765 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5766 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5767 | .AesEncryptionKey(128) |
| 5768 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5769 | .Padding(PaddingMode::NONE) |
| 5770 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5771 | |
| 5772 | string aad = "foobar"; |
| 5773 | string message = "123456789012345678901234567890123456"; |
| 5774 | |
| 5775 | auto begin_params = AuthorizationSetBuilder() |
| 5776 | .BlockMode(BlockMode::GCM) |
| 5777 | .Padding(PaddingMode::NONE) |
| 5778 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5779 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5780 | // Encrypt |
| 5781 | AuthorizationSet begin_out_params; |
| 5782 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 5783 | << "Begin encrypt"; |
| 5784 | string ciphertext; |
| 5785 | AuthorizationSet update_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5786 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5787 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5788 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5789 | |
| 5790 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 5791 | |
| 5792 | // Grab nonce |
| 5793 | begin_params.push_back(begin_out_params); |
| 5794 | |
| 5795 | // Decrypt. |
| 5796 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
| 5797 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5798 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5799 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5800 | ASSERT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5801 | sleep(5); |
| 5802 | EXPECT_EQ(ErrorCode::OK, Finish("", &plaintext)); |
| 5803 | EXPECT_EQ(message.length(), plaintext.length()); |
| 5804 | EXPECT_EQ(message, plaintext); |
| 5805 | } |
| 5806 | |
| 5807 | /* |
| 5808 | * EncryptionOperationsTest.AesGcmDifferentNonces |
| 5809 | * |
| 5810 | * Verifies that encrypting the same data with different nonces produces different outputs. |
| 5811 | */ |
| 5812 | TEST_P(EncryptionOperationsTest, AesGcmDifferentNonces) { |
| 5813 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5814 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5815 | .AesEncryptionKey(128) |
| 5816 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5817 | .Padding(PaddingMode::NONE) |
| 5818 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5819 | .Authorization(TAG_CALLER_NONCE))); |
| 5820 | |
| 5821 | string aad = "foobar"; |
| 5822 | string message = "123456789012345678901234567890123456"; |
| 5823 | string nonce1 = "000000000000"; |
| 5824 | string nonce2 = "111111111111"; |
| 5825 | string nonce3 = "222222222222"; |
| 5826 | |
| 5827 | string ciphertext1 = |
| 5828 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce1)); |
| 5829 | string ciphertext2 = |
| 5830 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce2)); |
| 5831 | string ciphertext3 = |
| 5832 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce3)); |
| 5833 | |
| 5834 | ASSERT_NE(ciphertext1, ciphertext2); |
| 5835 | ASSERT_NE(ciphertext1, ciphertext3); |
| 5836 | ASSERT_NE(ciphertext2, ciphertext3); |
| 5837 | } |
| 5838 | |
| 5839 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5840 | * EncryptionOperationsTest.AesGcmDifferentAutoNonces |
| 5841 | * |
| 5842 | * Verifies that encrypting the same data with KeyMint generated nonces produces different outputs. |
| 5843 | */ |
| 5844 | TEST_P(EncryptionOperationsTest, AesGcmDifferentAutoNonces) { |
| 5845 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5846 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5847 | .AesEncryptionKey(128) |
| 5848 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5849 | .Padding(PaddingMode::NONE) |
| 5850 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5851 | |
| 5852 | string aad = "foobar"; |
| 5853 | string message = "123456789012345678901234567890123456"; |
| 5854 | |
| 5855 | string ciphertext1 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5856 | string ciphertext2 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5857 | string ciphertext3 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5858 | |
| 5859 | ASSERT_NE(ciphertext1, ciphertext2); |
| 5860 | ASSERT_NE(ciphertext1, ciphertext3); |
| 5861 | ASSERT_NE(ciphertext2, ciphertext3); |
| 5862 | } |
| 5863 | |
| 5864 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5865 | * EncryptionOperationsTest.AesGcmTooShortTag |
| 5866 | * |
| 5867 | * Verifies that AES GCM mode fails correctly when a too-short tag length is specified. |
| 5868 | */ |
| 5869 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTag) { |
| 5870 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5871 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5872 | .AesEncryptionKey(128) |
| 5873 | .BlockMode(BlockMode::GCM) |
| 5874 | .Padding(PaddingMode::NONE) |
| 5875 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5876 | string message = "123456789012345678901234567890123456"; |
| 5877 | auto params = AuthorizationSetBuilder() |
| 5878 | .BlockMode(BlockMode::GCM) |
| 5879 | .Padding(PaddingMode::NONE) |
| 5880 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5881 | |
| 5882 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::ENCRYPT, params)); |
| 5883 | } |
| 5884 | |
| 5885 | /* |
| 5886 | * EncryptionOperationsTest.AesGcmTooShortTagOnDecrypt |
| 5887 | * |
| 5888 | * Verifies that AES GCM mode fails correctly when a too-short tag is provided to decryption. |
| 5889 | */ |
| 5890 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTagOnDecrypt) { |
| 5891 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5892 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5893 | .AesEncryptionKey(128) |
| 5894 | .BlockMode(BlockMode::GCM) |
| 5895 | .Padding(PaddingMode::NONE) |
| 5896 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5897 | string aad = "foobar"; |
| 5898 | string message = "123456789012345678901234567890123456"; |
| 5899 | auto params = AuthorizationSetBuilder() |
| 5900 | .BlockMode(BlockMode::GCM) |
| 5901 | .Padding(PaddingMode::NONE) |
| 5902 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5903 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5904 | // Encrypt |
| 5905 | AuthorizationSet begin_out_params; |
| 5906 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 5907 | EXPECT_EQ(1U, begin_out_params.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5908 | ASSERT_TRUE(begin_out_params.GetTagValue(TAG_NONCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5909 | |
| 5910 | AuthorizationSet finish_out_params; |
| 5911 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5912 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5913 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5914 | |
| 5915 | params = AuthorizationSetBuilder() |
| 5916 | .Authorizations(begin_out_params) |
| 5917 | .BlockMode(BlockMode::GCM) |
| 5918 | .Padding(PaddingMode::NONE) |
| 5919 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5920 | |
| 5921 | // Decrypt. |
| 5922 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::DECRYPT, params)); |
| 5923 | } |
| 5924 | |
| 5925 | /* |
| 5926 | * EncryptionOperationsTest.AesGcmCorruptKey |
| 5927 | * |
| 5928 | * Verifies that AES GCM mode fails correctly when the decryption key is incorrect. |
| 5929 | */ |
| 5930 | TEST_P(EncryptionOperationsTest, AesGcmCorruptKey) { |
| 5931 | const uint8_t nonce_bytes[] = { |
| 5932 | 0xb7, 0x94, 0x37, 0xae, 0x08, 0xff, 0x35, 0x5d, 0x7d, 0x8a, 0x4d, 0x0f, |
| 5933 | }; |
| 5934 | string nonce = make_string(nonce_bytes); |
| 5935 | const uint8_t ciphertext_bytes[] = { |
| 5936 | 0xb3, 0xf6, 0x79, 0x9e, 0x8f, 0x93, 0x26, 0xf2, 0xdf, 0x1e, 0x80, 0xfc, |
| 5937 | 0xd2, 0xcb, 0x16, 0xd7, 0x8c, 0x9d, 0xc7, 0xcc, 0x14, 0xbb, 0x67, 0x78, |
| 5938 | 0x62, 0xdc, 0x6c, 0x63, 0x9b, 0x3a, 0x63, 0x38, 0xd2, 0x4b, 0x31, 0x2d, |
| 5939 | 0x39, 0x89, 0xe5, 0x92, 0x0b, 0x5d, 0xbf, 0xc9, 0x76, 0x76, 0x5e, 0xfb, |
| 5940 | 0xfe, 0x57, 0xbb, 0x38, 0x59, 0x40, 0xa7, 0xa4, 0x3b, 0xdf, 0x05, 0xbd, |
| 5941 | 0xda, 0xe3, 0xc9, 0xd6, 0xa2, 0xfb, 0xbd, 0xfc, 0xc0, 0xcb, 0xa0, |
| 5942 | }; |
| 5943 | string ciphertext = make_string(ciphertext_bytes); |
| 5944 | |
| 5945 | auto params = AuthorizationSetBuilder() |
| 5946 | .BlockMode(BlockMode::GCM) |
| 5947 | .Padding(PaddingMode::NONE) |
| 5948 | .Authorization(TAG_MAC_LENGTH, 128) |
| 5949 | .Authorization(TAG_NONCE, nonce.data(), nonce.size()); |
| 5950 | |
| 5951 | auto import_params = AuthorizationSetBuilder() |
| 5952 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5953 | .AesEncryptionKey(128) |
| 5954 | .BlockMode(BlockMode::GCM) |
| 5955 | .Padding(PaddingMode::NONE) |
| 5956 | .Authorization(TAG_CALLER_NONCE) |
| 5957 | .Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 5958 | |
| 5959 | // Import correct key and decrypt |
| 5960 | const uint8_t key_bytes[] = { |
| 5961 | 0xba, 0x76, 0x35, 0x4f, 0x0a, 0xed, 0x6e, 0x8d, |
| 5962 | 0x91, 0xf4, 0x5c, 0x4f, 0xf5, 0xa0, 0x62, 0xdb, |
| 5963 | }; |
| 5964 | string key = make_string(key_bytes); |
| 5965 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5966 | string plaintext = DecryptMessage(ciphertext, params); |
| 5967 | CheckedDeleteKey(); |
| 5968 | |
| 5969 | // Corrupt key and attempt to decrypt |
| 5970 | key[0] = 0; |
| 5971 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5972 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5973 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
| 5974 | CheckedDeleteKey(); |
| 5975 | } |
| 5976 | |
| 5977 | /* |
| 5978 | * EncryptionOperationsTest.AesGcmAadNoData |
| 5979 | * |
| 5980 | * Verifies that AES GCM mode works when provided additional authenticated data, but no data to |
| 5981 | * encrypt. |
| 5982 | */ |
| 5983 | TEST_P(EncryptionOperationsTest, AesGcmAadNoData) { |
| 5984 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5985 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5986 | .AesEncryptionKey(128) |
| 5987 | .BlockMode(BlockMode::GCM) |
| 5988 | .Padding(PaddingMode::NONE) |
| 5989 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5990 | |
| 5991 | string aad = "1234567890123456"; |
| 5992 | auto params = AuthorizationSetBuilder() |
| 5993 | .BlockMode(BlockMode::GCM) |
| 5994 | .Padding(PaddingMode::NONE) |
| 5995 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5996 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5997 | // Encrypt |
| 5998 | AuthorizationSet begin_out_params; |
| 5999 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 6000 | string ciphertext; |
| 6001 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6002 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 6003 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6004 | EXPECT_TRUE(finish_out_params.empty()); |
| 6005 | |
| 6006 | // Grab nonce |
| 6007 | params.push_back(begin_out_params); |
| 6008 | |
| 6009 | // Decrypt. |
| 6010 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6011 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6012 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6013 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6014 | |
| 6015 | EXPECT_TRUE(finish_out_params.empty()); |
| 6016 | |
| 6017 | EXPECT_EQ("", plaintext); |
| 6018 | } |
| 6019 | |
| 6020 | /* |
| 6021 | * EncryptionOperationsTest.AesGcmMultiPartAad |
| 6022 | * |
| 6023 | * Verifies that AES GCM mode works when provided additional authenticated data in multiple |
| 6024 | * chunks. |
| 6025 | */ |
| 6026 | TEST_P(EncryptionOperationsTest, AesGcmMultiPartAad) { |
| 6027 | const size_t tag_bits = 128; |
| 6028 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6029 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6030 | .AesEncryptionKey(128) |
| 6031 | .BlockMode(BlockMode::GCM) |
| 6032 | .Padding(PaddingMode::NONE) |
| 6033 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6034 | |
| 6035 | string message = "123456789012345678901234567890123456"; |
| 6036 | auto begin_params = AuthorizationSetBuilder() |
| 6037 | .BlockMode(BlockMode::GCM) |
| 6038 | .Padding(PaddingMode::NONE) |
| 6039 | .Authorization(TAG_MAC_LENGTH, tag_bits); |
| 6040 | AuthorizationSet begin_out_params; |
| 6041 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6042 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 6043 | |
| 6044 | // No data, AAD only. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6045 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
| 6046 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6047 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6048 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 6049 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6050 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6051 | // Expect 128-bit (16-byte) tag appended to ciphertext. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6052 | EXPECT_EQ(message.size() + (tag_bits / 8), ciphertext.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6053 | |
| 6054 | // Grab nonce. |
| 6055 | begin_params.push_back(begin_out_params); |
| 6056 | |
| 6057 | // Decrypt |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6058 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6059 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foofoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6060 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6061 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6062 | EXPECT_EQ(message, plaintext); |
| 6063 | } |
| 6064 | |
| 6065 | /* |
| 6066 | * EncryptionOperationsTest.AesGcmAadOutOfOrder |
| 6067 | * |
| 6068 | * Verifies that AES GCM mode fails correctly when given AAD after data to encipher. |
| 6069 | */ |
| 6070 | TEST_P(EncryptionOperationsTest, AesGcmAadOutOfOrder) { |
| 6071 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6072 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6073 | .AesEncryptionKey(128) |
| 6074 | .BlockMode(BlockMode::GCM) |
| 6075 | .Padding(PaddingMode::NONE) |
| 6076 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6077 | |
| 6078 | string message = "123456789012345678901234567890123456"; |
| 6079 | auto begin_params = AuthorizationSetBuilder() |
| 6080 | .BlockMode(BlockMode::GCM) |
| 6081 | .Padding(PaddingMode::NONE) |
| 6082 | .Authorization(TAG_MAC_LENGTH, 128); |
| 6083 | AuthorizationSet begin_out_params; |
| 6084 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6085 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 6086 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6087 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6088 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6089 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 6090 | EXPECT_EQ(ErrorCode::INVALID_TAG, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6091 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6092 | // The failure should have already cancelled the operation. |
| 6093 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 6094 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6095 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | /* |
| 6099 | * EncryptionOperationsTest.AesGcmBadAad |
| 6100 | * |
| 6101 | * Verifies that AES GCM decryption fails correctly when additional authenticated date is wrong. |
| 6102 | */ |
| 6103 | TEST_P(EncryptionOperationsTest, AesGcmBadAad) { |
| 6104 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6105 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6106 | .AesEncryptionKey(128) |
| 6107 | .BlockMode(BlockMode::GCM) |
| 6108 | .Padding(PaddingMode::NONE) |
| 6109 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6110 | |
| 6111 | string message = "12345678901234567890123456789012"; |
| 6112 | auto begin_params = AuthorizationSetBuilder() |
| 6113 | .BlockMode(BlockMode::GCM) |
| 6114 | .Padding(PaddingMode::NONE) |
| 6115 | .Authorization(TAG_MAC_LENGTH, 128); |
| 6116 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6117 | // Encrypt |
| 6118 | AuthorizationSet begin_out_params; |
| 6119 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6120 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6121 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6122 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6123 | |
| 6124 | // Grab nonce |
| 6125 | begin_params.push_back(begin_out_params); |
| 6126 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6127 | // Decrypt. |
| 6128 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6129 | EXPECT_EQ(ErrorCode::OK, UpdateAad("barfoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6130 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6131 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | /* |
| 6135 | * EncryptionOperationsTest.AesGcmWrongNonce |
| 6136 | * |
| 6137 | * Verifies that AES GCM decryption fails correctly when the nonce is incorrect. |
| 6138 | */ |
| 6139 | TEST_P(EncryptionOperationsTest, AesGcmWrongNonce) { |
| 6140 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6141 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6142 | .AesEncryptionKey(128) |
| 6143 | .BlockMode(BlockMode::GCM) |
| 6144 | .Padding(PaddingMode::NONE) |
| 6145 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6146 | |
| 6147 | string message = "12345678901234567890123456789012"; |
| 6148 | auto begin_params = AuthorizationSetBuilder() |
| 6149 | .BlockMode(BlockMode::GCM) |
| 6150 | .Padding(PaddingMode::NONE) |
| 6151 | .Authorization(TAG_MAC_LENGTH, 128); |
| 6152 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6153 | // Encrypt |
| 6154 | AuthorizationSet begin_out_params; |
| 6155 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6156 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6157 | string ciphertext; |
| 6158 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6159 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6160 | |
| 6161 | // Wrong nonce |
| 6162 | begin_params.push_back(TAG_NONCE, AidlBuf("123456789012")); |
| 6163 | |
| 6164 | // Decrypt. |
| 6165 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6166 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6167 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6168 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6169 | |
| 6170 | // With wrong nonce, should have gotten garbage plaintext (or none). |
| 6171 | EXPECT_NE(message, plaintext); |
| 6172 | } |
| 6173 | |
| 6174 | /* |
| 6175 | * EncryptionOperationsTest.AesGcmCorruptTag |
| 6176 | * |
| 6177 | * Verifies that AES GCM decryption fails correctly when the tag is wrong. |
| 6178 | */ |
| 6179 | TEST_P(EncryptionOperationsTest, AesGcmCorruptTag) { |
| 6180 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6181 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6182 | .AesEncryptionKey(128) |
| 6183 | .BlockMode(BlockMode::GCM) |
| 6184 | .Padding(PaddingMode::NONE) |
| 6185 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6186 | |
| 6187 | string aad = "1234567890123456"; |
| 6188 | string message = "123456789012345678901234567890123456"; |
| 6189 | |
| 6190 | auto params = AuthorizationSetBuilder() |
| 6191 | .BlockMode(BlockMode::GCM) |
| 6192 | .Padding(PaddingMode::NONE) |
| 6193 | .Authorization(TAG_MAC_LENGTH, 128); |
| 6194 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6195 | // Encrypt |
| 6196 | AuthorizationSet begin_out_params; |
| 6197 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6198 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6199 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6200 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6201 | |
| 6202 | // Corrupt tag |
| 6203 | ++(*ciphertext.rbegin()); |
| 6204 | |
| 6205 | // Grab nonce |
| 6206 | params.push_back(begin_out_params); |
| 6207 | |
| 6208 | // Decrypt. |
| 6209 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6210 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6211 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6212 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | /* |
| 6216 | * EncryptionOperationsTest.TripleDesEcbRoundTripSuccess |
| 6217 | * |
| 6218 | * Verifies that 3DES is basically functional. |
| 6219 | */ |
| 6220 | TEST_P(EncryptionOperationsTest, TripleDesEcbRoundTripSuccess) { |
| 6221 | auto auths = AuthorizationSetBuilder() |
| 6222 | .TripleDesEncryptionKey(168) |
| 6223 | .BlockMode(BlockMode::ECB) |
| 6224 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6225 | .Padding(PaddingMode::NONE); |
| 6226 | |
| 6227 | ASSERT_EQ(ErrorCode::OK, GenerateKey(auths)); |
| 6228 | // Two-block message. |
| 6229 | string message = "1234567890123456"; |
| 6230 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 6231 | string ciphertext1 = EncryptMessage(message, inParams); |
| 6232 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6233 | |
| 6234 | string ciphertext2 = EncryptMessage(string(message), inParams); |
| 6235 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 6236 | |
| 6237 | // ECB is deterministic. |
| 6238 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 6239 | |
| 6240 | string plaintext = DecryptMessage(ciphertext1, inParams); |
| 6241 | EXPECT_EQ(message, plaintext); |
| 6242 | } |
| 6243 | |
| 6244 | /* |
| 6245 | * EncryptionOperationsTest.TripleDesEcbNotAuthorized |
| 6246 | * |
| 6247 | * Verifies that CBC keys reject ECB usage. |
| 6248 | */ |
| 6249 | TEST_P(EncryptionOperationsTest, TripleDesEcbNotAuthorized) { |
| 6250 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6251 | .TripleDesEncryptionKey(168) |
| 6252 | .BlockMode(BlockMode::CBC) |
| 6253 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6254 | .Padding(PaddingMode::NONE))); |
| 6255 | |
| 6256 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 6257 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
| 6258 | } |
| 6259 | |
| 6260 | /* |
| 6261 | * EncryptionOperationsTest.TripleDesEcbPkcs7Padding |
| 6262 | * |
| 6263 | * Tests ECB mode with PKCS#7 padding, various message sizes. |
| 6264 | */ |
| 6265 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7Padding) { |
| 6266 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6267 | .TripleDesEncryptionKey(168) |
| 6268 | .BlockMode(BlockMode::ECB) |
| 6269 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6270 | .Padding(PaddingMode::PKCS7))); |
| 6271 | |
| 6272 | for (size_t i = 0; i < 32; ++i) { |
| 6273 | string message(i, 'a'); |
| 6274 | auto inParams = |
| 6275 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 6276 | string ciphertext = EncryptMessage(message, inParams); |
| 6277 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 6278 | string plaintext = DecryptMessage(ciphertext, inParams); |
| 6279 | EXPECT_EQ(message, plaintext); |
| 6280 | } |
| 6281 | } |
| 6282 | |
| 6283 | /* |
| 6284 | * EncryptionOperationsTest.TripleDesEcbNoPaddingKeyWithPkcs7Padding |
| 6285 | * |
| 6286 | * Verifies that keys configured for no padding reject PKCS7 padding |
| 6287 | */ |
| 6288 | TEST_P(EncryptionOperationsTest, TripleDesEcbNoPaddingKeyWithPkcs7Padding) { |
| 6289 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6290 | .TripleDesEncryptionKey(168) |
| 6291 | .BlockMode(BlockMode::ECB) |
| 6292 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6293 | .Padding(PaddingMode::NONE))); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 6294 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 6295 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6296 | } |
| 6297 | |
| 6298 | /* |
| 6299 | * EncryptionOperationsTest.TripleDesEcbPkcs7PaddingCorrupted |
| 6300 | * |
| 6301 | * Verifies that corrupted padding is detected. |
| 6302 | */ |
| 6303 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7PaddingCorrupted) { |
| 6304 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6305 | .TripleDesEncryptionKey(168) |
| 6306 | .BlockMode(BlockMode::ECB) |
| 6307 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6308 | .Padding(PaddingMode::PKCS7))); |
| 6309 | |
| 6310 | string message = "a"; |
| 6311 | string ciphertext = EncryptMessage(message, BlockMode::ECB, PaddingMode::PKCS7); |
| 6312 | EXPECT_EQ(8U, ciphertext.size()); |
| 6313 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6314 | |
| 6315 | AuthorizationSetBuilder begin_params; |
| 6316 | begin_params.push_back(TAG_BLOCK_MODE, BlockMode::ECB); |
| 6317 | begin_params.push_back(TAG_PADDING, PaddingMode::PKCS7); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 6318 | |
| 6319 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 6320 | ++ciphertext[ciphertext.size() / 2]; |
| 6321 | |
| 6322 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 6323 | string plaintext; |
| 6324 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 6325 | ErrorCode error = Finish(&plaintext); |
| 6326 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 6327 | // This is the expected error, we can exit the test now. |
| 6328 | return; |
| 6329 | } else { |
| 6330 | // Very small chance we got valid decryption, so try again. |
| 6331 | ASSERT_EQ(error, ErrorCode::OK); |
| 6332 | } |
| 6333 | } |
| 6334 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | struct TripleDesTestVector { |
| 6338 | const char* name; |
| 6339 | const KeyPurpose purpose; |
| 6340 | const BlockMode block_mode; |
| 6341 | const PaddingMode padding_mode; |
| 6342 | const char* key; |
| 6343 | const char* iv; |
| 6344 | const char* input; |
| 6345 | const char* output; |
| 6346 | }; |
| 6347 | |
| 6348 | // These test vectors are from NIST CAVP, plus a few custom variants to test padding, since all |
| 6349 | // of the NIST vectors are multiples of the block size. |
| 6350 | static const TripleDesTestVector kTripleDesTestVectors[] = { |
| 6351 | { |
| 6352 | "TECBMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6353 | "a2b5bc67da13dc92cd9d344aa238544a0e1fa79ef76810cd", // key |
| 6354 | "", // IV |
| 6355 | "329d86bdf1bc5af4", // input |
| 6356 | "d946c2756d78633f", // output |
| 6357 | }, |
| 6358 | { |
| 6359 | "TECBMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6360 | "49e692290d2a5e46bace79b9648a4c5d491004c262dc9d49", // key |
| 6361 | "", // IV |
| 6362 | "6b1540781b01ce1997adae102dbf3c5b", // input |
| 6363 | "4d0dc182d6e481ac4a3dc6ab6976ccae", // output |
| 6364 | }, |
| 6365 | { |
| 6366 | "TECBMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6367 | "52daec2ac7dc1958377392682f37860b2cc1ea2304bab0e9", // key |
| 6368 | "", // IV |
| 6369 | "6daad94ce08acfe7", // input |
| 6370 | "660e7d32dcc90e79", // output |
| 6371 | }, |
| 6372 | { |
| 6373 | "TECBMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6374 | "7f8fe3d3f4a48394fb682c2919926d6ddfce8932529229ce", // key |
| 6375 | "", // IV |
| 6376 | "e9653a0a1f05d31b9acd12d73aa9879d", // input |
| 6377 | "9b2ae9d998efe62f1b592e7e1df8ff38", // output |
| 6378 | }, |
| 6379 | { |
| 6380 | "TCBCMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6381 | "b5cb1504802326c73df186e3e352a20de643b0d63ee30e37", // key |
| 6382 | "43f791134c5647ba", // IV |
| 6383 | "dcc153cef81d6f24", // input |
| 6384 | "92538bd8af18d3ba", // output |
| 6385 | }, |
| 6386 | { |
| 6387 | "TCBCMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6388 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6389 | "c2e999cb6249023c", // IV |
| 6390 | "c689aee38a301bb316da75db36f110b5", // input |
| 6391 | "e9afaba5ec75ea1bbe65506655bb4ecb", // output |
| 6392 | }, |
| 6393 | { |
| 6394 | "TCBCMMT3 Encrypt 1 PKCS7 variant", KeyPurpose::ENCRYPT, BlockMode::CBC, |
| 6395 | PaddingMode::PKCS7, |
| 6396 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6397 | "c2e999cb6249023c", // IV |
| 6398 | "c689aee38a301bb316da75db36f110b500", // input |
| 6399 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // output |
| 6400 | }, |
| 6401 | { |
| 6402 | "TCBCMMT3 Encrypt 1 PKCS7 decrypted", KeyPurpose::DECRYPT, BlockMode::CBC, |
| 6403 | PaddingMode::PKCS7, |
| 6404 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6405 | "c2e999cb6249023c", // IV |
| 6406 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // input |
| 6407 | "c689aee38a301bb316da75db36f110b500", // output |
| 6408 | }, |
| 6409 | { |
| 6410 | "TCBCMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6411 | "5eb6040d46082c7aa7d06dfd08dfeac8c18364c1548c3ba1", // key |
| 6412 | "41746c7e442d3681", // IV |
| 6413 | "c53a7b0ec40600fe", // input |
| 6414 | "d4f00eb455de1034", // output |
| 6415 | }, |
| 6416 | { |
| 6417 | "TCBCMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6418 | "5b1cce7c0dc1ec49130dfb4af45785ab9179e567f2c7d549", // key |
| 6419 | "3982bc02c3727d45", // IV |
| 6420 | "6006f10adef52991fcc777a1238bbb65", // input |
| 6421 | "edae09288e9e3bc05746d872b48e3b29", // output |
| 6422 | }, |
| 6423 | }; |
| 6424 | |
| 6425 | /* |
| 6426 | * EncryptionOperationsTest.TripleDesTestVector |
| 6427 | * |
| 6428 | * Verifies that NIST (plus a few extra) test vectors produce the correct results. |
| 6429 | */ |
| 6430 | TEST_P(EncryptionOperationsTest, TripleDesTestVector) { |
| 6431 | constexpr size_t num_tests = sizeof(kTripleDesTestVectors) / sizeof(TripleDesTestVector); |
| 6432 | for (auto* test = kTripleDesTestVectors; test < kTripleDesTestVectors + num_tests; ++test) { |
| 6433 | SCOPED_TRACE(test->name); |
| 6434 | CheckTripleDesTestVector(test->purpose, test->block_mode, test->padding_mode, |
| 6435 | hex2str(test->key), hex2str(test->iv), hex2str(test->input), |
| 6436 | hex2str(test->output)); |
| 6437 | } |
| 6438 | } |
| 6439 | |
| 6440 | /* |
| 6441 | * EncryptionOperationsTest.TripleDesCbcRoundTripSuccess |
| 6442 | * |
| 6443 | * Validates CBC mode functionality. |
| 6444 | */ |
| 6445 | TEST_P(EncryptionOperationsTest, TripleDesCbcRoundTripSuccess) { |
| 6446 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6447 | .TripleDesEncryptionKey(168) |
| 6448 | .BlockMode(BlockMode::CBC) |
| 6449 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6450 | .Padding(PaddingMode::NONE))); |
| 6451 | |
| 6452 | ASSERT_GT(key_blob_.size(), 0U); |
| 6453 | |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 6454 | // Four-block message. |
| 6455 | string message = "12345678901234561234567890123456"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6456 | vector<uint8_t> iv1; |
| 6457 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv1); |
| 6458 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6459 | |
| 6460 | vector<uint8_t> iv2; |
| 6461 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv2); |
| 6462 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 6463 | |
| 6464 | // IVs should be random, so ciphertexts should differ. |
| 6465 | EXPECT_NE(iv1, iv2); |
| 6466 | EXPECT_NE(ciphertext1, ciphertext2); |
| 6467 | |
| 6468 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv1); |
| 6469 | EXPECT_EQ(message, plaintext); |
| 6470 | } |
| 6471 | |
| 6472 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6473 | * EncryptionOperationsTest.TripleDesInvalidCallerIv |
| 6474 | * |
| 6475 | * Validates that keymint fails correctly when the user supplies an incorrect-size IV. |
| 6476 | */ |
| 6477 | TEST_P(EncryptionOperationsTest, TripleDesInvalidCallerIv) { |
| 6478 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6479 | .TripleDesEncryptionKey(168) |
| 6480 | .BlockMode(BlockMode::CBC) |
| 6481 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6482 | .Authorization(TAG_CALLER_NONCE) |
| 6483 | .Padding(PaddingMode::NONE))); |
| 6484 | auto params = AuthorizationSetBuilder() |
| 6485 | .BlockMode(BlockMode::CBC) |
| 6486 | .Padding(PaddingMode::NONE) |
| 6487 | .Authorization(TAG_NONCE, AidlBuf("abcdefg")); |
| 6488 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 6489 | } |
| 6490 | |
| 6491 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6492 | * EncryptionOperationsTest.TripleDesCallerIv |
| 6493 | * |
| 6494 | * Validates that 3DES keys can allow caller-specified IVs, and use them correctly. |
| 6495 | */ |
| 6496 | TEST_P(EncryptionOperationsTest, TripleDesCallerIv) { |
| 6497 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6498 | .TripleDesEncryptionKey(168) |
| 6499 | .BlockMode(BlockMode::CBC) |
| 6500 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6501 | .Authorization(TAG_CALLER_NONCE) |
| 6502 | .Padding(PaddingMode::NONE))); |
| 6503 | string message = "1234567890123456"; |
| 6504 | vector<uint8_t> iv; |
| 6505 | // Don't specify IV, should get a random one. |
| 6506 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 6507 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6508 | EXPECT_EQ(8U, iv.size()); |
| 6509 | |
| 6510 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6511 | EXPECT_EQ(message, plaintext); |
| 6512 | |
| 6513 | // Now specify an IV, should also work. |
| 6514 | iv = AidlBuf("abcdefgh"); |
| 6515 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6516 | |
| 6517 | // Decrypt with correct IV. |
| 6518 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6519 | EXPECT_EQ(message, plaintext); |
| 6520 | |
| 6521 | // Now try with wrong IV. |
| 6522 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, AidlBuf("aaaaaaaa")); |
| 6523 | EXPECT_NE(message, plaintext); |
| 6524 | } |
| 6525 | |
| 6526 | /* |
| 6527 | * EncryptionOperationsTest, TripleDesCallerNonceProhibited. |
| 6528 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6529 | * Verifies that 3DES keys without TAG_CALLER_NONCE do not allow caller-specified IVs. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6530 | */ |
| 6531 | TEST_P(EncryptionOperationsTest, TripleDesCallerNonceProhibited) { |
| 6532 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6533 | .TripleDesEncryptionKey(168) |
| 6534 | .BlockMode(BlockMode::CBC) |
| 6535 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6536 | .Padding(PaddingMode::NONE))); |
| 6537 | |
| 6538 | string message = "12345678901234567890123456789012"; |
| 6539 | vector<uint8_t> iv; |
| 6540 | // Don't specify nonce, should get a random one. |
| 6541 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 6542 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6543 | EXPECT_EQ(8U, iv.size()); |
| 6544 | |
| 6545 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6546 | EXPECT_EQ(message, plaintext); |
| 6547 | |
| 6548 | // Now specify a nonce, should fail. |
| 6549 | auto input_params = AuthorizationSetBuilder() |
| 6550 | .Authorization(TAG_NONCE, AidlBuf("abcdefgh")) |
| 6551 | .BlockMode(BlockMode::CBC) |
| 6552 | .Padding(PaddingMode::NONE); |
| 6553 | AuthorizationSet output_params; |
| 6554 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, |
| 6555 | Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 6556 | } |
| 6557 | |
| 6558 | /* |
| 6559 | * EncryptionOperationsTest.TripleDesCbcNotAuthorized |
| 6560 | * |
| 6561 | * Verifies that 3DES ECB-only keys do not allow CBC usage. |
| 6562 | */ |
| 6563 | TEST_P(EncryptionOperationsTest, TripleDesCbcNotAuthorized) { |
| 6564 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6565 | .TripleDesEncryptionKey(168) |
| 6566 | .BlockMode(BlockMode::ECB) |
| 6567 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6568 | .Padding(PaddingMode::NONE))); |
| 6569 | // Two-block message. |
| 6570 | string message = "1234567890123456"; |
| 6571 | auto begin_params = |
| 6572 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 6573 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 6574 | } |
| 6575 | |
| 6576 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6577 | * EncryptionOperationsTest.TripleDesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6578 | * |
| 6579 | * Verifies that unpadded CBC operations reject inputs that are not a multiple of block size. |
| 6580 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6581 | TEST_P(EncryptionOperationsTest, TripleDesEcbCbcNoPaddingWrongInputSize) { |
| 6582 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 6583 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6584 | .TripleDesEncryptionKey(168) |
| 6585 | .BlockMode(blockMode) |
| 6586 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6587 | .Padding(PaddingMode::NONE))); |
| 6588 | // Message is slightly shorter than two blocks. |
| 6589 | string message = "123456789012345"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6590 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6591 | auto begin_params = |
| 6592 | AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 6593 | AuthorizationSet output_params; |
| 6594 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &output_params)); |
| 6595 | string ciphertext; |
| 6596 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, "", &ciphertext)); |
| 6597 | |
| 6598 | CheckedDeleteKey(); |
| 6599 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | /* |
| 6603 | * EncryptionOperationsTest, TripleDesCbcPkcs7Padding. |
| 6604 | * |
| 6605 | * Verifies that PKCS7 padding works correctly in CBC mode. |
| 6606 | */ |
| 6607 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7Padding) { |
| 6608 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6609 | .TripleDesEncryptionKey(168) |
| 6610 | .BlockMode(BlockMode::CBC) |
| 6611 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6612 | .Padding(PaddingMode::PKCS7))); |
| 6613 | |
| 6614 | // Try various message lengths; all should work. |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 6615 | for (size_t i = 0; i <= 32; i++) { |
| 6616 | SCOPED_TRACE(testing::Message() << "i = " << i); |
| 6617 | // Edge case: '\t' (0x09) is also a valid PKCS7 padding character, albeit not for 3DES. |
| 6618 | string message(i, '\t'); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6619 | vector<uint8_t> iv; |
| 6620 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 6621 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 6622 | string plaintext = DecryptMessage(ciphertext, BlockMode::CBC, PaddingMode::PKCS7, iv); |
| 6623 | EXPECT_EQ(message, plaintext); |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | /* |
| 6628 | * EncryptionOperationsTest.TripleDesCbcNoPaddingKeyWithPkcs7Padding |
| 6629 | * |
| 6630 | * Verifies that a key that requires PKCS7 padding cannot be used in unpadded mode. |
| 6631 | */ |
| 6632 | TEST_P(EncryptionOperationsTest, TripleDesCbcNoPaddingKeyWithPkcs7Padding) { |
| 6633 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6634 | .TripleDesEncryptionKey(168) |
| 6635 | .BlockMode(BlockMode::CBC) |
| 6636 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6637 | .Padding(PaddingMode::NONE))); |
| 6638 | |
| 6639 | // Try various message lengths; all should fail. |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 6640 | for (size_t i = 0; i <= 32; i++) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6641 | auto begin_params = |
| 6642 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7); |
| 6643 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 6644 | } |
| 6645 | } |
| 6646 | |
| 6647 | /* |
| 6648 | * EncryptionOperationsTest.TripleDesCbcPkcs7PaddingCorrupted |
| 6649 | * |
| 6650 | * Verifies that corrupted PKCS7 padding is rejected during decryption. |
| 6651 | */ |
| 6652 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7PaddingCorrupted) { |
| 6653 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6654 | .TripleDesEncryptionKey(168) |
| 6655 | .BlockMode(BlockMode::CBC) |
| 6656 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6657 | .Padding(PaddingMode::PKCS7))); |
| 6658 | |
| 6659 | string message = "a"; |
| 6660 | vector<uint8_t> iv; |
| 6661 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 6662 | EXPECT_EQ(8U, ciphertext.size()); |
| 6663 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6664 | |
| 6665 | auto begin_params = AuthorizationSetBuilder() |
| 6666 | .BlockMode(BlockMode::CBC) |
| 6667 | .Padding(PaddingMode::PKCS7) |
| 6668 | .Authorization(TAG_NONCE, iv); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 6669 | |
| 6670 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
Brian J Murray | 734c841 | 2022-01-13 14:55:30 -0800 | [diff] [blame] | 6671 | SCOPED_TRACE(testing::Message() << "i = " << i); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 6672 | ++ciphertext[ciphertext.size() / 2]; |
| 6673 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 6674 | string plaintext; |
| 6675 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 6676 | ErrorCode error = Finish(&plaintext); |
| 6677 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 6678 | // This is the expected error, we can exit the test now. |
| 6679 | return; |
| 6680 | } else { |
| 6681 | // Very small chance we got valid decryption, so try again. |
| 6682 | ASSERT_EQ(error, ErrorCode::OK); |
| 6683 | } |
| 6684 | } |
| 6685 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | /* |
| 6689 | * EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding. |
| 6690 | * |
| 6691 | * Verifies that 3DES CBC works with many different input sizes. |
| 6692 | */ |
| 6693 | TEST_P(EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding) { |
| 6694 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6695 | .TripleDesEncryptionKey(168) |
| 6696 | .BlockMode(BlockMode::CBC) |
| 6697 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6698 | .Padding(PaddingMode::NONE))); |
| 6699 | |
| 6700 | int increment = 7; |
| 6701 | string message(240, 'a'); |
| 6702 | AuthorizationSet input_params = |
| 6703 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 6704 | AuthorizationSet output_params; |
| 6705 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 6706 | |
| 6707 | string ciphertext; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6708 | for (size_t i = 0; i < message.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6709 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6710 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
| 6711 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 6712 | |
| 6713 | // Move TAG_NONCE into input_params |
| 6714 | input_params = output_params; |
| 6715 | input_params.push_back(TAG_BLOCK_MODE, BlockMode::CBC); |
| 6716 | input_params.push_back(TAG_PADDING, PaddingMode::NONE); |
| 6717 | output_params.Clear(); |
| 6718 | |
| 6719 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, input_params, &output_params)); |
| 6720 | string plaintext; |
| 6721 | for (size_t i = 0; i < ciphertext.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6722 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6723 | EXPECT_EQ(ErrorCode::OK, Finish(&plaintext)); |
| 6724 | EXPECT_EQ(ciphertext.size(), plaintext.size()); |
| 6725 | EXPECT_EQ(message, plaintext); |
| 6726 | } |
| 6727 | |
| 6728 | INSTANTIATE_KEYMINT_AIDL_TEST(EncryptionOperationsTest); |
| 6729 | |
| 6730 | typedef KeyMintAidlTestBase MaxOperationsTest; |
| 6731 | |
| 6732 | /* |
| 6733 | * MaxOperationsTest.TestLimitAes |
| 6734 | * |
| 6735 | * Verifies that the max uses per boot tag works correctly with AES keys. |
| 6736 | */ |
| 6737 | TEST_P(MaxOperationsTest, TestLimitAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6738 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6739 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6740 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6741 | |
| 6742 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6743 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6744 | .AesEncryptionKey(128) |
| 6745 | .EcbMode() |
| 6746 | .Padding(PaddingMode::NONE) |
| 6747 | .Authorization(TAG_MAX_USES_PER_BOOT, 3))); |
| 6748 | |
| 6749 | string message = "1234567890123456"; |
| 6750 | |
| 6751 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6752 | |
| 6753 | EncryptMessage(message, params); |
| 6754 | EncryptMessage(message, params); |
| 6755 | EncryptMessage(message, params); |
| 6756 | |
| 6757 | // Fourth time should fail. |
| 6758 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::ENCRYPT, params)); |
| 6759 | } |
| 6760 | |
| 6761 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6762 | * MaxOperationsTest.TestLimitRsa |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6763 | * |
| 6764 | * Verifies that the max uses per boot tag works correctly with RSA keys. |
| 6765 | */ |
| 6766 | TEST_P(MaxOperationsTest, TestLimitRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6767 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6768 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6769 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6770 | |
| 6771 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6772 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6773 | .RsaSigningKey(1024, 65537) |
| 6774 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6775 | .Authorization(TAG_MAX_USES_PER_BOOT, 3) |
| 6776 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6777 | |
| 6778 | string message = "1234567890123456"; |
| 6779 | |
| 6780 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6781 | |
| 6782 | SignMessage(message, params); |
| 6783 | SignMessage(message, params); |
| 6784 | SignMessage(message, params); |
| 6785 | |
| 6786 | // Fourth time should fail. |
| 6787 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::SIGN, params)); |
| 6788 | } |
| 6789 | |
| 6790 | INSTANTIATE_KEYMINT_AIDL_TEST(MaxOperationsTest); |
| 6791 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6792 | typedef KeyMintAidlTestBase UsageCountLimitTest; |
| 6793 | |
| 6794 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6795 | * UsageCountLimitTest.TestSingleUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6796 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6797 | * Verifies that the usage count limit tag = 1 works correctly with AES keys. |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6798 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6799 | TEST_P(UsageCountLimitTest, TestSingleUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6800 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6801 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6802 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6803 | |
| 6804 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6805 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6806 | .AesEncryptionKey(128) |
| 6807 | .EcbMode() |
| 6808 | .Padding(PaddingMode::NONE) |
| 6809 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1))); |
| 6810 | |
| 6811 | // Check the usage count limit tag appears in the authorizations. |
| 6812 | AuthorizationSet auths; |
| 6813 | for (auto& entry : key_characteristics_) { |
| 6814 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6815 | } |
| 6816 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6817 | << "key usage count limit " << 1U << " missing"; |
| 6818 | |
| 6819 | string message = "1234567890123456"; |
| 6820 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6821 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6822 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6823 | AuthorizationSet keystore_auths = |
| 6824 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6825 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6826 | // First usage of AES key should work. |
| 6827 | EncryptMessage(message, params); |
| 6828 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6829 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 6830 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6831 | // must be invalidated from secure storage (such as RPMB partition). |
| 6832 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 6833 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6834 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6835 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6836 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 6837 | } |
| 6838 | } |
| 6839 | |
| 6840 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6841 | * UsageCountLimitTest.TestLimitedUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6842 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6843 | * Verifies that the usage count limit tag > 1 works correctly with AES keys. |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6844 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6845 | TEST_P(UsageCountLimitTest, TestLimitedUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6846 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6847 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6848 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6849 | |
| 6850 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6851 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6852 | .AesEncryptionKey(128) |
| 6853 | .EcbMode() |
| 6854 | .Padding(PaddingMode::NONE) |
| 6855 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3))); |
| 6856 | |
| 6857 | // Check the usage count limit tag appears in the authorizations. |
| 6858 | AuthorizationSet auths; |
| 6859 | for (auto& entry : key_characteristics_) { |
| 6860 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6861 | } |
| 6862 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 6863 | << "key usage count limit " << 3U << " missing"; |
| 6864 | |
| 6865 | string message = "1234567890123456"; |
| 6866 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6867 | |
| 6868 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6869 | AuthorizationSet keystore_auths = |
| 6870 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6871 | |
| 6872 | EncryptMessage(message, params); |
| 6873 | EncryptMessage(message, params); |
| 6874 | EncryptMessage(message, params); |
| 6875 | |
| 6876 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6877 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6878 | // must be invalidated from secure storage (such as RPMB partition). |
| 6879 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 6880 | } else { |
| 6881 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6882 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
| 6883 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 6884 | } |
| 6885 | } |
| 6886 | |
| 6887 | /* |
| 6888 | * UsageCountLimitTest.TestSingleUseRsa |
| 6889 | * |
| 6890 | * Verifies that the usage count limit tag = 1 works correctly with RSA keys. |
| 6891 | */ |
| 6892 | TEST_P(UsageCountLimitTest, TestSingleUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6893 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6894 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6895 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6896 | |
| 6897 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6898 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6899 | .RsaSigningKey(1024, 65537) |
| 6900 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6901 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 6902 | .SetDefaultValidity())); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6903 | |
| 6904 | // Check the usage count limit tag appears in the authorizations. |
| 6905 | AuthorizationSet auths; |
| 6906 | for (auto& entry : key_characteristics_) { |
| 6907 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6908 | } |
| 6909 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6910 | << "key usage count limit " << 1U << " missing"; |
| 6911 | |
| 6912 | string message = "1234567890123456"; |
| 6913 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6914 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6915 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6916 | AuthorizationSet keystore_auths = |
| 6917 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6918 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6919 | // First usage of RSA key should work. |
| 6920 | SignMessage(message, params); |
| 6921 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6922 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 6923 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6924 | // must be invalidated from secure storage (such as RPMB partition). |
| 6925 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6926 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6927 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6928 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
| 6929 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6930 | } |
| 6931 | } |
| 6932 | |
| 6933 | /* |
| 6934 | * UsageCountLimitTest.TestLimitUseRsa |
| 6935 | * |
| 6936 | * Verifies that the usage count limit tag > 1 works correctly with RSA keys. |
| 6937 | */ |
| 6938 | TEST_P(UsageCountLimitTest, TestLimitUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6939 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6940 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6941 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6942 | |
| 6943 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6944 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6945 | .RsaSigningKey(1024, 65537) |
| 6946 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6947 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3) |
| 6948 | .SetDefaultValidity())); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6949 | |
| 6950 | // Check the usage count limit tag appears in the authorizations. |
| 6951 | AuthorizationSet auths; |
| 6952 | for (auto& entry : key_characteristics_) { |
| 6953 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6954 | } |
| 6955 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 6956 | << "key usage count limit " << 3U << " missing"; |
| 6957 | |
| 6958 | string message = "1234567890123456"; |
| 6959 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6960 | |
| 6961 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6962 | AuthorizationSet keystore_auths = |
| 6963 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6964 | |
| 6965 | SignMessage(message, params); |
| 6966 | SignMessage(message, params); |
| 6967 | SignMessage(message, params); |
| 6968 | |
| 6969 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6970 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6971 | // must be invalidated from secure storage (such as RPMB partition). |
| 6972 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6973 | } else { |
| 6974 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6975 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6976 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6977 | } |
| 6978 | } |
| 6979 | |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6980 | /* |
| 6981 | * UsageCountLimitTest.TestSingleUseKeyAndRollbackResistance |
| 6982 | * |
| 6983 | * Verifies that when rollback resistance is supported by the KeyMint implementation with |
| 6984 | * the secure hardware, the single use key with usage count limit tag = 1 must also be enforced |
| 6985 | * in hardware. |
| 6986 | */ |
| 6987 | TEST_P(UsageCountLimitTest, TestSingleUseKeyAndRollbackResistance) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6988 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6989 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6990 | } |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6991 | |
| 6992 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6993 | .RsaSigningKey(2048, 65537) |
| 6994 | .Digest(Digest::NONE) |
| 6995 | .Padding(PaddingMode::NONE) |
| 6996 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6997 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6998 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6999 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 7000 | GTEST_SKIP() << "Rollback resistance not supported"; |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 7001 | } |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7002 | |
| 7003 | // Rollback resistance is supported by KeyMint, verify it is enforced in hardware. |
| 7004 | ASSERT_EQ(ErrorCode::OK, error); |
| 7005 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 7006 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
| 7007 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
| 7008 | |
| 7009 | // The KeyMint should also enforce single use key in hardware when it supports rollback |
| 7010 | // resistance. |
| 7011 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7012 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7013 | .RsaSigningKey(1024, 65537) |
| 7014 | .NoDigestOrPadding() |
| 7015 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 7016 | .SetDefaultValidity())); |
| 7017 | |
| 7018 | // Check the usage count limit tag appears in the hardware authorizations. |
| 7019 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 7020 | EXPECT_TRUE(hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 7021 | << "key usage count limit " << 1U << " missing"; |
| 7022 | |
| 7023 | string message = "1234567890123456"; |
| 7024 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 7025 | |
| 7026 | // First usage of RSA key should work. |
| 7027 | SignMessage(message, params); |
| 7028 | |
| 7029 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 7030 | // must be invalidated from secure storage (such as RPMB partition). |
| 7031 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 7032 | } |
| 7033 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 7034 | INSTANTIATE_KEYMINT_AIDL_TEST(UsageCountLimitTest); |
| 7035 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 7036 | typedef KeyMintAidlTestBase GetHardwareInfoTest; |
| 7037 | |
| 7038 | TEST_P(GetHardwareInfoTest, GetHardwareInfo) { |
| 7039 | // Retrieving hardware info should give the same result each time. |
| 7040 | KeyMintHardwareInfo info; |
| 7041 | ASSERT_TRUE(keyMint().getHardwareInfo(&info).isOk()); |
| 7042 | KeyMintHardwareInfo info2; |
| 7043 | ASSERT_TRUE(keyMint().getHardwareInfo(&info2).isOk()); |
| 7044 | EXPECT_EQ(info, info2); |
| 7045 | } |
| 7046 | |
| 7047 | INSTANTIATE_KEYMINT_AIDL_TEST(GetHardwareInfoTest); |
| 7048 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7049 | typedef KeyMintAidlTestBase AddEntropyTest; |
| 7050 | |
| 7051 | /* |
| 7052 | * AddEntropyTest.AddEntropy |
| 7053 | * |
| 7054 | * Verifies that the addRngEntropy method doesn't blow up. There's no way to test that entropy |
| 7055 | * is actually added. |
| 7056 | */ |
| 7057 | TEST_P(AddEntropyTest, AddEntropy) { |
| 7058 | string data = "foo"; |
| 7059 | EXPECT_TRUE(keyMint().addRngEntropy(vector<uint8_t>(data.begin(), data.end())).isOk()); |
| 7060 | } |
| 7061 | |
| 7062 | /* |
| 7063 | * AddEntropyTest.AddEmptyEntropy |
| 7064 | * |
| 7065 | * Verifies that the addRngEntropy method doesn't blow up when given an empty buffer. |
| 7066 | */ |
| 7067 | TEST_P(AddEntropyTest, AddEmptyEntropy) { |
| 7068 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf()).isOk()); |
| 7069 | } |
| 7070 | |
| 7071 | /* |
| 7072 | * AddEntropyTest.AddLargeEntropy |
| 7073 | * |
| 7074 | * Verifies that the addRngEntropy method doesn't blow up when given a largish amount of data. |
| 7075 | */ |
| 7076 | TEST_P(AddEntropyTest, AddLargeEntropy) { |
| 7077 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf(string(2 * 1024, 'a'))).isOk()); |
| 7078 | } |
| 7079 | |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 7080 | /* |
| 7081 | * AddEntropyTest.AddTooLargeEntropy |
| 7082 | * |
| 7083 | * Verifies that the addRngEntropy method rejects more than 2KiB of data. |
| 7084 | */ |
| 7085 | TEST_P(AddEntropyTest, AddTooLargeEntropy) { |
| 7086 | ErrorCode rc = GetReturnErrorCode(keyMint().addRngEntropy(AidlBuf(string(2 * 1024 + 1, 'a')))); |
| 7087 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, rc); |
| 7088 | } |
| 7089 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7090 | INSTANTIATE_KEYMINT_AIDL_TEST(AddEntropyTest); |
| 7091 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7092 | typedef KeyMintAidlTestBase KeyDeletionTest; |
| 7093 | |
| 7094 | /** |
| 7095 | * KeyDeletionTest.DeleteKey |
| 7096 | * |
| 7097 | * This test checks that if rollback protection is implemented, DeleteKey invalidates a formerly |
| 7098 | * valid key blob. |
| 7099 | */ |
| 7100 | TEST_P(KeyDeletionTest, DeleteKey) { |
| 7101 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 7102 | .RsaSigningKey(2048, 65537) |
| 7103 | .Digest(Digest::NONE) |
| 7104 | .Padding(PaddingMode::NONE) |
| 7105 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 7106 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 7107 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7108 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 7109 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 7110 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7111 | |
| 7112 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7113 | ASSERT_EQ(ErrorCode::OK, error); |
| 7114 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 7115 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7116 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7117 | ASSERT_EQ(ErrorCode::OK, DeleteKey(true /* keep key blob */)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7118 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7119 | string message = "12345678901234567890123456789012"; |
| 7120 | AuthorizationSet begin_out_params; |
| 7121 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 7122 | Begin(KeyPurpose::SIGN, key_blob_, |
| 7123 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 7124 | &begin_out_params)); |
| 7125 | AbortIfNeeded(); |
| 7126 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7127 | } |
| 7128 | |
| 7129 | /** |
| 7130 | * KeyDeletionTest.DeleteInvalidKey |
| 7131 | * |
| 7132 | * This test checks that the HAL excepts invalid key blobs.. |
| 7133 | */ |
| 7134 | TEST_P(KeyDeletionTest, DeleteInvalidKey) { |
| 7135 | // Generate key just to check if rollback protection is implemented |
| 7136 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 7137 | .RsaSigningKey(2048, 65537) |
| 7138 | .Digest(Digest::NONE) |
| 7139 | .Padding(PaddingMode::NONE) |
| 7140 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 7141 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 7142 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7143 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 7144 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 7145 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7146 | |
| 7147 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7148 | ASSERT_EQ(ErrorCode::OK, error); |
| 7149 | AuthorizationSet enforced(SecLevelAuthorizations()); |
| 7150 | ASSERT_TRUE(enforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7151 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7152 | // Delete the key we don't care about the result at this point. |
| 7153 | DeleteKey(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7154 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7155 | // Now create an invalid key blob and delete it. |
| 7156 | key_blob_ = AidlBuf("just some garbage data which is not a valid key blob"); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7157 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7158 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | /** |
| 7162 | * KeyDeletionTest.DeleteAllKeys |
| 7163 | * |
| 7164 | * This test is disarmed by default. To arm it use --arm_deleteAllKeys. |
| 7165 | * |
| 7166 | * BEWARE: This test has serious side effects. All user keys will be lost! This includes |
| 7167 | * FBE/FDE encryption keys, which means that the device will not even boot until after the |
| 7168 | * device has been wiped manually (e.g., fastboot flashall -w), and new FBE/FDE keys have |
| 7169 | * been provisioned. Use this test only on dedicated testing devices that have no valuable |
| 7170 | * credentials stored in Keystore/Keymint. |
| 7171 | */ |
| 7172 | TEST_P(KeyDeletionTest, DeleteAllKeys) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7173 | if (!arm_deleteAllKeys) { |
| 7174 | GTEST_SKIP() << "Option --arm_deleteAllKeys not set"; |
| 7175 | return; |
| 7176 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7177 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 7178 | .RsaSigningKey(2048, 65537) |
| 7179 | .Digest(Digest::NONE) |
| 7180 | .Padding(PaddingMode::NONE) |
| 7181 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 7182 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 7183 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7184 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 7185 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 7186 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7187 | |
| 7188 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7189 | ASSERT_EQ(ErrorCode::OK, error); |
| 7190 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 7191 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7192 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7193 | ASSERT_EQ(ErrorCode::OK, DeleteAllKeys()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7194 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7195 | string message = "12345678901234567890123456789012"; |
| 7196 | AuthorizationSet begin_out_params; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7197 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7198 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 7199 | Begin(KeyPurpose::SIGN, key_blob_, |
| 7200 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 7201 | &begin_out_params)); |
| 7202 | AbortIfNeeded(); |
| 7203 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyDeletionTest); |
| 7207 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7208 | typedef KeyMintAidlTestBase KeyUpgradeTest; |
| 7209 | |
| 7210 | /** |
| 7211 | * KeyUpgradeTest.UpgradeInvalidKey |
| 7212 | * |
| 7213 | * This test checks that the HAL excepts invalid key blobs.. |
| 7214 | */ |
| 7215 | TEST_P(KeyUpgradeTest, UpgradeInvalidKey) { |
| 7216 | AidlBuf key_blob = AidlBuf("just some garbage data which is not a valid key blob"); |
| 7217 | |
| 7218 | std::vector<uint8_t> new_blob; |
| 7219 | Status result = keymint_->upgradeKey(key_blob, |
| 7220 | AuthorizationSetBuilder() |
| 7221 | .Authorization(TAG_APPLICATION_ID, "clientid") |
| 7222 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 7223 | .vector_data(), |
| 7224 | &new_blob); |
| 7225 | ASSERT_EQ(ErrorCode::INVALID_KEY_BLOB, GetReturnErrorCode(result)); |
| 7226 | } |
| 7227 | |
| 7228 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyUpgradeTest); |
| 7229 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7230 | using UpgradeKeyTest = KeyMintAidlTestBase; |
| 7231 | |
| 7232 | /* |
| 7233 | * UpgradeKeyTest.UpgradeKey |
| 7234 | * |
| 7235 | * Verifies that calling upgrade key on an up-to-date key works (i.e. does nothing). |
| 7236 | */ |
| 7237 | TEST_P(UpgradeKeyTest, UpgradeKey) { |
| 7238 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7239 | .AesEncryptionKey(128) |
| 7240 | .Padding(PaddingMode::NONE) |
| 7241 | .Authorization(TAG_NO_AUTH_REQUIRED))); |
| 7242 | |
| 7243 | auto result = UpgradeKey(key_blob_); |
| 7244 | |
| 7245 | // Key doesn't need upgrading. Should get okay, but no new key blob. |
| 7246 | EXPECT_EQ(result, std::make_pair(ErrorCode::OK, vector<uint8_t>())); |
| 7247 | } |
| 7248 | |
| 7249 | INSTANTIATE_KEYMINT_AIDL_TEST(UpgradeKeyTest); |
| 7250 | |
| 7251 | using ClearOperationsTest = KeyMintAidlTestBase; |
| 7252 | |
| 7253 | /* |
| 7254 | * ClearSlotsTest.TooManyOperations |
| 7255 | * |
| 7256 | * Verifies that TOO_MANY_OPERATIONS is returned after the max number of |
| 7257 | * operations are started without being finished or aborted. Also verifies |
| 7258 | * that aborting the operations clears the operations. |
| 7259 | * |
| 7260 | */ |
| 7261 | TEST_P(ClearOperationsTest, TooManyOperations) { |
| 7262 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7263 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7264 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 7265 | .Padding(PaddingMode::NONE) |
| 7266 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7267 | |
| 7268 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 7269 | constexpr size_t max_operations = 100; // set to arbituary large number |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 7270 | std::shared_ptr<IKeyMintOperation> op_handles[max_operations]; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7271 | AuthorizationSet out_params; |
| 7272 | ErrorCode result; |
| 7273 | size_t i; |
| 7274 | |
| 7275 | for (i = 0; i < max_operations; i++) { |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 7276 | result = Begin(KeyPurpose::DECRYPT, key_blob_, params, &out_params, op_handles[i]); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7277 | if (ErrorCode::OK != result) { |
| 7278 | break; |
| 7279 | } |
| 7280 | } |
| 7281 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, result); |
| 7282 | // Try again just in case there's a weird overflow bug |
| 7283 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 7284 | Begin(KeyPurpose::DECRYPT, key_blob_, params, &out_params)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7285 | for (size_t j = 0; j < i; j++) { |
| 7286 | EXPECT_EQ(ErrorCode::OK, Abort(op_handles[j])) |
| 7287 | << "Aboort failed for i = " << j << std::endl; |
| 7288 | } |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 7289 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, key_blob_, params, &out_params)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7290 | AbortIfNeeded(); |
| 7291 | } |
| 7292 | |
| 7293 | INSTANTIATE_KEYMINT_AIDL_TEST(ClearOperationsTest); |
| 7294 | |
| 7295 | typedef KeyMintAidlTestBase TransportLimitTest; |
| 7296 | |
| 7297 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 7298 | * TransportLimitTest.LargeFinishInput |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7299 | * |
| 7300 | * Verifies that passing input data to finish succeeds as expected. |
| 7301 | */ |
| 7302 | TEST_P(TransportLimitTest, LargeFinishInput) { |
| 7303 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7304 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7305 | .AesEncryptionKey(128) |
| 7306 | .BlockMode(BlockMode::ECB) |
| 7307 | .Padding(PaddingMode::NONE))); |
| 7308 | |
| 7309 | for (int msg_size = 8 /* 256 bytes */; msg_size <= 11 /* 2 KiB */; msg_size++) { |
| 7310 | auto cipher_params = |
| 7311 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 7312 | |
| 7313 | AuthorizationSet out_params; |
| 7314 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, cipher_params, &out_params)); |
| 7315 | |
| 7316 | string plain_message = std::string(1 << msg_size, 'x'); |
| 7317 | string encrypted_message; |
| 7318 | auto rc = Finish(plain_message, &encrypted_message); |
| 7319 | |
| 7320 | EXPECT_EQ(ErrorCode::OK, rc); |
| 7321 | EXPECT_EQ(plain_message.size(), encrypted_message.size()) |
| 7322 | << "Encrypt finish returned OK, but did not consume all of the given input"; |
| 7323 | cipher_params.push_back(out_params); |
| 7324 | |
| 7325 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, cipher_params)); |
| 7326 | |
| 7327 | string decrypted_message; |
| 7328 | rc = Finish(encrypted_message, &decrypted_message); |
| 7329 | EXPECT_EQ(ErrorCode::OK, rc); |
| 7330 | EXPECT_EQ(plain_message.size(), decrypted_message.size()) |
| 7331 | << "Decrypt finish returned OK, did not consume all of the given input"; |
| 7332 | } |
| 7333 | } |
| 7334 | |
| 7335 | INSTANTIATE_KEYMINT_AIDL_TEST(TransportLimitTest); |
| 7336 | |
Seth Moore | d79a0ec | 2021-12-13 20:03:33 +0000 | [diff] [blame] | 7337 | static int EcdhCurveToOpenSslCurveName(EcCurve curve) { |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7338 | switch (curve) { |
| 7339 | case EcCurve::P_224: |
| 7340 | return NID_secp224r1; |
| 7341 | case EcCurve::P_256: |
| 7342 | return NID_X9_62_prime256v1; |
| 7343 | case EcCurve::P_384: |
| 7344 | return NID_secp384r1; |
| 7345 | case EcCurve::P_521: |
| 7346 | return NID_secp521r1; |
Seth Moore | d79a0ec | 2021-12-13 20:03:33 +0000 | [diff] [blame] | 7347 | case EcCurve::CURVE_25519: |
| 7348 | return NID_X25519; |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7349 | } |
| 7350 | } |
| 7351 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7352 | class KeyAgreementTest : public KeyMintAidlTestBase { |
| 7353 | protected: |
| 7354 | void GenerateLocalEcKey(EcCurve localCurve, EVP_PKEY_Ptr* localPrivKey, |
| 7355 | std::vector<uint8_t>* localPublicKey) { |
| 7356 | // Generate EC key locally (with access to private key material) |
| 7357 | if (localCurve == EcCurve::CURVE_25519) { |
| 7358 | uint8_t privKeyData[32]; |
| 7359 | uint8_t pubKeyData[32]; |
| 7360 | X25519_keypair(pubKeyData, privKeyData); |
| 7361 | *localPublicKey = vector<uint8_t>(pubKeyData, pubKeyData + 32); |
| 7362 | *localPrivKey = EVP_PKEY_Ptr(EVP_PKEY_new_raw_private_key( |
| 7363 | EVP_PKEY_X25519, nullptr, privKeyData, sizeof(privKeyData))); |
| 7364 | } else { |
| 7365 | auto ecKey = EC_KEY_Ptr(EC_KEY_new()); |
| 7366 | int curveName = EcdhCurveToOpenSslCurveName(localCurve); |
| 7367 | auto group = EC_GROUP_Ptr(EC_GROUP_new_by_curve_name(curveName)); |
| 7368 | ASSERT_NE(group, nullptr); |
| 7369 | ASSERT_EQ(EC_KEY_set_group(ecKey.get(), group.get()), 1); |
| 7370 | ASSERT_EQ(EC_KEY_generate_key(ecKey.get()), 1); |
| 7371 | *localPrivKey = EVP_PKEY_Ptr(EVP_PKEY_new()); |
| 7372 | ASSERT_EQ(EVP_PKEY_set1_EC_KEY(localPrivKey->get(), ecKey.get()), 1); |
| 7373 | |
| 7374 | // Get encoded form of the public part of the locally generated key... |
| 7375 | unsigned char* p = nullptr; |
| 7376 | int localPublicKeySize = i2d_PUBKEY(localPrivKey->get(), &p); |
| 7377 | ASSERT_GT(localPublicKeySize, 0); |
| 7378 | *localPublicKey = |
| 7379 | vector<uint8_t>(reinterpret_cast<const uint8_t*>(p), |
| 7380 | reinterpret_cast<const uint8_t*>(p + localPublicKeySize)); |
| 7381 | OPENSSL_free(p); |
| 7382 | } |
| 7383 | } |
| 7384 | |
| 7385 | void GenerateKeyMintEcKey(EcCurve curve, EVP_PKEY_Ptr* kmPubKey) { |
| 7386 | vector<uint8_t> challenge = {0x41, 0x42}; |
| 7387 | ErrorCode result = |
| 7388 | GenerateKey(AuthorizationSetBuilder() |
| 7389 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7390 | .Authorization(TAG_EC_CURVE, curve) |
| 7391 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 7392 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 7393 | .Authorization(TAG_ATTESTATION_APPLICATION_ID, {0x61, 0x62}) |
| 7394 | .Authorization(TAG_ATTESTATION_CHALLENGE, challenge) |
| 7395 | .SetDefaultValidity()); |
| 7396 | ASSERT_EQ(ErrorCode::OK, result) << "Failed to generate key"; |
| 7397 | ASSERT_GT(cert_chain_.size(), 0); |
| 7398 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 7399 | ASSERT_NE(kmKeyCert, nullptr); |
| 7400 | // Check that keyAgreement (bit 4) is set in KeyUsage |
| 7401 | EXPECT_TRUE((X509_get_key_usage(kmKeyCert.get()) & X509v3_KU_KEY_AGREEMENT) != 0); |
| 7402 | *kmPubKey = EVP_PKEY_Ptr(X509_get_pubkey(kmKeyCert.get())); |
| 7403 | ASSERT_NE(*kmPubKey, nullptr); |
| 7404 | if (dump_Attestations) { |
| 7405 | for (size_t n = 0; n < cert_chain_.size(); n++) { |
| 7406 | std::cout << bin2hex(cert_chain_[n].encodedCertificate) << std::endl; |
| 7407 | } |
| 7408 | } |
| 7409 | } |
| 7410 | |
| 7411 | void CheckAgreement(EVP_PKEY_Ptr kmPubKey, EVP_PKEY_Ptr localPrivKey, |
| 7412 | const std::vector<uint8_t>& localPublicKey) { |
| 7413 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7414 | string ZabFromKeyMintStr; |
| 7415 | ASSERT_EQ(ErrorCode::OK, |
| 7416 | Finish(string(localPublicKey.begin(), localPublicKey.end()), &ZabFromKeyMintStr)); |
| 7417 | vector<uint8_t> ZabFromKeyMint(ZabFromKeyMintStr.begin(), ZabFromKeyMintStr.end()); |
| 7418 | vector<uint8_t> ZabFromTest; |
| 7419 | |
| 7420 | if (EVP_PKEY_id(kmPubKey.get()) == EVP_PKEY_X25519) { |
| 7421 | size_t kmPubKeySize = 32; |
| 7422 | uint8_t kmPubKeyData[32]; |
| 7423 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 7424 | ASSERT_EQ(kmPubKeySize, 32); |
| 7425 | |
| 7426 | uint8_t localPrivKeyData[32]; |
| 7427 | size_t localPrivKeySize = 32; |
| 7428 | ASSERT_EQ(1, EVP_PKEY_get_raw_private_key(localPrivKey.get(), localPrivKeyData, |
| 7429 | &localPrivKeySize)); |
| 7430 | ASSERT_EQ(localPrivKeySize, 32); |
| 7431 | |
| 7432 | uint8_t sharedKey[32]; |
| 7433 | ASSERT_EQ(1, X25519(sharedKey, localPrivKeyData, kmPubKeyData)); |
| 7434 | ZabFromTest = std::vector<uint8_t>(sharedKey, sharedKey + 32); |
| 7435 | } else { |
| 7436 | // Perform local ECDH between the two keys so we can check if we get the same Zab.. |
| 7437 | auto ctx = EVP_PKEY_CTX_Ptr(EVP_PKEY_CTX_new(localPrivKey.get(), nullptr)); |
| 7438 | ASSERT_NE(ctx, nullptr); |
| 7439 | ASSERT_EQ(EVP_PKEY_derive_init(ctx.get()), 1); |
| 7440 | ASSERT_EQ(EVP_PKEY_derive_set_peer(ctx.get(), kmPubKey.get()), 1); |
| 7441 | size_t ZabFromTestLen = 0; |
| 7442 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), nullptr, &ZabFromTestLen), 1); |
| 7443 | ZabFromTest.resize(ZabFromTestLen); |
| 7444 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), ZabFromTest.data(), &ZabFromTestLen), 1); |
| 7445 | } |
| 7446 | EXPECT_EQ(ZabFromKeyMint, ZabFromTest); |
| 7447 | } |
| 7448 | }; |
| 7449 | |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7450 | /* |
| 7451 | * KeyAgreementTest.Ecdh |
| 7452 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7453 | * Verifies that ECDH works for all required curves |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7454 | */ |
| 7455 | TEST_P(KeyAgreementTest, Ecdh) { |
| 7456 | // Because it's possible to use this API with keys on different curves, we |
| 7457 | // check all N^2 combinations where N is the number of supported |
| 7458 | // curves. |
| 7459 | // |
| 7460 | // This is not a big deal as N is 4 so we only do 16 runs. If we end up with a |
| 7461 | // lot more curves we can be smart about things and just pick |otherCurve| so |
| 7462 | // it's not |curve| and that way we end up with only 2*N runs |
| 7463 | // |
| 7464 | for (auto curve : ValidCurves()) { |
| 7465 | for (auto localCurve : ValidCurves()) { |
| 7466 | // Generate EC key locally (with access to private key material) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7467 | EVP_PKEY_Ptr localPrivKey; |
| 7468 | vector<uint8_t> localPublicKey; |
| 7469 | GenerateLocalEcKey(localCurve, &localPrivKey, &localPublicKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7470 | |
| 7471 | // Generate EC key in KeyMint (only access to public key material) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7472 | EVP_PKEY_Ptr kmPubKey; |
| 7473 | GenerateKeyMintEcKey(curve, &kmPubKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7474 | |
| 7475 | // Now that we have the two keys, we ask KeyMint to perform ECDH... |
| 7476 | if (curve != localCurve) { |
| 7477 | // If the keys are using different curves KeyMint should fail with |
| 7478 | // ErrorCode:INVALID_ARGUMENT. Check that. |
| 7479 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7480 | string ZabFromKeyMintStr; |
| 7481 | EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7482 | Finish(string(localPublicKey.begin(), localPublicKey.end()), |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7483 | &ZabFromKeyMintStr)); |
| 7484 | |
| 7485 | } else { |
| 7486 | // Otherwise if the keys are using the same curve, it should work. |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7487 | CheckAgreement(std::move(kmPubKey), std::move(localPrivKey), localPublicKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7488 | } |
| 7489 | |
| 7490 | CheckedDeleteKey(); |
| 7491 | } |
| 7492 | } |
| 7493 | } |
| 7494 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 7495 | /* |
| 7496 | * KeyAgreementTest.EcdhCurve25519 |
| 7497 | * |
| 7498 | * Verifies that ECDH works for curve25519. This is also covered by the general |
| 7499 | * KeyAgreementTest.Ecdh case, but is pulled out separately here because this curve was added after |
| 7500 | * KeyMint 1.0. |
| 7501 | */ |
| 7502 | TEST_P(KeyAgreementTest, EcdhCurve25519) { |
| 7503 | if (!Curve25519Supported()) { |
| 7504 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7505 | } |
| 7506 | |
| 7507 | // Generate EC key in KeyMint (only access to public key material) |
| 7508 | EcCurve curve = EcCurve::CURVE_25519; |
| 7509 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7510 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7511 | |
| 7512 | // Generate EC key on same curve locally (with access to private key material). |
| 7513 | EVP_PKEY_Ptr privKey; |
| 7514 | vector<uint8_t> encodedPublicKey; |
| 7515 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7516 | |
| 7517 | // Agree on a key between local and KeyMint and check it. |
| 7518 | CheckAgreement(std::move(kmPubKey), std::move(privKey), encodedPublicKey); |
| 7519 | |
| 7520 | CheckedDeleteKey(); |
| 7521 | } |
| 7522 | |
| 7523 | /* |
| 7524 | * KeyAgreementTest.EcdhCurve25519Imported |
| 7525 | * |
| 7526 | * Verifies that ECDH works for an imported curve25519 key. |
| 7527 | */ |
| 7528 | TEST_P(KeyAgreementTest, EcdhCurve25519Imported) { |
| 7529 | if (!Curve25519Supported()) { |
| 7530 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7531 | } |
| 7532 | |
| 7533 | // Import x25519 key into KeyMint. |
| 7534 | EcCurve curve = EcCurve::CURVE_25519; |
| 7535 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 7536 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7537 | .EcdsaKey(EcCurve::CURVE_25519) |
| 7538 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 7539 | .SetDefaultValidity(), |
| 7540 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 7541 | ASSERT_GT(cert_chain_.size(), 0); |
| 7542 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 7543 | ASSERT_NE(kmKeyCert, nullptr); |
| 7544 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 7545 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 7546 | |
| 7547 | // Expect the import to emit corresponding public key data. |
| 7548 | size_t kmPubKeySize = 32; |
| 7549 | uint8_t kmPubKeyData[32]; |
| 7550 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 7551 | ASSERT_EQ(kmPubKeySize, 32); |
| 7552 | EXPECT_EQ(bin2hex(std::vector<uint8_t>(kmPubKeyData, kmPubKeyData + 32)), |
| 7553 | bin2hex(std::vector<uint8_t>(x25519_pubkey.begin(), x25519_pubkey.end()))); |
| 7554 | |
| 7555 | // Generate EC key on same curve locally (with access to private key material). |
| 7556 | EVP_PKEY_Ptr privKey; |
| 7557 | vector<uint8_t> encodedPublicKey; |
| 7558 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7559 | |
| 7560 | // Agree on a key between local and KeyMint and check it. |
| 7561 | CheckAgreement(std::move(kmPubKey), std::move(privKey), encodedPublicKey); |
| 7562 | |
| 7563 | CheckedDeleteKey(); |
| 7564 | } |
| 7565 | |
| 7566 | /* |
| 7567 | * KeyAgreementTest.EcdhCurve25519InvalidSize |
| 7568 | * |
| 7569 | * Verifies that ECDH fails for curve25519 if the wrong size of public key is provided. |
| 7570 | */ |
| 7571 | TEST_P(KeyAgreementTest, EcdhCurve25519InvalidSize) { |
| 7572 | if (!Curve25519Supported()) { |
| 7573 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7574 | } |
| 7575 | |
| 7576 | // Generate EC key in KeyMint (only access to public key material) |
| 7577 | EcCurve curve = EcCurve::CURVE_25519; |
| 7578 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7579 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7580 | |
| 7581 | // Generate EC key on same curve locally (with access to private key material). |
| 7582 | EVP_PKEY_Ptr privKey; |
| 7583 | vector<uint8_t> encodedPublicKey; |
| 7584 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7585 | |
| 7586 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7587 | string ZabFromKeyMintStr; |
| 7588 | // Send in an incomplete public key. |
| 7589 | ASSERT_NE(ErrorCode::OK, Finish(string(encodedPublicKey.begin(), encodedPublicKey.end() - 1), |
| 7590 | &ZabFromKeyMintStr)); |
| 7591 | |
| 7592 | CheckedDeleteKey(); |
| 7593 | } |
| 7594 | |
| 7595 | /* |
| 7596 | * KeyAgreementTest.EcdhCurve25519Mismatch |
| 7597 | * |
| 7598 | * Verifies that ECDH fails between curve25519 and other curves. |
| 7599 | */ |
| 7600 | TEST_P(KeyAgreementTest, EcdhCurve25519Mismatch) { |
| 7601 | if (!Curve25519Supported()) { |
| 7602 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7603 | } |
| 7604 | |
| 7605 | // Generate EC key in KeyMint (only access to public key material) |
| 7606 | EcCurve curve = EcCurve::CURVE_25519; |
| 7607 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7608 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7609 | |
| 7610 | for (auto localCurve : ValidCurves()) { |
| 7611 | if (localCurve == curve) { |
| 7612 | continue; |
| 7613 | } |
| 7614 | // Generate EC key on a different curve locally (with access to private key material). |
| 7615 | EVP_PKEY_Ptr privKey; |
| 7616 | vector<uint8_t> encodedPublicKey; |
| 7617 | GenerateLocalEcKey(localCurve, &privKey, &encodedPublicKey); |
| 7618 | |
| 7619 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7620 | string ZabFromKeyMintStr; |
| 7621 | EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, |
| 7622 | Finish(string(encodedPublicKey.begin(), encodedPublicKey.end()), |
| 7623 | &ZabFromKeyMintStr)); |
| 7624 | } |
| 7625 | |
| 7626 | CheckedDeleteKey(); |
| 7627 | } |
| 7628 | |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7629 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyAgreementTest); |
| 7630 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7631 | using DestroyAttestationIdsTest = KeyMintAidlTestBase; |
| 7632 | |
| 7633 | // This is a problematic test, as it can render the device under test permanently unusable. |
| 7634 | // Re-enable and run at your own risk. |
| 7635 | TEST_P(DestroyAttestationIdsTest, DISABLED_DestroyTest) { |
| 7636 | auto result = DestroyAttestationIds(); |
| 7637 | EXPECT_TRUE(result == ErrorCode::OK || result == ErrorCode::UNIMPLEMENTED); |
| 7638 | } |
| 7639 | |
| 7640 | INSTANTIATE_KEYMINT_AIDL_TEST(DestroyAttestationIdsTest); |
| 7641 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7642 | using EarlyBootKeyTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7643 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7644 | /* |
| 7645 | * EarlyBootKeyTest.CreateEarlyBootKeys |
| 7646 | * |
| 7647 | * Verifies that creating early boot keys succeeds, even at a later stage (after boot). |
| 7648 | */ |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7649 | TEST_P(EarlyBootKeyTest, CreateEarlyBootKeys) { |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7650 | // Early boot keys can be created after early boot. |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7651 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7652 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 7653 | |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7654 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
| 7655 | ASSERT_GT(keyData.blob.size(), 0U); |
| 7656 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 7657 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 7658 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7659 | CheckedDeleteKey(&aesKeyData.blob); |
| 7660 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7661 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7662 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7663 | } |
| 7664 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7665 | /* |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7666 | * EarlyBootKeyTest.CreateAttestedEarlyBootKey |
| 7667 | * |
| 7668 | * Verifies that creating an early boot key with attestation succeeds. |
| 7669 | */ |
| 7670 | TEST_P(EarlyBootKeyTest, CreateAttestedEarlyBootKey) { |
| 7671 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = CreateTestKeys( |
| 7672 | TAG_EARLY_BOOT_ONLY, ErrorCode::OK, [](AuthorizationSetBuilder* builder) { |
| 7673 | builder->AttestationChallenge("challenge"); |
| 7674 | builder->AttestationApplicationId("app_id"); |
| 7675 | }); |
| 7676 | |
| 7677 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 7678 | // Strongbox may not support factory attestation. Key creation might fail with |
| 7679 | // ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED |
| 7680 | if (SecLevel() == SecurityLevel::STRONGBOX && keyData.blob.size() == 0U) { |
| 7681 | continue; |
| 7682 | } |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7683 | ASSERT_GT(keyData.blob.size(), 0U); |
| 7684 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 7685 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 7686 | } |
| 7687 | CheckedDeleteKey(&aesKeyData.blob); |
| 7688 | CheckedDeleteKey(&hmacKeyData.blob); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 7689 | if (rsaKeyData.blob.size() != 0U) { |
| 7690 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7691 | } |
| 7692 | if (ecdsaKeyData.blob.size() != 0U) { |
| 7693 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7694 | } |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7695 | } |
| 7696 | |
| 7697 | /* |
| 7698 | * EarlyBootKeyTest.UseEarlyBootKeyFailure |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7699 | * |
| 7700 | * Verifies that using early boot keys at a later stage fails. |
| 7701 | */ |
| 7702 | TEST_P(EarlyBootKeyTest, UseEarlyBootKeyFailure) { |
| 7703 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7704 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7705 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 7706 | .HmacKey(128) |
| 7707 | .Digest(Digest::SHA_2_256) |
| 7708 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 7709 | AuthorizationSet output_params; |
| 7710 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, Begin(KeyPurpose::SIGN, key_blob_, |
| 7711 | AuthorizationSetBuilder() |
| 7712 | .Digest(Digest::SHA_2_256) |
| 7713 | .Authorization(TAG_MAC_LENGTH, 256), |
| 7714 | &output_params)); |
| 7715 | } |
| 7716 | |
| 7717 | /* |
| 7718 | * EarlyBootKeyTest.ImportEarlyBootKeyFailure |
| 7719 | * |
| 7720 | * Verifies that importing early boot keys fails. |
| 7721 | */ |
| 7722 | TEST_P(EarlyBootKeyTest, ImportEarlyBootKeyFailure) { |
| 7723 | ASSERT_EQ(ErrorCode::EARLY_BOOT_ENDED, ImportKey(AuthorizationSetBuilder() |
| 7724 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7725 | .Authorization(TAG_EARLY_BOOT_ONLY) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 7726 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7727 | .Digest(Digest::SHA_2_256) |
| 7728 | .SetDefaultValidity(), |
| 7729 | KeyFormat::PKCS8, ec_256_key)); |
| 7730 | } |
| 7731 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7732 | // This is a more comprehensive test, but it can only be run on a machine which is still in early |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7733 | // boot stage, which no proper Android device is by the time we can run VTS. To use this, |
| 7734 | // un-disable it and modify vold to remove the call to earlyBootEnded(). Running the test will end |
| 7735 | // early boot, so you'll have to reboot between runs. |
| 7736 | TEST_P(EarlyBootKeyTest, DISABLED_FullTest) { |
| 7737 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7738 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 7739 | // TAG_EARLY_BOOT_ONLY should be in hw-enforced. |
| 7740 | EXPECT_TRUE(HwEnforcedAuthorizations(aesKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7741 | EXPECT_TRUE( |
| 7742 | HwEnforcedAuthorizations(hmacKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7743 | EXPECT_TRUE(HwEnforcedAuthorizations(rsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7744 | EXPECT_TRUE( |
| 7745 | HwEnforcedAuthorizations(ecdsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7746 | |
| 7747 | // Should be able to use keys, since early boot has not ended |
| 7748 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 7749 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 7750 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 7751 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7752 | |
| 7753 | // End early boot |
| 7754 | ErrorCode earlyBootResult = GetReturnErrorCode(keyMint().earlyBootEnded()); |
| 7755 | EXPECT_EQ(earlyBootResult, ErrorCode::OK); |
| 7756 | |
| 7757 | // Should not be able to use already-created keys. |
| 7758 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseAesKey(aesKeyData.blob)); |
| 7759 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseHmacKey(hmacKeyData.blob)); |
| 7760 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseRsaKey(rsaKeyData.blob)); |
| 7761 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7762 | |
| 7763 | CheckedDeleteKey(&aesKeyData.blob); |
| 7764 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7765 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7766 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7767 | |
| 7768 | // Should not be able to create new keys |
| 7769 | std::tie(aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData) = |
| 7770 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::EARLY_BOOT_ENDED); |
| 7771 | |
| 7772 | CheckedDeleteKey(&aesKeyData.blob); |
| 7773 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7774 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7775 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7776 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7777 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7778 | INSTANTIATE_KEYMINT_AIDL_TEST(EarlyBootKeyTest); |
| 7779 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7780 | using UnlockedDeviceRequiredTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7781 | |
| 7782 | // This may be a problematic test. It can't be run repeatedly without unlocking the device in |
| 7783 | // between runs... and on most test devices there are no enrolled credentials so it can't be |
| 7784 | // unlocked at all, meaning the only way to get the test to pass again on a properly-functioning |
| 7785 | // device is to reboot it. For that reason, this is disabled by default. It can be used as part of |
| 7786 | // a manual test process, which includes unlocking between runs, which is why it's included here. |
| 7787 | // Well, that and the fact that it's the only test we can do without also making calls into the |
| 7788 | // Gatekeeper HAL. We haven't written any cross-HAL tests, and don't know what all of the |
| 7789 | // implications might be, so that may or may not be a solution. |
| 7790 | TEST_P(UnlockedDeviceRequiredTest, DISABLED_KeysBecomeUnusable) { |
| 7791 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7792 | CreateTestKeys(TAG_UNLOCKED_DEVICE_REQUIRED, ErrorCode::OK); |
| 7793 | |
| 7794 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 7795 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 7796 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 7797 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7798 | |
| 7799 | ErrorCode rc = GetReturnErrorCode( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7800 | keyMint().deviceLocked(false /* passwordOnly */, {} /* timestampToken */)); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7801 | ASSERT_EQ(ErrorCode::OK, rc); |
| 7802 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseAesKey(aesKeyData.blob)); |
| 7803 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseHmacKey(hmacKeyData.blob)); |
| 7804 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseRsaKey(rsaKeyData.blob)); |
| 7805 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7806 | |
| 7807 | CheckedDeleteKey(&aesKeyData.blob); |
| 7808 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7809 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7810 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7811 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7812 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7813 | INSTANTIATE_KEYMINT_AIDL_TEST(UnlockedDeviceRequiredTest); |
| 7814 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 7815 | } // namespace aidl::android::hardware::security::keymint::test |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7816 | |
| 7817 | int main(int argc, char** argv) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7818 | std::cout << "Testing "; |
| 7819 | auto halInstances = |
| 7820 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase::build_params(); |
| 7821 | std::cout << "HAL instances:\n"; |
| 7822 | for (auto& entry : halInstances) { |
| 7823 | std::cout << " " << entry << '\n'; |
| 7824 | } |
| 7825 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7826 | ::testing::InitGoogleTest(&argc, argv); |
| 7827 | for (int i = 1; i < argc; ++i) { |
| 7828 | if (argv[i][0] == '-') { |
| 7829 | if (std::string(argv[i]) == "--arm_deleteAllKeys") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7830 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 7831 | arm_deleteAllKeys = true; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7832 | } |
| 7833 | if (std::string(argv[i]) == "--dump_attestations") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7834 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 7835 | dump_Attestations = true; |
| 7836 | } else { |
| 7837 | std::cout << "NOT dumping attestations" << std::endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7838 | } |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 7839 | if (std::string(argv[i]) == "--skip_boot_pl_check") { |
| 7840 | // Allow checks of BOOT_PATCHLEVEL to be disabled, so that the tests can |
| 7841 | // be run in emulated environments that don't have the normal bootloader |
| 7842 | // interactions. |
| 7843 | aidl::android::hardware::security::keymint::test::check_boot_pl = false; |
| 7844 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7845 | } |
| 7846 | } |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 7847 | return RUN_ALL_TESTS(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7848 | } |