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 #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
10 #include "tf-psa-crypto/build_info.h"
11
12 #include "mbedtls/platform.h"
13 /* md.h is included this early since MD_CAN_XXX macros are defined there. */
14 #include "mbedtls/md.h"
15
16 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
17 !defined(MBEDTLS_RSA_C) || !defined(PSA_WANT_ALG_SHA_256) || \
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 PSA_WANT_ALG_SHA_256 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 #if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
34 #include <mbedtls/private/pk_private.h>
35 #endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
36
37 #include <stdio.h>
38 #include <string.h>
39
40
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 FILE *f;
44 int ret = 1;
45 int exit_code = MBEDTLS_EXIT_FAILURE;
46 size_t i;
47 mbedtls_pk_context pk;
48 unsigned char hash[32];
49 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
50 char filename[512];
51
52 mbedtls_pk_init(&pk);
53
54 psa_status_t status = psa_crypto_init();
55 if (status != PSA_SUCCESS) {
56 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
57 (int) status);
58 goto exit;
59 }
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 if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk),
86 MBEDTLS_RSA_PKCS_V21,
87 MBEDTLS_MD_SHA256)) != 0) {
88 mbedtls_printf(" failed\n ! Invalid padding\n");
89 goto exit;
90 }
91
92 /*
93 * Extract the RSA signature from the file
94 */
95 mbedtls_snprintf(filename, 512, "%s.sig", argv[2]);
96
97 if ((f = fopen(filename, "rb")) == NULL) {
98 mbedtls_printf("\n ! Could not open %s\n\n", filename);
99 goto exit;
100 }
101
102 i = fread(buf, 1, MBEDTLS_MPI_MAX_SIZE, f);
103
104 fclose(f);
105
106 /*
107 * Compute the SHA-256 hash of the input file and
108 * verify the signature
109 */
110 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
111 fflush(stdout);
112
113 if ((ret = mbedtls_md_file(
114 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
115 argv[2], hash)) != 0) {
116 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
117 goto exit;
118 }
119
120 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
121 buf, i)) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned %d\n\n", ret);
123 goto exit;
124 }
125
126 mbedtls_printf("\n . OK (the signature is valid)\n\n");
127
128 exit_code = MBEDTLS_EXIT_SUCCESS;
129
130 exit:
131 mbedtls_pk_free(&pk);
132 mbedtls_psa_crypto_free();
133
134 mbedtls_exit(exit_code);
135 }
136 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 &&
137 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
138