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 "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/logger.h>
26#include <keymaster/keymaster_context.h>
27
28#include "aes_key.h"
29#include "hmac_key.h"
30#include "openssl_err.h"
31
32namespace keymaster {
33
34keymaster_error_t SymmetricKeyFactory::GenerateKey(const AuthorizationSet& key_description,
35                                                   KeymasterKeyBlob* key_blob,
36                                                   AuthorizationSet* hw_enforced,
37                                                   AuthorizationSet* sw_enforced) const {
38    if (!key_blob || !hw_enforced || !sw_enforced)
39        return KM_ERROR_OUTPUT_PARAMETER_NULL;
40
41    uint32_t key_size_bits;
42    if (!key_description.GetTagValue(TAG_KEY_SIZE, &key_size_bits) ||
43        !key_size_supported(key_size_bits))
44        return KM_ERROR_UNSUPPORTED_KEY_SIZE;
45
46    keymaster_error_t error = validate_algorithm_specific_new_key_params(key_description);
47    if (error != KM_ERROR_OK)
48        return error;
49
50    size_t key_data_size = key_size_bits / 8;
51    KeymasterKeyBlob key_material(key_data_size);
52    if (!key_material.key_material)
53        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
54
55    error = context_->GenerateRandom(key_material.writable_data(), key_data_size);
56    if (error != KM_ERROR_OK) {
57        LOG_E("Error generating %d bit symmetric key", key_size_bits);
58        return error;
59    }
60
61    return context_->CreateKeyBlob(key_description, KM_ORIGIN_GENERATED, key_material, key_blob,
62                                   hw_enforced, sw_enforced);
63}
64
65keymaster_error_t SymmetricKeyFactory::ImportKey(const AuthorizationSet& key_description,
66                                                 keymaster_key_format_t input_key_material_format,
67                                                 const KeymasterKeyBlob& input_key_material,
68                                                 KeymasterKeyBlob* output_key_blob,
69                                                 AuthorizationSet* hw_enforced,
70                                                 AuthorizationSet* sw_enforced) const {
71    if (!output_key_blob || !hw_enforced || !sw_enforced)
72        return KM_ERROR_OUTPUT_PARAMETER_NULL;
73
74    AuthorizationSet authorizations(key_description);
75
76    uint32_t key_size_bits;
77    if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size_bits)) {
78        // Default key size if not specified.
79        key_size_bits = input_key_material.key_material_size * 8;
80        authorizations.push_back(TAG_KEY_SIZE, key_size_bits);
81    }
82
83    keymaster_error_t error = validate_algorithm_specific_new_key_params(key_description);
84    if (error != KM_ERROR_OK)
85        return error;
86
87    if (!key_size_supported(key_size_bits))
88        return KM_ERROR_UNSUPPORTED_KEY_SIZE;
89
90    if (input_key_material_format != KM_KEY_FORMAT_RAW)
91        return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
92
93    if (key_size_bits != input_key_material.key_material_size * 8) {
94        LOG_E("Expected %d-bit key data but got %d bits", key_size_bits,
95              input_key_material.key_material_size * 8);
96        return KM_ERROR_INVALID_KEY_BLOB;
97    }
98
99    return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
100                                   output_key_blob, hw_enforced, sw_enforced);
101}
102
103static const keymaster_key_format_t supported_import_formats[] = {KM_KEY_FORMAT_RAW};
104const keymaster_key_format_t*
105SymmetricKeyFactory::SupportedImportFormats(size_t* format_count) const {
106    *format_count = array_length(supported_import_formats);
107    return supported_import_formats;
108}
109
110SymmetricKey::SymmetricKey(const KeymasterKeyBlob& key_material,
111                           const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced,
112                           keymaster_error_t* error)
113    : Key(hw_enforced, sw_enforced, error) {
114    if (*error != KM_ERROR_OK)
115        return;
116
117    uint8_t* tmp = dup_buffer(key_material.key_material, key_material.key_material_size);
118    if (tmp) {
119        key_data_.reset(tmp);
120        key_data_size_ = key_material.key_material_size;
121        *error = KM_ERROR_OK;
122    } else {
123        *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
124    }
125}
126
127SymmetricKey::~SymmetricKey() {
128    memset_s(key_data_.get(), 0, key_data_size_);
129}
130
131keymaster_error_t SymmetricKey::key_material(UniquePtr<uint8_t[]>* key_material,
132                                             size_t* size) const {
133    *size = key_data_size_;
134    key_material->reset(new (std::nothrow) uint8_t[*size]);
135    if (!key_material->get())
136        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
137    memcpy(key_material->get(), key_data_.get(), *size);
138    return KM_ERROR_OK;
139}
140
141}  // namespace keymaster
142