mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-28 19:26:26 +08:00
update
This commit is contained in:
@@ -59,10 +59,13 @@
|
||||
*/
|
||||
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/sm2.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/sdf.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "../sm2/sm2_lcl.h"
|
||||
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCrefPublicKey(const ECCrefPublicKey *ref)
|
||||
{
|
||||
@@ -313,7 +316,7 @@ SM2CiphertextValue *SM2CiphertextValue_new_from_ECCCipher(const ECCCipher *ref)
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(cv = SM2CiphertextValue_new(group))) {
|
||||
if (!(cv = SM2CiphertextValue_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_NEW_FROM_ECCCIPHER,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
@@ -334,24 +337,10 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Different vendors might have different encoding of field elements when
|
||||
* the buffer is larger than requirment. We assume the encoding to be
|
||||
* big-endian, which means that there will be prefix zeros in the buffer
|
||||
* before the field element. Another popular encoding is to use the suffix
|
||||
* zeros. When the gmapi wrapper is working with vendor's SDF
|
||||
* implementations, developers have to check the encoding of the vendor's
|
||||
* library to make sure the encoding/decoding is correct
|
||||
*/
|
||||
int SM2CiphertextValue_set_ECCCipher(SM2CiphertextValue *cv,
|
||||
const ECCCipher *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x;
|
||||
BIGNUM *y;
|
||||
int nbytes;
|
||||
|
||||
/* check arguments */
|
||||
if (!cv || !ref) {
|
||||
@@ -360,35 +349,6 @@ int SM2CiphertextValue_set_ECCCipher(SM2CiphertextValue *cv,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* variables */
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
/* this will never happen with GmSSL's sdf.h */
|
||||
if (EC_GROUP_get_degree(group) > ECCref_MAX_BITS) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
|
||||
/* malloc */
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
BN_CTX_start(bn_ctx);
|
||||
x = BN_CTX_get(bn_ctx);
|
||||
y = BN_CTX_get(bn_ctx);
|
||||
if (!x || !y) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* ECCCipher ==> SM2CiphertextValue */
|
||||
if (!BN_bin2bn(ref->x, ECCref_MAX_LEN, cv->xCoordinate)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
@@ -401,72 +361,31 @@ int SM2CiphertextValue_set_ECCCipher(SM2CiphertextValue *cv,
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!cv->ephem_point) {
|
||||
if (!(cv->ephem_point = EC_POINT_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!ASN1_OCTET_STRING_set(cv->hash, ref->M, 32)) {
|
||||
goto end;
|
||||
}
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_set_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
cv->mactag_size = 32;
|
||||
memcpy(cv->mactag, ref->M, 32);
|
||||
|
||||
if (ref->L <= 0 || ref->L > INT_MAX) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
cv->ciphertext_size = (size_t)ref->L;
|
||||
|
||||
if (!(cv->ciphertext = OPENSSL_realloc(cv->ciphertext, (size_t)ref->L))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
if (!ASN1_OCTET_STRING_set(cv->ciphertext, ref->C, ref->L)) {
|
||||
goto end;
|
||||
}
|
||||
memcpy(cv->ciphertext, ref->C, (size_t)ref->L);
|
||||
|
||||
|
||||
/* set return value */
|
||||
ret = 0;
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* The caller need to prepare buffer larger than `sizeof(ECCipher)` to hold
|
||||
* the result. Some vendors might change the define of `ECCCipher->C[1]` to
|
||||
* larger buffer, such as `ECCCipher->C[ECC_CIPHER_MAX_LEN]`, but our
|
||||
* implementation will not know this. So always init `ECCCipher->L` to the
|
||||
* buffer size for ciphertext.
|
||||
* Some vendors might even change the definition of `ECCCipher`, then there
|
||||
* will be compiling errors when compiled with the vendor's header file. So
|
||||
* when you will use a vendor's SDF library, do not use `openssl/sdf.h`, but
|
||||
* use the vendor's header file. Then the errors can be found by the
|
||||
* compiler.
|
||||
*/
|
||||
int SM2CiphertextValue_get_ECCCipher(const SM2CiphertextValue *cv,
|
||||
ECCCipher *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x;
|
||||
BIGNUM *y;
|
||||
|
||||
/* check arguments */
|
||||
if (!cv || !ref) {
|
||||
@@ -474,101 +393,76 @@ int SM2CiphertextValue_get_ECCCipher(const SM2CiphertextValue *cv,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* as the `ECCCipher->C[1]` default size is too small, we have to
|
||||
* check `ECCCipher->L` to make sure caller has initialized this
|
||||
* structure and prepared enough buffer to hold variable length
|
||||
* ciphertext
|
||||
*/
|
||||
if (ref->L < cv->ciphertext_size) {
|
||||
if (ref->L < ASN1_STRING_length(cv->ciphertext)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* malloc */
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
BN_CTX_start(bn_ctx);
|
||||
x = BN_CTX_get(bn_ctx);
|
||||
y = BN_CTX_get(bn_ctx);
|
||||
if (!x || !y) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* SM2CiphertextValue ==> ECCCipher */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
|
||||
/* encode ephem point `ECCCipher->x`, `ECCCipher->y` */
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* check compatible of SM2CiphertextValue with EC_GROUP
|
||||
* In gmapi we only do simple checks, i.e. length of coordinates.
|
||||
* We assume that more checks, such as x, y in the range of [1, p]
|
||||
* and other semantic checks should be done by the `sm2` module.
|
||||
*/
|
||||
if (BN_num_bits(x) > EC_GROUP_get_degree(group) ||
|
||||
BN_num_bits(y) > EC_GROUP_get_degree(group)) {
|
||||
if (BN_num_bytes(cv->xCoordinate) > ECCref_MAX_LEN
|
||||
|| BN_num_bytes(cv->yCoordinate) > ECCref_MAX_LEN) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_POINT);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(x, ref->x + ECCref_MAX_LEN - BN_num_bytes(x))) {
|
||||
|
||||
if (ASN1_STRING_length(cv->hash) != 32) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* SM2CiphertextValue ==> ECCCipher */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
|
||||
if (!BN_bn2bin(cv->xCoordinate,
|
||||
ref->x + ECCref_MAX_LEN - BN_num_bytes(cv->xCoordinate))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, ref->y + ECCref_MAX_LEN - BN_num_bytes(y))) {
|
||||
|
||||
if (!BN_bn2bin(cv->yCoordinate,
|
||||
ref->y + ECCref_MAX_LEN - BN_num_bytes(cv->yCoordinate))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* encode mac `ECCCipher->M[32]` */
|
||||
if (cv->mactag_size != 32) {
|
||||
if (ASN1_STRING_length(cv->hash) != 32) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_MAC);
|
||||
goto end;
|
||||
}
|
||||
memcpy(ref->M, cv->mactag, cv->mactag_size);
|
||||
memcpy(ref->M, ASN1_STRING_get0_data(cv->hash),
|
||||
ASN1_STRING_length(cv->hash));
|
||||
|
||||
/* encode ciphertext `ECCCipher->L`, `ECCCipher->C[]` */
|
||||
if (cv->ciphertext_size <= 0 || cv->ciphertext_size > INT_MAX) {
|
||||
|
||||
if (ASN1_STRING_length(cv->ciphertext) <= 0
|
||||
|| ASN1_STRING_length(cv->ciphertext) > INT_MAX) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
ref->L = (unsigned int)cv->ciphertext_size;
|
||||
memcpy(ref->C, cv->ciphertext, cv->ciphertext_size);
|
||||
ref->L = ASN1_STRING_length(cv->ciphertext);
|
||||
memcpy(ref->C, ASN1_STRING_get0_data(cv->ciphertext),
|
||||
ASN1_STRING_length(cv->ciphertext));
|
||||
|
||||
/* set return value */
|
||||
ret = 1;
|
||||
end:
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
EC_GROUP_free(group);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
crypto/gmapi/gmapi_sdf_ec.o: crypto/gmapi/gmapi_sdf_ec.c \
|
||||
include/openssl/ec.h include/openssl/opensslconf.h \
|
||||
include/openssl/asn1.h include/openssl/e_os2.h include/openssl/bio.h \
|
||||
include/openssl/crypto.h include/openssl/stack.h \
|
||||
include/openssl/safestack.h include/openssl/opensslv.h \
|
||||
include/openssl/ossl_typ.h include/openssl/symhacks.h \
|
||||
include/openssl/bn.h include/openssl/err.h include/openssl/lhash.h \
|
||||
include/openssl/sdf.h include/openssl/sgd.h include/openssl/gmapi.h \
|
||||
include/openssl/sm2.h include/openssl/evp.h include/openssl/objects.h \
|
||||
include/openssl/obj_mac.h include/openssl/kdf2.h include/openssl/kdf.h \
|
||||
include/openssl/x509.h include/openssl/buffer.h \
|
||||
include/openssl/paillier.h include/openssl/rsa.h include/openssl/dsa.h \
|
||||
include/openssl/dh.h include/openssl/sha.h include/openssl/x509_vfy.h \
|
||||
include/openssl/pkcs7.h include/openssl/ecies.h include/openssl/sm3.h \
|
||||
include/openssl/saf.h include/openssl/skf.h include/openssl/sof.h
|
||||
@@ -48,11 +48,12 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/sm2.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include <openssl/skf.h>
|
||||
#include "../ec/ec_lcl.h"
|
||||
|
||||
#include "../sm2/sm2_lcl.h"
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCPUBLICKEYBLOB(const ECCPUBLICKEYBLOB *blob)
|
||||
{
|
||||
@@ -253,36 +254,21 @@ end:
|
||||
SM2CiphertextValue *SM2CiphertextValue_new_from_ECCCIPHERBLOB(
|
||||
const ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ok = 0;
|
||||
SM2CiphertextValue *ret = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(ret = SM2CiphertextValue_new(group))) {
|
||||
if (!(ret = SM2CiphertextValue_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!SM2CiphertextValue_set_ECCCIPHERBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_EC_PUBLIC_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
end:
|
||||
if (!ok) {
|
||||
SM2CiphertextValue_free(ret);
|
||||
ret = NULL;
|
||||
return NULL;
|
||||
}
|
||||
EC_GROUP_free(group);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -290,78 +276,37 @@ int SM2CiphertextValue_set_ECCCIPHERBLOB(SM2CiphertextValue *cv,
|
||||
const ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
int nbytes;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB,
|
||||
ERR_R_EC_LIB);
|
||||
if (!cv || !blob) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
if (nbytes > ECC_MAX_XCOORDINATE_BITS_LEN/8) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(x = BN_bin2bn(blob->XCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(y = BN_bin2bn(blob->YCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
if (!BN_bin2bn(blob->XCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8,
|
||||
cv->xCoordinate)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!cv->ephem_point) {
|
||||
if (!(cv->ephem_point = EC_POINT_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_set_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bin2bn(blob->YCoordinate, ECC_MAX_YCOORDINATE_BITS_LEN/8,
|
||||
cv->yCoordinate)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
memcpy(cv->mactag, blob->HASH, 32);
|
||||
cv->mactag_size = 32;
|
||||
if (!ASN1_OCTET_STRING_set(cv->hash, blob->HASH, 32)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB, ERR_R_ASN1_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((cv->ciphertext_size = blob->CipherLen) <= 0) {
|
||||
if (!ASN1_OCTET_STRING_set(cv->ciphertext, blob->Cipher,
|
||||
blob->CipherLen)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
if (!(cv->ciphertext = OPENSSL_realloc(cv->ciphertext, blob->CipherLen))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
memcpy(cv->ciphertext, blob->Cipher, blob->CipherLen);
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -369,70 +314,45 @@ int SM2CiphertextValue_get_ECCCIPHERBLOB(const SM2CiphertextValue *cv,
|
||||
ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
if (BN_num_bits(cv->xCoordinate) > ECC_MAX_XCOORDINATE_BITS_LEN ||
|
||||
BN_num_bits(cv->yCoordinate) > ECC_MAX_YCOORDINATE_BITS_LEN) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_POINT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
x = BN_new();
|
||||
y = BN_new();
|
||||
bn_ctx = BN_CTX_new();
|
||||
if (!x || !y || !bn_ctx) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if ((BN_num_bytes(x) > 256/8) || (BN_num_bytes(y) > 256/8)) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_POINT);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(x, blob->XCoordinate + 256/8 - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, blob->YCoordinate + 256/8 - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cv->mactag_size != 32) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_MAC);
|
||||
goto end;
|
||||
}
|
||||
memcpy(blob->HASH, cv->mactag, cv->mactag_size);
|
||||
|
||||
if (cv->ciphertext_size <= 0) {
|
||||
if (ASN1_STRING_length(cv->hash) != 32) {
|
||||
GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (blob->CipherLen < ASN1_STRING_length(cv->ciphertext)) {
|
||||
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;
|
||||
}
|
||||
memcpy(blob->Cipher, cv->ciphertext, cv->ciphertext_size);
|
||||
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;
|
||||
}
|
||||
|
||||
memcpy(blob->HASH, ASN1_STRING_get0_data(cv->hash),
|
||||
ASN1_STRING_length(cv->hash));
|
||||
|
||||
blob->CipherLen = ASN1_STRING_length(cv->ciphertext);
|
||||
memcpy(blob->Cipher, ASN1_STRING_get0_data(cv->ciphertext),
|
||||
ASN1_STRING_length(cv->ciphertext));
|
||||
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ 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 "serpent.h"
|
||||
#include <string.h>
|
||||
#include <openssl/serpent.h>
|
||||
|
||||
|
||||
void serpent_whiten(serpent_blk *dst, serpent_key_t *src, int idx) {
|
||||
@@ -169,7 +170,7 @@ void serpent_lt(serpent_blk* x, int enc)
|
||||
x->w[3] = x3;
|
||||
}
|
||||
|
||||
void serpent_set_encrypt_key(serpent_key_t *key, void *user_key)
|
||||
void serpent_set_encrypt_key(serpent_key_t *key, const unsigned char *user_key)
|
||||
{
|
||||
union {
|
||||
uint8_t b[32];
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
crypto/serpent/serpent.o: crypto/serpent/serpent.c \
|
||||
include/openssl/serpent.h
|
||||
Reference in New Issue
Block a user