| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifndef PSA_CRYPTO_STRUCT_H |
| 62 |
#define PSA_CRYPTO_STRUCT_H |
| 63 |
|
| 64 |
#ifdef __cplusplus |
| 65 |
extern "C" { |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#if !defined(MBEDTLS_CONFIG_FILE) |
| 71 |
#include "mbedtls/config.h" |
| 72 |
#else |
| 73 |
#include MBEDTLS_CONFIG_FILE |
| 74 |
#endif |
| 75 |
|
| 76 |
#include "mbedtls/cmac.h" |
| 77 |
#include "mbedtls/gcm.h" |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#include "psa/crypto_driver_contexts_primitives.h" |
| 82 |
|
| 83 |
struct psa_hash_operation_s |
| 84 |
{ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
unsigned int id; |
| 92 |
psa_driver_hash_context_t ctx; |
| 93 |
}; |
| 94 |
|
| 95 |
#define PSA_HASH_OPERATION_INIT {0, {0}} |
| 96 |
static inline struct psa_hash_operation_s psa_hash_operation_init( void ) |
| 97 |
{ |
| 98 |
const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT; |
| 99 |
return( v ); |
| 100 |
} |
| 101 |
|
| 102 |
struct psa_cipher_operation_s |
| 103 |
{ |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
unsigned int id; |
| 111 |
|
| 112 |
unsigned int iv_required : 1; |
| 113 |
unsigned int iv_set : 1; |
| 114 |
|
| 115 |
uint8_t default_iv_length; |
| 116 |
|
| 117 |
psa_driver_cipher_context_t ctx; |
| 118 |
}; |
| 119 |
|
| 120 |
#define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, {0}} |
| 121 |
static inline struct psa_cipher_operation_s psa_cipher_operation_init( void ) |
| 122 |
{ |
| 123 |
const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; |
| 124 |
return( v ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#include "psa/crypto_driver_contexts_composites.h" |
| 130 |
|
| 131 |
struct psa_mac_operation_s |
| 132 |
{ |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
unsigned int id; |
| 140 |
uint8_t mac_size; |
| 141 |
unsigned int is_sign : 1; |
| 142 |
psa_driver_mac_context_t ctx; |
| 143 |
}; |
| 144 |
|
| 145 |
#define PSA_MAC_OPERATION_INIT {0, 0, 0, {0}} |
| 146 |
static inline struct psa_mac_operation_s psa_mac_operation_init( void ) |
| 147 |
{ |
| 148 |
const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; |
| 149 |
return( v ); |
| 150 |
} |
| 151 |
|
| 152 |
struct psa_aead_operation_s |
| 153 |
{ |
| 154 |
psa_algorithm_t alg; |
| 155 |
unsigned int key_set : 1; |
| 156 |
unsigned int iv_set : 1; |
| 157 |
uint8_t iv_size; |
| 158 |
uint8_t block_size; |
| 159 |
union |
| 160 |
{ |
| 161 |
unsigned dummy; |
| 162 |
mbedtls_cipher_context_t cipher; |
| 163 |
} ctx; |
| 164 |
}; |
| 165 |
|
| 166 |
#define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}} |
| 167 |
static inline struct psa_aead_operation_s psa_aead_operation_init( void ) |
| 168 |
{ |
| 169 |
const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT; |
| 170 |
return( v ); |
| 171 |
} |
| 172 |
|
| 173 |
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 174 |
typedef struct |
| 175 |
{ |
| 176 |
uint8_t *info; |
| 177 |
size_t info_length; |
| 178 |
#if PSA_HASH_MAX_SIZE > 0xff |
| 179 |
#error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 180 |
#endif |
| 181 |
uint8_t offset_in_block; |
| 182 |
uint8_t block_number; |
| 183 |
unsigned int state : 2; |
| 184 |
unsigned int info_set : 1; |
| 185 |
uint8_t output_block[PSA_HASH_MAX_SIZE]; |
| 186 |
uint8_t prk[PSA_HASH_MAX_SIZE]; |
| 187 |
struct psa_mac_operation_s hmac; |
| 188 |
} psa_hkdf_key_derivation_t; |
| 189 |
#endif |
| 190 |
|
| 191 |
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 192 |
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 193 |
typedef enum |
| 194 |
{ |
| 195 |
PSA_TLS12_PRF_STATE_INIT, |
| 196 |
PSA_TLS12_PRF_STATE_SEED_SET, |
| 197 |
PSA_TLS12_PRF_STATE_KEY_SET, |
| 198 |
PSA_TLS12_PRF_STATE_LABEL_SET, |
| 199 |
PSA_TLS12_PRF_STATE_OUTPUT |
| 200 |
} psa_tls12_prf_key_derivation_state_t; |
| 201 |
|
| 202 |
typedef struct psa_tls12_prf_key_derivation_s |
| 203 |
{ |
| 204 |
#if PSA_HASH_MAX_SIZE > 0xff |
| 205 |
#error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 206 |
#endif |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
uint8_t left_in_block; |
| 211 |
|
| 212 |
|
| 213 |
uint8_t block_number; |
| 214 |
|
| 215 |
psa_tls12_prf_key_derivation_state_t state; |
| 216 |
|
| 217 |
uint8_t *secret; |
| 218 |
size_t secret_length; |
| 219 |
uint8_t *seed; |
| 220 |
size_t seed_length; |
| 221 |
uint8_t *label; |
| 222 |
size_t label_length; |
| 223 |
|
| 224 |
uint8_t Ai[PSA_HASH_MAX_SIZE]; |
| 225 |
|
| 226 |
|
| 227 |
uint8_t output_block[PSA_HASH_MAX_SIZE]; |
| 228 |
} psa_tls12_prf_key_derivation_t; |
| 229 |
#endif |
| 230 |
|
| 231 |
|
| 232 |
struct psa_key_derivation_s |
| 233 |
{ |
| 234 |
psa_algorithm_t alg; |
| 235 |
unsigned int can_output_key : 1; |
| 236 |
size_t capacity; |
| 237 |
union |
| 238 |
{ |
| 239 |
|
| 240 |
uint8_t dummy; |
| 241 |
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 242 |
psa_hkdf_key_derivation_t hkdf; |
| 243 |
#endif |
| 244 |
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 245 |
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 246 |
psa_tls12_prf_key_derivation_t tls12_prf; |
| 247 |
#endif |
| 248 |
} ctx; |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
#define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, 0, {0}} |
| 253 |
static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void ) |
| 254 |
{ |
| 255 |
const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 256 |
return( v ); |
| 257 |
} |
| 258 |
|
| 259 |
struct psa_key_policy_s |
| 260 |
{ |
| 261 |
psa_key_usage_t usage; |
| 262 |
psa_algorithm_t alg; |
| 263 |
psa_algorithm_t alg2; |
| 264 |
}; |
| 265 |
typedef struct psa_key_policy_s psa_key_policy_t; |
| 266 |
|
| 267 |
#define PSA_KEY_POLICY_INIT {0, 0, 0} |
| 268 |
static inline struct psa_key_policy_s psa_key_policy_init( void ) |
| 269 |
{ |
| 270 |
const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; |
| 271 |
return( v ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
typedef uint16_t psa_key_bits_t; |
| 277 |
|
| 278 |
|
| 279 |
#define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) ) |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
#define PSA_MAX_KEY_BITS 0xfff8 |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
typedef uint16_t psa_key_attributes_flag_t; |
| 295 |
|
| 296 |
#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \ |
| 297 |
( (psa_key_attributes_flag_t) 0x0001 ) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \ |
| 302 |
MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \ |
| 303 |
0 ) |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \ |
| 308 |
0 ) |
| 309 |
|
| 310 |
typedef struct |
| 311 |
{ |
| 312 |
psa_key_type_t type; |
| 313 |
psa_key_bits_t bits; |
| 314 |
psa_key_lifetime_t lifetime; |
| 315 |
mbedtls_svc_key_id_t id; |
| 316 |
psa_key_policy_t policy; |
| 317 |
psa_key_attributes_flag_t flags; |
| 318 |
} psa_core_key_attributes_t; |
| 319 |
|
| 320 |
#define PSA_CORE_KEY_ATTRIBUTES_INIT {PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0} |
| 321 |
|
| 322 |
struct psa_key_attributes_s |
| 323 |
{ |
| 324 |
psa_core_key_attributes_t core; |
| 325 |
#if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 326 |
psa_key_slot_number_t slot_number; |
| 327 |
#endif |
| 328 |
void *domain_parameters; |
| 329 |
size_t domain_parameters_size; |
| 330 |
}; |
| 331 |
|
| 332 |
#if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 333 |
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0} |
| 334 |
#else |
| 335 |
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0} |
| 336 |
#endif |
| 337 |
|
| 338 |
static inline struct psa_key_attributes_s psa_key_attributes_init( void ) |
| 339 |
{ |
| 340 |
const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
| 341 |
return( v ); |
| 342 |
} |
| 343 |
|
| 344 |
static inline void psa_set_key_id( psa_key_attributes_t *attributes, |
| 345 |
mbedtls_svc_key_id_t key ) |
| 346 |
{ |
| 347 |
psa_key_lifetime_t lifetime = attributes->core.lifetime; |
| 348 |
|
| 349 |
attributes->core.id = key; |
| 350 |
|
| 351 |
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 352 |
{ |
| 353 |
attributes->core.lifetime = |
| 354 |
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 355 |
PSA_KEY_LIFETIME_PERSISTENT, |
| 356 |
PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) ); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
static inline mbedtls_svc_key_id_t psa_get_key_id( |
| 361 |
const psa_key_attributes_t *attributes) |
| 362 |
{ |
| 363 |
return( attributes->core.id ); |
| 364 |
} |
| 365 |
|
| 366 |
#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 367 |
static inline void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes, |
| 368 |
mbedtls_key_owner_id_t owner ) |
| 369 |
{ |
| 370 |
attributes->core.id.owner = owner; |
| 371 |
} |
| 372 |
#endif |
| 373 |
|
| 374 |
static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 375 |
psa_key_lifetime_t lifetime) |
| 376 |
{ |
| 377 |
attributes->core.lifetime = lifetime; |
| 378 |
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 379 |
{ |
| 380 |
#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 381 |
attributes->core.id.key_id = 0; |
| 382 |
#else |
| 383 |
attributes->core.id = 0; |
| 384 |
#endif |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
static inline psa_key_lifetime_t psa_get_key_lifetime( |
| 389 |
const psa_key_attributes_t *attributes) |
| 390 |
{ |
| 391 |
return( attributes->core.lifetime ); |
| 392 |
} |
| 393 |
|
| 394 |
static inline void psa_extend_key_usage_flags( psa_key_usage_t *usage_flags ) |
| 395 |
{ |
| 396 |
if( *usage_flags & PSA_KEY_USAGE_SIGN_HASH ) |
| 397 |
*usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE; |
| 398 |
|
| 399 |
if( *usage_flags & PSA_KEY_USAGE_VERIFY_HASH ) |
| 400 |
*usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
| 401 |
} |
| 402 |
|
| 403 |
static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 404 |
psa_key_usage_t usage_flags) |
| 405 |
{ |
| 406 |
psa_extend_key_usage_flags( &usage_flags ); |
| 407 |
attributes->core.policy.usage = usage_flags; |
| 408 |
} |
| 409 |
|
| 410 |
static inline psa_key_usage_t psa_get_key_usage_flags( |
| 411 |
const psa_key_attributes_t *attributes) |
| 412 |
{ |
| 413 |
return( attributes->core.policy.usage ); |
| 414 |
} |
| 415 |
|
| 416 |
static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 417 |
psa_algorithm_t alg) |
| 418 |
{ |
| 419 |
attributes->core.policy.alg = alg; |
| 420 |
} |
| 421 |
|
| 422 |
static inline psa_algorithm_t psa_get_key_algorithm( |
| 423 |
const psa_key_attributes_t *attributes) |
| 424 |
{ |
| 425 |
return( attributes->core.policy.alg ); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
| 431 |
psa_key_type_t type, |
| 432 |
const uint8_t *data, |
| 433 |
size_t data_length); |
| 434 |
|
| 435 |
static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 436 |
psa_key_type_t type) |
| 437 |
{ |
| 438 |
if( attributes->domain_parameters == NULL ) |
| 439 |
{ |
| 440 |
|
| 441 |
attributes->core.type = type; |
| 442 |
} |
| 443 |
else |
| 444 |
{ |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
(void) psa_set_key_domain_parameters( attributes, type, NULL, 0 ); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
static inline psa_key_type_t psa_get_key_type( |
| 454 |
const psa_key_attributes_t *attributes) |
| 455 |
{ |
| 456 |
return( attributes->core.type ); |
| 457 |
} |
| 458 |
|
| 459 |
static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 460 |
size_t bits) |
| 461 |
{ |
| 462 |
if( bits > PSA_MAX_KEY_BITS ) |
| 463 |
attributes->core.bits = PSA_KEY_BITS_TOO_LARGE; |
| 464 |
else |
| 465 |
attributes->core.bits = (psa_key_bits_t) bits; |
| 466 |
} |
| 467 |
|
| 468 |
static inline size_t psa_get_key_bits( |
| 469 |
const psa_key_attributes_t *attributes) |
| 470 |
{ |
| 471 |
return( attributes->core.bits ); |
| 472 |
} |
| 473 |
|
| 474 |
#ifdef __cplusplus |
| 475 |
} |
| 476 |
#endif |
| 477 |
|
| 478 |
#endif |