Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/demos/signature/rsa_pss_hash.c
111746 views
1
/*
2
* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3
*
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
8
*/
9
10
#include <stdio.h>
11
#include <stdlib.h>
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>
18
#include "rsa_pss.h"
19
20
/* The data to be signed. This will be hashed. */
21
static const char test_message[] = "This is an example message to be signed.";
22
23
/* A property query used for selecting algorithm implementations. */
24
static const char *propq = NULL;
25
26
/*
27
* This function demonstrates RSA signing of an arbitrary-length message.
28
* Hashing is performed automatically. In this example, SHA-256 is used. If you
29
* have already hashed your message and simply want to sign the hash directly,
30
* see rsa_pss_direct.c.
31
*/
32
static int sign(OSSL_LIB_CTX *libctx, unsigned char **sig, size_t *sig_len)
33
{
34
int ret = 0;
35
EVP_PKEY *pkey = NULL;
36
EVP_MD_CTX *mctx = NULL;
37
OSSL_PARAM params[2], *p = params;
38
const unsigned char *ppriv_key = NULL;
39
40
*sig = NULL;
41
42
/* Load DER-encoded RSA private key. */
43
ppriv_key = rsa_priv_key;
44
pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &ppriv_key,
45
sizeof(rsa_priv_key), libctx, propq);
46
if (pkey == NULL) {
47
fprintf(stderr, "Failed to load private key\n");
48
goto end;
49
}
50
51
/* Create MD context used for signing. */
52
mctx = EVP_MD_CTX_new();
53
if (mctx == NULL) {
54
fprintf(stderr, "Failed to create MD context\n");
55
goto end;
56
}
57
58
/* Initialize MD context for signing. */
59
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
60
OSSL_PKEY_RSA_PAD_MODE_PSS, 0);
61
*p = OSSL_PARAM_construct_end();
62
63
if (EVP_DigestSignInit_ex(mctx, NULL, "SHA256", libctx, propq,
64
pkey, params)
65
== 0) {
66
fprintf(stderr, "Failed to initialize signing context\n");
67
goto end;
68
}
69
70
/*
71
* Feed data to be signed into the algorithm. This may
72
* be called multiple times.
73
*/
74
if (EVP_DigestSignUpdate(mctx, test_message, sizeof(test_message)) == 0) {
75
fprintf(stderr, "Failed to hash message into signing context\n");
76
goto end;
77
}
78
79
/* Determine signature length. */
80
if (EVP_DigestSignFinal(mctx, NULL, sig_len) == 0) {
81
fprintf(stderr, "Failed to get signature length\n");
82
goto end;
83
}
84
85
/* Allocate memory for signature. */
86
*sig = OPENSSL_malloc(*sig_len);
87
if (*sig == NULL) {
88
fprintf(stderr, "Failed to allocate memory for signature\n");
89
goto end;
90
}
91
92
/* Generate signature. */
93
if (EVP_DigestSignFinal(mctx, *sig, sig_len) == 0) {
94
fprintf(stderr, "Failed to sign\n");
95
goto end;
96
}
97
98
ret = 1;
99
end:
100
EVP_MD_CTX_free(mctx);
101
EVP_PKEY_free(pkey);
102
103
if (ret == 0)
104
OPENSSL_free(*sig);
105
106
return ret;
107
}
108
109
/*
110
* This function demonstrates verification of an RSA signature over an
111
* arbitrary-length message using the PSS signature scheme. Hashing is performed
112
* automatically.
113
*/
114
static int verify(OSSL_LIB_CTX *libctx, const unsigned char *sig, size_t sig_len)
115
{
116
int ret = 0;
117
EVP_PKEY *pkey = NULL;
118
EVP_MD_CTX *mctx = NULL;
119
OSSL_PARAM params[2], *p = params;
120
const unsigned char *ppub_key = NULL;
121
122
/* Load DER-encoded RSA public key. */
123
ppub_key = rsa_pub_key;
124
pkey = d2i_PublicKey(EVP_PKEY_RSA, NULL, &ppub_key, sizeof(rsa_pub_key));
125
if (pkey == NULL) {
126
fprintf(stderr, "Failed to load public key\n");
127
goto end;
128
}
129
130
/* Create MD context used for verification. */
131
mctx = EVP_MD_CTX_new();
132
if (mctx == NULL) {
133
fprintf(stderr, "Failed to create MD context\n");
134
goto end;
135
}
136
137
/* Initialize MD context for verification. */
138
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
139
OSSL_PKEY_RSA_PAD_MODE_PSS, 0);
140
*p = OSSL_PARAM_construct_end();
141
142
if (EVP_DigestVerifyInit_ex(mctx, NULL, "SHA256", libctx, propq,
143
pkey, params)
144
== 0) {
145
fprintf(stderr, "Failed to initialize signing context\n");
146
goto end;
147
}
148
149
/*
150
* Feed data to be signed into the algorithm. This may
151
* be called multiple times.
152
*/
153
if (EVP_DigestVerifyUpdate(mctx, test_message, sizeof(test_message)) == 0) {
154
fprintf(stderr, "Failed to hash message into signing context\n");
155
goto end;
156
}
157
158
/* Verify signature. */
159
if (EVP_DigestVerifyFinal(mctx, sig, sig_len) == 0) {
160
fprintf(stderr, "Failed to verify signature; "
161
"signature may be invalid\n");
162
goto end;
163
}
164
165
ret = 1;
166
end:
167
EVP_MD_CTX_free(mctx);
168
EVP_PKEY_free(pkey);
169
return ret;
170
}
171
172
int main(int argc, char **argv)
173
{
174
int ret = EXIT_FAILURE;
175
OSSL_LIB_CTX *libctx = NULL;
176
unsigned char *sig = NULL;
177
size_t sig_len = 0;
178
179
if (sign(libctx, &sig, &sig_len) == 0)
180
goto end;
181
182
if (verify(libctx, sig, sig_len) == 0)
183
goto end;
184
185
printf("Success\n");
186
187
ret = EXIT_SUCCESS;
188
end:
189
OPENSSL_free(sig);
190
OSSL_LIB_CTX_free(libctx);
191
return ret;
192
}
193
194