NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
aes_gcm_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef AES_GCM_COMMON_H_
27 #define AES_GCM_COMMON_H_
28 
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 
33 #include <doca_dev.h>
34 #include <doca_aes_gcm.h>
35 #include <doca_mmap.h>
36 #include <doca_error.h>
37 
38 #define USER_MAX_FILE_NAME 255 /* Max file name length */
39 #define MAX_FILE_NAME (USER_MAX_FILE_NAME + 1) /* Max file name string length */
40 
41 #define AES_GCM_KEY_128_SIZE_IN_BYTES 16 /* AES-GCM 128 bits key size */
42 #define AES_GCM_KEY_256_SIZE_IN_BYTES 32 /* AES-GCM 256 bits key size */
43 #define MAX_AES_GCM_KEY_SIZE AES_GCM_KEY_256_SIZE_IN_BYTES /* Max AES-GCM key size in bytes */
44 
45 #define AES_GCM_KEY_128_STR_SIZE (AES_GCM_KEY_128_SIZE_IN_BYTES * 2) /* AES-GCM 128 bits key string size */
46 #define AES_GCM_KEY_256_STR_SIZE (AES_GCM_KEY_256_SIZE_IN_BYTES * 2) /* AES-GCM 256 bits key string size */
47 #define MAX_AES_GCM_KEY_STR_SIZE (AES_GCM_KEY_256_STR_SIZE + 1) /* Max AES-GCM key string size */
48 
49 #define AES_GCM_AUTH_TAG_96_SIZE_IN_BYTES 12 /* AES-GCM 96 bits authentication tag size */
50 #define AES_GCM_AUTH_TAG_128_SIZE_IN_BYTES 16 /* AES-GCM 128 bits authentication tag size */
51 
52 #define MAX_AES_GCM_IV_LENGTH 12 /* Max IV length in bytes */
53 #define MAX_AES_GCM_IV_STR_LENGTH ((MAX_AES_GCM_IV_LENGTH * 2) + 1) /* Max IV string length */
54 
55 #define SLEEP_IN_NANOS (10 * 1000) /* Sample the task every 10 microseconds */
56 #define NUM_AES_GCM_TASKS (1) /* Number of AES-GCM tasks */
57 
58 /* AES-GCM modes */
60  AES_GCM_MODE_ENCRYPT, /* Encrypt mode */
61  AES_GCM_MODE_DECRYPT, /* Decrypt mode */
62 };
63 
64 /* Configuration struct */
65 struct aes_gcm_cfg {
66  char file_path[MAX_FILE_NAME]; /* File to encrypt/decrypt */
67  char output_path[MAX_FILE_NAME]; /* Output file */
68  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* Device PCI address */
69  uint8_t raw_key[MAX_AES_GCM_KEY_SIZE]; /* Raw key */
70  enum doca_aes_gcm_key_type raw_key_type; /* Raw key type */
71  uint8_t iv[MAX_AES_GCM_IV_LENGTH]; /* Initialization vector */
72  uint32_t iv_length; /* Initialization vector length */
73  uint32_t tag_size; /* Authentication tag size */
74  uint32_t aad_size; /* Additional authenticated data size */
75  enum aes_gcm_mode mode; /* AES-GCM task type */
76  int num_src_buf; /* Number of linked_list doca_buf element for the source buffer */
77  int num_dst_buf; /* Number of linked_list doca_buf element for the destination buffer */
78 };
79 
80 /* DOCA AES-GCM resources */
82  struct program_core_objects *state; /* DOCA program core objects */
83  struct doca_aes_gcm *aes_gcm; /* DOCA AES-GCM context */
84  size_t num_remaining_tasks; /* Number of remaining AES-GCM tasks */
85  enum aes_gcm_mode mode; /* AES-GCM mode - encrypt/decrypt */
86  bool run_pe_progress; /* Controls whether progress loop should run */
87 };
88 
89 /*
90  * Initialize AES-GCM parameters for the sample.
91  *
92  * @aes_gcm_cfg [in]: AES-GCM configuration struct
93  */
95 
96 /*
97  * Register the command line parameters for the sample.
98  *
99  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
100  */
102 
103 /*
104  * Allocate DOCA AES-GCM resources
105  *
106  * @pci_addr [in]: Device PCI address
107  * @max_bufs [in]: Maximum number of buffers for DOCA Inventory
108  * @resources [out]: DOCA AES-GCM resources to allocate
109  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
110  */
111 doca_error_t allocate_aes_gcm_resources(const char *pci_addr, uint32_t max_bufs, struct aes_gcm_resources *resources);
112 
113 /*
114  * Destroy DOCA AES-GCM resources
115  *
116  * @resources [in]: DOCA AES-GCM resources to destroy
117  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
118  */
120 
121 /*
122  * Submit AES-GCM encrypt task and wait for completion
123  *
124  * @resources [in]: DOCA AES-GCM resources
125  * @src_buf [in]: Source buffer
126  * @dst_buf [in]: Destination buffer
127  * @key [in]: DOCA AES-GCM key
128  * @iv [in]: Initialization vector
129  * @iv_length [in]: Initialization vector length in bytes
130  * @tag_size [in]: Authentication tag size in bytes
131  * @aad_size [in]: Additional authenticated data size in bytes
132  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
133  */
135  struct doca_buf *src_buf,
136  struct doca_buf *dst_buf,
137  struct doca_aes_gcm_key *key,
138  const uint8_t *iv,
139  uint32_t iv_length,
140  uint32_t tag_size,
141  uint32_t aad_size);
142 
143 /*
144  * Submit AES-GCM decrypt task and wait for completion
145  *
146  * @resources [in]: DOCA AES-GCM resources
147  * @src_buf [in]: Source buffer
148  * @dst_buf [in]: Destination buffer
149  * @key [in]: DOCA AES-GCM key
150  * @iv [in]: Initialization vector
151  * @iv_length [in]: Initialization vector length in bytes
152  * @tag_size [in]: Authentication tag size in bytes
153  * @aad_size [in]: Additional authenticated data size in bytes
154  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
155  */
157  struct doca_buf *src_buf,
158  struct doca_buf *dst_buf,
159  struct doca_aes_gcm_key *key,
160  const uint8_t *iv,
161  uint32_t iv_length,
162  uint32_t tag_size,
163  uint32_t aad_size);
164 
165 /*
166  * Check if given device is capable of executing a DOCA AES-GCM encrypt task.
167  *
168  * @devinfo [in]: The DOCA device information
169  * @return: DOCA_SUCCESS if the device supports DOCA AES-GCM encrypt task and DOCA_ERROR otherwise
170  */
171 doca_error_t aes_gcm_task_encrypt_is_supported(struct doca_devinfo *devinfo);
172 
173 /*
174  * Check if given device is capable of executing a DOCA AES-GCM decrypt task.
175  *
176  * @devinfo [in]: The DOCA device information
177  * @return: DOCA_SUCCESS if the device supports DOCA AES-GCM decrypt task and DOCA_ERROR otherwise
178  */
179 doca_error_t aes_gcm_task_decrypt_is_supported(struct doca_devinfo *devinfo);
180 
181 /*
182  * Encrypt task completed callback
183  *
184  * @encrypt_task [in]: Completed task
185  * @task_user_data [in]: doca_data from the task
186  * @ctx_user_data [in]: doca_data from the context
187  */
188 void encrypt_completed_callback(struct doca_aes_gcm_task_encrypt *encrypt_task,
189  union doca_data task_user_data,
190  union doca_data ctx_user_data);
191 
192 /*
193  * Encrypt task error callback
194  *
195  * @encrypt_task [in]: failed task
196  * @task_user_data [in]: doca_data from the task
197  * @ctx_user_data [in]: doca_data from the context
198  */
199 void encrypt_error_callback(struct doca_aes_gcm_task_encrypt *encrypt_task,
200  union doca_data task_user_data,
201  union doca_data ctx_user_data);
202 
203 /*
204  * Decrypt task completed callback
205  *
206  * @decrypt_task [in]: Completed task
207  * @task_user_data [in]: doca_data from the task
208  * @ctx_user_data [in]: doca_data from the context
209  */
210 void decrypt_completed_callback(struct doca_aes_gcm_task_decrypt *decrypt_task,
211  union doca_data task_user_data,
212  union doca_data ctx_user_data);
213 
214 /*
215  * Decrypt task error callback
216  *
217  * @decrypt_task [in]: failed task
218  * @task_user_data [in]: doca_data from the task
219  * @ctx_user_data [in]: doca_data from the context
220  */
221 void decrypt_error_callback(struct doca_aes_gcm_task_decrypt *decrypt_task,
222  union doca_data task_user_data,
223  union doca_data ctx_user_data);
224 
225 #endif /* AES-GCM_COMMON_H_ */
doca_error_t submit_aes_gcm_decrypt_task(struct aes_gcm_resources *resources, struct doca_buf *src_buf, struct doca_buf *dst_buf, struct doca_aes_gcm_key *key, const uint8_t *iv, uint32_t iv_length, uint32_t tag_size, uint32_t aad_size)
doca_error_t aes_gcm_task_decrypt_is_supported(struct doca_devinfo *devinfo)
doca_error_t submit_aes_gcm_encrypt_task(struct aes_gcm_resources *resources, struct doca_buf *src_buf, struct doca_buf *dst_buf, struct doca_aes_gcm_key *key, const uint8_t *iv, uint32_t iv_length, uint32_t tag_size, uint32_t aad_size)
doca_error_t destroy_aes_gcm_resources(struct aes_gcm_resources *resources)
void init_aes_gcm_params(struct aes_gcm_cfg *aes_gcm_cfg)
doca_error_t register_aes_gcm_params(void)
#define MAX_AES_GCM_KEY_SIZE
void decrypt_error_callback(struct doca_aes_gcm_task_decrypt *decrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
void encrypt_completed_callback(struct doca_aes_gcm_task_encrypt *encrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t allocate_aes_gcm_resources(const char *pci_addr, uint32_t max_bufs, struct aes_gcm_resources *resources)
void encrypt_error_callback(struct doca_aes_gcm_task_encrypt *encrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
#define MAX_AES_GCM_IV_LENGTH
aes_gcm_mode
@ AES_GCM_MODE_ENCRYPT
@ AES_GCM_MODE_DECRYPT
doca_error_t aes_gcm_task_encrypt_is_supported(struct doca_devinfo *devinfo)
#define MAX_FILE_NAME
void decrypt_completed_callback(struct doca_aes_gcm_task_decrypt *decrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
struct rdma_resources resources
doca_aes_gcm_key_type
AES-GCM key type.
Definition: doca_aes_gcm.h:131
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
enum doca_error doca_error_t
DOCA API return codes.
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]
uint8_t iv[MAX_AES_GCM_IV_LENGTH]
uint8_t raw_key[MAX_AES_GCM_KEY_SIZE]
char file_path[MAX_FILE_NAME]
uint32_t aad_size
enum aes_gcm_mode mode
uint32_t iv_length
enum doca_aes_gcm_key_type raw_key_type
uint32_t tag_size
char output_path[MAX_FILE_NAME]
struct doca_aes_gcm * aes_gcm
struct program_core_objects * state
enum aes_gcm_mode mode
size_t num_remaining_tasks
Convenience type for representing opaque data.
Definition: doca_types.h:56