Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Janis Danisevskis | f54cc93 | 2017-05-10 15:29:10 -0700 | [diff] [blame] | 17 | #include <keymaster/key_blob_utils/ocb_utils.h> |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <openssl/aes.h> |
| 22 | #include <openssl/sha.h> |
| 23 | |
| 24 | #include <hardware/keymaster_defs.h> |
Janis Danisevskis | f54cc93 | 2017-05-10 15:29:10 -0700 | [diff] [blame] | 25 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 26 | #include <keymaster/android_keymaster_utils.h> |
Inseob Kim | 5786bc9 | 2020-01-06 18:31:42 +0900 | [diff] [blame] | 27 | #include <keymaster/authorization_set.h> |
Janis Danisevskis | f54cc93 | 2017-05-10 15:29:10 -0700 | [diff] [blame] | 28 | #include <keymaster/km_openssl/openssl_err.h> |
Inseob Kim | 5786bc9 | 2020-01-06 18:31:42 +0900 | [diff] [blame] | 29 | #include <keymaster/new.h> |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 30 | |
| 31 | namespace keymaster { |
| 32 | |
| 33 | class AeCtx { |
| 34 | public: |
Yi Kong | 3712b27 | 2018-07-30 15:53:23 -0700 | [diff] [blame] | 35 | AeCtx() : ctx_(ae_allocate(nullptr)) {} |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 36 | ~AeCtx() { |
| 37 | ae_clear(ctx_); |
| 38 | ae_free(ctx_); |
| 39 | } |
| 40 | |
| 41 | ae_ctx* get() { return ctx_; } |
| 42 | |
| 43 | private: |
| 44 | ae_ctx* ctx_; |
| 45 | }; |
| 46 | |
| 47 | static keymaster_error_t BuildDerivationData(const AuthorizationSet& hw_enforced, |
| 48 | const AuthorizationSet& sw_enforced, |
| 49 | const AuthorizationSet& hidden, |
| 50 | UniquePtr<uint8_t[]>* derivation_data, |
| 51 | size_t* derivation_data_length) { |
| 52 | *derivation_data_length = |
| 53 | hidden.SerializedSize() + hw_enforced.SerializedSize() + sw_enforced.SerializedSize(); |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 54 | derivation_data->reset(new (std::nothrow) uint8_t[*derivation_data_length]); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 55 | if (!derivation_data->get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 56 | |
| 57 | uint8_t* buf = derivation_data->get(); |
| 58 | uint8_t* end = derivation_data->get() + *derivation_data_length; |
| 59 | buf = hidden.Serialize(buf, end); |
| 60 | buf = hw_enforced.Serialize(buf, end); |
| 61 | buf = sw_enforced.Serialize(buf, end); |
| 62 | |
| 63 | return KM_ERROR_OK; |
| 64 | } |
| 65 | |
| 66 | static keymaster_error_t InitializeKeyWrappingContext(const AuthorizationSet& hw_enforced, |
| 67 | const AuthorizationSet& sw_enforced, |
| 68 | const AuthorizationSet& hidden, |
| 69 | const KeymasterKeyBlob& master_key, |
| 70 | AeCtx* ctx) { |
| 71 | size_t derivation_data_length; |
| 72 | UniquePtr<uint8_t[]> derivation_data; |
| 73 | keymaster_error_t error = BuildDerivationData(hw_enforced, sw_enforced, hidden, |
| 74 | &derivation_data, &derivation_data_length); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 75 | if (error != KM_ERROR_OK) return error; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 76 | |
| 77 | SHA256_CTX sha256_ctx; |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 78 | UniquePtr<uint8_t[]> hash_buf(new (std::nothrow) uint8_t[SHA256_DIGEST_LENGTH]); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 79 | if (!hash_buf.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 80 | Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH); |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 81 | UniquePtr<uint8_t[]> derived_key(new (std::nothrow) uint8_t[AES_BLOCK_SIZE]); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 82 | if (!derived_key.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 83 | Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE); |
| 84 | |
| 85 | if (!ctx->get() || !hash_buf.get() || !derived_key.get()) |
| 86 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 87 | |
| 88 | // Hash derivation data. |
| 89 | Eraser sha256_ctx_eraser(sha256_ctx); |
| 90 | SHA256_Init(&sha256_ctx); |
| 91 | SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length); |
| 92 | SHA256_Final(hash_buf.get(), &sha256_ctx); |
| 93 | |
| 94 | // Encrypt hash with master key to build derived key. |
| 95 | AES_KEY aes_key; |
| 96 | Eraser aes_key_eraser(AES_KEY); |
| 97 | if (0 != |
| 98 | AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key)) |
| 99 | return TranslateLastOpenSslError(); |
| 100 | |
| 101 | AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key); |
| 102 | |
| 103 | // Set up AES OCB context using derived key. |
| 104 | if (ae_init(ctx->get(), derived_key.get(), AES_BLOCK_SIZE /* key length */, OCB_NONCE_LENGTH, |
| 105 | OCB_TAG_LENGTH) != AE_SUCCESS) { |
| 106 | memset_s(ctx->get(), 0, ae_ctx_sizeof()); |
| 107 | return KM_ERROR_UNKNOWN_ERROR; |
| 108 | } |
| 109 | |
| 110 | return KM_ERROR_OK; |
| 111 | } |
| 112 | |
| 113 | keymaster_error_t OcbEncryptKey(const AuthorizationSet& hw_enforced, |
| 114 | const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden, |
| 115 | const KeymasterKeyBlob& master_key, |
| 116 | const KeymasterKeyBlob& plaintext, const Buffer& nonce, |
| 117 | KeymasterKeyBlob* ciphertext, Buffer* tag) { |
| 118 | assert(ciphertext && tag); |
| 119 | |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 120 | if (nonce.available_read() != OCB_NONCE_LENGTH) return KM_ERROR_INVALID_ARGUMENT; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 121 | |
| 122 | AeCtx ctx; |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 123 | if (!ctx.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 124 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 125 | keymaster_error_t error = |
| 126 | InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 127 | if (error != KM_ERROR_OK) return error; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 128 | |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 129 | if (!ciphertext->Reset(plaintext.key_material_size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 130 | |
| 131 | int ae_err = ae_encrypt(ctx.get(), nonce.peek_read(), plaintext.key_material, |
Yi Kong | 3712b27 | 2018-07-30 15:53:23 -0700 | [diff] [blame] | 132 | plaintext.key_material_size, nullptr /* additional data */, |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 133 | 0 /* additional data length */, ciphertext->writable_data(), |
| 134 | tag->peek_write(), 1 /* final */); |
| 135 | if (ae_err < 0) { |
| 136 | LOG_E("Error %d while encrypting key", ae_err); |
| 137 | return KM_ERROR_UNKNOWN_ERROR; |
| 138 | } |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 139 | if (!tag->advance_write(OCB_TAG_LENGTH)) return KM_ERROR_UNKNOWN_ERROR; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 140 | assert(ae_err == static_cast<int>(plaintext.key_material_size)); |
| 141 | return KM_ERROR_OK; |
| 142 | } |
| 143 | |
| 144 | keymaster_error_t OcbDecryptKey(const AuthorizationSet& hw_enforced, |
| 145 | const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden, |
| 146 | const KeymasterKeyBlob& master_key, |
| 147 | const KeymasterKeyBlob& ciphertext, const Buffer& nonce, |
| 148 | const Buffer& tag, KeymasterKeyBlob* plaintext) { |
| 149 | assert(plaintext); |
| 150 | |
| 151 | if (nonce.available_read() != OCB_NONCE_LENGTH || tag.available_read() != OCB_TAG_LENGTH) |
| 152 | return KM_ERROR_INVALID_ARGUMENT; |
| 153 | |
| 154 | AeCtx ctx; |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 155 | if (!ctx.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 156 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 157 | keymaster_error_t error = |
| 158 | InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 159 | if (error != KM_ERROR_OK) return error; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 160 | |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 161 | if (!plaintext->Reset(ciphertext.key_material_size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 162 | |
| 163 | int ae_err = ae_decrypt(ctx.get(), nonce.peek_read(), ciphertext.key_material, |
Yi Kong | 3712b27 | 2018-07-30 15:53:23 -0700 | [diff] [blame] | 164 | ciphertext.key_material_size, nullptr /* additional data */, |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 165 | 0 /* additional data length */, plaintext->writable_data(), |
| 166 | tag.peek_read(), 1 /* final */); |
| 167 | if (ae_err == AE_INVALID) { |
| 168 | // Authentication failed! Decryption probably succeeded(ish), but we don't want to return |
| 169 | // any data when the authentication fails, so clear it. |
| 170 | plaintext->Clear(); |
| 171 | LOG_E("Failed to validate authentication tag during key decryption", 0); |
| 172 | return KM_ERROR_INVALID_KEY_BLOB; |
| 173 | } else if (ae_err < 0) { |
| 174 | LOG_E("Failed to decrypt key, error: %d", ae_err); |
| 175 | return KM_ERROR_UNKNOWN_ERROR; |
| 176 | } |
| 177 | assert(ae_err == static_cast<int>(ciphertext.key_material_size)); |
| 178 | return KM_ERROR_OK; |
| 179 | } |
| 180 | |
| 181 | } // namespace keymaster |