2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/core_names.h>
13 #include <openssl/evp.h>
14 #include <openssl/rsa.h>
15 #include <openssl/params.h>
16 #include <openssl/err.h>
17 #include <openssl/bio.h>
21 * The digest to be signed. This should be the output of a hash function.
22 * Here we sign an all-zeroes digest for demonstration purposes.
24 static const unsigned char test_digest[32] = {0};
26 /* A property query used for selecting algorithm implementations. */
27 static const char *propq = NULL;
30 * This function demonstrates RSA signing of a SHA-256 digest using the PSS
31 * padding scheme. You must already have hashed the data you want to sign.
32 * For a higher-level demonstration which does the hashing for you, see
35 * For more information, see RFC 8017 section 9.1. The digest passed in
36 * (test_digest above) corresponds to the 'mHash' value.
38 static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
41 EVP_PKEY *pkey = NULL;
42 EVP_PKEY_CTX *ctx = NULL;
44 const unsigned char *ppriv_key = NULL;
48 /* Load DER-encoded RSA private key. */
49 ppriv_key = rsa_priv_key;
50 pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
51 sizeof(rsa_priv_key), libctx, propq);
53 fprintf(stderr, "Failed to load private key\n");
57 /* Fetch hash algorithm we want to use. */
58 md = EVP_MD_fetch(libctx, "SHA256", propq);
60 fprintf(stderr, "Failed to fetch hash algorithm\n");
64 /* Create signing context. */
65 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
67 fprintf(stderr, "Failed to create signing context\n");
71 /* Initialize context for signing and set options. */
72 if (EVP_PKEY_sign_init(ctx) == 0) {
73 fprintf(stderr, "Failed to initialize signing context\n");
77 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
78 fprintf(stderr, "Failed to configure padding\n");
82 if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
83 fprintf(stderr, "Failed to configure digest type\n");
87 /* Determine length of signature. */
88 if (EVP_PKEY_sign(ctx, NULL, sig_len,
89 test_digest, sizeof(test_digest)) == 0) {
90 fprintf(stderr, "Failed to get signature length\n");
94 /* Allocate memory for signature. */
95 *sig = OPENSSL_malloc(*sig_len);
97 fprintf(stderr, "Failed to allocate memory for signature\n");
101 /* Generate signature. */
102 if (EVP_PKEY_sign(ctx, *sig, sig_len,
103 test_digest, sizeof(test_digest)) != 1) {
104 fprintf(stderr, "Failed to sign\n");
110 EVP_PKEY_CTX_free(ctx);
121 * This function demonstrates verification of an RSA signature over a SHA-256
122 * digest using the PSS signature scheme.
124 static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
127 const unsigned char *ppub_key = NULL;
128 EVP_PKEY *pkey = NULL;
129 EVP_PKEY_CTX *ctx = NULL;
132 /* Load DER-encoded RSA public key. */
133 ppub_key = rsa_pub_key;
134 pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
136 fprintf(stderr, "Failed to load public key\n");
140 /* Fetch hash algorithm we want to use. */
141 md = EVP_MD_fetch(libctx, "SHA256", propq);
143 fprintf(stderr, "Failed to fetch hash algorithm\n");
147 /* Create verification context. */
148 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
150 fprintf(stderr, "Failed to create verification context\n");
154 /* Initialize context for verification and set options. */
155 if (EVP_PKEY_verify_init(ctx) == 0) {
156 fprintf(stderr, "Failed to initialize verification context\n");
160 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
161 fprintf(stderr, "Failed to configure padding\n");
165 if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
166 fprintf(stderr, "Failed to configure digest type\n");
170 /* Verify signature. */
171 if (EVP_PKEY_verify(ctx, sig, sig_len,
172 test_digest, sizeof(test_digest)) == 0) {
173 fprintf(stderr, "Failed to verify signature; "
174 "signature may be invalid\n");
180 EVP_PKEY_CTX_free(ctx);
186 int main(int argc, char **argv)
189 OSSL_LIB_CTX *libctx = NULL;
190 unsigned char *sig = NULL;
193 if (sign(libctx, &sig, &sig_len) == 0)
196 if (verify(libctx, sig, sig_len) == 0)
202 OSSL_LIB_CTX_free(libctx);