mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 08:56:17 +08:00
Add encrypt/decrypt to sm2_ctx.c
This commit is contained in:
179
src/sm2_ctx.c
179
src/sm2_ctx.c
@@ -135,3 +135,182 @@ int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int sm2_encrypt_init(SM2_ENC_CTX *ctx, const SM2_KEY *sm2_key)
|
||||
{
|
||||
if (!ctx || !sm2_key) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->sm2_key = *sm2_key;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm2_encrypt_update(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
if (!ctx || !outlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size > SM2_MAX_PLAINTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (in) {
|
||||
if (inlen > SM2_MAX_PLAINTEXT_SIZE - ctx->buf_size) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(ctx->buf + ctx->buf_size, in, inlen);
|
||||
ctx->buf_size += inlen;
|
||||
}
|
||||
|
||||
*outlen = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm2_encrypt_finish(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
if (!ctx || !outlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size > SM2_MAX_PLAINTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = SM2_MAX_CIPHERTEXT_SIZE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size) {
|
||||
if (in) {
|
||||
if (inlen > SM2_MAX_PLAINTEXT_SIZE - ctx->buf_size) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memcpy(ctx->buf + ctx->buf_size, in, inlen);
|
||||
ctx->buf_size += inlen;
|
||||
}
|
||||
if (sm2_encrypt(&ctx->sm2_key, ctx->buf, ctx->buf_size, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (!in || !inlen || inlen > SM2_MAX_PLAINTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_encrypt(&ctx->sm2_key, in, inlen, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int sm2_decrypt_init(SM2_ENC_CTX *ctx, const SM2_KEY *sm2_key)
|
||||
{
|
||||
if (!ctx || !sm2_key) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->sm2_key = *sm2_key;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm2_decrypt_update(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
if (!ctx || !outlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size > SM2_MAX_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (in) {
|
||||
if (inlen > SM2_MAX_CIPHERTEXT_SIZE - ctx->buf_size) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(ctx->buf + ctx->buf_size, in, inlen);
|
||||
ctx->buf_size += inlen;
|
||||
}
|
||||
|
||||
*outlen = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm2_decrypt_finish(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
if (!ctx || !outlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size > SM2_MAX_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = SM2_MAX_PLAINTEXT_SIZE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ctx->buf_size) {
|
||||
if (in) {
|
||||
if (inlen > SM2_MAX_CIPHERTEXT_SIZE - ctx->buf_size) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memcpy(ctx->buf + ctx->buf_size, in, inlen);
|
||||
ctx->buf_size += inlen;
|
||||
}
|
||||
if (sm2_decrypt(&ctx->sm2_key, ctx->buf, ctx->buf_size, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (!in || !inlen || inlen > SM2_MAX_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_decrypt(&ctx->sm2_key, in, inlen, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user