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