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 Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 25 | #include <openssl/ec.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 26 | #include <openssl/evp.h> |
| 27 | #include <openssl/mem.h> |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 28 | #include <openssl/x509v3.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 29 | |
| 30 | #include <cutils/properties.h> |
| 31 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 32 | #include <android/binder_manager.h> |
| 33 | |
| 34 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 35 | #include <aidl/android/hardware/security/keymint/KeyFormat.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 36 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 37 | #include <keymint_support/key_param_output.h> |
| 38 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 39 | |
| 40 | #include "KeyMintAidlTestBase.h" |
| 41 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 42 | using aidl::android::hardware::security::keymint::AuthorizationSet; |
| 43 | using aidl::android::hardware::security::keymint::KeyCharacteristics; |
| 44 | using aidl::android::hardware::security::keymint::KeyFormat; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 45 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 46 | namespace std { |
| 47 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 48 | using namespace aidl::android::hardware::security::keymint; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 49 | |
| 50 | template <> |
| 51 | struct std::equal_to<KeyCharacteristics> { |
| 52 | bool operator()(const KeyCharacteristics& a, const KeyCharacteristics& b) const { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 53 | if (a.securityLevel != b.securityLevel) return false; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 54 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 55 | // this isn't very efficient. Oh, well. |
| 56 | AuthorizationSet a_auths(a.authorizations); |
| 57 | AuthorizationSet b_auths(b.authorizations); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 58 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 59 | a_auths.Sort(); |
| 60 | b_auths.Sort(); |
| 61 | |
| 62 | return a_auths == b_auths; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | } // namespace std |
| 67 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 68 | namespace aidl::android::hardware::security::keymint::test { |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 69 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 70 | namespace { |
| 71 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 72 | // The maximum number of times we'll attempt to verify that corruption |
| 73 | // of an ecrypted blob results in an error. Retries are necessary as there |
| 74 | // is a small (roughly 1/256) chance that corrupting ciphertext still results |
| 75 | // in valid PKCS7 padding. |
| 76 | constexpr size_t kMaxPaddingCorruptionRetries = 8; |
| 77 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 78 | template <TagType tag_type, Tag tag, typename ValueT> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 79 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag> ttag, |
| 80 | ValueT expected_value) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 81 | auto it = std::find_if(set.begin(), set.end(), [&](const KeyParameter& param) { |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 82 | if (auto p = authorizationValue(ttag, param)) { |
| 83 | return *p == expected_value; |
| 84 | } |
| 85 | return false; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 86 | }); |
| 87 | return (it != set.end()); |
| 88 | } |
| 89 | |
| 90 | template <TagType tag_type, Tag tag> |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 91 | bool contains(const vector<KeyParameter>& set, TypedTag<tag_type, tag>) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 92 | auto it = std::find_if(set.begin(), set.end(), |
| 93 | [&](const KeyParameter& param) { return param.tag == tag; }); |
| 94 | return (it != set.end()); |
| 95 | } |
| 96 | |
| 97 | constexpr char hex_value[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 98 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 99 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 100 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9' |
| 101 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F' |
| 102 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 103 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f' |
| 104 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 105 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 106 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 107 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 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 | |
| 114 | string hex2str(string a) { |
| 115 | string b; |
| 116 | size_t num = a.size() / 2; |
| 117 | b.resize(num); |
| 118 | for (size_t i = 0; i < num; i++) { |
| 119 | b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]); |
| 120 | } |
| 121 | return b; |
| 122 | } |
| 123 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 124 | string rsa_key = hex2str( |
| 125 | // RFC 5208 s5 |
| 126 | "30820275" // SEQUENCE length 0x275 (PrivateKeyInfo) { |
| 127 | "020100" // INTEGER length 1 value 0x00 (version) |
| 128 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 129 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 130 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 131 | "0500" // NULL (parameters) |
| 132 | // } end SEQUENCE (AlgorithmIdentifier) |
| 133 | "0482025f" // OCTET STRING length 0x25f (privateKey) holding... |
| 134 | // RFC 8017 A.1.2 |
| 135 | "3082025b" // SEQUENCE length 0x25b (RSAPrivateKey) { |
| 136 | "020100" // INTEGER length 1 value 0x00 (version) |
| 137 | "028181" // INTEGER length 0x81 value (modulus) ... |
| 138 | "00c6095409047d8634812d5a218176e4" |
| 139 | "5c41d60a75b13901f234226cffe77652" |
| 140 | "1c5a77b9e389417b71c0b6a44d13afe4" |
| 141 | "e4a2805d46c9da2935adb1ff0c1f24ea" |
| 142 | "06e62b20d776430a4d435157233c6f91" |
| 143 | "6783c30e310fcbd89b85c2d567711697" |
| 144 | "85ac12bca244abda72bfb19fc44d27c8" |
| 145 | "1e1d92de284f4061edfd99280745ea6d" |
| 146 | "25" |
| 147 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 148 | "028180" // INTEGER length 0x80 (privateExponent) value... |
| 149 | "1be0f04d9cae3718691f035338308e91" |
| 150 | "564b55899ffb5084d2460e6630257e05" |
| 151 | "b3ceab02972dfabcd6ce5f6ee2589eb6" |
| 152 | "7911ed0fac16e43a444b8c861e544a05" |
| 153 | "93365772f8baf6b22fc9e3c5f1024b06" |
| 154 | "3ac080a7b2234cf8aee8f6c47bbf4fd3" |
| 155 | "ace7240290bef16c0b3f7f3cdd64ce3a" |
| 156 | "b5912cf6e32f39ab188358afcccd8081" |
| 157 | "0241" // INTEGER length 0x41 (prime1) |
| 158 | "00e4b49ef50f765d3b24dde01aceaaf1" |
| 159 | "30f2c76670a91a61ae08af497b4a82be" |
| 160 | "6dee8fcdd5e3f7ba1cfb1f0c926b88f8" |
| 161 | "8c92bfab137fba2285227b83c342ff7c" |
| 162 | "55" |
| 163 | "0241" // INTEGER length 0x41 (prime2) |
| 164 | "00ddabb5839c4c7f6bf3d4183231f005" |
| 165 | "b31aa58affdda5c79e4cce217f6bc930" |
| 166 | "dbe563d480706c24e9ebfcab28a6cdef" |
| 167 | "d324b77e1bf7251b709092c24ff501fd" |
| 168 | "91" |
| 169 | "0240" // INTEGER length 0x40 (exponent1) |
| 170 | "23d4340eda3445d8cd26c14411da6fdc" |
| 171 | "a63c1ccd4b80a98ad52b78cc8ad8beb2" |
| 172 | "842c1d280405bc2f6c1bea214a1d742a" |
| 173 | "b996b35b63a82a5e470fa88dbf823cdd" |
| 174 | "0240" // INTEGER length 0x40 (exponent2) |
| 175 | "1b7b57449ad30d1518249a5f56bb9829" |
| 176 | "4d4b6ac12ffc86940497a5a5837a6cf9" |
| 177 | "46262b494526d328c11e1126380fde04" |
| 178 | "c24f916dec250892db09a6d77cdba351" |
| 179 | "0240" // INTEGER length 0x40 (coefficient) |
| 180 | "7762cd8f4d050da56bd591adb515d24d" |
| 181 | "7ccd32cca0d05f866d583514bd7324d5" |
| 182 | "f33645e8ed8b4a1cb3cc4a1d67987399" |
| 183 | "f2a09f5b3fb68c88d5e5d90ac33492d6" |
| 184 | // } end SEQUENCE (PrivateKey) |
| 185 | // } end SEQUENCE (PrivateKeyInfo) |
| 186 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 187 | |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 188 | /* |
| 189 | * DER-encoded PKCS#8 format RSA key. Generated using: |
| 190 | * |
| 191 | * openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 192 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 193 | string rsa_2048_key = hex2str( |
| 194 | // RFC 5208 s5 |
| 195 | "308204BD" // SEQUENCE length 0x4bd (PrivateKeyInfo) { |
| 196 | "020100" // INTEGER length 1 value 0x00 (version) |
| 197 | "300D" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 198 | "0609" // OBJECT IDENTIFIER length 9 (algorithm) |
| 199 | "2A864886F70D010101" // 1.2.840.113549.1.1.1 (rsaEncryption) |
| 200 | "0500" // NULL (parameters) |
| 201 | // } end SEQUENCE (AlgorithmIdentifier) |
| 202 | "048204A7" // OCTET STRING length 0x25f (privateKey) holding... |
| 203 | // RFC 8017 A.1.2 |
| 204 | "308204A3" // SEQUENCE length 0x4a3 (RSAPrivateKey) { |
| 205 | "020100" // INTEGER length 1 value 0x00 (version) |
| 206 | "02820101" // INTEGER length 0x101 value (modulus) ... |
| 207 | "00BEBC342B56D443B1299F9A6A7056E8" |
| 208 | "0A897E318476A5A18029E63B2ED739A6" |
| 209 | "1791D339F58DC763D9D14911F2EDEC38" |
| 210 | "3DEE11F6319B44510E7A3ECD9B79B973" |
| 211 | "82E49500ACF8117DC89CAF0E621F7775" |
| 212 | "6554A2FD4664BFE7AB8B59AB48340DBF" |
| 213 | "A27B93B5A81F6ECDEB02D0759307128D" |
| 214 | "F3E3BAD4055C8B840216DFAA5700670E" |
| 215 | "6C5126F0962FCB70FF308F25049164CC" |
| 216 | "F76CC2DA66A7DD9A81A714C2809D6918" |
| 217 | "6133D29D84568E892B6FFBF3199BDB14" |
| 218 | "383EE224407F190358F111A949552ABA" |
| 219 | "6714227D1BD7F6B20DD0CB88F9467B71" |
| 220 | "9339F33BFF35B3870B3F62204E4286B0" |
| 221 | "948EA348B524544B5F9838F29EE643B0" |
| 222 | "79EEF8A713B220D7806924CDF7295070" |
| 223 | "C5" |
| 224 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 225 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 226 | "69F377F35F2F584EF075353CCD1CA997" |
| 227 | "38DB3DBC7C7FF35F9366CE176DFD1B13" |
| 228 | "5AB10030344ABF5FBECF1D4659FDEF1C" |
| 229 | "0FC430834BE1BE3911951377BB3D563A" |
| 230 | "2EA9CA8F4AD9C48A8CE6FD516A735C66" |
| 231 | "2686C7B4B3C09A7B8354133E6F93F790" |
| 232 | "D59EAEB92E84C9A4339302CCE28FDF04" |
| 233 | "CCCAFA7DE3F3A827D4F6F7D38E68B0EC" |
| 234 | "6AB706645BF074A4E4090D06FB163124" |
| 235 | "365FD5EE7A20D350E9958CC30D91326E" |
| 236 | "1B292E9EF5DB408EC42DAF737D201497" |
| 237 | "04D0A678A0FB5B5446863B099228A352" |
| 238 | "D604BA8091A164D01D5AB05397C71EAD" |
| 239 | "20BE2A08FC528FE442817809C787FEE4" |
| 240 | "AB97F97B9130D022153EDC6EB6CBE7B0" |
| 241 | "F8E3473F2E901209B5DB10F93604DB01" |
| 242 | "028181" // INTEGER length 0x81 (prime1) |
| 243 | "00E83C0998214941EA4F9293F1B77E2E" |
| 244 | "99E6CF305FAF358238E126124FEAF2EB" |
| 245 | "9724B2EA7B78E6032343821A80E55D1D" |
| 246 | "88FB12D220C3F41A56142FEC85796D19" |
| 247 | "17F1E8C774F142B67D3D6E7B7E6B4383" |
| 248 | "E94DB5929089DBB346D5BDAB40CC2D96" |
| 249 | "EE0409475E175C63BF78CFD744136740" |
| 250 | "838127EA723FF3FE7FA368C1311B4A4E" |
| 251 | "05" |
| 252 | "028181" // INTEGER length 0x81 (prime2) |
| 253 | "00D240FCC0F5D7715CDE21CB2DC86EA1" |
| 254 | "46132EA3B06F61FF2AF54BF38473F59D" |
| 255 | "ADCCE32B5F4CC32DD0BA6F509347B4B5" |
| 256 | "B1B58C39F95E4798CCBB43E83D0119AC" |
| 257 | "F532F359CA743C85199F0286610E2009" |
| 258 | "97D7312917179AC9B67558773212EC96" |
| 259 | "1E8BCE7A3CC809BC5486A96E4B0E6AF3" |
| 260 | "94D94E066A0900B7B70E82A44FB30053" |
| 261 | "C1" |
| 262 | "028181" // INTEGER length 0x81 (exponent1) |
| 263 | "00AD15DA1CBD6A492B66851BA8C316D3" |
| 264 | "8AB700E2CFDDD926A658003513C54BAA" |
| 265 | "152B30021D667D20078F500F8AD3E7F3" |
| 266 | "945D74A891ED1A28EAD0FEEAEC8C14A8" |
| 267 | "E834CF46A13D1378C99D18940823CFDD" |
| 268 | "27EC5810D59339E0C34198AC638E09C8" |
| 269 | "7CBB1B634A9864AE9F4D5EB2D53514F6" |
| 270 | "7B4CAEC048C8AB849A02E397618F3271" |
| 271 | "35" |
| 272 | "028180" // INTEGER length 0x80 (exponent2) |
| 273 | "1FA2C1A5331880A92D8F3E281C617108" |
| 274 | "BF38244F16E352E69ED417C7153F9EC3" |
| 275 | "18F211839C643DCF8B4DD67CE2AC312E" |
| 276 | "95178D5D952F06B1BF779F4916924B70" |
| 277 | "F582A23F11304E02A5E7565AE22A35E7" |
| 278 | "4FECC8B6FDC93F92A1A37703E4CF0E63" |
| 279 | "783BD02EB716A7ECBBFA606B10B74D01" |
| 280 | "579522E7EF84D91FC522292108D902C1" |
| 281 | "028180" // INTEGER length 0x80 (coefficient) |
| 282 | "796FE3825F9DCC85DF22D58690065D93" |
| 283 | "898ACD65C087BEA8DA3A63BF4549B795" |
| 284 | "E2CD0E3BE08CDEBD9FCF1720D9CDC507" |
| 285 | "0D74F40DED8E1102C52152A31B6165F8" |
| 286 | "3A6722AECFCC35A493D7634664B888A0" |
| 287 | "8D3EB034F12EA28BFEE346E205D33482" |
| 288 | "7F778B16ED40872BD29FCB36536B6E93" |
| 289 | "FFB06778696B4A9D81BB0A9423E63DE5" |
| 290 | // } end SEQUENCE (PrivateKey) |
| 291 | // } end SEQUENCE (PrivateKeyInfo) |
| 292 | ); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 293 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 294 | string ec_256_key = hex2str( |
| 295 | // RFC 5208 s5 |
| 296 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 297 | "020100" // INTEGER length 1 value 0 (version) |
| 298 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 299 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 300 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 301 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 302 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 303 | // } end SEQUENCE (AlgorithmIdentifier) |
| 304 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 305 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 306 | "020101" // INTEGER length 1 value 1 (version) |
| 307 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 308 | "737c2ecd7b8d1940bf2930aa9b4ed3ff" |
| 309 | "941eed09366bc03299986481f3a4d859" |
| 310 | "a144" // TAG [1] len 0x44 (publicKey) { |
| 311 | "03420004bf85d7720d07c25461683bc6" |
| 312 | "48b4778a9a14dd8a024e3bdd8c7ddd9a" |
| 313 | "b2b528bbc7aa1b51f14ebbbb0bd0ce21" |
| 314 | "bcc41c6eb00083cf3376d11fd44949e0" |
| 315 | "b2183bfe" |
| 316 | // } end SEQUENCE (ECPrivateKey) |
| 317 | // } end SEQUENCE (PrivateKeyInfo) |
| 318 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 319 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 320 | string ec_521_key = hex2str( |
| 321 | // RFC 5208 s5 |
| 322 | "3081EE" // SEQUENCE length 0xee (PrivateKeyInfo) { |
| 323 | "020100" // INTEGER length 1 value 0 (version) |
| 324 | "3010" // SEQUENCE length 0x10 (AlgorithmIdentifier) { |
| 325 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 326 | "2A8648CE3D0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 327 | "0605" // OBJECT IDENTIFIER length 5 (param) |
| 328 | "2B81040023" // 1.3.132.0.35 (secp521r1) |
| 329 | // } end SEQUENCE (AlgorithmIdentifier) |
| 330 | "0481D6" // OCTET STRING length 0xd6 (privateKey) holding... |
| 331 | "3081D3" // SEQUENCE length 0xd3 (ECPrivateKey) |
| 332 | "020101" // INTEGER length 1 value 1 (version) |
| 333 | "0442" // OCTET STRING length 0x42 (privateKey) |
| 334 | "0011458C586DB5DAA92AFAB03F4FE46A" |
| 335 | "A9D9C3CE9A9B7A006A8384BEC4C78E8E" |
| 336 | "9D18D7D08B5BCFA0E53C75B064AD51C4" |
| 337 | "49BAE0258D54B94B1E885DED08ED4FB2" |
| 338 | "5CE9" |
| 339 | "A18189" // TAG [1] len 0x89 (publicKey) { |
| 340 | "03818600040149EC11C6DF0FA122C6A9" |
| 341 | "AFD9754A4FA9513A627CA329E349535A" |
| 342 | "5629875A8ADFBE27DCB932C051986377" |
| 343 | "108D054C28C6F39B6F2C9AF81802F9F3" |
| 344 | "26B842FF2E5F3C00AB7635CFB36157FC" |
| 345 | "0882D574A10D839C1A0C049DC5E0D775" |
| 346 | "E2EE50671A208431BB45E78E70BEFE93" |
| 347 | "0DB34818EE4D5C26259F5C6B8E28A652" |
| 348 | "950F9F88D7B4B2C9D9" |
| 349 | // } end SEQUENCE (ECPrivateKey) |
| 350 | // } end SEQUENCE (PrivateKeyInfo) |
| 351 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 352 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 353 | string ec_256_key_rfc5915 = hex2str( |
| 354 | // RFC 5208 s5 |
| 355 | "308193" // SEQUENCE length 0x93 (PrivateKeyInfo) { |
| 356 | "020100" // INTEGER length 1 value 0 (version) |
| 357 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 358 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 359 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 360 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 361 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 362 | // } end SEQUENCE (AlgorithmIdentifier) |
| 363 | "0479" // OCTET STRING length 0x79 (privateKey) holding... |
| 364 | // RFC 5915 s3 |
| 365 | "3077" // SEQUENCE length 0x77 (ECPrivateKey) |
| 366 | "020101" // INTEGER length 1 value 1 (version) |
| 367 | "0420" // OCTET STRING length 0x42 (privateKey) |
| 368 | "782370a8c8ce5537baadd04dcff079c8" |
| 369 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 370 | "a00a" // TAG [0] length 0xa (parameters) |
| 371 | "0608" // OBJECT IDENTIFIER length 8 |
| 372 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 373 | // } end TAG [0] |
| 374 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 375 | "0342" // BIT STRING length 0x42 |
| 376 | "00" // no pad bits |
| 377 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 378 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 379 | "8e68c905464666f98dad4f01573ba810" |
| 380 | "78b3428570a439ba3229fbc026c55068" |
| 381 | "2f" |
| 382 | // } end SEQUENCE (ECPrivateKey) |
| 383 | // } end SEQUENCE (PrivateKeyInfo) |
| 384 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 385 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 386 | string ec_256_key_sec1 = hex2str( |
| 387 | // RFC 5208 s5 |
| 388 | "308187" // SEQUENCE length 0x87 (PrivateKeyInfo) { |
| 389 | "020100" // INTEGER length 1 value 0 (version) |
| 390 | "3013" // SEQUENCE length 0x13 (AlgorithmIdentifier) { |
| 391 | "0607" // OBJECT IDENTIFIER length 7 (algorithm) |
| 392 | "2a8648ce3d0201" // 1.2.840.10045.2.1 (ecPublicKey) |
| 393 | "0608" // OBJECT IDENTIFIER length 8 (param) |
| 394 | "2a8648ce3d030107" // 1.2.840.10045.3.1.7 (secp256r1) |
| 395 | // } end SEQUENCE (AlgorithmIdentifier) |
| 396 | "046d" // OCTET STRING length 0x6d (privateKey) holding... |
| 397 | // SEC1-v2 C.4 |
| 398 | "306b" // SEQUENCE length 0x6b (ECPrivateKey) |
| 399 | "020101" // INTEGER length 1 value 0x01 (version) |
| 400 | "0420" // OCTET STRING length 0x20 (privateKey) |
| 401 | "782370a8c8ce5537baadd04dcff079c8" |
| 402 | "158cfa9c67b818b38e8d21c9fa750c1d" |
| 403 | "a144" // TAG [1] length 0x44 (publicKey) { |
| 404 | "0342" // BIT STRING length 0x42 |
| 405 | "00" // no pad bits |
| 406 | "04e2cc561ee701da0ad0ef0d176bb0c9" |
| 407 | "19d42e79c393fdc1bd6c4010d85cf2cf" |
| 408 | "8e68c905464666f98dad4f01573ba810" |
| 409 | "78b3428570a439ba3229fbc026c55068" |
| 410 | "2f" |
| 411 | // } end TAG [1] (publicKey) |
| 412 | // } end SEQUENCE (PrivateKeyInfo) |
| 413 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 414 | |
| 415 | struct RSA_Delete { |
| 416 | void operator()(RSA* p) { RSA_free(p); } |
| 417 | }; |
| 418 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 419 | std::string make_string(const uint8_t* data, size_t length) { |
| 420 | return std::string(reinterpret_cast<const char*>(data), length); |
| 421 | } |
| 422 | |
| 423 | template <size_t N> |
| 424 | std::string make_string(const uint8_t (&a)[N]) { |
| 425 | return make_string(a, N); |
| 426 | } |
| 427 | |
| 428 | class AidlBuf : public vector<uint8_t> { |
| 429 | typedef vector<uint8_t> super; |
| 430 | |
| 431 | public: |
| 432 | AidlBuf() {} |
| 433 | AidlBuf(const super& other) : super(other) {} |
| 434 | AidlBuf(super&& other) : super(std::move(other)) {} |
| 435 | explicit AidlBuf(const std::string& other) : AidlBuf() { *this = other; } |
| 436 | |
| 437 | AidlBuf& operator=(const super& other) { |
| 438 | super::operator=(other); |
| 439 | return *this; |
| 440 | } |
| 441 | |
| 442 | AidlBuf& operator=(super&& other) { |
| 443 | super::operator=(std::move(other)); |
| 444 | return *this; |
| 445 | } |
| 446 | |
| 447 | AidlBuf& operator=(const string& other) { |
| 448 | resize(other.size()); |
| 449 | for (size_t i = 0; i < other.size(); ++i) { |
| 450 | (*this)[i] = static_cast<uint8_t>(other[i]); |
| 451 | } |
| 452 | return *this; |
| 453 | } |
| 454 | |
| 455 | string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); } |
| 456 | }; |
| 457 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 458 | string device_suffix(const string& name) { |
| 459 | size_t pos = name.find('/'); |
| 460 | if (pos == string::npos) { |
| 461 | return name; |
| 462 | } |
| 463 | return name.substr(pos + 1); |
| 464 | } |
| 465 | |
| 466 | bool matching_rp_instance(const string& km_name, |
| 467 | std::shared_ptr<IRemotelyProvisionedComponent>* rp) { |
| 468 | string km_suffix = device_suffix(km_name); |
| 469 | |
| 470 | vector<string> rp_names = |
| 471 | ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor); |
| 472 | for (const string& rp_name : rp_names) { |
| 473 | // If the suffix of the RemotelyProvisionedComponent instance equals the suffix of the |
| 474 | // KeyMint instance, assume they match. |
| 475 | if (device_suffix(rp_name) == km_suffix && AServiceManager_isDeclared(rp_name.c_str())) { |
| 476 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(rp_name.c_str())); |
| 477 | *rp = IRemotelyProvisionedComponent::fromBinder(binder); |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | return false; |
| 482 | } |
| 483 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 484 | } // namespace |
| 485 | |
| 486 | class NewKeyGenerationTest : public KeyMintAidlTestBase { |
| 487 | protected: |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 488 | void CheckBaseParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 489 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 490 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 491 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 492 | // Check that some unexpected tags/values are NOT present. |
| 493 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 494 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void CheckSymmetricParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 498 | AuthorizationSet auths = CheckCommonParams(keyCharacteristics); |
| 499 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::ENCRYPT)); |
| 500 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 501 | |
| 502 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | AuthorizationSet CheckCommonParams(const vector<KeyCharacteristics>& keyCharacteristics) { |
| 506 | // TODO(swillden): Distinguish which params should be in which auth list. |
| 507 | AuthorizationSet auths; |
| 508 | for (auto& entry : keyCharacteristics) { |
| 509 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 510 | } |
| 511 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 512 | |
| 513 | // Verify that App data, ROT and auth timeout are NOT included. |
| 514 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 515 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 516 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 517 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 518 | // None of the tests specify CREATION_DATETIME so check that the KeyMint implementation |
| 519 | // never adds it. |
| 520 | EXPECT_FALSE(auths.Contains(TAG_CREATION_DATETIME)); |
| 521 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 522 | // Check OS details match the original hardware info. |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 523 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 524 | EXPECT_TRUE(os_ver); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 525 | EXPECT_EQ(*os_ver, os_version()); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 526 | auto os_pl = auths.GetTagValue(TAG_OS_PATCHLEVEL); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 527 | EXPECT_TRUE(os_pl); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 528 | EXPECT_EQ(*os_pl, os_patch_level()); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 529 | |
David Drysdale | f5bfa00 | 2021-09-27 17:30:41 +0100 | [diff] [blame] | 530 | // Should include vendor and boot patchlevels. |
| 531 | auto vendor_pl = auths.GetTagValue(TAG_VENDOR_PATCHLEVEL); |
| 532 | EXPECT_TRUE(vendor_pl); |
| 533 | EXPECT_EQ(*vendor_pl, vendor_patch_level()); |
| 534 | auto boot_pl = auths.GetTagValue(TAG_BOOT_PATCHLEVEL); |
| 535 | EXPECT_TRUE(boot_pl); |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 536 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 537 | return auths; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 538 | } |
| 539 | }; |
| 540 | |
| 541 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 542 | * NewKeyGenerationTest.Aes |
| 543 | * |
| 544 | * Verifies that keymint can generate all required AES key sizes, and that the resulting keys |
| 545 | * have correct characteristics. |
| 546 | */ |
| 547 | TEST_P(NewKeyGenerationTest, Aes) { |
| 548 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 549 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 550 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 551 | SCOPED_TRACE(testing::Message() |
| 552 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 553 | vector<uint8_t> key_blob; |
| 554 | vector<KeyCharacteristics> key_characteristics; |
| 555 | auto builder = AuthorizationSetBuilder() |
| 556 | .AesEncryptionKey(key_size) |
| 557 | .BlockMode(block_mode) |
| 558 | .Padding(padding_mode) |
| 559 | .SetDefaultValidity(); |
| 560 | if (block_mode == BlockMode::GCM) { |
| 561 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 562 | } |
| 563 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder, &key_blob, &key_characteristics)); |
| 564 | |
| 565 | EXPECT_GT(key_blob.size(), 0U); |
| 566 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 567 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 568 | |
| 569 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 570 | |
| 571 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::AES)); |
| 572 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 573 | << "Key size " << key_size << "missing"; |
| 574 | |
| 575 | CheckedDeleteKey(&key_blob); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * NewKeyGenerationTest.AesInvalidSize |
| 583 | * |
| 584 | * Verifies that specifying an invalid key size for AES key generation returns |
| 585 | * UNSUPPORTED_KEY_SIZE. |
| 586 | */ |
| 587 | TEST_P(NewKeyGenerationTest, AesInvalidSize) { |
| 588 | for (auto key_size : InvalidKeySizes(Algorithm::AES)) { |
| 589 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 590 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 591 | SCOPED_TRACE(testing::Message() |
| 592 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 593 | vector<uint8_t> key_blob; |
| 594 | vector<KeyCharacteristics> key_characteristics; |
| 595 | auto builder = AuthorizationSetBuilder() |
| 596 | .AesEncryptionKey(key_size) |
| 597 | .BlockMode(block_mode) |
| 598 | .Padding(padding_mode) |
| 599 | .SetDefaultValidity(); |
| 600 | if (block_mode == BlockMode::GCM) { |
| 601 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 602 | } |
| 603 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 604 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 610 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 611 | vector<uint8_t> key_blob; |
| 612 | vector<KeyCharacteristics> key_characteristics; |
| 613 | // No key size specified |
| 614 | auto builder = AuthorizationSetBuilder() |
| 615 | .Authorization(TAG_ALGORITHM, Algorithm::AES) |
| 616 | .BlockMode(block_mode) |
| 617 | .Padding(padding_mode) |
| 618 | .SetDefaultValidity(); |
| 619 | if (block_mode == BlockMode::GCM) { |
| 620 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 621 | } |
| 622 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 623 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * NewKeyGenerationTest.AesInvalidPadding |
| 630 | * |
| 631 | * Verifies that specifying an invalid padding on AES keys gives a failure |
| 632 | * somewhere along the way. |
| 633 | */ |
| 634 | TEST_P(NewKeyGenerationTest, AesInvalidPadding) { |
| 635 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 636 | for (auto block_mode : ValidBlockModes(Algorithm::AES)) { |
| 637 | for (auto padding_mode : InvalidPaddingModes(Algorithm::AES, block_mode)) { |
| 638 | SCOPED_TRACE(testing::Message() |
| 639 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 640 | auto builder = AuthorizationSetBuilder() |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 641 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 642 | .AesEncryptionKey(key_size) |
| 643 | .BlockMode(block_mode) |
| 644 | .Padding(padding_mode) |
| 645 | .SetDefaultValidity(); |
| 646 | if (block_mode == BlockMode::GCM) { |
| 647 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 648 | } |
| 649 | |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 650 | auto result = GenerateKey(builder); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 651 | if (result == ErrorCode::OK) { |
| 652 | // Key creation was OK but has generated a key that cannot be used. |
| 653 | auto params = |
| 654 | AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 655 | if (block_mode == BlockMode::GCM) { |
| 656 | params.Authorization(TAG_MAC_LENGTH, 128); |
| 657 | } |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 658 | auto result = Begin(KeyPurpose::ENCRYPT, params); |
| 659 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 660 | result == ErrorCode::INVALID_KEY_BLOB) |
| 661 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 662 | } else { |
| 663 | // The KeyMint implementation detected that the generated key |
| 664 | // is unusable. |
| 665 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, result); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * NewKeyGenerationTest.AesGcmMissingMinMac |
| 674 | * |
| 675 | * Verifies that specifying an invalid key size for AES key generation returns |
| 676 | * UNSUPPORTED_KEY_SIZE. |
| 677 | */ |
| 678 | TEST_P(NewKeyGenerationTest, AesGcmMissingMinMac) { |
| 679 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 680 | BlockMode block_mode = BlockMode::GCM; |
| 681 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 682 | SCOPED_TRACE(testing::Message() |
| 683 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 684 | vector<uint8_t> key_blob; |
| 685 | vector<KeyCharacteristics> key_characteristics; |
| 686 | // No MIN_MAC_LENGTH provided. |
| 687 | auto builder = AuthorizationSetBuilder() |
| 688 | .AesEncryptionKey(key_size) |
| 689 | .BlockMode(block_mode) |
| 690 | .Padding(padding_mode) |
| 691 | .SetDefaultValidity(); |
| 692 | EXPECT_EQ(ErrorCode::MISSING_MIN_MAC_LENGTH, |
| 693 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 699 | * NewKeyGenerationTest.AesGcmMinMacOutOfRange |
| 700 | * |
| 701 | * Verifies that specifying an invalid min MAC size for AES key generation returns |
| 702 | * UNSUPPORTED_MIN_MAC_LENGTH. |
| 703 | */ |
| 704 | TEST_P(NewKeyGenerationTest, AesGcmMinMacOutOfRange) { |
| 705 | for (size_t min_mac_len : {88, 136}) { |
| 706 | for (auto key_size : ValidKeySizes(Algorithm::AES)) { |
| 707 | BlockMode block_mode = BlockMode::GCM; |
| 708 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 709 | SCOPED_TRACE(testing::Message() |
| 710 | << "AES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 711 | vector<uint8_t> key_blob; |
| 712 | vector<KeyCharacteristics> key_characteristics; |
| 713 | auto builder = AuthorizationSetBuilder() |
| 714 | .AesEncryptionKey(key_size) |
| 715 | .BlockMode(block_mode) |
| 716 | .Padding(padding_mode) |
| 717 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_len) |
| 718 | .SetDefaultValidity(); |
| 719 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 720 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 727 | * NewKeyGenerationTest.TripleDes |
| 728 | * |
| 729 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 730 | * have correct characteristics. |
| 731 | */ |
| 732 | TEST_P(NewKeyGenerationTest, TripleDes) { |
| 733 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 734 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 735 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 736 | SCOPED_TRACE(testing::Message() |
| 737 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 738 | vector<uint8_t> key_blob; |
| 739 | vector<KeyCharacteristics> key_characteristics; |
| 740 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 741 | .TripleDesEncryptionKey(key_size) |
| 742 | .BlockMode(block_mode) |
| 743 | .Padding(padding_mode) |
| 744 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 745 | .SetDefaultValidity(), |
| 746 | &key_blob, &key_characteristics)); |
| 747 | |
| 748 | EXPECT_GT(key_blob.size(), 0U); |
| 749 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 750 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 751 | |
| 752 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 753 | |
| 754 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 755 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 756 | << "Key size " << key_size << "missing"; |
| 757 | |
| 758 | CheckedDeleteKey(&key_blob); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * NewKeyGenerationTest.TripleDesWithAttestation |
| 766 | * |
| 767 | * Verifies that keymint can generate all required 3DES key sizes, and that the resulting keys |
| 768 | * have correct characteristics. |
| 769 | * |
| 770 | * Request attestation, which doesn't help for symmetric keys (as there is no public key to |
| 771 | * put in a certificate) but which isn't an error. |
| 772 | */ |
| 773 | TEST_P(NewKeyGenerationTest, TripleDesWithAttestation) { |
| 774 | for (auto key_size : ValidKeySizes(Algorithm::TRIPLE_DES)) { |
| 775 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 776 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 777 | SCOPED_TRACE(testing::Message() |
| 778 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 779 | |
| 780 | auto challenge = "hello"; |
| 781 | auto app_id = "foo"; |
| 782 | |
| 783 | vector<uint8_t> key_blob; |
| 784 | vector<KeyCharacteristics> key_characteristics; |
| 785 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 786 | .TripleDesEncryptionKey(key_size) |
| 787 | .BlockMode(block_mode) |
| 788 | .Padding(padding_mode) |
| 789 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 790 | .AttestationChallenge(challenge) |
| 791 | .AttestationApplicationId(app_id) |
| 792 | .SetDefaultValidity(), |
| 793 | &key_blob, &key_characteristics)); |
| 794 | |
| 795 | EXPECT_GT(key_blob.size(), 0U); |
| 796 | CheckSymmetricParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 797 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 798 | |
| 799 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 800 | |
| 801 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::TRIPLE_DES)); |
| 802 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 803 | << "Key size " << key_size << "missing"; |
| 804 | |
| 805 | CheckedDeleteKey(&key_blob); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * NewKeyGenerationTest.TripleDesInvalidSize |
| 813 | * |
| 814 | * Verifies that specifying an invalid key size for 3-DES key generation returns |
| 815 | * UNSUPPORTED_KEY_SIZE. |
| 816 | */ |
| 817 | TEST_P(NewKeyGenerationTest, TripleDesInvalidSize) { |
| 818 | for (auto key_size : InvalidKeySizes(Algorithm::TRIPLE_DES)) { |
| 819 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 820 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 821 | SCOPED_TRACE(testing::Message() |
| 822 | << "3DES-" << key_size << "-" << block_mode << "-" << padding_mode); |
| 823 | vector<uint8_t> key_blob; |
| 824 | vector<KeyCharacteristics> key_characteristics; |
| 825 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 826 | GenerateKey(AuthorizationSetBuilder() |
| 827 | .TripleDesEncryptionKey(key_size) |
| 828 | .BlockMode(block_mode) |
| 829 | .Padding(padding_mode) |
| 830 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 831 | .SetDefaultValidity(), |
| 832 | &key_blob, &key_characteristics)); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // Omitting the key size fails. |
| 838 | for (auto block_mode : ValidBlockModes(Algorithm::TRIPLE_DES)) { |
| 839 | for (auto padding_mode : ValidPaddingModes(Algorithm::AES, block_mode)) { |
| 840 | SCOPED_TRACE(testing::Message() |
| 841 | << "3DES-default-" << block_mode << "-" << padding_mode); |
| 842 | vector<uint8_t> key_blob; |
| 843 | vector<KeyCharacteristics> key_characteristics; |
| 844 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 845 | GenerateKey(AuthorizationSetBuilder() |
| 846 | .Authorization(TAG_ALGORITHM, Algorithm::TRIPLE_DES) |
| 847 | .BlockMode(block_mode) |
| 848 | .Padding(padding_mode) |
| 849 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 850 | .SetDefaultValidity(), |
| 851 | &key_blob, &key_characteristics)); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 857 | * NewKeyGenerationTest.Rsa |
| 858 | * |
| 859 | * Verifies that keymint can generate all required RSA key sizes, and that the resulting keys |
| 860 | * have correct characteristics. |
| 861 | */ |
| 862 | TEST_P(NewKeyGenerationTest, Rsa) { |
| 863 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 864 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 865 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 866 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 867 | .RsaSigningKey(key_size, 65537) |
| 868 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 869 | .Padding(PaddingMode::NONE) |
| 870 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 871 | &key_blob, &key_characteristics)); |
| 872 | |
| 873 | ASSERT_GT(key_blob.size(), 0U); |
| 874 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 875 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 876 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 877 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 878 | |
| 879 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 880 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 881 | << "Key size " << key_size << "missing"; |
| 882 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 883 | |
| 884 | CheckedDeleteKey(&key_blob); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 889 | * NewKeyGenerationTest.RsaWithAttestation |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 890 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 891 | * Verifies that keymint can generate all required RSA key sizes with attestation, and that the |
| 892 | * resulting keys have correct characteristics. |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 893 | */ |
| 894 | TEST_P(NewKeyGenerationTest, RsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 895 | auto challenge = "hello"; |
| 896 | auto app_id = "foo"; |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 897 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 898 | auto subject = "cert subj 2"; |
| 899 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 900 | |
| 901 | uint64_t serial_int = 66; |
| 902 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 903 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 904 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 905 | vector<uint8_t> key_blob; |
| 906 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 907 | ASSERT_EQ(ErrorCode::OK, |
| 908 | GenerateKey(AuthorizationSetBuilder() |
| 909 | .RsaSigningKey(key_size, 65537) |
| 910 | .Digest(Digest::NONE) |
| 911 | .Padding(PaddingMode::NONE) |
| 912 | .AttestationChallenge(challenge) |
| 913 | .AttestationApplicationId(app_id) |
| 914 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 915 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 916 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 917 | .SetDefaultValidity(), |
| 918 | &key_blob, &key_characteristics)); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 919 | |
| 920 | ASSERT_GT(key_blob.size(), 0U); |
| 921 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 922 | CheckCharacteristics(key_blob, key_characteristics); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 923 | |
| 924 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 925 | |
| 926 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 927 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 928 | << "Key size " << key_size << "missing"; |
| 929 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 930 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 931 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 932 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 933 | ASSERT_GT(cert_chain_.size(), 0); |
| 934 | |
| 935 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 936 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 937 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 938 | sw_enforced, hw_enforced, SecLevel(), |
| 939 | cert_chain_[0].encodedCertificate)); |
| 940 | |
| 941 | CheckedDeleteKey(&key_blob); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /* |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 946 | * NewKeyGenerationTest.RsaWithRpkAttestation |
| 947 | * |
| 948 | * Verifies that keymint can generate all required RSA key sizes, using an attestation key |
| 949 | * that has been generated using an associate IRemotelyProvisionedComponent. |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 950 | * |
| 951 | * This test is disabled because the KeyMint specification does not require that implementations |
| 952 | * of the first version of KeyMint have to also implement IRemotelyProvisionedComponent. |
| 953 | * 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] | 954 | */ |
David Drysdale | 0fce69d | 2021-04-13 17:22:13 +0100 | [diff] [blame] | 955 | TEST_P(NewKeyGenerationTest, DISABLED_RsaWithRpkAttestation) { |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 956 | // There should be an IRemotelyProvisionedComponent instance associated with the KeyMint |
| 957 | // instance. |
| 958 | std::shared_ptr<IRemotelyProvisionedComponent> rp; |
| 959 | ASSERT_TRUE(matching_rp_instance(GetParam(), &rp)) |
| 960 | << "No IRemotelyProvisionedComponent found that matches KeyMint device " << GetParam(); |
| 961 | |
| 962 | // Generate a P-256 keypair to use as an attestation key. |
| 963 | MacedPublicKey macedPubKey; |
| 964 | std::vector<uint8_t> privateKeyBlob; |
| 965 | auto status = |
| 966 | rp->generateEcdsaP256KeyPair(/* testMode= */ false, &macedPubKey, &privateKeyBlob); |
| 967 | ASSERT_TRUE(status.isOk()); |
| 968 | vector<uint8_t> coseKeyData; |
| 969 | check_maced_pubkey(macedPubKey, /* testMode= */ false, &coseKeyData); |
| 970 | |
| 971 | AttestationKey attestation_key; |
| 972 | attestation_key.keyBlob = std::move(privateKeyBlob); |
| 973 | attestation_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 974 | |
| 975 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 976 | auto challenge = "hello"; |
| 977 | auto app_id = "foo"; |
| 978 | |
| 979 | vector<uint8_t> key_blob; |
| 980 | vector<KeyCharacteristics> key_characteristics; |
| 981 | ASSERT_EQ(ErrorCode::OK, |
| 982 | GenerateKey(AuthorizationSetBuilder() |
| 983 | .RsaSigningKey(key_size, 65537) |
| 984 | .Digest(Digest::NONE) |
| 985 | .Padding(PaddingMode::NONE) |
| 986 | .AttestationChallenge(challenge) |
| 987 | .AttestationApplicationId(app_id) |
| 988 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 989 | .SetDefaultValidity(), |
| 990 | attestation_key, &key_blob, &key_characteristics, &cert_chain_)); |
| 991 | |
| 992 | ASSERT_GT(key_blob.size(), 0U); |
| 993 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 994 | CheckCharacteristics(key_blob, key_characteristics); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 995 | |
| 996 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 997 | |
| 998 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 999 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1000 | << "Key size " << key_size << "missing"; |
| 1001 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1002 | |
| 1003 | // Attestation by itself is not valid (last entry is not self-signed). |
| 1004 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_)); |
| 1005 | |
| 1006 | // The signature over the attested key should correspond to the P256 public key. |
| 1007 | X509_Ptr key_cert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 1008 | ASSERT_TRUE(key_cert.get()); |
| 1009 | EVP_PKEY_Ptr signing_pubkey; |
| 1010 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 1011 | ASSERT_TRUE(signing_pubkey.get()); |
| 1012 | |
| 1013 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 1014 | << "Verification of attested certificate failed " |
| 1015 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
| 1016 | |
| 1017 | CheckedDeleteKey(&key_blob); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1022 | * NewKeyGenerationTest.RsaEncryptionWithAttestation |
| 1023 | * |
| 1024 | * Verifies that keymint attestation for RSA encryption keys with challenge and |
| 1025 | * app id is also successful. |
| 1026 | */ |
| 1027 | TEST_P(NewKeyGenerationTest, RsaEncryptionWithAttestation) { |
| 1028 | auto key_size = 2048; |
| 1029 | auto challenge = "hello"; |
| 1030 | auto app_id = "foo"; |
| 1031 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1032 | auto subject = "subj 2"; |
| 1033 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1034 | |
| 1035 | uint64_t serial_int = 111166; |
| 1036 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1037 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1038 | vector<uint8_t> key_blob; |
| 1039 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1040 | ASSERT_EQ(ErrorCode::OK, |
| 1041 | GenerateKey(AuthorizationSetBuilder() |
| 1042 | .RsaEncryptionKey(key_size, 65537) |
| 1043 | .Padding(PaddingMode::NONE) |
| 1044 | .AttestationChallenge(challenge) |
| 1045 | .AttestationApplicationId(app_id) |
| 1046 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1047 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1048 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1049 | .SetDefaultValidity(), |
| 1050 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1051 | |
| 1052 | ASSERT_GT(key_blob.size(), 0U); |
| 1053 | AuthorizationSet auths; |
| 1054 | for (auto& entry : key_characteristics) { |
| 1055 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1056 | } |
| 1057 | |
| 1058 | EXPECT_TRUE(auths.Contains(TAG_ORIGIN, KeyOrigin::GENERATED)); |
| 1059 | EXPECT_TRUE(auths.Contains(TAG_PURPOSE, KeyPurpose::DECRYPT)); |
| 1060 | |
| 1061 | // Verify that App data and ROT are NOT included. |
| 1062 | EXPECT_FALSE(auths.Contains(TAG_ROOT_OF_TRUST)); |
| 1063 | EXPECT_FALSE(auths.Contains(TAG_APPLICATION_DATA)); |
| 1064 | |
| 1065 | // Check that some unexpected tags/values are NOT present. |
| 1066 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::SIGN)); |
| 1067 | EXPECT_FALSE(auths.Contains(TAG_PURPOSE, KeyPurpose::VERIFY)); |
| 1068 | |
| 1069 | EXPECT_FALSE(auths.Contains(TAG_AUTH_TIMEOUT, 301U)); |
| 1070 | |
| 1071 | auto os_ver = auths.GetTagValue(TAG_OS_VERSION); |
| 1072 | ASSERT_TRUE(os_ver); |
| 1073 | EXPECT_EQ(*os_ver, os_version()); |
| 1074 | |
| 1075 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1076 | |
| 1077 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1078 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1079 | << "Key size " << key_size << "missing"; |
| 1080 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1081 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1082 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1083 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1084 | ASSERT_GT(cert_chain_.size(), 0); |
| 1085 | |
| 1086 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1087 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1088 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 1089 | sw_enforced, hw_enforced, SecLevel(), |
| 1090 | cert_chain_[0].encodedCertificate)); |
| 1091 | |
| 1092 | CheckedDeleteKey(&key_blob); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * NewKeyGenerationTest.RsaWithSelfSign |
| 1097 | * |
| 1098 | * Verifies that attesting to RSA key generation is successful, and returns |
| 1099 | * self signed certificate if no challenge is provided. And signing etc |
| 1100 | * works as expected. |
| 1101 | */ |
| 1102 | TEST_P(NewKeyGenerationTest, RsaWithSelfSign) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1103 | auto subject = "cert subj subj subj subj subj subj 22222222222222222222"; |
| 1104 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1105 | |
| 1106 | uint64_t serial_int = 0; |
| 1107 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1108 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1109 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1110 | vector<uint8_t> key_blob; |
| 1111 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1112 | ASSERT_EQ(ErrorCode::OK, |
| 1113 | GenerateKey(AuthorizationSetBuilder() |
| 1114 | .RsaSigningKey(key_size, 65537) |
| 1115 | .Digest(Digest::NONE) |
| 1116 | .Padding(PaddingMode::NONE) |
| 1117 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1118 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1119 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1120 | .SetDefaultValidity(), |
| 1121 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1122 | |
| 1123 | ASSERT_GT(key_blob.size(), 0U); |
| 1124 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1125 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1126 | |
| 1127 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1128 | |
| 1129 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1130 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1131 | << "Key size " << key_size << "missing"; |
| 1132 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1133 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1134 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1135 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1136 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1137 | |
| 1138 | CheckedDeleteKey(&key_blob); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * NewKeyGenerationTest.RsaWithAttestationMissAppId |
| 1144 | * |
| 1145 | * Verifies that attesting to RSA checks for missing app ID. |
| 1146 | */ |
| 1147 | TEST_P(NewKeyGenerationTest, RsaWithAttestationMissAppId) { |
| 1148 | auto challenge = "hello"; |
| 1149 | vector<uint8_t> key_blob; |
| 1150 | vector<KeyCharacteristics> key_characteristics; |
| 1151 | |
| 1152 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, |
| 1153 | GenerateKey(AuthorizationSetBuilder() |
| 1154 | .RsaSigningKey(2048, 65537) |
| 1155 | .Digest(Digest::NONE) |
| 1156 | .Padding(PaddingMode::NONE) |
| 1157 | .AttestationChallenge(challenge) |
| 1158 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1159 | .SetDefaultValidity(), |
| 1160 | &key_blob, &key_characteristics)); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * NewKeyGenerationTest.RsaWithAttestationAppIdIgnored |
| 1165 | * |
| 1166 | * Verifies that attesting to RSA ignores app id if challenge is missing. |
| 1167 | */ |
| 1168 | TEST_P(NewKeyGenerationTest, RsaWithAttestationAppIdIgnored) { |
| 1169 | auto key_size = 2048; |
| 1170 | auto app_id = "foo"; |
| 1171 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1172 | auto subject = "cert subj 2"; |
| 1173 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1174 | |
| 1175 | uint64_t serial_int = 1; |
| 1176 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1177 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1178 | vector<uint8_t> key_blob; |
| 1179 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1180 | ASSERT_EQ(ErrorCode::OK, |
| 1181 | GenerateKey(AuthorizationSetBuilder() |
| 1182 | .RsaSigningKey(key_size, 65537) |
| 1183 | .Digest(Digest::NONE) |
| 1184 | .Padding(PaddingMode::NONE) |
| 1185 | .AttestationApplicationId(app_id) |
| 1186 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1187 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1188 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1189 | .SetDefaultValidity(), |
| 1190 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | ASSERT_GT(key_blob.size(), 0U); |
| 1193 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1194 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1195 | |
| 1196 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1197 | |
| 1198 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1199 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1200 | << "Key size " << key_size << "missing"; |
| 1201 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1202 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1203 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1204 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1205 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1206 | |
| 1207 | CheckedDeleteKey(&key_blob); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1211 | * NewKeyGenerationTest.LimitedUsageRsa |
| 1212 | * |
| 1213 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1214 | * resulting keys have correct characteristics. |
| 1215 | */ |
| 1216 | TEST_P(NewKeyGenerationTest, LimitedUsageRsa) { |
| 1217 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1218 | vector<uint8_t> key_blob; |
| 1219 | vector<KeyCharacteristics> key_characteristics; |
| 1220 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1221 | .RsaSigningKey(key_size, 65537) |
| 1222 | .Digest(Digest::NONE) |
| 1223 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1224 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1225 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1226 | &key_blob, &key_characteristics)); |
| 1227 | |
| 1228 | ASSERT_GT(key_blob.size(), 0U); |
| 1229 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1230 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1231 | |
| 1232 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1233 | |
| 1234 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1235 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1236 | << "Key size " << key_size << "missing"; |
| 1237 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1238 | |
| 1239 | // Check the usage count limit tag appears in the authorizations. |
| 1240 | AuthorizationSet auths; |
| 1241 | for (auto& entry : key_characteristics) { |
| 1242 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1243 | } |
| 1244 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1245 | << "key usage count limit " << 1U << " missing"; |
| 1246 | |
| 1247 | CheckedDeleteKey(&key_blob); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1252 | * NewKeyGenerationTest.LimitedUsageRsaWithAttestation |
| 1253 | * |
| 1254 | * Verifies that KeyMint can generate all required RSA key sizes with limited usage, and that the |
| 1255 | * resulting keys have correct characteristics and attestation. |
| 1256 | */ |
| 1257 | TEST_P(NewKeyGenerationTest, LimitedUsageRsaWithAttestation) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1258 | auto challenge = "hello"; |
| 1259 | auto app_id = "foo"; |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1260 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1261 | auto subject = "cert subj 2"; |
| 1262 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1263 | |
| 1264 | uint64_t serial_int = 66; |
| 1265 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1266 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1267 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1268 | vector<uint8_t> key_blob; |
| 1269 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1270 | ASSERT_EQ(ErrorCode::OK, |
| 1271 | GenerateKey(AuthorizationSetBuilder() |
| 1272 | .RsaSigningKey(key_size, 65537) |
| 1273 | .Digest(Digest::NONE) |
| 1274 | .Padding(PaddingMode::NONE) |
| 1275 | .AttestationChallenge(challenge) |
| 1276 | .AttestationApplicationId(app_id) |
| 1277 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1278 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1279 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1280 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1281 | .SetDefaultValidity(), |
| 1282 | &key_blob, &key_characteristics)); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1283 | |
| 1284 | ASSERT_GT(key_blob.size(), 0U); |
| 1285 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1286 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1287 | |
| 1288 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1289 | |
| 1290 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::RSA)); |
| 1291 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1292 | << "Key size " << key_size << "missing"; |
| 1293 | EXPECT_TRUE(crypto_params.Contains(TAG_RSA_PUBLIC_EXPONENT, 65537U)); |
| 1294 | |
| 1295 | // Check the usage count limit tag appears in the authorizations. |
| 1296 | AuthorizationSet auths; |
| 1297 | for (auto& entry : key_characteristics) { |
| 1298 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1299 | } |
| 1300 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1301 | << "key usage count limit " << 1U << " missing"; |
| 1302 | |
| 1303 | // Check the usage count limit tag also appears in the attestation. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1304 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1305 | ASSERT_GT(cert_chain_.size(), 0); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1306 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1307 | |
| 1308 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1309 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1310 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 1311 | sw_enforced, hw_enforced, SecLevel(), |
| 1312 | cert_chain_[0].encodedCertificate)); |
| 1313 | |
| 1314 | CheckedDeleteKey(&key_blob); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1319 | * NewKeyGenerationTest.NoInvalidRsaSizes |
| 1320 | * |
| 1321 | * Verifies that keymint cannot generate any RSA key sizes that are designated as invalid. |
| 1322 | */ |
| 1323 | TEST_P(NewKeyGenerationTest, NoInvalidRsaSizes) { |
| 1324 | for (auto key_size : InvalidKeySizes(Algorithm::RSA)) { |
| 1325 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1326 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1327 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1328 | GenerateKey(AuthorizationSetBuilder() |
| 1329 | .RsaSigningKey(key_size, 65537) |
| 1330 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1331 | .Padding(PaddingMode::NONE) |
| 1332 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1333 | &key_blob, &key_characteristics)); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * NewKeyGenerationTest.RsaNoDefaultSize |
| 1339 | * |
| 1340 | * Verifies that failing to specify a key size for RSA key generation returns |
| 1341 | * UNSUPPORTED_KEY_SIZE. |
| 1342 | */ |
| 1343 | TEST_P(NewKeyGenerationTest, RsaNoDefaultSize) { |
| 1344 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1345 | GenerateKey(AuthorizationSetBuilder() |
| 1346 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 1347 | .Authorization(TAG_RSA_PUBLIC_EXPONENT, 3U) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1348 | .SigningKey() |
| 1349 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1353 | * NewKeyGenerationTest.RsaMissingParams |
| 1354 | * |
| 1355 | * Verifies that omitting optional tags works. |
| 1356 | */ |
| 1357 | TEST_P(NewKeyGenerationTest, RsaMissingParams) { |
| 1358 | for (auto key_size : ValidKeySizes(Algorithm::RSA)) { |
| 1359 | ASSERT_EQ(ErrorCode::OK, |
| 1360 | GenerateKey( |
| 1361 | AuthorizationSetBuilder().RsaKey(key_size, 65537).SetDefaultValidity())); |
| 1362 | CheckedDeleteKey(); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1367 | * NewKeyGenerationTest.Ecdsa |
| 1368 | * |
| 1369 | * Verifies that keymint can generate all required EC key sizes, and that the resulting keys |
| 1370 | * have correct characteristics. |
| 1371 | */ |
| 1372 | TEST_P(NewKeyGenerationTest, Ecdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1373 | for (auto curve : ValidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1374 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1375 | vector<KeyCharacteristics> key_characteristics; |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1376 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1377 | .EcdsaSigningKey(curve) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1378 | .Digest(Digest::NONE) |
| 1379 | .SetDefaultValidity(), |
| 1380 | &key_blob, &key_characteristics)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1381 | ASSERT_GT(key_blob.size(), 0U); |
| 1382 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1383 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1384 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1385 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1386 | |
| 1387 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1388 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1389 | |
| 1390 | CheckedDeleteKey(&key_blob); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1395 | * NewKeyGenerationTest.EcdsaAttestation |
| 1396 | * |
| 1397 | * Verifies that for all Ecdsa key sizes, if challenge and app id is provided, |
| 1398 | * an attestation will be generated. |
| 1399 | */ |
| 1400 | TEST_P(NewKeyGenerationTest, EcdsaAttestation) { |
| 1401 | auto challenge = "hello"; |
| 1402 | auto app_id = "foo"; |
| 1403 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1404 | auto subject = "cert subj 2"; |
| 1405 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1406 | |
| 1407 | uint64_t serial_int = 0xFFFFFFFFFFFFFFFF; |
| 1408 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1409 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1410 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1411 | vector<uint8_t> key_blob; |
| 1412 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1413 | ASSERT_EQ(ErrorCode::OK, |
| 1414 | GenerateKey(AuthorizationSetBuilder() |
| 1415 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1416 | .EcdsaSigningKey(curve) |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1417 | .Digest(Digest::NONE) |
| 1418 | .AttestationChallenge(challenge) |
| 1419 | .AttestationApplicationId(app_id) |
| 1420 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1421 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1422 | .SetDefaultValidity(), |
| 1423 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1424 | ASSERT_GT(key_blob.size(), 0U); |
| 1425 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1426 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1427 | |
| 1428 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1429 | |
| 1430 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1431 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1432 | |
| 1433 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 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); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1436 | |
| 1437 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1438 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1439 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 1440 | sw_enforced, hw_enforced, SecLevel(), |
| 1441 | cert_chain_[0].encodedCertificate)); |
| 1442 | |
| 1443 | CheckedDeleteKey(&key_blob); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1448 | * NewKeyGenerationTest.EcdsaAttestationTags |
| 1449 | * |
| 1450 | * Verifies that creation of an attested ECDSA key includes various tags in the |
| 1451 | * attestation extension. |
| 1452 | */ |
| 1453 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTags) { |
| 1454 | auto challenge = "hello"; |
| 1455 | auto app_id = "foo"; |
| 1456 | auto subject = "cert subj 2"; |
| 1457 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1458 | uint64_t serial_int = 0x1010; |
| 1459 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1460 | const AuthorizationSetBuilder base_builder = |
| 1461 | AuthorizationSetBuilder() |
| 1462 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1463 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1464 | .Digest(Digest::NONE) |
| 1465 | .AttestationChallenge(challenge) |
| 1466 | .AttestationApplicationId(app_id) |
| 1467 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1468 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1469 | .SetDefaultValidity(); |
| 1470 | |
| 1471 | // Various tags that map to fields in the attestation extension ASN.1 schema. |
| 1472 | auto extra_tags = AuthorizationSetBuilder() |
| 1473 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 1474 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 1475 | .Authorization(TAG_ACTIVE_DATETIME, 1619621648000) |
| 1476 | .Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, 1619621648000) |
| 1477 | .Authorization(TAG_USAGE_EXPIRE_DATETIME, 1619621999000) |
| 1478 | .Authorization(TAG_USAGE_COUNT_LIMIT, 42) |
| 1479 | .Authorization(TAG_AUTH_TIMEOUT, 100000) |
| 1480 | .Authorization(TAG_ALLOW_WHILE_ON_BODY) |
| 1481 | .Authorization(TAG_TRUSTED_USER_PRESENCE_REQUIRED) |
| 1482 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 1483 | .Authorization(TAG_UNLOCKED_DEVICE_REQUIRED) |
| 1484 | .Authorization(TAG_CREATION_DATETIME, 1619621648000); |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1485 | |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1486 | for (const KeyParameter& tag : extra_tags) { |
| 1487 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1488 | vector<uint8_t> key_blob; |
| 1489 | vector<KeyCharacteristics> key_characteristics; |
| 1490 | AuthorizationSetBuilder builder = base_builder; |
| 1491 | builder.push_back(tag); |
| 1492 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
| 1493 | if (result == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE && |
| 1494 | tag.tag == TAG_ROLLBACK_RESISTANCE) { |
| 1495 | continue; |
| 1496 | } |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1497 | if (result == ErrorCode::UNSUPPORTED_TAG && tag.tag == TAG_TRUSTED_USER_PRESENCE_REQUIRED) { |
| 1498 | // Tag not required to be supported by all KeyMint implementations. |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1499 | continue; |
| 1500 | } |
| 1501 | ASSERT_EQ(result, ErrorCode::OK); |
| 1502 | ASSERT_GT(key_blob.size(), 0U); |
| 1503 | |
| 1504 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1505 | ASSERT_GT(cert_chain_.size(), 0); |
| 1506 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1507 | |
| 1508 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1509 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
Seth Moore | b393b08 | 2021-07-12 14:18:28 -0700 | [diff] [blame] | 1510 | // Some tags are optional, so don't require them to be in the enforcements. |
| 1511 | 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] | 1512 | EXPECT_TRUE(hw_enforced.Contains(tag.tag) || sw_enforced.Contains(tag.tag)) |
| 1513 | << tag << " not in hw:" << hw_enforced << " nor sw:" << sw_enforced; |
| 1514 | } |
| 1515 | |
| 1516 | // Verifying the attestation record will check for the specific tag because |
| 1517 | // it's included in the authorizations. |
| 1518 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, sw_enforced, hw_enforced, |
| 1519 | SecLevel(), cert_chain_[0].encodedCertificate)); |
| 1520 | |
| 1521 | CheckedDeleteKey(&key_blob); |
| 1522 | } |
| 1523 | |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1524 | // Collection of invalid attestation ID tags. |
| 1525 | auto invalid_tags = |
| 1526 | AuthorizationSetBuilder() |
| 1527 | .Authorization(TAG_ATTESTATION_ID_BRAND, "bogus-brand") |
| 1528 | .Authorization(TAG_ATTESTATION_ID_DEVICE, "devious-device") |
| 1529 | .Authorization(TAG_ATTESTATION_ID_PRODUCT, "punctured-product") |
| 1530 | .Authorization(TAG_ATTESTATION_ID_SERIAL, "suspicious-serial") |
| 1531 | .Authorization(TAG_ATTESTATION_ID_IMEI, "invalid-imei") |
| 1532 | .Authorization(TAG_ATTESTATION_ID_MEID, "mismatching-meid") |
| 1533 | .Authorization(TAG_ATTESTATION_ID_MANUFACTURER, "malformed-manufacturer") |
| 1534 | .Authorization(TAG_ATTESTATION_ID_MODEL, "malicious-model"); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1535 | for (const KeyParameter& tag : invalid_tags) { |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1536 | SCOPED_TRACE(testing::Message() << "-incorrect-tag-" << tag); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1537 | vector<uint8_t> key_blob; |
| 1538 | vector<KeyCharacteristics> key_characteristics; |
| 1539 | AuthorizationSetBuilder builder = |
| 1540 | AuthorizationSetBuilder() |
| 1541 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1542 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1543 | .Digest(Digest::NONE) |
| 1544 | .AttestationChallenge(challenge) |
| 1545 | .AttestationApplicationId(app_id) |
| 1546 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1547 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1548 | .SetDefaultValidity(); |
| 1549 | builder.push_back(tag); |
| 1550 | ASSERT_EQ(ErrorCode::CANNOT_ATTEST_IDS, |
| 1551 | GenerateKey(builder, &key_blob, &key_characteristics)); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /* |
David Drysdale | c53b7d9 | 2021-10-11 12:35:58 +0100 | [diff] [blame] | 1556 | * NewKeyGenerationTest.EcdsaAttestationIdTags |
| 1557 | * |
| 1558 | * Verifies that creation of an attested ECDSA key includes various ID tags in the |
| 1559 | * attestation extension. |
| 1560 | */ |
| 1561 | TEST_P(NewKeyGenerationTest, EcdsaAttestationIdTags) { |
| 1562 | auto challenge = "hello"; |
| 1563 | auto app_id = "foo"; |
| 1564 | auto subject = "cert subj 2"; |
| 1565 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1566 | uint64_t serial_int = 0x1010; |
| 1567 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1568 | const AuthorizationSetBuilder base_builder = |
| 1569 | AuthorizationSetBuilder() |
| 1570 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1571 | .EcdsaSigningKey(EcCurve::P_256) |
| 1572 | .Digest(Digest::NONE) |
| 1573 | .AttestationChallenge(challenge) |
| 1574 | .AttestationApplicationId(app_id) |
| 1575 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1576 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1577 | .SetDefaultValidity(); |
| 1578 | |
| 1579 | // Various ATTESTATION_ID_* tags that map to fields in the attestation extension ASN.1 schema. |
| 1580 | auto extra_tags = AuthorizationSetBuilder(); |
| 1581 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_BRAND, "ro.product.brand"); |
| 1582 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_DEVICE, "ro.product.device"); |
| 1583 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_PRODUCT, "ro.product.name"); |
| 1584 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_SERIAL, "ro.serial"); |
| 1585 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MANUFACTURER, "ro.product.manufacturer"); |
| 1586 | add_tag_from_prop(&extra_tags, TAG_ATTESTATION_ID_MODEL, "ro.product.model"); |
| 1587 | |
| 1588 | for (const KeyParameter& tag : extra_tags) { |
| 1589 | SCOPED_TRACE(testing::Message() << "tag-" << tag); |
| 1590 | vector<uint8_t> key_blob; |
| 1591 | vector<KeyCharacteristics> key_characteristics; |
| 1592 | AuthorizationSetBuilder builder = base_builder; |
| 1593 | builder.push_back(tag); |
| 1594 | auto result = GenerateKey(builder, &key_blob, &key_characteristics); |
| 1595 | if (result == ErrorCode::CANNOT_ATTEST_IDS) { |
| 1596 | // Device ID attestation is optional; KeyMint may not support it at all. |
| 1597 | continue; |
| 1598 | } |
| 1599 | ASSERT_EQ(result, ErrorCode::OK); |
| 1600 | ASSERT_GT(key_blob.size(), 0U); |
| 1601 | |
| 1602 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1603 | ASSERT_GT(cert_chain_.size(), 0); |
| 1604 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1605 | |
| 1606 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1607 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1608 | |
| 1609 | // The attested key characteristics will not contain APPLICATION_ID_* fields (their |
| 1610 | // spec definitions all have "Must never appear in KeyCharacteristics"), but the |
| 1611 | // attestation extension should contain them, so make sure the extra tag is added. |
| 1612 | hw_enforced.push_back(tag); |
| 1613 | |
| 1614 | // Verifying the attestation record will check for the specific tag because |
| 1615 | // it's included in the authorizations. |
| 1616 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, sw_enforced, hw_enforced, |
| 1617 | SecLevel(), cert_chain_[0].encodedCertificate)); |
| 1618 | |
| 1619 | CheckedDeleteKey(&key_blob); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | /* |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1624 | * NewKeyGenerationTest.EcdsaAttestationTagNoApplicationId |
| 1625 | * |
| 1626 | * Verifies that creation of an attested ECDSA key does not include APPLICATION_ID. |
| 1627 | */ |
| 1628 | TEST_P(NewKeyGenerationTest, EcdsaAttestationTagNoApplicationId) { |
| 1629 | auto challenge = "hello"; |
| 1630 | auto attest_app_id = "foo"; |
| 1631 | auto subject = "cert subj 2"; |
| 1632 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1633 | uint64_t serial_int = 0x1010; |
| 1634 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1635 | |
| 1636 | // Earlier versions of the attestation extension schema included a slot: |
| 1637 | // applicationId [601] EXPLICIT OCTET_STRING OPTIONAL, |
| 1638 | // This should never have been included, and should never be filled in. |
| 1639 | // Generate an attested key that include APPLICATION_ID and APPLICATION_DATA, |
| 1640 | // to confirm that this field never makes it into the attestation extension. |
| 1641 | vector<uint8_t> key_blob; |
| 1642 | vector<KeyCharacteristics> key_characteristics; |
| 1643 | auto result = GenerateKey(AuthorizationSetBuilder() |
| 1644 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 1645 | .EcdsaSigningKey(EcCurve::P_256) |
| 1646 | .Digest(Digest::NONE) |
| 1647 | .AttestationChallenge(challenge) |
| 1648 | .AttestationApplicationId(attest_app_id) |
| 1649 | .Authorization(TAG_APPLICATION_ID, "client_id") |
| 1650 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 1651 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1652 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1653 | .SetDefaultValidity(), |
| 1654 | &key_blob, &key_characteristics); |
| 1655 | ASSERT_EQ(result, ErrorCode::OK); |
| 1656 | ASSERT_GT(key_blob.size(), 0U); |
| 1657 | |
| 1658 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1659 | ASSERT_GT(cert_chain_.size(), 0); |
| 1660 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, /* self_signed = */ false); |
| 1661 | |
| 1662 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1663 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1664 | EXPECT_TRUE(verify_attestation_record(challenge, attest_app_id, sw_enforced, hw_enforced, |
| 1665 | SecLevel(), cert_chain_[0].encodedCertificate)); |
| 1666 | |
| 1667 | // Check that the app id is not in the cert. |
| 1668 | string app_id = "clientid"; |
| 1669 | std::vector<uint8_t> needle(reinterpret_cast<const uint8_t*>(app_id.data()), |
| 1670 | reinterpret_cast<const uint8_t*>(app_id.data()) + app_id.size()); |
| 1671 | ASSERT_EQ(std::search(cert_chain_[0].encodedCertificate.begin(), |
| 1672 | cert_chain_[0].encodedCertificate.end(), needle.begin(), needle.end()), |
| 1673 | cert_chain_[0].encodedCertificate.end()); |
| 1674 | |
| 1675 | CheckedDeleteKey(&key_blob); |
| 1676 | } |
| 1677 | |
| 1678 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1679 | * NewKeyGenerationTest.EcdsaSelfSignAttestation |
| 1680 | * |
| 1681 | * Verifies that if no challenge is provided to an Ecdsa key generation, then |
| 1682 | * the key will generate a self signed attestation. |
| 1683 | */ |
| 1684 | TEST_P(NewKeyGenerationTest, EcdsaSelfSignAttestation) { |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1685 | auto subject = "cert subj 2"; |
| 1686 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 1687 | |
| 1688 | uint64_t serial_int = 0x123456FFF1234; |
| 1689 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 1690 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1691 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1692 | vector<uint8_t> key_blob; |
| 1693 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1694 | ASSERT_EQ(ErrorCode::OK, |
| 1695 | GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1696 | .EcdsaSigningKey(curve) |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1697 | .Digest(Digest::NONE) |
| 1698 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 1699 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 1700 | .SetDefaultValidity(), |
| 1701 | &key_blob, &key_characteristics)); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1702 | ASSERT_GT(key_blob.size(), 0U); |
| 1703 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1704 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1705 | |
| 1706 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1707 | |
| 1708 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1709 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1710 | |
| 1711 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1712 | verify_subject_and_serial(cert_chain_[0], serial_int, subject, false); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1713 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1714 | |
| 1715 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1716 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1717 | |
| 1718 | CheckedDeleteKey(&key_blob); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * NewKeyGenerationTest.EcdsaAttestationRequireAppId |
| 1724 | * |
| 1725 | * Verifies that if attestation challenge is provided to Ecdsa key generation, then |
| 1726 | * app id must also be provided or else it will fail. |
| 1727 | */ |
| 1728 | TEST_P(NewKeyGenerationTest, EcdsaAttestationRequireAppId) { |
| 1729 | auto challenge = "hello"; |
| 1730 | vector<uint8_t> key_blob; |
| 1731 | vector<KeyCharacteristics> key_characteristics; |
| 1732 | |
| 1733 | ASSERT_EQ(ErrorCode::ATTESTATION_APPLICATION_ID_MISSING, |
| 1734 | GenerateKey(AuthorizationSetBuilder() |
| 1735 | .EcdsaSigningKey(EcCurve::P_256) |
| 1736 | .Digest(Digest::NONE) |
| 1737 | .AttestationChallenge(challenge) |
| 1738 | .SetDefaultValidity(), |
| 1739 | &key_blob, &key_characteristics)); |
| 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | * NewKeyGenerationTest.EcdsaIgnoreAppId |
| 1744 | * |
| 1745 | * Verifies that if no challenge is provided to the Ecdsa key generation, then |
| 1746 | * any appid will be ignored, and keymint will generate a self sign certificate. |
| 1747 | */ |
| 1748 | TEST_P(NewKeyGenerationTest, EcdsaIgnoreAppId) { |
| 1749 | auto app_id = "foo"; |
| 1750 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1751 | for (auto curve : ValidCurves()) { |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1752 | vector<uint8_t> key_blob; |
| 1753 | vector<KeyCharacteristics> key_characteristics; |
| 1754 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1755 | .EcdsaSigningKey(curve) |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1756 | .Digest(Digest::NONE) |
| 1757 | .AttestationApplicationId(app_id) |
| 1758 | .SetDefaultValidity(), |
| 1759 | &key_blob, &key_characteristics)); |
| 1760 | |
| 1761 | ASSERT_GT(key_blob.size(), 0U); |
| 1762 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1763 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1764 | |
| 1765 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1766 | |
| 1767 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1768 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1769 | |
| 1770 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1771 | ASSERT_EQ(cert_chain_.size(), 1); |
| 1772 | |
| 1773 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1774 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1775 | |
| 1776 | CheckedDeleteKey(&key_blob); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /* |
| 1781 | * NewKeyGenerationTest.AttestationApplicationIDLengthProperlyEncoded |
| 1782 | * |
| 1783 | * Verifies that the Attestation Application ID software enforced tag has a proper length encoding. |
| 1784 | * Some implementations break strict encoding rules by encoding a length between 127 and 256 in one |
| 1785 | * byte. Proper DER encoding specifies that for lengths greater than 127, one byte should be used |
| 1786 | * to specify how many following bytes will be used to encode the length. |
| 1787 | */ |
| 1788 | TEST_P(NewKeyGenerationTest, AttestationApplicationIDLengthProperlyEncoded) { |
| 1789 | auto challenge = "hello"; |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1790 | std::vector<uint32_t> app_id_lengths{143, 258}; |
| 1791 | |
| 1792 | for (uint32_t length : app_id_lengths) { |
| 1793 | const string app_id(length, 'a'); |
| 1794 | vector<uint8_t> key_blob; |
| 1795 | vector<KeyCharacteristics> key_characteristics; |
| 1796 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1797 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1798 | .EcdsaSigningKey(EcCurve::P_256) |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1799 | .Digest(Digest::NONE) |
| 1800 | .AttestationChallenge(challenge) |
| 1801 | .AttestationApplicationId(app_id) |
| 1802 | .SetDefaultValidity(), |
| 1803 | &key_blob, &key_characteristics)); |
| 1804 | ASSERT_GT(key_blob.size(), 0U); |
| 1805 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1806 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1807 | |
| 1808 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1809 | |
| 1810 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1811 | 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] | 1812 | |
| 1813 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_)); |
| 1814 | ASSERT_GT(cert_chain_.size(), 0); |
| 1815 | |
| 1816 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 1817 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 1818 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 1819 | sw_enforced, hw_enforced, SecLevel(), |
| 1820 | cert_chain_[0].encodedCertificate)); |
| 1821 | |
| 1822 | CheckedDeleteKey(&key_blob); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1827 | * NewKeyGenerationTest.LimitedUsageEcdsa |
| 1828 | * |
| 1829 | * Verifies that KeyMint can generate all required EC key sizes with limited usage, and that the |
| 1830 | * resulting keys have correct characteristics. |
| 1831 | */ |
| 1832 | TEST_P(NewKeyGenerationTest, LimitedUsageEcdsa) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1833 | for (auto curve : ValidCurves()) { |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1834 | vector<uint8_t> key_blob; |
| 1835 | vector<KeyCharacteristics> key_characteristics; |
| 1836 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1837 | .EcdsaSigningKey(curve) |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1838 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1839 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 1840 | .SetDefaultValidity(), |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1841 | &key_blob, &key_characteristics)); |
| 1842 | |
| 1843 | ASSERT_GT(key_blob.size(), 0U); |
| 1844 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1845 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1846 | |
| 1847 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1848 | |
| 1849 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::EC)); |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1850 | EXPECT_TRUE(crypto_params.Contains(TAG_EC_CURVE, curve)) << "Curve " << curve << "missing"; |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 1851 | |
| 1852 | // Check the usage count limit tag appears in the authorizations. |
| 1853 | AuthorizationSet auths; |
| 1854 | for (auto& entry : key_characteristics) { |
| 1855 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 1856 | } |
| 1857 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 1858 | << "key usage count limit " << 1U << " missing"; |
| 1859 | |
| 1860 | CheckedDeleteKey(&key_blob); |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1865 | * NewKeyGenerationTest.EcdsaDefaultSize |
| 1866 | * |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1867 | * Verifies that failing to specify a curve for EC key generation returns |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1868 | * UNSUPPORTED_KEY_SIZE. |
| 1869 | */ |
| 1870 | TEST_P(NewKeyGenerationTest, EcdsaDefaultSize) { |
| 1871 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1872 | GenerateKey(AuthorizationSetBuilder() |
| 1873 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 1874 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1875 | .Digest(Digest::NONE) |
| 1876 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * NewKeyGenerationTest.EcdsaInvalidSize |
| 1881 | * |
| 1882 | * Verifies that specifying an invalid key size for EC key generation returns |
| 1883 | * UNSUPPORTED_KEY_SIZE. |
| 1884 | */ |
| 1885 | TEST_P(NewKeyGenerationTest, EcdsaInvalidSize) { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1886 | for (auto curve : InvalidCurves()) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1887 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1888 | vector<KeyCharacteristics> key_characteristics; |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1889 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, GenerateKey(AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1890 | .EcdsaSigningKey(curve) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1891 | .Digest(Digest::NONE) |
| 1892 | .SetDefaultValidity(), |
| 1893 | &key_blob, &key_characteristics)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1896 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 1897 | GenerateKey(AuthorizationSetBuilder() |
| 1898 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 1899 | .Authorization(TAG_KEY_SIZE, 190) |
| 1900 | .SigningKey() |
| 1901 | .Digest(Digest::NONE) |
| 1902 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* |
| 1906 | * NewKeyGenerationTest.EcdsaMismatchKeySize |
| 1907 | * |
| 1908 | * Verifies that specifying mismatched key size and curve for EC key generation returns |
| 1909 | * INVALID_ARGUMENT. |
| 1910 | */ |
| 1911 | TEST_P(NewKeyGenerationTest, EcdsaMismatchKeySize) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 1912 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1913 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 1914 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1915 | |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1916 | auto result = GenerateKey(AuthorizationSetBuilder() |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 1917 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1918 | .Authorization(TAG_KEY_SIZE, 224) |
| 1919 | .Authorization(TAG_EC_CURVE, EcCurve::P_256) |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 1920 | .SigningKey() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1921 | .Digest(Digest::NONE) |
| 1922 | .SetDefaultValidity()); |
David Drysdale | ff81928 | 2021-08-18 16:45:50 +0100 | [diff] [blame] | 1923 | ASSERT_TRUE(result == ErrorCode::INVALID_ARGUMENT); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1927 | * NewKeyGenerationTest.EcdsaAllValidCurves |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1928 | * |
| 1929 | * Verifies that keymint does not support any curve designated as unsupported. |
| 1930 | */ |
| 1931 | TEST_P(NewKeyGenerationTest, EcdsaAllValidCurves) { |
| 1932 | Digest digest; |
| 1933 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1934 | digest = Digest::SHA_2_256; |
| 1935 | } else { |
| 1936 | digest = Digest::SHA_2_512; |
| 1937 | } |
| 1938 | for (auto curve : ValidCurves()) { |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 1939 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1940 | .EcdsaSigningKey(curve) |
| 1941 | .Digest(digest) |
| 1942 | .SetDefaultValidity())) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1943 | << "Failed to generate key on curve: " << curve; |
| 1944 | CheckedDeleteKey(); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * NewKeyGenerationTest.Hmac |
| 1950 | * |
| 1951 | * Verifies that keymint supports all required digests, and that the resulting keys have correct |
| 1952 | * characteristics. |
| 1953 | */ |
| 1954 | TEST_P(NewKeyGenerationTest, Hmac) { |
| 1955 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 1956 | vector<uint8_t> key_blob; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1957 | vector<KeyCharacteristics> key_characteristics; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1958 | constexpr size_t key_size = 128; |
| 1959 | ASSERT_EQ(ErrorCode::OK, |
| 1960 | GenerateKey( |
| 1961 | AuthorizationSetBuilder().HmacKey(key_size).Digest(digest).Authorization( |
| 1962 | TAG_MIN_MAC_LENGTH, 128), |
| 1963 | &key_blob, &key_characteristics)); |
| 1964 | |
| 1965 | ASSERT_GT(key_blob.size(), 0U); |
| 1966 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 1967 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1968 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1969 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 1970 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 1971 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 1972 | << "Key size " << key_size << "missing"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1973 | |
| 1974 | CheckedDeleteKey(&key_blob); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | /* |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1979 | * NewKeyGenerationTest.HmacNoAttestation |
| 1980 | * |
| 1981 | * Verifies that for Hmac key generation, no attestation will be generated even if challenge |
| 1982 | * and app id are provided. |
| 1983 | */ |
| 1984 | TEST_P(NewKeyGenerationTest, HmacNoAttestation) { |
| 1985 | auto challenge = "hello"; |
| 1986 | auto app_id = "foo"; |
| 1987 | |
| 1988 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 1989 | vector<uint8_t> key_blob; |
| 1990 | vector<KeyCharacteristics> key_characteristics; |
| 1991 | constexpr size_t key_size = 128; |
| 1992 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 1993 | .HmacKey(key_size) |
| 1994 | .Digest(digest) |
| 1995 | .AttestationChallenge(challenge) |
| 1996 | .AttestationApplicationId(app_id) |
| 1997 | .Authorization(TAG_MIN_MAC_LENGTH, 128), |
| 1998 | &key_blob, &key_characteristics)); |
| 1999 | |
| 2000 | ASSERT_GT(key_blob.size(), 0U); |
| 2001 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2002 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2003 | CheckCharacteristics(key_blob, key_characteristics); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2004 | |
| 2005 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2006 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2007 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2008 | << "Key size " << key_size << "missing"; |
| 2009 | |
| 2010 | CheckedDeleteKey(&key_blob); |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2015 | * NewKeyGenerationTest.LimitedUsageHmac |
| 2016 | * |
| 2017 | * Verifies that KeyMint supports all required digests with limited usage Hmac, and that the |
| 2018 | * resulting keys have correct characteristics. |
| 2019 | */ |
| 2020 | TEST_P(NewKeyGenerationTest, LimitedUsageHmac) { |
| 2021 | for (auto digest : ValidDigests(false /* withNone */, true /* withMD5 */)) { |
| 2022 | vector<uint8_t> key_blob; |
| 2023 | vector<KeyCharacteristics> key_characteristics; |
| 2024 | constexpr size_t key_size = 128; |
| 2025 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2026 | .HmacKey(key_size) |
| 2027 | .Digest(digest) |
| 2028 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 2029 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1), |
| 2030 | &key_blob, &key_characteristics)); |
| 2031 | |
| 2032 | ASSERT_GT(key_blob.size(), 0U); |
| 2033 | CheckBaseParams(key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2034 | CheckCharacteristics(key_blob, key_characteristics); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 2035 | |
| 2036 | AuthorizationSet crypto_params = SecLevelAuthorizations(key_characteristics); |
| 2037 | EXPECT_TRUE(crypto_params.Contains(TAG_ALGORITHM, Algorithm::HMAC)); |
| 2038 | EXPECT_TRUE(crypto_params.Contains(TAG_KEY_SIZE, key_size)) |
| 2039 | << "Key size " << key_size << "missing"; |
| 2040 | |
| 2041 | // Check the usage count limit tag appears in the authorizations. |
| 2042 | AuthorizationSet auths; |
| 2043 | for (auto& entry : key_characteristics) { |
| 2044 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 2045 | } |
| 2046 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 2047 | << "key usage count limit " << 1U << " missing"; |
| 2048 | |
| 2049 | CheckedDeleteKey(&key_blob); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2054 | * NewKeyGenerationTest.HmacCheckKeySizes |
| 2055 | * |
| 2056 | * Verifies that keymint supports all key sizes, and rejects all invalid key sizes. |
| 2057 | */ |
| 2058 | TEST_P(NewKeyGenerationTest, HmacCheckKeySizes) { |
| 2059 | for (size_t key_size = 0; key_size <= 512; ++key_size) { |
| 2060 | if (key_size < 64 || key_size % 8 != 0) { |
| 2061 | // To keep this test from being very slow, we only test a random fraction of |
| 2062 | // non-byte key sizes. We test only ~10% of such cases. Since there are 392 of |
| 2063 | // them, we expect to run ~40 of them in each run. |
| 2064 | if (key_size % 8 == 0 || random() % 10 == 0) { |
| 2065 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2066 | GenerateKey(AuthorizationSetBuilder() |
| 2067 | .HmacKey(key_size) |
| 2068 | .Digest(Digest::SHA_2_256) |
| 2069 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2070 | << "HMAC key size " << key_size << " invalid"; |
| 2071 | } |
| 2072 | } else { |
| 2073 | EXPECT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2074 | .HmacKey(key_size) |
| 2075 | .Digest(Digest::SHA_2_256) |
| 2076 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2077 | << "Failed to generate HMAC key of size " << key_size; |
| 2078 | CheckedDeleteKey(); |
| 2079 | } |
| 2080 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2081 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2082 | // STRONGBOX devices must not support keys larger than 512 bits. |
| 2083 | size_t key_size = 520; |
| 2084 | EXPECT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 2085 | GenerateKey(AuthorizationSetBuilder() |
| 2086 | .HmacKey(key_size) |
| 2087 | .Digest(Digest::SHA_2_256) |
| 2088 | .Authorization(TAG_MIN_MAC_LENGTH, 256))) |
| 2089 | << "HMAC key size " << key_size << " unexpectedly valid"; |
| 2090 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * NewKeyGenerationTest.HmacCheckMinMacLengths |
| 2095 | * |
| 2096 | * Verifies that keymint supports all required MAC lengths and rejects all invalid lengths. This |
| 2097 | * test is probabilistic in order to keep the runtime down, but any failure prints out the |
| 2098 | * specific MAC length that failed, so reproducing a failed run will be easy. |
| 2099 | */ |
| 2100 | TEST_P(NewKeyGenerationTest, HmacCheckMinMacLengths) { |
| 2101 | for (size_t min_mac_length = 0; min_mac_length <= 256; ++min_mac_length) { |
| 2102 | if (min_mac_length < 64 || min_mac_length % 8 != 0) { |
| 2103 | // To keep this test from being very long, we only test a random fraction of |
| 2104 | // non-byte lengths. We test only ~10% of such cases. Since there are 172 of them, |
| 2105 | // we expect to run ~17 of them in each run. |
| 2106 | if (min_mac_length % 8 == 0 || random() % 10 == 0) { |
| 2107 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2108 | GenerateKey(AuthorizationSetBuilder() |
| 2109 | .HmacKey(128) |
| 2110 | .Digest(Digest::SHA_2_256) |
| 2111 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2112 | << "HMAC min mac length " << min_mac_length << " invalid."; |
| 2113 | } |
| 2114 | } else { |
| 2115 | EXPECT_EQ(ErrorCode::OK, |
| 2116 | GenerateKey(AuthorizationSetBuilder() |
| 2117 | .HmacKey(128) |
| 2118 | .Digest(Digest::SHA_2_256) |
| 2119 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2120 | << "Failed to generate HMAC key with min MAC length " << min_mac_length; |
| 2121 | CheckedDeleteKey(); |
| 2122 | } |
| 2123 | } |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2124 | |
| 2125 | // Minimum MAC length must be no more than 512 bits. |
| 2126 | size_t min_mac_length = 520; |
| 2127 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MIN_MAC_LENGTH, |
| 2128 | GenerateKey(AuthorizationSetBuilder() |
| 2129 | .HmacKey(128) |
| 2130 | .Digest(Digest::SHA_2_256) |
| 2131 | .Authorization(TAG_MIN_MAC_LENGTH, min_mac_length))) |
| 2132 | << "HMAC min mac length " << min_mac_length << " invalid."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* |
| 2136 | * NewKeyGenerationTest.HmacMultipleDigests |
| 2137 | * |
| 2138 | * Verifies that keymint rejects HMAC key generation with multiple specified digest algorithms. |
| 2139 | */ |
| 2140 | TEST_P(NewKeyGenerationTest, HmacMultipleDigests) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2141 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2142 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2143 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2144 | |
| 2145 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2146 | GenerateKey(AuthorizationSetBuilder() |
| 2147 | .HmacKey(128) |
| 2148 | .Digest(Digest::SHA1) |
| 2149 | .Digest(Digest::SHA_2_256) |
| 2150 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * NewKeyGenerationTest.HmacDigestNone |
| 2155 | * |
| 2156 | * Verifies that keymint rejects HMAC key generation with no digest or Digest::NONE |
| 2157 | */ |
| 2158 | TEST_P(NewKeyGenerationTest, HmacDigestNone) { |
| 2159 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2160 | GenerateKey(AuthorizationSetBuilder().HmacKey(128).Authorization(TAG_MIN_MAC_LENGTH, |
| 2161 | 128))); |
| 2162 | |
| 2163 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2164 | GenerateKey(AuthorizationSetBuilder() |
| 2165 | .HmacKey(128) |
| 2166 | .Digest(Digest::NONE) |
| 2167 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2168 | } |
| 2169 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 2170 | /* |
| 2171 | * NewKeyGenerationTest.AesNoAttestation |
| 2172 | * |
| 2173 | * Verifies that attestation parameters to AES keys are ignored and generateKey |
| 2174 | * will succeed. |
| 2175 | */ |
| 2176 | TEST_P(NewKeyGenerationTest, AesNoAttestation) { |
| 2177 | auto challenge = "hello"; |
| 2178 | auto app_id = "foo"; |
| 2179 | |
| 2180 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2181 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2182 | .AesEncryptionKey(128) |
| 2183 | .EcbMode() |
| 2184 | .Padding(PaddingMode::PKCS7) |
| 2185 | .AttestationChallenge(challenge) |
| 2186 | .AttestationApplicationId(app_id))); |
| 2187 | |
| 2188 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * NewKeyGenerationTest.TripleDesNoAttestation |
| 2193 | * |
| 2194 | * Verifies that attesting parameters to 3DES keys are ignored and generate key |
| 2195 | * will be successful. No attestation should be generated. |
| 2196 | */ |
| 2197 | TEST_P(NewKeyGenerationTest, TripleDesNoAttestation) { |
| 2198 | auto challenge = "hello"; |
| 2199 | auto app_id = "foo"; |
| 2200 | |
| 2201 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2202 | .TripleDesEncryptionKey(168) |
| 2203 | .BlockMode(BlockMode::ECB) |
| 2204 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2205 | .Padding(PaddingMode::NONE) |
| 2206 | .AttestationChallenge(challenge) |
| 2207 | .AttestationApplicationId(app_id))); |
| 2208 | ASSERT_EQ(cert_chain_.size(), 0); |
| 2209 | } |
| 2210 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2211 | INSTANTIATE_KEYMINT_AIDL_TEST(NewKeyGenerationTest); |
| 2212 | |
| 2213 | typedef KeyMintAidlTestBase SigningOperationsTest; |
| 2214 | |
| 2215 | /* |
| 2216 | * SigningOperationsTest.RsaSuccess |
| 2217 | * |
| 2218 | * Verifies that raw RSA signature operations succeed. |
| 2219 | */ |
| 2220 | TEST_P(SigningOperationsTest, RsaSuccess) { |
| 2221 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2222 | .RsaSigningKey(2048, 65537) |
| 2223 | .Digest(Digest::NONE) |
| 2224 | .Padding(PaddingMode::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2225 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2226 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2227 | string message = "12345678901234567890123456789012"; |
| 2228 | string signature = SignMessage( |
| 2229 | message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 2230 | LocalVerifyMessage(message, signature, |
| 2231 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * SigningOperationsTest.RsaAllPaddingsAndDigests |
| 2236 | * |
| 2237 | * Verifies RSA signature/verification for all padding modes and digests. |
| 2238 | */ |
| 2239 | TEST_P(SigningOperationsTest, RsaAllPaddingsAndDigests) { |
| 2240 | auto authorizations = AuthorizationSetBuilder() |
| 2241 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2242 | .RsaSigningKey(2048, 65537) |
| 2243 | .Digest(ValidDigests(true /* withNone */, true /* withMD5 */)) |
| 2244 | .Padding(PaddingMode::NONE) |
| 2245 | .Padding(PaddingMode::RSA_PSS) |
| 2246 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2247 | .SetDefaultValidity(); |
| 2248 | |
| 2249 | ASSERT_EQ(ErrorCode::OK, GenerateKey(authorizations)); |
| 2250 | |
| 2251 | string message(128, 'a'); |
| 2252 | string corrupt_message(message); |
| 2253 | ++corrupt_message[corrupt_message.size() / 2]; |
| 2254 | |
| 2255 | for (auto padding : |
| 2256 | {PaddingMode::NONE, PaddingMode::RSA_PSS, PaddingMode::RSA_PKCS1_1_5_SIGN}) { |
| 2257 | for (auto digest : ValidDigests(true /* withNone */, true /* withMD5 */)) { |
| 2258 | if (padding == PaddingMode::NONE && digest != Digest::NONE) { |
| 2259 | // Digesting only makes sense with padding. |
| 2260 | continue; |
| 2261 | } |
| 2262 | |
| 2263 | if (padding == PaddingMode::RSA_PSS && digest == Digest::NONE) { |
| 2264 | // PSS requires digesting. |
| 2265 | continue; |
| 2266 | } |
| 2267 | |
| 2268 | string signature = |
| 2269 | SignMessage(message, AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2270 | LocalVerifyMessage(message, signature, |
| 2271 | AuthorizationSetBuilder().Digest(digest).Padding(padding)); |
| 2272 | } |
| 2273 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | /* |
| 2277 | * SigningOperationsTest.RsaUseRequiresCorrectAppIdAppData |
| 2278 | * |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 2279 | * Verifies that using an RSA key requires the correct app data. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2280 | */ |
| 2281 | TEST_P(SigningOperationsTest, RsaUseRequiresCorrectAppIdAppData) { |
| 2282 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2283 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2284 | .RsaSigningKey(2048, 65537) |
| 2285 | .Digest(Digest::NONE) |
| 2286 | .Padding(PaddingMode::NONE) |
| 2287 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2288 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2289 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2290 | |
| 2291 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 2292 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2293 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2294 | Begin(KeyPurpose::SIGN, |
| 2295 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2296 | AbortIfNeeded(); |
| 2297 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2298 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2299 | .Digest(Digest::NONE) |
| 2300 | .Padding(PaddingMode::NONE) |
| 2301 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2302 | AbortIfNeeded(); |
| 2303 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2304 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2305 | .Digest(Digest::NONE) |
| 2306 | .Padding(PaddingMode::NONE) |
| 2307 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 2308 | AbortIfNeeded(); |
| 2309 | EXPECT_EQ(ErrorCode::OK, |
| 2310 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2311 | .Digest(Digest::NONE) |
| 2312 | .Padding(PaddingMode::NONE) |
| 2313 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2314 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2315 | AbortIfNeeded(); |
| 2316 | } |
| 2317 | |
| 2318 | /* |
| 2319 | * SigningOperationsTest.RsaPssSha256Success |
| 2320 | * |
| 2321 | * Verifies that RSA-PSS signature operations succeed. |
| 2322 | */ |
| 2323 | TEST_P(SigningOperationsTest, RsaPssSha256Success) { |
| 2324 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2325 | .RsaSigningKey(2048, 65537) |
| 2326 | .Digest(Digest::SHA_2_256) |
| 2327 | .Padding(PaddingMode::RSA_PSS) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2328 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2329 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2330 | // Use large message, which won't work without digesting. |
| 2331 | string message(1024, 'a'); |
| 2332 | string signature = SignMessage( |
| 2333 | message, |
| 2334 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS)); |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * SigningOperationsTest.RsaPaddingNoneDoesNotAllowOther |
| 2339 | * |
| 2340 | * Verifies that keymint rejects signature operations that specify a padding mode when the key |
| 2341 | * supports only unpadded operations. |
| 2342 | */ |
| 2343 | TEST_P(SigningOperationsTest, RsaPaddingNoneDoesNotAllowOther) { |
| 2344 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2345 | .RsaSigningKey(2048, 65537) |
| 2346 | .Digest(Digest::NONE) |
| 2347 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2348 | .Padding(PaddingMode::NONE) |
| 2349 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2350 | string message = "12345678901234567890123456789012"; |
| 2351 | string signature; |
| 2352 | |
| 2353 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 2354 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2355 | .Digest(Digest::NONE) |
| 2356 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * SigningOperationsTest.NoUserConfirmation |
| 2361 | * |
| 2362 | * Verifies that keymint rejects signing operations for keys with |
| 2363 | * TRUSTED_CONFIRMATION_REQUIRED and no valid confirmation token |
| 2364 | * presented. |
| 2365 | */ |
| 2366 | TEST_P(SigningOperationsTest, NoUserConfirmation) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2367 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2368 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2369 | } |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2370 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2371 | .RsaSigningKey(1024, 65537) |
| 2372 | .Digest(Digest::NONE) |
| 2373 | .Padding(PaddingMode::NONE) |
| 2374 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2375 | .Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED) |
| 2376 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2377 | |
| 2378 | const string message = "12345678901234567890123456789012"; |
| 2379 | EXPECT_EQ(ErrorCode::OK, |
| 2380 | Begin(KeyPurpose::SIGN, |
| 2381 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2382 | string signature; |
| 2383 | EXPECT_EQ(ErrorCode::NO_USER_CONFIRMATION, Finish(message, &signature)); |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * SigningOperationsTest.RsaPkcs1Sha256Success |
| 2388 | * |
| 2389 | * Verifies that digested RSA-PKCS1 signature operations succeed. |
| 2390 | */ |
| 2391 | TEST_P(SigningOperationsTest, RsaPkcs1Sha256Success) { |
| 2392 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2393 | .RsaSigningKey(2048, 65537) |
| 2394 | .Digest(Digest::SHA_2_256) |
| 2395 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2396 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2397 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2398 | string message(1024, 'a'); |
| 2399 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2400 | .Digest(Digest::SHA_2_256) |
| 2401 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * SigningOperationsTest.RsaPkcs1NoDigestSuccess |
| 2406 | * |
| 2407 | * Verifies that undigested RSA-PKCS1 signature operations succeed. |
| 2408 | */ |
| 2409 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestSuccess) { |
| 2410 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2411 | .RsaSigningKey(2048, 65537) |
| 2412 | .Digest(Digest::NONE) |
| 2413 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2414 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2415 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2416 | string message(53, 'a'); |
| 2417 | string signature = SignMessage(message, AuthorizationSetBuilder() |
| 2418 | .Digest(Digest::NONE) |
| 2419 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * SigningOperationsTest.RsaPkcs1NoDigestTooLarge |
| 2424 | * |
| 2425 | * Verifies that undigested RSA-PKCS1 signature operations fail with the correct error code when |
| 2426 | * given a too-long message. |
| 2427 | */ |
| 2428 | TEST_P(SigningOperationsTest, RsaPkcs1NoDigestTooLong) { |
| 2429 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2430 | .RsaSigningKey(2048, 65537) |
| 2431 | .Digest(Digest::NONE) |
| 2432 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2433 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2434 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2435 | string message(257, 'a'); |
| 2436 | |
| 2437 | EXPECT_EQ(ErrorCode::OK, |
| 2438 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2439 | .Digest(Digest::NONE) |
| 2440 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2441 | string signature; |
| 2442 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &signature)); |
| 2443 | } |
| 2444 | |
| 2445 | /* |
| 2446 | * SigningOperationsTest.RsaPssSha512TooSmallKey |
| 2447 | * |
| 2448 | * Verifies that undigested RSA-PSS signature operations fail with the correct error code when |
| 2449 | * used with a key that is too small for the message. |
| 2450 | * |
| 2451 | * A PSS-padded message is of length salt_size + digest_size + 16 (sizes in bits), and the |
| 2452 | * keymint specification requires that salt_size == digest_size, so the message will be |
| 2453 | * digest_size * 2 + |
| 2454 | * 16. Such a message can only be signed by a given key if the key is at least that size. This |
| 2455 | * test uses SHA512, which has a digest_size == 512, so the message size is 1040 bits, too large |
| 2456 | * for a 1024-bit key. |
| 2457 | */ |
| 2458 | TEST_P(SigningOperationsTest, RsaPssSha512TooSmallKey) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 2459 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 2460 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 2461 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2462 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2463 | .RsaSigningKey(1024, 65537) |
| 2464 | .Digest(Digest::SHA_2_512) |
| 2465 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2466 | .Padding(PaddingMode::RSA_PSS) |
| 2467 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2468 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2469 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2470 | .Digest(Digest::SHA_2_512) |
| 2471 | .Padding(PaddingMode::RSA_PSS))); |
| 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * SigningOperationsTest.RsaNoPaddingTooLong |
| 2476 | * |
| 2477 | * Verifies that raw RSA signature operations fail with the correct error code when |
| 2478 | * given a too-long message. |
| 2479 | */ |
| 2480 | TEST_P(SigningOperationsTest, RsaNoPaddingTooLong) { |
| 2481 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2482 | .RsaSigningKey(2048, 65537) |
| 2483 | .Digest(Digest::NONE) |
| 2484 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2485 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2486 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2487 | // One byte too long |
| 2488 | string message(2048 / 8 + 1, 'a'); |
| 2489 | ASSERT_EQ(ErrorCode::OK, |
| 2490 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2491 | .Digest(Digest::NONE) |
| 2492 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2493 | string result; |
| 2494 | ErrorCode finish_error_code = Finish(message, &result); |
| 2495 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2496 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2497 | |
| 2498 | // Very large message that should exceed the transfer buffer size of any reasonable TEE. |
| 2499 | message = string(128 * 1024, 'a'); |
| 2500 | ASSERT_EQ(ErrorCode::OK, |
| 2501 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2502 | .Digest(Digest::NONE) |
| 2503 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2504 | finish_error_code = Finish(message, &result); |
| 2505 | EXPECT_TRUE(finish_error_code == ErrorCode::INVALID_INPUT_LENGTH || |
| 2506 | finish_error_code == ErrorCode::INVALID_ARGUMENT); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * SigningOperationsTest.RsaAbort |
| 2511 | * |
| 2512 | * Verifies that operations can be aborted correctly. Uses an RSA signing operation for the |
| 2513 | * test, but the behavior should be algorithm and purpose-independent. |
| 2514 | */ |
| 2515 | TEST_P(SigningOperationsTest, RsaAbort) { |
| 2516 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2517 | .RsaSigningKey(2048, 65537) |
| 2518 | .Digest(Digest::NONE) |
| 2519 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2520 | .Padding(PaddingMode::NONE) |
| 2521 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2522 | |
| 2523 | ASSERT_EQ(ErrorCode::OK, |
| 2524 | Begin(KeyPurpose::SIGN, |
| 2525 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2526 | EXPECT_EQ(ErrorCode::OK, Abort()); |
| 2527 | |
| 2528 | // Another abort should fail |
| 2529 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 2530 | |
| 2531 | // Set to sentinel, so TearDown() doesn't try to abort again. |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 2532 | op_.reset(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2536 | * SigningOperationsTest.RsaNonUniqueParams |
| 2537 | * |
| 2538 | * Verifies that an operation with multiple padding modes is rejected. |
| 2539 | */ |
| 2540 | TEST_P(SigningOperationsTest, RsaNonUniqueParams) { |
| 2541 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2542 | .RsaSigningKey(2048, 65537) |
| 2543 | .Digest(Digest::NONE) |
| 2544 | .Digest(Digest::SHA1) |
| 2545 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2546 | .Padding(PaddingMode::NONE) |
| 2547 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 2548 | .SetDefaultValidity())); |
| 2549 | |
| 2550 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2551 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2552 | .Digest(Digest::NONE) |
| 2553 | .Padding(PaddingMode::NONE) |
| 2554 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2555 | |
Tommy Chiu | c93c439 | 2021-05-11 18:36:50 +0800 | [diff] [blame] | 2556 | auto result = Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2557 | .Digest(Digest::NONE) |
| 2558 | .Digest(Digest::SHA1) |
| 2559 | .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)); |
| 2560 | ASSERT_TRUE(result == ErrorCode::UNSUPPORTED_DIGEST || result == ErrorCode::INVALID_ARGUMENT); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2561 | |
| 2562 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2563 | Begin(KeyPurpose::SIGN, |
| 2564 | AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_SIGN))); |
| 2565 | } |
| 2566 | |
| 2567 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2568 | * SigningOperationsTest.RsaUnsupportedPadding |
| 2569 | * |
| 2570 | * Verifies that RSA operations fail with the correct error (but key gen succeeds) when used |
| 2571 | * with a padding mode inappropriate for RSA. |
| 2572 | */ |
| 2573 | TEST_P(SigningOperationsTest, RsaUnsupportedPadding) { |
| 2574 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2575 | .RsaSigningKey(2048, 65537) |
| 2576 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2577 | .Digest(Digest::SHA_2_256 /* supported digest */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2578 | .Padding(PaddingMode::PKCS7) |
| 2579 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2580 | ASSERT_EQ( |
| 2581 | ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2582 | Begin(KeyPurpose::SIGN, |
| 2583 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::PKCS7))); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2584 | CheckedDeleteKey(); |
| 2585 | |
| 2586 | ASSERT_EQ(ErrorCode::OK, |
| 2587 | GenerateKey( |
| 2588 | AuthorizationSetBuilder() |
| 2589 | .RsaSigningKey(2048, 65537) |
| 2590 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2591 | .Digest(Digest::SHA_2_256 /* supported digest */) |
| 2592 | .Padding(PaddingMode::RSA_OAEP) /* padding mode for encryption only */ |
| 2593 | .SetDefaultValidity())); |
| 2594 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2595 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2596 | .Digest(Digest::SHA_2_256) |
| 2597 | .Padding(PaddingMode::RSA_OAEP))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | /* |
| 2601 | * SigningOperationsTest.RsaPssNoDigest |
| 2602 | * |
| 2603 | * Verifies that RSA PSS operations fail when no digest is used. PSS requires a digest. |
| 2604 | */ |
| 2605 | TEST_P(SigningOperationsTest, RsaNoDigest) { |
| 2606 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2607 | .RsaSigningKey(2048, 65537) |
| 2608 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2609 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2610 | .Padding(PaddingMode::RSA_PSS) |
| 2611 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2612 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2613 | Begin(KeyPurpose::SIGN, |
| 2614 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::RSA_PSS))); |
| 2615 | |
| 2616 | ASSERT_EQ(ErrorCode::UNSUPPORTED_DIGEST, |
| 2617 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS))); |
| 2618 | } |
| 2619 | |
| 2620 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 2621 | * SigningOperationsTest.RsaPssNoPadding |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2622 | * |
| 2623 | * Verifies that RSA operations fail when no padding mode is specified. PaddingMode::NONE is |
| 2624 | * supported in some cases (as validated in other tests), but a mode must be specified. |
| 2625 | */ |
| 2626 | TEST_P(SigningOperationsTest, RsaNoPadding) { |
| 2627 | // Padding must be specified |
| 2628 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2629 | .RsaKey(2048, 65537) |
| 2630 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2631 | .SigningKey() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2632 | .Digest(Digest::NONE) |
| 2633 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2634 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, |
| 2635 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * SigningOperationsTest.RsaShortMessage |
| 2640 | * |
| 2641 | * Verifies that raw RSA signatures succeed with a message shorter than the key size. |
| 2642 | */ |
| 2643 | TEST_P(SigningOperationsTest, RsaTooShortMessage) { |
| 2644 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2645 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2646 | .RsaSigningKey(2048, 65537) |
| 2647 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2648 | .Padding(PaddingMode::NONE) |
| 2649 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2650 | |
| 2651 | // Barely shorter |
| 2652 | string message(2048 / 8 - 1, 'a'); |
| 2653 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2654 | |
| 2655 | // Much shorter |
| 2656 | message = "a"; |
| 2657 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 2658 | } |
| 2659 | |
| 2660 | /* |
| 2661 | * SigningOperationsTest.RsaSignWithEncryptionKey |
| 2662 | * |
| 2663 | * Verifies that RSA encryption keys cannot be used to sign. |
| 2664 | */ |
| 2665 | TEST_P(SigningOperationsTest, RsaSignWithEncryptionKey) { |
| 2666 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2667 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2668 | .RsaEncryptionKey(2048, 65537) |
| 2669 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2670 | .Padding(PaddingMode::NONE) |
| 2671 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2672 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 2673 | Begin(KeyPurpose::SIGN, |
| 2674 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE))); |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * SigningOperationsTest.RsaSignTooLargeMessage |
| 2679 | * |
| 2680 | * Verifies that attempting a raw signature of a message which is the same length as the key, |
| 2681 | * but numerically larger than the public modulus, fails with the correct error. |
| 2682 | */ |
| 2683 | TEST_P(SigningOperationsTest, RsaSignTooLargeMessage) { |
| 2684 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2685 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2686 | .RsaSigningKey(2048, 65537) |
| 2687 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2688 | .Padding(PaddingMode::NONE) |
| 2689 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2690 | |
| 2691 | // Largest possible message will always be larger than the public modulus. |
| 2692 | string message(2048 / 8, static_cast<char>(0xff)); |
| 2693 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2694 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2695 | .Digest(Digest::NONE) |
| 2696 | .Padding(PaddingMode::NONE))); |
| 2697 | string signature; |
| 2698 | ASSERT_EQ(ErrorCode::INVALID_ARGUMENT, Finish(message, &signature)); |
| 2699 | } |
| 2700 | |
| 2701 | /* |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 2702 | * SigningOperationsTest.EcdsaAllDigestsAndCurves |
| 2703 | * |
| 2704 | * Verifies ECDSA signature/verification for all digests and curves. |
| 2705 | */ |
| 2706 | TEST_P(SigningOperationsTest, EcdsaAllDigestsAndCurves) { |
| 2707 | auto digests = ValidDigests(true /* withNone */, false /* withMD5 */); |
| 2708 | |
| 2709 | string message = "1234567890"; |
| 2710 | string corrupt_message = "2234567890"; |
| 2711 | for (auto curve : ValidCurves()) { |
| 2712 | SCOPED_TRACE(testing::Message() << "Curve::" << curve); |
| 2713 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 2714 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2715 | .EcdsaSigningKey(curve) |
| 2716 | .Digest(digests) |
| 2717 | .SetDefaultValidity()); |
| 2718 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate key for EC curve " << curve; |
| 2719 | if (error != ErrorCode::OK) { |
| 2720 | continue; |
| 2721 | } |
| 2722 | |
| 2723 | for (auto digest : digests) { |
| 2724 | SCOPED_TRACE(testing::Message() << "Digest::" << digest); |
| 2725 | string signature = SignMessage(message, AuthorizationSetBuilder().Digest(digest)); |
| 2726 | LocalVerifyMessage(message, signature, AuthorizationSetBuilder().Digest(digest)); |
| 2727 | } |
| 2728 | |
| 2729 | auto rc = DeleteKey(); |
| 2730 | ASSERT_TRUE(rc == ErrorCode::OK || rc == ErrorCode::UNIMPLEMENTED); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2735 | * SigningOperationsTest.EcdsaAllCurves |
| 2736 | * |
| 2737 | * Verifies that ECDSA operations succeed with all possible curves. |
| 2738 | */ |
| 2739 | TEST_P(SigningOperationsTest, EcdsaAllCurves) { |
| 2740 | for (auto curve : ValidCurves()) { |
| 2741 | ErrorCode error = GenerateKey(AuthorizationSetBuilder() |
| 2742 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2743 | .EcdsaSigningKey(curve) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2744 | .Digest(Digest::SHA_2_256) |
| 2745 | .SetDefaultValidity()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2746 | EXPECT_EQ(ErrorCode::OK, error) << "Failed to generate ECDSA key with curve " << curve; |
| 2747 | if (error != ErrorCode::OK) continue; |
| 2748 | |
| 2749 | string message(1024, 'a'); |
| 2750 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 2751 | CheckedDeleteKey(); |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /* |
| 2756 | * SigningOperationsTest.EcdsaNoDigestHugeData |
| 2757 | * |
| 2758 | * Verifies that ECDSA operations support very large messages, even without digesting. This |
| 2759 | * should work because ECDSA actually only signs the leftmost L_n bits of the message, however |
| 2760 | * large it may be. Not using digesting is a bad idea, but in some cases digesting is done by |
| 2761 | * the framework. |
| 2762 | */ |
| 2763 | TEST_P(SigningOperationsTest, EcdsaNoDigestHugeData) { |
| 2764 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2765 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2766 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2767 | .Digest(Digest::NONE) |
| 2768 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2769 | string message(1 * 1024, 'a'); |
| 2770 | SignMessage(message, AuthorizationSetBuilder().Digest(Digest::NONE)); |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | * SigningOperationsTest.EcUseRequiresCorrectAppIdAppData |
| 2775 | * |
| 2776 | * Verifies that using an EC key requires the correct app ID/data. |
| 2777 | */ |
| 2778 | TEST_P(SigningOperationsTest, EcUseRequiresCorrectAppIdAppData) { |
| 2779 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2780 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2781 | .EcdsaSigningKey(EcCurve::P_256) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2782 | .Digest(Digest::NONE) |
| 2783 | .Authorization(TAG_APPLICATION_ID, "clientid") |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 2784 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2785 | .SetDefaultValidity())); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 2786 | |
| 2787 | CheckAppIdCharacteristics(key_blob_, "clientid", "appdata", key_characteristics_); |
| 2788 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2789 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2790 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::NONE))); |
| 2791 | AbortIfNeeded(); |
| 2792 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2793 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2794 | .Digest(Digest::NONE) |
| 2795 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2796 | AbortIfNeeded(); |
| 2797 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 2798 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2799 | .Digest(Digest::NONE) |
| 2800 | .Authorization(TAG_APPLICATION_DATA, "appdata"))); |
| 2801 | AbortIfNeeded(); |
| 2802 | EXPECT_EQ(ErrorCode::OK, |
| 2803 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder() |
| 2804 | .Digest(Digest::NONE) |
| 2805 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 2806 | .Authorization(TAG_APPLICATION_ID, "clientid"))); |
| 2807 | AbortIfNeeded(); |
| 2808 | } |
| 2809 | |
| 2810 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2811 | * SigningOperationsTest.EcdsaIncompatibleDigest |
| 2812 | * |
| 2813 | * Verifies that using an EC key requires compatible digest. |
| 2814 | */ |
| 2815 | TEST_P(SigningOperationsTest, EcdsaIncompatibleDigest) { |
| 2816 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2817 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 2818 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2819 | .Digest(Digest::NONE) |
| 2820 | .Digest(Digest::SHA1) |
| 2821 | .SetDefaultValidity())); |
| 2822 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 2823 | Begin(KeyPurpose::SIGN, AuthorizationSetBuilder().Digest(Digest::SHA_2_256))); |
| 2824 | AbortIfNeeded(); |
| 2825 | } |
| 2826 | |
| 2827 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2828 | * SigningOperationsTest.AesEcbSign |
| 2829 | * |
| 2830 | * Verifies that attempts to use AES keys to sign fail in the correct way. |
| 2831 | */ |
| 2832 | TEST_P(SigningOperationsTest, AesEcbSign) { |
| 2833 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2834 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2835 | .SigningKey() |
| 2836 | .AesEncryptionKey(128) |
| 2837 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB))); |
| 2838 | |
| 2839 | AuthorizationSet out_params; |
| 2840 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 2841 | Begin(KeyPurpose::SIGN, AuthorizationSet() /* in_params */, &out_params)); |
| 2842 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, |
| 2843 | Begin(KeyPurpose::VERIFY, AuthorizationSet() /* in_params */, &out_params)); |
| 2844 | } |
| 2845 | |
| 2846 | /* |
| 2847 | * SigningOperationsTest.HmacAllDigests |
| 2848 | * |
| 2849 | * Verifies that HMAC works with all digests. |
| 2850 | */ |
| 2851 | TEST_P(SigningOperationsTest, HmacAllDigests) { |
| 2852 | for (auto digest : ValidDigests(false /* withNone */, false /* withMD5 */)) { |
| 2853 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2854 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2855 | .HmacKey(128) |
| 2856 | .Digest(digest) |
| 2857 | .Authorization(TAG_MIN_MAC_LENGTH, 160))) |
| 2858 | << "Failed to create HMAC key with digest " << digest; |
| 2859 | string message = "12345678901234567890123456789012"; |
| 2860 | string signature = MacMessage(message, digest, 160); |
| 2861 | EXPECT_EQ(160U / 8U, signature.size()) |
| 2862 | << "Failed to sign with HMAC key with digest " << digest; |
| 2863 | CheckedDeleteKey(); |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * SigningOperationsTest.HmacSha256TooLargeMacLength |
| 2869 | * |
| 2870 | * Verifies that HMAC fails in the correct way when asked to generate a MAC larger than the |
| 2871 | * digest size. |
| 2872 | */ |
| 2873 | TEST_P(SigningOperationsTest, HmacSha256TooLargeMacLength) { |
| 2874 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2875 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2876 | .HmacKey(128) |
| 2877 | .Digest(Digest::SHA_2_256) |
| 2878 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 2879 | AuthorizationSet output_params; |
| 2880 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 2881 | AuthorizationSetBuilder() |
| 2882 | .Digest(Digest::SHA_2_256) |
| 2883 | .Authorization(TAG_MAC_LENGTH, 264), |
| 2884 | &output_params)); |
| 2885 | } |
| 2886 | |
| 2887 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 2888 | * SigningOperationsTest.HmacSha256InvalidMacLength |
| 2889 | * |
| 2890 | * Verifies that HMAC fails in the correct way when asked to generate a MAC whose length is |
| 2891 | * not a multiple of 8. |
| 2892 | */ |
| 2893 | TEST_P(SigningOperationsTest, HmacSha256InvalidMacLength) { |
| 2894 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2895 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2896 | .HmacKey(128) |
| 2897 | .Digest(Digest::SHA_2_256) |
| 2898 | .Authorization(TAG_MIN_MAC_LENGTH, 160))); |
| 2899 | AuthorizationSet output_params; |
| 2900 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 2901 | AuthorizationSetBuilder() |
| 2902 | .Digest(Digest::SHA_2_256) |
| 2903 | .Authorization(TAG_MAC_LENGTH, 161), |
| 2904 | &output_params)); |
| 2905 | } |
| 2906 | |
| 2907 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2908 | * SigningOperationsTest.HmacSha256TooSmallMacLength |
| 2909 | * |
| 2910 | * Verifies that HMAC fails in the correct way when asked to generate a MAC smaller than the |
| 2911 | * specified minimum MAC length. |
| 2912 | */ |
| 2913 | TEST_P(SigningOperationsTest, HmacSha256TooSmallMacLength) { |
| 2914 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 2915 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 2916 | .HmacKey(128) |
| 2917 | .Digest(Digest::SHA_2_256) |
| 2918 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 2919 | AuthorizationSet output_params; |
| 2920 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::SIGN, key_blob_, |
| 2921 | AuthorizationSetBuilder() |
| 2922 | .Digest(Digest::SHA_2_256) |
| 2923 | .Authorization(TAG_MAC_LENGTH, 120), |
| 2924 | &output_params)); |
| 2925 | } |
| 2926 | |
| 2927 | /* |
| 2928 | * SigningOperationsTest.HmacRfc4231TestCase3 |
| 2929 | * |
| 2930 | * Validates against the test vectors from RFC 4231 test case 3. |
| 2931 | */ |
| 2932 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase3) { |
| 2933 | string key(20, 0xaa); |
| 2934 | string message(50, 0xdd); |
| 2935 | uint8_t sha_224_expected[] = { |
| 2936 | 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9, 0x69, 0x4d, 0x7d, 0x6a, |
| 2937 | 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1, 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea, |
| 2938 | }; |
| 2939 | uint8_t sha_256_expected[] = { |
| 2940 | 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, |
| 2941 | 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, |
| 2942 | 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe, |
| 2943 | }; |
| 2944 | uint8_t sha_384_expected[] = { |
| 2945 | 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2, 0xac, 0xe0, |
| 2946 | 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9, 0x47, 0xac, 0x9f, 0xeb, |
| 2947 | 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, |
| 2948 | 0xc1, 0x38, 0x14, 0xb9, 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27, |
| 2949 | }; |
| 2950 | uint8_t sha_512_expected[] = { |
| 2951 | 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0, 0xf0, 0x75, 0x6c, |
| 2952 | 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd, 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, |
| 2953 | 0x3e, 0x33, 0xb2, 0x27, 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, |
| 2954 | 0xc8, 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46, 0xa3, 0x37, |
| 2955 | 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59, 0xe1, 0x32, 0x92, 0xfb, |
| 2956 | }; |
| 2957 | |
| 2958 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 2959 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 2960 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 2961 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 2962 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * SigningOperationsTest.HmacRfc4231TestCase5 |
| 2968 | * |
| 2969 | * Validates against the test vectors from RFC 4231 test case 5. |
| 2970 | */ |
| 2971 | TEST_P(SigningOperationsTest, HmacRfc4231TestCase5) { |
| 2972 | string key(20, 0x0c); |
| 2973 | string message = "Test With Truncation"; |
| 2974 | |
| 2975 | uint8_t sha_224_expected[] = { |
| 2976 | 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, |
| 2977 | 0xc9, 0x88, 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8, |
| 2978 | }; |
| 2979 | uint8_t sha_256_expected[] = { |
| 2980 | 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, |
| 2981 | 0x6e, 0x0c, 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b, |
| 2982 | }; |
| 2983 | uint8_t sha_384_expected[] = { |
| 2984 | 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, |
| 2985 | 0xa4, 0x6e, 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97, |
| 2986 | }; |
| 2987 | uint8_t sha_512_expected[] = { |
| 2988 | 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, |
| 2989 | 0x1d, 0x41, 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6, |
| 2990 | }; |
| 2991 | |
| 2992 | CheckHmacTestVector(key, message, Digest::SHA_2_256, make_string(sha_256_expected)); |
| 2993 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 2994 | CheckHmacTestVector(key, message, Digest::SHA_2_224, make_string(sha_224_expected)); |
| 2995 | CheckHmacTestVector(key, message, Digest::SHA_2_384, make_string(sha_384_expected)); |
| 2996 | CheckHmacTestVector(key, message, Digest::SHA_2_512, make_string(sha_512_expected)); |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | INSTANTIATE_KEYMINT_AIDL_TEST(SigningOperationsTest); |
| 3001 | |
| 3002 | typedef KeyMintAidlTestBase VerificationOperationsTest; |
| 3003 | |
| 3004 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3005 | * VerificationOperationsTest.HmacSigningKeyCannotVerify |
| 3006 | * |
| 3007 | * Verifies HMAC signing and verification, but that a signing key cannot be used to verify. |
| 3008 | */ |
| 3009 | TEST_P(VerificationOperationsTest, HmacSigningKeyCannotVerify) { |
| 3010 | string key_material = "HelloThisIsAKey"; |
| 3011 | |
| 3012 | vector<uint8_t> signing_key, verification_key; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3013 | vector<KeyCharacteristics> signing_key_chars, verification_key_chars; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3014 | EXPECT_EQ(ErrorCode::OK, |
| 3015 | ImportKey(AuthorizationSetBuilder() |
| 3016 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3017 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3018 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 3019 | .Digest(Digest::SHA_2_256) |
| 3020 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3021 | KeyFormat::RAW, key_material, &signing_key, &signing_key_chars)); |
| 3022 | EXPECT_EQ(ErrorCode::OK, |
| 3023 | ImportKey(AuthorizationSetBuilder() |
| 3024 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3025 | .Authorization(TAG_ALGORITHM, Algorithm::HMAC) |
| 3026 | .Authorization(TAG_PURPOSE, KeyPurpose::VERIFY) |
| 3027 | .Digest(Digest::SHA_2_256) |
| 3028 | .Authorization(TAG_MIN_MAC_LENGTH, 160), |
| 3029 | KeyFormat::RAW, key_material, &verification_key, &verification_key_chars)); |
| 3030 | |
| 3031 | string message = "This is a message."; |
| 3032 | string signature = SignMessage( |
| 3033 | signing_key, message, |
| 3034 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Authorization(TAG_MAC_LENGTH, 160)); |
| 3035 | |
| 3036 | // Signing key should not work. |
| 3037 | AuthorizationSet out_params; |
| 3038 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3039 | Begin(KeyPurpose::VERIFY, signing_key, |
| 3040 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256), &out_params)); |
| 3041 | |
| 3042 | // Verification key should work. |
| 3043 | VerifyMessage(verification_key, message, signature, |
| 3044 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 3045 | |
| 3046 | CheckedDeleteKey(&signing_key); |
| 3047 | CheckedDeleteKey(&verification_key); |
| 3048 | } |
| 3049 | |
| 3050 | INSTANTIATE_KEYMINT_AIDL_TEST(VerificationOperationsTest); |
| 3051 | |
| 3052 | typedef KeyMintAidlTestBase ExportKeyTest; |
| 3053 | |
| 3054 | /* |
| 3055 | * ExportKeyTest.RsaUnsupportedKeyFormat |
| 3056 | * |
| 3057 | * Verifies that attempting to export RSA keys in PKCS#8 format fails with the correct error. |
| 3058 | */ |
| 3059 | // TODO(seleneh) add ExportKey to GenerateKey |
| 3060 | // check result |
| 3061 | |
| 3062 | class ImportKeyTest : public KeyMintAidlTestBase { |
| 3063 | public: |
| 3064 | template <TagType tag_type, Tag tag, typename ValueT> |
| 3065 | void CheckCryptoParam(TypedTag<tag_type, tag> ttag, ValueT expected) { |
| 3066 | SCOPED_TRACE("CheckCryptoParam"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3067 | for (auto& entry : key_characteristics_) { |
| 3068 | if (entry.securityLevel == SecLevel()) { |
| 3069 | EXPECT_TRUE(contains(entry.authorizations, ttag, expected)) |
| 3070 | << "Tag " << tag << " with value " << expected |
| 3071 | << " not found at security level" << entry.securityLevel; |
| 3072 | } else { |
| 3073 | EXPECT_FALSE(contains(entry.authorizations, ttag, expected)) |
| 3074 | << "Tag " << tag << " found at security level " << entry.securityLevel; |
| 3075 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | void CheckOrigin() { |
| 3080 | SCOPED_TRACE("CheckOrigin"); |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 3081 | // Origin isn't a crypto param, but it always lives with them. |
| 3082 | return CheckCryptoParam(TAG_ORIGIN, KeyOrigin::IMPORTED); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3083 | } |
| 3084 | }; |
| 3085 | |
| 3086 | /* |
| 3087 | * ImportKeyTest.RsaSuccess |
| 3088 | * |
| 3089 | * Verifies that importing and using an RSA key pair works correctly. |
| 3090 | */ |
| 3091 | TEST_P(ImportKeyTest, RsaSuccess) { |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3092 | uint32_t key_size; |
| 3093 | string key; |
| 3094 | |
| 3095 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3096 | key_size = 2048; |
| 3097 | key = rsa_2048_key; |
| 3098 | } else { |
| 3099 | key_size = 1024; |
| 3100 | key = rsa_key; |
| 3101 | } |
| 3102 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3103 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3104 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3105 | .RsaSigningKey(key_size, 65537) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3106 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3107 | .Padding(PaddingMode::RSA_PSS) |
| 3108 | .SetDefaultValidity(), |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3109 | KeyFormat::PKCS8, key)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3110 | |
| 3111 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
Selene Huang | e5727e6 | 2021-04-13 22:41:20 -0700 | [diff] [blame] | 3112 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3113 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3114 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3115 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3116 | CheckOrigin(); |
| 3117 | |
| 3118 | string message(1024 / 8, 'a'); |
| 3119 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3120 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3121 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3125 | * ImportKeyTest.RsaSuccessWithoutParams |
| 3126 | * |
| 3127 | * Verifies that importing and using an RSA key pair without specifying parameters |
| 3128 | * works correctly. |
| 3129 | */ |
| 3130 | TEST_P(ImportKeyTest, RsaSuccessWithoutParams) { |
| 3131 | uint32_t key_size; |
| 3132 | string key; |
| 3133 | |
| 3134 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3135 | key_size = 2048; |
| 3136 | key = rsa_2048_key; |
| 3137 | } else { |
| 3138 | key_size = 1024; |
| 3139 | key = rsa_key; |
| 3140 | } |
| 3141 | |
| 3142 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3143 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3144 | .SigningKey() |
| 3145 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 3146 | .Digest(Digest::SHA_2_256) |
| 3147 | .Padding(PaddingMode::RSA_PSS) |
| 3148 | .SetDefaultValidity(), |
| 3149 | KeyFormat::PKCS8, key)); |
| 3150 | |
| 3151 | // Key size and public exponent are determined from the imported key material. |
| 3152 | CheckCryptoParam(TAG_KEY_SIZE, key_size); |
| 3153 | CheckCryptoParam(TAG_RSA_PUBLIC_EXPONENT, 65537U); |
| 3154 | |
| 3155 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::RSA); |
| 3156 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3157 | CheckCryptoParam(TAG_PADDING, PaddingMode::RSA_PSS); |
| 3158 | CheckOrigin(); |
| 3159 | |
| 3160 | string message(1024 / 8, 'a'); |
| 3161 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256).Padding(PaddingMode::RSA_PSS); |
| 3162 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3163 | LocalVerifyMessage(message, signature, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3167 | * ImportKeyTest.RsaKeySizeMismatch |
| 3168 | * |
| 3169 | * Verifies that importing an RSA key pair with a size that doesn't match the key fails in the |
| 3170 | * correct way. |
| 3171 | */ |
| 3172 | TEST_P(ImportKeyTest, RsaKeySizeMismatch) { |
| 3173 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3174 | ImportKey(AuthorizationSetBuilder() |
| 3175 | .RsaSigningKey(2048 /* Doesn't match key */, 65537) |
| 3176 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3177 | .Padding(PaddingMode::NONE) |
| 3178 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3179 | KeyFormat::PKCS8, rsa_key)); |
| 3180 | } |
| 3181 | |
| 3182 | /* |
| 3183 | * ImportKeyTest.RsaPublicExponentMismatch |
| 3184 | * |
| 3185 | * Verifies that importing an RSA key pair with a public exponent that doesn't match the key |
| 3186 | * fails in the correct way. |
| 3187 | */ |
| 3188 | TEST_P(ImportKeyTest, RsaPublicExponentMismatch) { |
| 3189 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3190 | ImportKey(AuthorizationSetBuilder() |
| 3191 | .RsaSigningKey(1024, 3 /* Doesn't match key */) |
| 3192 | .Digest(Digest::NONE) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3193 | .Padding(PaddingMode::NONE) |
| 3194 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3195 | KeyFormat::PKCS8, rsa_key)); |
| 3196 | } |
| 3197 | |
| 3198 | /* |
| 3199 | * ImportKeyTest.EcdsaSuccess |
| 3200 | * |
| 3201 | * Verifies that importing and using an ECDSA P-256 key pair works correctly. |
| 3202 | */ |
| 3203 | TEST_P(ImportKeyTest, EcdsaSuccess) { |
| 3204 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3205 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3206 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3207 | .Digest(Digest::SHA_2_256) |
| 3208 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3209 | KeyFormat::PKCS8, ec_256_key)); |
| 3210 | |
| 3211 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3212 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3213 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3214 | |
| 3215 | CheckOrigin(); |
| 3216 | |
| 3217 | string message(32, 'a'); |
| 3218 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3219 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3220 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | /* |
| 3224 | * ImportKeyTest.EcdsaP256RFC5915Success |
| 3225 | * |
| 3226 | * Verifies that importing and using an ECDSA P-256 key pair encoded using RFC5915 works |
| 3227 | * correctly. |
| 3228 | */ |
| 3229 | TEST_P(ImportKeyTest, EcdsaP256RFC5915Success) { |
| 3230 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3231 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3232 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3233 | .Digest(Digest::SHA_2_256) |
| 3234 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3235 | KeyFormat::PKCS8, ec_256_key_rfc5915)); |
| 3236 | |
| 3237 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3238 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3239 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3240 | |
| 3241 | CheckOrigin(); |
| 3242 | |
| 3243 | string message(32, 'a'); |
| 3244 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3245 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3246 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | /* |
| 3250 | * ImportKeyTest.EcdsaP256SEC1Success |
| 3251 | * |
| 3252 | * Verifies that importing and using an ECDSA P-256 key pair encoded using SEC1 works correctly. |
| 3253 | */ |
| 3254 | TEST_P(ImportKeyTest, EcdsaP256SEC1Success) { |
| 3255 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3256 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3257 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3258 | .Digest(Digest::SHA_2_256) |
| 3259 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3260 | KeyFormat::PKCS8, ec_256_key_sec1)); |
| 3261 | |
| 3262 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3263 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3264 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_256); |
| 3265 | |
| 3266 | CheckOrigin(); |
| 3267 | |
| 3268 | string message(32, 'a'); |
| 3269 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3270 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3271 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | /* |
| 3275 | * ImportKeyTest.Ecdsa521Success |
| 3276 | * |
| 3277 | * Verifies that importing and using an ECDSA P-521 key pair works correctly. |
| 3278 | */ |
| 3279 | TEST_P(ImportKeyTest, Ecdsa521Success) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 3280 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3281 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 3282 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3283 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3284 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 3285 | .EcdsaSigningKey(EcCurve::P_521) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3286 | .Digest(Digest::SHA_2_256) |
| 3287 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3288 | KeyFormat::PKCS8, ec_521_key)); |
| 3289 | |
| 3290 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::EC); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3291 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3292 | CheckCryptoParam(TAG_EC_CURVE, EcCurve::P_521); |
| 3293 | CheckOrigin(); |
| 3294 | |
| 3295 | string message(32, 'a'); |
| 3296 | auto params = AuthorizationSetBuilder().Digest(Digest::SHA_2_256); |
| 3297 | string signature = SignMessage(message, params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 3298 | LocalVerifyMessage(message, signature, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3302 | * ImportKeyTest.EcdsaCurveMismatch |
| 3303 | * |
| 3304 | * Verifies that importing an ECDSA key pair with a curve that doesn't match the key fails in |
| 3305 | * the correct way. |
| 3306 | */ |
| 3307 | TEST_P(ImportKeyTest, EcdsaCurveMismatch) { |
| 3308 | ASSERT_EQ(ErrorCode::IMPORT_PARAMETER_MISMATCH, |
| 3309 | ImportKey(AuthorizationSetBuilder() |
| 3310 | .EcdsaSigningKey(EcCurve::P_224 /* Doesn't match key */) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3311 | .Digest(Digest::NONE) |
| 3312 | .SetDefaultValidity(), |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3313 | KeyFormat::PKCS8, ec_256_key)); |
| 3314 | } |
| 3315 | |
| 3316 | /* |
| 3317 | * ImportKeyTest.AesSuccess |
| 3318 | * |
| 3319 | * Verifies that importing and using an AES key works. |
| 3320 | */ |
| 3321 | TEST_P(ImportKeyTest, AesSuccess) { |
| 3322 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 3323 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3324 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3325 | .AesEncryptionKey(key.size() * 8) |
| 3326 | .EcbMode() |
| 3327 | .Padding(PaddingMode::PKCS7), |
| 3328 | KeyFormat::RAW, key)); |
| 3329 | |
| 3330 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::AES); |
| 3331 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 3332 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 3333 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 3334 | CheckOrigin(); |
| 3335 | |
| 3336 | string message = "Hello World!"; |
| 3337 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 3338 | string ciphertext = EncryptMessage(message, params); |
| 3339 | string plaintext = DecryptMessage(ciphertext, params); |
| 3340 | EXPECT_EQ(message, plaintext); |
| 3341 | } |
| 3342 | |
| 3343 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3344 | * ImportKeyTest.AesFailure |
| 3345 | * |
| 3346 | * Verifies that importing an invalid AES key fails. |
| 3347 | */ |
| 3348 | TEST_P(ImportKeyTest, AesFailure) { |
| 3349 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 3350 | uint32_t bitlen = key.size() * 8; |
| 3351 | 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] | 3352 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 3353 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 3354 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3355 | .AesEncryptionKey(key_size) |
| 3356 | .EcbMode() |
| 3357 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 3358 | KeyFormat::RAW, key); |
| 3359 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3360 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 3361 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3362 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3363 | |
| 3364 | // Explicit key size matches that of the provided key, but it's not a valid size. |
| 3365 | string long_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 3366 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 3367 | ImportKey(AuthorizationSetBuilder() |
| 3368 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3369 | .AesEncryptionKey(long_key.size() * 8) |
| 3370 | .EcbMode() |
| 3371 | .Padding(PaddingMode::PKCS7), |
| 3372 | KeyFormat::RAW, long_key)); |
| 3373 | string short_key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 3374 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 3375 | ImportKey(AuthorizationSetBuilder() |
| 3376 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3377 | .AesEncryptionKey(short_key.size() * 8) |
| 3378 | .EcbMode() |
| 3379 | .Padding(PaddingMode::PKCS7), |
| 3380 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * ImportKeyTest.TripleDesSuccess |
| 3385 | * |
| 3386 | * Verifies that importing and using a 3DES key works. |
| 3387 | */ |
| 3388 | TEST_P(ImportKeyTest, TripleDesSuccess) { |
| 3389 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
| 3390 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3391 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3392 | .TripleDesEncryptionKey(168) |
| 3393 | .EcbMode() |
| 3394 | .Padding(PaddingMode::PKCS7), |
| 3395 | KeyFormat::RAW, key)); |
| 3396 | |
| 3397 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::TRIPLE_DES); |
| 3398 | CheckCryptoParam(TAG_KEY_SIZE, 168U); |
| 3399 | CheckCryptoParam(TAG_PADDING, PaddingMode::PKCS7); |
| 3400 | CheckCryptoParam(TAG_BLOCK_MODE, BlockMode::ECB); |
| 3401 | CheckOrigin(); |
| 3402 | |
| 3403 | string message = "Hello World!"; |
| 3404 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 3405 | string ciphertext = EncryptMessage(message, params); |
| 3406 | string plaintext = DecryptMessage(ciphertext, params); |
| 3407 | EXPECT_EQ(message, plaintext); |
| 3408 | } |
| 3409 | |
| 3410 | /* |
| 3411 | * ImportKeyTest.TripleDesFailure |
| 3412 | * |
| 3413 | * Verifies that importing an invalid 3DES key fails. |
| 3414 | */ |
| 3415 | TEST_P(ImportKeyTest, TripleDesFailure) { |
| 3416 | string key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358"); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 3417 | uint32_t bitlen = key.size() * 7; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3418 | 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] | 3419 | // Explicit key size doesn't match that of the provided key. |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 3420 | auto result = ImportKey(AuthorizationSetBuilder() |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 3421 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3422 | .TripleDesEncryptionKey(key_size) |
| 3423 | .EcbMode() |
| 3424 | .Padding(PaddingMode::PKCS7), |
Tommy Chiu | 3950b45 | 2021-05-03 22:01:46 +0800 | [diff] [blame] | 3425 | KeyFormat::RAW, key); |
| 3426 | ASSERT_TRUE(result == ErrorCode::IMPORT_PARAMETER_MISMATCH || |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3427 | result == ErrorCode::UNSUPPORTED_KEY_SIZE) |
| 3428 | << "unexpected result: " << result; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3429 | } |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3430 | // 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] | 3431 | string long_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f735800"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3432 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 3433 | ImportKey(AuthorizationSetBuilder() |
| 3434 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 3435 | .TripleDesEncryptionKey(long_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3436 | .EcbMode() |
| 3437 | .Padding(PaddingMode::PKCS7), |
| 3438 | KeyFormat::RAW, long_key)); |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 3439 | string short_key = hex2str("a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f73"); |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3440 | ASSERT_EQ(ErrorCode::UNSUPPORTED_KEY_SIZE, |
| 3441 | ImportKey(AuthorizationSetBuilder() |
| 3442 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 2a73db3 | 2021-05-10 10:58:58 +0100 | [diff] [blame] | 3443 | .TripleDesEncryptionKey(short_key.size() * 7) |
David Drysdale | c9bc2f7 | 2021-05-04 10:47:58 +0100 | [diff] [blame] | 3444 | .EcbMode() |
| 3445 | .Padding(PaddingMode::PKCS7), |
| 3446 | KeyFormat::RAW, short_key)); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /* |
| 3450 | * ImportKeyTest.HmacKeySuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3451 | * |
| 3452 | * Verifies that importing and using an HMAC key works. |
| 3453 | */ |
| 3454 | TEST_P(ImportKeyTest, HmacKeySuccess) { |
| 3455 | string key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 3456 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 3457 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3458 | .HmacKey(key.size() * 8) |
| 3459 | .Digest(Digest::SHA_2_256) |
| 3460 | .Authorization(TAG_MIN_MAC_LENGTH, 256), |
| 3461 | KeyFormat::RAW, key)); |
| 3462 | |
| 3463 | CheckCryptoParam(TAG_ALGORITHM, Algorithm::HMAC); |
| 3464 | CheckCryptoParam(TAG_KEY_SIZE, 128U); |
| 3465 | CheckCryptoParam(TAG_DIGEST, Digest::SHA_2_256); |
| 3466 | CheckOrigin(); |
| 3467 | |
| 3468 | string message = "Hello World!"; |
| 3469 | string signature = MacMessage(message, Digest::SHA_2_256, 256); |
| 3470 | VerifyMessage(message, signature, AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
| 3471 | } |
| 3472 | |
| 3473 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportKeyTest); |
| 3474 | |
| 3475 | auto wrapped_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3476 | // IKeyMintDevice.aidl |
| 3477 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 3478 | "020100" // INTEGER length 1 value 0x00 (version) |
| 3479 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 3480 | "934bf94e2aa28a3f83c9f79297250262" |
| 3481 | "fbe3276b5a1c91159bbfa3ef8957aac8" |
| 3482 | "4b59b30b455a79c2973480823d8b3863" |
| 3483 | "c3deef4a8e243590268d80e18751a0e1" |
| 3484 | "30f67ce6a1ace9f79b95e097474febc9" |
| 3485 | "81195b1d13a69086c0863f66a7b7fdb4" |
| 3486 | "8792227b1ac5e2489febdf087ab54864" |
| 3487 | "83033a6f001ca5d1ec1e27f5c30f4cec" |
| 3488 | "2642074a39ae68aee552e196627a8e3d" |
| 3489 | "867e67a8c01b11e75f13cca0a97ab668" |
| 3490 | "b50cda07a8ecb7cd8e3dd7009c963653" |
| 3491 | "4f6f239cffe1fc8daa466f78b676c711" |
| 3492 | "9efb96bce4e69ca2a25d0b34ed9c3ff9" |
| 3493 | "99b801597d5220e307eaa5bee507fb94" |
| 3494 | "d1fa69f9e519b2de315bac92c36f2ea1" |
| 3495 | "fa1df4478c0ddedeae8c70e0233cd098" |
| 3496 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 3497 | "d796b02c370f1fa4cc0124f1" |
| 3498 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 3499 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 3500 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 3501 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 3502 | "3106" // SET length 0x06 |
| 3503 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 3504 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 3505 | // } end SET |
| 3506 | // } end [1] |
| 3507 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 3508 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 3509 | // } end [2] |
| 3510 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 3511 | "02020100" // INTEGER length 2 value 0x100 |
| 3512 | // } end [3] |
| 3513 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode) |
| 3514 | "3103" // SET length 0x03 { |
| 3515 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 3516 | // } end SET |
| 3517 | // } end [4] |
| 3518 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 3519 | "3103" // SET length 0x03 { |
| 3520 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 3521 | // } end SET |
| 3522 | // } end [5] |
| 3523 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 3524 | // (noAuthRequired) |
| 3525 | "0500" // NULL |
| 3526 | // } end [503] |
| 3527 | // } end SEQUENCE (AuthorizationList) |
| 3528 | // } end SEQUENCE (KeyDescription) |
| 3529 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 3530 | "ccd540855f833a5e1480bfd2d36faf3a" |
| 3531 | "eee15df5beabe2691bc82dde2a7aa910" |
| 3532 | "0410" // OCTET STRING length 0x10 (tag) |
| 3533 | "64c9f689c60ff6223ab6e6999e0eb6e5" |
| 3534 | // } SEQUENCE (SecureKeyWrapper) |
| 3535 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3536 | |
| 3537 | auto wrapped_key_masked = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3538 | // IKeyMintDevice.aidl |
| 3539 | "30820179" // SEQUENCE length 0x179 (SecureKeyWrapper) { |
| 3540 | "020100" // INTEGER length 1 value 0x00 (version) |
| 3541 | "04820100" // OCTET STRING length 0x100 (encryptedTransportKey) |
| 3542 | "aad93ed5924f283b4bb5526fbe7a1412" |
| 3543 | "f9d9749ec30db9062b29e574a8546f33" |
| 3544 | "c88732452f5b8e6a391ee76c39ed1712" |
| 3545 | "c61d8df6213dec1cffbc17a8c6d04c7b" |
| 3546 | "30893d8daa9b2015213e219468215532" |
| 3547 | "07f8f9931c4caba23ed3bee28b36947e" |
| 3548 | "47f10e0a5c3dc51c988a628daad3e5e1" |
| 3549 | "f4005e79c2d5a96c284b4b8d7e4948f3" |
| 3550 | "31e5b85dd5a236f85579f3ea1d1b8484" |
| 3551 | "87470bdb0ab4f81a12bee42c99fe0df4" |
| 3552 | "bee3759453e69ad1d68a809ce06b949f" |
| 3553 | "7694a990429b2fe81e066ff43e56a216" |
| 3554 | "02db70757922a4bcc23ab89f1e35da77" |
| 3555 | "586775f423e519c2ea394caf48a28d0c" |
| 3556 | "8020f1dcf6b3a68ec246f615ae96dae9" |
| 3557 | "a079b1f6eb959033c1af5c125fd94168" |
| 3558 | "040c" // OCTET STRING length 0x0c (initializationVector) |
| 3559 | "6d9721d08589581ab49204a3" |
| 3560 | "302e" // SEQUENCE length 0x2e (KeyDescription) { |
| 3561 | "020103" // INTEGER length 1 value 0x03 (keyFormat = RAW) |
| 3562 | "3029" // SEQUENCE length 0x29 (AuthorizationList) { |
| 3563 | "a108" // [1] context-specific constructed tag=1 length 0x08 { (purpose) |
| 3564 | "3106" // SET length 0x06 |
| 3565 | "020100" // INTEGER length 1 value 0x00 (Encrypt) |
| 3566 | "020101" // INTEGER length 1 value 0x01 (Decrypt) |
| 3567 | // } end SET |
| 3568 | // } end [1] |
| 3569 | "a203" // [2] context-specific constructed tag=2 length 0x02 { (algorithm) |
| 3570 | "020120" // INTEGER length 1 value 0x20 (AES) |
| 3571 | // } end [2] |
| 3572 | "a304" // [3] context-specific constructed tag=3 length 0x04 { (keySize) |
| 3573 | "02020100" // INTEGER length 2 value 0x100 |
| 3574 | // } end [3] |
| 3575 | "a405" // [4] context-specific constructed tag=4 length 0x05 { (blockMode |
| 3576 | "3103" // SET length 0x03 { |
| 3577 | "020101" // INTEGER length 1 value 0x01 (ECB) |
| 3578 | // } end SET |
| 3579 | // } end [4] |
| 3580 | "a605" // [6] context-specific constructed tag=6 length 0x05 { (padding) |
| 3581 | "3103" // SET length 0x03 { |
| 3582 | "020140" // INTEGER length 1 value 0x40 (PKCS7) |
| 3583 | // } end SET |
| 3584 | // } end [5] |
| 3585 | "bf837702" // [503] context-specific constructed tag=503=0x1F7 length 0x02 { |
| 3586 | // (noAuthRequired) |
| 3587 | "0500" // NULL |
| 3588 | // } end [503] |
| 3589 | // } end SEQUENCE (AuthorizationList) |
| 3590 | // } end SEQUENCE (KeyDescription) |
| 3591 | "0420" // OCTET STRING length 0x20 (encryptedKey) |
| 3592 | "a61c6e247e25b3e6e69aa78eb03c2d4a" |
| 3593 | "c20d1f99a9a024a76f35c8e2cab9b68d" |
| 3594 | "0410" // OCTET STRING length 0x10 (tag) |
| 3595 | "2560c70109ae67c030f00b98b512a670" |
| 3596 | // } SEQUENCE (SecureKeyWrapper) |
| 3597 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3598 | |
| 3599 | auto wrapping_key = hex2str( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3600 | // RFC 5208 s5 |
| 3601 | "308204be" // SEQUENCE length 0x4be (PrivateKeyInfo) { |
| 3602 | "020100" // INTEGER length 1 value 0x00 (version) |
| 3603 | "300d" // SEQUENCE length 0x0d (AlgorithmIdentifier) { |
| 3604 | "0609" // OBJECT IDENTIFIER length 0x09 (algorithm) |
| 3605 | "2a864886f70d010101" // 1.2.840.113549.1.1.1 (RSAES-PKCS1-v1_5 encryption scheme) |
| 3606 | "0500" // NULL (parameters) |
| 3607 | // } SEQUENCE (AlgorithmIdentifier) |
| 3608 | "048204a8" // OCTET STRING len 0x4a8 (privateKey), which contains... |
| 3609 | // RFC 8017 A.1.2 |
| 3610 | "308204a4" // SEQUENCE len 0x4a4 (RSAPrivateKey) { |
| 3611 | "020100" // INTEGER length 1 value 0x00 (version) |
| 3612 | "02820101" // INTEGER length 0x0101 (modulus) value... |
| 3613 | "00aec367931d8900ce56b0067f7d70e1" // 0x10 |
| 3614 | "fc653f3f34d194c1fed50018fb43db93" // 0x20 |
| 3615 | "7b06e673a837313d56b1c725150a3fef" // 0x30 |
| 3616 | "86acbddc41bb759c2854eae32d35841e" // 0x40 |
| 3617 | "fb5c18d82bc90a1cb5c1d55adf245b02" // 0x50 |
| 3618 | "911f0b7cda88c421ff0ebafe7c0d23be" // 0x60 |
| 3619 | "312d7bd5921ffaea1347c157406fef71" // 0x70 |
| 3620 | "8f682643e4e5d33c6703d61c0cf7ac0b" // 0x80 |
| 3621 | "f4645c11f5c1374c3886427411c44979" // 0x90 |
| 3622 | "6792e0bef75dec858a2123c36753e02a" // 0xa0 |
| 3623 | "95a96d7c454b504de385a642e0dfc3e6" // 0xb0 |
| 3624 | "0ac3a7ee4991d0d48b0172a95f9536f0" // 0xc0 |
| 3625 | "2ba13cecccb92b727db5c27e5b2f5cec" // 0xd0 |
| 3626 | "09600b286af5cf14c42024c61ddfe71c" // 0xe0 |
| 3627 | "2a8d7458f185234cb00e01d282f10f8f" // 0xf0 |
| 3628 | "c6721d2aed3f4833cca2bd8fa62821dd" // 0x100 |
| 3629 | "55" // 0x101 |
| 3630 | "0203010001" // INTEGER length 3 value 0x10001 (publicExponent) |
| 3631 | "02820100" // INTEGER length 0x100 (privateExponent) value... |
| 3632 | "431447b6251908112b1ee76f99f3711a" // 0x10 |
| 3633 | "52b6630960046c2de70de188d833f8b8" // 0x20 |
| 3634 | "b91e4d785caeeeaf4f0f74414e2cda40" // 0x30 |
| 3635 | "641f7fe24f14c67a88959bdb27766df9" // 0x40 |
| 3636 | "e710b630a03adc683b5d2c43080e52be" // 0x50 |
| 3637 | "e71e9eaeb6de297a5fea1072070d181c" // 0x60 |
| 3638 | "822bccff087d63c940ba8a45f670feb2" // 0x70 |
| 3639 | "9fb4484d1c95e6d2579ba02aae0a0090" // 0x80 |
| 3640 | "0c3ebf490e3d2cd7ee8d0e20c536e4dc" // 0x90 |
| 3641 | "5a5097272888cddd7e91f228b1c4d747" // 0xa0 |
| 3642 | "4c55b8fcd618c4a957bbddd5ad7407cc" // 0xb0 |
| 3643 | "312d8d98a5caf7e08f4a0d6b45bb41c6" // 0xc0 |
| 3644 | "52659d5a5ba05b663737a8696281865b" // 0xd0 |
| 3645 | "a20fbdd7f851e6c56e8cbe0ddbbf24dc" // 0xe0 |
| 3646 | "03b2d2cb4c3d540fb0af52e034a2d066" // 0xf0 |
| 3647 | "98b128e5f101e3b51a34f8d8b4f86181" // 0x100 |
| 3648 | "028181" // INTEGER length 0x81 (prime1) value... |
| 3649 | "00de392e18d682c829266cc3454e1d61" // 0x10 |
| 3650 | "66242f32d9a1d10577753e904ea7d08b" // 0x20 |
| 3651 | "ff841be5bac82a164c5970007047b8c5" // 0x30 |
| 3652 | "17db8f8f84e37bd5988561bdf503d4dc" // 0x40 |
| 3653 | "2bdb38f885434ae42c355f725c9a60f9" // 0x50 |
| 3654 | "1f0788e1f1a97223b524b5357fdf72e2" // 0x60 |
| 3655 | "f696bab7d78e32bf92ba8e1864eab122" // 0x70 |
| 3656 | "9e91346130748a6e3c124f9149d71c74" // 0x80 |
| 3657 | "35" |
| 3658 | "028181" // INTEGER length 0x81 (prime2) value... |
| 3659 | "00c95387c0f9d35f137b57d0d65c397c" // 0x10 |
| 3660 | "5e21cc251e47008ed62a542409c8b6b6" // 0x20 |
| 3661 | "ac7f8967b3863ca645fcce49582a9aa1" // 0x30 |
| 3662 | "7349db6c4a95affdae0dae612e1afac9" // 0x40 |
| 3663 | "9ed39a2d934c880440aed8832f984316" // 0x50 |
| 3664 | "3a47f27f392199dc1202f9a0f9bd0830" // 0x60 |
| 3665 | "8007cb1e4e7f58309366a7de25f7c3c9" // 0x70 |
| 3666 | "b880677c068e1be936e81288815252a8" // 0x80 |
| 3667 | "a1" |
| 3668 | "028180" // INTEGER length 0x80 (exponent1) value... |
| 3669 | "57ff8ca1895080b2cae486ef0adfd791" // 0x10 |
| 3670 | "fb0235c0b8b36cd6c136e52e4085f4ea" // 0x20 |
| 3671 | "5a063212a4f105a3764743e53281988a" // 0x30 |
| 3672 | "ba073f6e0027298e1c4378556e0efca0" // 0x40 |
| 3673 | "e14ece1af76ad0b030f27af6f0ab35fb" // 0x50 |
| 3674 | "73a060d8b1a0e142fa2647e93b32e36d" // 0x60 |
| 3675 | "8282ae0a4de50ab7afe85500a16f43a6" // 0x70 |
| 3676 | "4719d6e2b9439823719cd08bcd031781" // 0x80 |
| 3677 | "028181" // INTEGER length 0x81 (exponent2) value... |
| 3678 | "00ba73b0bb28e3f81e9bd1c568713b10" // 0x10 |
| 3679 | "1241acc607976c4ddccc90e65b6556ca" // 0x20 |
| 3680 | "31516058f92b6e09f3b160ff0e374ec4" // 0x30 |
| 3681 | "0d78ae4d4979fde6ac06a1a400c61dd3" // 0x40 |
| 3682 | "1254186af30b22c10582a8a43e34fe94" // 0x50 |
| 3683 | "9c5f3b9755bae7baa7b7b7a6bd03b38c" // 0x60 |
| 3684 | "ef55c86885fc6c1978b9cee7ef33da50" // 0x70 |
| 3685 | "7c9df6b9277cff1e6aaa5d57aca52846" // 0x80 |
| 3686 | "61" |
| 3687 | "028181" // INTEGER length 0x81 (coefficient) value... |
| 3688 | "00c931617c77829dfb1270502be9195c" // 0x10 |
| 3689 | "8f2830885f57dba869536811e6864236" // 0x20 |
| 3690 | "d0c4736a0008a145af36b8357a7c3d13" // 0x30 |
| 3691 | "9966d04c4e00934ea1aede3bb6b8ec84" // 0x40 |
| 3692 | "1dc95e3f579751e2bfdfe27ae778983f" // 0x50 |
| 3693 | "959356210723287b0affcc9f727044d4" // 0x60 |
| 3694 | "8c373f1babde0724fa17a4fd4da0902c" // 0x70 |
| 3695 | "7c9b9bf27ba61be6ad02dfddda8f4e68" // 0x80 |
| 3696 | "22" |
| 3697 | // } SEQUENCE |
| 3698 | // } SEQUENCE () |
| 3699 | ); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3700 | |
| 3701 | string zero_masking_key = |
| 3702 | hex2str("0000000000000000000000000000000000000000000000000000000000000000"); |
| 3703 | string masking_key = hex2str("D796B02C370F1FA4CC0124F14EC8CBEBE987E825246265050F399A51FD477DFC"); |
| 3704 | |
| 3705 | class ImportWrappedKeyTest : public KeyMintAidlTestBase {}; |
| 3706 | |
| 3707 | TEST_P(ImportWrappedKeyTest, Success) { |
| 3708 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3709 | .RsaEncryptionKey(2048, 65537) |
| 3710 | .Digest(Digest::SHA_2_256) |
| 3711 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3712 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3713 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3714 | |
| 3715 | ASSERT_EQ(ErrorCode::OK, |
| 3716 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3717 | AuthorizationSetBuilder() |
| 3718 | .Digest(Digest::SHA_2_256) |
| 3719 | .Padding(PaddingMode::RSA_OAEP))); |
| 3720 | |
| 3721 | string message = "Hello World!"; |
| 3722 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 3723 | string ciphertext = EncryptMessage(message, params); |
| 3724 | string plaintext = DecryptMessage(ciphertext, params); |
| 3725 | EXPECT_EQ(message, plaintext); |
| 3726 | } |
| 3727 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3728 | /* |
| 3729 | * ImportWrappedKeyTest.SuccessSidsIgnored |
| 3730 | * |
| 3731 | * Verifies that password_sid and biometric_sid are ignored on import if the authorizations don't |
| 3732 | * include Tag:USER_SECURE_ID. |
| 3733 | */ |
| 3734 | TEST_P(ImportWrappedKeyTest, SuccessSidsIgnored) { |
| 3735 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3736 | .RsaEncryptionKey(2048, 65537) |
| 3737 | .Digest(Digest::SHA_2_256) |
| 3738 | .Padding(PaddingMode::RSA_OAEP) |
| 3739 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3740 | .SetDefaultValidity(); |
| 3741 | |
| 3742 | int64_t password_sid = 42; |
| 3743 | int64_t biometric_sid = 24; |
| 3744 | ASSERT_EQ(ErrorCode::OK, |
| 3745 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3746 | AuthorizationSetBuilder() |
| 3747 | .Digest(Digest::SHA_2_256) |
| 3748 | .Padding(PaddingMode::RSA_OAEP), |
| 3749 | password_sid, biometric_sid)); |
| 3750 | |
| 3751 | string message = "Hello World!"; |
| 3752 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 3753 | string ciphertext = EncryptMessage(message, params); |
| 3754 | string plaintext = DecryptMessage(ciphertext, params); |
| 3755 | EXPECT_EQ(message, plaintext); |
| 3756 | } |
| 3757 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3758 | TEST_P(ImportWrappedKeyTest, SuccessMasked) { |
| 3759 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3760 | .RsaEncryptionKey(2048, 65537) |
| 3761 | .Digest(Digest::SHA_2_256) |
| 3762 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3763 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3764 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3765 | |
| 3766 | ASSERT_EQ(ErrorCode::OK, |
| 3767 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, masking_key, |
| 3768 | AuthorizationSetBuilder() |
| 3769 | .Digest(Digest::SHA_2_256) |
| 3770 | .Padding(PaddingMode::RSA_OAEP))); |
| 3771 | } |
| 3772 | |
| 3773 | TEST_P(ImportWrappedKeyTest, WrongMask) { |
| 3774 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3775 | .RsaEncryptionKey(2048, 65537) |
| 3776 | .Digest(Digest::SHA_2_256) |
| 3777 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3778 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3779 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3780 | |
| 3781 | ASSERT_EQ( |
| 3782 | ErrorCode::VERIFICATION_FAILED, |
| 3783 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3784 | AuthorizationSetBuilder() |
| 3785 | .Digest(Digest::SHA_2_256) |
| 3786 | .Padding(PaddingMode::RSA_OAEP))); |
| 3787 | } |
| 3788 | |
| 3789 | TEST_P(ImportWrappedKeyTest, WrongPurpose) { |
| 3790 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3791 | .RsaEncryptionKey(2048, 65537) |
| 3792 | .Digest(Digest::SHA_2_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3793 | .Padding(PaddingMode::RSA_OAEP) |
| 3794 | .SetDefaultValidity(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3795 | |
| 3796 | ASSERT_EQ( |
| 3797 | ErrorCode::INCOMPATIBLE_PURPOSE, |
| 3798 | ImportWrappedKey(wrapped_key_masked, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3799 | AuthorizationSetBuilder() |
| 3800 | .Digest(Digest::SHA_2_256) |
| 3801 | .Padding(PaddingMode::RSA_OAEP))); |
| 3802 | } |
| 3803 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3804 | TEST_P(ImportWrappedKeyTest, WrongPaddingMode) { |
| 3805 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3806 | .RsaEncryptionKey(2048, 65537) |
| 3807 | .Digest(Digest::SHA_2_256) |
| 3808 | .Padding(PaddingMode::RSA_PSS) |
| 3809 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3810 | .SetDefaultValidity(); |
| 3811 | |
| 3812 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 3813 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3814 | AuthorizationSetBuilder() |
| 3815 | .Digest(Digest::SHA_2_256) |
| 3816 | .Padding(PaddingMode::RSA_OAEP))); |
| 3817 | } |
| 3818 | |
| 3819 | TEST_P(ImportWrappedKeyTest, WrongDigest) { |
| 3820 | auto wrapping_key_desc = AuthorizationSetBuilder() |
| 3821 | .RsaEncryptionKey(2048, 65537) |
| 3822 | .Digest(Digest::SHA_2_512) |
| 3823 | .Padding(PaddingMode::RSA_OAEP) |
| 3824 | .Authorization(TAG_PURPOSE, KeyPurpose::WRAP_KEY) |
| 3825 | .SetDefaultValidity(); |
| 3826 | |
| 3827 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, |
| 3828 | ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, zero_masking_key, |
| 3829 | AuthorizationSetBuilder() |
| 3830 | .Digest(Digest::SHA_2_256) |
| 3831 | .Padding(PaddingMode::RSA_OAEP))); |
| 3832 | } |
| 3833 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3834 | INSTANTIATE_KEYMINT_AIDL_TEST(ImportWrappedKeyTest); |
| 3835 | |
| 3836 | typedef KeyMintAidlTestBase EncryptionOperationsTest; |
| 3837 | |
| 3838 | /* |
| 3839 | * EncryptionOperationsTest.RsaNoPaddingSuccess |
| 3840 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3841 | * Verifies that raw RSA decryption works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3842 | */ |
| 3843 | TEST_P(EncryptionOperationsTest, RsaNoPaddingSuccess) { |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3844 | for (uint64_t exponent : {3, 65537}) { |
| 3845 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3846 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3847 | .RsaEncryptionKey(2048, exponent) |
| 3848 | .Padding(PaddingMode::NONE) |
| 3849 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3850 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3851 | string message = string(2048 / 8, 'a'); |
| 3852 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3853 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3854 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3855 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3856 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3857 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3858 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3859 | // Unpadded RSA is deterministic |
| 3860 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 3861 | |
| 3862 | CheckedDeleteKey(); |
| 3863 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | /* |
| 3867 | * EncryptionOperationsTest.RsaNoPaddingShortMessage |
| 3868 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3869 | * Verifies that raw RSA decryption of short messages works. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3870 | */ |
| 3871 | TEST_P(EncryptionOperationsTest, RsaNoPaddingShortMessage) { |
| 3872 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3873 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3874 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3875 | .Padding(PaddingMode::NONE) |
| 3876 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3877 | |
| 3878 | string message = "1"; |
| 3879 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 3880 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3881 | string ciphertext = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3882 | EXPECT_EQ(2048U / 8, ciphertext.size()); |
| 3883 | |
| 3884 | string expected_plaintext = string(2048U / 8 - 1, 0) + message; |
| 3885 | string plaintext = DecryptMessage(ciphertext, params); |
| 3886 | |
| 3887 | EXPECT_EQ(expected_plaintext, plaintext); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3891 | * EncryptionOperationsTest.RsaOaepSuccess |
| 3892 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3893 | * Verifies that RSA-OAEP decryption operations work, with all digests. |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3894 | */ |
| 3895 | TEST_P(EncryptionOperationsTest, RsaOaepSuccess) { |
| 3896 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 3897 | |
| 3898 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3899 | ASSERT_EQ(ErrorCode::OK, |
| 3900 | GenerateKey(AuthorizationSetBuilder() |
| 3901 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3902 | .RsaEncryptionKey(key_size, 65537) |
| 3903 | .Padding(PaddingMode::RSA_OAEP) |
| 3904 | .Digest(digests) |
| 3905 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1) |
| 3906 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3907 | |
| 3908 | string message = "Hello"; |
| 3909 | |
| 3910 | for (auto digest : digests) { |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3911 | SCOPED_TRACE(testing::Message() << "digest-" << digest); |
| 3912 | |
| 3913 | auto params = AuthorizationSetBuilder() |
| 3914 | .Digest(digest) |
| 3915 | .Padding(PaddingMode::RSA_OAEP) |
| 3916 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA1); |
| 3917 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3918 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 3919 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 3920 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3921 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3922 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 3923 | |
| 3924 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 3925 | // probability). |
| 3926 | EXPECT_NE(ciphertext1, ciphertext2); |
| 3927 | |
| 3928 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 3929 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 3930 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 3931 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 3932 | |
| 3933 | // Decrypting corrupted ciphertext should fail. |
| 3934 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 3935 | char corrupt_byte; |
| 3936 | do { |
| 3937 | corrupt_byte = static_cast<char>(random() % 256); |
| 3938 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 3939 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 3940 | |
| 3941 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 3942 | string result; |
| 3943 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 3944 | EXPECT_EQ(0U, result.size()); |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | /* |
| 3949 | * EncryptionOperationsTest.RsaOaepInvalidDigest |
| 3950 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3951 | * 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] | 3952 | * without a digest. |
| 3953 | */ |
| 3954 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidDigest) { |
| 3955 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3956 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3957 | .RsaEncryptionKey(2048, 65537) |
| 3958 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3959 | .Digest(Digest::NONE) |
| 3960 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3961 | |
| 3962 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_OAEP).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3963 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3967 | * EncryptionOperationsTest.RsaOaepInvalidPadding |
| 3968 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3969 | * 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] | 3970 | * with a padding value that is only suitable for signing/verifying. |
| 3971 | */ |
| 3972 | TEST_P(EncryptionOperationsTest, RsaOaepInvalidPadding) { |
| 3973 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3974 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3975 | .RsaEncryptionKey(2048, 65537) |
| 3976 | .Padding(PaddingMode::RSA_PSS) |
| 3977 | .Digest(Digest::NONE) |
| 3978 | .SetDefaultValidity())); |
| 3979 | |
| 3980 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PSS).Digest(Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3981 | EXPECT_EQ(ErrorCode::UNSUPPORTED_PADDING_MODE, Begin(KeyPurpose::DECRYPT, params)); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 3985 | * EncryptionOperationsTest.RsaOaepDecryptWithWrongDigest |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3986 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 3987 | * 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] | 3988 | * with a different digest than was used to encrypt. |
| 3989 | */ |
| 3990 | TEST_P(EncryptionOperationsTest, RsaOaepDecryptWithWrongDigest) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 3991 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 3992 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 3993 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 3994 | |
| 3995 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 3996 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 3997 | .RsaEncryptionKey(1024, 65537) |
| 3998 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 3999 | .Digest(Digest::SHA_2_224, Digest::SHA_2_256) |
| 4000 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4001 | string message = "Hello World!"; |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4002 | string ciphertext = LocalRsaEncryptMessage( |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4003 | message, |
| 4004 | AuthorizationSetBuilder().Digest(Digest::SHA_2_224).Padding(PaddingMode::RSA_OAEP)); |
| 4005 | |
| 4006 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 4007 | .Digest(Digest::SHA_2_256) |
| 4008 | .Padding(PaddingMode::RSA_OAEP))); |
| 4009 | string result; |
| 4010 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext, &result)); |
| 4011 | EXPECT_EQ(0U, result.size()); |
| 4012 | } |
| 4013 | |
| 4014 | /* |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4015 | * EncryptionOperationsTest.RsaOaepWithMGFDigestSuccess |
| 4016 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4017 | * 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] | 4018 | * digests. |
| 4019 | */ |
| 4020 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFDigestSuccess) { |
| 4021 | auto digests = ValidDigests(false /* withNone */, true /* withMD5 */); |
| 4022 | |
| 4023 | size_t key_size = 2048; // Need largish key for SHA-512 test. |
| 4024 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4025 | .OaepMGFDigest(digests) |
| 4026 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4027 | .RsaEncryptionKey(key_size, 65537) |
| 4028 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4029 | .Digest(Digest::SHA_2_256) |
| 4030 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4031 | |
| 4032 | string message = "Hello"; |
| 4033 | |
| 4034 | for (auto digest : digests) { |
| 4035 | auto params = AuthorizationSetBuilder() |
| 4036 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, digest) |
| 4037 | .Digest(Digest::SHA_2_256) |
| 4038 | .Padding(PaddingMode::RSA_OAEP); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4039 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4040 | if (HasNonfatalFailure()) std::cout << "-->" << digest << std::endl; |
| 4041 | EXPECT_EQ(key_size / 8, ciphertext1.size()); |
| 4042 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4043 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4044 | EXPECT_EQ(key_size / 8, ciphertext2.size()); |
| 4045 | |
| 4046 | // OAEP randomizes padding so every result should be different (with astronomically high |
| 4047 | // probability). |
| 4048 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4049 | |
| 4050 | string plaintext1 = DecryptMessage(ciphertext1, params); |
| 4051 | EXPECT_EQ(message, plaintext1) << "RSA-OAEP failed with digest " << digest; |
| 4052 | string plaintext2 = DecryptMessage(ciphertext2, params); |
| 4053 | EXPECT_EQ(message, plaintext2) << "RSA-OAEP failed with digest " << digest; |
| 4054 | |
| 4055 | // Decrypting corrupted ciphertext should fail. |
| 4056 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4057 | char corrupt_byte; |
| 4058 | do { |
| 4059 | corrupt_byte = static_cast<char>(random() % 256); |
| 4060 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4061 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4062 | |
| 4063 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4064 | string result; |
| 4065 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4066 | EXPECT_EQ(0U, result.size()); |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | /* |
| 4071 | * EncryptionOperationsTest.RsaOaepWithMGFIncompatibleDigest |
| 4072 | * |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4073 | * 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] | 4074 | * with incompatible MGF digest. |
| 4075 | */ |
| 4076 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFIncompatibleDigest) { |
| 4077 | ASSERT_EQ(ErrorCode::OK, |
| 4078 | GenerateKey(AuthorizationSetBuilder() |
| 4079 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 4080 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4081 | .RsaEncryptionKey(2048, 65537) |
| 4082 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4083 | .Digest(Digest::SHA_2_256) |
| 4084 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4085 | string message = "Hello World!"; |
| 4086 | |
| 4087 | auto params = AuthorizationSetBuilder() |
| 4088 | .Padding(PaddingMode::RSA_OAEP) |
| 4089 | .Digest(Digest::SHA_2_256) |
| 4090 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_224); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4091 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* |
| 4095 | * EncryptionOperationsTest.RsaOaepWithMGFUnsupportedDigest |
| 4096 | * |
| 4097 | * Verifies that RSA-OAEP encryption operations fail in the correct way when asked to operate |
| 4098 | * with unsupported MGF digest. |
| 4099 | */ |
| 4100 | TEST_P(EncryptionOperationsTest, RsaOaepWithMGFUnsupportedDigest) { |
| 4101 | ASSERT_EQ(ErrorCode::OK, |
| 4102 | GenerateKey(AuthorizationSetBuilder() |
| 4103 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::SHA_2_256) |
| 4104 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4105 | .RsaEncryptionKey(2048, 65537) |
| 4106 | .Padding(PaddingMode::RSA_OAEP) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4107 | .Digest(Digest::SHA_2_256) |
| 4108 | .SetDefaultValidity())); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4109 | string message = "Hello World!"; |
| 4110 | |
| 4111 | auto params = AuthorizationSetBuilder() |
| 4112 | .Padding(PaddingMode::RSA_OAEP) |
| 4113 | .Digest(Digest::SHA_2_256) |
| 4114 | .Authorization(TAG_RSA_OAEP_MGF_DIGEST, Digest::NONE); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4115 | EXPECT_EQ(ErrorCode::UNSUPPORTED_MGF_DIGEST, Begin(KeyPurpose::DECRYPT, params)); |
Chirag Pathak | 8b7455a | 2020-12-21 18:42:52 -0500 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4119 | * EncryptionOperationsTest.RsaPkcs1Success |
| 4120 | * |
| 4121 | * Verifies that RSA PKCS encryption/decrypts works. |
| 4122 | */ |
| 4123 | TEST_P(EncryptionOperationsTest, RsaPkcs1Success) { |
| 4124 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4125 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4126 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4127 | .Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT) |
| 4128 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4129 | |
| 4130 | string message = "Hello World!"; |
| 4131 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::RSA_PKCS1_1_5_ENCRYPT); |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4132 | string ciphertext1 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4133 | EXPECT_EQ(2048U / 8, ciphertext1.size()); |
| 4134 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 4135 | string ciphertext2 = LocalRsaEncryptMessage(message, params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4136 | EXPECT_EQ(2048U / 8, ciphertext2.size()); |
| 4137 | |
| 4138 | // PKCS1 v1.5 randomizes padding so every result should be different. |
| 4139 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4140 | |
| 4141 | string plaintext = DecryptMessage(ciphertext1, params); |
| 4142 | EXPECT_EQ(message, plaintext); |
| 4143 | |
| 4144 | // Decrypting corrupted ciphertext should fail. |
| 4145 | size_t offset_to_corrupt = random() % ciphertext1.size(); |
| 4146 | char corrupt_byte; |
| 4147 | do { |
| 4148 | corrupt_byte = static_cast<char>(random() % 256); |
| 4149 | } while (corrupt_byte == ciphertext1[offset_to_corrupt]); |
| 4150 | ciphertext1[offset_to_corrupt] = corrupt_byte; |
| 4151 | |
| 4152 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4153 | string result; |
| 4154 | EXPECT_EQ(ErrorCode::UNKNOWN_ERROR, Finish(ciphertext1, &result)); |
| 4155 | EXPECT_EQ(0U, result.size()); |
| 4156 | } |
| 4157 | |
| 4158 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4159 | * EncryptionOperationsTest.EcdsaEncrypt |
| 4160 | * |
| 4161 | * Verifies that attempting to use ECDSA keys to encrypt fails in the correct way. |
| 4162 | */ |
| 4163 | TEST_P(EncryptionOperationsTest, EcdsaEncrypt) { |
| 4164 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4165 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 4166 | .EcdsaSigningKey(EcCurve::P_256) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 4167 | .Digest(Digest::NONE) |
| 4168 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4169 | auto params = AuthorizationSetBuilder().Digest(Digest::NONE); |
| 4170 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4171 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 4172 | } |
| 4173 | |
| 4174 | /* |
| 4175 | * EncryptionOperationsTest.HmacEncrypt |
| 4176 | * |
| 4177 | * Verifies that attempting to use HMAC keys to encrypt fails in the correct way. |
| 4178 | */ |
| 4179 | TEST_P(EncryptionOperationsTest, HmacEncrypt) { |
| 4180 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4181 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4182 | .HmacKey(128) |
| 4183 | .Digest(Digest::SHA_2_256) |
| 4184 | .Padding(PaddingMode::NONE) |
| 4185 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4186 | auto params = AuthorizationSetBuilder() |
| 4187 | .Digest(Digest::SHA_2_256) |
| 4188 | .Padding(PaddingMode::NONE) |
| 4189 | .Authorization(TAG_MAC_LENGTH, 128); |
| 4190 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4191 | ASSERT_EQ(ErrorCode::UNSUPPORTED_PURPOSE, Begin(KeyPurpose::DECRYPT, params)); |
| 4192 | } |
| 4193 | |
| 4194 | /* |
| 4195 | * EncryptionOperationsTest.AesEcbRoundTripSuccess |
| 4196 | * |
| 4197 | * Verifies that AES ECB mode works. |
| 4198 | */ |
| 4199 | TEST_P(EncryptionOperationsTest, AesEcbRoundTripSuccess) { |
| 4200 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4201 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4202 | .AesEncryptionKey(128) |
| 4203 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4204 | .Padding(PaddingMode::NONE))); |
| 4205 | |
| 4206 | ASSERT_GT(key_blob_.size(), 0U); |
| 4207 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 4208 | |
| 4209 | // Two-block message. |
| 4210 | string message = "12345678901234567890123456789012"; |
| 4211 | string ciphertext1 = EncryptMessage(message, params); |
| 4212 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 4213 | |
| 4214 | string ciphertext2 = EncryptMessage(string(message), params); |
| 4215 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 4216 | |
| 4217 | // ECB is deterministic. |
| 4218 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 4219 | |
| 4220 | string plaintext = DecryptMessage(ciphertext1, params); |
| 4221 | EXPECT_EQ(message, plaintext); |
| 4222 | } |
| 4223 | |
| 4224 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4225 | * EncryptionOperationsTest.AesEcbUnknownTag |
| 4226 | * |
| 4227 | * Verifies that AES ECB operations ignore unknown tags. |
| 4228 | */ |
| 4229 | TEST_P(EncryptionOperationsTest, AesEcbUnknownTag) { |
| 4230 | int32_t unknown_tag_value = ((7 << 28) /* TagType:BOOL */ | 150); |
| 4231 | Tag unknown_tag = static_cast<Tag>(unknown_tag_value); |
| 4232 | KeyParameter unknown_param; |
| 4233 | unknown_param.tag = unknown_tag; |
| 4234 | |
| 4235 | vector<KeyCharacteristics> key_characteristics; |
| 4236 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4237 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4238 | .AesEncryptionKey(128) |
| 4239 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4240 | .Padding(PaddingMode::NONE) |
| 4241 | .Authorization(unknown_param), |
| 4242 | &key_blob_, &key_characteristics)); |
| 4243 | ASSERT_GT(key_blob_.size(), 0U); |
| 4244 | |
| 4245 | // Unknown tags should not be returned in key characteristics. |
| 4246 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(key_characteristics); |
| 4247 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(key_characteristics); |
| 4248 | EXPECT_EQ(hw_enforced.find(unknown_tag), -1); |
| 4249 | EXPECT_EQ(sw_enforced.find(unknown_tag), -1); |
| 4250 | |
| 4251 | // Encrypt without mentioning the unknown parameter. |
| 4252 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 4253 | string message = "12345678901234567890123456789012"; |
| 4254 | string ciphertext = EncryptMessage(message, params); |
| 4255 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 4256 | |
| 4257 | // Decrypt including the unknown parameter. |
| 4258 | auto decrypt_params = AuthorizationSetBuilder() |
| 4259 | .BlockMode(BlockMode::ECB) |
| 4260 | .Padding(PaddingMode::NONE) |
| 4261 | .Authorization(unknown_param); |
| 4262 | string plaintext = DecryptMessage(ciphertext, decrypt_params); |
| 4263 | EXPECT_EQ(message, plaintext); |
| 4264 | } |
| 4265 | |
| 4266 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4267 | * EncryptionOperationsTest.AesWrongMode |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4268 | * |
| 4269 | * Verifies that AES encryption fails in the correct way when an unauthorized mode is specified. |
| 4270 | */ |
| 4271 | TEST_P(EncryptionOperationsTest, AesWrongMode) { |
| 4272 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4273 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4274 | .AesEncryptionKey(128) |
| 4275 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4276 | .Padding(PaddingMode::NONE))); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4277 | ASSERT_GT(key_blob_.size(), 0U); |
| 4278 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4279 | EXPECT_EQ( |
| 4280 | ErrorCode::INCOMPATIBLE_BLOCK_MODE, |
| 4281 | Begin(KeyPurpose::ENCRYPT, |
| 4282 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE))); |
| 4283 | } |
| 4284 | |
| 4285 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4286 | * EncryptionOperationsTest.AesWrongPadding |
| 4287 | * |
| 4288 | * Verifies that AES encryption fails in the correct way when an unauthorized padding is specified. |
| 4289 | */ |
| 4290 | TEST_P(EncryptionOperationsTest, AesWrongPadding) { |
| 4291 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4292 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4293 | .AesEncryptionKey(128) |
| 4294 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4295 | .Padding(PaddingMode::NONE))); |
| 4296 | ASSERT_GT(key_blob_.size(), 0U); |
| 4297 | |
| 4298 | EXPECT_EQ( |
| 4299 | ErrorCode::INCOMPATIBLE_PADDING_MODE, |
| 4300 | Begin(KeyPurpose::ENCRYPT, |
| 4301 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7))); |
| 4302 | } |
| 4303 | |
| 4304 | /* |
| 4305 | * EncryptionOperationsTest.AesInvalidParams |
| 4306 | * |
| 4307 | * Verifies that AES encryption fails in the correct way when an duplicate parameters are specified. |
| 4308 | */ |
| 4309 | TEST_P(EncryptionOperationsTest, AesInvalidParams) { |
| 4310 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4311 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4312 | .AesEncryptionKey(128) |
| 4313 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4314 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4315 | .Padding(PaddingMode::NONE) |
| 4316 | .Padding(PaddingMode::PKCS7))); |
| 4317 | ASSERT_GT(key_blob_.size(), 0U); |
| 4318 | |
| 4319 | auto result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 4320 | .BlockMode(BlockMode::CBC) |
| 4321 | .BlockMode(BlockMode::ECB) |
| 4322 | .Padding(PaddingMode::NONE)); |
| 4323 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_BLOCK_MODE || |
| 4324 | result == ErrorCode::UNSUPPORTED_BLOCK_MODE); |
| 4325 | |
| 4326 | result = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 4327 | .BlockMode(BlockMode::ECB) |
| 4328 | .Padding(PaddingMode::NONE) |
| 4329 | .Padding(PaddingMode::PKCS7)); |
| 4330 | EXPECT_TRUE(result == ErrorCode::INCOMPATIBLE_PADDING_MODE || |
| 4331 | result == ErrorCode::UNSUPPORTED_PADDING_MODE); |
| 4332 | } |
| 4333 | |
| 4334 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4335 | * EncryptionOperationsTest.AesWrongPurpose |
| 4336 | * |
| 4337 | * Verifies that AES encryption fails in the correct way when an unauthorized purpose is |
| 4338 | * specified. |
| 4339 | */ |
| 4340 | TEST_P(EncryptionOperationsTest, AesWrongPurpose) { |
| 4341 | auto err = GenerateKey(AuthorizationSetBuilder() |
| 4342 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4343 | .AesKey(128) |
| 4344 | .Authorization(TAG_PURPOSE, KeyPurpose::ENCRYPT) |
| 4345 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4346 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 4347 | .Padding(PaddingMode::NONE)); |
| 4348 | ASSERT_EQ(ErrorCode::OK, err) << "Got " << err; |
| 4349 | ASSERT_GT(key_blob_.size(), 0U); |
| 4350 | |
| 4351 | err = Begin(KeyPurpose::DECRYPT, AuthorizationSetBuilder() |
| 4352 | .BlockMode(BlockMode::GCM) |
| 4353 | .Padding(PaddingMode::NONE) |
| 4354 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 4355 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 4356 | |
| 4357 | CheckedDeleteKey(); |
| 4358 | |
| 4359 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4360 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4361 | .AesKey(128) |
| 4362 | .Authorization(TAG_PURPOSE, KeyPurpose::DECRYPT) |
| 4363 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4364 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 4365 | .Padding(PaddingMode::NONE))); |
| 4366 | |
| 4367 | err = Begin(KeyPurpose::ENCRYPT, AuthorizationSetBuilder() |
| 4368 | .BlockMode(BlockMode::GCM) |
| 4369 | .Padding(PaddingMode::NONE) |
| 4370 | .Authorization(TAG_MAC_LENGTH, 128)); |
| 4371 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, err) << "Got " << err; |
| 4372 | } |
| 4373 | |
| 4374 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4375 | * EncryptionOperationsTest.AesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4376 | * |
| 4377 | * Verifies that AES encryption fails in the correct way when provided an input that is not a |
| 4378 | * multiple of the block size and no padding is specified. |
| 4379 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4380 | TEST_P(EncryptionOperationsTest, AesEcbCbcNoPaddingWrongInputSize) { |
| 4381 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 4382 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4383 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4384 | .AesEncryptionKey(128) |
| 4385 | .Authorization(TAG_BLOCK_MODE, blockMode) |
| 4386 | .Padding(PaddingMode::NONE))); |
| 4387 | // Message is slightly shorter than two blocks. |
| 4388 | string message(16 * 2 - 1, 'a'); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4389 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4390 | auto params = AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 4391 | AuthorizationSet out_params; |
| 4392 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 4393 | string ciphertext; |
| 4394 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, &ciphertext)); |
| 4395 | EXPECT_EQ(0U, ciphertext.size()); |
| 4396 | |
| 4397 | CheckedDeleteKey(); |
| 4398 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * EncryptionOperationsTest.AesEcbPkcs7Padding |
| 4403 | * |
| 4404 | * Verifies that AES PKCS7 padding works for any message length. |
| 4405 | */ |
| 4406 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7Padding) { |
| 4407 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4408 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4409 | .AesEncryptionKey(128) |
| 4410 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4411 | .Padding(PaddingMode::PKCS7))); |
| 4412 | |
| 4413 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4414 | |
| 4415 | // Try various message lengths; all should work. |
| 4416 | for (size_t i = 0; i < 32; ++i) { |
| 4417 | string message(i, 'a'); |
| 4418 | string ciphertext = EncryptMessage(message, params); |
| 4419 | EXPECT_EQ(i + 16 - (i % 16), ciphertext.size()); |
| 4420 | string plaintext = DecryptMessage(ciphertext, params); |
| 4421 | EXPECT_EQ(message, plaintext); |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | /* |
| 4426 | * EncryptionOperationsTest.AesEcbWrongPadding |
| 4427 | * |
| 4428 | * Verifies that AES enryption fails in the correct way when an unauthorized padding mode is |
| 4429 | * specified. |
| 4430 | */ |
| 4431 | TEST_P(EncryptionOperationsTest, AesEcbWrongPadding) { |
| 4432 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4433 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4434 | .AesEncryptionKey(128) |
| 4435 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4436 | .Padding(PaddingMode::NONE))); |
| 4437 | |
| 4438 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4439 | |
| 4440 | // Try various message lengths; all should fail |
| 4441 | for (size_t i = 0; i < 32; ++i) { |
| 4442 | string message(i, 'a'); |
| 4443 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* |
| 4448 | * EncryptionOperationsTest.AesEcbPkcs7PaddingCorrupted |
| 4449 | * |
| 4450 | * Verifies that AES decryption fails in the correct way when the padding is corrupted. |
| 4451 | */ |
| 4452 | TEST_P(EncryptionOperationsTest, AesEcbPkcs7PaddingCorrupted) { |
| 4453 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4454 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4455 | .AesEncryptionKey(128) |
| 4456 | .Authorization(TAG_BLOCK_MODE, BlockMode::ECB) |
| 4457 | .Padding(PaddingMode::PKCS7))); |
| 4458 | |
| 4459 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 4460 | |
| 4461 | string message = "a"; |
| 4462 | string ciphertext = EncryptMessage(message, params); |
| 4463 | EXPECT_EQ(16U, ciphertext.size()); |
| 4464 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4465 | |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 4466 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 4467 | ++ciphertext[ciphertext.size() / 2]; |
| 4468 | |
| 4469 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 4470 | string plaintext; |
| 4471 | ErrorCode error = Finish(message, &plaintext); |
| 4472 | if (error == ErrorCode::INVALID_INPUT_LENGTH) { |
| 4473 | // This is the expected error, we can exit the test now. |
| 4474 | return; |
| 4475 | } else { |
| 4476 | // Very small chance we got valid decryption, so try again. |
| 4477 | ASSERT_EQ(error, ErrorCode::OK); |
| 4478 | } |
| 4479 | } |
| 4480 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | vector<uint8_t> CopyIv(const AuthorizationSet& set) { |
| 4484 | auto iv = set.GetTagValue(TAG_NONCE); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4485 | EXPECT_TRUE(iv); |
| 4486 | return iv->get(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * EncryptionOperationsTest.AesCtrRoundTripSuccess |
| 4491 | * |
| 4492 | * Verifies that AES CTR mode works. |
| 4493 | */ |
| 4494 | TEST_P(EncryptionOperationsTest, AesCtrRoundTripSuccess) { |
| 4495 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4496 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4497 | .AesEncryptionKey(128) |
| 4498 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 4499 | .Padding(PaddingMode::NONE))); |
| 4500 | |
| 4501 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 4502 | |
| 4503 | string message = "123"; |
| 4504 | AuthorizationSet out_params; |
| 4505 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 4506 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 4507 | EXPECT_EQ(16U, iv1.size()); |
| 4508 | |
| 4509 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 4510 | |
| 4511 | out_params.Clear(); |
| 4512 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 4513 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 4514 | EXPECT_EQ(16U, iv2.size()); |
| 4515 | |
| 4516 | // IVs should be random, so ciphertexts should differ. |
| 4517 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4518 | |
| 4519 | auto params_iv1 = |
| 4520 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv1); |
| 4521 | auto params_iv2 = |
| 4522 | AuthorizationSetBuilder().Authorizations(params).Authorization(TAG_NONCE, iv2); |
| 4523 | |
| 4524 | string plaintext = DecryptMessage(ciphertext1, params_iv1); |
| 4525 | EXPECT_EQ(message, plaintext); |
| 4526 | plaintext = DecryptMessage(ciphertext2, params_iv2); |
| 4527 | EXPECT_EQ(message, plaintext); |
| 4528 | |
| 4529 | // Using the wrong IV will result in a "valid" decryption, but the data will be garbage. |
| 4530 | plaintext = DecryptMessage(ciphertext1, params_iv2); |
| 4531 | EXPECT_NE(message, plaintext); |
| 4532 | plaintext = DecryptMessage(ciphertext2, params_iv1); |
| 4533 | EXPECT_NE(message, plaintext); |
| 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | * EncryptionOperationsTest.AesIncremental |
| 4538 | * |
| 4539 | * Verifies that AES works, all modes, when provided data in various size increments. |
| 4540 | */ |
| 4541 | TEST_P(EncryptionOperationsTest, AesIncremental) { |
| 4542 | auto block_modes = { |
| 4543 | BlockMode::ECB, |
| 4544 | BlockMode::CBC, |
| 4545 | BlockMode::CTR, |
| 4546 | BlockMode::GCM, |
| 4547 | }; |
| 4548 | |
| 4549 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4550 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4551 | .AesEncryptionKey(128) |
| 4552 | .BlockMode(block_modes) |
| 4553 | .Padding(PaddingMode::NONE) |
| 4554 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4555 | |
| 4556 | for (int increment = 1; increment <= 240; ++increment) { |
| 4557 | for (auto block_mode : block_modes) { |
| 4558 | string message(240, 'a'); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4559 | auto params = |
| 4560 | AuthorizationSetBuilder().BlockMode(block_mode).Padding(PaddingMode::NONE); |
| 4561 | if (block_mode == BlockMode::GCM) { |
| 4562 | params.Authorization(TAG_MAC_LENGTH, 128) /* for GCM */; |
| 4563 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4564 | |
| 4565 | AuthorizationSet output_params; |
| 4566 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &output_params)); |
| 4567 | |
| 4568 | string ciphertext; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4569 | string to_send; |
| 4570 | for (size_t i = 0; i < message.size(); i += increment) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4571 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4572 | } |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4573 | EXPECT_EQ(ErrorCode::OK, Finish(to_send, &ciphertext)) |
| 4574 | << "Error sending " << to_send << " with block mode " << block_mode; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4575 | |
| 4576 | switch (block_mode) { |
| 4577 | case BlockMode::GCM: |
| 4578 | EXPECT_EQ(message.size() + 16, ciphertext.size()); |
| 4579 | break; |
| 4580 | case BlockMode::CTR: |
| 4581 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 4582 | break; |
| 4583 | case BlockMode::CBC: |
| 4584 | case BlockMode::ECB: |
| 4585 | EXPECT_EQ(message.size() + message.size() % 16, ciphertext.size()); |
| 4586 | break; |
| 4587 | } |
| 4588 | |
| 4589 | auto iv = output_params.GetTagValue(TAG_NONCE); |
| 4590 | switch (block_mode) { |
| 4591 | case BlockMode::CBC: |
| 4592 | case BlockMode::GCM: |
| 4593 | case BlockMode::CTR: |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4594 | ASSERT_TRUE(iv) << "No IV for block mode " << block_mode; |
| 4595 | EXPECT_EQ(block_mode == BlockMode::GCM ? 12U : 16U, iv->get().size()); |
| 4596 | params.push_back(TAG_NONCE, iv->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4597 | break; |
| 4598 | |
| 4599 | case BlockMode::ECB: |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4600 | EXPECT_FALSE(iv) << "ECB mode should not generate IV"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4601 | break; |
| 4602 | } |
| 4603 | |
| 4604 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)) |
| 4605 | << "Decrypt begin() failed for block mode " << block_mode; |
| 4606 | |
| 4607 | string plaintext; |
| 4608 | for (size_t i = 0; i < ciphertext.size(); i += increment) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4609 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4610 | } |
| 4611 | ErrorCode error = Finish(to_send, &plaintext); |
| 4612 | ASSERT_EQ(ErrorCode::OK, error) << "Decryption failed for block mode " << block_mode |
| 4613 | << " and increment " << increment; |
| 4614 | if (error == ErrorCode::OK) { |
| 4615 | ASSERT_EQ(message, plaintext) << "Decryption didn't match for block mode " |
| 4616 | << block_mode << " and increment " << increment; |
| 4617 | } |
| 4618 | } |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | struct AesCtrSp80038aTestVector { |
| 4623 | const char* key; |
| 4624 | const char* nonce; |
| 4625 | const char* plaintext; |
| 4626 | const char* ciphertext; |
| 4627 | }; |
| 4628 | |
| 4629 | // These test vectors are taken from |
| 4630 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section F.5. |
| 4631 | static const AesCtrSp80038aTestVector kAesCtrSp80038aTestVectors[] = { |
| 4632 | // AES-128 |
| 4633 | { |
| 4634 | "2b7e151628aed2a6abf7158809cf4f3c", |
| 4635 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 4636 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 4637 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 4638 | "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff" |
| 4639 | "5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee", |
| 4640 | }, |
| 4641 | // AES-192 |
| 4642 | { |
| 4643 | "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", |
| 4644 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 4645 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 4646 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 4647 | "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94" |
| 4648 | "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050", |
| 4649 | }, |
| 4650 | // AES-256 |
| 4651 | { |
| 4652 | "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", |
| 4653 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
| 4654 | "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" |
| 4655 | "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", |
| 4656 | "601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5" |
| 4657 | "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6", |
| 4658 | }, |
| 4659 | }; |
| 4660 | |
| 4661 | /* |
| 4662 | * EncryptionOperationsTest.AesCtrSp80038aTestVector |
| 4663 | * |
| 4664 | * Verifies AES CTR implementation against SP800-38A test vectors. |
| 4665 | */ |
| 4666 | TEST_P(EncryptionOperationsTest, AesCtrSp80038aTestVector) { |
| 4667 | std::vector<uint32_t> InvalidSizes = InvalidKeySizes(Algorithm::AES); |
| 4668 | for (size_t i = 0; i < 3; i++) { |
| 4669 | const AesCtrSp80038aTestVector& test(kAesCtrSp80038aTestVectors[i]); |
| 4670 | const string key = hex2str(test.key); |
| 4671 | if (std::find(InvalidSizes.begin(), InvalidSizes.end(), (key.size() * 8)) != |
| 4672 | InvalidSizes.end()) |
| 4673 | continue; |
| 4674 | const string nonce = hex2str(test.nonce); |
| 4675 | const string plaintext = hex2str(test.plaintext); |
| 4676 | const string ciphertext = hex2str(test.ciphertext); |
| 4677 | CheckAesCtrTestVector(key, nonce, plaintext, ciphertext); |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | /* |
| 4682 | * EncryptionOperationsTest.AesCtrIncompatiblePaddingMode |
| 4683 | * |
| 4684 | * Verifies that keymint rejects use of CTR mode with PKCS7 padding in the correct way. |
| 4685 | */ |
| 4686 | TEST_P(EncryptionOperationsTest, AesCtrIncompatiblePaddingMode) { |
| 4687 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4688 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4689 | .AesEncryptionKey(128) |
| 4690 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 4691 | .Padding(PaddingMode::PKCS7))); |
| 4692 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CTR).Padding(PaddingMode::NONE); |
| 4693 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4694 | } |
| 4695 | |
| 4696 | /* |
| 4697 | * EncryptionOperationsTest.AesCtrInvalidCallerNonce |
| 4698 | * |
| 4699 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 4700 | */ |
| 4701 | TEST_P(EncryptionOperationsTest, AesCtrInvalidCallerNonce) { |
| 4702 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4703 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4704 | .AesEncryptionKey(128) |
| 4705 | .Authorization(TAG_BLOCK_MODE, BlockMode::CTR) |
| 4706 | .Authorization(TAG_CALLER_NONCE) |
| 4707 | .Padding(PaddingMode::NONE))); |
| 4708 | |
| 4709 | auto params = AuthorizationSetBuilder() |
| 4710 | .BlockMode(BlockMode::CTR) |
| 4711 | .Padding(PaddingMode::NONE) |
| 4712 | .Authorization(TAG_NONCE, AidlBuf(string(1, 'a'))); |
| 4713 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4714 | |
| 4715 | params = AuthorizationSetBuilder() |
| 4716 | .BlockMode(BlockMode::CTR) |
| 4717 | .Padding(PaddingMode::NONE) |
| 4718 | .Authorization(TAG_NONCE, AidlBuf(string(15, 'a'))); |
| 4719 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4720 | |
| 4721 | params = AuthorizationSetBuilder() |
| 4722 | .BlockMode(BlockMode::CTR) |
| 4723 | .Padding(PaddingMode::NONE) |
| 4724 | .Authorization(TAG_NONCE, AidlBuf(string(17, 'a'))); |
| 4725 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 4726 | } |
| 4727 | |
| 4728 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 4729 | * EncryptionOperationsTest.AesCbcRoundTripSuccess |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4730 | * |
| 4731 | * Verifies that keymint fails correctly when the user supplies an incorrect-size nonce. |
| 4732 | */ |
| 4733 | TEST_P(EncryptionOperationsTest, AesCbcRoundTripSuccess) { |
| 4734 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4735 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4736 | .AesEncryptionKey(128) |
| 4737 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4738 | .Padding(PaddingMode::NONE))); |
| 4739 | // Two-block message. |
| 4740 | string message = "12345678901234567890123456789012"; |
| 4741 | auto params = AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 4742 | AuthorizationSet out_params; |
| 4743 | string ciphertext1 = EncryptMessage(message, params, &out_params); |
| 4744 | vector<uint8_t> iv1 = CopyIv(out_params); |
| 4745 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 4746 | |
| 4747 | out_params.Clear(); |
| 4748 | |
| 4749 | string ciphertext2 = EncryptMessage(message, params, &out_params); |
| 4750 | vector<uint8_t> iv2 = CopyIv(out_params); |
| 4751 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 4752 | |
| 4753 | // IVs should be random, so ciphertexts should differ. |
| 4754 | EXPECT_NE(ciphertext1, ciphertext2); |
| 4755 | |
| 4756 | params.push_back(TAG_NONCE, iv1); |
| 4757 | string plaintext = DecryptMessage(ciphertext1, params); |
| 4758 | EXPECT_EQ(message, plaintext); |
| 4759 | } |
| 4760 | |
| 4761 | /* |
| 4762 | * EncryptionOperationsTest.AesCallerNonce |
| 4763 | * |
| 4764 | * Verifies that AES caller-provided nonces work correctly. |
| 4765 | */ |
| 4766 | TEST_P(EncryptionOperationsTest, AesCallerNonce) { |
| 4767 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4768 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4769 | .AesEncryptionKey(128) |
| 4770 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4771 | .Authorization(TAG_CALLER_NONCE) |
| 4772 | .Padding(PaddingMode::NONE))); |
| 4773 | |
| 4774 | string message = "12345678901234567890123456789012"; |
| 4775 | |
| 4776 | // Don't specify nonce, should get a random one. |
| 4777 | AuthorizationSetBuilder params = |
| 4778 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 4779 | AuthorizationSet out_params; |
| 4780 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 4781 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4782 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4783 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4784 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4785 | string plaintext = DecryptMessage(ciphertext, params); |
| 4786 | EXPECT_EQ(message, plaintext); |
| 4787 | |
| 4788 | // Now specify a nonce, should also work. |
| 4789 | params = AuthorizationSetBuilder() |
| 4790 | .BlockMode(BlockMode::CBC) |
| 4791 | .Padding(PaddingMode::NONE) |
| 4792 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 4793 | out_params.Clear(); |
| 4794 | ciphertext = EncryptMessage(message, params, &out_params); |
| 4795 | |
| 4796 | // Decrypt with correct nonce. |
| 4797 | plaintext = DecryptMessage(ciphertext, params); |
| 4798 | EXPECT_EQ(message, plaintext); |
| 4799 | |
| 4800 | // Try with wrong nonce. |
| 4801 | params = AuthorizationSetBuilder() |
| 4802 | .BlockMode(BlockMode::CBC) |
| 4803 | .Padding(PaddingMode::NONE) |
| 4804 | .Authorization(TAG_NONCE, AidlBuf("aaaaaaaaaaaaaaaa")); |
| 4805 | plaintext = DecryptMessage(ciphertext, params); |
| 4806 | EXPECT_NE(message, plaintext); |
| 4807 | } |
| 4808 | |
| 4809 | /* |
| 4810 | * EncryptionOperationsTest.AesCallerNonceProhibited |
| 4811 | * |
| 4812 | * Verifies that caller-provided nonces are not permitted when not specified in the key |
| 4813 | * authorizations. |
| 4814 | */ |
| 4815 | TEST_P(EncryptionOperationsTest, AesCallerNonceProhibited) { |
| 4816 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4817 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4818 | .AesEncryptionKey(128) |
| 4819 | .Authorization(TAG_BLOCK_MODE, BlockMode::CBC) |
| 4820 | .Padding(PaddingMode::NONE))); |
| 4821 | |
| 4822 | string message = "12345678901234567890123456789012"; |
| 4823 | |
| 4824 | // Don't specify nonce, should get a random one. |
| 4825 | AuthorizationSetBuilder params = |
| 4826 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 4827 | AuthorizationSet out_params; |
| 4828 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 4829 | EXPECT_EQ(message.size(), ciphertext.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4830 | EXPECT_EQ(16U, out_params.GetTagValue(TAG_NONCE)->get().size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4831 | |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 4832 | params.push_back(TAG_NONCE, out_params.GetTagValue(TAG_NONCE)->get()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4833 | string plaintext = DecryptMessage(ciphertext, params); |
| 4834 | EXPECT_EQ(message, plaintext); |
| 4835 | |
| 4836 | // Now specify a nonce, should fail |
| 4837 | params = AuthorizationSetBuilder() |
| 4838 | .BlockMode(BlockMode::CBC) |
| 4839 | .Padding(PaddingMode::NONE) |
| 4840 | .Authorization(TAG_NONCE, AidlBuf("abcdefghijklmnop")); |
| 4841 | out_params.Clear(); |
| 4842 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, Begin(KeyPurpose::ENCRYPT, params, &out_params)); |
| 4843 | } |
| 4844 | |
| 4845 | /* |
| 4846 | * EncryptionOperationsTest.AesGcmRoundTripSuccess |
| 4847 | * |
| 4848 | * Verifies that AES GCM mode works. |
| 4849 | */ |
| 4850 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripSuccess) { |
| 4851 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4852 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4853 | .AesEncryptionKey(128) |
| 4854 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4855 | .Padding(PaddingMode::NONE) |
| 4856 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4857 | |
| 4858 | string aad = "foobar"; |
| 4859 | string message = "123456789012345678901234567890123456"; |
| 4860 | |
| 4861 | auto begin_params = AuthorizationSetBuilder() |
| 4862 | .BlockMode(BlockMode::GCM) |
| 4863 | .Padding(PaddingMode::NONE) |
| 4864 | .Authorization(TAG_MAC_LENGTH, 128); |
| 4865 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4866 | // Encrypt |
| 4867 | AuthorizationSet begin_out_params; |
| 4868 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 4869 | << "Begin encrypt"; |
| 4870 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4871 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 4872 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4873 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 4874 | |
| 4875 | // Grab nonce |
| 4876 | begin_params.push_back(begin_out_params); |
| 4877 | |
| 4878 | // Decrypt. |
| 4879 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4880 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4881 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4882 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4883 | EXPECT_EQ(message.length(), plaintext.length()); |
| 4884 | EXPECT_EQ(message, plaintext); |
| 4885 | } |
| 4886 | |
| 4887 | /* |
| 4888 | * EncryptionOperationsTest.AesGcmRoundTripWithDelaySuccess |
| 4889 | * |
| 4890 | * Verifies that AES GCM mode works, even when there's a long delay |
| 4891 | * between operations. |
| 4892 | */ |
| 4893 | TEST_P(EncryptionOperationsTest, AesGcmRoundTripWithDelaySuccess) { |
| 4894 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4895 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4896 | .AesEncryptionKey(128) |
| 4897 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4898 | .Padding(PaddingMode::NONE) |
| 4899 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4900 | |
| 4901 | string aad = "foobar"; |
| 4902 | string message = "123456789012345678901234567890123456"; |
| 4903 | |
| 4904 | auto begin_params = AuthorizationSetBuilder() |
| 4905 | .BlockMode(BlockMode::GCM) |
| 4906 | .Padding(PaddingMode::NONE) |
| 4907 | .Authorization(TAG_MAC_LENGTH, 128); |
| 4908 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4909 | // Encrypt |
| 4910 | AuthorizationSet begin_out_params; |
| 4911 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)) |
| 4912 | << "Begin encrypt"; |
| 4913 | string ciphertext; |
| 4914 | AuthorizationSet update_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4915 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4916 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4917 | ASSERT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4918 | |
| 4919 | ASSERT_EQ(ciphertext.length(), message.length() + 16); |
| 4920 | |
| 4921 | // Grab nonce |
| 4922 | begin_params.push_back(begin_out_params); |
| 4923 | |
| 4924 | // Decrypt. |
| 4925 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)) << "Begin decrypt"; |
| 4926 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4927 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4928 | sleep(5); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 4929 | ASSERT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4930 | sleep(5); |
| 4931 | EXPECT_EQ(ErrorCode::OK, Finish("", &plaintext)); |
| 4932 | EXPECT_EQ(message.length(), plaintext.length()); |
| 4933 | EXPECT_EQ(message, plaintext); |
| 4934 | } |
| 4935 | |
| 4936 | /* |
| 4937 | * EncryptionOperationsTest.AesGcmDifferentNonces |
| 4938 | * |
| 4939 | * Verifies that encrypting the same data with different nonces produces different outputs. |
| 4940 | */ |
| 4941 | TEST_P(EncryptionOperationsTest, AesGcmDifferentNonces) { |
| 4942 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4943 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4944 | .AesEncryptionKey(128) |
| 4945 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4946 | .Padding(PaddingMode::NONE) |
| 4947 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 4948 | .Authorization(TAG_CALLER_NONCE))); |
| 4949 | |
| 4950 | string aad = "foobar"; |
| 4951 | string message = "123456789012345678901234567890123456"; |
| 4952 | string nonce1 = "000000000000"; |
| 4953 | string nonce2 = "111111111111"; |
| 4954 | string nonce3 = "222222222222"; |
| 4955 | |
| 4956 | string ciphertext1 = |
| 4957 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce1)); |
| 4958 | string ciphertext2 = |
| 4959 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce2)); |
| 4960 | string ciphertext3 = |
| 4961 | EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128, AidlBuf(nonce3)); |
| 4962 | |
| 4963 | ASSERT_NE(ciphertext1, ciphertext2); |
| 4964 | ASSERT_NE(ciphertext1, ciphertext3); |
| 4965 | ASSERT_NE(ciphertext2, ciphertext3); |
| 4966 | } |
| 4967 | |
| 4968 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 4969 | * EncryptionOperationsTest.AesGcmDifferentAutoNonces |
| 4970 | * |
| 4971 | * Verifies that encrypting the same data with KeyMint generated nonces produces different outputs. |
| 4972 | */ |
| 4973 | TEST_P(EncryptionOperationsTest, AesGcmDifferentAutoNonces) { |
| 4974 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 4975 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 4976 | .AesEncryptionKey(128) |
| 4977 | .Authorization(TAG_BLOCK_MODE, BlockMode::GCM) |
| 4978 | .Padding(PaddingMode::NONE) |
| 4979 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 4980 | |
| 4981 | string aad = "foobar"; |
| 4982 | string message = "123456789012345678901234567890123456"; |
| 4983 | |
| 4984 | string ciphertext1 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 4985 | string ciphertext2 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 4986 | string ciphertext3 = EncryptMessage(message, BlockMode::GCM, PaddingMode::NONE, 128); |
| 4987 | |
| 4988 | ASSERT_NE(ciphertext1, ciphertext2); |
| 4989 | ASSERT_NE(ciphertext1, ciphertext3); |
| 4990 | ASSERT_NE(ciphertext2, ciphertext3); |
| 4991 | } |
| 4992 | |
| 4993 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 4994 | * EncryptionOperationsTest.AesGcmTooShortTag |
| 4995 | * |
| 4996 | * Verifies that AES GCM mode fails correctly when a too-short tag length is specified. |
| 4997 | */ |
| 4998 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTag) { |
| 4999 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5000 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5001 | .AesEncryptionKey(128) |
| 5002 | .BlockMode(BlockMode::GCM) |
| 5003 | .Padding(PaddingMode::NONE) |
| 5004 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5005 | string message = "123456789012345678901234567890123456"; |
| 5006 | auto params = AuthorizationSetBuilder() |
| 5007 | .BlockMode(BlockMode::GCM) |
| 5008 | .Padding(PaddingMode::NONE) |
| 5009 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5010 | |
| 5011 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::ENCRYPT, params)); |
| 5012 | } |
| 5013 | |
| 5014 | /* |
| 5015 | * EncryptionOperationsTest.AesGcmTooShortTagOnDecrypt |
| 5016 | * |
| 5017 | * Verifies that AES GCM mode fails correctly when a too-short tag is provided to decryption. |
| 5018 | */ |
| 5019 | TEST_P(EncryptionOperationsTest, AesGcmTooShortTagOnDecrypt) { |
| 5020 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5021 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5022 | .AesEncryptionKey(128) |
| 5023 | .BlockMode(BlockMode::GCM) |
| 5024 | .Padding(PaddingMode::NONE) |
| 5025 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5026 | string aad = "foobar"; |
| 5027 | string message = "123456789012345678901234567890123456"; |
| 5028 | auto params = AuthorizationSetBuilder() |
| 5029 | .BlockMode(BlockMode::GCM) |
| 5030 | .Padding(PaddingMode::NONE) |
| 5031 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5032 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5033 | // Encrypt |
| 5034 | AuthorizationSet begin_out_params; |
| 5035 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 5036 | EXPECT_EQ(1U, begin_out_params.size()); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 5037 | ASSERT_TRUE(begin_out_params.GetTagValue(TAG_NONCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5038 | |
| 5039 | AuthorizationSet finish_out_params; |
| 5040 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5041 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5042 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5043 | |
| 5044 | params = AuthorizationSetBuilder() |
| 5045 | .Authorizations(begin_out_params) |
| 5046 | .BlockMode(BlockMode::GCM) |
| 5047 | .Padding(PaddingMode::NONE) |
| 5048 | .Authorization(TAG_MAC_LENGTH, 96); |
| 5049 | |
| 5050 | // Decrypt. |
| 5051 | EXPECT_EQ(ErrorCode::INVALID_MAC_LENGTH, Begin(KeyPurpose::DECRYPT, params)); |
| 5052 | } |
| 5053 | |
| 5054 | /* |
| 5055 | * EncryptionOperationsTest.AesGcmCorruptKey |
| 5056 | * |
| 5057 | * Verifies that AES GCM mode fails correctly when the decryption key is incorrect. |
| 5058 | */ |
| 5059 | TEST_P(EncryptionOperationsTest, AesGcmCorruptKey) { |
| 5060 | const uint8_t nonce_bytes[] = { |
| 5061 | 0xb7, 0x94, 0x37, 0xae, 0x08, 0xff, 0x35, 0x5d, 0x7d, 0x8a, 0x4d, 0x0f, |
| 5062 | }; |
| 5063 | string nonce = make_string(nonce_bytes); |
| 5064 | const uint8_t ciphertext_bytes[] = { |
| 5065 | 0xb3, 0xf6, 0x79, 0x9e, 0x8f, 0x93, 0x26, 0xf2, 0xdf, 0x1e, 0x80, 0xfc, |
| 5066 | 0xd2, 0xcb, 0x16, 0xd7, 0x8c, 0x9d, 0xc7, 0xcc, 0x14, 0xbb, 0x67, 0x78, |
| 5067 | 0x62, 0xdc, 0x6c, 0x63, 0x9b, 0x3a, 0x63, 0x38, 0xd2, 0x4b, 0x31, 0x2d, |
| 5068 | 0x39, 0x89, 0xe5, 0x92, 0x0b, 0x5d, 0xbf, 0xc9, 0x76, 0x76, 0x5e, 0xfb, |
| 5069 | 0xfe, 0x57, 0xbb, 0x38, 0x59, 0x40, 0xa7, 0xa4, 0x3b, 0xdf, 0x05, 0xbd, |
| 5070 | 0xda, 0xe3, 0xc9, 0xd6, 0xa2, 0xfb, 0xbd, 0xfc, 0xc0, 0xcb, 0xa0, |
| 5071 | }; |
| 5072 | string ciphertext = make_string(ciphertext_bytes); |
| 5073 | |
| 5074 | auto params = AuthorizationSetBuilder() |
| 5075 | .BlockMode(BlockMode::GCM) |
| 5076 | .Padding(PaddingMode::NONE) |
| 5077 | .Authorization(TAG_MAC_LENGTH, 128) |
| 5078 | .Authorization(TAG_NONCE, nonce.data(), nonce.size()); |
| 5079 | |
| 5080 | auto import_params = AuthorizationSetBuilder() |
| 5081 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5082 | .AesEncryptionKey(128) |
| 5083 | .BlockMode(BlockMode::GCM) |
| 5084 | .Padding(PaddingMode::NONE) |
| 5085 | .Authorization(TAG_CALLER_NONCE) |
| 5086 | .Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 5087 | |
| 5088 | // Import correct key and decrypt |
| 5089 | const uint8_t key_bytes[] = { |
| 5090 | 0xba, 0x76, 0x35, 0x4f, 0x0a, 0xed, 0x6e, 0x8d, |
| 5091 | 0x91, 0xf4, 0x5c, 0x4f, 0xf5, 0xa0, 0x62, 0xdb, |
| 5092 | }; |
| 5093 | string key = make_string(key_bytes); |
| 5094 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5095 | string plaintext = DecryptMessage(ciphertext, params); |
| 5096 | CheckedDeleteKey(); |
| 5097 | |
| 5098 | // Corrupt key and attempt to decrypt |
| 5099 | key[0] = 0; |
| 5100 | ASSERT_EQ(ErrorCode::OK, ImportKey(import_params, KeyFormat::RAW, key)); |
| 5101 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
| 5102 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
| 5103 | CheckedDeleteKey(); |
| 5104 | } |
| 5105 | |
| 5106 | /* |
| 5107 | * EncryptionOperationsTest.AesGcmAadNoData |
| 5108 | * |
| 5109 | * Verifies that AES GCM mode works when provided additional authenticated data, but no data to |
| 5110 | * encrypt. |
| 5111 | */ |
| 5112 | TEST_P(EncryptionOperationsTest, AesGcmAadNoData) { |
| 5113 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5114 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5115 | .AesEncryptionKey(128) |
| 5116 | .BlockMode(BlockMode::GCM) |
| 5117 | .Padding(PaddingMode::NONE) |
| 5118 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5119 | |
| 5120 | string aad = "1234567890123456"; |
| 5121 | auto params = AuthorizationSetBuilder() |
| 5122 | .BlockMode(BlockMode::GCM) |
| 5123 | .Padding(PaddingMode::NONE) |
| 5124 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5125 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5126 | // Encrypt |
| 5127 | AuthorizationSet begin_out_params; |
| 5128 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
| 5129 | string ciphertext; |
| 5130 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5131 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
| 5132 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5133 | EXPECT_TRUE(finish_out_params.empty()); |
| 5134 | |
| 5135 | // Grab nonce |
| 5136 | params.push_back(begin_out_params); |
| 5137 | |
| 5138 | // Decrypt. |
| 5139 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5140 | ASSERT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5141 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5142 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5143 | |
| 5144 | EXPECT_TRUE(finish_out_params.empty()); |
| 5145 | |
| 5146 | EXPECT_EQ("", plaintext); |
| 5147 | } |
| 5148 | |
| 5149 | /* |
| 5150 | * EncryptionOperationsTest.AesGcmMultiPartAad |
| 5151 | * |
| 5152 | * Verifies that AES GCM mode works when provided additional authenticated data in multiple |
| 5153 | * chunks. |
| 5154 | */ |
| 5155 | TEST_P(EncryptionOperationsTest, AesGcmMultiPartAad) { |
| 5156 | const size_t tag_bits = 128; |
| 5157 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5158 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5159 | .AesEncryptionKey(128) |
| 5160 | .BlockMode(BlockMode::GCM) |
| 5161 | .Padding(PaddingMode::NONE) |
| 5162 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5163 | |
| 5164 | string message = "123456789012345678901234567890123456"; |
| 5165 | auto begin_params = AuthorizationSetBuilder() |
| 5166 | .BlockMode(BlockMode::GCM) |
| 5167 | .Padding(PaddingMode::NONE) |
| 5168 | .Authorization(TAG_MAC_LENGTH, tag_bits); |
| 5169 | AuthorizationSet begin_out_params; |
| 5170 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5171 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 5172 | |
| 5173 | // No data, AAD only. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5174 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
| 5175 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5176 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5177 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 5178 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5179 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5180 | // Expect 128-bit (16-byte) tag appended to ciphertext. |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5181 | EXPECT_EQ(message.size() + (tag_bits / 8), ciphertext.size()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5182 | |
| 5183 | // Grab nonce. |
| 5184 | begin_params.push_back(begin_out_params); |
| 5185 | |
| 5186 | // Decrypt |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5187 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5188 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foofoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5189 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5190 | EXPECT_EQ(ErrorCode::OK, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5191 | EXPECT_EQ(message, plaintext); |
| 5192 | } |
| 5193 | |
| 5194 | /* |
| 5195 | * EncryptionOperationsTest.AesGcmAadOutOfOrder |
| 5196 | * |
| 5197 | * Verifies that AES GCM mode fails correctly when given AAD after data to encipher. |
| 5198 | */ |
| 5199 | TEST_P(EncryptionOperationsTest, AesGcmAadOutOfOrder) { |
| 5200 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5201 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5202 | .AesEncryptionKey(128) |
| 5203 | .BlockMode(BlockMode::GCM) |
| 5204 | .Padding(PaddingMode::NONE) |
| 5205 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5206 | |
| 5207 | string message = "123456789012345678901234567890123456"; |
| 5208 | auto begin_params = AuthorizationSetBuilder() |
| 5209 | .BlockMode(BlockMode::GCM) |
| 5210 | .Padding(PaddingMode::NONE) |
| 5211 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5212 | AuthorizationSet begin_out_params; |
| 5213 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5214 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
| 5215 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5216 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5217 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5218 | EXPECT_EQ(ErrorCode::OK, Update(message, &ciphertext)); |
| 5219 | EXPECT_EQ(ErrorCode::INVALID_TAG, UpdateAad("foo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5220 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5221 | // The failure should have already cancelled the operation. |
| 5222 | EXPECT_EQ(ErrorCode::INVALID_OPERATION_HANDLE, Abort()); |
| 5223 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5224 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | /* |
| 5228 | * EncryptionOperationsTest.AesGcmBadAad |
| 5229 | * |
| 5230 | * Verifies that AES GCM decryption fails correctly when additional authenticated date is wrong. |
| 5231 | */ |
| 5232 | TEST_P(EncryptionOperationsTest, AesGcmBadAad) { |
| 5233 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5234 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5235 | .AesEncryptionKey(128) |
| 5236 | .BlockMode(BlockMode::GCM) |
| 5237 | .Padding(PaddingMode::NONE) |
| 5238 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5239 | |
| 5240 | string message = "12345678901234567890123456789012"; |
| 5241 | auto begin_params = AuthorizationSetBuilder() |
| 5242 | .BlockMode(BlockMode::GCM) |
| 5243 | .Padding(PaddingMode::NONE) |
| 5244 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5245 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5246 | // Encrypt |
| 5247 | AuthorizationSet begin_out_params; |
| 5248 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5249 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5250 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5251 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5252 | |
| 5253 | // Grab nonce |
| 5254 | begin_params.push_back(begin_out_params); |
| 5255 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5256 | // Decrypt. |
| 5257 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5258 | EXPECT_EQ(ErrorCode::OK, UpdateAad("barfoo")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5259 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5260 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5261 | } |
| 5262 | |
| 5263 | /* |
| 5264 | * EncryptionOperationsTest.AesGcmWrongNonce |
| 5265 | * |
| 5266 | * Verifies that AES GCM decryption fails correctly when the nonce is incorrect. |
| 5267 | */ |
| 5268 | TEST_P(EncryptionOperationsTest, AesGcmWrongNonce) { |
| 5269 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5270 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5271 | .AesEncryptionKey(128) |
| 5272 | .BlockMode(BlockMode::GCM) |
| 5273 | .Padding(PaddingMode::NONE) |
| 5274 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5275 | |
| 5276 | string message = "12345678901234567890123456789012"; |
| 5277 | auto begin_params = AuthorizationSetBuilder() |
| 5278 | .BlockMode(BlockMode::GCM) |
| 5279 | .Padding(PaddingMode::NONE) |
| 5280 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5281 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5282 | // Encrypt |
| 5283 | AuthorizationSet begin_out_params; |
| 5284 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5285 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5286 | string ciphertext; |
| 5287 | AuthorizationSet finish_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5288 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5289 | |
| 5290 | // Wrong nonce |
| 5291 | begin_params.push_back(TAG_NONCE, AidlBuf("123456789012")); |
| 5292 | |
| 5293 | // Decrypt. |
| 5294 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5295 | EXPECT_EQ(ErrorCode::OK, UpdateAad("foobar")); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5296 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5297 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5298 | |
| 5299 | // With wrong nonce, should have gotten garbage plaintext (or none). |
| 5300 | EXPECT_NE(message, plaintext); |
| 5301 | } |
| 5302 | |
| 5303 | /* |
| 5304 | * EncryptionOperationsTest.AesGcmCorruptTag |
| 5305 | * |
| 5306 | * Verifies that AES GCM decryption fails correctly when the tag is wrong. |
| 5307 | */ |
| 5308 | TEST_P(EncryptionOperationsTest, AesGcmCorruptTag) { |
| 5309 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5310 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5311 | .AesEncryptionKey(128) |
| 5312 | .BlockMode(BlockMode::GCM) |
| 5313 | .Padding(PaddingMode::NONE) |
| 5314 | .Authorization(TAG_MIN_MAC_LENGTH, 128))); |
| 5315 | |
| 5316 | string aad = "1234567890123456"; |
| 5317 | string message = "123456789012345678901234567890123456"; |
| 5318 | |
| 5319 | auto params = AuthorizationSetBuilder() |
| 5320 | .BlockMode(BlockMode::GCM) |
| 5321 | .Padding(PaddingMode::NONE) |
| 5322 | .Authorization(TAG_MAC_LENGTH, 128); |
| 5323 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5324 | // Encrypt |
| 5325 | AuthorizationSet begin_out_params; |
| 5326 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &begin_out_params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5327 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5328 | string ciphertext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5329 | EXPECT_EQ(ErrorCode::OK, Finish(message, &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5330 | |
| 5331 | // Corrupt tag |
| 5332 | ++(*ciphertext.rbegin()); |
| 5333 | |
| 5334 | // Grab nonce |
| 5335 | params.push_back(begin_out_params); |
| 5336 | |
| 5337 | // Decrypt. |
| 5338 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5339 | EXPECT_EQ(ErrorCode::OK, UpdateAad(aad)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5340 | string plaintext; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5341 | EXPECT_EQ(ErrorCode::VERIFICATION_FAILED, Finish(ciphertext, &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | /* |
| 5345 | * EncryptionOperationsTest.TripleDesEcbRoundTripSuccess |
| 5346 | * |
| 5347 | * Verifies that 3DES is basically functional. |
| 5348 | */ |
| 5349 | TEST_P(EncryptionOperationsTest, TripleDesEcbRoundTripSuccess) { |
| 5350 | auto auths = AuthorizationSetBuilder() |
| 5351 | .TripleDesEncryptionKey(168) |
| 5352 | .BlockMode(BlockMode::ECB) |
| 5353 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5354 | .Padding(PaddingMode::NONE); |
| 5355 | |
| 5356 | ASSERT_EQ(ErrorCode::OK, GenerateKey(auths)); |
| 5357 | // Two-block message. |
| 5358 | string message = "1234567890123456"; |
| 5359 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 5360 | string ciphertext1 = EncryptMessage(message, inParams); |
| 5361 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5362 | |
| 5363 | string ciphertext2 = EncryptMessage(string(message), inParams); |
| 5364 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 5365 | |
| 5366 | // ECB is deterministic. |
| 5367 | EXPECT_EQ(ciphertext1, ciphertext2); |
| 5368 | |
| 5369 | string plaintext = DecryptMessage(ciphertext1, inParams); |
| 5370 | EXPECT_EQ(message, plaintext); |
| 5371 | } |
| 5372 | |
| 5373 | /* |
| 5374 | * EncryptionOperationsTest.TripleDesEcbNotAuthorized |
| 5375 | * |
| 5376 | * Verifies that CBC keys reject ECB usage. |
| 5377 | */ |
| 5378 | TEST_P(EncryptionOperationsTest, TripleDesEcbNotAuthorized) { |
| 5379 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5380 | .TripleDesEncryptionKey(168) |
| 5381 | .BlockMode(BlockMode::CBC) |
| 5382 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5383 | .Padding(PaddingMode::NONE))); |
| 5384 | |
| 5385 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 5386 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
| 5387 | } |
| 5388 | |
| 5389 | /* |
| 5390 | * EncryptionOperationsTest.TripleDesEcbPkcs7Padding |
| 5391 | * |
| 5392 | * Tests ECB mode with PKCS#7 padding, various message sizes. |
| 5393 | */ |
| 5394 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7Padding) { |
| 5395 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5396 | .TripleDesEncryptionKey(168) |
| 5397 | .BlockMode(BlockMode::ECB) |
| 5398 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5399 | .Padding(PaddingMode::PKCS7))); |
| 5400 | |
| 5401 | for (size_t i = 0; i < 32; ++i) { |
| 5402 | string message(i, 'a'); |
| 5403 | auto inParams = |
| 5404 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5405 | string ciphertext = EncryptMessage(message, inParams); |
| 5406 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 5407 | string plaintext = DecryptMessage(ciphertext, inParams); |
| 5408 | EXPECT_EQ(message, plaintext); |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | /* |
| 5413 | * EncryptionOperationsTest.TripleDesEcbNoPaddingKeyWithPkcs7Padding |
| 5414 | * |
| 5415 | * Verifies that keys configured for no padding reject PKCS7 padding |
| 5416 | */ |
| 5417 | TEST_P(EncryptionOperationsTest, TripleDesEcbNoPaddingKeyWithPkcs7Padding) { |
| 5418 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5419 | .TripleDesEncryptionKey(168) |
| 5420 | .BlockMode(BlockMode::ECB) |
| 5421 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5422 | .Padding(PaddingMode::NONE))); |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 5423 | auto inParams = AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::PKCS7); |
| 5424 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, inParams)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | /* |
| 5428 | * EncryptionOperationsTest.TripleDesEcbPkcs7PaddingCorrupted |
| 5429 | * |
| 5430 | * Verifies that corrupted padding is detected. |
| 5431 | */ |
| 5432 | TEST_P(EncryptionOperationsTest, TripleDesEcbPkcs7PaddingCorrupted) { |
| 5433 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5434 | .TripleDesEncryptionKey(168) |
| 5435 | .BlockMode(BlockMode::ECB) |
| 5436 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5437 | .Padding(PaddingMode::PKCS7))); |
| 5438 | |
| 5439 | string message = "a"; |
| 5440 | string ciphertext = EncryptMessage(message, BlockMode::ECB, PaddingMode::PKCS7); |
| 5441 | EXPECT_EQ(8U, ciphertext.size()); |
| 5442 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5443 | |
| 5444 | AuthorizationSetBuilder begin_params; |
| 5445 | begin_params.push_back(TAG_BLOCK_MODE, BlockMode::ECB); |
| 5446 | begin_params.push_back(TAG_PADDING, PaddingMode::PKCS7); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 5447 | |
| 5448 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 5449 | ++ciphertext[ciphertext.size() / 2]; |
| 5450 | |
| 5451 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 5452 | string plaintext; |
| 5453 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 5454 | ErrorCode error = Finish(&plaintext); |
| 5455 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 5456 | // This is the expected error, we can exit the test now. |
| 5457 | return; |
| 5458 | } else { |
| 5459 | // Very small chance we got valid decryption, so try again. |
| 5460 | ASSERT_EQ(error, ErrorCode::OK); |
| 5461 | } |
| 5462 | } |
| 5463 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | struct TripleDesTestVector { |
| 5467 | const char* name; |
| 5468 | const KeyPurpose purpose; |
| 5469 | const BlockMode block_mode; |
| 5470 | const PaddingMode padding_mode; |
| 5471 | const char* key; |
| 5472 | const char* iv; |
| 5473 | const char* input; |
| 5474 | const char* output; |
| 5475 | }; |
| 5476 | |
| 5477 | // These test vectors are from NIST CAVP, plus a few custom variants to test padding, since all |
| 5478 | // of the NIST vectors are multiples of the block size. |
| 5479 | static const TripleDesTestVector kTripleDesTestVectors[] = { |
| 5480 | { |
| 5481 | "TECBMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 5482 | "a2b5bc67da13dc92cd9d344aa238544a0e1fa79ef76810cd", // key |
| 5483 | "", // IV |
| 5484 | "329d86bdf1bc5af4", // input |
| 5485 | "d946c2756d78633f", // output |
| 5486 | }, |
| 5487 | { |
| 5488 | "TECBMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 5489 | "49e692290d2a5e46bace79b9648a4c5d491004c262dc9d49", // key |
| 5490 | "", // IV |
| 5491 | "6b1540781b01ce1997adae102dbf3c5b", // input |
| 5492 | "4d0dc182d6e481ac4a3dc6ab6976ccae", // output |
| 5493 | }, |
| 5494 | { |
| 5495 | "TECBMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 5496 | "52daec2ac7dc1958377392682f37860b2cc1ea2304bab0e9", // key |
| 5497 | "", // IV |
| 5498 | "6daad94ce08acfe7", // input |
| 5499 | "660e7d32dcc90e79", // output |
| 5500 | }, |
| 5501 | { |
| 5502 | "TECBMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::ECB, PaddingMode::NONE, |
| 5503 | "7f8fe3d3f4a48394fb682c2919926d6ddfce8932529229ce", // key |
| 5504 | "", // IV |
| 5505 | "e9653a0a1f05d31b9acd12d73aa9879d", // input |
| 5506 | "9b2ae9d998efe62f1b592e7e1df8ff38", // output |
| 5507 | }, |
| 5508 | { |
| 5509 | "TCBCMMT3 Encrypt 0", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 5510 | "b5cb1504802326c73df186e3e352a20de643b0d63ee30e37", // key |
| 5511 | "43f791134c5647ba", // IV |
| 5512 | "dcc153cef81d6f24", // input |
| 5513 | "92538bd8af18d3ba", // output |
| 5514 | }, |
| 5515 | { |
| 5516 | "TCBCMMT3 Encrypt 1", KeyPurpose::ENCRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 5517 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 5518 | "c2e999cb6249023c", // IV |
| 5519 | "c689aee38a301bb316da75db36f110b5", // input |
| 5520 | "e9afaba5ec75ea1bbe65506655bb4ecb", // output |
| 5521 | }, |
| 5522 | { |
| 5523 | "TCBCMMT3 Encrypt 1 PKCS7 variant", KeyPurpose::ENCRYPT, BlockMode::CBC, |
| 5524 | PaddingMode::PKCS7, |
| 5525 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 5526 | "c2e999cb6249023c", // IV |
| 5527 | "c689aee38a301bb316da75db36f110b500", // input |
| 5528 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // output |
| 5529 | }, |
| 5530 | { |
| 5531 | "TCBCMMT3 Encrypt 1 PKCS7 decrypted", KeyPurpose::DECRYPT, BlockMode::CBC, |
| 5532 | PaddingMode::PKCS7, |
| 5533 | "a49d7564199e97cb529d2c9d97bf2f98d35edf57ba1f7358", // key |
| 5534 | "c2e999cb6249023c", // IV |
| 5535 | "e9afaba5ec75ea1bbe65506655bb4ecb825aa27ec0656156", // input |
| 5536 | "c689aee38a301bb316da75db36f110b500", // output |
| 5537 | }, |
| 5538 | { |
| 5539 | "TCBCMMT3 Decrypt 0", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 5540 | "5eb6040d46082c7aa7d06dfd08dfeac8c18364c1548c3ba1", // key |
| 5541 | "41746c7e442d3681", // IV |
| 5542 | "c53a7b0ec40600fe", // input |
| 5543 | "d4f00eb455de1034", // output |
| 5544 | }, |
| 5545 | { |
| 5546 | "TCBCMMT3 Decrypt 1", KeyPurpose::DECRYPT, BlockMode::CBC, PaddingMode::NONE, |
| 5547 | "5b1cce7c0dc1ec49130dfb4af45785ab9179e567f2c7d549", // key |
| 5548 | "3982bc02c3727d45", // IV |
| 5549 | "6006f10adef52991fcc777a1238bbb65", // input |
| 5550 | "edae09288e9e3bc05746d872b48e3b29", // output |
| 5551 | }, |
| 5552 | }; |
| 5553 | |
| 5554 | /* |
| 5555 | * EncryptionOperationsTest.TripleDesTestVector |
| 5556 | * |
| 5557 | * Verifies that NIST (plus a few extra) test vectors produce the correct results. |
| 5558 | */ |
| 5559 | TEST_P(EncryptionOperationsTest, TripleDesTestVector) { |
| 5560 | constexpr size_t num_tests = sizeof(kTripleDesTestVectors) / sizeof(TripleDesTestVector); |
| 5561 | for (auto* test = kTripleDesTestVectors; test < kTripleDesTestVectors + num_tests; ++test) { |
| 5562 | SCOPED_TRACE(test->name); |
| 5563 | CheckTripleDesTestVector(test->purpose, test->block_mode, test->padding_mode, |
| 5564 | hex2str(test->key), hex2str(test->iv), hex2str(test->input), |
| 5565 | hex2str(test->output)); |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | /* |
| 5570 | * EncryptionOperationsTest.TripleDesCbcRoundTripSuccess |
| 5571 | * |
| 5572 | * Validates CBC mode functionality. |
| 5573 | */ |
| 5574 | TEST_P(EncryptionOperationsTest, TripleDesCbcRoundTripSuccess) { |
| 5575 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5576 | .TripleDesEncryptionKey(168) |
| 5577 | .BlockMode(BlockMode::CBC) |
| 5578 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5579 | .Padding(PaddingMode::NONE))); |
| 5580 | |
| 5581 | ASSERT_GT(key_blob_.size(), 0U); |
| 5582 | |
| 5583 | // Two-block message. |
| 5584 | string message = "1234567890123456"; |
| 5585 | vector<uint8_t> iv1; |
| 5586 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv1); |
| 5587 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5588 | |
| 5589 | vector<uint8_t> iv2; |
| 5590 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv2); |
| 5591 | EXPECT_EQ(message.size(), ciphertext2.size()); |
| 5592 | |
| 5593 | // IVs should be random, so ciphertexts should differ. |
| 5594 | EXPECT_NE(iv1, iv2); |
| 5595 | EXPECT_NE(ciphertext1, ciphertext2); |
| 5596 | |
| 5597 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv1); |
| 5598 | EXPECT_EQ(message, plaintext); |
| 5599 | } |
| 5600 | |
| 5601 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5602 | * EncryptionOperationsTest.TripleDesInvalidCallerIv |
| 5603 | * |
| 5604 | * Validates that keymint fails correctly when the user supplies an incorrect-size IV. |
| 5605 | */ |
| 5606 | TEST_P(EncryptionOperationsTest, TripleDesInvalidCallerIv) { |
| 5607 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5608 | .TripleDesEncryptionKey(168) |
| 5609 | .BlockMode(BlockMode::CBC) |
| 5610 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5611 | .Authorization(TAG_CALLER_NONCE) |
| 5612 | .Padding(PaddingMode::NONE))); |
| 5613 | auto params = AuthorizationSetBuilder() |
| 5614 | .BlockMode(BlockMode::CBC) |
| 5615 | .Padding(PaddingMode::NONE) |
| 5616 | .Authorization(TAG_NONCE, AidlBuf("abcdefg")); |
| 5617 | EXPECT_EQ(ErrorCode::INVALID_NONCE, Begin(KeyPurpose::ENCRYPT, params)); |
| 5618 | } |
| 5619 | |
| 5620 | /* |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5621 | * EncryptionOperationsTest.TripleDesCallerIv |
| 5622 | * |
| 5623 | * Validates that 3DES keys can allow caller-specified IVs, and use them correctly. |
| 5624 | */ |
| 5625 | TEST_P(EncryptionOperationsTest, TripleDesCallerIv) { |
| 5626 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5627 | .TripleDesEncryptionKey(168) |
| 5628 | .BlockMode(BlockMode::CBC) |
| 5629 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5630 | .Authorization(TAG_CALLER_NONCE) |
| 5631 | .Padding(PaddingMode::NONE))); |
| 5632 | string message = "1234567890123456"; |
| 5633 | vector<uint8_t> iv; |
| 5634 | // Don't specify IV, should get a random one. |
| 5635 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 5636 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5637 | EXPECT_EQ(8U, iv.size()); |
| 5638 | |
| 5639 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 5640 | EXPECT_EQ(message, plaintext); |
| 5641 | |
| 5642 | // Now specify an IV, should also work. |
| 5643 | iv = AidlBuf("abcdefgh"); |
| 5644 | string ciphertext2 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, iv); |
| 5645 | |
| 5646 | // Decrypt with correct IV. |
| 5647 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, iv); |
| 5648 | EXPECT_EQ(message, plaintext); |
| 5649 | |
| 5650 | // Now try with wrong IV. |
| 5651 | plaintext = DecryptMessage(ciphertext2, BlockMode::CBC, PaddingMode::NONE, AidlBuf("aaaaaaaa")); |
| 5652 | EXPECT_NE(message, plaintext); |
| 5653 | } |
| 5654 | |
| 5655 | /* |
| 5656 | * EncryptionOperationsTest, TripleDesCallerNonceProhibited. |
| 5657 | * |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5658 | * 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] | 5659 | */ |
| 5660 | TEST_P(EncryptionOperationsTest, TripleDesCallerNonceProhibited) { |
| 5661 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5662 | .TripleDesEncryptionKey(168) |
| 5663 | .BlockMode(BlockMode::CBC) |
| 5664 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5665 | .Padding(PaddingMode::NONE))); |
| 5666 | |
| 5667 | string message = "12345678901234567890123456789012"; |
| 5668 | vector<uint8_t> iv; |
| 5669 | // Don't specify nonce, should get a random one. |
| 5670 | string ciphertext1 = EncryptMessage(message, BlockMode::CBC, PaddingMode::NONE, &iv); |
| 5671 | EXPECT_EQ(message.size(), ciphertext1.size()); |
| 5672 | EXPECT_EQ(8U, iv.size()); |
| 5673 | |
| 5674 | string plaintext = DecryptMessage(ciphertext1, BlockMode::CBC, PaddingMode::NONE, iv); |
| 5675 | EXPECT_EQ(message, plaintext); |
| 5676 | |
| 5677 | // Now specify a nonce, should fail. |
| 5678 | auto input_params = AuthorizationSetBuilder() |
| 5679 | .Authorization(TAG_NONCE, AidlBuf("abcdefgh")) |
| 5680 | .BlockMode(BlockMode::CBC) |
| 5681 | .Padding(PaddingMode::NONE); |
| 5682 | AuthorizationSet output_params; |
| 5683 | EXPECT_EQ(ErrorCode::CALLER_NONCE_PROHIBITED, |
| 5684 | Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 5685 | } |
| 5686 | |
| 5687 | /* |
| 5688 | * EncryptionOperationsTest.TripleDesCbcNotAuthorized |
| 5689 | * |
| 5690 | * Verifies that 3DES ECB-only keys do not allow CBC usage. |
| 5691 | */ |
| 5692 | TEST_P(EncryptionOperationsTest, TripleDesCbcNotAuthorized) { |
| 5693 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5694 | .TripleDesEncryptionKey(168) |
| 5695 | .BlockMode(BlockMode::ECB) |
| 5696 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5697 | .Padding(PaddingMode::NONE))); |
| 5698 | // Two-block message. |
| 5699 | string message = "1234567890123456"; |
| 5700 | auto begin_params = |
| 5701 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5702 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_BLOCK_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 5703 | } |
| 5704 | |
| 5705 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5706 | * EncryptionOperationsTest.TripleDesEcbCbcNoPaddingWrongInputSize |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5707 | * |
| 5708 | * Verifies that unpadded CBC operations reject inputs that are not a multiple of block size. |
| 5709 | */ |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5710 | TEST_P(EncryptionOperationsTest, TripleDesEcbCbcNoPaddingWrongInputSize) { |
| 5711 | for (BlockMode blockMode : {BlockMode::ECB, BlockMode::CBC}) { |
| 5712 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5713 | .TripleDesEncryptionKey(168) |
| 5714 | .BlockMode(blockMode) |
| 5715 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5716 | .Padding(PaddingMode::NONE))); |
| 5717 | // Message is slightly shorter than two blocks. |
| 5718 | string message = "123456789012345"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5719 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 5720 | auto begin_params = |
| 5721 | AuthorizationSetBuilder().BlockMode(blockMode).Padding(PaddingMode::NONE); |
| 5722 | AuthorizationSet output_params; |
| 5723 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, begin_params, &output_params)); |
| 5724 | string ciphertext; |
| 5725 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, Finish(message, "", &ciphertext)); |
| 5726 | |
| 5727 | CheckedDeleteKey(); |
| 5728 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | /* |
| 5732 | * EncryptionOperationsTest, TripleDesCbcPkcs7Padding. |
| 5733 | * |
| 5734 | * Verifies that PKCS7 padding works correctly in CBC mode. |
| 5735 | */ |
| 5736 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7Padding) { |
| 5737 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5738 | .TripleDesEncryptionKey(168) |
| 5739 | .BlockMode(BlockMode::CBC) |
| 5740 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5741 | .Padding(PaddingMode::PKCS7))); |
| 5742 | |
| 5743 | // Try various message lengths; all should work. |
| 5744 | for (size_t i = 0; i < 32; ++i) { |
| 5745 | string message(i, 'a'); |
| 5746 | vector<uint8_t> iv; |
| 5747 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 5748 | EXPECT_EQ(i + 8 - (i % 8), ciphertext.size()); |
| 5749 | string plaintext = DecryptMessage(ciphertext, BlockMode::CBC, PaddingMode::PKCS7, iv); |
| 5750 | EXPECT_EQ(message, plaintext); |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | /* |
| 5755 | * EncryptionOperationsTest.TripleDesCbcNoPaddingKeyWithPkcs7Padding |
| 5756 | * |
| 5757 | * Verifies that a key that requires PKCS7 padding cannot be used in unpadded mode. |
| 5758 | */ |
| 5759 | TEST_P(EncryptionOperationsTest, TripleDesCbcNoPaddingKeyWithPkcs7Padding) { |
| 5760 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5761 | .TripleDesEncryptionKey(168) |
| 5762 | .BlockMode(BlockMode::CBC) |
| 5763 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5764 | .Padding(PaddingMode::NONE))); |
| 5765 | |
| 5766 | // Try various message lengths; all should fail. |
| 5767 | for (size_t i = 0; i < 32; ++i) { |
| 5768 | auto begin_params = |
| 5769 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::PKCS7); |
| 5770 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PADDING_MODE, Begin(KeyPurpose::ENCRYPT, begin_params)); |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | /* |
| 5775 | * EncryptionOperationsTest.TripleDesCbcPkcs7PaddingCorrupted |
| 5776 | * |
| 5777 | * Verifies that corrupted PKCS7 padding is rejected during decryption. |
| 5778 | */ |
| 5779 | TEST_P(EncryptionOperationsTest, TripleDesCbcPkcs7PaddingCorrupted) { |
| 5780 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5781 | .TripleDesEncryptionKey(168) |
| 5782 | .BlockMode(BlockMode::CBC) |
| 5783 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5784 | .Padding(PaddingMode::PKCS7))); |
| 5785 | |
| 5786 | string message = "a"; |
| 5787 | vector<uint8_t> iv; |
| 5788 | string ciphertext = EncryptMessage(message, BlockMode::CBC, PaddingMode::PKCS7, &iv); |
| 5789 | EXPECT_EQ(8U, ciphertext.size()); |
| 5790 | EXPECT_NE(ciphertext, message); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5791 | |
| 5792 | auto begin_params = AuthorizationSetBuilder() |
| 5793 | .BlockMode(BlockMode::CBC) |
| 5794 | .Padding(PaddingMode::PKCS7) |
| 5795 | .Authorization(TAG_NONCE, iv); |
Seth Moore | 7a55ae3 | 2021-06-23 14:28:11 -0700 | [diff] [blame] | 5796 | |
| 5797 | for (size_t i = 0; i < kMaxPaddingCorruptionRetries; ++i) { |
| 5798 | ++ciphertext[ciphertext.size() / 2]; |
| 5799 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, begin_params)); |
| 5800 | string plaintext; |
| 5801 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext, &plaintext)); |
| 5802 | ErrorCode error = Finish(&plaintext); |
| 5803 | if (error == ErrorCode::INVALID_ARGUMENT) { |
| 5804 | // This is the expected error, we can exit the test now. |
| 5805 | return; |
| 5806 | } else { |
| 5807 | // Very small chance we got valid decryption, so try again. |
| 5808 | ASSERT_EQ(error, ErrorCode::OK); |
| 5809 | } |
| 5810 | } |
| 5811 | FAIL() << "Corrupt ciphertext should have failed to decrypt by now."; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | /* |
| 5815 | * EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding. |
| 5816 | * |
| 5817 | * Verifies that 3DES CBC works with many different input sizes. |
| 5818 | */ |
| 5819 | TEST_P(EncryptionOperationsTest, TripleDesCbcIncrementalNoPadding) { |
| 5820 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5821 | .TripleDesEncryptionKey(168) |
| 5822 | .BlockMode(BlockMode::CBC) |
| 5823 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5824 | .Padding(PaddingMode::NONE))); |
| 5825 | |
| 5826 | int increment = 7; |
| 5827 | string message(240, 'a'); |
| 5828 | AuthorizationSet input_params = |
| 5829 | AuthorizationSetBuilder().BlockMode(BlockMode::CBC).Padding(PaddingMode::NONE); |
| 5830 | AuthorizationSet output_params; |
| 5831 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, input_params, &output_params)); |
| 5832 | |
| 5833 | string ciphertext; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5834 | for (size_t i = 0; i < message.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5835 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5836 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
| 5837 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 5838 | |
| 5839 | // Move TAG_NONCE into input_params |
| 5840 | input_params = output_params; |
| 5841 | input_params.push_back(TAG_BLOCK_MODE, BlockMode::CBC); |
| 5842 | input_params.push_back(TAG_PADDING, PaddingMode::NONE); |
| 5843 | output_params.Clear(); |
| 5844 | |
| 5845 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, input_params, &output_params)); |
| 5846 | string plaintext; |
| 5847 | for (size_t i = 0; i < ciphertext.size(); i += increment) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 5848 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5849 | EXPECT_EQ(ErrorCode::OK, Finish(&plaintext)); |
| 5850 | EXPECT_EQ(ciphertext.size(), plaintext.size()); |
| 5851 | EXPECT_EQ(message, plaintext); |
| 5852 | } |
| 5853 | |
| 5854 | INSTANTIATE_KEYMINT_AIDL_TEST(EncryptionOperationsTest); |
| 5855 | |
| 5856 | typedef KeyMintAidlTestBase MaxOperationsTest; |
| 5857 | |
| 5858 | /* |
| 5859 | * MaxOperationsTest.TestLimitAes |
| 5860 | * |
| 5861 | * Verifies that the max uses per boot tag works correctly with AES keys. |
| 5862 | */ |
| 5863 | TEST_P(MaxOperationsTest, TestLimitAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 5864 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 5865 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 5866 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5867 | |
| 5868 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5869 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5870 | .AesEncryptionKey(128) |
| 5871 | .EcbMode() |
| 5872 | .Padding(PaddingMode::NONE) |
| 5873 | .Authorization(TAG_MAX_USES_PER_BOOT, 3))); |
| 5874 | |
| 5875 | string message = "1234567890123456"; |
| 5876 | |
| 5877 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 5878 | |
| 5879 | EncryptMessage(message, params); |
| 5880 | EncryptMessage(message, params); |
| 5881 | EncryptMessage(message, params); |
| 5882 | |
| 5883 | // Fourth time should fail. |
| 5884 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::ENCRYPT, params)); |
| 5885 | } |
| 5886 | |
| 5887 | /* |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5888 | * MaxOperationsTest.TestLimitRsa |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5889 | * |
| 5890 | * Verifies that the max uses per boot tag works correctly with RSA keys. |
| 5891 | */ |
| 5892 | TEST_P(MaxOperationsTest, TestLimitRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 5893 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 5894 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 5895 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5896 | |
| 5897 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5898 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5899 | .RsaSigningKey(1024, 65537) |
| 5900 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 5901 | .Authorization(TAG_MAX_USES_PER_BOOT, 3) |
| 5902 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 5903 | |
| 5904 | string message = "1234567890123456"; |
| 5905 | |
| 5906 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 5907 | |
| 5908 | SignMessage(message, params); |
| 5909 | SignMessage(message, params); |
| 5910 | SignMessage(message, params); |
| 5911 | |
| 5912 | // Fourth time should fail. |
| 5913 | EXPECT_EQ(ErrorCode::KEY_MAX_OPS_EXCEEDED, Begin(KeyPurpose::SIGN, params)); |
| 5914 | } |
| 5915 | |
| 5916 | INSTANTIATE_KEYMINT_AIDL_TEST(MaxOperationsTest); |
| 5917 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5918 | typedef KeyMintAidlTestBase UsageCountLimitTest; |
| 5919 | |
| 5920 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5921 | * UsageCountLimitTest.TestSingleUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5922 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5923 | * 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] | 5924 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5925 | TEST_P(UsageCountLimitTest, TestSingleUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 5926 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 5927 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 5928 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5929 | |
| 5930 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5931 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5932 | .AesEncryptionKey(128) |
| 5933 | .EcbMode() |
| 5934 | .Padding(PaddingMode::NONE) |
| 5935 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1))); |
| 5936 | |
| 5937 | // Check the usage count limit tag appears in the authorizations. |
| 5938 | AuthorizationSet auths; |
| 5939 | for (auto& entry : key_characteristics_) { |
| 5940 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 5941 | } |
| 5942 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 5943 | << "key usage count limit " << 1U << " missing"; |
| 5944 | |
| 5945 | string message = "1234567890123456"; |
| 5946 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 5947 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5948 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 5949 | AuthorizationSet keystore_auths = |
| 5950 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 5951 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5952 | // First usage of AES key should work. |
| 5953 | EncryptMessage(message, params); |
| 5954 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5955 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 5956 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 5957 | // must be invalidated from secure storage (such as RPMB partition). |
| 5958 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 5959 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5960 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 5961 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5962 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | /* |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5967 | * UsageCountLimitTest.TestLimitedUseAes |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 5968 | * |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5969 | * 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] | 5970 | */ |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5971 | TEST_P(UsageCountLimitTest, TestLimitedUseAes) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 5972 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 5973 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 5974 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 5975 | |
| 5976 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 5977 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 5978 | .AesEncryptionKey(128) |
| 5979 | .EcbMode() |
| 5980 | .Padding(PaddingMode::NONE) |
| 5981 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3))); |
| 5982 | |
| 5983 | // Check the usage count limit tag appears in the authorizations. |
| 5984 | AuthorizationSet auths; |
| 5985 | for (auto& entry : key_characteristics_) { |
| 5986 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 5987 | } |
| 5988 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 5989 | << "key usage count limit " << 3U << " missing"; |
| 5990 | |
| 5991 | string message = "1234567890123456"; |
| 5992 | auto params = AuthorizationSetBuilder().EcbMode().Padding(PaddingMode::NONE); |
| 5993 | |
| 5994 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 5995 | AuthorizationSet keystore_auths = |
| 5996 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 5997 | |
| 5998 | EncryptMessage(message, params); |
| 5999 | EncryptMessage(message, params); |
| 6000 | EncryptMessage(message, params); |
| 6001 | |
| 6002 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6003 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6004 | // must be invalidated from secure storage (such as RPMB partition). |
| 6005 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::ENCRYPT, params)); |
| 6006 | } else { |
| 6007 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6008 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
| 6009 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params)); |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | /* |
| 6014 | * UsageCountLimitTest.TestSingleUseRsa |
| 6015 | * |
| 6016 | * Verifies that the usage count limit tag = 1 works correctly with RSA keys. |
| 6017 | */ |
| 6018 | TEST_P(UsageCountLimitTest, TestSingleUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6019 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6020 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6021 | } |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6022 | |
| 6023 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6024 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6025 | .RsaSigningKey(1024, 65537) |
| 6026 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6027 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 6028 | .SetDefaultValidity())); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6029 | |
| 6030 | // Check the usage count limit tag appears in the authorizations. |
| 6031 | AuthorizationSet auths; |
| 6032 | for (auto& entry : key_characteristics_) { |
| 6033 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6034 | } |
| 6035 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6036 | << "key usage count limit " << 1U << " missing"; |
| 6037 | |
| 6038 | string message = "1234567890123456"; |
| 6039 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6040 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6041 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6042 | AuthorizationSet keystore_auths = |
| 6043 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6044 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6045 | // First usage of RSA key should work. |
| 6046 | SignMessage(message, params); |
| 6047 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6048 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) { |
| 6049 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6050 | // must be invalidated from secure storage (such as RPMB partition). |
| 6051 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6052 | } else { |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6053 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6054 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)); |
| 6055 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6056 | } |
| 6057 | } |
| 6058 | |
| 6059 | /* |
| 6060 | * UsageCountLimitTest.TestLimitUseRsa |
| 6061 | * |
| 6062 | * Verifies that the usage count limit tag > 1 works correctly with RSA keys. |
| 6063 | */ |
| 6064 | TEST_P(UsageCountLimitTest, TestLimitUseRsa) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6065 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6066 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6067 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6068 | |
| 6069 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6070 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6071 | .RsaSigningKey(1024, 65537) |
| 6072 | .NoDigestOrPadding() |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6073 | .Authorization(TAG_USAGE_COUNT_LIMIT, 3) |
| 6074 | .SetDefaultValidity())); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 6075 | |
| 6076 | // Check the usage count limit tag appears in the authorizations. |
| 6077 | AuthorizationSet auths; |
| 6078 | for (auto& entry : key_characteristics_) { |
| 6079 | auths.push_back(AuthorizationSet(entry.authorizations)); |
| 6080 | } |
| 6081 | EXPECT_TRUE(auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) |
| 6082 | << "key usage count limit " << 3U << " missing"; |
| 6083 | |
| 6084 | string message = "1234567890123456"; |
| 6085 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6086 | |
| 6087 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6088 | AuthorizationSet keystore_auths = |
| 6089 | SecLevelAuthorizations(key_characteristics_, SecurityLevel::KEYSTORE); |
| 6090 | |
| 6091 | SignMessage(message, params); |
| 6092 | SignMessage(message, params); |
| 6093 | SignMessage(message, params); |
| 6094 | |
| 6095 | if (hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)) { |
| 6096 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6097 | // must be invalidated from secure storage (such as RPMB partition). |
| 6098 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
| 6099 | } else { |
| 6100 | // Usage count limit tag is enforced by keystore, keymint does nothing. |
| 6101 | EXPECT_TRUE(keystore_auths.Contains(TAG_USAGE_COUNT_LIMIT, 3U)); |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6102 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::SIGN, params)); |
| 6103 | } |
| 6104 | } |
| 6105 | |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6106 | /* |
| 6107 | * UsageCountLimitTest.TestSingleUseKeyAndRollbackResistance |
| 6108 | * |
| 6109 | * Verifies that when rollback resistance is supported by the KeyMint implementation with |
| 6110 | * the secure hardware, the single use key with usage count limit tag = 1 must also be enforced |
| 6111 | * in hardware. |
| 6112 | */ |
| 6113 | TEST_P(UsageCountLimitTest, TestSingleUseKeyAndRollbackResistance) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6114 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 6115 | GTEST_SKIP() << "Test not applicable to StrongBox device"; |
| 6116 | } |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6117 | |
| 6118 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6119 | .RsaSigningKey(2048, 65537) |
| 6120 | .Digest(Digest::NONE) |
| 6121 | .Padding(PaddingMode::NONE) |
| 6122 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6123 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6124 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6125 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6126 | GTEST_SKIP() << "Rollback resistance not supported"; |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6127 | } |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6128 | |
| 6129 | // Rollback resistance is supported by KeyMint, verify it is enforced in hardware. |
| 6130 | ASSERT_EQ(ErrorCode::OK, error); |
| 6131 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 6132 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
| 6133 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
| 6134 | |
| 6135 | // The KeyMint should also enforce single use key in hardware when it supports rollback |
| 6136 | // resistance. |
| 6137 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6138 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6139 | .RsaSigningKey(1024, 65537) |
| 6140 | .NoDigestOrPadding() |
| 6141 | .Authorization(TAG_USAGE_COUNT_LIMIT, 1) |
| 6142 | .SetDefaultValidity())); |
| 6143 | |
| 6144 | // Check the usage count limit tag appears in the hardware authorizations. |
| 6145 | AuthorizationSet hardware_auths = HwEnforcedAuthorizations(key_characteristics_); |
| 6146 | EXPECT_TRUE(hardware_auths.Contains(TAG_USAGE_COUNT_LIMIT, 1U)) |
| 6147 | << "key usage count limit " << 1U << " missing"; |
| 6148 | |
| 6149 | string message = "1234567890123456"; |
| 6150 | auto params = AuthorizationSetBuilder().NoDigestOrPadding(); |
| 6151 | |
| 6152 | // First usage of RSA key should work. |
| 6153 | SignMessage(message, params); |
| 6154 | |
| 6155 | // Usage count limit tag is enforced by hardware. After using the key, the key blob |
| 6156 | // must be invalidated from secure storage (such as RPMB partition). |
| 6157 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, Begin(KeyPurpose::SIGN, params)); |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6158 | } |
| 6159 | |
Qi Wu | d22ec84 | 2020-11-26 13:27:53 +0800 | [diff] [blame] | 6160 | INSTANTIATE_KEYMINT_AIDL_TEST(UsageCountLimitTest); |
| 6161 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 6162 | typedef KeyMintAidlTestBase GetHardwareInfoTest; |
| 6163 | |
| 6164 | TEST_P(GetHardwareInfoTest, GetHardwareInfo) { |
| 6165 | // Retrieving hardware info should give the same result each time. |
| 6166 | KeyMintHardwareInfo info; |
| 6167 | ASSERT_TRUE(keyMint().getHardwareInfo(&info).isOk()); |
| 6168 | KeyMintHardwareInfo info2; |
| 6169 | ASSERT_TRUE(keyMint().getHardwareInfo(&info2).isOk()); |
| 6170 | EXPECT_EQ(info, info2); |
| 6171 | } |
| 6172 | |
| 6173 | INSTANTIATE_KEYMINT_AIDL_TEST(GetHardwareInfoTest); |
| 6174 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6175 | typedef KeyMintAidlTestBase AddEntropyTest; |
| 6176 | |
| 6177 | /* |
| 6178 | * AddEntropyTest.AddEntropy |
| 6179 | * |
| 6180 | * Verifies that the addRngEntropy method doesn't blow up. There's no way to test that entropy |
| 6181 | * is actually added. |
| 6182 | */ |
| 6183 | TEST_P(AddEntropyTest, AddEntropy) { |
| 6184 | string data = "foo"; |
| 6185 | EXPECT_TRUE(keyMint().addRngEntropy(vector<uint8_t>(data.begin(), data.end())).isOk()); |
| 6186 | } |
| 6187 | |
| 6188 | /* |
| 6189 | * AddEntropyTest.AddEmptyEntropy |
| 6190 | * |
| 6191 | * Verifies that the addRngEntropy method doesn't blow up when given an empty buffer. |
| 6192 | */ |
| 6193 | TEST_P(AddEntropyTest, AddEmptyEntropy) { |
| 6194 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf()).isOk()); |
| 6195 | } |
| 6196 | |
| 6197 | /* |
| 6198 | * AddEntropyTest.AddLargeEntropy |
| 6199 | * |
| 6200 | * Verifies that the addRngEntropy method doesn't blow up when given a largish amount of data. |
| 6201 | */ |
| 6202 | TEST_P(AddEntropyTest, AddLargeEntropy) { |
| 6203 | EXPECT_TRUE(keyMint().addRngEntropy(AidlBuf(string(2 * 1024, 'a'))).isOk()); |
| 6204 | } |
| 6205 | |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 6206 | /* |
| 6207 | * AddEntropyTest.AddTooLargeEntropy |
| 6208 | * |
| 6209 | * Verifies that the addRngEntropy method rejects more than 2KiB of data. |
| 6210 | */ |
| 6211 | TEST_P(AddEntropyTest, AddTooLargeEntropy) { |
| 6212 | ErrorCode rc = GetReturnErrorCode(keyMint().addRngEntropy(AidlBuf(string(2 * 1024 + 1, 'a')))); |
| 6213 | EXPECT_EQ(ErrorCode::INVALID_INPUT_LENGTH, rc); |
| 6214 | } |
| 6215 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6216 | INSTANTIATE_KEYMINT_AIDL_TEST(AddEntropyTest); |
| 6217 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6218 | typedef KeyMintAidlTestBase KeyDeletionTest; |
| 6219 | |
| 6220 | /** |
| 6221 | * KeyDeletionTest.DeleteKey |
| 6222 | * |
| 6223 | * This test checks that if rollback protection is implemented, DeleteKey invalidates a formerly |
| 6224 | * valid key blob. |
| 6225 | */ |
| 6226 | TEST_P(KeyDeletionTest, DeleteKey) { |
| 6227 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6228 | .RsaSigningKey(2048, 65537) |
| 6229 | .Digest(Digest::NONE) |
| 6230 | .Padding(PaddingMode::NONE) |
| 6231 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6232 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6233 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6234 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6235 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 6236 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6237 | |
| 6238 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6239 | ASSERT_EQ(ErrorCode::OK, error); |
| 6240 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 6241 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6242 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6243 | ASSERT_EQ(ErrorCode::OK, DeleteKey(true /* keep key blob */)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6244 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6245 | string message = "12345678901234567890123456789012"; |
| 6246 | AuthorizationSet begin_out_params; |
| 6247 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 6248 | Begin(KeyPurpose::SIGN, key_blob_, |
| 6249 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 6250 | &begin_out_params)); |
| 6251 | AbortIfNeeded(); |
| 6252 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | /** |
| 6256 | * KeyDeletionTest.DeleteInvalidKey |
| 6257 | * |
| 6258 | * This test checks that the HAL excepts invalid key blobs.. |
| 6259 | */ |
| 6260 | TEST_P(KeyDeletionTest, DeleteInvalidKey) { |
| 6261 | // Generate key just to check if rollback protection is implemented |
| 6262 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6263 | .RsaSigningKey(2048, 65537) |
| 6264 | .Digest(Digest::NONE) |
| 6265 | .Padding(PaddingMode::NONE) |
| 6266 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Qi Wu | 8e727f7 | 2021-02-11 02:49:33 +0800 | [diff] [blame] | 6267 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6268 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6269 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6270 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 6271 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6272 | |
| 6273 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6274 | ASSERT_EQ(ErrorCode::OK, error); |
| 6275 | AuthorizationSet enforced(SecLevelAuthorizations()); |
| 6276 | ASSERT_TRUE(enforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6277 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6278 | // Delete the key we don't care about the result at this point. |
| 6279 | DeleteKey(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6280 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6281 | // Now create an invalid key blob and delete it. |
| 6282 | 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] | 6283 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6284 | ASSERT_EQ(ErrorCode::OK, DeleteKey()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | /** |
| 6288 | * KeyDeletionTest.DeleteAllKeys |
| 6289 | * |
| 6290 | * This test is disarmed by default. To arm it use --arm_deleteAllKeys. |
| 6291 | * |
| 6292 | * BEWARE: This test has serious side effects. All user keys will be lost! This includes |
| 6293 | * FBE/FDE encryption keys, which means that the device will not even boot until after the |
| 6294 | * device has been wiped manually (e.g., fastboot flashall -w), and new FBE/FDE keys have |
| 6295 | * been provisioned. Use this test only on dedicated testing devices that have no valuable |
| 6296 | * credentials stored in Keystore/Keymint. |
| 6297 | */ |
| 6298 | TEST_P(KeyDeletionTest, DeleteAllKeys) { |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6299 | if (!arm_deleteAllKeys) { |
| 6300 | GTEST_SKIP() << "Option --arm_deleteAllKeys not set"; |
| 6301 | return; |
| 6302 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6303 | auto error = GenerateKey(AuthorizationSetBuilder() |
| 6304 | .RsaSigningKey(2048, 65537) |
| 6305 | .Digest(Digest::NONE) |
| 6306 | .Padding(PaddingMode::NONE) |
| 6307 | .Authorization(TAG_NO_AUTH_REQUIRED) |
Shawn Willden | 9a7410e | 2021-08-02 12:28:42 -0600 | [diff] [blame] | 6308 | .Authorization(TAG_ROLLBACK_RESISTANCE) |
| 6309 | .SetDefaultValidity()); |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6310 | if (error == ErrorCode::ROLLBACK_RESISTANCE_UNAVAILABLE) { |
| 6311 | GTEST_SKIP() << "Rollback resistance not supported"; |
| 6312 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6313 | |
| 6314 | // Delete must work if rollback protection is implemented |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6315 | ASSERT_EQ(ErrorCode::OK, error); |
| 6316 | AuthorizationSet hardwareEnforced(SecLevelAuthorizations()); |
| 6317 | ASSERT_TRUE(hardwareEnforced.Contains(TAG_ROLLBACK_RESISTANCE)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6318 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6319 | ASSERT_EQ(ErrorCode::OK, DeleteAllKeys()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6320 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6321 | string message = "12345678901234567890123456789012"; |
| 6322 | AuthorizationSet begin_out_params; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6323 | |
David Drysdale | 513bf12 | 2021-10-06 11:53:13 +0100 | [diff] [blame] | 6324 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 6325 | Begin(KeyPurpose::SIGN, key_blob_, |
| 6326 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE), |
| 6327 | &begin_out_params)); |
| 6328 | AbortIfNeeded(); |
| 6329 | key_blob_ = AidlBuf(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyDeletionTest); |
| 6333 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6334 | typedef KeyMintAidlTestBase KeyUpgradeTest; |
| 6335 | |
| 6336 | /** |
| 6337 | * KeyUpgradeTest.UpgradeInvalidKey |
| 6338 | * |
| 6339 | * This test checks that the HAL excepts invalid key blobs.. |
| 6340 | */ |
| 6341 | TEST_P(KeyUpgradeTest, UpgradeInvalidKey) { |
| 6342 | AidlBuf key_blob = AidlBuf("just some garbage data which is not a valid key blob"); |
| 6343 | |
| 6344 | std::vector<uint8_t> new_blob; |
| 6345 | Status result = keymint_->upgradeKey(key_blob, |
| 6346 | AuthorizationSetBuilder() |
| 6347 | .Authorization(TAG_APPLICATION_ID, "clientid") |
| 6348 | .Authorization(TAG_APPLICATION_DATA, "appdata") |
| 6349 | .vector_data(), |
| 6350 | &new_blob); |
| 6351 | ASSERT_EQ(ErrorCode::INVALID_KEY_BLOB, GetReturnErrorCode(result)); |
| 6352 | } |
| 6353 | |
| 6354 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyUpgradeTest); |
| 6355 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6356 | using UpgradeKeyTest = KeyMintAidlTestBase; |
| 6357 | |
| 6358 | /* |
| 6359 | * UpgradeKeyTest.UpgradeKey |
| 6360 | * |
| 6361 | * Verifies that calling upgrade key on an up-to-date key works (i.e. does nothing). |
| 6362 | */ |
| 6363 | TEST_P(UpgradeKeyTest, UpgradeKey) { |
| 6364 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6365 | .AesEncryptionKey(128) |
| 6366 | .Padding(PaddingMode::NONE) |
| 6367 | .Authorization(TAG_NO_AUTH_REQUIRED))); |
| 6368 | |
| 6369 | auto result = UpgradeKey(key_blob_); |
| 6370 | |
| 6371 | // Key doesn't need upgrading. Should get okay, but no new key blob. |
| 6372 | EXPECT_EQ(result, std::make_pair(ErrorCode::OK, vector<uint8_t>())); |
| 6373 | } |
| 6374 | |
| 6375 | INSTANTIATE_KEYMINT_AIDL_TEST(UpgradeKeyTest); |
| 6376 | |
| 6377 | using ClearOperationsTest = KeyMintAidlTestBase; |
| 6378 | |
| 6379 | /* |
| 6380 | * ClearSlotsTest.TooManyOperations |
| 6381 | * |
| 6382 | * Verifies that TOO_MANY_OPERATIONS is returned after the max number of |
| 6383 | * operations are started without being finished or aborted. Also verifies |
| 6384 | * that aborting the operations clears the operations. |
| 6385 | * |
| 6386 | */ |
| 6387 | TEST_P(ClearOperationsTest, TooManyOperations) { |
| 6388 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6389 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6390 | .RsaEncryptionKey(2048, 65537) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6391 | .Padding(PaddingMode::NONE) |
| 6392 | .SetDefaultValidity())); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6393 | |
| 6394 | auto params = AuthorizationSetBuilder().Padding(PaddingMode::NONE); |
| 6395 | constexpr size_t max_operations = 100; // set to arbituary large number |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 6396 | std::shared_ptr<IKeyMintOperation> op_handles[max_operations]; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6397 | AuthorizationSet out_params; |
| 6398 | ErrorCode result; |
| 6399 | size_t i; |
| 6400 | |
| 6401 | for (i = 0; i < max_operations; i++) { |
| 6402 | result = Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params, op_handles[i]); |
| 6403 | if (ErrorCode::OK != result) { |
| 6404 | break; |
| 6405 | } |
| 6406 | } |
| 6407 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, result); |
| 6408 | // Try again just in case there's a weird overflow bug |
| 6409 | EXPECT_EQ(ErrorCode::TOO_MANY_OPERATIONS, |
| 6410 | Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params)); |
| 6411 | for (size_t j = 0; j < i; j++) { |
| 6412 | EXPECT_EQ(ErrorCode::OK, Abort(op_handles[j])) |
| 6413 | << "Aboort failed for i = " << j << std::endl; |
| 6414 | } |
| 6415 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, key_blob_, params, &out_params)); |
| 6416 | AbortIfNeeded(); |
| 6417 | } |
| 6418 | |
| 6419 | INSTANTIATE_KEYMINT_AIDL_TEST(ClearOperationsTest); |
| 6420 | |
| 6421 | typedef KeyMintAidlTestBase TransportLimitTest; |
| 6422 | |
| 6423 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 6424 | * TransportLimitTest.LargeFinishInput |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6425 | * |
| 6426 | * Verifies that passing input data to finish succeeds as expected. |
| 6427 | */ |
| 6428 | TEST_P(TransportLimitTest, LargeFinishInput) { |
| 6429 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6430 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6431 | .AesEncryptionKey(128) |
| 6432 | .BlockMode(BlockMode::ECB) |
| 6433 | .Padding(PaddingMode::NONE))); |
| 6434 | |
| 6435 | for (int msg_size = 8 /* 256 bytes */; msg_size <= 11 /* 2 KiB */; msg_size++) { |
| 6436 | auto cipher_params = |
| 6437 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE); |
| 6438 | |
| 6439 | AuthorizationSet out_params; |
| 6440 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, cipher_params, &out_params)); |
| 6441 | |
| 6442 | string plain_message = std::string(1 << msg_size, 'x'); |
| 6443 | string encrypted_message; |
| 6444 | auto rc = Finish(plain_message, &encrypted_message); |
| 6445 | |
| 6446 | EXPECT_EQ(ErrorCode::OK, rc); |
| 6447 | EXPECT_EQ(plain_message.size(), encrypted_message.size()) |
| 6448 | << "Encrypt finish returned OK, but did not consume all of the given input"; |
| 6449 | cipher_params.push_back(out_params); |
| 6450 | |
| 6451 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, cipher_params)); |
| 6452 | |
| 6453 | string decrypted_message; |
| 6454 | rc = Finish(encrypted_message, &decrypted_message); |
| 6455 | EXPECT_EQ(ErrorCode::OK, rc); |
| 6456 | EXPECT_EQ(plain_message.size(), decrypted_message.size()) |
| 6457 | << "Decrypt finish returned OK, did not consume all of the given input"; |
| 6458 | } |
| 6459 | } |
| 6460 | |
| 6461 | INSTANTIATE_KEYMINT_AIDL_TEST(TransportLimitTest); |
| 6462 | |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 6463 | typedef KeyMintAidlTestBase KeyAgreementTest; |
| 6464 | |
| 6465 | int CurveToOpenSslCurveName(EcCurve curve) { |
| 6466 | switch (curve) { |
| 6467 | case EcCurve::P_224: |
| 6468 | return NID_secp224r1; |
| 6469 | case EcCurve::P_256: |
| 6470 | return NID_X9_62_prime256v1; |
| 6471 | case EcCurve::P_384: |
| 6472 | return NID_secp384r1; |
| 6473 | case EcCurve::P_521: |
| 6474 | return NID_secp521r1; |
| 6475 | } |
| 6476 | } |
| 6477 | |
| 6478 | /* |
| 6479 | * KeyAgreementTest.Ecdh |
| 6480 | * |
| 6481 | * Verifies that ECDH works for all curves |
| 6482 | */ |
| 6483 | TEST_P(KeyAgreementTest, Ecdh) { |
| 6484 | // Because it's possible to use this API with keys on different curves, we |
| 6485 | // check all N^2 combinations where N is the number of supported |
| 6486 | // curves. |
| 6487 | // |
| 6488 | // This is not a big deal as N is 4 so we only do 16 runs. If we end up with a |
| 6489 | // lot more curves we can be smart about things and just pick |otherCurve| so |
| 6490 | // it's not |curve| and that way we end up with only 2*N runs |
| 6491 | // |
| 6492 | for (auto curve : ValidCurves()) { |
| 6493 | for (auto localCurve : ValidCurves()) { |
| 6494 | // Generate EC key locally (with access to private key material) |
| 6495 | auto ecKey = EC_KEY_Ptr(EC_KEY_new()); |
| 6496 | int curveName = CurveToOpenSslCurveName(localCurve); |
| 6497 | auto group = EC_GROUP_Ptr(EC_GROUP_new_by_curve_name(curveName)); |
| 6498 | ASSERT_NE(group, nullptr); |
| 6499 | ASSERT_EQ(EC_KEY_set_group(ecKey.get(), group.get()), 1); |
| 6500 | ASSERT_EQ(EC_KEY_generate_key(ecKey.get()), 1); |
| 6501 | auto pkey = EVP_PKEY_Ptr(EVP_PKEY_new()); |
| 6502 | ASSERT_EQ(EVP_PKEY_set1_EC_KEY(pkey.get(), ecKey.get()), 1); |
| 6503 | |
| 6504 | // Get encoded form of the public part of the locally generated key... |
| 6505 | unsigned char* p = nullptr; |
| 6506 | int encodedPublicKeySize = i2d_PUBKEY(pkey.get(), &p); |
| 6507 | ASSERT_GT(encodedPublicKeySize, 0); |
| 6508 | vector<uint8_t> encodedPublicKey( |
| 6509 | reinterpret_cast<const uint8_t*>(p), |
| 6510 | reinterpret_cast<const uint8_t*>(p + encodedPublicKeySize)); |
| 6511 | OPENSSL_free(p); |
| 6512 | |
| 6513 | // Generate EC key in KeyMint (only access to public key material) |
| 6514 | vector<uint8_t> challenge = {0x41, 0x42}; |
| 6515 | EXPECT_EQ( |
| 6516 | ErrorCode::OK, |
| 6517 | GenerateKey(AuthorizationSetBuilder() |
| 6518 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6519 | .Authorization(TAG_EC_CURVE, curve) |
| 6520 | .Authorization(TAG_PURPOSE, KeyPurpose::AGREE_KEY) |
| 6521 | .Authorization(TAG_ALGORITHM, Algorithm::EC) |
| 6522 | .Authorization(TAG_ATTESTATION_APPLICATION_ID, {0x61, 0x62}) |
Janis Danisevskis | 164bb87 | 2021-02-09 11:30:25 -0800 | [diff] [blame] | 6523 | .Authorization(TAG_ATTESTATION_CHALLENGE, challenge) |
| 6524 | .SetDefaultValidity())) |
David Zeuthen | e0c4089 | 2021-01-08 12:54:11 -0500 | [diff] [blame] | 6525 | << "Failed to generate key"; |
| 6526 | ASSERT_GT(cert_chain_.size(), 0); |
| 6527 | X509_Ptr kmKeyCert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
| 6528 | ASSERT_NE(kmKeyCert, nullptr); |
| 6529 | // Check that keyAgreement (bit 4) is set in KeyUsage |
| 6530 | EXPECT_TRUE((X509_get_key_usage(kmKeyCert.get()) & X509v3_KU_KEY_AGREEMENT) != 0); |
| 6531 | auto kmPkey = EVP_PKEY_Ptr(X509_get_pubkey(kmKeyCert.get())); |
| 6532 | ASSERT_NE(kmPkey, nullptr); |
| 6533 | if (dump_Attestations) { |
| 6534 | for (size_t n = 0; n < cert_chain_.size(); n++) { |
| 6535 | std::cout << bin2hex(cert_chain_[n].encodedCertificate) << std::endl; |
| 6536 | } |
| 6537 | } |
| 6538 | |
| 6539 | // Now that we have the two keys, we ask KeyMint to perform ECDH... |
| 6540 | if (curve != localCurve) { |
| 6541 | // If the keys are using different curves KeyMint should fail with |
| 6542 | // ErrorCode:INVALID_ARGUMENT. Check that. |
| 6543 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 6544 | string ZabFromKeyMintStr; |
| 6545 | EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, |
| 6546 | Finish(string(encodedPublicKey.begin(), encodedPublicKey.end()), |
| 6547 | &ZabFromKeyMintStr)); |
| 6548 | |
| 6549 | } else { |
| 6550 | // Otherwise if the keys are using the same curve, it should work. |
| 6551 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::AGREE_KEY, AuthorizationSetBuilder())); |
| 6552 | string ZabFromKeyMintStr; |
| 6553 | EXPECT_EQ(ErrorCode::OK, |
| 6554 | Finish(string(encodedPublicKey.begin(), encodedPublicKey.end()), |
| 6555 | &ZabFromKeyMintStr)); |
| 6556 | vector<uint8_t> ZabFromKeyMint(ZabFromKeyMintStr.begin(), ZabFromKeyMintStr.end()); |
| 6557 | |
| 6558 | // Perform local ECDH between the two keys so we can check if we get the same Zab.. |
| 6559 | auto ctx = EVP_PKEY_CTX_Ptr(EVP_PKEY_CTX_new(pkey.get(), nullptr)); |
| 6560 | ASSERT_NE(ctx, nullptr); |
| 6561 | ASSERT_EQ(EVP_PKEY_derive_init(ctx.get()), 1); |
| 6562 | ASSERT_EQ(EVP_PKEY_derive_set_peer(ctx.get(), kmPkey.get()), 1); |
| 6563 | size_t ZabFromTestLen = 0; |
| 6564 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), nullptr, &ZabFromTestLen), 1); |
| 6565 | vector<uint8_t> ZabFromTest; |
| 6566 | ZabFromTest.resize(ZabFromTestLen); |
| 6567 | ASSERT_EQ(EVP_PKEY_derive(ctx.get(), ZabFromTest.data(), &ZabFromTestLen), 1); |
| 6568 | |
| 6569 | EXPECT_EQ(ZabFromKeyMint, ZabFromTest); |
| 6570 | } |
| 6571 | |
| 6572 | CheckedDeleteKey(); |
| 6573 | } |
| 6574 | } |
| 6575 | } |
| 6576 | |
| 6577 | INSTANTIATE_KEYMINT_AIDL_TEST(KeyAgreementTest); |
| 6578 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6579 | using DestroyAttestationIdsTest = KeyMintAidlTestBase; |
| 6580 | |
| 6581 | // This is a problematic test, as it can render the device under test permanently unusable. |
| 6582 | // Re-enable and run at your own risk. |
| 6583 | TEST_P(DestroyAttestationIdsTest, DISABLED_DestroyTest) { |
| 6584 | auto result = DestroyAttestationIds(); |
| 6585 | EXPECT_TRUE(result == ErrorCode::OK || result == ErrorCode::UNIMPLEMENTED); |
| 6586 | } |
| 6587 | |
| 6588 | INSTANTIATE_KEYMINT_AIDL_TEST(DestroyAttestationIdsTest); |
| 6589 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 6590 | using EarlyBootKeyTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6591 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 6592 | /* |
| 6593 | * EarlyBootKeyTest.CreateEarlyBootKeys |
| 6594 | * |
| 6595 | * Verifies that creating early boot keys succeeds, even at a later stage (after boot). |
| 6596 | */ |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6597 | TEST_P(EarlyBootKeyTest, CreateEarlyBootKeys) { |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 6598 | // Early boot keys can be created after early boot. |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6599 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 6600 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 6601 | |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 6602 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
| 6603 | ASSERT_GT(keyData.blob.size(), 0U); |
| 6604 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 6605 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 6606 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6607 | CheckedDeleteKey(&aesKeyData.blob); |
| 6608 | CheckedDeleteKey(&hmacKeyData.blob); |
| 6609 | CheckedDeleteKey(&rsaKeyData.blob); |
| 6610 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 6611 | } |
| 6612 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 6613 | /* |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 6614 | * EarlyBootKeyTest.CreateAttestedEarlyBootKey |
| 6615 | * |
| 6616 | * Verifies that creating an early boot key with attestation succeeds. |
| 6617 | */ |
| 6618 | TEST_P(EarlyBootKeyTest, CreateAttestedEarlyBootKey) { |
| 6619 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = CreateTestKeys( |
| 6620 | TAG_EARLY_BOOT_ONLY, ErrorCode::OK, [](AuthorizationSetBuilder* builder) { |
| 6621 | builder->AttestationChallenge("challenge"); |
| 6622 | builder->AttestationApplicationId("app_id"); |
| 6623 | }); |
| 6624 | |
| 6625 | for (const auto& keyData : {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}) { |
| 6626 | ASSERT_GT(keyData.blob.size(), 0U); |
| 6627 | AuthorizationSet crypto_params = SecLevelAuthorizations(keyData.characteristics); |
| 6628 | EXPECT_TRUE(crypto_params.Contains(TAG_EARLY_BOOT_ONLY)) << crypto_params; |
| 6629 | } |
| 6630 | CheckedDeleteKey(&aesKeyData.blob); |
| 6631 | CheckedDeleteKey(&hmacKeyData.blob); |
| 6632 | CheckedDeleteKey(&rsaKeyData.blob); |
| 6633 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 6634 | } |
| 6635 | |
| 6636 | /* |
| 6637 | * EarlyBootKeyTest.UseEarlyBootKeyFailure |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 6638 | * |
| 6639 | * Verifies that using early boot keys at a later stage fails. |
| 6640 | */ |
| 6641 | TEST_P(EarlyBootKeyTest, UseEarlyBootKeyFailure) { |
| 6642 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 6643 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6644 | .Authorization(TAG_EARLY_BOOT_ONLY) |
| 6645 | .HmacKey(128) |
| 6646 | .Digest(Digest::SHA_2_256) |
| 6647 | .Authorization(TAG_MIN_MAC_LENGTH, 256))); |
| 6648 | AuthorizationSet output_params; |
| 6649 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, Begin(KeyPurpose::SIGN, key_blob_, |
| 6650 | AuthorizationSetBuilder() |
| 6651 | .Digest(Digest::SHA_2_256) |
| 6652 | .Authorization(TAG_MAC_LENGTH, 256), |
| 6653 | &output_params)); |
| 6654 | } |
| 6655 | |
| 6656 | /* |
| 6657 | * EarlyBootKeyTest.ImportEarlyBootKeyFailure |
| 6658 | * |
| 6659 | * Verifies that importing early boot keys fails. |
| 6660 | */ |
| 6661 | TEST_P(EarlyBootKeyTest, ImportEarlyBootKeyFailure) { |
| 6662 | ASSERT_EQ(ErrorCode::EARLY_BOOT_ENDED, ImportKey(AuthorizationSetBuilder() |
| 6663 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 6664 | .Authorization(TAG_EARLY_BOOT_ONLY) |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 6665 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 6666 | .Digest(Digest::SHA_2_256) |
| 6667 | .SetDefaultValidity(), |
| 6668 | KeyFormat::PKCS8, ec_256_key)); |
| 6669 | } |
| 6670 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6671 | // 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] | 6672 | // boot stage, which no proper Android device is by the time we can run VTS. To use this, |
| 6673 | // un-disable it and modify vold to remove the call to earlyBootEnded(). Running the test will end |
| 6674 | // early boot, so you'll have to reboot between runs. |
| 6675 | TEST_P(EarlyBootKeyTest, DISABLED_FullTest) { |
| 6676 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 6677 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::OK); |
| 6678 | // TAG_EARLY_BOOT_ONLY should be in hw-enforced. |
| 6679 | EXPECT_TRUE(HwEnforcedAuthorizations(aesKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 6680 | EXPECT_TRUE( |
| 6681 | HwEnforcedAuthorizations(hmacKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 6682 | EXPECT_TRUE(HwEnforcedAuthorizations(rsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 6683 | EXPECT_TRUE( |
| 6684 | HwEnforcedAuthorizations(ecdsaKeyData.characteristics).Contains(TAG_EARLY_BOOT_ONLY)); |
| 6685 | |
| 6686 | // Should be able to use keys, since early boot has not ended |
| 6687 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 6688 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 6689 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 6690 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 6691 | |
| 6692 | // End early boot |
| 6693 | ErrorCode earlyBootResult = GetReturnErrorCode(keyMint().earlyBootEnded()); |
| 6694 | EXPECT_EQ(earlyBootResult, ErrorCode::OK); |
| 6695 | |
| 6696 | // Should not be able to use already-created keys. |
| 6697 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseAesKey(aesKeyData.blob)); |
| 6698 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseHmacKey(hmacKeyData.blob)); |
| 6699 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseRsaKey(rsaKeyData.blob)); |
| 6700 | EXPECT_EQ(ErrorCode::EARLY_BOOT_ENDED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 6701 | |
| 6702 | CheckedDeleteKey(&aesKeyData.blob); |
| 6703 | CheckedDeleteKey(&hmacKeyData.blob); |
| 6704 | CheckedDeleteKey(&rsaKeyData.blob); |
| 6705 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 6706 | |
| 6707 | // Should not be able to create new keys |
| 6708 | std::tie(aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData) = |
| 6709 | CreateTestKeys(TAG_EARLY_BOOT_ONLY, ErrorCode::EARLY_BOOT_ENDED); |
| 6710 | |
| 6711 | CheckedDeleteKey(&aesKeyData.blob); |
| 6712 | CheckedDeleteKey(&hmacKeyData.blob); |
| 6713 | CheckedDeleteKey(&rsaKeyData.blob); |
| 6714 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 6715 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 6716 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6717 | INSTANTIATE_KEYMINT_AIDL_TEST(EarlyBootKeyTest); |
| 6718 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 6719 | using UnlockedDeviceRequiredTest = KeyMintAidlTestBase; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6720 | |
| 6721 | // This may be a problematic test. It can't be run repeatedly without unlocking the device in |
| 6722 | // between runs... and on most test devices there are no enrolled credentials so it can't be |
| 6723 | // unlocked at all, meaning the only way to get the test to pass again on a properly-functioning |
| 6724 | // device is to reboot it. For that reason, this is disabled by default. It can be used as part of |
| 6725 | // a manual test process, which includes unlocking between runs, which is why it's included here. |
| 6726 | // Well, that and the fact that it's the only test we can do without also making calls into the |
| 6727 | // Gatekeeper HAL. We haven't written any cross-HAL tests, and don't know what all of the |
| 6728 | // implications might be, so that may or may not be a solution. |
| 6729 | TEST_P(UnlockedDeviceRequiredTest, DISABLED_KeysBecomeUnusable) { |
| 6730 | auto [aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData] = |
| 6731 | CreateTestKeys(TAG_UNLOCKED_DEVICE_REQUIRED, ErrorCode::OK); |
| 6732 | |
| 6733 | EXPECT_EQ(ErrorCode::OK, UseAesKey(aesKeyData.blob)); |
| 6734 | EXPECT_EQ(ErrorCode::OK, UseHmacKey(hmacKeyData.blob)); |
| 6735 | EXPECT_EQ(ErrorCode::OK, UseRsaKey(rsaKeyData.blob)); |
| 6736 | EXPECT_EQ(ErrorCode::OK, UseEcdsaKey(ecdsaKeyData.blob)); |
| 6737 | |
| 6738 | ErrorCode rc = GetReturnErrorCode( |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 6739 | keyMint().deviceLocked(false /* passwordOnly */, {} /* timestampToken */)); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6740 | ASSERT_EQ(ErrorCode::OK, rc); |
| 6741 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseAesKey(aesKeyData.blob)); |
| 6742 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseHmacKey(hmacKeyData.blob)); |
| 6743 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseRsaKey(rsaKeyData.blob)); |
| 6744 | EXPECT_EQ(ErrorCode::DEVICE_LOCKED, UseEcdsaKey(ecdsaKeyData.blob)); |
| 6745 | |
| 6746 | CheckedDeleteKey(&aesKeyData.blob); |
| 6747 | CheckedDeleteKey(&hmacKeyData.blob); |
| 6748 | CheckedDeleteKey(&rsaKeyData.blob); |
| 6749 | CheckedDeleteKey(&ecdsaKeyData.blob); |
| 6750 | } |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 6751 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 6752 | INSTANTIATE_KEYMINT_AIDL_TEST(UnlockedDeviceRequiredTest); |
| 6753 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 6754 | } // namespace aidl::android::hardware::security::keymint::test |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6755 | |
| 6756 | int main(int argc, char** argv) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 6757 | std::cout << "Testing "; |
| 6758 | auto halInstances = |
| 6759 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase::build_params(); |
| 6760 | std::cout << "HAL instances:\n"; |
| 6761 | for (auto& entry : halInstances) { |
| 6762 | std::cout << " " << entry << '\n'; |
| 6763 | } |
| 6764 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6765 | ::testing::InitGoogleTest(&argc, argv); |
| 6766 | for (int i = 1; i < argc; ++i) { |
| 6767 | if (argv[i][0] == '-') { |
| 6768 | if (std::string(argv[i]) == "--arm_deleteAllKeys") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 6769 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 6770 | arm_deleteAllKeys = true; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6771 | } |
| 6772 | if (std::string(argv[i]) == "--dump_attestations") { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 6773 | aidl::android::hardware::security::keymint::test::KeyMintAidlTestBase:: |
| 6774 | dump_Attestations = true; |
| 6775 | } else { |
| 6776 | std::cout << "NOT dumping attestations" << std::endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6777 | } |
| 6778 | } |
| 6779 | } |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 6780 | return RUN_ALL_TESTS(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 6781 | } |