1 /* 2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); you may 5 * not use this file except in compliance with the License. 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 */ 9 10 11 #ifndef SDFUTIL_SDF_METH_H 12 #define SDFUTIL_SDF_METH_H 13 14 #include "sdf.h" 15 16 typedef int (*SDF_OpenDevice_FuncPtr)( 17 void **phDeviceHandle); 18 19 typedef int (*SDF_CloseDevice_FuncPtr)( 20 void *hDeviceHandle); 21 22 typedef int (*SDF_OpenSession_FuncPtr)( 23 void *hDeviceHandle, 24 void **phSessionHandle); 25 26 typedef int (*SDF_CloseSession_FuncPtr)( 27 void *hSessionHandle); 28 29 typedef int (*SDF_GetDeviceInfo_FuncPtr)( 30 void *hSessionHandle, 31 DEVICEINFO *pstDeviceInfo); 32 33 typedef int (*SDF_GenerateRandom_FuncPtr)( 34 void *hSessionHandle, 35 unsigned int uiLength, 36 unsigned char *pucRandom); 37 38 typedef int (*SDF_GetPrivateKeyAccessRight_FuncPtr)( 39 void *hSessionHandle, 40 unsigned int uiKeyIndex, 41 unsigned char *pucPassword, 42 unsigned int uiPwdLength); 43 44 typedef int (*SDF_ReleasePrivateKeyAccessRight_FuncPtr)( 45 void *hSessionHandle, 46 unsigned int uiKeyIndex); 47 48 typedef int (*SDF_ExportSignPublicKey_RSA_FuncPtr)( 49 void *hSessionHandle, 50 unsigned int uiKeyIndex, 51 RSArefPublicKey *pucPublicKey); 52 53 typedef int (*SDF_ExportEncPublicKey_RSA_FuncPtr)( 54 void *hSessionHandle, 55 unsigned int uiKeyIndex, 56 RSArefPublicKey *pucPublicKey); 57 58 typedef int (*SDF_GenerateKeyPair_RSA_FuncPtr)( 59 void *hSessionHandle, 60 unsigned int uiKeyBits, 61 RSArefPublicKey *pucPublicKey, 62 RSArefPrivateKey *pucPrivateKey); 63 64 typedef int (*SDF_GenerateKeyWithIPK_RSA_FuncPtr)( 65 void *hSessionHandle, 66 unsigned int uiIPKIndex, 67 unsigned int uiKeyBits, 68 unsigned char *pucKey, 69 unsigned int *puiKeyLength, 70 void **phKeyHandle); 71 72 typedef int (*SDF_GenerateKeyWithEPK_RSA_FuncPtr)( 73 void *hSessionHandle, 74 unsigned int uiKeyBits, 75 RSArefPublicKey *pucPublicKey, 76 unsigned char *pucKey, 77 unsigned int *puiKeyLength, 78 void **phKeyHandle); 79 80 typedef int (*SDF_ImportKeyWithISK_RSA_FuncPtr)( 81 void *hSessionHandle, 82 unsigned int uiISKIndex, 83 unsigned char *pucKey, 84 unsigned int uiKeyLength, 85 void **phKeyHandle); 86 87 typedef int (*SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr)( 88 void *hSessionHandle, 89 unsigned int uiKeyIndex, 90 RSArefPublicKey *pucPublicKey, 91 unsigned char *pucDEInput, 92 unsigned int uiDELength, 93 unsigned char *pucDEOutput, 94 unsigned int *puiDELength); 95 96 typedef int (*SDF_ExportSignPublicKey_ECC_FuncPtr)( 97 void *hSessionHandle, 98 unsigned int uiKeyIndex, 99 ECCrefPublicKey *pucPublicKey); 100 101 typedef int (*SDF_ExportEncPublicKey_ECC_FuncPtr)( 102 void *hSessionHandle, 103 unsigned int uiKeyIndex, 104 ECCrefPublicKey *pucPublicKey); 105 106 typedef int (*SDF_GenerateKeyPair_ECC_FuncPtr)( 107 void *hSessionHandle, 108 unsigned int uiAlgID, 109 unsigned int uiKeyBits, 110 ECCrefPublicKey *pucPublicKey, 111 ECCrefPrivateKey *pucPrivateKey); 112 113 typedef int (*SDF_GenerateKeyWithIPK_ECC_FuncPtr)( 114 void *hSessionHandle, 115 unsigned int uiIPKIndex, 116 unsigned int uiKeyBits, 117 ECCCipher *pucKey, 118 void **phKeyHandle); 119 120 typedef int (*SDF_GenerateKeyWithEPK_ECC_FuncPtr)( 121 void *hSessionHandle, 122 unsigned int uiKeyBits, 123 unsigned int uiAlgID, 124 ECCrefPublicKey *pucPublicKey, 125 ECCCipher *pucKey, 126 void **phKeyHandle); 127 128 typedef int (*SDF_ImportKeyWithISK_ECC_FuncPtr)( 129 void *hSessionHandle, 130 unsigned int uiISKIndex, 131 ECCCipher *pucKey, 132 void **phKeyHandle); 133 134 typedef int (*SDF_GenerateAgreementDataWithECC_FuncPtr)( 135 void *hSessionHandle, 136 unsigned int uiISKIndex, 137 unsigned int uiKeyBits, 138 unsigned char *pucSponsorID, 139 unsigned int uiSponsorIDLength, 140 ECCrefPublicKey *pucSponsorPublicKey, 141 ECCrefPublicKey *pucSponsorTmpPublicKey, 142 void **phAgreementHandle); 143 144 typedef int (*SDF_GenerateKeyWithECC_FuncPtr)( 145 void *hSessionHandle, 146 unsigned char *pucResponseID, 147 unsigned int uiResponseIDLength, 148 ECCrefPublicKey *pucResponsePublicKey, 149 ECCrefPublicKey *pucResponseTmpPublicKey, 150 void *hAgreementHandle, 151 void **phKeyHandle); 152 153 typedef int (*SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr)( 154 void *hSessionHandle, 155 unsigned int uiISKIndex, 156 unsigned int uiKeyBits, 157 unsigned char *pucResponseID, 158 unsigned int uiResponseIDLength, 159 unsigned char *pucSponsorID, 160 unsigned int uiSponsorIDLength, 161 ECCrefPublicKey *pucSponsorPublicKey, 162 ECCrefPublicKey *pucSponsorTmpPublicKey, 163 ECCrefPublicKey *pucResponsePublicKey, 164 ECCrefPublicKey *pucResponseTmpPublicKey, 165 void **phKeyHandle); 166 167 typedef int (*SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr)( 168 void *hSessionHandle, 169 unsigned int uiKeyIndex, 170 unsigned int uiAlgID, 171 ECCrefPublicKey *pucPublicKey, 172 ECCCipher *pucEncDataIn, 173 ECCCipher *pucEncDataOut); 174 175 typedef int (*SDF_GenerateKeyWithKEK_FuncPtr)( 176 void *hSessionHandle, 177 unsigned int uiKeyBits, 178 unsigned int uiAlgID, 179 unsigned int uiKEKIndex, 180 unsigned char *pucKey, 181 unsigned int *puiKeyLength, 182 void **phKeyHandle); 183 184 typedef int (*SDF_ImportKeyWithKEK_FuncPtr)( 185 void *hSessionHandle, 186 unsigned int uiAlgID, 187 unsigned int uiKEKIndex, 188 unsigned char *pucKey, 189 unsigned int uiKeyLength, 190 void **phKeyHandle); 191 192 typedef int (*SDF_DestroyKey_FuncPtr)( 193 void *hSessionHandle, 194 void *hKeyHandle); 195 196 typedef int (*SDF_ExternalPublicKeyOperation_RSA_FuncPtr)( 197 void *hSessionHandle, 198 RSArefPublicKey *pucPublicKey, 199 unsigned char *pucDataInput, 200 unsigned int uiInputLength, 201 unsigned char *pucDataOutput, 202 unsigned int *puiOutputLength); 203 204 typedef int (*SDF_InternalPublicKeyOperation_RSA_FuncPtr)( 205 void *hSessionHandle, 206 unsigned int uiKeyIndex, 207 unsigned char *pucDataInput, 208 unsigned int uiInputLength, 209 unsigned char *pucDataOutput, 210 unsigned int *puiOutputLength); 211 212 typedef int (*SDF_InternalPrivateKeyOperation_RSA_FuncPtr)( 213 void *hSessionHandle, 214 unsigned int uiKeyIndex, 215 unsigned char *pucDataInput, 216 unsigned int uiInputLength, 217 unsigned char *pucDataOutput, 218 unsigned int *puiOutputLength); 219 220 typedef int (*SDF_ExternalVerify_ECC_FuncPtr)( 221 void *hSessionHandle, 222 unsigned int uiAlgID, 223 ECCrefPublicKey *pucPublicKey, 224 unsigned char *pucDataInput, 225 unsigned int uiInputLength, 226 ECCSignature *pucSignature); 227 228 typedef int (*SDF_InternalSign_ECC_FuncPtr)( 229 void *hSessionHandle, 230 unsigned int uiISKIndex, 231 unsigned char *pucData, 232 unsigned int uiDataLength, 233 ECCSignature *pucSignature); 234 235 typedef int (*SDF_InternalVerify_ECC_FuncPtr)( 236 void *hSessionHandle, 237 unsigned int uiIPKIndex, 238 unsigned char *pucData, 239 unsigned int uiDataLength, 240 ECCSignature *pucSignature); 241 242 typedef int (*SDF_ExternalEncrypt_ECC_FuncPtr)( 243 void *hSessionHandle, 244 unsigned int uiAlgID, 245 ECCrefPublicKey *pucPublicKey, 246 unsigned char *pucData, 247 unsigned int uiDataLength, 248 ECCCipher *pucEncData); 249 250 typedef int (*SDF_ExternalDecrypt_ECC_FuncPtr)( 251 void *hSessionHandle, 252 unsigned int uiAlgID, 253 ECCrefPrivateKey *pucPrivateKey, 254 ECCCipher *pucEncData, 255 unsigned char *pucData, 256 unsigned int *puiDataLength); 257 258 typedef int (*SDF_InternalEncrypt_ECC_FuncPtr)( 259 void *hSessionHandle, 260 unsigned int uiIPKIndex, 261 unsigned int uiAlgID, 262 unsigned char *pucData, 263 unsigned int uiDataLength, 264 ECCCipher *pucEncData); 265 266 typedef int (*SDF_InternalDecrypt_ECC_FuncPtr)( 267 void *hSessionHandle, 268 unsigned int uiISKIndex, 269 unsigned int uiAlgID, 270 ECCCipher *pucEncData, 271 unsigned char *pucData, 272 unsigned int *puiDataLength); 273 274 typedef int (*SDF_Encrypt_FuncPtr)( 275 void *hSessionHandle, 276 void *hKeyHandle, 277 unsigned int uiAlgID, 278 unsigned char *pucIV, 279 unsigned char *pucData, 280 unsigned int uiDataLength, 281 unsigned char *pucEncData, 282 unsigned int *puiEncDataLength); 283 284 typedef int (*SDF_Decrypt_FuncPtr)( 285 void *hSessionHandle, 286 void *hKeyHandle, 287 unsigned int uiAlgID, 288 unsigned char *pucIV, 289 unsigned char *pucEncData, 290 unsigned int uiEncDataLength, 291 unsigned char *pucData, 292 unsigned int *puiDataLength); 293 294 typedef int (*SDF_CalculateMAC_FuncPtr)( 295 void *hSessionHandle, 296 void *hKeyHandle, 297 unsigned int uiAlgID, 298 unsigned char *pucIV, 299 unsigned char *pucData, 300 unsigned int uiDataLength, 301 unsigned char *pucMAC, 302 unsigned int *puiMACLength); 303 304 typedef int (*SDF_HashInit_FuncPtr)( 305 void *hSessionHandle, 306 unsigned int uiAlgID, 307 ECCrefPublicKey *pucPublicKey, 308 unsigned char *pucID, 309 unsigned int uiIDLength); 310 311 typedef int (*SDF_HashUpdate_FuncPtr)( 312 void *hSessionHandle, 313 unsigned char *pucData, 314 unsigned int uiDataLength); 315 316 typedef int (*SDF_HashFinal_FuncPtr)(void *hSessionHandle, 317 unsigned char *pucHash, 318 unsigned int *puiHashLength); 319 320 typedef int (*SDF_CreateObject_FuncPtr)( 321 void *hSessionHandle, 322 unsigned char *pucFileName, 323 unsigned int uiNameLen, 324 unsigned int uiFileSize); 325 326 typedef int (*SDF_ReadObject_FuncPtr)( 327 void *hSessionHandle, 328 unsigned char *pucFileName, 329 unsigned int uiNameLen, 330 unsigned int uiOffset, 331 unsigned int *puiReadLength, 332 unsigned char *pucBuffer); 333 334 typedef int (*SDF_WriteObject_FuncPtr)( 335 void *hSessionHandle, 336 unsigned char *pucFileName, 337 unsigned int uiNameLen, 338 unsigned int uiOffset, 339 unsigned int uiWriteLength, 340 unsigned char *pucBuffer); 341 342 typedef int (*SDF_DeleteObject_FuncPtr)( 343 void *hSessionHandle, 344 unsigned char *pucFileName, 345 unsigned int uiNameLen); 346 347 typedef struct sdf_method_st { 348 char *name; 349 void *dso; 350 SDF_OpenDevice_FuncPtr OpenDevice; 351 SDF_CloseDevice_FuncPtr CloseDevice; 352 SDF_OpenSession_FuncPtr OpenSession; 353 SDF_CloseSession_FuncPtr CloseSession; 354 SDF_GetDeviceInfo_FuncPtr GetDeviceInfo; 355 SDF_GenerateRandom_FuncPtr GenerateRandom; 356 SDF_GetPrivateKeyAccessRight_FuncPtr GetPrivateKeyAccessRight; 357 SDF_ReleasePrivateKeyAccessRight_FuncPtr ReleasePrivateKeyAccessRight; 358 SDF_ExportSignPublicKey_RSA_FuncPtr ExportSignPublicKey_RSA; 359 SDF_ExportEncPublicKey_RSA_FuncPtr ExportEncPublicKey_RSA; 360 SDF_GenerateKeyPair_RSA_FuncPtr GenerateKeyPair_RSA; 361 SDF_GenerateKeyWithIPK_RSA_FuncPtr GenerateKeyWithIPK_RSA; 362 SDF_GenerateKeyWithEPK_RSA_FuncPtr GenerateKeyWithEPK_RSA; 363 SDF_ImportKeyWithISK_RSA_FuncPtr ImportKeyWithISK_RSA; 364 SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr ExchangeDigitEnvelopeBaseOnRSA; 365 SDF_ExportSignPublicKey_ECC_FuncPtr ExportSignPublicKey_ECC; 366 SDF_ExportEncPublicKey_ECC_FuncPtr ExportEncPublicKey_ECC; 367 SDF_GenerateKeyPair_ECC_FuncPtr GenerateKeyPair_ECC; 368 SDF_GenerateKeyWithIPK_ECC_FuncPtr GenerateKeyWithIPK_ECC; 369 SDF_GenerateKeyWithEPK_ECC_FuncPtr GenerateKeyWithEPK_ECC; 370 SDF_ImportKeyWithISK_ECC_FuncPtr ImportKeyWithISK_ECC; 371 SDF_GenerateAgreementDataWithECC_FuncPtr GenerateAgreementDataWithECC; 372 SDF_GenerateKeyWithECC_FuncPtr GenerateKeyWithECC; 373 SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr GenerateAgreementDataAndKeyWithECC; 374 SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr ExchangeDigitEnvelopeBaseOnECC; 375 SDF_GenerateKeyWithKEK_FuncPtr GenerateKeyWithKEK; 376 SDF_ImportKeyWithKEK_FuncPtr ImportKeyWithKEK; 377 SDF_DestroyKey_FuncPtr DestroyKey; 378 SDF_ExternalPublicKeyOperation_RSA_FuncPtr ExternalPublicKeyOperation_RSA; 379 SDF_InternalPublicKeyOperation_RSA_FuncPtr InternalPublicKeyOperation_RSA; 380 SDF_InternalPrivateKeyOperation_RSA_FuncPtr InternalPrivateKeyOperation_RSA; 381 SDF_ExternalVerify_ECC_FuncPtr ExternalVerify_ECC; 382 SDF_InternalSign_ECC_FuncPtr InternalSign_ECC; 383 SDF_InternalVerify_ECC_FuncPtr InternalVerify_ECC; 384 SDF_ExternalEncrypt_ECC_FuncPtr ExternalEncrypt_ECC; 385 SDF_ExternalDecrypt_ECC_FuncPtr ExternalDecrypt_ECC; 386 SDF_InternalEncrypt_ECC_FuncPtr InternalEncrypt_ECC; 387 SDF_InternalDecrypt_ECC_FuncPtr InternalDecrypt_ECC; 388 SDF_Encrypt_FuncPtr Encrypt; 389 SDF_Decrypt_FuncPtr Decrypt; 390 SDF_CalculateMAC_FuncPtr CalculateMAC; 391 SDF_HashInit_FuncPtr HashInit; 392 SDF_HashUpdate_FuncPtr HashUpdate; 393 SDF_HashFinal_FuncPtr HashFinal; 394 SDF_CreateObject_FuncPtr CreateObject; 395 SDF_ReadObject_FuncPtr ReadObject; 396 SDF_WriteObject_FuncPtr WriteObject; 397 SDF_DeleteObject_FuncPtr DeleteObject; 398 } SDF_METHOD; 399 400 SDF_METHOD *SDF_METHOD_load_library(const char *so_path); 401 void SDF_METHOD_free(SDF_METHOD *meth); 402 403 404 typedef struct sdf_vendor_st { 405 char *name; 406 unsigned int (*cipher_vendor2std)(unsigned int vendor_id); 407 unsigned int (*cipher_std2vendor)(unsigned int std_id); 408 unsigned int (*cipher_cap)(unsigned int vendor_cap); 409 unsigned int (*digest_vendor2std)(unsigned int vendor_id); 410 unsigned int (*digest_std2vendor)(unsigned int std_id); 411 unsigned int (*digest_cap)(unsigned int vendor_cap); 412 unsigned int (*pkey_vendor2std)(unsigned int vendor_id); 413 unsigned int (*pkey_std2vendor)(unsigned int std_id); 414 unsigned int (*pkey_cap)(unsigned int vendor_cap); 415 int (*encode_ecccipher)(const ECCCipher *a, void *buf); 416 int (*decode_ecccipher)(ECCCipher *a, const void *buf); 417 unsigned long (*get_error_reason)(int err); 418 } SDF_VENDOR; 419 420 421 #endif 422