blob: 86708375b163b60d5c29046812cec34f629a5e4b [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 Willden0c0f0362014-09-23 10:00:37 -060027KeyBlob::KeyBlob(const uint8_t* key_blob, size_t key_blob_length) : error_(KM_ERROR_OK) {
28 Deserialize(&key_blob, key_blob + key_blob_length);
29}
30
Shawn Willden5b877622014-09-17 14:32:43 -060031KeyBlob::KeyBlob(const keymaster_key_blob_t& key_blob) : error_(KM_ERROR_OK) {
Shawn Willden72014ad2014-09-17 13:04:10 -060032 const uint8_t* key_material = key_blob.key_material;
Shawn Willden0c0f0362014-09-23 10:00:37 -060033 Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size);
Shawn Willden407d4122014-08-25 16:49:13 -060034}
35
Shawn Willden4db3fbd2014-08-08 22:13:44 -060036size_t KeyBlob::SerializedSize() const {
Shawn Willden5b877622014-09-17 14:32:43 -060037 return 1 /* version byte */ + sizeof(uint32_t) /* nonce length */ + NONCE_LENGTH +
38 sizeof(uint32_t) + key_material_length() + sizeof(uint32_t) /* tag length */ +
39 TAG_LENGTH + enforced_.SerializedSize() + unenforced_.SerializedSize();
Shawn Willden4db3fbd2014-08-08 22:13:44 -060040}
41
Shawn Willden5b877622014-09-17 14:32:43 -060042const uint8_t BLOB_VERSION = 0;
43
Shawn Willden4db3fbd2014-08-08 22:13:44 -060044uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const {
Andreas Gampe7568e032014-11-25 19:52:47 -080045 const uint8_t* start __attribute__((__unused__)) = buf;
Shawn Willden5b877622014-09-17 14:32:43 -060046 *buf++ = BLOB_VERSION;
47 buf = append_size_and_data_to_buf(buf, end, nonce(), NONCE_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060048 buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length());
Shawn Willden5b877622014-09-17 14:32:43 -060049 buf = append_size_and_data_to_buf(buf, end, tag(), TAG_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060050 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 Willden5b877622014-09-17 14:32:43 -060057 const uint8_t* start = *buf_ptr;
58 uint8_t version = *(*buf_ptr)++;
59 size_t nonce_length;
60 size_t tag_length;
61 if (version != BLOB_VERSION ||
62 !copy_size_and_data_from_buf(buf_ptr, end, &nonce_length, &nonce_) ||
63 nonce_length != NONCE_LENGTH ||
64 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
65 &encrypted_key_material_) ||
66 !copy_size_and_data_from_buf(buf_ptr, end, &tag_length, &tag_) ||
67 tag_length != TAG_LENGTH || !enforced_.Deserialize(buf_ptr, end) ||
68 !unenforced_.Deserialize(buf_ptr, end)) {
69 *buf_ptr = start;
70 // This blob failed to parse. Either it's corrupted or it's a blob generated by an earlier
71 // version of keymaster using a previous blob format which did not include the version byte
72 // or the nonce or tag length fields. So we try to parse it as that previous version.
73 //
74 // Note that it's not really a problem if we erronously parse a corrupted blob, because
75 // decryption will fail the authentication check.
76 //
77 // A bigger potential problem is: What if a valid unversioned blob appears to parse
78 // correctly as a versioned blob? It would then be rejected during decryption, causing a
79 // valid key to become unusable. If this is a disk encryption key, upgrading to a keymaster
80 // version with the new format would destroy the user's data.
81 //
82 // What is the probability that an unversioned key could be successfully parsed as a version
83 // 0 key? The first 12 bytes of an unversioned key are the nonce, which, in the only
84 // keymaster version released with unversioned keys, is chosen randomly. In order for an
85 // unversioned key to parse as a version 0 key, the following must be true about the first
86 // five of those random bytes:
87 //
88 // 1. The first byte must be zero. This will happen with probability 1/2^8.
89 //
90 // 2. The second through fifth bytes must contain an unsigned integer value equal to
91 // NONCE_LENGTH. This will happen with probability 1/2^32.
92 //
93 // Based on those two checks alone, the probability of interpreting an unversioned blob as a
94 // version 0 blob is 1/2^40. That's small enough to be negligible, but there are additional
95 // checks which lower it further.
96 *buf_ptr = start;
97 if (!DeserializeUnversionedBlob(buf_ptr, end))
98 return false;
99 }
100 return ExtractKeyCharacteristics();
101}
102
103bool KeyBlob::DeserializeUnversionedBlob(const uint8_t** buf_ptr, const uint8_t* end) {
104 nonce_.reset(new uint8_t[NONCE_LENGTH]);
105 tag_.reset(new uint8_t[TAG_LENGTH]);
106 if (!nonce_.get() || !tag_.get()) {
107 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
108 return false;
109 }
110
Shawn Willdenf2282b32014-08-25 06:49:54 -0600111 if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) ||
Shawn Willden72014ad2014-09-17 13:04:10 -0600112 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
113 &encrypted_key_material_) ||
Shawn Willdenf2282b32014-08-25 06:49:54 -0600114 !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) ||
115 !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) {
Shawn Willden5b877622014-09-17 14:32:43 -0600116 encrypted_key_material_.reset();
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600117 error_ = KM_ERROR_INVALID_KEY_BLOB;
118 return false;
119 }
Shawn Willden72014ad2014-09-17 13:04:10 -0600120 return ExtractKeyCharacteristics();
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600121}
122
Shawn Willden72014ad2014-09-17 13:04:10 -0600123KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced)
124 : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600125}
126
Shawn Willden72014ad2014-09-17 13:04:10 -0600127void KeyBlob::SetEncryptedKey(uint8_t* encrypted_key_material, size_t encrypted_key_material_length,
128 uint8_t* nonce, uint8_t* tag) {
129 ClearKeyData();
130 encrypted_key_material_.reset(encrypted_key_material);
131 key_material_length_ = encrypted_key_material_length;
132 nonce_.reset(nonce);
133 tag_.reset(tag);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600134}
135
Shawn Willden1615f2e2014-08-13 10:37:40 -0600136bool KeyBlob::ExtractKeyCharacteristics() {
137 if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) &&
138 !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) {
139 error_ = KM_ERROR_UNSUPPORTED_ALGORITHM;
140 return false;
141 }
142 if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) &&
143 !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) {
144 error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE;
145 return false;
146 }
147 return true;
148}
149
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600150} // namespace keymaster