size_t overhead;
if (md_size < 0) {
- SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_DIGEST);
+ ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
return 0;
}
if (field_size == 0) {
- SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_FIELD);
+ ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_FIELD);
return 0;
}
overhead = 10 + 2 * field_size + (size_t)md_size;
if (msg_len <= overhead) {
- SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
+ ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
return 0;
}
ctext_struct.C3 = NULL;
if (hash == NULL || C3_size <= 0) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
field_size = ec_field_size(group);
if (field_size == 0) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
kP = EC_POINT_new(group);
ctx = BN_CTX_new_ex(libctx);
if (kG == NULL || kP == NULL || ctx == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
y2 = BN_CTX_get(ctx);
if (y2 == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
goto done;
}
C3 = OPENSSL_zalloc(C3_size);
if (x2y2 == NULL || C3 == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
memset(ciphertext_buf, 0, *ciphertext_len);
if (!BN_priv_rand_range(k, order)) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
|| !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
|| !EC_POINT_mul(group, kP, NULL, P, k, ctx)
|| !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
goto done;
}
if (BN_bn2binpad(x2, x2y2, field_size) < 0
|| BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
msg_mask = OPENSSL_zalloc(msg_len);
if (msg_mask == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
/* X9.63 with no salt happens to match the KDF used in SM2 */
if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
digest, libctx, propq)) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
goto done;
}
fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(digest), propq);
if (fetched_digest == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
if (EVP_DigestInit(hash, fetched_digest) == 0
|| EVP_DigestUpdate(hash, msg, msg_len) == 0
|| EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0
|| EVP_DigestFinal(hash, C3, NULL) == 0) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
goto done;
}
ctext_struct.C2 = ASN1_OCTET_STRING_new();
if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)
|| !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
/* Ensure cast to size_t is safe */
if (ciphertext_leni < 0) {
- SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
*ciphertext_len = (size_t)ciphertext_leni;
sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
if (sm2_ctext == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
+ ERR_raise(ERR_LIB_SM2, SM2_R_ASN1_ERROR);
goto done;
}
if (sm2_ctext->C3->length != hash_size) {
- SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
+ ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
goto done;
}
ctx = BN_CTX_new_ex(libctx);
if (ctx == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
y2 = BN_CTX_get(ctx);
if (y2 == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
goto done;
}
computed_C3 = OPENSSL_zalloc(hash_size);
if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
C1 = EC_POINT_new(group);
if (C1 == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
|| !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
ctx)
|| !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
goto done;
}
|| BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
|| !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
digest, libctx, propq)) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
hash = EVP_MD_CTX_new();
if (hash == NULL) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
goto done;
}
|| !EVP_DigestUpdate(hash, ptext_buf, msg_len)
|| !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
|| !EVP_DigestFinal(hash, computed_C3, NULL)) {
- SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
+ ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
goto done;
}
if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
- SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
+ ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
goto done;
}