blob: 8cfa64b8eb03b60f9de2a34a8639994d339f153c [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
Valerio Settia7c9e092023-07-27 14:39:50 +020030#if defined(MBEDTLS_USE_PSA_CRYPTO)
31#include "psa/crypto.h"
32#endif /* MBEDTLS_USE_PSA_CRYPTO */
33
Neil Armstronge0326a62022-02-25 08:57:19 +010034/*
35 * Max sizes of key per types. Shown as tag + len (+ content).
36 */
37
38#if defined(MBEDTLS_RSA_C)
39/*
40 * RSA public keys:
41 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
42 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
43 * + 1 + 1 + 9 (rsa oid)
44 * + 1 + 1 (params null)
45 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
46 * RSAPublicKey ::= SEQUENCE { 1 + 3
47 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
48 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
49 * }
50 */
Gilles Peskine449bd832023-01-11 14:50:10 +010051#define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
Neil Armstronge0326a62022-02-25 08:57:19 +010052
53/*
54 * RSA private keys:
55 * RSAPrivateKey ::= SEQUENCE { 1 + 3
56 * version Version, 1 + 1 + 1
57 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
58 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
59 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
60 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
61 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
62 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
63 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
64 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
65 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
66 * }
67 */
Gilles Peskine449bd832023-01-11 14:50:10 +010068#define MBEDTLS_MPI_MAX_SIZE_2 (MBEDTLS_MPI_MAX_SIZE / 2 + \
69 MBEDTLS_MPI_MAX_SIZE % 2)
70#define MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES (47 + 3 * MBEDTLS_MPI_MAX_SIZE \
71 + 5 * MBEDTLS_MPI_MAX_SIZE_2)
Neil Armstronge0326a62022-02-25 08:57:19 +010072
73#else /* MBEDTLS_RSA_C */
74
Neil Armstronge9ecd272022-03-01 10:03:21 +010075#define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES 0
76#define MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES 0
Neil Armstronge0326a62022-02-25 08:57:19 +010077
78#endif /* MBEDTLS_RSA_C */
79
Valerio Setti81d75122023-06-14 14:49:33 +020080#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Settia7c9e092023-07-27 14:39:50 +020081
82/* Find the maximum number of bytes necessary to store an EC point. When USE_PSA
83 * is defined this means looking for the maximum between PSA and built-in
84 * supported curves. */
85#if defined(MBEDTLS_USE_PSA_CRYPTO)
86#define MBEDTLS_PK_MAX_ECC_BYTES (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) > \
87 MBEDTLS_ECP_MAX_BYTES ? \
88 PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS) : \
89 MBEDTLS_ECP_MAX_BYTES)
90#else /* MBEDTLS_USE_PSA_CRYPTO */
91#define MBEDTLS_PK_MAX_ECC_BYTES MBEDTLS_ECP_MAX_BYTES
92#endif /* MBEDTLS_USE_PSA_CRYPTO */
93
Neil Armstronge0326a62022-02-25 08:57:19 +010094/*
95 * EC public keys:
96 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
97 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
98 * + 1 + 1 + 7 (ec oid)
99 * + 1 + 1 + 9 (namedCurve oid)
100 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
101 * + 1 (point format) [1]
102 * + 2 * ECP_MAX (coords) [1]
103 * }
104 */
Valerio Settia7c9e092023-07-27 14:39:50 +0200105#define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_PK_MAX_ECC_BYTES)
Neil Armstronge0326a62022-02-25 08:57:19 +0100106
107/*
108 * EC private keys:
109 * ECPrivateKey ::= SEQUENCE { 1 + 2
110 * version INTEGER , 1 + 1 + 1
111 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
112 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
113 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
114 * }
115 */
Valerio Settia7c9e092023-07-27 14:39:50 +0200116#define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES (29 + 3 * MBEDTLS_PK_MAX_ECC_BYTES)
Neil Armstronge0326a62022-02-25 08:57:19 +0100117
Valerio Setti81d75122023-06-14 14:49:33 +0200118#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstronge0326a62022-02-25 08:57:19 +0100119
Neil Armstronge9ecd272022-03-01 10:03:21 +0100120#define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES 0
121#define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES 0
Neil Armstronge0326a62022-02-25 08:57:19 +0100122
Valerio Setti81d75122023-06-14 14:49:33 +0200123#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstronge0326a62022-02-25 08:57:19 +0100124#endif /* MBEDTLS_PK_WRITE_H */