summaryrefslogtreecommitdiff
path: root/legacy_support/keymaster_passthrough_engine.cpp
blob: d966d716342c791f39154e05b9220b020f210f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
**
** Copyright 2017, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include "keymaster_passthrough_operation.h"
#include <keymaster/legacy_support/keymaster_passthrough_engine.h>
#include <keymaster/legacy_support/keymaster_passthrough_key.h>

#include <hardware/keymaster1.h>
#include <hardware/keymaster2.h>

#include <assert.h>

#include <algorithm>
#include <memory>
#include <type_traits>

#define LOG_TAG "Keymaster2Engine"
#include <android/log.h>

using std::unique_ptr;

namespace keymaster {

template <typename KeymasterDeviceType>
class TKeymasterPassthroughEngine : public KeymasterPassthroughEngine {
    using opfactory_t = KeymasterPassthroughOperationFactory<KeymasterDeviceType>;

  public:
    /**
     * The engine takes ownership of the device, and will close it during destruction.
     */
    explicit TKeymasterPassthroughEngine(const KeymasterDeviceType* km_device)
        : km_device_(km_device) {
        rsa_encrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_ENCRYPT, km_device_));
        rsa_decrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_DECRYPT, km_device_));
        rsa_sign_op_factory_.reset(new (std::nothrow)
                                       opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_SIGN, km_device_));
        rsa_verify_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_RSA, KM_PURPOSE_VERIFY, km_device_));
        ec_encrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_ENCRYPT, km_device_));
        ec_decrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_DECRYPT, km_device_));
        ec_sign_op_factory_.reset(new (std::nothrow)
                                      opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_SIGN, km_device_));
        ec_verify_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY, km_device_));
        ec_derive_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_EC, KM_PURPOSE_DERIVE_KEY, km_device_));
        aes_encrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_AES, KM_PURPOSE_ENCRYPT, km_device_));
        aes_decrypt_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_AES, KM_PURPOSE_DECRYPT, km_device_));
        triple_des_encrypt_op_factory_.reset(new (std::nothrow) opfactory_t(
            KM_ALGORITHM_TRIPLE_DES, KM_PURPOSE_ENCRYPT, km_device_));
        triple_des_decrypt_op_factory_.reset(new (std::nothrow) opfactory_t(
            KM_ALGORITHM_TRIPLE_DES, KM_PURPOSE_DECRYPT, km_device_));
        hmac_sign_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_HMAC, KM_PURPOSE_SIGN, km_device_));
        hmac_verify_op_factory_.reset(
            new (std::nothrow) opfactory_t(KM_ALGORITHM_HMAC, KM_PURPOSE_VERIFY, km_device_));
    }
    virtual ~TKeymasterPassthroughEngine() {
        // QUIRK: we only take ownership if this is a KM2 device.
        //        For KM1 the Keymaster1Engine takes ownership
        if (std::is_same<KeymasterDeviceType, keymaster2_device_t>::value)
            km_device_->common.close(
                reinterpret_cast<hw_device_t*>(const_cast<KeymasterDeviceType*>(km_device_)));
    }

    keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
                                  KeymasterKeyBlob* key_material, AuthorizationSet* hw_enforced,
                                  AuthorizationSet* sw_enforced) const override;

    keymaster_error_t ImportKey(const AuthorizationSet& key_description,
                                keymaster_key_format_t input_key_material_format,
                                const KeymasterKeyBlob& input_key_material,
                                KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
                                AuthorizationSet* sw_enforced) const override;
    keymaster_error_t ExportKey(keymaster_key_format_t format, const KeymasterKeyBlob& blob,
                                const KeymasterBlob& client_id, const KeymasterBlob& app_data,
                                KeymasterBlob* export_data) const override {
        keymaster_blob_t my_export_data = {};
        keymaster_error_t error = km_device_->export_key(km_device_, format, &blob, &client_id,
                                                         &app_data, &my_export_data);
        if (error != KM_ERROR_OK) return error;
        *export_data = KeymasterBlob(my_export_data.data, my_export_data.data_length);
        free(const_cast<uint8_t*>(my_export_data.data));
        if (export_data->data == nullptr) {
            return KM_ERROR_MEMORY_ALLOCATION_FAILED;
        }
        return error;
    }
    keymaster_error_t DeleteKey(const KeymasterKeyBlob& blob) const override {
        return km_device_->delete_key(km_device_, &blob);
    }
    keymaster_error_t DeleteAllKeys() const override {
        return km_device_->delete_all_keys(km_device_);
    }
    OperationFactory* GetOperationFactory(keymaster_purpose_t purpose,
                                          keymaster_algorithm_t algorithm) const override {
        switch (algorithm) {
        case KM_ALGORITHM_RSA:
            switch (purpose) {
            case KM_PURPOSE_ENCRYPT:
                return rsa_encrypt_op_factory_.get();
            case KM_PURPOSE_DECRYPT:
                return rsa_decrypt_op_factory_.get();
            case KM_PURPOSE_SIGN:
                return rsa_sign_op_factory_.get();
            case KM_PURPOSE_VERIFY:
                return rsa_verify_op_factory_.get();
            default:
                return nullptr;
            }
        case KM_ALGORITHM_EC:
            switch (purpose) {
            case KM_PURPOSE_ENCRYPT:
                return ec_encrypt_op_factory_.get();
            case KM_PURPOSE_DECRYPT:
                return ec_decrypt_op_factory_.get();
            case KM_PURPOSE_SIGN:
                return ec_sign_op_factory_.get();
            case KM_PURPOSE_VERIFY:
                return ec_verify_op_factory_.get();
            case KM_PURPOSE_DERIVE_KEY:
                return ec_derive_op_factory_.get();
            default:
                return nullptr;
            }
        case KM_ALGORITHM_AES:
            switch (purpose) {
            case KM_PURPOSE_ENCRYPT:
                return aes_encrypt_op_factory_.get();
            case KM_PURPOSE_DECRYPT:
                return aes_decrypt_op_factory_.get();
            default:
                return nullptr;
            }
        case KM_ALGORITHM_TRIPLE_DES:
            switch (purpose) {
            case KM_PURPOSE_ENCRYPT:
                return triple_des_encrypt_op_factory_.get();
            case KM_PURPOSE_DECRYPT:
                return triple_des_decrypt_op_factory_.get();
            default:
                return nullptr;
            }
        case KM_ALGORITHM_HMAC:
            switch (purpose) {
            case KM_PURPOSE_SIGN:
                return hmac_sign_op_factory_.get();
            case KM_PURPOSE_VERIFY:
                return hmac_verify_op_factory_.get();
            default:
                return nullptr;
            }
        }
    }

    const KeymasterDeviceType* device() const { return km_device_; }

  private:
    TKeymasterPassthroughEngine(const KeymasterPassthroughEngine&) = delete;  // Uncopyable
    void operator=(const KeymasterPassthroughEngine&) = delete;               // Unassignable

    const KeymasterDeviceType* const km_device_;
    std::unique_ptr<opfactory_t> rsa_encrypt_op_factory_;
    std::unique_ptr<opfactory_t> rsa_decrypt_op_factory_;
    std::unique_ptr<opfactory_t> rsa_sign_op_factory_;
    std::unique_ptr<opfactory_t> rsa_verify_op_factory_;
    std::unique_ptr<opfactory_t> ec_encrypt_op_factory_;
    std::unique_ptr<opfactory_t> ec_decrypt_op_factory_;
    std::unique_ptr<opfactory_t> ec_sign_op_factory_;
    std::unique_ptr<opfactory_t> ec_verify_op_factory_;
    std::unique_ptr<opfactory_t> ec_derive_op_factory_;
    std::unique_ptr<opfactory_t> aes_encrypt_op_factory_;
    std::unique_ptr<opfactory_t> aes_decrypt_op_factory_;
    std::unique_ptr<opfactory_t> triple_des_encrypt_op_factory_;
    std::unique_ptr<opfactory_t> triple_des_decrypt_op_factory_;
    std::unique_ptr<opfactory_t> hmac_sign_op_factory_;
    std::unique_ptr<opfactory_t> hmac_verify_op_factory_;
};

