blob: f1d9ce550ae8eed0d248dbb1009c1e457627dcd1 [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
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
26namespace keymaster {
27
28struct AeCtxDelete {
29 void operator()(ae_ctx* p) {
30 ae_clear(p);
31 ae_free(p);
32 }
33};
34
35const size_t KeyBlob::NONCE_LENGTH;
36const size_t KeyBlob::TAG_LENGTH;
37
38KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced,
Shawn Willden39b970b2014-08-11 09:11:21 -060039 const AuthorizationSet& hidden, const keymaster_key_blob_t& key,
Shawn Willdenebf627f2014-08-12 11:15:29 -060040 const keymaster_key_blob_t& master_key, const uint8_t nonce[NONCE_LENGTH])
Shawn Willden39b970b2014-08-11 09:11:21 -060041 : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced), hidden_(hidden) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060042 if (enforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE ||
Shawn Willden76364712014-08-11 17:48:04 -060043 unenforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE ||
44 hidden_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060045 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
46 return;
47 }
48
49 if (enforced_.is_valid() != AuthorizationSet::OK ||
Shawn Willden76364712014-08-11 17:48:04 -060050 unenforced_.is_valid() != AuthorizationSet::OK ||
51 hidden_.is_valid() != AuthorizationSet::OK) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060052 error_ = KM_ERROR_UNKNOWN_ERROR;
53 return;
54 }
55
Shawn Willden1615f2e2014-08-13 10:37:40 -060056 if (!ExtractKeyCharacteristics())
57 return;
58
Shawn Willden4db3fbd2014-08-08 22:13:44 -060059 memcpy(nonce_, nonce, NONCE_LENGTH);
60
61 key_material_length_ = key.key_material_size;
62 key_material_.reset(new uint8_t[key_material_length_]);
63 encrypted_key_material_.reset(new uint8_t[key_material_length_]);
64
65 if (key_material_.get() == NULL || encrypted_key_material_.get() == NULL) {
66 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
67 return;
68 }
69
70 memcpy(key_material_.get(), key.key_material, key_material_length_);
71 EncryptKey(master_key);
72}
73
Shawn Willden39b970b2014-08-11 09:11:21 -060074KeyBlob::KeyBlob(const keymaster_key_blob_t& key, const AuthorizationSet& hidden,
75 const keymaster_key_blob_t& master_key)
76 : hidden_(hidden) {
Shawn Willden172f8c92014-08-17 07:50:34 -060077 const uint8_t* p = key.key_material;
78 if (!Deserialize(&p, key.key_material + key.key_material_size))
Shawn Willden4db3fbd2014-08-08 22:13:44 -060079 return;
80 DecryptKey(master_key);
81}
82
83size_t KeyBlob::SerializedSize() const {
84 return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH +
85 enforced_.SerializedSize() + unenforced_.SerializedSize();
86}
87
88uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const {
89 const uint8_t* start = buf;
90 buf = append_to_buf(buf, end, nonce(), NONCE_LENGTH);
91 buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length());
92 buf = append_to_buf(buf, end, tag(), TAG_LENGTH);
93 buf = enforced_.Serialize(buf, end);
94 buf = unenforced_.Serialize(buf, end);
95 assert(buf - start == static_cast<ptrdiff_t>(SerializedSize()));
96 return buf;
97}
98
Shawn Willden172f8c92014-08-17 07:50:34 -060099bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600100 uint8_t* tmp_key_ptr = NULL;
Shawn Willden172f8c92014-08-17 07:50:34 -0600101 if (!copy_from_buf(buf_ptr, end, nonce_, NONCE_LENGTH) ||
102 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_, &tmp_key_ptr) ||
103 !copy_from_buf(buf_ptr, end, tag_, TAG_LENGTH) || !enforced_.Deserialize(buf_ptr, end) ||
104 !unenforced_.Deserialize(buf_ptr, end)) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600105 if (tmp_key_ptr != NULL)
Shawn Willden39b970b2014-08-11 09:11:21 -0600106 delete[] tmp_key_ptr;
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600107 error_ = KM_ERROR_INVALID_KEY_BLOB;
108 return false;
109 }
110
Shawn Willden1615f2e2014-08-13 10:37:40 -0600111 if (!ExtractKeyCharacteristics())
112 return false;
113
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600114 encrypted_key_material_.reset(tmp_key_ptr);
115 key_material_.reset(new uint8_t[key_material_length_]);
116 return true;
117}
118
119void KeyBlob::EncryptKey(const keymaster_key_blob_t& master_key) {
120 UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_));
121 if (error_ != KM_ERROR_OK)
122 return;
123
124 int ae_err = ae_encrypt(ctx.get(), nonce_, key_material(), key_material_length(),
125 NULL /* additional data */, 0 /* additional data length */,
126 encrypted_key_material_.get(), tag_, 1 /* final */);
127 if (ae_err < 0) {
128 error_ = KM_ERROR_UNKNOWN_ERROR;
129 return;
130 }
131 assert(ae_err == static_cast<int>(key_material_length_));
132 error_ = KM_ERROR_OK;
133}
134
135void KeyBlob::DecryptKey(const keymaster_key_blob_t& master_key) {
136 UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_));
137 if (error_ != KM_ERROR_OK)
138 return;
139
140 int ae_err = ae_decrypt(ctx.get(), nonce_, encrypted_key_material(), key_material_length(),
141 NULL /* additional data */, 0 /* additional data length */,
142 key_material_.get(), tag(), 1 /* final */);
143 if (ae_err == AE_INVALID) {
144 // Authentication failed! Decryption probably succeeded(ish), but we don't want to return
145 // any data when the authentication fails, so clear it.
Shawn Willden74aff352014-08-11 14:08:31 -0600146 memset_s(key_material_.get(), 0, key_material_length());
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600147 error_ = KM_ERROR_INVALID_KEY_BLOB;
148 return;
149 } else if (ae_err < 0) {
150 error_ = KM_ERROR_UNKNOWN_ERROR;
151 return;
152 }
153 assert(ae_err == static_cast<int>(key_material_length_));
154 error_ = KM_ERROR_OK;
155}
156
157ae_ctx* KeyBlob::InitializeKeyWrappingContext(const keymaster_key_blob_t& master_key,
158 keymaster_error_t* error) const {
Shawn Willden39b970b2014-08-11 09:11:21 -0600159 size_t derivation_data_length;
160 UniquePtr<const uint8_t[]> derivation_data(BuildDerivationData(&derivation_data_length));
161 if (derivation_data.get() == NULL) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600162 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
163 return NULL;
164 }
165
166 *error = KM_ERROR_OK;
167 UniquePtr<ae_ctx, AeCtxDelete> ctx(ae_allocate(NULL));
168
169 SHA256_CTX sha256_ctx;
170 UniquePtr<uint8_t[]> hash_buf(new uint8_t[SHA256_DIGEST_LENGTH]);
171 Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH);
172 UniquePtr<uint8_t[]> derived_key(new uint8_t[AES_BLOCK_SIZE]);
173 Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE);
174
175 if (ctx.get() == NULL || hash_buf.get() == NULL || derived_key.get() == NULL) {
176 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
177 return NULL;
178 }
179
180 Eraser sha256_ctx_eraser(sha256_ctx);
181
182 // Hash derivation data.
183 SHA256_Init(&sha256_ctx);
Shawn Willden39b970b2014-08-11 09:11:21 -0600184 SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600185 SHA256_Final(hash_buf.get(), &sha256_ctx);
186
187 // Encrypt hash with master key to build derived key.
188 AES_KEY aes_key;
189 Eraser aes_key_eraser(AES_KEY);
190 if (AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key) !=
191 0) {
192 *error = KM_ERROR_UNKNOWN_ERROR;
193 return NULL;
194 }
195 AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key);
196
197 // Set up AES OCB context using derived key.
198 if (ae_init(ctx.get(), derived_key.get(), AES_BLOCK_SIZE, NONCE_LENGTH, TAG_LENGTH) ==
199 AE_SUCCESS)
200 return ctx.release();
201 else {
Shawn Willden74aff352014-08-11 14:08:31 -0600202 memset_s(ctx.get(), 0, ae_ctx_sizeof());
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600203 return NULL;
204 }
205}
206
Shawn Willden39b970b2014-08-11 09:11:21 -0600207const uint8_t* KeyBlob::BuildDerivationData(size_t* derivation_data_length) const {
208 *derivation_data_length =
209 hidden_.SerializedSize() + enforced_.SerializedSize() + unenforced_.SerializedSize();
210 uint8_t* derivation_data = new uint8_t[*derivation_data_length];
211 if (derivation_data != NULL) {
212 uint8_t* buf = derivation_data;
213 uint8_t* end = derivation_data + *derivation_data_length;
214 buf = hidden_.Serialize(buf, end);
215 buf = enforced_.Serialize(buf, end);
216 buf = unenforced_.Serialize(buf, end);
217 }
218 return derivation_data;
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600219}
220
Shawn Willden1615f2e2014-08-13 10:37:40 -0600221bool KeyBlob::ExtractKeyCharacteristics() {
222 if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) &&
223 !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) {
224 error_ = KM_ERROR_UNSUPPORTED_ALGORITHM;
225 return false;
226 }
227 if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) &&
228 !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) {
229 error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE;
230 return false;
231 }
232 return true;
233}
234
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600235} // namespace keymaster