Add encrypt/decrypt to sm2_ctx.c

This commit is contained in:
Zhi Guan
2024-01-07 17:26:29 +08:00
parent 2dab02f76a
commit 31efcb5d87
4 changed files with 212 additions and 4 deletions

View File

@@ -30,6 +30,7 @@ int sm2decrypt_main(int argc, char **argv)
FILE *infp = stdin;
FILE *outfp = stdout;
SM2_KEY key;
SM2_ENC_CTX ctx;
uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE];
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
size_t inlen, outlen;
@@ -101,7 +102,12 @@ bad:
fprintf(stderr, "%s: read input failed : %s\n", prog, strerror(errno));
goto end;
}
if (sm2_decrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
if (sm2_decrypt_init(&ctx, &key) != 1) {
fprintf(stderr, "%s: sm2_decrypt_init failed\n", prog);
goto end;
}
if (sm2_decrypt_finish(&ctx, inbuf, inlen, outbuf, &outlen) != 1) {
fprintf(stderr, "%s: decryption failure\n", prog);
goto end;
}
@@ -112,6 +118,8 @@ bad:
ret = 0;
end:
gmssl_secure_clear(&key, sizeof(key));
gmssl_secure_clear(&ctx, sizeof(ctx));
gmssl_secure_clear(outbuf, sizeof(outbuf));
if (keyfp) fclose(keyfp);
if (infile && infp) fclose(infp);
if (outfile && outfp) fclose(outfp);