1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Context structure declaration of the Mbed TLS software-based PSA drivers
3a8e1175bSopenharmony_ci *  called through the PSA Crypto driver dispatch layer.
4a8e1175bSopenharmony_ci *  This file contains the context structures of key derivation algorithms
5a8e1175bSopenharmony_ci *  which need to rely on other algorithms.
6a8e1175bSopenharmony_ci *
7a8e1175bSopenharmony_ci * \note This file may not be included directly. Applications must
8a8e1175bSopenharmony_ci * include psa/crypto.h.
9a8e1175bSopenharmony_ci *
10a8e1175bSopenharmony_ci * \note This header and its content are not part of the Mbed TLS API and
11a8e1175bSopenharmony_ci * applications must not depend on it. Its main purpose is to define the
12a8e1175bSopenharmony_ci * multi-part state objects of the Mbed TLS software-based PSA drivers. The
13a8e1175bSopenharmony_ci * definitions of these objects are then used by crypto_struct.h to define the
14a8e1175bSopenharmony_ci * implementation-defined types of PSA multi-part state objects.
15a8e1175bSopenharmony_ci */
16a8e1175bSopenharmony_ci/*
17a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
18a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
19a8e1175bSopenharmony_ci */
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci#ifndef PSA_CRYPTO_BUILTIN_KEY_DERIVATION_H
22a8e1175bSopenharmony_ci#define PSA_CRYPTO_BUILTIN_KEY_DERIVATION_H
23a8e1175bSopenharmony_ci#include "mbedtls/private_access.h"
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci#include <psa/crypto_driver_common.h>
26a8e1175bSopenharmony_ci
27a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
28a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
29a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
30a8e1175bSopenharmony_citypedef struct {
31a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(info);
32a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(info_length);
33a8e1175bSopenharmony_ci#if PSA_HASH_MAX_SIZE > 0xff
34a8e1175bSopenharmony_ci#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
35a8e1175bSopenharmony_ci#endif
36a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(offset_in_block);
37a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(block_number);
38a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(state) : 2;
39a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(info_set) : 1;
40a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
41a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(prk)[PSA_HASH_MAX_SIZE];
42a8e1175bSopenharmony_ci    struct psa_mac_operation_s MBEDTLS_PRIVATE(hmac);
43a8e1175bSopenharmony_ci} psa_hkdf_key_derivation_t;
44a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF ||
45a8e1175bSopenharmony_ci          MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
46a8e1175bSopenharmony_ci          MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
47a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
48a8e1175bSopenharmony_citypedef struct {
49a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(data)[PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE];
50a8e1175bSopenharmony_ci} psa_tls12_ecjpake_to_pms_t;
51a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
54a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
55a8e1175bSopenharmony_citypedef enum {
56a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_INIT,             /* no input provided */
57a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_SEED_SET,         /* seed has been set */
58a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_OTHER_KEY_SET,    /* other key has been set - optional */
59a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_KEY_SET,          /* key has been set */
60a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_LABEL_SET,        /* label has been set */
61a8e1175bSopenharmony_ci    PSA_TLS12_PRF_STATE_OUTPUT            /* output has been started */
62a8e1175bSopenharmony_ci} psa_tls12_prf_key_derivation_state_t;
63a8e1175bSopenharmony_ci
64a8e1175bSopenharmony_citypedef struct psa_tls12_prf_key_derivation_s {
65a8e1175bSopenharmony_ci#if PSA_HASH_MAX_SIZE > 0xff
66a8e1175bSopenharmony_ci#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
67a8e1175bSopenharmony_ci#endif
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci    /* Indicates how many bytes in the current HMAC block have
70a8e1175bSopenharmony_ci     * not yet been read by the user. */
71a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(left_in_block);
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci    /* The 1-based number of the block. */
74a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(block_number);
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci    psa_tls12_prf_key_derivation_state_t MBEDTLS_PRIVATE(state);
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(secret);
79a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(secret_length);
80a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(seed);
81a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(seed_length);
82a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(label);
83a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(label_length);
84a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
85a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(other_secret);
86a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(other_secret_length);
87a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(Ai)[PSA_HASH_MAX_SIZE];
90a8e1175bSopenharmony_ci
91a8e1175bSopenharmony_ci    /* `HMAC_hash( prk, A( i ) + seed )` in the notation of RFC 5246, Sect. 5. */
92a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
93a8e1175bSopenharmony_ci} psa_tls12_prf_key_derivation_t;
94a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
95a8e1175bSopenharmony_ci        * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
96a8e1175bSopenharmony_ci#if defined(PSA_HAVE_SOFT_PBKDF2)
97a8e1175bSopenharmony_citypedef enum {
98a8e1175bSopenharmony_ci    PSA_PBKDF2_STATE_INIT,             /* no input provided */
99a8e1175bSopenharmony_ci    PSA_PBKDF2_STATE_INPUT_COST_SET,   /* input cost has been set */
100a8e1175bSopenharmony_ci    PSA_PBKDF2_STATE_SALT_SET,         /* salt has been set */
101a8e1175bSopenharmony_ci    PSA_PBKDF2_STATE_PASSWORD_SET,     /* password has been set */
102a8e1175bSopenharmony_ci    PSA_PBKDF2_STATE_OUTPUT            /* output has been started */
103a8e1175bSopenharmony_ci} psa_pbkdf2_key_derivation_state_t;
104a8e1175bSopenharmony_ci
105a8e1175bSopenharmony_citypedef struct {
106a8e1175bSopenharmony_ci    psa_pbkdf2_key_derivation_state_t MBEDTLS_PRIVATE(state);
107a8e1175bSopenharmony_ci    uint64_t MBEDTLS_PRIVATE(input_cost);
108a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(salt);
109a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(salt_length);
110a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(password)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
111a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(password_length);
112a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
113a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(bytes_used);
114a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(block_number);
115a8e1175bSopenharmony_ci} psa_pbkdf2_key_derivation_t;
116a8e1175bSopenharmony_ci#endif /* PSA_HAVE_SOFT_PBKDF2 */
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_BUILTIN_KEY_DERIVATION_H */
119