| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file psa/crypto_struct.h | 
|  | 3 | * | 
|  | 4 | * \brief PSA cryptography module: Mbed TLS structured type implementations | 
| Gilles Peskine | 07c91f5 | 2018-06-28 18:02:53 +0200 | [diff] [blame] | 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 definitions. | 
|  | 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. | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 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 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it | 
|  | 39 | * in each of its header files. */ | 
|  | 40 | #if !defined(MBEDTLS_CONFIG_FILE) | 
|  | 41 | #include "../mbedtls/config.h" | 
|  | 42 | #else | 
|  | 43 | #include MBEDTLS_CONFIG_FILE | 
|  | 44 | #endif | 
|  | 45 |  | 
|  | 46 | #include "mbedtls/cipher.h" | 
|  | 47 | #include "mbedtls/cmac.h" | 
|  | 48 | #include "mbedtls/gcm.h" | 
|  | 49 | #include "mbedtls/md.h" | 
|  | 50 | #include "mbedtls/md2.h" | 
|  | 51 | #include "mbedtls/md4.h" | 
|  | 52 | #include "mbedtls/md5.h" | 
|  | 53 | #include "mbedtls/ripemd160.h" | 
|  | 54 | #include "mbedtls/sha1.h" | 
|  | 55 | #include "mbedtls/sha256.h" | 
|  | 56 | #include "mbedtls/sha512.h" | 
|  | 57 |  | 
|  | 58 | struct psa_hash_operation_s | 
|  | 59 | { | 
|  | 60 | psa_algorithm_t alg; | 
|  | 61 | union | 
|  | 62 | { | 
| Gilles Peskine | 058e0b9 | 2018-03-22 16:20:19 +0100 | [diff] [blame] | 63 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 64 | #if defined(MBEDTLS_MD2_C) | 
|  | 65 | mbedtls_md2_context md2; | 
|  | 66 | #endif | 
|  | 67 | #if defined(MBEDTLS_MD4_C) | 
|  | 68 | mbedtls_md4_context md4; | 
|  | 69 | #endif | 
|  | 70 | #if defined(MBEDTLS_MD5_C) | 
|  | 71 | mbedtls_md5_context md5; | 
|  | 72 | #endif | 
|  | 73 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 74 | mbedtls_ripemd160_context ripemd160; | 
|  | 75 | #endif | 
|  | 76 | #if defined(MBEDTLS_SHA1_C) | 
|  | 77 | mbedtls_sha1_context sha1; | 
|  | 78 | #endif | 
|  | 79 | #if defined(MBEDTLS_SHA256_C) | 
|  | 80 | mbedtls_sha256_context sha256; | 
|  | 81 | #endif | 
|  | 82 | #if defined(MBEDTLS_SHA512_C) | 
|  | 83 | mbedtls_sha512_context sha512; | 
|  | 84 | #endif | 
|  | 85 | } ctx; | 
|  | 86 | }; | 
|  | 87 |  | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 88 | #define PSA_HASH_OPERATION_INIT {0, {0}} | 
|  | 89 | static inline struct psa_hash_operation_s psa_hash_operation_init( void ) | 
|  | 90 | { | 
|  | 91 | const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT; | 
|  | 92 | return( v ); | 
|  | 93 | } | 
|  | 94 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 95 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 96 | typedef struct | 
|  | 97 | { | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 98 | /** The hash context. */ | 
|  | 99 | struct psa_hash_operation_s hash_ctx; | 
|  | 100 | /** The HMAC part of the context. */ | 
| Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 101 | uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 102 | } psa_hmac_internal_data; | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 103 | #endif /* MBEDTLS_MD_C */ | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 104 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 105 | struct psa_mac_operation_s | 
|  | 106 | { | 
|  | 107 | psa_algorithm_t alg; | 
| Darryl Green | 80bed23 | 2018-07-26 13:03:38 +0100 | [diff] [blame] | 108 | unsigned int key_set : 1; | 
|  | 109 | unsigned int iv_required : 1; | 
|  | 110 | unsigned int iv_set : 1; | 
|  | 111 | unsigned int has_input : 1; | 
|  | 112 | unsigned int is_sign : 1; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 113 | uint8_t mac_size; | 
|  | 114 | union | 
|  | 115 | { | 
| Gilles Peskine | 058e0b9 | 2018-03-22 16:20:19 +0100 | [diff] [blame] | 116 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_MD_C) | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 118 | psa_hmac_internal_data hmac; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 119 | #endif | 
|  | 120 | #if defined(MBEDTLS_CMAC_C) | 
|  | 121 | mbedtls_cipher_context_t cmac; | 
|  | 122 | #endif | 
|  | 123 | } ctx; | 
|  | 124 | }; | 
|  | 125 |  | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 126 | #define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}} | 
|  | 127 | static inline struct psa_mac_operation_s psa_mac_operation_init( void ) | 
|  | 128 | { | 
|  | 129 | const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; | 
|  | 130 | return( v ); | 
|  | 131 | } | 
|  | 132 |  | 
| Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 133 | struct psa_cipher_operation_s | 
|  | 134 | { | 
|  | 135 | psa_algorithm_t alg; | 
| Darryl Green | 80bed23 | 2018-07-26 13:03:38 +0100 | [diff] [blame] | 136 | unsigned int key_set : 1; | 
|  | 137 | unsigned int iv_required : 1; | 
|  | 138 | unsigned int iv_set : 1; | 
| Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 139 | uint8_t iv_size; | 
|  | 140 | uint8_t block_size; | 
|  | 141 | union | 
|  | 142 | { | 
| Jaeden Amero | 5a5dc77 | 2019-01-04 15:33:37 +0000 | [diff] [blame] | 143 | unsigned dummy; /* Enable easier initializing of the union. */ | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 144 | mbedtls_cipher_context_t cipher; | 
| Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 145 | } ctx; | 
|  | 146 | }; | 
|  | 147 |  | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 148 | #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}} | 
|  | 149 | static inline struct psa_cipher_operation_s psa_cipher_operation_init( void ) | 
|  | 150 | { | 
|  | 151 | const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; | 
|  | 152 | return( v ); | 
|  | 153 | } | 
|  | 154 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 155 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 156 | typedef struct | 
|  | 157 | { | 
|  | 158 | uint8_t *info; | 
|  | 159 | size_t info_length; | 
|  | 160 | psa_hmac_internal_data hmac; | 
|  | 161 | uint8_t prk[PSA_HASH_MAX_SIZE]; | 
|  | 162 | uint8_t output_block[PSA_HASH_MAX_SIZE]; | 
|  | 163 | #if PSA_HASH_MAX_SIZE > 0xff | 
|  | 164 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" | 
|  | 165 | #endif | 
|  | 166 | uint8_t offset_in_block; | 
|  | 167 | uint8_t block_number; | 
|  | 168 | } psa_hkdf_generator_t; | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 169 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 170 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 171 | #if defined(MBEDTLS_MD_C) | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 172 | typedef struct psa_tls12_prf_generator_s | 
|  | 173 | { | 
|  | 174 | /* The TLS 1.2 PRF uses the key for each HMAC iteration, | 
|  | 175 | * hence we must store it for the lifetime of the generator. | 
|  | 176 | * This is different from HKDF, where the key is only used | 
|  | 177 | * in the extraction phase, but not during expansion. */ | 
|  | 178 | unsigned char *key; | 
|  | 179 | size_t key_len; | 
|  | 180 |  | 
|  | 181 | /* `A(i) + seed` in the notation of RFC 5246, Sect. 5 */ | 
| Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 182 | uint8_t *Ai_with_seed; | 
|  | 183 | size_t Ai_with_seed_len; | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 184 |  | 
|  | 185 | /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */ | 
|  | 186 | uint8_t output_block[PSA_HASH_MAX_SIZE]; | 
|  | 187 |  | 
|  | 188 | #if PSA_HASH_MAX_SIZE > 0xff | 
|  | 189 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" | 
|  | 190 | #endif | 
|  | 191 |  | 
|  | 192 | /* Indicates how many bytes in the current HMAC block have | 
|  | 193 | * already been read by the user. */ | 
|  | 194 | uint8_t offset_in_block; | 
|  | 195 |  | 
|  | 196 | /* The 1-based number of the block. */ | 
|  | 197 | uint8_t block_number; | 
|  | 198 |  | 
|  | 199 | } psa_tls12_prf_generator_t; | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 200 | #endif /* MBEDTLS_MD_C */ | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 201 |  | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 202 | struct psa_crypto_generator_s | 
|  | 203 | { | 
|  | 204 | psa_algorithm_t alg; | 
|  | 205 | size_t capacity; | 
|  | 206 | union | 
|  | 207 | { | 
|  | 208 | struct | 
|  | 209 | { | 
|  | 210 | uint8_t *data; | 
|  | 211 | size_t size; | 
|  | 212 | } buffer; | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 213 | #if defined(MBEDTLS_MD_C) | 
|  | 214 | psa_hkdf_generator_t hkdf; | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 215 | psa_tls12_prf_generator_t tls12_prf; | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 216 | #endif | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 217 | } ctx; | 
|  | 218 | }; | 
|  | 219 |  | 
|  | 220 | #define PSA_CRYPTO_GENERATOR_INIT {0, 0, {{0, 0}}} | 
|  | 221 | static inline struct psa_crypto_generator_s psa_crypto_generator_init( void ) | 
|  | 222 | { | 
|  | 223 | const struct psa_crypto_generator_s v = PSA_CRYPTO_GENERATOR_INIT; | 
|  | 224 | return( v ); | 
|  | 225 | } | 
|  | 226 |  | 
| Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 227 | struct psa_key_policy_s | 
|  | 228 | { | 
|  | 229 | psa_key_usage_t usage; | 
|  | 230 | psa_algorithm_t alg; | 
|  | 231 | }; | 
|  | 232 |  | 
| Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 233 | #define PSA_KEY_POLICY_INIT {0, 0} | 
|  | 234 | static inline struct psa_key_policy_s psa_key_policy_init( void ) | 
|  | 235 | { | 
|  | 236 | const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; | 
|  | 237 | return( v ); | 
|  | 238 | } | 
|  | 239 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 240 | #endif /* PSA_CRYPTO_STRUCT_H */ |