1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Public key-based signature creation program
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6a8e1175bSopenharmony_ci */
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci#include "mbedtls/platform.h"
11a8e1175bSopenharmony_ci/* md.h is included this early since MD_CAN_XXX macros are defined there. */
12a8e1175bSopenharmony_ci#include "mbedtls/md.h"
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
15a8e1175bSopenharmony_ci    !defined(MBEDTLS_MD_CAN_SHA256) || !defined(MBEDTLS_MD_C) || \
16a8e1175bSopenharmony_ci    !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) ||    \
17a8e1175bSopenharmony_ci    !defined(MBEDTLS_CTR_DRBG_C)
18a8e1175bSopenharmony_ciint main(void)
19a8e1175bSopenharmony_ci{
20a8e1175bSopenharmony_ci    mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
21a8e1175bSopenharmony_ci                   "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_MD_C and/or "
22a8e1175bSopenharmony_ci                   "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
23a8e1175bSopenharmony_ci                   "MBEDTLS_CTR_DRBG_C not defined.\n");
24a8e1175bSopenharmony_ci    mbedtls_exit(0);
25a8e1175bSopenharmony_ci}
26a8e1175bSopenharmony_ci#else
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_ci#include "mbedtls/error.h"
29a8e1175bSopenharmony_ci#include "mbedtls/entropy.h"
30a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h"
31a8e1175bSopenharmony_ci#include "mbedtls/pk.h"
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci#include <stdio.h>
34a8e1175bSopenharmony_ci#include <string.h>
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ciint main(int argc, char *argv[])
37a8e1175bSopenharmony_ci{
38a8e1175bSopenharmony_ci    FILE *f;
39a8e1175bSopenharmony_ci    int ret = 1;
40a8e1175bSopenharmony_ci    int exit_code = MBEDTLS_EXIT_FAILURE;
41a8e1175bSopenharmony_ci    mbedtls_pk_context pk;
42a8e1175bSopenharmony_ci    mbedtls_entropy_context entropy;
43a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_context ctr_drbg;
44a8e1175bSopenharmony_ci    unsigned char hash[32];
45a8e1175bSopenharmony_ci    unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
46a8e1175bSopenharmony_ci    char filename[512];
47a8e1175bSopenharmony_ci    const char *pers = "mbedtls_pk_sign";
48a8e1175bSopenharmony_ci    size_t olen = 0;
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci    mbedtls_entropy_init(&entropy);
51a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_init(&ctr_drbg);
52a8e1175bSopenharmony_ci    mbedtls_pk_init(&pk);
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
55a8e1175bSopenharmony_ci    psa_status_t status = psa_crypto_init();
56a8e1175bSopenharmony_ci    if (status != PSA_SUCCESS) {
57a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
58a8e1175bSopenharmony_ci                        (int) status);
59a8e1175bSopenharmony_ci        goto exit;
60a8e1175bSopenharmony_ci    }
61a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci    if (argc != 3) {
64a8e1175bSopenharmony_ci        mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ci#if defined(_WIN32)
67a8e1175bSopenharmony_ci        mbedtls_printf("\n");
68a8e1175bSopenharmony_ci#endif
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_ci        goto exit;
71a8e1175bSopenharmony_ci    }
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Seeding the random number generator...");
74a8e1175bSopenharmony_ci    fflush(stdout);
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci    if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
77a8e1175bSopenharmony_ci                                     (const unsigned char *) pers,
78a8e1175bSopenharmony_ci                                     strlen(pers))) != 0) {
79a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
80a8e1175bSopenharmony_ci                       (unsigned int) -ret);
81a8e1175bSopenharmony_ci        goto exit;
82a8e1175bSopenharmony_ci    }
83a8e1175bSopenharmony_ci
84a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Reading private key from '%s'", argv[1]);
85a8e1175bSopenharmony_ci    fflush(stdout);
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci    if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
88a8e1175bSopenharmony_ci                                        mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
89a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not parse '%s'\n", argv[1]);
90a8e1175bSopenharmony_ci        goto exit;
91a8e1175bSopenharmony_ci    }
92a8e1175bSopenharmony_ci
93a8e1175bSopenharmony_ci    /*
94a8e1175bSopenharmony_ci     * Compute the SHA-256 hash of the input file,
95a8e1175bSopenharmony_ci     * then calculate the signature of the hash.
96a8e1175bSopenharmony_ci     */
97a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Generating the SHA-256 signature");
98a8e1175bSopenharmony_ci    fflush(stdout);
99a8e1175bSopenharmony_ci
100a8e1175bSopenharmony_ci    if ((ret = mbedtls_md_file(
101a8e1175bSopenharmony_ci             mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
102a8e1175bSopenharmony_ci             argv[2], hash)) != 0) {
103a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[2]);
104a8e1175bSopenharmony_ci        goto exit;
105a8e1175bSopenharmony_ci    }
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci    if ((ret = mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0,
108a8e1175bSopenharmony_ci                               buf, sizeof(buf), &olen,
109a8e1175bSopenharmony_ci                               mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
110a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret);
111a8e1175bSopenharmony_ci        goto exit;
112a8e1175bSopenharmony_ci    }
113a8e1175bSopenharmony_ci
114a8e1175bSopenharmony_ci    /*
115a8e1175bSopenharmony_ci     * Write the signature into <filename>.sig
116a8e1175bSopenharmony_ci     */
117a8e1175bSopenharmony_ci    mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
118a8e1175bSopenharmony_ci
119a8e1175bSopenharmony_ci    if ((f = fopen(filename, "wb+")) == NULL) {
120a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not create %s\n\n", filename);
121a8e1175bSopenharmony_ci        goto exit;
122a8e1175bSopenharmony_ci    }
123a8e1175bSopenharmony_ci
124a8e1175bSopenharmony_ci    if (fwrite(buf, 1, olen, f) != olen) {
125a8e1175bSopenharmony_ci        mbedtls_printf("failed\n  ! fwrite failed\n\n");
126a8e1175bSopenharmony_ci        fclose(f);
127a8e1175bSopenharmony_ci        goto exit;
128a8e1175bSopenharmony_ci    }
129a8e1175bSopenharmony_ci
130a8e1175bSopenharmony_ci    fclose(f);
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Done (created \"%s\")\n\n", filename);
133a8e1175bSopenharmony_ci
134a8e1175bSopenharmony_ci    exit_code = MBEDTLS_EXIT_SUCCESS;
135a8e1175bSopenharmony_ci
136a8e1175bSopenharmony_ciexit:
137a8e1175bSopenharmony_ci    mbedtls_pk_free(&pk);
138a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_free(&ctr_drbg);
139a8e1175bSopenharmony_ci    mbedtls_entropy_free(&entropy);
140a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
141a8e1175bSopenharmony_ci    mbedtls_psa_crypto_free();
142a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
143a8e1175bSopenharmony_ci
144a8e1175bSopenharmony_ci#if defined(MBEDTLS_ERROR_C)
145a8e1175bSopenharmony_ci    if (exit_code != MBEDTLS_EXIT_SUCCESS) {
146a8e1175bSopenharmony_ci        mbedtls_strerror(ret, (char *) buf, sizeof(buf));
147a8e1175bSopenharmony_ci        mbedtls_printf("  !  Last error was: %s\n", buf);
148a8e1175bSopenharmony_ci    }
149a8e1175bSopenharmony_ci#endif
150a8e1175bSopenharmony_ci
151a8e1175bSopenharmony_ci    mbedtls_exit(exit_code);
152a8e1175bSopenharmony_ci}
153a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
154a8e1175bSopenharmony_ci          MBEDTLS_MD_CAN_SHA256 && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
155a8e1175bSopenharmony_ci          MBEDTLS_CTR_DRBG_C */
156