#include <stdio.h>
#include <stdlib.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include "rsa_pss.h"
static const unsigned char test_digest[32] = { 0 };
static const char *propq = NULL;
static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_MD *md = NULL;
const unsigned char *ppriv_key = NULL;
*sig = NULL;
ppriv_key = rsa_priv_key;
pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
sizeof(rsa_priv_key), libctx, propq);
if (pkey == NULL) {
fprintf(stderr, "Failed to load private key\n");
goto end;
}
md = EVP_MD_fetch(libctx, "SHA256", propq);
if (md == NULL) {
fprintf(stderr, "Failed to fetch hash algorithm\n");
goto end;
}
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (ctx == NULL) {
fprintf(stderr, "Failed to create signing context\n");
goto end;
}
if (EVP_PKEY_sign_init(ctx) == 0) {
fprintf(stderr, "Failed to initialize signing context\n");
goto end;
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
fprintf(stderr, "Failed to configure padding\n");
goto end;
}
if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
fprintf(stderr, "Failed to configure digest type\n");
goto end;
}
if (EVP_PKEY_sign(ctx, NULL, sig_len,
test_digest, sizeof(test_digest))
== 0) {
fprintf(stderr, "Failed to get signature length\n");
goto end;
}
*sig = OPENSSL_malloc(*sig_len);
if (*sig == NULL) {
fprintf(stderr, "Failed to allocate memory for signature\n");
goto end;
}
if (EVP_PKEY_sign(ctx, *sig, sig_len,
test_digest, sizeof(test_digest))
!= 1) {
fprintf(stderr, "Failed to sign\n");
goto end;
}
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
EVP_MD_free(md);
if (ret == 0)
OPENSSL_free(*sig);
return ret;
}
static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
{
int ret = 0;
const unsigned char *ppub_key = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_MD *md = NULL;
ppub_key = rsa_pub_key;
pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
if (pkey == NULL) {
fprintf(stderr, "Failed to load public key\n");
goto end;
}
md = EVP_MD_fetch(libctx, "SHA256", propq);
if (md == NULL) {
fprintf(stderr, "Failed to fetch hash algorithm\n");
goto end;
}
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (ctx == NULL) {
fprintf(stderr, "Failed to create verification context\n");
goto end;
}
if (EVP_PKEY_verify_init(ctx) == 0) {
fprintf(stderr, "Failed to initialize verification context\n");
goto end;
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
fprintf(stderr, "Failed to configure padding\n");
goto end;
}
if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
fprintf(stderr, "Failed to configure digest type\n");
goto end;
}
if (EVP_PKEY_verify(ctx, sig, sig_len,
test_digest, sizeof(test_digest))
== 0) {
fprintf(stderr, "Failed to verify signature; "
"signature may be invalid\n");
goto end;
}
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
EVP_MD_free(md);
return ret;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
OSSL_LIB_CTX *libctx = NULL;
unsigned char *sig = NULL;
size_t sig_len = 0;
if (sign(libctx, &sig, &sig_len) == 0)
goto end;
if (verify(libctx, sig, sig_len) == 0)
goto end;
printf("Success\n");
ret = EXIT_SUCCESS;
end:
OPENSSL_free(sig);
OSSL_LIB_CTX_free(libctx);
return ret;
}