NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
aes_gcm_common.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 <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <time.h>
31 #include <ctype.h>
32 
33 #include <doca_ctx.h>
34 #include <doca_log.h>
35 #include <doca_error.h>
36 #include <doca_pe.h>
37 #include <doca_argp.h>
38 #include <doca_aes_gcm.h>
39 
40 #include "../common.h"
41 #include "aes_gcm_common.h"
42 
43 DOCA_LOG_REGISTER(AES_GCM::COMMON);
44 
45 /*
46  * Initialize AES-GCM parameters for the sample.
47  *
48  * @file_path [in]: Default file path name
49  * @aes_gcm_cfg [in]: AES-GCM configuration struct
50  */
52 {
53  strcpy(aes_gcm_cfg->output_path, "/tmp/out.txt");
54  strcpy(aes_gcm_cfg->pci_address, "03:00.0");
60  aes_gcm_cfg->aad_size = 0;
63 }
64 
65 /*
66  * Parse hex string to array of uint8_t
67  *
68  * @hex_str [in]: hex format string
69  * @hex_str_size [in]: the hex string length
70  * @bytes_arr [out]: the parsed bytes array
71  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
72  */
73 static doca_error_t parse_hex_to_bytes(const char *hex_str, size_t hex_str_size, uint8_t *bytes_arr)
74 {
75  uint8_t digit;
76  size_t i;
77 
78  /* Parse every digit (nibble) and translate it to the matching numeric value */
79  for (i = 0; i < hex_str_size; i++) {
80  /* Must be alpha-numeric */
81  if ('0' <= hex_str[i] && hex_str[i] <= '9')
82  digit = hex_str[i] - '0';
83  else if ('a' <= tolower(hex_str[i]) && tolower(hex_str[i]) <= 'f')
84  digit = tolower(hex_str[i]) - 'a' + 10;
85  else {
86  DOCA_LOG_ERR("Wrong format for input (%s) - need to be in hex format (1-9) or (a-f) values",
87  hex_str);
89  }
90  /* There are 2 nibbles (digits) in each byte, place them at their numeric place */
91  bytes_arr[i / 2] = (bytes_arr[i / 2] << 4) + digit;
92  }
93  return DOCA_SUCCESS;
94 }
95 
96 /*
97  * ARGP Callback - Handle PCI device address parameter
98  *
99  * @param [in]: Input parameter
100  * @config [in/out]: Program configuration context
101  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
102  */
103 static doca_error_t pci_address_callback(void *param, void *config)
104 {
105  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
106  char *pci_address = (char *)param;
107  int len;
108 
111  DOCA_LOG_ERR("Entered device PCI address exceeding the maximum size of %d",
114  }
115  strncpy(aes_gcm_cfg->pci_address, pci_address, len + 1);
116  return DOCA_SUCCESS;
117 }
118 
119 /*
120  * ARGP Callback - Handle user file parameter
121  *
122  * @param [in]: Input parameter
123  * @config [in/out]: Program configuration context
124  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
125  */
126 static doca_error_t file_callback(void *param, void *config)
127 {
128  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
129  char *file = (char *)param;
130  int len;
131 
132  len = strnlen(file, MAX_FILE_NAME);
133  if (len >= MAX_FILE_NAME) {
134  DOCA_LOG_ERR("Invalid file name length, max %d", USER_MAX_FILE_NAME);
136  }
137  strcpy(aes_gcm_cfg->file_path, file);
138  return DOCA_SUCCESS;
139 }
140 
141 /*
142  * ARGP Callback - Handle output file parameter
143  *
144  * @param [in]: Input parameter
145  * @config [in/out]: Program configuration context
146  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
147  */
148 static doca_error_t output_callback(void *param, void *config)
149 {
150  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
151  char *file = (char *)param;
152  int len;
153 
154  len = strnlen(file, MAX_FILE_NAME);
155  if (len >= MAX_FILE_NAME) {
156  DOCA_LOG_ERR("Invalid file name length, max %d", USER_MAX_FILE_NAME);
158  }
159  strcpy(aes_gcm_cfg->output_path, file);
160  return DOCA_SUCCESS;
161 }
162 
163 /*
164  * ARGP Callback - Handle raw key parameter
165  *
166  * @param [in]: Input parameter
167  * @config [in/out]: Program configuration context
168  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
169  */
170 static doca_error_t raw_key_callback(void *param, void *config)
171 {
172  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
173  char *raw_key = (char *)param;
175  int len;
176 
179  DOCA_LOG_ERR(
180  "Invalid string length %d to represent a key, string length should be %d or %d characters long",
181  len,
185  }
187  if (result != DOCA_SUCCESS)
188  return result;
190  return DOCA_SUCCESS;
191 }
192 
193 /*
194  * ARGP Callback - Handle initialization vector parameter
195  *
196  * @param [in]: Input parameter
197  * @config [in/out]: Program configuration context
198  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
199  */
200 static doca_error_t iv_callback(void *param, void *config)
201 {
202  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
203  char *iv = (char *)param;
205  int len;
206 
207  len = strnlen(iv, MAX_AES_GCM_IV_STR_LENGTH);
209  DOCA_LOG_ERR(
210  "Invalid string length %d to represent the initialization vector, max string length should be %d",
211  len,
214  }
216  if (result != DOCA_SUCCESS)
217  return result;
218  aes_gcm_cfg->iv_length = (len / 2) + (len % 2);
219  return DOCA_SUCCESS;
220 }
221 
222 /*
223  * ARGP Callback - Handle authentication tag parameter
224  *
225  * @param [in]: Input parameter
226  * @config [in/out]: Program configuration context
227  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
228  */
229 static doca_error_t tag_callback(void *param, void *config)
230 {
231  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
232  uint32_t tag_size = *(uint32_t *)param;
233 
235  DOCA_LOG_ERR("Invalid authentication tag size %d, tag size can be %d bytes or %d bytes",
236  tag_size,
240  }
242  return DOCA_SUCCESS;
243 }
244 
245 /*
246  * ARGP Callback - Handle additional authenticated data parameter
247  *
248  * @param [in]: Input parameter
249  * @config [in/out]: Program configuration context
250  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
251  */
252 static doca_error_t aad_callback(void *param, void *config)
253 {
254  struct aes_gcm_cfg *aes_gcm_cfg = (struct aes_gcm_cfg *)config;
255 
256  aes_gcm_cfg->aad_size = *(uint32_t *)param;
257  return DOCA_SUCCESS;
258 }
259 
260 /*
261  * ARGP Callback - Handle number of source doca_buf parameter
262  *
263  * @param [in]: Input parameter
264  * @config [in/out]: Program configuration context
265  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
266  */
267 static doca_error_t num_src_buf_callback(void *param, void *config)
268 {
269  struct aes_gcm_cfg *conf = (struct aes_gcm_cfg *)config;
270  const int *num = (int *)param;
271 
272  if (*num <= 0) {
273  DOCA_LOG_ERR("Entered number of source doca_buf: %d, valid value must be >= 1", *num);
275  }
276 
277  conf->num_src_buf = *num;
278 
279  return DOCA_SUCCESS;
280 }
281 
282 /*
283  * ARGP Callback - Handle number of destination doca_buf parameter
284  *
285  * @param [in]: Input parameter
286  * @config [in/out]: Program configuration context
287  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
288  */
289 static doca_error_t num_dst_buf_callback(void *param, void *config)
290 {
291  struct aes_gcm_cfg *conf = (struct aes_gcm_cfg *)config;
292  const int *num = (int *)param;
293 
294  if (*num <= 0) {
295  DOCA_LOG_ERR("Entered number of destination doca_buf: %d, valid value must be >= 1", *num);
297  }
298 
299  conf->num_dst_buf = *num;
300 
301  return DOCA_SUCCESS;
302 }
303 
304 /*
305  * Register the command line parameters for the sample.
306  *
307  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
308  */
310 {
312  struct doca_argp_param *pci_param, *file_param, *output_param, *raw_key_param, *iv_param, *tag_size_param,
313  *aad_size_param;
314  struct doca_argp_param *num_src_buf_param, *num_dst_buf_param;
315 
316  result = doca_argp_param_create(&pci_param);
317  if (result != DOCA_SUCCESS) {
318  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
319  return result;
320  }
321  doca_argp_param_set_short_name(pci_param, "p");
322  doca_argp_param_set_long_name(pci_param, "pci-addr");
323  doca_argp_param_set_description(pci_param, "DOCA device PCI device address - default: 03:00.0");
326  result = doca_argp_register_param(pci_param);
327  if (result != DOCA_SUCCESS) {
328  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
329  return result;
330  }
331 
332  result = doca_argp_param_create(&file_param);
333  if (result != DOCA_SUCCESS) {
334  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
335  return result;
336  }
337  doca_argp_param_set_short_name(file_param, "f");
338  doca_argp_param_set_long_name(file_param, "file");
339  doca_argp_param_set_description(file_param, "Input file to encrypt/decrypt");
340  doca_argp_param_set_mandatory(file_param);
343  result = doca_argp_register_param(file_param);
344  if (result != DOCA_SUCCESS) {
345  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
346  return result;
347  }
348 
349  result = doca_argp_param_create(&output_param);
350  if (result != DOCA_SUCCESS) {
351  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
352  return result;
353  }
354  doca_argp_param_set_short_name(output_param, "o");
355  doca_argp_param_set_long_name(output_param, "output");
356  doca_argp_param_set_description(output_param, "Output file - default: /tmp/out.txt");
359  result = doca_argp_register_param(output_param);
360  if (result != DOCA_SUCCESS) {
361  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
362  return result;
363  }
364 
365  result = doca_argp_param_create(&raw_key_param);
366  if (result != DOCA_SUCCESS) {
367  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
368  return result;
369  }
370  doca_argp_param_set_short_name(raw_key_param, "k");
371  doca_argp_param_set_long_name(raw_key_param, "key");
373  raw_key_param,
374  "Raw key to encrypt/decrypt with, represented in hex format (32 characters for 128-bit key, and 64 for 256-bit key) - default: 256-bit key, equals to zero");
377  result = doca_argp_register_param(raw_key_param);
378  if (result != DOCA_SUCCESS) {
379  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
380  return result;
381  }
382 
383  result = doca_argp_param_create(&iv_param);
384  if (result != DOCA_SUCCESS) {
385  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
386  return result;
387  }
388  doca_argp_param_set_short_name(iv_param, "i");
389  doca_argp_param_set_long_name(iv_param, "iv");
391  iv_param,
392  "Initialization vector, represented in hex format (0-24 characters for 0-96-bit IV) - default: 96-bit IV, equals to zero");
395  result = doca_argp_register_param(iv_param);
396  if (result != DOCA_SUCCESS) {
397  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
398  return result;
399  }
400 
401  result = doca_argp_param_create(&tag_size_param);
402  if (result != DOCA_SUCCESS) {
403  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
404  return result;
405  }
406  doca_argp_param_set_short_name(tag_size_param, "t");
407  doca_argp_param_set_long_name(tag_size_param, "tag-size");
409  tag_size_param,
410  "Authentication tag size. Tag size is in bytes and can be 12B or 16B - default: 12");
413  result = doca_argp_register_param(tag_size_param);
414  if (result != DOCA_SUCCESS) {
415  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
416  return result;
417  }
418 
419  result = doca_argp_param_create(&aad_size_param);
420  if (result != DOCA_SUCCESS) {
421  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
422  return result;
423  }
424  doca_argp_param_set_short_name(aad_size_param, "a");
425  doca_argp_param_set_long_name(aad_size_param, "aad-size");
426  doca_argp_param_set_description(aad_size_param, "Additional authenticated data size - default: 0");
429  result = doca_argp_register_param(aad_size_param);
430  if (result != DOCA_SUCCESS) {
431  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
432  return result;
433  }
434 
435  /* Create and register number of source doca_buf param */
436  result = doca_argp_param_create(&num_src_buf_param);
437  if (result != DOCA_SUCCESS) {
438  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
439  return result;
440  }
441  doca_argp_param_set_short_name(num_src_buf_param, "ns");
442  doca_argp_param_set_long_name(num_src_buf_param, "num-src-buf");
443  doca_argp_param_set_description(num_src_buf_param, "number of doca_buf for source buffer");
445  doca_argp_param_set_type(num_src_buf_param, DOCA_ARGP_TYPE_INT);
446  result = doca_argp_register_param(num_src_buf_param);
447  if (result != DOCA_SUCCESS) {
448  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
449  return result;
450  }
451 
452  /* Create and register number of destination doca_buf param */
453  result = doca_argp_param_create(&num_dst_buf_param);
454  if (result != DOCA_SUCCESS) {
455  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
456  return result;
457  }
458  doca_argp_param_set_short_name(num_dst_buf_param, "nd");
459  doca_argp_param_set_long_name(num_dst_buf_param, "num-dst-buf");
460  doca_argp_param_set_description(num_dst_buf_param, "number of doca_buf for destination buffer");
462  doca_argp_param_set_type(num_dst_buf_param, DOCA_ARGP_TYPE_INT);
463  result = doca_argp_register_param(num_dst_buf_param);
464  if (result != DOCA_SUCCESS) {
465  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
466  return result;
467  }
468 
469  return DOCA_SUCCESS;
470 }
471 
480 static void aes_gcm_state_changed_callback(const union doca_data user_data,
481  struct doca_ctx *ctx,
482  enum doca_ctx_states prev_state,
483  enum doca_ctx_states next_state)
484 {
485  (void)ctx;
486  (void)prev_state;
487 
488  struct aes_gcm_resources *resources = (struct aes_gcm_resources *)user_data.ptr;
489 
490  switch (next_state) {
491  case DOCA_CTX_STATE_IDLE:
492  DOCA_LOG_INFO("AES-GCM context has been stopped");
493  /* We can stop progressing the PE */
494  resources->run_pe_progress = false;
495  break;
500  DOCA_LOG_ERR("AES-GCM context entered into starting state. Unexpected transition");
501  break;
503  DOCA_LOG_INFO("AES-GCM context is running");
504  break;
513  DOCA_LOG_INFO("AES-GCM context entered into stopping state. Any inflight tasks will be flushed");
514  break;
515  default:
516  break;
517  }
518 }
519 
520 doca_error_t allocate_aes_gcm_resources(const char *pci_addr, uint32_t max_bufs, struct aes_gcm_resources *resources)
521 {
522  struct program_core_objects *state = NULL;
523  union doca_data ctx_user_data = {0};
524  doca_error_t result, tmp_result;
525  uint32_t max_buf_list_len = 0;
526 
527  resources->state = malloc(sizeof(*resources->state));
528  if (resources->state == NULL) {
530  DOCA_LOG_ERR("Failed to allocate DOCA program core objects: %s", doca_error_get_descr(result));
531  return result;
532  }
534 
535  state = resources->state;
536 
537  /* Open DOCA device */
538  if (pci_addr != NULL) {
539  /* If pci_addr was provided then open using it */
540  if (resources->mode == AES_GCM_MODE_ENCRYPT)
542  else
544  } else {
545  /* If pci_addr was not provided then look for DOCA device */
546  if (resources->mode == AES_GCM_MODE_ENCRYPT)
548  else
550  }
551 
552  if (result != DOCA_SUCCESS) {
553  DOCA_LOG_ERR("Failed to open DOCA device for DOCA AES-GCM: %s", doca_error_get_descr(result));
554  goto free_state;
555  }
556 
557  if (resources->mode == AES_GCM_MODE_ENCRYPT) {
559  &max_buf_list_len);
560  } else {
562  &max_buf_list_len);
563  }
564  if (result != DOCA_SUCCESS) {
565  DOCA_LOG_ERR("Failed to get task max_buf_list_len capability: %s", doca_error_get_descr(result));
566  return result;
567  }
568  if (max_bufs > max_buf_list_len) {
570  DOCA_LOG_ERR("Number of doca_buf [%d] exceed the limitation [%u]: %s",
571  max_bufs,
572  max_buf_list_len,
574  return result;
575  }
576 
577  result = doca_aes_gcm_create(state->dev, &resources->aes_gcm);
578  if (result != DOCA_SUCCESS) {
579  DOCA_LOG_ERR("Unable to create AES-GCM engine: %s", doca_error_get_descr(result));
580  goto close_device;
581  }
582 
583  state->ctx = doca_aes_gcm_as_ctx(resources->aes_gcm);
584 
585  result = create_core_objects(state, max_bufs);
586  if (result != DOCA_SUCCESS) {
587  DOCA_LOG_ERR("Unable to create DOCA core objects: %s", doca_error_get_descr(result));
588  goto destroy_aes_gcm;
589  }
590 
591  result = doca_pe_connect_ctx(state->pe, state->ctx);
592  if (result != DOCA_SUCCESS) {
593  DOCA_LOG_ERR("Unable to set progress engine for PE: %s", doca_error_get_descr(result));
595  }
596 
598  if (result != DOCA_SUCCESS) {
599  DOCA_LOG_ERR("Unable to set AES-GCM state change callback: %s", doca_error_get_descr(result));
601  }
602 
603  if (resources->mode == AES_GCM_MODE_ENCRYPT)
608  else
613  if (result != DOCA_SUCCESS) {
614  DOCA_LOG_ERR("Unable to set configurations for AES-GCM task: %s", doca_error_get_descr(result));
616  }
617 
618  /* Include resources in user data of context to be used in callbacks */
619  ctx_user_data.ptr = resources;
620  result = doca_ctx_set_user_data(state->ctx, ctx_user_data);
621  if (result != DOCA_SUCCESS) {
622  DOCA_LOG_ERR("Unable to set user data for AES-GCM ctx: %s", doca_error_get_descr(result));
624  }
625 
626  return result;
627 
629  tmp_result = destroy_core_objects(state);
630  if (tmp_result != DOCA_SUCCESS) {
631  DOCA_LOG_ERR("Failed to destroy DOCA core objects: %s", doca_error_get_descr(tmp_result));
632  DOCA_ERROR_PROPAGATE(result, tmp_result);
633  }
634 destroy_aes_gcm:
635  tmp_result = doca_aes_gcm_destroy(resources->aes_gcm);
636  if (tmp_result != DOCA_SUCCESS) {
637  DOCA_LOG_ERR("Failed to destroy DOCA AES-GCM: %s", doca_error_get_descr(tmp_result));
638  DOCA_ERROR_PROPAGATE(result, tmp_result);
639  }
640 close_device:
641  tmp_result = doca_dev_close(state->dev);
642  if (tmp_result != DOCA_SUCCESS) {
643  DOCA_ERROR_PROPAGATE(result, tmp_result);
644  DOCA_LOG_ERR("Failed to close device: %s", doca_error_get_descr(tmp_result));
645  }
646 free_state:
647  free(resources->state);
648 
649  return result;
650 }
651 
653 {
654  struct program_core_objects *state = resources->state;
655  doca_error_t result = DOCA_SUCCESS, tmp_result;
656 
657  if (resources->aes_gcm != NULL) {
658  result = doca_ctx_stop(state->ctx);
659  if (result != DOCA_SUCCESS)
660  DOCA_LOG_ERR("Unable to stop context: %s", doca_error_get_descr(result));
661  state->ctx = NULL;
662 
663  tmp_result = doca_aes_gcm_destroy(resources->aes_gcm);
664  if (tmp_result != DOCA_SUCCESS) {
665  DOCA_LOG_ERR("Failed to destroy DOCA AES-GCM: %s", doca_error_get_descr(tmp_result));
666  DOCA_ERROR_PROPAGATE(result, tmp_result);
667  }
668  }
669 
670  tmp_result = destroy_core_objects(state);
671  if (tmp_result != DOCA_SUCCESS) {
672  DOCA_LOG_ERR("Failed to destroy DOCA core objects: %s", doca_error_get_descr(tmp_result));
673  DOCA_ERROR_PROPAGATE(result, tmp_result);
674  }
675 
676  free(state);
677  resources->state = NULL;
678 
679  return result;
680 }
681 
683  struct doca_buf *src_buf,
684  struct doca_buf *dst_buf,
685  struct doca_aes_gcm_key *key,
686  const uint8_t *iv,
687  uint32_t iv_length,
688  uint32_t tag_size,
689  uint32_t aad_size)
690 {
691  struct doca_aes_gcm_task_encrypt *encrypt_task;
692  struct program_core_objects *state = resources->state;
693  struct doca_task *task;
694  union doca_data task_user_data = {0};
695  struct timespec ts = {
696  .tv_sec = 0,
697  .tv_nsec = SLEEP_IN_NANOS,
698  };
699  doca_error_t result, task_result;
700 
701  /* Include result in user data of task to be used in the callbacks */
702  task_user_data.ptr = &task_result;
703  /* Allocate and construct encrypt task */
705  src_buf,
706  dst_buf,
707  key,
708  iv,
709  iv_length,
710  tag_size,
711  aad_size,
712  task_user_data,
713  &encrypt_task);
714  if (result != DOCA_SUCCESS) {
715  DOCA_LOG_ERR("Failed to allocate encrypt task: %s", doca_error_get_descr(result));
716  return result;
717  }
718 
719  task = doca_aes_gcm_task_encrypt_as_task(encrypt_task);
720 
721  /* Submit encrypt task */
723  result = doca_task_submit(task);
724  if (result != DOCA_SUCCESS) {
725  DOCA_LOG_ERR("Failed to submit encrypt task: %s", doca_error_get_descr(result));
726  doca_task_free(task);
727  return result;
728  }
729 
730  resources->run_pe_progress = true;
731 
732  /* Wait for all tasks to be completed and context to stop */
733  while (resources->run_pe_progress) {
734  if (doca_pe_progress(state->pe) == 0)
735  nanosleep(&ts, &ts);
736  }
737 
738  return task_result;
739 }
740 
742  struct doca_buf *src_buf,
743  struct doca_buf *dst_buf,
744  struct doca_aes_gcm_key *key,
745  const uint8_t *iv,
746  uint32_t iv_length,
747  uint32_t tag_size,
748  uint32_t aad_size)
749 {
750  struct doca_aes_gcm_task_decrypt *decrypt_task;
751  struct program_core_objects *state = resources->state;
752  struct doca_task *task;
753  union doca_data task_user_data = {0};
754  struct timespec ts = {
755  .tv_sec = 0,
756  .tv_nsec = SLEEP_IN_NANOS,
757  };
758  doca_error_t result, task_result;
759 
760  /* Include result in user data of task to be used in the callbacks */
761  task_user_data.ptr = &task_result;
762  /* Allocate and construct decrypt task */
764  src_buf,
765  dst_buf,
766  key,
767  iv,
768  iv_length,
769  tag_size,
770  aad_size,
771  task_user_data,
772  &decrypt_task);
773  if (result != DOCA_SUCCESS) {
774  DOCA_LOG_ERR("Failed to allocate decrypt task: %s", doca_error_get_descr(result));
775  return result;
776  }
777 
778  task = doca_aes_gcm_task_decrypt_as_task(decrypt_task);
779 
780  /* Submit decrypt task */
782  result = doca_task_submit(task);
783  if (result != DOCA_SUCCESS) {
784  DOCA_LOG_ERR("Failed to submit decrypt task: %s", doca_error_get_descr(result));
785  doca_task_free(task);
786  return result;
787  }
788 
789  resources->run_pe_progress = true;
790 
791  /* Wait for all tasks to be completed and context to stop */
792  while (resources->run_pe_progress) {
793  if (doca_pe_progress(state->pe) == 0)
794  nanosleep(&ts, &ts);
795  }
796 
797  return task_result;
798 }
799 
800 doca_error_t aes_gcm_task_encrypt_is_supported(struct doca_devinfo *devinfo)
801 {
803 }
804 
805 doca_error_t aes_gcm_task_decrypt_is_supported(struct doca_devinfo *devinfo)
806 {
808 }
809 
810 void encrypt_completed_callback(struct doca_aes_gcm_task_encrypt *encrypt_task,
811  union doca_data task_user_data,
812  union doca_data ctx_user_data)
813 {
814  struct aes_gcm_resources *resources = (struct aes_gcm_resources *)ctx_user_data.ptr;
815  doca_error_t *result = (doca_error_t *)task_user_data.ptr;
816 
817  DOCA_LOG_INFO("Encrypt task was done successfully");
818 
819  /* Assign success to the result */
820  *result = DOCA_SUCCESS;
821  /* Free task */
823  /* Decrement number of remaining tasks */
825  /* Stop context once all tasks are completed */
826  if (resources->num_remaining_tasks == 0)
827  (void)doca_ctx_stop(resources->state->ctx);
828 }
829 
830 void encrypt_error_callback(struct doca_aes_gcm_task_encrypt *encrypt_task,
831  union doca_data task_user_data,
832  union doca_data ctx_user_data)
833 {
834  struct aes_gcm_resources *resources = (struct aes_gcm_resources *)ctx_user_data.ptr;
835  struct doca_task *task = doca_aes_gcm_task_encrypt_as_task(encrypt_task);
836  doca_error_t *result = (doca_error_t *)task_user_data.ptr;
837 
838  /* Get the result of the task */
839  *result = doca_task_get_status(task);
840  DOCA_LOG_ERR("Encrypt task failed: %s", doca_error_get_descr(*result));
841  /* Free task */
842  doca_task_free(task);
843  /* Decrement number of remaining tasks */
845  /* Stop context once all tasks are completed */
846  if (resources->num_remaining_tasks == 0)
847  (void)doca_ctx_stop(resources->state->ctx);
848 }
849 
850 void decrypt_completed_callback(struct doca_aes_gcm_task_decrypt *decrypt_task,
851  union doca_data task_user_data,
852  union doca_data ctx_user_data)
853 {
854  struct aes_gcm_resources *resources = (struct aes_gcm_resources *)ctx_user_data.ptr;
855  doca_error_t *result = (doca_error_t *)task_user_data.ptr;
856 
857  DOCA_LOG_INFO("Decrypt task was done successfully");
858 
859  /* Assign success to the result */
860  *result = DOCA_SUCCESS;
861  /* Free task */
863  /* Decrement number of remaining tasks */
865  /* Stop context once all tasks are completed */
866  if (resources->num_remaining_tasks == 0)
867  (void)doca_ctx_stop(resources->state->ctx);
868 }
869 
870 void decrypt_error_callback(struct doca_aes_gcm_task_decrypt *decrypt_task,
871  union doca_data task_user_data,
872  union doca_data ctx_user_data)
873 {
874  struct aes_gcm_resources *resources = (struct aes_gcm_resources *)ctx_user_data.ptr;
875  struct doca_task *task = doca_aes_gcm_task_decrypt_as_task(decrypt_task);
876  doca_error_t *result = (doca_error_t *)task_user_data.ptr;
877 
878  /* Get the result of the task */
879  *result = doca_task_get_status(task);
880  DOCA_LOG_ERR("Decrypt task failed: %s", doca_error_get_descr(*result));
881  /* Free task */
882  doca_task_free(task);
883  /* Decrement number of remaining tasks */
885  /* Stop context once all tasks are completed */
886  if (resources->num_remaining_tasks == 0)
887  (void)doca_ctx_stop(resources->state->ctx);
888 }
#define NULL
Definition: __stddef_null.h:26
static void aes_gcm_state_changed_callback(const union doca_data user_data, struct doca_ctx *ctx, enum doca_ctx_states prev_state, enum doca_ctx_states next_state)
static doca_error_t raw_key_callback(void *param, void *config)
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)
static doca_error_t pci_address_callback(void *param, void *config)
doca_error_t aes_gcm_task_decrypt_is_supported(struct doca_devinfo *devinfo)
DOCA_LOG_REGISTER(AES_GCM::COMMON)
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)
static doca_error_t aad_callback(void *param, void *config)
void init_aes_gcm_params(struct aes_gcm_cfg *aes_gcm_cfg)
doca_error_t register_aes_gcm_params(void)
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)
static doca_error_t iv_callback(void *param, void *config)
void encrypt_error_callback(struct doca_aes_gcm_task_encrypt *encrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
static doca_error_t num_dst_buf_callback(void *param, void *config)
static doca_error_t output_callback(void *param, void *config)
static doca_error_t tag_callback(void *param, void *config)
static doca_error_t parse_hex_to_bytes(const char *hex_str, size_t hex_str_size, uint8_t *bytes_arr)
static doca_error_t num_src_buf_callback(void *param, void *config)
doca_error_t aes_gcm_task_encrypt_is_supported(struct doca_devinfo *devinfo)
static doca_error_t file_callback(void *param, void *config)
void decrypt_completed_callback(struct doca_aes_gcm_task_decrypt *decrypt_task, union doca_data task_user_data, union doca_data ctx_user_data)
#define AES_GCM_KEY_256_STR_SIZE
#define NUM_AES_GCM_TASKS
#define AES_GCM_AUTH_TAG_96_SIZE_IN_BYTES
#define MAX_AES_GCM_KEY_STR_SIZE
#define AES_GCM_KEY_128_STR_SIZE
#define MAX_AES_GCM_KEY_SIZE
#define AES_GCM_AUTH_TAG_128_SIZE_IN_BYTES
#define USER_MAX_FILE_NAME
#define MAX_AES_GCM_IV_LENGTH
#define MAX_AES_GCM_IV_STR_LENGTH
@ AES_GCM_MODE_ENCRYPT
int32_t result
#define SLEEP_IN_NANOS
Definition: comch_utils.c:40
doca_error_t destroy_core_objects(struct program_core_objects *state)
Definition: common.c:392
doca_error_t create_core_objects(struct program_core_objects *state, uint32_t max_bufs)
Definition: common.c:302
doca_error_t open_doca_device_with_capabilities(tasks_check func, struct doca_dev **retval)
Definition: common.c:188
static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
Definition: device.c:43
uint64_t len
#define MAX_FILE_NAME
struct rdma_resources resources
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_task_decrypt_alloc_init(struct doca_aes_gcm *aes_gcm, struct doca_buf const *src_buff, struct doca_buf *dst_buff, struct doca_aes_gcm_key *key, const uint8_t *iv, uint32_t iv_length, uint32_t tag_size, uint32_t aad_size, union doca_data user_data, struct doca_aes_gcm_task_decrypt **task)
Allocate aes_gcm decrypt task.
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_cap_task_decrypt_get_max_list_buf_num_elem(const struct doca_devinfo *devinfo, uint32_t *max_list_num_elem)
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_cap_task_encrypt_get_max_list_buf_num_elem(const struct doca_devinfo *devinfo, uint32_t *max_list_num_elem)
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_task_encrypt_alloc_init(struct doca_aes_gcm *aes_gcm, struct doca_buf const *src_buff, struct doca_buf *dst_buff, struct doca_aes_gcm_key *key, const uint8_t *iv, uint32_t iv_length, uint32_t tag_size, uint32_t aad_size, union doca_data user_data, struct doca_aes_gcm_task_encrypt **task)
Allocate aes_gcm encrypt task.
DOCA_EXPERIMENTAL struct doca_task * doca_aes_gcm_task_encrypt_as_task(struct doca_aes_gcm_task_encrypt *task)
convert aes_gcm encrypt task to doca_task
DOCA_EXPERIMENTAL struct doca_ctx * doca_aes_gcm_as_ctx(struct doca_aes_gcm *aes_gcm)
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_task_encrypt_set_conf(struct doca_aes_gcm *aes_gcm, doca_aes_gcm_task_encrypt_completion_cb_t task_completion_cb, doca_aes_gcm_task_encrypt_completion_cb_t task_error_cb, uint32_t num_tasks)
This method sets the aes_gcm encrypt task configuration.
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_cap_task_encrypt_is_supported(const struct doca_devinfo *devinfo)
Check if a aes_gcm encrypt task is supported by a device.
DOCA_EXPERIMENTAL struct doca_task * doca_aes_gcm_task_decrypt_as_task(struct doca_aes_gcm_task_decrypt *task)
convert aes_gcm decrypt task to doca_task
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_create(struct doca_dev *dev, struct doca_aes_gcm **aes_gcm)
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_task_decrypt_set_conf(struct doca_aes_gcm *aes_gcm, doca_aes_gcm_task_decrypt_completion_cb_t task_completion_cb, doca_aes_gcm_task_decrypt_completion_cb_t task_error_cb, uint32_t num_tasks)
This method sets the aes_gcm decrypt task configuration.
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_cap_task_decrypt_is_supported(const struct doca_devinfo *devinfo)
Check if a aes_gcm decrypt task is supported by a device.
DOCA_EXPERIMENTAL doca_error_t doca_aes_gcm_destroy(struct doca_aes_gcm *aes_gcm)
@ DOCA_AES_GCM_KEY_256
Definition: doca_aes_gcm.h:133
@ DOCA_AES_GCM_KEY_128
Definition: doca_aes_gcm.h:132
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_mandatory(struct doca_argp_param *param)
Mark the program param as mandatory.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
@ DOCA_ARGP_TYPE_INT
Definition: doca_argp.h:57
DOCA_STABLE doca_error_t doca_ctx_set_state_changed_cb(struct doca_ctx *ctx, doca_ctx_state_changed_callback_t cb)
Set state changed callback.
DOCA_STABLE doca_error_t doca_ctx_set_user_data(struct doca_ctx *ctx, union doca_data user_data)
set user data to context
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
doca_ctx_states
This enum defines the states of a context.
Definition: doca_ctx.h:83
@ DOCA_CTX_STATE_STARTING
Definition: doca_ctx.h:93
@ DOCA_CTX_STATE_STOPPING
Definition: doca_ctx.h:106
@ DOCA_CTX_STATE_IDLE
Definition: doca_ctx.h:88
@ DOCA_CTX_STATE_RUNNING
Definition: doca_ctx.h:98
#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
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...
DOCA_STABLE doca_error_t doca_dev_close(struct doca_dev *dev)
Destroy allocated local device instance.
#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_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ 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_task_get_status(const struct doca_task *task)
Get task status.
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
DOCA_STABLE doca_error_t doca_task_submit(struct doca_task *task)
Submit a task to a progress engine.
DOCA_STABLE uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
DOCA_STABLE void doca_task_free(struct doca_task *task)
Free a task back to where it was allocated from.
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
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_pe * pe
Definition: common.h:51
struct doca_dev * dev
Definition: common.h:46
struct doca_ctx * ctx
Definition: common.h:50
size_t num_remaining_tasks
Definition: rdma_common.h:134
bool run_pe_progress
Definition: rdma_common.h:133
Convenience type for representing opaque data.
Definition: doca_types.h:56
void * ptr
Definition: doca_types.h:57
struct upf_accel_ctx * ctx