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, |
| 40 | const keymaster_key_blob_t& master_key, uint8_t nonce[NONCE_LENGTH]) |
| 41 | : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced), hidden_(hidden) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 42 | if (enforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE || |
| 43 | unenforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE) { |
| 44 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (enforced_.is_valid() != AuthorizationSet::OK || |
| 49 | unenforced_.is_valid() != AuthorizationSet::OK) { |
| 50 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | memcpy(nonce_, nonce, NONCE_LENGTH); |
| 55 | |
| 56 | key_material_length_ = key.key_material_size; |
| 57 | key_material_.reset(new uint8_t[key_material_length_]); |
| 58 | encrypted_key_material_.reset(new uint8_t[key_material_length_]); |
| 59 | |
| 60 | if (key_material_.get() == NULL || encrypted_key_material_.get() == NULL) { |
| 61 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | memcpy(key_material_.get(), key.key_material, key_material_length_); |
| 66 | EncryptKey(master_key); |
| 67 | } |
| 68 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame^] | 69 | KeyBlob::KeyBlob(const keymaster_key_blob_t& key, const AuthorizationSet& hidden, |
| 70 | const keymaster_key_blob_t& master_key) |
| 71 | : hidden_(hidden) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 72 | if (!Deserialize(const_cast<const uint8_t**>(&(key.key_material)), |
| 73 | key.key_material + key.key_material_size)) |
| 74 | return; |
| 75 | DecryptKey(master_key); |
| 76 | } |
| 77 | |
| 78 | size_t KeyBlob::SerializedSize() const { |
| 79 | return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH + |
| 80 | enforced_.SerializedSize() + unenforced_.SerializedSize(); |
| 81 | } |
| 82 | |
| 83 | uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 84 | const uint8_t* start = buf; |
| 85 | buf = append_to_buf(buf, end, nonce(), NONCE_LENGTH); |
| 86 | buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length()); |
| 87 | buf = append_to_buf(buf, end, tag(), TAG_LENGTH); |
| 88 | buf = enforced_.Serialize(buf, end); |
| 89 | buf = unenforced_.Serialize(buf, end); |
| 90 | assert(buf - start == static_cast<ptrdiff_t>(SerializedSize())); |
| 91 | return buf; |
| 92 | } |
| 93 | |
| 94 | bool KeyBlob::Deserialize(const uint8_t** buf, const uint8_t* end) { |
| 95 | uint8_t* tmp_key_ptr = NULL; |
| 96 | |
| 97 | if (!copy_from_buf(buf, end, nonce_, NONCE_LENGTH) || |
| 98 | !copy_size_and_data_from_buf(buf, end, &key_material_length_, &tmp_key_ptr) || |
| 99 | !copy_from_buf(buf, end, tag_, TAG_LENGTH) || !enforced_.Deserialize(buf, end) || |
| 100 | !unenforced_.Deserialize(buf, end)) { |
| 101 | if (tmp_key_ptr != NULL) |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame^] | 102 | delete[] tmp_key_ptr; |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 103 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | encrypted_key_material_.reset(tmp_key_ptr); |
| 108 | key_material_.reset(new uint8_t[key_material_length_]); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | void KeyBlob::EncryptKey(const keymaster_key_blob_t& master_key) { |
| 113 | UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_)); |
| 114 | if (error_ != KM_ERROR_OK) |
| 115 | return; |
| 116 | |
| 117 | int ae_err = ae_encrypt(ctx.get(), nonce_, key_material(), key_material_length(), |
| 118 | NULL /* additional data */, 0 /* additional data length */, |
| 119 | encrypted_key_material_.get(), tag_, 1 /* final */); |
| 120 | if (ae_err < 0) { |
| 121 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 122 | return; |
| 123 | } |
| 124 | assert(ae_err == static_cast<int>(key_material_length_)); |
| 125 | error_ = KM_ERROR_OK; |
| 126 | } |
| 127 | |
| 128 | void KeyBlob::DecryptKey(const keymaster_key_blob_t& master_key) { |
| 129 | UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_)); |
| 130 | if (error_ != KM_ERROR_OK) |
| 131 | return; |
| 132 | |
| 133 | int ae_err = ae_decrypt(ctx.get(), nonce_, encrypted_key_material(), key_material_length(), |
| 134 | NULL /* additional data */, 0 /* additional data length */, |
| 135 | key_material_.get(), tag(), 1 /* final */); |
| 136 | if (ae_err == AE_INVALID) { |
| 137 | // Authentication failed! Decryption probably succeeded(ish), but we don't want to return |
| 138 | // any data when the authentication fails, so clear it. |
| 139 | memset(key_material_.get(), 0, key_material_length()); |
| 140 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 141 | return; |
| 142 | } else if (ae_err < 0) { |
| 143 | error_ = KM_ERROR_UNKNOWN_ERROR; |
| 144 | return; |
| 145 | } |
| 146 | assert(ae_err == static_cast<int>(key_material_length_)); |
| 147 | error_ = KM_ERROR_OK; |
| 148 | } |
| 149 | |
| 150 | ae_ctx* KeyBlob::InitializeKeyWrappingContext(const keymaster_key_blob_t& master_key, |
| 151 | keymaster_error_t* error) const { |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame^] | 152 | size_t derivation_data_length; |
| 153 | UniquePtr<const uint8_t[]> derivation_data(BuildDerivationData(&derivation_data_length)); |
| 154 | if (derivation_data.get() == NULL) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 155 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | *error = KM_ERROR_OK; |
| 160 | UniquePtr<ae_ctx, AeCtxDelete> ctx(ae_allocate(NULL)); |
| 161 | |
| 162 | SHA256_CTX sha256_ctx; |
| 163 | UniquePtr<uint8_t[]> hash_buf(new uint8_t[SHA256_DIGEST_LENGTH]); |
| 164 | Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH); |
| 165 | UniquePtr<uint8_t[]> derived_key(new uint8_t[AES_BLOCK_SIZE]); |
| 166 | Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE); |
| 167 | |
| 168 | if (ctx.get() == NULL || hash_buf.get() == NULL || derived_key.get() == NULL) { |
| 169 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | Eraser sha256_ctx_eraser(sha256_ctx); |
| 174 | |
| 175 | // Hash derivation data. |
| 176 | SHA256_Init(&sha256_ctx); |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame^] | 177 | SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 178 | SHA256_Final(hash_buf.get(), &sha256_ctx); |
| 179 | |
| 180 | // Encrypt hash with master key to build derived key. |
| 181 | AES_KEY aes_key; |
| 182 | Eraser aes_key_eraser(AES_KEY); |
| 183 | if (AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key) != |
| 184 | 0) { |
| 185 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 186 | return NULL; |
| 187 | } |
| 188 | AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key); |
| 189 | |
| 190 | // Set up AES OCB context using derived key. |
| 191 | if (ae_init(ctx.get(), derived_key.get(), AES_BLOCK_SIZE, NONCE_LENGTH, TAG_LENGTH) == |
| 192 | AE_SUCCESS) |
| 193 | return ctx.release(); |
| 194 | else { |
| 195 | memset(ctx.get(), 0, ae_ctx_sizeof()); |
| 196 | return NULL; |
| 197 | } |
| 198 | } |
| 199 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame^] | 200 | const uint8_t* KeyBlob::BuildDerivationData(size_t* derivation_data_length) const { |
| 201 | *derivation_data_length = |
| 202 | hidden_.SerializedSize() + enforced_.SerializedSize() + unenforced_.SerializedSize(); |
| 203 | uint8_t* derivation_data = new uint8_t[*derivation_data_length]; |
| 204 | if (derivation_data != NULL) { |
| 205 | uint8_t* buf = derivation_data; |
| 206 | uint8_t* end = derivation_data + *derivation_data_length; |
| 207 | buf = hidden_.Serialize(buf, end); |
| 208 | buf = enforced_.Serialize(buf, end); |
| 209 | buf = unenforced_.Serialize(buf, end); |
| 210 | } |
| 211 | return derivation_data; |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace keymaster |