blob: e1a9c4964d44812dfb0f68775ff0887f1c27198e [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
56 memcpy(nonce_, nonce, NONCE_LENGTH);
57
58 key_material_length_ = key.key_material_size;
59 key_material_.reset(new uint8_t[key_material_length_]);
60 encrypted_key_material_.reset(new uint8_t[key_material_length_]);
61
62 if (key_material_.get() == NULL || encrypted_key_material_.get() == NULL) {
63 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
64 return;
65 }
66
67 memcpy(key_material_.get(), key.key_material, key_material_length_);
68 EncryptKey(master_key);
69}
70
Shawn Willden39b970b2014-08-11 09:11:21 -060071KeyBlob::KeyBlob(const keymaster_key_blob_t& key, const AuthorizationSet& hidden,
72 const keymaster_key_blob_t& master_key)
73 : hidden_(hidden) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -060074 if (!Deserialize(const_cast<const uint8_t**>(&(key.key_material)),
75 key.key_material + key.key_material_size))
76 return;
77 DecryptKey(master_key);
78}
79
80size_t KeyBlob::SerializedSize() const {
81 return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH +
82 enforced_.SerializedSize() + unenforced_.SerializedSize();
83}
84
85uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const {
86 const uint8_t* start = buf;
87 buf = append_to_buf(buf, end, nonce(), NONCE_LENGTH);
88 buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length());
89 buf = append_to_buf(buf, end, tag(), TAG_LENGTH);
90 buf = enforced_.Serialize(buf, end);
91 buf = unenforced_.Serialize(buf, end);
92 assert(buf - start == static_cast<ptrdiff_t>(SerializedSize()));
93 return buf;
94}
95
96bool KeyBlob::Deserialize(const uint8_t** buf, const uint8_t* end) {
97 uint8_t* tmp_key_ptr = NULL;
98
99 if (!copy_from_buf(buf, end, nonce_, NONCE_LENGTH) ||
100 !copy_size_and_data_from_buf(buf, end, &key_material_length_, &tmp_key_ptr) ||
101 !copy_from_buf(buf, end, tag_, TAG_LENGTH) || !enforced_.Deserialize(buf, end) ||
102 !unenforced_.Deserialize(buf, end)) {
103 if (tmp_key_ptr != NULL)
Shawn Willden39b970b2014-08-11 09:11:21 -0600104 delete[] tmp_key_ptr;
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600105 error_ = KM_ERROR_INVALID_KEY_BLOB;
106 return false;
107 }
108
109 encrypted_key_material_.reset(tmp_key_ptr);
110 key_material_.reset(new uint8_t[key_material_length_]);
111 return true;
112}
113
114void KeyBlob::EncryptKey(const keymaster_key_blob_t& master_key) {
115 UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_));
116 if (error_ != KM_ERROR_OK)
117 return;
118
119 int ae_err = ae_encrypt(ctx.get(), nonce_, key_material(), key_material_length(),
120 NULL /* additional data */, 0 /* additional data length */,
121 encrypted_key_material_.get(), tag_, 1 /* final */);
122 if (ae_err < 0) {
123 error_ = KM_ERROR_UNKNOWN_ERROR;
124 return;
125 }
126 assert(ae_err == static_cast<int>(key_material_length_));
127 error_ = KM_ERROR_OK;
128}
129
130void KeyBlob::DecryptKey(const keymaster_key_blob_t& master_key) {
131 UniquePtr<ae_ctx, AeCtxDelete> ctx(InitializeKeyWrappingContext(master_key, &error_));
132 if (error_ != KM_ERROR_OK)
133 return;
134
135 int ae_err = ae_decrypt(ctx.get(), nonce_, encrypted_key_material(), key_material_length(),
136 NULL /* additional data */, 0 /* additional data length */,
137 key_material_.get(), tag(), 1 /* final */);
138 if (ae_err == AE_INVALID) {
139 // Authentication failed! Decryption probably succeeded(ish), but we don't want to return
140 // any data when the authentication fails, so clear it.
Shawn Willden74aff352014-08-11 14:08:31 -0600141 memset_s(key_material_.get(), 0, key_material_length());
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600142 error_ = KM_ERROR_INVALID_KEY_BLOB;
143 return;
144 } else if (ae_err < 0) {
145 error_ = KM_ERROR_UNKNOWN_ERROR;
146 return;
147 }
148 assert(ae_err == static_cast<int>(key_material_length_));
149 error_ = KM_ERROR_OK;
150}
151
152ae_ctx* KeyBlob::InitializeKeyWrappingContext(const keymaster_key_blob_t& master_key,
153 keymaster_error_t* error) const {
Shawn Willden39b970b2014-08-11 09:11:21 -0600154 size_t derivation_data_length;
155 UniquePtr<const uint8_t[]> derivation_data(BuildDerivationData(&derivation_data_length));
156 if (derivation_data.get() == NULL) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600157 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
158 return NULL;
159 }
160
161 *error = KM_ERROR_OK;
162 UniquePtr<ae_ctx, AeCtxDelete> ctx(ae_allocate(NULL));
163
164 SHA256_CTX sha256_ctx;
165 UniquePtr<uint8_t[]> hash_buf(new uint8_t[SHA256_DIGEST_LENGTH]);
166 Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH);
167 UniquePtr<uint8_t[]> derived_key(new uint8_t[AES_BLOCK_SIZE]);
168 Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE);
169
170 if (ctx.get() == NULL || hash_buf.get() == NULL || derived_key.get() == NULL) {
171 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
172 return NULL;
173 }
174
175 Eraser sha256_ctx_eraser(sha256_ctx);
176
177 // Hash derivation data.
178 SHA256_Init(&sha256_ctx);
Shawn Willden39b970b2014-08-11 09:11:21 -0600179 SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600180 SHA256_Final(hash_buf.get(), &sha256_ctx);
181
182 // Encrypt hash with master key to build derived key.
183 AES_KEY aes_key;
184 Eraser aes_key_eraser(AES_KEY);
185 if (AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key) !=
186 0) {
187 *error = KM_ERROR_UNKNOWN_ERROR;
188 return NULL;
189 }
190 AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key);
191
192 // Set up AES OCB context using derived key.
193 if (ae_init(ctx.get(), derived_key.get(), AES_BLOCK_SIZE, NONCE_LENGTH, TAG_LENGTH) ==
194 AE_SUCCESS)
195 return ctx.release();
196 else {
Shawn Willden74aff352014-08-11 14:08:31 -0600197 memset_s(ctx.get(), 0, ae_ctx_sizeof());
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600198 return NULL;
199 }
200}
201
Shawn Willden39b970b2014-08-11 09:11:21 -0600202const uint8_t* KeyBlob::BuildDerivationData(size_t* derivation_data_length) const {
203 *derivation_data_length =
204 hidden_.SerializedSize() + enforced_.SerializedSize() + unenforced_.SerializedSize();
205 uint8_t* derivation_data = new uint8_t[*derivation_data_length];
206 if (derivation_data != NULL) {
207 uint8_t* buf = derivation_data;
208 uint8_t* end = derivation_data + *derivation_data_length;
209 buf = hidden_.Serialize(buf, end);
210 buf = enforced_.Serialize(buf, end);
211 buf = unenforced_.Serialize(buf, end);
212 }
213 return derivation_data;
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600214}
215
216} // namespace keymaster