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