1a8e1175bSopenharmony_ci/* 2a8e1175bSopenharmony_ci * RSASSA-PSS/SHA-256 signature creation 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/entropy.h" 29a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h" 30a8e1175bSopenharmony_ci#include "mbedtls/rsa.h" 31a8e1175bSopenharmony_ci#include "mbedtls/pk.h" 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci#include <stdio.h> 34a8e1175bSopenharmony_ci#include <string.h> 35a8e1175bSopenharmony_ci 36a8e1175bSopenharmony_ci 37a8e1175bSopenharmony_ciint main(int argc, char *argv[]) 38a8e1175bSopenharmony_ci{ 39a8e1175bSopenharmony_ci FILE *f; 40a8e1175bSopenharmony_ci int ret = 1; 41a8e1175bSopenharmony_ci int exit_code = MBEDTLS_EXIT_FAILURE; 42a8e1175bSopenharmony_ci mbedtls_pk_context pk; 43a8e1175bSopenharmony_ci mbedtls_entropy_context entropy; 44a8e1175bSopenharmony_ci mbedtls_ctr_drbg_context ctr_drbg; 45a8e1175bSopenharmony_ci unsigned char hash[32]; 46a8e1175bSopenharmony_ci unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; 47a8e1175bSopenharmony_ci char filename[512]; 48a8e1175bSopenharmony_ci const char *pers = "rsa_sign_pss"; 49a8e1175bSopenharmony_ci size_t olen = 0; 50a8e1175bSopenharmony_ci 51a8e1175bSopenharmony_ci mbedtls_entropy_init(&entropy); 52a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 53a8e1175bSopenharmony_ci mbedtls_ctr_drbg_init(&ctr_drbg); 54a8e1175bSopenharmony_ci 55a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 56a8e1175bSopenharmony_ci psa_status_t status = psa_crypto_init(); 57a8e1175bSopenharmony_ci if (status != PSA_SUCCESS) { 58a8e1175bSopenharmony_ci mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", 59a8e1175bSopenharmony_ci (int) status); 60a8e1175bSopenharmony_ci goto exit; 61a8e1175bSopenharmony_ci } 62a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 63a8e1175bSopenharmony_ci 64a8e1175bSopenharmony_ci if (argc != 3) { 65a8e1175bSopenharmony_ci mbedtls_printf("usage: rsa_sign_pss <key_file> <filename>\n"); 66a8e1175bSopenharmony_ci 67a8e1175bSopenharmony_ci#if defined(_WIN32) 68a8e1175bSopenharmony_ci mbedtls_printf("\n"); 69a8e1175bSopenharmony_ci#endif 70a8e1175bSopenharmony_ci 71a8e1175bSopenharmony_ci goto exit; 72a8e1175bSopenharmony_ci } 73a8e1175bSopenharmony_ci 74a8e1175bSopenharmony_ci mbedtls_printf("\n . Seeding the random number generator..."); 75a8e1175bSopenharmony_ci fflush(stdout); 76a8e1175bSopenharmony_ci 77a8e1175bSopenharmony_ci if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, 78a8e1175bSopenharmony_ci (const unsigned char *) pers, 79a8e1175bSopenharmony_ci strlen(pers))) != 0) { 80a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); 81a8e1175bSopenharmony_ci goto exit; 82a8e1175bSopenharmony_ci } 83a8e1175bSopenharmony_ci 84a8e1175bSopenharmony_ci mbedtls_printf("\n . Reading private key from '%s'", argv[1]); 85a8e1175bSopenharmony_ci fflush(stdout); 86a8e1175bSopenharmony_ci 87a8e1175bSopenharmony_ci if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "", 88a8e1175bSopenharmony_ci mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { 89a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! Could not read key from '%s'\n", argv[1]); 90a8e1175bSopenharmony_ci mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret); 91a8e1175bSopenharmony_ci goto exit; 92a8e1175bSopenharmony_ci } 93a8e1175bSopenharmony_ci 94a8e1175bSopenharmony_ci if (!mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA)) { 95a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! Key is not an RSA key\n"); 96a8e1175bSopenharmony_ci goto exit; 97a8e1175bSopenharmony_ci } 98a8e1175bSopenharmony_ci 99a8e1175bSopenharmony_ci if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk), 100a8e1175bSopenharmony_ci MBEDTLS_RSA_PKCS_V21, 101a8e1175bSopenharmony_ci MBEDTLS_MD_SHA256)) != 0) { 102a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! Padding not supported\n"); 103a8e1175bSopenharmony_ci goto exit; 104a8e1175bSopenharmony_ci } 105a8e1175bSopenharmony_ci 106a8e1175bSopenharmony_ci /* 107a8e1175bSopenharmony_ci * Compute the SHA-256 hash of the input file, 108a8e1175bSopenharmony_ci * then calculate the RSA signature of the hash. 109a8e1175bSopenharmony_ci */ 110a8e1175bSopenharmony_ci mbedtls_printf("\n . Generating the RSA/SHA-256 signature"); 111a8e1175bSopenharmony_ci fflush(stdout); 112a8e1175bSopenharmony_ci 113a8e1175bSopenharmony_ci if ((ret = mbedtls_md_file( 114a8e1175bSopenharmony_ci mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 115a8e1175bSopenharmony_ci argv[2], hash)) != 0) { 116a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]); 117a8e1175bSopenharmony_ci goto exit; 118a8e1175bSopenharmony_ci } 119a8e1175bSopenharmony_ci 120a8e1175bSopenharmony_ci if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0, 121a8e1175bSopenharmony_ci buf, sizeof(buf), &olen, 122a8e1175bSopenharmony_ci mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { 123a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_pk_sign returned %d\n\n", ret); 124a8e1175bSopenharmony_ci goto exit; 125a8e1175bSopenharmony_ci } 126a8e1175bSopenharmony_ci 127a8e1175bSopenharmony_ci /* 128a8e1175bSopenharmony_ci * Write the signature into <filename>.sig 129a8e1175bSopenharmony_ci */ 130a8e1175bSopenharmony_ci mbedtls_snprintf(filename, 512, "%s.sig", argv[2]); 131a8e1175bSopenharmony_ci 132a8e1175bSopenharmony_ci if ((f = fopen(filename, "wb+")) == NULL) { 133a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! Could not create %s\n\n", filename); 134a8e1175bSopenharmony_ci goto exit; 135a8e1175bSopenharmony_ci } 136a8e1175bSopenharmony_ci 137a8e1175bSopenharmony_ci if (fwrite(buf, 1, olen, f) != olen) { 138a8e1175bSopenharmony_ci mbedtls_printf("failed\n ! fwrite failed\n\n"); 139a8e1175bSopenharmony_ci fclose(f); 140a8e1175bSopenharmony_ci goto exit; 141a8e1175bSopenharmony_ci } 142a8e1175bSopenharmony_ci 143a8e1175bSopenharmony_ci fclose(f); 144a8e1175bSopenharmony_ci 145a8e1175bSopenharmony_ci mbedtls_printf("\n . Done (created \"%s\")\n\n", filename); 146a8e1175bSopenharmony_ci 147a8e1175bSopenharmony_ci exit_code = MBEDTLS_EXIT_SUCCESS; 148a8e1175bSopenharmony_ci 149a8e1175bSopenharmony_ciexit: 150a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 151a8e1175bSopenharmony_ci mbedtls_ctr_drbg_free(&ctr_drbg); 152a8e1175bSopenharmony_ci mbedtls_entropy_free(&entropy); 153a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 154a8e1175bSopenharmony_ci mbedtls_psa_crypto_free(); 155a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 156a8e1175bSopenharmony_ci 157a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 158a8e1175bSopenharmony_ci} 159a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C && 160a8e1175bSopenharmony_ci MBEDTLS_MD_CAN_SHA256 && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && 161a8e1175bSopenharmony_ci MBEDTLS_CTR_DRBG_C */ 162