mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-27 07:43:47 +08:00
remove warnings
This commit is contained in:
@@ -99,6 +99,10 @@ static int sm9_master_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
|
||||
|
||||
static int sm9_master_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
||||
{
|
||||
/* FIXME: implement this! */
|
||||
(void)a;
|
||||
(void)b;
|
||||
|
||||
/*
|
||||
if (OBJ_cmp(a->pkey.sm9_master->pairing, b->pkey.sm9_master->pairing) != 0
|
||||
|| OBJ_cmp(a->pkey.sm9_master->scheme, b->pkey.sm9_master->scheme) != 0
|
||||
@@ -186,12 +190,14 @@ static int do_sm9_master_key_print(BIO *bp, const SM9_MASTER_KEY *x, int off, in
|
||||
static int sm9_master_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
(void)ctx; /* FIXME: how to use ASN1_PCTX? */
|
||||
return do_sm9_master_key_print(bp, pkey->pkey.sm9_master, indent, 0);
|
||||
}
|
||||
|
||||
static int sm9_master_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
(void)ctx; /* FIXME: how to use ASN1_PCTX? */
|
||||
return do_sm9_master_key_print(bp, pkey->pkey.sm9_master, indent, 1);
|
||||
}
|
||||
|
||||
@@ -232,16 +238,25 @@ static int sm9_master_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
|
||||
|
||||
static int sm9_pkey_size(const EVP_PKEY *pkey)
|
||||
{
|
||||
/* FIXME: wrap a SM9_xxx() */
|
||||
(void)pkey;
|
||||
|
||||
return 32 * 12;
|
||||
}
|
||||
|
||||
static int sm9_pkey_bits(const EVP_PKEY *pkey)
|
||||
{
|
||||
/* FIXME: wrap a SM9_xxx() */
|
||||
(void)pkey;
|
||||
|
||||
return 256 * 12;
|
||||
}
|
||||
|
||||
static int sm9_pkey_security_bits(const EVP_PKEY *pkey)
|
||||
{
|
||||
/* FIXME: wrap a SM9_xxx() */
|
||||
(void)pkey;
|
||||
|
||||
return 256/2;
|
||||
}
|
||||
|
||||
@@ -252,6 +267,11 @@ static void sm9_master_pkey_free(EVP_PKEY *pkey)
|
||||
|
||||
static int sm9_master_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
|
||||
{
|
||||
(void)pkey;
|
||||
(void)op;
|
||||
(void)arg1;
|
||||
(void)arg2;
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -434,12 +454,14 @@ static int do_sm9_key_print(BIO *bp, const SM9PrivateKey *x, int off, int priv)
|
||||
static int sm9_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
(void)ctx; /* FIXME: how to use ASN1_PCTX? */
|
||||
return do_sm9_key_print(bp, pkey->pkey.sm9, indent, 0);
|
||||
}
|
||||
|
||||
static int sm9_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
(void)ctx; /* FIXME: how to use ASN1_PCTX? */
|
||||
return do_sm9_key_print(bp, pkey->pkey.sm9, indent, 1);
|
||||
}
|
||||
|
||||
@@ -485,6 +507,12 @@ static void sm9_pkey_free(EVP_PKEY *pkey)
|
||||
|
||||
static int sm9_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)pkey;
|
||||
(void)op;
|
||||
(void)arg1;
|
||||
(void)arg2;
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +59,10 @@
|
||||
static int sm9_master_key_cb(int operation, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it, void *exarg)
|
||||
{
|
||||
/* FIXME: usage of it and exarg? */
|
||||
(void)it;
|
||||
(void)exarg;
|
||||
|
||||
if (operation == ASN1_OP_NEW_PRE) {
|
||||
*pval = (ASN1_VALUE *)SM9_MASTER_KEY_new();
|
||||
if (*pval)
|
||||
@@ -93,6 +97,10 @@ IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(SM9_MASTER_KEY,SM9PublicParameters,S
|
||||
static int sm9_key_cb(int operation, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it, void *exarg)
|
||||
{
|
||||
/* FIXME: usage of it and exarg? */
|
||||
(void)it;
|
||||
(void)exarg;
|
||||
|
||||
if (operation == ASN1_OP_NEW_PRE) {
|
||||
*pval = (ASN1_VALUE *)SM9_KEY_new();
|
||||
if (*pval)
|
||||
@@ -144,6 +152,10 @@ IMPLEMENT_ASN1_DUP_FUNCTION(SM9Signature)
|
||||
|
||||
int SM9PublicKey_gmtls_encode(SM9PublicKey *pk, unsigned char key[1024])
|
||||
{
|
||||
/* FIMXE: implement this */
|
||||
(void)pk;
|
||||
(void)key;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -177,9 +189,6 @@ SM9PrivateKey *d2i_SM9PrivateKey_bio(BIO *bp, SM9PrivateKey **a)
|
||||
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(SM9PrivateKey), bp, a);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int i2d_SM9Signature_bio(BIO *bp, SM9Signature *a)
|
||||
{
|
||||
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(SM9Signature), bp, a);
|
||||
|
||||
6
crypto/sm9/sm9_bn.c
Normal file
6
crypto/sm9/sm9_bn.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
@@ -196,7 +196,7 @@ int SM9_wrap_key(int type, /* NID_sm9kdf_with_sm3 */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (keylen > EVP_MD_size(kdf_md) * 255) {
|
||||
if (keylen > (size_t)EVP_MD_size(kdf_md) * 255) {
|
||||
SM9err(SM9_F_SM9_WRAP_KEY, SM9_R_INVALID_KEM_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
@@ -343,7 +343,8 @@ int SM9_encrypt(int type,
|
||||
size_t C1_len;
|
||||
unsigned char mac[EVP_MAX_MD_SIZE];
|
||||
unsigned int maclen = sizeof(mac);
|
||||
int len, i;
|
||||
int len;
|
||||
size_t i;
|
||||
|
||||
/* parse type */
|
||||
switch (type) {
|
||||
@@ -465,7 +466,7 @@ int SM9_decrypt(int type,
|
||||
*outlen = C2_len;
|
||||
ret = 1;
|
||||
goto end;
|
||||
} else if (*outlen < C2_len) {
|
||||
} else if (*outlen < (size_t)C2_len) {
|
||||
SM9err(SM9_F_SM9_DECRYPT, SM9_R_BUFFER_TOO_SMALL);
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -162,35 +162,164 @@ const BIGNUM *SM9_get0_generator2_y0(void);
|
||||
const BIGNUM *SM9_get0_generator2_y1(void);
|
||||
|
||||
typedef BIGNUM *fp2_t[2];
|
||||
|
||||
int fp2_init(fp2_t a, BN_CTX *ctx);
|
||||
void fp2_cleanup(fp2_t a);
|
||||
void fp2_clear_cleanup(fp2_t a);
|
||||
int fp2_is_zero(const fp2_t a);
|
||||
int fp2_is_zero(const fp2_t a);
|
||||
int fp2_print(const fp2_t a);
|
||||
int fp2_is_one(const fp2_t a);
|
||||
void fp2_set_zero(fp2_t r);
|
||||
int fp2_set_one(fp2_t r);
|
||||
int fp2_copy(fp2_t r, const fp2_t a);
|
||||
int fp2_set(fp2_t r, const BIGNUM *a0, const BIGNUM *a1);
|
||||
int fp2_set_hex(fp2_t r, const char *str[2]);
|
||||
int fp2_set_u(fp2_t r);
|
||||
int fp2_set_5u(fp2_t r);
|
||||
int fp2_set_bn(fp2_t r, const BIGNUM *a);
|
||||
int fp2_set_word(fp2_t r, unsigned long a);
|
||||
int fp2_equ(const fp2_t a, const fp2_t b);
|
||||
int fp2_equ_hex(const fp2_t a, const char *str[2], BN_CTX *ctx);
|
||||
int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_add(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_dbl(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_sub(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_neg(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_div(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp2_to_bin(const fp2_t a, unsigned char to[64]);
|
||||
int fp2_from_bin(fp2_t a, const unsigned char from[64]);
|
||||
int fp2_test(const BIGNUM *p, BN_CTX *ctx);
|
||||
|
||||
|
||||
typedef fp2_t fp4_t[2];
|
||||
|
||||
int fp4_init(fp4_t a, BN_CTX *ctx);
|
||||
void fp4_cleanup(fp4_t a);
|
||||
void fp4_clear_cleanup(fp4_t a);
|
||||
int fp4_print(const fp4_t a);
|
||||
int fp4_is_zero(const fp4_t a);
|
||||
int fp4_is_one(const fp4_t a);
|
||||
void fp4_set_zero(fp4_t r);
|
||||
int fp4_set_one(fp4_t r);
|
||||
int fp4_set_bn(fp4_t r, const BIGNUM *a);
|
||||
int fp4_set_word(fp4_t r, unsigned long a);
|
||||
int fp4_set_fp2(fp4_t r, const fp2_t a);
|
||||
int fp4_set(fp4_t r, const fp2_t a0, const fp2_t a1);
|
||||
int fp4_set_hex(fp4_t r, const char *str[4]);
|
||||
int fp4_copy(fp4_t r, const fp4_t a);
|
||||
int fp4_set_u(fp4_t r);
|
||||
int fp4_set_v(fp4_t r);
|
||||
int fp4_equ(const fp4_t a, const fp4_t b);
|
||||
int fp4_equ_hex(const fp4_t a, const char *str[4], BN_CTX *ctx);
|
||||
int fp4_to_bin(const fp4_t a, unsigned char to[128]);
|
||||
int fp4_from_bin(fp4_t a, const unsigned char from[128]);
|
||||
int fp4_add(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_dbl(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_sub(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_neg(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp4_test(const BIGNUM *p, BN_CTX *ctx);
|
||||
|
||||
|
||||
typedef fp4_t fp12_t[3];
|
||||
|
||||
int fp12_init(fp12_t a, BN_CTX *ctx);
|
||||
void fp12_cleanup(fp12_t a);
|
||||
void fp12_clear_cleanup(fp12_t a);
|
||||
int fp12_print(const fp12_t a);
|
||||
int fp12_is_zero(const fp12_t a);
|
||||
int fp12_is_one(const fp12_t a);
|
||||
void fp12_set_zero(fp12_t r);
|
||||
int fp12_set_one(fp12_t r);
|
||||
int fp12_copy(fp12_t r, const fp12_t a);
|
||||
int fp12_set(fp12_t r, const fp4_t a0, const fp4_t a1, const fp4_t a2);
|
||||
int fp12_set_hex(fp12_t r, const char *str[12]);
|
||||
int fp12_set_fp4(fp12_t r, const fp4_t a);
|
||||
int fp12_set_fp2(fp12_t r, const fp2_t a);
|
||||
int fp12_set_bn(fp12_t r, const BIGNUM *a);
|
||||
int fp12_set_word(fp12_t r, unsigned long a);
|
||||
int fp12_set_u(fp12_t r);
|
||||
int fp12_set_v(fp12_t r);
|
||||
int fp12_set_w(fp12_t r);
|
||||
int fp12_set_w_sqr(fp12_t r);
|
||||
int fp12_equ(const fp12_t a, const fp12_t b);
|
||||
int fp12_equ_hex(const fp12_t a, const char *str[12], BN_CTX *ctx);
|
||||
int fp12_to_bin(const fp12_t a, unsigned char to[384]);
|
||||
int fp12_from_bin(fp4_t a, const unsigned char from[384]);
|
||||
int fp12_add(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_dbl(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_sub(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_neg(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_mul(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_div(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_pow(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_test(const BIGNUM *p, BN_CTX *ctx);
|
||||
|
||||
|
||||
typedef struct point_t {
|
||||
fp2_t X;
|
||||
fp2_t Y;
|
||||
fp2_t Z;
|
||||
} point_t;
|
||||
|
||||
int fp12_init(fp12_t a, BN_CTX *ctx);
|
||||
int fp12_mul(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_pow(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fp12_to_bin(const fp12_t a, unsigned char to[384]);
|
||||
int fp12_print(const fp12_t a);
|
||||
void fp12_cleanup(fp12_t a);
|
||||
|
||||
int point_init(point_t *P, BN_CTX *ctx);
|
||||
int point_copy(point_t *R, const point_t *P);
|
||||
void point_cleanup(point_t *P);
|
||||
void point_print(const point_t *P);
|
||||
int point_copy(point_t *R, const point_t *P);
|
||||
int point_equ(const point_t *P, const point_t *Q);
|
||||
int point_equ_hex(const point_t *P, const char *str[4], BN_CTX *ctx);
|
||||
int point_is_at_infinity(const point_t *P);
|
||||
int point_set_to_infinity(point_t *P);
|
||||
int point_set_affine_coordinates(point_t *P, const fp2_t x, const fp2_t y);
|
||||
int point_set_affine_coordinates_hex(point_t *P, const char *str[4]);
|
||||
int point_set_affine_coordinates_bignums(point_t *P,
|
||||
const BIGNUM *x0, const BIGNUM *x1, const BIGNUM *y0, const BIGNUM *y1);
|
||||
int point_get_affine_coordinates(const point_t *P, fp2_t x, fp2_t y);
|
||||
int point_get_ext_affine_coordinates(const point_t *P, fp12_t x, fp12_t y, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_set_ext_affine_coordinates(point_t *P, const fp12_t x, const fp12_t y, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_is_on_curve(point_t *P, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_to_octets(const point_t *P, unsigned char to[129], BN_CTX *ctx);
|
||||
int point_from_octets(point_t *P, const unsigned char from[129], const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_add(point_t *R, const point_t *A, const point_t *B, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_dbl(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_neg(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_sub(point_t *R, const point_t *P, const point_t *Q, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_mul(point_t *R, const BIGNUM *k, const point_t *P, const BIGNUM *p, BN_CTX *ctx);
|
||||
int point_mul_generator(point_t *R, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
void point_cleanup(point_t *P);
|
||||
|
||||
|
||||
int eval_tangent(fp12_t r, const point_t *T, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx);
|
||||
int eval_line(fp12_t r, const point_t *T, const point_t *Q,
|
||||
const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx);
|
||||
int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx);
|
||||
int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx);
|
||||
int rate_test(void);
|
||||
int rate_pairing(fp12_t r, const point_t *Q, const EC_POINT *P, BN_CTX *ctx);
|
||||
|
||||
int params_test(void);
|
||||
|
||||
int sm9_check_pairing(int nid);
|
||||
int sm9_check_scheme(int nid);
|
||||
int sm9_check_hash1(int nid);
|
||||
|
||||
@@ -139,12 +139,22 @@ void SM9_KEY_free(SM9_KEY *key)
|
||||
int SM9PrivateKey_get_gmtls_public_key(SM9PublicParameters *mpk,
|
||||
SM9PrivateKey *sk, unsigned char pub_key[1024])
|
||||
{
|
||||
/* FIXME */
|
||||
(void)mpk;
|
||||
(void)sk;
|
||||
(void)pub_key;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SM9PublicKey_get_gmtls_encoded(SM9PublicParameters *mpk,
|
||||
SM9PublicKey *pk, unsigned char encoded[1024])
|
||||
{
|
||||
/* FIXME */
|
||||
(void)mpk;
|
||||
(void)pk;
|
||||
(void)encoded;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -186,26 +196,41 @@ int SM9_KEY_up_ref(SM9_KEY *sk)
|
||||
|
||||
int sm9_check_pairing(int nid)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)nid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm9_check_scheme(int nid)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)nid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm9_check_hash1(int nid)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)nid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm9_check_encrypt_scheme(int nid)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)nid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm9_check_sign_scheme(int nid)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)nid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -314,5 +339,4 @@ int SM9_hash2(const EVP_MD *md, BIGNUM **r,
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -183,6 +183,11 @@ static int pkey_sm9_master_encrypt(EVP_PKEY_CTX *ctx,
|
||||
|
||||
static int pkey_sm9_master_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)ctx;
|
||||
(void)key;
|
||||
(void)keylen;
|
||||
|
||||
/*
|
||||
SM9_MASTER_PKEY_CTX *dctx = EVP_PKEY_CTX_get_data(ctx);
|
||||
SM9_MASTER_KEY *sm9_master = EVP_PKEY_get0_SM9_MASTER(
|
||||
@@ -387,6 +392,11 @@ static int pkey_sm9_decrypt(EVP_PKEY_CTX *ctx,
|
||||
|
||||
static int pkey_sm9_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
|
||||
{
|
||||
/* FIXME */
|
||||
(void)ctx;
|
||||
(void)key;
|
||||
(void)keylen;
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,9 @@
|
||||
#include "sm9_lcl.h"
|
||||
|
||||
|
||||
static int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
#define NOSM9_FAST
|
||||
|
||||
int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
{
|
||||
a[0] = NULL;
|
||||
a[1] = NULL;
|
||||
@@ -72,7 +74,7 @@ static int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void fp2_cleanup(fp2_t a)
|
||||
void fp2_cleanup(fp2_t a)
|
||||
{
|
||||
BN_free(a[0]);
|
||||
BN_free(a[1]);
|
||||
@@ -80,7 +82,7 @@ static void fp2_cleanup(fp2_t a)
|
||||
a[1] = NULL;
|
||||
}
|
||||
|
||||
static void fp2_clear_cleanup(fp2_t a)
|
||||
void fp2_clear_cleanup(fp2_t a)
|
||||
{
|
||||
BN_clear_free(a[0]);
|
||||
BN_clear_free(a[1]);
|
||||
@@ -88,96 +90,93 @@ static void fp2_clear_cleanup(fp2_t a)
|
||||
a[1] = NULL;
|
||||
}
|
||||
|
||||
static int fp2_is_zero(const fp2_t a)
|
||||
int fp2_is_zero(const fp2_t a)
|
||||
{
|
||||
return BN_is_zero(a[0])
|
||||
&& BN_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static int fp2_print(const fp2_t a)
|
||||
int fp2_print(const fp2_t a)
|
||||
{
|
||||
printf("%s\n", BN_bn2hex(a[0]));
|
||||
printf("%s\n", BN_bn2hex(a[1]));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_is_one(const fp2_t a)
|
||||
int fp2_is_one(const fp2_t a)
|
||||
{
|
||||
return BN_is_one(a[0])
|
||||
&& BN_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static void fp2_set_zero(fp2_t r)
|
||||
void fp2_set_zero(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
BN_zero(r[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_one(fp2_t r)
|
||||
int fp2_set_one(fp2_t r)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp2_copy(fp2_t r, const fp2_t a)
|
||||
int fp2_copy(fp2_t r, const fp2_t a)
|
||||
{
|
||||
return BN_copy(r[0], a[0])
|
||||
&& BN_copy(r[1], a[1]);
|
||||
}
|
||||
|
||||
static int fp2_set(fp2_t r, const BIGNUM *a0, const BIGNUM *a1)
|
||||
int fp2_set(fp2_t r, const BIGNUM *a0, const BIGNUM *a1)
|
||||
{
|
||||
return BN_copy(r[0], a0)
|
||||
&& BN_copy(r[1], a1);
|
||||
}
|
||||
|
||||
static int fp2_set_hex(fp2_t r, const char *str[2])
|
||||
int fp2_set_hex(fp2_t r, const char *str[2])
|
||||
{
|
||||
return BN_hex2bn(&r[0], str[0])
|
||||
&& BN_hex2bn(&r[1], str[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_u(fp2_t r)
|
||||
int fp2_set_u(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
return BN_one(r[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_5u(fp2_t r)
|
||||
int fp2_set_5u(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
return BN_set_word(r[1], 5);
|
||||
}
|
||||
|
||||
static int fp2_set_bn(fp2_t r, const BIGNUM *a)
|
||||
int fp2_set_bn(fp2_t r, const BIGNUM *a)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_copy(r[0], a) != NULL;
|
||||
}
|
||||
|
||||
static int fp2_set_word(fp2_t r, unsigned long a)
|
||||
int fp2_set_word(fp2_t r, unsigned long a)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp2_equ(const fp2_t a, const fp2_t b)
|
||||
int fp2_equ(const fp2_t a, const fp2_t b)
|
||||
{
|
||||
return !BN_cmp(a[0], b[0]) && !BN_cmp(a[1], b[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_equ_hex(const fp2_t a, const char *str[2], BN_CTX *ctx)
|
||||
int fp2_equ_hex(const fp2_t a, const char *str[2], BN_CTX *ctx)
|
||||
{
|
||||
fp2_t t;
|
||||
fp2_init(t, ctx);
|
||||
fp2_set_hex(t, str);
|
||||
return fp2_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *w = NULL;
|
||||
if (!(w = BN_CTX_get(ctx))
|
||||
@@ -190,21 +189,20 @@ static int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p
|
||||
BN_free(w);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp2_add(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_add(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_add(r[0], a[0], b[0], p, ctx)
|
||||
&& BN_mod_add(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_dbl(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_dbl(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_add(r[0], a[0], a[0], p, ctx)
|
||||
&& BN_mod_add(r[1], a[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t t;
|
||||
if (!fp2_init(t, ctx)
|
||||
@@ -217,19 +215,19 @@ static int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sub(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sub(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& BN_mod_sub(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_neg(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_neg(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_sub(r[0], p, a[0], p, ctx)
|
||||
&& BN_mod_sub(r[1], p, a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *t = NULL;
|
||||
BIGNUM *r0 = NULL;
|
||||
@@ -262,7 +260,7 @@ static int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CT
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -297,7 +295,7 @@ static int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -318,7 +316,7 @@ static int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -348,7 +346,7 @@ static int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -381,7 +379,7 @@ static int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
if (BN_is_zero(a[0])) {
|
||||
/* r0 = 0 */
|
||||
@@ -430,15 +428,13 @@ static int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_div(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_div(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_inv(r, b, p, ctx)
|
||||
&& fp2_mul(r, a, r, p, ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
{
|
||||
memset(to, 0, 64);
|
||||
BN_bn2bin(a[1], to + 32 - BN_num_bytes(a[1]));
|
||||
@@ -446,14 +442,13 @@ static int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_from_bin(fp2_t a, const unsigned char from[64])
|
||||
int fp2_from_bin(fp2_t a, const unsigned char from[64])
|
||||
{
|
||||
return BN_bin2bn(from, 32, a[1])
|
||||
&& BN_bin2bn(from + 32, 32, a[0]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"5f25ce2083fc970a6b9fdcd819fb1966d300af2afd58d480c59e02b320852183",
|
||||
@@ -568,9 +563,8 @@ static int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
{
|
||||
int r;
|
||||
r = fp2_init(a[0], ctx);
|
||||
@@ -582,21 +576,19 @@ static int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
return r;
|
||||
}
|
||||
|
||||
static void fp4_cleanup(fp4_t a)
|
||||
void fp4_cleanup(fp4_t a)
|
||||
{
|
||||
fp2_cleanup(a[0]);
|
||||
fp2_cleanup(a[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static void fp4_clear_cleanup(fp4_t a)
|
||||
void fp4_clear_cleanup(fp4_t a)
|
||||
{
|
||||
fp2_clear_cleanup(a[0]);
|
||||
fp2_clear_cleanup(a[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_print(const fp4_t a)
|
||||
int fp4_print(const fp4_t a)
|
||||
{
|
||||
fp2_print(a[0]);
|
||||
fp2_print(a[1]);
|
||||
@@ -604,131 +596,129 @@ static int fp4_print(const fp4_t a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_is_zero(const fp4_t a)
|
||||
int fp4_is_zero(const fp4_t a)
|
||||
{
|
||||
return fp2_is_zero(a[0])
|
||||
&& fp2_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static int fp4_is_one(const fp4_t a)
|
||||
int fp4_is_one(const fp4_t a)
|
||||
{
|
||||
return fp2_is_one(a[0])
|
||||
&& fp2_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static void fp4_set_zero(fp4_t r)
|
||||
void fp4_set_zero(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[0]);
|
||||
fp2_set_zero(r[1]);
|
||||
}
|
||||
|
||||
static int fp4_set_one(fp4_t r)
|
||||
int fp4_set_one(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp4_set_bn(fp4_t r, const BIGNUM *a)
|
||||
int fp4_set_bn(fp4_t r, const BIGNUM *a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_bn(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set_word(fp4_t r, unsigned long a)
|
||||
int fp4_set_word(fp4_t r, unsigned long a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set_fp2(fp4_t r, const fp2_t a)
|
||||
int fp4_set_fp2(fp4_t r, const fp2_t a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_copy(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set(fp4_t r, const fp2_t a0, const fp2_t a1)
|
||||
int fp4_set(fp4_t r, const fp2_t a0, const fp2_t a1)
|
||||
{
|
||||
return fp2_copy(r[0], a0)
|
||||
&& fp2_copy(r[1], a1);
|
||||
}
|
||||
|
||||
static int fp4_set_hex(fp4_t r, const char *str[4])
|
||||
int fp4_set_hex(fp4_t r, const char *str[4])
|
||||
{
|
||||
return fp2_set_hex(r[0], str)
|
||||
&& fp2_set_hex(r[1], str+2);
|
||||
}
|
||||
|
||||
static int fp4_copy(fp4_t r, const fp4_t a)
|
||||
int fp4_copy(fp4_t r, const fp4_t a)
|
||||
{
|
||||
return fp2_copy(r[0], a[0])
|
||||
&& fp2_copy(r[1], a[1]);
|
||||
}
|
||||
|
||||
static int fp4_set_u(fp4_t r)
|
||||
int fp4_set_u(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_u(r[0]);
|
||||
}
|
||||
|
||||
static int fp4_set_v(fp4_t r)
|
||||
int fp4_set_v(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[0]);
|
||||
return fp2_set_one(r[1]);
|
||||
}
|
||||
|
||||
static int fp4_equ(const fp4_t a, const fp4_t b)
|
||||
int fp4_equ(const fp4_t a, const fp4_t b)
|
||||
{
|
||||
return fp2_equ(a[0], b[0])
|
||||
&& fp2_equ(a[1], b[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp4_equ_hex(const fp4_t a, const char *str[4], BN_CTX *ctx)
|
||||
int fp4_equ_hex(const fp4_t a, const char *str[4], BN_CTX *ctx)
|
||||
{
|
||||
fp4_t t;
|
||||
fp4_init(t, ctx);
|
||||
fp4_set_hex(t, str);
|
||||
return fp4_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_to_bin(const fp4_t a, unsigned char to[128])
|
||||
int fp4_to_bin(const fp4_t a, unsigned char to[128])
|
||||
{
|
||||
return fp2_to_bin(a[1], to)
|
||||
&& fp2_to_bin(a[0], to + 64);
|
||||
}
|
||||
|
||||
static int fp4_from_bin(fp4_t a, const unsigned char from[128])
|
||||
int fp4_from_bin(fp4_t a, const unsigned char from[128])
|
||||
{
|
||||
return fp2_from_bin(a[1], from)
|
||||
&& fp2_from_bin(a[0], from + 64);
|
||||
}
|
||||
|
||||
static int fp4_add(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_add(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_add(r[0], a[0], b[0], p, ctx)
|
||||
&& fp2_add(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_dbl(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_dbl(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_dbl(r[0], a[0], p, ctx)
|
||||
&& fp2_dbl(r[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_sub(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sub(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& fp2_sub(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_neg(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_neg(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_neg(r[0], a[0], p, ctx)
|
||||
&&fp2_neg(r[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
|
||||
@@ -759,7 +749,7 @@ static int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CT
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -788,7 +778,7 @@ static int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -814,7 +804,7 @@ static int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -841,7 +831,7 @@ static int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, k;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -875,8 +865,7 @@ static int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"bec057c34cec656c05f236d9399cd00c64319632885d200f964e4591dd7ca77",
|
||||
@@ -1012,7 +1001,6 @@ static int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_init(fp12_t a, BN_CTX *ctx)
|
||||
{
|
||||
@@ -1035,14 +1023,12 @@ void fp12_cleanup(fp12_t a)
|
||||
fp4_cleanup(a[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static void fp12_clear_cleanup(fp12_t a)
|
||||
void fp12_clear_cleanup(fp12_t a)
|
||||
{
|
||||
fp4_clear_cleanup(a[0]);
|
||||
fp4_clear_cleanup(a[1]);
|
||||
fp4_clear_cleanup(a[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_print(const fp12_t a)
|
||||
{
|
||||
@@ -1052,137 +1038,125 @@ int fp12_print(const fp12_t a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_is_zero(const fp12_t a)
|
||||
int fp12_is_zero(const fp12_t a)
|
||||
{
|
||||
return fp4_is_zero(a[0])
|
||||
&& fp4_is_zero(a[1])
|
||||
&& fp4_is_zero(a[2]);
|
||||
}
|
||||
|
||||
static int fp12_is_one(const fp12_t a)
|
||||
int fp12_is_one(const fp12_t a)
|
||||
{
|
||||
return fp4_is_one(a[0])
|
||||
&& fp4_is_zero(a[1])
|
||||
&& fp4_is_zero(a[2]);
|
||||
}
|
||||
|
||||
static void fp12_set_zero(fp12_t r)
|
||||
void fp12_set_zero(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_one(fp12_t r)
|
||||
int fp12_set_one(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp12_copy(fp12_t r, const fp12_t a)
|
||||
int fp12_copy(fp12_t r, const fp12_t a)
|
||||
{
|
||||
return fp4_copy(r[0], a[0])
|
||||
&& fp4_copy(r[1], a[1])
|
||||
&& fp4_copy(r[2], a[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set(fp12_t r, const fp4_t a0, const fp4_t a1, const fp4_t a2)
|
||||
int fp12_set(fp12_t r, const fp4_t a0, const fp4_t a1, const fp4_t a2)
|
||||
{
|
||||
return fp4_copy(r[0], a0)
|
||||
&& fp4_copy(r[1], a1)
|
||||
&& fp4_copy(r[2], a2);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_hex(fp12_t r, const char *str[12])
|
||||
int fp12_set_hex(fp12_t r, const char *str[12])
|
||||
{
|
||||
return fp4_set_hex(r[0], str)
|
||||
&& fp4_set_hex(r[1], str + 4)
|
||||
&& fp4_set_hex(r[2], str + 8);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_fp4(fp12_t r, const fp4_t a)
|
||||
int fp12_set_fp4(fp12_t r, const fp4_t a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_copy(r[0], a);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_fp2(fp12_t r, const fp2_t a)
|
||||
int fp12_set_fp2(fp12_t r, const fp2_t a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_fp2(r[0], a);
|
||||
}
|
||||
|
||||
static int fp12_set_bn(fp12_t r, const BIGNUM *a)
|
||||
int fp12_set_bn(fp12_t r, const BIGNUM *a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_bn(r[0], a);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_word(fp12_t r, unsigned long a)
|
||||
int fp12_set_word(fp12_t r, unsigned long a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp12_set_u(fp12_t r)
|
||||
int fp12_set_u(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_u(r[0]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_v(fp12_t r)
|
||||
int fp12_set_v(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_v(r[0]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_w(fp12_t r)
|
||||
int fp12_set_w(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_one(r[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_w_sqr(fp12_t r)
|
||||
int fp12_set_w_sqr(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[1]);
|
||||
return fp4_set_one(r[2]);
|
||||
}
|
||||
|
||||
static int fp12_equ(const fp12_t a, const fp12_t b)
|
||||
int fp12_equ(const fp12_t a, const fp12_t b)
|
||||
{
|
||||
return fp4_equ(a[0], b[0])
|
||||
&& fp4_equ(a[1], b[1])
|
||||
&& fp4_equ(a[2], b[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_equ_hex(const fp12_t a, const char *str[12], BN_CTX *ctx)
|
||||
int fp12_equ_hex(const fp12_t a, const char *str[12], BN_CTX *ctx)
|
||||
{
|
||||
fp12_t t;
|
||||
fp12_init(t, ctx);
|
||||
fp12_set_hex(t, str);
|
||||
return fp12_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_to_bin(const fp12_t a, unsigned char to[384])
|
||||
{
|
||||
@@ -1191,28 +1165,28 @@ int fp12_to_bin(const fp12_t a, unsigned char to[384])
|
||||
&& fp4_to_bin(a[0], to + 256);
|
||||
}
|
||||
|
||||
static int fp12_from_bin(fp4_t a, const unsigned char from[384])
|
||||
int fp12_from_bin(fp4_t a, const unsigned char from[384])
|
||||
{
|
||||
return fp4_from_bin(&a[2], from)
|
||||
&& fp4_from_bin(&a[1], from + 128)
|
||||
&& fp4_from_bin(&a[0], from + 256);
|
||||
}
|
||||
|
||||
static int fp12_add(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_add(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_add(r[0], a[0], b[0], p, ctx)
|
||||
&& fp4_add(r[1], a[1], b[1], p, ctx)
|
||||
&& fp4_add(r[2], a[2], b[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_dbl(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_dbl(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_dbl(r[0], a[0], p, ctx)
|
||||
&& fp4_dbl(r[1], a[1], p, ctx)
|
||||
&& fp4_dbl(r[2], a[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp12_t t;
|
||||
fp12_init(t, ctx);
|
||||
@@ -1226,14 +1200,14 @@ static int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_sub(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_sub(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& fp4_sub(r[1], a[1], b[1], p, ctx)
|
||||
&& fp4_sub(r[2], a[2], b[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_neg(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_neg(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_neg(r[0], a[0], p, ctx)
|
||||
&& fp4_neg(r[1], a[1], p, ctx)
|
||||
@@ -1286,7 +1260,7 @@ int fp12_mul(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp4_t r0, r1, r2, t;
|
||||
fp4_init(r0, ctx);
|
||||
@@ -1328,7 +1302,7 @@ static int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
if (fp4_is_zero(a[2])) {
|
||||
fp4_t k;
|
||||
@@ -1338,7 +1312,7 @@ static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
}
|
||||
|
||||
fp4_t r0, r1, r2;
|
||||
fp4_init(r0, ctx);
|
||||
fp4_init(r0, ctx); // FIXME: r0, r1, r2 never used
|
||||
fp4_init(r1, ctx);
|
||||
fp4_init(r2, ctx);
|
||||
|
||||
@@ -1433,7 +1407,7 @@ static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
}
|
||||
|
||||
//TODO: check this!
|
||||
static int fp12_div(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_div(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp12_inv(r, b, p, ctx)
|
||||
&& fp12_mul(r, a, r, p, ctx);
|
||||
@@ -1475,7 +1449,7 @@ int fp12_pow(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_copy(r[0][0], a[0][0])
|
||||
&& fp2_neg (r[0][1], a[0][1], p, ctx)
|
||||
@@ -1485,7 +1459,7 @@ static int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *
|
||||
&& fp2_neg (r[2][1], a[2][1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const BIGNUM *pw20;
|
||||
const BIGNUM *pw21;
|
||||
@@ -1508,8 +1482,7 @@ static int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"3a4b2fdf33cfe01aab98d17aefc8d38b0508061c3117685839bd0dfdeb5783a8",
|
||||
@@ -1792,7 +1765,6 @@ static int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int point_init(point_t *P, BN_CTX *ctx)
|
||||
{
|
||||
@@ -1873,7 +1845,7 @@ int point_set_affine_coordinates_hex(point_t *P, const char *str[4])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int point_equ_hex(const point_t *P, const char *str[4], BN_CTX *ctx)
|
||||
int point_equ_hex(const point_t *P, const char *str[4], BN_CTX *ctx)
|
||||
{
|
||||
point_t T;
|
||||
point_init(&T, ctx);
|
||||
@@ -2226,8 +2198,7 @@ int point_mul_generator(point_t *R, const BIGNUM *k, const BIGNUM *p, BN_CTX *ct
|
||||
return point_mul(R, k, &G, p, ctx);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_G[] = {
|
||||
"3722755292130b08d2aab97fd34ec120ee265948d19c17abf9b7213baf82d65b",
|
||||
@@ -2327,9 +2298,8 @@ static int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
//fp12_cleanup(y);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int eval_tangent(fp12_t r, const point_t *T, const BIGNUM *xP, const BIGNUM *yP,
|
||||
int eval_tangent(fp12_t r, const point_t *T, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int ret;
|
||||
@@ -2376,7 +2346,7 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int eval_line(fp12_t r, const point_t *T, const point_t *Q,
|
||||
int eval_line(fp12_t r, const point_t *T, const point_t *Q,
|
||||
const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
@@ -2426,7 +2396,7 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp12_t x, y;
|
||||
|
||||
@@ -2447,7 +2417,7 @@ static int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
frobenius(R, P, p, ctx);
|
||||
frobenius(R, R, p, ctx);
|
||||
@@ -2455,7 +2425,7 @@ static int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX
|
||||
}
|
||||
|
||||
|
||||
static int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int i, n;
|
||||
fp12_t t;
|
||||
@@ -2484,7 +2454,7 @@ static int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p
|
||||
}
|
||||
|
||||
|
||||
static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int i, n;
|
||||
fp12_t t;
|
||||
@@ -2544,7 +2514,7 @@ static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGN
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -2638,7 +2608,7 @@ static int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int params_test(void)
|
||||
int params_test(void)
|
||||
{
|
||||
const BIGNUM *p = SM9_get0_prime();
|
||||
const BIGNUM *a = SM9_get0_loop_count();
|
||||
@@ -2701,8 +2671,7 @@ int rate_pairing(fp12_t r, const point_t *Q, const EC_POINT *P, BN_CTX *ctx)
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int rate_test(void)
|
||||
int rate_test(void)
|
||||
{
|
||||
const char *Ppubs_str[] = {
|
||||
"29DBA116152D1F786CE843ED24A3B573414D2177386A92DD8F14D65696EA5E32",
|
||||
@@ -2742,16 +2711,14 @@ static int rate_test(void)
|
||||
rate_pairing(g, &Ppubs, P1, ctx);
|
||||
|
||||
ok = fp12_equ_hex(g, g_str, ctx);
|
||||
printf("rate %d: %s\n", __LINE__, ok ? "ok" : "error");
|
||||
|
||||
fp12_cleanup(g);
|
||||
point_cleanup(&Ppubs);
|
||||
EC_GROUP_free(group);
|
||||
BN_CTX_free(ctx);
|
||||
|
||||
return 1;
|
||||
return ok;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* for SM9 sign, the (xP, yP) is the fixed generator of E(Fp)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user