blob: 188e75958a9f6906eb34b290a6db9cc170400e01 [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 Willden5b877622014-09-17 14:32:43 -060027KeyBlob::KeyBlob(const keymaster_key_blob_t& key_blob) : error_(KM_ERROR_OK) {
Shawn Willden72014ad2014-09-17 13:04:10 -060028 const uint8_t* key_material = key_blob.key_material;
29 if (!Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size))
Shawn Willden407d4122014-08-25 16:49:13 -060030 return;
31}
32
Shawn Willden4db3fbd2014-08-08 22:13:44 -060033size_t KeyBlob::SerializedSize() const {
Shawn Willden5b877622014-09-17 14:32:43 -060034 return 1 /* version byte */ + sizeof(uint32_t) /* nonce length */ + NONCE_LENGTH +
35 sizeof(uint32_t) + key_material_length() + sizeof(uint32_t) /* tag length */ +
36 TAG_LENGTH + enforced_.SerializedSize() + unenforced_.SerializedSize();
Shawn Willden4db3fbd2014-08-08 22:13:44 -060037}
38
Shawn Willden5b877622014-09-17 14:32:43 -060039const uint8_t BLOB_VERSION = 0;
40
Shawn Willden4db3fbd2014-08-08 22:13:44 -060041uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const {
42 const uint8_t* start = buf;
Shawn Willden5b877622014-09-17 14:32:43 -060043 *buf++ = BLOB_VERSION;
44 buf = append_size_and_data_to_buf(buf, end, nonce(), NONCE_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060045 buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length());
Shawn Willden5b877622014-09-17 14:32:43 -060046 buf = append_size_and_data_to_buf(buf, end, tag(), TAG_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060047 buf = enforced_.Serialize(buf, end);
48 buf = unenforced_.Serialize(buf, end);
49 assert(buf - start == static_cast<ptrdiff_t>(SerializedSize()));
50 return buf;
51}
52
Shawn Willden172f8c92014-08-17 07:50:34 -060053bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden5b877622014-09-17 14:32:43 -060054 const uint8_t* start = *buf_ptr;
55 uint8_t version = *(*buf_ptr)++;
56 size_t nonce_length;
57 size_t tag_length;
58 if (version != BLOB_VERSION ||
59 !copy_size_and_data_from_buf(buf_ptr, end, &nonce_length, &nonce_) ||
60 nonce_length != NONCE_LENGTH ||
61 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
62 &encrypted_key_material_) ||
63 !copy_size_and_data_from_buf(buf_ptr, end, &tag_length, &tag_) ||
64 tag_length != TAG_LENGTH || !enforced_.Deserialize(buf_ptr, end) ||
65 !unenforced_.Deserialize(buf_ptr, end)) {
66 *buf_ptr = start;
67 // This blob failed to parse. Either it's corrupted or it's a blob generated by an earlier
68 // version of keymaster using a previous blob format which did not include the version byte
69 // or the nonce or tag length fields. So we try to parse it as that previous version.
70 //
71 // Note that it's not really a problem if we erronously parse a corrupted blob, because
72 // decryption will fail the authentication check.
73 //
74 // A bigger potential problem is: What if a valid unversioned blob appears to parse
75 // correctly as a versioned blob? It would then be rejected during decryption, causing a
76 // valid key to become unusable. If this is a disk encryption key, upgrading to a keymaster
77 // version with the new format would destroy the user's data.
78 //
79 // What is the probability that an unversioned key could be successfully parsed as a version
80 // 0 key? The first 12 bytes of an unversioned key are the nonce, which, in the only
81 // keymaster version released with unversioned keys, is chosen randomly. In order for an
82 // unversioned key to parse as a version 0 key, the following must be true about the first
83 // five of those random bytes:
84 //
85 // 1. The first byte must be zero. This will happen with probability 1/2^8.
86 //
87 // 2. The second through fifth bytes must contain an unsigned integer value equal to
88 // NONCE_LENGTH. This will happen with probability 1/2^32.
89 //
90 // Based on those two checks alone, the probability of interpreting an unversioned blob as a
91 // version 0 blob is 1/2^40. That's small enough to be negligible, but there are additional
92 // checks which lower it further.
93 *buf_ptr = start;
94 if (!DeserializeUnversionedBlob(buf_ptr, end))
95 return false;
96 }
97 return ExtractKeyCharacteristics();
98}
99
100bool KeyBlob::DeserializeUnversionedBlob(const uint8_t** buf_ptr, const uint8_t* end) {
101 nonce_.reset(new uint8_t[NONCE_LENGTH]);
102 tag_.reset(new uint8_t[TAG_LENGTH]);
103 if (!nonce_.get() || !tag_.get()) {
104 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
105 return false;
106 }
107
Shawn Willdenf2282b32014-08-25 06:49:54 -0600108 if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) ||
Shawn Willden72014ad2014-09-17 13:04:10 -0600109 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
110 &encrypted_key_material_) ||
Shawn Willdenf2282b32014-08-25 06:49:54 -0600111 !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) ||
112 !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) {
Shawn Willden5b877622014-09-17 14:32:43 -0600113 encrypted_key_material_.reset();
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600114 error_ = KM_ERROR_INVALID_KEY_BLOB;
115 return false;
116 }
Shawn Willden72014ad2014-09-17 13:04:10 -0600117 return ExtractKeyCharacteristics();
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600118}
119
Shawn Willden72014ad2014-09-17 13:04:10 -0600120KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced)
121 : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600122}
123
Shawn Willden72014ad2014-09-17 13:04:10 -0600124void KeyBlob::SetEncryptedKey(uint8_t* encrypted_key_material, size_t encrypted_key_material_length,
125 uint8_t* nonce, uint8_t* tag) {
126 ClearKeyData();
127 encrypted_key_material_.reset(encrypted_key_material);
128 key_material_length_ = encrypted_key_material_length;
129 nonce_.reset(nonce);
130 tag_.reset(tag);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600131}
132
Shawn Willden1615f2e2014-08-13 10:37:40 -0600133bool KeyBlob::ExtractKeyCharacteristics() {
134 if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) &&
135 !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) {
136 error_ = KM_ERROR_UNSUPPORTED_ALGORITHM;
137 return false;
138 }
139 if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) &&
140 !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) {
141 error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE;
142 return false;
143 }
144 return true;
145}
146
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600147} // namespace keymaster