blob: 8db233373843b866b742d88ec99764416e8446d2 [file] [log] [blame]
Neil Armstronge0326a62022-02-25 08:57:19 +01001/**
2 * \file pkwrite.h
3 *
4 * \brief Internal defines shared by the PK write module
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#ifndef MBEDTLS_PK_WRITE_H
24#define MBEDTLS_PK_WRITE_H
25
26#include "mbedtls/build_info.h"
27
28#include "mbedtls/pk.h"
29
30/*
31 * Max sizes of key per types. Shown as tag + len (+ content).
32 */
33
34#if defined(MBEDTLS_RSA_C)
35/*
36 * RSA public keys:
37 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
38 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
39 * + 1 + 1 + 9 (rsa oid)
40 * + 1 + 1 (params null)
41 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
42 * RSAPublicKey ::= SEQUENCE { 1 + 3
43 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
44 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
45 * }
46 */
Gilles Peskine449bd832023-01-11 14:50:10 +010047#define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
Neil Armstronge0326a62022-02-25 08:57:19 +010048
49/*
50 * RSA private keys:
51 * RSAPrivateKey ::= SEQUENCE { 1 + 3
52 * version Version, 1 + 1 + 1
53 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
54 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
55 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
56 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
57 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
58 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
59 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
60 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
61 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
62 * }
63 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064#define MBEDTLS_MPI_MAX_SIZE_2 (MBEDTLS_MPI_MAX_SIZE / 2 + \
65 MBEDTLS_MPI_MAX_SIZE % 2)
66#define MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES (47 + 3 * MBEDTLS_MPI_MAX_SIZE \
67 + 5 * MBEDTLS_MPI_MAX_SIZE_2)
Neil Armstronge0326a62022-02-25 08:57:19 +010068
69#else /* MBEDTLS_RSA_C */
70
Neil Armstronge9ecd272022-03-01 10:03:21 +010071#define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES 0
72#define MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES 0
Neil Armstronge0326a62022-02-25 08:57:19 +010073
74#endif /* MBEDTLS_RSA_C */
75
Valerio Setti4064dbb2023-05-17 15:33:07 +020076#if defined(MBEDTLS_ECP_LIGHT)
Neil Armstronge0326a62022-02-25 08:57:19 +010077/*
78 * EC public keys:
79 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
80 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
81 * + 1 + 1 + 7 (ec oid)
82 * + 1 + 1 + 9 (namedCurve oid)
83 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
84 * + 1 (point format) [1]
85 * + 2 * ECP_MAX (coords) [1]
86 * }
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088#define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
Neil Armstronge0326a62022-02-25 08:57:19 +010089
90/*
91 * EC private keys:
92 * ECPrivateKey ::= SEQUENCE { 1 + 2
93 * version INTEGER , 1 + 1 + 1
94 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
95 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
96 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
97 * }
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099#define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES (29 + 3 * MBEDTLS_ECP_MAX_BYTES)
Neil Armstronge0326a62022-02-25 08:57:19 +0100100
Valerio Setti4064dbb2023-05-17 15:33:07 +0200101#else /* MBEDTLS_ECP_LIGHT */
Neil Armstronge0326a62022-02-25 08:57:19 +0100102
Neil Armstronge9ecd272022-03-01 10:03:21 +0100103#define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES 0
104#define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES 0
Neil Armstronge0326a62022-02-25 08:57:19 +0100105
Jethro Beekman01672442023-04-19 14:08:14 +0200106#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstronge0326a62022-02-25 08:57:19 +0100107#endif /* MBEDTLS_PK_WRITE_H */