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