blob: 3e6ff9ab6ed256b20284552fedf3e4a9aa71673d [file] [log] [blame]
Shawn Willden4db3fbd2014-08-08 22:13:44 -06001/*
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 Willden98d9b922014-08-26 08:14:10 -060019#include <keymaster/google_keymaster_utils.h>
Shawn Willden368bc772014-08-27 06:45:34 -060020#include <keymaster/key_blob.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060021
Shawn Willden4db3fbd2014-08-08 22:13:44 -060022namespace keymaster {
23
Shawn Willden4db3fbd2014-08-08 22:13:44 -060024const size_t KeyBlob::NONCE_LENGTH;
25const size_t KeyBlob::TAG_LENGTH;
26
Shawn Willden72014ad2014-09-17 13:04:10 -060027KeyBlob::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 Willden407d4122014-08-25 16:49:13 -060029 if (!nonce_.get() || !tag_.get()) {
30 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
31 return;
32 }
33 error_ = KM_ERROR_OK;
34
Shawn Willden72014ad2014-09-17 13:04:10 -060035 const uint8_t* key_material = key_blob.key_material;
36 if (!Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size))
Shawn Willden407d4122014-08-25 16:49:13 -060037 return;
38}
39
Shawn Willden4db3fbd2014-08-08 22:13:44 -060040size_t KeyBlob::SerializedSize() const {
41 return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH +
42 enforced_.SerializedSize() + unenforced_.SerializedSize();
43}
44
45uint8_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 Willden172f8c92014-08-17 07:50:34 -060056bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -060057 if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) ||
Shawn Willden72014ad2014-09-17 13:04:10 -060058 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
59 &encrypted_key_material_) ||
Shawn Willdenf2282b32014-08-25 06:49:54 -060060 !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) ||
61 !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060062 error_ = KM_ERROR_INVALID_KEY_BLOB;
63 return false;
64 }
Shawn Willden72014ad2014-09-17 13:04:10 -060065 return ExtractKeyCharacteristics();
Shawn Willden4db3fbd2014-08-08 22:13:44 -060066}
67
Shawn Willden72014ad2014-09-17 13:04:10 -060068KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced)
69 : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060070}
71
Shawn Willden72014ad2014-09-17 13:04:10 -060072void 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 Willden4db3fbd2014-08-08 22:13:44 -060079}
80
Shawn Willden1615f2e2014-08-13 10:37:40 -060081bool 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 Willden4db3fbd2014-08-08 22:13:44 -060095} // namespace keymaster