remove warnings

This commit is contained in:
Zhi Guan
2020-06-19 15:56:33 +08:00
parent ec29454f1d
commit 5a000d0998
42 changed files with 6607 additions and 5928 deletions

View File

@@ -25,9 +25,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
{ERR_FUNC(EC_F_D2I_ECPKPARAMETERS), "d2i_ECPKParameters"},
{ERR_FUNC(EC_F_D2I_ECPRIVATEKEY), "d2i_ECPrivateKey"},
{ERR_FUNC(EC_F_DO_EC_KEY_PRINT), "do_EC_KEY_print"},
{ERR_FUNC(EC_F_ECAHE_CIPHERTEXT_SIZE), "ECAHE_ciphertext_size"},
{ERR_FUNC(EC_F_ECAHE_DECRYPT), "ECAHE_decrypt"},
{ERR_FUNC(EC_F_ECAHE_ENCRYPT), "ECAHE_encrypt"},
{ERR_FUNC(EC_F_ECDH_CMS_DECRYPT), "ecdh_cms_decrypt"},
{ERR_FUNC(EC_F_ECDH_CMS_SET_SHARED_INFO), "ecdh_cms_set_shared_info"},
{ERR_FUNC(EC_F_ECDH_COMPUTE_KEY), "ECDH_compute_key"},
@@ -141,8 +138,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
{ERR_FUNC(EC_F_EC_GROUP_CHECK_DISCRIMINANT),
"EC_GROUP_check_discriminant"},
{ERR_FUNC(EC_F_EC_GROUP_COPY), "EC_GROUP_copy"},
{ERR_FUNC(EC_F_EC_GROUP_GENERATE_TYPE1CURVE),
"EC_GROUP_generate_type1curve"},
{ERR_FUNC(EC_F_EC_GROUP_GET_CURVE_GF2M), "EC_GROUP_get_curve_GF2m"},
{ERR_FUNC(EC_F_EC_GROUP_GET_CURVE_GFP), "EC_GROUP_get_curve_GFp"},
{ERR_FUNC(EC_F_EC_GROUP_GET_DEGREE), "EC_GROUP_get_degree"},
@@ -153,11 +148,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
"EC_GROUP_get_pentanomial_basis"},
{ERR_FUNC(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS),
"EC_GROUP_get_trinomial_basis"},
{ERR_FUNC(EC_F_EC_GROUP_GET_TYPE1CURVE_ETA),
"EC_GROUP_get_type1curve_eta"},
{ERR_FUNC(EC_F_EC_GROUP_GET_TYPE1CURVE_ZETA),
"EC_GROUP_get_type1curve_zeta"},
{ERR_FUNC(EC_F_EC_GROUP_IS_TYPE1CURVE), "EC_GROUP_is_type1curve"},
{ERR_FUNC(EC_F_EC_GROUP_NEW), "EC_GROUP_new"},
{ERR_FUNC(EC_F_EC_GROUP_NEW_BY_CURVE_NAME), "EC_GROUP_new_by_curve_name"},
{ERR_FUNC(EC_F_EC_GROUP_NEW_FROM_DATA), "ec_group_new_from_data"},
@@ -165,7 +155,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
"EC_GROUP_new_from_ecparameters"},
{ERR_FUNC(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS),
"EC_GROUP_new_from_ecpkparameters"},
{ERR_FUNC(EC_F_EC_GROUP_NEW_TYPE1CURVE), "EC_GROUP_new_type1curve"},
{ERR_FUNC(EC_F_EC_GROUP_SET_CURVE_GF2M), "EC_GROUP_set_curve_GF2m"},
{ERR_FUNC(EC_F_EC_GROUP_SET_CURVE_GFP), "EC_GROUP_set_curve_GFp"},
{ERR_FUNC(EC_F_EC_GROUP_SET_GENERATOR), "EC_GROUP_set_generator"},
@@ -186,7 +175,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
{ERR_FUNC(EC_F_EC_POINTS_MAKE_AFFINE), "EC_POINTs_make_affine"},
{ERR_FUNC(EC_F_EC_POINT_ADD), "EC_POINT_add"},
{ERR_FUNC(EC_F_EC_POINT_CMP), "EC_POINT_cmp"},
{ERR_FUNC(EC_F_EC_POINT_CMP_FPPOINT), "EC_POINT_cmp_fppoint"},
{ERR_FUNC(EC_F_EC_POINT_COPY), "EC_POINT_copy"},
{ERR_FUNC(EC_F_EC_POINT_DBL), "EC_POINT_dbl"},
{ERR_FUNC(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M),
@@ -195,7 +183,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
"EC_POINT_get_affine_coordinates_GFp"},
{ERR_FUNC(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP),
"EC_POINT_get_Jprojective_coordinates_GFp"},
{ERR_FUNC(EC_F_EC_POINT_HASH2POINT), "EC_POINT_hash2point"},
{ERR_FUNC(EC_F_EC_POINT_INVERT), "EC_POINT_invert"},
{ERR_FUNC(EC_F_EC_POINT_IS_AT_INFINITY), "EC_POINT_is_at_infinity"},
{ERR_FUNC(EC_F_EC_POINT_IS_ON_CURVE), "EC_POINT_is_on_curve"},
@@ -215,7 +202,6 @@ static ERR_STRING_DATA EC_str_functs[] = {
"EC_POINT_set_Jprojective_coordinates_GFp"},
{ERR_FUNC(EC_F_EC_POINT_SET_TO_INFINITY), "EC_POINT_set_to_infinity"},
{ERR_FUNC(EC_F_EC_PRE_COMP_NEW), "ec_pre_comp_new"},
{ERR_FUNC(EC_F_EC_TYPE1CURVE_TATE), "EC_type1curve_tate"},
{ERR_FUNC(EC_F_EC_WNAF_MUL), "ec_wNAF_mul"},
{ERR_FUNC(EC_F_EC_WNAF_PRECOMPUTE_MULT), "ec_wNAF_precompute_mult"},
{ERR_FUNC(EC_F_I2D_ECIESPARAMETERS), "i2d_ECIESParameters"},
@@ -243,17 +229,13 @@ static ERR_STRING_DATA EC_str_functs[] = {
{ERR_FUNC(EC_F_SM2P256_PRE_COMP_NEW), "sm2p256_pre_comp_new"},
{ERR_FUNC(EC_F_SM2_COMPUTE_ID_DIGEST), "SM2_compute_id_digest"},
{ERR_FUNC(EC_F_SM2_COMPUTE_MESSAGE_DIGEST), "SM2_compute_message_digest"},
{ERR_FUNC(EC_F_SM2_COMPUTE_SHARE_KEY), "SM2_compute_share_key"},
{ERR_FUNC(EC_F_SM2_DO_ENCRYPT), "SM2_do_encrypt"},
{ERR_FUNC(EC_F_SM2_GET_PUBLIC_KEY_DATA), "SM2_get_public_key_data"},
{ERR_FUNC(EC_F_SM2_KAP_COMPUTE_KEY), "SM2_KAP_compute_key"},
{ERR_FUNC(EC_F_SM2_KAP_CTX_INIT), "SM2_KAP_CTX_init"},
{ERR_FUNC(EC_F_SM2_KAP_FINAL_CHECK), "SM2_KAP_final_check"},
{ERR_FUNC(EC_F_SM2_KAP_PREPARE), "SM2_KAP_prepare"},
{ERR_FUNC(EC_F_TYPE1CURVE_EVAL_LINE_TEXTBOOK),
"type1curve_eval_line_textbook"},
{ERR_FUNC(EC_F_TYPE1CURVE_EVAL_MILLER_TEXTBOOK),
"type1curve_eval_miller_textbook"},
{ERR_FUNC(EC_F_TYPE1CURVE_PHI), "type1curve_phi"},
{0, NULL}
};
@@ -291,8 +273,6 @@ static ERR_STRING_DATA EC_str_reasons[] = {
{ERR_REASON(EC_R_GEN_MAC_FAILED), "gen mac failed"},
{ERR_REASON(EC_R_GET_PUBLIC_KEY_DATA_FAILURE),
"get public key data failure"},
{ERR_REASON(EC_R_GET_TYPE1CURVE_ZETA_FAILURE),
"get type1curve zeta failure"},
{ERR_REASON(EC_R_GF2M_NOT_SUPPORTED), "gf2m not supported"},
{ERR_REASON(EC_R_GROUP2PKPARAMETERS_FAILURE),
"group2pkparameters failure"},
@@ -335,9 +315,6 @@ static ERR_STRING_DATA EC_str_reasons[] = {
"invalid sm2 kap checksum value"},
{ERR_REASON(EC_R_INVALID_SPLIT_PARAMETER), "invalid split parameter"},
{ERR_REASON(EC_R_INVALID_TRINOMIAL_BASIS), "invalid trinomial basis"},
{ERR_REASON(EC_R_INVALID_TYPE1CURVE), "invalid type1curve"},
{ERR_REASON(EC_R_INVALID_TYPE1_CURVE), "invalid type1 curve"},
{ERR_REASON(EC_R_INVLID_TYPE1CURVE), "invlid type1curve"},
{ERR_REASON(EC_R_KDF_PARAMETER_ERROR), "kdf parameter error"},
{ERR_REASON(EC_R_KEYS_NOT_SET), "keys not set"},
{ERR_REASON(EC_R_MISSING_PARAMETERS), "missing parameters"},

View File

@@ -14,11 +14,6 @@
#include <openssl/x509.h>
#include "internal/evp_int.h"
const EVP_CIPHER *EVP_get_default_cipher(void)
{
return NULL;
}
/*
* use MD5 as default:
* X509_REQ_to_X509 x509_r2x.c
@@ -40,35 +35,3 @@ const EVP_MD *EVP_get_default_digest(void)
return NULL;
#endif
}
static void cipher_name_len(const EVP_CIPHER *cipher, const char *from,
const char *to, void *x)
{
*((int *)x) += strlen(EVP_CIPHER_name(cipher));
}
static void cipher_name(const EVP_CIPHER *cipher, const char *from,
const char *to, void *x)
{
strcat((char *)x, EVP_CIPHER_name(cipher));
}
char *EVP_get_ciphernames(int aliases)
{
char *ret = NULL;
int len = 0;
EVP_CIPHER_do_all_sorted(cipher_name_len, &len);
ret = OPENSSL_zalloc(len);
if (!ret) {
return NULL;
}
EVP_CIPHER_do_all_sorted(cipher_name, ret);
return ret;
}
char *EVP_get_digestnames(int aliases)
{
return "sm3:sha1:sha256";
}

View File

@@ -401,7 +401,7 @@ int SM2CiphertextValue_get_ECCCipher(const SM2CiphertextValue *cv,
* structure and prepared enough buffer to hold variable length
* ciphertext
*/
if (ref->L < ASN1_STRING_length(cv->ciphertext)) {
if (ref->L < (unsigned int)ASN1_STRING_length(cv->ciphertext)) {
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
GMAPI_R_BUFFER_TOO_SMALL);
return 0;
@@ -625,18 +625,6 @@ end:
return ret;
}
/*
ECCCipher *d2i_ECCCipher_bio(BIO *bp, ECCCipher **a)
{
return NULL;
}
ECCCipher *d2i_ECCCipher_fp(FILE *fp, ECCCipher **a)
{
return NULL;
}
*/
int i2d_ECCCipher(ECCCipher *a, unsigned char **pp)
{
int ret;
@@ -652,24 +640,18 @@ int i2d_ECCCipher(ECCCipher *a, unsigned char **pp)
return ret;
}
/*
int i2d_ECCCipher_bio(BIO *bp, ECCCipher *a)
{
return 0;
}
int i2d_ECCCipher_fp(FILE *fp, ECCCipher *a)
{
return 0;
}
*/
ECCSignature *d2i_ECCSignature(ECCSignature **a, const unsigned char **pp, long length)
{
ECCSignature *ret = NULL;
ECCSignature *sdf_sig = NULL;
ECDSA_SIG *sig = NULL;
/* FIXME: `a` not set */
(void)a;
if (!(sig = d2i_ECDSA_SIG(NULL, pp, length))) {
GMAPIerr(GMAPI_F_D2I_ECCSIGNATURE, ERR_R_EC_LIB);
goto end;
@@ -694,18 +676,6 @@ end:
return ret;
}
/*
ECCSignature *d2i_ECCSignature_bio(BIO *bp, ECCSignature **a)
{
return NULL;
}
ECCSignature *d2i_ECCSignature_fp(FILE *fp, ECCSignature **a)
{
return NULL;
}
*/
int i2d_ECCSignature(ECCSignature *a, unsigned char **pp)
{
int ret;
@@ -721,18 +691,6 @@ int i2d_ECCSignature(ECCSignature *a, unsigned char **pp)
return ret;
}
/*
int i2d_ECCSignature_bio(BIO *bp, ECCSignature *a)
{
return 0;
}
int i2d_ECCSignature_fp(FILE *fp, ECCSignature *a)
{
return 0;
}
*/
# ifndef OPENSSL_NO_ECIES
ECIES_CIPHERTEXT_VALUE *ECIES_CIPHERTEXT_VALUE_new_from_ECCCipher(
const ECCCipher *ref)

View File

@@ -309,7 +309,7 @@ int SM2CiphertextValue_get_ECCCIPHERBLOB(const SM2CiphertextValue *cv,
return 0;
}
if (blob->CipherLen < ASN1_STRING_length(cv->ciphertext)) {
if (blob->CipherLen < (unsigned int)ASN1_STRING_length(cv->ciphertext)) {
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
GMAPI_R_BUFFER_TOO_SMALL);
return 0;
@@ -378,7 +378,8 @@ int ECDSA_SIG_set_ECCSIGNATUREBLOB(ECDSA_SIG *sig, const ECCSIGNATUREBLOB *blob)
int ECDSA_SIG_get_ECCSIGNATUREBLOB(const ECDSA_SIG *sig, ECCSIGNATUREBLOB *blob)
{
if ((BN_num_bytes(sig->r) > sizeof(blob->r)) || (BN_num_bytes(sig->s) > sizeof(blob->s))) {
if (((size_t)BN_num_bytes(sig->r) > sizeof(blob->r))
|| ((size_t)BN_num_bytes(sig->s) > sizeof(blob->s))) {
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, GMAPI_R_INVALID_BIGNUM_LENGTH);
return 0;
}
@@ -545,13 +546,13 @@ int ECIES_CIPHERTEXT_VALUE_get_ECCCIPHERBLOB(const ECIES_CIPHERTEXT_VALUE *cv, E
goto end;
}
if (BN_num_bytes(x) > sizeof(blob->XCoordinate)) {
if ((size_t)BN_num_bytes(x) > sizeof(blob->XCoordinate)) {
GMAPIerr(GMAPI_F_ECIES_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, GMAPI_R_INVALID_SKF_EC_CIPHERTEXT);
goto end;
}
BN_bn2bin(x, blob->XCoordinate + sizeof(blob->XCoordinate) - BN_num_bytes(x));
if (BN_num_bytes(y) > sizeof(blob->YCoordinate)) {
if ((size_t)BN_num_bytes(y) > sizeof(blob->YCoordinate)) {
GMAPIerr(GMAPI_F_ECIES_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, GMAPI_R_INVALID_SKF_EC_CIPHERTEXT);
goto end;
}
@@ -585,6 +586,9 @@ ECCCIPHERBLOB *d2i_ECCCIPHERBLOB(ECCCIPHERBLOB **a, const unsigned char **pp, lo
ECCCIPHERBLOB *blob = NULL;
SM2CiphertextValue *cv = NULL;
/* FIXME: set `a` */
(void)a;
if (!(cv = d2i_SM2CiphertextValue(NULL, pp, length))) {
GMAPIerr(GMAPI_F_D2I_ECCCIPHERBLOB, ERR_R_SM2_LIB);
goto end;
@@ -632,6 +636,9 @@ ECCSIGNATUREBLOB *d2i_ECCSIGNATUREBLOB(ECCSIGNATUREBLOB **a, const unsigned char
ECCSIGNATUREBLOB *blob = NULL;
ECDSA_SIG *sig = NULL;
/* FIXME: set `a` */
(void)a;
if (!(sig = d2i_ECDSA_SIG(NULL, pp, length))) {
GMAPIerr(GMAPI_F_D2I_ECCSIGNATUREBLOB, ERR_R_EC_LIB);
goto end;

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -42,8 +42,6 @@ static ERR_STRING_DATA SDF_str_functs[] = {
{ERR_FUNC(SDF_F_SDF_EXPORTSIGNPUBLICKEY_RSA),
"SDF_ExportSignPublicKey_RSA"},
{ERR_FUNC(SDF_F_SDF_EXTERNALENCRYPT_ECC), "SDF_ExternalEncrypt_ECC"},
{ERR_FUNC(SDF_F_SDF_EXTERNALPRIVATEKEYOPERATION_RSA),
"SDF_EXTERNALPRIVATEKEYOPERATION_RSA"},
{ERR_FUNC(SDF_F_SDF_EXTERNALPUBLICKEYOPERATION_RSA),
"SDF_ExternalPublicKeyOperation_RSA"},
{ERR_FUNC(SDF_F_SDF_EXTERNALVERIFY_ECC), "SDF_ExternalVerify_ECC"},
@@ -106,29 +104,23 @@ static ERR_STRING_DATA SDF_str_reasons[] = {
{ERR_REASON(SDF_R_FILE_ALREADY_EXIST), "file already exist"},
{ERR_REASON(SDF_R_FILE_NOT_EXIST), "file not exist"},
{ERR_REASON(SDF_R_HARDWARE_ERROR), "hardware error"},
{ERR_REASON(SDF_R_INVALID_CIPHER_ALGOR), "invalid cipher algor"},
{ERR_REASON(SDF_R_INVALID_DIGEST_ALGOR), "invalid digest algor"},
{ERR_REASON(SDF_R_INVALID_FILE_OFFSET), "invalid file offset"},
{ERR_REASON(SDF_R_INVALID_FILE_SIZE), "invalid file size"},
{ERR_REASON(SDF_R_INVALID_INPUT_ARGUMENT), "invalid input argument"},
{ERR_REASON(SDF_R_INVALID_KEY), "invalid key"},
{ERR_REASON(SDF_R_INVALID_KEY_LENGTH), "invalid key length"},
{ERR_REASON(SDF_R_INVALID_KEY_TYPE), "invalid key type"},
{ERR_REASON(SDF_R_INVALID_OUTPUT_ARGUMENT), "invalid output argument"},
{ERR_REASON(SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH),
"invalid sansec ecccipher length"},
{ERR_REASON(SDF_R_INVALID_SDF_LIBRARY), "invalid sdf library"},
{ERR_REASON(SDF_R_INVALID_SESSION_HANDLE), "invalid session handle"},
{ERR_REASON(SDF_R_INVALID_SM2_CIPHERTEXT_LENGTH),
"invalid sm2 ciphertext length"},
{ERR_REASON(SDF_R_KEY_NOT_EXIST), "key not exist"},
{ERR_REASON(SDF_R_LOAD_LIBRARY_FAILURE), "load library failure"},
{ERR_REASON(SDF_R_MAC_ERROR), "mac error"},
{ERR_REASON(SDF_R_METHOD_OPERATION_FAILURE), "method operation failure"},
{ERR_REASON(SDF_R_MULTI_STEP_OPERATION_ERROR),
"multi step operation error"},
{ERR_REASON(SDF_R_NOT_IMPLEMENTED), "not implemented"},
{ERR_REASON(SDF_R_NOT_INITIALIZED), "not initialized"},
{ERR_REASON(SDF_R_NOT_SUPPORTED), "not supported"},
{ERR_REASON(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR),
"not supported cipher algor"},
{ERR_REASON(SDF_R_NOT_SUPPORTED_DIGEST_ALGOR),
@@ -139,7 +131,6 @@ static ERR_STRING_DATA SDF_str_reasons[] = {
"no private key access right"},
{ERR_REASON(SDF_R_OPEN_DEVICE_FAILURE), "open device failure"},
{ERR_REASON(SDF_R_OPEN_SESSION_FAILURE), "open session failure"},
{ERR_REASON(SDF_R_OPERATION_FAILED), "operation failed"},
{ERR_REASON(SDF_R_OPERATION_NOT_SUPPORTED), "operation not supported"},
{ERR_REASON(SDF_R_PRIVATE_KEY_OPERATION_FAILURE),
"private key operation failure"},
@@ -247,9 +238,6 @@ static ERR_STRING_DATA SDF_str_reasons[] = {
{ERR_REASON(SDF_R_SANSEC_SOCKET_TIMEOUT), "sansec socket timeout"},
{ERR_REASON(SDF_R_SANSEC_SYNC_ERROR), "sansec sync error"},
{ERR_REASON(SDF_R_SANSEC_SYNC_LOGIN_ERROR), "sansec sync login error"},
{ERR_REASON(SDF_R_SDF_METHOD_RETURN_FAILURE),
"sdf method return failure"},
{ERR_REASON(SDF_R_SDF_OPERATION_FAILED), "sdf operation failed"},
{ERR_REASON(SDF_R_SIGNING_FAILURE), "signing failure"},
{ERR_REASON(SDF_R_SUCCESS), "success"},
{ERR_REASON(SDF_R_SYMMETRIC_OPERATION_FAILURE),

View File

@@ -235,7 +235,12 @@ int SDF_ImportKey(
unsigned int uiKeyLength,
void **phKeyHandle)
{
return 0;
(void)hSessionHandle;
(void)pucKey;
(void)uiKeyLength;
(void)phKeyHandle;
SDFerr(SDF_F_SDF_IMPORTKEY, SDF_R_NOT_IMPLEMENTED);
return SDR_NOTSUPPORT;
}
int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen)

View File

@@ -124,7 +124,7 @@ static SDF_ERR_REASON sdf_errors[] = {
static unsigned long sdf_get_error_reason(int err)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sdf_errors); i++) {
if (err == sdf_errors[i].err) {
return sdf_errors[i].reason;

View File

@@ -100,7 +100,7 @@ static SDF_ALGOR_PAIR sansec_ciphers[] = {
static unsigned int sansec_cipher_vendor2std(unsigned int vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_ciphers); i++) {
if (vendor_id == sansec_ciphers[i].vendor_id) {
return sansec_ciphers[i].std_id;
@@ -111,7 +111,7 @@ static unsigned int sansec_cipher_vendor2std(unsigned int vendor_id)
static unsigned int sansec_cipher_std2vendor(unsigned int std_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_ciphers); i++) {
if (std_id == sansec_ciphers[i].std_id) {
return sansec_ciphers[i].vendor_id;
@@ -123,7 +123,7 @@ static unsigned int sansec_cipher_std2vendor(unsigned int std_id)
static unsigned int sansec_cipher_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_ciphers); i++) {
if (vendor_cap & sansec_ciphers[i].vendor_id) {
@@ -146,7 +146,7 @@ static SDF_ALGOR_PAIR sansec_digests[] = {
static unsigned int sansec_digest_vendor2std(unsigned int vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_digests); i++) {
if (vendor_id == sansec_digests[i].vendor_id) {
return sansec_digests[i].std_id;
@@ -157,7 +157,7 @@ static unsigned int sansec_digest_vendor2std(unsigned int vendor_id)
static unsigned int sansec_digest_std2vendor(unsigned int std_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_digests); i++) {
if (std_id == sansec_digests[i].std_id) {
return sansec_digests[i].vendor_id;
@@ -169,7 +169,7 @@ static unsigned int sansec_digest_std2vendor(unsigned int std_id)
static unsigned int sansec_digest_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_digests); i++) {
if (vendor_cap & sansec_digests[i].vendor_id) {
@@ -192,7 +192,7 @@ static SDF_ALGOR_PAIR sansec_pkeys[] = {
static unsigned int sansec_pkey_vendor2std(unsigned int vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_pkeys); i++) {
if (vendor_id == sansec_pkeys[i].vendor_id) {
return sansec_pkeys[i].std_id;
@@ -203,7 +203,7 @@ static unsigned int sansec_pkey_vendor2std(unsigned int vendor_id)
static unsigned int sansec_pkey_std2vendor(unsigned int std_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_pkeys); i++) {
if (std_id == sansec_pkeys[i].std_id) {
return sansec_pkeys[i].vendor_id;
@@ -215,7 +215,7 @@ static unsigned int sansec_pkey_std2vendor(unsigned int std_id)
static unsigned int sansec_pkey_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(sansec_pkeys); i++) {
if (vendor_cap & sansec_pkeys[i].vendor_id) {
@@ -341,7 +341,7 @@ static SDF_ERR_REASON sansec_errors[] = {
static unsigned long sansec_get_error_reason(int err)
{
int i = 0;
size_t i = 0;
for (i = 0; i < OSSL_NELEM(sansec_errors); i++) {
if (err == sansec_errors[i].err) {
return sansec_errors[i].reason;

View File

@@ -356,7 +356,12 @@ ULONG SKF_PrintECCSignature(BIO *out, ECCSIGNATUREBLOB *blob)
ULONG DEVAPI SKF_GetAlgorName(ULONG ulAlgID, LPSTR *szName)
{
return SAR_OK;
char *name;
if ((name = skf_algor_name(ulAlgID)) != NULL) {
*szName = (LPSTR)&name;
return SAR_OK;
}
return SAR_FAIL;
}
ULONG DEVAPI SKF_PrintErrorString(BIO *out, ULONG ulError)

View File

@@ -81,7 +81,7 @@ static SKF_ALGOR_PAIR wisec_ciphers[] = {
static ULONG wisec_get_cipher_algor(ULONG vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_ciphers); i++) {
if (vendor_id == wisec_ciphers[i].vendor_id) {
return wisec_ciphers[i].std_id;
@@ -93,7 +93,7 @@ static ULONG wisec_get_cipher_algor(ULONG vendor_id)
static ULONG wisec_get_cipher_cap(ULONG vendor_cap)
{
ULONG std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_ciphers); i++) {
if (vendor_cap & wisec_ciphers[i].vendor_id) {
std_cap |= wisec_ciphers[i].std_id;
@@ -110,7 +110,7 @@ static SKF_ALGOR_PAIR wisec_digests[] = {
static ULONG wisec_get_digest_algor(ULONG vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_digests); i++) {
if (vendor_id == wisec_digests[i].vendor_id) {
return wisec_digests[i].std_id;
@@ -122,7 +122,7 @@ static ULONG wisec_get_digest_algor(ULONG vendor_id)
static ULONG wisec_get_digest_cap(ULONG vendor_cap)
{
ULONG std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_digests); i++) {
if (vendor_cap & wisec_digests[i].vendor_id) {
std_cap |= wisec_digests[i].std_id;
@@ -143,7 +143,7 @@ static SKF_ALGOR_PAIR wisec_pkeys[] = {
static ULONG wisec_get_pkey_algor(ULONG vendor_id)
{
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_pkeys); i++) {
if (vendor_id == wisec_pkeys[i].vendor_id) {
return wisec_pkeys[i].std_id;
@@ -155,7 +155,7 @@ static ULONG wisec_get_pkey_algor(ULONG vendor_id)
static ULONG wisec_get_pkey_cap(ULONG vendor_cap)
{
ULONG std_cap = 0;
int i;
size_t i;
for (i = 0; i < OSSL_NELEM(wisec_pkeys); i++) {
if (vendor_cap & wisec_pkeys[i].vendor_id) {
std_cap |= wisec_pkeys[i].std_id;
@@ -178,7 +178,7 @@ static SKF_ERR_REASON wisec_errors[] = {
static unsigned long wisec_get_error_reason(ULONG err)
{
int i = 0;
size_t i = 0;
for (i = 0; i < OSSL_NELEM(wisec_errors); i++) {
if (err == wisec_errors[i].err) {
return wisec_errors[i].reason;

View File

@@ -248,7 +248,7 @@ int SM2_encrypt(int type, const unsigned char *in, size_t inlen,
if (!out) {
*outlen = clen;
return 1;
} else if (*outlen < clen) {
} else if (*outlen < (size_t)clen) {
SM2err(SM2_F_SM2_ENCRYPT, SM2_R_BUFFER_TOO_SMALL);
return 0;
}

View File

@@ -563,7 +563,7 @@ int SM2_KAP_final_check(SM2_KAP_CTX *ctx, const unsigned char *checksum,
size_t checksumlen)
{
if (ctx->do_checksum) {
if (checksumlen != EVP_MD_size(ctx->checksum_md)) {
if (checksumlen != (size_t)EVP_MD_size(ctx->checksum_md)) {
ECerr(EC_F_SM2_KAP_FINAL_CHECK, EC_R_INVALID_SM2_KAP_CHECKSUM_LENGTH);
return 0;
}
@@ -581,6 +581,18 @@ int SM2_compute_share_key(unsigned char *out, size_t *outlen,
const EC_POINT *peer_pk, const unsigned char *peer_z, size_t peer_zlen,
const unsigned char *z, size_t zlen, EC_KEY *sk, int initiator)
{
memset(out, 1, *outlen);
return 1;
(void)out;
(void)outlen;
(void)peer_ephem;
(void)ephem;
(void)peer_pk;
(void)peer_z;
(void)peer_zlen;
(void)z;
(void)zlen;
(void)sk;
(void)initiator;
ECerr(EC_F_SM2_COMPUTE_SHARE_KEY, EC_R_NOT_IMPLEMENTED);
return 0;
}

View File

@@ -231,7 +231,7 @@ int SM2_compute_id_digest(const EVP_MD *md, const char *id, size_t idlen,
*outlen = EVP_MD_size(md);
return 1;
}
if (*outlen < EVP_MD_size(md)) {
if (*outlen < (size_t)EVP_MD_size(md)) {
ECerr(EC_F_SM2_COMPUTE_ID_DIGEST, EC_R_BUFFER_TOO_SMALL);
return 0;
}

View File

@@ -96,26 +96,3 @@ struct sm2_kap_ctx_st {
};
int SM2_ciphertext_size(const EC_KEY *ec_key, size_t inlen);
struct SM2_COSIGNER1_SHARE_st {
BIGNUM *a;
BIGNUM *b;
};
struct SM2_COSIGNER2_SHARE_st {
BIGNUM *a;
BIGNUM *b;
};
struct SM2_COSIGNER1_PROOF_st {
BIGNUM *a;
BIGNUM *b;
};
struct SM2_COSIGNER2_PROOF_st {
BIGNUM *a;
BIGNUM *b;
};

View File

@@ -67,6 +67,12 @@ static void sm2_sign_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
BN_clear_free(bn);
CRYPTO_set_ex_data(ad, sm2_sign_idx, NULL);
}
(void)parent;
(void)ptr;
(void)idx;
(void)argl;
(void)argp;
}
static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **xp)
@@ -542,6 +548,10 @@ int SM2_sign_ex(int type, const unsigned char *dgst, int dgstlen,
{
ECDSA_SIG *s;
if (type != NID_undef) {
return 0;
}
RAND_seed(dgst, dgstlen);
if (!(s = SM2_do_sign_ex(dgst, dgstlen, k, x, ec_key))) {
@@ -570,6 +580,10 @@ int SM2_verify(int type, const unsigned char *dgst, int dgstlen,
int derlen = -1;
int ret = -1;
if (type != NID_undef) {
return ret;
}
if (!(s = ECDSA_SIG_new())) {
return ret;
}

356
crypto/sm3/sm3_mb.c Executable file
View File

@@ -0,0 +1,356 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <string.h>
#include <openssl/sm3.h>
#include "internal/rotate.h"
#include "internal/byteorder.h"
#include "modes_lcl.h"
#include <immintrin.h>
typedef struct {
uint32_t A[8];
uint32_t B[8];
uint32_t C[8];
uint32_t D[8];
uint32_t E[8];
uint32_t F[8];
uint32_t G[8];
uint32_t H[8];
} SM3_AVX2_CTX;
void sm3_mb_init(sm3_mb_ctx_t *ctx)
{
int i;
memset(ctx, 0, sizeof(*ctx));
for (i = 0; i < 8; i++) {
ctx->A[i] = 0x7380166F;
ctx->B[i] = 0x4914B2B9;
ctx->C[i] = 0x172442D7;
ctx->D[i] = 0xDA8A0600;
ctx->E[i] = 0xA96F30BC;
ctx->F[i] = 0x163138AA;
ctx->G[i] = 0xE38DEE4D;
ctx->H[i] = 0xB0FB0E4E;
}
}
void sm3_update(sm3_ctx_t *ctx, const unsigned char *data, size_t data_len)
{
size_t blocks = data_len / SM3_BLOCK_SIZE;
if (ctx->num) {
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
if (data_len < left) {
memcpy(ctx->block + ctx->num, data, data_len);
ctx->num += data_len;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sm3_compress(ctx->digest, ctx->block);
ctx->nblocks++;
data += left;
data_len -= left;
}
}
sm3_compress_blocks(ctx->digest, data, blocks);
ctx->nblocks += blocks;
data += SM3_BLOCK_SIZE * blocks;
data_len -= SM3_BLOCK_SIZE * blocks;
ctx->num = data_len;
if (data_len) {
memcpy(ctx->block, data, data_len);
}
}
void sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
{
int i;
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= SM3_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 1);
sm3_compress(ctx->digest, ctx->block);
memset(ctx->block, 0, SM3_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sm3_compress(ctx->digest, ctx->block);
for (i = 0; i < 8; i++) {
PUTU32(digest + i*4, ctx->digest[i]);
}
}
#define VPUT(addr, X) _mm256_store_si256((__m256i *)(addr), _mm256_load_si256((__m256i *)(addr), X))
# define _mm256_rotl_epi32(X, i) \
_mm256_xor_si256( \
_mm256_slli_epi32(a, i), \
_mm256_srli_epi32(a, 32 - i))
/* P0(X) = X ^ (X <<< 9) ^ (X <<< 17) */
#define P0(X) \
_mm256_xor_si256( \
_mm256_xor_si256(X, \
_mm256_rotl_epi32(X, 9)), \
_mm256_rotl_epi32(X, 17))
/* P1(X) = X ^ (X <<< 15) ^ (X <<< 23) */
#define P1(X) \
_mm256_xor_si256( \
_mm256_xor_si256(X, \
_mm256_rotl_epi32(X, 15)), \
_mm256_rotl_epi32(X, 23))
/* FF0(X, Y, Z) = X ^ Y ^ Z */
#define FF00(X,Y,Z) \
_mm256_xor_si256(X, _mm256_xor_si256(Y, Z))
/* FF16(X, Y, Z) = (X and Y) or (X and Z) or (Y and Z) */
#define FF16(X,Y,Z) \
_mm256_or_si256( \
_mm256_and_si256(X, Y), \
_mm256_or_si256( \
_mm256_and_si256(X, Z), \
_mm256_and_si256(Y, Z)))
/* GG00(X, Y, Z) = X ^ Y ^ Z */
#define G00(X,Y,Z) F00(X,Y,Z)
/* #define GG16(x,y,z) ((((y)^(z)) & (x)) ^ (z)) */
#define GG16(X,Y,Z) \
_mm256_xor_si256(Z, \
_mm256_and_si256(X, \
_mm256_xor_si256(Y, Z)))
#define R(A, B, C, D, E, F, G, H, xx) \
SS1 = ROL32((ROL32(A, 12) + E + K[j]), 7); \
A12 = VROL(A, 12); \
SS1 = VADD(A12, E); \
SS1 = VADD(SS1, K(j)); \
SS1 = VROL(SS1, 7); \
SS2 = VXOR(SS1, A12); \
TT1 = FF##xx(A, B, C);
TT1 = VADD(TT1, D);
TT1 = VADD(TT1, SS2);
TT1 = VXOR(W(j), W(j+4));
TT2 =
SS2 = SS1 ^ ROL32(A, 12); \
TT1 = FF##xx(A, B, C) + D + SS2 + (W[j] ^ W[j + 4]); \
TT2 = GG##xx(E, F, G) + H + SS1 + W[j]; \
B = ROL32(B, 9); \
H = TT1; \
F = ROL32(F, 19); \
D = P0(TT2); \
j++
#define R8(A, B, C, D, E, F, G, H, xx) \
R1(A, B, C, D, E, F, G, H, xx); \
R1(H, A, B, C, D, E, F, G, xx); \
R1(G, H, A, B, C, D, E, F, xx); \
R1(F, G, H, A, B, C, D, E, xx); \
R1(E, F, G, H, A, B, C, D, xx); \
R1(D, E, F, G, H, A, B, C, xx); \
R1(C, D, E, F, G, H, A, B, xx); \
R1(B, C, D, E, F, G, H, A, xx)
#define T00 0x79cc4519U
#define T16 0x7a879d8aU
#define K0 0x79cc4519U
#define K1 0xf3988a32U
#define K2 0xe7311465U
#define K3 0xce6228cbU
#define K4 0x9cc45197U
#define K5 0x3988a32fU
#define K6 0x7311465eU
#define K7 0xe6228cbcU
#define K8 0xcc451979U
#define K9 0x988a32f3U
#define K10 0x311465e7U
#define K11 0x6228cbceU
#define K12 0xc451979cU
#define K13 0x88a32f39U
#define K14 0x11465e73U
#define K15 0x228cbce6U
#define K16 0x9d8a7a87U
#define K17 0x3b14f50fU
#define K18 0x7629ea1eU
#define K19 0xec53d43cU
#define K20 0xd8a7a879U
#define K21 0xb14f50f3U
#define K22 0x629ea1e7U
#define K23 0xc53d43ceU
#define K24 0x8a7a879dU
#define K25 0x14f50f3bU
#define K26 0x29ea1e76U
#define K27 0x53d43cecU
#define K28 0xa7a879d8U
#define K29 0x4f50f3b1U
#define K30 0x9ea1e762U
#define K31 0x3d43cec5U
#define K32 0x7a879d8aU
#define K33 0xf50f3b14U
#define K34 0xea1e7629U
#define K35 0xd43cec53U
#define K36 0xa879d8a7U
#define K37 0x50f3b14fU
#define K38 0xa1e7629eU
#define K39 0x43cec53dU
#define K40 0x879d8a7aU
#define K41 0x0f3b14f5U
#define K42 0x1e7629eaU
#define K43 0x3cec53d4U
#define K44 0x79d8a7a8U
#define K45 0xf3b14f50U
#define K46 0xe7629ea1U
#define K47 0xcec53d43U
#define K48 0x9d8a7a87U
#define K49 0x3b14f50fU
#define K50 0x7629ea1eU
#define K51 0xec53d43cU
#define K52 0xd8a7a879U
#define K53 0xb14f50f3U
#define K54 0x629ea1e7U
#define K55 0xc53d43ceU
#define K56 0x8a7a879dU
#define K57 0x14f50f3bU
#define K58 0x29ea1e76U
#define K59 0x53d43cecU
#define K60 0xa7a879d8U
#define K61 0x4f50f3b1U
#define K62 0x9ea1e762U
#define K63 0x3d43cec5U
static void sm3_avx2_compress_blocks(uint32_t digest[8][8],
const unsigned char *data, size_t offset[8], size_t blocks)
{
VREG A, B, C, D, E, F, G, H;
VREG SS1, SS2, TT1, TT2;
uint32_t W[68][8];
int j;
while (blocks--) {
A = _mm256_load_si256((__m256i *)ctx->A);
B = _mm256_load_si256((__m256i *)ctx->B);
C = _mm256_load_si256((__m256i *)ctx->C);
D = _mm256_load_si256((__m256i *)ctx->D);
E = _mm256_load_si256((__m256i *)ctx->E);
F = _mm256_load_si256((__m256i *)ctx->F);
G = _mm256_load_si256((__m256i *)ctx->G);
H = _mm256_load_si256((__m256i *)ctx->H);
for (j = 0; j < 16; j++) {
W = _mm256_i32gather_epi32((int *)data, I, 4);
W = _mm255_shuffle_epi8(W, vindex_swap);
_mm256_store_si256((__m256i *)(w + j * 8), W);
}
for (; j < 68; j++) {
PUTV32(W[j],
P1(GETV32(W[j - 16]) ^ GETV32(W[j - 9]) ^ ROL32(GETV32(W[j - 3]), 15))
^ ROL32(GETV32(W[j - 13]), 7) ^ W[j - 6];
}
j = 0;
VPUT(ctx->A, A);
VPUT(ctx->B, B);
VPUT(ctx->C, C);
VPUT(ctx->D, D);
VPUT(ctx->E, E);
VPUT(ctx->F, F);
VPUT(ctx->G, G);
VPUT(ctx->H, H);
data += 64;
}
}
void sm3_compress(uint32_t digest[8], const unsigned char block[64])
{
return sm3_compress_blocks(digest, block, 1);
}
void sm3(const unsigned char *msg, size_t msglen,
unsigned char dgst[SM3_DIGEST_LENGTH])
{
sm3_ctx_t ctx;
sm3_init(&ctx);
sm3_update(&ctx, msg, msglen);
sm3_final(&ctx, dgst);
memset(&ctx, 0, sizeof(sm3_ctx_t));
}

221
crypto/sm3/sm3_simd.c Normal file
View File

@@ -0,0 +1,221 @@
#include <stdio.h>
#include <string.h>
#include <x86intrin.h>
#include <immintrin.h>
# define GETU32(p) \
((unsigned int)(p)[0] << 24 | \
(unsigned int)(p)[1] << 16 | \
(unsigned int)(p)[2] << 8 | \
(unsigned int)(p)[3])
# define ROL32(X,n) \
(((X) << (n)) | (((X) & 0xffffffff) >> (32-(n))))
# define P1(X) \
((X) ^ ROL32((X), 15) ^ ROL32((X), 23))
# define _mm_rotl_epi32(X, i) _mm_xor_si128( \
_mm_slli_epi32((X), i), _mm_srli_epi32((X), 32 - i))
# define P1_128(X) \
T = _mm_rotl_epi32((X), (23-15)); \
T = _mm_xor_si128(T, (X)); \
T = _mm_rotl_epi32(T, 15); \
X = _mm_xor_si128((X), T)
/*
# define GETU128(p) \
_mm_setr_epi32( \
*((unsigned int *)(((unsigned char *)(p)) + 0)), \
*((unsigned int *)(((unsigned char *)(p)) + 4)), \
*((unsigned int *)(((unsigned char *)(p)) + 8)), \
*((unsigned int *)(((unsigned char *)(p)) + 12)))
*/
# define GETU128(p) \
_mm_loadu_si128((__m128i *)(p))
/*
* _mm_lddqu_si128 SSE3 <pmmintrin.h> faster when data crosses a cache line boundary
* _mm_load_si128 SSE2 <emmintrin.h>
* _mm_loadu_si128 SSE2 <emmintrin.h> no aligned mem
* _mm_loadu_si32 SSE2 <immintrin.h> load u32 to lower 32-bit of xmm
* _mm_loadl_epi64 SSE2 <emmintrin.h> load u64 to lower 64-bit of xmm
* _mm_loadu_si64 SSE <immintrin.h> load unaligned u64 to lower 64-bit of xmm
*/
static void print_words(unsigned int W[68])
{
int i;
for (i = 0; i < 68; i++) {
printf("%08x ", W[i]);
if (i % 4 == 3)
printf("\n");
}
printf("\n");
}
static void expand(unsigned int W[68], const unsigned char data[64])
{
int j;
unsigned int X;
unsigned int T;
for (j = 0; j < 16; j++)
W[j] = GETU32(data + j*4);
for (; j < 68; j++) {
W[j] = P1(W[j - 16] ^ W[j - 9] ^ ROL32(W[j - 3], 15))
^ ROL32(W[j - 13], 7) ^ W[j - 6];
}
}
static void print_mm(const char *s, __m128i X) {
unsigned int buf[4];
int i;
_mm_store_si128((__m128i *)buf, X);
printf("%s = ", s);
for (i = 0; i < 4; i++) {
printf("%08x ", buf[i]);
}
printf("\n");
}
static __m128i V;
static __m128i M;
static __m128i M2;
static void expand_simd(unsigned int W[68], const unsigned char data[64])
{
__m128i X, T, R;
int j;
const uint32_t *p = (uint32_t *)data;
for (j = 0; j < 16; j += sizeof(X)/sizeof(W[0])) {
X = _mm_loadu_si128((__m128i *)(data + j * 4));
X = _mm_shuffle_epi8(X, V);
_mm_storeu_si128((__m128i *)(W + j), X);
}
for (j = 16; j < 68; j += 4) {
/* X = (W[j - 3], W[j - 2], W[j - 1], 0) */
X = _mm_loadu_si128((__m128i *)(W + j - 3));
X = _mm_andnot_si128(M, X);
X = _mm_rotl_epi32(X, 15);
T = _mm_loadu_si128((__m128i *)(W + j - 9));
X = _mm_xor_si128(X, T);
T = _mm_loadu_si128((__m128i *)(W + j - 16));
X = _mm_xor_si128(X, T);
/* P1() */
T = _mm_rotl_epi32(X, (23 - 15));
T = _mm_xor_si128(T, X);
T = _mm_rotl_epi32(T, 15);
X = _mm_xor_si128(X, T);
T = _mm_loadu_si128((__m128i *)(W + j - 13));
T = _mm_rotl_epi32(T, 7);
X = _mm_xor_si128(X, T);
T = _mm_loadu_si128((__m128i *)(W + j - 6));
X = _mm_xor_si128(X, T);
/* W[j + 3] ^= P1(ROL32(W[j + 1], 15)) */
R = _mm_shuffle_epi32(X, 0);
R = _mm_and_si128(R, M);
T = _mm_rotl_epi32(R, 15);
T = _mm_xor_si128(T, R);
T = _mm_rotl_epi32(T, 9);
R = _mm_xor_si128(R, T);
R = _mm_rotl_epi32(R, 6);
X = _mm_xor_si128(X, R);
_mm_storeu_si128((__m128i *)(W + j), X);
}
}
void test()
{
unsigned char block[64] __attribute__((aligned(0x10))) = {0};
unsigned int W1[68] __attribute__((aligned(0x10))) = {0};
unsigned int W2[68] __attribute__((aligned(0x10))) = {0};
int i;
for (i = 0; i < 64; i++)
block[i] = i;
printf("W1 = %08x\n", &W1[0]);
printf("W2 = %08x\n", &W2[0]);
expand(W1, block);
expand_simd(W2, block);
for (i = 0; i < 68; i++) {
printf("%08x ", W1[i]);
if (i % 4 == 3) printf("\n");
}
printf("\n");
for (i = 0; i < 68; i++) {
printf("%08x ", W2[i]);
if (i % 4 == 3) printf("\n");
}
printf("\n");
for (i = 0; i < 68; i++) {
if (W1[i] != W2[i]) {
printf("failed on %d\n" , i);
printf("%08x\n", W1[i]);
printf("%08x\n", W2[i]);
}
}
}
int main(int argc, char **argv)
{
unsigned char block[64] __attribute__((aligned(0x10))) = {0};
unsigned int W[68] __attribute__((aligned(0x10))) = {0};
int i;
V = _mm_setr_epi8(3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12);
M = _mm_setr_epi32(0, 0, 0, 0xffffffff);
for (i = 0; i < 64; i++)
block[i] = i;
/*
for (i = 0; i < 1000000; i++) {
expand(W, block);
}
*/
/*
for (i = 0; i < 1000000; i++) {
expand_simd(W, block);
}
*/
test();
return 0;
}

View File

@@ -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;
}

View File

@@ -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
View File

@@ -0,0 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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)
*/

View File

@@ -78,6 +78,7 @@ void sms4_ede_encrypt(const unsigned char in[16], unsigned char out[16],
void sms4_ede_ecb_encrypt(const unsigned char *in, unsigned char *out,
const sms4_ede_key_t *key, int enc)
{
(void)enc;
sms4_ede_encrypt(in, out, key);
}