Path: blob/main/crypto/openssl/demos/signature/rsa_pss_direct.c
34907 views
/*1* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdio.h>10#include <stdlib.h>11#include <openssl/core_names.h>12#include <openssl/evp.h>13#include <openssl/rsa.h>14#include <openssl/params.h>15#include <openssl/err.h>16#include <openssl/bio.h>17#include "rsa_pss.h"1819/*20* The digest to be signed. This should be the output of a hash function.21* Here we sign an all-zeroes digest for demonstration purposes.22*/23static const unsigned char test_digest[32] = {0};2425/* A property query used for selecting algorithm implementations. */26static const char *propq = NULL;2728/*29* This function demonstrates RSA signing of a SHA-256 digest using the PSS30* padding scheme. You must already have hashed the data you want to sign.31* For a higher-level demonstration which does the hashing for you, see32* rsa_pss_hash.c.33*34* For more information, see RFC 8017 section 9.1. The digest passed in35* (test_digest above) corresponds to the 'mHash' value.36*/37static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)38{39int ret = 0;40EVP_PKEY *pkey = NULL;41EVP_PKEY_CTX *ctx = NULL;42EVP_MD *md = NULL;43const unsigned char *ppriv_key = NULL;4445*sig = NULL;4647/* Load DER-encoded RSA private key. */48ppriv_key = rsa_priv_key;49pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,50sizeof(rsa_priv_key), libctx, propq);51if (pkey == NULL) {52fprintf(stderr, "Failed to load private key\n");53goto end;54}5556/* Fetch hash algorithm we want to use. */57md = EVP_MD_fetch(libctx, "SHA256", propq);58if (md == NULL) {59fprintf(stderr, "Failed to fetch hash algorithm\n");60goto end;61}6263/* Create signing context. */64ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);65if (ctx == NULL) {66fprintf(stderr, "Failed to create signing context\n");67goto end;68}6970/* Initialize context for signing and set options. */71if (EVP_PKEY_sign_init(ctx) == 0) {72fprintf(stderr, "Failed to initialize signing context\n");73goto end;74}7576if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {77fprintf(stderr, "Failed to configure padding\n");78goto end;79}8081if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {82fprintf(stderr, "Failed to configure digest type\n");83goto end;84}8586/* Determine length of signature. */87if (EVP_PKEY_sign(ctx, NULL, sig_len,88test_digest, sizeof(test_digest)) == 0) {89fprintf(stderr, "Failed to get signature length\n");90goto end;91}9293/* Allocate memory for signature. */94*sig = OPENSSL_malloc(*sig_len);95if (*sig == NULL) {96fprintf(stderr, "Failed to allocate memory for signature\n");97goto end;98}99100/* Generate signature. */101if (EVP_PKEY_sign(ctx, *sig, sig_len,102test_digest, sizeof(test_digest)) != 1) {103fprintf(stderr, "Failed to sign\n");104goto end;105}106107ret = 1;108end:109EVP_PKEY_CTX_free(ctx);110EVP_PKEY_free(pkey);111EVP_MD_free(md);112113if (ret == 0)114OPENSSL_free(*sig);115116return ret;117}118119/*120* This function demonstrates verification of an RSA signature over a SHA-256121* digest using the PSS signature scheme.122*/123static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)124{125int ret = 0;126const unsigned char *ppub_key = NULL;127EVP_PKEY *pkey = NULL;128EVP_PKEY_CTX *ctx = NULL;129EVP_MD *md = NULL;130131/* Load DER-encoded RSA public key. */132ppub_key = rsa_pub_key;133pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));134if (pkey == NULL) {135fprintf(stderr, "Failed to load public key\n");136goto end;137}138139/* Fetch hash algorithm we want to use. */140md = EVP_MD_fetch(libctx, "SHA256", propq);141if (md == NULL) {142fprintf(stderr, "Failed to fetch hash algorithm\n");143goto end;144}145146/* Create verification context. */147ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);148if (ctx == NULL) {149fprintf(stderr, "Failed to create verification context\n");150goto end;151}152153/* Initialize context for verification and set options. */154if (EVP_PKEY_verify_init(ctx) == 0) {155fprintf(stderr, "Failed to initialize verification context\n");156goto end;157}158159if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {160fprintf(stderr, "Failed to configure padding\n");161goto end;162}163164if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {165fprintf(stderr, "Failed to configure digest type\n");166goto end;167}168169/* Verify signature. */170if (EVP_PKEY_verify(ctx, sig, sig_len,171test_digest, sizeof(test_digest)) == 0) {172fprintf(stderr, "Failed to verify signature; "173"signature may be invalid\n");174goto end;175}176177ret = 1;178end:179EVP_PKEY_CTX_free(ctx);180EVP_PKEY_free(pkey);181EVP_MD_free(md);182return ret;183}184185int main(int argc, char **argv)186{187int ret = EXIT_FAILURE;188OSSL_LIB_CTX *libctx = NULL;189unsigned char *sig = NULL;190size_t sig_len = 0;191192if (sign(libctx, &sig, &sig_len) == 0)193goto end;194195if (verify(libctx, sig, sig_len) == 0)196goto end;197198printf("Success\n");199200ret = EXIT_SUCCESS;201end:202OPENSSL_free(sig);203OSSL_LIB_CTX_free(libctx);204return ret;205}206207208