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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 29 | !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 31 | !defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 32 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 33 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 35 | "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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 39 | } |
| 40 | #else |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 41 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 51 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 52 | { |
| 53 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 54 | int ret = 1; |
Andres Amaya Garcia | 82b2726 | 2018-04-29 22:26:25 +0100 | [diff] [blame] | 55 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | mbedtls_pk_context pk; |
| 57 | mbedtls_entropy_context entropy; |
| 58 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 59 | unsigned char hash[32]; |
Gilles Peskine | 96a7cd1 | 2019-11-08 19:22:35 +0100 | [diff] [blame] | 60 | unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 61 | char filename[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | const char *pers = "mbedtls_pk_sign"; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 63 | size_t olen = 0; |
| 64 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | mbedtls_entropy_init(&entropy); |
| 66 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 67 | mbedtls_pk_init(&pk); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 68 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | if (argc != 3) { |
| 70 | mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 71 | |
| 72 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | goto exit; |
| 77 | } |
| 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 80 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 81 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 87 | goto exit; |
| 88 | } |
| 89 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | mbedtls_printf("\n . Reading private key from '%s'", argv[1]); |
| 91 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 92 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) { |
| 94 | mbedtls_printf(" failed\n ! Could not parse '%s'\n", argv[1]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 95 | goto exit; |
| 96 | } |
| 97 | |
| 98 | /* |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 99 | * Compute the SHA-256 hash of the input file, |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 100 | * then calculate the signature of the hash. |
| 101 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | mbedtls_printf("\n . Generating the SHA-256 signature"); |
| 103 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 109 | goto exit; |
| 110 | } |
| 111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | |
| 118 | /* |
Manuel Pégourié-Gonnard | 1d8f2da | 2015-08-27 21:42:27 +0200 | [diff] [blame] | 119 | * Write the signature into <filename>.sig |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 120 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | if ((f = fopen(filename, "wb+")) == NULL) { |
| 124 | mbedtls_printf(" failed\n ! Could not create %s\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 125 | goto exit; |
| 126 | } |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | if (fwrite(buf, 1, olen, f) != olen) { |
| 129 | mbedtls_printf("failed\n ! fwrite failed\n\n"); |
| 130 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 131 | goto exit; |
| 132 | } |
| 133 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | mbedtls_printf("\n . Done (created \"%s\")\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 137 | |
Andres Amaya Garcia | 82b2726 | 2018-04-29 22:26:25 +0100 | [diff] [blame] | 138 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 139 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 140 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | mbedtls_pk_free(&pk); |
| 142 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 143 | mbedtls_entropy_free(&entropy); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | 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é-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 149 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 150 | #endif |
| 151 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 152 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 154 | fflush(stdout); getchar(); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 155 | #endif |
| 156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 158 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && |
| 160 | MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && |
| 161 | MBEDTLS_CTR_DRBG_C */ |