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 * SDF API is a cryptographic API for PCI-E cards defined in standard 12 * GM/T 0018-2012: Interface Specifications of Cryptography Device Application 13 * 14 * Note: this header file follows the specification of GM/T 0018-2012. As we 15 * know, some vendors provide header files with some differences, especially 16 * the definations of data structures. So be sure to check the file provided by 17 * vendors and compare with this one. 18 * 19 * The implementations of SDF API from different vendors might have different 20 * behaviors on the same function. The comments in this file will show 21 * information and warnings on these issues. If the application developer use 22 * the GmSSL implementation, see `crypto/gmapi/sdf_lcl.h` for more information. 23 */ 24 25 #ifndef HEADER_SDF_H 26 #define HEADER_SDF_H 27 28 #include <stdio.h> 29 #include "../sgd.h" 30 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 37 38 #pragma pack(1) 39 typedef struct DeviceInfo_st { 40 unsigned char IssuerName[40]; 41 unsigned char DeviceName[16]; 42 unsigned char DeviceSerial[16]; /* 8-char date + 43 * 3-char batch num + 44 * 5-char serial num 45 */ 46 unsigned int DeviceVersion; 47 unsigned int StandardVersion; 48 unsigned int AsymAlgAbility[2]; /* AsymAlgAbility[0] = algors 49 * AsymAlgAbility[1] = modulus lens 50 */ 51 unsigned int SymAlgAbility; 52 unsigned int HashAlgAbility; 53 unsigned int BufferSize; 54 } DEVICEINFO; 55 56 typedef struct RSArefPublicKey_st { 57 unsigned int bits; 58 unsigned char m[RSAref_MAX_LEN]; 59 unsigned char e[RSAref_MAX_LEN]; 60 } RSArefPublicKey; 61 62 typedef struct RSArefPrivateKey_st { 63 unsigned int bits; 64 unsigned char m[RSAref_MAX_LEN]; 65 unsigned char e[RSAref_MAX_LEN]; 66 unsigned char d[RSAref_MAX_LEN]; 67 unsigned char prime[2][RSAref_MAX_PLEN]; 68 unsigned char pexp[2][RSAref_MAX_PLEN]; 69 unsigned char coef[RSAref_MAX_PLEN]; 70 } RSArefPrivateKey; 71 72 typedef struct ECCrefPublicKey_st { 73 unsigned int bits; 74 unsigned char x[ECCref_MAX_LEN]; 75 unsigned char y[ECCref_MAX_LEN]; 76 } ECCrefPublicKey; 77 78 typedef struct ECCrefPrivateKey_st { 79 unsigned int bits; 80 unsigned char K[ECCref_MAX_LEN]; 81 } ECCrefPrivateKey; 82 83 typedef struct ECCCipher_st { 84 unsigned char x[ECCref_MAX_LEN]; 85 unsigned char y[ECCref_MAX_LEN]; 86 unsigned char M[32]; 87 unsigned int L; 88 unsigned char C[1]; 89 } ECCCipher; 90 91 typedef struct ECCSignature_st { 92 unsigned char r[ECCref_MAX_LEN]; 93 unsigned char s[ECCref_MAX_LEN]; 94 } ECCSignature; 95 96 typedef struct SDF_ENVELOPEDKEYBLOB { 97 unsigned long Version; 98 unsigned long ulSymmAlgID; 99 ECCCipher ECCCipehrBlob; 100 ECCrefPublicKey PubKey; 101 unsigned char cbEncryptedPrivKey[64]; 102 } EnvelopedKeyBlob, *PEnvelopedKeyBlob; 103 #pragma pack() 104 105 int SDF_OpenDevice( 106 void **phDeviceHandle); 107 108 int SDF_CloseDevice( 109 void *hDeviceHandle); 110 111 int SDF_OpenSession( 112 void *hDeviceHandle, 113 void **phSessionHandle); 114 115 int SDF_CloseSession( 116 void *hSessionHandle); 117 118 int SDF_GetDeviceInfo( 119 void *hSessionHandle, 120 DEVICEINFO *pstDeviceInfo); 121 122 int SDF_GenerateRandom( 123 void *hSessionHandle, 124 unsigned int uiLength, 125 unsigned char *pucRandom); 126 127 int SDF_GetPrivateKeyAccessRight( 128 void *hSessionHandle, 129 unsigned int uiKeyIndex, 130 unsigned char *pucPassword, 131 unsigned int uiPwdLength); 132 133 int SDF_ReleasePrivateKeyAccessRight( 134 void *hSessionHandle, 135 unsigned int uiKeyIndex); 136 137 int SDF_ExportSignPublicKey_RSA( 138 void *hSessionHandle, 139 unsigned int uiKeyIndex, 140 RSArefPublicKey *pucPublicKey); 141 142 int SDF_ExportEncPublicKey_RSA( 143 void *hSessionHandle, 144 unsigned int uiKeyIndex, 145 RSArefPublicKey *pucPublicKey); 146 147 int SDF_GenerateKeyPair_RSA( 148 void *hSessionHandle, 149 unsigned int uiKeyBits, 150 RSArefPublicKey *pucPublicKey, 151 RSArefPrivateKey *pucPrivateKey); 152 153 int SDF_GenerateKeyWithIPK_RSA( 154 void *hSessionHandle, 155 unsigned int uiIPKIndex, 156 unsigned int uiKeyBits, 157 unsigned char *pucKey, 158 unsigned int *puiKeyLength, 159 void **phKeyHandle); 160 161 int SDF_GenerateKeyWithEPK_RSA( 162 void *hSessionHandle, 163 unsigned int uiKeyBits, 164 RSArefPublicKey *pucPublicKey, 165 unsigned char *pucKey, 166 unsigned int *puiKeyLength, 167 void **phKeyHandle); 168 169 int SDF_ImportKeyWithISK_RSA( 170 void *hSessionHandle, 171 unsigned int uiISKIndex, 172 unsigned char *pucKey, 173 unsigned int uiKeyLength, 174 void **phKeyHandle); 175 176 int SDF_ExchangeDigitEnvelopeBaseOnRSA( 177 void *hSessionHandle, 178 unsigned int uiKeyIndex, 179 RSArefPublicKey *pucPublicKey, 180 unsigned char *pucDEInput, 181 unsigned int uiDELength, 182 unsigned char *pucDEOutput, 183 unsigned int *puiDELength); 184 185 int SDF_ExportSignPublicKey_ECC( 186 void *hSessionHandle, 187 unsigned int uiKeyIndex, 188 ECCrefPublicKey *pucPublicKey); 189 190 int SDF_ExportEncPublicKey_ECC( 191 void *hSessionHandle, 192 unsigned int uiKeyIndex, 193 ECCrefPublicKey *pucPublicKey); 194 195 int SDF_GenerateKeyPair_ECC( 196 void *hSessionHandle, 197 unsigned int uiAlgID, 198 unsigned int uiKeyBits, 199 ECCrefPublicKey *pucPublicKey, 200 ECCrefPrivateKey *pucPrivateKey); 201 202 int SDF_GenerateKeyWithIPK_ECC( 203 void *hSessionHandle, 204 unsigned int uiIPKIndex, 205 unsigned int uiKeyBits, 206 ECCCipher *pucKey, 207 void **phKeyHandle); 208 209 int SDF_GenerateKeyWithEPK_ECC( 210 void *hSessionHandle, 211 unsigned int uiKeyBits, 212 unsigned int uiAlgID, 213 ECCrefPublicKey *pucPublicKey, 214 ECCCipher *pucKey, 215 void **phKeyHandle); 216 217 int SDF_ImportKeyWithISK_ECC( 218 void *hSessionHandle, 219 unsigned int uiISKIndex, 220 ECCCipher *pucKey, 221 void **phKeyHandle); 222 223 int SDF_GenerateAgreementDataWithECC( 224 void *hSessionHandle, 225 unsigned int uiISKIndex, 226 unsigned int uiKeyBits, 227 unsigned char *pucSponsorID, 228 unsigned int uiSponsorIDLength, 229 ECCrefPublicKey *pucSponsorPublicKey, 230 ECCrefPublicKey *pucSponsorTmpPublicKey, 231 void **phAgreementHandle); 232 233 int SDF_GenerateKeyWithECC( 234 void *hSessionHandle, 235 unsigned char *pucResponseID, 236 unsigned int uiResponseIDLength, 237 ECCrefPublicKey *pucResponsePublicKey, 238 ECCrefPublicKey *pucResponseTmpPublicKey, 239 void *hAgreementHandle, 240 void **phKeyHandle); 241 242 int SDF_GenerateAgreementDataAndKeyWithECC( 243 void *hSessionHandle, 244 unsigned int uiISKIndex, 245 unsigned int uiKeyBits, 246 unsigned char *pucResponseID, 247 unsigned int uiResponseIDLength, 248 unsigned char *pucSponsorID, 249 unsigned int uiSponsorIDLength, 250 ECCrefPublicKey *pucSponsorPublicKey, 251 ECCrefPublicKey *pucSponsorTmpPublicKey, 252 ECCrefPublicKey *pucResponsePublicKey, 253 ECCrefPublicKey *pucResponseTmpPublicKey, 254 void **phKeyHandle); 255 256 int SDF_ExchangeDigitEnvelopeBaseOnECC( 257 void *hSessionHandle, 258 unsigned int uiKeyIndex, 259 unsigned int uiAlgID, 260 ECCrefPublicKey *pucPublicKey, 261 ECCCipher *pucEncDataIn, 262 ECCCipher *pucEncDataOut); 263 264 int SDF_GenerateKeyWithKEK( 265 void *hSessionHandle, 266 unsigned int uiKeyBits, 267 unsigned int uiAlgID, 268 unsigned int uiKEKIndex, 269 unsigned char *pucKey, 270 unsigned int *puiKeyLength, 271 void **phKeyHandle); 272 273 int SDF_ImportKeyWithKEK( 274 void *hSessionHandle, 275 unsigned int uiAlgID, 276 unsigned int uiKEKIndex, 277 unsigned char *pucKey, 278 unsigned int uiKeyLength, 279 void **phKeyHandle); 280 281 int SDF_DestroyKey( 282 void *hSessionHandle, 283 void *hKeyHandle); 284 285 int SDF_ExternalPublicKeyOperation_RSA( 286 void *hSessionHandle, 287 RSArefPublicKey *pucPublicKey, 288 unsigned char *pucDataInput, 289 unsigned int uiInputLength, 290 unsigned char *pucDataOutput, 291 unsigned int *puiOutputLength); 292 293 int SDF_InternalPublicKeyOperation_RSA( 294 void *hSessionHandle, 295 unsigned int uiKeyIndex, 296 unsigned char *pucDataInput, 297 unsigned int uiInputLength, 298 unsigned char *pucDataOutput, 299 unsigned int *puiOutputLength); 300 301 int SDF_InternalPrivateKeyOperation_RSA( 302 void *hSessionHandle, 303 unsigned int uiKeyIndex, 304 unsigned char *pucDataInput, 305 unsigned int uiInputLength, 306 unsigned char *pucDataOutput, 307 unsigned int *puiOutputLength); 308 309 int SDF_ExternalVerify_ECC( 310 void *hSessionHandle, 311 unsigned int uiAlgID, 312 ECCrefPublicKey *pucPublicKey, 313 unsigned char *pucDataInput, 314 unsigned int uiInputLength, 315 ECCSignature *pucSignature); 316 317 int SDF_InternalSign_ECC( 318 void *hSessionHandle, 319 unsigned int uiISKIndex, 320 unsigned char *pucData, 321 unsigned int uiDataLength, 322 ECCSignature *pucSignature); 323 324 int SDF_InternalVerify_ECC( 325 void *hSessionHandle, 326 unsigned int uiIPKIndex, 327 unsigned char *pucData, 328 unsigned int uiDataLength, 329 ECCSignature *pucSignature); 330 331 int SDF_ExternalEncrypt_ECC( 332 void *hSessionHandle, 333 unsigned int uiAlgID, 334 ECCrefPublicKey *pucPublicKey, 335 unsigned char *pucData, 336 unsigned int uiDataLength, 337 ECCCipher *pucEncData); 338 339 int SDF_InternalEncrypt_ECC( 340 void *hSessionHandle, 341 unsigned int uiIPKIndex, 342 unsigned int uiAlgID, 343 unsigned char *pucData, 344 unsigned int uiDataLength, 345 ECCCipher *pucEncData); 346 347 int SDF_InternalDecrypt_ECC( 348 void *hSessionHandle, 349 unsigned int uiISKIndex, 350 unsigned int uiAlgID, 351 ECCCipher *pucEncData, 352 unsigned char *pucData, 353 unsigned int *uiDataLength); 354 355 int SDF_Encrypt( 356 void *hSessionHandle, 357 void *hKeyHandle, 358 unsigned int uiAlgID, 359 unsigned char *pucIV, 360 unsigned char *pucData, 361 unsigned int uiDataLength, 362 unsigned char *pucEncData, 363 unsigned int *puiEncDataLength); 364 365 int SDF_Decrypt( 366 void *hSessionHandle, 367 void *hKeyHandle, 368 unsigned int uiAlgID, 369 unsigned char *pucIV, 370 unsigned char *pucEncData, 371 unsigned int uiEncDataLength, 372 unsigned char *pucData, 373 unsigned int *puiDataLength); 374 375 int SDF_CalculateMAC( 376 void *hSessionHandle, 377 void *hKeyHandle, 378 unsigned int uiAlgID, 379 unsigned char *pucIV, 380 unsigned char *pucData, 381 unsigned int uiDataLength, 382 unsigned char *pucMAC, 383 unsigned int *puiMACLength); 384 385 int SDF_HashInit( 386 void *hSessionHandle, 387 unsigned int uiAlgID, 388 ECCrefPublicKey *pucPublicKey, 389 unsigned char *pucID, 390 unsigned int uiIDLength); 391 392 int SDF_HashUpdate( 393 void *hSessionHandle, 394 unsigned char *pucData, 395 unsigned int uiDataLength); 396 397 int SDF_HashFinal(void *hSessionHandle, 398 unsigned char *pucHash, 399 unsigned int *puiHashLength); 400 401 int SDF_CreateFile( 402 void *hSessionHandle, 403 unsigned char *pucFileName, 404 unsigned int uiNameLen, /* max 128-byte */ 405 unsigned int uiFileSize); 406 407 int SDF_ReadFile( 408 void *hSessionHandle, 409 unsigned char *pucFileName, 410 unsigned int uiNameLen, 411 unsigned int uiOffset, 412 unsigned int *puiReadLength, 413 unsigned char *pucBuffer); 414 415 int SDF_WriteFile( 416 void *hSessionHandle, 417 unsigned char *pucFileName, 418 unsigned int uiNameLen, 419 unsigned int uiOffset, 420 unsigned int uiWriteLength, 421 unsigned char *pucBuffer); 422 423 int SDF_DeleteFile( 424 void *hSessionHandle, 425 unsigned char *pucFileName, 426 unsigned int uiNameLen); 427 428 #define SDR_OK 0x0 429 #define SDR_BASE 0x01000000 430 #define SDR_UNKNOWERR (SDR_BASE + 0x00000001) 431 #define SDR_NOTSUPPORT (SDR_BASE + 0x00000002) 432 #define SDR_COMMFAIL (SDR_BASE + 0x00000003) 433 #define SDR_HARDFAIL (SDR_BASE + 0x00000004) 434 #define SDR_OPENDEVICE (SDR_BASE + 0x00000005) 435 #define SDR_OPENSESSION (SDR_BASE + 0x00000006) 436 #define SDR_PARDENY (SDR_BASE + 0x00000007) 437 #define SDR_KEYNOTEXIST (SDR_BASE + 0x00000008) 438 #define SDR_ALGNOTSUPPORT (SDR_BASE + 0x00000009) 439 #define SDR_ALGMODNOTSUPPORT (SDR_BASE + 0x0000000A) 440 #define SDR_PKOPERR (SDR_BASE + 0x0000000B) 441 #define SDR_SKOPERR (SDR_BASE + 0x0000000C) 442 #define SDR_SIGNERR (SDR_BASE + 0x0000000D) 443 #define SDR_VERIFYERR (SDR_BASE + 0x0000000E) 444 #define SDR_SYMOPERR (SDR_BASE + 0x0000000F) 445 #define SDR_STEPERR (SDR_BASE + 0x00000010) 446 #define SDR_FILESIZEERR (SDR_BASE + 0x00000011) 447 #define SDR_FILENOEXIST (SDR_BASE + 0x00000012) 448 #define SDR_FILEOFSERR (SDR_BASE + 0x00000013) 449 #define SDR_KEYTYPEERR (SDR_BASE + 0x00000014) 450 #define SDR_KEYERR (SDR_BASE + 0x00000015) 451 #define SDR_ENCDATAERR (SDR_BASE + 0x00000016) 452 #define SDR_RANDERR (SDR_BASE + 0x00000017) 453 #define SDR_PRKRERR (SDR_BASE + 0x00000018) 454 #define SDR_MACERR (SDR_BASE + 0x00000019) 455 #define SDR_FILEEXSITS (SDR_BASE + 0x0000001A) 456 #define SDR_FILEWERR (SDR_BASE + 0x0000001B) 457 #define SDR_NOBUFFER (SDR_BASE + 0x0000001C) 458 #define SDR_INARGERR (SDR_BASE + 0x0000001D) 459 #define SDR_OUTARGERR (SDR_BASE + 0x0000001E) 460 461 462 #ifdef __cplusplus 463 } 464 #endif 465 #endif 466