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 | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 73 | // Whether to check that BOOT_PATCHLEVEL is populated. |
| 74 | bool check_boot_pl = true; |
| 75 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 76 | // The maximum number of times we'll attempt to verify that corruption |
David Drysdale | 4c1f6ac | 2021-11-25 16:08:29 +0000 | [diff] [blame] | 77 | // 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] | 78 | // is a small (roughly 1/256) chance that corrupting ciphertext still results |
| 79 | // in valid PKCS7 padding. |
| 80 | constexpr size_t kMaxPaddingCorruptionRetries = 8; |
| 81 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 82 | template <TagType tag_type, Tag tag, typename ValueT> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 83 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag> ttag, |
| 84 | ValueT expected_value) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 85 | auto it = std::find_if(set.begin(), set.end(), [&](const KeyParameter& param) { |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 86 | if (auto p = authorizationValue(ttag, param)) { |
| 87 | return *p == expected_value; |
| 88 | } |
| 89 | return false; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 90 | }); |
| 91 | return (it != set.end()); |
| 92 | } |
| 93 | |
| 94 | template <TagType tag_type, Tag tag> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 95 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag>) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 96 | auto it = std::find_if(set.begin(), set.end(), |
| 97 | [&](const KeyParameter& param) { return param.tag == tag; }); |
| 98 | return (it != set.end()); |
| 99 | } |
| 100 | |
| 101 | constexpr char hex_value[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 102 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 103 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 104 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9' |
| 105 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F' |
| 106 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 107 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f' |
| 108 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 109 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 110 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 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 | |
| 118 | string hex2str(string a) { |
| 119 | string b; |
| 120 | size_t num = a.size() / 2; |
| 121 | b.resize(num); |
| 122 | for (size_t i = 0; i < num; i++) { |
| 123 | b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]); |
| 124 | } |
| 125 | return b; |
| 126 | } |
| 127 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 128 | string rsa_key = hex2str( |
| 129 | // RFC 5208 s5 |
| 130 | "30820275" // SEQUENCE length 0x275 (PrivateKeyInfo) { |
| 131 | "020100" // INTEGER length 1 value 0x00 (version) |
| 132 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 133 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 134 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 135 | "0500" // NULL (parameters) |
| 136 | // } end SEQUENCE (AlgorithmIdentifier) |
| 137 | "0482025f" // OCTET STRING length 0x25f (privateKey) holding... |
| 138 | // RFC 8017 A.1.2 |
| 139 | "3082025b" // SEQUENCE length 0x25b (RSAPrivateKey) { |
| 140 | "020100" // INTEGER length 1 value 0x00 (version) |
| 141 | "028181" // INTEGER length 0x81 value (modulus) ... |
| 142 | "00c6095409047d8634812d5a218176e4" |
| 143 | "5c41d60a75b13901f234226cffe77652" |
| 144 | "1c5a77b9e389417b71c0b6a44d13afe4" |
| 145 | "e4a2805d46c9da2935adb1ff0c1f24ea" |
| 146 | "06e62b20d776430a4d435157233c6f91" |
| 147 | "6783c30e310fcbd89b85c2d567711697" |
| 148 | "85ac12bca244abda72bfb19fc44d27c8" |
| 149 | "1e1d92de284f4061edfd99280745ea6d" |
| 150 | "25" |
| 151 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 152 | "028180" // INTEGER length 0x80 (privateExponent) value... |
| 153 | "1be0f04d9cae3718691f035338308e91" |
| 154 | "564b55899ffb5084d2460e6630257e05" |
| 155 | "b3ceab02972dfabcd6ce5f6ee2589eb6" |
| 156 | "7911ed0fac16e43a444b8c861e544a05" |
| 157 | "93365772f8baf6b22fc9e3c5f1024b06" |
| 158 | "3ac080a7b2234cf8aee8f6c47bbf4fd3" |
| 159 | "ace7240290bef16c0b3f7f3cdd64ce3a" |
| 160 | "b5912cf6e32f39ab188358afcccd8081" |
| 161 | "0241" // INTEGER length 0x41 (prime1) |
| 162 | "00e4b49ef50f765d3b24dde01aceaaf1" |
| 163 | "30f2c76670a91a61ae08af497b4a82be" |
| 164 | "6dee8fcdd5e3f7ba1cfb1f0c926b88f8" |
| 165 | "8c92bfab137fba2285227b83c342ff7c" |
| 166 | "55" |
| 167 | "0241" // INTEGER length 0x41 (prime2) |
| 168 | "00ddabb5839c4c7f6bf3d4183231f005" |
| 169 | "b31aa58affdda5c79e4cce217f6bc930" |
| 170 | "dbe563d480706c24e9ebfcab28a6cdef" |
| 171 | "d324b77e1bf7251b709092c24ff501fd" |
| 172 | "91" |
| 173 | "0240" // INTEGER length 0x40 (exponent1) |
| 174 | "23d4340eda3445d8cd26c14411da6fdc" |
| 175 | "a63c1ccd4b80a98ad52b78cc8ad8beb2" |
| 176 | "842c1d280405bc2f6c1bea214a1d742a" |
| 177 | "b996b35b63a82a5e470fa88dbf823cdd" |
| 178 | "0240" // INTEGER length 0x40 (exponent2) |
| 179 | "1b7b57449ad30d1518249a5f56bb9829" |
| 180 | "4d4b6ac12ffc86940497a5a5837a6cf9" |
| 181 | "46262b494526d328c11e1126380fde04" |
| 182 | "c24f916dec250892db09a6d77cdba351" |
| 183 | "0240" // INTEGER length 0x40 (coefficient) |
| 184 | "7762cd8f4d050da56bd591adb515d24d" |
| 185 | "7ccd32cca0d05f866d583514bd7324d5" |
| 186 | "f33645e8ed8b4a1cb3cc4a1d67987399" |
| 187 | "f2a09f5b3fb68c88d5e5d90ac33492d6" |
| 188 | // } end SEQUENCE (PrivateKey) |
| 189 | // } end SEQUENCE (PrivateKeyInfo) |
| 190 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 191 | |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 192 | /* |
| 193 | * DER-encoded PKCS#8 format RSA key. Generated using: |
| 194 | * |
| 195 | * openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 196 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 197 | string rsa_2048_key = hex2str( |
| 198 | // RFC 5208 s5 |
| 199 | "308204BD" // SEQUENCE length 0x4bd (PrivateKeyInfo) { |
| 200 | "020100" // INTEGER length 1 value 0x00 (version) |
| 201 | "300D" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 202 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 203 | "2A864886F70D010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 204 | "0500" // NULL (parameters) |
| 205 | // } end SEQUENCE (AlgorithmIdentifier) |
| 206 | "048204A7" // OCTET STRING length 0x25f (privateKey) holding... |
| 207 | // RFC 8017 A.1.2 |
| 208 | "308204A3" // SEQUENCE length 0x4a3 (RSAPrivateKey) { |
| 209 | "020100" // INTEGER length 1 value 0x00 (version) |
| 210 | "02820101" // INTEGER length 0x101 value (modulus) ... |
| 211 | "00BEBC342B56D443B1299F9A6A7056E8" |
| 212 | "0A897E318476A5A18029E63B2ED739A6" |
| 213 | "1791D339F58DC763D9D14911F2EDEC38" |
| 214 | "3DEE11F6319B44510E7A3ECD9B79B973" |
| 215 | "82E49500ACF8117DC89CAF0E621F7775" |
| 216 | "6554A2FD4664BFE7AB8B59AB48340DBF" |
| 217 | "A27B93B5A81F6ECDEB02D0759307128D" |
| 218 | "F3E3BAD4055C8B840216DFAA5700670E" |
| 219 | "6C5126F0962FCB70FF308F25049164CC" |
| 220 | "F76CC2DA66A7DD9A81A714C2809D6918" |
| 221 | "6133D29D84568E892B6FFBF3199BDB14" |
| 222 | "383EE224407F190358F111A949552ABA" |
| 223 | "6714227D1BD7F6B20DD0CB88F9467B71" |
| 224 | "9339F33BFF35B3870B3F62204E4286B0" |
| 225 | "948EA348B524544B5F9838F29EE643B0" |
| 226 | "79EEF8A713B220D7806924CDF7295070" |
| 227 | "C5" |
| 228 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 229 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 230 | "69F377F35F2F584EF075353CCD1CA997" |
| 231 | "38DB3DBC7C7FF35F9366CE176DFD1B13" |
| 232 | "5AB10030344ABF5FBECF1D4659FDEF1C" |
| 233 | "0FC430834BE1BE3911951377BB3D563A" |
| 234 | "2EA9CA8F4AD9C48A8CE6FD516A735C66" |
| 235 | "2686C7B4B3C09A7B8354133E6F93F790" |
| 236 | "D59EAEB92E84C9A4339302CCE28FDF04" |
| 237 | "CCCAFA7DE3F3A827D4F6F7D38E68B0EC" |
| 238 | "6AB706645BF074A4E4090D06FB163124" |
| 239 | "365FD5EE7A20D350E9958CC30D91326E" |
| 240 | "1B292E9EF5DB408EC42DAF737D201497" |
| 241 | "04D0A678A0FB5B5446863B099228A352" |
| 242 | "D604BA8091A164D01D5AB05397C71EAD" |
| 243 | "20BE2A08FC528FE442817809C787FEE4" |
| 244 | "AB97F97B9130D022153EDC6EB6CBE7B0" |
| 245 | "F8E3473F2E901209B5DB10F93604DB01" |
| 246 | "028181" // INTEGER length 0x81 (prime1) |
| 247 | "00E83C0998214941EA4F9293F1B77E2E" |
| 248 | "99E6CF305FAF358238E126124FEAF2EB" |
| 249 | "9724B2EA7B78E6032343821A80E55D1D" |
| 250 | "88FB12D220C3F41A56142FEC85796D19" |
| 251 | "17F1E8C774F142B67D3D6E7B7E6B4383" |
| 252 | "E94DB5929089DBB346D5BDAB40CC2D96" |
| 253 | "EE0409475E175C63BF78CFD744136740" |
| 254 | "838127EA723FF3FE7FA368C1311B4A4E" |
| 255 | "05" |
| 256 | "028181" // INTEGER length 0x81 (prime2) |
| 257 | "00D240FCC0F5D7715CDE21CB2DC86EA1" |
| 258 | "46132EA3B06F61FF2AF54BF38473F59D" |
| 259 | "ADCCE32B5F4CC32DD0BA6F509347B4B5" |
| 260 | "B1B58C39F95E4798CCBB43E83D0119AC" |
| 261 | "F532F359CA743C85199F0286610E2009" |
| 262 | "97D7312917179AC9B67558773212EC96" |
| 263 | "1E8BCE7A3CC809BC5486A96E4B0E6AF3" |
| 264 | "94D94E066A0900B7B70E82A44FB30053" |
| 265 | "C1" |
| 266 | "028181" // INTEGER length 0x81 (exponent1) |
| 267 | "00AD15DA1CBD6A492B66851BA8C316D3" |
| 268 | "8AB700E2CFDDD926A658003513C54BAA" |
| 269 | "152B30021D667D20078F500F8AD3E7F3" |
| 270 | "945D74A891ED1A28EAD0FEEAEC8C14A8" |
| 271 | "E834CF46A13D1378C99D18940823CFDD" |
| 272 | "27EC5810D59339E0C34198AC638E09C8" |
| 273 | "7CBB1B634A9864AE9F4D5EB2D53514F6" |
| 274 | "7B4CAEC048C8AB849A02E397618F3271" |
| 275 | "35" |
| 276 | "028180" // INTEGER length 0x80 (exponent2) |
| 277 | "1FA2C1A5331880A92D8F3E281C617108" |
| 278 | "BF38244F16E352E69ED417C7153F9EC3" |
| 279 | "18F211839C643DCF8B4DD67CE2AC312E" |
| 280 | "95178D5D952F06B1BF779F4916924B70" |
| 281 | "F582A23F11304E02A5E7565AE22A35E7" |
| 282 | "4FECC8B6FDC93F92A1A37703E4CF0E63" |
| 283 | "783BD02EB716A7ECBBFA606B10B74D01" |
| 284 | "579522E7EF84D91FC522292108D902C1" |
| 285 | "028180" // INTEGER length 0x80 (coefficient) |
| 286 | "796FE3825F9DCC85DF22D58690065D93" |
| 287 | "898ACD65C087BEA8DA3A63BF4549B795" |
| 288 | "E2CD0E3BE08CDEBD9FCF1720D9CDC507" |
| 289 | "0D74F40DED8E1102C52152A31B6165F8" |
| 290 | "3A6722AECFCC35A493D7634664B888A0" |
| 291 | "8D3EB034F12EA28BFEE346E205D33482" |
| 292 | "7F778B16ED40872BD29FCB36536B6E93" |
| 293 | "FFB06778696B4A9D81BB0A9423E63DE5" |
| 294 | // } end SEQUENCE (PrivateKey) |
| 295 | // } end SEQUENCE (PrivateKeyInfo) |
| 296 | ); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 297 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 298 | string ec_256_key = hex2str( |
| 299 | // RFC 5208 s5 |
| 300 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 301 | "020100" // INTEGER length 1 value 0 (version) |
| 302 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 303 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 304 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 305 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 306 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 307 | // } end SEQUENCE (AlgorithmIdentifier) |
| 308 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 309 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 310 | "020101" // INTEGER length 1 value 1 (version) |
| 311 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 312 | "737c2ecd7b8d1940bf2930aa9b4ed3ff" |
| 313 | "941eed09366bc03299986481f3a4d859" |
| 314 | "a144" // TAG [1] len 0x44 (publicKey) { |
| 315 | "03420004bf85d7720d07c25461683bc6" |
| 316 | "48b4778a9a14dd8a024e3bdd8c7ddd9a" |
| 317 | "b2b528bbc7aa1b51f14ebbbb0bd0ce21" |
| 318 | "bcc41c6eb00083cf3376d11fd44949e0" |
| 319 | "b2183bfe" |
| 320 | // } end SEQUENCE (ECPrivateKey) |
| 321 | // } end SEQUENCE (PrivateKeyInfo) |
| 322 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 323 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 324 | string ec_521_key = hex2str( |
| 325 | // RFC 5208 s5 |
| 326 | "3081EE" // SEQUENCE length 0xee (PrivateKeyInfo) { |
| 327 | "020100" // INTEGER length 1 value 0 (version) |
| 328 | "3010" // SEQUENCE length 0x10 (AlgorithmIdentifier) { |
| 329 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 330 | "2A8648CE3D0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 331 | "0605" // OBJECT IDENTIFIER length 5 (param) |
| 332 | "2B81040023" // 1.3.132.0.35 (secp521r1) |
| 333 | // } end SEQUENCE (AlgorithmIdentifier) |
| 334 | "0481D6" // OCTET STRING length 0xd6 (privateKey) holding... |
| 335 | "3081D3" // SEQUENCE length 0xd3 (ECPrivateKey) |
| 336 | "020101" // INTEGER length 1 value 1 (version) |
| 337 | "0442" // OCTET STRING length 0x42 (privateKey) |
| 338 | "0011458C586DB5DAA92AFAB03F4FE46A" |
| 339 | "A9D9C3CE9A9B7A006A8384BEC4C78E8E" |
| 340 | "9D18D7D08B5BCFA0E53C75B064AD51C4" |
| 341 | "49BAE0258D54B94B1E885DED08ED4FB2" |
| 342 | "5CE9" |
| 343 | "A18189" // TAG [1] len 0x89 (publicKey) { |
| 344 | "03818600040149EC11C6DF0FA122C6A9" |
| 345 | "AFD9754A4FA9513A627CA329E349535A" |
| 346 | "5629875A8ADFBE27DCB932C051986377" |
| 347 | "108D054C28C6F39B6F2C9AF81802F9F3" |
| 348 | "26B842FF2E5F3C00AB7635CFB36157FC" |
| 349 | "0882D574A10D839C1A0C049DC5E0D775" |
| 350 | "E2EE50671A208431BB45E78E70BEFE93" |
| 351 | "0DB34818EE4D5C26259F5C6B8E28A652" |
| 352 | "950F9F88D7B4B2C9D9" |
| 353 | // } end SEQUENCE (ECPrivateKey) |
| 354 | // } end SEQUENCE (PrivateKeyInfo) |
| 355 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 356 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 357 | string ec_256_key_rfc5915 = hex2str( |
| 358 | // RFC 5208 s5 |
| 359 | "308193" // SEQUENCE length 0x93 (PrivateKeyInfo) { |
| 360 | "020100" // INTEGER length 1 value 0 (version) |
| 361 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 362 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 363 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 364 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 365 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 366 | // } end SEQUENCE (AlgorithmIdentifier) |
| 367 | "0479" // OCTET STRING length 0x79 (privateKey) holding... |
| 368 | // RFC 5915 s3 |
| 369 | "3077" // SEQUENCE length 0x77 (ECPrivateKey) |
| 370 | "020101" // INTEGER length 1 value 1 (version) |
| 371 | "0420" // OCTET STRING length 0x42 (privateKey) |
| 372 | "782370a8c8ce5537baadd04dcff079c8" |
| 373 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 374 | "a00a" // TAG [0] length 0xa (parameters) |
| 375 | "0608" // OBJECT IDENTIFIER length 8 |
| 376 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 377 | // } end TAG [0] |
| 378 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 379 | "0342" // BIT STRING length 0x42 |
| 380 | "00" // no pad bits |
| 381 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 382 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 383 | "8e68c905464666f98dad4f01573ba810" |
| 384 | "78b3428570a439ba3229fbc026c55068" |
| 385 | "2f" |
| 386 | // } end SEQUENCE (ECPrivateKey) |
| 387 | // } end SEQUENCE (PrivateKeyInfo) |
| 388 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 389 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 390 | string ec_256_key_sec1 = hex2str( |
| 391 | // RFC 5208 s5 |
| 392 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 393 | "020100" // INTEGER length 1 value 0 (version) |
| 394 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 395 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 396 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 397 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 398 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 399 | // } end SEQUENCE (AlgorithmIdentifier) |
| 400 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 401 | // SEC1-v2 C.4 |
| 402 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 403 | "020101" // INTEGER length 1 value 0x01 (version) |
| 404 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 405 | "782370a8c8ce5537baadd04dcff079c8" |
| 406 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 407 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 408 | "0342" // BIT STRING length 0x42 |
| 409 | "00" // no pad bits |
| 410 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 411 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 412 | "8e68c905464666f98dad4f01573ba810" |
| 413 | "78b3428570a439ba3229fbc026c55068" |
| 414 | "2f" |
| 415 | // } end TAG [1] (publicKey) |
| 416 | // } end SEQUENCE (PrivateKeyInfo) |
| 417 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 418 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 419 | /** |
| 420 | * Ed25519 key pair generated as follows: |
| 421 | * ``` |
| 422 | * % openssl req -x509 -newkey ED25519 -days 700 -nodes \ |
| 423 | * -keyout ed25519_priv.key -out ed25519.pem * -subj "/CN=fake.ed25519.com" |
| 424 | * Generating a ED25519 private key writing new private key to |
| 425 | * 'ed25519_priv.key' |
| 426 | * ----- |
| 427 | * % cat ed25519_priv.key |
| 428 | * -----BEGIN PRIVATE KEY----- |
| 429 | * MC4CAQAwBQYDK2VwBCIEIKl3A5quNywcj1P+0XI9SBalFPIvO52NxceMLRH6dVmR |
| 430 | * -----END PRIVATE KEY----- |
| 431 | * % der2ascii -pem -i ed25519_priv.key |
| 432 | * SEQUENCE { |
| 433 | * INTEGER { 0 } |
| 434 | * SEQUENCE { |
| 435 | * # ed25519 |
| 436 | * OBJECT_IDENTIFIER { 1.3.101.112 } |
| 437 | * } |
| 438 | * OCTET_STRING { |
| 439 | * OCTET_STRING { `a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991` } |
| 440 | * } |
| 441 | * } |
| 442 | * % cat ed25519.pem |
| 443 | * -----BEGIN CERTIFICATE----- |
| 444 | * MIIBSjCB/aADAgECAhR0Jron3eKcdgqyecv/eEfGWAzn8DAFBgMrZXAwGzEZMBcG |
| 445 | * A1UEAwwQZmFrZS5lZDI1NTE5LmNvbTAeFw0yMTEwMjAwODI3NDJaFw0yMzA5MjAw |
| 446 | * ODI3NDJaMBsxGTAXBgNVBAMMEGZha2UuZWQyNTUxOS5jb20wKjAFBgMrZXADIQDv |
| 447 | * uwHz+3TaQ69D2digxlz0fFfsZg0rPqgQae3jBPRWkaNTMFEwHQYDVR0OBBYEFN9O |
| 448 | * od30SY4JTs66ZR403UPya+iXMB8GA1UdIwQYMBaAFN9Ood30SY4JTs66ZR403UPy |
| 449 | * a+iXMA8GA1UdEwEB/wQFMAMBAf8wBQYDK2VwA0EAKjVrYQjuE/gEL2j/ABpDbFjV |
| 450 | * Ilg5tJ6MN/P3psAv3Cs7f0X1lFqdlt15nJ/6aj2cmGCwNRXt5wcyYDKNu+v2Dw== |
| 451 | * -----END CERTIFICATE----- |
| 452 | * % openssl x509 -in ed25519.pem -text -noout |
| 453 | * Certificate: |
| 454 | * Data: |
| 455 | * Version: 3 (0x2) |
| 456 | * Serial Number: |
| 457 | * 74:26:ba:27:dd:e2:9c:76:0a:b2:79:cb:ff:78:47:c6:58:0c:e7:f0 |
| 458 | * Signature Algorithm: ED25519 |
| 459 | * Issuer: CN = fake.ed25519.com |
| 460 | * Validity |
| 461 | * Not Before: Oct 20 08:27:42 2021 GMT |
| 462 | * Not After : Sep 20 08:27:42 2023 GMT |
| 463 | * Subject: CN = fake.ed25519.com |
| 464 | * Subject Public Key Info: |
| 465 | * Public Key Algorithm: ED25519 |
| 466 | * ED25519 Public-Key: |
| 467 | * pub: |
| 468 | * ef:bb:01:f3:fb:74:da:43:af:43:d9:d8:a0:c6:5c: |
| 469 | * f4:7c:57:ec:66:0d:2b:3e:a8:10:69:ed:e3:04:f4: |
| 470 | * 56:91 |
| 471 | * X509v3 extensions: |
| 472 | * X509v3 Subject Key Identifier: |
| 473 | * DF:4E:A1:DD:F4:49:8E:09:4E:CE:BA:65:1E:34:DD:43:F2:6B:E8:97 |
| 474 | * X509v3 Authority Key Identifier: |
| 475 | * keyid:DF:4E:A1:DD:F4:49:8E:09:4E:CE:BA:65:1E:34:DD:43:F2:6B:E8:97 |
| 476 | * |
| 477 | * X509v3 Basic Constraints: critical |
| 478 | * CA:TRUE |
| 479 | * Signature Algorithm: ED25519 |
| 480 | * 2a:35:6b:61:08:ee:13:f8:04:2f:68:ff:00:1a:43:6c:58:d5: |
| 481 | * 22:58:39:b4:9e:8c:37:f3:f7:a6:c0:2f:dc:2b:3b:7f:45:f5: |
| 482 | * 94:5a:9d:96:dd:79:9c:9f:fa:6a:3d:9c:98:60:b0:35:15:ed: |
| 483 | * e7:07:32:60:32:8d:bb:eb:f6:0f |
| 484 | * ``` |
| 485 | */ |
| 486 | string ed25519_key = hex2str("a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991"); |
| 487 | string ed25519_pkcs8_key = hex2str( |
| 488 | // RFC 5208 s5 |
| 489 | "302e" // SEQUENCE length 0x2e (PrivateKeyInfo) { |
| 490 | "0201" // INTEGER length 1 (Version) |
| 491 | "00" // version 0 |
| 492 | "3005" // SEQUENCE length 05 (AlgorithmIdentifier) { |
| 493 | "0603" // OBJECT IDENTIFIER length 3 (algorithm) |
| 494 | "2b6570" // 1.3.101.112 (id-Ed125519 RFC 8410 s3) |
| 495 | // } end SEQUENCE (AlgorithmIdentifier) |
| 496 | "0422" // OCTET STRING length 0x22 (PrivateKey) |
| 497 | "0420" // OCTET STRING length 0x20 (RFC 8410 s7) |
| 498 | "a977039aae372c1c8f53fed1723d4816a514f22f3b9d8dc5c78c2d11fa755991" |
| 499 | // } end SEQUENCE (PrivateKeyInfo) |
| 500 | ); |
| 501 | string ed25519_pubkey = hex2str("efbb01f3fb74da43af43d9d8a0c65cf47c57ec660d2b3ea81069ede304f45691"); |
| 502 | |
| 503 | /** |
| 504 | * X25519 key pair generated as follows: |
| 505 | * ``` |
| 506 | * % openssl genpkey -algorithm X25519 > x25519_priv.key |
| 507 | * % cat x25519_priv.key |
| 508 | * -----BEGIN PRIVATE KEY----- |
| 509 | * MC4CAQAwBQYDK2VuBCIEIGgPwF3NLwQx/Sfwr2nfJvXitwlDNh3Skzh+TISN/y1C |
| 510 | * -----END PRIVATE KEY----- |
| 511 | * % der2ascii -pem -i x25519_priv.key |
| 512 | * SEQUENCE { |
| 513 | * INTEGER { 0 } |
| 514 | * SEQUENCE { |
| 515 | * # x25519 |
| 516 | * OBJECT_IDENTIFIER { 1.3.101.110 } |
| 517 | * } |
| 518 | * OCTET_STRING { |
| 519 | * OCTET_STRING { `680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42` } |
| 520 | * } |
| 521 | * } |
| 522 | * ``` |
| 523 | */ |
| 524 | |
| 525 | string x25519_key = hex2str("680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42"); |
| 526 | string x25519_pkcs8_key = hex2str( |
| 527 | // RFC 5208 s5 |
| 528 | "302e" // SEQUENCE length 0x2e (PrivateKeyInfo) { |
| 529 | "0201" // INTEGER length 1 (Version) |
| 530 | "00" // version 0 |
| 531 | "3005" // SEQUENCE length 05 (AlgorithmIdentifier) { |
| 532 | "0603" // OBJECT IDENTIFIER length 3 (algorithm) |
| 533 | "2b656e" // 1.3.101.110 (id-X125519 RFC 8410 s3) |
| 534 | "0422" // OCTET STRING length 0x22 (PrivateKey) |
| 535 | "0420" // OCTET STRING length 0x20 (RFC 8410 s7) |
| 536 | "680fc05dcd2f0431fd27f0af69df26f5e2b70943361dd293387e4c848dff2d42"); |
| 537 | string x25519_pubkey = hex2str("be46925a857f17831d6d454b9d3d36a4a30166edf80eb82b684661c3e258f768"); |
| 538 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 539 | struct RSA_Delete { |
| 540 | void operator()(RSA* p) { RSA_free(p); } |
| 541 | }; |
| 542 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 543 | std::string make_string(const uint8_t* data, size_t length) { |
| 544 | return std::string(reinterpret_cast<const char*>(data), length); |
| 545 | } |
| 546 | |
| 547 | template <size_t N> |
| 548 | std::string make_string(const uint8_t (&a)[N]) { |
| 549 | return make_string(a, N); |
| 550 | } |
| 551 | |
| 552 | class AidlBuf : public vector<uint8_t> { |
| 553 | typedef vector<uint8_t> super; |
| 554 | |
| 555 | public: |
| 556 | AidlBuf() {} |
| 557 | AidlBuf(const super& other) : super(other) {} |
| 558 | AidlBuf(super&& other) : super(std::move(other)) {} |
| 559 | explicit AidlBuf(const std::string& other) : AidlBuf() { *this = other; } |
| 560 | |
| 561 | AidlBuf& operator=(const super& other) { |
| 562 | super::operator=(other); |
| 563 | return *this; |
| 564 | } |
| 565 | |
| 566 | AidlBuf& operator=(super&& other) { |
| 567 | super::operator=(std::move(other)); |
| 568 | return *this; |
| 569 | } |
| 570 | |
| 571 | AidlBuf& operator=(const string& other) { |
| 572 | resize(other.size()); |
| 573 | for (size_t i = 0; i < other.size(); ++i) { |
| 574 | (*this)[i] = static_cast<uint8_t>(other[i]); |
| 575 | } |
| 576 | return *this; |
| 577 | } |
| 578 | |
| 579 | string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); } |
| 580 | }; |
| 581 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 582 | string device_suffix(const string& name) { |
| 583 | size_t pos = name.find('/'); |
| 584 | if (pos == string::npos) { |
| 585 | return name; |
| 586 | } |
| 587 | return name.substr(pos + 1); |
| 588 | } |
| 589 | |
| 590 | bool matching_rp_instance(const string& km_name, |
| 591 | std::shared_ptr<IRemotelyProvisionedComponent>* rp) { |
| 592 | string km_suffix = device_suffix(km_name); |
| 593 | |
| 594 | vector<string> rp_names = |
| 595 | ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor); |
| 596 | for (const string& rp_name : rp_names) { |
| 597 | // If the suffix of the RemotelyProvisionedComponent instance equals the suffix of the |
| 598 | // KeyMint instance, assume they match. |
| 599 | if (device_suffix(rp_name) == km_suffix && AServiceManager_isDeclared(rp_name.c_str())) { |
| 600 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(rp_name.c_str())); |
| 601 | *rp = IRemotelyProvisionedComponent::fromBinder(binder); |
| 602 | return true; |
| 603 | } |
| 604 | } |
| 605 | return false; |
| 606 | } |
| 607 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 608 | } // namespace |
| 609 | |
| 610 | class NewKeyGenerationTest : public KeyMintAidlTestBase { |
| 611 | protected: |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 612 | void CheckBaseParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 613 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 614 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 615 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 616 | // Check that some unexpected tags/values are NOT present. |
| 617 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 618 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void CheckSymmetricParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 622 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
| 623 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 624 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 625 | |
| 626 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | AuthorizationSet CheckCommonParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 630 | // TODO(swillden): Distinguish which params should be in which auth list. |
| 631 | AuthorizationSet auths; |
| 632 | for (auto& entry : keyCharacteristics) { |
| 633 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 634 | } |
| 635 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 636 | |
| 637 | // Verify that App data, ROT and auth timeout are NOT included. |
| 638 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 639 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 640 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 641 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 642 | // None of the tests specify CREATION_DATETIME so check that the KeyMint implementation |
| 643 | // never adds it. |
| 644 | EXPECT_FALSE(auths.Contains(TAG_CREATION_DATETIME)); |
| 645 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 646 | // Check OS details match the original hardware info. |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 647 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 648 | EXPECT_TRUE(os_ver); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 649 | EXPECT_EQ(*os_ver, os_version()); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 650 | auto os_pl = auths.GetTagValue(TAG_OS_PATCHLEVEL); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 651 | EXPECT_TRUE(os_pl); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 652 | EXPECT_EQ(*os_pl, os_patch_level()); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 653 | |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 654 | // Should include vendor patchlevel. |
David Drysdale | f5bfa00 | 2021-09-27 17:30:41 +0100 | [diff] [blame] | 655 | auto vendor_pl = auths.GetTagValue(TAG_VENDOR_PATCHLEVEL); |
| 656 | EXPECT_TRUE(vendor_pl); |
| 657 | EXPECT_EQ(*vendor_pl, vendor_patch_level()); |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 658 | |
| 659 | // Should include boot patchlevel (but there are some test scenarios where this is not |
| 660 | // possible). |
| 661 | if (check_boot_pl) { |
| 662 | auto boot_pl = auths.GetTagValue(TAG_BOOT_PATCHLEVEL); |
| 663 | EXPECT_TRUE(boot_pl); |
| 664 | } |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 665 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 666 | return auths; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 667 | } |
| 668 | }; |
| 669 | |
| 670 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 671 | * NewKeyGenerationTest.Aes |
| 672 | * |
| 673 | * Verifies that keymint can generate all required AES key sizes, and that the resulting keys |
| 674 | * have correct characteristics. |
| 675 | */ |
| 676 | TEST_P(NewKeyGenerationTest, Aes) { |
| 677 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 678 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 679 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 680 | SCOPED_TRACE(testing::Message() |
| 681 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 682 | vector<uint8_t> key_blob; |
| 683 | vector<KeyCharacteristics> key_characteristics; |
| 684 | auto builder = AuthorizationSetBuilder() |
| 685 | .AesEncryptionKey(key_size) |
| 686 | .BlockMode(block_mode) |
| 687 | .Padding(padding_mode) |
| 688 | .SetDefaultValidity(); |
| 689 | if (block_mode == BlockMode::GCM) { |
| 690 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 691 | } |
| 692 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder, &key_blob, &key_characteristics)); |
| 693 | |
| 694 | EXPECT_GT(key_blob.size(), 0U); |
| 695 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 696 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 697 | |
| 698 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 699 | |
| 700 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::AES)); |
| 701 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 702 | << "Key size " << key_size << "missing"; |
| 703 | |
| 704 | CheckedDeleteKey(&key_blob); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * NewKeyGenerationTest.AesInvalidSize |
| 712 | * |
| 713 | * Verifies that specifying an invalid key size for AES key generation returns |
| 714 | * UNSUPPORTED_KEY_SIZE. |
| 715 | */ |
| 716 | TEST_P(NewKeyGenerationTest, AesInvalidSize) { |
| 717 | for (auto key_size : InvalidKeySizes(Algorithm::AES)) { |
| 718 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 719 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 720 | SCOPED_TRACE(testing::Message() |
| 721 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 722 | vector<uint8_t> key_blob; |
| 723 | vector<KeyCharacteristics> key_characteristics; |
| 724 | auto builder = AuthorizationSetBuilder() |
| 725 | .AesEncryptionKey(key_size) |
| 726 | .BlockMode(block_mode) |
| 727 | .Padding(padding_mode) |
| 728 | .SetDefaultValidity(); |
| 729 | if (block_mode == BlockMode::GCM) { |
| 730 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 731 | } |
| 732 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 733 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 739 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 740 | vector<uint8_t> key_blob; |
| 741 | vector<KeyCharacteristics> key_characteristics; |
| 742 | // No key size specified |
| 743 | auto builder = AuthorizationSetBuilder() |
| 744 | .Authorization(TAG_ALGORITHM, Algorithm::AES) |
| 745 | .BlockMode(block_mode) |
| 746 | .Padding(padding_mode) |
| 747 | .SetDefaultValidity(); |
| 748 | if (block_mode == BlockMode::GCM) { |
| 749 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 750 | } |
| 751 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 752 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * NewKeyGenerationTest.AesInvalidPadding |
| 759 | * |
| 760 | * Verifies that specifying an invalid padding on AES keys gives a failure |
| 761 | * somewhere along the way. |
| 762 | */ |
| 763 | TEST_P(NewKeyGenerationTest, AesInvalidPadding) { |
| 764 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 765 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 766 | for (auto padding_mode : InvalidPaddingModes(Algorithm::AES, block_mode)) { |
| 767 | SCOPED_TRACE(testing::Message() |
| 768 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 769 | auto builder = AuthorizationSetBuilder() |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 770 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 771 | .AesEncryptionKey(key_size) |
| 772 | .BlockMode(block_mode) |
| 773 | .Padding(padding_mode) |
| 774 | .SetDefaultValidity(); |
| 775 | if (block_mode == BlockMode::GCM) { |
| 776 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 777 | } |
| 778 | |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 779 | auto result = GenerateKey(builder); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 780 | if (result == ErrorCode::OK) { |
| 781 | // Key creation was OK but has generated a key that cannot be used. |
| 782 | auto params = |
| 783 | AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 784 | if (block_mode == BlockMode::GCM) { |
| 785 | params.Authorization(TAG_MAC_LENGTH, 128); |
| 786 | } |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 787 | auto result = Begin(KeyPurpose::ENCRYPT, params); |
| 788 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 789 | result == ErrorCode::INVALID_KEY_BLOB) |
| 790 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 791 | } else { |
| 792 | // The KeyMint implementation detected that the generated key |
| 793 | // is unusable. |
| 794 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, result); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * NewKeyGenerationTest.AesGcmMissingMinMac |
| 803 | * |
| 804 | * Verifies that specifying an invalid key size for AES key generation returns |
| 805 | * UNSUPPORTED_KEY_SIZE. |
| 806 | */ |
| 807 | TEST_P(NewKeyGenerationTest, AesGcmMissingMinMac) { |
| 808 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 809 | BlockMode block_mode = BlockMode::GCM; |
| 810 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 811 | SCOPED_TRACE(testing::Message() |
| 812 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 813 | vector<uint8_t> key_blob; |
| 814 | vector<KeyCharacteristics> key_characteristics; |
| 815 | // No MIN_MAC_LENGTH provided. |
| 816 | auto builder = AuthorizationSetBuilder() |
| 817 | .AesEncryptionKey(key_size) |
| 818 | .BlockMode(block_mode) |
| 819 | .Padding(padding_mode) |
| 820 | .SetDefaultValidity(); |
| 821 | EXPECT_EQ(ErrorCode::MISSING_MIN_MAC_LENGTH, |
| 822 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 828 | * NewKeyGenerationTest.AesGcmMinMacOutOfRange |
| 829 | * |
| 830 | * Verifies that specifying an invalid min MAC size for AES key generation returns |
| 831 | * UNSUPPORTED_MIN_MAC_LENGTH. |
| 832 | */ |
| 833 | TEST_P(NewKeyGenerationTest, AesGcmMinMacOutOfRange) { |
| 834 | for (size_t min_mac_len : {88, 136}) { |
| 835 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 836 | BlockMode block_mode = BlockMode::GCM; |
| 837 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 838 | SCOPED_TRACE(testing::Message() |
| 839 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 840 | vector<uint8_t> key_blob; |
| 841 | vector<KeyCharacteristics> key_characteristics; |
| 842 | auto builder = AuthorizationSetBuilder() |
| 843 | .AesEncryptionKey(key_size) |
| 844 | .BlockMode(block_mode) |
| 845 | .Padding(padding_mode) |
| 846 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_len) |
| 847 | .SetDefaultValidity(); |
| 848 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 849 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 856 | * NewKeyGenerationTest.TripleDes |
| 857 | * |
| 858 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 859 | * have correct characteristics. |
| 860 | */ |
| 861 | TEST_P(NewKeyGenerationTest, TripleDes) { |
| 862 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 863 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 864 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 865 | SCOPED_TRACE(testing::Message() |
| 866 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 867 | vector<uint8_t> key_blob; |
| 868 | vector<KeyCharacteristics> key_characteristics; |
| 869 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 870 | .TripleDesEncryptionKey(key_size) |
| 871 | .BlockMode(block_mode) |
| 872 | .Padding(padding_mode) |
| 873 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 874 | .SetDefaultValidity(), |
| 875 | &key_blob, &key_characteristics)); |
| 876 | |
| 877 | EXPECT_GT(key_blob.size(), 0U); |
| 878 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 879 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 880 | |
| 881 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 882 | |
| 883 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 884 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 885 | << "Key size " << key_size << "missing"; |
| 886 | |
| 887 | CheckedDeleteKey(&key_blob); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * NewKeyGenerationTest.TripleDesWithAttestation |
| 895 | * |
| 896 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 897 | * have correct characteristics. |
| 898 | * |
| 899 | * Request attestation, which doesn't help for symmetric keys (as there is no public key to |
| 900 | * put in a certificate) but which isn't an error. |
| 901 | */ |
| 902 | TEST_P(NewKeyGenerationTest, TripleDesWithAttestation) { |
| 903 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 904 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 905 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 906 | SCOPED_TRACE(testing::Message() |
| 907 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 908 | |
| 909 | auto challenge = "hello"; |
| 910 | auto app_id = "foo"; |
| 911 | |
| 912 | vector<uint8_t> key_blob; |
| 913 | vector<KeyCharacteristics> key_characteristics; |
| 914 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 915 | .TripleDesEncryptionKey(key_size) |
| 916 | .BlockMode(block_mode) |
| 917 | .Padding(padding_mode) |
| 918 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 919 | .AttestationChallenge(challenge) |
| 920 | .AttestationApplicationId(app_id) |
| 921 | .SetDefaultValidity(), |
| 922 | &key_blob, &key_characteristics)); |
| 923 | |
| 924 | EXPECT_GT(key_blob.size(), 0U); |
| 925 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 926 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 927 | |
| 928 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 929 | |
| 930 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 931 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 932 | << "Key size " << key_size << "missing"; |
| 933 | |
| 934 | CheckedDeleteKey(&key_blob); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * NewKeyGenerationTest.TripleDesInvalidSize |
| 942 | * |
| 943 | * Verifies that specifying an invalid key size for 3-DES key generation returns |
| 944 | * UNSUPPORTED_KEY_SIZE. |
| 945 | */ |
| 946 | TEST_P(NewKeyGenerationTest, TripleDesInvalidSize) { |
| 947 | for (auto key_size : InvalidKeySizes(Algorithm::TRIPLE_DES)) { |
| 948 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 949 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 950 | SCOPED_TRACE(testing::Message() |
| 951 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 952 | vector<uint8_t> key_blob; |
| 953 | vector<KeyCharacteristics> key_characteristics; |
| 954 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 955 | GenerateKey(AuthorizationSetBuilder() |
| 956 | .TripleDesEncryptionKey(key_size) |
| 957 | .BlockMode(block_mode) |
| 958 | .Padding(padding_mode) |
| 959 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 960 | .SetDefaultValidity(), |
| 961 | &key_blob, &key_characteristics)); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | // Omitting the key size fails. |
| 967 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 968 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 969 | SCOPED_TRACE(testing::Message() |
| 970 | << "3DES-default-" << block_mode << "-" << padding_mode); |
| 971 | vector<uint8_t> key_blob; |
| 972 | vector<KeyCharacteristics> key_characteristics; |
| 973 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 974 | GenerateKey(AuthorizationSetBuilder() |
| 975 | .Authorization(TAG_ALGORITHM, Algorithm::TRIPLE_DES) |
| 976 | .BlockMode(block_mode) |
| 977 | .Padding(padding_mode) |
| 978 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 979 | .SetDefaultValidity(), |
| 980 | &key_blob, &key_characteristics)); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 986 | * NewKeyGenerationTest.Rsa |
| 987 | * |
| 988 | * Verifies that keymint can generate all required RSA key sizes, and that the resulting keys |
| 989 | * have correct characteristics. |
| 990 | */ |
| 991 | TEST_P(NewKeyGenerationTest, Rsa) { |
| 992 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 993 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 994 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 995 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 996 | .RsaSigningKey(key_size, 65537) |
| 997 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 998 | .Padding(PaddingMode::NONE) |
| 999 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1000 | &key_blob, &key_characteristics)); |
| 1001 | |
| 1002 | ASSERT_GT(key_blob.size(), 0U); |
| 1003 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1004 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1005 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1006 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1007 | |
| 1008 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1009 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1010 | << "Key size " << key_size << "missing"; |
| 1011 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1012 | |
| 1013 | CheckedDeleteKey(&key_blob); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1018 | * NewKeyGenerationTest.RsaWithAttestation |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1019 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1020 | * Verifies that keymint can generate all required RSA key sizes with attestation, and that the |
| 1021 | * resulting keys have correct characteristics. |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1022 | */ |
| 1023 | TEST_P(NewKeyGenerationTest, RsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1024 | auto challenge = "hello"; |
| 1025 | auto app_id = "foo"; |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1026 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1027 | auto subject = "cert subj 2"; |
| 1028 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1029 | |
| 1030 | uint64_t serial_int = 66; |
| 1031 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1032 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1033 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1034 | vector<uint8_t> key_blob; |
| 1035 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1036 | ASSERT_EQ(ErrorCode::OK, |
| 1037 | GenerateKey(AuthorizationSetBuilder() |
| 1038 | .RsaSigningKey(key_size, 65537) |
| 1039 | .Digest(Digest::NONE) |
| 1040 | .Padding(PaddingMode::NONE) |
| 1041 | .AttestationChallenge(challenge) |
| 1042 | .AttestationApplicationId(app_id) |
| 1043 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1044 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1045 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1046 | .SetDefaultValidity(), |
| 1047 | &key_blob, &key_characteristics)); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1048 | |
| 1049 | ASSERT_GT(key_blob.size(), 0U); |
| 1050 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1051 | CheckCharacteristics(key_blob, key_characteristics); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1052 | |
| 1053 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1054 | |
| 1055 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1056 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1057 | << "Key size " << key_size << "missing"; |
| 1058 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1059 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1060 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1061 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1062 | ASSERT_GT(cert_chain_.size(), 0); |
| 1063 | |
| 1064 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1065 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1066 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1067 | sw_enforced, hw_enforced, SecLevel(), |
| 1068 | cert_chain_[0].encodedCertificate)); |
| 1069 | |
| 1070 | CheckedDeleteKey(&key_blob); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /* |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1075 | * NewKeyGenerationTest.RsaWithRpkAttestation |
| 1076 | * |
| 1077 | * Verifies that keymint can generate all required RSA key sizes, using an attestation key |
| 1078 | * that has been generated using an associate IRemotelyProvisionedComponent. |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 1079 | * |
| 1080 | * This test is disabled because the KeyMint specification does not require that implementations |
| 1081 | * of the first version of KeyMint have to also implement IRemotelyProvisionedComponent. |
| 1082 | * 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] | 1083 | */ |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 1084 | TEST_P(NewKeyGenerationTest, DISABLED_RsaWithRpkAttestation) { |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1085 | // There should be an IRemotelyProvisionedComponent instance associated with the KeyMint |
| 1086 | // instance. |
| 1087 | std::shared_ptr<IRemotelyProvisionedComponent> rp; |
| 1088 | ASSERT_TRUE(matching_rp_instance(GetParam(), &rp)) |
| 1089 | << "No IRemotelyProvisionedComponent found that matches KeyMint device " << GetParam(); |
| 1090 | |
| 1091 | // Generate a P-256 keypair to use as an attestation key. |
| 1092 | MacedPublicKey macedPubKey; |
| 1093 | std::vector<uint8_t> privateKeyBlob; |
| 1094 | auto status = |
| 1095 | rp->generateEcdsaP256KeyPair(/* testMode= */ false, &macedPubKey, &privateKeyBlob); |
| 1096 | ASSERT_TRUE(status.isOk()); |
| 1097 | vector<uint8_t> coseKeyData; |
| 1098 | check_maced_pubkey(macedPubKey, /* testMode= */ false, &coseKeyData); |
| 1099 | |
| 1100 | AttestationKey attestation_key; |
| 1101 | attestation_key.keyBlob = std::move(privateKeyBlob); |
| 1102 | attestation_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 1103 | |
| 1104 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1105 | auto challenge = "hello"; |
| 1106 | auto app_id = "foo"; |
| 1107 | |
| 1108 | vector<uint8_t> key_blob; |
| 1109 | vector<KeyCharacteristics> key_characteristics; |
| 1110 | ASSERT_EQ(ErrorCode::OK, |
| 1111 | GenerateKey(AuthorizationSetBuilder() |
| 1112 | .RsaSigningKey(key_size, 65537) |
| 1113 | .Digest(Digest::NONE) |
| 1114 | .Padding(PaddingMode::NONE) |
| 1115 | .AttestationChallenge(challenge) |
| 1116 | .AttestationApplicationId(app_id) |
| 1117 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1118 | .SetDefaultValidity(), |
| 1119 | attestation_key, &key_blob, &key_characteristics, &cert_chain_)); |
| 1120 | |
| 1121 | ASSERT_GT(key_blob.size(), 0U); |
| 1122 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1123 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1124 | |
| 1125 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1126 | |
| 1127 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1128 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1129 | << "Key size " << key_size << "missing"; |
| 1130 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1131 | |
| 1132 | // Attestation by itself is not valid (last entry is not self-signed). |
| 1133 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_)); |
| 1134 | |
| 1135 | // The signature over the attested key should correspond to the P256 public key. |
| 1136 | X509_Ptr key_cert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 1137 | ASSERT_TRUE(key_cert.get()); |
| 1138 | EVP_PKEY_Ptr signing_pubkey; |
| 1139 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 1140 | ASSERT_TRUE(signing_pubkey.get()); |
| 1141 | |
| 1142 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 1143 | << "Verification of attested certificate failed " |
| 1144 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
| 1145 | |
| 1146 | CheckedDeleteKey(&key_blob); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1151 | * NewKeyGenerationTest.RsaEncryptionWithAttestation |
| 1152 | * |
| 1153 | * Verifies that keymint attestation for RSA encryption keys with challenge and |
| 1154 | * app id is also successful. |
| 1155 | */ |
| 1156 | TEST_P(NewKeyGenerationTest, RsaEncryptionWithAttestation) { |
| 1157 | auto key_size = 2048; |
| 1158 | auto challenge = "hello"; |
| 1159 | auto app_id = "foo"; |
| 1160 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1161 | auto subject = "subj 2"; |
| 1162 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1163 | |
| 1164 | uint64_t serial_int = 111166; |
| 1165 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1166 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1167 | vector<uint8_t> key_blob; |
| 1168 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1169 | ASSERT_EQ(ErrorCode::OK, |
| 1170 | GenerateKey(AuthorizationSetBuilder() |
| 1171 | .RsaEncryptionKey(key_size, 65537) |
| 1172 | .Padding(PaddingMode::NONE) |
| 1173 | .AttestationChallenge(challenge) |
| 1174 | .AttestationApplicationId(app_id) |
| 1175 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1176 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1177 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1178 | .SetDefaultValidity(), |
| 1179 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1180 | |
| 1181 | ASSERT_GT(key_blob.size(), 0U); |
| 1182 | AuthorizationSet auths; |
| 1183 | for (auto& entry : key_characteristics) { |
| 1184 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1185 | } |
| 1186 | |
| 1187 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 1188 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 1189 | |
| 1190 | // Verify that App data and ROT are NOT included. |
| 1191 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 1192 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
| 1193 | |
| 1194 | // Check that some unexpected tags/values are NOT present. |
| 1195 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
| 1196 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::VERIFY)); |
| 1197 | |
| 1198 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 1199 | |
| 1200 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
| 1201 | ASSERT_TRUE(os_ver); |
| 1202 | EXPECT_EQ(*os_ver, os_version()); |
| 1203 | |
| 1204 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1205 | |
| 1206 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1207 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1208 | << "Key size " << key_size << "missing"; |
| 1209 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1210 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1211 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1212 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1213 | ASSERT_GT(cert_chain_.size(), 0); |
| 1214 | |
| 1215 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1216 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1217 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1218 | sw_enforced, hw_enforced, SecLevel(), |
| 1219 | cert_chain_[0].encodedCertificate)); |
| 1220 | |
| 1221 | CheckedDeleteKey(&key_blob); |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * NewKeyGenerationTest.RsaWithSelfSign |
| 1226 | * |
| 1227 | * Verifies that attesting to RSA key generation is successful, and returns |
| 1228 | * self signed certificate if no challenge is provided. And signing etc |
| 1229 | * works as expected. |
| 1230 | */ |
| 1231 | TEST_P(NewKeyGenerationTest, RsaWithSelfSign) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1232 | auto subject = "cert subj subj subj subj subj subj 22222222222222222222"; |
| 1233 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1234 | |
| 1235 | uint64_t serial_int = 0; |
| 1236 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1237 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1238 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1239 | vector<uint8_t> key_blob; |
| 1240 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1241 | ASSERT_EQ(ErrorCode::OK, |
| 1242 | GenerateKey(AuthorizationSetBuilder() |
| 1243 | .RsaSigningKey(key_size, 65537) |
| 1244 | .Digest(Digest::NONE) |
| 1245 | .Padding(PaddingMode::NONE) |
| 1246 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1247 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1248 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1249 | .SetDefaultValidity(), |
| 1250 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1251 | |
| 1252 | ASSERT_GT(key_blob.size(), 0U); |
| 1253 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1254 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1255 | |
| 1256 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1257 | |
| 1258 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1259 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1260 | << "Key size " << key_size << "missing"; |
| 1261 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1262 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1263 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1264 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1265 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1266 | |
| 1267 | CheckedDeleteKey(&key_blob); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * NewKeyGenerationTest.RsaWithAttestationMissAppId |
| 1273 | * |
| 1274 | * Verifies that attesting to RSA checks for missing app ID. |
| 1275 | */ |
| 1276 | TEST_P(NewKeyGenerationTest, RsaWithAttestationMissAppId) { |
| 1277 | auto challenge = "hello"; |
| 1278 | vector<uint8_t> key_blob; |
| 1279 | vector<KeyCharacteristics> key_characteristics; |
| 1280 | |
| 1281 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, |
| 1282 | GenerateKey(AuthorizationSetBuilder() |
| 1283 | .RsaSigningKey(2048, 65537) |
| 1284 | .Digest(Digest::NONE) |
| 1285 | .Padding(PaddingMode::NONE) |
| 1286 | .AttestationChallenge(challenge) |
| 1287 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1288 | .SetDefaultValidity(), |
| 1289 | &key_blob, &key_characteristics)); |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * NewKeyGenerationTest.RsaWithAttestationAppIdIgnored |
| 1294 | * |
| 1295 | * Verifies that attesting to RSA ignores app id if challenge is missing. |
| 1296 | */ |
| 1297 | TEST_P(NewKeyGenerationTest, RsaWithAttestationAppIdIgnored) { |
| 1298 | auto key_size = 2048; |
| 1299 | auto app_id = "foo"; |
| 1300 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1301 | auto subject = "cert subj 2"; |
| 1302 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1303 | |
| 1304 | uint64_t serial_int = 1; |
| 1305 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1306 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1307 | vector<uint8_t> key_blob; |
| 1308 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1309 | ASSERT_EQ(ErrorCode::OK, |
| 1310 | GenerateKey(AuthorizationSetBuilder() |
| 1311 | .RsaSigningKey(key_size, 65537) |
| 1312 | .Digest(Digest::NONE) |
| 1313 | .Padding(PaddingMode::NONE) |
| 1314 | .AttestationApplicationId(app_id) |
| 1315 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1316 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1317 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1318 | .SetDefaultValidity(), |
| 1319 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1320 | |
| 1321 | ASSERT_GT(key_blob.size(), 0U); |
| 1322 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1323 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1324 | |
| 1325 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1326 | |
| 1327 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1328 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1329 | << "Key size " << key_size << "missing"; |
| 1330 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1331 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1332 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1333 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1334 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1335 | |
| 1336 | CheckedDeleteKey(&key_blob); |
| 1337 | } |
| 1338 | |
| 1339 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1340 | * NewKeyGenerationTest.LimitedUsageRsa |
| 1341 | * |
| 1342 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1343 | * resulting keys have correct characteristics. |
| 1344 | */ |
| 1345 | TEST_P(NewKeyGenerationTest, LimitedUsageRsa) { |
| 1346 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1347 | vector<uint8_t> key_blob; |
| 1348 | vector<KeyCharacteristics> key_characteristics; |
| 1349 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1350 | .RsaSigningKey(key_size, 65537) |
| 1351 | .Digest(Digest::NONE) |
| 1352 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1353 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1354 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1355 | &key_blob, &key_characteristics)); |
| 1356 | |
| 1357 | ASSERT_GT(key_blob.size(), 0U); |
| 1358 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1359 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1360 | |
| 1361 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1362 | |
| 1363 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1364 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1365 | << "Key size " << key_size << "missing"; |
| 1366 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1367 | |
| 1368 | // Check the usage count limit tag appears in the authorizations. |
| 1369 | AuthorizationSet auths; |
| 1370 | for (auto& entry : key_characteristics) { |
| 1371 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1372 | } |
| 1373 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1374 | << "key usage count limit " << 1U << " missing"; |
| 1375 | |
| 1376 | CheckedDeleteKey(&key_blob); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1381 | * NewKeyGenerationTest.LimitedUsageRsaWithAttestation |
| 1382 | * |
| 1383 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1384 | * resulting keys have correct characteristics and attestation. |
| 1385 | */ |
| 1386 | TEST_P(NewKeyGenerationTest, LimitedUsageRsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1387 | auto challenge = "hello"; |
| 1388 | auto app_id = "foo"; |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1389 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1390 | auto subject = "cert subj 2"; |
| 1391 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1392 | |
| 1393 | uint64_t serial_int = 66; |
| 1394 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1395 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1396 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1397 | vector<uint8_t> key_blob; |
| 1398 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1399 | ASSERT_EQ(ErrorCode::OK, |
| 1400 | GenerateKey(AuthorizationSetBuilder() |
| 1401 | .RsaSigningKey(key_size, 65537) |
| 1402 | .Digest(Digest::NONE) |
| 1403 | .Padding(PaddingMode::NONE) |
| 1404 | .AttestationChallenge(challenge) |
| 1405 | .AttestationApplicationId(app_id) |
| 1406 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1407 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1408 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1409 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1410 | .SetDefaultValidity(), |
| 1411 | &key_blob, &key_characteristics)); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1412 | |
| 1413 | ASSERT_GT(key_blob.size(), 0U); |
| 1414 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1415 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1416 | |
| 1417 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1418 | |
| 1419 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1420 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1421 | << "Key size " << key_size << "missing"; |
| 1422 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1423 | |
| 1424 | // Check the usage count limit tag appears in the authorizations. |
| 1425 | AuthorizationSet auths; |
| 1426 | for (auto& entry : key_characteristics) { |
| 1427 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1428 | } |
| 1429 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1430 | << "key usage count limit " << 1U << " missing"; |
| 1431 | |
| 1432 | // Check the usage count limit tag also appears in the attestation. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1433 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1434 | ASSERT_GT(cert_chain_.size(), 0); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1435 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1436 | |
| 1437 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1438 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1439 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1440 | sw_enforced, hw_enforced, SecLevel(), |
| 1441 | cert_chain_[0].encodedCertificate)); |
| 1442 | |
| 1443 | CheckedDeleteKey(&key_blob); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1448 | * NewKeyGenerationTest.NoInvalidRsaSizes |
| 1449 | * |
| 1450 | * Verifies that keymint cannot generate any RSA key sizes that are designated as invalid. |
| 1451 | */ |
| 1452 | TEST_P(NewKeyGenerationTest, NoInvalidRsaSizes) { |
| 1453 | for (auto key_size : InvalidKeySizes(Algorithm::RSA)) { |
| 1454 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1455 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1456 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1457 | GenerateKey(AuthorizationSetBuilder() |
| 1458 | .RsaSigningKey(key_size, 65537) |
| 1459 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1460 | .Padding(PaddingMode::NONE) |
| 1461 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1462 | &key_blob, &key_characteristics)); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * NewKeyGenerationTest.RsaNoDefaultSize |
| 1468 | * |
| 1469 | * Verifies that failing to specify a key size for RSA key generation returns |
| 1470 | * UNSUPPORTED_KEY_SIZE. |
| 1471 | */ |
| 1472 | TEST_P(NewKeyGenerationTest, RsaNoDefaultSize) { |
| 1473 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1474 | GenerateKey(AuthorizationSetBuilder() |
| 1475 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 1476 | .Authorization(TAG_RSA_PUBLIC_EXPONENT, 3U) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1477 | .SigningKey() |
| 1478 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1482 | * NewKeyGenerationTest.RsaMissingParams |
| 1483 | * |
| 1484 | * Verifies that omitting optional tags works. |
| 1485 | */ |
| 1486 | TEST_P(NewKeyGenerationTest, RsaMissingParams) { |
| 1487 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1488 | ASSERT_EQ(ErrorCode::OK, |
| 1489 | GenerateKey( |
| 1490 | AuthorizationSetBuilder().RsaKey(key_size, 65537).SetDefaultValidity())); |
| 1491 | CheckedDeleteKey(); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1496 | * NewKeyGenerationTest.Ecdsa |
| 1497 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 1498 | * 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] | 1499 | * have correct characteristics. |
| 1500 | */ |
| 1501 | TEST_P(NewKeyGenerationTest, Ecdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1502 | for (auto curve : ValidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1503 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1504 | vector<KeyCharacteristics> key_characteristics; |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1505 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1506 | .EcdsaSigningKey(curve) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1507 | .Digest(Digest::NONE) |
| 1508 | .SetDefaultValidity(), |
| 1509 | &key_blob, &key_characteristics)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1510 | ASSERT_GT(key_blob.size(), 0U); |
| 1511 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1512 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1513 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1514 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1515 | |
| 1516 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1517 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1518 | |
| 1519 | CheckedDeleteKey(&key_blob); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 1524 | * NewKeyGenerationTest.EcdsaCurve25519 |
| 1525 | * |
| 1526 | * Verifies that keymint can generate a curve25519 key, and that the resulting key |
| 1527 | * has correct characteristics. |
| 1528 | */ |
| 1529 | TEST_P(NewKeyGenerationTest, EcdsaCurve25519) { |
| 1530 | if (!Curve25519Supported()) { |
| 1531 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1532 | } |
| 1533 | |
| 1534 | EcCurve curve = EcCurve::CURVE_25519; |
| 1535 | vector<uint8_t> key_blob; |
| 1536 | vector<KeyCharacteristics> key_characteristics; |
| 1537 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1538 | .EcdsaSigningKey(curve) |
| 1539 | .Digest(Digest::NONE) |
| 1540 | .SetDefaultValidity(), |
| 1541 | &key_blob, &key_characteristics); |
| 1542 | ASSERT_EQ(result, ErrorCode::OK); |
| 1543 | ASSERT_GT(key_blob.size(), 0U); |
| 1544 | |
| 1545 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1546 | ASSERT_GT(cert_chain_.size(), 0); |
| 1547 | |
| 1548 | CheckBaseParams(key_characteristics); |
| 1549 | CheckCharacteristics(key_blob, key_characteristics); |
| 1550 | |
| 1551 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1552 | |
| 1553 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
| 1554 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
| 1555 | |
| 1556 | CheckedDeleteKey(&key_blob); |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * NewKeyGenerationTest.EcCurve25519MultiPurposeFail |
| 1561 | * |
| 1562 | * Verifies that KeyMint rejects an attempt to generate a curve 25519 key for both |
| 1563 | * SIGN and AGREE_KEY. |
| 1564 | */ |
| 1565 | TEST_P(NewKeyGenerationTest, EcdsaCurve25519MultiPurposeFail) { |
| 1566 | if (!Curve25519Supported()) { |
| 1567 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1568 | } |
| 1569 | |
| 1570 | EcCurve curve = EcCurve::CURVE_25519; |
| 1571 | vector<uint8_t> key_blob; |
| 1572 | vector<KeyCharacteristics> key_characteristics; |
| 1573 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1574 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 1575 | .EcdsaSigningKey(curve) |
| 1576 | .Digest(Digest::NONE) |
| 1577 | .SetDefaultValidity(), |
| 1578 | &key_blob, &key_characteristics); |
| 1579 | ASSERT_EQ(result, ErrorCode::INCOMPATIBLE_PURPOSE); |
| 1580 | } |
| 1581 | |
| 1582 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1583 | * NewKeyGenerationTest.EcdsaAttestation |
| 1584 | * |
| 1585 | * Verifies that for all Ecdsa key sizes, if challenge and app id is provided, |
| 1586 | * an attestation will be generated. |
| 1587 | */ |
| 1588 | TEST_P(NewKeyGenerationTest, EcdsaAttestation) { |
| 1589 | auto challenge = "hello"; |
| 1590 | auto app_id = "foo"; |
| 1591 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1592 | auto subject = "cert subj 2"; |
| 1593 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1594 | |
| 1595 | uint64_t serial_int = 0xFFFFFFFFFFFFFFFF; |
| 1596 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1597 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1598 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1599 | vector<uint8_t> key_blob; |
| 1600 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1601 | ASSERT_EQ(ErrorCode::OK, |
| 1602 | GenerateKey(AuthorizationSetBuilder() |
| 1603 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1604 | .EcdsaSigningKey(curve) |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1605 | .Digest(Digest::NONE) |
| 1606 | .AttestationChallenge(challenge) |
| 1607 | .AttestationApplicationId(app_id) |
| 1608 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1609 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1610 | .SetDefaultValidity(), |
| 1611 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1612 | ASSERT_GT(key_blob.size(), 0U); |
| 1613 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1614 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1617 | |
| 1618 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1619 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1620 | |
| 1621 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1622 | ASSERT_GT(cert_chain_.size(), 0); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1623 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1624 | |
| 1625 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1626 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1627 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1628 | sw_enforced, hw_enforced, SecLevel(), |
| 1629 | cert_chain_[0].encodedCertificate)); |
| 1630 | |
| 1631 | CheckedDeleteKey(&key_blob); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 1636 | * NewKeyGenerationTest.EcdsaAttestationCurve25519 |
| 1637 | * |
| 1638 | * Verifies that for a curve 25519 key, if challenge and app id is provided, |
| 1639 | * an attestation will be generated. |
| 1640 | */ |
| 1641 | TEST_P(NewKeyGenerationTest, EcdsaAttestationCurve25519) { |
| 1642 | if (!Curve25519Supported()) { |
| 1643 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 1644 | } |
| 1645 | |
| 1646 | EcCurve curve = EcCurve::CURVE_25519; |
| 1647 | auto challenge = "hello"; |
| 1648 | auto app_id = "foo"; |
| 1649 | |
| 1650 | auto subject = "cert subj 2"; |
| 1651 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1652 | |
| 1653 | uint64_t serial_int = 0xFFFFFFFFFFFFFFFF; |
| 1654 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1655 | |
| 1656 | vector<uint8_t> key_blob; |
| 1657 | vector<KeyCharacteristics> key_characteristics; |
| 1658 | ErrorCode result = GenerateKey(AuthorizationSetBuilder() |
| 1659 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1660 | .EcdsaSigningKey(curve) |
| 1661 | .Digest(Digest::NONE) |
| 1662 | .AttestationChallenge(challenge) |
| 1663 | .AttestationApplicationId(app_id) |
| 1664 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1665 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1666 | .SetDefaultValidity(), |
| 1667 | &key_blob, &key_characteristics); |
| 1668 | ASSERT_EQ(ErrorCode::OK, result); |
| 1669 | ASSERT_GT(key_blob.size(), 0U); |
| 1670 | CheckBaseParams(key_characteristics); |
| 1671 | CheckCharacteristics(key_blob, key_characteristics); |
| 1672 | |
| 1673 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1674 | |
| 1675 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
| 1676 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
| 1677 | |
| 1678 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1679 | ASSERT_GT(cert_chain_.size(), 0); |
| 1680 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
| 1681 | |
| 1682 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1683 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1684 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
| 1685 | sw_enforced, hw_enforced, SecLevel(), |
| 1686 | cert_chain_[0].encodedCertificate)); |
| 1687 | |
| 1688 | CheckedDeleteKey(&key_blob); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1692 | * NewKeyGenerationTest.EcdsaAttestationTags |
| 1693 | * |
| 1694 | * Verifies that creation of an attested ECDSA key includes various tags in the |
| 1695 | * attestation extension. |
| 1696 | */ |
| 1697 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTags) { |
| 1698 | auto challenge = "hello"; |
| 1699 | auto app_id = "foo"; |
| 1700 | auto subject = "cert subj 2"; |
| 1701 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1702 | uint64_t serial_int = 0x1010; |
| 1703 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1704 | const AuthorizationSetBuilder base_builder = |
| 1705 | AuthorizationSetBuilder() |
| 1706 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1707 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1708 | .Digest(Digest::NONE) |
| 1709 | .AttestationChallenge(challenge) |
| 1710 | .AttestationApplicationId(app_id) |
| 1711 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1712 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1713 | .SetDefaultValidity(); |
| 1714 | |
| 1715 | // Various tags that map to fields in the attestation extension ASN.1 schema. |
| 1716 | auto extra_tags = AuthorizationSetBuilder() |
| 1717 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 1718 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 1719 | .Authorization(TAG_ACTIVE_DATETIME, 1619621648000) |
| 1720 | .Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, 1619621648000) |
| 1721 | .Authorization(TAG_USAGE_EXPIRE_DATETIME, 1619621999000) |
| 1722 | .Authorization(TAG_USAGE_COUNT_LIMIT, 42) |
| 1723 | .Authorization(TAG_AUTH_TIMEOUT, 100000) |
| 1724 | .Authorization(TAG_ALLOW_WHILE_ON_BODY) |
| 1725 | .Authorization(TAG_TRUSTED_USER_PRESENCE_REQUIRED) |
| 1726 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 1727 | .Authorization(TAG_UNLOCKED_DEVICE_REQUIRED) |
| 1728 | .Authorization(TAG_CREATION_DATETIME, 1619621648000); |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1729 | |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1730 | for (const KeyParameter& tag : extra_tags) { |
| 1731 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1732 | vector<uint8_t> key_blob; |
| 1733 | vector<KeyCharacteristics> key_characteristics; |
| 1734 | AuthorizationSetBuilder builder = base_builder; |
| 1735 | builder.push_back(tag); |
| 1736 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
| 1737 | if (result == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE && |
| 1738 | tag.tag == TAG_ROLLBACK_RESISTANCE) { |
| 1739 | continue; |
| 1740 | } |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1741 | if (result == ErrorCode::UNSUPPORTED_TAG && tag.tag == TAG_TRUSTED_USER_PRESENCE_REQUIRED) { |
| 1742 | // Tag not required to be supported by all KeyMint implementations. |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1743 | continue; |
| 1744 | } |
| 1745 | ASSERT_EQ(result, ErrorCode::OK); |
| 1746 | ASSERT_GT(key_blob.size(), 0U); |
| 1747 | |
| 1748 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1749 | ASSERT_GT(cert_chain_.size(), 0); |
| 1750 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1751 | |
| 1752 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1753 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1754 | // Some tags are optional, so don't require them to be in the enforcements. |
| 1755 | 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] | 1756 | EXPECT_TRUE(hw_enforced.Contains(tag.tag) || sw_enforced.Contains(tag.tag)) |
| 1757 | << tag << " not in hw:" << hw_enforced << " nor sw:" << sw_enforced; |
| 1758 | } |
| 1759 | |
| 1760 | // Verifying the attestation record will check for the specific tag because |
| 1761 | // it's included in the authorizations. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1762 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 1763 | hw_enforced, SecLevel(), |
| 1764 | cert_chain_[0].encodedCertificate)); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1765 | |
| 1766 | CheckedDeleteKey(&key_blob); |
| 1767 | } |
| 1768 | |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1769 | // Collection of invalid attestation ID tags. |
| 1770 | auto invalid_tags = |
| 1771 | AuthorizationSetBuilder() |
| 1772 | .Authorization(TAG_ATTESTATION_ID_BRAND, "bogus-brand") |
| 1773 | .Authorization(TAG_ATTESTATION_ID_DEVICE, "devious-device") |
| 1774 | .Authorization(TAG_ATTESTATION_ID_PRODUCT, "punctured-product") |
| 1775 | .Authorization(TAG_ATTESTATION_ID_SERIAL, "suspicious-serial") |
| 1776 | .Authorization(TAG_ATTESTATION_ID_IMEI, "invalid-imei") |
| 1777 | .Authorization(TAG_ATTESTATION_ID_MEID, "mismatching-meid") |
| 1778 | .Authorization(TAG_ATTESTATION_ID_MANUFACTURER, "malformed-manufacturer") |
| 1779 | .Authorization(TAG_ATTESTATION_ID_MODEL, "malicious-model"); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1780 | for (const KeyParameter& tag : invalid_tags) { |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1781 | SCOPED_TRACE(testing::Message() << "-incorrect-tag-" << tag); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1782 | vector<uint8_t> key_blob; |
| 1783 | vector<KeyCharacteristics> key_characteristics; |
| 1784 | AuthorizationSetBuilder builder = |
| 1785 | AuthorizationSetBuilder() |
| 1786 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1787 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1788 | .Digest(Digest::NONE) |
| 1789 | .AttestationChallenge(challenge) |
| 1790 | .AttestationApplicationId(app_id) |
| 1791 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1792 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1793 | .SetDefaultValidity(); |
| 1794 | builder.push_back(tag); |
| 1795 | ASSERT_EQ(ErrorCode::CANNOT_ATTEST_IDS, |
| 1796 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /* |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1801 | * NewKeyGenerationTest.EcdsaAttestationIdTags |
| 1802 | * |
| 1803 | * Verifies that creation of an attested ECDSA key includes various ID tags in the |
| 1804 | * attestation extension. |
| 1805 | */ |
| 1806 | TEST_P(NewKeyGenerationTest, EcdsaAttestationIdTags) { |
| 1807 | auto challenge = "hello"; |
| 1808 | auto app_id = "foo"; |
| 1809 | auto subject = "cert subj 2"; |
| 1810 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1811 | uint64_t serial_int = 0x1010; |
| 1812 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1813 | const AuthorizationSetBuilder base_builder = |
| 1814 | AuthorizationSetBuilder() |
| 1815 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1816 | .EcdsaSigningKey(EcCurve::P_256) |
| 1817 | .Digest(Digest::NONE) |
| 1818 | .AttestationChallenge(challenge) |
| 1819 | .AttestationApplicationId(app_id) |
| 1820 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1821 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1822 | .SetDefaultValidity(); |
| 1823 | |
| 1824 | // Various ATTESTATION_ID_* tags that map to fields in the attestation extension ASN.1 schema. |
| 1825 | auto extra_tags = AuthorizationSetBuilder(); |
| 1826 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_BRAND, "ro.product.brand"); |
| 1827 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_DEVICE, "ro.product.device"); |
| 1828 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_PRODUCT, "ro.product.name"); |
| 1829 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_SERIAL, "ro.serial"); |
| 1830 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MANUFACTURER, "ro.product.manufacturer"); |
| 1831 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MODEL, "ro.product.model"); |
| 1832 | |
| 1833 | for (const KeyParameter& tag : extra_tags) { |
| 1834 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1835 | vector<uint8_t> key_blob; |
| 1836 | vector<KeyCharacteristics> key_characteristics; |
| 1837 | AuthorizationSetBuilder builder = base_builder; |
| 1838 | builder.push_back(tag); |
| 1839 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
| 1840 | if (result == ErrorCode::CANNOT_ATTEST_IDS) { |
| 1841 | // Device ID attestation is optional; KeyMint may not support it at all. |
| 1842 | continue; |
| 1843 | } |
| 1844 | ASSERT_EQ(result, ErrorCode::OK); |
| 1845 | ASSERT_GT(key_blob.size(), 0U); |
| 1846 | |
| 1847 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1848 | ASSERT_GT(cert_chain_.size(), 0); |
| 1849 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1850 | |
| 1851 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1852 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1853 | |
| 1854 | // The attested key characteristics will not contain APPLICATION_ID_* fields (their |
| 1855 | // spec definitions all have "Must never appear in KeyCharacteristics"), but the |
| 1856 | // attestation extension should contain them, so make sure the extra tag is added. |
| 1857 | hw_enforced.push_back(tag); |
| 1858 | |
| 1859 | // Verifying the attestation record will check for the specific tag because |
| 1860 | // it's included in the authorizations. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1861 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 1862 | hw_enforced, SecLevel(), |
| 1863 | cert_chain_[0].encodedCertificate)); |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1864 | |
| 1865 | CheckedDeleteKey(&key_blob); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1870 | * NewKeyGenerationTest.EcdsaAttestationUniqueId |
| 1871 | * |
| 1872 | * Verifies that creation of an attested ECDSA key with a UNIQUE_ID included. |
| 1873 | */ |
| 1874 | TEST_P(NewKeyGenerationTest, EcdsaAttestationUniqueId) { |
| 1875 | 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] | 1876 | vector<uint8_t>* unique_id, bool reset = false) { |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1877 | auto challenge = "hello"; |
| 1878 | auto subject = "cert subj 2"; |
| 1879 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1880 | uint64_t serial_int = 0x1010; |
| 1881 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1882 | AuthorizationSetBuilder builder = |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1883 | AuthorizationSetBuilder() |
| 1884 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1885 | .Authorization(TAG_INCLUDE_UNIQUE_ID) |
| 1886 | .EcdsaSigningKey(EcCurve::P_256) |
| 1887 | .Digest(Digest::NONE) |
| 1888 | .AttestationChallenge(challenge) |
| 1889 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1890 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1891 | .AttestationApplicationId(app_id) |
| 1892 | .Authorization(TAG_CREATION_DATETIME, datetime) |
| 1893 | .SetDefaultValidity(); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1894 | if (reset) { |
| 1895 | builder.Authorization(TAG_RESET_SINCE_ID_ROTATION); |
| 1896 | } |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1897 | |
| 1898 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder)); |
| 1899 | ASSERT_GT(key_blob_.size(), 0U); |
| 1900 | |
| 1901 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1902 | ASSERT_GT(cert_chain_.size(), 0); |
| 1903 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1904 | |
| 1905 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics_); |
| 1906 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics_); |
| 1907 | |
| 1908 | // Check that the unique ID field in the extension is non-empty. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1909 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, sw_enforced, |
| 1910 | hw_enforced, SecLevel(), |
| 1911 | cert_chain_[0].encodedCertificate, unique_id)); |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1912 | EXPECT_GT(unique_id->size(), 0); |
| 1913 | CheckedDeleteKey(); |
| 1914 | }; |
| 1915 | |
| 1916 | // Generate unique ID |
| 1917 | auto app_id = "foo"; |
| 1918 | uint64_t cert_date = 1619621648000; // Wed Apr 28 14:54:08 2021 in ms since epoch |
| 1919 | vector<uint8_t> unique_id; |
| 1920 | get_unique_id(app_id, cert_date, &unique_id); |
| 1921 | |
| 1922 | // Generating a new key with the same parameters should give the same unique ID. |
| 1923 | vector<uint8_t> unique_id2; |
| 1924 | get_unique_id(app_id, cert_date, &unique_id2); |
| 1925 | EXPECT_EQ(unique_id, unique_id2); |
| 1926 | |
| 1927 | // Generating a new key with a slightly different date should give the same unique ID. |
| 1928 | uint64_t rounded_date = cert_date / 2592000000LLU; |
| 1929 | uint64_t min_date = rounded_date * 2592000000LLU; |
| 1930 | uint64_t max_date = ((rounded_date + 1) * 2592000000LLU) - 1; |
| 1931 | |
| 1932 | vector<uint8_t> unique_id3; |
| 1933 | get_unique_id(app_id, min_date, &unique_id3); |
| 1934 | EXPECT_EQ(unique_id, unique_id3); |
| 1935 | |
| 1936 | vector<uint8_t> unique_id4; |
| 1937 | get_unique_id(app_id, max_date, &unique_id4); |
| 1938 | EXPECT_EQ(unique_id, unique_id4); |
| 1939 | |
| 1940 | // A different attestation application ID should yield a different unique ID. |
| 1941 | auto app_id2 = "different_foo"; |
| 1942 | vector<uint8_t> unique_id5; |
| 1943 | get_unique_id(app_id2, cert_date, &unique_id5); |
| 1944 | EXPECT_NE(unique_id, unique_id5); |
| 1945 | |
| 1946 | // A radically different date should yield a different unique ID. |
| 1947 | vector<uint8_t> unique_id6; |
| 1948 | get_unique_id(app_id, 1611621648000, &unique_id6); |
| 1949 | EXPECT_NE(unique_id, unique_id6); |
| 1950 | |
| 1951 | vector<uint8_t> unique_id7; |
| 1952 | get_unique_id(app_id, max_date + 1, &unique_id7); |
| 1953 | EXPECT_NE(unique_id, unique_id7); |
| 1954 | |
| 1955 | vector<uint8_t> unique_id8; |
| 1956 | get_unique_id(app_id, min_date - 1, &unique_id8); |
| 1957 | EXPECT_NE(unique_id, unique_id8); |
David Drysdale | 13f2a40 | 2021-11-01 11:40:08 +0000 | [diff] [blame] | 1958 | |
| 1959 | // Marking RESET_SINCE_ID_ROTATION should give a different unique ID. |
| 1960 | vector<uint8_t> unique_id9; |
| 1961 | get_unique_id(app_id, cert_date, &unique_id9, /* reset_id = */ true); |
| 1962 | EXPECT_NE(unique_id, unique_id9); |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1966 | * NewKeyGenerationTest.EcdsaAttestationTagNoApplicationId |
| 1967 | * |
| 1968 | * Verifies that creation of an attested ECDSA key does not include APPLICATION_ID. |
| 1969 | */ |
| 1970 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTagNoApplicationId) { |
| 1971 | auto challenge = "hello"; |
| 1972 | auto attest_app_id = "foo"; |
| 1973 | auto subject = "cert subj 2"; |
| 1974 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1975 | uint64_t serial_int = 0x1010; |
| 1976 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1977 | |
| 1978 | // Earlier versions of the attestation extension schema included a slot: |
| 1979 | // applicationId [601] EXPLICIT OCTET_STRING OPTIONAL, |
| 1980 | // This should never have been included, and should never be filled in. |
| 1981 | // Generate an attested key that include APPLICATION_ID and APPLICATION_DATA, |
| 1982 | // to confirm that this field never makes it into the attestation extension. |
| 1983 | vector<uint8_t> key_blob; |
| 1984 | vector<KeyCharacteristics> key_characteristics; |
| 1985 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1986 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1987 | .EcdsaSigningKey(EcCurve::P_256) |
| 1988 | .Digest(Digest::NONE) |
| 1989 | .AttestationChallenge(challenge) |
| 1990 | .AttestationApplicationId(attest_app_id) |
| 1991 | .Authorization(TAG_APPLICATION_ID, "client_id") |
| 1992 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 1993 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1994 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1995 | .SetDefaultValidity(), |
| 1996 | &key_blob, &key_characteristics); |
| 1997 | ASSERT_EQ(result, ErrorCode::OK); |
| 1998 | ASSERT_GT(key_blob.size(), 0U); |
| 1999 | |
| 2000 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2001 | ASSERT_GT(cert_chain_.size(), 0); |
| 2002 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 2003 | |
| 2004 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2005 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 2006 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, attest_app_id, sw_enforced, |
| 2007 | hw_enforced, SecLevel(), |
| 2008 | cert_chain_[0].encodedCertificate)); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 2009 | |
| 2010 | // Check that the app id is not in the cert. |
| 2011 | string app_id = "clientid"; |
| 2012 | std::vector<uint8_t> needle(reinterpret_cast<const uint8_t*>(app_id.data()), |
| 2013 | reinterpret_cast<const uint8_t*>(app_id.data()) + app_id.size()); |
| 2014 | ASSERT_EQ(std::search(cert_chain_[0].encodedCertificate.begin(), |
| 2015 | cert_chain_[0].encodedCertificate.end(), needle.begin(), needle.end()), |
| 2016 | cert_chain_[0].encodedCertificate.end()); |
| 2017 | |
| 2018 | CheckedDeleteKey(&key_blob); |
| 2019 | } |
| 2020 | |
| 2021 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2022 | * NewKeyGenerationTest.EcdsaSelfSignAttestation |
| 2023 | * |
| 2024 | * Verifies that if no challenge is provided to an Ecdsa key generation, then |
| 2025 | * the key will generate a self signed attestation. |
| 2026 | */ |
| 2027 | TEST_P(NewKeyGenerationTest, EcdsaSelfSignAttestation) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2028 | auto subject = "cert subj 2"; |
| 2029 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 2030 | |
| 2031 | uint64_t serial_int = 0x123456FFF1234; |
| 2032 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 2033 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2034 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2035 | vector<uint8_t> key_blob; |
| 2036 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2037 | ASSERT_EQ(ErrorCode::OK, |
| 2038 | GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2039 | .EcdsaSigningKey(curve) |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2040 | .Digest(Digest::NONE) |
| 2041 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 2042 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 2043 | .SetDefaultValidity(), |
| 2044 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2045 | ASSERT_GT(key_blob.size(), 0U); |
| 2046 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2047 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2048 | |
| 2049 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2050 | |
| 2051 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2052 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2053 | |
| 2054 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 2055 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2056 | ASSERT_EQ(cert_chain_.size(), 1); |
| 2057 | |
| 2058 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2059 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 2060 | |
| 2061 | CheckedDeleteKey(&key_blob); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * NewKeyGenerationTest.EcdsaAttestationRequireAppId |
| 2067 | * |
| 2068 | * Verifies that if attestation challenge is provided to Ecdsa key generation, then |
| 2069 | * app id must also be provided or else it will fail. |
| 2070 | */ |
| 2071 | TEST_P(NewKeyGenerationTest, EcdsaAttestationRequireAppId) { |
| 2072 | auto challenge = "hello"; |
| 2073 | vector<uint8_t> key_blob; |
| 2074 | vector<KeyCharacteristics> key_characteristics; |
| 2075 | |
| 2076 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, |
| 2077 | GenerateKey(AuthorizationSetBuilder() |
| 2078 | .EcdsaSigningKey(EcCurve::P_256) |
| 2079 | .Digest(Digest::NONE) |
| 2080 | .AttestationChallenge(challenge) |
| 2081 | .SetDefaultValidity(), |
| 2082 | &key_blob, &key_characteristics)); |
| 2083 | } |
| 2084 | |
| 2085 | /* |
| 2086 | * NewKeyGenerationTest.EcdsaIgnoreAppId |
| 2087 | * |
| 2088 | * Verifies that if no challenge is provided to the Ecdsa key generation, then |
| 2089 | * any appid will be ignored, and keymint will generate a self sign certificate. |
| 2090 | */ |
| 2091 | TEST_P(NewKeyGenerationTest, EcdsaIgnoreAppId) { |
| 2092 | auto app_id = "foo"; |
| 2093 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2094 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2095 | vector<uint8_t> key_blob; |
| 2096 | vector<KeyCharacteristics> key_characteristics; |
| 2097 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2098 | .EcdsaSigningKey(curve) |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2099 | .Digest(Digest::NONE) |
| 2100 | .AttestationApplicationId(app_id) |
| 2101 | .SetDefaultValidity(), |
| 2102 | &key_blob, &key_characteristics)); |
| 2103 | |
| 2104 | ASSERT_GT(key_blob.size(), 0U); |
| 2105 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2106 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2107 | |
| 2108 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2109 | |
| 2110 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2111 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2112 | |
| 2113 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2114 | ASSERT_EQ(cert_chain_.size(), 1); |
| 2115 | |
| 2116 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2117 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 2118 | |
| 2119 | CheckedDeleteKey(&key_blob); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * NewKeyGenerationTest.AttestationApplicationIDLengthProperlyEncoded |
| 2125 | * |
| 2126 | * Verifies that the Attestation Application ID software enforced tag has a proper length encoding. |
| 2127 | * Some implementations break strict encoding rules by encoding a length between 127 and 256 in one |
| 2128 | * byte. Proper DER encoding specifies that for lengths greater than 127, one byte should be used |
| 2129 | * to specify how many following bytes will be used to encode the length. |
| 2130 | */ |
| 2131 | TEST_P(NewKeyGenerationTest, AttestationApplicationIDLengthProperlyEncoded) { |
| 2132 | auto challenge = "hello"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2133 | std::vector<uint32_t> app_id_lengths{143, 258}; |
| 2134 | |
| 2135 | for (uint32_t length : app_id_lengths) { |
| 2136 | const string app_id(length, 'a'); |
| 2137 | vector<uint8_t> key_blob; |
| 2138 | vector<KeyCharacteristics> key_characteristics; |
| 2139 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2140 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2141 | .EcdsaSigningKey(EcCurve::P_256) |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2142 | .Digest(Digest::NONE) |
| 2143 | .AttestationChallenge(challenge) |
| 2144 | .AttestationApplicationId(app_id) |
| 2145 | .SetDefaultValidity(), |
| 2146 | &key_blob, &key_characteristics)); |
| 2147 | ASSERT_GT(key_blob.size(), 0U); |
| 2148 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2149 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2150 | |
| 2151 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2152 | |
| 2153 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2154 | 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] | 2155 | |
| 2156 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 2157 | ASSERT_GT(cert_chain_.size(), 0); |
| 2158 | |
| 2159 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 2160 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 2161 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2162 | sw_enforced, hw_enforced, SecLevel(), |
| 2163 | cert_chain_[0].encodedCertificate)); |
| 2164 | |
| 2165 | CheckedDeleteKey(&key_blob); |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2170 | * NewKeyGenerationTest.LimitedUsageEcdsa |
| 2171 | * |
| 2172 | * Verifies that KeyMint can generate all required EC key sizes with limited usage, and that the |
| 2173 | * resulting keys have correct characteristics. |
| 2174 | */ |
| 2175 | TEST_P(NewKeyGenerationTest, LimitedUsageEcdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2176 | for (auto curve : ValidCurves()) { |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2177 | vector<uint8_t> key_blob; |
| 2178 | vector<KeyCharacteristics> key_characteristics; |
| 2179 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2180 | .EcdsaSigningKey(curve) |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2181 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2182 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 2183 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2184 | &key_blob, &key_characteristics)); |
| 2185 | |
| 2186 | ASSERT_GT(key_blob.size(), 0U); |
| 2187 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2188 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2189 | |
| 2190 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2191 | |
| 2192 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2193 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2194 | |
| 2195 | // Check the usage count limit tag appears in the authorizations. |
| 2196 | AuthorizationSet auths; |
| 2197 | for (auto& entry : key_characteristics) { |
| 2198 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 2199 | } |
| 2200 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 2201 | << "key usage count limit " << 1U << " missing"; |
| 2202 | |
| 2203 | CheckedDeleteKey(&key_blob); |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2208 | * NewKeyGenerationTest.EcdsaDefaultSize |
| 2209 | * |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2210 | * Verifies that failing to specify a curve for EC key generation returns |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2211 | * UNSUPPORTED_KEY_SIZE. |
| 2212 | */ |
| 2213 | TEST_P(NewKeyGenerationTest, EcdsaDefaultSize) { |
| 2214 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2215 | GenerateKey(AuthorizationSetBuilder() |
| 2216 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 2217 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2218 | .Digest(Digest::NONE) |
| 2219 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 2223 | * NewKeyGenerationTest.EcdsaInvalidCurve |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2224 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 2225 | * Verifies that specifying an invalid curve for EC key generation returns |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2226 | * UNSUPPORTED_KEY_SIZE. |
| 2227 | */ |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 2228 | TEST_P(NewKeyGenerationTest, EcdsaInvalidCurve) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2229 | for (auto curve : InvalidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2230 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2231 | vector<KeyCharacteristics> key_characteristics; |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 2232 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 2233 | .EcdsaSigningKey(curve) |
| 2234 | .Digest(Digest::NONE) |
| 2235 | .SetDefaultValidity(), |
| 2236 | &key_blob, &key_characteristics); |
| 2237 | ASSERT_TRUE(result == ErrorCode::UNSUPPORTED_KEY_SIZE || |
| 2238 | result == ErrorCode::UNSUPPORTED_EC_CURVE); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2241 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2242 | GenerateKey(AuthorizationSetBuilder() |
| 2243 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 2244 | .Authorization(TAG_KEY_SIZE, 190) |
| 2245 | .SigningKey() |
| 2246 | .Digest(Digest::NONE) |
| 2247 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /* |
| 2251 | * NewKeyGenerationTest.EcdsaMismatchKeySize |
| 2252 | * |
| 2253 | * Verifies that specifying mismatched key size and curve for EC key generation returns |
| 2254 | * INVALID_ARGUMENT. |
| 2255 | */ |
| 2256 | TEST_P(NewKeyGenerationTest, EcdsaMismatchKeySize) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2257 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2258 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2259 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2260 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2261 | auto result = GenerateKey(AuthorizationSetBuilder() |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2262 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2263 | .Authorization(TAG_KEY_SIZE, 224) |
| 2264 | .Authorization(TAG_EC_CURVE, EcCurve::P_256) |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2265 | .SigningKey() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2266 | .Digest(Digest::NONE) |
| 2267 | .SetDefaultValidity()); |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 2268 | ASSERT_TRUE(result == ErrorCode::INVALID_ARGUMENT); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2272 | * NewKeyGenerationTest.EcdsaAllValidCurves |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2273 | * |
| 2274 | * Verifies that keymint does not support any curve designated as unsupported. |
| 2275 | */ |
| 2276 | TEST_P(NewKeyGenerationTest, EcdsaAllValidCurves) { |
| 2277 | Digest digest; |
| 2278 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2279 | digest = Digest::SHA_2_256; |
| 2280 | } else { |
| 2281 | digest = Digest::SHA_2_512; |
| 2282 | } |
| 2283 | for (auto curve : ValidCurves()) { |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2284 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2285 | .EcdsaSigningKey(curve) |
| 2286 | .Digest(digest) |
| 2287 | .SetDefaultValidity())) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2288 | << "Failed to generate key on curve: " << curve; |
| 2289 | CheckedDeleteKey(); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * NewKeyGenerationTest.Hmac |
| 2295 | * |
| 2296 | * Verifies that keymint supports all required digests, and that the resulting keys have correct |
| 2297 | * characteristics. |
| 2298 | */ |
| 2299 | TEST_P(NewKeyGenerationTest, Hmac) { |
| 2300 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2301 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2302 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2303 | constexpr size_t key_size = 128; |
| 2304 | ASSERT_EQ(ErrorCode::OK, |
| 2305 | GenerateKey( |
| 2306 | AuthorizationSetBuilder().HmacKey(key_size).Digest(digest).Authorization( |
| 2307 | TAG_MIN_MAC_LENGTH, 128), |
| 2308 | &key_blob, &key_characteristics)); |
| 2309 | |
| 2310 | ASSERT_GT(key_blob.size(), 0U); |
| 2311 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2312 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2313 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2314 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2315 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2316 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2317 | << "Key size " << key_size << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2318 | |
| 2319 | CheckedDeleteKey(&key_blob); |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2324 | * NewKeyGenerationTest.HmacNoAttestation |
| 2325 | * |
| 2326 | * Verifies that for Hmac key generation, no attestation will be generated even if challenge |
| 2327 | * and app id are provided. |
| 2328 | */ |
| 2329 | TEST_P(NewKeyGenerationTest, HmacNoAttestation) { |
| 2330 | auto challenge = "hello"; |
| 2331 | auto app_id = "foo"; |
| 2332 | |
| 2333 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2334 | vector<uint8_t> key_blob; |
| 2335 | vector<KeyCharacteristics> key_characteristics; |
| 2336 | constexpr size_t key_size = 128; |
| 2337 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2338 | .HmacKey(key_size) |
| 2339 | .Digest(digest) |
| 2340 | .AttestationChallenge(challenge) |
| 2341 | .AttestationApplicationId(app_id) |
| 2342 | .Authorization(TAG_MIN_MAC_LENGTH, 128), |
| 2343 | &key_blob, &key_characteristics)); |
| 2344 | |
| 2345 | ASSERT_GT(key_blob.size(), 0U); |
| 2346 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2347 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2348 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2349 | |
| 2350 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2351 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2352 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2353 | << "Key size " << key_size << "missing"; |
| 2354 | |
| 2355 | CheckedDeleteKey(&key_blob); |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2360 | * NewKeyGenerationTest.LimitedUsageHmac |
| 2361 | * |
| 2362 | * Verifies that KeyMint supports all required digests with limited usage Hmac, and that the |
| 2363 | * resulting keys have correct characteristics. |
| 2364 | */ |
| 2365 | TEST_P(NewKeyGenerationTest, LimitedUsageHmac) { |
| 2366 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2367 | vector<uint8_t> key_blob; |
| 2368 | vector<KeyCharacteristics> key_characteristics; |
| 2369 | constexpr size_t key_size = 128; |
| 2370 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2371 | .HmacKey(key_size) |
| 2372 | .Digest(digest) |
| 2373 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 2374 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1), |
| 2375 | &key_blob, &key_characteristics)); |
| 2376 | |
| 2377 | ASSERT_GT(key_blob.size(), 0U); |
| 2378 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2379 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2380 | |
| 2381 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2382 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2383 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2384 | << "Key size " << key_size << "missing"; |
| 2385 | |
| 2386 | // Check the usage count limit tag appears in the authorizations. |
| 2387 | AuthorizationSet auths; |
| 2388 | for (auto& entry : key_characteristics) { |
| 2389 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 2390 | } |
| 2391 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 2392 | << "key usage count limit " << 1U << " missing"; |
| 2393 | |
| 2394 | CheckedDeleteKey(&key_blob); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2399 | * NewKeyGenerationTest.HmacCheckKeySizes |
| 2400 | * |
| 2401 | * Verifies that keymint supports all key sizes, and rejects all invalid key sizes. |
| 2402 | */ |
| 2403 | TEST_P(NewKeyGenerationTest, HmacCheckKeySizes) { |
| 2404 | for (size_t key_size = 0; key_size <= 512; ++key_size) { |
| 2405 | if (key_size < 64 || key_size % 8 != 0) { |
| 2406 | // To keep this test from being very slow, we only test a random fraction of |
| 2407 | // non-byte key sizes. We test only ~10% of such cases. Since there are 392 of |
| 2408 | // them, we expect to run ~40 of them in each run. |
| 2409 | if (key_size % 8 == 0 || random() % 10 == 0) { |
| 2410 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2411 | GenerateKey(AuthorizationSetBuilder() |
| 2412 | .HmacKey(key_size) |
| 2413 | .Digest(Digest::SHA_2_256) |
| 2414 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2415 | << "HMAC key size " << key_size << " invalid"; |
| 2416 | } |
| 2417 | } else { |
| 2418 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2419 | .HmacKey(key_size) |
| 2420 | .Digest(Digest::SHA_2_256) |
| 2421 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2422 | << "Failed to generate HMAC key of size " << key_size; |
| 2423 | CheckedDeleteKey(); |
| 2424 | } |
| 2425 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2426 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2427 | // STRONGBOX devices must not support keys larger than 512 bits. |
| 2428 | size_t key_size = 520; |
| 2429 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2430 | GenerateKey(AuthorizationSetBuilder() |
| 2431 | .HmacKey(key_size) |
| 2432 | .Digest(Digest::SHA_2_256) |
| 2433 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2434 | << "HMAC key size " << key_size << " unexpectedly valid"; |
| 2435 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * NewKeyGenerationTest.HmacCheckMinMacLengths |
| 2440 | * |
| 2441 | * Verifies that keymint supports all required MAC lengths and rejects all invalid lengths. This |
| 2442 | * test is probabilistic in order to keep the runtime down, but any failure prints out the |
| 2443 | * specific MAC length that failed, so reproducing a failed run will be easy. |
| 2444 | */ |
| 2445 | TEST_P(NewKeyGenerationTest, HmacCheckMinMacLengths) { |
| 2446 | for (size_t min_mac_length = 0; min_mac_length <= 256; ++min_mac_length) { |
| 2447 | if (min_mac_length < 64 || min_mac_length % 8 != 0) { |
| 2448 | // To keep this test from being very long, we only test a random fraction of |
| 2449 | // non-byte lengths. We test only ~10% of such cases. Since there are 172 of them, |
| 2450 | // we expect to run ~17 of them in each run. |
| 2451 | if (min_mac_length % 8 == 0 || random() % 10 == 0) { |
| 2452 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2453 | GenerateKey(AuthorizationSetBuilder() |
| 2454 | .HmacKey(128) |
| 2455 | .Digest(Digest::SHA_2_256) |
| 2456 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2457 | << "HMAC min mac length " << min_mac_length << " invalid."; |
| 2458 | } |
| 2459 | } else { |
| 2460 | EXPECT_EQ(ErrorCode::OK, |
| 2461 | GenerateKey(AuthorizationSetBuilder() |
| 2462 | .HmacKey(128) |
| 2463 | .Digest(Digest::SHA_2_256) |
| 2464 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2465 | << "Failed to generate HMAC key with min MAC length " << min_mac_length; |
| 2466 | CheckedDeleteKey(); |
| 2467 | } |
| 2468 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2469 | |
| 2470 | // Minimum MAC length must be no more than 512 bits. |
| 2471 | size_t min_mac_length = 520; |
| 2472 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2473 | GenerateKey(AuthorizationSetBuilder() |
| 2474 | .HmacKey(128) |
| 2475 | .Digest(Digest::SHA_2_256) |
| 2476 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2477 | << "HMAC min mac length " << min_mac_length << " invalid."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | * NewKeyGenerationTest.HmacMultipleDigests |
| 2482 | * |
| 2483 | * Verifies that keymint rejects HMAC key generation with multiple specified digest algorithms. |
| 2484 | */ |
| 2485 | TEST_P(NewKeyGenerationTest, HmacMultipleDigests) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2486 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2487 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2488 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2489 | |
| 2490 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2491 | GenerateKey(AuthorizationSetBuilder() |
| 2492 | .HmacKey(128) |
| 2493 | .Digest(Digest::SHA1) |
| 2494 | .Digest(Digest::SHA_2_256) |
| 2495 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * NewKeyGenerationTest.HmacDigestNone |
| 2500 | * |
| 2501 | * Verifies that keymint rejects HMAC key generation with no digest or Digest::NONE |
| 2502 | */ |
| 2503 | TEST_P(NewKeyGenerationTest, HmacDigestNone) { |
| 2504 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2505 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Authorization(TAG_MIN_MAC_LENGTH, |
| 2506 | 128))); |
| 2507 | |
| 2508 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2509 | GenerateKey(AuthorizationSetBuilder() |
| 2510 | .HmacKey(128) |
| 2511 | .Digest(Digest::NONE) |
| 2512 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2513 | } |
| 2514 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2515 | /* |
| 2516 | * NewKeyGenerationTest.AesNoAttestation |
| 2517 | * |
| 2518 | * Verifies that attestation parameters to AES keys are ignored and generateKey |
| 2519 | * will succeed. |
| 2520 | */ |
| 2521 | TEST_P(NewKeyGenerationTest, AesNoAttestation) { |
| 2522 | auto challenge = "hello"; |
| 2523 | auto app_id = "foo"; |
| 2524 | |
| 2525 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2526 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2527 | .AesEncryptionKey(128) |
| 2528 | .EcbMode() |
| 2529 | .Padding(PaddingMode::PKCS7) |
| 2530 | .AttestationChallenge(challenge) |
| 2531 | .AttestationApplicationId(app_id))); |
| 2532 | |
| 2533 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * NewKeyGenerationTest.TripleDesNoAttestation |
| 2538 | * |
| 2539 | * Verifies that attesting parameters to 3DES keys are ignored and generate key |
| 2540 | * will be successful. No attestation should be generated. |
| 2541 | */ |
| 2542 | TEST_P(NewKeyGenerationTest, TripleDesNoAttestation) { |
| 2543 | auto challenge = "hello"; |
| 2544 | auto app_id = "foo"; |
| 2545 | |
| 2546 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2547 | .TripleDesEncryptionKey(168) |
| 2548 | .BlockMode(BlockMode::ECB) |
| 2549 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2550 | .Padding(PaddingMode::NONE) |
| 2551 | .AttestationChallenge(challenge) |
| 2552 | .AttestationApplicationId(app_id))); |
| 2553 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2554 | } |
| 2555 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2556 | INSTANTIATE_KEYMINT_AIDL_TEST(NewKeyGenerationTest); |
| 2557 | |
| 2558 | typedef KeyMintAidlTestBase SigningOperationsTest; |
| 2559 | |
| 2560 | /* |
| 2561 | * SigningOperationsTest.RsaSuccess |
| 2562 | * |
| 2563 | * Verifies that raw RSA signature operations succeed. |
| 2564 | */ |
| 2565 | TEST_P(SigningOperationsTest, RsaSuccess) { |
| 2566 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2567 | .RsaSigningKey(2048, 65537) |
| 2568 | .Digest(Digest::NONE) |
| 2569 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2570 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2571 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2572 | string message = "12345678901234567890123456789012"; |
| 2573 | string signature = SignMessage( |
| 2574 | message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 2575 | LocalVerifyMessage(message, signature, |
| 2576 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * SigningOperationsTest.RsaAllPaddingsAndDigests |
| 2581 | * |
| 2582 | * Verifies RSA signature/verification for all padding modes and digests. |
| 2583 | */ |
| 2584 | TEST_P(SigningOperationsTest, RsaAllPaddingsAndDigests) { |
| 2585 | auto authorizations = AuthorizationSetBuilder() |
| 2586 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2587 | .RsaSigningKey(2048, 65537) |
| 2588 | .Digest(ValidDigests(true /* withNone */, true /* withMD5 */)) |
| 2589 | .Padding(PaddingMode::NONE) |
| 2590 | .Padding(PaddingMode::RSA_PSS) |
| 2591 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2592 | .SetDefaultValidity(); |
| 2593 | |
| 2594 | ASSERT_EQ(ErrorCode::OK, GenerateKey(authorizations)); |
| 2595 | |
| 2596 | string message(128, 'a'); |
| 2597 | string corrupt_message(message); |
| 2598 | ++corrupt_message[corrupt_message.size() / 2]; |
| 2599 | |
| 2600 | for (auto padding : |
| 2601 | {PaddingMode::NONE, PaddingMode::RSA_PSS, PaddingMode::RSA_PKCS1_1_5_SIGN}) { |
| 2602 | for (auto digest : ValidDigests(true /* withNone */, true /* withMD5 */)) { |
| 2603 | if (padding == PaddingMode::NONE && digest != Digest::NONE) { |
| 2604 | // Digesting only makes sense with padding. |
| 2605 | continue; |
| 2606 | } |
| 2607 | |
| 2608 | if (padding == PaddingMode::RSA_PSS && digest == Digest::NONE) { |
| 2609 | // PSS requires digesting. |
| 2610 | continue; |
| 2611 | } |
| 2612 | |
| 2613 | string signature = |
| 2614 | SignMessage(message, AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2615 | LocalVerifyMessage(message, signature, |
| 2616 | AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2617 | } |
| 2618 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * SigningOperationsTest.RsaUseRequiresCorrectAppIdAppData |
| 2623 | * |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2624 | * Verifies that using an RSA key requires the correct app data. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2625 | */ |
| 2626 | TEST_P(SigningOperationsTest, RsaUseRequiresCorrectAppIdAppData) { |
| 2627 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2628 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2629 | .RsaSigningKey(2048, 65537) |
| 2630 | .Digest(Digest::NONE) |
| 2631 | .Padding(PaddingMode::NONE) |
| 2632 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2633 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2634 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2635 | |
| 2636 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 2637 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2638 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2639 | Begin(KeyPurpose::SIGN, |
| 2640 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2641 | AbortIfNeeded(); |
| 2642 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2643 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2644 | .Digest(Digest::NONE) |
| 2645 | .Padding(PaddingMode::NONE) |
| 2646 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2647 | AbortIfNeeded(); |
| 2648 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2649 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2650 | .Digest(Digest::NONE) |
| 2651 | .Padding(PaddingMode::NONE) |
| 2652 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 2653 | AbortIfNeeded(); |
| 2654 | EXPECT_EQ(ErrorCode::OK, |
| 2655 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2656 | .Digest(Digest::NONE) |
| 2657 | .Padding(PaddingMode::NONE) |
| 2658 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2659 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2660 | AbortIfNeeded(); |
| 2661 | } |
| 2662 | |
| 2663 | /* |
| 2664 | * SigningOperationsTest.RsaPssSha256Success |
| 2665 | * |
| 2666 | * Verifies that RSA-PSS signature operations succeed. |
| 2667 | */ |
| 2668 | TEST_P(SigningOperationsTest, RsaPssSha256Success) { |
| 2669 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2670 | .RsaSigningKey(2048, 65537) |
| 2671 | .Digest(Digest::SHA_2_256) |
| 2672 | .Padding(PaddingMode::RSA_PSS) |
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 | // Use large message, which won't work without digesting. |
| 2676 | string message(1024, 'a'); |
| 2677 | string signature = SignMessage( |
| 2678 | message, |
| 2679 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS)); |
| 2680 | } |
| 2681 | |
| 2682 | /* |
| 2683 | * SigningOperationsTest.RsaPaddingNoneDoesNotAllowOther |
| 2684 | * |
| 2685 | * Verifies that keymint rejects signature operations that specify a padding mode when the key |
| 2686 | * supports only unpadded operations. |
| 2687 | */ |
| 2688 | TEST_P(SigningOperationsTest, RsaPaddingNoneDoesNotAllowOther) { |
| 2689 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2690 | .RsaSigningKey(2048, 65537) |
| 2691 | .Digest(Digest::NONE) |
| 2692 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2693 | .Padding(PaddingMode::NONE) |
| 2694 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2695 | string message = "12345678901234567890123456789012"; |
| 2696 | string signature; |
| 2697 | |
| 2698 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 2699 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2700 | .Digest(Digest::NONE) |
| 2701 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * SigningOperationsTest.NoUserConfirmation |
| 2706 | * |
| 2707 | * Verifies that keymint rejects signing operations for keys with |
| 2708 | * TRUSTED_CONFIRMATION_REQUIRED and no valid confirmation token |
| 2709 | * presented. |
| 2710 | */ |
| 2711 | TEST_P(SigningOperationsTest, NoUserConfirmation) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2712 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2713 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2714 | } |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2715 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2716 | .RsaSigningKey(1024, 65537) |
| 2717 | .Digest(Digest::NONE) |
| 2718 | .Padding(PaddingMode::NONE) |
| 2719 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2720 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 2721 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2722 | |
| 2723 | const string message = "12345678901234567890123456789012"; |
| 2724 | EXPECT_EQ(ErrorCode::OK, |
| 2725 | Begin(KeyPurpose::SIGN, |
| 2726 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2727 | string signature; |
| 2728 | EXPECT_EQ(ErrorCode::NO_USER_CONFIRMATION, Finish(message, &signature)); |
| 2729 | } |
| 2730 | |
| 2731 | /* |
| 2732 | * SigningOperationsTest.RsaPkcs1Sha256Success |
| 2733 | * |
| 2734 | * Verifies that digested RSA-PKCS1 signature operations succeed. |
| 2735 | */ |
| 2736 | TEST_P(SigningOperationsTest, RsaPkcs1Sha256Success) { |
| 2737 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2738 | .RsaSigningKey(2048, 65537) |
| 2739 | .Digest(Digest::SHA_2_256) |
| 2740 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2741 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2742 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2743 | string message(1024, 'a'); |
| 2744 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2745 | .Digest(Digest::SHA_2_256) |
| 2746 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2747 | } |
| 2748 | |
| 2749 | /* |
| 2750 | * SigningOperationsTest.RsaPkcs1NoDigestSuccess |
| 2751 | * |
| 2752 | * Verifies that undigested RSA-PKCS1 signature operations succeed. |
| 2753 | */ |
| 2754 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestSuccess) { |
| 2755 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2756 | .RsaSigningKey(2048, 65537) |
| 2757 | .Digest(Digest::NONE) |
| 2758 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2759 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2760 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2761 | string message(53, 'a'); |
| 2762 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2763 | .Digest(Digest::NONE) |
| 2764 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * SigningOperationsTest.RsaPkcs1NoDigestTooLarge |
| 2769 | * |
| 2770 | * Verifies that undigested RSA-PKCS1 signature operations fail with the correct error code when |
| 2771 | * given a too-long message. |
| 2772 | */ |
| 2773 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestTooLong) { |
| 2774 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2775 | .RsaSigningKey(2048, 65537) |
| 2776 | .Digest(Digest::NONE) |
| 2777 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2778 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2779 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2780 | string message(257, 'a'); |
| 2781 | |
| 2782 | EXPECT_EQ(ErrorCode::OK, |
| 2783 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2784 | .Digest(Digest::NONE) |
| 2785 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2786 | string signature; |
| 2787 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &signature)); |
| 2788 | } |
| 2789 | |
| 2790 | /* |
| 2791 | * SigningOperationsTest.RsaPssSha512TooSmallKey |
| 2792 | * |
| 2793 | * Verifies that undigested RSA-PSS signature operations fail with the correct error code when |
| 2794 | * used with a key that is too small for the message. |
| 2795 | * |
| 2796 | * A PSS-padded message is of length salt_size + digest_size + 16 (sizes in bits), and the |
| 2797 | * keymint specification requires that salt_size == digest_size, so the message will be |
| 2798 | * digest_size * 2 + |
| 2799 | * 16. Such a message can only be signed by a given key if the key is at least that size. This |
| 2800 | * test uses SHA512, which has a digest_size == 512, so the message size is 1040 bits, too large |
| 2801 | * for a 1024-bit key. |
| 2802 | */ |
| 2803 | TEST_P(SigningOperationsTest, RsaPssSha512TooSmallKey) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2804 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2805 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2806 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2807 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2808 | .RsaSigningKey(1024, 65537) |
| 2809 | .Digest(Digest::SHA_2_512) |
| 2810 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2811 | .Padding(PaddingMode::RSA_PSS) |
| 2812 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2813 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2814 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2815 | .Digest(Digest::SHA_2_512) |
| 2816 | .Padding(PaddingMode::RSA_PSS))); |
| 2817 | } |
| 2818 | |
| 2819 | /* |
| 2820 | * SigningOperationsTest.RsaNoPaddingTooLong |
| 2821 | * |
| 2822 | * Verifies that raw RSA signature operations fail with the correct error code when |
| 2823 | * given a too-long message. |
| 2824 | */ |
| 2825 | TEST_P(SigningOperationsTest, RsaNoPaddingTooLong) { |
| 2826 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2827 | .RsaSigningKey(2048, 65537) |
| 2828 | .Digest(Digest::NONE) |
| 2829 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2830 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2831 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2832 | // One byte too long |
| 2833 | string message(2048 / 8 + 1, 'a'); |
| 2834 | ASSERT_EQ(ErrorCode::OK, |
| 2835 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2836 | .Digest(Digest::NONE) |
| 2837 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2838 | string result; |
| 2839 | ErrorCode finish_error_code = Finish(message, &result); |
| 2840 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2841 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2842 | |
| 2843 | // Very large message that should exceed the transfer buffer size of any reasonable TEE. |
| 2844 | message = string(128 * 1024, 'a'); |
| 2845 | ASSERT_EQ(ErrorCode::OK, |
| 2846 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2847 | .Digest(Digest::NONE) |
| 2848 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2849 | finish_error_code = Finish(message, &result); |
| 2850 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2851 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * SigningOperationsTest.RsaAbort |
| 2856 | * |
| 2857 | * Verifies that operations can be aborted correctly. Uses an RSA signing operation for the |
| 2858 | * test, but the behavior should be algorithm and purpose-independent. |
| 2859 | */ |
| 2860 | TEST_P(SigningOperationsTest, RsaAbort) { |
| 2861 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2862 | .RsaSigningKey(2048, 65537) |
| 2863 | .Digest(Digest::NONE) |
| 2864 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2865 | .Padding(PaddingMode::NONE) |
| 2866 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2867 | |
| 2868 | ASSERT_EQ(ErrorCode::OK, |
| 2869 | Begin(KeyPurpose::SIGN, |
| 2870 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2871 | EXPECT_EQ(ErrorCode::OK, Abort()); |
| 2872 | |
| 2873 | // Another abort should fail |
| 2874 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 2875 | |
| 2876 | // Set to sentinel, so TearDown() doesn't try to abort again. |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 2877 | op_.reset(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2881 | * SigningOperationsTest.RsaNonUniqueParams |
| 2882 | * |
| 2883 | * Verifies that an operation with multiple padding modes is rejected. |
| 2884 | */ |
| 2885 | TEST_P(SigningOperationsTest, RsaNonUniqueParams) { |
| 2886 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2887 | .RsaSigningKey(2048, 65537) |
| 2888 | .Digest(Digest::NONE) |
| 2889 | .Digest(Digest::SHA1) |
| 2890 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2891 | .Padding(PaddingMode::NONE) |
| 2892 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2893 | .SetDefaultValidity())); |
| 2894 | |
| 2895 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2896 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2897 | .Digest(Digest::NONE) |
| 2898 | .Padding(PaddingMode::NONE) |
| 2899 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2900 | |
Tommy Chiu | c93c439 | 2021-05-11 18:36:50 +0800 | [diff] [blame] | 2901 | auto result = Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2902 | .Digest(Digest::NONE) |
| 2903 | .Digest(Digest::SHA1) |
| 2904 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2905 | ASSERT_TRUE(result == ErrorCode::UNSUPPORTED_DIGEST || result == ErrorCode::INVALID_ARGUMENT); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2906 | |
| 2907 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2908 | Begin(KeyPurpose::SIGN, |
| 2909 | AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2910 | } |
| 2911 | |
| 2912 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2913 | * SigningOperationsTest.RsaUnsupportedPadding |
| 2914 | * |
| 2915 | * Verifies that RSA operations fail with the correct error (but key gen succeeds) when used |
| 2916 | * with a padding mode inappropriate for RSA. |
| 2917 | */ |
| 2918 | TEST_P(SigningOperationsTest, RsaUnsupportedPadding) { |
| 2919 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2920 | .RsaSigningKey(2048, 65537) |
| 2921 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2922 | .Digest(Digest::SHA_2_256 /* supported digest */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2923 | .Padding(PaddingMode::PKCS7) |
| 2924 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2925 | ASSERT_EQ( |
| 2926 | ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2927 | Begin(KeyPurpose::SIGN, |
| 2928 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::PKCS7))); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2929 | CheckedDeleteKey(); |
| 2930 | |
| 2931 | ASSERT_EQ(ErrorCode::OK, |
| 2932 | GenerateKey( |
| 2933 | AuthorizationSetBuilder() |
| 2934 | .RsaSigningKey(2048, 65537) |
| 2935 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2936 | .Digest(Digest::SHA_2_256 /* supported digest */) |
| 2937 | .Padding(PaddingMode::RSA_OAEP) /* padding mode for encryption only */ |
| 2938 | .SetDefaultValidity())); |
| 2939 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2940 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2941 | .Digest(Digest::SHA_2_256) |
| 2942 | .Padding(PaddingMode::RSA_OAEP))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * SigningOperationsTest.RsaPssNoDigest |
| 2947 | * |
| 2948 | * Verifies that RSA PSS operations fail when no digest is used. PSS requires a digest. |
| 2949 | */ |
| 2950 | TEST_P(SigningOperationsTest, RsaNoDigest) { |
| 2951 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2952 | .RsaSigningKey(2048, 65537) |
| 2953 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2954 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2955 | .Padding(PaddingMode::RSA_PSS) |
| 2956 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2957 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2958 | Begin(KeyPurpose::SIGN, |
| 2959 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::RSA_PSS))); |
| 2960 | |
| 2961 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2962 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS))); |
| 2963 | } |
| 2964 | |
| 2965 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 2966 | * SigningOperationsTest.RsaPssNoPadding |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2967 | * |
| 2968 | * Verifies that RSA operations fail when no padding mode is specified. PaddingMode::NONE is |
| 2969 | * supported in some cases (as validated in other tests), but a mode must be specified. |
| 2970 | */ |
| 2971 | TEST_P(SigningOperationsTest, RsaNoPadding) { |
| 2972 | // Padding must be specified |
| 2973 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2974 | .RsaKey(2048, 65537) |
| 2975 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2976 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2977 | .Digest(Digest::NONE) |
| 2978 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2979 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2980 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 2981 | } |
| 2982 | |
| 2983 | /* |
| 2984 | * SigningOperationsTest.RsaShortMessage |
| 2985 | * |
| 2986 | * Verifies that raw RSA signatures succeed with a message shorter than the key size. |
| 2987 | */ |
| 2988 | TEST_P(SigningOperationsTest, RsaTooShortMessage) { |
| 2989 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2990 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2991 | .RsaSigningKey(2048, 65537) |
| 2992 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2993 | .Padding(PaddingMode::NONE) |
| 2994 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2995 | |
| 2996 | // Barely shorter |
| 2997 | string message(2048 / 8 - 1, 'a'); |
| 2998 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2999 | |
| 3000 | // Much shorter |
| 3001 | message = "a"; |
| 3002 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 3003 | } |
| 3004 | |
| 3005 | /* |
| 3006 | * SigningOperationsTest.RsaSignWithEncryptionKey |
| 3007 | * |
| 3008 | * Verifies that RSA encryption keys cannot be used to sign. |
| 3009 | */ |
| 3010 | TEST_P(SigningOperationsTest, RsaSignWithEncryptionKey) { |
| 3011 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3012 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3013 | .RsaEncryptionKey(2048, 65537) |
| 3014 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3015 | .Padding(PaddingMode::NONE) |
| 3016 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3017 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3018 | Begin(KeyPurpose::SIGN, |
| 3019 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * SigningOperationsTest.RsaSignTooLargeMessage |
| 3024 | * |
| 3025 | * Verifies that attempting a raw signature of a message which is the same length as the key, |
| 3026 | * but numerically larger than the public modulus, fails with the correct error. |
| 3027 | */ |
| 3028 | TEST_P(SigningOperationsTest, RsaSignTooLargeMessage) { |
| 3029 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3030 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3031 | .RsaSigningKey(2048, 65537) |
| 3032 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3033 | .Padding(PaddingMode::NONE) |
| 3034 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3035 | |
| 3036 | // Largest possible message will always be larger than the public modulus. |
| 3037 | string message(2048 / 8, static_cast<char>(0xff)); |
| 3038 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3039 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3040 | .Digest(Digest::NONE) |
| 3041 | .Padding(PaddingMode::NONE))); |
| 3042 | string signature; |
| 3043 | ASSERT_EQ(ErrorCode::INVALID_ARGUMENT, Finish(message, &signature)); |
| 3044 | } |
| 3045 | |
| 3046 | /* |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3047 | * SigningOperationsTest.EcdsaAllDigestsAndCurves |
| 3048 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3049 | * Verifies ECDSA signature/verification for all digests and required curves. |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3050 | */ |
| 3051 | TEST_P(SigningOperationsTest, EcdsaAllDigestsAndCurves) { |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3052 | |
| 3053 | string message = "1234567890"; |
| 3054 | string corrupt_message = "2234567890"; |
| 3055 | for (auto curve : ValidCurves()) { |
| 3056 | SCOPED_TRACE(testing::Message() << "Curve::" << curve); |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3057 | // Ed25519 only allows Digest::NONE. |
| 3058 | auto digests = (curve == EcCurve::CURVE_25519) |
| 3059 | ? std::vector<Digest>(1, Digest::NONE) |
| 3060 | : ValidDigests(true /* withNone */, false /* withMD5 */); |
| 3061 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3062 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3063 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3064 | .EcdsaSigningKey(curve) |
| 3065 | .Digest(digests) |
| 3066 | .SetDefaultValidity()); |
| 3067 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate key for EC curve " << curve; |
| 3068 | if (error != ErrorCode::OK) { |
| 3069 | continue; |
| 3070 | } |
| 3071 | |
| 3072 | for (auto digest : digests) { |
| 3073 | SCOPED_TRACE(testing::Message() << "Digest::" << digest); |
| 3074 | string signature = SignMessage(message, AuthorizationSetBuilder().Digest(digest)); |
| 3075 | LocalVerifyMessage(message, signature, AuthorizationSetBuilder().Digest(digest)); |
| 3076 | } |
| 3077 | |
| 3078 | auto rc = DeleteKey(); |
| 3079 | ASSERT_TRUE(rc == ErrorCode::OK || rc == ErrorCode::UNIMPLEMENTED); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3084 | * SigningOperationsTest.EcdsaAllCurves |
| 3085 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3086 | * Verifies that ECDSA operations succeed with all required curves. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3087 | */ |
| 3088 | TEST_P(SigningOperationsTest, EcdsaAllCurves) { |
| 3089 | for (auto curve : ValidCurves()) { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3090 | Digest digest = (curve == EcCurve::CURVE_25519 ? Digest::NONE : Digest::SHA_2_256); |
| 3091 | SCOPED_TRACE(testing::Message() << "Curve::" << curve); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3092 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3093 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3094 | .EcdsaSigningKey(curve) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3095 | .Digest(digest) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3096 | .SetDefaultValidity()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3097 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3098 | if (error != ErrorCode::OK) continue; |
| 3099 | |
| 3100 | string message(1024, 'a'); |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3101 | SignMessage(message, AuthorizationSetBuilder().Digest(digest)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3102 | CheckedDeleteKey(); |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3107 | * SigningOperationsTest.EcdsaCurve25519 |
| 3108 | * |
| 3109 | * Verifies that ECDSA operations succeed with curve25519. |
| 3110 | */ |
| 3111 | TEST_P(SigningOperationsTest, EcdsaCurve25519) { |
| 3112 | if (!Curve25519Supported()) { |
| 3113 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3114 | } |
| 3115 | |
| 3116 | EcCurve curve = EcCurve::CURVE_25519; |
| 3117 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 3118 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3119 | .EcdsaSigningKey(curve) |
| 3120 | .Digest(Digest::NONE) |
| 3121 | .SetDefaultValidity()); |
| 3122 | ASSERT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 3123 | |
| 3124 | string message(1024, 'a'); |
| 3125 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE)); |
| 3126 | CheckedDeleteKey(); |
| 3127 | } |
| 3128 | |
| 3129 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3130 | * SigningOperationsTest.EcdsaNoDigestHugeData |
| 3131 | * |
| 3132 | * Verifies that ECDSA operations support very large messages, even without digesting. This |
| 3133 | * should work because ECDSA actually only signs the leftmost L_n bits of the message, however |
| 3134 | * large it may be. Not using digesting is a bad idea, but in some cases digesting is done by |
| 3135 | * the framework. |
| 3136 | */ |
| 3137 | TEST_P(SigningOperationsTest, EcdsaNoDigestHugeData) { |
| 3138 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3139 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3140 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3141 | .Digest(Digest::NONE) |
| 3142 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3143 | string message(1 * 1024, 'a'); |
| 3144 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE)); |
| 3145 | } |
| 3146 | |
| 3147 | /* |
| 3148 | * SigningOperationsTest.EcUseRequiresCorrectAppIdAppData |
| 3149 | * |
| 3150 | * Verifies that using an EC key requires the correct app ID/data. |
| 3151 | */ |
| 3152 | TEST_P(SigningOperationsTest, EcUseRequiresCorrectAppIdAppData) { |
| 3153 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3154 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3155 | .EcdsaSigningKey(EcCurve::P_256) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3156 | .Digest(Digest::NONE) |
| 3157 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3158 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 3159 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 3160 | |
| 3161 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 3162 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3163 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3164 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 3165 | AbortIfNeeded(); |
| 3166 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3167 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3168 | .Digest(Digest::NONE) |
| 3169 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 3170 | AbortIfNeeded(); |
| 3171 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 3172 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3173 | .Digest(Digest::NONE) |
| 3174 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 3175 | AbortIfNeeded(); |
| 3176 | EXPECT_EQ(ErrorCode::OK, |
| 3177 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 3178 | .Digest(Digest::NONE) |
| 3179 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 3180 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 3181 | AbortIfNeeded(); |
| 3182 | } |
| 3183 | |
| 3184 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3185 | * SigningOperationsTest.EcdsaIncompatibleDigest |
| 3186 | * |
| 3187 | * Verifies that using an EC key requires compatible digest. |
| 3188 | */ |
| 3189 | TEST_P(SigningOperationsTest, EcdsaIncompatibleDigest) { |
| 3190 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3191 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3192 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3193 | .Digest(Digest::NONE) |
| 3194 | .Digest(Digest::SHA1) |
| 3195 | .SetDefaultValidity())); |
| 3196 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 3197 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::SHA_2_256))); |
| 3198 | AbortIfNeeded(); |
| 3199 | } |
| 3200 | |
| 3201 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3202 | * SigningOperationsTest.AesEcbSign |
| 3203 | * |
| 3204 | * Verifies that attempts to use AES keys to sign fail in the correct way. |
| 3205 | */ |
| 3206 | TEST_P(SigningOperationsTest, AesEcbSign) { |
| 3207 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3208 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3209 | .SigningKey() |
| 3210 | .AesEncryptionKey(128) |
| 3211 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB))); |
| 3212 | |
| 3213 | AuthorizationSet out_params; |
| 3214 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 3215 | Begin(KeyPurpose::SIGN, AuthorizationSet() /* in_params */, &out_params)); |
| 3216 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 3217 | Begin(KeyPurpose::VERIFY, AuthorizationSet() /* in_params */, &out_params)); |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * SigningOperationsTest.HmacAllDigests |
| 3222 | * |
| 3223 | * Verifies that HMAC works with all digests. |
| 3224 | */ |
| 3225 | TEST_P(SigningOperationsTest, HmacAllDigests) { |
| 3226 | for (auto digest : ValidDigests(false /* withNone */, false /* withMD5 */)) { |
| 3227 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3228 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3229 | .HmacKey(128) |
| 3230 | .Digest(digest) |
| 3231 | .Authorization(TAG_MIN_MAC_LENGTH, 160))) |
| 3232 | << "Failed to create HMAC key with digest " << digest; |
| 3233 | string message = "12345678901234567890123456789012"; |
| 3234 | string signature = MacMessage(message, digest, 160); |
| 3235 | EXPECT_EQ(160U / 8U, signature.size()) |
| 3236 | << "Failed to sign with HMAC key with digest " << digest; |
| 3237 | CheckedDeleteKey(); |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * SigningOperationsTest.HmacSha256TooLargeMacLength |
| 3243 | * |
| 3244 | * Verifies that HMAC fails in the correct way when asked to generate a MAC larger than the |
| 3245 | * digest size. |
| 3246 | */ |
| 3247 | TEST_P(SigningOperationsTest, HmacSha256TooLargeMacLength) { |
| 3248 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3249 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3250 | .HmacKey(128) |
| 3251 | .Digest(Digest::SHA_2_256) |
| 3252 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 3253 | AuthorizationSet output_params; |
| 3254 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3255 | AuthorizationSetBuilder() |
| 3256 | .Digest(Digest::SHA_2_256) |
| 3257 | .Authorization(TAG_MAC_LENGTH, 264), |
| 3258 | &output_params)); |
| 3259 | } |
| 3260 | |
| 3261 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3262 | * SigningOperationsTest.HmacSha256InvalidMacLength |
| 3263 | * |
| 3264 | * Verifies that HMAC fails in the correct way when asked to generate a MAC whose length is |
| 3265 | * not a multiple of 8. |
| 3266 | */ |
| 3267 | TEST_P(SigningOperationsTest, HmacSha256InvalidMacLength) { |
| 3268 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3269 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3270 | .HmacKey(128) |
| 3271 | .Digest(Digest::SHA_2_256) |
| 3272 | .Authorization(TAG_MIN_MAC_LENGTH, 160))); |
| 3273 | AuthorizationSet output_params; |
| 3274 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3275 | AuthorizationSetBuilder() |
| 3276 | .Digest(Digest::SHA_2_256) |
| 3277 | .Authorization(TAG_MAC_LENGTH, 161), |
| 3278 | &output_params)); |
| 3279 | } |
| 3280 | |
| 3281 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3282 | * SigningOperationsTest.HmacSha256TooSmallMacLength |
| 3283 | * |
| 3284 | * Verifies that HMAC fails in the correct way when asked to generate a MAC smaller than the |
| 3285 | * specified minimum MAC length. |
| 3286 | */ |
| 3287 | TEST_P(SigningOperationsTest, HmacSha256TooSmallMacLength) { |
| 3288 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3289 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3290 | .HmacKey(128) |
| 3291 | .Digest(Digest::SHA_2_256) |
| 3292 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 3293 | AuthorizationSet output_params; |
| 3294 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 3295 | AuthorizationSetBuilder() |
| 3296 | .Digest(Digest::SHA_2_256) |
| 3297 | .Authorization(TAG_MAC_LENGTH, 120), |
| 3298 | &output_params)); |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * SigningOperationsTest.HmacRfc4231TestCase3 |
| 3303 | * |
| 3304 | * Validates against the test vectors from RFC 4231 test case 3. |
| 3305 | */ |
| 3306 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase3) { |
| 3307 | string key(20, 0xaa); |
| 3308 | string message(50, 0xdd); |
| 3309 | uint8_t sha_224_expected[] = { |
| 3310 | 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9, 0x69, 0x4d, 0x7d, 0x6a, |
| 3311 | 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1, 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea, |
| 3312 | }; |
| 3313 | uint8_t sha_256_expected[] = { |
| 3314 | 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, |
| 3315 | 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, |
| 3316 | 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe, |
| 3317 | }; |
| 3318 | uint8_t sha_384_expected[] = { |
| 3319 | 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2, 0xac, 0xe0, |
| 3320 | 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9, 0x47, 0xac, 0x9f, 0xeb, |
| 3321 | 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, |
| 3322 | 0xc1, 0x38, 0x14, 0xb9, 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27, |
| 3323 | }; |
| 3324 | uint8_t sha_512_expected[] = { |
| 3325 | 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0, 0xf0, 0x75, 0x6c, |
| 3326 | 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd, 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, |
| 3327 | 0x3e, 0x33, 0xb2, 0x27, 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, |
| 3328 | 0xc8, 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46, 0xa3, 0x37, |
| 3329 | 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59, 0xe1, 0x32, 0x92, 0xfb, |
| 3330 | }; |
| 3331 | |
| 3332 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 3333 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 3334 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 3335 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 3336 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | /* |
| 3341 | * SigningOperationsTest.HmacRfc4231TestCase5 |
| 3342 | * |
| 3343 | * Validates against the test vectors from RFC 4231 test case 5. |
| 3344 | */ |
| 3345 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase5) { |
| 3346 | string key(20, 0x0c); |
| 3347 | string message = "Test With Truncation"; |
| 3348 | |
| 3349 | uint8_t sha_224_expected[] = { |
| 3350 | 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, |
| 3351 | 0xc9, 0x88, 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8, |
| 3352 | }; |
| 3353 | uint8_t sha_256_expected[] = { |
| 3354 | 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, |
| 3355 | 0x6e, 0x0c, 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b, |
| 3356 | }; |
| 3357 | uint8_t sha_384_expected[] = { |
| 3358 | 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, |
| 3359 | 0xa4, 0x6e, 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97, |
| 3360 | }; |
| 3361 | uint8_t sha_512_expected[] = { |
| 3362 | 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, |
| 3363 | 0x1d, 0x41, 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6, |
| 3364 | }; |
| 3365 | |
| 3366 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 3367 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 3368 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 3369 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 3370 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | INSTANTIATE_KEYMINT_AIDL_TEST(SigningOperationsTest); |
| 3375 | |
| 3376 | typedef KeyMintAidlTestBase VerificationOperationsTest; |
| 3377 | |
| 3378 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3379 | * VerificationOperationsTest.HmacSigningKeyCannotVerify |
| 3380 | * |
| 3381 | * Verifies HMAC signing and verification, but that a signing key cannot be used to verify. |
| 3382 | */ |
| 3383 | TEST_P(VerificationOperationsTest, HmacSigningKeyCannotVerify) { |
| 3384 | string key_material = "HelloThisIsAKey"; |
| 3385 | |
| 3386 | vector<uint8_t> signing_key, verification_key; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3387 | vector<KeyCharacteristics> signing_key_chars, verification_key_chars; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3388 | EXPECT_EQ(ErrorCode::OK, |
| 3389 | ImportKey(AuthorizationSetBuilder() |
| 3390 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3391 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3392 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 3393 | .Digest(Digest::SHA_2_256) |
| 3394 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3395 | KeyFormat::RAW, key_material, &signing_key, &signing_key_chars)); |
| 3396 | EXPECT_EQ(ErrorCode::OK, |
| 3397 | ImportKey(AuthorizationSetBuilder() |
| 3398 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3399 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3400 | .Authorization(TAG_PURPOSE, KeyPurpose::VERIFY) |
| 3401 | .Digest(Digest::SHA_2_256) |
| 3402 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3403 | KeyFormat::RAW, key_material, &verification_key, &verification_key_chars)); |
| 3404 | |
| 3405 | string message = "This is a message."; |
| 3406 | string signature = SignMessage( |
| 3407 | signing_key, message, |
| 3408 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Authorization(TAG_MAC_LENGTH, 160)); |
| 3409 | |
| 3410 | // Signing key should not work. |
| 3411 | AuthorizationSet out_params; |
| 3412 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3413 | Begin(KeyPurpose::VERIFY, signing_key, |
| 3414 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &out_params)); |
| 3415 | |
| 3416 | // Verification key should work. |
| 3417 | VerifyMessage(verification_key, message, signature, |
| 3418 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 3419 | |
| 3420 | CheckedDeleteKey(&signing_key); |
| 3421 | CheckedDeleteKey(&verification_key); |
| 3422 | } |
| 3423 | |
Prashant Patil | dec9fdc | 2021-12-08 15:25:47 +0000 | [diff] [blame] | 3424 | /* |
| 3425 | * VerificationOperationsTest.HmacVerificationFailsForCorruptSignature |
| 3426 | * |
| 3427 | * Verifies HMAC signature verification should fails if message or signature is corrupted. |
| 3428 | */ |
| 3429 | TEST_P(VerificationOperationsTest, HmacVerificationFailsForCorruptSignature) { |
| 3430 | string key_material = "HelloThisIsAKey"; |
| 3431 | |
| 3432 | vector<uint8_t> signing_key, verification_key; |
| 3433 | vector<KeyCharacteristics> signing_key_chars, verification_key_chars; |
| 3434 | EXPECT_EQ(ErrorCode::OK, |
| 3435 | ImportKey(AuthorizationSetBuilder() |
| 3436 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3437 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3438 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 3439 | .Digest(Digest::SHA_2_256) |
| 3440 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3441 | KeyFormat::RAW, key_material, &signing_key, &signing_key_chars)); |
| 3442 | EXPECT_EQ(ErrorCode::OK, |
| 3443 | ImportKey(AuthorizationSetBuilder() |
| 3444 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3445 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3446 | .Authorization(TAG_PURPOSE, KeyPurpose::VERIFY) |
| 3447 | .Digest(Digest::SHA_2_256) |
| 3448 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3449 | KeyFormat::RAW, key_material, &verification_key, &verification_key_chars)); |
| 3450 | |
| 3451 | string message = "This is a message."; |
| 3452 | string signature = SignMessage( |
| 3453 | signing_key, message, |
| 3454 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Authorization(TAG_MAC_LENGTH, 160)); |
| 3455 | |
| 3456 | AuthorizationSet begin_out_params; |
| 3457 | ASSERT_EQ(ErrorCode::OK, |
| 3458 | Begin(KeyPurpose::VERIFY, verification_key, |
| 3459 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &begin_out_params)); |
| 3460 | |
| 3461 | string corruptMessage = "This is b message."; // Corrupted message |
| 3462 | string output; |
| 3463 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(corruptMessage, signature, &output)); |
| 3464 | |
| 3465 | ASSERT_EQ(ErrorCode::OK, |
| 3466 | Begin(KeyPurpose::VERIFY, verification_key, |
| 3467 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &begin_out_params)); |
| 3468 | |
| 3469 | signature[0] += 1; // Corrupt a signature |
| 3470 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(message, signature, &output)); |
| 3471 | |
| 3472 | CheckedDeleteKey(&signing_key); |
| 3473 | CheckedDeleteKey(&verification_key); |
| 3474 | } |
| 3475 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3476 | INSTANTIATE_KEYMINT_AIDL_TEST(VerificationOperationsTest); |
| 3477 | |
| 3478 | typedef KeyMintAidlTestBase ExportKeyTest; |
| 3479 | |
| 3480 | /* |
| 3481 | * ExportKeyTest.RsaUnsupportedKeyFormat |
| 3482 | * |
| 3483 | * Verifies that attempting to export RSA keys in PKCS#8 format fails with the correct error. |
| 3484 | */ |
| 3485 | // TODO(seleneh) add ExportKey to GenerateKey |
| 3486 | // check result |
| 3487 | |
| 3488 | class ImportKeyTest : public KeyMintAidlTestBase { |
| 3489 | public: |
| 3490 | template <TagType tag_type, Tag tag, typename ValueT> |
| 3491 | void CheckCryptoParam(TypedTag<tag_type, tag> ttag, ValueT expected) { |
| 3492 | SCOPED_TRACE("CheckCryptoParam"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3493 | for (auto& entry : key_characteristics_) { |
| 3494 | if (entry.securityLevel == SecLevel()) { |
| 3495 | EXPECT_TRUE(contains(entry.authorizations, ttag, expected)) |
| 3496 | << "Tag " << tag << " with value " << expected |
| 3497 | << " not found at security level" << entry.securityLevel; |
| 3498 | } else { |
| 3499 | EXPECT_FALSE(contains(entry.authorizations, ttag, expected)) |
| 3500 | << "Tag " << tag << " found at security level " << entry.securityLevel; |
| 3501 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | void CheckOrigin() { |
| 3506 | SCOPED_TRACE("CheckOrigin"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3507 | // Origin isn't a crypto param, but it always lives with them. |
| 3508 | return CheckCryptoParam(TAG_ORIGIN, KeyOrigin::IMPORTED); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3509 | } |
| 3510 | }; |
| 3511 | |
| 3512 | /* |
| 3513 | * ImportKeyTest.RsaSuccess |
| 3514 | * |
| 3515 | * Verifies that importing and using an RSA key pair works correctly. |
| 3516 | */ |
| 3517 | TEST_P(ImportKeyTest, RsaSuccess) { |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3518 | uint32_t key_size; |
| 3519 | string key; |
| 3520 | |
| 3521 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3522 | key_size = 2048; |
| 3523 | key = rsa_2048_key; |
| 3524 | } else { |
| 3525 | key_size = 1024; |
| 3526 | key = rsa_key; |
| 3527 | } |
| 3528 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3529 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3530 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3531 | .RsaSigningKey(key_size, 65537) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3532 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3533 | .Padding(PaddingMode::RSA_PSS) |
| 3534 | .SetDefaultValidity(), |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3535 | KeyFormat::PKCS8, key)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3536 | |
| 3537 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3538 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3539 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3540 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3541 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3542 | CheckOrigin(); |
| 3543 | |
| 3544 | string message(1024 / 8, 'a'); |
| 3545 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3546 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3547 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3551 | * ImportKeyTest.RsaSuccessWithoutParams |
| 3552 | * |
| 3553 | * Verifies that importing and using an RSA key pair without specifying parameters |
| 3554 | * works correctly. |
| 3555 | */ |
| 3556 | TEST_P(ImportKeyTest, RsaSuccessWithoutParams) { |
| 3557 | uint32_t key_size; |
| 3558 | string key; |
| 3559 | |
| 3560 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3561 | key_size = 2048; |
| 3562 | key = rsa_2048_key; |
| 3563 | } else { |
| 3564 | key_size = 1024; |
| 3565 | key = rsa_key; |
| 3566 | } |
| 3567 | |
| 3568 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3569 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3570 | .SigningKey() |
| 3571 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 3572 | .Digest(Digest::SHA_2_256) |
| 3573 | .Padding(PaddingMode::RSA_PSS) |
| 3574 | .SetDefaultValidity(), |
| 3575 | KeyFormat::PKCS8, key)); |
| 3576 | |
| 3577 | // Key size and public exponent are determined from the imported key material. |
| 3578 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
| 3579 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3580 | |
| 3581 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
| 3582 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3583 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3584 | CheckOrigin(); |
| 3585 | |
| 3586 | string message(1024 / 8, 'a'); |
| 3587 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3588 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3589 | LocalVerifyMessage(message, signature, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3593 | * ImportKeyTest.RsaKeySizeMismatch |
| 3594 | * |
| 3595 | * Verifies that importing an RSA key pair with a size that doesn't match the key fails in the |
| 3596 | * correct way. |
| 3597 | */ |
| 3598 | TEST_P(ImportKeyTest, RsaKeySizeMismatch) { |
| 3599 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3600 | ImportKey(AuthorizationSetBuilder() |
| 3601 | .RsaSigningKey(2048 /* Doesn't match key */, 65537) |
| 3602 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3603 | .Padding(PaddingMode::NONE) |
| 3604 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3605 | KeyFormat::PKCS8, rsa_key)); |
| 3606 | } |
| 3607 | |
| 3608 | /* |
| 3609 | * ImportKeyTest.RsaPublicExponentMismatch |
| 3610 | * |
| 3611 | * Verifies that importing an RSA key pair with a public exponent that doesn't match the key |
| 3612 | * fails in the correct way. |
| 3613 | */ |
| 3614 | TEST_P(ImportKeyTest, RsaPublicExponentMismatch) { |
| 3615 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3616 | ImportKey(AuthorizationSetBuilder() |
| 3617 | .RsaSigningKey(1024, 3 /* Doesn't match key */) |
| 3618 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3619 | .Padding(PaddingMode::NONE) |
| 3620 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3621 | KeyFormat::PKCS8, rsa_key)); |
| 3622 | } |
| 3623 | |
| 3624 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3625 | * ImportKeyTest.RsaAttestMultiPurposeFail |
| 3626 | * |
| 3627 | * Verifies that importing an RSA key pair with purpose ATTEST_KEY+SIGN fails. |
| 3628 | */ |
| 3629 | TEST_P(ImportKeyTest, RsaAttestMultiPurposeFail) { |
| 3630 | uint32_t key_size = 2048; |
| 3631 | string key = rsa_2048_key; |
| 3632 | |
| 3633 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3634 | ImportKey(AuthorizationSetBuilder() |
| 3635 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3636 | .RsaSigningKey(key_size, 65537) |
| 3637 | .AttestKey() |
| 3638 | .Digest(Digest::SHA_2_256) |
| 3639 | .Padding(PaddingMode::RSA_PSS) |
| 3640 | .SetDefaultValidity(), |
| 3641 | KeyFormat::PKCS8, key)); |
| 3642 | } |
| 3643 | |
| 3644 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3645 | * ImportKeyTest.EcdsaSuccess |
| 3646 | * |
| 3647 | * Verifies that importing and using an ECDSA P-256 key pair works correctly. |
| 3648 | */ |
| 3649 | TEST_P(ImportKeyTest, EcdsaSuccess) { |
| 3650 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3651 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3652 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3653 | .Digest(Digest::SHA_2_256) |
| 3654 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3655 | KeyFormat::PKCS8, ec_256_key)); |
| 3656 | |
| 3657 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3658 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3659 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3660 | |
| 3661 | CheckOrigin(); |
| 3662 | |
| 3663 | string message(32, 'a'); |
| 3664 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3665 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3666 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * ImportKeyTest.EcdsaP256RFC5915Success |
| 3671 | * |
| 3672 | * Verifies that importing and using an ECDSA P-256 key pair encoded using RFC5915 works |
| 3673 | * correctly. |
| 3674 | */ |
| 3675 | TEST_P(ImportKeyTest, EcdsaP256RFC5915Success) { |
| 3676 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3677 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3678 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3679 | .Digest(Digest::SHA_2_256) |
| 3680 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3681 | KeyFormat::PKCS8, ec_256_key_rfc5915)); |
| 3682 | |
| 3683 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3684 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3685 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3686 | |
| 3687 | CheckOrigin(); |
| 3688 | |
| 3689 | string message(32, 'a'); |
| 3690 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3691 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3692 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /* |
| 3696 | * ImportKeyTest.EcdsaP256SEC1Success |
| 3697 | * |
| 3698 | * Verifies that importing and using an ECDSA P-256 key pair encoded using SEC1 works correctly. |
| 3699 | */ |
| 3700 | TEST_P(ImportKeyTest, EcdsaP256SEC1Success) { |
| 3701 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3702 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3703 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3704 | .Digest(Digest::SHA_2_256) |
| 3705 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3706 | KeyFormat::PKCS8, ec_256_key_sec1)); |
| 3707 | |
| 3708 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3709 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3710 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3711 | |
| 3712 | CheckOrigin(); |
| 3713 | |
| 3714 | string message(32, 'a'); |
| 3715 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3716 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3717 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | /* |
| 3721 | * ImportKeyTest.Ecdsa521Success |
| 3722 | * |
| 3723 | * Verifies that importing and using an ECDSA P-521 key pair works correctly. |
| 3724 | */ |
| 3725 | TEST_P(ImportKeyTest, Ecdsa521Success) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 3726 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3727 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 3728 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3729 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3730 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3731 | .EcdsaSigningKey(EcCurve::P_521) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3732 | .Digest(Digest::SHA_2_256) |
| 3733 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3734 | KeyFormat::PKCS8, ec_521_key)); |
| 3735 | |
| 3736 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3737 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3738 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_521); |
| 3739 | CheckOrigin(); |
| 3740 | |
| 3741 | string message(32, 'a'); |
| 3742 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3743 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3744 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3745 | } |
| 3746 | |
| 3747 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3748 | * ImportKeyTest.EcdsaCurveMismatch |
| 3749 | * |
| 3750 | * Verifies that importing an ECDSA key pair with a curve that doesn't match the key fails in |
| 3751 | * the correct way. |
| 3752 | */ |
| 3753 | TEST_P(ImportKeyTest, EcdsaCurveMismatch) { |
| 3754 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3755 | ImportKey(AuthorizationSetBuilder() |
| 3756 | .EcdsaSigningKey(EcCurve::P_224 /* Doesn't match key */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3757 | .Digest(Digest::NONE) |
| 3758 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3759 | KeyFormat::PKCS8, ec_256_key)); |
| 3760 | } |
| 3761 | |
| 3762 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 3763 | * ImportKeyTest.EcdsaAttestMultiPurposeFail |
| 3764 | * |
| 3765 | * Verifies that importing and using an ECDSA P-256 key pair with purpose ATTEST_KEY+SIGN fails. |
| 3766 | */ |
| 3767 | TEST_P(ImportKeyTest, EcdsaAttestMultiPurposeFail) { |
| 3768 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3769 | ImportKey(AuthorizationSetBuilder() |
| 3770 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3771 | .EcdsaSigningKey(EcCurve::P_256) |
| 3772 | .AttestKey() |
| 3773 | .Digest(Digest::SHA_2_256) |
| 3774 | .SetDefaultValidity(), |
| 3775 | KeyFormat::PKCS8, ec_256_key)); |
| 3776 | } |
| 3777 | |
| 3778 | /* |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 3779 | * ImportKeyTest.Ed25519RawSuccess |
| 3780 | * |
| 3781 | * Verifies that importing and using a raw Ed25519 private key works correctly. |
| 3782 | */ |
| 3783 | TEST_P(ImportKeyTest, Ed25519RawSuccess) { |
| 3784 | if (!Curve25519Supported()) { |
| 3785 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3786 | } |
| 3787 | |
| 3788 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3789 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3790 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3791 | .Digest(Digest::NONE) |
| 3792 | .SetDefaultValidity(), |
| 3793 | KeyFormat::RAW, ed25519_key)); |
| 3794 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 3795 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 3796 | CheckOrigin(); |
| 3797 | |
| 3798 | // The returned cert should hold the correct public key. |
| 3799 | ASSERT_GT(cert_chain_.size(), 0); |
| 3800 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 3801 | ASSERT_NE(kmKeyCert, nullptr); |
| 3802 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 3803 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 3804 | size_t kmPubKeySize = 32; |
| 3805 | uint8_t kmPubKeyData[32]; |
| 3806 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 3807 | ASSERT_EQ(kmPubKeySize, 32); |
| 3808 | EXPECT_EQ(string(kmPubKeyData, kmPubKeyData + 32), ed25519_pubkey); |
| 3809 | |
| 3810 | string message(32, 'a'); |
| 3811 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 3812 | string signature = SignMessage(message, params); |
| 3813 | LocalVerifyMessage(message, signature, params); |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * ImportKeyTest.Ed25519Pkcs8Success |
| 3818 | * |
| 3819 | * Verifies that importing and using a PKCS#8-encoded Ed25519 private key works correctly. |
| 3820 | */ |
| 3821 | TEST_P(ImportKeyTest, Ed25519Pkcs8Success) { |
| 3822 | if (!Curve25519Supported()) { |
| 3823 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3824 | } |
| 3825 | |
| 3826 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3827 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3828 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3829 | .Digest(Digest::NONE) |
| 3830 | .SetDefaultValidity(), |
| 3831 | KeyFormat::PKCS8, ed25519_pkcs8_key)); |
| 3832 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 3833 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 3834 | CheckOrigin(); |
| 3835 | |
| 3836 | // The returned cert should hold the correct public key. |
| 3837 | ASSERT_GT(cert_chain_.size(), 0); |
| 3838 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 3839 | ASSERT_NE(kmKeyCert, nullptr); |
| 3840 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 3841 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 3842 | size_t kmPubKeySize = 32; |
| 3843 | uint8_t kmPubKeyData[32]; |
| 3844 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 3845 | ASSERT_EQ(kmPubKeySize, 32); |
| 3846 | EXPECT_EQ(string(kmPubKeyData, kmPubKeyData + 32), ed25519_pubkey); |
| 3847 | |
| 3848 | string message(32, 'a'); |
| 3849 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 3850 | string signature = SignMessage(message, params); |
| 3851 | LocalVerifyMessage(message, signature, params); |
| 3852 | } |
| 3853 | |
| 3854 | /* |
| 3855 | * ImportKeyTest.Ed25519CurveMismatch |
| 3856 | * |
| 3857 | * Verifies that importing an Ed25519 key pair with a curve that doesn't match the key fails in |
| 3858 | * the correct way. |
| 3859 | */ |
| 3860 | TEST_P(ImportKeyTest, Ed25519CurveMismatch) { |
| 3861 | if (!Curve25519Supported()) { |
| 3862 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3863 | } |
| 3864 | |
| 3865 | ASSERT_NE(ErrorCode::OK, |
| 3866 | ImportKey(AuthorizationSetBuilder() |
| 3867 | .EcdsaSigningKey(EcCurve::P_224 /* Doesn't match key */) |
| 3868 | .Digest(Digest::NONE) |
| 3869 | .SetDefaultValidity(), |
| 3870 | KeyFormat::RAW, ed25519_key)); |
| 3871 | } |
| 3872 | |
| 3873 | /* |
| 3874 | * ImportKeyTest.Ed25519FormatMismatch |
| 3875 | * |
| 3876 | * Verifies that importing an Ed25519 key pair with an invalid format fails. |
| 3877 | */ |
| 3878 | TEST_P(ImportKeyTest, Ed25519FormatMismatch) { |
| 3879 | if (!Curve25519Supported()) { |
| 3880 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3881 | } |
| 3882 | |
| 3883 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3884 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3885 | .Digest(Digest::NONE) |
| 3886 | .SetDefaultValidity(), |
| 3887 | KeyFormat::PKCS8, ed25519_key)); |
| 3888 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3889 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3890 | .Digest(Digest::NONE) |
| 3891 | .SetDefaultValidity(), |
| 3892 | KeyFormat::RAW, ed25519_pkcs8_key)); |
| 3893 | } |
| 3894 | |
| 3895 | /* |
| 3896 | * ImportKeyTest.Ed25519PurposeMismatch |
| 3897 | * |
| 3898 | * Verifies that importing an Ed25519 key pair with an invalid purpose fails. |
| 3899 | */ |
| 3900 | TEST_P(ImportKeyTest, Ed25519PurposeMismatch) { |
| 3901 | if (!Curve25519Supported()) { |
| 3902 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3903 | } |
| 3904 | |
| 3905 | // Can't have both SIGN and ATTEST_KEY |
| 3906 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3907 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 3908 | .Authorization(TAG_PURPOSE, KeyPurpose::ATTEST_KEY) |
| 3909 | .Digest(Digest::NONE) |
| 3910 | .SetDefaultValidity(), |
| 3911 | KeyFormat::RAW, ed25519_key)); |
| 3912 | // AGREE_KEY is for X25519 (but can only tell the difference if the import key is in |
| 3913 | // PKCS#8 format and so includes an OID). |
| 3914 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3915 | .EcdsaKey(EcCurve::CURVE_25519) |
| 3916 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 3917 | .Digest(Digest::NONE) |
| 3918 | .SetDefaultValidity(), |
| 3919 | KeyFormat::PKCS8, ed25519_pkcs8_key)); |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * ImportKeyTest.X25519RawSuccess |
| 3924 | * |
| 3925 | * Verifies that importing and using a raw X25519 private key works correctly. |
| 3926 | */ |
| 3927 | TEST_P(ImportKeyTest, X25519RawSuccess) { |
| 3928 | if (!Curve25519Supported()) { |
| 3929 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3930 | } |
| 3931 | |
| 3932 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3933 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3934 | .EcdsaKey(EcCurve::CURVE_25519) |
| 3935 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 3936 | .SetDefaultValidity(), |
| 3937 | KeyFormat::RAW, x25519_key)); |
| 3938 | |
| 3939 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 3940 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 3941 | CheckOrigin(); |
| 3942 | } |
| 3943 | |
| 3944 | /* |
| 3945 | * ImportKeyTest.X25519Pkcs8Success |
| 3946 | * |
| 3947 | * Verifies that importing and using a PKCS#8-encoded X25519 private key works correctly. |
| 3948 | */ |
| 3949 | TEST_P(ImportKeyTest, X25519Pkcs8Success) { |
| 3950 | if (!Curve25519Supported()) { |
| 3951 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3952 | } |
| 3953 | |
| 3954 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3955 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3956 | .EcdsaKey(EcCurve::CURVE_25519) |
| 3957 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 3958 | .SetDefaultValidity(), |
| 3959 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 3960 | |
| 3961 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
| 3962 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::CURVE_25519); |
| 3963 | CheckOrigin(); |
| 3964 | } |
| 3965 | |
| 3966 | /* |
| 3967 | * ImportKeyTest.X25519CurveMismatch |
| 3968 | * |
| 3969 | * Verifies that importing an X25519 key with a curve that doesn't match the key fails in |
| 3970 | * the correct way. |
| 3971 | */ |
| 3972 | TEST_P(ImportKeyTest, X25519CurveMismatch) { |
| 3973 | if (!Curve25519Supported()) { |
| 3974 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3975 | } |
| 3976 | |
| 3977 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3978 | .EcdsaKey(EcCurve::P_224 /* Doesn't match key */) |
| 3979 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 3980 | .SetDefaultValidity(), |
| 3981 | KeyFormat::RAW, x25519_key)); |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * ImportKeyTest.X25519FormatMismatch |
| 3986 | * |
| 3987 | * Verifies that importing an X25519 key with an invalid format fails. |
| 3988 | */ |
| 3989 | TEST_P(ImportKeyTest, X25519FormatMismatch) { |
| 3990 | if (!Curve25519Supported()) { |
| 3991 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 3992 | } |
| 3993 | |
| 3994 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3995 | .EcdsaKey(EcCurve::CURVE_25519) |
| 3996 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 3997 | .SetDefaultValidity(), |
| 3998 | KeyFormat::PKCS8, x25519_key)); |
| 3999 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4000 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4001 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 4002 | .SetDefaultValidity(), |
| 4003 | KeyFormat::RAW, x25519_pkcs8_key)); |
| 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | * ImportKeyTest.X25519PurposeMismatch |
| 4008 | * |
| 4009 | * Verifies that importing an X25519 key pair with an invalid format fails. |
| 4010 | */ |
| 4011 | TEST_P(ImportKeyTest, X25519PurposeMismatch) { |
| 4012 | if (!Curve25519Supported()) { |
| 4013 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 4014 | } |
| 4015 | |
| 4016 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4017 | .EcdsaKey(EcCurve::CURVE_25519) |
| 4018 | .Authorization(TAG_PURPOSE, KeyPurpose::ATTEST_KEY) |
| 4019 | .SetDefaultValidity(), |
| 4020 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 4021 | ASSERT_NE(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4022 | .EcdsaSigningKey(EcCurve::CURVE_25519) |
| 4023 | .SetDefaultValidity(), |
| 4024 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 4025 | } |
| 4026 | |
| 4027 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4028 | * ImportKeyTest.AesSuccess |
| 4029 | * |
| 4030 | * Verifies that importing and using an AES key works. |
| 4031 | */ |
| 4032 | TEST_P(ImportKeyTest, AesSuccess) { |
| 4033 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4034 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4035 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4036 | .AesEncryptionKey(key.size() * 8) |
| 4037 | .EcbMode() |
| 4038 | .Padding(PaddingMode::PKCS7), |
| 4039 | KeyFormat::RAW, key)); |
| 4040 | |
| 4041 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::AES); |
| 4042 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 4043 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 4044 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 4045 | CheckOrigin(); |
| 4046 | |
| 4047 | string message = "Hello World!"; |
| 4048 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4049 | string ciphertext = EncryptMessage(message, params); |
| 4050 | string plaintext = DecryptMessage(ciphertext, params); |
| 4051 | EXPECT_EQ(message, plaintext); |
| 4052 | } |
| 4053 | |
| 4054 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4055 | * ImportKeyTest.AesFailure |
| 4056 | * |
| 4057 | * Verifies that importing an invalid AES key fails. |
| 4058 | */ |
| 4059 | TEST_P(ImportKeyTest, AesFailure) { |
| 4060 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4061 | uint32_t bitlen = key.size() * 8; |
| 4062 | 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] | 4063 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4064 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 4065 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4066 | .AesEncryptionKey(key_size) |
| 4067 | .EcbMode() |
| 4068 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4069 | KeyFormat::RAW, key); |
| 4070 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4071 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 4072 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4073 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4074 | |
| 4075 | // Explicit key size matches that of the provided key, but it's not a valid size. |
| 4076 | string long_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4077 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4078 | ImportKey(AuthorizationSetBuilder() |
| 4079 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4080 | .AesEncryptionKey(long_key.size() * 8) |
| 4081 | .EcbMode() |
| 4082 | .Padding(PaddingMode::PKCS7), |
| 4083 | KeyFormat::RAW, long_key)); |
| 4084 | string short_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4085 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4086 | ImportKey(AuthorizationSetBuilder() |
| 4087 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4088 | .AesEncryptionKey(short_key.size() * 8) |
| 4089 | .EcbMode() |
| 4090 | .Padding(PaddingMode::PKCS7), |
| 4091 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* |
| 4095 | * ImportKeyTest.TripleDesSuccess |
| 4096 | * |
| 4097 | * Verifies that importing and using a 3DES key works. |
| 4098 | */ |
| 4099 | TEST_P(ImportKeyTest, TripleDesSuccess) { |
| 4100 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
| 4101 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4102 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4103 | .TripleDesEncryptionKey(168) |
| 4104 | .EcbMode() |
| 4105 | .Padding(PaddingMode::PKCS7), |
| 4106 | KeyFormat::RAW, key)); |
| 4107 | |
| 4108 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::TRIPLE_DES); |
| 4109 | CheckCryptoParam(TAG_KEY_SIZE, 168U); |
| 4110 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 4111 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 4112 | CheckOrigin(); |
| 4113 | |
| 4114 | string message = "Hello World!"; |
| 4115 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4116 | string ciphertext = EncryptMessage(message, params); |
| 4117 | string plaintext = DecryptMessage(ciphertext, params); |
| 4118 | EXPECT_EQ(message, plaintext); |
| 4119 | } |
| 4120 | |
| 4121 | /* |
| 4122 | * ImportKeyTest.TripleDesFailure |
| 4123 | * |
| 4124 | * Verifies that importing an invalid 3DES key fails. |
| 4125 | */ |
| 4126 | TEST_P(ImportKeyTest, TripleDesFailure) { |
| 4127 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4128 | uint32_t bitlen = key.size() * 7; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4129 | 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] | 4130 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4131 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 4132 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4133 | .TripleDesEncryptionKey(key_size) |
| 4134 | .EcbMode() |
| 4135 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 4136 | KeyFormat::RAW, key); |
| 4137 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4138 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 4139 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4140 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4141 | // 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] | 4142 | string long_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f735800"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4143 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4144 | ImportKey(AuthorizationSetBuilder() |
| 4145 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4146 | .TripleDesEncryptionKey(long_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4147 | .EcbMode() |
| 4148 | .Padding(PaddingMode::PKCS7), |
| 4149 | KeyFormat::RAW, long_key)); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4150 | string short_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f73"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4151 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 4152 | ImportKey(AuthorizationSetBuilder() |
| 4153 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 4154 | .TripleDesEncryptionKey(short_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 4155 | .EcbMode() |
| 4156 | .Padding(PaddingMode::PKCS7), |
| 4157 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | /* |
| 4161 | * ImportKeyTest.HmacKeySuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4162 | * |
| 4163 | * Verifies that importing and using an HMAC key works. |
| 4164 | */ |
| 4165 | TEST_P(ImportKeyTest, HmacKeySuccess) { |
| 4166 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4167 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 4168 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4169 | .HmacKey(key.size() * 8) |
| 4170 | .Digest(Digest::SHA_2_256) |
| 4171 | .Authorization(TAG_MIN_MAC_LENGTH, 256), |
| 4172 | KeyFormat::RAW, key)); |
| 4173 | |
| 4174 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::HMAC); |
| 4175 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 4176 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 4177 | CheckOrigin(); |
| 4178 | |
| 4179 | string message = "Hello World!"; |
| 4180 | string signature = MacMessage(message, Digest::SHA_2_256, 256); |
| 4181 | VerifyMessage(message, signature, AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 4182 | } |
| 4183 | |
| 4184 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportKeyTest); |
| 4185 | |
| 4186 | auto wrapped_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4187 | // IKeyMintDevice.aidl |
| 4188 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 4189 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4190 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 4191 | "934bf94e2aa28a3f83c9f79297250262" |
| 4192 | "fbe3276b5a1c91159bbfa3ef8957aac8" |
| 4193 | "4b59b30b455a79c2973480823d8b3863" |
| 4194 | "c3deef4a8e243590268d80e18751a0e1" |
| 4195 | "30f67ce6a1ace9f79b95e097474febc9" |
| 4196 | "81195b1d13a69086c0863f66a7b7fdb4" |
| 4197 | "8792227b1ac5e2489febdf087ab54864" |
| 4198 | "83033a6f001ca5d1ec1e27f5c30f4cec" |
| 4199 | "2642074a39ae68aee552e196627a8e3d" |
| 4200 | "867e67a8c01b11e75f13cca0a97ab668" |
| 4201 | "b50cda07a8ecb7cd8e3dd7009c963653" |
| 4202 | "4f6f239cffe1fc8daa466f78b676c711" |
| 4203 | "9efb96bce4e69ca2a25d0b34ed9c3ff9" |
| 4204 | "99b801597d5220e307eaa5bee507fb94" |
| 4205 | "d1fa69f9e519b2de315bac92c36f2ea1" |
| 4206 | "fa1df4478c0ddedeae8c70e0233cd098" |
| 4207 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 4208 | "d796b02c370f1fa4cc0124f1" |
| 4209 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 4210 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 4211 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 4212 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 4213 | "3106" // SET length 0x06 |
| 4214 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 4215 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 4216 | // } end SET |
| 4217 | // } end [1] |
| 4218 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 4219 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 4220 | // } end [2] |
| 4221 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 4222 | "02020100" // INTEGER length 2 value 0x100 |
| 4223 | // } end [3] |
| 4224 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode) |
| 4225 | "3103" // SET length 0x03 { |
| 4226 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 4227 | // } end SET |
| 4228 | // } end [4] |
| 4229 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 4230 | "3103" // SET length 0x03 { |
| 4231 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 4232 | // } end SET |
| 4233 | // } end [5] |
| 4234 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 4235 | // (noAuthRequired) |
| 4236 | "0500" // NULL |
| 4237 | // } end [503] |
| 4238 | // } end SEQUENCE (AuthorizationList) |
| 4239 | // } end SEQUENCE (KeyDescription) |
| 4240 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 4241 | "ccd540855f833a5e1480bfd2d36faf3a" |
| 4242 | "eee15df5beabe2691bc82dde2a7aa910" |
| 4243 | "0410" // OCTET STRING length 0x10 (tag) |
| 4244 | "64c9f689c60ff6223ab6e6999e0eb6e5" |
| 4245 | // } SEQUENCE (SecureKeyWrapper) |
| 4246 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4247 | |
| 4248 | auto wrapped_key_masked = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4249 | // IKeyMintDevice.aidl |
| 4250 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 4251 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4252 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 4253 | "aad93ed5924f283b4bb5526fbe7a1412" |
| 4254 | "f9d9749ec30db9062b29e574a8546f33" |
| 4255 | "c88732452f5b8e6a391ee76c39ed1712" |
| 4256 | "c61d8df6213dec1cffbc17a8c6d04c7b" |
| 4257 | "30893d8daa9b2015213e219468215532" |
| 4258 | "07f8f9931c4caba23ed3bee28b36947e" |
| 4259 | "47f10e0a5c3dc51c988a628daad3e5e1" |
| 4260 | "f4005e79c2d5a96c284b4b8d7e4948f3" |
| 4261 | "31e5b85dd5a236f85579f3ea1d1b8484" |
| 4262 | "87470bdb0ab4f81a12bee42c99fe0df4" |
| 4263 | "bee3759453e69ad1d68a809ce06b949f" |
| 4264 | "7694a990429b2fe81e066ff43e56a216" |
| 4265 | "02db70757922a4bcc23ab89f1e35da77" |
| 4266 | "586775f423e519c2ea394caf48a28d0c" |
| 4267 | "8020f1dcf6b3a68ec246f615ae96dae9" |
| 4268 | "a079b1f6eb959033c1af5c125fd94168" |
| 4269 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 4270 | "6d9721d08589581ab49204a3" |
| 4271 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 4272 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 4273 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 4274 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 4275 | "3106" // SET length 0x06 |
| 4276 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 4277 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 4278 | // } end SET |
| 4279 | // } end [1] |
| 4280 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 4281 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 4282 | // } end [2] |
| 4283 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 4284 | "02020100" // INTEGER length 2 value 0x100 |
| 4285 | // } end [3] |
| 4286 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode |
| 4287 | "3103" // SET length 0x03 { |
| 4288 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 4289 | // } end SET |
| 4290 | // } end [4] |
| 4291 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 4292 | "3103" // SET length 0x03 { |
| 4293 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 4294 | // } end SET |
| 4295 | // } end [5] |
| 4296 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 4297 | // (noAuthRequired) |
| 4298 | "0500" // NULL |
| 4299 | // } end [503] |
| 4300 | // } end SEQUENCE (AuthorizationList) |
| 4301 | // } end SEQUENCE (KeyDescription) |
| 4302 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 4303 | "a61c6e247e25b3e6e69aa78eb03c2d4a" |
| 4304 | "c20d1f99a9a024a76f35c8e2cab9b68d" |
| 4305 | "0410" // OCTET STRING length 0x10 (tag) |
| 4306 | "2560c70109ae67c030f00b98b512a670" |
| 4307 | // } SEQUENCE (SecureKeyWrapper) |
| 4308 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4309 | |
| 4310 | auto wrapping_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4311 | // RFC 5208 s5 |
| 4312 | "308204be" // SEQUENCE length 0x4be (PrivateKeyInfo) { |
| 4313 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4314 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 4315 | "0609" // OBJECT IDENTIFIER length 0x09 (algorithm) |
| 4316 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (RSAES-PKCS1-v1_5 encryption scheme) |
| 4317 | "0500" // NULL (parameters) |
| 4318 | // } SEQUENCE (AlgorithmIdentifier) |
| 4319 | "048204a8" // OCTET STRING len 0x4a8 (privateKey), which contains... |
| 4320 | // RFC 8017 A.1.2 |
| 4321 | "308204a4" // SEQUENCE len 0x4a4 (RSAPrivateKey) { |
| 4322 | "020100" // INTEGER length 1 value 0x00 (version) |
| 4323 | "02820101" // INTEGER length 0x0101 (modulus) value... |
| 4324 | "00aec367931d8900ce56b0067f7d70e1" // 0x10 |
| 4325 | "fc653f3f34d194c1fed50018fb43db93" // 0x20 |
| 4326 | "7b06e673a837313d56b1c725150a3fef" // 0x30 |
| 4327 | "86acbddc41bb759c2854eae32d35841e" // 0x40 |
| 4328 | "fb5c18d82bc90a1cb5c1d55adf245b02" // 0x50 |
| 4329 | "911f0b7cda88c421ff0ebafe7c0d23be" // 0x60 |
| 4330 | "312d7bd5921ffaea1347c157406fef71" // 0x70 |
| 4331 | "8f682643e4e5d33c6703d61c0cf7ac0b" // 0x80 |
| 4332 | "f4645c11f5c1374c3886427411c44979" // 0x90 |
| 4333 | "6792e0bef75dec858a2123c36753e02a" // 0xa0 |
| 4334 | "95a96d7c454b504de385a642e0dfc3e6" // 0xb0 |
| 4335 | "0ac3a7ee4991d0d48b0172a95f9536f0" // 0xc0 |
| 4336 | "2ba13cecccb92b727db5c27e5b2f5cec" // 0xd0 |
| 4337 | "09600b286af5cf14c42024c61ddfe71c" // 0xe0 |
| 4338 | "2a8d7458f185234cb00e01d282f10f8f" // 0xf0 |
| 4339 | "c6721d2aed3f4833cca2bd8fa62821dd" // 0x100 |
| 4340 | "55" // 0x101 |
| 4341 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 4342 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 4343 | "431447b6251908112b1ee76f99f3711a" // 0x10 |
| 4344 | "52b6630960046c2de70de188d833f8b8" // 0x20 |
| 4345 | "b91e4d785caeeeaf4f0f74414e2cda40" // 0x30 |
| 4346 | "641f7fe24f14c67a88959bdb27766df9" // 0x40 |
| 4347 | "e710b630a03adc683b5d2c43080e52be" // 0x50 |
| 4348 | "e71e9eaeb6de297a5fea1072070d181c" // 0x60 |
| 4349 | "822bccff087d63c940ba8a45f670feb2" // 0x70 |
| 4350 | "9fb4484d1c95e6d2579ba02aae0a0090" // 0x80 |
| 4351 | "0c3ebf490e3d2cd7ee8d0e20c536e4dc" // 0x90 |
| 4352 | "5a5097272888cddd7e91f228b1c4d747" // 0xa0 |
| 4353 | "4c55b8fcd618c4a957bbddd5ad7407cc" // 0xb0 |
| 4354 | "312d8d98a5caf7e08f4a0d6b45bb41c6" // 0xc0 |
| 4355 | "52659d5a5ba05b663737a8696281865b" // 0xd0 |
| 4356 | "a20fbdd7f851e6c56e8cbe0ddbbf24dc" // 0xe0 |
| 4357 | "03b2d2cb4c3d540fb0af52e034a2d066" // 0xf0 |
| 4358 | "98b128e5f101e3b51a34f8d8b4f86181" // 0x100 |
| 4359 | "028181" // INTEGER length 0x81 (prime1) value... |
| 4360 | "00de392e18d682c829266cc3454e1d61" // 0x10 |
| 4361 | "66242f32d9a1d10577753e904ea7d08b" // 0x20 |
| 4362 | "ff841be5bac82a164c5970007047b8c5" // 0x30 |
| 4363 | "17db8f8f84e37bd5988561bdf503d4dc" // 0x40 |
| 4364 | "2bdb38f885434ae42c355f725c9a60f9" // 0x50 |
| 4365 | "1f0788e1f1a97223b524b5357fdf72e2" // 0x60 |
| 4366 | "f696bab7d78e32bf92ba8e1864eab122" // 0x70 |
| 4367 | "9e91346130748a6e3c124f9149d71c74" // 0x80 |
| 4368 | "35" |
| 4369 | "028181" // INTEGER length 0x81 (prime2) value... |
| 4370 | "00c95387c0f9d35f137b57d0d65c397c" // 0x10 |
| 4371 | "5e21cc251e47008ed62a542409c8b6b6" // 0x20 |
| 4372 | "ac7f8967b3863ca645fcce49582a9aa1" // 0x30 |
| 4373 | "7349db6c4a95affdae0dae612e1afac9" // 0x40 |
| 4374 | "9ed39a2d934c880440aed8832f984316" // 0x50 |
| 4375 | "3a47f27f392199dc1202f9a0f9bd0830" // 0x60 |
| 4376 | "8007cb1e4e7f58309366a7de25f7c3c9" // 0x70 |
| 4377 | "b880677c068e1be936e81288815252a8" // 0x80 |
| 4378 | "a1" |
| 4379 | "028180" // INTEGER length 0x80 (exponent1) value... |
| 4380 | "57ff8ca1895080b2cae486ef0adfd791" // 0x10 |
| 4381 | "fb0235c0b8b36cd6c136e52e4085f4ea" // 0x20 |
| 4382 | "5a063212a4f105a3764743e53281988a" // 0x30 |
| 4383 | "ba073f6e0027298e1c4378556e0efca0" // 0x40 |
| 4384 | "e14ece1af76ad0b030f27af6f0ab35fb" // 0x50 |
| 4385 | "73a060d8b1a0e142fa2647e93b32e36d" // 0x60 |
| 4386 | "8282ae0a4de50ab7afe85500a16f43a6" // 0x70 |
| 4387 | "4719d6e2b9439823719cd08bcd031781" // 0x80 |
| 4388 | "028181" // INTEGER length 0x81 (exponent2) value... |
| 4389 | "00ba73b0bb28e3f81e9bd1c568713b10" // 0x10 |
| 4390 | "1241acc607976c4ddccc90e65b6556ca" // 0x20 |
| 4391 | "31516058f92b6e09f3b160ff0e374ec4" // 0x30 |
| 4392 | "0d78ae4d4979fde6ac06a1a400c61dd3" // 0x40 |
| 4393 | "1254186af30b22c10582a8a43e34fe94" // 0x50 |
| 4394 | "9c5f3b9755bae7baa7b7b7a6bd03b38c" // 0x60 |
| 4395 | "ef55c86885fc6c1978b9cee7ef33da50" // 0x70 |
| 4396 | "7c9df6b9277cff1e6aaa5d57aca52846" // 0x80 |
| 4397 | "61" |
| 4398 | "028181" // INTEGER length 0x81 (coefficient) value... |
| 4399 | "00c931617c77829dfb1270502be9195c" // 0x10 |
| 4400 | "8f2830885f57dba869536811e6864236" // 0x20 |
| 4401 | "d0c4736a0008a145af36b8357a7c3d13" // 0x30 |
| 4402 | "9966d04c4e00934ea1aede3bb6b8ec84" // 0x40 |
| 4403 | "1dc95e3f579751e2bfdfe27ae778983f" // 0x50 |
| 4404 | "959356210723287b0affcc9f727044d4" // 0x60 |
| 4405 | "8c373f1babde0724fa17a4fd4da0902c" // 0x70 |
| 4406 | "7c9b9bf27ba61be6ad02dfddda8f4e68" // 0x80 |
| 4407 | "22" |
| 4408 | // } SEQUENCE |
| 4409 | // } SEQUENCE () |
| 4410 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4411 | |
| 4412 | string zero_masking_key = |
| 4413 | hex2str("0000000000000000000000000000000000000000000000000000000000000000"); |
| 4414 | string masking_key = hex2str("D796B02C370F1FA4CC0124F14EC8CBEBE987E825246265050F399A51FD477DFC"); |
| 4415 | |
| 4416 | class ImportWrappedKeyTest : public KeyMintAidlTestBase {}; |
| 4417 | |
| 4418 | TEST_P(ImportWrappedKeyTest, Success) { |
| 4419 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4420 | .RsaEncryptionKey(2048, 65537) |
| 4421 | .Digest(Digest::SHA_2_256) |
| 4422 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4423 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4424 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4425 | |
| 4426 | ASSERT_EQ(ErrorCode::OK, |
| 4427 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4428 | AuthorizationSetBuilder() |
| 4429 | .Digest(Digest::SHA_2_256) |
| 4430 | .Padding(PaddingMode::RSA_OAEP))); |
| 4431 | |
| 4432 | string message = "Hello World!"; |
| 4433 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4434 | string ciphertext = EncryptMessage(message, params); |
| 4435 | string plaintext = DecryptMessage(ciphertext, params); |
| 4436 | EXPECT_EQ(message, plaintext); |
| 4437 | } |
| 4438 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4439 | /* |
| 4440 | * ImportWrappedKeyTest.SuccessSidsIgnored |
| 4441 | * |
| 4442 | * Verifies that password_sid and biometric_sid are ignored on import if the authorizations don't |
| 4443 | * include Tag:USER_SECURE_ID. |
| 4444 | */ |
| 4445 | TEST_P(ImportWrappedKeyTest, SuccessSidsIgnored) { |
| 4446 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4447 | .RsaEncryptionKey(2048, 65537) |
| 4448 | .Digest(Digest::SHA_2_256) |
| 4449 | .Padding(PaddingMode::RSA_OAEP) |
| 4450 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4451 | .SetDefaultValidity(); |
| 4452 | |
| 4453 | int64_t password_sid = 42; |
| 4454 | int64_t biometric_sid = 24; |
| 4455 | ASSERT_EQ(ErrorCode::OK, |
| 4456 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4457 | AuthorizationSetBuilder() |
| 4458 | .Digest(Digest::SHA_2_256) |
| 4459 | .Padding(PaddingMode::RSA_OAEP), |
| 4460 | password_sid, biometric_sid)); |
| 4461 | |
| 4462 | string message = "Hello World!"; |
| 4463 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4464 | string ciphertext = EncryptMessage(message, params); |
| 4465 | string plaintext = DecryptMessage(ciphertext, params); |
| 4466 | EXPECT_EQ(message, plaintext); |
| 4467 | } |
| 4468 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4469 | TEST_P(ImportWrappedKeyTest, SuccessMasked) { |
| 4470 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4471 | .RsaEncryptionKey(2048, 65537) |
| 4472 | .Digest(Digest::SHA_2_256) |
| 4473 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4474 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4475 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4476 | |
| 4477 | ASSERT_EQ(ErrorCode::OK, |
| 4478 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, masking_key, |
| 4479 | AuthorizationSetBuilder() |
| 4480 | .Digest(Digest::SHA_2_256) |
| 4481 | .Padding(PaddingMode::RSA_OAEP))); |
| 4482 | } |
| 4483 | |
| 4484 | TEST_P(ImportWrappedKeyTest, WrongMask) { |
| 4485 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4486 | .RsaEncryptionKey(2048, 65537) |
| 4487 | .Digest(Digest::SHA_2_256) |
| 4488 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4489 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4490 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4491 | |
| 4492 | ASSERT_EQ( |
| 4493 | ErrorCode::VERIFICATION_FAILED, |
| 4494 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4495 | AuthorizationSetBuilder() |
| 4496 | .Digest(Digest::SHA_2_256) |
| 4497 | .Padding(PaddingMode::RSA_OAEP))); |
| 4498 | } |
| 4499 | |
| 4500 | TEST_P(ImportWrappedKeyTest, WrongPurpose) { |
| 4501 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4502 | .RsaEncryptionKey(2048, 65537) |
| 4503 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4504 | .Padding(PaddingMode::RSA_OAEP) |
| 4505 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4506 | |
| 4507 | ASSERT_EQ( |
| 4508 | ErrorCode::INCOMPATIBLE_PURPOSE, |
| 4509 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4510 | AuthorizationSetBuilder() |
| 4511 | .Digest(Digest::SHA_2_256) |
| 4512 | .Padding(PaddingMode::RSA_OAEP))); |
| 4513 | } |
| 4514 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4515 | TEST_P(ImportWrappedKeyTest, WrongPaddingMode) { |
| 4516 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4517 | .RsaEncryptionKey(2048, 65537) |
| 4518 | .Digest(Digest::SHA_2_256) |
| 4519 | .Padding(PaddingMode::RSA_PSS) |
| 4520 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4521 | .SetDefaultValidity(); |
| 4522 | |
| 4523 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 4524 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4525 | AuthorizationSetBuilder() |
| 4526 | .Digest(Digest::SHA_2_256) |
| 4527 | .Padding(PaddingMode::RSA_OAEP))); |
| 4528 | } |
| 4529 | |
| 4530 | TEST_P(ImportWrappedKeyTest, WrongDigest) { |
| 4531 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 4532 | .RsaEncryptionKey(2048, 65537) |
| 4533 | .Digest(Digest::SHA_2_512) |
| 4534 | .Padding(PaddingMode::RSA_OAEP) |
| 4535 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 4536 | .SetDefaultValidity(); |
| 4537 | |
| 4538 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 4539 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 4540 | AuthorizationSetBuilder() |
| 4541 | .Digest(Digest::SHA_2_256) |
| 4542 | .Padding(PaddingMode::RSA_OAEP))); |
| 4543 | } |
| 4544 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4545 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportWrappedKeyTest); |
| 4546 | |
| 4547 | typedef KeyMintAidlTestBase EncryptionOperationsTest; |
| 4548 | |
| 4549 | /* |
| 4550 | * EncryptionOperationsTest.RsaNoPaddingSuccess |
| 4551 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4552 | * Verifies that raw RSA decryption works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4553 | */ |
| 4554 | TEST_P(EncryptionOperationsTest, RsaNoPaddingSuccess) { |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4555 | for (uint64_t exponent : {3, 65537}) { |
| 4556 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4557 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4558 | .RsaEncryptionKey(2048, exponent) |
| 4559 | .Padding(PaddingMode::NONE) |
| 4560 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4561 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4562 | string message = string(2048 / 8, 'a'); |
| 4563 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4564 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4565 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4566 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4567 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4568 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4569 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4570 | // Unpadded RSA is deterministic |
| 4571 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 4572 | |
| 4573 | CheckedDeleteKey(); |
| 4574 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | /* |
| 4578 | * EncryptionOperationsTest.RsaNoPaddingShortMessage |
| 4579 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4580 | * Verifies that raw RSA decryption of short messages works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4581 | */ |
| 4582 | TEST_P(EncryptionOperationsTest, RsaNoPaddingShortMessage) { |
| 4583 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4584 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4585 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4586 | .Padding(PaddingMode::NONE) |
| 4587 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4588 | |
| 4589 | string message = "1"; |
| 4590 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 4591 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4592 | string ciphertext = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4593 | EXPECT_EQ(2048U / 8, ciphertext.size()); |
| 4594 | |
| 4595 | string expected_plaintext = string(2048U / 8 - 1, 0) + message; |
| 4596 | string plaintext = DecryptMessage(ciphertext, params); |
| 4597 | |
| 4598 | EXPECT_EQ(expected_plaintext, plaintext); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4602 | * EncryptionOperationsTest.RsaOaepSuccess |
| 4603 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4604 | * Verifies that RSA-OAEP decryption operations work, with all digests. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4605 | */ |
| 4606 | TEST_P(EncryptionOperationsTest, RsaOaepSuccess) { |
| 4607 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 4608 | |
| 4609 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4610 | ASSERT_EQ(ErrorCode::OK, |
| 4611 | GenerateKey(AuthorizationSetBuilder() |
| 4612 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4613 | .RsaEncryptionKey(key_size, 65537) |
| 4614 | .Padding(PaddingMode::RSA_OAEP) |
| 4615 | .Digest(digests) |
| 4616 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1) |
| 4617 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4618 | |
| 4619 | string message = "Hello"; |
| 4620 | |
| 4621 | for (auto digest : digests) { |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4622 | SCOPED_TRACE(testing::Message() << "digest-" << digest); |
| 4623 | |
| 4624 | auto params = AuthorizationSetBuilder() |
| 4625 | .Digest(digest) |
| 4626 | .Padding(PaddingMode::RSA_OAEP) |
| 4627 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1); |
| 4628 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4629 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 4630 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 4631 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4632 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4633 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 4634 | |
| 4635 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 4636 | // probability). |
| 4637 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4638 | |
| 4639 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 4640 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 4641 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 4642 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 4643 | |
| 4644 | // Decrypting corrupted ciphertext should fail. |
| 4645 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4646 | char corrupt_byte; |
| 4647 | do { |
| 4648 | corrupt_byte = static_cast<char>(random() % 256); |
| 4649 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4650 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4651 | |
| 4652 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4653 | string result; |
| 4654 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4655 | EXPECT_EQ(0U, result.size()); |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | /* |
| 4660 | * EncryptionOperationsTest.RsaOaepInvalidDigest |
| 4661 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4662 | * 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] | 4663 | * without a digest. |
| 4664 | */ |
| 4665 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidDigest) { |
| 4666 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4667 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4668 | .RsaEncryptionKey(2048, 65537) |
| 4669 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4670 | .Digest(Digest::NONE) |
| 4671 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4672 | |
| 4673 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_OAEP).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4674 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4678 | * EncryptionOperationsTest.RsaOaepInvalidPadding |
| 4679 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4680 | * 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] | 4681 | * with a padding value that is only suitable for signing/verifying. |
| 4682 | */ |
| 4683 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidPadding) { |
| 4684 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4685 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4686 | .RsaEncryptionKey(2048, 65537) |
| 4687 | .Padding(PaddingMode::RSA_PSS) |
| 4688 | .Digest(Digest::NONE) |
| 4689 | .SetDefaultValidity())); |
| 4690 | |
| 4691 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4692 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, Begin(KeyPurpose::DECRYPT, params)); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4696 | * EncryptionOperationsTest.RsaOaepDecryptWithWrongDigest |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4697 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4698 | * 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] | 4699 | * with a different digest than was used to encrypt. |
| 4700 | */ |
| 4701 | TEST_P(EncryptionOperationsTest, RsaOaepDecryptWithWrongDigest) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 4702 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 4703 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 4704 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4705 | |
| 4706 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4707 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4708 | .RsaEncryptionKey(1024, 65537) |
| 4709 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4710 | .Digest(Digest::SHA_2_224, Digest::SHA_2_256) |
| 4711 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4712 | string message = "Hello World!"; |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4713 | string ciphertext = LocalRsaEncryptMessage( |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4714 | message, |
| 4715 | AuthorizationSetBuilder().Digest(Digest::SHA_2_224).Padding(PaddingMode::RSA_OAEP)); |
| 4716 | |
| 4717 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 4718 | .Digest(Digest::SHA_2_256) |
| 4719 | .Padding(PaddingMode::RSA_OAEP))); |
| 4720 | string result; |
| 4721 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext, &result)); |
| 4722 | EXPECT_EQ(0U, result.size()); |
| 4723 | } |
| 4724 | |
| 4725 | /* |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4726 | * EncryptionOperationsTest.RsaOaepWithMGFDigestSuccess |
| 4727 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4728 | * 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] | 4729 | * digests. |
| 4730 | */ |
| 4731 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFDigestSuccess) { |
| 4732 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 4733 | |
| 4734 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
| 4735 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4736 | .OaepMGFDigest(digests) |
| 4737 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4738 | .RsaEncryptionKey(key_size, 65537) |
| 4739 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4740 | .Digest(Digest::SHA_2_256) |
| 4741 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4742 | |
| 4743 | string message = "Hello"; |
| 4744 | |
| 4745 | for (auto digest : digests) { |
| 4746 | auto params = AuthorizationSetBuilder() |
| 4747 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, digest) |
| 4748 | .Digest(Digest::SHA_2_256) |
| 4749 | .Padding(PaddingMode::RSA_OAEP); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4750 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4751 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 4752 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 4753 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4754 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4755 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 4756 | |
| 4757 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 4758 | // probability). |
| 4759 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4760 | |
| 4761 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 4762 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 4763 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 4764 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 4765 | |
| 4766 | // Decrypting corrupted ciphertext should fail. |
| 4767 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4768 | char corrupt_byte; |
| 4769 | do { |
| 4770 | corrupt_byte = static_cast<char>(random() % 256); |
| 4771 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4772 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4773 | |
| 4774 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4775 | string result; |
| 4776 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4777 | EXPECT_EQ(0U, result.size()); |
| 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | /* |
| 4782 | * EncryptionOperationsTest.RsaOaepWithMGFIncompatibleDigest |
| 4783 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4784 | * 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] | 4785 | * with incompatible MGF digest. |
| 4786 | */ |
| 4787 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFIncompatibleDigest) { |
| 4788 | ASSERT_EQ(ErrorCode::OK, |
| 4789 | GenerateKey(AuthorizationSetBuilder() |
| 4790 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 4791 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4792 | .RsaEncryptionKey(2048, 65537) |
| 4793 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4794 | .Digest(Digest::SHA_2_256) |
| 4795 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4796 | string message = "Hello World!"; |
| 4797 | |
| 4798 | auto params = AuthorizationSetBuilder() |
| 4799 | .Padding(PaddingMode::RSA_OAEP) |
| 4800 | .Digest(Digest::SHA_2_256) |
| 4801 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_224); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4802 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /* |
| 4806 | * EncryptionOperationsTest.RsaOaepWithMGFUnsupportedDigest |
| 4807 | * |
| 4808 | * Verifies that RSA-OAEP encryption operations fail in the correct way when asked to operate |
| 4809 | * with unsupported MGF digest. |
| 4810 | */ |
| 4811 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFUnsupportedDigest) { |
| 4812 | ASSERT_EQ(ErrorCode::OK, |
| 4813 | GenerateKey(AuthorizationSetBuilder() |
| 4814 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 4815 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4816 | .RsaEncryptionKey(2048, 65537) |
| 4817 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4818 | .Digest(Digest::SHA_2_256) |
| 4819 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4820 | string message = "Hello World!"; |
| 4821 | |
| 4822 | auto params = AuthorizationSetBuilder() |
| 4823 | .Padding(PaddingMode::RSA_OAEP) |
| 4824 | .Digest(Digest::SHA_2_256) |
| 4825 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4826 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4830 | * EncryptionOperationsTest.RsaPkcs1Success |
| 4831 | * |
| 4832 | * Verifies that RSA PKCS encryption/decrypts works. |
| 4833 | */ |
| 4834 | TEST_P(EncryptionOperationsTest, RsaPkcs1Success) { |
| 4835 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4836 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4837 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4838 | .Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT) |
| 4839 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4840 | |
| 4841 | string message = "Hello World!"; |
| 4842 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4843 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4844 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
| 4845 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4846 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4847 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
| 4848 | |
| 4849 | // PKCS1 v1.5 randomizes padding so every result should be different. |
| 4850 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4851 | |
| 4852 | string plaintext = DecryptMessage(ciphertext1, params); |
| 4853 | EXPECT_EQ(message, plaintext); |
| 4854 | |
| 4855 | // Decrypting corrupted ciphertext should fail. |
| 4856 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4857 | char corrupt_byte; |
| 4858 | do { |
| 4859 | corrupt_byte = static_cast<char>(random() % 256); |
| 4860 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4861 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4862 | |
| 4863 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4864 | string result; |
| 4865 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4866 | EXPECT_EQ(0U, result.size()); |
| 4867 | } |
| 4868 | |
| 4869 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4870 | * EncryptionOperationsTest.EcdsaEncrypt |
| 4871 | * |
| 4872 | * Verifies that attempting to use ECDSA keys to encrypt fails in the correct way. |
| 4873 | */ |
| 4874 | TEST_P(EncryptionOperationsTest, EcdsaEncrypt) { |
| 4875 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4876 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 4877 | .EcdsaSigningKey(EcCurve::P_256) |
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 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 4881 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4882 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * EncryptionOperationsTest.HmacEncrypt |
| 4887 | * |
| 4888 | * Verifies that attempting to use HMAC keys to encrypt fails in the correct way. |
| 4889 | */ |
| 4890 | TEST_P(EncryptionOperationsTest, HmacEncrypt) { |
| 4891 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4892 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4893 | .HmacKey(128) |
| 4894 | .Digest(Digest::SHA_2_256) |
| 4895 | .Padding(PaddingMode::NONE) |
| 4896 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4897 | auto params = AuthorizationSetBuilder() |
| 4898 | .Digest(Digest::SHA_2_256) |
| 4899 | .Padding(PaddingMode::NONE) |
| 4900 | .Authorization(TAG_MAC_LENGTH, 128); |
| 4901 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4902 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 4903 | } |
| 4904 | |
| 4905 | /* |
| 4906 | * EncryptionOperationsTest.AesEcbRoundTripSuccess |
| 4907 | * |
| 4908 | * Verifies that AES ECB mode works. |
| 4909 | */ |
| 4910 | TEST_P(EncryptionOperationsTest, AesEcbRoundTripSuccess) { |
| 4911 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4912 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4913 | .AesEncryptionKey(128) |
| 4914 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4915 | .Padding(PaddingMode::NONE))); |
| 4916 | |
| 4917 | ASSERT_GT(key_blob_.size(), 0U); |
| 4918 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 4919 | |
| 4920 | // Two-block message. |
| 4921 | string message = "12345678901234567890123456789012"; |
| 4922 | string ciphertext1 = EncryptMessage(message, params); |
| 4923 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 4924 | |
| 4925 | string ciphertext2 = EncryptMessage(string(message), params); |
| 4926 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 4927 | |
| 4928 | // ECB is deterministic. |
| 4929 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 4930 | |
| 4931 | string plaintext = DecryptMessage(ciphertext1, params); |
| 4932 | EXPECT_EQ(message, plaintext); |
| 4933 | } |
| 4934 | |
| 4935 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4936 | * EncryptionOperationsTest.AesEcbUnknownTag |
| 4937 | * |
| 4938 | * Verifies that AES ECB operations ignore unknown tags. |
| 4939 | */ |
| 4940 | TEST_P(EncryptionOperationsTest, AesEcbUnknownTag) { |
| 4941 | int32_t unknown_tag_value = ((7 << 28) /* TagType:BOOL */ | 150); |
| 4942 | Tag unknown_tag = static_cast<Tag>(unknown_tag_value); |
| 4943 | KeyParameter unknown_param; |
| 4944 | unknown_param.tag = unknown_tag; |
| 4945 | |
| 4946 | vector<KeyCharacteristics> key_characteristics; |
| 4947 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4948 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4949 | .AesEncryptionKey(128) |
| 4950 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4951 | .Padding(PaddingMode::NONE) |
| 4952 | .Authorization(unknown_param), |
| 4953 | &key_blob_, &key_characteristics)); |
| 4954 | ASSERT_GT(key_blob_.size(), 0U); |
| 4955 | |
| 4956 | // Unknown tags should not be returned in key characteristics. |
| 4957 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 4958 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 4959 | EXPECT_EQ(hw_enforced.find(unknown_tag), -1); |
| 4960 | EXPECT_EQ(sw_enforced.find(unknown_tag), -1); |
| 4961 | |
| 4962 | // Encrypt without mentioning the unknown parameter. |
| 4963 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 4964 | string message = "12345678901234567890123456789012"; |
| 4965 | string ciphertext = EncryptMessage(message, params); |
| 4966 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 4967 | |
| 4968 | // Decrypt including the unknown parameter. |
| 4969 | auto decrypt_params = AuthorizationSetBuilder() |
| 4970 | .BlockMode(BlockMode::ECB) |
| 4971 | .Padding(PaddingMode::NONE) |
| 4972 | .Authorization(unknown_param); |
| 4973 | string plaintext = DecryptMessage(ciphertext, decrypt_params); |
| 4974 | EXPECT_EQ(message, plaintext); |
| 4975 | } |
| 4976 | |
| 4977 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4978 | * EncryptionOperationsTest.AesWrongMode |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4979 | * |
| 4980 | * Verifies that AES encryption fails in the correct way when an unauthorized mode is specified. |
| 4981 | */ |
| 4982 | TEST_P(EncryptionOperationsTest, AesWrongMode) { |
| 4983 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4984 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4985 | .AesEncryptionKey(128) |
| 4986 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4987 | .Padding(PaddingMode::NONE))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4988 | ASSERT_GT(key_blob_.size(), 0U); |
| 4989 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4990 | EXPECT_EQ( |
| 4991 | ErrorCode::INCOMPATIBLE_BLOCK_MODE, |
| 4992 | Begin(KeyPurpose::ENCRYPT, |
| 4993 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE))); |
| 4994 | } |
| 4995 | |
| 4996 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4997 | * EncryptionOperationsTest.AesWrongPadding |
| 4998 | * |
| 4999 | * Verifies that AES encryption fails in the correct way when an unauthorized padding is specified. |
| 5000 | */ |
| 5001 | TEST_P(EncryptionOperationsTest, AesWrongPadding) { |
| 5002 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5003 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5004 | .AesEncryptionKey(128) |
| 5005 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5006 | .Padding(PaddingMode::NONE))); |
| 5007 | ASSERT_GT(key_blob_.size(), 0U); |
| 5008 | |
| 5009 | EXPECT_EQ( |
| 5010 | ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 5011 | Begin(KeyPurpose::ENCRYPT, |
| 5012 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7))); |
| 5013 | } |
| 5014 | |
| 5015 | /* |
| 5016 | * EncryptionOperationsTest.AesInvalidParams |
| 5017 | * |
| 5018 | * Verifies that AES encryption fails in the correct way when an duplicate parameters are specified. |
| 5019 | */ |
| 5020 | TEST_P(EncryptionOperationsTest, AesInvalidParams) { |
| 5021 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5022 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5023 | .AesEncryptionKey(128) |
| 5024 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5025 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5026 | .Padding(PaddingMode::NONE) |
| 5027 | .Padding(PaddingMode::PKCS7))); |
| 5028 | ASSERT_GT(key_blob_.size(), 0U); |
| 5029 | |
| 5030 | auto result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5031 | .BlockMode(BlockMode::CBC) |
| 5032 | .BlockMode(BlockMode::ECB) |
| 5033 | .Padding(PaddingMode::NONE)); |
| 5034 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_BLOCK_MODE || |
| 5035 | result == ErrorCode::UNSUPPORTED_BLOCK_MODE); |
| 5036 | |
| 5037 | result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5038 | .BlockMode(BlockMode::ECB) |
| 5039 | .Padding(PaddingMode::NONE) |
| 5040 | .Padding(PaddingMode::PKCS7)); |
| 5041 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
| 5042 | result == ErrorCode::UNSUPPORTED_PADDING_MODE); |
| 5043 | } |
| 5044 | |
| 5045 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5046 | * EncryptionOperationsTest.AesWrongPurpose |
| 5047 | * |
| 5048 | * Verifies that AES encryption fails in the correct way when an unauthorized purpose is |
| 5049 | * specified. |
| 5050 | */ |
| 5051 | TEST_P(EncryptionOperationsTest, AesWrongPurpose) { |
| 5052 | auto err = GenerateKey(AuthorizationSetBuilder() |
| 5053 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5054 | .AesKey(128) |
| 5055 | .Authorization(TAG_PURPOSE, KeyPurpose::ENCRYPT) |
| 5056 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5057 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5058 | .Padding(PaddingMode::NONE)); |
| 5059 | ASSERT_EQ(ErrorCode::OK, err) << "Got " << err; |
| 5060 | ASSERT_GT(key_blob_.size(), 0U); |
| 5061 | |
| 5062 | err = Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 5063 | .BlockMode(BlockMode::GCM) |
| 5064 | .Padding(PaddingMode::NONE) |
| 5065 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 5066 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 5067 | |
| 5068 | CheckedDeleteKey(); |
| 5069 | |
| 5070 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5071 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5072 | .AesKey(128) |
| 5073 | .Authorization(TAG_PURPOSE, KeyPurpose::DECRYPT) |
| 5074 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5075 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5076 | .Padding(PaddingMode::NONE))); |
| 5077 | |
| 5078 | err = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 5079 | .BlockMode(BlockMode::GCM) |
| 5080 | .Padding(PaddingMode::NONE) |
| 5081 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 5082 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 5083 | } |
| 5084 | |
| 5085 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5086 | * EncryptionOperationsTest.AesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5087 | * |
| 5088 | * Verifies that AES encryption fails in the correct way when provided an input that is not a |
| 5089 | * multiple of the block size and no padding is specified. |
| 5090 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5091 | TEST_P(EncryptionOperationsTest, AesEcbCbcNoPaddingWrongInputSize) { |
| 5092 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 5093 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5094 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5095 | .AesEncryptionKey(128) |
| 5096 | .Authorization(TAG_BLOCK_MODE, blockMode) |
| 5097 | .Padding(PaddingMode::NONE))); |
| 5098 | // Message is slightly shorter than two blocks. |
| 5099 | string message(16 * 2 - 1, 'a'); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5100 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5101 | auto params = AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 5102 | AuthorizationSet out_params; |
| 5103 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 5104 | string ciphertext; |
| 5105 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &ciphertext)); |
| 5106 | EXPECT_EQ(0U, ciphertext.size()); |
| 5107 | |
| 5108 | CheckedDeleteKey(); |
| 5109 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | /* |
| 5113 | * EncryptionOperationsTest.AesEcbPkcs7Padding |
| 5114 | * |
| 5115 | * Verifies that AES PKCS7 padding works for any message length. |
| 5116 | */ |
| 5117 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7Padding) { |
| 5118 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5119 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5120 | .AesEncryptionKey(128) |
| 5121 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5122 | .Padding(PaddingMode::PKCS7))); |
| 5123 | |
| 5124 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5125 | |
| 5126 | // Try various message lengths; all should work. |
| 5127 | for (size_t i = 0; i < 32; ++i) { |
| 5128 | string message(i, 'a'); |
| 5129 | string ciphertext = EncryptMessage(message, params); |
| 5130 | EXPECT_EQ(i + 16 - (i % 16), ciphertext.size()); |
| 5131 | string plaintext = DecryptMessage(ciphertext, params); |
| 5132 | EXPECT_EQ(message, plaintext); |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | /* |
| 5137 | * EncryptionOperationsTest.AesEcbWrongPadding |
| 5138 | * |
| 5139 | * Verifies that AES enryption fails in the correct way when an unauthorized padding mode is |
| 5140 | * specified. |
| 5141 | */ |
| 5142 | TEST_P(EncryptionOperationsTest, AesEcbWrongPadding) { |
| 5143 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5144 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5145 | .AesEncryptionKey(128) |
| 5146 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5147 | .Padding(PaddingMode::NONE))); |
| 5148 | |
| 5149 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5150 | |
| 5151 | // Try various message lengths; all should fail |
| 5152 | for (size_t i = 0; i < 32; ++i) { |
| 5153 | string message(i, 'a'); |
| 5154 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | /* |
| 5159 | * EncryptionOperationsTest.AesEcbPkcs7PaddingCorrupted |
| 5160 | * |
| 5161 | * Verifies that AES decryption fails in the correct way when the padding is corrupted. |
| 5162 | */ |
| 5163 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7PaddingCorrupted) { |
| 5164 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5165 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5166 | .AesEncryptionKey(128) |
| 5167 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 5168 | .Padding(PaddingMode::PKCS7))); |
| 5169 | |
| 5170 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5171 | |
| 5172 | string message = "a"; |
| 5173 | string ciphertext = EncryptMessage(message, params); |
| 5174 | EXPECT_EQ(16U, ciphertext.size()); |
| 5175 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5176 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 5177 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 5178 | ++ciphertext[ciphertext.size() / 2]; |
| 5179 | |
| 5180 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5181 | string plaintext; |
| 5182 | ErrorCode error = Finish(message, &plaintext); |
| 5183 | if (error == ErrorCode::INVALID_INPUT_LENGTH) { |
| 5184 | // This is the expected error, we can exit the test now. |
| 5185 | return; |
| 5186 | } else { |
| 5187 | // Very small chance we got valid decryption, so try again. |
| 5188 | ASSERT_EQ(error, ErrorCode::OK); |
| 5189 | } |
| 5190 | } |
| 5191 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | vector<uint8_t> CopyIv(const AuthorizationSet& set) { |
| 5195 | auto iv = set.GetTagValue(TAG_NONCE); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5196 | EXPECT_TRUE(iv); |
| 5197 | return iv->get(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | /* |
| 5201 | * EncryptionOperationsTest.AesCtrRoundTripSuccess |
| 5202 | * |
| 5203 | * Verifies that AES CTR mode works. |
| 5204 | */ |
| 5205 | TEST_P(EncryptionOperationsTest, AesCtrRoundTripSuccess) { |
| 5206 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5207 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5208 | .AesEncryptionKey(128) |
| 5209 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5210 | .Padding(PaddingMode::NONE))); |
| 5211 | |
| 5212 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 5213 | |
| 5214 | string message = "123"; |
| 5215 | AuthorizationSet out_params; |
| 5216 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 5217 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 5218 | EXPECT_EQ(16U, iv1.size()); |
| 5219 | |
| 5220 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5221 | |
| 5222 | out_params.Clear(); |
| 5223 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 5224 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 5225 | EXPECT_EQ(16U, iv2.size()); |
| 5226 | |
| 5227 | // IVs should be random, so ciphertexts should differ. |
| 5228 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5229 | |
| 5230 | auto params_iv1 = |
| 5231 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv1); |
| 5232 | auto params_iv2 = |
| 5233 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv2); |
| 5234 | |
| 5235 | string plaintext = DecryptMessage(ciphertext1, params_iv1); |
| 5236 | EXPECT_EQ(message, plaintext); |
| 5237 | plaintext = DecryptMessage(ciphertext2, params_iv2); |
| 5238 | EXPECT_EQ(message, plaintext); |
| 5239 | |
| 5240 | // Using the wrong IV will result in a "valid" decryption, but the data will be garbage. |
| 5241 | plaintext = DecryptMessage(ciphertext1, params_iv2); |
| 5242 | EXPECT_NE(message, plaintext); |
| 5243 | plaintext = DecryptMessage(ciphertext2, params_iv1); |
| 5244 | EXPECT_NE(message, plaintext); |
| 5245 | } |
| 5246 | |
| 5247 | /* |
| 5248 | * EncryptionOperationsTest.AesIncremental |
| 5249 | * |
| 5250 | * Verifies that AES works, all modes, when provided data in various size increments. |
| 5251 | */ |
| 5252 | TEST_P(EncryptionOperationsTest, AesIncremental) { |
| 5253 | auto block_modes = { |
| 5254 | BlockMode::ECB, |
| 5255 | BlockMode::CBC, |
| 5256 | BlockMode::CTR, |
| 5257 | BlockMode::GCM, |
| 5258 | }; |
| 5259 | |
| 5260 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5261 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5262 | .AesEncryptionKey(128) |
| 5263 | .BlockMode(block_modes) |
| 5264 | .Padding(PaddingMode::NONE) |
| 5265 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5266 | |
| 5267 | for (int increment = 1; increment <= 240; ++increment) { |
| 5268 | for (auto block_mode : block_modes) { |
| 5269 | string message(240, 'a'); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5270 | auto params = |
| 5271 | AuthorizationSetBuilder().BlockMode(block_mode).Padding(PaddingMode::NONE); |
| 5272 | if (block_mode == BlockMode::GCM) { |
| 5273 | params.Authorization(TAG_MAC_LENGTH, 128) /* for GCM */; |
| 5274 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5275 | |
| 5276 | AuthorizationSet output_params; |
| 5277 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &output_params)); |
| 5278 | |
| 5279 | string ciphertext; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5280 | string to_send; |
| 5281 | for (size_t i = 0; i < message.size(); i += increment) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5282 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5283 | } |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5284 | EXPECT_EQ(ErrorCode::OK, Finish(to_send, &ciphertext)) |
| 5285 | << "Error sending " << to_send << " with block mode " << block_mode; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5286 | |
| 5287 | switch (block_mode) { |
| 5288 | case BlockMode::GCM: |
| 5289 | EXPECT_EQ(message.size() + 16, ciphertext.size()); |
| 5290 | break; |
| 5291 | case BlockMode::CTR: |
| 5292 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 5293 | break; |
| 5294 | case BlockMode::CBC: |
| 5295 | case BlockMode::ECB: |
| 5296 | EXPECT_EQ(message.size() + message.size() % 16, ciphertext.size()); |
| 5297 | break; |
| 5298 | } |
| 5299 | |
| 5300 | auto iv = output_params.GetTagValue(TAG_NONCE); |
| 5301 | switch (block_mode) { |
| 5302 | case BlockMode::CBC: |
| 5303 | case BlockMode::GCM: |
| 5304 | case BlockMode::CTR: |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5305 | ASSERT_TRUE(iv) << "No IV for block mode " << block_mode; |
| 5306 | EXPECT_EQ(block_mode == BlockMode::GCM ? 12U : 16U, iv->get().size()); |
| 5307 | params.push_back(TAG_NONCE, iv->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5308 | break; |
| 5309 | |
| 5310 | case BlockMode::ECB: |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5311 | EXPECT_FALSE(iv) << "ECB mode should not generate IV"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5312 | break; |
| 5313 | } |
| 5314 | |
| 5315 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)) |
| 5316 | << "Decrypt begin() failed for block mode " << block_mode; |
| 5317 | |
| 5318 | string plaintext; |
| 5319 | for (size_t i = 0; i < ciphertext.size(); i += increment) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5320 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5321 | } |
| 5322 | ErrorCode error = Finish(to_send, &plaintext); |
| 5323 | ASSERT_EQ(ErrorCode::OK, error) << "Decryption failed for block mode " << block_mode |
| 5324 | << " and increment " << increment; |
| 5325 | if (error == ErrorCode::OK) { |
| 5326 | ASSERT_EQ(message, plaintext) << "Decryption didn't match for block mode " |
| 5327 | << block_mode << " and increment " << increment; |
| 5328 | } |
| 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | |
| 5333 | struct AesCtrSp80038aTestVector { |
| 5334 | const char* key; |
| 5335 | const char* nonce; |
| 5336 | const char* plaintext; |
| 5337 | const char* ciphertext; |
| 5338 | }; |
| 5339 | |
| 5340 | // These test vectors are taken from |
| 5341 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section F.5. |
| 5342 | static const AesCtrSp80038aTestVector kAesCtrSp80038aTestVectors[] = { |
| 5343 | // AES-128 |
| 5344 | { |
| 5345 | "2b7e151628aed2a6abf7158809cf4f3c", |
| 5346 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5347 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5348 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5349 | "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff" |
| 5350 | "5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee", |
| 5351 | }, |
| 5352 | // AES-192 |
| 5353 | { |
| 5354 | "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", |
| 5355 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5356 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5357 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5358 | "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94" |
| 5359 | "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050", |
| 5360 | }, |
| 5361 | // AES-256 |
| 5362 | { |
| 5363 | "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", |
| 5364 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 5365 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 5366 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 5367 | "601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5" |
| 5368 | "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6", |
| 5369 | }, |
| 5370 | }; |
| 5371 | |
| 5372 | /* |
| 5373 | * EncryptionOperationsTest.AesCtrSp80038aTestVector |
| 5374 | * |
| 5375 | * Verifies AES CTR implementation against SP800-38A test vectors. |
| 5376 | */ |
| 5377 | TEST_P(EncryptionOperationsTest, AesCtrSp80038aTestVector) { |
| 5378 | std::vector<uint32_t> InvalidSizes = InvalidKeySizes(Algorithm::AES); |
| 5379 | for (size_t i = 0; i < 3; i++) { |
| 5380 | const AesCtrSp80038aTestVector& test(kAesCtrSp80038aTestVectors[i]); |
| 5381 | const string key = hex2str(test.key); |
| 5382 | if (std::find(InvalidSizes.begin(), InvalidSizes.end(), (key.size() * 8)) != |
| 5383 | InvalidSizes.end()) |
| 5384 | continue; |
| 5385 | const string nonce = hex2str(test.nonce); |
| 5386 | const string plaintext = hex2str(test.plaintext); |
| 5387 | const string ciphertext = hex2str(test.ciphertext); |
| 5388 | CheckAesCtrTestVector(key, nonce, plaintext, ciphertext); |
| 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | /* |
| 5393 | * EncryptionOperationsTest.AesCtrIncompatiblePaddingMode |
| 5394 | * |
| 5395 | * Verifies that keymint rejects use of CTR mode with PKCS7 padding in the correct way. |
| 5396 | */ |
| 5397 | TEST_P(EncryptionOperationsTest, AesCtrIncompatiblePaddingMode) { |
| 5398 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5399 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5400 | .AesEncryptionKey(128) |
| 5401 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5402 | .Padding(PaddingMode::PKCS7))); |
| 5403 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 5404 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5405 | } |
| 5406 | |
| 5407 | /* |
| 5408 | * EncryptionOperationsTest.AesCtrInvalidCallerNonce |
| 5409 | * |
| 5410 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 5411 | */ |
| 5412 | TEST_P(EncryptionOperationsTest, AesCtrInvalidCallerNonce) { |
| 5413 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5414 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5415 | .AesEncryptionKey(128) |
| 5416 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 5417 | .Authorization(TAG_CALLER_NONCE) |
| 5418 | .Padding(PaddingMode::NONE))); |
| 5419 | |
| 5420 | auto params = AuthorizationSetBuilder() |
| 5421 | .BlockMode(BlockMode::CTR) |
| 5422 | .Padding(PaddingMode::NONE) |
| 5423 | .Authorization(TAG_NONCE, AidlBuf(string(1, 'a'))); |
| 5424 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5425 | |
| 5426 | params = AuthorizationSetBuilder() |
| 5427 | .BlockMode(BlockMode::CTR) |
| 5428 | .Padding(PaddingMode::NONE) |
| 5429 | .Authorization(TAG_NONCE, AidlBuf(string(15, 'a'))); |
| 5430 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5431 | |
| 5432 | params = AuthorizationSetBuilder() |
| 5433 | .BlockMode(BlockMode::CTR) |
| 5434 | .Padding(PaddingMode::NONE) |
| 5435 | .Authorization(TAG_NONCE, AidlBuf(string(17, 'a'))); |
| 5436 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5437 | } |
| 5438 | |
| 5439 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 5440 | * EncryptionOperationsTest.AesCbcRoundTripSuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5441 | * |
| 5442 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 5443 | */ |
| 5444 | TEST_P(EncryptionOperationsTest, AesCbcRoundTripSuccess) { |
| 5445 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5446 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5447 | .AesEncryptionKey(128) |
| 5448 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5449 | .Padding(PaddingMode::NONE))); |
| 5450 | // Two-block message. |
| 5451 | string message = "12345678901234567890123456789012"; |
| 5452 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5453 | AuthorizationSet out_params; |
| 5454 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 5455 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 5456 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5457 | |
| 5458 | out_params.Clear(); |
| 5459 | |
| 5460 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 5461 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 5462 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 5463 | |
| 5464 | // IVs should be random, so ciphertexts should differ. |
| 5465 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5466 | |
| 5467 | params.push_back(TAG_NONCE, iv1); |
| 5468 | string plaintext = DecryptMessage(ciphertext1, params); |
| 5469 | EXPECT_EQ(message, plaintext); |
| 5470 | } |
| 5471 | |
| 5472 | /* |
| 5473 | * EncryptionOperationsTest.AesCallerNonce |
| 5474 | * |
| 5475 | * Verifies that AES caller-provided nonces work correctly. |
| 5476 | */ |
| 5477 | TEST_P(EncryptionOperationsTest, AesCallerNonce) { |
| 5478 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5479 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5480 | .AesEncryptionKey(128) |
| 5481 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5482 | .Authorization(TAG_CALLER_NONCE) |
| 5483 | .Padding(PaddingMode::NONE))); |
| 5484 | |
| 5485 | string message = "12345678901234567890123456789012"; |
| 5486 | |
| 5487 | // Don't specify nonce, should get a random one. |
| 5488 | AuthorizationSetBuilder params = |
| 5489 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5490 | AuthorizationSet out_params; |
| 5491 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 5492 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5493 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5494 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5495 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5496 | string plaintext = DecryptMessage(ciphertext, params); |
| 5497 | EXPECT_EQ(message, plaintext); |
| 5498 | |
| 5499 | // Now specify a nonce, should also work. |
| 5500 | params = AuthorizationSetBuilder() |
| 5501 | .BlockMode(BlockMode::CBC) |
| 5502 | .Padding(PaddingMode::NONE) |
| 5503 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 5504 | out_params.Clear(); |
| 5505 | ciphertext = EncryptMessage(message, params, &out_params); |
| 5506 | |
| 5507 | // Decrypt with correct nonce. |
| 5508 | plaintext = DecryptMessage(ciphertext, params); |
| 5509 | EXPECT_EQ(message, plaintext); |
| 5510 | |
| 5511 | // Try with wrong nonce. |
| 5512 | params = AuthorizationSetBuilder() |
| 5513 | .BlockMode(BlockMode::CBC) |
| 5514 | .Padding(PaddingMode::NONE) |
| 5515 | .Authorization(TAG_NONCE, AidlBuf("aaaaaaaaaaaaaaaa")); |
| 5516 | plaintext = DecryptMessage(ciphertext, params); |
| 5517 | EXPECT_NE(message, plaintext); |
| 5518 | } |
| 5519 | |
| 5520 | /* |
| 5521 | * EncryptionOperationsTest.AesCallerNonceProhibited |
| 5522 | * |
| 5523 | * Verifies that caller-provided nonces are not permitted when not specified in the key |
| 5524 | * authorizations. |
| 5525 | */ |
| 5526 | TEST_P(EncryptionOperationsTest, AesCallerNonceProhibited) { |
| 5527 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5528 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5529 | .AesEncryptionKey(128) |
| 5530 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 5531 | .Padding(PaddingMode::NONE))); |
| 5532 | |
| 5533 | string message = "12345678901234567890123456789012"; |
| 5534 | |
| 5535 | // Don't specify nonce, should get a random one. |
| 5536 | AuthorizationSetBuilder params = |
| 5537 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5538 | AuthorizationSet out_params; |
| 5539 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 5540 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5541 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5542 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5543 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5544 | string plaintext = DecryptMessage(ciphertext, params); |
| 5545 | EXPECT_EQ(message, plaintext); |
| 5546 | |
| 5547 | // Now specify a nonce, should fail |
| 5548 | params = AuthorizationSetBuilder() |
| 5549 | .BlockMode(BlockMode::CBC) |
| 5550 | .Padding(PaddingMode::NONE) |
| 5551 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 5552 | out_params.Clear(); |
| 5553 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 5554 | } |
| 5555 | |
| 5556 | /* |
| 5557 | * EncryptionOperationsTest.AesGcmRoundTripSuccess |
| 5558 | * |
| 5559 | * Verifies that AES GCM mode works. |
| 5560 | */ |
| 5561 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripSuccess) { |
| 5562 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5563 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5564 | .AesEncryptionKey(128) |
| 5565 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5566 | .Padding(PaddingMode::NONE) |
| 5567 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5568 | |
| 5569 | string aad = "foobar"; |
| 5570 | string message = "123456789012345678901234567890123456"; |
| 5571 | |
| 5572 | auto begin_params = AuthorizationSetBuilder() |
| 5573 | .BlockMode(BlockMode::GCM) |
| 5574 | .Padding(PaddingMode::NONE) |
| 5575 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5576 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5577 | // Encrypt |
| 5578 | AuthorizationSet begin_out_params; |
| 5579 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 5580 | << "Begin encrypt"; |
| 5581 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5582 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5583 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5584 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 5585 | |
| 5586 | // Grab nonce |
| 5587 | begin_params.push_back(begin_out_params); |
| 5588 | |
| 5589 | // Decrypt. |
| 5590 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5591 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5592 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5593 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5594 | EXPECT_EQ(message.length(), plaintext.length()); |
| 5595 | EXPECT_EQ(message, plaintext); |
| 5596 | } |
| 5597 | |
| 5598 | /* |
| 5599 | * EncryptionOperationsTest.AesGcmRoundTripWithDelaySuccess |
| 5600 | * |
| 5601 | * Verifies that AES GCM mode works, even when there's a long delay |
| 5602 | * between operations. |
| 5603 | */ |
| 5604 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripWithDelaySuccess) { |
| 5605 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5606 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5607 | .AesEncryptionKey(128) |
| 5608 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5609 | .Padding(PaddingMode::NONE) |
| 5610 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5611 | |
| 5612 | string aad = "foobar"; |
| 5613 | string message = "123456789012345678901234567890123456"; |
| 5614 | |
| 5615 | auto begin_params = AuthorizationSetBuilder() |
| 5616 | .BlockMode(BlockMode::GCM) |
| 5617 | .Padding(PaddingMode::NONE) |
| 5618 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5619 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5620 | // Encrypt |
| 5621 | AuthorizationSet begin_out_params; |
| 5622 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 5623 | << "Begin encrypt"; |
| 5624 | string ciphertext; |
| 5625 | AuthorizationSet update_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5626 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5627 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5628 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5629 | |
| 5630 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 5631 | |
| 5632 | // Grab nonce |
| 5633 | begin_params.push_back(begin_out_params); |
| 5634 | |
| 5635 | // Decrypt. |
| 5636 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
| 5637 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5638 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5639 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5640 | ASSERT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5641 | sleep(5); |
| 5642 | EXPECT_EQ(ErrorCode::OK, Finish("", &plaintext)); |
| 5643 | EXPECT_EQ(message.length(), plaintext.length()); |
| 5644 | EXPECT_EQ(message, plaintext); |
| 5645 | } |
| 5646 | |
| 5647 | /* |
| 5648 | * EncryptionOperationsTest.AesGcmDifferentNonces |
| 5649 | * |
| 5650 | * Verifies that encrypting the same data with different nonces produces different outputs. |
| 5651 | */ |
| 5652 | TEST_P(EncryptionOperationsTest, AesGcmDifferentNonces) { |
| 5653 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5654 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5655 | .AesEncryptionKey(128) |
| 5656 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5657 | .Padding(PaddingMode::NONE) |
| 5658 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 5659 | .Authorization(TAG_CALLER_NONCE))); |
| 5660 | |
| 5661 | string aad = "foobar"; |
| 5662 | string message = "123456789012345678901234567890123456"; |
| 5663 | string nonce1 = "000000000000"; |
| 5664 | string nonce2 = "111111111111"; |
| 5665 | string nonce3 = "222222222222"; |
| 5666 | |
| 5667 | string ciphertext1 = |
| 5668 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce1)); |
| 5669 | string ciphertext2 = |
| 5670 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce2)); |
| 5671 | string ciphertext3 = |
| 5672 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce3)); |
| 5673 | |
| 5674 | ASSERT_NE(ciphertext1, ciphertext2); |
| 5675 | ASSERT_NE(ciphertext1, ciphertext3); |
| 5676 | ASSERT_NE(ciphertext2, ciphertext3); |
| 5677 | } |
| 5678 | |
| 5679 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5680 | * EncryptionOperationsTest.AesGcmDifferentAutoNonces |
| 5681 | * |
| 5682 | * Verifies that encrypting the same data with KeyMint generated nonces produces different outputs. |
| 5683 | */ |
| 5684 | TEST_P(EncryptionOperationsTest, AesGcmDifferentAutoNonces) { |
| 5685 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5686 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5687 | .AesEncryptionKey(128) |
| 5688 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 5689 | .Padding(PaddingMode::NONE) |
| 5690 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5691 | |
| 5692 | string aad = "foobar"; |
| 5693 | string message = "123456789012345678901234567890123456"; |
| 5694 | |
| 5695 | string ciphertext1 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5696 | string ciphertext2 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5697 | string ciphertext3 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 5698 | |
| 5699 | ASSERT_NE(ciphertext1, ciphertext2); |
| 5700 | ASSERT_NE(ciphertext1, ciphertext3); |
| 5701 | ASSERT_NE(ciphertext2, ciphertext3); |
| 5702 | } |
| 5703 | |
| 5704 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5705 | * EncryptionOperationsTest.AesGcmTooShortTag |
| 5706 | * |
| 5707 | * Verifies that AES GCM mode fails correctly when a too-short tag length is specified. |
| 5708 | */ |
| 5709 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTag) { |
| 5710 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5711 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5712 | .AesEncryptionKey(128) |
| 5713 | .BlockMode(BlockMode::GCM) |
| 5714 | .Padding(PaddingMode::NONE) |
| 5715 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5716 | string message = "123456789012345678901234567890123456"; |
| 5717 | auto params = AuthorizationSetBuilder() |
| 5718 | .BlockMode(BlockMode::GCM) |
| 5719 | .Padding(PaddingMode::NONE) |
| 5720 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5721 | |
| 5722 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::ENCRYPT, params)); |
| 5723 | } |
| 5724 | |
| 5725 | /* |
| 5726 | * EncryptionOperationsTest.AesGcmTooShortTagOnDecrypt |
| 5727 | * |
| 5728 | * Verifies that AES GCM mode fails correctly when a too-short tag is provided to decryption. |
| 5729 | */ |
| 5730 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTagOnDecrypt) { |
| 5731 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5732 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5733 | .AesEncryptionKey(128) |
| 5734 | .BlockMode(BlockMode::GCM) |
| 5735 | .Padding(PaddingMode::NONE) |
| 5736 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5737 | string aad = "foobar"; |
| 5738 | string message = "123456789012345678901234567890123456"; |
| 5739 | auto params = AuthorizationSetBuilder() |
| 5740 | .BlockMode(BlockMode::GCM) |
| 5741 | .Padding(PaddingMode::NONE) |
| 5742 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5743 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5744 | // Encrypt |
| 5745 | AuthorizationSet begin_out_params; |
| 5746 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 5747 | EXPECT_EQ(1U, begin_out_params.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5748 | ASSERT_TRUE(begin_out_params.GetTagValue(TAG_NONCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5749 | |
| 5750 | AuthorizationSet finish_out_params; |
| 5751 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5752 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5753 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5754 | |
| 5755 | params = AuthorizationSetBuilder() |
| 5756 | .Authorizations(begin_out_params) |
| 5757 | .BlockMode(BlockMode::GCM) |
| 5758 | .Padding(PaddingMode::NONE) |
| 5759 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5760 | |
| 5761 | // Decrypt. |
| 5762 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::DECRYPT, params)); |
| 5763 | } |
| 5764 | |
| 5765 | /* |
| 5766 | * EncryptionOperationsTest.AesGcmCorruptKey |
| 5767 | * |
| 5768 | * Verifies that AES GCM mode fails correctly when the decryption key is incorrect. |
| 5769 | */ |
| 5770 | TEST_P(EncryptionOperationsTest, AesGcmCorruptKey) { |
| 5771 | const uint8_t nonce_bytes[] = { |
| 5772 | 0xb7, 0x94, 0x37, 0xae, 0x08, 0xff, 0x35, 0x5d, 0x7d, 0x8a, 0x4d, 0x0f, |
| 5773 | }; |
| 5774 | string nonce = make_string(nonce_bytes); |
| 5775 | const uint8_t ciphertext_bytes[] = { |
| 5776 | 0xb3, 0xf6, 0x79, 0x9e, 0x8f, 0x93, 0x26, 0xf2, 0xdf, 0x1e, 0x80, 0xfc, |
| 5777 | 0xd2, 0xcb, 0x16, 0xd7, 0x8c, 0x9d, 0xc7, 0xcc, 0x14, 0xbb, 0x67, 0x78, |
| 5778 | 0x62, 0xdc, 0x6c, 0x63, 0x9b, 0x3a, 0x63, 0x38, 0xd2, 0x4b, 0x31, 0x2d, |
| 5779 | 0x39, 0x89, 0xe5, 0x92, 0x0b, 0x5d, 0xbf, 0xc9, 0x76, 0x76, 0x5e, 0xfb, |
| 5780 | 0xfe, 0x57, 0xbb, 0x38, 0x59, 0x40, 0xa7, 0xa4, 0x3b, 0xdf, 0x05, 0xbd, |
| 5781 | 0xda, 0xe3, 0xc9, 0xd6, 0xa2, 0xfb, 0xbd, 0xfc, 0xc0, 0xcb, 0xa0, |
| 5782 | }; |
| 5783 | string ciphertext = make_string(ciphertext_bytes); |
| 5784 | |
| 5785 | auto params = AuthorizationSetBuilder() |
| 5786 | .BlockMode(BlockMode::GCM) |
| 5787 | .Padding(PaddingMode::NONE) |
| 5788 | .Authorization(TAG_MAC_LENGTH, 128) |
| 5789 | .Authorization(TAG_NONCE, nonce.data(), nonce.size()); |
| 5790 | |
| 5791 | auto import_params = AuthorizationSetBuilder() |
| 5792 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5793 | .AesEncryptionKey(128) |
| 5794 | .BlockMode(BlockMode::GCM) |
| 5795 | .Padding(PaddingMode::NONE) |
| 5796 | .Authorization(TAG_CALLER_NONCE) |
| 5797 | .Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 5798 | |
| 5799 | // Import correct key and decrypt |
| 5800 | const uint8_t key_bytes[] = { |
| 5801 | 0xba, 0x76, 0x35, 0x4f, 0x0a, 0xed, 0x6e, 0x8d, |
| 5802 | 0x91, 0xf4, 0x5c, 0x4f, 0xf5, 0xa0, 0x62, 0xdb, |
| 5803 | }; |
| 5804 | string key = make_string(key_bytes); |
| 5805 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5806 | string plaintext = DecryptMessage(ciphertext, params); |
| 5807 | CheckedDeleteKey(); |
| 5808 | |
| 5809 | // Corrupt key and attempt to decrypt |
| 5810 | key[0] = 0; |
| 5811 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5812 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5813 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
| 5814 | CheckedDeleteKey(); |
| 5815 | } |
| 5816 | |
| 5817 | /* |
| 5818 | * EncryptionOperationsTest.AesGcmAadNoData |
| 5819 | * |
| 5820 | * Verifies that AES GCM mode works when provided additional authenticated data, but no data to |
| 5821 | * encrypt. |
| 5822 | */ |
| 5823 | TEST_P(EncryptionOperationsTest, AesGcmAadNoData) { |
| 5824 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5825 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5826 | .AesEncryptionKey(128) |
| 5827 | .BlockMode(BlockMode::GCM) |
| 5828 | .Padding(PaddingMode::NONE) |
| 5829 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5830 | |
| 5831 | string aad = "1234567890123456"; |
| 5832 | auto params = AuthorizationSetBuilder() |
| 5833 | .BlockMode(BlockMode::GCM) |
| 5834 | .Padding(PaddingMode::NONE) |
| 5835 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5836 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5837 | // Encrypt |
| 5838 | AuthorizationSet begin_out_params; |
| 5839 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 5840 | string ciphertext; |
| 5841 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5842 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5843 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5844 | EXPECT_TRUE(finish_out_params.empty()); |
| 5845 | |
| 5846 | // Grab nonce |
| 5847 | params.push_back(begin_out_params); |
| 5848 | |
| 5849 | // Decrypt. |
| 5850 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5851 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5852 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5853 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5854 | |
| 5855 | EXPECT_TRUE(finish_out_params.empty()); |
| 5856 | |
| 5857 | EXPECT_EQ("", plaintext); |
| 5858 | } |
| 5859 | |
| 5860 | /* |
| 5861 | * EncryptionOperationsTest.AesGcmMultiPartAad |
| 5862 | * |
| 5863 | * Verifies that AES GCM mode works when provided additional authenticated data in multiple |
| 5864 | * chunks. |
| 5865 | */ |
| 5866 | TEST_P(EncryptionOperationsTest, AesGcmMultiPartAad) { |
| 5867 | const size_t tag_bits = 128; |
| 5868 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5869 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5870 | .AesEncryptionKey(128) |
| 5871 | .BlockMode(BlockMode::GCM) |
| 5872 | .Padding(PaddingMode::NONE) |
| 5873 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5874 | |
| 5875 | string message = "123456789012345678901234567890123456"; |
| 5876 | auto begin_params = AuthorizationSetBuilder() |
| 5877 | .BlockMode(BlockMode::GCM) |
| 5878 | .Padding(PaddingMode::NONE) |
| 5879 | .Authorization(TAG_MAC_LENGTH, tag_bits); |
| 5880 | AuthorizationSet begin_out_params; |
| 5881 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5882 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 5883 | |
| 5884 | // No data, AAD only. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5885 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
| 5886 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5887 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5888 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 5889 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5890 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5891 | // Expect 128-bit (16-byte) tag appended to ciphertext. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5892 | EXPECT_EQ(message.size() + (tag_bits / 8), ciphertext.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5893 | |
| 5894 | // Grab nonce. |
| 5895 | begin_params.push_back(begin_out_params); |
| 5896 | |
| 5897 | // Decrypt |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5898 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5899 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foofoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5900 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5901 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5902 | EXPECT_EQ(message, plaintext); |
| 5903 | } |
| 5904 | |
| 5905 | /* |
| 5906 | * EncryptionOperationsTest.AesGcmAadOutOfOrder |
| 5907 | * |
| 5908 | * Verifies that AES GCM mode fails correctly when given AAD after data to encipher. |
| 5909 | */ |
| 5910 | TEST_P(EncryptionOperationsTest, AesGcmAadOutOfOrder) { |
| 5911 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5912 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5913 | .AesEncryptionKey(128) |
| 5914 | .BlockMode(BlockMode::GCM) |
| 5915 | .Padding(PaddingMode::NONE) |
| 5916 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5917 | |
| 5918 | string message = "123456789012345678901234567890123456"; |
| 5919 | auto begin_params = AuthorizationSetBuilder() |
| 5920 | .BlockMode(BlockMode::GCM) |
| 5921 | .Padding(PaddingMode::NONE) |
| 5922 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5923 | AuthorizationSet begin_out_params; |
| 5924 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5925 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 5926 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5927 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5928 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5929 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 5930 | EXPECT_EQ(ErrorCode::INVALID_TAG, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5931 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5932 | // The failure should have already cancelled the operation. |
| 5933 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 5934 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5935 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | /* |
| 5939 | * EncryptionOperationsTest.AesGcmBadAad |
| 5940 | * |
| 5941 | * Verifies that AES GCM decryption fails correctly when additional authenticated date is wrong. |
| 5942 | */ |
| 5943 | TEST_P(EncryptionOperationsTest, AesGcmBadAad) { |
| 5944 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5945 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5946 | .AesEncryptionKey(128) |
| 5947 | .BlockMode(BlockMode::GCM) |
| 5948 | .Padding(PaddingMode::NONE) |
| 5949 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5950 | |
| 5951 | string message = "12345678901234567890123456789012"; |
| 5952 | auto begin_params = AuthorizationSetBuilder() |
| 5953 | .BlockMode(BlockMode::GCM) |
| 5954 | .Padding(PaddingMode::NONE) |
| 5955 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5956 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5957 | // Encrypt |
| 5958 | AuthorizationSet begin_out_params; |
| 5959 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5960 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5961 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5962 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5963 | |
| 5964 | // Grab nonce |
| 5965 | begin_params.push_back(begin_out_params); |
| 5966 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5967 | // Decrypt. |
| 5968 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5969 | EXPECT_EQ(ErrorCode::OK, UpdateAad("barfoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5970 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5971 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
| 5974 | /* |
| 5975 | * EncryptionOperationsTest.AesGcmWrongNonce |
| 5976 | * |
| 5977 | * Verifies that AES GCM decryption fails correctly when the nonce is incorrect. |
| 5978 | */ |
| 5979 | TEST_P(EncryptionOperationsTest, AesGcmWrongNonce) { |
| 5980 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5981 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5982 | .AesEncryptionKey(128) |
| 5983 | .BlockMode(BlockMode::GCM) |
| 5984 | .Padding(PaddingMode::NONE) |
| 5985 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5986 | |
| 5987 | string message = "12345678901234567890123456789012"; |
| 5988 | auto begin_params = AuthorizationSetBuilder() |
| 5989 | .BlockMode(BlockMode::GCM) |
| 5990 | .Padding(PaddingMode::NONE) |
| 5991 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5992 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5993 | // Encrypt |
| 5994 | AuthorizationSet begin_out_params; |
| 5995 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5996 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5997 | string ciphertext; |
| 5998 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5999 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6000 | |
| 6001 | // Wrong nonce |
| 6002 | begin_params.push_back(TAG_NONCE, AidlBuf("123456789012")); |
| 6003 | |
| 6004 | // Decrypt. |
| 6005 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6006 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6007 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6008 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6009 | |
| 6010 | // With wrong nonce, should have gotten garbage plaintext (or none). |
| 6011 | EXPECT_NE(message, plaintext); |
| 6012 | } |
| 6013 | |
| 6014 | /* |
| 6015 | * EncryptionOperationsTest.AesGcmCorruptTag |
| 6016 | * |
| 6017 | * Verifies that AES GCM decryption fails correctly when the tag is wrong. |
| 6018 | */ |
| 6019 | TEST_P(EncryptionOperationsTest, AesGcmCorruptTag) { |
| 6020 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6021 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6022 | .AesEncryptionKey(128) |
| 6023 | .BlockMode(BlockMode::GCM) |
| 6024 | .Padding(PaddingMode::NONE) |
| 6025 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 6026 | |
| 6027 | string aad = "1234567890123456"; |
| 6028 | string message = "123456789012345678901234567890123456"; |
| 6029 | |
| 6030 | auto params = AuthorizationSetBuilder() |
| 6031 | .BlockMode(BlockMode::GCM) |
| 6032 | .Padding(PaddingMode::NONE) |
| 6033 | .Authorization(TAG_MAC_LENGTH, 128); |
| 6034 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6035 | // Encrypt |
| 6036 | AuthorizationSet begin_out_params; |
| 6037 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6038 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6039 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6040 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6041 | |
| 6042 | // Corrupt tag |
| 6043 | ++(*ciphertext.rbegin()); |
| 6044 | |
| 6045 | // Grab nonce |
| 6046 | params.push_back(begin_out_params); |
| 6047 | |
| 6048 | // Decrypt. |
| 6049 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6050 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6051 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6052 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | /* |
| 6056 | * EncryptionOperationsTest.TripleDesEcbRoundTripSuccess |
| 6057 | * |
| 6058 | * Verifies that 3DES is basically functional. |
| 6059 | */ |
| 6060 | TEST_P(EncryptionOperationsTest, TripleDesEcbRoundTripSuccess) { |
| 6061 | auto auths = AuthorizationSetBuilder() |
| 6062 | .TripleDesEncryptionKey(168) |
| 6063 | .BlockMode(BlockMode::ECB) |
| 6064 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6065 | .Padding(PaddingMode::NONE); |
| 6066 | |
| 6067 | ASSERT_EQ(ErrorCode::OK, GenerateKey(auths)); |
| 6068 | // Two-block message. |
| 6069 | string message = "1234567890123456"; |
| 6070 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 6071 | string ciphertext1 = EncryptMessage(message, inParams); |
| 6072 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6073 | |
| 6074 | string ciphertext2 = EncryptMessage(string(message), inParams); |
| 6075 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 6076 | |
| 6077 | // ECB is deterministic. |
| 6078 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 6079 | |
| 6080 | string plaintext = DecryptMessage(ciphertext1, inParams); |
| 6081 | EXPECT_EQ(message, plaintext); |
| 6082 | } |
| 6083 | |
| 6084 | /* |
| 6085 | * EncryptionOperationsTest.TripleDesEcbNotAuthorized |
| 6086 | * |
| 6087 | * Verifies that CBC keys reject ECB usage. |
| 6088 | */ |
| 6089 | TEST_P(EncryptionOperationsTest, TripleDesEcbNotAuthorized) { |
| 6090 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6091 | .TripleDesEncryptionKey(168) |
| 6092 | .BlockMode(BlockMode::CBC) |
| 6093 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6094 | .Padding(PaddingMode::NONE))); |
| 6095 | |
| 6096 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 6097 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
| 6098 | } |
| 6099 | |
| 6100 | /* |
| 6101 | * EncryptionOperationsTest.TripleDesEcbPkcs7Padding |
| 6102 | * |
| 6103 | * Tests ECB mode with PKCS#7 padding, various message sizes. |
| 6104 | */ |
| 6105 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7Padding) { |
| 6106 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6107 | .TripleDesEncryptionKey(168) |
| 6108 | .BlockMode(BlockMode::ECB) |
| 6109 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6110 | .Padding(PaddingMode::PKCS7))); |
| 6111 | |
| 6112 | for (size_t i = 0; i < 32; ++i) { |
| 6113 | string message(i, 'a'); |
| 6114 | auto inParams = |
| 6115 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 6116 | string ciphertext = EncryptMessage(message, inParams); |
| 6117 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 6118 | string plaintext = DecryptMessage(ciphertext, inParams); |
| 6119 | EXPECT_EQ(message, plaintext); |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | /* |
| 6124 | * EncryptionOperationsTest.TripleDesEcbNoPaddingKeyWithPkcs7Padding |
| 6125 | * |
| 6126 | * Verifies that keys configured for no padding reject PKCS7 padding |
| 6127 | */ |
| 6128 | TEST_P(EncryptionOperationsTest, TripleDesEcbNoPaddingKeyWithPkcs7Padding) { |
| 6129 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6130 | .TripleDesEncryptionKey(168) |
| 6131 | .BlockMode(BlockMode::ECB) |
| 6132 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6133 | .Padding(PaddingMode::NONE))); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 6134 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 6135 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | /* |
| 6139 | * EncryptionOperationsTest.TripleDesEcbPkcs7PaddingCorrupted |
| 6140 | * |
| 6141 | * Verifies that corrupted padding is detected. |
| 6142 | */ |
| 6143 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7PaddingCorrupted) { |
| 6144 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6145 | .TripleDesEncryptionKey(168) |
| 6146 | .BlockMode(BlockMode::ECB) |
| 6147 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6148 | .Padding(PaddingMode::PKCS7))); |
| 6149 | |
| 6150 | string message = "a"; |
| 6151 | string ciphertext = EncryptMessage(message, BlockMode::ECB, PaddingMode::PKCS7); |
| 6152 | EXPECT_EQ(8U, ciphertext.size()); |
| 6153 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6154 | |
| 6155 | AuthorizationSetBuilder begin_params; |
| 6156 | begin_params.push_back(TAG_BLOCK_MODE, BlockMode::ECB); |
| 6157 | begin_params.push_back(TAG_PADDING, PaddingMode::PKCS7); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 6158 | |
| 6159 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 6160 | ++ciphertext[ciphertext.size() / 2]; |
| 6161 | |
| 6162 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 6163 | string plaintext; |
| 6164 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 6165 | ErrorCode error = Finish(&plaintext); |
| 6166 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 6167 | // This is the expected error, we can exit the test now. |
| 6168 | return; |
| 6169 | } else { |
| 6170 | // Very small chance we got valid decryption, so try again. |
| 6171 | ASSERT_EQ(error, ErrorCode::OK); |
| 6172 | } |
| 6173 | } |
| 6174 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | struct TripleDesTestVector { |
| 6178 | const char* name; |
| 6179 | const KeyPurpose purpose; |
| 6180 | const BlockMode block_mode; |
| 6181 | const PaddingMode padding_mode; |
| 6182 | const char* key; |
| 6183 | const char* iv; |
| 6184 | const char* input; |
| 6185 | const char* output; |
| 6186 | }; |
| 6187 | |
| 6188 | // These test vectors are from NIST CAVP, plus a few custom variants to test padding, since all |
| 6189 | // of the NIST vectors are multiples of the block size. |
| 6190 | static const TripleDesTestVector kTripleDesTestVectors[] = { |
| 6191 | { |
| 6192 | "TECBMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6193 | "a2b5bc67da13dc92cd9d344aa238544a0e1fa79ef76810cd", // key |
| 6194 | "", // IV |
| 6195 | "329d86bdf1bc5af4", // input |
| 6196 | "d946c2756d78633f", // output |
| 6197 | }, |
| 6198 | { |
| 6199 | "TECBMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6200 | "49e692290d2a5e46bace79b9648a4c5d491004c262dc9d49", // key |
| 6201 | "", // IV |
| 6202 | "6b1540781b01ce1997adae102dbf3c5b", // input |
| 6203 | "4d0dc182d6e481ac4a3dc6ab6976ccae", // output |
| 6204 | }, |
| 6205 | { |
| 6206 | "TECBMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6207 | "52daec2ac7dc1958377392682f37860b2cc1ea2304bab0e9", // key |
| 6208 | "", // IV |
| 6209 | "6daad94ce08acfe7", // input |
| 6210 | "660e7d32dcc90e79", // output |
| 6211 | }, |
| 6212 | { |
| 6213 | "TECBMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 6214 | "7f8fe3d3f4a48394fb682c2919926d6ddfce8932529229ce", // key |
| 6215 | "", // IV |
| 6216 | "e9653a0a1f05d31b9acd12d73aa9879d", // input |
| 6217 | "9b2ae9d998efe62f1b592e7e1df8ff38", // output |
| 6218 | }, |
| 6219 | { |
| 6220 | "TCBCMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6221 | "b5cb1504802326c73df186e3e352a20de643b0d63ee30e37", // key |
| 6222 | "43f791134c5647ba", // IV |
| 6223 | "dcc153cef81d6f24", // input |
| 6224 | "92538bd8af18d3ba", // output |
| 6225 | }, |
| 6226 | { |
| 6227 | "TCBCMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6228 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6229 | "c2e999cb6249023c", // IV |
| 6230 | "c689aee38a301bb316da75db36f110b5", // input |
| 6231 | "e9afaba5ec75ea1bbe65506655bb4ecb", // output |
| 6232 | }, |
| 6233 | { |
| 6234 | "TCBCMMT3 Encrypt 1 PKCS7 variant", KeyPurpose::ENCRYPT, BlockMode::CBC, |
| 6235 | PaddingMode::PKCS7, |
| 6236 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6237 | "c2e999cb6249023c", // IV |
| 6238 | "c689aee38a301bb316da75db36f110b500", // input |
| 6239 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // output |
| 6240 | }, |
| 6241 | { |
| 6242 | "TCBCMMT3 Encrypt 1 PKCS7 decrypted", KeyPurpose::DECRYPT, BlockMode::CBC, |
| 6243 | PaddingMode::PKCS7, |
| 6244 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 6245 | "c2e999cb6249023c", // IV |
| 6246 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // input |
| 6247 | "c689aee38a301bb316da75db36f110b500", // output |
| 6248 | }, |
| 6249 | { |
| 6250 | "TCBCMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6251 | "5eb6040d46082c7aa7d06dfd08dfeac8c18364c1548c3ba1", // key |
| 6252 | "41746c7e442d3681", // IV |
| 6253 | "c53a7b0ec40600fe", // input |
| 6254 | "d4f00eb455de1034", // output |
| 6255 | }, |
| 6256 | { |
| 6257 | "TCBCMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 6258 | "5b1cce7c0dc1ec49130dfb4af45785ab9179e567f2c7d549", // key |
| 6259 | "3982bc02c3727d45", // IV |
| 6260 | "6006f10adef52991fcc777a1238bbb65", // input |
| 6261 | "edae09288e9e3bc05746d872b48e3b29", // output |
| 6262 | }, |
| 6263 | }; |
| 6264 | |
| 6265 | /* |
| 6266 | * EncryptionOperationsTest.TripleDesTestVector |
| 6267 | * |
| 6268 | * Verifies that NIST (plus a few extra) test vectors produce the correct results. |
| 6269 | */ |
| 6270 | TEST_P(EncryptionOperationsTest, TripleDesTestVector) { |
| 6271 | constexpr size_t num_tests = sizeof(kTripleDesTestVectors) / sizeof(TripleDesTestVector); |
| 6272 | for (auto* test = kTripleDesTestVectors; test < kTripleDesTestVectors + num_tests; ++test) { |
| 6273 | SCOPED_TRACE(test->name); |
| 6274 | CheckTripleDesTestVector(test->purpose, test->block_mode, test->padding_mode, |
| 6275 | hex2str(test->key), hex2str(test->iv), hex2str(test->input), |
| 6276 | hex2str(test->output)); |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | /* |
| 6281 | * EncryptionOperationsTest.TripleDesCbcRoundTripSuccess |
| 6282 | * |
| 6283 | * Validates CBC mode functionality. |
| 6284 | */ |
| 6285 | TEST_P(EncryptionOperationsTest, TripleDesCbcRoundTripSuccess) { |
| 6286 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6287 | .TripleDesEncryptionKey(168) |
| 6288 | .BlockMode(BlockMode::CBC) |
| 6289 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6290 | .Padding(PaddingMode::NONE))); |
| 6291 | |
| 6292 | ASSERT_GT(key_blob_.size(), 0U); |
| 6293 | |
| 6294 | // Two-block message. |
| 6295 | string message = "1234567890123456"; |
| 6296 | vector<uint8_t> iv1; |
| 6297 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv1); |
| 6298 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6299 | |
| 6300 | vector<uint8_t> iv2; |
| 6301 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv2); |
| 6302 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 6303 | |
| 6304 | // IVs should be random, so ciphertexts should differ. |
| 6305 | EXPECT_NE(iv1, iv2); |
| 6306 | EXPECT_NE(ciphertext1, ciphertext2); |
| 6307 | |
| 6308 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv1); |
| 6309 | EXPECT_EQ(message, plaintext); |
| 6310 | } |
| 6311 | |
| 6312 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6313 | * EncryptionOperationsTest.TripleDesInvalidCallerIv |
| 6314 | * |
| 6315 | * Validates that keymint fails correctly when the user supplies an incorrect-size IV. |
| 6316 | */ |
| 6317 | TEST_P(EncryptionOperationsTest, TripleDesInvalidCallerIv) { |
| 6318 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6319 | .TripleDesEncryptionKey(168) |
| 6320 | .BlockMode(BlockMode::CBC) |
| 6321 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6322 | .Authorization(TAG_CALLER_NONCE) |
| 6323 | .Padding(PaddingMode::NONE))); |
| 6324 | auto params = AuthorizationSetBuilder() |
| 6325 | .BlockMode(BlockMode::CBC) |
| 6326 | .Padding(PaddingMode::NONE) |
| 6327 | .Authorization(TAG_NONCE, AidlBuf("abcdefg")); |
| 6328 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 6329 | } |
| 6330 | |
| 6331 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6332 | * EncryptionOperationsTest.TripleDesCallerIv |
| 6333 | * |
| 6334 | * Validates that 3DES keys can allow caller-specified IVs, and use them correctly. |
| 6335 | */ |
| 6336 | TEST_P(EncryptionOperationsTest, TripleDesCallerIv) { |
| 6337 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6338 | .TripleDesEncryptionKey(168) |
| 6339 | .BlockMode(BlockMode::CBC) |
| 6340 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6341 | .Authorization(TAG_CALLER_NONCE) |
| 6342 | .Padding(PaddingMode::NONE))); |
| 6343 | string message = "1234567890123456"; |
| 6344 | vector<uint8_t> iv; |
| 6345 | // Don't specify IV, should get a random one. |
| 6346 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 6347 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6348 | EXPECT_EQ(8U, iv.size()); |
| 6349 | |
| 6350 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6351 | EXPECT_EQ(message, plaintext); |
| 6352 | |
| 6353 | // Now specify an IV, should also work. |
| 6354 | iv = AidlBuf("abcdefgh"); |
| 6355 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6356 | |
| 6357 | // Decrypt with correct IV. |
| 6358 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6359 | EXPECT_EQ(message, plaintext); |
| 6360 | |
| 6361 | // Now try with wrong IV. |
| 6362 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, AidlBuf("aaaaaaaa")); |
| 6363 | EXPECT_NE(message, plaintext); |
| 6364 | } |
| 6365 | |
| 6366 | /* |
| 6367 | * EncryptionOperationsTest, TripleDesCallerNonceProhibited. |
| 6368 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6369 | * 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] | 6370 | */ |
| 6371 | TEST_P(EncryptionOperationsTest, TripleDesCallerNonceProhibited) { |
| 6372 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6373 | .TripleDesEncryptionKey(168) |
| 6374 | .BlockMode(BlockMode::CBC) |
| 6375 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6376 | .Padding(PaddingMode::NONE))); |
| 6377 | |
| 6378 | string message = "12345678901234567890123456789012"; |
| 6379 | vector<uint8_t> iv; |
| 6380 | // Don't specify nonce, should get a random one. |
| 6381 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 6382 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 6383 | EXPECT_EQ(8U, iv.size()); |
| 6384 | |
| 6385 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 6386 | EXPECT_EQ(message, plaintext); |
| 6387 | |
| 6388 | // Now specify a nonce, should fail. |
| 6389 | auto input_params = AuthorizationSetBuilder() |
| 6390 | .Authorization(TAG_NONCE, AidlBuf("abcdefgh")) |
| 6391 | .BlockMode(BlockMode::CBC) |
| 6392 | .Padding(PaddingMode::NONE); |
| 6393 | AuthorizationSet output_params; |
| 6394 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, |
| 6395 | Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 6396 | } |
| 6397 | |
| 6398 | /* |
| 6399 | * EncryptionOperationsTest.TripleDesCbcNotAuthorized |
| 6400 | * |
| 6401 | * Verifies that 3DES ECB-only keys do not allow CBC usage. |
| 6402 | */ |
| 6403 | TEST_P(EncryptionOperationsTest, TripleDesCbcNotAuthorized) { |
| 6404 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6405 | .TripleDesEncryptionKey(168) |
| 6406 | .BlockMode(BlockMode::ECB) |
| 6407 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6408 | .Padding(PaddingMode::NONE))); |
| 6409 | // Two-block message. |
| 6410 | string message = "1234567890123456"; |
| 6411 | auto begin_params = |
| 6412 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 6413 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 6414 | } |
| 6415 | |
| 6416 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6417 | * EncryptionOperationsTest.TripleDesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6418 | * |
| 6419 | * Verifies that unpadded CBC operations reject inputs that are not a multiple of block size. |
| 6420 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6421 | TEST_P(EncryptionOperationsTest, TripleDesEcbCbcNoPaddingWrongInputSize) { |
| 6422 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 6423 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6424 | .TripleDesEncryptionKey(168) |
| 6425 | .BlockMode(blockMode) |
| 6426 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6427 | .Padding(PaddingMode::NONE))); |
| 6428 | // Message is slightly shorter than two blocks. |
| 6429 | string message = "123456789012345"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6430 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6431 | auto begin_params = |
| 6432 | AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 6433 | AuthorizationSet output_params; |
| 6434 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &output_params)); |
| 6435 | string ciphertext; |
| 6436 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, "", &ciphertext)); |
| 6437 | |
| 6438 | CheckedDeleteKey(); |
| 6439 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | /* |
| 6443 | * EncryptionOperationsTest, TripleDesCbcPkcs7Padding. |
| 6444 | * |
| 6445 | * Verifies that PKCS7 padding works correctly in CBC mode. |
| 6446 | */ |
| 6447 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7Padding) { |
| 6448 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6449 | .TripleDesEncryptionKey(168) |
| 6450 | .BlockMode(BlockMode::CBC) |
| 6451 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6452 | .Padding(PaddingMode::PKCS7))); |
| 6453 | |
| 6454 | // Try various message lengths; all should work. |
| 6455 | for (size_t i = 0; i < 32; ++i) { |
| 6456 | string message(i, 'a'); |
| 6457 | vector<uint8_t> iv; |
| 6458 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 6459 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 6460 | string plaintext = DecryptMessage(ciphertext, BlockMode::CBC, PaddingMode::PKCS7, iv); |
| 6461 | EXPECT_EQ(message, plaintext); |
| 6462 | } |
| 6463 | } |
| 6464 | |
| 6465 | /* |
| 6466 | * EncryptionOperationsTest.TripleDesCbcNoPaddingKeyWithPkcs7Padding |
| 6467 | * |
| 6468 | * Verifies that a key that requires PKCS7 padding cannot be used in unpadded mode. |
| 6469 | */ |
| 6470 | TEST_P(EncryptionOperationsTest, TripleDesCbcNoPaddingKeyWithPkcs7Padding) { |
| 6471 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6472 | .TripleDesEncryptionKey(168) |
| 6473 | .BlockMode(BlockMode::CBC) |
| 6474 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6475 | .Padding(PaddingMode::NONE))); |
| 6476 | |
| 6477 | // Try various message lengths; all should fail. |
| 6478 | for (size_t i = 0; i < 32; ++i) { |
| 6479 | auto begin_params = |
| 6480 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7); |
| 6481 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | /* |
| 6486 | * EncryptionOperationsTest.TripleDesCbcPkcs7PaddingCorrupted |
| 6487 | * |
| 6488 | * Verifies that corrupted PKCS7 padding is rejected during decryption. |
| 6489 | */ |
| 6490 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7PaddingCorrupted) { |
| 6491 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6492 | .TripleDesEncryptionKey(168) |
| 6493 | .BlockMode(BlockMode::CBC) |
| 6494 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6495 | .Padding(PaddingMode::PKCS7))); |
| 6496 | |
| 6497 | string message = "a"; |
| 6498 | vector<uint8_t> iv; |
| 6499 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 6500 | EXPECT_EQ(8U, ciphertext.size()); |
| 6501 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6502 | |
| 6503 | auto begin_params = AuthorizationSetBuilder() |
| 6504 | .BlockMode(BlockMode::CBC) |
| 6505 | .Padding(PaddingMode::PKCS7) |
| 6506 | .Authorization(TAG_NONCE, iv); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 6507 | |
| 6508 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 6509 | ++ciphertext[ciphertext.size() / 2]; |
| 6510 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 6511 | string plaintext; |
| 6512 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 6513 | ErrorCode error = Finish(&plaintext); |
| 6514 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 6515 | // This is the expected error, we can exit the test now. |
| 6516 | return; |
| 6517 | } else { |
| 6518 | // Very small chance we got valid decryption, so try again. |
| 6519 | ASSERT_EQ(error, ErrorCode::OK); |
| 6520 | } |
| 6521 | } |
| 6522 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6523 | } |
| 6524 | |
| 6525 | /* |
| 6526 | * EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding. |
| 6527 | * |
| 6528 | * Verifies that 3DES CBC works with many different input sizes. |
| 6529 | */ |
| 6530 | TEST_P(EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding) { |
| 6531 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6532 | .TripleDesEncryptionKey(168) |
| 6533 | .BlockMode(BlockMode::CBC) |
| 6534 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6535 | .Padding(PaddingMode::NONE))); |
| 6536 | |
| 6537 | int increment = 7; |
| 6538 | string message(240, 'a'); |
| 6539 | AuthorizationSet input_params = |
| 6540 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 6541 | AuthorizationSet output_params; |
| 6542 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 6543 | |
| 6544 | string ciphertext; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6545 | for (size_t i = 0; i < message.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6546 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6547 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
| 6548 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 6549 | |
| 6550 | // Move TAG_NONCE into input_params |
| 6551 | input_params = output_params; |
| 6552 | input_params.push_back(TAG_BLOCK_MODE, BlockMode::CBC); |
| 6553 | input_params.push_back(TAG_PADDING, PaddingMode::NONE); |
| 6554 | output_params.Clear(); |
| 6555 | |
| 6556 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, input_params, &output_params)); |
| 6557 | string plaintext; |
| 6558 | for (size_t i = 0; i < ciphertext.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 6559 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6560 | EXPECT_EQ(ErrorCode::OK, Finish(&plaintext)); |
| 6561 | EXPECT_EQ(ciphertext.size(), plaintext.size()); |
| 6562 | EXPECT_EQ(message, plaintext); |
| 6563 | } |
| 6564 | |
| 6565 | INSTANTIATE_KEYMINT_AIDL_TEST(EncryptionOperationsTest); |
| 6566 | |
| 6567 | typedef KeyMintAidlTestBase MaxOperationsTest; |
| 6568 | |
| 6569 | /* |
| 6570 | * MaxOperationsTest.TestLimitAes |
| 6571 | * |
| 6572 | * Verifies that the max uses per boot tag works correctly with AES keys. |
| 6573 | */ |
| 6574 | TEST_P(MaxOperationsTest, TestLimitAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6575 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6576 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6577 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6578 | |
| 6579 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6580 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6581 | .AesEncryptionKey(128) |
| 6582 | .EcbMode() |
| 6583 | .Padding(PaddingMode::NONE) |
| 6584 | .Authorization(TAG_MAX_USES_PER_BOOT, 3))); |
| 6585 | |
| 6586 | string message = "1234567890123456"; |
| 6587 | |
| 6588 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6589 | |
| 6590 | EncryptMessage(message, params); |
| 6591 | EncryptMessage(message, params); |
| 6592 | EncryptMessage(message, params); |
| 6593 | |
| 6594 | // Fourth time should fail. |
| 6595 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::ENCRYPT, params)); |
| 6596 | } |
| 6597 | |
| 6598 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6599 | * MaxOperationsTest.TestLimitRsa |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6600 | * |
| 6601 | * Verifies that the max uses per boot tag works correctly with RSA keys. |
| 6602 | */ |
| 6603 | TEST_P(MaxOperationsTest, TestLimitRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6604 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6605 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6606 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6607 | |
| 6608 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6609 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6610 | .RsaSigningKey(1024, 65537) |
| 6611 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6612 | .Authorization(TAG_MAX_USES_PER_BOOT, 3) |
| 6613 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6614 | |
| 6615 | string message = "1234567890123456"; |
| 6616 | |
| 6617 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6618 | |
| 6619 | SignMessage(message, params); |
| 6620 | SignMessage(message, params); |
| 6621 | SignMessage(message, params); |
| 6622 | |
| 6623 | // Fourth time should fail. |
| 6624 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::SIGN, params)); |
| 6625 | } |
| 6626 | |
| 6627 | INSTANTIATE_KEYMINT_AIDL_TEST(MaxOperationsTest); |
| 6628 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6629 | typedef KeyMintAidlTestBase UsageCountLimitTest; |
| 6630 | |
| 6631 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6632 | * UsageCountLimitTest.TestSingleUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6633 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6634 | * 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] | 6635 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6636 | TEST_P(UsageCountLimitTest, TestSingleUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6637 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6638 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6639 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6640 | |
| 6641 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6642 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6643 | .AesEncryptionKey(128) |
| 6644 | .EcbMode() |
| 6645 | .Padding(PaddingMode::NONE) |
| 6646 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1))); |
| 6647 | |
| 6648 | // Check the usage count limit tag appears in the authorizations. |
| 6649 | AuthorizationSet auths; |
| 6650 | for (auto& entry : key_characteristics_) { |
| 6651 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6652 | } |
| 6653 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6654 | << "key usage count limit " << 1U << " missing"; |
| 6655 | |
| 6656 | string message = "1234567890123456"; |
| 6657 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6658 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6659 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6660 | AuthorizationSet keystore_auths = |
| 6661 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6662 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6663 | // First usage of AES key should work. |
| 6664 | EncryptMessage(message, params); |
| 6665 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6666 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 6667 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6668 | // must be invalidated from secure storage (such as RPMB partition). |
| 6669 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 6670 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6671 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6672 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6673 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 6674 | } |
| 6675 | } |
| 6676 | |
| 6677 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6678 | * UsageCountLimitTest.TestLimitedUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6679 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6680 | * 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] | 6681 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6682 | TEST_P(UsageCountLimitTest, TestLimitedUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6683 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6684 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6685 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6686 | |
| 6687 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6688 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6689 | .AesEncryptionKey(128) |
| 6690 | .EcbMode() |
| 6691 | .Padding(PaddingMode::NONE) |
| 6692 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3))); |
| 6693 | |
| 6694 | // Check the usage count limit tag appears in the authorizations. |
| 6695 | AuthorizationSet auths; |
| 6696 | for (auto& entry : key_characteristics_) { |
| 6697 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6698 | } |
| 6699 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 6700 | << "key usage count limit " << 3U << " missing"; |
| 6701 | |
| 6702 | string message = "1234567890123456"; |
| 6703 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 6704 | |
| 6705 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6706 | AuthorizationSet keystore_auths = |
| 6707 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6708 | |
| 6709 | EncryptMessage(message, params); |
| 6710 | EncryptMessage(message, params); |
| 6711 | EncryptMessage(message, params); |
| 6712 | |
| 6713 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6714 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6715 | // must be invalidated from secure storage (such as RPMB partition). |
| 6716 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 6717 | } else { |
| 6718 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6719 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
| 6720 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 6721 | } |
| 6722 | } |
| 6723 | |
| 6724 | /* |
| 6725 | * UsageCountLimitTest.TestSingleUseRsa |
| 6726 | * |
| 6727 | * Verifies that the usage count limit tag = 1 works correctly with RSA keys. |
| 6728 | */ |
| 6729 | TEST_P(UsageCountLimitTest, TestSingleUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6730 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6731 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6732 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6733 | |
| 6734 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6735 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6736 | .RsaSigningKey(1024, 65537) |
| 6737 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6738 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 6739 | .SetDefaultValidity())); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6740 | |
| 6741 | // Check the usage count limit tag appears in the authorizations. |
| 6742 | AuthorizationSet auths; |
| 6743 | for (auto& entry : key_characteristics_) { |
| 6744 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6745 | } |
| 6746 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6747 | << "key usage count limit " << 1U << " missing"; |
| 6748 | |
| 6749 | string message = "1234567890123456"; |
| 6750 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6751 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6752 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6753 | AuthorizationSet keystore_auths = |
| 6754 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6755 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6756 | // First usage of RSA key should work. |
| 6757 | SignMessage(message, params); |
| 6758 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6759 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 6760 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6761 | // must be invalidated from secure storage (such as RPMB partition). |
| 6762 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6763 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6764 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6765 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
| 6766 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | /* |
| 6771 | * UsageCountLimitTest.TestLimitUseRsa |
| 6772 | * |
| 6773 | * Verifies that the usage count limit tag > 1 works correctly with RSA keys. |
| 6774 | */ |
| 6775 | TEST_P(UsageCountLimitTest, TestLimitUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6776 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6777 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6778 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6779 | |
| 6780 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6781 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6782 | .RsaSigningKey(1024, 65537) |
| 6783 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6784 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3) |
| 6785 | .SetDefaultValidity())); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6786 | |
| 6787 | // Check the usage count limit tag appears in the authorizations. |
| 6788 | AuthorizationSet auths; |
| 6789 | for (auto& entry : key_characteristics_) { |
| 6790 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6791 | } |
| 6792 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 6793 | << "key usage count limit " << 3U << " missing"; |
| 6794 | |
| 6795 | string message = "1234567890123456"; |
| 6796 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6797 | |
| 6798 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6799 | AuthorizationSet keystore_auths = |
| 6800 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6801 | |
| 6802 | SignMessage(message, params); |
| 6803 | SignMessage(message, params); |
| 6804 | SignMessage(message, params); |
| 6805 | |
| 6806 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6807 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6808 | // must be invalidated from secure storage (such as RPMB partition). |
| 6809 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6810 | } else { |
| 6811 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6812 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6813 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6814 | } |
| 6815 | } |
| 6816 | |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6817 | /* |
| 6818 | * UsageCountLimitTest.TestSingleUseKeyAndRollbackResistance |
| 6819 | * |
| 6820 | * Verifies that when rollback resistance is supported by the KeyMint implementation with |
| 6821 | * the secure hardware, the single use key with usage count limit tag = 1 must also be enforced |
| 6822 | * in hardware. |
| 6823 | */ |
| 6824 | TEST_P(UsageCountLimitTest, TestSingleUseKeyAndRollbackResistance) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6825 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6826 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6827 | } |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6828 | |
| 6829 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6830 | .RsaSigningKey(2048, 65537) |
| 6831 | .Digest(Digest::NONE) |
| 6832 | .Padding(PaddingMode::NONE) |
| 6833 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6834 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6835 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6836 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6837 | GTEST_SKIP() << "Rollback resistance not supported"; |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6838 | } |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6839 | |
| 6840 | // Rollback resistance is supported by KeyMint, verify it is enforced in hardware. |
| 6841 | ASSERT_EQ(ErrorCode::OK, error); |
| 6842 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 6843 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
| 6844 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
| 6845 | |
| 6846 | // The KeyMint should also enforce single use key in hardware when it supports rollback |
| 6847 | // resistance. |
| 6848 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6849 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6850 | .RsaSigningKey(1024, 65537) |
| 6851 | .NoDigestOrPadding() |
| 6852 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 6853 | .SetDefaultValidity())); |
| 6854 | |
| 6855 | // Check the usage count limit tag appears in the hardware authorizations. |
| 6856 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6857 | EXPECT_TRUE(hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6858 | << "key usage count limit " << 1U << " missing"; |
| 6859 | |
| 6860 | string message = "1234567890123456"; |
| 6861 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6862 | |
| 6863 | // First usage of RSA key should work. |
| 6864 | SignMessage(message, params); |
| 6865 | |
| 6866 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6867 | // must be invalidated from secure storage (such as RPMB partition). |
| 6868 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6869 | } |
| 6870 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6871 | INSTANTIATE_KEYMINT_AIDL_TEST(UsageCountLimitTest); |
| 6872 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 6873 | typedef KeyMintAidlTestBase GetHardwareInfoTest; |
| 6874 | |
| 6875 | TEST_P(GetHardwareInfoTest, GetHardwareInfo) { |
| 6876 | // Retrieving hardware info should give the same result each time. |
| 6877 | KeyMintHardwareInfo info; |
| 6878 | ASSERT_TRUE(keyMint().getHardwareInfo(&info).isOk()); |
| 6879 | KeyMintHardwareInfo info2; |
| 6880 | ASSERT_TRUE(keyMint().getHardwareInfo(&info2).isOk()); |
| 6881 | EXPECT_EQ(info, info2); |
| 6882 | } |
| 6883 | |
| 6884 | INSTANTIATE_KEYMINT_AIDL_TEST(GetHardwareInfoTest); |
| 6885 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6886 | typedef KeyMintAidlTestBase AddEntropyTest; |
| 6887 | |
| 6888 | /* |
| 6889 | * AddEntropyTest.AddEntropy |
| 6890 | * |
| 6891 | * Verifies that the addRngEntropy method doesn't blow up. There's no way to test that entropy |
| 6892 | * is actually added. |
| 6893 | */ |
| 6894 | TEST_P(AddEntropyTest, AddEntropy) { |
| 6895 | string data = "foo"; |
| 6896 | EXPECT_TRUE(keyMint().addRngEntropy(vector<uint8_t>(data.begin(), data.end())).isOk()); |
| 6897 | } |
| 6898 | |
| 6899 | /* |
| 6900 | * AddEntropyTest.AddEmptyEntropy |
| 6901 | * |
| 6902 | * Verifies that the addRngEntropy method doesn't blow up when given an empty buffer. |
| 6903 | */ |
| 6904 | TEST_P(AddEntropyTest, AddEmptyEntropy) { |
| 6905 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf()).isOk()); |
| 6906 | } |
| 6907 | |
| 6908 | /* |
| 6909 | * AddEntropyTest.AddLargeEntropy |
| 6910 | * |
| 6911 | * Verifies that the addRngEntropy method doesn't blow up when given a largish amount of data. |
| 6912 | */ |
| 6913 | TEST_P(AddEntropyTest, AddLargeEntropy) { |
| 6914 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf(string(2 * 1024, 'a'))).isOk()); |
| 6915 | } |
| 6916 | |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 6917 | /* |
| 6918 | * AddEntropyTest.AddTooLargeEntropy |
| 6919 | * |
| 6920 | * Verifies that the addRngEntropy method rejects more than 2KiB of data. |
| 6921 | */ |
| 6922 | TEST_P(AddEntropyTest, AddTooLargeEntropy) { |
| 6923 | ErrorCode rc = GetReturnErrorCode(keyMint().addRngEntropy(AidlBuf(string(2 * 1024 + 1, 'a')))); |
| 6924 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, rc); |
| 6925 | } |
| 6926 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6927 | INSTANTIATE_KEYMINT_AIDL_TEST(AddEntropyTest); |
| 6928 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6929 | typedef KeyMintAidlTestBase KeyDeletionTest; |
| 6930 | |
| 6931 | /** |
| 6932 | * KeyDeletionTest.DeleteKey |
| 6933 | * |
| 6934 | * This test checks that if rollback protection is implemented, DeleteKey invalidates a formerly |
| 6935 | * valid key blob. |
| 6936 | */ |
| 6937 | TEST_P(KeyDeletionTest, DeleteKey) { |
| 6938 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6939 | .RsaSigningKey(2048, 65537) |
| 6940 | .Digest(Digest::NONE) |
| 6941 | .Padding(PaddingMode::NONE) |
| 6942 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6943 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6944 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6945 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6946 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 6947 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6948 | |
| 6949 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6950 | ASSERT_EQ(ErrorCode::OK, error); |
| 6951 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 6952 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6953 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6954 | ASSERT_EQ(ErrorCode::OK, DeleteKey(true /* keep key blob */)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6955 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6956 | string message = "12345678901234567890123456789012"; |
| 6957 | AuthorizationSet begin_out_params; |
| 6958 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 6959 | Begin(KeyPurpose::SIGN, key_blob_, |
| 6960 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 6961 | &begin_out_params)); |
| 6962 | AbortIfNeeded(); |
| 6963 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6964 | } |
| 6965 | |
| 6966 | /** |
| 6967 | * KeyDeletionTest.DeleteInvalidKey |
| 6968 | * |
| 6969 | * This test checks that the HAL excepts invalid key blobs.. |
| 6970 | */ |
| 6971 | TEST_P(KeyDeletionTest, DeleteInvalidKey) { |
| 6972 | // Generate key just to check if rollback protection is implemented |
| 6973 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6974 | .RsaSigningKey(2048, 65537) |
| 6975 | .Digest(Digest::NONE) |
| 6976 | .Padding(PaddingMode::NONE) |
| 6977 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6978 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6979 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6980 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6981 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 6982 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6983 | |
| 6984 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6985 | ASSERT_EQ(ErrorCode::OK, error); |
| 6986 | AuthorizationSet enforced(SecLevelAuthorizations()); |
| 6987 | ASSERT_TRUE(enforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6988 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6989 | // Delete the key we don't care about the result at this point. |
| 6990 | DeleteKey(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6991 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6992 | // Now create an invalid key blob and delete it. |
| 6993 | 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] | 6994 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6995 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6996 | } |
| 6997 | |
| 6998 | /** |
| 6999 | * KeyDeletionTest.DeleteAllKeys |
| 7000 | * |
| 7001 | * This test is disarmed by default. To arm it use --arm_deleteAllKeys. |
| 7002 | * |
| 7003 | * BEWARE: This test has serious side effects. All user keys will be lost! This includes |
| 7004 | * FBE/FDE encryption keys, which means that the device will not even boot until after the |
| 7005 | * device has been wiped manually (e.g., fastboot flashall -w), and new FBE/FDE keys have |
| 7006 | * been provisioned. Use this test only on dedicated testing devices that have no valuable |
| 7007 | * credentials stored in Keystore/Keymint. |
| 7008 | */ |
| 7009 | TEST_P(KeyDeletionTest, DeleteAllKeys) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7010 | if (!arm_deleteAllKeys) { |
| 7011 | GTEST_SKIP() << "Option --arm_deleteAllKeys not set"; |
| 7012 | return; |
| 7013 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7014 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 7015 | .RsaSigningKey(2048, 65537) |
| 7016 | .Digest(Digest::NONE) |
| 7017 | .Padding(PaddingMode::NONE) |
| 7018 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 7019 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 7020 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7021 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 7022 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 7023 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7024 | |
| 7025 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7026 | ASSERT_EQ(ErrorCode::OK, error); |
| 7027 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 7028 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7029 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7030 | ASSERT_EQ(ErrorCode::OK, DeleteAllKeys()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7031 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7032 | string message = "12345678901234567890123456789012"; |
| 7033 | AuthorizationSet begin_out_params; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7034 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 7035 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 7036 | Begin(KeyPurpose::SIGN, key_blob_, |
| 7037 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 7038 | &begin_out_params)); |
| 7039 | AbortIfNeeded(); |
| 7040 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyDeletionTest); |
| 7044 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7045 | typedef KeyMintAidlTestBase KeyUpgradeTest; |
| 7046 | |
| 7047 | /** |
| 7048 | * KeyUpgradeTest.UpgradeInvalidKey |
| 7049 | * |
| 7050 | * This test checks that the HAL excepts invalid key blobs.. |
| 7051 | */ |
| 7052 | TEST_P(KeyUpgradeTest, UpgradeInvalidKey) { |
| 7053 | AidlBuf key_blob = AidlBuf("just some garbage data which is not a valid key blob"); |
| 7054 | |
| 7055 | std::vector<uint8_t> new_blob; |
| 7056 | Status result = keymint_->upgradeKey(key_blob, |
| 7057 | AuthorizationSetBuilder() |
| 7058 | .Authorization(TAG_APPLICATION_ID, "clientid") |
| 7059 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 7060 | .vector_data(), |
| 7061 | &new_blob); |
| 7062 | ASSERT_EQ(ErrorCode::INVALID_KEY_BLOB, GetReturnErrorCode(result)); |
| 7063 | } |
| 7064 | |
| 7065 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyUpgradeTest); |
| 7066 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7067 | using UpgradeKeyTest = KeyMintAidlTestBase; |
| 7068 | |
| 7069 | /* |
| 7070 | * UpgradeKeyTest.UpgradeKey |
| 7071 | * |
| 7072 | * Verifies that calling upgrade key on an up-to-date key works (i.e. does nothing). |
| 7073 | */ |
| 7074 | TEST_P(UpgradeKeyTest, UpgradeKey) { |
| 7075 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7076 | .AesEncryptionKey(128) |
| 7077 | .Padding(PaddingMode::NONE) |
| 7078 | .Authorization(TAG_NO_AUTH_REQUIRED))); |
| 7079 | |
| 7080 | auto result = UpgradeKey(key_blob_); |
| 7081 | |
| 7082 | // Key doesn't need upgrading. Should get okay, but no new key blob. |
| 7083 | EXPECT_EQ(result, std::make_pair(ErrorCode::OK, vector<uint8_t>())); |
| 7084 | } |
| 7085 | |
| 7086 | INSTANTIATE_KEYMINT_AIDL_TEST(UpgradeKeyTest); |
| 7087 | |
| 7088 | using ClearOperationsTest = KeyMintAidlTestBase; |
| 7089 | |
| 7090 | /* |
| 7091 | * ClearSlotsTest.TooManyOperations |
| 7092 | * |
| 7093 | * Verifies that TOO_MANY_OPERATIONS is returned after the max number of |
| 7094 | * operations are started without being finished or aborted. Also verifies |
| 7095 | * that aborting the operations clears the operations. |
| 7096 | * |
| 7097 | */ |
| 7098 | TEST_P(ClearOperationsTest, TooManyOperations) { |
| 7099 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7100 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7101 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 7102 | .Padding(PaddingMode::NONE) |
| 7103 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7104 | |
| 7105 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 7106 | constexpr size_t max_operations = 100; // set to arbituary large number |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 7107 | std::shared_ptr<IKeyMintOperation> op_handles[max_operations]; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7108 | AuthorizationSet out_params; |
| 7109 | ErrorCode result; |
| 7110 | size_t i; |
| 7111 | |
| 7112 | for (i = 0; i < max_operations; i++) { |
| 7113 | result = Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params, op_handles[i]); |
| 7114 | if (ErrorCode::OK != result) { |
| 7115 | break; |
| 7116 | } |
| 7117 | } |
| 7118 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, result); |
| 7119 | // Try again just in case there's a weird overflow bug |
| 7120 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, |
| 7121 | Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params)); |
| 7122 | for (size_t j = 0; j < i; j++) { |
| 7123 | EXPECT_EQ(ErrorCode::OK, Abort(op_handles[j])) |
| 7124 | << "Aboort failed for i = " << j << std::endl; |
| 7125 | } |
| 7126 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params)); |
| 7127 | AbortIfNeeded(); |
| 7128 | } |
| 7129 | |
| 7130 | INSTANTIATE_KEYMINT_AIDL_TEST(ClearOperationsTest); |
| 7131 | |
| 7132 | typedef KeyMintAidlTestBase TransportLimitTest; |
| 7133 | |
| 7134 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 7135 | * TransportLimitTest.LargeFinishInput |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7136 | * |
| 7137 | * Verifies that passing input data to finish succeeds as expected. |
| 7138 | */ |
| 7139 | TEST_P(TransportLimitTest, LargeFinishInput) { |
| 7140 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7141 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7142 | .AesEncryptionKey(128) |
| 7143 | .BlockMode(BlockMode::ECB) |
| 7144 | .Padding(PaddingMode::NONE))); |
| 7145 | |
| 7146 | for (int msg_size = 8 /* 256 bytes */; msg_size <= 11 /* 2 KiB */; msg_size++) { |
| 7147 | auto cipher_params = |
| 7148 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 7149 | |
| 7150 | AuthorizationSet out_params; |
| 7151 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, cipher_params, &out_params)); |
| 7152 | |
| 7153 | string plain_message = std::string(1 << msg_size, 'x'); |
| 7154 | string encrypted_message; |
| 7155 | auto rc = Finish(plain_message, &encrypted_message); |
| 7156 | |
| 7157 | EXPECT_EQ(ErrorCode::OK, rc); |
| 7158 | EXPECT_EQ(plain_message.size(), encrypted_message.size()) |
| 7159 | << "Encrypt finish returned OK, but did not consume all of the given input"; |
| 7160 | cipher_params.push_back(out_params); |
| 7161 | |
| 7162 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, cipher_params)); |
| 7163 | |
| 7164 | string decrypted_message; |
| 7165 | rc = Finish(encrypted_message, &decrypted_message); |
| 7166 | EXPECT_EQ(ErrorCode::OK, rc); |
| 7167 | EXPECT_EQ(plain_message.size(), decrypted_message.size()) |
| 7168 | << "Decrypt finish returned OK, did not consume all of the given input"; |
| 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | INSTANTIATE_KEYMINT_AIDL_TEST(TransportLimitTest); |
| 7173 | |
Seth Moore | d79a0ec | 2021-12-13 20:03:33 +0000 | [diff] [blame] | 7174 | static int EcdhCurveToOpenSslCurveName(EcCurve curve) { |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7175 | switch (curve) { |
| 7176 | case EcCurve::P_224: |
| 7177 | return NID_secp224r1; |
| 7178 | case EcCurve::P_256: |
| 7179 | return NID_X9_62_prime256v1; |
| 7180 | case EcCurve::P_384: |
| 7181 | return NID_secp384r1; |
| 7182 | case EcCurve::P_521: |
| 7183 | return NID_secp521r1; |
Seth Moore | d79a0ec | 2021-12-13 20:03:33 +0000 | [diff] [blame] | 7184 | case EcCurve::CURVE_25519: |
| 7185 | return NID_X25519; |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7186 | } |
| 7187 | } |
| 7188 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7189 | class KeyAgreementTest : public KeyMintAidlTestBase { |
| 7190 | protected: |
| 7191 | void GenerateLocalEcKey(EcCurve localCurve, EVP_PKEY_Ptr* localPrivKey, |
| 7192 | std::vector<uint8_t>* localPublicKey) { |
| 7193 | // Generate EC key locally (with access to private key material) |
| 7194 | if (localCurve == EcCurve::CURVE_25519) { |
| 7195 | uint8_t privKeyData[32]; |
| 7196 | uint8_t pubKeyData[32]; |
| 7197 | X25519_keypair(pubKeyData, privKeyData); |
| 7198 | *localPublicKey = vector<uint8_t>(pubKeyData, pubKeyData + 32); |
| 7199 | *localPrivKey = EVP_PKEY_Ptr(EVP_PKEY_new_raw_private_key( |
| 7200 | EVP_PKEY_X25519, nullptr, privKeyData, sizeof(privKeyData))); |
| 7201 | } else { |
| 7202 | auto ecKey = EC_KEY_Ptr(EC_KEY_new()); |
| 7203 | int curveName = EcdhCurveToOpenSslCurveName(localCurve); |
| 7204 | auto group = EC_GROUP_Ptr(EC_GROUP_new_by_curve_name(curveName)); |
| 7205 | ASSERT_NE(group, nullptr); |
| 7206 | ASSERT_EQ(EC_KEY_set_group(ecKey.get(), group.get()), 1); |
| 7207 | ASSERT_EQ(EC_KEY_generate_key(ecKey.get()), 1); |
| 7208 | *localPrivKey = EVP_PKEY_Ptr(EVP_PKEY_new()); |
| 7209 | ASSERT_EQ(EVP_PKEY_set1_EC_KEY(localPrivKey->get(), ecKey.get()), 1); |
| 7210 | |
| 7211 | // Get encoded form of the public part of the locally generated key... |
| 7212 | unsigned char* p = nullptr; |
| 7213 | int localPublicKeySize = i2d_PUBKEY(localPrivKey->get(), &p); |
| 7214 | ASSERT_GT(localPublicKeySize, 0); |
| 7215 | *localPublicKey = |
| 7216 | vector<uint8_t>(reinterpret_cast<const uint8_t*>(p), |
| 7217 | reinterpret_cast<const uint8_t*>(p + localPublicKeySize)); |
| 7218 | OPENSSL_free(p); |
| 7219 | } |
| 7220 | } |
| 7221 | |
| 7222 | void GenerateKeyMintEcKey(EcCurve curve, EVP_PKEY_Ptr* kmPubKey) { |
| 7223 | vector<uint8_t> challenge = {0x41, 0x42}; |
| 7224 | ErrorCode result = |
| 7225 | GenerateKey(AuthorizationSetBuilder() |
| 7226 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7227 | .Authorization(TAG_EC_CURVE, curve) |
| 7228 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 7229 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 7230 | .Authorization(TAG_ATTESTATION_APPLICATION_ID, {0x61, 0x62}) |
| 7231 | .Authorization(TAG_ATTESTATION_CHALLENGE, challenge) |
| 7232 | .SetDefaultValidity()); |
| 7233 | ASSERT_EQ(ErrorCode::OK, result) << "Failed to generate key"; |
| 7234 | ASSERT_GT(cert_chain_.size(), 0); |
| 7235 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 7236 | ASSERT_NE(kmKeyCert, nullptr); |
| 7237 | // Check that keyAgreement (bit 4) is set in KeyUsage |
| 7238 | EXPECT_TRUE((X509_get_key_usage(kmKeyCert.get()) & X509v3_KU_KEY_AGREEMENT) != 0); |
| 7239 | *kmPubKey = EVP_PKEY_Ptr(X509_get_pubkey(kmKeyCert.get())); |
| 7240 | ASSERT_NE(*kmPubKey, nullptr); |
| 7241 | if (dump_Attestations) { |
| 7242 | for (size_t n = 0; n < cert_chain_.size(); n++) { |
| 7243 | std::cout << bin2hex(cert_chain_[n].encodedCertificate) << std::endl; |
| 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | |
| 7248 | void CheckAgreement(EVP_PKEY_Ptr kmPubKey, EVP_PKEY_Ptr localPrivKey, |
| 7249 | const std::vector<uint8_t>& localPublicKey) { |
| 7250 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7251 | string ZabFromKeyMintStr; |
| 7252 | ASSERT_EQ(ErrorCode::OK, |
| 7253 | Finish(string(localPublicKey.begin(), localPublicKey.end()), &ZabFromKeyMintStr)); |
| 7254 | vector<uint8_t> ZabFromKeyMint(ZabFromKeyMintStr.begin(), ZabFromKeyMintStr.end()); |
| 7255 | vector<uint8_t> ZabFromTest; |
| 7256 | |
| 7257 | if (EVP_PKEY_id(kmPubKey.get()) == EVP_PKEY_X25519) { |
| 7258 | size_t kmPubKeySize = 32; |
| 7259 | uint8_t kmPubKeyData[32]; |
| 7260 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 7261 | ASSERT_EQ(kmPubKeySize, 32); |
| 7262 | |
| 7263 | uint8_t localPrivKeyData[32]; |
| 7264 | size_t localPrivKeySize = 32; |
| 7265 | ASSERT_EQ(1, EVP_PKEY_get_raw_private_key(localPrivKey.get(), localPrivKeyData, |
| 7266 | &localPrivKeySize)); |
| 7267 | ASSERT_EQ(localPrivKeySize, 32); |
| 7268 | |
| 7269 | uint8_t sharedKey[32]; |
| 7270 | ASSERT_EQ(1, X25519(sharedKey, localPrivKeyData, kmPubKeyData)); |
| 7271 | ZabFromTest = std::vector<uint8_t>(sharedKey, sharedKey + 32); |
| 7272 | } else { |
| 7273 | // Perform local ECDH between the two keys so we can check if we get the same Zab.. |
| 7274 | auto ctx = EVP_PKEY_CTX_Ptr(EVP_PKEY_CTX_new(localPrivKey.get(), nullptr)); |
| 7275 | ASSERT_NE(ctx, nullptr); |
| 7276 | ASSERT_EQ(EVP_PKEY_derive_init(ctx.get()), 1); |
| 7277 | ASSERT_EQ(EVP_PKEY_derive_set_peer(ctx.get(), kmPubKey.get()), 1); |
| 7278 | size_t ZabFromTestLen = 0; |
| 7279 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), nullptr, &ZabFromTestLen), 1); |
| 7280 | ZabFromTest.resize(ZabFromTestLen); |
| 7281 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), ZabFromTest.data(), &ZabFromTestLen), 1); |
| 7282 | } |
| 7283 | EXPECT_EQ(ZabFromKeyMint, ZabFromTest); |
| 7284 | } |
| 7285 | }; |
| 7286 | |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7287 | /* |
| 7288 | * KeyAgreementTest.Ecdh |
| 7289 | * |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7290 | * Verifies that ECDH works for all required curves |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7291 | */ |
| 7292 | TEST_P(KeyAgreementTest, Ecdh) { |
| 7293 | // Because it's possible to use this API with keys on different curves, we |
| 7294 | // check all N^2 combinations where N is the number of supported |
| 7295 | // curves. |
| 7296 | // |
| 7297 | // This is not a big deal as N is 4 so we only do 16 runs. If we end up with a |
| 7298 | // lot more curves we can be smart about things and just pick |otherCurve| so |
| 7299 | // it's not |curve| and that way we end up with only 2*N runs |
| 7300 | // |
| 7301 | for (auto curve : ValidCurves()) { |
| 7302 | for (auto localCurve : ValidCurves()) { |
| 7303 | // Generate EC key locally (with access to private key material) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7304 | EVP_PKEY_Ptr localPrivKey; |
| 7305 | vector<uint8_t> localPublicKey; |
| 7306 | GenerateLocalEcKey(localCurve, &localPrivKey, &localPublicKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7307 | |
| 7308 | // Generate EC key in KeyMint (only access to public key material) |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7309 | EVP_PKEY_Ptr kmPubKey; |
| 7310 | GenerateKeyMintEcKey(curve, &kmPubKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7311 | |
| 7312 | // Now that we have the two keys, we ask KeyMint to perform ECDH... |
| 7313 | if (curve != localCurve) { |
| 7314 | // If the keys are using different curves KeyMint should fail with |
| 7315 | // ErrorCode:INVALID_ARGUMENT. Check that. |
| 7316 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7317 | string ZabFromKeyMintStr; |
| 7318 | EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7319 | Finish(string(localPublicKey.begin(), localPublicKey.end()), |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7320 | &ZabFromKeyMintStr)); |
| 7321 | |
| 7322 | } else { |
| 7323 | // Otherwise if the keys are using the same curve, it should work. |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7324 | CheckAgreement(std::move(kmPubKey), std::move(localPrivKey), localPublicKey); |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7325 | } |
| 7326 | |
| 7327 | CheckedDeleteKey(); |
| 7328 | } |
| 7329 | } |
| 7330 | } |
| 7331 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame^] | 7332 | /* |
| 7333 | * KeyAgreementTest.EcdhCurve25519 |
| 7334 | * |
| 7335 | * Verifies that ECDH works for curve25519. This is also covered by the general |
| 7336 | * KeyAgreementTest.Ecdh case, but is pulled out separately here because this curve was added after |
| 7337 | * KeyMint 1.0. |
| 7338 | */ |
| 7339 | TEST_P(KeyAgreementTest, EcdhCurve25519) { |
| 7340 | if (!Curve25519Supported()) { |
| 7341 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7342 | } |
| 7343 | |
| 7344 | // Generate EC key in KeyMint (only access to public key material) |
| 7345 | EcCurve curve = EcCurve::CURVE_25519; |
| 7346 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7347 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7348 | |
| 7349 | // Generate EC key on same curve locally (with access to private key material). |
| 7350 | EVP_PKEY_Ptr privKey; |
| 7351 | vector<uint8_t> encodedPublicKey; |
| 7352 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7353 | |
| 7354 | // Agree on a key between local and KeyMint and check it. |
| 7355 | CheckAgreement(std::move(kmPubKey), std::move(privKey), encodedPublicKey); |
| 7356 | |
| 7357 | CheckedDeleteKey(); |
| 7358 | } |
| 7359 | |
| 7360 | /* |
| 7361 | * KeyAgreementTest.EcdhCurve25519Imported |
| 7362 | * |
| 7363 | * Verifies that ECDH works for an imported curve25519 key. |
| 7364 | */ |
| 7365 | TEST_P(KeyAgreementTest, EcdhCurve25519Imported) { |
| 7366 | if (!Curve25519Supported()) { |
| 7367 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7368 | } |
| 7369 | |
| 7370 | // Import x25519 key into KeyMint. |
| 7371 | EcCurve curve = EcCurve::CURVE_25519; |
| 7372 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 7373 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7374 | .EcdsaKey(EcCurve::CURVE_25519) |
| 7375 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 7376 | .SetDefaultValidity(), |
| 7377 | KeyFormat::PKCS8, x25519_pkcs8_key)); |
| 7378 | ASSERT_GT(cert_chain_.size(), 0); |
| 7379 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 7380 | ASSERT_NE(kmKeyCert, nullptr); |
| 7381 | EVP_PKEY_Ptr kmPubKey(X509_get_pubkey(kmKeyCert.get())); |
| 7382 | ASSERT_NE(kmPubKey.get(), nullptr); |
| 7383 | |
| 7384 | // Expect the import to emit corresponding public key data. |
| 7385 | size_t kmPubKeySize = 32; |
| 7386 | uint8_t kmPubKeyData[32]; |
| 7387 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(kmPubKey.get(), kmPubKeyData, &kmPubKeySize)); |
| 7388 | ASSERT_EQ(kmPubKeySize, 32); |
| 7389 | EXPECT_EQ(bin2hex(std::vector<uint8_t>(kmPubKeyData, kmPubKeyData + 32)), |
| 7390 | bin2hex(std::vector<uint8_t>(x25519_pubkey.begin(), x25519_pubkey.end()))); |
| 7391 | |
| 7392 | // Generate EC key on same curve locally (with access to private key material). |
| 7393 | EVP_PKEY_Ptr privKey; |
| 7394 | vector<uint8_t> encodedPublicKey; |
| 7395 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7396 | |
| 7397 | // Agree on a key between local and KeyMint and check it. |
| 7398 | CheckAgreement(std::move(kmPubKey), std::move(privKey), encodedPublicKey); |
| 7399 | |
| 7400 | CheckedDeleteKey(); |
| 7401 | } |
| 7402 | |
| 7403 | /* |
| 7404 | * KeyAgreementTest.EcdhCurve25519InvalidSize |
| 7405 | * |
| 7406 | * Verifies that ECDH fails for curve25519 if the wrong size of public key is provided. |
| 7407 | */ |
| 7408 | TEST_P(KeyAgreementTest, EcdhCurve25519InvalidSize) { |
| 7409 | if (!Curve25519Supported()) { |
| 7410 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7411 | } |
| 7412 | |
| 7413 | // Generate EC key in KeyMint (only access to public key material) |
| 7414 | EcCurve curve = EcCurve::CURVE_25519; |
| 7415 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7416 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7417 | |
| 7418 | // Generate EC key on same curve locally (with access to private key material). |
| 7419 | EVP_PKEY_Ptr privKey; |
| 7420 | vector<uint8_t> encodedPublicKey; |
| 7421 | GenerateLocalEcKey(curve, &privKey, &encodedPublicKey); |
| 7422 | |
| 7423 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7424 | string ZabFromKeyMintStr; |
| 7425 | // Send in an incomplete public key. |
| 7426 | ASSERT_NE(ErrorCode::OK, Finish(string(encodedPublicKey.begin(), encodedPublicKey.end() - 1), |
| 7427 | &ZabFromKeyMintStr)); |
| 7428 | |
| 7429 | CheckedDeleteKey(); |
| 7430 | } |
| 7431 | |
| 7432 | /* |
| 7433 | * KeyAgreementTest.EcdhCurve25519Mismatch |
| 7434 | * |
| 7435 | * Verifies that ECDH fails between curve25519 and other curves. |
| 7436 | */ |
| 7437 | TEST_P(KeyAgreementTest, EcdhCurve25519Mismatch) { |
| 7438 | if (!Curve25519Supported()) { |
| 7439 | GTEST_SKIP() << "Test not applicable to device that is not expected to support curve 25519"; |
| 7440 | } |
| 7441 | |
| 7442 | // Generate EC key in KeyMint (only access to public key material) |
| 7443 | EcCurve curve = EcCurve::CURVE_25519; |
| 7444 | EVP_PKEY_Ptr kmPubKey = nullptr; |
| 7445 | GenerateKeyMintEcKey(curve, &kmPubKey); |
| 7446 | |
| 7447 | for (auto localCurve : ValidCurves()) { |
| 7448 | if (localCurve == curve) { |
| 7449 | continue; |
| 7450 | } |
| 7451 | // Generate EC key on a different curve locally (with access to private key material). |
| 7452 | EVP_PKEY_Ptr privKey; |
| 7453 | vector<uint8_t> encodedPublicKey; |
| 7454 | GenerateLocalEcKey(localCurve, &privKey, &encodedPublicKey); |
| 7455 | |
| 7456 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 7457 | string ZabFromKeyMintStr; |
| 7458 | EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, |
| 7459 | Finish(string(encodedPublicKey.begin(), encodedPublicKey.end()), |
| 7460 | &ZabFromKeyMintStr)); |
| 7461 | } |
| 7462 | |
| 7463 | CheckedDeleteKey(); |
| 7464 | } |
| 7465 | |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 7466 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyAgreementTest); |
| 7467 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7468 | using DestroyAttestationIdsTest = KeyMintAidlTestBase; |
| 7469 | |
| 7470 | // This is a problematic test, as it can render the device under test permanently unusable. |
| 7471 | // Re-enable and run at your own risk. |
| 7472 | TEST_P(DestroyAttestationIdsTest, DISABLED_DestroyTest) { |
| 7473 | auto result = DestroyAttestationIds(); |
| 7474 | EXPECT_TRUE(result == ErrorCode::OK || result == ErrorCode::UNIMPLEMENTED); |
| 7475 | } |
| 7476 | |
| 7477 | INSTANTIATE_KEYMINT_AIDL_TEST(DestroyAttestationIdsTest); |
| 7478 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7479 | using EarlyBootKeyTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7480 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7481 | /* |
| 7482 | * EarlyBootKeyTest.CreateEarlyBootKeys |
| 7483 | * |
| 7484 | * Verifies that creating early boot keys succeeds, even at a later stage (after boot). |
| 7485 | */ |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7486 | TEST_P(EarlyBootKeyTest, CreateEarlyBootKeys) { |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7487 | // Early boot keys can be created after early boot. |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7488 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7489 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 7490 | |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7491 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
| 7492 | ASSERT_GT(keyData.blob.size(), 0U); |
| 7493 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 7494 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 7495 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7496 | CheckedDeleteKey(&aesKeyData.blob); |
| 7497 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7498 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7499 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7500 | } |
| 7501 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7502 | /* |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 7503 | * EarlyBootKeyTest.CreateAttestedEarlyBootKey |
| 7504 | * |
| 7505 | * Verifies that creating an early boot key with attestation succeeds. |
| 7506 | */ |
| 7507 | TEST_P(EarlyBootKeyTest, CreateAttestedEarlyBootKey) { |
| 7508 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = CreateTestKeys( |
| 7509 | TAG_EARLY_BOOT_ONLY, ErrorCode::OK, [](AuthorizationSetBuilder* builder) { |
| 7510 | builder->AttestationChallenge("challenge"); |
| 7511 | builder->AttestationApplicationId("app_id"); |
| 7512 | }); |
| 7513 | |
| 7514 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
| 7515 | ASSERT_GT(keyData.blob.size(), 0U); |
| 7516 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 7517 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 7518 | } |
| 7519 | CheckedDeleteKey(&aesKeyData.blob); |
| 7520 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7521 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7522 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7523 | } |
| 7524 | |
| 7525 | /* |
| 7526 | * EarlyBootKeyTest.UseEarlyBootKeyFailure |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7527 | * |
| 7528 | * Verifies that using early boot keys at a later stage fails. |
| 7529 | */ |
| 7530 | TEST_P(EarlyBootKeyTest, UseEarlyBootKeyFailure) { |
| 7531 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 7532 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7533 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 7534 | .HmacKey(128) |
| 7535 | .Digest(Digest::SHA_2_256) |
| 7536 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 7537 | AuthorizationSet output_params; |
| 7538 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, Begin(KeyPurpose::SIGN, key_blob_, |
| 7539 | AuthorizationSetBuilder() |
| 7540 | .Digest(Digest::SHA_2_256) |
| 7541 | .Authorization(TAG_MAC_LENGTH, 256), |
| 7542 | &output_params)); |
| 7543 | } |
| 7544 | |
| 7545 | /* |
| 7546 | * EarlyBootKeyTest.ImportEarlyBootKeyFailure |
| 7547 | * |
| 7548 | * Verifies that importing early boot keys fails. |
| 7549 | */ |
| 7550 | TEST_P(EarlyBootKeyTest, ImportEarlyBootKeyFailure) { |
| 7551 | ASSERT_EQ(ErrorCode::EARLY_BOOT_ENDED, ImportKey(AuthorizationSetBuilder() |
| 7552 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 7553 | .Authorization(TAG_EARLY_BOOT_ONLY) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 7554 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 7555 | .Digest(Digest::SHA_2_256) |
| 7556 | .SetDefaultValidity(), |
| 7557 | KeyFormat::PKCS8, ec_256_key)); |
| 7558 | } |
| 7559 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7560 | // 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] | 7561 | // boot stage, which no proper Android device is by the time we can run VTS. To use this, |
| 7562 | // un-disable it and modify vold to remove the call to earlyBootEnded(). Running the test will end |
| 7563 | // early boot, so you'll have to reboot between runs. |
| 7564 | TEST_P(EarlyBootKeyTest, DISABLED_FullTest) { |
| 7565 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7566 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 7567 | // TAG_EARLY_BOOT_ONLY should be in hw-enforced. |
| 7568 | EXPECT_TRUE(HwEnforcedAuthorizations(aesKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7569 | EXPECT_TRUE( |
| 7570 | HwEnforcedAuthorizations(hmacKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7571 | EXPECT_TRUE(HwEnforcedAuthorizations(rsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7572 | EXPECT_TRUE( |
| 7573 | HwEnforcedAuthorizations(ecdsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 7574 | |
| 7575 | // Should be able to use keys, since early boot has not ended |
| 7576 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 7577 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 7578 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 7579 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7580 | |
| 7581 | // End early boot |
| 7582 | ErrorCode earlyBootResult = GetReturnErrorCode(keyMint().earlyBootEnded()); |
| 7583 | EXPECT_EQ(earlyBootResult, ErrorCode::OK); |
| 7584 | |
| 7585 | // Should not be able to use already-created keys. |
| 7586 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseAesKey(aesKeyData.blob)); |
| 7587 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseHmacKey(hmacKeyData.blob)); |
| 7588 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseRsaKey(rsaKeyData.blob)); |
| 7589 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7590 | |
| 7591 | CheckedDeleteKey(&aesKeyData.blob); |
| 7592 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7593 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7594 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7595 | |
| 7596 | // Should not be able to create new keys |
| 7597 | std::tie(aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData) = |
| 7598 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::EARLY_BOOT_ENDED); |
| 7599 | |
| 7600 | CheckedDeleteKey(&aesKeyData.blob); |
| 7601 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7602 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7603 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7604 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7605 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7606 | INSTANTIATE_KEYMINT_AIDL_TEST(EarlyBootKeyTest); |
| 7607 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7608 | using UnlockedDeviceRequiredTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7609 | |
| 7610 | // This may be a problematic test. It can't be run repeatedly without unlocking the device in |
| 7611 | // between runs... and on most test devices there are no enrolled credentials so it can't be |
| 7612 | // unlocked at all, meaning the only way to get the test to pass again on a properly-functioning |
| 7613 | // device is to reboot it. For that reason, this is disabled by default. It can be used as part of |
| 7614 | // a manual test process, which includes unlocking between runs, which is why it's included here. |
| 7615 | // Well, that and the fact that it's the only test we can do without also making calls into the |
| 7616 | // Gatekeeper HAL. We haven't written any cross-HAL tests, and don't know what all of the |
| 7617 | // implications might be, so that may or may not be a solution. |
| 7618 | TEST_P(UnlockedDeviceRequiredTest, DISABLED_KeysBecomeUnusable) { |
| 7619 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 7620 | CreateTestKeys(TAG_UNLOCKED_DEVICE_REQUIRED, ErrorCode::OK); |
| 7621 | |
| 7622 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 7623 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 7624 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 7625 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7626 | |
| 7627 | ErrorCode rc = GetReturnErrorCode( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 7628 | keyMint().deviceLocked(false /* passwordOnly */, {} /* timestampToken */)); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7629 | ASSERT_EQ(ErrorCode::OK, rc); |
| 7630 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseAesKey(aesKeyData.blob)); |
| 7631 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseHmacKey(hmacKeyData.blob)); |
| 7632 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseRsaKey(rsaKeyData.blob)); |
| 7633 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 7634 | |
| 7635 | CheckedDeleteKey(&aesKeyData.blob); |
| 7636 | CheckedDeleteKey(&hmacKeyData.blob); |
| 7637 | CheckedDeleteKey(&rsaKeyData.blob); |
| 7638 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 7639 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 7640 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 7641 | INSTANTIATE_KEYMINT_AIDL_TEST(UnlockedDeviceRequiredTest); |
| 7642 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 7643 | } // namespace aidl::android::hardware::security::keymint::test |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7644 | |
| 7645 | int main(int argc, char** argv) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7646 | std::cout << "Testing "; |
| 7647 | auto halInstances = |
| 7648 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase::build_params(); |
| 7649 | std::cout << "HAL instances:\n"; |
| 7650 | for (auto& entry : halInstances) { |
| 7651 | std::cout << " " << entry << '\n'; |
| 7652 | } |
| 7653 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7654 | ::testing::InitGoogleTest(&argc, argv); |
| 7655 | for (int i = 1; i < argc; ++i) { |
| 7656 | if (argv[i][0] == '-') { |
| 7657 | if (std::string(argv[i]) == "--arm_deleteAllKeys") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7658 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 7659 | arm_deleteAllKeys = true; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7660 | } |
| 7661 | if (std::string(argv[i]) == "--dump_attestations") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 7662 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 7663 | dump_Attestations = true; |
| 7664 | } else { |
| 7665 | std::cout << "NOT dumping attestations" << std::endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7666 | } |
David Drysdale | dbbbe2e | 2021-12-02 07:44:23 +0000 | [diff] [blame] | 7667 | if (std::string(argv[i]) == "--skip_boot_pl_check") { |
| 7668 | // Allow checks of BOOT_PATCHLEVEL to be disabled, so that the tests can |
| 7669 | // be run in emulated environments that don't have the normal bootloader |
| 7670 | // interactions. |
| 7671 | aidl::android::hardware::security::keymint::test::check_boot_pl = false; |
| 7672 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7673 | } |
| 7674 | } |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 7675 | return RUN_ALL_TESTS(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 7676 | } |