blob: d26ddfaaeb02353b637df1eb6d29bcfdbf6e1674 [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 Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker940f9ce2013-09-18 15:34:57 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.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) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020017 !defined(MBEDTLS_SHA256_C) || !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 Peskine1b6c09a2023-01-11 14:52:35 +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 "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023 "MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
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 Bakker940f9ce2013-09-18 15:34:57 +020027}
28#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020029
30#include "mbedtls/error.h"
31#include "mbedtls/entropy.h"
32#include "mbedtls/ctr_drbg.h"
33#include "mbedtls/md.h"
34#include "mbedtls/pk.h"
35
36#include <stdio.h>
37#include <string.h>
38
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020040{
41 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020042 int ret = 1;
Andres Amaya Garcia82b27262018-04-29 22:26:25 +010043 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_pk_context pk;
45 mbedtls_entropy_context entropy;
46 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020047 unsigned char hash[32];
Gilles Peskine96a7cd12019-11-08 19:22:35 +010048 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020049 char filename[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 const char *pers = "mbedtls_pk_sign";
Paul Bakker940f9ce2013-09-18 15:34:57 +020051 size_t olen = 0;
52
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 mbedtls_entropy_init(&entropy);
54 mbedtls_ctr_drbg_init(&ctr_drbg);
55 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020056
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +020057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58 psa_status_t status = psa_crypto_init();
59 if (status != PSA_SUCCESS) {
60 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
61 (int) status);
62 goto exit;
63 }
64#endif /* MBEDTLS_USE_PSA_CRYPTO */
65
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 if (argc != 3) {
67 mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020068
69#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020071#endif
72
73 goto exit;
74 }
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_printf("\n . Seeding the random number generator...");
77 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
80 (const unsigned char *) pers,
81 strlen(pers))) != 0) {
82 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
83 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020084 goto exit;
85 }
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
88 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) {
91 mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]);
Paul Bakker940f9ce2013-09-18 15:34:57 +020092 goto exit;
93 }
94
95 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000096 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +020097 * then calculate the signature of the hash.
98 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_printf("\n . Generating the SHA-256 signature");
100 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if ((ret = mbedtls_md_file(
103 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
104 argv[2], hash)) != 0) {
105 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200106 goto exit;
107 }
108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
110 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
111 mbedtls_printf(" failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112 goto exit;
113 }
114
115 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200116 * Write the signature into <filename>.sig
Paul Bakker940f9ce2013-09-18 15:34:57 +0200117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if ((f = fopen(filename, "wb+")) == NULL) {
121 mbedtls_printf(" failed\n ! Could not create %s\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122 goto exit;
123 }
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 if (fwrite(buf, 1, olen, f) != olen) {
126 mbedtls_printf("failed\n ! fwrite failed\n\n");
127 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128 goto exit;
129 }
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134
Andres Amaya Garcia82b27262018-04-29 22:26:25 +0100135 exit_code = MBEDTLS_EXIT_SUCCESS;
136
Paul Bakker940f9ce2013-09-18 15:34:57 +0200137exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_pk_free(&pk);
139 mbedtls_ctr_drbg_free(&ctr_drbg);
140 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200142 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200143#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
147 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
148 mbedtls_printf(" ! Last error was: %s\n", buf);
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200149 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200150#endif
151
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 mbedtls_printf(" + Press Enter to exit this program.\n");
154 fflush(stdout); getchar();
Paul Bakker940f9ce2013-09-18 15:34:57 +0200155#endif
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
160 MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
161 MBEDTLS_CTR_DRBG_C */