1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Example RSA key generation program
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
6a8e1175bSopenharmony_ci *
7a8e1175bSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8a8e1175bSopenharmony_ci *  not use this file except in compliance with the License.
9a8e1175bSopenharmony_ci *  You may obtain a copy of the License at
10a8e1175bSopenharmony_ci *
11a8e1175bSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
12a8e1175bSopenharmony_ci *
13a8e1175bSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
14a8e1175bSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15a8e1175bSopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16a8e1175bSopenharmony_ci *  See the License for the specific language governing permissions and
17a8e1175bSopenharmony_ci *  limitations under the License.
18a8e1175bSopenharmony_ci */
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci#include "mbedtls/platform.h"
23a8e1175bSopenharmony_ci
24a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
25a8e1175bSopenharmony_ci    defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
26a8e1175bSopenharmony_ci    defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
27a8e1175bSopenharmony_ci#include "mbedtls/entropy.h"
28a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h"
29a8e1175bSopenharmony_ci#include "mbedtls/bignum.h"
30a8e1175bSopenharmony_ci#include "mbedtls/rsa.h"
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_ci#include <stdio.h>
33a8e1175bSopenharmony_ci#include <string.h>
34a8e1175bSopenharmony_ci#endif
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ci#define KEY_SIZE 2048
37a8e1175bSopenharmony_ci#define EXPONENT 65537
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_ci#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \
40a8e1175bSopenharmony_ci    !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) ||      \
41a8e1175bSopenharmony_ci    !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
42a8e1175bSopenharmony_ciint main(void)
43a8e1175bSopenharmony_ci{
44a8e1175bSopenharmony_ci    mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
45a8e1175bSopenharmony_ci                   "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
46a8e1175bSopenharmony_ci                   "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
47a8e1175bSopenharmony_ci    mbedtls_exit(0);
48a8e1175bSopenharmony_ci}
49a8e1175bSopenharmony_ci#else
50a8e1175bSopenharmony_ci
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_ciint main(void)
53a8e1175bSopenharmony_ci{
54a8e1175bSopenharmony_ci    int ret = 1;
55a8e1175bSopenharmony_ci    int exit_code = MBEDTLS_EXIT_FAILURE;
56a8e1175bSopenharmony_ci    mbedtls_rsa_context rsa;
57a8e1175bSopenharmony_ci    mbedtls_entropy_context entropy;
58a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_context ctr_drbg;
59a8e1175bSopenharmony_ci    mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
60a8e1175bSopenharmony_ci    FILE *fpub  = NULL;
61a8e1175bSopenharmony_ci    FILE *fpriv = NULL;
62a8e1175bSopenharmony_ci    const char *pers = "rsa_genkey";
63a8e1175bSopenharmony_ci
64a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_init(&ctr_drbg);
65a8e1175bSopenharmony_ci    mbedtls_rsa_init(&rsa);
66a8e1175bSopenharmony_ci    mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
67a8e1175bSopenharmony_ci    mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
68a8e1175bSopenharmony_ci    mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Seeding the random number generator...");
71a8e1175bSopenharmony_ci    fflush(stdout);
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci    mbedtls_entropy_init(&entropy);
74a8e1175bSopenharmony_ci    if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
75a8e1175bSopenharmony_ci                                     (const unsigned char *) pers,
76a8e1175bSopenharmony_ci                                     strlen(pers))) != 0) {
77a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret);
78a8e1175bSopenharmony_ci        goto exit;
79a8e1175bSopenharmony_ci    }
80a8e1175bSopenharmony_ci
81a8e1175bSopenharmony_ci    mbedtls_printf(" ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
82a8e1175bSopenharmony_ci    fflush(stdout);
83a8e1175bSopenharmony_ci
84a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
85a8e1175bSopenharmony_ci                                   EXPONENT)) != 0) {
86a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret);
87a8e1175bSopenharmony_ci        goto exit;
88a8e1175bSopenharmony_ci    }
89a8e1175bSopenharmony_ci
90a8e1175bSopenharmony_ci    mbedtls_printf(" ok\n  . Exporting the public  key in rsa_pub.txt....");
91a8e1175bSopenharmony_ci    fflush(stdout);
92a8e1175bSopenharmony_ci
93a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
94a8e1175bSopenharmony_ci        (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP))      != 0) {
95a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! could not export RSA parameters\n\n");
96a8e1175bSopenharmony_ci        goto exit;
97a8e1175bSopenharmony_ci    }
98a8e1175bSopenharmony_ci
99a8e1175bSopenharmony_ci    if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
100a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! could not open rsa_pub.txt for writing\n\n");
101a8e1175bSopenharmony_ci        goto exit;
102a8e1175bSopenharmony_ci    }
103a8e1175bSopenharmony_ci
104a8e1175bSopenharmony_ci    if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
105a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
106a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret);
107a8e1175bSopenharmony_ci        goto exit;
108a8e1175bSopenharmony_ci    }
109a8e1175bSopenharmony_ci
110a8e1175bSopenharmony_ci    mbedtls_printf(" ok\n  . Exporting the private key in rsa_priv.txt...");
111a8e1175bSopenharmony_ci    fflush(stdout);
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci    if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
114a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! could not open rsa_priv.txt for writing\n");
115a8e1175bSopenharmony_ci        goto exit;
116a8e1175bSopenharmony_ci    }
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci    if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
119a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
120a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
121a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
122a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
123a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
124a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
125a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
126a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret);
127a8e1175bSopenharmony_ci        goto exit;
128a8e1175bSopenharmony_ci    }
129a8e1175bSopenharmony_ci    mbedtls_printf(" ok\n\n");
130a8e1175bSopenharmony_ci
131a8e1175bSopenharmony_ci    exit_code = MBEDTLS_EXIT_SUCCESS;
132a8e1175bSopenharmony_ci
133a8e1175bSopenharmony_ciexit:
134a8e1175bSopenharmony_ci
135a8e1175bSopenharmony_ci    if (fpub  != NULL) {
136a8e1175bSopenharmony_ci        fclose(fpub);
137a8e1175bSopenharmony_ci    }
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci    if (fpriv != NULL) {
140a8e1175bSopenharmony_ci        fclose(fpriv);
141a8e1175bSopenharmony_ci    }
142a8e1175bSopenharmony_ci
143a8e1175bSopenharmony_ci    mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
144a8e1175bSopenharmony_ci    mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
145a8e1175bSopenharmony_ci    mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
146a8e1175bSopenharmony_ci    mbedtls_rsa_free(&rsa);
147a8e1175bSopenharmony_ci    mbedtls_ctr_drbg_free(&ctr_drbg);
148a8e1175bSopenharmony_ci    mbedtls_entropy_free(&entropy);
149a8e1175bSopenharmony_ci
150a8e1175bSopenharmony_ci    mbedtls_exit(exit_code);
151a8e1175bSopenharmony_ci}
152a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
153a8e1175bSopenharmony_ci          MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
154