NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
aes_gcm_decrypt_sample.c
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 #include <string.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include <doca_buf.h>
32 #include <doca_buf_inventory.h>
33 #include <doca_ctx.h>
34 #include <doca_aes_gcm.h>
35 #include <doca_error.h>
36 #include <doca_log.h>
37 
38 #include "common.h"
39 #include "aes_gcm_common.h"
40 
41 DOCA_LOG_REGISTER(AES_GCM_DECRYPT);
42 
43 /*
44  * Run aes_gcm_decrypt sample
45  *
46  * @cfg [in]: Configuration parameters
47  * @file_data [in]: file data for the decrypt task
48  * @file_size [in]: file size
49  * @return: DOCA_SUCCESS on success, DOCA_ERROR otherwise.
50  */
51 doca_error_t aes_gcm_decrypt(struct aes_gcm_cfg *cfg, char *file_data, size_t file_size)
52 {
53  struct aes_gcm_resources resources = {0};
54  struct program_core_objects *state = NULL;
55  struct doca_buf *src_doca_buf = NULL;
56  struct doca_buf *dst_doca_buf = NULL;
57  uint32_t max_bufs = cfg->num_dst_buf + cfg->num_src_buf;
58  char *dst_buffer = NULL;
59  uint8_t *resp_head = NULL;
60  size_t data_len = 0;
61  char *dump = NULL;
62  FILE *out_file = NULL;
63  struct doca_aes_gcm_key *key = NULL;
65  doca_error_t tmp_result = DOCA_SUCCESS;
66  uint64_t max_decrypt_buf_size = 0;
67 
68  out_file = fopen(cfg->output_path, "wr");
69  if (out_file == NULL) {
70  DOCA_LOG_ERR("Unable to open output file: %s", cfg->output_path);
71  return DOCA_ERROR_NO_MEMORY;
72  }
73 
74  /* Allocate resources */
76  result = allocate_aes_gcm_resources(cfg->pci_address, max_bufs, &resources);
77  if (result != DOCA_SUCCESS) {
78  DOCA_LOG_ERR("Failed to allocate AES-GCM resources: %s", doca_error_get_descr(result));
79  goto close_file;
80  }
81 
82  state = resources.state;
83 
85  if (result != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Failed to query AES-GCM decrypt max buf size: %s", doca_error_get_descr(result));
87  goto destroy_resources;
88  }
89 
90  if (file_size > max_decrypt_buf_size) {
91  DOCA_LOG_ERR("File size %zu > max buffer size %zu", file_size, max_decrypt_buf_size);
92  goto destroy_resources;
93  }
94 
95  /* Start AES-GCM context */
96  result = doca_ctx_start(state->ctx);
97  if (result != DOCA_SUCCESS) {
98  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
99  goto destroy_resources;
100  }
101 
102  dst_buffer = calloc(1, max_decrypt_buf_size);
103  if (dst_buffer == NULL) {
105  DOCA_LOG_ERR("Failed to allocate memory: %s", doca_error_get_descr(result));
106  goto destroy_resources;
107  }
108 
109  result = doca_mmap_set_memrange(state->dst_mmap, dst_buffer, max_decrypt_buf_size);
110  if (result != DOCA_SUCCESS) {
111  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
112  goto free_dst_buf;
113  }
114  result = doca_mmap_start(state->dst_mmap);
115  if (result != DOCA_SUCCESS) {
116  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
117  goto free_dst_buf;
118  }
119 
120  result = doca_mmap_set_memrange(state->src_mmap, file_data, file_size);
121  if (result != DOCA_SUCCESS) {
122  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
123  goto free_dst_buf;
124  }
125 
126  result = doca_mmap_start(state->src_mmap);
127  if (result != DOCA_SUCCESS) {
128  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
129  goto free_dst_buf;
130  }
131 
132  /* Construct DOCA buffer for each address range */
134  state->src_mmap,
135  file_data,
136  file_size,
137  cfg->num_src_buf,
138  true,
139  &src_doca_buf);
140  if (result != DOCA_SUCCESS) {
141  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing source buffer: %s",
143  goto free_dst_buf;
144  }
145 
146  /* Construct DOCA buffer for each address range */
148  state->dst_mmap,
149  dst_buffer,
150  max_decrypt_buf_size,
151  cfg->num_dst_buf,
152  false,
153  &dst_doca_buf);
154  if (result != DOCA_SUCCESS) {
155  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
157  goto destroy_src_buf;
158  }
159 
160  /* Create DOCA AES-GCM key */
161  result = doca_aes_gcm_key_create(resources.aes_gcm, cfg->raw_key, cfg->raw_key_type, &key);
162  if (result != DOCA_SUCCESS) {
163  DOCA_LOG_ERR("Unable to create DOCA AES-GCM key: %s", doca_error_get_descr(result));
164  goto destroy_dst_buf;
165  }
166 
167  /* Submit AES-GCM decrypt task */
169  src_doca_buf,
170  dst_doca_buf,
171  key,
172  (uint8_t *)cfg->iv,
173  cfg->iv_length,
174  cfg->tag_size,
175  cfg->aad_size);
176  if (result != DOCA_SUCCESS) {
177  DOCA_LOG_ERR("AES-GCM decrypt task failed: %s", doca_error_get_descr(result));
178  goto destroy_key;
179  }
180 
181  /* Write the result to output file */
182  doca_buf_get_head(dst_doca_buf, (void **)&resp_head);
183  doca_buf_get_data_len(dst_doca_buf, &data_len);
184  fwrite(resp_head, sizeof(uint8_t), data_len, out_file);
185  DOCA_LOG_INFO("File was decrypted successfully and saved in: %s", cfg->output_path);
186 
187  /* Print destination buffer data */
188  dump = hex_dump(resp_head, data_len);
189  if (dump == NULL) {
190  DOCA_LOG_ERR("Failed to allocate memory for printing buffer content");
192  goto destroy_key;
193  }
194 
195  DOCA_LOG_INFO("AES-GCM decrypted data:\n%s", dump);
196  free(dump);
197 
198 destroy_key:
199  tmp_result = doca_aes_gcm_key_destroy(key);
200  if (tmp_result != DOCA_SUCCESS) {
201  DOCA_LOG_ERR("Failed to destroy DOCA AES-GCM key: %s", doca_error_get_descr(tmp_result));
202  DOCA_ERROR_PROPAGATE(result, tmp_result);
203  }
204 destroy_dst_buf:
205  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
206  if (tmp_result != DOCA_SUCCESS) {
207  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
208  doca_error_get_descr(tmp_result));
209  DOCA_ERROR_PROPAGATE(result, tmp_result);
210  }
211 destroy_src_buf:
212  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
213  if (tmp_result != DOCA_SUCCESS) {
214  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
215  doca_error_get_descr(tmp_result));
216  DOCA_ERROR_PROPAGATE(result, tmp_result);
217  }
218 free_dst_buf:
219  free(dst_buffer);
220 destroy_resources:
221  tmp_result = destroy_aes_gcm_resources(&resources);
222  if (tmp_result != DOCA_SUCCESS) {
223  DOCA_LOG_ERR("Failed to destroy AES-GCM resources: %s", doca_error_get_descr(tmp_result));
224  DOCA_ERROR_PROPAGATE(result, tmp_result);
225  }
226 close_file:
227  fclose(out_file);
228 
229  return result;
230 }
#define NULL
Definition: __stddef_null.h:26
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 destroy_aes_gcm_resources(struct aes_gcm_resources *resources)
doca_error_t allocate_aes_gcm_resources(const char *pci_addr, uint32_t max_bufs, struct aes_gcm_resources *resources)
@ AES_GCM_MODE_DECRYPT
DOCA_LOG_REGISTER(AES_GCM_DECRYPT)
doca_error_t aes_gcm_decrypt(struct aes_gcm_cfg *cfg, char *file_data, size_t file_size)
int32_t result
doca_error_t allocat_doca_buf_list(struct doca_buf_inventory *buf_inv, struct doca_mmap *mmap, void *buf_addr, size_t buf_len, int num_buf, bool set_data_pos, struct doca_buf **dbuf)
Definition: common.c:527
struct rdma_resources resources
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_key_create(struct doca_aes_gcm *aes_gcm, const void *raw_key, enum doca_aes_gcm_key_type raw_key_type, struct doca_aes_gcm_key **key)
Create an AES-GCM key from the user raw key to send with the task to allow encrypt/decrypt operations...
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_key_destroy(struct doca_aes_gcm_key *key)
Destroy AES-GCM key that was created in doca_aes_gcm_key_create.
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_cap_task_decrypt_get_max_buf_size(const struct doca_devinfo *devinfo, uint64_t *max_buffer_size)
Get aes_gcm decrypt max buffer size.
DOCA_STABLE doca_error_t doca_buf_dec_refcount(struct doca_buf *buf, uint16_t *refcount)
Decrease the object reference count by 1, if 0 reached, return the element back to the inventory.
DOCA_STABLE doca_error_t doca_buf_get_data_len(const struct doca_buf *buf, size_t *data_len)
Get buffer's data length.
DOCA_STABLE doca_error_t doca_buf_get_head(const struct doca_buf *buf, void **head)
Get the buffer's head.
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE struct doca_devinfo * doca_dev_as_devinfo(const struct doca_dev *dev)
Get local device info from device. This should be useful when wanting to query information about devi...
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_NO_MEMORY
Definition: doca_error.h:45
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_STABLE doca_error_t doca_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
char * hex_dump(const void *data, size_t size)
struct doca_mmap * src_mmap
Definition: common.h:47
struct doca_buf_inventory * buf_inv
Definition: common.h:49
struct doca_dev * dev
Definition: common.h:46
struct doca_mmap * dst_mmap
Definition: common.h:48
struct doca_ctx * ctx
Definition: common.h:50