1e0c4386eSCy Schubert /*
2*e7be843bSPierre Pronchery * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert *
4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert */
9e0c4386eSCy Schubert
10e0c4386eSCy Schubert #include <stdio.h>
11e0c4386eSCy Schubert #include <stdlib.h>
12e0c4386eSCy Schubert #include <openssl/core_names.h>
13e0c4386eSCy Schubert #include <openssl/evp.h>
14e0c4386eSCy Schubert #include <openssl/rsa.h>
15e0c4386eSCy Schubert #include <openssl/params.h>
16e0c4386eSCy Schubert #include <openssl/err.h>
17e0c4386eSCy Schubert #include <openssl/bio.h>
18e0c4386eSCy Schubert #include "rsa_pss.h"
19e0c4386eSCy Schubert
20e0c4386eSCy Schubert /*
21e0c4386eSCy Schubert * The digest to be signed. This should be the output of a hash function.
22e0c4386eSCy Schubert * Here we sign an all-zeroes digest for demonstration purposes.
23e0c4386eSCy Schubert */
24e0c4386eSCy Schubert static const unsigned char test_digest[32] = {0};
25e0c4386eSCy Schubert
26e0c4386eSCy Schubert /* A property query used for selecting algorithm implementations. */
27e0c4386eSCy Schubert static const char *propq = NULL;
28e0c4386eSCy Schubert
29e0c4386eSCy Schubert /*
30e0c4386eSCy Schubert * This function demonstrates RSA signing of a SHA-256 digest using the PSS
31e0c4386eSCy Schubert * padding scheme. You must already have hashed the data you want to sign.
32e0c4386eSCy Schubert * For a higher-level demonstration which does the hashing for you, see
33e0c4386eSCy Schubert * rsa_pss_hash.c.
34e0c4386eSCy Schubert *
35e0c4386eSCy Schubert * For more information, see RFC 8017 section 9.1. The digest passed in
36e0c4386eSCy Schubert * (test_digest above) corresponds to the 'mHash' value.
37e0c4386eSCy Schubert */
sign(OSSL_LIB_CTX * libctx,unsigned char ** sig,size_t * sig_len)38e0c4386eSCy Schubert static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
39e0c4386eSCy Schubert {
40*e7be843bSPierre Pronchery int ret = 0;
41e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
42e0c4386eSCy Schubert EVP_PKEY_CTX *ctx = NULL;
43e0c4386eSCy Schubert EVP_MD *md = NULL;
44e0c4386eSCy Schubert const unsigned char *ppriv_key = NULL;
45e0c4386eSCy Schubert
46e0c4386eSCy Schubert *sig = NULL;
47e0c4386eSCy Schubert
48e0c4386eSCy Schubert /* Load DER-encoded RSA private key. */
49e0c4386eSCy Schubert ppriv_key = rsa_priv_key;
50e0c4386eSCy Schubert pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
51e0c4386eSCy Schubert sizeof(rsa_priv_key), libctx, propq);
52e0c4386eSCy Schubert if (pkey == NULL) {
53e0c4386eSCy Schubert fprintf(stderr, "Failed to load private key\n");
54e0c4386eSCy Schubert goto end;
55e0c4386eSCy Schubert }
56e0c4386eSCy Schubert
57e0c4386eSCy Schubert /* Fetch hash algorithm we want to use. */
58e0c4386eSCy Schubert md = EVP_MD_fetch(libctx, "SHA256", propq);
59e0c4386eSCy Schubert if (md == NULL) {
60e0c4386eSCy Schubert fprintf(stderr, "Failed to fetch hash algorithm\n");
61e0c4386eSCy Schubert goto end;
62e0c4386eSCy Schubert }
63e0c4386eSCy Schubert
64e0c4386eSCy Schubert /* Create signing context. */
65e0c4386eSCy Schubert ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
66e0c4386eSCy Schubert if (ctx == NULL) {
67e0c4386eSCy Schubert fprintf(stderr, "Failed to create signing context\n");
68e0c4386eSCy Schubert goto end;
69e0c4386eSCy Schubert }
70e0c4386eSCy Schubert
71e0c4386eSCy Schubert /* Initialize context for signing and set options. */
72e0c4386eSCy Schubert if (EVP_PKEY_sign_init(ctx) == 0) {
73e0c4386eSCy Schubert fprintf(stderr, "Failed to initialize signing context\n");
74e0c4386eSCy Schubert goto end;
75e0c4386eSCy Schubert }
76e0c4386eSCy Schubert
77e0c4386eSCy Schubert if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
78e0c4386eSCy Schubert fprintf(stderr, "Failed to configure padding\n");
79e0c4386eSCy Schubert goto end;
80e0c4386eSCy Schubert }
81e0c4386eSCy Schubert
82e0c4386eSCy Schubert if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
83e0c4386eSCy Schubert fprintf(stderr, "Failed to configure digest type\n");
84e0c4386eSCy Schubert goto end;
85e0c4386eSCy Schubert }
86e0c4386eSCy Schubert
87e0c4386eSCy Schubert /* Determine length of signature. */
88e0c4386eSCy Schubert if (EVP_PKEY_sign(ctx, NULL, sig_len,
89e0c4386eSCy Schubert test_digest, sizeof(test_digest)) == 0) {
90e0c4386eSCy Schubert fprintf(stderr, "Failed to get signature length\n");
91e0c4386eSCy Schubert goto end;
92e0c4386eSCy Schubert }
93e0c4386eSCy Schubert
94e0c4386eSCy Schubert /* Allocate memory for signature. */
95e0c4386eSCy Schubert *sig = OPENSSL_malloc(*sig_len);
96e0c4386eSCy Schubert if (*sig == NULL) {
97e0c4386eSCy Schubert fprintf(stderr, "Failed to allocate memory for signature\n");
98e0c4386eSCy Schubert goto end;
99e0c4386eSCy Schubert }
100e0c4386eSCy Schubert
101e0c4386eSCy Schubert /* Generate signature. */
102e0c4386eSCy Schubert if (EVP_PKEY_sign(ctx, *sig, sig_len,
103e0c4386eSCy Schubert test_digest, sizeof(test_digest)) != 1) {
104e0c4386eSCy Schubert fprintf(stderr, "Failed to sign\n");
105e0c4386eSCy Schubert goto end;
106e0c4386eSCy Schubert }
107e0c4386eSCy Schubert
108*e7be843bSPierre Pronchery ret = 1;
109e0c4386eSCy Schubert end:
110e0c4386eSCy Schubert EVP_PKEY_CTX_free(ctx);
111e0c4386eSCy Schubert EVP_PKEY_free(pkey);
112e0c4386eSCy Schubert EVP_MD_free(md);
113e0c4386eSCy Schubert
114*e7be843bSPierre Pronchery if (ret == 0)
115e0c4386eSCy Schubert OPENSSL_free(*sig);
116e0c4386eSCy Schubert
117*e7be843bSPierre Pronchery return ret;
118e0c4386eSCy Schubert }
119e0c4386eSCy Schubert
120e0c4386eSCy Schubert /*
121e0c4386eSCy Schubert * This function demonstrates verification of an RSA signature over a SHA-256
122e0c4386eSCy Schubert * digest using the PSS signature scheme.
123e0c4386eSCy Schubert */
verify(OSSL_LIB_CTX * libctx,const unsigned char * sig,size_t sig_len)124e0c4386eSCy Schubert static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
125e0c4386eSCy Schubert {
126*e7be843bSPierre Pronchery int ret = 0;
127e0c4386eSCy Schubert const unsigned char *ppub_key = NULL;
128e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
129e0c4386eSCy Schubert EVP_PKEY_CTX *ctx = NULL;
130e0c4386eSCy Schubert EVP_MD *md = NULL;
131e0c4386eSCy Schubert
132e0c4386eSCy Schubert /* Load DER-encoded RSA public key. */
133e0c4386eSCy Schubert ppub_key = rsa_pub_key;
134e0c4386eSCy Schubert pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
135e0c4386eSCy Schubert if (pkey == NULL) {
136e0c4386eSCy Schubert fprintf(stderr, "Failed to load public key\n");
137e0c4386eSCy Schubert goto end;
138e0c4386eSCy Schubert }
139e0c4386eSCy Schubert
140e0c4386eSCy Schubert /* Fetch hash algorithm we want to use. */
141e0c4386eSCy Schubert md = EVP_MD_fetch(libctx, "SHA256", propq);
142e0c4386eSCy Schubert if (md == NULL) {
143e0c4386eSCy Schubert fprintf(stderr, "Failed to fetch hash algorithm\n");
144e0c4386eSCy Schubert goto end;
145e0c4386eSCy Schubert }
146e0c4386eSCy Schubert
147e0c4386eSCy Schubert /* Create verification context. */
148e0c4386eSCy Schubert ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
149e0c4386eSCy Schubert if (ctx == NULL) {
150e0c4386eSCy Schubert fprintf(stderr, "Failed to create verification context\n");
151e0c4386eSCy Schubert goto end;
152e0c4386eSCy Schubert }
153e0c4386eSCy Schubert
154e0c4386eSCy Schubert /* Initialize context for verification and set options. */
155e0c4386eSCy Schubert if (EVP_PKEY_verify_init(ctx) == 0) {
156e0c4386eSCy Schubert fprintf(stderr, "Failed to initialize verification context\n");
157e0c4386eSCy Schubert goto end;
158e0c4386eSCy Schubert }
159e0c4386eSCy Schubert
160e0c4386eSCy Schubert if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 0) {
161e0c4386eSCy Schubert fprintf(stderr, "Failed to configure padding\n");
162e0c4386eSCy Schubert goto end;
163e0c4386eSCy Schubert }
164e0c4386eSCy Schubert
165e0c4386eSCy Schubert if (EVP_PKEY_CTX_set_signature_md(ctx, md) == 0) {
166e0c4386eSCy Schubert fprintf(stderr, "Failed to configure digest type\n");
167e0c4386eSCy Schubert goto end;
168e0c4386eSCy Schubert }
169e0c4386eSCy Schubert
170e0c4386eSCy Schubert /* Verify signature. */
171e0c4386eSCy Schubert if (EVP_PKEY_verify(ctx, sig, sig_len,
172e0c4386eSCy Schubert test_digest, sizeof(test_digest)) == 0) {
173e0c4386eSCy Schubert fprintf(stderr, "Failed to verify signature; "
174e0c4386eSCy Schubert "signature may be invalid\n");
175e0c4386eSCy Schubert goto end;
176e0c4386eSCy Schubert }
177e0c4386eSCy Schubert
178*e7be843bSPierre Pronchery ret = 1;
179e0c4386eSCy Schubert end:
180e0c4386eSCy Schubert EVP_PKEY_CTX_free(ctx);
181e0c4386eSCy Schubert EVP_PKEY_free(pkey);
182e0c4386eSCy Schubert EVP_MD_free(md);
183*e7be843bSPierre Pronchery return ret;
184e0c4386eSCy Schubert }
185e0c4386eSCy Schubert
main(int argc,char ** argv)186e0c4386eSCy Schubert int main(int argc, char **argv)
187e0c4386eSCy Schubert {
188*e7be843bSPierre Pronchery int ret = EXIT_FAILURE;
189e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
190e0c4386eSCy Schubert unsigned char *sig = NULL;
191e0c4386eSCy Schubert size_t sig_len = 0;
192e0c4386eSCy Schubert
193e0c4386eSCy Schubert if (sign(libctx, &sig, &sig_len) == 0)
194e0c4386eSCy Schubert goto end;
195e0c4386eSCy Schubert
196e0c4386eSCy Schubert if (verify(libctx, sig, sig_len) == 0)
197e0c4386eSCy Schubert goto end;
198e0c4386eSCy Schubert
199*e7be843bSPierre Pronchery printf("Success\n");
200*e7be843bSPierre Pronchery
201*e7be843bSPierre Pronchery ret = EXIT_SUCCESS;
202e0c4386eSCy Schubert end:
203e0c4386eSCy Schubert OPENSSL_free(sig);
204e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
205*e7be843bSPierre Pronchery return ret;
206e0c4386eSCy Schubert }
207