blob: 7540dc509e17e3b38b1655c00034584590a0bc5f [file] [log] [blame]
Shawn Willden0cb69422015-05-26 08:31:37 -06001/*
2 * Copyright 2015 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
Janis Danisevskisf54cc932017-05-10 15:29:10 -070017#include <keymaster/key_blob_utils/ocb_utils.h>
Shawn Willden0cb69422015-05-26 08:31:37 -060018
19#include <assert.h>
20
21#include <openssl/aes.h>
22#include <openssl/sha.h>
23
24#include <hardware/keymaster_defs.h>
Janis Danisevskisf54cc932017-05-10 15:29:10 -070025
Shawn Willden0cb69422015-05-26 08:31:37 -060026#include <keymaster/android_keymaster_utils.h>
Inseob Kim5786bc92020-01-06 18:31:42 +090027#include <keymaster/authorization_set.h>
Janis Danisevskisf54cc932017-05-10 15:29:10 -070028#include <keymaster/km_openssl/openssl_err.h>
Inseob Kim5786bc92020-01-06 18:31:42 +090029#include <keymaster/new.h>
Shawn Willden0cb69422015-05-26 08:31:37 -060030
31namespace keymaster {
32
33class AeCtx {
34 public:
Yi Kong3712b272018-07-30 15:53:23 -070035 AeCtx() : ctx_(ae_allocate(nullptr)) {}
Shawn Willden0cb69422015-05-26 08:31:37 -060036 ~AeCtx() {
37 ae_clear(ctx_);
38 ae_free(ctx_);
39 }
40
41 ae_ctx* get() { return ctx_; }
42
43 private:
44 ae_ctx* ctx_;
45};
46
47static keymaster_error_t BuildDerivationData(const AuthorizationSet& hw_enforced,
48 const AuthorizationSet& sw_enforced,
49 const AuthorizationSet& hidden,
50 UniquePtr<uint8_t[]>* derivation_data,
51 size_t* derivation_data_length) {
52 *derivation_data_length =
53 hidden.SerializedSize() + hw_enforced.SerializedSize() + sw_enforced.SerializedSize();
Shawn Willden0f906ec2015-06-20 09:16:30 -060054 derivation_data->reset(new (std::nothrow) uint8_t[*derivation_data_length]);
Shawn Willden32873522020-12-14 22:29:46 -070055 if (!derivation_data->get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0cb69422015-05-26 08:31:37 -060056
57 uint8_t* buf = derivation_data->get();
58 uint8_t* end = derivation_data->get() + *derivation_data_length;
59 buf = hidden.Serialize(buf, end);
60 buf = hw_enforced.Serialize(buf, end);
61 buf = sw_enforced.Serialize(buf, end);
62
63 return KM_ERROR_OK;
64}
65
66static keymaster_error_t InitializeKeyWrappingContext(const AuthorizationSet& hw_enforced,
67 const AuthorizationSet& sw_enforced,
68 const AuthorizationSet& hidden,
69 const KeymasterKeyBlob& master_key,
70 AeCtx* ctx) {
71 size_t derivation_data_length;
72 UniquePtr<uint8_t[]> derivation_data;
73 keymaster_error_t error = BuildDerivationData(hw_enforced, sw_enforced, hidden,
74 &derivation_data, &derivation_data_length);
Shawn Willden32873522020-12-14 22:29:46 -070075 if (error != KM_ERROR_OK) return error;
Shawn Willden0cb69422015-05-26 08:31:37 -060076
77 SHA256_CTX sha256_ctx;
Shawn Willden0f906ec2015-06-20 09:16:30 -060078 UniquePtr<uint8_t[]> hash_buf(new (std::nothrow) uint8_t[SHA256_DIGEST_LENGTH]);
Shawn Willden32873522020-12-14 22:29:46 -070079 if (!hash_buf.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0cb69422015-05-26 08:31:37 -060080 Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH);
Shawn Willden0f906ec2015-06-20 09:16:30 -060081 UniquePtr<uint8_t[]> derived_key(new (std::nothrow) uint8_t[AES_BLOCK_SIZE]);
Shawn Willden32873522020-12-14 22:29:46 -070082 if (!derived_key.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0cb69422015-05-26 08:31:37 -060083 Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE);
84
85 if (!ctx->get() || !hash_buf.get() || !derived_key.get())
86 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
87
88 // Hash derivation data.
89 Eraser sha256_ctx_eraser(sha256_ctx);
90 SHA256_Init(&sha256_ctx);
91 SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length);
92 SHA256_Final(hash_buf.get(), &sha256_ctx);
93
94 // Encrypt hash with master key to build derived key.
95 AES_KEY aes_key;
96 Eraser aes_key_eraser(AES_KEY);
97 if (0 !=
98 AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key))
99 return TranslateLastOpenSslError();
100
101 AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key);
102
103 // Set up AES OCB context using derived key.
104 if (ae_init(ctx->get(), derived_key.get(), AES_BLOCK_SIZE /* key length */, OCB_NONCE_LENGTH,
105 OCB_TAG_LENGTH) != AE_SUCCESS) {
106 memset_s(ctx->get(), 0, ae_ctx_sizeof());
107 return KM_ERROR_UNKNOWN_ERROR;
108 }
109
110 return KM_ERROR_OK;
111}
112
113keymaster_error_t OcbEncryptKey(const AuthorizationSet& hw_enforced,
114 const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden,
115 const KeymasterKeyBlob& master_key,
116 const KeymasterKeyBlob& plaintext, const Buffer& nonce,
117 KeymasterKeyBlob* ciphertext, Buffer* tag) {
118 assert(ciphertext && tag);
119
Shawn Willden32873522020-12-14 22:29:46 -0700120 if (nonce.available_read() != OCB_NONCE_LENGTH) return KM_ERROR_INVALID_ARGUMENT;
Shawn Willden0cb69422015-05-26 08:31:37 -0600121
122 AeCtx ctx;
Shawn Willden32873522020-12-14 22:29:46 -0700123 if (!ctx.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0f906ec2015-06-20 09:16:30 -0600124
Shawn Willden0cb69422015-05-26 08:31:37 -0600125 keymaster_error_t error =
126 InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx);
Shawn Willden32873522020-12-14 22:29:46 -0700127 if (error != KM_ERROR_OK) return error;
Shawn Willden0cb69422015-05-26 08:31:37 -0600128
Shawn Willden32873522020-12-14 22:29:46 -0700129 if (!ciphertext->Reset(plaintext.key_material_size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0cb69422015-05-26 08:31:37 -0600130
131 int ae_err = ae_encrypt(ctx.get(), nonce.peek_read(), plaintext.key_material,
Yi Kong3712b272018-07-30 15:53:23 -0700132 plaintext.key_material_size, nullptr /* additional data */,
Shawn Willden0cb69422015-05-26 08:31:37 -0600133 0 /* additional data length */, ciphertext->writable_data(),
134 tag->peek_write(), 1 /* final */);
135 if (ae_err < 0) {
136 LOG_E("Error %d while encrypting key", ae_err);
137 return KM_ERROR_UNKNOWN_ERROR;
138 }
Shawn Willden32873522020-12-14 22:29:46 -0700139 if (!tag->advance_write(OCB_TAG_LENGTH)) return KM_ERROR_UNKNOWN_ERROR;
Shawn Willden0cb69422015-05-26 08:31:37 -0600140 assert(ae_err == static_cast<int>(plaintext.key_material_size));
141 return KM_ERROR_OK;
142}
143
144keymaster_error_t OcbDecryptKey(const AuthorizationSet& hw_enforced,
145 const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden,
146 const KeymasterKeyBlob& master_key,
147 const KeymasterKeyBlob& ciphertext, const Buffer& nonce,
148 const Buffer& tag, KeymasterKeyBlob* plaintext) {
149 assert(plaintext);
150
151 if (nonce.available_read() != OCB_NONCE_LENGTH || tag.available_read() != OCB_TAG_LENGTH)
152 return KM_ERROR_INVALID_ARGUMENT;
153
154 AeCtx ctx;
Shawn Willden32873522020-12-14 22:29:46 -0700155 if (!ctx.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0f906ec2015-06-20 09:16:30 -0600156
Shawn Willden0cb69422015-05-26 08:31:37 -0600157 keymaster_error_t error =
158 InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx);
Shawn Willden32873522020-12-14 22:29:46 -0700159 if (error != KM_ERROR_OK) return error;
Shawn Willden0cb69422015-05-26 08:31:37 -0600160
Shawn Willden32873522020-12-14 22:29:46 -0700161 if (!plaintext->Reset(ciphertext.key_material_size)) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0cb69422015-05-26 08:31:37 -0600162
163 int ae_err = ae_decrypt(ctx.get(), nonce.peek_read(), ciphertext.key_material,
Yi Kong3712b272018-07-30 15:53:23 -0700164 ciphertext.key_material_size, nullptr /* additional data */,
Shawn Willden0cb69422015-05-26 08:31:37 -0600165 0 /* additional data length */, plaintext->writable_data(),
166 tag.peek_read(), 1 /* final */);
167 if (ae_err == AE_INVALID) {
168 // Authentication failed! Decryption probably succeeded(ish), but we don't want to return
169 // any data when the authentication fails, so clear it.
170 plaintext->Clear();
171 LOG_E("Failed to validate authentication tag during key decryption", 0);
172 return KM_ERROR_INVALID_KEY_BLOB;
173 } else if (ae_err < 0) {
174 LOG_E("Failed to decrypt key, error: %d", ae_err);
175 return KM_ERROR_UNKNOWN_ERROR;
176 }
177 assert(ae_err == static_cast<int>(ciphertext.key_material_size));
178 return KM_ERROR_OK;
179}
180
181} // namespace keymaster