kem.h
1 // SPDX-FileCopyrightText: 2025 Le'Sec Core collective 2 // 3 // SPDX-License-Identifier: LGPL-3.0-or-later 4 5 #ifndef LSC_KEM_H 6 # define LSC_KEM_H 7 8 # include <lscore/operation.h> 9 # include <lscrypto/op-functions.h> 10 # include <lscrypto/types.h> 11 12 # ifdef __cplusplus 13 extern "C" { 14 # endif 15 16 // Produces the following symbols: 17 // 18 // LSC_encapsulator_st 19 // LSC_encapsulator_t 20 // LSC_encapsulator_dispatch_fn 21 // LSC_encapsulator_destroy_fn 22 // LSC_new_encapsulator() 23 // LSC_free_encapsulator() 24 LSC_IMPL_BASIC_OPERATION_CLASS(encapsulator, encapsulation); 25 26 // Implementors should use the following bread and butter code factories 27 // in their encapsulator dispatch function: 28 // 29 // switch (num) { 30 // LSC_ENCAPSULATOR_TYPE_BASE_DISPATCHES("{typename}", {key-var}, 31 // {status-var}, {va_list-var}, 32 // {cmds-var}); 33 // ... 34 # define LSC_ENCAPSULATOR_TYPE_BASE_COMMANDS() \ 35 LSC_OPERATION_TYPE_BASE_COMMANDS(encapsulator) 36 # define LSC_ENCAPSULATOR_TYPE_BASE_DISPATCHES(lsc_id, lsc_obj, lsc_status, \ 37 lsc_valist, lsc_cmds) \ 38 LSC_OPERATION_TYPE_BASE_DISPATCHES(encapsulator, lsc_id, lsc_obj, \ 39 lsc_status, lsc_valist, lsc_cmds) 40 41 // Size functions. These are slightly different from the ones defined 42 // by op-functions.h macros, as encapsulation doesn't really have an input, 43 // but rather a second output. We therefore *steal* the input size cmd 44 // number to make the shared secret size, which is an output here. 45 // 46 // LSC_get_encapsulator_shared_secret_size() 47 // LSC_get_encapsulator_output_size() 48 enum { 49 LSC_NR_get_encapsulator_shared_secret_size = (0 + LSC_NR__crypto_op_class_start), 50 LSC_NR_get_encapsulator_output_size = (1 + LSC_NR__crypto_op_class_start) 51 }; 52 LSC_CRYPTO_OP_SIZE_FUNCTION(encapsulator, shared_secret); 53 LSC_CRYPTO_OP_SIZE_FUNCTION(encapsulator, output); 54 # define LSC_ENCAPSULATOR_SIZE_COMMANDS() \ 55 LSC_CRYPTO_OP_SIZE_COMMAND(encapsulator, shared_secret), \ 56 LSC_CRYPTO_OP_SIZE_COMMAND(encapsulator, output) 57 58 // Produces the following functions: 59 // 60 // LSC_can_encapsulator_use_key() 61 // LSC_use_encapsulator_key() 62 // LSC_get_expected_encapsulator_key_identity() 63 LSC_OPERATION_CLASS_OBJECT_FUNCTIONS(encapsulator, key); 64 65 // Encapsulation is a bit different, so we make a custom function for 66 // it, reflecting the fact that KEM encapsulation has two outputs. 67 LSC_CRYPTO_OP_ONESHOT_COMMAND_DEFS(encapsulation); 68 static inline LE_STATUS LSC_NAME(perform_encapsulation_once) 69 (LSC_NAME(encapsulator_t) *op, 70 unsigned char *wrappedsecret, size_t wrappedsecretsize, size_t *wrappedsecretlen, 71 unsigned char *sharedsecret, size_t sharedsecretsize, size_t *sharedsecretlen) 72 { 73 if (op == NULL) 74 return LE_STS_ERROR; 75 return op->lsc_dispatch(op, LSC_NR_perform_encapsulation_once, 76 wrappedsecret, wrappedsecretsize, wrappedsecretlen, 77 sharedsecret, sharedsecretsize, sharedsecretlen); 78 } 79 80 // Registration functions 81 LSC_OPERATION_CLASS_REGISTRATION_FUNCTIONS(encapsulator); 82 83 // ---------------------------------------------------------------------- 84 85 // Produces the following symbols: 86 // 87 // LSC_decapsulator_st 88 // LSC_decapsulator_t 89 // LSC_decapsulator_dispatch_fn 90 // LSC_decapsulator_destroy_fn 91 // LSC_new_decapsulator() 92 // LSC_free_decapsulator() 93 LSC_IMPL_BASIC_OPERATION_CLASS(decapsulator, decapsulation); 94 95 // Implementors should use the following bread and butter code factories 96 // in their decapsulator dispatch function: 97 // 98 // switch (num) { 99 // LSC_DECAPSULATOR_TYPE_BASE_DISPATCHES("{typename}", {key-var}, 100 // {status-var}, {va_list-var}, 101 // {cmds-var}); 102 // ... 103 # define LSC_DECAPSULATOR_TYPE_BASE_COMMANDS() \ 104 LSC_OPERATION_TYPE_BASE_COMMANDS(decapsulator) 105 # define LSC_DECAPSULATOR_TYPE_BASE_DISPATCHES(lsc_id, lsc_obj, lsc_status, \ 106 lsc_valist, lsc_cmds) \ 107 LSC_OPERATION_TYPE_BASE_DISPATCHES(decapsulator, lsc_id, lsc_obj, \ 108 lsc_status, lsc_valist, lsc_cmds) 109 110 // Produces the following functions: 111 // 112 // LSC_can_decapsulator_use_key() 113 // LSC_use_decapsulator_key() 114 // LSC_get_expected_decapsulator_key_identity() 115 LSC_OPERATION_CLASS_OBJECT_FUNCTIONS(decapsulator, key); 116 117 // Size functions. These are slightly different from the ones defined 118 // by op-functions.h macros, as decapsulation doesn't really have an input, 119 // but rather a second output. We therefore *steal* the input size cmd 120 // number to make the shared secret size. 121 // 122 // LSC_get_decapsulator_input_size() 123 // LSC_get_decapsulator_shared_secret_size() 124 enum { 125 LSC_NR_get_decapsulator_input_size = (0 + LSC_NR__crypto_op_class_start), 126 LSC_NR_get_decapsulator_shared_secret_size = (1 + LSC_NR__crypto_op_class_start) 127 }; 128 LSC_CRYPTO_OP_SIZE_FUNCTION(decapsulator, input); 129 LSC_CRYPTO_OP_SIZE_FUNCTION(decapsulator, shared_secret); 130 # define LSC_DECAPSULATOR_SIZE_COMMANDS() \ 131 LSC_CRYPTO_OP_SIZE_COMMAND(decapsulator, input), \ 132 LSC_CRYPTO_OP_SIZE_COMMAND(decapsulator, shared_secret) 133 134 // Produces the following functions: 135 // 136 // LSC_perform_decapsulation_once 137 LSC_CRYPTO_OP_ONESHOT_FUNCTIONS(decapsulator, decapsulation); 138 139 // Registration functions 140 LSC_OPERATION_CLASS_REGISTRATION_FUNCTIONS(decapsulator); 141 142 # ifdef __cplusplus 143 } 144 # endif 145 146 #endif