/ include / lsplugin / mkoperator.h
mkoperator.h
  1  /*                                                                  
  2   * SPDX-FileCopyrightText: 2023-2024 Le'Sec Plugin collective
  3   *
  4   * SPDX-License-Identifier: LGPL-3.0-or-later
  5   */
  6  
  7  #ifndef LSPLUGIN_MKOPERATOR_H
  8  # define LSPLUGIN_MKOPERATOR_H
  9  
 10  # include <lscrypto/rand.h>
 11  # include <lscrypto/encrypt.h>
 12  # include <lscrypto/sign.h>
 13  # include <lscrypto/derive.h>
 14  # include <lscrypto/digest.h>
 15  # include <lscrypto/mac.h>
 16  # include <lscrypto/kem.h>
 17  
 18  # define LSplugin_encryptor_FUNCTIONS()                                 \
 19    LE_STATUS (*lsp_get_encryptor_unit_size)(LSC_encryptor_t *lsp_enc,    \
 20                                             size_t *lsp_size);           \
 21    LE_STATUS (*lsp_get_encryptor_input_size)(LSC_encryptor_t *lsp_enc,   \
 22                                              size_t *lsp_size);          \
 23    LE_STATUS (*lsp_get_encryptor_output_size)(LSC_encryptor_t *lsp_enc,  \
 24                                                size_t *lsp_size);        \
 25    LE_STATUS (*lsp_perform_encryption_once)(LSC_encryptor_t *lsp_op,     \
 26                                             const unsigned char *lsp_in, \
 27                                             size_t lsp_inlen,            \
 28                                             unsigned char *lsp_out,      \
 29                                             size_t lsp_outsize,          \
 30                                             size_t *lsp_outlen);         \
 31    LE_STATUS (*lsp_start_encryption)(LSC_encryptor_t *lsp_enc);          \
 32    LE_STATUS (*lsp_perform_encryption)(LSC_encryptor_t *lsp_enc,         \
 33                                        const unsigned char *lsp_in,      \
 34                                        size_t lsp_inlen,                 \
 35                                        unsigned char *lsp_out,           \
 36                                        size_t lsp_outsize,               \
 37                                        size_t *lsp_outlen);              \
 38    LE_STATUS (*lsp_finalize_encryption)(LSC_encryptor_t *lsp_enc,        \
 39                                         unsigned char *lsp_out,          \
 40                                         size_t lsp_outsize,              \
 41                                         size_t *lsp_outlen);             \
 42    LE_STATUS (*lsp_stop_encryption)(LSC_encryptor_t *lsp_enc)
 43  
 44  # define LSplugin_decryptor_FUNCTIONS()                                 \
 45    LE_STATUS (*lsp_get_decryptor_unit_size)(LSC_decryptor_t *lsp_enc,    \
 46                                              size_t *lsp_size);          \
 47    LE_STATUS (*lsp_get_decryptor_input_size)(LSC_decryptor_t *lsp_enc,   \
 48                                               size_t *lsp_size);         \
 49    LE_STATUS (*lsp_get_decryptor_output_size)(LSC_decryptor_t *lsp_enc,  \
 50                                                size_t *lsp_size);        \
 51    LE_STATUS (*lsp_perform_decryption_once)(LSC_decryptor_t *lsp_op,     \
 52                                             const unsigned char *lsp_in, \
 53                                             size_t lsp_inlen,            \
 54                                             unsigned char *lsp_out,      \
 55                                             size_t lsp_outsize,          \
 56                                             size_t *lsp_outlen);         \
 57    LE_STATUS (*lsp_start_decryption)(LSC_decryptor_t *lsp_dec);          \
 58    LE_STATUS (*lsp_perform_decryption)(LSC_decryptor_t *lsp_dec,         \
 59                                        const unsigned char *lsp_in,      \
 60                                        size_t lsp_inlen,                 \
 61                                        unsigned char *lsp_out,           \
 62                                        size_t lsp_outsize,               \
 63                                        size_t *lsp_outlen);              \
 64    LE_STATUS (*lsp_finalize_decryption)(LSC_decryptor_t *lsp_dec,        \
 65                                         unsigned char *lsp_out,          \
 66                                         size_t lsp_outsize,              \
 67                                         size_t *lsp_outlen);             \
 68    LE_STATUS (*lsp_stop_decryption)(LSC_decryptor_t *lsp_dec)
 69  
 70  # define LSplugin_signer_FUNCTIONS()                                    \
 71    LE_STATUS (*lsp_get_signer_input_size)(LSC_signer_t *lsp_sig,         \
 72                                           size_t *lsp_size);             \
 73    LE_STATUS (*lsp_get_signer_signature_size)(LSC_signer_t *lsp_sig,     \
 74                                               size_t *lsp_size);         \
 75    LE_STATUS (*lsp_perform_signature_once)(LSC_signer_t *lsp_op,         \
 76                                            const unsigned char *lsp_msg, \
 77                                            size_t lsp_msglen,            \
 78                                            unsigned char *lsp_sig,       \
 79                                            size_t lsp_sigsize,           \
 80                                            size_t *lsp_siglen);          \
 81    LE_STATUS (*lsp_start_signature)(LSC_signer_t *lsp_dec);              \
 82    LE_STATUS (*lsp_accumulate_signature_input)(LSC_signer_t *lsp_dec,    \
 83                                                const unsigned char *lsp_in, \
 84                                                size_t lsp_inlen);        \
 85    LE_STATUS (*lsp_finalize_signature)(LSC_signer_t *lsp_dec,            \
 86                                        unsigned char *lsp_sig,           \
 87                                        size_t lsp_sigsize,               \
 88                                        size_t *lsp_siglen);              \
 89    LE_STATUS (*lsp_stop_signature)(LSC_signer_t *lsp_dec)
 90  
 91  # define LSplugin_verifier_FUNCTIONS()                                  \
 92    LE_STATUS (*lsp_get_verifier_input_size)(LSC_verifier_t *lsp_ver,     \
 93                                             size_t *lsp_size);           \
 94    LE_STATUS (*lsp_get_verifier_signature_size)(LSC_verifier_t *lsp_ver, \
 95                                                 size_t *lsp_size);       \
 96    LE_STATUS (*lsp_perform_verification_once)(LSC_verifier_t *lsp_ver,   \
 97                                               const unsigned char *lsp_msg, \
 98                                               size_t lsp_msglen,         \
 99                                               const unsigned char *lsp_sig, \
100                                               size_t lsp_siglen,         \
101                                               _Bool *lsp_confirmed);     \
102    LE_STATUS (*lsp_start_verification)(LSC_verifier_t *lsp_dec);         \
103    LE_STATUS (*lsp_accumulate_verification_input)(LSC_verifier_t *lsp_dec, \
104                                                   const unsigned char *lsp_in, \
105                                                   size_t lsp_inlen);     \
106    LE_STATUS (*lsp_finalize_verification)(LSC_verifier_t *lsp_dec,       \
107                                           const unsigned char *lsp_sig,  \
108                                           size_t lsp_siglen,             \
109                                           _Bool *lsp_confirmed);         \
110    LE_STATUS (*lsp_stop_verification)(LSC_verifier_t *lsp_dec)
111  
112  # define LSplugin_derivator_FUNCTIONS()                                 \
113    LE_STATUS (*lsp_perform_derivation_once)(LSC_derivator_t *lsp_drv,    \
114                                             LSC_key_t *lsp_peer,         \
115                                             unsigned char *lsp_out,      \
116                                             size_t lsp_outsize,          \
117                                             size_t *lsp_outlen)
118  
119  # define LSplugin_digester_FUNCTIONS()                                  \
120    LE_STATUS (*lsp_get_digester_input_size)(LSC_digester_t *lsp_d,       \
121                                              size_t *lsp_size);          \
122    LE_STATUS (*lsp_get_digester_digest_size)(LSC_digester_t *lsp_d,      \
123                                              size_t *lsp_size);          \
124    LE_STATUS (*lsp_perform_digestion_once)(LSC_digester_t *lsp_d,        \
125                                            const unsigned char *lsp_msg, \
126                                            size_t lsp_msglen,            \
127                                            unsigned char *lsp_digest,    \
128                                            size_t lsp_digestsize,        \
129                                            size_t *lsp_digestlen);       \
130    LE_STATUS (*lsp_start_digestion)(LSC_digester_t *lsp_d);              \
131    LE_STATUS (*lsp_accumulate_digestion_input)(LSC_digester_t *lsp_d,    \
132                                                const unsigned char *lsp_msg, \
133                                                size_t lsp_msglen);       \
134    LE_STATUS (*lsp_extract_digestion_output)(LSC_digester_t *lsp_d,      \
135                                              unsigned char *lsp_out,     \
136                                              size_t lsp_outsize,         \
137                                              size_t *lsp_outlen);        \
138    LE_STATUS (*lsp_finalize_digestion)(LSC_digester_t *lsp_d,            \
139                                        unsigned char *lsp_digest,        \
140                                        size_t lsp_digestsize,            \
141                                        size_t *lsp_digestlen);           \
142    LE_STATUS (*lsp_stop_digestion)(LSC_digester_t *lsp_d)
143  
144  # define LSplugin_mac_FUNCTIONS()                                       \
145    LE_STATUS (*lsp_get_mac_input_size)(LSC_mac_t *lsp_d,                 \
146                                         size_t *lsp_size);               \
147    LE_STATUS (*lsp_get_mac_output_size)(LSC_mac_t *lsp_d,                \
148                                         size_t *lsp_size);               \
149    LE_STATUS (*lsp_perform_mac_once)(LSC_mac_t *lsp_d,                   \
150                                      const unsigned char *lsp_msg,       \
151                                      size_t lsp_msglen,                  \
152                                      unsigned char *lsp_digest,          \
153                                      size_t lsp_digestsize,              \
154                                      size_t *lsp_digestlen);             \
155    LE_STATUS (*lsp_start_mac)(LSC_mac_t *lsp_d);                         \
156    LE_STATUS (*lsp_accumulate_mac_input)(LSC_mac_t *lsp_d,               \
157                                          const unsigned char *lsp_msg,   \
158                                          size_t lsp_msglen);             \
159    LE_STATUS (*lsp_finalize_mac)(LSC_mac_t *lsp_d,                       \
160                                  unsigned char *lsp_digest,              \
161                                  size_t lsp_digestsize,                  \
162                                  size_t *lsp_digestlen);                 \
163    LE_STATUS (*lsp_stop_mac)(LSC_mac_t *lsp_d)
164  
165  # define LSplugin_entropy_FUNCTIONS()                                   \
166    LE_STATUS (*lsp_add_entropy_source)(LSC_entropy_t *lsp_entropy,       \
167                                        LSC_entropy_t *lsp_source);       \
168    LE_STATUS (*lsp_start_entropy)(LSC_entropy_t *lsp_d);                 \
169    LE_STATUS (*lsp_extract_entropy_output)(LSC_entropy_t *lsp_d,         \
170                                            size_t lsp_bits_of_entropy,   \
171                                            unsigned char *lsp_out,       \
172                                            size_t lsp_outsize,           \
173                                            size_t *lsp_outlen);          \
174    LE_STATUS (*lsp_stop_entropy)(LSC_entropy_t *lsp_d)
175  
176  # define LSplugin_rbg_FUNCTIONS()                                       \
177    LE_STATUS (*lsp_set_entropy_source)(LSC_rbg_t *lsp_rbg,               \
178                                        LSC_entropy_t *lsp_entropy);      \
179    LE_STATUS (*lsp_start_rbg)(LSC_rbg_t *lsp_d,                          \
180                               const char *lsp_personalization);          \
181    LE_STATUS (*lsp_extract_rbg_output)(LSC_rbg_t *lsp_d,                 \
182                                        size_t lsp_number_of_bits,        \
183                                        size_t lsp_security_strength,     \
184                                        _Bool lsp_prediction_resistance,  \
185                                        const char *lsp_additional_input, \
186                                        unsigned char *lsp_out,           \
187                                        size_t lsp_outsize,               \
188                                        size_t *lsp_outlen);              \
189    LE_STATUS (*lsp_reseed_rgb)(LSC_rbg_t *lsp_rbg,                       \
190                                const char *lsp_additional_input);        \
191    LE_STATUS (*lsp_stop_rbg)(LSC_rbg_t *lsp_d)
192  
193  # define LSplugin_encapsulator_FUNCTIONS()                              \
194    LE_STATUS                                                             \
195    (*lsp_get_encapsulator_shared_secret_size)(LSC_encapsulator_t *lsp_sig, \
196                                                          size_t *lsp_size); \
197    LE_STATUS                                                             \
198    (*lsp_get_encapsulator_output_size)(LSC_encapsulator_t *lsp_sig,      \
199                                         size_t *lsp_size);               \
200    LE_STATUS                                                             \
201    (*lsp_perform_encapsulation_once)(LSC_encapsulator_t *lsp_op,         \
202                                      unsigned char *lsp_wrappedsecret,   \
203                                      size_t lsp_wrappedsecretsize,       \
204                                      size_t *lsp_wrappedsecretlen,       \
205                                      unsigned char *lsp_sharedsecret,    \
206                                      size_t lsp_sharedsecretsize,        \
207                                      size_t *lsp_sharedsecretlen)
208  
209  # define LSplugin_decapsulator_FUNCTIONS()                              \
210    LE_STATUS                                                             \
211    (*lsp_get_decapsulator_input_size)(LSC_decapsulator_t *lsp_ver,       \
212                                       size_t *lsp_size);                 \
213    LE_STATUS                                                             \
214    (*lsp_get_decapsulator_shared_secret_size)(LSC_decapsulator_t *lsp_ver, \
215                                               size_t *lsp_size);         \
216    LE_STATUS                                                             \
217    (*lsp_perform_decapsulation_once)(LSC_decapsulator_t *lsp_ver,        \
218                                      const unsigned char *lsp_wrappedsecret, \
219                                      size_t lsp_wrappedsecretlen,        \
220                                      unsigned char *lsp_sharedsecret,    \
221                                      size_t lsp_sharedsecretsize,        \
222                                      size_t *lsp_sharedsecretlen)
223  
224  # define LSplugin_OPERATOR_DESC(OP_T, OPN_T)                            \
225    typedef struct LSplugin_##OP_T##_desc_st LSplugin_##OP_T##_desc_t;    \
226    struct LSplugin_##OP_T##_desc_st {                                    \
227      /* General documentation.  Params are documented separately */      \
228      const char *lsp_docstring;                                          \
229                                                                          \
230      /*                                                                  \
231       * If |id| is NULL, this is a descriptor for a key-associated       \
232       * operator.  Otherwise, it's a self-standing operator.             \
233       */                                                                 \
234      const char *lsp_id;                                                 \
235      const void *lsp_priv_desc;                                          \
236                                                                          \
237      /*                                                                  \
238       * The key identity handled by this operation, or NULL if the       \
239       * operation doesn't use a key, or is a key assocaited operation.   \
240       */                                                                 \
241      const char *lsp_key_id;                                             \
242                                                                          \
243      /*                                                                  \
244       * Operator data conceptually contains two groups of data:          \
245       *                                                                  \
246       * - diverse data set by the called, such as keys, IVs.             \
247       * - diverse running state data, i.e. data that's kept by the       \
248       *   functions performing the operation itself.                     \
249       *                                                                  \
250       * To manage the operator data, two functions are needed:           \
251       *                                                                  \
252       * - setup_operator_data constructs the memory storage space for    \
253       *   the operator data.  In other words, if there is no such space  \
254       *   yet, this function allocates it and initializes it (usually by \
255       *   zeroing it).                                                   \
256       *   This is expected to be called any time there's a need for      \
257       *   storage of operator specific data.                             \
258       * - clean_operator_data cleans away all operator data, and may     \
259       *   even deallocate it.                                            \
260       *   This is expected to be called by the destroy function at the   \
261       *   very least.                                                    \
262       */                                                                 \
263      LE_STATUS (*lsp_setup_##OP_T##_data)(LSC_##OP_T##_t *op);           \
264      LE_STATUS (*lsp_clean_##OP_T##_data)(LSC_##OP_T##_t *op);           \
265                                                                          \
266      /*                                                                  \
267       * for self-standing operators, set_key_##OP_T MUST be NULL,        \
268       * and should not be used.  For key-associated operators,           \
269       * set_key_##OP_T() may be non-NULL, if the key struct caches       \
270       * the associated operator.                                         \
271       * set_##OP_T##_key and get_##OP_T##_key should be non-NULL.        \
272       */                                                                 \
273      LE_STATUS (*lsp_set_key_##OP_T)(LSC_key_t *key, LSC_##OP_T##_t *op); \
274      LE_STATUS (*lsp_set_##OP_T##_key)(LSC_##OP_T##_t *op, LSC_key_t *key); \
275      LE_STATUS (*lsp_get_##OP_T##_key)(LSC_##OP_T##_t *op, LSC_key_t **key); \
276      const int *lsp_dispatch_cmds;                                       \
277                                                                          \
278      /*                                                                  \
279       * Parameter data is usually op->lsc_data.  However, there are      \
280       * cases where this isn't so, and the implementation may supply     \
281       * an alternative way of getting the parameter data with this       \
282       * function pointer.                                                \
283       */                                                                 \
284      LE_STATUS (*lsp_get_##OP_T##_param_data)                            \
285        (LSC_##OP_T##_t *lsp_op, void **lsp_data);                        \
286      LE_STATUS (*lsp_get_##OPN_T##_gettable_param_desc)                  \
287        (LSC_##OP_T##_t *lsp_op, const LSC_param_desc_t **lsp_param_desc); \
288      LE_STATUS (*lsp_get_##OPN_T##_settable_param_desc)                  \
289        (LSC_##OP_T##_t *lsp_op, const LSC_param_desc_t **lsp_param_desc); \
290      LSplugin_##OP_T##_FUNCTIONS();                                      \
291    }
292  
293  LSplugin_OPERATOR_DESC(encryptor, encryption);
294  LSplugin_OPERATOR_DESC(decryptor, decryption);
295  LSplugin_OPERATOR_DESC(signer, signature);
296  LSplugin_OPERATOR_DESC(verifier, verification);
297  LSplugin_OPERATOR_DESC(derivator, derivation);
298  LSplugin_OPERATOR_DESC(digester, digestion);
299  LSplugin_OPERATOR_DESC(mac, mac);
300  LSplugin_OPERATOR_DESC(entropy, entropy);
301  LSplugin_OPERATOR_DESC(rbg, rbg);
302  LSplugin_OPERATOR_DESC(encapsulator, encapsulation);
303  LSplugin_OPERATOR_DESC(decapsulator, decapsulation);
304  
305  LE_STATUS LSplugin_encryptor_dispatch(LSC_encryptor_t *lsp_op, int lsp_num, ...);
306  LE_STATUS LSplugin_destroy_encryptor(LSC_encryptor_t *lsp_op);
307  LE_STATUS LSplugin_decryptor_dispatch(LSC_decryptor_t *lsp_op, int lsp_num, ...);
308  LE_STATUS LSplugin_destroy_decryptor(LSC_decryptor_t *lsp_op);
309  LE_STATUS LSplugin_signer_dispatch(LSC_signer_t *lsp_op, int lsp_num, ...);
310  LE_STATUS LSplugin_destroy_signer(LSC_signer_t *lsp_op);
311  LE_STATUS LSplugin_verifier_dispatch(LSC_verifier_t *lsp_op, int lsp_num, ...);
312  LE_STATUS LSplugin_destroy_verifier(LSC_verifier_t *lsp_op);
313  LE_STATUS LSplugin_derivator_dispatch(LSC_derivator_t *lsp_op, int lsp_num, ...);
314  LE_STATUS LSplugin_destroy_derivator(LSC_derivator_t *lsp_op);
315  LE_STATUS LSplugin_digester_dispatch(LSC_digester_t *lsp_op, int lsp_num, ...);
316  LE_STATUS LSplugin_destroy_digester(LSC_digester_t *lsp_op);
317  LE_STATUS LSplugin_mac_dispatch(LSC_mac_t *lsp_op, int lsp_num, ...);
318  LE_STATUS LSplugin_destroy_mac(LSC_mac_t *lsp_op);
319  LE_STATUS LSplugin_entropy_dispatch(LSC_entropy_t *lsp_op, int lsp_num, ...);
320  LE_STATUS LSplugin_destroy_entropy(LSC_entropy_t *lsp_op);
321  LE_STATUS LSplugin_rbg_dispatch(LSC_rbg_t *lsp_op, int lsp_num, ...);
322  LE_STATUS LSplugin_destroy_rbg(LSC_rbg_t *lsp_op);
323  LE_STATUS LSplugin_encapsulator_dispatch(LSC_encapsulator_t *lsp_op, int lsp_num, ...);
324  LE_STATUS LSplugin_destroy_encapsulator(LSC_encapsulator_t *lsp_op);
325  LE_STATUS LSplugin_decapsulator_dispatch(LSC_decapsulator_t *lsp_op, int lsp_num, ...);
326  LE_STATUS LSplugin_destroy_decapsulator(LSC_decapsulator_t *lsp_op);
327  
328  #endif