Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based signature creation program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Felix Conway | 998760a | 2025-03-24 11:37:33 +0000 | [diff] [blame] | 8 | #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS |
| 9 | |
Ronald Cron | 0815c67 | 2025-04-12 11:52:18 +0200 | [diff] [blame^] | 10 | #include "tf-psa-crypto/build_info.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/platform.h" |
Andrzej Kurek | 0af3248 | 2023-04-07 03:10:28 -0400 | [diff] [blame] | 13 | /* md.h is included this early since MD_CAN_XXX macros are defined there. */ |
Andrzej Kurek | 1b75e5f | 2023-04-04 09:55:06 -0400 | [diff] [blame] | 14 | #include "mbedtls/md.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 17 | !defined(PSA_WANT_ALG_SHA_256) || !defined(MBEDTLS_MD_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 19 | !defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 21 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 23 | "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_MD_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 24 | "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or " |
| 25 | "MBEDTLS_CTR_DRBG_C not defined.\n"); |
| 26 | mbedtls_exit(0); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 27 | } |
| 28 | #else |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 30 | #include "mbedtls/entropy.h" |
| 31 | #include "mbedtls/ctr_drbg.h" |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 32 | #include "mbedtls/pk.h" |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 38 | { |
| 39 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 40 | int ret = 1; |
Andres Amaya Garcia | 82b2726 | 2018-04-29 22:26:25 +0100 | [diff] [blame] | 41 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_pk_context pk; |
| 43 | mbedtls_entropy_context entropy; |
| 44 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 45 | unsigned char hash[32]; |
Gilles Peskine | 96a7cd1 | 2019-11-08 19:22:35 +0100 | [diff] [blame] | 46 | unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 47 | char filename[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | const char *pers = "mbedtls_pk_sign"; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 49 | size_t olen = 0; |
| 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | mbedtls_entropy_init(&entropy); |
| 52 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 53 | mbedtls_pk_init(&pk); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 54 | |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | if (argc != 3) { |
| 65 | mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 66 | |
| 67 | #if defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | goto exit; |
| 72 | } |
| 73 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 75 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 -0x%04x\n", |
| 81 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 82 | goto exit; |
| 83 | } |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | mbedtls_printf("\n . Reading private key from '%s'", argv[1]); |
| 86 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 87 | |
Ben Taylor | 440cb2a | 2025-03-05 09:40:08 +0000 | [diff] [blame] | 88 | if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 90 | goto exit; |
| 91 | } |
| 92 | |
| 93 | /* |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 94 | * Compute the SHA-256 hash of the input file, |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 95 | * then calculate the signature of the hash. |
| 96 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | mbedtls_printf("\n . Generating the SHA-256 signature"); |
| 98 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | if ((ret = mbedtls_md_file( |
| 101 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 102 | argv[2], hash)) != 0) { |
| 103 | mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 104 | goto exit; |
| 105 | } |
| 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0, |
Ben Taylor | 440cb2a | 2025-03-05 09:40:08 +0000 | [diff] [blame] | 108 | buf, sizeof(buf), &olen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | mbedtls_printf(" failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | /* |
Manuel Pégourié-Gonnard | 1d8f2da | 2015-08-27 21:42:27 +0200 | [diff] [blame] | 114 | * Write the signature into <filename>.sig |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | if ((f = fopen(filename, "wb+")) == NULL) { |
| 119 | mbedtls_printf(" failed\n ! Could not create %s\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 120 | goto exit; |
| 121 | } |
| 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | if (fwrite(buf, 1, olen, f) != olen) { |
| 124 | mbedtls_printf("failed\n ! fwrite failed\n\n"); |
| 125 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 126 | goto exit; |
| 127 | } |
| 128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | mbedtls_printf("\n . Done (created \"%s\")\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 132 | |
Andres Amaya Garcia | 82b2726 | 2018-04-29 22:26:25 +0100 | [diff] [blame] | 133 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 134 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 135 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | mbedtls_pk_free(&pk); |
| 137 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 138 | mbedtls_entropy_free(&entropy); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 140 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 141 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
Harry Ramsey | 9c66405 | 2024-10-16 14:08:19 +0100 | [diff] [blame] | 145 | mbedtls_printf("Error code: %d", ret); |
| 146 | /* mbedtls_strerror(ret, (char *) buf, sizeof(buf)); |
| 147 | mbedtls_printf(" ! Last error was: %s\n", buf); */ |
Manuel Pégourié-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 148 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 149 | #endif |
| 150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 152 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 154 | PSA_WANT_ALG_SHA_256 && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | MBEDTLS_CTR_DRBG_C */ |