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 | |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 19 | #include <keymaster/google_keymaster_utils.h> |
Shawn Willden | 368bc77 | 2014-08-27 06:45:34 -0600 | [diff] [blame] | 20 | #include <keymaster/key_blob.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 21 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 22 | namespace keymaster { |
| 23 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 24 | const size_t KeyBlob::NONCE_LENGTH; |
| 25 | const size_t KeyBlob::TAG_LENGTH; |
| 26 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 27 | KeyBlob::KeyBlob(const keymaster_key_blob_t& key_blob) |
| 28 | : error_(KM_ERROR_OK), nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]) { |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame] | 29 | if (!nonce_.get() || !tag_.get()) { |
| 30 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 31 | return; |
| 32 | } |
| 33 | error_ = KM_ERROR_OK; |
| 34 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 35 | const uint8_t* key_material = key_blob.key_material; |
| 36 | if (!Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size)) |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 40 | size_t KeyBlob::SerializedSize() const { |
| 41 | return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH + |
| 42 | enforced_.SerializedSize() + unenforced_.SerializedSize(); |
| 43 | } |
| 44 | |
| 45 | uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 46 | const uint8_t* start = buf; |
| 47 | buf = append_to_buf(buf, end, nonce(), NONCE_LENGTH); |
| 48 | buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length()); |
| 49 | buf = append_to_buf(buf, end, tag(), TAG_LENGTH); |
| 50 | buf = enforced_.Serialize(buf, end); |
| 51 | buf = unenforced_.Serialize(buf, end); |
| 52 | assert(buf - start == static_cast<ptrdiff_t>(SerializedSize())); |
| 53 | return buf; |
| 54 | } |
| 55 | |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame] | 56 | bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 57 | if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) || |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 58 | !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_, |
| 59 | &encrypted_key_material_) || |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 60 | !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) || |
| 61 | !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 62 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 63 | return false; |
| 64 | } |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 65 | return ExtractKeyCharacteristics(); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 68 | KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced) |
| 69 | : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame^] | 72 | void KeyBlob::SetEncryptedKey(uint8_t* encrypted_key_material, size_t encrypted_key_material_length, |
| 73 | uint8_t* nonce, uint8_t* tag) { |
| 74 | ClearKeyData(); |
| 75 | encrypted_key_material_.reset(encrypted_key_material); |
| 76 | key_material_length_ = encrypted_key_material_length; |
| 77 | nonce_.reset(nonce); |
| 78 | tag_.reset(tag); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 81 | bool KeyBlob::ExtractKeyCharacteristics() { |
| 82 | if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) && |
| 83 | !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) { |
| 84 | error_ = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 85 | return false; |
| 86 | } |
| 87 | if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) && |
| 88 | !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) { |
| 89 | error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 95 | } // namespace keymaster |