blob: 67e5444fc549751fa89ae17bbbab57aa8ee40fb2 [file] [log] [blame]
Przemek Stekiel359f4622022-12-05 14:11:55 +01001/*
2 * PSA FFDH layer on top of Mbed TLS crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_FFDH_H
22#define PSA_CRYPTO_FFDH_H
23
24#include <psa/crypto.h>
25#include <mbedtls/dhm.h>
26
27/** Perform a key agreement and return the FFDH shared secret.
28 *
29 * \param[in] attributes The attributes of the key to use for the
30 * operation.
31 * \param[in] peer_key The buffer containing the key context
32 * of the peer's public key.
33 * \param[in] peer_key_length Size of the \p peer_key buffer in
34 * bytes.
35 * \param[in] key_buffer The buffer containing the private key
36 * context.
37 * \param[in] key_buffer_size Size of the \p key_buffer buffer in
38 * bytes.
39 * \param[out] shared_secret The buffer to which the shared secret
40 * is to be written.
41 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
42 * bytes.
43 * \param[out] shared_secret_length On success, the number of bytes that make
44 * up the returned shared secret.
45 * \retval #PSA_SUCCESS
46 * Success. Shared secret successfully calculated.
47 * \retval #PSA_ERROR_INVALID_ARGUMENT
48 * \p key_buffer_size, \p peer_key_length, \p shared_secret_size
49 * do not match
Paul Elliott24f4b732023-06-20 15:51:46 +010050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
51 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Przemek Stekiel359f4622022-12-05 14:11:55 +010052 */
Przemek Stekiel152bb462023-06-01 11:52:39 +020053psa_status_t mbedtls_psa_ffdh_key_agreement(
Przemek Stekiel359f4622022-12-05 14:11:55 +010054 const psa_key_attributes_t *attributes,
55 const uint8_t *peer_key,
56 size_t peer_key_length,
57 const uint8_t *key_buffer,
58 size_t key_buffer_size,
59 uint8_t *shared_secret,
60 size_t shared_secret_size,
61 size_t *shared_secret_length);
62
Przemek Stekiel6d85afa2023-04-28 11:42:17 +020063/** Export a public key or the public part of a DH key pair in binary format.
Przemek Stekiel359f4622022-12-05 14:11:55 +010064 *
65 * \param[in] attributes The attributes for the key to export.
66 * \param[in] key_buffer Material or context of the key to export.
67 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
68 * \param[out] data Buffer where the key data is to be written.
69 * \param[in] data_size Size of the \p data buffer in bytes.
70 * \param[out] data_length On success, the number of bytes written in
71 * \p data
72 *
73 * \retval #PSA_SUCCESS The public key was exported successfully.
74 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
75 * The size of \p key_buffer is too small.
Paul Elliott24f4b732023-06-20 15:51:46 +010076 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
77 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
78 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Przemek Stekiel359f4622022-12-05 14:11:55 +010079 */
Przemek Stekiel152bb462023-06-01 11:52:39 +020080psa_status_t mbedtls_psa_ffdh_export_public_key(
Przemek Stekiel359f4622022-12-05 14:11:55 +010081 const psa_key_attributes_t *attributes,
82 const uint8_t *key_buffer,
83 size_t key_buffer_size,
84 uint8_t *data,
85 size_t data_size,
86 size_t *data_length);
87
88/**
Przemek Stekiel6d85afa2023-04-28 11:42:17 +020089 * \brief Generate DH key.
Przemek Stekiel359f4622022-12-05 14:11:55 +010090 *
91 * \note The signature of the function is that of a PSA driver generate_key
92 * entry point.
93 *
94 * \param[in] attributes The attributes for the key to generate.
95 * \param[out] key_buffer Buffer where the key data is to be written.
96 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
97 * \param[out] key_buffer_length On success, the number of bytes written in
98 * \p key_buffer.
99 *
100 * \retval #PSA_SUCCESS
101 * The key was generated successfully.
102 * \retval #PSA_ERROR_NOT_SUPPORTED
103 * Key size in bits is invalid.
104 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
105 * The size of \p key_buffer is too small.
Paul Elliott24f4b732023-06-20 15:51:46 +0100106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
107 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Przemek Stekiel359f4622022-12-05 14:11:55 +0100108 */
109psa_status_t mbedtls_psa_ffdh_generate_key(
110 const psa_key_attributes_t *attributes,
111 uint8_t *key_buffer,
112 size_t key_buffer_size,
113 size_t *key_buffer_length);
114
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200115/**
116 * \brief Import DH key.
117 *
118 * \note The signature of the function is that of a PSA driver import_key
119 * entry point.
120 *
121 * \param[in] attributes The attributes for the key to import.
122 * \param[in] data The buffer containing the key data in import
123 * format.
124 * \param[in] data_length Size of the \p data buffer in bytes.
125 * \param[out] key_buffer The buffer containing the key data in output
126 * format.
127 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
128 * size is greater or equal to \p data_length.
129 * \param[out] key_buffer_length The length of the data written in \p
130 * key_buffer in bytes.
131 * \param[out] bits The key size in number of bits.
132 *
133 * \retval #PSA_SUCCESS
134 * The key was generated successfully.
135 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
136 * The size of \p key_buffer is too small.
137 */
138psa_status_t mbedtls_psa_ffdh_import_key(
139 const psa_key_attributes_t *attributes,
140 const uint8_t *data, size_t data_length,
141 uint8_t *key_buffer, size_t key_buffer_size,
142 size_t *key_buffer_length, size_t *bits);
143
Przemek Stekiel359f4622022-12-05 14:11:55 +0100144#endif /* PSA_CRYPTO_FFDH_H */