xref: /aosp_15_r20/system/keymaster/android_keymaster/operation_table.cpp (revision 789431f29546679ab5188a97751fb38e3018d44d)
1 /*
2  * Copyright (C) 2015 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 <utility>
18 
19 #include <keymaster/android_keymaster_utils.h>
20 #include <keymaster/operation.h>
21 #include <keymaster/operation_table.h>
22 
23 namespace keymaster {
24 
Add(OperationPtr && operation)25 keymaster_error_t OperationTable::Add(OperationPtr&& operation) {
26     if (!table_) {
27         table_.reset(new (std::nothrow) OperationPtr[table_size_]);
28         if (!table_) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
29     }
30     for (size_t i = 0; i < table_size_; ++i) {
31         if (!table_[i]) {
32             table_[i] = std::move(operation);
33             return KM_ERROR_OK;
34         }
35     }
36     return KM_ERROR_TOO_MANY_OPERATIONS;
37 }
38 
Find(keymaster_operation_handle_t op_handle)39 Operation* OperationTable::Find(keymaster_operation_handle_t op_handle) {
40     if (op_handle == 0) return nullptr;
41 
42     if (!table_.get()) return nullptr;
43 
44     for (size_t i = 0; i < table_size_; ++i) {
45         if (table_[i] && table_[i]->operation_handle() == op_handle) return table_[i].get();
46     }
47     return nullptr;
48 }
49 
Delete(keymaster_operation_handle_t op_handle)50 bool OperationTable::Delete(keymaster_operation_handle_t op_handle) {
51     if (!table_.get()) return false;
52 
53     for (size_t i = 0; i < table_size_; ++i) {
54         if (table_[i] && table_[i]->operation_handle() == op_handle) {
55             table_[i].reset();
56             return true;
57         }
58     }
59     return false;
60 }
61 
62 }  // namespace keymaster
63