static void ConvertCharacteristics(const keymaster_key_characteristics_t& characteristics,
                                   AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) {
    if (hw_enforced) hw_enforced->Reinitialize(characteristics.hw_enforced);
    if (sw_enforced) sw_enforced->Reinitialize(characteristics.sw_enforced);
}

template <>
keymaster_error_t TKeymasterPassthroughEngine<keymaster1_device_t>::GenerateKey(
    const AuthorizationSet& key_description, KeymasterKeyBlob* key_blob,
    AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
    assert(key_blob);

    keymaster_key_characteristics_t* characteristics = nullptr;
    keymaster_key_blob_t blob = {};
    keymaster_error_t error =
        km_device_->generate_key(km_device_, &key_description, &blob, &characteristics);
    if (error != KM_ERROR_OK) return error;
    unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
    key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
    key_blob->key_material_size = blob.key_material_size;

    ConvertCharacteristics(*characteristics, hw_enforced, sw_enforced);
    keymaster_free_characteristics(characteristics);
    free(characteristics);
    return error;
}
template <>
keymaster_error_t TKeymasterPassthroughEngine<keymaster2_device_t>::GenerateKey(
    const AuthorizationSet& key_description, KeymasterKeyBlob* key_blob,
    AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
    assert(key_blob);

    keymaster_key_characteristics_t characteristics = {};
    keymaster_key_blob_t blob = {};
    keymaster_error_t error =
        km_device_->generate_key(km_device_, &key_description, &blob, &characteristics);
    if (error != KM_ERROR_OK) return error;
    unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
    key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
    key_blob->key_material_size = blob.key_material_size;

    ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
    keymaster_free_characteristics(&characteristics);
    return error;
}

