blob: c1640d66a32e03226305639d1e96b7a86d04d21b [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +02009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040011/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040012#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
Elena Uziunaite0916cd72024-05-23 17:01:07 +010015 !defined(PSA_WANT_ALG_SHA_256) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
17 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010018int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Elena Uziunaite0916cd72024-05-23 17:01:07 +010021 "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_MD_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010022 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
23 "MBEDTLS_CTR_DRBG_C not defined.\n");
24 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020025}
26#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020027
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020028#include "mbedtls/entropy.h"
29#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020030#include "mbedtls/pk.h"
31
32#include <stdio.h>
33#include <string.h>
34
Gilles Peskine449bd832023-01-11 14:50:10 +010035int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020036{
37 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020038 int ret = 1;
Andres Amaya Garcia82b27262018-04-29 22:26:25 +010039 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_pk_context pk;
41 mbedtls_entropy_context entropy;
42 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020043 unsigned char hash[32];
Gilles Peskine96a7cd12019-11-08 19:22:35 +010044 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 char filename[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 const char *pers = "mbedtls_pk_sign";
Paul Bakker940f9ce2013-09-18 15:34:57 +020047 size_t olen = 0;
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_entropy_init(&entropy);
50 mbedtls_ctr_drbg_init(&ctr_drbg);
51 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020052
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54 psa_status_t status = psa_crypto_init();
55 if (status != PSA_SUCCESS) {
56 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
57 (int) status);
58 goto exit;
59 }
60#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (argc != 3) {
63 mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020064
65#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020067#endif
68
69 goto exit;
70 }
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_printf("\n . Seeding the random number generator...");
73 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
76 (const unsigned char *) pers,
77 strlen(pers))) != 0) {
78 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
79 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020080 goto exit;
81 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
84 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
87 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
88 mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]);
Paul Bakker940f9ce2013-09-18 15:34:57 +020089 goto exit;
90 }
91
92 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000093 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 * then calculate the signature of the hash.
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_printf("\n . Generating the SHA-256 signature");
97 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if ((ret = mbedtls_md_file(
100 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
101 argv[2], hash)) != 0) {
102 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103 goto exit;
104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0,
107 buf, sizeof(buf), &olen,
108 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
109 mbedtls_printf(" failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 goto exit;
111 }
112
113 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200114 * Write the signature into <filename>.sig
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if ((f = fopen(filename, "wb+")) == NULL) {
119 mbedtls_printf(" failed\n ! Could not create %s\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (fwrite(buf, 1, olen, f) != olen) {
124 mbedtls_printf("failed\n ! fwrite failed\n\n");
125 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200126 goto exit;
127 }
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
Andres Amaya Garcia82b27262018-04-29 22:26:25 +0100133 exit_code = MBEDTLS_EXIT_SUCCESS;
134
Paul Bakker940f9ce2013-09-18 15:34:57 +0200135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_pk_free(&pk);
137 mbedtls_ctr_drbg_free(&ctr_drbg);
138 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200140 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100145 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é-Gonnardcf9ab632015-08-27 22:03:33 +0200148 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200149#endif
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100154 PSA_WANT_ALG_SHA_256 && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_CTR_DRBG_C */