sm9接口

/* crypto/sm9/sm9.h */
/**
 * \file   crypto/sm9/sm9.h
 * \author [email protected]
 *
 * This product includes cryptographic software written by Eric Young
 * ([email protected]).  This product includes software written by Tim
 * Hudson ([email protected]).
 *
 */

#ifndef HEADER_SM9_H
#define HEADER_SM9_H

#include <openssl/ec.h>
#include <openssl/bn.h>

# ifdef  __cplusplus
extern "C" {
# endif

#define SM9_ASK_MEMORY_ERR          0x00000001 //申請內存失敗
#define SM9_H_OUTRANGE              0x00000002 //簽名 H 不屬於[1,N-1]
#define SM9_DATA_MEMCMP_ERR         0x00000003 //數據對比不一致
#define SM9_MEMBER_ERR              0x00000004 //羣的階錯誤
#define SM9_MY_ECAP_12A_ERR         0x00000005 //R-ate 對計算出現錯誤
#define SM9_S_NOT_VALID_G1          0x00000006 //S 不屬於羣 G1
#define SM9_G1BASEPOINT_SET_ERR     0x00000007 //G1 基點設置錯誤
#define SM9_G2BASEPOINT_SET_ERR     0x00000008 //G2 基點設置錯誤
#define SM9_L_ERR                   0x00000009 //參數 L 錯誤
#define SM9_GEPUB_ERR               0x0000000A //生成公鑰錯誤
#define SM9_GEPRI_ERR               0x0000000B //生成私鑰錯誤
#define SM9_SIGN_ERR                0x0000000C //簽名錯誤
#define SM9_NOT_VALID_G1            0x0000000D //C1 不屬於羣 G1
#define SM9_ENCRYPT_ERR             0x0000000E //加密錯誤
#define SM9_ERR_K1_ZERO             0x0000000F //K1 全 0
#define SM9_C3_MEMCMP_ERR           0x00000010 //C3 比對不一致
#define SM9_DECRYPT_ERR             0x00000011 //解密錯誤
#define SM9_ERR_Encap_C             0x00000012 //cipher error in key encapsulation
#define SM9_ERR_Encap_K             0x00000013 //key to be encapsulated
#define SM9_ERR_Decap_K             0x00000014 //key generated by decapsulation
#define SM9_ERR_CMP_S1SB            0x00000015 //S1!=SB
#define SM9_ERR_CMP_S2SA            0x00000016 //S2!=SA
#define SM9_ERR_RA                  0x00000017 //RA error
#define SM9_ERR_RB                  0x00000018 //RB error
#define SM9_ERR_SA                  0x00000019 //SA error
#define SM9_ERR_SB                  0x0000001A //SB error

typedef struct _FP2 {
    BIGNUM a;
    BIGNUM b;
} FP2;

typedef struct _FP4 {
    FP2 a;
    FP2 b;
    unsigned char unitary;
} FP4;

typedef struct _FP12 {
    FP4 a;
    FP4 b;
    FP4 c;
    unsigned char unitary;
    unsigned char miller;
} FP12;

typedef struct _ECN2 {
    int marker;
    FP2 x;
    FP2 y;
    FP2 z;
} ECN2;

/** Stores information regarding the groups involved in pairing computation. */
struct pairing_group_st {
    EC_GROUP *ec;
    BN_MONT_CTX *mont;
    BN_CTX *bn_ctx;
    BIGNUM *t;
    BIGNUM *field;
    BIGNUM *order;
    BIGNUM *one;
    BIGNUM *zero;
    EC_POINT *g1;
    ECN2 *g2;
    FP2 *X;          //Frobniues constant
    int twist;
    int coord;
    int pmod8;
    int qnr;
};

/** Convenient type to manipulate pairing groups. */
typedef struct pairing_group_st PAIRING_GROUP;

int SM9_Group_init(PAIRING_GROUP *group);
void SM9_Group_free(const PAIRING_GROUP *group);

int SM9_GenerateSignKey(const PAIRING_GROUP *group, unsigned char *hid, const char *ID, int IDlen, BIGNUM *ks,
        unsigned char *Ppubs, unsigned char *dsa);
int SM9_Sign(const PAIRING_GROUP *group, unsigned char *hid, const char *IDA, unsigned char *message, int len,
        unsigned char *rand, unsigned char *dsa, unsigned char *Ppub, unsigned char *H, unsigned char *S);
int SM9_Verify(PAIRING_GROUP *group, unsigned char *H, unsigned char *S, unsigned char *hid, const char *IDA,
        unsigned char *message, int len, unsigned char *Ppub);

int SM9_GenerateEncryptKey(const PAIRING_GROUP *group, unsigned char *hid, const char *ID, int IDlen, BIGNUM *ks,
        unsigned char *Ppube, unsigned char *dea);
int SM9_Encrypt(const PAIRING_GROUP *group, unsigned char *hid, const char *IDB, unsigned char *message, int mlen,
        unsigned char *rand, int EncID, int k1_len, int k2_len, unsigned char *Ppub, unsigned char *C, int *C_len);
int SM9_Decrypt(const PAIRING_GROUP *group, unsigned char *C, int C_len, unsigned char *deB, const char *IDB, int EncID,
        int k1_len, int k2_len, unsigned char *M, int *Mlen);

int SM9_Key_encap(const PAIRING_GROUP *group, unsigned char *hid, const char *IDB, unsigned char *rand,
        unsigned char *Ppub, unsigned char *C, unsigned char *K, int Klen);
int SM9_Key_decap(const PAIRING_GROUP *group, const char *IDB, unsigned char *deB, unsigned char *C, int Klen,
        unsigned char *K);

int SM9_KeyEx_InitA_I(const PAIRING_GROUP *group, unsigned char *hid, const char *IDB, unsigned char *randA,
        unsigned char *Ppub, unsigned char *deA, EC_POINT *RA);
int SM9_KeyEx_InitA_II(const PAIRING_GROUP *group, const char *IDA, const char *IDB, unsigned char *randA,
        unsigned char *Ppub, unsigned char *deA, EC_POINT *RA, EC_POINT *RB, unsigned char *SB, unsigned char *SA);
int SM9_KeyEx_ReB_I(const PAIRING_GROUP *group, unsigned char *hid, const char *IDA, const char *IDB,
        unsigned char *randB, unsigned char *Ppub, unsigned char *deB, EC_POINT *RA, EC_POINT *RB, unsigned char *SB,
        FP12 *g1, FP12 *g2, FP12 *g3);
int SM9_KeyEx_ReB_II(const PAIRING_GROUP *group, const char *IDA, const char *IDB, FP12 *g1, FP12 *g2, FP12 *g3,
        EC_POINT *RA, EC_POINT *RB, unsigned char *SA);

#ifdef  __cplusplus
}
#endif

#endif

源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章