Skip to content

Commit 0ad3904

Browse files
committed
Make crypto structs opaque
It's important to be clear that the contents of the crypto structs are not part of the API, and not guaranteed to be the same between versions. The only guarantee is that the sizes of the structs will not shrink. If the structs grow, this is an ABI break, but not an API break, as the user-allocated object may not be big enough for the new larger struct. A recompile of the application with the new header will fix this. XXX This makes crypto structs partially opaque. I've left in a few things that we could promise will always be present in the structs. If we choose not to always promise these, we can change what's in the crypto_structs.h file.
1 parent 9f49906 commit 0ad3904

File tree

3 files changed

+193
-79
lines changed

3 files changed

+193
-79
lines changed

include/psa/crypto_struct.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* \file psa/crypto_struct.h
3+
*
4+
* \brief PSA cryptography module: Mbed TLS structured type implementations
5+
*
6+
* \note This file may not be included directly. Applications must
7+
* include psa/crypto.h.
8+
*
9+
* This file contains the definitions of some data structures with
10+
* implementation-specific sizes.
11+
*
12+
* In implementations with isolation between the application and the
13+
* cryptography module, it is expected that the front-end and the back-end
14+
* would have different versions of this file.
15+
*/
16+
/*
17+
* Copyright (C) 2018, ARM Limited, All Rights Reserved
18+
* SPDX-License-Identifier: Apache-2.0
19+
*
20+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
21+
* not use this file except in compliance with the License.
22+
* You may obtain a copy of the License at
23+
*
24+
* http://www.apache.org/licenses/LICENSE-2.0
25+
*
26+
* Unless required by applicable law or agreed to in writing, software
27+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
* See the License for the specific language governing permissions and
30+
* limitations under the License.
31+
*
32+
* This file is part of mbed TLS (https://tls.mbed.org)
33+
*/
34+
35+
#ifndef PSA_CRYPTO_STRUCT_H
36+
#define PSA_CRYPTO_STRUCT_H
37+
38+
struct psa_hash_operation_s
39+
{
40+
psa_algorithm_t alg;
41+
uint8_t reserved[217];
42+
};
43+
44+
struct psa_mac_operation_s
45+
{
46+
psa_algorithm_t alg;
47+
uint8_t reserved[353];
48+
};
49+
50+
struct psa_cipher_operation_s
51+
{
52+
psa_algorithm_t alg;
53+
uint8_t reserved[97];
54+
};
55+
56+
struct psa_crypto_generator_s
57+
{
58+
psa_algorithm_t alg;
59+
size_t capacity;
60+
uint8_t reserved[497];
61+
};
62+
63+
#define PSA_CRYPTO_GENERATOR_INIT {0, 0, {0}}
64+
static inline struct psa_crypto_generator_s psa_crypto_generator_init( void )
65+
{
66+
const struct psa_crypto_generator_s v = PSA_CRYPTO_GENERATOR_INIT;
67+
return( v );
68+
}
69+
70+
struct psa_key_policy_s
71+
{
72+
psa_key_usage_t usage;
73+
psa_algorithm_t alg;
74+
};
75+
76+
#endif /* PSA_CRYPTO_STRUCT_H */

library/include/crypto_struct.h renamed to library/include/crypto_struct_impl.h

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/**
2-
* \file crypto_struct.h
2+
* \file crypto_struct_impl.h
33
*
4-
* \brief PSA cryptography module: Mbed TLS structured type implementations
5-
*
6-
* \note This file may not be included directly. Applications must
7-
* include psa/crypto.h.
4+
* \brief Mbed Crypto structured type implementations
85
*
96
* This file contains the definitions of some data structures with
107
* implementation-specific definitions.
118
*
12-
* In implementations with isolation between the application and the
13-
* cryptography module, it is expected that the front-end and the back-end
14-
* would have different versions of this file.
9+
* The definitions in this file provide the implementation-specific detail of
10+
* the structs defined in psa/crypto_struct.h for use by the library itself
11+
* (not users of the library). The implementation-specific detail here is free
12+
* to change between versions of the library, so long as the size of the
13+
* structs never decreases (unless an ABI break is tolerable). The size of the
14+
* structs in psa/crypto_struct.h must be at least as big as those in this file
15+
* in order for users of the library to be able to allocate sufficient memory
16+
* for these structs.
1517
*/
1618
/*
1719
* Copyright (C) 2018, ARM Limited, All Rights Reserved
@@ -32,8 +34,8 @@
3234
* This file is part of mbed TLS (https://tls.mbed.org)
3335
*/
3436

