blob: 551173e496ce79a4d370153c2127657ba7f6b992 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature creation program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker940f9ce2013-09-18 15:34:57 +02006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Ronald Cron0815c672025-04-12 11:52:18 +020010#include "tf-psa-crypto/build_info.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040013/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040014#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
Elena Uziunaite0916cd72024-05-23 17:01:07 +010017 !defined(PSA_WANT_ALG_SHA_256) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
19 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010020int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Elena Uziunaite0916cd72024-05-23 17:01:07 +010023 "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_MD_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010024 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
25 "MBEDTLS_CTR_DRBG_C not defined.\n");
26 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020027}
28#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020029
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020030#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020032#include "mbedtls/pk.h"
Ben Taylorc801d322025-07-03 15:01:39 +010033#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
34#include <mbedtls/private/pk_private.h>
35#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020036
37#include <stdio.h>
38#include <string.h>
39
Gilles Peskine449bd832023-01-11 14:50:10 +010040int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020041{
42 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020043 int ret = 1;
Andres Amaya Garcia82b27262018-04-29 22:26:25 +010044 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_pk_context pk;
46 mbedtls_entropy_context entropy;
47 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020048 unsigned char hash[32];
Gilles Peskine96a7cd12019-11-08 19:22:35 +010049 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020050 char filename[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 const char *pers = "mbedtls_pk_sign";
Paul Bakker940f9ce2013-09-18 15:34:57 +020052 size_t olen = 0;
53
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_entropy_init(&entropy);
55 mbedtls_ctr_drbg_init(&ctr_drbg);
56 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020057
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO)
59 psa_status_t status = psa_crypto_init();
60 if (status != PSA_SUCCESS) {
61 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
62 (int) status);
63 goto exit;
64 }
65#endif /* MBEDTLS_USE_PSA_CRYPTO */
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (argc != 3) {
68 mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020069
70#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020072#endif
73
74 goto exit;
75 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_printf("\n . Seeding the random number generator...");
78 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
81 (const unsigned char *) pers,
82 strlen(pers))) != 0) {
83 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
84 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020085 goto exit;
86 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
89 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020090
Ben Taylor440cb2a2025-03-05 09:40:08 +000091 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]);
Paul Bakker940f9ce2013-09-18 15:34:57 +020093 goto exit;
94 }
95
96 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000097 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +020098 * then calculate the signature of the hash.
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_printf("\n . Generating the SHA-256 signature");
101 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if ((ret = mbedtls_md_file(
104 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
105 argv[2], hash)) != 0) {
106 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200107 goto exit;
108 }
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000111 buf, sizeof(buf), &olen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_printf(" failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113 goto exit;
114 }
115
116 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200117 * Write the signature into <filename>.sig
Paul Bakker940f9ce2013-09-18 15:34:57 +0200118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if ((f = fopen(filename, "wb+")) == NULL) {
122 mbedtls_printf(" failed\n ! Could not create %s\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200123 goto exit;
124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (fwrite(buf, 1, olen, f) != olen) {
127 mbedtls_printf("failed\n ! fwrite failed\n\n");
128 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129 goto exit;
130 }
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200135
Andres Amaya Garcia82b27262018-04-29 22:26:25 +0100136 exit_code = MBEDTLS_EXIT_SUCCESS;
137
Paul Bakker940f9ce2013-09-18 15:34:57 +0200138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_pk_free(&pk);
140 mbedtls_ctr_drbg_free(&ctr_drbg);
141 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200143 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200144#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100148 mbedtls_printf("Error code: %d", ret);
149 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
150 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200151 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200152#endif
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100157 PSA_WANT_ALG_SHA_256 && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_CTR_DRBG_C */