Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <assert.h> |
| 18 | |
| 19 | #include <openssl/aes.h> |
| 20 | #include <openssl/sha.h> |
| 21 | |
| 22 | #include "ae.h" |
| 23 | #include "key_blob.h" |
| 24 | #include "google_keymaster_utils.h" |
| 25 | |
| 26 | namespace keymaster { |
| 27 | |
| 28 | struct AeCtxDelete { |
| 29 | void operator()(ae_ctx* p) { |
| 30 | ae_clear(p); |
| 31 | ae_free(p); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | const size_t KeyBlob::NONCE_LENGTH; |
| 36 | const size_t KeyBlob::TAG_LENGTH; |
| 37 | |
| 38 | KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced, |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 39 | const AuthorizationSet& hidden, const keymaster_key_blob_t& key, |
Shawn Willden | ebf627f | 2014-08-12 11:15:29 -0600 | [diff] [blame] | 40 | const keymaster_key_blob_t& master_key, const uint8_t nonce[NONCE_LENGTH]) |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 41 | : error_(KM_ERROR_OK), nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]), |
| 42 | enforced_(enforced), unenforced_(unenforced), hidden_(hidden) { |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame^] | 43 | if (!nonce_.get() || !tag_.get()) { |
| 44 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 45 | return; |
| 46 | } |
| 47 | error_ = KM_ERROR_OK; |
| 48 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 49 | if (enforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE || |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 50 | unenforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE || |
| 51 | hidden_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 52 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (enforced_.is_valid() != AuthorizationSet::OK || |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 57 | unenforced_.is_valid() != AuthorizationSet::OK || |
| 58 | hidden_.is_valid() != AuthorizationSet::OK) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 59 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 60 | return; |
| 61 | } |
| 62 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 63 | if (!ExtractKeyCharacteristics()) |
| 64 | return; |
| 65 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 66 | key_material_length_ = key.key_material_size; |
| 67 | key_material_.reset(new uint8_t[key_material_length_]); |
| 68 | encrypted_key_material_.reset(new uint8_t[key_material_length_]); |
| 69 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 70 | if (!key_material_.get() || !encrypted_key_material_.get() || !nonce_.get() || !tag_.get()) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 71 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 72 | return; |
| 73 | } |
| 74 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 75 | memcpy(nonce_.get(), nonce, NONCE_LENGTH); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 76 | memcpy(key_material_.get(), key.key_material, key_material_length_); |
| 77 | EncryptKey(master_key); |
| 78 | } |
| 79 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 80 | KeyBlob::KeyBlob(const keymaster_key_blob_t& key, const AuthorizationSet& hidden, |
| 81 | const keymaster_key_blob_t& master_key) |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 82 | : nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]), hidden_(hidden) { |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame^] | 83 | if (!nonce_.get() || !tag_.get()) { |
| 84 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 85 | return; |
| 86 | } |
| 87 | error_ = KM_ERROR_OK; |
| 88 | |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame] | 89 | const uint8_t* p = key.key_material; |
| 90 | if (!Deserialize(&p, key.key_material + key.key_material_size)) |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 91 | return; |
| 92 | DecryptKey(master_key); |
| 93 | } |
| 94 | |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame^] | 95 | KeyBlob::KeyBlob(const uint8_t* key_blob, size_t blob_size) |
| 96 | : nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]) { |
| 97 | if (!nonce_.get() || !tag_.get()) { |
| 98 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 99 | return; |
| 100 | } |
| 101 | error_ = KM_ERROR_OK; |
| 102 | |
| 103 | if (!Deserialize(&key_blob, key_blob + blob_size)) |
| 104 | return; |
| 105 | } |
| 106 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 107 | size_t KeyBlob::SerializedSize() const { |
| 108 | return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH + |
| 109 | enforced_.SerializedSize() + unenforced_.SerializedSize(); |
| 110 | } |
| 111 | |
| 112 | uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 113 | const uint8_t* start = buf; |
| 114 | buf = append_to_buf(buf, end, nonce(), NONCE_LENGTH); |
| 115 | buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length()); |
| 116 | buf = append_to_buf(buf, end, tag(), TAG_LENGTH); |
| 117 | buf = enforced_.Serialize(buf, end); |
| 118 | buf = unenforced_.Serialize(buf, end); |
| 119 | assert(buf - start == static_cast<ptrdiff_t>(SerializedSize())); |
| 120 | return buf; |
| 121 | } |
| 122 | |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame] | 123 | bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 124 | UniquePtr<uint8_t[]> tmp_key_ptr; |
| 125 | if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) || |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame] | 126 | !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_, &tmp_key_ptr) || |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 127 | !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) || |
| 128 | !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 129 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 130 | return false; |
| 131 | } |
| 132 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 133 | if (!ExtractKeyCharacteristics()) |
| 134 | return false; |
| 135 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 136 | encrypted_key_material_.reset(tmp_key_ptr.release()); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 137 | key_material_.reset(new uint8_t[key_material_length_]); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | void KeyBlob::EncryptKey(const keymaster_key_blob_t& master_key) { |
| 142 | UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_)); |
| 143 | if (error_ != KM_ERROR_OK) |
| 144 | return; |
| 145 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 146 | int ae_err = ae_encrypt(ctx.get(), nonce_.get(), key_material(), key_material_length(), |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 147 | NULL /* additional data */, 0 /* additional data length */, |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 148 | encrypted_key_material_.get(), tag_.get(), 1 /* final */); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 149 | if (ae_err < 0) { |
| 150 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 151 | return; |
| 152 | } |
| 153 | assert(ae_err == static_cast<int>(key_material_length_)); |
| 154 | error_ = KM_ERROR_OK; |
| 155 | } |
| 156 | |
| 157 | void KeyBlob::DecryptKey(const keymaster_key_blob_t& master_key) { |
| 158 | UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_)); |
| 159 | if (error_ != KM_ERROR_OK) |
| 160 | return; |
| 161 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 162 | int ae_err = |
| 163 | ae_decrypt(ctx.get(), nonce_.get(), encrypted_key_material(), key_material_length(), |
| 164 | NULL /* additional data */, 0 /* additional data length */, key_material_.get(), |
| 165 | tag_.get(), 1 /* final */); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 166 | if (ae_err == AE_INVALID) { |
| 167 | // Authentication failed! Decryption probably succeeded(ish), but we don't want to return |
| 168 | // any data when the authentication fails, so clear it. |
Shawn Willden | 74aff35 | 2014-08-11 14:08:31 -0600 | [diff] [blame] | 169 | memset_s(key_material_.get(), 0, key_material_length()); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 170 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 171 | return; |
| 172 | } else if (ae_err < 0) { |
| 173 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 174 | return; |
| 175 | } |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 176 | assert(ae_err == static_cast<int>(key_material_length())); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 177 | error_ = KM_ERROR_OK; |
| 178 | } |
| 179 | |
| 180 | ae_ctx* KeyBlob::InitializeKeyWrappingContext(const keymaster_key_blob_t& master_key, |
| 181 | keymaster_error_t* error) const { |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 182 | size_t derivation_data_length; |
| 183 | UniquePtr<const uint8_t[]> derivation_data(BuildDerivationData(&derivation_data_length)); |
| 184 | if (derivation_data.get() == NULL) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 185 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | *error = KM_ERROR_OK; |
| 190 | UniquePtr<ae_ctx, AeCtxDelete> ctx(ae_allocate(NULL)); |
| 191 | |
| 192 | SHA256_CTX sha256_ctx; |
| 193 | UniquePtr<uint8_t[]> hash_buf(new uint8_t[SHA256_DIGEST_LENGTH]); |
| 194 | Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH); |
| 195 | UniquePtr<uint8_t[]> derived_key(new uint8_t[AES_BLOCK_SIZE]); |
| 196 | Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE); |
| 197 | |
| 198 | if (ctx.get() == NULL || hash_buf.get() == NULL || derived_key.get() == NULL) { |
| 199 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | Eraser sha256_ctx_eraser(sha256_ctx); |
| 204 | |
| 205 | // Hash derivation data. |
| 206 | SHA256_Init(&sha256_ctx); |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 207 | SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 208 | SHA256_Final(hash_buf.get(), &sha256_ctx); |
| 209 | |
| 210 | // Encrypt hash with master key to build derived key. |
| 211 | AES_KEY aes_key; |
| 212 | Eraser aes_key_eraser(AES_KEY); |
| 213 | if (AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key) != |
| 214 | 0) { |
| 215 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 216 | return NULL; |
| 217 | } |
| 218 | AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key); |
| 219 | |
| 220 | // Set up AES OCB context using derived key. |
| 221 | if (ae_init(ctx.get(), derived_key.get(), AES_BLOCK_SIZE, NONCE_LENGTH, TAG_LENGTH) == |
| 222 | AE_SUCCESS) |
| 223 | return ctx.release(); |
| 224 | else { |
Shawn Willden | 74aff35 | 2014-08-11 14:08:31 -0600 | [diff] [blame] | 225 | memset_s(ctx.get(), 0, ae_ctx_sizeof()); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
| 228 | } |
| 229 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 230 | const uint8_t* KeyBlob::BuildDerivationData(size_t* derivation_data_length) const { |
| 231 | *derivation_data_length = |
| 232 | hidden_.SerializedSize() + enforced_.SerializedSize() + unenforced_.SerializedSize(); |
| 233 | uint8_t* derivation_data = new uint8_t[*derivation_data_length]; |
| 234 | if (derivation_data != NULL) { |
| 235 | uint8_t* buf = derivation_data; |
| 236 | uint8_t* end = derivation_data + *derivation_data_length; |
| 237 | buf = hidden_.Serialize(buf, end); |
| 238 | buf = enforced_.Serialize(buf, end); |
| 239 | buf = unenforced_.Serialize(buf, end); |
| 240 | } |
| 241 | return derivation_data; |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 242 | } |
| 243 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 244 | bool KeyBlob::ExtractKeyCharacteristics() { |
| 245 | if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) && |
| 246 | !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) { |
| 247 | error_ = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 248 | return false; |
| 249 | } |
| 250 | if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) && |
| 251 | !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) { |
| 252 | error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 258 | } // namespace keymaster |