template <>
keymaster_error_t TKeymasterPassthroughEngine<keymaster1_device_t>::ImportKey(
    const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
    const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
    AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
    assert(output_key_blob);

    keymaster_key_characteristics_t* characteristics = {};
    const keymaster_blob_t input_key = {input_key_material.key_material,
                                        input_key_material.key_material_size};
    keymaster_key_blob_t blob = {};
    keymaster_error_t error =
        km_device_->import_key(km_device_, &key_description, input_key_material_format, &input_key,
                               &blob, &characteristics);
    if (error != KM_ERROR_OK) return error;
    unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));

    *output_key_blob = KeymasterKeyBlob(blob);

    ConvertCharacteristics(*characteristics, hw_enforced, sw_enforced);
    keymaster_free_characteristics(characteristics);
    free(characteristics);
    return error;
}

template <>
keymaster_error_t TKeymasterPassthroughEngine<keymaster2_device_t>::ImportKey(
    const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
    const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
    AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
    assert(output_key_blob);

    keymaster_key_characteristics_t characteristics = {};
    const keymaster_blob_t input_key = {input_key_material.key_material,
                                        input_key_material.key_material_size};
    keymaster_key_blob_t blob = {};
    keymaster_error_t error =
        km_device_->import_key(km_device_, &key_description, input_key_material_format, &input_key,
                               &blob, &characteristics);
    if (error != KM_ERROR_OK) return error;
    unique_ptr<uint8_t, Malloc_Delete> blob_deleter(const_cast<uint8_t*>(blob.key_material));
    // TODO why duplicate the blob if we have ownership here anyway?
    output_key_blob->key_material = dup_buffer(blob.key_material, blob.key_material_size);
    output_key_blob->key_material_size = blob.key_material_size;

    ConvertCharacteristics(characteristics, hw_enforced, sw_enforced);
    keymaster_free_characteristics(&characteristics);
    return error;
}

typedef UniquePtr<KeymasterPassthroughEngine> engine_ptr_t;

engine_ptr_t KeymasterPassthroughEngine::createInstance(const keymaster1_device_t* dev) {
    return engine_ptr_t(new (std::nothrow) TKeymasterPassthroughEngine<keymaster1_device_t>(dev));
}
engine_ptr_t KeymasterPassthroughEngine::createInstance(const keymaster2_device_t* dev) {
    return engine_ptr_t(new (std::nothrow) TKeymasterPassthroughEngine<keymaster2_device_t>(dev));
}

}  // namespace keymaster