/ include / lscrypto / encrypt.h
encrypt.h
  1  // SPDX-FileCopyrightText: 2023-2025 Le'Sec Core collective
  2  //
  3  // SPDX-License-Identifier: LGPL-3.0-or-later
  4  
  5  #ifndef LSC_ENCRYPT_H
  6  # define LSC_ENCRYPT_H
  7  
  8  # include <lscore/operation.h>
  9  # include <lscrypto/op-functions.h>
 10  # include <lscrypto/key.h>
 11  
 12  # ifdef __cplusplus
 13  extern "C" {
 14  # endif
 15  
 16    // Produces the following symbols:
 17    //
 18    // LSC_encryptor_st
 19    // LSC_encryptor_t
 20    // LSC_encryptor_dispatch_fn
 21    // LSC_encryptor_destroy_fn
 22    // LSC_new_encryptor()
 23    // LSC_free_encryptor()
 24    LSC_IMPL_BASIC_OPERATION_CLASS(encryptor, encryption);
 25  
 26    // Implementors should use the following bread and butter code factories
 27    // in their encryptor dispatch function:
 28    //
 29    // switch (num) {
 30    //   LSC_ENCRYPTOR_TYPE_BASE_DISPATCHES("{typename}", {key-var},
 31    //                                      {status-var}, {va_list-var},
 32    //                                      {cmds-var});
 33    //   ...
 34  # define LSC_ENCRYPTOR_TYPE_BASE_COMMANDS()                             \
 35    LSC_OPERATION_TYPE_BASE_COMMANDS(encryptor)
 36  # define LSC_ENCRYPTOR_TYPE_BASE_DISPATCHES(lsc_id, lsc_obj,            \
 37                                              lsc_status, lsc_valist,     \
 38                                              lsc_cmds)                   \
 39    LSC_OPERATION_TYPE_BASE_DISPATCHES(encryptor, lsc_id, lsc_obj,        \
 40                                       lsc_status, lsc_valist, lsc_cmds)
 41  
 42    // Produces the following functions:
 43    //
 44    // LSC_get_encryptor_unit_size():
 45    //
 46    // Returns the size of the unit that the encryptor crypto operation works
 47    // with.  All inputs and outputs are expected to be treated as arrays
 48    // of such units internally, and in some cases, input and output sizes are
 49    // expected to be exact multiples of the unit size.  In case the caller
 50    // can't know, it's safer to assume the latter.
 51    //
 52    // LSC_get_encryptor_input_size():
 53    //
 54    // Returns the expected input size.  This function may be undefined, which
 55    // means that the input size is unlimited.
 56    //
 57    // LSC_get_encryptor_output_size():
 58    //
 59    // Returns the expected output size.  This function may be undefined, which
 60    // means that the output size is unlimited.
 61    enum {
 62      LSC_NR_get_encryptor_unit_size   = (0 + LSC_NR__crypto_op_class_start),
 63      LSC_NR_get_encryptor_input_size  = (1 + LSC_NR__crypto_op_class_start),
 64      LSC_NR_get_encryptor_output_size = (2 + LSC_NR__crypto_op_class_start)
 65    };
 66    LSC_CRYPTO_OP_SIZE_FUNCTION(encryptor, unit);
 67    LSC_CRYPTO_OP_SIZE_FUNCTION(encryptor, input);
 68    LSC_CRYPTO_OP_SIZE_FUNCTION(encryptor, output);
 69  # define LSC_ENCRYPTOR_SIZE_COMMANDS()                                  \
 70    LSC_CRYPTO_OP_SIZE_COMMAND(encryptor, unit),                          \
 71    LSC_CRYPTO_OP_SIZE_COMMAND(encryptor, input),                         \
 72    LSC_CRYPTO_OP_SIZE_COMMAND(encryptor, output)
 73  
 74    // Produces the following functions:
 75    //
 76    // LSC_can_encryptor_use_key()
 77    // LSC_use_encryptor_key()
 78    // LSC_get_expected_encryptor_key_identity()
 79    LSC_OPERATION_CLASS_OBJECT_FUNCTIONS(encryptor, key);
 80  
 81    // Produces the following function:
 82    //
 83    // LSC_perform_encryption_once():
 84    LSC_CRYPTO_OP_ONESHOT_FUNCTIONS(encryptor, encryption);
 85  
 86    // Produces the following function:
 87    //
 88    // LSC_start_encryption():
 89    // LSC_stop_encryption():
 90    // LSC_perform_encryption():
 91    // LSC_finalize_encryption():
 92    LSC_CRYPTO_OP_BASE_FUNCTIONS(encryptor, encryption);
 93    LSC_CRYPTO_OP_PERFORM_FUNCTIONS(encryptor, encryption);
 94    LSC_CRYPTO_OP_FINALIZE_FUNCTIONS(encryptor, encryption);
 95  
 96    // Registration functions
 97    LSC_OPERATION_CLASS_REGISTRATION_FUNCTIONS(encryptor);
 98  
 99    // ----------------------------------------------------------------------
