blob: 19a855b2b0b12a0c5abd71dcd14e61845ff6bb78 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker940f9ce2013-09-18 15:34:57 +020018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020029 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
31 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 "MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
36 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
37 "MBEDTLS_CTR_DRBG_C not defined.\n");
38 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020039}
40#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020041
42#include "mbedtls/error.h"
43#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
45#include "mbedtls/md.h"
46#include "mbedtls/pk.h"
47
48#include <stdio.h>
49#include <string.h>
50
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020052{
53 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020054 int ret = 1;
Andres Amaya Garcia82b27262018-04-29 22:26:25 +010055 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_pk_context pk;
57 mbedtls_entropy_context entropy;
58 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020059 unsigned char hash[32];
Gilles Peskine96a7cd12019-11-08 19:22:35 +010060 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020061 char filename[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 const char *pers = "mbedtls_pk_sign";
Paul Bakker940f9ce2013-09-18 15:34:57 +020063 size_t olen = 0;
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_entropy_init(&entropy);
66 mbedtls_ctr_drbg_init(&ctr_drbg);
67 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 if (argc != 3) {
70 mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020071
72#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020074#endif
75
76 goto exit;
77 }
78
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 mbedtls_printf("\n . Seeding the random number generator...");
80 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
83 (const unsigned char *) pers,
84 strlen(pers))) != 0) {
85 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
86 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020087 goto exit;
88 }
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
91 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) {
94 mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]);
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
98 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000099 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200100 * then calculate the signature of the hash.
101 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 mbedtls_printf("\n . Generating the SHA-256 signature");
103 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if ((ret = mbedtls_md_file(
106 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
107 argv[2], hash)) != 0) {
108 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200109 goto exit;
110 }
111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
113 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
114 mbedtls_printf(" failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 goto exit;
116 }
117
118 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200119 * Write the signature into <filename>.sig
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 if ((f = fopen(filename, "wb+")) == NULL) {
124 mbedtls_printf(" failed\n ! Could not create %s\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200125 goto exit;
126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 if (fwrite(buf, 1, olen, f) != olen) {
129 mbedtls_printf("failed\n ! fwrite failed\n\n");
130 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131 goto exit;
132 }
133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200137
Andres Amaya Garcia82b27262018-04-29 22:26:25 +0100138 exit_code = MBEDTLS_EXIT_SUCCESS;
139
Paul Bakker940f9ce2013-09-18 15:34:57 +0200140exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_pk_free(&pk);
142 mbedtls_ctr_drbg_free(&ctr_drbg);
143 mbedtls_entropy_free(&entropy);
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 */