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