1 /* 2 * Copyright (C) 2017-2019 Alibaba Group Holding Limited 3 */ 4 5 /****************************************************************************** 6 * @file drv_rsa.h 7 * @brief header file for rsa driver 8 * @version V1.0 9 * @date 02. June 2017 10 * @model rsa 11 ******************************************************************************/ 12 #ifndef _CSI_RSA_H_ 13 #define _CSI_RSA_H_ 14 15 16 #include <stdint.h> 17 #include <drv/common.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /// definition for rsa handle. 24 typedef void *rsa_handle_t; 25 26 /****** RSA specific error codes *****/ 27 typedef enum { 28 RSA_ERROR_DATA_BITS = (DRV_ERROR_SPECIFIC + 1), ///< Specified number of Data bits not supported 29 RSA_ERROR_ENDIAN ///< Specified endian not supported 30 } rsa_error_e; 31 32 /*----- RSA Control Codes: Mode Parameters: Data Bits -----*/ 33 typedef enum { 34 RSA_DATA_BITS_192 = 0, ///< 192 Data bits 35 RSA_DATA_BITS_256, ///< 256 Data bits 36 RSA_DATA_BITS_512, ///< 512 Data bits 37 RSA_DATA_BITS_1024, ///< 1024 Data bits 38 RSA_DATA_BITS_2048, ///< 2048 Data bits 39 RSA_DATA_BITS_3072 ///< 3072 Data bits 40 } rsa_data_bits_e; 41 42 /*----- RSA Control Codes: Mode Parameters: Endian -----*/ 43 typedef enum { 44 RSA_ENDIAN_MODE_LITTLE = 0, ///< RSA Little Endian Mode 45 RSA_ENDIAN_MODE_BIG ///< RSA Big Endian Mode 46 } rsa_endian_mode_e; 47 48 typedef enum { 49 RSA_PADDING_MODE_PKCS1 = 1, ///< RSA PKCS1 Padding Mode 50 RSA_PADDING_MODE_NO, ///< RSA NO Padding Mode 51 RSA_PADDING_MODE_SSLV23, ///< RSA SSLV23 Padding Mode 52 RSA_PADDING_MODE_PKCS1_OAEP, ///< RSA PKCS1 OAEP Padding Mode 53 RSA_PADDING_MODE_X931, ///< RSA X931 Padding Mode 54 RSA_PADDING_MODE_PSS ///< RSA PSS Padding Mode 55 } rsa_padding_type_e; 56 57 typedef enum { 58 RSA_HASH_TYPE_MD5 = 0, 59 RSA_HASH_TYPE_SHA1, 60 RSA_HASH_TYPE_SHA224, 61 RSA_HASH_TYPE_SHA256, 62 RSA_HASH_TYPE_SHA384, 63 RSA_HASH_TYPE_SHA512 64 } rsa_hash_type_e; 65 66 /*----- RSA Control Codes: Mode Parameters: Padding mode -----*/ 67 typedef struct { 68 rsa_padding_type_e padding_type; 69 rsa_hash_type_e hash_type; 70 } rsa_padding_t; 71 72 /** 73 \brief RSA Status 74 */ 75 typedef struct { 76 uint32_t busy : 1; ///< Calculate busy flag 77 } rsa_status_t; 78 79 /****** RSA Event *****/ 80 typedef enum { 81 RSA_EVENT_ENCRYPT_COMPLETE = 0, ///< Encrypt completed 82 RSA_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed 83 RSA_EVENT_SIGN_COMPLETE, ///< Sign completed 84 RSA_EVENT_VERIFY_COMPLETE, ///< Verify completed 85 } rsa_event_e; 86 87 typedef void (*rsa_event_cb_t)(int32_t idx, rsa_event_e event); ///< Pointer to \ref rsa_event_cb_t : RSA Event call back. 88 89 90 /** 91 \brief RSA Device Driver Capabilities. 92 */ 93 typedef struct { 94 uint32_t bits_192 : 1; ///< supports 192bits modular length 95 uint32_t bits_256 : 1; ///< supports 256bits modular length 96 uint32_t bits_512 : 1; ///< supports 512bits modular length 97 uint32_t bits_1024 : 1; ///< supports 1024bits modular length 98 uint32_t bits_2048 : 1; ///< supports 2048bits modular length 99 uint32_t bits_3072 : 1; ///< supports 30728bits modular length 100 } rsa_capabilities_t; 101 102 103 // Function documentation 104 105 /** 106 \brief Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function 107 \param[in] idx device id 108 \param[in] cb_event event callback function \ref rsa_event_cb_t 109 \return pointer to rsa handle 110 */ 111 rsa_handle_t csi_rsa_initialize(int32_t idx, rsa_event_cb_t cb_event); 112 113 /** 114 \brief De-initialize RSA Interface. stops operation and releases the software resources used by the interface 115 \param[in] handle rsa handle to operate. 116 \return error code 117 */ 118 int32_t csi_rsa_uninitialize(rsa_handle_t handle); 119 120 /** 121 \brief control rsa power. 122 \param[in] handle rsa handle to operate. 123 \param[in] state power state.\ref csi_power_stat_e. 124 \return error code 125 */ 126 int32_t csi_rsa_power_control(rsa_handle_t handle, csi_power_stat_e state); 127 128 /** 129 \brief Get driver capabilities. 130 \param[in] idx device id 131 \return \ref rsa_capabilities_t 132 */ 133 rsa_capabilities_t csi_rsa_get_capabilities(int32_t idx); 134 135 /** 136 \brief config rsa mode. 137 \param[in] handle rsa handle to operate. 138 \param[in] data_bits \ref rsa_data_bits_e 139 \param[in] endian \ref rsa_endian_mode_e 140 \param[in] arg the addr of modulus value 141 \return error code 142 */ 143 int32_t csi_rsa_config(rsa_handle_t handle, 144 rsa_data_bits_e data_bits, 145 rsa_endian_mode_e endian, 146 void *arg 147 ); 148 149 /** 150 \brief encrypt 151 \param[in] handle rsa handle to operate. 152 \param[in] n Pointer to the public modulus 153 \param[in] e Pointer to the public exponent 154 \param[in] src Pointer to the source data. 155 \param[in] src_size the source data len 156 \param[out] out Pointer to the result buffer 157 \param[out] out_size the result size 158 \param[in] padding \ref rsa_padding_t 159 \return error code 160 */ 161 int32_t csi_rsa_encrypt(rsa_handle_t handle, void *n, void *e, void *src, uint32_t src_size, void *out, uint32_t *out_size, rsa_padding_t padding); 162 163 164 /** 165 \brief decrypt 166 \param[in] handle rsa handle to operate. 167 \param[in] n Pointer to the public modulus 168 \param[in] d Pointer to the privte exponent 169 \param[in] src Pointer to the source data. 170 \param[in] src_size the source data len 171 \param[out] out Pointer to the result buffer 172 \param[out] out_size the result size 173 \param[in] padding \ref rsa_padding_t 174 \return error code 175 */ 176 int32_t csi_rsa_decrypt(rsa_handle_t handle, void *n, void *d, void *src, uint32_t src_size, void *out, uint32_t *out_size, rsa_padding_t padding); 177 178 /** 179 \brief rsa sign 180 \param[in] handle rsa handle to operate. 181 \param[in] n Pointer to the public modulus 182 \param[in] d Pointer to the privte exponent 183 \param[in] src Pointer to the source data. 184 \param[in] src_size the source data len 185 \param[out] signature Pointer to the signature 186 \param[out] sig_size the signature size 187 \param[in] padding \ref rsa_padding_t 188 \return error code 189 */ 190 int32_t csi_rsa_sign(rsa_handle_t handle, void *n, void *d, void *src, uint32_t src_size, void *signature, uint32_t *sig_size, rsa_padding_t padding); 191 192 /** 193 \brief rsa verify 194 \param[in] handle rsa handle to operate. 195 \param[in] n Pointer to the public modulus 196 \param[in] e Pointer to the public exponent 197 \param[in] src Pointer to the source data. 198 \param[in] src_size the source data len 199 \param[in] signature Pointer to the signature 200 \param[in] sig_size the signature size 201 \param[out] result Pointer to the result 202 \param[in] padding \ref rsa_padding_t 203 \return error code 204 */ 205 int32_t csi_rsa_verify(rsa_handle_t handle, void *n, void *e, void *src, uint32_t src_size, void *signature, uint32_t sig_size, void *result, rsa_padding_t padding); 206 /** 207 \brief Get RSA status. 208 \param[in] handle rsa handle to operate. 209 \return RSA status \ref rsa_status_t 210 */ 211 rsa_status_t csi_rsa_get_status(rsa_handle_t handle); 212 213 214 #ifdef __cplusplus 215 } 216 #endif 217 218 #endif /* _CSI_RSA_H_ */ 219