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