blob: 2e814a69b818fe0132b667fe7fea0454986be776 [file] [log] [blame]
Shawn Willden0d560bf2014-12-15 17:44:02 -07001/*
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 "hmac_key.h"
18
Shawn Willden0f906ec2015-06-20 09:16:30 -060019#include <new>
20
Shawn Willden0d560bf2014-12-15 17:44:02 -070021#include <openssl/err.h>
22#include <openssl/rand.h>
23
24#include "hmac_operation.h"
25
26namespace keymaster {
27
Shawn Willden06298102015-05-25 23:12:48 -060028static HmacSignOperationFactory sign_factory;
29static HmacVerifyOperationFactory verify_factory;
30
31OperationFactory* HmacKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
32 switch (purpose) {
33 case KM_PURPOSE_SIGN:
34 return &sign_factory;
35 case KM_PURPOSE_VERIFY:
36 return &verify_factory;
37 default:
38 return nullptr;
39 }
40}
41
Shawn Willden0cb69422015-05-26 08:31:37 -060042keymaster_error_t HmacKeyFactory::LoadKey(const KeymasterKeyBlob& key_material,
43 const AuthorizationSet& hw_enforced,
44 const AuthorizationSet& sw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060045 UniquePtr<Key>* key) const {
Shawn Willden0cb69422015-05-26 08:31:37 -060046 if (!key)
47 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willdena278f612014-12-23 11:22:21 -070048
Shawn Willden0cb69422015-05-26 08:31:37 -060049 keymaster_error_t error;
Shawn Willden0f906ec2015-06-20 09:16:30 -060050 key->reset(new (std::nothrow) HmacKey(key_material, hw_enforced, sw_enforced, &error));
Shawn Willden0cb69422015-05-26 08:31:37 -060051 if (!key->get())
52 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
53 return error;
Shawn Willdenf9239632015-05-12 06:57:37 -060054}
Shawn Willdena278f612014-12-23 11:22:21 -070055
Shawn Willden0d560bf2014-12-15 17:44:02 -070056} // namespace keymaster