1 /* 2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 */ 18 19 #ifndef CRYP_SYMC_H_ 20 #define CRYP_SYMC_H_ 21 22 #include "hi_cipher_compat.h" 23 #include "drv_cipher_kapi.h" 24 #include "drv_osal_lib.h" 25 #include "drv_symc.h" 26 27 /* 28 * brief Initialize crypto of symc 29 */ 30 hi_s32 cryp_symc_init(hi_void); 31 32 /* 33 * brief Deinitialize crypto of symc 34 */ 35 hi_void cryp_symc_deinit(hi_void); 36 37 /* 38 * brief Create symc handle 39 * 40 * param handle symc handle to be initialized 41 * param chn symc channel 42 */ 43 typedef hi_void *(*func_symc_create)(hi_u32 hard_chn); 44 45 /** 46 * brief Clear symc context 47 * 48 * param handle symc handle to be destroy 49 */ 50 typedef hi_s32 (*func_symc_destroy)(hi_void *ctx); 51 52 /** 53 * brief symc key schedule 54 * 55 * param handle symc handle 56 * param[in] fkey first key buffer, default 57 * param[in] skey second key buffer, expand 58 * param hisi_klen input key type, output key length in bytes 59 * 60 * return 0 if successful, or HI_SYMC_ERR_KEY_LEN_INVALID 61 */ 62 typedef hi_s32 (*func_symc_setkey)(hi_void *ctx, const hi_u8 *fkey, const hi_u8 *skey, hi_u32 *hisi_klen); 63 64 /** 65 * brief Symc iv schedule 66 * 67 * param handle symc handle 68 * param IV Symc IV 69 * param ivlen length of iv 70 * 71 * return 0 if successful. 72 */ 73 typedef hi_s32 (*func_symc_setiv)(hi_void *ctx, const hi_u8 *iv, hi_u32 ivlen, hi_u32 usage); 74 75 /** 76 * brief Symc iv schedule 77 * 78 * param handle symc handle 79 * param IV Symc IV 80 * param ivlen must be 128, 192 or 256 81 * 82 * return 0 if successful. 83 */ 84 typedef hi_void (*func_symc_getiv)(const hi_void *ctx, hi_u8 *iv, hi_u32 *ivlen); 85 86 /** 87 * brief SYMC alg and mode 88 * 89 * param handle SYMC handle 90 * param alg Symmetric cipher alg 91 * param mode Symmetric cipher mode 92 * param keybits must be 128, 192 or 256 93 * 94 * return 0 if successful. 95 */ 96 typedef hi_void (*func_symc_setmode)(hi_void *ctx, symc_alg alg, symc_mode mode, symc_width width); 97 98 /** 99 * brief SYMC wait done 100 * 101 * param ctx SYMC handle 102 * return 0 if successful. 103 */ 104 typedef hi_s32 (*func_symc_wait_done)(hi_void *ctx, hi_u32 timeout); 105 106 /** 107 * brief SYMC alg and mode 108 * 109 * param handle SYMC handle 110 * param round SM1 round number 111 * 112 * return 0 if successful. 113 */ 114 typedef hi_s32 (*func_symc_sm1_setround)(hi_void *ctx, hi_u32 round); 115 116 /** 117 * brief symc buffer encryption/decryption. 118 * 119 * Note: Due to the nature of aes you should use the same key schedule for 120 * both encryption and decryption. 121 * 122 * param ctx symc ctx 123 * param operation decrypt or encrypt 124 * param pack package for encrypt or decrypt. 125 * param wait last or not 126 * 127 * return 0 if successful 128 */ 129 typedef hi_s32 (*func_symc_crypto)(hi_void *ctx, hi_u32 operation, symc_multi_pack *pack, hi_u32 wait); 130 131 /** 132 * brief CCM/GCM set Associated Data 133 * 134 * param ctx SYMC handle 135 * param aad Associated Data 136 * param alen Associated Data Length 137 * param tlen Tag length 138 * 139 * return 0 if successful. 140 */ 141 typedef hi_s32 (*func_aead_set_aad)(hi_void *ctx, compat_addr aad, hi_u32 alen, hi_u32 tlen); 142 143 /** 144 * brief SYMC multiple buffer encryption/decryption. 145 * param[in] id The channel number. 146 * param[in] tag tag data of CCM/GCM 147 * param uuid uuid The user identification. 148 * 149 * return 0 if successful 150 */ 151 typedef hi_s32 (*func_aead_get_tag)(hi_void *ctx, hi_u32 tag[AEAD_TAG_SIZE_IN_WORD], hi_u32 *taglen); 152 153 /* struct of Symmetric cipher function template */ 154 typedef struct { 155 hi_u32 valid; /* valid or not */ 156 symc_alg alg; /* Alg of Symmetric cipher */ 157 symc_mode mode; /* Mode of Symmetric cipher */ 158 func_symc_setmode setmode; /* Set mode function */ 159 func_symc_sm1_setround setround; /* SM1 set round function */ 160 func_symc_create create; /* Create function */ 161 func_symc_destroy destroy; /* destroy function */ 162 func_symc_setkey setkey; /* setkey function */ 163 func_symc_setiv setiv; /* setiv function */ 164 func_symc_getiv getiv; /* getiv function */ 165 func_aead_set_aad setadd; /* setadd function */ 166 func_aead_get_tag gettag; /* get tag function */ 167 func_symc_crypto crypto; /* crypto function */ 168 func_symc_wait_done waitdone; /* wait done */ 169 } symc_func; 170 171 /* 172 * brief symc alloc channel. 173 * param[out] hard_chn symc channel. 174 * retval On success, func is returned. On error, HI_NULL is returned. 175 */ 176 hi_s32 cryp_symc_alloc_chn(hi_u32 *hard_chn); 177 178 /* 179 * brief symc free channel. 180 * param[in] hard_chn symc channel. 181 * retval On success, func is returned. On error, HI_NULL is returned. 182 */ 183 hi_void cryp_symc_free_chn(hi_u32 hard_chn); 184 185 /* 186 * brief Clone the function from template of aes engine. 187 * param[in] alg The alg of Symmetric cipher. 188 * param[in] mode The work mode. 189 * retval On success, func is returned. On error, HI_NULL is returned. 190 */ 191 symc_func *cryp_get_symc_op(hi_cipher_alg alg, hi_cipher_work_mode mode); 192 #endif 193