1 /**
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  */
47 #define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES    (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
48 
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  */
64 #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)
68 
69 #else /* MBEDTLS_RSA_C */
70 
71 #define MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES   0
72 #define MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES   0
73 
74 #endif /* MBEDTLS_RSA_C */
75 
76 #if defined(MBEDTLS_ECP_C)
77 /*
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  */
88 #define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES    (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
89 
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  */
99 #define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES    (29 + 3 * MBEDTLS_ECP_MAX_BYTES)
100 
101 #else /* MBEDTLS_ECP_C */
102 
103 #define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES   0
104 #define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES   0
105 
106 #endif /* MBEDTLS_ECP_C */
107 
108 #endif /* MBEDTLS_PK_WRITE_H */
109