From e9492d1cecf459261f1f5ac0eb03e9c631600537 Mon Sep 17 00:00:00 2001 From: Hugo Landau Date: Wed, 2 Mar 2022 17:39:28 +0000 Subject: Add EVP demos for RSA-PSS signing and verification Two demos are provided: one using RSA-PSS directly in which a digest must be provided, and one using RSA-PSS with the EVP_DigestSign APIs which performs the hashing for you. Fixes #14113. Reviewed-by: Matt Caswell Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/17800) --- demos/README.txt | 2 + demos/signature/Makefile | 8 +- demos/signature/rsa_pss.h | 256 +++++++++++++++++++++++++++++++++++++++ demos/signature/rsa_pss_direct.c | 204 +++++++++++++++++++++++++++++++ demos/signature/rsa_pss_hash.c | 189 +++++++++++++++++++++++++++++ 5 files changed, 656 insertions(+), 3 deletions(-) create mode 100644 demos/signature/rsa_pss.h create mode 100644 demos/signature/rsa_pss_direct.c create mode 100644 demos/signature/rsa_pss_hash.c (limited to 'demos') diff --git a/demos/README.txt b/demos/README.txt index 42b464bc0e..045be5ffa1 100644 --- a/demos/README.txt +++ b/demos/README.txt @@ -43,3 +43,5 @@ pkwrite.c Add a password to an existing PKCS12 file. signature: EVP_Signature_demo.c Compute and verify a signature from multiple buffers +rsa_pss_direct.c Compute and verify an RSA-PSS signature from a hash +rsa_pss_hash.c Compute and verify an RSA-PSS signature over a buffer diff --git a/demos/signature/Makefile b/demos/signature/Makefile index b4071c2edc..563ec7799f 100644 --- a/demos/signature/Makefile +++ b/demos/signature/Makefile @@ -3,18 +3,20 @@ # # LD_LIBRARY_PATH=../.. ./EVP_Signature_demo -CFLAGS = -I../../include -g +CFLAGS = -I../../include -g -Wall LDFLAGS = -L../.. LDLIBS = -lcrypto -all: EVP_Signature_demo +all: EVP_Signature_demo rsa_pss_direct rsa_pss_hash %.o: %.c $(CC) $(CFLAGS) -c $< EVP_Signature_demo: EVP_Signature_demo.o +rsa_pss_direct: rsa_pss_direct.o +rsa_pss_hash: rsa_pss_hash.o test: ; clean: - $(RM) *.o EVP_Signature_demo + $(RM) *.o EVP_Signature_demo rsa_pss_direct rsa_pss_hash diff --git a/demos/signature/rsa_pss.h b/demos/signature/rsa_pss.h new file mode 100644 index 0000000000..bdab9f891e --- /dev/null +++ b/demos/signature/rsa_pss.h @@ -0,0 +1,256 @@ +/*- + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* 4096-bit RSA private key, DER. */ +static const unsigned char rsa_priv_key[] = { + 0x30, 0x82, 0x09, 0x28, 0x02, 0x01, 0x00, 0x02, 0x82, 0x02, 0x01, 0x00, + 0xa3, 0x14, 0xe4, 0xb8, 0xd8, 0x58, 0x0d, 0xab, 0xd7, 0x87, 0xa4, 0xf6, + 0x84, 0x51, 0x74, 0x60, 0x4c, 0xe3, 0x60, 0x28, 0x89, 0x49, 0x65, 0x18, + 0x5c, 0x8f, 0x1a, 0x1b, 0xe9, 0xdb, 0xc1, 0xc1, 0xf7, 0x08, 0x27, 0x44, + 0xe5, 0x9d, 0x9a, 0x33, 0xc3, 0xac, 0x5a, 0xca, 0xba, 0x20, 0x5a, 0x9e, + 0x3a, 0x18, 0xb5, 0x3d, 0xe3, 0x9d, 0x94, 0x58, 0xa7, 0xa9, 0x5a, 0x0b, + 0x4f, 0xb8, 0xe5, 0xa3, 0x7b, 0x01, 0x11, 0x0f, 0x16, 0x11, 0xb8, 0x65, + 0x2f, 0xa8, 0x95, 0xf7, 0x58, 0x2c, 0xec, 0x1d, 0x41, 0xad, 0xd1, 0x12, + 0xca, 0x4a, 0x80, 0x35, 0x35, 0x43, 0x7e, 0xe0, 0x97, 0xfc, 0x86, 0x8f, + 0xcf, 0x4b, 0xdc, 0xbc, 0x15, 0x2c, 0x8e, 0x90, 0x84, 0x26, 0x83, 0xc1, + 0x96, 0x97, 0xf4, 0xd7, 0x90, 0xce, 0xfe, 0xd4, 0xf3, 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, 0x45, 0xfc, 0x52, 0x54, 0x82, 0x9b, 0xdf, 0x64, + 0xd6, 0x59, 0x6c, 0x12, 0x70, 0xf0, 0x19, 0xd8, 0x46, 0xbb, 0x08, 0x43, + 0x81, 0xa1, 0x73, 0xa8, 0x00, 0xc9, 0x4e, 0xb9, 0xd5, 0xfd, 0x42, 0x5f, + 0xcf, 0x94, 0x14, 0x18, 0xab, 0x9d, 0x11, 0xd0, 0xbd, 0x44, 0x88, 0x2c, + 0xd8, 0x29, 0xec, 0x94, 0x70, 0xf9, 0x42, 0x14, 0xf4, 0xb0, 0x3f, 0xfe, + 0x27, 0x16, 0x43, 0x59, 0x90, 0x14, 0x48, 0x61, 0x8c, 0x91, 0xd9, 0x37, + 0x41, 0xef, 0xf1, 0xe9, 0x15, 0x4a, 0x4f, 0x5e, 0x1f, 0x50, 0x25, 0x20, + 0x2d, 0xa6, 0xf8, 0x79, 0x0d, 0x92, 0xb0, 0x00, 0x0b, 0xa2, 0xfb, 0xc3, + 0x7b, 0x0f, 0xa6, 0xff, 0x75, 0x5d, 0x70, 0xaa, 0xcf, 0x0a, 0xdf, 0xe1, + 0xfc, 0x32, 0x53, 0x1e, 0xf6, 0xe6, 0x69, 0x9f, 0x09, 0xd0, 0xc8, 0xab, + 0xaf, 0xec, 0xb0, 0x04, 0xfa, 0x83, 0xe2, 0x29, 0x23, 0x54, 0x37, 0x87, + 0x63, 0x47, 0x75, 0x9b, 0xdb, 0x1f, 0x4f, 0x1b, 0x6b, 0xa6, 0xe2, 0x67, + 0x1c, 0xb4, 0x74, 0x9e, 0x48, 0x77, 0x61, 0xc2, 0x9a, 0x3e, 0x6b, 0x89, + 0xa9, 0x68, 0x74, 0x27, 0x01, 0x29, 0xd6, 0x46, 0xe8, 0x0f, 0xd0, 0x33, + 0x22, 0x00, 0x45, 0x6c, 0xde, 0x32, 0x28, 0x42, 0x57, 0xaf, 0x70, 0x28, + 0xa0, 0xd5, 0x99, 0xbb, 0x1f, 0xd7, 0x3c, 0x84, 0x20, 0x70, 0x1f, 0xe3, + 0xa9, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe6, 0x68, 0xfe, 0x5f, 0x75, 0x71, + 0x2a, 0xd8, 0xcf, 0x0d, 0x1d, 0xf4, 0xa1, 0x06, 0x8b, 0xa5, 0x70, 0x6f, + 0x29, 0x03, 0xf3, 0x50, 0xd3, 0x83, 0x39, 0xf9, 0xf6, 0xe5, 0x79, 0x7a, + 0x29, 0x75, 0xde, 0xda, 0x6a, 0x98, 0x7c, 0x33, 0xf8, 0x64, 0xca, 0x86, + 0x5a, 0xda, 0x55, 0x5b, 0x4d, 0x7b, 0x1a, 0xe5, 0x5d, 0x19, 0x7d, 0xf3, + 0x57, 0x49, 0x3d, 0x7a, 0xe8, 0x3f, 0x5a, 0x40, 0x8c, 0x15, 0xc7, 0xb0, + 0x53, 0xf8, 0x63, 0x42, 0x17, 0x7c, 0x20, 0xb9, 0xfc, 0xff, 0x27, 0xd0, + 0xc2, 0x0c, 0x45, 0x52, 0x1b, 0x75, 0x1f, 0x89, 0x87, 0xc4, 0xa8, 0x07, + 0x3b, 0x73, 0x16, 0xc7, 0xd7, 0x77, 0x2e, 0x47, 0xa2, 0x7d, 0x12, 0xb4, + 0x25, 0x24, 0x5e, 0xa5, 0xb2, 0x12, 0x76, 0x65, 0xd1, 0xcd, 0xa4, 0x66, + 0x33, 0x2d, 0xed, 0xb2, 0x85, 0xb0, 0xb3, 0x33, 0x56, 0x18, 0x5a, 0xb3, + 0x75, 0x43, 0x4d, 0x40, 0x14, 0x22, 0x55, 0xf6, 0x5a, 0x0c, 0x6a, 0xb3, + 0xc3, 0x8a, 0x9b, 0x76, 0x1e, 0x23, 0x8d, 0x4a, 0x8f, 0x38, 0x21, 0x25, + 0x43, 0x45, 0xf6, 0x25, 0x46, 0xdb, 0xae, 0x42, 0x43, 0x74, 0x69, 0x15, + 0x46, 0xf0, 0x3a, 0x41, 0x4f, 0x9f, 0xfe, 0xda, 0x07, 0x0b, 0x38, 0xbe, + 0x6b, 0xad, 0xc2, 0xef, 0x5b, 0x97, 0x18, 0x42, 0x13, 0xac, 0x13, 0x15, + 0x70, 0x7b, 0xe2, 0x00, 0xbb, 0x41, 0x22, 0x99, 0xe5, 0xd3, 0x67, 0xfe, + 0xfd, 0xbd, 0x8e, 0xc3, 0xca, 0x60, 0x59, 0x3d, 0x8f, 0x85, 0x76, 0x41, + 0xf0, 0xb8, 0x09, 0x1a, 0x48, 0x50, 0xe4, 0x9c, 0x4a, 0x56, 0x02, 0x60, + 0x76, 0xff, 0xde, 0xd4, 0x8e, 0x76, 0xa3, 0x9c, 0x30, 0xb4, 0xa4, 0x73, + 0xe6, 0xb0, 0x70, 0xac, 0x67, 0x5f, 0x25, 0xd2, 0x94, 0xc5, 0x25, 0xb6, + 0xbf, 0xf6, 0x0b, 0xd8, 0x9f, 0x35, 0x8c, 0x20, 0xb6, 0xdd, 0x02, 0x82, + 0x01, 0x01, 0x00, 0xb5, 0x31, 0x9e, 0xa2, 0x10, 0x38, 0xca, 0x2b, 0x07, + 0xc9, 0x3f, 0x0f, 0x18, 0x2c, 0x98, 0x7f, 0x15, 0x87, 0x92, 0x93, 0x2e, + 0xce, 0x6b, 0x11, 0x42, 0x2a, 0x94, 0x3e, 0x31, 0xd0, 0xf5, 0xae, 0x9d, + 0xc7, 0x67, 0x51, 0x3c, 0x0a, 0x52, 0x04, 0x94, 0x86, 0x2e, 0x50, 0x32, + 0xe1, 0x48, 0x83, 0x85, 0xe8, 0x82, 0x04, 0x2f, 0x25, 0xbc, 0xea, 0xfc, + 0x3d, 0x4b, 0xd1, 0x53, 0x90, 0x61, 0x97, 0x47, 0x73, 0xcd, 0x1f, 0xa9, + 0x5a, 0x3f, 0xfb, 0xbf, 0xc3, 0xd5, 0x19, 0xb6, 0xd3, 0x59, 0x57, 0x37, + 0xd9, 0x09, 0x29, 0xd3, 0x80, 0xc4, 0xae, 0x52, 0xce, 0xce, 0x82, 0x29, + 0x6b, 0x95, 0x44,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x94, 0x19, 0xc1, 0x40, 0xa2, 0x36, 0x78, + 0x73, 0x21, 0x78, 0x86, 0x83, 0xd1, 0x15, 0x28, 0x59, 0x00, 0xfa, 0x0a, + 0xf0, 0x1f, 0xab, 0x03, 0x38, 0x35, 0x50, 0x78, 0x32, 0xe6, 0xdf, 0x98, + 0x2b, 0x91, 0x7b, 0xd4, 0x84, 0x90, 0x43, 0xab, 0x5a, 0x24, 0x8b, 0xa3, + 0xb6, 0x08, 0x4d, 0x5b, 0x05, 0xb5, 0xad, 0x43, 0x74, 0x7e, 0x22, 0xb7, + 0x09, 0xb0, 0x3a, 0x78, 0x55, 0xfa, 0x4c, 0x3c, 0xa2, 0x2c, 0xa6, 0xf7, + 0x19, 0xff, 0x76, 0xa4, 0x3d, 0x1e, 0x99, 0x51, 0xa7, 0x4e, 0x76, 0x47, + 0x0f, 0x70, 0xef, 0x0b, 0x3f, 0xf2, 0x94, 0x36, 0xf3, 0x63, 0x76, 0xb9, + 0x09, 0x88, 0xbb, 0xfe, 0xf9, 0x86, 0x33, 0xdf, 0x81, 0xbe, 0x6f, 0xcc, + 0xa9, 0x75, 0x09, 0xe5, 0x8f, 0x8b, 0x42, 0xd0, 0x19, 0x03, 0x61, 0xd8, + 0xb5, 0x78, 0xcb, 0x9c, 0xbe, 0x63, 0x4d, 0xbd, 0xce, 0x5e, 0xae, 0x7f, + 0xae, 0x97, 0x88, 0x7b, 0xf4, 0x7a, 0x7b, 0xdb, 0xf6, 0x7e, 0x2c, 0x7d, + 0x95, 0x6e, 0x72, 0x3a, 0x48, 0x13, 0xdb, 0xf7, 0x10, 0x07, 0x83, 0xac, + 0xa1, 0x7a, 0x68, 0x18, 0x70, 0x18, 0x99, 0x7f, 0xf4, 0x8e, 0x93, 0x1a, + 0x40, 0x5d, 0x04, 0x07, 0xcb, 0x4d, 0xd7, 0x66, 0x96, 0xb5, 0xd3, 0x7d, + 0x8e, 0xfb, 0xe6, 0x12, 0xd0, 0x7d, 0xf0, 0xe7, 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e, 0x85, + 0x75, 0x81, 0xb9, 0x0f, 0x33, 0xd0, 0xd4, 0xd0, 0xaa, 0xbd, 0x32, 0xc9, + 0x62, 0xe8, 0x21, 0x24, 0xeb, 0x03, 0x73, 0x46, 0xb3, 0x84, 0x65, 0xf2, + 0x40, 0x7d, 0x1b, 0x1b, 0x8f, 0x86, 0x7c, 0xe7 +}; + +/* The corresponding public key, DER. */ +static const unsigned char rsa_pub_key[] = { + 0x30, 0x82, 0x02, 0x0a, 0x02, 0x82, 0x02, 0x01, 0x00, 0xa3, 0x14, 0xe4, + 0xb8, 0xd8, 0x58, 0x0d, 0xab, 0xd7, 0x87, 0xa4, 0xf6, 0x84, 0x51, 0x74, + 0x60, 0x4c, 0xe3, 0x60, 0x28, 0x89, 0x49, 0x65, 0x18, 0x5c, 0x8f, 0x1a, + 0x1b, 0xe9, 0xdb, 0xc1, 0xc1, 0xf7, 0x08, 0x27, 0x44, 0xe5, 0x9d, 0x9a, + 0x33, 0xc3, 0xac, 0x5a, 0xca, 0xba, 0x20, 0x5a, 0x9e, 0x3a, 0x18, 0xb5, + 0x3d, 0xe3, 0x9d, 0x94, 0x58, 0xa7, 0xa9, 0x5a, 0x0b, 0x4f, 0xb8, 0xe5, + 0xa3, 0x7b, 0x01, 0x11, 0x0f, 0x16, 0x11, 0xb8, 0x65, 0x2f, 0xa8, 0x95, + 0xf7, 0x58, 0x2c, 0xec, 0x1d, 0x41, 0xad, 0xd1, 0x12, 0xca, 0x4a, 0x80, + 0x35, 0x35, 0x43, 0x7e, 0xe0, 0x97, 0xfc, 0x86, 0x8f, 0xcf, 0x4b, 0xdc, + 0xbc, 0x15, 0x2c, 0x8e, 0x90, 0x84, 0x26, 0x83, 0xc1, 0x96, 0x97, 0xf4, + 0xd7, 0x90, 0xce, 0xfe, 0xd4, 0xf3, 0x70, 0x22, 0xa8, 0xb0, 0x1f, 0xed, + 0x08, 0xd7, 0xc5, 0xc0, 0xd6, 0x41, 0x6b, 0x24, 0x68, 0x5c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, 0x74, 0x65, 0x4c, 0x04, 0xe0, 0x80, 0x3e, 0xaf, 0x5e, 0x10, + 0xd6, 0x9b, 0x28, 0x37, 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01 +}; diff --git a/demos/signature/rsa_pss_direct.c b/demos/signature/rsa_pss_direct.c new file mode 100644 index 0000000000..1ecfa9d897 --- /dev/null +++ b/demos/signature/rsa_pss_direct.c @@ -0,0 +1,204 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "rsa_pss.h" + +/* + * The digest to be signed. This should be the output of a hash function. + * Here we sign an all-zeroes digest for demonstration purposes. + */ +static const unsigned char test_digest[32] = {0}; + +/* A property query used for selecting algorithm implementations. */ +static const char *propq = NULL; + +/* + * This function demonstrates RSA signing of a SHA-256 digest using the PSS + * padding scheme. You must already have hashed the data you want to sign. + * For a higher-level demonstration which does the hashing for you, see + * rsa_pss_hash.c. + * + * For more information, see RFC 8017 section 9.1. The digest passed in + * (test_digest above) corresponds to the 'mHash' value. + */ +static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len) +{ + int rv = 0; + EVP_PKEY *pkey = NULL; + EVP_PKEY_CTX *ctx = NULL; + EVP_MD *md = NULL; + const unsigned char *ppriv_key = NULL; + + *sig = NULL; + + /* Load DER-encoded RSA private key. */ + 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; + } + + /* Fetch hash algorithm we want to use. */ + md = EVP_MD_fetch(libctx, "SHA256", propq); + if (md == NULL) { + fprintf(stderr, "Failed to fetch hash algorithm\n"); + goto end; + } + + /* Create signing context. */ + ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); + if (ctx == NULL) { + fprintf(stderr, "Failed to create signing context\n"); + goto end; + } + + /* Initialize context for signing and set options. */ + 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; + } + + /* Determine length of signature. */ + if (EVP_PKEY_sign(ctx, NULL, sig_len, + test_digest, sizeof(test_digest)) == 0) { + fprintf(stderr, "Failed to get signature length\n"); + goto end; + } + + /* Allocate memory for signature. */ + *sig = OPENSSL_malloc(*sig_len); + if (*sig == NULL) { + fprintf(stderr, "Failed to allocate memory for signature\n"); + goto end; + } + + /* Generate signature. */ + if (EVP_PKEY_sign(ctx, *sig, sig_len, + test_digest, sizeof(test_digest)) != 1) { + fprintf(stderr, "Failed to sign\n"); + goto end; + } + + rv = 1; +end: + EVP_PKEY_CTX_free(ctx); + EVP_PKEY_free(pkey); + EVP_MD_free(md); + + if (rv == 0) + OPENSSL_free(*sig); + + return rv; +} + +/* + * This function demonstrates verification of an RSA signature over a SHA-256 + * digest using the PSS signature scheme. + */ +static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len) +{ + int rv = 0; + const unsigned char *ppub_key = NULL; + EVP_PKEY *pkey = NULL; + EVP_PKEY_CTX *ctx = NULL; + EVP_MD *md = NULL; + + /* Load DER-encoded RSA public key. */ + 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; + } + + /* Fetch hash algorithm we want to use. */ + md = EVP_MD_fetch(libctx, "SHA256", propq); + if (md == NULL) { + fprintf(stderr, "Failed to fetch hash algorithm\n"); + goto end; + } + + /* Create verification context. */ + ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); + if (ctx == NULL) { + fprintf(stderr, "Failed to create verification context\n"); + goto end; + } + + /* Initialize context for verification and set options. */ + 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; + } + + /* Verify signature. */ + 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; + } + + rv = 1; +end: + EVP_PKEY_CTX_free(ctx); + EVP_PKEY_free(pkey); + EVP_MD_free(md); + return rv; +} + +int main(int argc, char **argv) +{ + int rv = 1; + 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; + + rv = 0; +end: + OPENSSL_free(sig); + OSSL_LIB_CTX_free(libctx); + return rv; +} diff --git a/demos/signature/rsa_pss_hash.c b/demos/signature/rsa_pss_hash.c new file mode 100644 index 0000000000..e1d4e7cc9d --- /dev/null +++ b/demos/signature/rsa_pss_hash.c @@ -0,0 +1,189 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "rsa_pss.h" + +/* The data to be signed. This will be hashed. */ +static const char test_message[] = + "This is an example message to be signed."; + +/* A property query used for selecting algorithm implementations. */ +static const char *propq = NULL; + +/* + * This function demonstrates RSA signing of an arbitrary-length message. + * Hashing is performed automatically. In this example, SHA-256 is used. If you + * have already hashed your message and simply want to sign the hash directly, + * see rsa_pss_direct.c. + */ +static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len) +{ + int rv = 0; + EVP_PKEY *pkey = NULL; + EVP_MD_CTX *mctx = NULL; + OSSL_PARAM params[2], *p = params; + const unsigned char *ppriv_key = NULL; + + *sig = NULL; + + /* Load DER-encoded RSA private key. */ + 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; + } + + /* Create MD context used for signing. */ + mctx = EVP_MD_CTX_new(); + if (mctx == NULL) { + fprintf(stderr, "Failed to create MD context\n"); + goto end; + } + + /* Initialize MD context for signing. */ + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, + OSSL_PKEY_RSA_PAD_MODE_PSS, 0); + *p = OSSL_PARAM_construct_end(); + + if (EVP_DigestSignInit_ex(mctx, NULL, "SHA256", libctx, propq, + pkey, params) == 0) { + fprintf(stderr, "Failed to initialize signing context\n"); + goto end; + } + + /* + * Feed data to be signed into the algorithm. This may + * be called multiple times. + */ + if (EVP_DigestSignUpdate(mctx, test_message, sizeof(test_message)) == 0) { + fprintf(stderr, "Failed to hash message into signing context\n"); + goto end; + } + + /* Determine signature length. */ + if (EVP_DigestSignFinal(mctx, NULL, sig_len) == 0) { + fprintf(stderr, "Failed to get signature length\n"); + goto end; + } + + /* Allocate memory for signature. */ + *sig = OPENSSL_malloc(*sig_len); + if (*sig == NULL) { + fprintf(stderr, "Failed to allocate memory for signature\n"); + goto end; + } + + /* Generate signature. */ + if (EVP_DigestSignFinal(mctx, *sig, sig_len) == 0) { + fprintf(stderr, "Failed to sign\n"); + goto end; + } + + rv = 1; +end: + EVP_MD_CTX_free(mctx); + EVP_PKEY_free(pkey); + + if (rv == 0) + OPENSSL_free(*sig); + + return rv; +} + +/* + * This function demonstrates verification of an RSA signature over an + * arbitrary-length message using the PSS signature scheme. Hashing is performed + * automatically. + */ +static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len) +{ + int rv = 0; + EVP_PKEY *pkey = NULL; + EVP_MD_CTX *mctx = NULL; + OSSL_PARAM params[2], *p = params; + const unsigned char *ppub_key = NULL; + + /* Load DER-encoded RSA public key. */ + 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; + } + + /* Create MD context used for verification. */ + mctx = EVP_MD_CTX_new(); + if (mctx == NULL) { + fprintf(stderr, "Failed to create MD context\n"); + goto end; + } + + /* Initialize MD context for verification. */ + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, + OSSL_PKEY_RSA_PAD_MODE_PSS, 0); + *p = OSSL_PARAM_construct_end(); + + if (EVP_DigestVerifyInit_ex(mctx, NULL, "SHA256", libctx, propq, + pkey, params) == 0) { + fprintf(stderr, "Failed to initialize signing context\n"); + goto end; + } + + /* + * Feed data to be signed into the algorithm. This may + * be called multiple times. + */ + if (EVP_DigestVerifyUpdate(mctx, test_message, sizeof(test_message)) == 0) { + fprintf(stderr, "Failed to hash message into signing context\n"); + goto end; + } + + /* Verify signature. */ + if (EVP_DigestVerifyFinal(mctx, sig, sig_len) == 0) { + fprintf(stderr, "Failed to verify signature; " + "signature may be invalid\n"); + goto end; + } + + rv = 1; +end: + EVP_MD_CTX_free(mctx); + EVP_PKEY_free(pkey); + return rv; +} + +int main(int argc, char **argv) +{ + int rv = 1; + 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; + + rv = 0; +end: + OPENSSL_free(sig); + OSSL_LIB_CTX_free(libctx); + return rv; +} -- cgit v1.2.3