100  
101    // Produces the following symbols:
102    //
103    // LSC_decryptor_st
104    // LSC_decryptor_t
105    // LSC_decryptor_dispatch_fn
106    // LSC_decryptor_destroy_fn
107    // LSC_new_decryptor()
108    // LSC_free_decryptor()
109    LSC_IMPL_BASIC_OPERATION_CLASS(decryptor, decryption);
110  
111    // Implementors should use the following bread and butter code factories
112    // in their decryptor dispatch function:
113    //
114    // switch (num) {
115    //   LSC_DECRYPTOR_TYPE_BASE_DISPATCHES("{typename}", {key-var},
116    //                                      {status-var}, {va_list-var},
117    //                                      {cmds-var});
118    //   ...
119  # define LSC_DECRYPTOR_TYPE_BASE_COMMANDS()                             \
120    LSC_OPERATION_TYPE_BASE_COMMANDS(decryptor)
121  # define LSC_DECRYPTOR_TYPE_BASE_DISPATCHES(lsc_id, lsc_obj,            \
122                                              lsc_status, lsc_valist,     \
123                                              lsc_cmds)                   \
124    LSC_OPERATION_TYPE_BASE_DISPATCHES(decryptor, lsc_id, lsc_obj,        \
125                                       lsc_status, lsc_valist, lsc_cmds)
126  
127    // Produces the following functions:
128    //
129    // LSC_get_decryptor_unit_size():
130    //
131    // Returns the size of the unit that the decryptor crypto operation works
132    // with.  All inputs and outputs are expected to be treated as arrays
133    // of such units internally, and in some cases, input and output sizes are
134    // expected to be exact multiples of the unit size.  In case the caller
135    // can't know, it's safer to assume the latter.
136    //
137    // LSC_get_decryptor_input_size():
138    //
139    // Returns the expected input size.  This function may be undefined, which
140    // means that the input size is unlimited.
141    //
142    // LSC_get_decryptor_output_size():
143    //
144    // Returns the expected output size.  This function may be undefined, which
145    // means that the output size is unlimited.
146    enum {
147      LSC_NR_get_decryptor_unit_size   = (0 + LSC_NR__crypto_op_class_start),
148      LSC_NR_get_decryptor_input_size  = (1 + LSC_NR__crypto_op_class_start),
149      LSC_NR_get_decryptor_output_size = (2 + LSC_NR__crypto_op_class_start)
150    };
151    LSC_CRYPTO_OP_SIZE_FUNCTION(decryptor, unit);
152    LSC_CRYPTO_OP_SIZE_FUNCTION(decryptor, input);
153    LSC_CRYPTO_OP_SIZE_FUNCTION(decryptor, output);
154  # define LSC_DECRYPTOR_SIZE_COMMANDS()                                  \
155    LSC_CRYPTO_OP_SIZE_COMMAND(decryptor, unit),                          \
156    LSC_CRYPTO_OP_SIZE_COMMAND(decryptor, input),                         \
157    LSC_CRYPTO_OP_SIZE_COMMAND(decryptor, output)
158  
159    // Produces the following functions:
160    //
161    // LSC_can_decryptor_use_key()
162    // LSC_use_decryptor_key()
163    // LSC_get_expected_decryptor_key_identity()
164    LSC_OPERATION_CLASS_OBJECT_FUNCTIONS(decryptor, key);
165  
166    // Produces the following function:
167    //
168    // LSC_perform_decryption_once():
169    LSC_CRYPTO_OP_ONESHOT_FUNCTIONS(decryptor, decryption);
170  
171    // Produces the following function:
172    //
173    // LSC_start_decryption():
174    // LSC_stop_decryption():
175    // LSC_perform_decryption():
176    // LSC_finalize_decryption():
177    LSC_CRYPTO_OP_BASE_FUNCTIONS(decryptor, decryption);
178    LSC_CRYPTO_OP_PERFORM_FUNCTIONS(decryptor, decryption);
179    LSC_CRYPTO_OP_FINALIZE_FUNCTIONS(decryptor, decryption);
180  
181    // Registration functions
182    LSC_OPERATION_CLASS_REGISTRATION_FUNCTIONS(decryptor);
183  
184  # ifdef __cplusplus
185  }
186  # endif
187  
188  #endif