35-
#ifndef PSA_CRYPTO_STRUCT_H
36-
#define PSA_CRYPTO_STRUCT_H
37+
#ifndef MC_CRYPTO_STRUCT_IMPL_H
38+
#define MC_CRYPTO_STRUCT_IMPL_H
3739

3840
/* Include the Mbed TLS configuration file, the way Mbed TLS does it
3941
* in each of its header files. */
@@ -43,6 +45,8 @@
4345
#include MBEDTLS_CONFIG_FILE
4446
#endif
4547

48+
#include "utils.h"
49+
4650
#include "mbedtls/cipher.h"
4751
#include "mbedtls/cmac.h"
4852
#include "mbedtls/gcm.h"
@@ -55,7 +59,7 @@
5559
#include "mbedtls/sha256.h"
5660
#include "mbedtls/sha512.h"
5761

58-
struct psa_hash_operation_s
62+
typedef struct psa_hash_operation_impl_s
5963
{
6064
psa_algorithm_t alg;
6165
union
@@ -83,7 +87,11 @@ struct psa_hash_operation_s
8387
mbedtls_sha512_context sha512;
8488
#endif
8589
} ctx;
86-
};
90+
} psa_hash_operation_impl_t;
91+
92+
STATIC_ASSERT(
93+
sizeof(psa_hash_operation_t) >= sizeof(psa_hash_operation_impl_t),
94+
psa_hash_operation_t_too_small);
8795

8896
#if defined(MBEDTLS_MD_C)
8997
typedef struct
@@ -95,7 +103,7 @@ typedef struct
95103
} psa_hmac_internal_data;
96104
#endif /* MBEDTLS_MD_C */
97105

98-
struct psa_mac_operation_s
106+
typedef struct psa_mac_operation_impl_s
99107
{
100108
psa_algorithm_t alg;
101109
unsigned int key_set : 1;
@@ -114,9 +122,13 @@ struct psa_mac_operation_s
114122
mbedtls_cipher_context_t cmac;
115123
#endif
116124
} ctx;
117-
};
125+
} psa_mac_operation_impl_t;
118126

119-
struct psa_cipher_operation_s
127+
STATIC_ASSERT(
128+
sizeof(psa_mac_operation_t) >= sizeof(psa_mac_operation_impl_t),
129+
psa_mac_operation_t_too_small);
130+
131+
typedef struct psa_cipher_operation_impl_s
120132
{
121133
psa_algorithm_t alg;
122134
unsigned int key_set : 1;
@@ -128,7 +140,11 @@ struct psa_cipher_operation_s
128140
{
129141
mbedtls_cipher_context_t cipher;
130142
} ctx;
131-
};
143+
} psa_cipher_operation_impl_t;
144+
145+
STATIC_ASSERT(
146+
sizeof(psa_cipher_operation_t) >= sizeof(psa_cipher_operation_impl_t),
147+
psa_cipher_operation_t_too_small);
132148

133149
#if defined(MBEDTLS_MD_C)
134150
typedef struct
@@ -177,7 +193,7 @@ typedef struct psa_tls12_prf_generator_s
177193
} psa_tls12_prf_generator_t;
178194
#endif /* MBEDTLS_MD_C */
179195

180-
struct psa_crypto_generator_s
196+
typedef struct psa_crypto_generator_impl_s
181197
{
182198
psa_algorithm_t alg;
183199
size_t capacity;
@@ -193,19 +209,10 @@ struct psa_crypto_generator_s
193209
psa_tls12_prf_generator_t tls12_prf;
194210
#endif
195211
} ctx;
196-
};
212+
} psa_crypto_generator_impl_t;
197213

198-
#define PSA_CRYPTO_GENERATOR_INIT {0, 0, {{0, 0}}}
199-
static inline struct psa_crypto_generator_s psa_crypto_generator_init( void )
200-
{
201-
const struct psa_crypto_generator_s v = PSA_CRYPTO_GENERATOR_INIT;
202-
return( v );
203-
}
204-
205-
struct psa_key_policy_s
206-
{
207-
psa_key_usage_t usage;
208-
psa_algorithm_t alg;
209-
};
214+
STATIC_ASSERT(
215+
sizeof(psa_crypto_generator_t) >= sizeof(psa_crypto_generator_impl_t),
216+
psa_crypto_generator_t_too_small);
210217

211-
#endif /* PSA_CRYPTO_STRUCT_H */
218+
#endif /* MC_CRYPTO_STRUCT_IMPL_H */

0 commit comments

Comments
 (0)