gitee.com/lh-her-team/common@v1.5.1/opencrypto/gmssl/gmssl/include/openssl/sdf.h (about)

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