1 /*
2 * RSASSA-PSS/SHA-256 signature verification program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
17 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
18 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
19 !defined(MBEDTLS_CTR_DRBG_C)
main(void)20 int main(void)
21 {
22 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
23 "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
24 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
25 "MBEDTLS_CTR_DRBG_C not defined.\n");
26 mbedtls_exit(0);
27 }
28 #else
29
30 #include "mbedtls/md.h"
31 #include "mbedtls/pem.h"
32 #include "mbedtls/pk.h"
33 #include "mbedtls/md.h"
34
35 #include <stdio.h>
36 #include <string.h>
37
38
main(int argc,char * argv[])39 int main(int argc, char *argv[])
40 {
41 FILE *f;
42 int ret = 1;
43 int exit_code = MBEDTLS_EXIT_FAILURE;
44 size_t i;
45 mbedtls_pk_context pk;
46 unsigned char hash[32];
47 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
48 char filename[512];
49
50 mbedtls_pk_init(&pk);
51
52 #if defined(MBEDTLS_USE_PSA_CRYPTO)
53 psa_status_t status = psa_crypto_init();
54 if (status != PSA_SUCCESS) {
55 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
56 (int) status);
57 goto exit;
58 }
59 #endif /* MBEDTLS_USE_PSA_CRYPTO */
60
61 if (argc != 3) {
62 mbedtls_printf("usage: rsa_verify_pss <key_file> <filename>\n");
63
64 #if defined(_WIN32)
65 mbedtls_printf("\n");
66 #endif
67
68 goto exit;
69 }
70
71 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
72 fflush(stdout);
73
74 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
75 mbedtls_printf(" failed\n ! Could not read key from '%s'\n", argv[1]);
76 mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret);
77 goto exit;
78 }
79
80 if (!mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA)) {
81 mbedtls_printf(" failed\n ! Key is not an RSA key\n");
82 goto exit;
83 }
84
85 mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
86
87 /*
88 * Extract the RSA signature from the file
89 */
90 mbedtls_snprintf(filename, 512, "%s.sig", argv[2]);
91
92 if ((f = fopen(filename, "rb")) == NULL) {
93 mbedtls_printf("\n ! Could not open %s\n\n", filename);
94 goto exit;
95 }
96
97 i = fread(buf, 1, MBEDTLS_MPI_MAX_SIZE, f);
98
99 fclose(f);
100
101 /*
102 * Compute the SHA-256 hash of the input file and
103 * verify the signature
104 */
105 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
106 fflush(stdout);
107
108 if ((ret = mbedtls_md_file(
109 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
110 argv[2], hash)) != 0) {
111 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
112 goto exit;
113 }
114
115 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
116 buf, i)) != 0) {
117 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned %d\n\n", ret);
118 goto exit;
119 }
120
121 mbedtls_printf("\n . OK (the signature is valid)\n\n");
122
123 exit_code = MBEDTLS_EXIT_SUCCESS;
124
125 exit:
126 mbedtls_pk_free(&pk);
127 #if defined(MBEDTLS_USE_PSA_CRYPTO)
128 mbedtls_psa_crypto_free();
129 #endif /* MBEDTLS_USE_PSA_CRYPTO */
130
131 #if defined(_WIN32)
132 mbedtls_printf(" + Press Enter to exit this program.\n");
133 fflush(stdout); getchar();
134 #endif
135
136 mbedtls_exit(exit_code);
137 }
138 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
139 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
140