blob: 171d07e32b7b76293c5434f1fe2a84536ee0ef1a [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
17#include "ocb_utils.h"
18
19#include <assert.h>
20
21#include <openssl/aes.h>
22#include <openssl/sha.h>
23
24#include <hardware/keymaster_defs.h>
25#include <keymaster/authorization_set.h>
26#include <keymaster/android_keymaster_utils.h>
27
28#include "openssl_err.h"
29
30namespace keymaster {
31
32class AeCtx {
33 public:
34 AeCtx() : ctx_(ae_allocate(NULL)) {}
35 ~AeCtx() {
36 ae_clear(ctx_);
37 ae_free(ctx_);
38 }
39
40 ae_ctx* get() { return ctx_; }
41
42 private:
43 ae_ctx* ctx_;
44};
45
46static keymaster_error_t BuildDerivationData(const AuthorizationSet& hw_enforced,
47 const AuthorizationSet& sw_enforced,
48 const AuthorizationSet& hidden,
49 UniquePtr<uint8_t[]>* derivation_data,
50 size_t* derivation_data_length) {
51 *derivation_data_length =
52 hidden.SerializedSize() + hw_enforced.SerializedSize() + sw_enforced.SerializedSize();
53 derivation_data->reset(new uint8_t[*derivation_data_length]);
54 if (!derivation_data->get())
55 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
56
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);
75 if (error != KM_ERROR_OK)
76 return error;
77
78 SHA256_CTX sha256_ctx;
79 UniquePtr<uint8_t[]> hash_buf(new uint8_t[SHA256_DIGEST_LENGTH]);
80 Eraser hash_eraser(hash_buf.get(), SHA256_DIGEST_LENGTH);
81 UniquePtr<uint8_t[]> derived_key(new uint8_t[AES_BLOCK_SIZE]);
82 Eraser derived_key_eraser(derived_key.get(), AES_BLOCK_SIZE);
83
84 if (!ctx->get() || !hash_buf.get() || !derived_key.get())
85 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
86
87 // Hash derivation data.
88 Eraser sha256_ctx_eraser(sha256_ctx);
89 SHA256_Init(&sha256_ctx);
90 SHA256_Update(&sha256_ctx, derivation_data.get(), derivation_data_length);
91 SHA256_Final(hash_buf.get(), &sha256_ctx);
92
93 // Encrypt hash with master key to build derived key.
94 AES_KEY aes_key;
95 Eraser aes_key_eraser(AES_KEY);
96 if (0 !=
97 AES_set_encrypt_key(master_key.key_material, master_key.key_material_size * 8, &aes_key))
98 return TranslateLastOpenSslError();
99
100 AES_encrypt(hash_buf.get(), derived_key.get(), &aes_key);
101
102 // Set up AES OCB context using derived key.
103 if (ae_init(ctx->get(), derived_key.get(), AES_BLOCK_SIZE /* key length */, OCB_NONCE_LENGTH,
104 OCB_TAG_LENGTH) != AE_SUCCESS) {
105 memset_s(ctx->get(), 0, ae_ctx_sizeof());
106 return KM_ERROR_UNKNOWN_ERROR;
107 }
108
109 return KM_ERROR_OK;
110}
111
112keymaster_error_t OcbEncryptKey(const AuthorizationSet& hw_enforced,
113 const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden,
114 const KeymasterKeyBlob& master_key,
115 const KeymasterKeyBlob& plaintext, const Buffer& nonce,
116 KeymasterKeyBlob* ciphertext, Buffer* tag) {
117 assert(ciphertext && tag);
118
119 if (nonce.available_read() != OCB_NONCE_LENGTH)
120 return KM_ERROR_INVALID_ARGUMENT;
121
122 AeCtx ctx;
123 keymaster_error_t error =
124 InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx);
125 if (error != KM_ERROR_OK)
126 return error;
127
128 ciphertext->Reset(plaintext.key_material_size);
129 if (!ciphertext->key_material)
130 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
131
132 int ae_err = ae_encrypt(ctx.get(), nonce.peek_read(), plaintext.key_material,
133 plaintext.key_material_size, NULL /* additional data */,
134 0 /* additional data length */, ciphertext->writable_data(),
135 tag->peek_write(), 1 /* final */);
136 if (ae_err < 0) {
137 LOG_E("Error %d while encrypting key", ae_err);
138 return KM_ERROR_UNKNOWN_ERROR;
139 }
140 tag->advance_write(OCB_TAG_LENGTH);
141 assert(ae_err == static_cast<int>(plaintext.key_material_size));
142 return KM_ERROR_OK;
143}
144
145keymaster_error_t OcbDecryptKey(const AuthorizationSet& hw_enforced,
146 const AuthorizationSet& sw_enforced, const AuthorizationSet& hidden,
147 const KeymasterKeyBlob& master_key,
148 const KeymasterKeyBlob& ciphertext, const Buffer& nonce,
149 const Buffer& tag, KeymasterKeyBlob* plaintext) {
150 assert(plaintext);
151
152 if (nonce.available_read() != OCB_NONCE_LENGTH || tag.available_read() != OCB_TAG_LENGTH)
153 return KM_ERROR_INVALID_ARGUMENT;
154
155 AeCtx ctx;
156 keymaster_error_t error =
157 InitializeKeyWrappingContext(hw_enforced, sw_enforced, hidden, master_key, &ctx);
158 if (error != KM_ERROR_OK)
159 return error;
160
161 plaintext->Reset(ciphertext.key_material_size);
162 if (!plaintext->key_material)
163 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
164
165 int ae_err = ae_decrypt(ctx.get(), nonce.peek_read(), ciphertext.key_material,
166 ciphertext.key_material_size, NULL /* additional data */,
167 0 /* additional data length */, plaintext->writable_data(),
168 tag.peek_read(), 1 /* final */);
169 if (ae_err == AE_INVALID) {
170 // Authentication failed! Decryption probably succeeded(ish), but we don't want to return
171 // any data when the authentication fails, so clear it.
172 plaintext->Clear();
173 LOG_E("Failed to validate authentication tag during key decryption", 0);
174 return KM_ERROR_INVALID_KEY_BLOB;
175 } else if (ae_err < 0) {
176 LOG_E("Failed to decrypt key, error: %d", ae_err);
177 return KM_ERROR_UNKNOWN_ERROR;
178 }
179 assert(ae_err == static_cast<int>(ciphertext.key_material_size));
180 return KM_ERROR_OK;
181}
182
183} // namespace keymaster