mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 17:06:25 +08:00
skf
This commit is contained in:
@@ -79,27 +79,17 @@ int EC_KEY_set_ECCPUBLICKEYBLOB(EC_KEY *ec_key, const ECCPUBLICKEYBLOB *blob)
|
||||
int ret = 0;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
int nbytes;
|
||||
|
||||
if (blob->BitLen != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 1
|
||||
nbytes = 64;
|
||||
#else
|
||||
/* we assume that the coordinates are stored in big-endian format.
|
||||
* but if it is not
|
||||
*/
|
||||
nbytes = (blob->BitLen + 7)/8;
|
||||
#endif
|
||||
|
||||
if (!(x = BN_bin2bn(blob->XCoordinate, nbytes, NULL))) {
|
||||
if (!(x = BN_bin2bn(blob->XCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(y = BN_bin2bn(blob->YCoordinate, nbytes, NULL))) {
|
||||
if (!(y = BN_bin2bn(blob->YCoordinate, ECC_MAX_YCOORDINATE_BITS_LEN/8, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
@@ -118,15 +108,13 @@ end:
|
||||
int EC_KEY_get_ECCPUBLICKEYBLOB(EC_KEY *ec_key, ECCPUBLICKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
int nbytes;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
|
||||
const EC_POINT *point = EC_KEY_get0_public_key(ec_key);
|
||||
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
if (nbytes > ECC_MAX_MODULUS_BITS_LEN/8) {
|
||||
if (EC_GROUP_get_degree(group) > ECC_MAX_MODULUS_BITS_LEN) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
@@ -153,11 +141,11 @@ int EC_KEY_get_ECCPUBLICKEYBLOB(EC_KEY *ec_key, ECCPUBLICKEYBLOB *blob)
|
||||
|
||||
memset(blob, 0, sizeof(*blob));
|
||||
blob->BitLen = EC_GROUP_get_degree(group);
|
||||
if (!BN_bn2bin(x, blob->XCoordinate + nbytes - BN_num_bytes(x))) {
|
||||
if (!BN_bn2bin(x, blob->XCoordinate + (ECC_MAX_XCOORDINATE_BITS_LEN+7)/8 - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, blob->YCoordinate + nbytes - BN_num_bytes(y))) {
|
||||
if (!BN_bn2bin(y, blob->YCoordinate + (ECC_MAX_YCOORDINATE_BITS_LEN+7)/8 - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
@@ -193,20 +181,17 @@ int EC_KEY_set_ECCPRIVATEKEYBLOB(EC_KEY *ec_key, const ECCPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *d = NULL;
|
||||
int nbytes;
|
||||
|
||||
//FIXME: is this right?
|
||||
if (blob->BitLen != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
nbytes = (blob->BitLen + 7)/8;
|
||||
|
||||
if (!(d = BN_bin2bn(blob->PrivateKey, nbytes, NULL))) {
|
||||
if (!(d = BN_bin2bn(blob->PrivateKey, (ECC_MAX_MODULUS_BITS_LEN+7)/8, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_private_key(ec_key, d)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_PRIVATE_KEY);
|
||||
goto end;
|
||||
@@ -220,42 +205,28 @@ end:
|
||||
|
||||
int EC_KEY_get_ECCPRIVATEKEYBLOB(EC_KEY *ec_key, ECCPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *order = NULL;
|
||||
const BIGNUM *d;
|
||||
int nbytes;
|
||||
|
||||
if (!(order = BN_new())) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EC_GROUP_get_order(EC_KEY_get0_group(ec_key), order, NULL)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
nbytes = BN_num_bytes(order);
|
||||
if (nbytes > ECC_MAX_MODULUS_BITS_LEN/8) {
|
||||
if (EC_GROUP_get_degree(EC_KEY_get0_group(ec_key)) > ECC_MAX_MODULUS_BITS_LEN) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(d = EC_KEY_get0_private_key(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_EC_KEY);
|
||||
goto end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(d, blob->PrivateKey + nbytes - BN_num_bytes(d))) {
|
||||
memset(blob, 0, sizeof(*blob));
|
||||
|
||||
blob->BitLen = EC_GROUP_get_degree(EC_KEY_get0_group(ec_key));
|
||||
|
||||
if (!BN_bn2bin(d, blob->PrivateKey + (ECC_MAX_MODULUS_BITS_LEN+7)/8 - BN_num_bytes(d))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
BN_free(order);
|
||||
return ret;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SM2CiphertextValue *SM2CiphertextValue_new_from_ECCCIPHERBLOB(
|
||||
@@ -336,18 +307,21 @@ int SM2CiphertextValue_get_ECCCIPHERBLOB(const SM2CiphertextValue *cv,
|
||||
}
|
||||
|
||||
if (blob->CipherLen < ASN1_STRING_length(cv->ciphertext)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(cv->xCoordinate, blob->XCoordinate +
|
||||
ECC_MAX_XCOORDINATE_BITS_LEN/8 - BN_num_bytes(cv->xCoordinate))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
return 0;
|
||||
}
|
||||
if (!BN_bn2bin(cv->yCoordinate, blob->YCoordinate +
|
||||
ECC_MAX_YCOORDINATE_BITS_LEN/8 - BN_num_bytes(cv->yCoordinate))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(blob->HASH, ASN1_STRING_get0_data(cv->hash),
|
||||
@@ -357,10 +331,7 @@ int SM2CiphertextValue_get_ECCCIPHERBLOB(const SM2CiphertextValue *cv,
|
||||
memcpy(blob->Cipher, ASN1_STRING_get0_data(cv->ciphertext),
|
||||
ASN1_STRING_length(cv->ciphertext));
|
||||
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
return ret;
|
||||
return 1;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_new_from_ECCSIGNATUREBLOB(const ECCSIGNATUREBLOB *blob)
|
||||
@@ -385,35 +356,35 @@ ECDSA_SIG *ECDSA_SIG_new_from_ECCSIGNATUREBLOB(const ECCSIGNATUREBLOB *blob)
|
||||
|
||||
int ECDSA_SIG_set_ECCSIGNATUREBLOB(ECDSA_SIG *sig, const ECCSIGNATUREBLOB *blob)
|
||||
{
|
||||
if (!(sig->r = BN_bin2bn(blob->r, 256/8, sig->r))) {
|
||||
if (!(sig->r = BN_bin2bn(blob->r, 64, sig->r))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(sig->s = BN_bin2bn(blob->s, 256/8, sig->s))) {
|
||||
if (!(sig->s = BN_bin2bn(blob->s, 64, sig->s))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SAR_OK;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_get_ECCSIGNATUREBLOB(const ECDSA_SIG *sig, ECCSIGNATUREBLOB *blob)
|
||||
{
|
||||
if ((BN_num_bytes(sig->r) > 256/8) || (BN_num_bytes(sig->s) > 256/8)) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, GMAPI_R_INVALID_BIGNUM_LENGTH);
|
||||
return SAR_FAIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(sig->r, blob->r + 256/8 - BN_num_bytes(sig->r))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(sig->s, blob->s + 256/8 - BN_num_bytes(sig->s))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SAR_OK;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user