1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <lk/compiler.h>
20 #include <stdbool.h>
21 #include <sys/types.h>
22 
23 #include <interface/hwaes/hwaes.h>
24 #include <trusty_ipc.h>
25 
26 __BEGIN_CDECLS
27 
28 typedef handle_t hwaes_session_t;
29 
30 /**
31  * hwaes_open() - Opens a trusty hwaes session.
32  * @session: pointer to the returned session handle.
33  *
34  * Return: NO_ERROR on success, error code less than 0 on error.
35  */
36 int hwaes_open(hwaes_session_t* session);
37 
38 /**
39  * struct hwcrypt_shm_hd - Handle descriptor for a shared memory.
40  * @handle: handle to the shared memory.
41  * @base:   base address (on client virtual address space) of the shared memory.
42  * @size:   size of the shared memory region.
43  */
44 struct hwcrypt_shm_hd {
45     handle_t handle;
46     const void* base;
47     size_t size;
48 };
49 
50 /**
51  * struct hwcrypt_arg_in - Input argument struct for hwcrypt.
52  * @data_ptr:   pointer to the argument data.
53  * @len:        length of the argument data.
54  * @shm_hd_ptr: pointer to the shared memory descriptor handler.
55  *              It is only set when the argument is stored on shared memory.
56  *              It is an optional field, which shall be null if not used.
57  *
58  * If shared memory is not used, the data will be copied into TIPC message
59  * and sent to the server.
60  */
61 struct hwcrypt_arg_in {
62     const void* data_ptr;
63     size_t len;
64     struct hwcrypt_shm_hd* shm_hd_ptr;
65 };
66 
67 /**
68  * struct hwcrypt_arg_out - Output argument struct for hwcrypt.
69  * @data_ptr:   pointer to the argument data.
70  * @len:        length of the argument data.
71  * @shm_hd_ptr: pointer to the shared memory descriptor handler.
72  *              It is only set when the argument is stored on shared memory.
73  *              It is an optional field, which shall be null if not used.
74  */
75 struct hwcrypt_arg_out {
76     void* data_ptr;
77     size_t len;
78     struct hwcrypt_shm_hd* shm_hd_ptr;
79 };
80 
81 /**
82  * struct hwcrypt_args - Arguments struct for hwcrypt.
83  * @key:      key of the crypt operation.
84  * @iv:       iv of the crypt operation.
85  * @aad:      aad of the crypt operation.
86  * @text_in:  input text of the crypt operation.
87  * @tag_in:   input tag of the crypt operation.
88  *            It is an optional field.
89  * @text_out: output text of the crypt operation.
90  * @tag_out:  output tag of the crypt operation.
91  *            It is an optional field.
92  * @padding:  the type of padding.
93  * @key_type: the type of key.
94  * @mode:     the mode of the crypt operation.
95  */
96 struct hwcrypt_args {
97     struct hwcrypt_arg_in key;
98     struct hwcrypt_arg_in iv;
99     struct hwcrypt_arg_in aad;
100     struct hwcrypt_arg_in text_in;
101     struct hwcrypt_arg_in tag_in;
102     struct hwcrypt_arg_out text_out;
103     struct hwcrypt_arg_out tag_out;
104     uint32_t key_type;
105     uint32_t padding;
106     uint32_t mode;
107 };
108 
109 /**
110  * hwaes_encrypt() - Perform AES encryption.
111  * @session: session handle retrieved from hwaes_open.
112  * @args:    arguments for the AES encryption.
113  *
114  * Return: NO_ERROR on success, error code less than 0 on error.
115  *
116  */
117 int hwaes_encrypt(hwaes_session_t session, const struct hwcrypt_args* args);
118 
119 /**
120  * hwaes_decrypt() - Perform AES decryption.
121  * @session: session handle retrieved from hwaes_open.
122  * @args:    arguments for the AES decryption.
123  *
124  * Return: NO_ERROR on success, error code less than 0 on error.
125  *
126  */
127 int hwaes_decrypt(hwaes_session_t session, const struct hwcrypt_args* args);
128 
129 /**
130  * hwaes_close() - Closes the session.
131  */
132 void hwaes_close(hwaes_session_t session);
133 
134 __END_CDECLS
135