1 /*
2  * Copyright 2018 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/triple_des_key.h>
18 
19 #include <assert.h>
20 
21 #include <keymaster/new.h>
22 
23 #include <openssl/err.h>
24 #include <openssl/rand.h>
25 
26 #include "triple_des_operation.h"
27 
28 namespace keymaster {
29 
30 static TripleDesOperationFactory encrypt_factory(KM_PURPOSE_ENCRYPT);
31 static TripleDesOperationFactory decrypt_factory(KM_PURPOSE_DECRYPT);
32 
GetOperationFactory(keymaster_purpose_t purpose) const33 OperationFactory* TripleDesKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
34     switch (purpose) {
35     case KM_PURPOSE_ENCRYPT:
36         return &encrypt_factory;
37     case KM_PURPOSE_DECRYPT:
38         return &decrypt_factory;
39     default:
40         return nullptr;
41     }
42 }
43 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet &,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const44 keymaster_error_t TripleDesKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
45                                                const AuthorizationSet& /* additional_params */,
46                                                AuthorizationSet&& hw_enforced,
47                                                AuthorizationSet&& sw_enforced,
48                                                UniquePtr<Key>* key) const {
49     if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
50 
51     keymaster_error_t error = KM_ERROR_OK;
52     key->reset(new (std::nothrow)
53                    TripleDesKey(move(key_material), move(hw_enforced), move(sw_enforced), this));
54     if (!key->get()) error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
55     return error;
56 }
57 
validate_algorithm_specific_new_key_params(const AuthorizationSet & key_description) const58 keymaster_error_t TripleDesKeyFactory::validate_algorithm_specific_new_key_params(
59     const AuthorizationSet& key_description) const {
60     if (key_description.Contains(TAG_MIN_MAC_LENGTH)) return KM_ERROR_INVALID_TAG;
61     return KM_ERROR_OK;
62 }
63 
64 }  // namespace keymaster
65