1 /*
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 <keymaster/km_openssl/symmetric_key.h>
18
19 #include <utility>
20
21 #include <assert.h>
22 #include <inttypes.h>
23
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
26
27 #include <keymaster/android_keymaster_utils.h>
28 #include <keymaster/keymaster_context.h>
29 #include <keymaster/km_openssl/aes_key.h>
30 #include <keymaster/km_openssl/hmac_key.h>
31 #include <keymaster/km_openssl/openssl_err.h>
32 #include <keymaster/logger.h>
33
34 namespace keymaster {
35
GenerateKey(const AuthorizationSet & key_description,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const36 keymaster_error_t SymmetricKeyFactory::GenerateKey(const AuthorizationSet& key_description,
37 UniquePtr<Key> /* attest_key */,
38 const KeymasterBlob& /* issuer_subject */,
39 KeymasterKeyBlob* key_blob,
40 AuthorizationSet* hw_enforced,
41 AuthorizationSet* sw_enforced,
42 CertificateChain* /* cert_chain */) const {
43 if (!key_blob || !hw_enforced || !sw_enforced) return KM_ERROR_OUTPUT_PARAMETER_NULL;
44
45 uint32_t key_size_bits;
46 if (!key_description.GetTagValue(TAG_KEY_SIZE, &key_size_bits) ||
47 !key_size_supported(key_size_bits))
48 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
49
50 keymaster_error_t error = validate_algorithm_specific_new_key_params(key_description);
51 if (error != KM_ERROR_OK) return error;
52
53 size_t key_data_size = key_size_bytes(key_size_bits);
54 KeymasterKeyBlob key_material(key_data_size);
55 if (!key_material.key_material) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
56
57 error = random_source_.GenerateRandom(key_material.writable_data(), key_data_size);
58 if (error != KM_ERROR_OK) {
59 LOG_E("Error generating %d bit symmetric key", key_size_bits);
60 return error;
61 }
62
63 return blob_maker_.CreateKeyBlob(key_description, KM_ORIGIN_GENERATED, key_material, key_blob,
64 hw_enforced, sw_enforced);
65 }
66
ImportKey(const AuthorizationSet & key_description,keymaster_key_format_t input_key_material_format,const KeymasterKeyBlob & input_key_material,UniquePtr<Key>,const KeymasterBlob &,KeymasterKeyBlob * output_key_blob,AuthorizationSet * hw_enforced,AuthorizationSet * sw_enforced,CertificateChain *) const67 keymaster_error_t SymmetricKeyFactory::ImportKey(const AuthorizationSet& key_description, //
68 keymaster_key_format_t input_key_material_format,
69 const KeymasterKeyBlob& input_key_material, //
70 UniquePtr<Key> /* attest_key */,
71 const KeymasterBlob& /* issuer_subject */,
72 KeymasterKeyBlob* output_key_blob,
73 AuthorizationSet* hw_enforced,
74 AuthorizationSet* sw_enforced,
75 CertificateChain* /* cert_chain */) const {
76 if (!output_key_blob || !hw_enforced || !sw_enforced) return KM_ERROR_OUTPUT_PARAMETER_NULL;
77
78 AuthorizationSet authorizations(key_description);
79
80 uint32_t key_bits;
81 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_bits)) {
82 // Determine key size if not provided.
83 key_bits = key_size_bits(input_key_material.key_material_size);
84 authorizations.push_back(TAG_KEY_SIZE, key_bits);
85 }
86
87 keymaster_error_t error = validate_algorithm_specific_new_key_params(key_description);
88 if (error != KM_ERROR_OK) return error;
89 if (!key_size_supported(key_bits)) return KM_ERROR_UNSUPPORTED_KEY_SIZE;
90 if (input_key_material_format != KM_KEY_FORMAT_RAW) return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
91
92 if (key_bits != key_size_bits(input_key_material.key_material_size)) {
93 LOG_E("Expected %" PRIu32 "-bit key data but got %zu-bit key", key_bits,
94 key_size_bits(input_key_material.key_material_size));
95 return KM_ERROR_INVALID_KEY_BLOB;
96 }
97
98 return blob_maker_.CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
99 output_key_blob, hw_enforced, sw_enforced);
100 }
101
102 static const keymaster_key_format_t supported_import_formats[] = {KM_KEY_FORMAT_RAW};
103 const keymaster_key_format_t*
SupportedImportFormats(size_t * format_count) const104 SymmetricKeyFactory::SupportedImportFormats(size_t* format_count) const {
105 *format_count = array_length(supported_import_formats);
106 return supported_import_formats;
107 }
108
SymmetricKey(KeymasterKeyBlob && key_material,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,const KeyFactory * key_factory)109 SymmetricKey::SymmetricKey(KeymasterKeyBlob&& key_material, AuthorizationSet&& hw_enforced,
110 AuthorizationSet&& sw_enforced, const KeyFactory* key_factory)
111 : Key(std::move(hw_enforced), std::move(sw_enforced), key_factory) {
112 key_material_ = std::move(key_material);
113 }
114
~SymmetricKey()115 SymmetricKey::~SymmetricKey() {}
116
117 } // namespace keymaster
118