mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-28 08:37:25 +00:00
Merge remote-tracking branch 'development/development' into development-restricted
Signed-off-by: Paul Elliott <paul.elliott@arm.com>
This commit is contained in:
commit
e4622a3436
56
.travis.yml
56
.travis.yml
@ -52,8 +52,8 @@ jobs:
|
||||
- programs/test/selftest
|
||||
- tests/scripts/test_psa_constant_names.py
|
||||
- tests/ssl-opt.sh
|
||||
# Modern OpenSSL does not support fixed ECDH or null ciphers.
|
||||
- tests/compat.sh -p OpenSSL -e 'NULL\|ECDH_'
|
||||
# Modern OpenSSL does not support null ciphers.
|
||||
- tests/compat.sh -p OpenSSL -e 'NULL'
|
||||
- tests/scripts/travis-log-failure.sh
|
||||
# GnuTLS supports CAMELLIA but compat.sh doesn't properly enable it.
|
||||
- tests/compat.sh -p GnuTLS -e 'CAMELLIA'
|
||||
@ -80,6 +80,58 @@ jobs:
|
||||
- sleep 5
|
||||
- scripts/windows_msbuild.bat v141 # Visual Studio 2017
|
||||
|
||||
- name: full configuration on arm64
|
||||
os: linux
|
||||
dist: focal
|
||||
arch: arm64
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- gcc
|
||||
script:
|
||||
# Do a manual build+test sequence rather than using all.sh, because
|
||||
# there's no all.sh component that does what we want. We should set
|
||||
# CFLAGS for arm64 host CC.
|
||||
- scripts/config.py full
|
||||
- scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
|
||||
- scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY
|
||||
- scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
|
||||
- scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
|
||||
- make generated_files
|
||||
- make CFLAGS='-march=armv8-a+crypto -O3 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all' LDFLAGS='-Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
|
||||
- make test
|
||||
- programs/test/selftest
|
||||
- tests/scripts/test_psa_constant_names.py
|
||||
# Modern OpenSSL does not support fixed ECDH or null ciphers.
|
||||
- tests/compat.sh -p OpenSSL -e 'NULL\|ECDH_'
|
||||
- tests/scripts/travis-log-failure.sh
|
||||
- tests/context-info.sh
|
||||
|
||||
- name: full configuration(GnuTLS compat tests) on arm64
|
||||
os: linux
|
||||
dist: focal
|
||||
arch: arm64
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- clang
|
||||
- gnutls-bin
|
||||
script:
|
||||
# Do a manual build+test sequence rather than using all.sh, because
|
||||
# there's no all.sh component that does what we want. We should set
|
||||
# CFLAGS for arm64 host CC.
|
||||
- scripts/config.py full
|
||||
- scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
|
||||
- scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY
|
||||
- scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
|
||||
- scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
|
||||
- make generated_files
|
||||
- make CC=clang CFLAGS='-march=armv8-a+crypto -O3 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all' LDFLAGS='-Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
|
||||
# GnuTLS supports CAMELLIA but compat.sh doesn't properly enable it.
|
||||
- tests/compat.sh -p GnuTLS -e 'CAMELLIA'
|
||||
- tests/scripts/travis-log-failure.sh
|
||||
- tests/context-info.sh
|
||||
|
||||
after_failure:
|
||||
- tests/scripts/travis-log-failure.sh
|
||||
|
||||
|
29
ChangeLog
29
ChangeLog
@ -106,11 +106,11 @@ Security
|
||||
* Fix potential heap buffer overread and overwrite in DTLS if
|
||||
MBEDTLS_SSL_DTLS_CONNECTION_ID is enabled and
|
||||
MBEDTLS_SSL_CID_IN_LEN_MAX > 2 * MBEDTLS_SSL_CID_OUT_LEN_MAX.
|
||||
* An adversary with access to precise enough information about memory
|
||||
accesses (typically, an untrusted operating system attacking a secure
|
||||
enclave) could recover an RSA private key after observing the victim
|
||||
performing a single private-key operation if the window size used for the
|
||||
exponentiation was 3 or smaller. Found and reported by Zili KOU,
|
||||
* Fix an issue where an adversary with access to precise enough information
|
||||
about memory accesses (typically, an untrusted operating system attacking
|
||||
a secure enclave) could recover an RSA private key after observing the
|
||||
victim performing a single private-key operation if the window size used
|
||||
for the exponentiation was 3 or smaller. Found and reported by Zili KOU,
|
||||
Wenjian HE, Sharad Sinha, and Wei ZHANG. See "Cache Side-channel Attacks
|
||||
and Defenses of the Sliding Window Algorithm in TEEs" - Design, Automation
|
||||
and Test in Europe 2023.
|
||||
@ -969,16 +969,17 @@ Security
|
||||
signature, allowing the recovery of the private key after observing a
|
||||
large number of signature operations. This completes a partial fix in
|
||||
Mbed TLS 2.20.0.
|
||||
* An adversary with access to precise enough information about memory
|
||||
accesses (typically, an untrusted operating system attacking a secure
|
||||
enclave) could recover an RSA private key after observing the victim
|
||||
performing a single private-key operation. Found and reported by
|
||||
* Fix an issue where an adversary with access to precise enough information
|
||||
about memory accesses (typically, an untrusted operating system attacking
|
||||
a secure enclave) could recover an RSA private key after observing the
|
||||
victim performing a single private-key operation. Found and reported by
|
||||
Zili KOU, Wenjian HE, Sharad Sinha, and Wei ZHANG.
|
||||
* An adversary with access to precise enough timing information (typically, a
|
||||
co-located process) could recover a Curve25519 or Curve448 static ECDH key
|
||||
after inputting a chosen public key and observing the victim performing the
|
||||
corresponding private-key operation. Found and reported by Leila Batina,
|
||||
Lukas Chmielewski, Björn Haase, Niels Samwel and Peter Schwabe.
|
||||
* Fix an issue where an adversary with access to precise enough timing
|
||||
information (typically, a co-located process) could recover a Curve25519
|
||||
or Curve448 static ECDH key after inputting a chosen public key and
|
||||
observing the victim performing the corresponding private-key operation.
|
||||
Found and reported by Leila Batina, Lukas Chmielewski, Björn Haase, Niels
|
||||
Samwel and Peter Schwabe.
|
||||
|
||||
Bugfix
|
||||
* Fix premature fopen() call in mbedtls_entropy_write_seed_file which may
|
||||
|
5
ChangeLog.d/add_interruptible_sign_hash
Normal file
5
ChangeLog.d/add_interruptible_sign_hash
Normal file
@ -0,0 +1,5 @@
|
||||
Features
|
||||
* Add an interruptible version of sign and verify hash to the PSA interface,
|
||||
backed by internal library support for ECDSA signing and verification.
|
||||
|
||||
|
7
ChangeLog.d/driver-only-ecdsa.txt
Normal file
7
ChangeLog.d/driver-only-ecdsa.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Features
|
||||
* When a PSA driver for ECDSA is present, it is now possible to disable
|
||||
MBEDTLS_ECDSA_C in the build in order to save code size. For PK, X.509
|
||||
and TLS to fully work, this requires MBEDTLS_USE_PSA_CRYPTO to be enabled.
|
||||
Restartable/interruptible ECDSA operations in PK, X.509 and TLS are not
|
||||
supported in those builds yet, as driver support for interruptible ECDSA
|
||||
operations is not present yet.
|
3
ChangeLog.d/empty-retval-description.txt
Normal file
3
ChangeLog.d/empty-retval-description.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Bugfix
|
||||
* Silence warnings from clang -Wdocumentation about empty \retval
|
||||
descriptions, which started appearing with Clang 15. Fixes #6960.
|
6
ChangeLog.d/fix-oid-to-string-bugs.txt
Normal file
6
ChangeLog.d/fix-oid-to-string-bugs.txt
Normal file
@ -0,0 +1,6 @@
|
||||
Bugfix
|
||||
* Fix bug in conversion from OID to string in
|
||||
mbedtls_oid_get_numeric_string(). OIDs such as 2.40.0.25 are now printed
|
||||
correctly.
|
||||
* Reject OIDs with overlong-encoded subidentifiers when converting
|
||||
OID-to-string.
|
3
ChangeLog.d/mbedtls_ssl_read_undefined_behavior.txt
Normal file
3
ChangeLog.d/mbedtls_ssl_read_undefined_behavior.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Bugfix
|
||||
* Fix undefined behavior in mbedtls_ssl_read() and mbedtls_ssl_write() if
|
||||
len argument is 0 and buffer is NULL.
|
7
ChangeLog.d/mpi-window-perf
Normal file
7
ChangeLog.d/mpi-window-perf
Normal file
@ -0,0 +1,7 @@
|
||||
Changes
|
||||
* Changed the default MBEDTLS_ECP_WINDOW_SIZE from 6 to 2.
|
||||
As tested in issue 6790, the correlation between this define and
|
||||
RSA decryption performance has changed lately due to security fixes.
|
||||
To fix the performance degradation when using default values the
|
||||
window was reduced from 6 to 2, a value that gives the best or close
|
||||
to best results when tested on Cortex-M4 and Intel i7.
|
3
ChangeLog.d/platform-zeroization.txt
Normal file
3
ChangeLog.d/platform-zeroization.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Security
|
||||
* Use platform-provided secure zeroization function where possible, such as
|
||||
explicit_bzero().
|
4
ChangeLog.d/psa-alt-headers.txt
Normal file
4
ChangeLog.d/psa-alt-headers.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Features
|
||||
* The configuration macros MBEDTLS_PSA_CRYPTO_PLATFORM_FILE and
|
||||
MBEDTLS_PSA_CRYPTO_STRUCT_FILE specify alternative locations for
|
||||
the headers "psa/crypto_platform.h" and "psa/crypto_struct.h".
|
3
ChangeLog.d/san_rfc822Name.txt
Normal file
3
ChangeLog.d/san_rfc822Name.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Features
|
||||
* Add parsing of rfc822Name subtype for subjectAltName
|
||||
extension in x509 certificates.
|
5
ChangeLog.d/tls13-only-renegotiation.txt
Normal file
5
ChangeLog.d/tls13-only-renegotiation.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Bugfix
|
||||
* Fix the handling of renegotiation attempts in TLS 1.3. They are now
|
||||
systematically rejected.
|
||||
* Fix an unused-variable warning in TLS 1.3-only builds if
|
||||
MBEDTLS_SSL_RENEGOTIATION was enabled. Fixes #6200.
|
@ -61,10 +61,11 @@ The source code of Mbed TLS includes some files that are automatically generated
|
||||
The following tools are required:
|
||||
|
||||
* Perl, for some library source files and for Visual Studio build files.
|
||||
* Python 3 and some Python packages, for some library source files, sample programs and test data. To install the necessary packages, run
|
||||
* Python 3 and some Python packages, for some library source files, sample programs and test data. To install the necessary packages, run:
|
||||
```
|
||||
python -m pip install -r scripts/basic.requirements.txt
|
||||
python3 -m pip install --user -r scripts/basic.requirements.txt
|
||||
```
|
||||
Depending on your Python installation, you may need to invoke `python` instead of `python3`. To install the packages system-wide, omit the `--user` option.
|
||||
* A C compiler for the host platform, for some test data.
|
||||
|
||||
If you are cross-compiling, you must set the `CC` environment variable to a C compiler for the host platform when generating the configuration-independent files.
|
||||
|
@ -1,962 +0,0 @@
|
||||
## Getting started with Mbed TLS
|
||||
|
||||
### What is Mbed TLS?
|
||||
|
||||
Mbed TLS is an open source cryptographic library that supports a wide range of
|
||||
cryptographic operations, including:
|
||||
* Key management
|
||||
* Hashing
|
||||
* Symmetric cryptography
|
||||
* Asymmetric cryptography
|
||||
* Message authentication (MAC)
|
||||
* Key generation and derivation
|
||||
* Authenticated encryption with associated data (AEAD)
|
||||
|
||||
Mbed TLS provides a reference implementation of the cryptography interface of
|
||||
the Arm Platform Security Architecture (PSA). It is written in portable C.
|
||||
|
||||
Mbed TLS is distributed under the Apache License, version 2.0.
|
||||
|
||||
#### Platform Security Architecture (PSA)
|
||||
|
||||
Arm's Platform Security Architecture (PSA) is a holistic set of threat models,
|
||||
security analyses, hardware and firmware architecture specifications, and an
|
||||
open source firmware reference implementation. PSA provides a recipe, based on
|
||||
industry best practice, that enables you to design security into both hardware
|
||||
and firmware consistently. Part of the API provided by PSA is the cryptography
|
||||
interface, which provides access to a set of primitives.
|
||||
|
||||
### Using Mbed TLS
|
||||
|
||||
* [Getting the Mbed TLS library](#getting-the-mbed-tls-library)
|
||||
* [Building the Mbed TLS library](#building-the-mbed-tls-library)
|
||||
* [Using the PSA Crypto API](#using-the-psa-crypto-api)
|
||||
* [Importing a key](#importing-a-key)
|
||||
* [Signing a message using RSA](#signing-a-message-using-RSA)
|
||||
* [Encrypting or decrypting using symmetric ciphers](#encrypting-or-decrypting-using-symmetric-ciphers)
|
||||
* [Hashing a message](#hashing-a-message)
|
||||
* [Deriving a new key from an existing key](#deriving-a-new-key-from-an-existing-key)
|
||||
* [Generating a random value](#generating-a-random-value)
|
||||
* [Authenticating and encrypting or decrypting a message](#authenticating-and-encrypting-or-decrypting-a-message)
|
||||
* [Generating and exporting keys](#generating-and-exporting-keys)
|
||||
* [More about the PSA Crypto API](#more-about-the-psa-crypto-api)
|
||||
|
||||
### Getting the Mbed TLS library
|
||||
|
||||
Mbed TLS releases are available in the [public GitHub repository](https://github.com/Mbed-TLS/mbedtls).
|
||||
|
||||
### Building the Mbed TLS library
|
||||
|
||||
**Prerequisites to building the library with the provided makefiles:**
|
||||
* GNU Make.
|
||||
* A C toolchain (compiler, linker, archiver) that supports C99.
|
||||
* Python 3.6 to generate the test code.
|
||||
* Perl to run the tests.
|
||||
|
||||
If you have a C compiler such as GCC or Clang, just run `make` in the top-level
|
||||
directory to build the library, a set of unit tests and some sample programs.
|
||||
|
||||
To select a different compiler, set the `CC` variable to the name or path of the
|
||||
compiler and linker (default: `cc`) and set `AR` to a compatible archiver
|
||||
(default: `ar`); for example:
|
||||
```
|
||||
make CC=arm-linux-gnueabi-gcc AR=arm-linux-gnueabi-ar
|
||||
```
|
||||
The provided makefiles pass options to the compiler that assume a GCC-like
|
||||
command line syntax. To use a different compiler, you may need to pass different
|
||||
values for `CFLAGS`, `WARNINGS_CFLAGS` and `LDFLAGS`.
|
||||
|
||||
To run the unit tests on the host machine, run `make test` from the top-level
|
||||
directory. If you are cross-compiling, copy the test executable from the `tests`
|
||||
directory to the target machine.
|
||||
|
||||
### Using the PSA Crypto API
|
||||
|
||||
If using PSA Crypto, you must initialize the library by calling
|
||||
`psa_crypto_init()` before any other PSA API.
|
||||
|
||||
### Importing a key
|
||||
|
||||
To use a key for cryptography operations in PSA, you need to first
|
||||
import it. The import operation returns the identifier of the key for use
|
||||
with other function calls.
|
||||
|
||||
**Prerequisites to importing keys:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
|
||||
This example shows how to import a key:
|
||||
```C
|
||||
void import_a_key(const uint8_t *key, size_t key_len)
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Import an AES key...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set key attributes */
|
||||
psa_set_key_usage_flags(&attributes, 0);
|
||||
psa_set_key_algorithm(&attributes, 0);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
|
||||
/* Import the key */
|
||||
status = psa_import_key(&attributes, key, key_len, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import key\n");
|
||||
return;
|
||||
}
|
||||
printf("Imported a key\n");
|
||||
|
||||
/* Free the attributes */
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
}
|
||||
```
|
||||
|
||||
### Signing a message using RSA
|
||||
|
||||
The PSA Crypto API supports encrypting, decrypting, signing and verifying
|
||||
messages using public key signature algorithms, such as RSA or ECDSA.
|
||||
|
||||
**Prerequisites to performing asymmetric signature operations:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
* Have a valid key with appropriate attributes set:
|
||||
* Usage flag `PSA_KEY_USAGE_SIGN_HASH` to allow signing.
|
||||
* Usage flag `PSA_KEY_USAGE_VERIFY_HASH` to allow signature verification.
|
||||
* Algorithm set to the desired signature algorithm.
|
||||
|
||||
This example shows how to sign a hash that has already been calculated:
|
||||
```C
|
||||
void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
uint8_t hash[32] = {0x50, 0xd8, 0x58, 0xe0, 0x98, 0x5e, 0xcc, 0x7f,
|
||||
0x60, 0x41, 0x8a, 0xaf, 0x0c, 0xc5, 0xab, 0x58,
|
||||
0x7f, 0x42, 0xc2, 0x57, 0x0a, 0x88, 0x40, 0x95,
|
||||
0xa9, 0xe8, 0xcc, 0xac, 0xd0, 0xf6, 0x54, 0x5c};
|
||||
uint8_t signature[PSA_SIGNATURE_MAX_SIZE] = {0};
|
||||
size_t signature_length;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Sign a message...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set key attributes */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
|
||||
psa_set_key_bits(&attributes, 1024);
|
||||
|
||||
/* Import the key */
|
||||
status = psa_import_key(&attributes, key, key_len, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import key\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Sign message using the key */
|
||||
status = psa_sign_hash(key_id, PSA_ALG_RSA_PKCS1V15_SIGN_RAW,
|
||||
hash, sizeof(hash),
|
||||
signature, sizeof(signature),
|
||||
&signature_length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to sign\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Signed a message\n");
|
||||
|
||||
/* Free the attributes */
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
}
|
||||
```
|
||||
|
||||
### Using symmetric ciphers
|
||||
|
||||
The PSA Crypto API supports encrypting and decrypting messages using various
|
||||
symmetric cipher algorithms (both block and stream ciphers).
|
||||
|
||||
**Prerequisites to working with the symmetric cipher API:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
* Have a symmetric key. This key's usage flags must include
|
||||
`PSA_KEY_USAGE_ENCRYPT` to allow encryption or `PSA_KEY_USAGE_DECRYPT` to
|
||||
allow decryption.
|
||||
|
||||
**To encrypt a message with a symmetric cipher:**
|
||||
1. Allocate an operation (`psa_cipher_operation_t`) structure to pass to the
|
||||
cipher functions.
|
||||
1. Initialize the operation structure to zero or to `PSA_CIPHER_OPERATION_INIT`.
|
||||
1. Call `psa_cipher_encrypt_setup()` to specify the algorithm and the key to be
|
||||
used.
|
||||
1. Call either `psa_cipher_generate_iv()` or `psa_cipher_set_iv()` to generate
|
||||
or set the initialization vector (IV). We recommend calling
|
||||
`psa_cipher_generate_iv()`, unless you require a specific IV value.
|
||||
1. Call `psa_cipher_update()` with the message to encrypt. You may call this
|
||||
function multiple times, passing successive fragments of the message on
|
||||
successive calls.
|
||||
1. Call `psa_cipher_finish()` to end the operation and output the encrypted
|
||||
message.
|
||||
|
||||
This example shows how to encrypt data using an AES (Advanced Encryption
|
||||
Standard) key in CBC (Cipher Block Chaining) mode with no padding (assuming all
|
||||
prerequisites have been fulfilled):
|
||||
```c
|
||||
void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES),
|
||||
};
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
|
||||
uint8_t plaintext[block_size] = SOME_PLAINTEXT;
|
||||
uint8_t iv[block_size];
|
||||
size_t iv_len;
|
||||
uint8_t output[block_size];
|
||||
size_t output_len;
|
||||
psa_key_id_t key_id;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
|
||||
printf("Encrypt with cipher...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS)
|
||||
{
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Import a key */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_import_key(&attributes, key, key_len, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import a key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Encrypt the plaintext */
|
||||
status = psa_cipher_encrypt_setup(&operation, key_id, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to begin cipher operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to generate IV\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
|
||||
output, sizeof(output), &output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to update cipher operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_finish(&operation, output + output_len,
|
||||
sizeof(output) - output_len, &output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to finish cipher operation\n");
|
||||
return;
|
||||
}
|
||||
printf("Encrypted plaintext\n");
|
||||
|
||||
/* Clean up cipher operation context */
|
||||
psa_cipher_abort(&operation);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
}
|
||||
```
|
||||
|
||||
**To decrypt a message with a symmetric cipher:**
|
||||
1. Allocate an operation (`psa_cipher_operation_t`) structure to pass to the
|
||||
cipher functions.
|
||||
1. Initialize the operation structure to zero or to `PSA_CIPHER_OPERATION_INIT`.
|
||||
1. Call `psa_cipher_decrypt_setup()` to specify the algorithm and the key to be
|
||||
used.
|
||||
1. Call `psa_cipher_set_iv()` with the IV for the decryption.
|
||||
1. Call `psa_cipher_update()` with the message to encrypt. You may call this
|
||||
function multiple times, passing successive fragments of the message on
|
||||
successive calls.
|
||||
1. Call `psa_cipher_finish()` to end the operation and output the decrypted
|
||||
message.
|
||||
|
||||
This example shows how to decrypt encrypted data using an AES key in CBC mode
|
||||
with no padding (assuming all prerequisites have been fulfilled):
|
||||
```c
|
||||
void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES),
|
||||
};
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
uint8_t ciphertext[block_size] = SOME_CIPHERTEXT;
|
||||
uint8_t iv[block_size] = ENCRYPTED_WITH_IV;
|
||||
uint8_t output[block_size];
|
||||
size_t output_len;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Decrypt with cipher...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS)
|
||||
{
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Import a key */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_import_key(&attributes, key, key_len, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import a key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Decrypt the ciphertext */
|
||||
status = psa_cipher_decrypt_setup(&operation, key_id, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to begin cipher operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to set IV\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
|
||||
output, sizeof(output), &output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to update cipher operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_cipher_finish(&operation, output + output_len,
|
||||
sizeof(output) - output_len, &output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to finish cipher operation\n");
|
||||
return;
|
||||
}
|
||||
printf("Decrypted ciphertext\n");
|
||||
|
||||
/* Clean up cipher operation context */
|
||||
psa_cipher_abort(&operation);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
}
|
||||
```
|
||||
|
||||
#### Handling cipher operation contexts
|
||||
|
||||
After you've initialized the operation structure with a successful call to
|
||||
`psa_cipher_encrypt_setup()` or `psa_cipher_decrypt_setup()`, you can terminate
|
||||
the operation at any time by calling `psa_cipher_abort()`.
|
||||
|
||||
The call to `psa_cipher_abort()` frees any resources associated with the
|
||||
operation, except for the operation structure itself.
|
||||
|
||||
The PSA Crypto API implicitly calls `psa_cipher_abort()` when:
|
||||
* A call to `psa_cipher_generate_iv()`, `psa_cipher_set_iv()` or
|
||||
`psa_cipher_update()` fails (returning any status other than `PSA_SUCCESS`).
|
||||
* A call to `psa_cipher_finish()` succeeds or fails.
|
||||
|
||||
After an implicit or explicit call to `psa_cipher_abort()`, the operation
|
||||
structure is invalidated; in other words, you cannot reuse the operation
|
||||
structure for the same operation. You can, however, reuse the operation
|
||||
structure for a different operation by calling either
|
||||
`psa_cipher_encrypt_setup()` or `psa_cipher_decrypt_setup()` again.
|
||||
|
||||
You must call `psa_cipher_abort()` at some point for any operation that is
|
||||
initialized successfully (by a successful call to `psa_cipher_encrypt_setup()`
|
||||
or `psa_cipher_decrypt_setup()`).
|
||||
|
||||
Making multiple sequential calls to `psa_cipher_abort()` on an operation that
|
||||
is terminated (either implicitly or explicitly) is safe and has no effect.
|
||||
|
||||
### Hashing a message
|
||||
|
||||
The PSA Crypto API lets you compute and verify hashes using various hashing
|
||||
algorithms.
|
||||
|
||||
**Prerequisites to working with the hash APIs:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
|
||||
**To calculate a hash:**
|
||||
1. Allocate an operation structure (`psa_hash_operation_t`) to pass to the hash
|
||||
functions.
|
||||
1. Initialize the operation structure to zero or to `PSA_HASH_OPERATION_INIT`.
|
||||
1. Call `psa_hash_setup()` to specify the hash algorithm.
|
||||
1. Call `psa_hash_update()` with the message to encrypt. You may call this
|
||||
function multiple times, passing successive fragments of the message on
|
||||
successive calls.
|
||||
1. Call `psa_hash_finish()` to calculate the hash, or `psa_hash_verify()` to
|
||||
compare the computed hash with an expected hash value.
|
||||
|
||||
This example shows how to calculate the SHA-256 hash of a message:
|
||||
```c
|
||||
psa_status_t status;
|
||||
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
||||
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
||||
unsigned char input[] = { 'a', 'b', 'c' };
|
||||
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
|
||||
size_t actual_hash_len;
|
||||
|
||||
printf("Hash a message...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Compute hash of message */
|
||||
status = psa_hash_setup(&operation, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to begin hash operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_hash_update(&operation, input, sizeof(input));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to update hash operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash),
|
||||
&actual_hash_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to finish hash operation\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Hashed a message\n");
|
||||
|
||||
/* Clean up hash operation context */
|
||||
psa_hash_abort(&operation);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
This example shows how to verify the SHA-256 hash of a message:
|
||||
```c
|
||||
psa_status_t status;
|
||||
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
||||
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
||||
unsigned char input[] = { 'a', 'b', 'c' };
|
||||
unsigned char expected_hash[] = {
|
||||
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
|
||||
0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
|
||||
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
|
||||
};
|
||||
size_t expected_hash_len = PSA_HASH_LENGTH(alg);
|
||||
|
||||
printf("Verify a hash...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Verify message hash */
|
||||
status = psa_hash_setup(&operation, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to begin hash operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_hash_update(&operation, input, sizeof(input));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to update hash operation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_hash_verify(&operation, expected_hash, expected_hash_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to verify hash\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Verified a hash\n");
|
||||
|
||||
/* Clean up hash operation context */
|
||||
psa_hash_abort(&operation);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
The API provides the macro `PSA_HASH_LENGTH`, which returns the expected hash
|
||||
length (in bytes) for the specified algorithm.
|
||||
|
||||
#### Handling hash operation contexts
|
||||
|
||||
After a successful call to `psa_hash_setup()`, you can terminate the operation
|
||||
at any time by calling `psa_hash_abort()`. The call to `psa_hash_abort()` frees
|
||||
any resources associated with the operation, except for the operation structure
|
||||
itself.
|
||||
|
||||
The PSA Crypto API implicitly calls `psa_hash_abort()` when:
|
||||
1. A call to `psa_hash_update()` fails (returning any status other than
|
||||
`PSA_SUCCESS`).
|
||||
1. A call to `psa_hash_finish()` succeeds or fails.
|
||||
1. A call to `psa_hash_verify()` succeeds or fails.
|
||||
|
||||
After an implicit or explicit call to `psa_hash_abort()`, the operation
|
||||
structure is invalidated; in other words, you cannot reuse the operation
|
||||
structure for the same operation. You can, however, reuse the operation
|
||||
structure for a different operation by calling `psa_hash_setup()` again.
|
||||
|
||||
You must call `psa_hash_abort()` at some point for any operation that is
|
||||
initialized successfully (by a successful call to `psa_hash_setup()`) .
|
||||
|
||||
Making multiple sequential calls to `psa_hash_abort()` on an operation that has
|
||||
already been terminated (either implicitly or explicitly) is safe and has no
|
||||
effect.
|
||||
|
||||
### Generating a random value
|
||||
|
||||
The PSA Crypto API can generate random data.
|
||||
|
||||
**Prerequisites to generating random data:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
|
||||
<span class="notes">**Note:** To generate a random key, use `psa_generate_key()`
|
||||
instead of `psa_generate_random()`.</span>
|
||||
|
||||
This example shows how to generate ten bytes of random data by calling
|
||||
`psa_generate_random()`:
|
||||
```C
|
||||
psa_status_t status;
|
||||
uint8_t random[10] = { 0 };
|
||||
|
||||
printf("Generate random...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
status = psa_generate_random(random, sizeof(random));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to generate a random value\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Generated random data\n");
|
||||
|
||||
/* Clean up */
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
### Deriving a new key from an existing key
|
||||
|
||||
The PSA Crypto API provides a key derivation API that lets you derive new keys
|
||||
from existing ones. The key derivation API has functions to take inputs,
|
||||
including other keys and data, and functions to generate outputs, such as
|
||||
new keys or other data.
|
||||
|
||||
You must first initialize and set up a key derivation context,
|
||||
provided with a key and, optionally, other data. Then, use the key derivation
|
||||
context to either read derived data to a buffer or send derived data directly
|
||||
to a key slot.
|
||||
|
||||
See the documentation for the particular algorithm (such as HKDF or the
|
||||
TLS 1.2 PRF) for information about which inputs to pass when, and when you can
|
||||
obtain which outputs.
|
||||
|
||||
**Prerequisites to working with the key derivation APIs:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
* Use a key with the appropriate attributes set:
|
||||
* Usage flags set for key derivation (`PSA_KEY_USAGE_DERIVE`)
|
||||
* Key type set to `PSA_KEY_TYPE_DERIVE`.
|
||||
* Algorithm set to a key derivation algorithm
|
||||
(for example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)`).
|
||||
|
||||
**To derive a new AES-CTR 128-bit encryption key into a given key slot using HKDF
|
||||
with a given key, salt and info:**
|
||||
|
||||
1. Set up the key derivation context using the `psa_key_derivation_setup()`
|
||||
function, specifying the derivation algorithm `PSA_ALG_HKDF(PSA_ALG_SHA_256)`.
|
||||
1. Provide an optional salt with `psa_key_derivation_input_bytes()`.
|
||||
1. Provide info with `psa_key_derivation_input_bytes()`.
|
||||
1. Provide a secret with `psa_key_derivation_input_key()`, referencing a key
|
||||
that can be used for key derivation.
|
||||
1. Set the key attributes desired for the new derived key. We'll set
|
||||
the `PSA_KEY_USAGE_ENCRYPT` usage flag and the `PSA_ALG_CTR` algorithm for
|
||||
this example.
|
||||
1. Derive the key by calling `psa_key_derivation_output_key()`.
|
||||
1. Clean up the key derivation context.
|
||||
|
||||
At this point, the derived key slot holds a new 128-bit AES-CTR encryption key
|
||||
derived from the key, salt and info provided:
|
||||
```C
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
static const unsigned char key[] = {
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b };
|
||||
static const unsigned char salt[] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
||||
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c };
|
||||
static const unsigned char info[] = {
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
|
||||
0xf7, 0xf8, 0xf9 };
|
||||
psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
|
||||
psa_key_derivation_operation_t operation =
|
||||
PSA_KEY_DERIVATION_OPERATION_INIT;
|
||||
size_t derived_bits = 128;
|
||||
size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
|
||||
psa_key_id_t base_key;
|
||||
psa_key_id_t derived_key;
|
||||
|
||||
printf("Derive a key (HKDF)...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Import a key for use in key derivation. If such a key has already been
|
||||
* generated or imported, you can skip this part. */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
|
||||
psa_set_key_algorithm(&attributes, alg);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
|
||||
status = psa_import_key(&attributes, key, sizeof(key), &base_key);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import a key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Derive a key */
|
||||
status = psa_key_derivation_setup(&operation, alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to begin key derivation\n");
|
||||
return;
|
||||
}
|
||||
status = psa_key_derivation_set_capacity(&operation, capacity);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to set capacity\n");
|
||||
return;
|
||||
}
|
||||
status = psa_key_derivation_input_bytes(&operation,
|
||||
PSA_KEY_DERIVATION_INPUT_SALT,
|
||||
salt, sizeof(salt));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to input salt (extract)\n");
|
||||
return;
|
||||
}
|
||||
status = psa_key_derivation_input_key(&operation,
|
||||
PSA_KEY_DERIVATION_INPUT_SECRET,
|
||||
base_key);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to input key (extract)\n");
|
||||
return;
|
||||
}
|
||||
status = psa_key_derivation_input_bytes(&operation,
|
||||
PSA_KEY_DERIVATION_INPUT_INFO,
|
||||
info, sizeof(info));
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to input info (expand)\n");
|
||||
return;
|
||||
}
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_key_derivation_output_key(&attributes, &operation,
|
||||
&derived_key);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to derive key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
printf("Derived key\n");
|
||||
|
||||
/* Clean up key derivation operation */
|
||||
psa_key_derivation_abort(&operation);
|
||||
|
||||
/* Destroy the keys */
|
||||
psa_destroy_key(derived_key);
|
||||
psa_destroy_key(base_key);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
### Authenticating and encrypting or decrypting a message
|
||||
|
||||
The PSA Crypto API provides a simple way to authenticate and encrypt with
|
||||
associated data (AEAD), supporting the `PSA_ALG_CCM` algorithm.
|
||||
|
||||
**Prerequisites to working with the AEAD cipher APIs:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
* The key attributes for the key used for derivation must have the
|
||||
`PSA_KEY_USAGE_ENCRYPT` or `PSA_KEY_USAGE_DECRYPT` usage flags.
|
||||
|
||||
This example shows how to authenticate and encrypt a message:
|
||||
```C
|
||||
psa_status_t status;
|
||||
static const uint8_t key[] = {
|
||||
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
||||
0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF };
|
||||
static const uint8_t nonce[] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B };
|
||||
static const uint8_t additional_data[] = {
|
||||
0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25,
|
||||
0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 };
|
||||
static const uint8_t input_data[] = {
|
||||
0xB9, 0x6B, 0x49, 0xE2, 0x1D, 0x62, 0x17, 0x41,
|
||||
0x63, 0x28, 0x75, 0xDB, 0x7F, 0x6C, 0x92, 0x43,
|
||||
0xD2, 0xD7, 0xC2 };
|
||||
uint8_t *output_data = NULL;
|
||||
size_t output_size = 0;
|
||||
size_t output_length = 0;
|
||||
size_t tag_length = 16;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Authenticate encrypt...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
output_size = sizeof(input_data) + tag_length;
|
||||
output_data = (uint8_t *)malloc(output_size);
|
||||
if (!output_data) {
|
||||
printf("Out of memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Import a key */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_import_key(&attributes, key, sizeof(key), &key_id);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Authenticate and encrypt */
|
||||
status = psa_aead_encrypt(key_id, PSA_ALG_CCM,
|
||||
nonce, sizeof(nonce),
|
||||
additional_data, sizeof(additional_data),
|
||||
input_data, sizeof(input_data),
|
||||
output_data, output_size,
|
||||
&output_length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to authenticate and encrypt\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Authenticated and encrypted\n");
|
||||
|
||||
/* Clean up */
|
||||
free(output_data);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
This example shows how to authenticate and decrypt a message:
|
||||
|
||||
```C
|
||||
psa_status_t status;
|
||||
static const uint8_t key_data[] = {
|
||||
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
||||
0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF };
|
||||
static const uint8_t nonce[] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B };
|
||||
static const uint8_t additional_data[] = {
|
||||
0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25,
|
||||
0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 };
|
||||
static const uint8_t input_data[] = {
|
||||
0x20, 0x30, 0xE0, 0x36, 0xED, 0x09, 0xA0, 0x45, 0xAF, 0x3C, 0xBA, 0xEE,
|
||||
0x0F, 0xC8, 0x48, 0xAF, 0xCD, 0x89, 0x54, 0xF4, 0xF6, 0x3F, 0x28, 0x9A,
|
||||
0xA1, 0xDD, 0xB2, 0xB8, 0x09, 0xCD, 0x7C, 0xE1, 0x46, 0xE9, 0x98 };
|
||||
uint8_t *output_data = NULL;
|
||||
size_t output_size = 0;
|
||||
size_t output_length = 0;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Authenticate decrypt...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
output_size = sizeof(input_data);
|
||||
output_data = (uint8_t *)malloc(output_size);
|
||||
if (!output_data) {
|
||||
printf("Out of memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Import a key */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_import_key(&attributes, key_data, sizeof(key_data), &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to import a key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
/* Authenticate and decrypt */
|
||||
status = psa_aead_decrypt(key_id, PSA_ALG_CCM,
|
||||
nonce, sizeof(nonce),
|
||||
additional_data, sizeof(additional_data),
|
||||
input_data, sizeof(input_data),
|
||||
output_data, output_size,
|
||||
&output_length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to authenticate and decrypt %ld\n", status);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Authenticated and decrypted\n");
|
||||
|
||||
/* Clean up */
|
||||
free(output_data);
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
### Generating and exporting keys
|
||||
|
||||
The PSA Crypto API provides a simple way to generate a key or key pair.
|
||||
|
||||
**Prerequisites to using key generation and export APIs:**
|
||||
* Initialize the library with a successful call to `psa_crypto_init()`.
|
||||
|
||||
**To generate an ECDSA key:**
|
||||
1. Set the desired key attributes for key generation by calling
|
||||
`psa_set_key_algorithm()` with the chosen ECDSA algorithm (such as
|
||||
`PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256)`). You only want to export the
|
||||
public key, not the key pair (or private key); therefore, do not
|
||||
set `PSA_KEY_USAGE_EXPORT`.
|
||||
1. Generate a key by calling `psa_generate_key()`.
|
||||
1. Export the generated public key by calling `psa_export_public_key()`:
|
||||
```C
|
||||
enum {
|
||||
key_bits = 256,
|
||||
};
|
||||
psa_status_t status;
|
||||
size_t exported_length = 0;
|
||||
static uint8_t exported[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)];
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_id_t key_id;
|
||||
|
||||
printf("Generate a key pair...\t");
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize PSA Crypto */
|
||||
status = psa_crypto_init();
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to initialize PSA Crypto\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Generate a key */
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
||||
psa_set_key_algorithm(&attributes,
|
||||
PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
|
||||
psa_set_key_type(&attributes,
|
||||
PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, key_bits);
|
||||
status = psa_generate_key(&attributes, &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to generate key\n");
|
||||
return;
|
||||
}
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
status = psa_export_public_key(key_id, exported, sizeof(exported),
|
||||
&exported_length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
printf("Failed to export public key %ld\n", status);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Exported a public key\n");
|
||||
|
||||
/* Destroy the key */
|
||||
psa_destroy_key(key_id);
|
||||
|
||||
mbedtls_psa_crypto_free();
|
||||
```
|
||||
|
||||
### More about the PSA Crypto API
|
||||
|
||||
For more information about the PSA Crypto API, please see the
|
||||
[PSA Cryptography API Specification](https://arm-software.github.io/psa-api/crypto/).
|
@ -321,6 +321,159 @@ TODO: key input and output for opaque drivers; deterministic key generation for
|
||||
|
||||
TODO
|
||||
|
||||
### Driver entry points for PAKE
|
||||
|
||||
A PAKE operation is divided into two stages: collecting inputs and computation. Core side is responsible for keeping inputs and core set-data functions do not have driver entry points. Collected inputs are available for drivers via get-data functions for `password`, `role` and `cipher_suite`.
|
||||
|
||||
### PAKE driver dispatch logic
|
||||
The core decides whether to dispatch a PAKE operation to a driver based on the location of the provided password.
|
||||
When all inputs are collected and `"psa_pake_output"` or `"psa_pake_input"` is called for the first time `"pake_setup"` driver entry point is invoked.
|
||||
|
||||
1. If the location of the `password` is the local storage
|
||||
- if there is a transparent driver for the specified ciphersuite, the core calls that driver's `"pake_setup"` and subsequent entry points.
|
||||
- otherwise, or on fallback, the core uses its built-in implementation.
|
||||
2. If the location of the `password` is the location of a secure element
|
||||
- the core calls the `"pake_setup"` entry point of the secure element driver and subsequent entry points.
|
||||
|
||||
### Summary of entry points for PAKE
|
||||
|
||||
A PAKE driver has the following entry points:
|
||||
* `"pake_setup"` (mandatory): always the first entry point to be called. It is called when all inputs are collected and the computation stage starts.
|
||||
* `"pake_output"` (mandatory): derive cryptographic material for the specified step and output it.
|
||||
* `"pake_input"` (mandatory): provides cryptographic material in the format appropriate for the specified step.
|
||||
* `"pake_get_implicit_key"` (mandatory): returns implicitly confirmed shared secret from a PAKE.
|
||||
* `"pake_abort"` (mandatory): always the last entry point to be called.
|
||||
|
||||
For naming purposes, here and in the following subsection, this specification takes the example of a driver with the prefix `"acme"` that implements the PAKE entry point family with a capability that does not use the `"names"` property to declare different type and entry point names. Such a driver must implement the following type and functions, as well as the entry points listed above and described in the following subsections:
|
||||
```
|
||||
typedef ... acme_pake_operation_t;
|
||||
psa_status_t acme_pake_abort( acme_pake_operation_t *operation );
|
||||
```
|
||||
|
||||
#### PAKE driver inputs
|
||||
|
||||
The core conveys the initial inputs for a PAKE operation via an opaque data structure of type `psa_crypto_driver_pake_inputs_t`.
|
||||
|
||||
```
|
||||
typedef ... psa_crypto_driver_pake_inputs_t; // implementation-specific type
|
||||
```
|
||||
|
||||
A driver receiving an argument that points to a `psa_crypto_driver_pake_inputs_t` can retrieve its contents by calling one of the get-data functions below.
|
||||
|
||||
```
|
||||
psa_status_t psa_crypto_driver_pake_get_password_len(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
size_t *password_len);
|
||||
|
||||
psa_status_t psa_crypto_driver_pake_get_password_bytes(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
|
||||
|
||||
psa_status_t psa_crypto_driver_pake_get_password_key(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
uint8_t** p_key_buffer, size_t *key_buffer_size,
|
||||
const psa_key_attributes_t *attributes);
|
||||
|
||||
psa_status_t psa_crypto_driver_pake_get_role(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
psa_pake_role_t *role);
|
||||
|
||||
psa_status_t psa_crypto_driver_pake_get_cipher_suite(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
psa_pake_cipher_suite_t *cipher_suite);
|
||||
```
|
||||
The get-data functions take the following parameters:
|
||||
|
||||
The first parameter `inputs` must be a pointer passed by the core to a PAKE driver setup entry point.
|
||||
Next parameters are return buffers (must not be null pointers).
|
||||
|
||||
These functions can return the following statuses:
|
||||
* `PSA_SUCCESS`: value has been successfully obtained
|
||||
* `PSA_ERROR_BAD_STATE`: the inputs are not ready
|
||||
* `PSA_ERROR_BUFFER_TOO_SMALL` (`psa_crypto_driver_pake_get_password_bytes` and `psa_crypto_driver_pake_get_password_key` only): the output buffer is too small. This is not a fatal error and the driver can, for example, subsequently call the same function again with a larger buffer. Call `psa_crypto_driver_pake_get_password_len` to obtain the required size.
|
||||
|
||||
#### PAKE driver setup
|
||||
|
||||
```
|
||||
psa_status_t acme_pake_setup( acme_pake_operation_t *operation,
|
||||
const psa_crypto_driver_pake_inputs_t *inputs );
|
||||
```
|
||||
|
||||
* `operation` is a zero-initialized operation object.
|
||||
* `inputs` is an opaque pointer to the [inputs](#pake-driver-inputs) for the PAKE operation.
|
||||
|
||||
The setup driver function should preserve the inputs using get-data functions.
|
||||
|
||||
The pointer output by `psa_crypto_driver_pake_get_password_key` is only valid until the "pake_setup" entry point returns. Opaque drivers must copy all relevant data from the key buffer during the "pake_setup" entry point and must not store the pointer itself.
|
||||
|
||||
#### PAKE driver output
|
||||
|
||||
```
|
||||
psa_status_t acme_pake_output(acme_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
```
|
||||
|
||||
* `operation` is an operation object.
|
||||
* `step` computation step based on which driver should perform an action.
|
||||
* `output` buffer where the output is to be written.
|
||||
* `output_size` size of the output buffer in bytes.
|
||||
* `output_length` the number of bytes of the returned output.
|
||||
|
||||
For `PSA_ALG_JPAKE` the following steps are available for output operation:
|
||||
`step` can be one of the following values:
|
||||
* `PSA_JPAKE_X1_STEP_KEY_SHARE` Round 1: output our key share (for ephemeral private key X1)
|
||||
* `PSA_JPAKE_X1_STEP_ZK_PUBLIC` Round 1: output Schnorr NIZKP public key for the X1 key
|
||||
* `PSA_JPAKE_X1_STEP_ZK_PROOF` Round 1: output Schnorr NIZKP proof for the X1 key
|
||||
* `PSA_JPAKE_X2_STEP_KEY_SHARE` Round 1: output our key share (for ephemeral private key X2)
|
||||
* `PSA_JPAKE_X2_STEP_ZK_PUBLIC` Round 1: output Schnorr NIZKP public key for the X2 key
|
||||
* `PSA_JPAKE_X2_STEP_ZK_PROOF` Round 1: output Schnorr NIZKP proof for the X2 key
|
||||
* `PSA_JPAKE_X2S_STEP_KEY_SHARE` Round 2: output our X2S key
|
||||
* `PSA_JPAKE_X2S_STEP_ZK_PUBLIC` Round 2: output Schnorr NIZKP public key for the X2S key
|
||||
* `PSA_JPAKE_X2S_STEP_ZK_PROOF` Round 2: output Schnorr NIZKP proof for the X2S key
|
||||
|
||||
#### PAKE driver input
|
||||
```
|
||||
psa_status_t acme_pake_input(acme_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *input,
|
||||
size_t input_size);
|
||||
```
|
||||
|
||||
* `operation` is an operation object.
|
||||
* `step` computation step based on which driver should perform an action.
|
||||
* `input` buffer containing the input.
|
||||
* `input_length` length of the input in bytes.
|
||||
|
||||
For `PSA_ALG_JPAKE` the following steps are available for input operation:
|
||||
* `PSA_JPAKE_X1_STEP_KEY_SHARE` Round 1: input key share from peer (for ephemeral private key X1)
|
||||
* `PSA_JPAKE_X1_STEP_ZK_PUBLIC` Round 1: input Schnorr NIZKP public key for the X1 key
|
||||
* `PSA_JPAKE_X1_STEP_ZK_PROOF` Round 1: input Schnorr NIZKP proof for the X1 key
|
||||
* `PSA_JPAKE_X2_STEP_KEY_SHARE` Round 1: input key share from peer (for ephemeral private key X2)
|
||||
* `PSA_JPAKE_X2_STEP_ZK_PUBLIC` Round 1: input Schnorr NIZKP public key for the X2 key
|
||||
* `PSA_JPAKE_X2_STEP_ZK_PROOF` Round 1: input Schnorr NIZKP proof for the X2 key
|
||||
* `PSA_JPAKE_X4S_STEP_KEY_SHARE` Round 2: input X4S key from peer
|
||||
* `PSA_JPAKE_X4S_STEP_ZK_PUBLIC` Round 2: input Schnorr NIZKP public key for the X4S key
|
||||
* `PSA_JPAKE_X4S_STEP_ZK_PROOF` Round 2: input Schnorr NIZKP proof for the X4S key
|
||||
|
||||
The core checks that input_length is smaller than PSA_PAKE_INPUT_MAX_SIZE.
|
||||
|
||||
### PAKE driver get implicit key
|
||||
|
||||
```
|
||||
psa_status_t acme_pake_get_implicit_key(
|
||||
acme_pake_operation_t *operation,
|
||||
uint8_t *output, size_t output_size,
|
||||
size_t *output_length );
|
||||
```
|
||||
|
||||
* `operation` The driver PAKE operation object to use.
|
||||
* `output` Buffer where the implicit key is to be written.
|
||||
* `output_size` Size of the output buffer in bytes.
|
||||
* `output_length` On success, the number of bytes of the implicit key.
|
||||
|
||||
### Driver entry points for key management
|
||||
|
||||
The driver entry points for key management differ significantly between [transparent drivers](#key-management-with-transparent-drivers) and [opaque drivers](#key-management-with-opaque-drivers). This section describes common elements. Refer to the applicable section for each driver type for more information.
|
||||
|
@ -11,12 +11,15 @@ General considerations
|
||||
`psa_crypto_init()` before calling any function from the SSL/TLS, X.509 or PK
|
||||
module.
|
||||
|
||||
**Scope:** `MBEDTLS_USE_PSA_CRYPTO` has no effect on the parts of the code that
|
||||
are specific to TLS 1.3; those parts always use PSA Crypto. The parts of the
|
||||
TLS 1.3 code that are common with TLS 1.2, however, follow this option;
|
||||
currently this is the record protection code, computation of the running
|
||||
handshake hash, and X.509. You need to enable `MBEDTLS_USE_PSA_CRYPTO` if you
|
||||
want TLS 1.3 to use PSA everywhere.
|
||||
**Scope:** `MBEDTLS_USE_PSA_CRYPTO` has no effect on the most of the TLS 1.3
|
||||
code, which always uses PSA crypto. The parts of the TLS 1.3 code that will
|
||||
use PSA Crypto or not depending on the value of this option are:
|
||||
- record protection;
|
||||
- running handshake hash;
|
||||
- asymmetric signature verification & generation;
|
||||
- X.509 certificate chain verification.
|
||||
You need to enable `MBEDTLS_USE_PSA_CRYPTO` if you want TLS 1.3 to use PSA
|
||||
everywhere.
|
||||
|
||||
New APIs / API extensions
|
||||
-------------------------
|
||||
|
@ -27,3 +27,18 @@ HAVE_DOT = YES
|
||||
DOT_GRAPH_MAX_NODES = 200
|
||||
MAX_DOT_GRAPH_DEPTH = 1000
|
||||
DOT_TRANSPARENT = YES
|
||||
|
||||
# We mostly use \retval declarations to document which error codes a function
|
||||
# can return. The reader can follow the hyperlink to the definition of the
|
||||
# constant to get the generic documentation of that error code. If we don't
|
||||
# have anything to say about the specific error code for the specific
|
||||
# function, we can leave the description part of the \retval command blank.
|
||||
# This is perfectly valid as far as Doxygen is concerned. However, with
|
||||
# Clang >=15, the -Wdocumentation option emits a warning for empty
|
||||
# descriptions.
|
||||
# https://github.com/Mbed-TLS/mbedtls/issues/6960
|
||||
# https://github.com/llvm/llvm-project/issues/60315
|
||||
# As a workaround, you can write something like
|
||||
# \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
# This avoids writing redundant text and keeps Clang happy.
|
||||
ALIASES += emptydescription=""
|
||||
|
@ -63,7 +63,7 @@
|
||||
|
||||
#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
|
||||
/*
|
||||
* Maximum window size used for modular exponentiation. Default: 6
|
||||
* Maximum window size used for modular exponentiation. Default: 2
|
||||
* Minimum value: 1. Maximum value: 6.
|
||||
*
|
||||
* Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
|
||||
@ -71,7 +71,7 @@
|
||||
*
|
||||
* Reduction in size, reduces speed.
|
||||
*/
|
||||
#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
|
||||
#define MBEDTLS_MPI_WINDOW_SIZE 2 /**< Maximum window size used. */
|
||||
#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
|
||||
|
||||
#if !defined(MBEDTLS_MPI_MAX_SIZE)
|
||||
|
@ -80,26 +80,29 @@
|
||||
#include MBEDTLS_USER_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
/* Auto-enable MBEDTLS_MD_LIGHT based on MBEDTLS_MD_C.
|
||||
* This allows checking for MD_LIGHT rather than MD_LIGHT || MD_C.
|
||||
*/
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
#define MBEDTLS_MD_LIGHT
|
||||
#endif
|
||||
|
||||
/* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT
|
||||
* is defined as well to include all PSA code.
|
||||
*/
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#define MBEDTLS_PSA_CRYPTO_CLIENT
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
/* The PK wrappers need pk_write functions to format RSA key objects
|
||||
* when they are dispatching to the PSA API. This happens under USE_PSA_CRYPTO,
|
||||
* and also even without USE_PSA_CRYPTO for mbedtls_pk_sign_ext().
|
||||
* PSA crypto also needs pk_write to export RSA keys (otherwise the build
|
||||
* goes through but psa_export_key() and psa_export_public_key() fail on
|
||||
* RSA keys), and pk_parse to work with RSA keys in almost any way.
|
||||
*/
|
||||
* and also even without USE_PSA_CRYPTO for mbedtls_pk_sign_ext(). */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_RSA_C)
|
||||
#define MBEDTLS_PK_C
|
||||
#define MBEDTLS_PK_WRITE_C
|
||||
#define MBEDTLS_PK_PARSE_C
|
||||
#endif
|
||||
|
||||
/* Under MBEDTLS_USE_PSA_CRYPTO, the pk module needs pk_write functions
|
||||
* to pass ECC keys to PSA. */
|
||||
#if defined(MBEDTLS_PK_C) && \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECP_C)
|
||||
#define MBEDTLS_PK_WRITE_C
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
#undef MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
|
||||
#undef MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
|
||||
|
@ -70,6 +70,10 @@
|
||||
#error "MBEDTLS_AESNI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && !defined(MBEDTLS_HAVE_ASM)
|
||||
#error "MBEDTLS_AESCE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C)
|
||||
#error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
@ -275,8 +279,20 @@
|
||||
#error "MBEDTLS_HMAC_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
/* Helper for ECDSA dependencies, will be undefined at the end of the file */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(PSA_HAVE_FULL_ECDSA)
|
||||
#define MBEDTLS_PK_HAVE_ECDSA
|
||||
#endif
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#define MBEDTLS_PK_HAVE_ECDSA
|
||||
#endif
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
|
||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
|
||||
( !defined(MBEDTLS_ECDH_C) || \
|
||||
!defined(MBEDTLS_PK_HAVE_ECDSA) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C) )
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
@ -308,8 +324,9 @@
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
||||
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
||||
( !defined(MBEDTLS_ECDH_C) || \
|
||||
!defined(MBEDTLS_PK_HAVE_ECDSA) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C) )
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
@ -708,41 +725,6 @@
|
||||
#if defined(MBEDTLS_SHA512_ALT) || defined(MBEDTLS_SHA512_PROCESS_ALT)
|
||||
#error "MBEDTLS_SHA512_*ALT can't be used with MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
#endif
|
||||
/*
|
||||
* Best performance comes from most recent compilers, with intrinsics and -O3.
|
||||
* Must compile with -march=armv8.2-a+sha3, but we can't detect armv8.2-a, and
|
||||
* can't always detect __ARM_FEATURE_SHA512 (notably clang 7-12).
|
||||
*
|
||||
* GCC < 8 won't work at all (lacks the sha512 instructions)
|
||||
* GCC >= 8 uses intrinsics, sets __ARM_FEATURE_SHA512
|
||||
*
|
||||
* Clang < 7 won't work at all (lacks the sha512 instructions)
|
||||
* Clang 7-12 don't have intrinsics (but we work around that with inline
|
||||
* assembler) or __ARM_FEATURE_SHA512
|
||||
* Clang == 13.0.0 same as clang 12 (only seen on macOS)
|
||||
* Clang >= 13.0.1 has __ARM_FEATURE_SHA512 and intrinsics
|
||||
*/
|
||||
#if defined(__aarch64__) && !defined(__ARM_FEATURE_SHA512)
|
||||
/* Test Clang first, as it defines __GNUC__ */
|
||||
# if defined(__clang__)
|
||||
# if __clang_major__ < 7
|
||||
# error "A more recent Clang is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# elif __clang_major__ < 13 || \
|
||||
(__clang_major__ == 13 && __clang_minor__ == 0 && __clang_patchlevel__ == 0)
|
||||
/* We implement the intrinsics with inline assembler, so don't error */
|
||||
# else
|
||||
# error "Must use minimum -march=armv8.2-a+sha3 for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
# elif defined(__GNUC__)
|
||||
# if __GNUC__ < 8
|
||||
# error "A more recent GCC is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# else
|
||||
# error "Must use minimum -march=armv8.2-a+sha3 for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
# else
|
||||
# error "Only GCC and Clang supported for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY */
|
||||
|
||||
@ -763,9 +745,7 @@
|
||||
#if defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_SHA256_PROCESS_ALT)
|
||||
#error "MBEDTLS_SHA256_*ALT can't be used with MBEDTLS_SHA256_USE_A64_CRYPTO_*"
|
||||
#endif
|
||||
#if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO)
|
||||
#error "Must use minimum -march=armv8-a+crypto for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY) && \
|
||||
@ -803,7 +783,7 @@
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
||||
#if !( defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||
( defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_PKCS1_V21) ) )
|
||||
( defined(MBEDTLS_PK_HAVE_ECDSA) || defined(MBEDTLS_PKCS1_V21) ) )
|
||||
#error "MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
#endif
|
||||
@ -932,6 +912,11 @@
|
||||
#error "MBEDTLS_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_RENEGOTIATION) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
#error "MBEDTLS_SSL_RENEGOTIATION defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TICKET_C) && ( !defined(MBEDTLS_CIPHER_C) && \
|
||||
!defined(MBEDTLS_USE_PSA_CRYPTO) )
|
||||
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
|
||||
@ -1094,6 +1079,9 @@
|
||||
#error "MBEDTLS_PKCS7_C is defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
/* Undefine helper symbols */
|
||||
#undef MBEDTLS_PK_HAVE_ECDSA
|
||||
|
||||
/*
|
||||
* Avoid warning from -pedantic. This is a convenient place for this
|
||||
* workaround since this is included by every single file before the
|
||||
|
@ -147,12 +147,14 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(PSA_WANT_ALG_JPAKE)
|
||||
#if !defined(MBEDTLS_PSA_ACCEL_ALG_JPAKE)
|
||||
#define MBEDTLS_PSA_BUILTIN_PAKE 1
|
||||
#define MBEDTLS_PSA_BUILTIN_ALG_JPAKE 1
|
||||
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
|
||||
#define MBEDTLS_BIGNUM_C
|
||||
#define MBEDTLS_ECP_C
|
||||
#define MBEDTLS_ECJPAKE_C
|
||||
#endif /* MBEDTLS_PSA_ACCEL_ALG_JPAKE */
|
||||
#endif /* PSA_WANT_ALG_JPAKE */
|
||||
|
||||
#if defined(PSA_WANT_ALG_RIPEMD160) && !defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
|
||||
@ -841,6 +843,11 @@ extern "C" {
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CONFIG */
|
||||
|
||||
#if defined(PSA_WANT_ALG_ECDSA) && defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) && \
|
||||
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||
#define PSA_HAVE_FULL_ECDSA 1
|
||||
#endif
|
||||
|
||||
/* These features are always enabled. */
|
||||
#define PSA_WANT_KEY_TYPE_DERIVE 1
|
||||
#define PSA_WANT_KEY_TYPE_PASSWORD 1
|
||||
|
@ -222,6 +222,134 @@ int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
|
||||
void *p_rng_blind);
|
||||
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
|
||||
#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
|
||||
/**
|
||||
* \brief This function computes the ECDSA signature of a
|
||||
* previously-hashed message, in a restartable way.
|
||||
*
|
||||
* \note The deterministic version implemented in
|
||||
* mbedtls_ecdsa_sign_det_restartable() is usually
|
||||
* preferred.
|
||||
*
|
||||
* \note This function is like \c mbedtls_ecdsa_sign() but
|
||||
* it can return early and restart according to the
|
||||
* limit set with \c mbedtls_ecp_set_max_ops() to
|
||||
* reduce blocking.
|
||||
*
|
||||
* \note If the bitlength of the message hash is larger
|
||||
* than the bitlength of the group order, then the
|
||||
* hash is truncated as defined in <em>Standards for
|
||||
* Efficient Cryptography Group (SECG): SEC1 Elliptic
|
||||
* Curve Cryptography</em>, section 4.1.3, step 5.
|
||||
*
|
||||
* \see ecp.h
|
||||
*
|
||||
* \param grp The context for the elliptic curve to use.
|
||||
* This must be initialized and have group parameters
|
||||
* set, for example through mbedtls_ecp_group_load().
|
||||
* \param r The MPI context in which to store the first part
|
||||
* the signature. This must be initialized.
|
||||
* \param s The MPI context in which to store the second part
|
||||
* the signature. This must be initialized.
|
||||
* \param d The private signing key. This must be initialized
|
||||
* and setup, for example through
|
||||
* mbedtls_ecp_gen_privkey().
|
||||
* \param buf The hashed content to be signed. This must be a readable
|
||||
* buffer of length \p blen Bytes. It may be \c NULL if
|
||||
* \p blen is zero.
|
||||
* \param blen The length of \p buf in Bytes.
|
||||
* \param f_rng The RNG function. This must not be \c NULL.
|
||||
* \param p_rng The RNG context to be passed to \p f_rng. This may be
|
||||
* \c NULL if \p f_rng doesn't need a context parameter.
|
||||
* \param f_rng_blind The RNG function used for blinding. This must not be
|
||||
* \c NULL.
|
||||
* \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
|
||||
* \c NULL if \p f_rng doesn't need a context parameter.
|
||||
* \param rs_ctx The restart context to use. This may be \c NULL
|
||||
* to disable restarting. If it is not \c NULL, it
|
||||
* must point to an initialized restart context.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
|
||||
* operations was reached: see \c
|
||||
* mbedtls_ecp_set_max_ops().
|
||||
* \return Another \c MBEDTLS_ERR_ECP_XXX, \c
|
||||
* MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
|
||||
* error code on failure.
|
||||
*/
|
||||
int mbedtls_ecdsa_sign_restartable(
|
||||
mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d,
|
||||
const unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx);
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||
|
||||
/**
|
||||
* \brief This function computes the ECDSA signature of a
|
||||
* previously-hashed message, in a restartable way.
|
||||
*
|
||||
* \note This function is like \c
|
||||
* mbedtls_ecdsa_sign_det_ext() but it can return
|
||||
* early and restart according to the limit set with
|
||||
* \c mbedtls_ecp_set_max_ops() to reduce blocking.
|
||||
*
|
||||
* \note If the bitlength of the message hash is larger
|
||||
* than the bitlength of the group order, then the
|
||||
* hash is truncated as defined in <em>Standards for
|
||||
* Efficient Cryptography Group (SECG): SEC1 Elliptic
|
||||
* Curve Cryptography</em>, section 4.1.3, step 5.
|
||||
*
|
||||
* \see ecp.h
|
||||
*
|
||||
* \param grp The context for the elliptic curve to use.
|
||||
* This must be initialized and have group parameters
|
||||
* set, for example through mbedtls_ecp_group_load().
|
||||
* \param r The MPI context in which to store the first part
|
||||
* the signature. This must be initialized.
|
||||
* \param s The MPI context in which to store the second part
|
||||
* the signature. This must be initialized.
|
||||
* \param d The private signing key. This must be initialized
|
||||
* and setup, for example through
|
||||
* mbedtls_ecp_gen_privkey().
|
||||
* \param buf The hashed content to be signed. This must be a readable
|
||||
* buffer of length \p blen Bytes. It may be \c NULL if
|
||||
* \p blen is zero.
|
||||
* \param blen The length of \p buf in Bytes.
|
||||
* \param f_rng_blind The RNG function used for blinding. This must not be
|
||||
* \c NULL.
|
||||
* \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
|
||||
* \c NULL if \p f_rng doesn't need a context parameter.
|
||||
* \param rs_ctx The restart context to use. This may be \c NULL
|
||||
* to disable restarting. If it is not \c NULL, it
|
||||
* must point to an initialized restart context.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
|
||||
* operations was reached: see \c
|
||||
* mbedtls_ecp_set_max_ops().
|
||||
* \return Another \c MBEDTLS_ERR_ECP_XXX, \c
|
||||
* MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
|
||||
* error code on failure.
|
||||
*/
|
||||
int mbedtls_ecdsa_sign_det_restartable(
|
||||
mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
mbedtls_md_type_t md_alg,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx);
|
||||
|
||||
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
|
||||
#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
|
||||
|
||||
/**
|
||||
* \brief This function verifies the ECDSA signature of a
|
||||
* previously-hashed message.
|
||||
@ -257,6 +385,51 @@ int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
|
||||
const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
|
||||
const mbedtls_mpi *s);
|
||||
|
||||
#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
|
||||
/**
|
||||
* \brief This function verifies the ECDSA signature of a
|
||||
* previously-hashed message, in a restartable manner
|
||||
*
|
||||
* \note If the bitlength of the message hash is larger than the
|
||||
* bitlength of the group order, then the hash is truncated as
|
||||
* defined in <em>Standards for Efficient Cryptography Group
|
||||
* (SECG): SEC1 Elliptic Curve Cryptography</em>, section
|
||||
* 4.1.4, step 3.
|
||||
*
|
||||
* \see ecp.h
|
||||
*
|
||||
* \param grp The ECP group to use.
|
||||
* This must be initialized and have group parameters
|
||||
* set, for example through mbedtls_ecp_group_load().
|
||||
* \param buf The hashed content that was signed. This must be a readable
|
||||
* buffer of length \p blen Bytes. It may be \c NULL if
|
||||
* \p blen is zero.
|
||||
* \param blen The length of \p buf in Bytes.
|
||||
* \param Q The public key to use for verification. This must be
|
||||
* initialized and setup.
|
||||
* \param r The first integer of the signature.
|
||||
* This must be initialized.
|
||||
* \param s The second integer of the signature.
|
||||
* This must be initialized.
|
||||
* \param rs_ctx The restart context to use. This may be \c NULL to disable
|
||||
* restarting. If it is not \c NULL, it must point to an
|
||||
* initialized restart context.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
|
||||
* operations was reached: see \c mbedtls_ecp_set_max_ops().
|
||||
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
|
||||
* error code on failure.
|
||||
*/
|
||||
int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
|
||||
const unsigned char *buf, size_t blen,
|
||||
const mbedtls_ecp_point *Q,
|
||||
const mbedtls_mpi *r,
|
||||
const mbedtls_mpi *s,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx);
|
||||
|
||||
#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
|
||||
|
||||
/**
|
||||
* \brief This function computes the ECDSA signature and writes it
|
||||
* to a buffer, serialized as defined in <em>RFC-4492:
|
||||
|
@ -141,6 +141,15 @@ typedef enum {
|
||||
MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
|
||||
} mbedtls_ecp_curve_type;
|
||||
|
||||
/*
|
||||
* Curve modulus types
|
||||
*/
|
||||
typedef enum {
|
||||
MBEDTLS_ECP_MOD_NONE = 0,
|
||||
MBEDTLS_ECP_MOD_COORDINATE,
|
||||
MBEDTLS_ECP_MOD_SCALAR
|
||||
} mbedtls_ecp_modulus_type;
|
||||
|
||||
/**
|
||||
* Curve information, for use by other modules.
|
||||
*
|
||||
@ -472,6 +481,12 @@ mbedtls_ecp_keypair;
|
||||
* only enabled for specific sides and key exchanges
|
||||
* (currently only for clients and ECDHE-ECDSA).
|
||||
*
|
||||
* \warning Using the PSA interruptible interfaces with keys in local
|
||||
* storage and no accelerator driver will also call this
|
||||
* function to set the values specified via those interfaces,
|
||||
* overwriting values previously set. Care should be taken if
|
||||
* mixing these two interfaces.
|
||||
*
|
||||
* \param max_ops Maximum number of basic operations done in a row.
|
||||
* Default: 0 (unlimited).
|
||||
* Lower (non-zero) values mean ECC functions will block for
|
||||
|
@ -705,7 +705,7 @@
|
||||
* - Changes the behaviour of TLS 1.2 clients (not servers) when using the
|
||||
* ECDHE-ECDSA key exchange (not other key exchanges) to make all ECC
|
||||
* computations restartable:
|
||||
* - ECDH operations from the key exchange, only for Short Weierstass
|
||||
* - ECDH operations from the key exchange, only for Short Weierstrass
|
||||
* curves, only when MBEDTLS_USE_PSA_CRYPTO is not enabled.
|
||||
* - verification of the server's key exchange signature;
|
||||
* - verification of the server's certificate chain;
|
||||
@ -1578,11 +1578,14 @@
|
||||
* Requires: MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
|
||||
* Requires: MBEDTLS_PSA_CRYPTO_C
|
||||
*
|
||||
* Note: even though TLS 1.3 depends on PSA Crypto, and uses it unconditionally
|
||||
* for most operations, if you want it to only use PSA for all crypto
|
||||
* operations, you need to also enable MBEDTLS_USE_PSA_CRYPTO; otherwise X.509
|
||||
* operations, and functions that are common with TLS 1.2 (record protection,
|
||||
* running handshake hash) will still use non-PSA crypto.
|
||||
* \note TLS 1.3 uses PSA crypto for cryptographic operations that are
|
||||
* directly performed by TLS 1.3 code. As a consequence, you must
|
||||
* call psa_crypto_init() before the first TLS 1.3 handshake.
|
||||
*
|
||||
* \note Cryptographic operations performed indirectly via another module
|
||||
* (X.509, PK) or by code shared with TLS 1.2 (record protection,
|
||||
* running handshake hash) only use PSA crypto if
|
||||
* #MBEDTLS_USE_PSA_CRYPTO is enabled.
|
||||
*
|
||||
* Uncomment this macro to enable the support for TLS 1.3.
|
||||
*/
|
||||
@ -1651,45 +1654,6 @@
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE
|
||||
*
|
||||
* Maximum time difference in milliseconds tolerated between the age of a
|
||||
* ticket from the server and client point of view.
|
||||
* From the client point of view, the age of a ticket is the time difference
|
||||
* between the time when the client proposes to the server to use the ticket
|
||||
* (time of writing of the Pre-Shared Key Extension including the ticket) and
|
||||
* the time the client received the ticket from the server.
|
||||
* From the server point of view, the age of a ticket is the time difference
|
||||
* between the time when the server receives a proposition from the client
|
||||
* to use the ticket and the time when the ticket was created by the server.
|
||||
* The server age is expected to be always greater than the client one and
|
||||
* MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE defines the
|
||||
* maximum difference tolerated for the server to accept the ticket.
|
||||
* This is not used in TLS 1.2.
|
||||
*
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE 6000
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH
|
||||
*
|
||||
* Size in bytes of a ticket nonce. This is not used in TLS 1.2.
|
||||
*
|
||||
* This must be less than 256.
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH 32
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS
|
||||
*
|
||||
* Default number of NewSessionTicket messages to be sent by a TLS 1.3 server
|
||||
* after handshake completion. This is not used in TLS 1.2 and relevant only if
|
||||
* the MBEDTLS_SSL_SESSION_TICKETS option is enabled.
|
||||
*
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS 1
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_EARLY_DATA
|
||||
*
|
||||
@ -2065,6 +2029,34 @@
|
||||
*/
|
||||
#define MBEDTLS_AESNI_C
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_AESCE_C
|
||||
*
|
||||
* Enable AES crypto extension support on Arm64.
|
||||
*
|
||||
* Module: library/aesce.c
|
||||
* Caller: library/aes.c
|
||||
*
|
||||
* Requires: MBEDTLS_HAVE_ASM, MBEDTLS_AES_C
|
||||
*
|
||||
* \note The code uses Neon intrinsics, so \c CFLAGS must be set to a minimum
|
||||
* of \c -march=armv8-a+crypto .
|
||||
*
|
||||
* \warning If the target architecture is set to something that includes the
|
||||
* SHA3 feature (e.g. `-march=armv8.2-a+sha3`), for example because
|
||||
* `MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT` is desired, compilers
|
||||
* generate code for `MBEDTLS_AESCE_C` that includes instructions
|
||||
* only present with the (optional) SHA3 feature. This will lead to an
|
||||
* undefined instruction exception if the code is run on a CPU without
|
||||
* that feature.
|
||||
*
|
||||
* \warning Runtime detection only works on linux. For non-linux operation
|
||||
* system, crypto extension MUST be supported by CPU.
|
||||
*
|
||||
* This module adds support for the AES crypto instructions on Arm64
|
||||
*/
|
||||
#define MBEDTLS_AESCE_C
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_AES_C
|
||||
*
|
||||
@ -2643,7 +2635,7 @@
|
||||
/**
|
||||
* \def MBEDTLS_MD_C
|
||||
*
|
||||
* Enable the generic message digest layer.
|
||||
* Enable the generic layer for message digest (hashing) and HMAC.
|
||||
*
|
||||
* Requires: one of: MBEDTLS_MD5_C, MBEDTLS_RIPEMD160_C, MBEDTLS_SHA1_C,
|
||||
* MBEDTLS_SHA224_C, MBEDTLS_SHA256_C, MBEDTLS_SHA384_C,
|
||||
@ -3087,9 +3079,6 @@
|
||||
* \note If MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT is defined when building
|
||||
* for a non-Aarch64 build it will be silently ignored.
|
||||
*
|
||||
* \note The code uses Neon intrinsics, so \c CFLAGS must be set to a minimum
|
||||
* of \c -march=armv8-a+crypto.
|
||||
*
|
||||
* \warning MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT cannot be defined at the
|
||||
* same time as MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY.
|
||||
*
|
||||
@ -3112,9 +3101,6 @@
|
||||
* \note This allows builds with a smaller code size than with
|
||||
* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
|
||||
*
|
||||
* \note The code uses Neon intrinsics, so \c CFLAGS must be set to a minimum
|
||||
* of \c -march=armv8-a+crypto.
|
||||
*
|
||||
* \warning MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY cannot be defined at the same
|
||||
* time as MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT.
|
||||
*
|
||||
@ -3169,9 +3155,7 @@
|
||||
* for a non-Aarch64 build it will be silently ignored.
|
||||
*
|
||||
* \note The code uses the SHA-512 Neon intrinsics, so requires GCC >= 8 or
|
||||
* Clang >= 7, and \c CFLAGS must be set to a minimum of
|
||||
* \c -march=armv8.2-a+sha3. An optimisation level of \c -O3 generates the
|
||||
* fastest code.
|
||||
* Clang >= 7.
|
||||
*
|
||||
* \warning MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT cannot be defined at the
|
||||
* same time as MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY.
|
||||
@ -3196,9 +3180,7 @@
|
||||
* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
|
||||
*
|
||||
* \note The code uses the SHA-512 Neon intrinsics, so requires GCC >= 8 or
|
||||
* Clang >= 7, and \c CFLAGS must be set to a minimum of
|
||||
* \c -march=armv8.2-a+sha3. An optimisation level of \c -O3 generates the
|
||||
* fastest code.
|
||||
* Clang >= 7.
|
||||
*
|
||||
* \warning MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY cannot be defined at the same
|
||||
* time as MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT.
|
||||
@ -3537,6 +3519,53 @@
|
||||
*/
|
||||
//#define MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE "/dev/null"
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
|
||||
*
|
||||
* If defined, this is a header which will be included instead of
|
||||
* `"psa/crypto_platform.h"`. This file should declare the same identifiers
|
||||
* as the one in Mbed TLS, but with definitions adapted to the platform on
|
||||
* which the library code will run.
|
||||
*
|
||||
* \note The required content of this header can vary from one version of
|
||||
* Mbed TLS to the next. Integrators who provide an alternative file
|
||||
* should review the changes in the original file whenever they
|
||||
* upgrade Mbed TLS.
|
||||
*
|
||||
* This macro is expanded after an <tt>\#include</tt> directive. This is a popular but
|
||||
* non-standard feature of the C language, so this feature is only available
|
||||
* with compilers that perform macro expansion on an <tt>\#include</tt> line.
|
||||
*
|
||||
* The value of this symbol is typically a path in double quotes, either
|
||||
* absolute or relative to a directory on the include search path.
|
||||
*/
|
||||
//#define MBEDTLS_PSA_CRYPTO_PLATFORM_FILE "psa/crypto_platform_alt.h"
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_PSA_CRYPTO_STRUCT_FILE
|
||||
*
|
||||
* If defined, this is a header which will be included instead of
|
||||
* `"psa/crypto_struct.h"`. This file should declare the same identifiers
|
||||
* as the one in Mbed TLS, but with definitions adapted to the environment
|
||||
* in which the library code will run. The typical use for this feature
|
||||
* is to provide alternative type definitions on the client side in
|
||||
* client-server integrations of PSA crypto, where operation structures
|
||||
* contain handles instead of cryptographic data.
|
||||
*
|
||||
* \note The required content of this header can vary from one version of
|
||||
* Mbed TLS to the next. Integrators who provide an alternative file
|
||||
* should review the changes in the original file whenever they
|
||||
* upgrade Mbed TLS.
|
||||
*
|
||||
* This macro is expanded after an <tt>\#include</tt> directive. This is a popular but
|
||||
* non-standard feature of the C language, so this feature is only available
|
||||
* with compilers that perform macro expansion on an <tt>\#include</tt> line.
|
||||
*
|
||||
* The value of this symbol is typically a path in double quotes, either
|
||||
* absolute or relative to a directory on the include search path.
|
||||
*/
|
||||
//#define MBEDTLS_PSA_CRYPTO_STRUCT_FILE "psa/crypto_struct_alt.h"
|
||||
|
||||
/** \} name SECTION: General configuration options */
|
||||
|
||||
/**
|
||||
@ -3559,7 +3588,7 @@
|
||||
* comment in the specific module. */
|
||||
|
||||
/* MPI / BIGNUM options */
|
||||
//#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
|
||||
//#define MBEDTLS_MPI_WINDOW_SIZE 2 /**< Maximum window size used. */
|
||||
//#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
|
||||
|
||||
/* CTR_DRBG options */
|
||||
@ -3778,6 +3807,45 @@
|
||||
*/
|
||||
//#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE
|
||||
*
|
||||
* Maximum time difference in milliseconds tolerated between the age of a
|
||||
* ticket from the server and client point of view.
|
||||
* From the client point of view, the age of a ticket is the time difference
|
||||
* between the time when the client proposes to the server to use the ticket
|
||||
* (time of writing of the Pre-Shared Key Extension including the ticket) and
|
||||
* the time the client received the ticket from the server.
|
||||
* From the server point of view, the age of a ticket is the time difference
|
||||
* between the time when the server receives a proposition from the client
|
||||
* to use the ticket and the time when the ticket was created by the server.
|
||||
* The server age is expected to be always greater than the client one and
|
||||
* MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE defines the
|
||||
* maximum difference tolerated for the server to accept the ticket.
|
||||
* This is not used in TLS 1.2.
|
||||
*
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE 6000
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH
|
||||
*
|
||||
* Size in bytes of a ticket nonce. This is not used in TLS 1.2.
|
||||
*
|
||||
* This must be less than 256.
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH 32
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS
|
||||
*
|
||||
* Default number of NewSessionTicket messages to be sent by a TLS 1.3 server
|
||||
* after handshake completion. This is not used in TLS 1.2 and relevant only if
|
||||
* the MBEDTLS_SSL_SESSION_TICKETS option is enabled.
|
||||
*
|
||||
*/
|
||||
#define MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS 1
|
||||
|
||||
/* X509 options */
|
||||
//#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8 /**< Maximum number of intermediate CAs in a verification chain. */
|
||||
//#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512 /**< Maximum length of a path/filename string in bytes including the null terminator character ('\0'). */
|
||||
|
@ -1,7 +1,8 @@
|
||||
/**
|
||||
* \file md.h
|
||||
*
|
||||
* \brief This file contains the generic message-digest wrapper.
|
||||
* \brief This file contains the generic functions for message-digest
|
||||
* (hashing) and HMAC.
|
||||
*
|
||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
||||
*/
|
||||
@ -107,30 +108,6 @@ typedef struct mbedtls_md_context_t {
|
||||
void *MBEDTLS_PRIVATE(hmac_ctx);
|
||||
} mbedtls_md_context_t;
|
||||
|
||||
/**
|
||||
* \brief This function returns the list of digests supported by the
|
||||
* generic digest module.
|
||||
*
|
||||
* \note The list starts with the strongest available hashes.
|
||||
*
|
||||
* \return A statically allocated array of digests. Each element
|
||||
* in the returned list is an integer belonging to the
|
||||
* message-digest enumeration #mbedtls_md_type_t.
|
||||
* The last entry is 0.
|
||||
*/
|
||||
const int *mbedtls_md_list(void);
|
||||
|
||||
/**
|
||||
* \brief This function returns the message-digest information
|
||||
* associated with the given digest name.
|
||||
*
|
||||
* \param md_name The name of the digest to search for.
|
||||
*
|
||||
* \return The message-digest information associated with \p md_name.
|
||||
* \return NULL if the associated message-digest information is not found.
|
||||
*/
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
|
||||
|
||||
/**
|
||||
* \brief This function returns the message-digest information
|
||||
* associated with the given digest type.
|
||||
@ -142,19 +119,6 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
|
||||
*/
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
|
||||
|
||||
/**
|
||||
* \brief This function returns the message-digest information
|
||||
* from the given context.
|
||||
*
|
||||
* \param ctx The context from which to extract the information.
|
||||
* This must be initialized (or \c NULL).
|
||||
*
|
||||
* \return The message-digest information associated with \p ctx.
|
||||
* \return \c NULL if \p ctx is \c NULL.
|
||||
*/
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx);
|
||||
|
||||
/**
|
||||
* \brief This function initializes a message-digest context without
|
||||
* binding it to a particular message-digest algorithm.
|
||||
@ -248,17 +212,6 @@ unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
|
||||
*/
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
|
||||
|
||||
/**
|
||||
* \brief This function extracts the message-digest name from the
|
||||
* message-digest information structure.
|
||||
*
|
||||
* \param md_info The information structure of the message-digest algorithm
|
||||
* to use.
|
||||
*
|
||||
* \return The name of the message digest.
|
||||
*/
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
|
||||
|
||||
/**
|
||||
* \brief This function starts a message-digest computation.
|
||||
*
|
||||
@ -337,6 +290,54 @@ MBEDTLS_CHECK_RETURN_TYPICAL
|
||||
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
|
||||
unsigned char *output);
|
||||
|
||||
/**
|
||||
* \brief This function returns the list of digests supported by the
|
||||
* generic digest module.
|
||||
*
|
||||
* \note The list starts with the strongest available hashes.
|
||||
*
|
||||
* \return A statically allocated array of digests. Each element
|
||||
* in the returned list is an integer belonging to the
|
||||
* message-digest enumeration #mbedtls_md_type_t.
|
||||
* The last entry is 0.
|
||||
*/
|
||||
const int *mbedtls_md_list(void);
|
||||
|
||||
/**
|
||||
* \brief This function returns the message-digest information
|
||||
* associated with the given digest name.
|
||||
*
|
||||
* \param md_name The name of the digest to search for.
|
||||
*
|
||||
* \return The message-digest information associated with \p md_name.
|
||||
* \return NULL if the associated message-digest information is not found.
|
||||
*/
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
|
||||
|
||||
/**
|
||||
* \brief This function extracts the message-digest name from the
|
||||
* message-digest information structure.
|
||||
*
|
||||
* \param md_info The information structure of the message-digest algorithm
|
||||
* to use.
|
||||
*
|
||||
* \return The name of the message digest.
|
||||
*/
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
|
||||
|
||||
/**
|
||||
* \brief This function returns the message-digest information
|
||||
* from the given context.
|
||||
*
|
||||
* \param ctx The context from which to extract the information.
|
||||
* This must be initialized (or \c NULL).
|
||||
*
|
||||
* \return The message-digest information associated with \p ctx.
|
||||
* \return \c NULL if \p ctx is \c NULL.
|
||||
*/
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx);
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
/**
|
||||
* \brief This function calculates the message-digest checksum
|
||||
@ -471,10 +472,6 @@ int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output);
|
||||
|
||||
/* Internal use */
|
||||
MBEDTLS_CHECK_RETURN_TYPICAL
|
||||
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -796,6 +796,10 @@ static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
|
||||
/**
|
||||
* \brief Parse a private key in PEM or DER format
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param ctx The PK context to fill. It must have been initialized
|
||||
* but not set up.
|
||||
* \param key Input buffer to parse.
|
||||
@ -832,6 +836,10 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
|
||||
/**
|
||||
* \brief Parse a public key in PEM or DER format
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param ctx The PK context to fill. It must have been initialized
|
||||
* but not set up.
|
||||
* \param key Input buffer to parse.
|
||||
@ -861,6 +869,10 @@ int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
|
||||
/**
|
||||
* \brief Load and parse a private key
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param ctx The PK context to fill. It must have been initialized
|
||||
* but not set up.
|
||||
* \param path filename to read the private key from
|
||||
|
@ -135,22 +135,12 @@ typedef struct mbedtls_pkcs7_signer_info {
|
||||
}
|
||||
mbedtls_pkcs7_signer_info;
|
||||
|
||||
/**
|
||||
* Structure holding attached data as part of PKCS7 signed data format
|
||||
*/
|
||||
typedef struct mbedtls_pkcs7_data {
|
||||
mbedtls_pkcs7_buf MBEDTLS_PRIVATE(oid);
|
||||
mbedtls_pkcs7_buf MBEDTLS_PRIVATE(data);
|
||||
}
|
||||
mbedtls_pkcs7_data;
|
||||
|
||||
/**
|
||||
* Structure holding the signed data section
|
||||
*/
|
||||
typedef struct mbedtls_pkcs7_signed_data {
|
||||
int MBEDTLS_PRIVATE(version);
|
||||
mbedtls_pkcs7_buf MBEDTLS_PRIVATE(digest_alg_identifiers);
|
||||
struct mbedtls_pkcs7_data MBEDTLS_PRIVATE(content);
|
||||
int MBEDTLS_PRIVATE(no_of_certs);
|
||||
mbedtls_x509_crt MBEDTLS_PRIVATE(certs);
|
||||
int MBEDTLS_PRIVATE(no_of_crls);
|
||||
@ -165,7 +155,6 @@ mbedtls_pkcs7_signed_data;
|
||||
*/
|
||||
typedef struct mbedtls_pkcs7 {
|
||||
mbedtls_pkcs7_buf MBEDTLS_PRIVATE(raw);
|
||||
mbedtls_pkcs7_buf MBEDTLS_PRIVATE(content_type_oid);
|
||||
mbedtls_pkcs7_signed_data MBEDTLS_PRIVATE(signed_data);
|
||||
}
|
||||
mbedtls_pkcs7;
|
||||
@ -178,7 +167,7 @@ mbedtls_pkcs7;
|
||||
void mbedtls_pkcs7_init(mbedtls_pkcs7 *pkcs7);
|
||||
|
||||
/**
|
||||
* \brief Parse a single DER formatted pkcs7 content.
|
||||
* \brief Parse a single DER formatted pkcs7 detached signature.
|
||||
*
|
||||
* \param pkcs7 The pkcs7 structure to be filled by parser for the output.
|
||||
* \param buf The buffer holding only the DER encoded pkcs7.
|
||||
@ -188,6 +177,7 @@ void mbedtls_pkcs7_init(mbedtls_pkcs7 *pkcs7);
|
||||
* \note This function makes an internal copy of the PKCS7 buffer
|
||||
* \p buf. In particular, \p buf may be destroyed or reused
|
||||
* after this call returns.
|
||||
* \note Signatures with internal data are not supported.
|
||||
*
|
||||
* \return The \c mbedtls_pkcs7_type of \p buf, if successful.
|
||||
* \return A negative error code on failure.
|
||||
@ -207,7 +197,8 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
||||
* matches.
|
||||
*
|
||||
* This function does not use the certificates held within the
|
||||
* PKCS7 structure itself.
|
||||
* PKCS7 structure itself, and does not check that the
|
||||
* certificate is signed by a trusted certification authority.
|
||||
*
|
||||
* \param pkcs7 PKCS7 structure containing signature.
|
||||
* \param cert Certificate containing key to verify signature.
|
||||
@ -228,15 +219,15 @@ int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7,
|
||||
* \brief Verification of PKCS7 signature against a caller-supplied
|
||||
* certificate.
|
||||
*
|
||||
* For each signer in the PKCS structure, this function computes
|
||||
* a signature over the supplied hash, using the supplied
|
||||
* certificate and the same digest algorithm as specified by the
|
||||
* signer. It then compares this signature against the
|
||||
* signer's signature; verification succeeds if any comparison
|
||||
* matches.
|
||||
* For each signer in the PKCS structure, this function
|
||||
* validates a signature over the supplied hash, using the
|
||||
* supplied certificate and the same digest algorithm as
|
||||
* specified by the signer. Verification succeeds if any
|
||||
* signature is good.
|
||||
*
|
||||
* This function does not use the certificates held within the
|
||||
* PKCS7 structure itself.
|
||||
* PKCS7 structure itself, and does not check that the
|
||||
* certificate is signed by a trusted certification authority.
|
||||
*
|
||||
* \param pkcs7 PKCS7 structure containing signature.
|
||||
* \param cert Certificate containing key to verify signature.
|
||||
@ -244,7 +235,7 @@ int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7,
|
||||
* \param hashlen Length of the hash.
|
||||
*
|
||||
* \note This function is different from mbedtls_pkcs7_signed_data_verify()
|
||||
* in a way that it directly receives the hash of the data.
|
||||
* in that it is directly passed the hash of the data.
|
||||
*
|
||||
* \return 0 if the signature verifies, or a negative error code on failure.
|
||||
*/
|
||||
|
@ -257,6 +257,9 @@ static inline int mbedtls_psa_get_ecc_oid_from_id(
|
||||
#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
|
||||
PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
|
||||
|
||||
#define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
|
||||
PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
|
||||
|
||||
/* Expose whatever RNG the PSA subsystem uses to applications using the
|
||||
* mbedtls_xxx API. The declarations and definitions here need to be
|
||||
* consistent with the implementation in library/psa_crypto_random_impl.h.
|
||||
@ -341,6 +344,52 @@ extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
|
||||
|
||||
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
typedef struct {
|
||||
psa_status_t psa_status;
|
||||
int16_t mbedtls_error;
|
||||
} mbedtls_error_pair_t;
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_MD5_C) || defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
extern const mbedtls_error_pair_t psa_to_md_errors[4];
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_LMS_C)
|
||||
extern const mbedtls_error_pair_t psa_to_lms_errors[3];
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
extern const mbedtls_error_pair_t psa_to_ssl_errors[7];
|
||||
#endif
|
||||
|
||||
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
|
||||
defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
|
||||
extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8];
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||
extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7];
|
||||
#endif
|
||||
|
||||
/* Generic fallback function for error translation,
|
||||
* when the received state was not module-specific. */
|
||||
int psa_generic_status_to_mbedtls(psa_status_t status);
|
||||
|
||||
/* This function iterates over provided local error translations,
|
||||
* and if no match was found - calls the fallback error translation function. */
|
||||
int psa_status_to_mbedtls(psa_status_t status,
|
||||
const mbedtls_error_pair_t *local_translations,
|
||||
size_t local_errors_num,
|
||||
int (*fallback_f)(psa_status_t));
|
||||
|
||||
/* The second out of three-stage error handling functions of the pk module,
|
||||
* acts as a fallback after RSA / ECDSA error translation, and if no match
|
||||
* is found, it itself calls psa_generic_status_to_mbedtls. */
|
||||
int psa_pk_status_to_mbedtls(psa_status_t status);
|
||||
|
||||
/* Utility macro to shorten the defines of error translator in modules. */
|
||||
#define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
|
||||
psa_status_to_mbedtls(status, error_list, sizeof(error_list), fallback_f)
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
#endif /* MBEDTLS_PSA_UTIL_H */
|
||||
|
@ -1883,6 +1883,10 @@ void mbedtls_ssl_init(mbedtls_ssl_context *ssl);
|
||||
* Calling mbedtls_ssl_setup again is not supported, even
|
||||
* if no session is active.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param ssl SSL context
|
||||
* \param conf SSL configuration to use
|
||||
*
|
||||
@ -4698,6 +4702,11 @@ int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
|
||||
* in which case the datagram of the underlying transport that is
|
||||
* currently being processed might or might not contain further
|
||||
* DTLS records.
|
||||
*
|
||||
* \note If the context is configured to allow TLS 1.3, or if
|
||||
* #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*/
|
||||
int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl);
|
||||
|
||||
|
@ -294,7 +294,7 @@ typedef struct mbedtls_x509_subject_alternative_name {
|
||||
int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
|
||||
union {
|
||||
mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
|
||||
mbedtls_x509_buf unstructured_name; /**< The buffer for the unconstructed types. Only dnsName and uniformResourceIdentifier are currently supported */
|
||||
mbedtls_x509_buf unstructured_name; /**< The buffer for the unconstructed types. Only rfc822Name, dnsName and uniformResourceIdentifier are currently supported */
|
||||
}
|
||||
san; /**< A union of the supported SAN types */
|
||||
}
|
||||
@ -386,7 +386,7 @@ int mbedtls_x509_time_is_future(const mbedtls_x509_time *from);
|
||||
* of the subject alternative name encoded in \p san_raw.
|
||||
*
|
||||
* \note Supported GeneralName types, as defined in RFC 5280:
|
||||
* "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
|
||||
* "rfc822Name", "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
|
||||
* of type "otherName", as defined in RFC 4108.
|
||||
*
|
||||
* \note This function should be called on a single raw data of
|
||||
|
@ -107,6 +107,10 @@ mbedtls_x509_crl;
|
||||
/**
|
||||
* \brief Parse a DER-encoded CRL and append it to the chained list
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain points to the start of the chain
|
||||
* \param buf buffer holding the CRL data in DER format
|
||||
* \param buflen size of the buffer
|
||||
@ -121,6 +125,10 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
||||
*
|
||||
* \note Multiple CRLs are accepted only if using PEM format
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain points to the start of the chain
|
||||
* \param buf buffer holding the CRL data in PEM or DER format
|
||||
* \param buflen size of the buffer
|
||||
@ -136,6 +144,10 @@ int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, si
|
||||
*
|
||||
* \note Multiple CRLs are accepted only if using PEM format
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain points to the start of the chain
|
||||
* \param path filename to read the CRLs from (in PEM or DER encoding)
|
||||
*
|
||||
|
@ -341,6 +341,10 @@ extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none;
|
||||
* \brief Parse a single DER formatted certificate and add it
|
||||
* to the end of the provided chained list.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain The pointer to the start of the CRT chain to attach to.
|
||||
* When parsing the first CRT in a chain, this should point
|
||||
* to an instance of ::mbedtls_x509_crt initialized through
|
||||
@ -402,6 +406,10 @@ typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx,
|
||||
* \brief Parse a single DER formatted certificate and add it
|
||||
* to the end of the provided chained list.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain The pointer to the start of the CRT chain to attach to.
|
||||
* When parsing the first CRT in a chain, this should point
|
||||
* to an instance of ::mbedtls_x509_crt initialized through
|
||||
@ -452,6 +460,10 @@ int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
|
||||
* temporary ownership of the CRT buffer until the CRT
|
||||
* is destroyed.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain The pointer to the start of the CRT chain to attach to.
|
||||
* When parsing the first CRT in a chain, this should point
|
||||
* to an instance of ::mbedtls_x509_crt initialized through
|
||||
@ -492,6 +504,10 @@ int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
|
||||
* long as the certificates are enclosed in the PEM specific
|
||||
* '-----{BEGIN/END} CERTIFICATE-----' delimiters.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain The chain to which to add the parsed certificates.
|
||||
* \param buf The buffer holding the certificate data in PEM or DER format.
|
||||
* For certificates in PEM encoding, this may be a concatenation
|
||||
@ -516,6 +532,10 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, si
|
||||
* of failed certificates it encountered. If none complete
|
||||
* correctly, the first error is returned.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param chain points to the start of the chain
|
||||
* \param path filename to read the certificates from
|
||||
*
|
||||
|
@ -89,6 +89,10 @@ mbedtls_x509write_csr;
|
||||
*
|
||||
* \note CSR attributes (if any) are currently silently ignored.
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param csr CSR context to fill
|
||||
* \param buf buffer holding the CRL data
|
||||
* \param buflen size of the buffer
|
||||
@ -103,6 +107,10 @@ int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
|
||||
*
|
||||
* \note See notes for \c mbedtls_x509_csr_parse_der()
|
||||
*
|
||||
* \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
|
||||
* subsystem must have been initialized by calling
|
||||
* psa_crypto_init() before calling this function.
|
||||
*
|
||||
* \param csr CSR context to fill
|
||||
* \param buf buffer holding the CRL data
|
||||
* \param buflen size of the buffer
|
||||
|
1441
include/psa/crypto.h
1441
include/psa/crypto.h
File diff suppressed because it is too large
Load Diff
@ -107,4 +107,113 @@ typedef struct {
|
||||
|
||||
#define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
|
||||
|
||||
#include "mbedtls/ecdsa.h"
|
||||
|
||||
/* Context structure for the Mbed TLS interruptible sign hash implementation. */
|
||||
typedef struct {
|
||||
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
||||
defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
|
||||
mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
|
||||
|
||||
uint32_t MBEDTLS_PRIVATE(num_ops);
|
||||
|
||||
size_t MBEDTLS_PRIVATE(coordinate_bytes);
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(alg);
|
||||
mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
|
||||
uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
|
||||
size_t MBEDTLS_PRIVATE(hash_length);
|
||||
|
||||
#else
|
||||
/* Make the struct non-empty if algs not supported. */
|
||||
unsigned MBEDTLS_PRIVATE(dummy);
|
||||
|
||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
||||
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
||||
} mbedtls_psa_sign_hash_interruptible_operation_t;
|
||||
|
||||
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
||||
defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
#define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 }
|
||||
#else
|
||||
#define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
|
||||
#endif
|
||||
|
||||
/* Context structure for the Mbed TLS interruptible verify hash
|
||||
* implementation.*/
|
||||
typedef struct {
|
||||
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
||||
defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
|
||||
mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
|
||||
mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
|
||||
|
||||
uint32_t MBEDTLS_PRIVATE(num_ops);
|
||||
|
||||
uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
|
||||
size_t MBEDTLS_PRIVATE(hash_length);
|
||||
|
||||
mbedtls_mpi MBEDTLS_PRIVATE(r);
|
||||
mbedtls_mpi MBEDTLS_PRIVATE(s);
|
||||
|
||||
#else
|
||||
/* Make the struct non-empty if algs not supported. */
|
||||
unsigned MBEDTLS_PRIVATE(dummy);
|
||||
|
||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
|
||||
* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
|
||||
* defined( MBEDTLS_ECP_RESTARTABLE ) */
|
||||
|
||||
} mbedtls_psa_verify_hash_interruptible_operation_t;
|
||||
|
||||
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
|
||||
defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
#define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \
|
||||
{ 0 } }
|
||||
#else
|
||||
#define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
|
||||
#endif
|
||||
|
||||
|
||||
/* EC-JPAKE operation definitions */
|
||||
|
||||
#include "mbedtls/ecjpake.h"
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
#define MBEDTLS_PSA_BUILTIN_PAKE 1
|
||||
#endif
|
||||
|
||||
/* Note: the format for mbedtls_ecjpake_read/write function has an extra
|
||||
* length byte for each step, plus an extra 3 bytes for ECParameters in the
|
||||
* server's 2nd round. */
|
||||
#define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
|
||||
|
||||
typedef struct {
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(alg);
|
||||
|
||||
uint8_t *MBEDTLS_PRIVATE(password);
|
||||
size_t MBEDTLS_PRIVATE(password_len);
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
uint8_t MBEDTLS_PRIVATE(role);
|
||||
uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]);
|
||||
size_t MBEDTLS_PRIVATE(buffer_length);
|
||||
size_t MBEDTLS_PRIVATE(buffer_offset);
|
||||
#endif
|
||||
/* Context structure for the Mbed TLS EC-JPAKE implementation. */
|
||||
union {
|
||||
unsigned int MBEDTLS_PRIVATE(dummy);
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake);
|
||||
#endif
|
||||
} MBEDTLS_PRIVATE(ctx);
|
||||
|
||||
} mbedtls_psa_pake_operation_t;
|
||||
|
||||
#define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } }
|
||||
|
||||
#endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */
|
||||
|
@ -105,11 +105,11 @@ static inline int psa_key_handle_is_null(psa_key_handle_t handle)
|
||||
* permission to access it. Note that this specification does not
|
||||
* define any way to create such a key, but it may be possible
|
||||
* through implementation-specific means.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
@ -149,8 +149,8 @@ psa_status_t psa_open_key(mbedtls_svc_key_id_t key,
|
||||
* \p handle was a valid handle or \c 0. It is now closed.
|
||||
* \retval #PSA_ERROR_INVALID_HANDLE
|
||||
* \p handle is not a valid handle nor \c 0.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
|
@ -88,6 +88,32 @@ typedef mbedtls_psa_aead_operation_t
|
||||
|
||||
#endif /* MBEDTLS_TEST_LIBTESTDRIVER1 && LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD */
|
||||
|
||||
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
|
||||
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
|
||||
|
||||
typedef libtestdriver1_mbedtls_psa_pake_operation_t
|
||||
mbedtls_transparent_test_driver_pake_operation_t;
|
||||
typedef libtestdriver1_mbedtls_psa_pake_operation_t
|
||||
mbedtls_opaque_test_driver_pake_operation_t;
|
||||
|
||||
#define MBEDTLS_TRANSPARENT_TEST_DRIVER_PAKE_OPERATION_INIT \
|
||||
LIBTESTDRIVER1_MBEDTLS_PSA_PAKE_OPERATION_INIT
|
||||
#define MBEDTLS_OPAQUE_TEST_DRIVER_PAKE_OPERATION_INIT \
|
||||
LIBTESTDRIVER1_MBEDTLS_PSA_PAKE_OPERATION_INIT
|
||||
|
||||
#else
|
||||
typedef mbedtls_psa_pake_operation_t
|
||||
mbedtls_transparent_test_driver_pake_operation_t;
|
||||
typedef mbedtls_psa_pake_operation_t
|
||||
mbedtls_opaque_test_driver_pake_operation_t;
|
||||
|
||||
#define MBEDTLS_TRANSPARENT_TEST_DRIVER_PAKE_OPERATION_INIT \
|
||||
MBEDTLS_PSA_PAKE_OPERATION_INIT
|
||||
#define MBEDTLS_OPAQUE_TEST_DRIVER_PAKE_OPERATION_INIT \
|
||||
MBEDTLS_PSA_PAKE_OPERATION_INIT
|
||||
|
||||
#endif /* MBEDTLS_TEST_LIBTESTDRIVER1 && LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE */
|
||||
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
|
||||
/* Define the context to be used for an operation that is executed through the
|
||||
@ -114,5 +140,24 @@ typedef union {
|
||||
#endif
|
||||
} psa_driver_aead_context_t;
|
||||
|
||||
typedef union {
|
||||
unsigned dummy; /* Make sure this union is always non-empty */
|
||||
mbedtls_psa_sign_hash_interruptible_operation_t mbedtls_ctx;
|
||||
} psa_driver_sign_hash_interruptible_context_t;
|
||||
|
||||
typedef union {
|
||||
unsigned dummy; /* Make sure this union is always non-empty */
|
||||
mbedtls_psa_verify_hash_interruptible_operation_t mbedtls_ctx;
|
||||
} psa_driver_verify_hash_interruptible_context_t;
|
||||
|
||||
typedef union {
|
||||
unsigned dummy; /* Make sure this union is always non-empty */
|
||||
mbedtls_psa_pake_operation_t mbedtls_ctx;
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
mbedtls_transparent_test_driver_pake_operation_t transparent_test_driver_ctx;
|
||||
mbedtls_opaque_test_driver_pake_operation_t opaque_test_driver_ctx;
|
||||
#endif
|
||||
} psa_driver_pake_context_t;
|
||||
|
||||
#endif /* PSA_CRYPTO_DRIVER_CONTEXTS_COMPOSITES_H */
|
||||
/* End of automatically generated file. */
|
||||
|
@ -189,12 +189,12 @@ static inline void psa_clear_key_slot_number(
|
||||
* or the specified slot number is not valid.
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED
|
||||
* The caller is not authorized to register the specified key slot.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
@ -429,6 +429,10 @@ psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
|
||||
*/
|
||||
#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
|
||||
|
||||
/** PAKE operation stages. */
|
||||
#define PSA_PAKE_OPERATION_STAGE_SETUP 0
|
||||
#define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
|
||||
#define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
|
||||
|
||||
/**
|
||||
* \brief Set domain parameters for a key.
|
||||
@ -491,10 +495,10 @@ psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
|
||||
* according to \p type as described above.
|
||||
* \param data_length Size of the \p data buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
|
||||
psa_key_type_t type,
|
||||
@ -521,8 +525,8 @@ psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
|
||||
* \param[out] data_length On success, the number of bytes
|
||||
* that make up the key domain parameters data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
|
||||
*/
|
||||
psa_status_t psa_get_key_domain_parameters(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -1286,10 +1290,74 @@ static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
|
||||
* Implementation details can change in future versions without notice. */
|
||||
typedef struct psa_pake_operation_s psa_pake_operation_t;
|
||||
|
||||
/** The type of input values for PAKE operations. */
|
||||
typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
|
||||
|
||||
/** The type of computation stage for J-PAKE operations. */
|
||||
typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
|
||||
|
||||
/** Return an initial value for a PAKE operation object.
|
||||
*/
|
||||
static psa_pake_operation_t psa_pake_operation_init(void);
|
||||
|
||||
/** Get the lengths of the password in bytes from given inputs.
|
||||
*
|
||||
* \param[in] inputs Operation inputs.
|
||||
* \param[out] password_len Return buffer for password length.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* Password hasn't been set yet.
|
||||
*/
|
||||
psa_status_t psa_crypto_driver_pake_get_password_len(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
size_t *password_len);
|
||||
|
||||
/** Get the password from given inputs.
|
||||
*
|
||||
* \param[in] inputs Operation inputs.
|
||||
* \param[out] buffer Return buffer for password.
|
||||
* \param buffer_size Size of the return buffer in bytes.
|
||||
* \param[out] buffer_length Actual size of the password in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* Password hasn't been set yet.
|
||||
*/
|
||||
psa_status_t psa_crypto_driver_pake_get_password(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
|
||||
|
||||
/** Get the role from given inputs.
|
||||
*
|
||||
* \param[in] inputs Operation inputs.
|
||||
* \param[out] role Return buffer for role.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* Role hasn't been set yet.
|
||||
*/
|
||||
psa_status_t psa_crypto_driver_pake_get_role(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
psa_pake_role_t *role);
|
||||
|
||||
/** Get the cipher suite from given inputs.
|
||||
*
|
||||
* \param[in] inputs Operation inputs.
|
||||
* \param[out] cipher_suite Return buffer for role.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* Cipher_suite hasn't been set yet.
|
||||
*/
|
||||
psa_status_t psa_crypto_driver_pake_get_cipher_suite(
|
||||
const psa_crypto_driver_pake_inputs_t *inputs,
|
||||
psa_pake_cipher_suite_t *cipher_suite);
|
||||
|
||||
/** Set the session information for a password-authenticated key exchange.
|
||||
*
|
||||
* The sequence of operations to set up a password-authenticated key exchange
|
||||
@ -1352,8 +1420,8 @@ static psa_pake_operation_t psa_pake_operation_init(void);
|
||||
* compatible with the PAKE algorithm, or the hash algorithm in
|
||||
* \p cipher_suite is not supported or not compatible with the PAKE
|
||||
* algorithm and primitive.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid, or
|
||||
* the library has not been previously initialized by psa_crypto_init().
|
||||
@ -1397,11 +1465,11 @@ psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* The key type or key size of \p password is not supported with the
|
||||
* \p operation's cipher suite.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must have been set up.), or
|
||||
* the library has not been previously initialized by psa_crypto_init().
|
||||
@ -1439,9 +1507,9 @@ psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
|
||||
* suite.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* The value of \p user_id is not supported by the implementation.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid, or
|
||||
* the library has not been previously initialized by psa_crypto_init().
|
||||
@ -1480,9 +1548,9 @@ psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
|
||||
* suite.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* The algorithm doesn't associate a second identity with the session.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* Calling psa_pake_set_peer() is invalid with the \p operation's
|
||||
* algorithm, the operation state is not valid, or the library has not
|
||||
@ -1524,8 +1592,8 @@ psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
|
||||
* The \p role is not a valid PAKE role in the \p operation’s algorithm.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* The \p role for this algorithm is not supported or is not valid.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid, or
|
||||
* the library has not been previously initialized by psa_crypto_init().
|
||||
@ -1575,13 +1643,13 @@ psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
|
||||
* \p step is not compatible with the operation's algorithm.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p step is not supported with the operation's algorithm.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be active, and fully set
|
||||
* up, and this call must conform to the algorithm's requirements
|
||||
@ -1631,12 +1699,12 @@ psa_status_t psa_pake_output(psa_pake_operation_t *operation,
|
||||
* \p step p is not supported with the \p operation's algorithm, or the
|
||||
* \p input is not supported for the \p operation's algorithm, cipher
|
||||
* suite or \p step.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be active, and fully set
|
||||
* up, and this call must conform to the algorithm's requirements
|
||||
@ -1691,12 +1759,12 @@ psa_status_t psa_pake_input(psa_pake_operation_t *operation,
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* Input from a PAKE is not supported by the algorithm in the \p output
|
||||
* key derivation operation.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The PAKE operation state is not valid (it must be active, but beyond
|
||||
* that validity is specific to the algorithm), or
|
||||
@ -1728,8 +1796,8 @@ psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
@ -1826,14 +1894,8 @@ psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
|
||||
/** Returns a suitable initializer for a PAKE operation object of type
|
||||
* psa_pake_operation_t.
|
||||
*/
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
|
||||
#define PSA_PAKE_OPERATION_INIT { PSA_ALG_NONE, 0, 0, 0, 0, \
|
||||
NULL, 0, \
|
||||
PSA_PAKE_ROLE_NONE, { 0 }, 0, 0, \
|
||||
{ .dummy = 0 } }
|
||||
#else
|
||||
#define PSA_PAKE_OPERATION_INIT { PSA_ALG_NONE, 0, 0, { 0 } }
|
||||
#endif
|
||||
#define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, PSA_PAKE_OPERATION_STAGE_SETUP, \
|
||||
{ 0 }, { { 0 } } }
|
||||
|
||||
struct psa_pake_cipher_suite_s {
|
||||
psa_algorithm_t algorithm;
|
||||
@ -1904,35 +1966,90 @@ static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
#include <mbedtls/ecjpake.h>
|
||||
/* Note: the format for mbedtls_ecjpake_read/write function has an extra
|
||||
* length byte for each step, plus an extra 3 bytes for ECParameters in the
|
||||
* server's 2nd round. */
|
||||
#define MBEDTLS_PSA_PAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
|
||||
#endif
|
||||
|
||||
struct psa_pake_operation_s {
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(alg);
|
||||
unsigned int MBEDTLS_PRIVATE(state);
|
||||
unsigned int MBEDTLS_PRIVATE(sequence);
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
|
||||
unsigned int MBEDTLS_PRIVATE(input_step);
|
||||
unsigned int MBEDTLS_PRIVATE(output_step);
|
||||
struct psa_crypto_driver_pake_inputs_s {
|
||||
uint8_t *MBEDTLS_PRIVATE(password);
|
||||
size_t MBEDTLS_PRIVATE(password_len);
|
||||
psa_pake_role_t MBEDTLS_PRIVATE(role);
|
||||
uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_PAKE_BUFFER_SIZE]);
|
||||
size_t MBEDTLS_PRIVATE(buffer_length);
|
||||
size_t MBEDTLS_PRIVATE(buffer_offset);
|
||||
#endif
|
||||
psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
|
||||
psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
|
||||
};
|
||||
|
||||
typedef enum psa_jpake_step {
|
||||
PSA_PAKE_STEP_INVALID = 0,
|
||||
PSA_PAKE_STEP_X1_X2 = 1,
|
||||
PSA_PAKE_STEP_X2S = 2,
|
||||
PSA_PAKE_STEP_DERIVE = 3,
|
||||
} psa_jpake_step_t;
|
||||
|
||||
typedef enum psa_jpake_state {
|
||||
PSA_PAKE_STATE_INVALID = 0,
|
||||
PSA_PAKE_STATE_SETUP = 1,
|
||||
PSA_PAKE_STATE_READY = 2,
|
||||
PSA_PAKE_OUTPUT_X1_X2 = 3,
|
||||
PSA_PAKE_OUTPUT_X2S = 4,
|
||||
PSA_PAKE_INPUT_X1_X2 = 5,
|
||||
PSA_PAKE_INPUT_X4S = 6,
|
||||
} psa_jpake_state_t;
|
||||
|
||||
typedef enum psa_jpake_sequence {
|
||||
PSA_PAKE_SEQ_INVALID = 0,
|
||||
PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
|
||||
PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
|
||||
PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
|
||||
PSA_PAKE_X2_STEP_KEY_SHARE = 4,
|
||||
PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
|
||||
PSA_PAKE_X2_STEP_ZK_PROOF = 6,
|
||||
PSA_PAKE_SEQ_END = 7,
|
||||
} psa_jpake_sequence_t;
|
||||
|
||||
typedef enum psa_crypto_driver_pake_step {
|
||||
PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
|
||||
PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
|
||||
PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
|
||||
PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
|
||||
PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
|
||||
PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
|
||||
PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
|
||||
PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
|
||||
PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
|
||||
PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
|
||||
PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
|
||||
PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
|
||||
PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
|
||||
} psa_crypto_driver_pake_step_t;
|
||||
|
||||
|
||||
struct psa_jpake_computation_stage_s {
|
||||
psa_jpake_state_t MBEDTLS_PRIVATE(state);
|
||||
psa_jpake_sequence_t MBEDTLS_PRIVATE(sequence);
|
||||
psa_jpake_step_t MBEDTLS_PRIVATE(input_step);
|
||||
psa_jpake_step_t MBEDTLS_PRIVATE(output_step);
|
||||
};
|
||||
|
||||
struct psa_pake_operation_s {
|
||||
/** Unique ID indicating which driver got assigned to do the
|
||||
* operation. Since driver contexts are driver-specific, swapping
|
||||
* drivers halfway through the operation is not supported.
|
||||
* ID values are auto-generated in psa_crypto_driver_wrappers.h
|
||||
* ID value zero means the context is not valid or not assigned to
|
||||
* any driver (i.e. none of the driver contexts are active). */
|
||||
unsigned int MBEDTLS_PRIVATE(id);
|
||||
/* Algorithm of the PAKE operation */
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(alg);
|
||||
/* Stage of the PAKE operation: waiting for the setup, collecting inputs
|
||||
* or computing. */
|
||||
uint8_t MBEDTLS_PRIVATE(stage);
|
||||
/* Holds computation stage of the PAKE algorithms. */
|
||||
union {
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
mbedtls_ecjpake_context ecjpake;
|
||||
uint8_t MBEDTLS_PRIVATE(dummy);
|
||||
#if defined(PSA_WANT_ALG_JPAKE)
|
||||
psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
|
||||
#endif
|
||||
/* Make the union non-empty even with no supported algorithms. */
|
||||
uint8_t dummy;
|
||||
} MBEDTLS_PRIVATE(ctx);
|
||||
} MBEDTLS_PRIVATE(computation_stage);
|
||||
union {
|
||||
psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
|
||||
psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
|
||||
} MBEDTLS_PRIVATE(data);
|
||||
};
|
||||
|
||||
static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
|
||||
|
@ -385,8 +385,8 @@ typedef struct {
|
||||
* \param[in] direction Indicates whether the operation is an encrypt
|
||||
* or decrypt
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
|
||||
void *op_context,
|
||||
@ -407,7 +407,7 @@ typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_cont
|
||||
* \param[in] p_iv A buffer containing the initialization vector
|
||||
* \param[in] iv_length The size (in bytes) of the `p_iv` buffer
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
|
||||
const uint8_t *p_iv,
|
||||
@ -429,7 +429,7 @@ typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
|
||||
* \param[out] p_output_length After completion, will contain the number
|
||||
* of bytes placed in the `p_output` buffer
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
|
||||
const uint8_t *p_input,
|
||||
@ -450,7 +450,7 @@ typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
|
||||
* \param[out] p_output_length After completion, will contain the number of
|
||||
* bytes placed in the `p_output` buffer
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
|
||||
uint8_t *p_output,
|
||||
@ -485,8 +485,8 @@ typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
|
||||
* \param[in] output_size The allocated size in bytes of the `p_output`
|
||||
* buffer
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
|
||||
psa_key_slot_number_t key_slot,
|
||||
@ -554,7 +554,7 @@ typedef struct {
|
||||
* \param[out] p_signature_length On success, the number of bytes
|
||||
* that make up the returned signature value
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
|
||||
psa_key_slot_number_t key_slot,
|
||||
@ -618,7 +618,7 @@ typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv
|
||||
* \param[out] p_output_length On success, the number of bytes that make up
|
||||
* the returned output
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
|
||||
psa_key_slot_number_t key_slot,
|
||||
@ -658,7 +658,7 @@ typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *dr
|
||||
* \param[out] p_output_length On success, the number of bytes
|
||||
* that make up the returned output
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
|
||||
psa_key_slot_number_t key_slot,
|
||||
@ -904,8 +904,8 @@ typedef enum {
|
||||
* Success.
|
||||
* The core will record \c *key_slot as the key slot where the key
|
||||
* is stored and will update the persistent data in storage.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_allocate_key_t)(
|
||||
psa_drv_se_context_t *drv_context,
|
||||
@ -1043,13 +1043,13 @@ typedef psa_status_t (*psa_drv_se_destroy_key_t)(
|
||||
* \param[out] p_data_length On success, the number of bytes
|
||||
* that make up the key data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
|
||||
psa_key_slot_number_t key,
|
||||
@ -1196,7 +1196,7 @@ typedef struct {
|
||||
* \param[in] source_key The key to be used as the source material for
|
||||
* the key derivation
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
|
||||
void *op_context,
|
||||
@ -1216,7 +1216,7 @@ typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *
|
||||
* \param[in] p_collateral A buffer containing the collateral data
|
||||
* \param[in] collateral_size The size in bytes of the collateral
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
|
||||
uint32_t collateral_id,
|
||||
@ -1231,7 +1231,7 @@ typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
|
||||
* \param[in] dest_key The slot where the generated key material
|
||||
* should be placed
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
|
||||
psa_key_slot_number_t dest_key);
|
||||
@ -1245,7 +1245,7 @@ typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
|
||||
* \param[out] p_output_length Upon success, contains the number of bytes of
|
||||
* key material placed in `p_output`
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
|
||||
uint8_t *p_output,
|
||||
|
@ -491,6 +491,66 @@ static inline size_t psa_get_key_bits(
|
||||
return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief The context for PSA interruptible hash signing.
|
||||
*/
|
||||
struct psa_sign_hash_interruptible_operation_s {
|
||||
/** Unique ID indicating which driver got assigned to do the
|
||||
* operation. Since driver contexts are driver-specific, swapping
|
||||
* drivers halfway through the operation is not supported.
|
||||
* ID values are auto-generated in psa_crypto_driver_wrappers.h
|
||||
* ID value zero means the context is not valid or not assigned to
|
||||
* any driver (i.e. none of the driver contexts are active). */
|
||||
unsigned int MBEDTLS_PRIVATE(id);
|
||||
|
||||
psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
|
||||
|
||||
unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
|
||||
|
||||
uint32_t MBEDTLS_PRIVATE(num_ops);
|
||||
};
|
||||
|
||||
#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
|
||||
|
||||
static inline struct psa_sign_hash_interruptible_operation_s
|
||||
psa_sign_hash_interruptible_operation_init(void)
|
||||
{
|
||||
const struct psa_sign_hash_interruptible_operation_s v =
|
||||
PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief The context for PSA interruptible hash verification.
|
||||
*/
|
||||
struct psa_verify_hash_interruptible_operation_s {
|
||||
/** Unique ID indicating which driver got assigned to do the
|
||||
* operation. Since driver contexts are driver-specific, swapping
|
||||
* drivers halfway through the operation is not supported.
|
||||
* ID values are auto-generated in psa_crypto_driver_wrappers.h
|
||||
* ID value zero means the context is not valid or not assigned to
|
||||
* any driver (i.e. none of the driver contexts are active). */
|
||||
unsigned int MBEDTLS_PRIVATE(id);
|
||||
|
||||
psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
|
||||
|
||||
unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
|
||||
|
||||
uint32_t MBEDTLS_PRIVATE(num_ops);
|
||||
};
|
||||
|
||||
#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
|
||||
|
||||
static inline struct psa_verify_hash_interruptible_operation_s
|
||||
psa_verify_hash_interruptible_operation_init(void)
|
||||
{
|
||||
const struct psa_verify_hash_interruptible_operation_s v =
|
||||
PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -32,16 +32,17 @@
|
||||
|
||||
#ifndef PSA_CRYPTO_TYPES_H
|
||||
#define PSA_CRYPTO_TYPES_H
|
||||
|
||||
/* Make sure the Mbed TLS configuration is visible. */
|
||||
#include "mbedtls/build_info.h"
|
||||
/* Define the MBEDTLS_PRIVATE macro. */
|
||||
#include "mbedtls/private_access.h"
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
|
||||
#include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
|
||||
#else
|
||||
#include "crypto_platform.h"
|
||||
|
||||
/* If MBEDTLS_PSA_CRYPTO_C is defined, make sure MBEDTLS_PSA_CRYPTO_CLIENT
|
||||
* is defined as well to include all PSA code.
|
||||
*/
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#define MBEDTLS_PSA_CRYPTO_CLIENT
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -335,6 +335,13 @@
|
||||
*/
|
||||
#define PSA_ERROR_DATA_INVALID ((psa_status_t)-153)
|
||||
|
||||
/** The function that returns this status is defined as interruptible and
|
||||
* still has work to do, thus the user should call the function again with the
|
||||
* same operation context until it either returns #PSA_SUCCESS or any other
|
||||
* error. This is not an error per se, more a notification of status.
|
||||
*/
|
||||
#define PSA_OPERATION_INCOMPLETE ((psa_status_t)-248)
|
||||
|
||||
/* *INDENT-ON* */
|
||||
|
||||
/**@}*/
|
||||
@ -2739,4 +2746,18 @@ static inline int mbedtls_svc_key_id_is_null(mbedtls_svc_key_id_t key)
|
||||
|
||||
/**@}*/
|
||||
|
||||
/**@}*/
|
||||
|
||||
/** \defgroup interruptible Interruptible operations
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Maximum value for use with \c psa_interruptible_set_max_ops() to determine
|
||||
* the maximum number of ops allowed to be executed by an interruptible
|
||||
* function in a single call.
|
||||
*/
|
||||
#define PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED UINT32_MAX
|
||||
|
||||
/**@}*/
|
||||
|
||||
#endif /* PSA_CRYPTO_VALUES_H */
|
||||
|
@ -13,6 +13,7 @@ endif()
|
||||
set(src_crypto
|
||||
aes.c
|
||||
aesni.c
|
||||
aesce.c
|
||||
aria.c
|
||||
asn1parse.c
|
||||
asn1write.c
|
||||
@ -76,6 +77,7 @@ set(src_crypto
|
||||
psa_crypto_slot_management.c
|
||||
psa_crypto_storage.c
|
||||
psa_its_file.c
|
||||
psa_util.c
|
||||
ripemd160.c
|
||||
rsa.c
|
||||
rsa_alt_helpers.c
|
||||
|
@ -78,6 +78,7 @@ endif
|
||||
OBJS_CRYPTO= \
|
||||
aes.o \
|
||||
aesni.o \
|
||||
aesce.o \
|
||||
aria.o \
|
||||
asn1parse.o \
|
||||
asn1write.o \
|
||||
@ -141,6 +142,7 @@ OBJS_CRYPTO= \
|
||||
psa_crypto_slot_management.o \
|
||||
psa_crypto_storage.o \
|
||||
psa_its_file.o \
|
||||
psa_util.o \
|
||||
ripemd160.o \
|
||||
rsa.o \
|
||||
rsa_alt_helpers.o \
|
||||
|
@ -39,6 +39,9 @@
|
||||
#if defined(MBEDTLS_AESNI_C)
|
||||
#include "aesni.h"
|
||||
#endif
|
||||
#if defined(MBEDTLS_AESCE_C)
|
||||
#include "aesce.h"
|
||||
#endif
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
@ -544,6 +547,12 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
if (mbedtls_aesce_has_support()) {
|
||||
return mbedtls_aesce_setkey_enc((unsigned char *) RK, key, keybits);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i = 0; i < (keybits >> 5); i++) {
|
||||
RK[i] = MBEDTLS_GET_UINT32_LE(key, i << 2);
|
||||
}
|
||||
@ -652,6 +661,16 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
if (mbedtls_aesce_has_support()) {
|
||||
mbedtls_aesce_inverse_key(
|
||||
(unsigned char *) RK,
|
||||
(const unsigned char *) (cty.buf + cty.rk_offset),
|
||||
ctx->nr);
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
SK = cty.buf + cty.rk_offset + cty.nr * 4;
|
||||
|
||||
*RK++ = *SK++;
|
||||
@ -944,6 +963,12 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
if (mbedtls_aesce_has_support()) {
|
||||
return mbedtls_aesce_crypt_ecb(ctx, mode, input, output);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
|
||||
if (aes_padlock_ace > 0) {
|
||||
if (mbedtls_padlock_xcryptecb(ctx, mode, input, output) == 0) {
|
||||
|
257
library/aesce.c
Normal file
257
library/aesce.c
Normal file
@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Arm64 crypto extension support functions
|
||||
*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C)
|
||||
|
||||
#include "aesce.h"
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ARM64)
|
||||
|
||||
#if defined(__clang__)
|
||||
# if __clang_major__ < 4
|
||||
# error "A more recent Clang is required for MBEDTLS_AESCE_C"
|
||||
# endif
|
||||
#elif defined(__GNUC__)
|
||||
# if __GNUC__ < 6
|
||||
# error "A more recent GCC is required for MBEDTLS_AESCE_C"
|
||||
# endif
|
||||
#else
|
||||
# error "Only GCC and Clang supported for MBEDTLS_AESCE_C"
|
||||
#endif
|
||||
|
||||
#if !defined(__ARM_FEATURE_CRYPTO)
|
||||
# error "`crypto` feature modifier MUST be enabled for MBEDTLS_AESCE_C."
|
||||
# error "Typical option for GCC and Clang is `-march=armv8-a+crypto`."
|
||||
#endif /* !__ARM_FEATURE_CRYPTO */
|
||||
|
||||
#include <arm_neon.h>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <asm/hwcap.h>
|
||||
#include <sys/auxv.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* AES instruction support detection routine
|
||||
*/
|
||||
int mbedtls_aesce_has_support(void)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
unsigned long auxval = getauxval(AT_HWCAP);
|
||||
return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
|
||||
(HWCAP_ASIMD | HWCAP_AES);
|
||||
#else
|
||||
/* Assume AES instructions are supported. */
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint8x16_t aesce_encrypt_block(uint8x16_t block,
|
||||
unsigned char *keys,
|
||||
int rounds)
|
||||
{
|
||||
for (int i = 0; i < rounds - 1; i++) {
|
||||
/* AES AddRoundKey, SubBytes, ShiftRows (in this order).
|
||||
* AddRoundKey adds the round key for the previous round. */
|
||||
block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
|
||||
/* AES mix columns */
|
||||
block = vaesmcq_u8(block);
|
||||
}
|
||||
|
||||
/* AES AddRoundKey for the previous round.
|
||||
* SubBytes, ShiftRows for the final round. */
|
||||
block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
|
||||
|
||||
/* Final round: no MixColumns */
|
||||
|
||||
/* Final AddRoundKey */
|
||||
block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
static uint8x16_t aesce_decrypt_block(uint8x16_t block,
|
||||
unsigned char *keys,
|
||||
int rounds)
|
||||
{
|
||||
|
||||
for (int i = 0; i < rounds - 1; i++) {
|
||||
/* AES AddRoundKey, SubBytes, ShiftRows */
|
||||
block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
|
||||
/* AES inverse MixColumns for the next round.
|
||||
*
|
||||
* This means that we switch the order of the inverse AddRoundKey and
|
||||
* inverse MixColumns operations. We have to do this as AddRoundKey is
|
||||
* done in an atomic instruction together with the inverses of SubBytes
|
||||
* and ShiftRows.
|
||||
*
|
||||
* It works because MixColumns is a linear operation over GF(2^8) and
|
||||
* AddRoundKey is an exclusive or, which is equivalent to addition over
|
||||
* GF(2^8). (The inverse of MixColumns needs to be applied to the
|
||||
* affected round keys separately which has been done when the
|
||||
* decryption round keys were calculated.) */
|
||||
block = vaesimcq_u8(block);
|
||||
}
|
||||
|
||||
/* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
|
||||
* last full round. */
|
||||
block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
|
||||
|
||||
/* Inverse AddRoundKey for inverting the initial round key addition. */
|
||||
block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
/*
|
||||
* AES-ECB block en(de)cryption
|
||||
*/
|
||||
int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
uint8x16_t block = vld1q_u8(&input[0]);
|
||||
unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
|
||||
|
||||
if (mode == MBEDTLS_AES_ENCRYPT) {
|
||||
block = aesce_encrypt_block(block, keys, ctx->nr);
|
||||
} else {
|
||||
block = aesce_decrypt_block(block, keys, ctx->nr);
|
||||
}
|
||||
vst1q_u8(&output[0], block);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute decryption round keys from encryption round keys
|
||||
*/
|
||||
void mbedtls_aesce_inverse_key(unsigned char *invkey,
|
||||
const unsigned char *fwdkey,
|
||||
int nr)
|
||||
{
|
||||
int i, j;
|
||||
j = nr;
|
||||
vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
|
||||
for (i = 1, j--; j > 0; i++, j--) {
|
||||
vst1q_u8(invkey + i * 16,
|
||||
vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
|
||||
}
|
||||
vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
|
||||
|
||||
}
|
||||
|
||||
static inline uint32_t aes_rot_word(uint32_t word)
|
||||
{
|
||||
return (word << (32 - 8)) | (word >> 8);
|
||||
}
|
||||
|
||||
static inline uint32_t aes_sub_word(uint32_t in)
|
||||
{
|
||||
uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
|
||||
uint8x16_t zero = vdupq_n_u8(0);
|
||||
|
||||
/* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
|
||||
* the correct result as ShiftRows doesn't change the first row. */
|
||||
v = vaeseq_u8(zero, v);
|
||||
return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Key expansion function
|
||||
*/
|
||||
static void aesce_setkey_enc(unsigned char *rk,
|
||||
const unsigned char *key,
|
||||
const size_t key_bit_length)
|
||||
{
|
||||
static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
|
||||
0x20, 0x40, 0x80, 0x1b, 0x36 };
|
||||
/* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
|
||||
* - Section 5, Nr = Nk + 6
|
||||
* - Section 5.2, the key expansion size is Nb*(Nr+1)
|
||||
*/
|
||||
const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */
|
||||
const size_t round_key_len_in_words = 4; /* Nb */
|
||||
const size_t round_keys_needed = key_len_in_words + 6; /* Nr */
|
||||
const size_t key_expansion_size_in_words =
|
||||
round_key_len_in_words * (round_keys_needed + 1); /* Nb*(Nr+1) */
|
||||
const uint32_t *rko_end = (uint32_t *) rk + key_expansion_size_in_words;
|
||||
|
||||
memcpy(rk, key, key_len_in_words * 4);
|
||||
|
||||
for (uint32_t *rki = (uint32_t *) rk;
|
||||
rki + key_len_in_words < rko_end;
|
||||
rki += key_len_in_words) {
|
||||
|
||||
size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
|
||||
uint32_t *rko;
|
||||
rko = rki + key_len_in_words;
|
||||
rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
|
||||
rko[0] ^= rcon[iteration] ^ rki[0];
|
||||
rko[1] = rko[0] ^ rki[1];
|
||||
rko[2] = rko[1] ^ rki[2];
|
||||
rko[3] = rko[2] ^ rki[3];
|
||||
if (rko + key_len_in_words > rko_end) {
|
||||
/* Do not write overflow words.*/
|
||||
continue;
|
||||
}
|
||||
switch (key_bit_length) {
|
||||
case 128:
|
||||
break;
|
||||
case 192:
|
||||
rko[4] = rko[3] ^ rki[4];
|
||||
rko[5] = rko[4] ^ rki[5];
|
||||
break;
|
||||
case 256:
|
||||
rko[4] = aes_sub_word(rko[3]) ^ rki[4];
|
||||
rko[5] = rko[4] ^ rki[5];
|
||||
rko[6] = rko[5] ^ rki[6];
|
||||
rko[7] = rko[6] ^ rki[7];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Key expansion, wrapper
|
||||
*/
|
||||
int mbedtls_aesce_setkey_enc(unsigned char *rk,
|
||||
const unsigned char *key,
|
||||
size_t bits)
|
||||
{
|
||||
switch (bits) {
|
||||
case 128:
|
||||
case 192:
|
||||
case 256:
|
||||
aesce_setkey_enc(rk, key, bits);
|
||||
break;
|
||||
default:
|
||||
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_HAVE_ARM64 */
|
||||
|
||||
#endif /* MBEDTLS_AESCE_C */
|
98
library/aesce.h
Normal file
98
library/aesce.h
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* \file aesce.h
|
||||
*
|
||||
* \brief AES-CE for hardware AES acceleration on ARMv8 processors with crypto
|
||||
* extension.
|
||||
*
|
||||
* \warning These functions are only for internal use by other library
|
||||
* functions; you must not call them directly.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBEDTLS_AESCE_H
|
||||
#define MBEDTLS_AESCE_H
|
||||
|
||||
#include "mbedtls/build_info.h"
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
|
||||
defined(__aarch64__) && !defined(MBEDTLS_HAVE_ARM64)
|
||||
#define MBEDTLS_HAVE_ARM64
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ARM64)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Internal function to detect the crypto extension in CPUs.
|
||||
*
|
||||
* \return 1 if CPU has support for the feature, 0 otherwise
|
||||
*/
|
||||
int mbedtls_aesce_has_support(void);
|
||||
|
||||
/**
|
||||
* \brief Internal AES-ECB block encryption and decryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 on success (cannot fail)
|
||||
*/
|
||||
int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief Internal round key inversion. This function computes
|
||||
* decryption round keys from the encryption round keys.
|
||||
*
|
||||
* \param invkey Round keys for the equivalent inverse cipher
|
||||
* \param fwdkey Original round keys (for encryption)
|
||||
* \param nr Number of rounds (that is, number of round keys minus one)
|
||||
*/
|
||||
void mbedtls_aesce_inverse_key(unsigned char *invkey,
|
||||
const unsigned char *fwdkey,
|
||||
int nr);
|
||||
|
||||
/**
|
||||
* \brief Internal key expansion for encryption
|
||||
*
|
||||
* \param rk Destination buffer where the round keys are written
|
||||
* \param key Encryption key
|
||||
* \param bits Key size in bits (must be 128, 192 or 256)
|
||||
*
|
||||
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int mbedtls_aesce_setkey_enc(unsigned char *rk,
|
||||
const unsigned char *key,
|
||||
size_t bits);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_HAVE_ARM64 */
|
||||
|
||||
#endif /* MBEDTLS_AESCE_H */
|
@ -130,7 +130,7 @@ inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
|
||||
* byte from x, where byte 0 is the least significant byte.
|
||||
*/
|
||||
#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
|
||||
#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
|
||||
#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
|
||||
#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
|
||||
#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
|
||||
#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
|
||||
@ -155,13 +155,13 @@ inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
|
||||
* Detect Clang built-in byteswap routines
|
||||
*/
|
||||
#if defined(__clang__) && defined(__has_builtin)
|
||||
#if __has_builtin(__builtin_bswap16)
|
||||
#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
|
||||
#define MBEDTLS_BSWAP16 __builtin_bswap16
|
||||
#endif /* __has_builtin(__builtin_bswap16) */
|
||||
#if __has_builtin(__builtin_bswap32)
|
||||
#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
|
||||
#define MBEDTLS_BSWAP32 __builtin_bswap32
|
||||
#endif /* __has_builtin(__builtin_bswap32) */
|
||||
#if __has_builtin(__builtin_bswap64)
|
||||
#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
|
||||
#define MBEDTLS_BSWAP64 __builtin_bswap64
|
||||
#endif /* __has_builtin(__builtin_bswap64) */
|
||||
#endif /* defined(__clang__) && defined(__has_builtin) */
|
||||
@ -170,13 +170,19 @@ inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
|
||||
* Detect MSVC built-in byteswap routines
|
||||
*/
|
||||
#if defined(_MSC_VER)
|
||||
#if !defined(MBEDTLS_BSWAP16)
|
||||
#define MBEDTLS_BSWAP16 _byteswap_ushort
|
||||
#endif
|
||||
#if !defined(MBEDTLS_BSWAP32)
|
||||
#define MBEDTLS_BSWAP32 _byteswap_ulong
|
||||
#endif
|
||||
#if !defined(MBEDTLS_BSWAP64)
|
||||
#define MBEDTLS_BSWAP64 _byteswap_uint64
|
||||
#endif
|
||||
#endif /* defined(_MSC_VER) */
|
||||
|
||||
/* Detect armcc built-in byteswap routine */
|
||||
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000)
|
||||
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
|
||||
#define MBEDTLS_BSWAP32 __rev
|
||||
#endif
|
||||
|
||||
@ -211,14 +217,14 @@ static inline uint32_t mbedtls_bswap32(uint32_t x)
|
||||
static inline uint64_t mbedtls_bswap64(uint64_t x)
|
||||
{
|
||||
return
|
||||
(x & 0x00000000000000ff) << 56 |
|
||||
(x & 0x000000000000ff00) << 40 |
|
||||
(x & 0x0000000000ff0000) << 24 |
|
||||
(x & 0x00000000ff000000) << 8 |
|
||||
(x & 0x000000ff00000000) >> 8 |
|
||||
(x & 0x0000ff0000000000) >> 24 |
|
||||
(x & 0x00ff000000000000) >> 40 |
|
||||
(x & 0xff00000000000000) >> 56;
|
||||
(x & 0x00000000000000ffULL) << 56 |
|
||||
(x & 0x000000000000ff00ULL) << 40 |
|
||||
(x & 0x0000000000ff0000ULL) << 24 |
|
||||
(x & 0x00000000ff000000ULL) << 8 |
|
||||
(x & 0x000000ff00000000ULL) >> 8 |
|
||||
(x & 0x0000ff0000000000ULL) >> 24 |
|
||||
(x & 0x00ff000000000000ULL) >> 40 |
|
||||
(x & 0xff00000000000000ULL) >> 56;
|
||||
}
|
||||
#define MBEDTLS_BSWAP64 mbedtls_bswap64
|
||||
#endif /* !defined(MBEDTLS_BSWAP64) */
|
||||
@ -239,8 +245,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the four bytes to build the 32 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT32_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT32_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? mbedtls_get_unaligned_uint32((data) + (offset)) \
|
||||
: MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
|
||||
)
|
||||
@ -254,11 +260,11 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the most significant
|
||||
* byte of the 32 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
|
||||
mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
@ -275,8 +281,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the four bytes to build the 32 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT32_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT32_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
|
||||
: mbedtls_get_unaligned_uint32((data) + (offset)) \
|
||||
)
|
||||
@ -291,15 +297,15 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the least significant
|
||||
* byte of the 32 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
|
||||
mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
|
||||
} \
|
||||
}
|
||||
|
||||
@ -312,8 +318,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the two bytes to build the 16 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT16_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT16_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
|
||||
: mbedtls_get_unaligned_uint16((data) + (offset)) \
|
||||
)
|
||||
@ -327,15 +333,15 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the least significant
|
||||
* byte of the 16 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
|
||||
mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
|
||||
} \
|
||||
}
|
||||
|
||||
@ -348,8 +354,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the two bytes to build the 16 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT16_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT16_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? mbedtls_get_unaligned_uint16((data) + (offset)) \
|
||||
: MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
|
||||
)
|
||||
@ -363,11 +369,11 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the most significant
|
||||
* byte of the 16 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
|
||||
mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
@ -384,11 +390,11 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the three bytes to build the 24 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT24_BE(data, offset) \
|
||||
( \
|
||||
((uint32_t) (data)[(offset)] << 16) \
|
||||
| ((uint32_t) (data)[(offset) + 1] << 8) \
|
||||
| ((uint32_t) (data)[(offset) + 2]) \
|
||||
#define MBEDTLS_GET_UINT24_BE(data, offset) \
|
||||
( \
|
||||
((uint32_t) (data)[(offset)] << 16) \
|
||||
| ((uint32_t) (data)[(offset) + 1] << 8) \
|
||||
| ((uint32_t) (data)[(offset) + 2]) \
|
||||
)
|
||||
|
||||
/**
|
||||
@ -401,8 +407,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the 24 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
|
||||
{ \
|
||||
(data)[(offset)] = MBEDTLS_BYTE_2(n); \
|
||||
{ \
|
||||
(data)[(offset)] = MBEDTLS_BYTE_2(n); \
|
||||
(data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
|
||||
(data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
|
||||
}
|
||||
@ -416,9 +422,9 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the three bytes to build the 24 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT24_LE(data, offset) \
|
||||
( \
|
||||
((uint32_t) (data)[(offset)]) \
|
||||
#define MBEDTLS_GET_UINT24_LE(data, offset) \
|
||||
( \
|
||||
((uint32_t) (data)[(offset)]) \
|
||||
| ((uint32_t) (data)[(offset) + 1] << 8) \
|
||||
| ((uint32_t) (data)[(offset) + 2] << 16) \
|
||||
)
|
||||
@ -433,8 +439,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the 24 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
|
||||
{ \
|
||||
(data)[(offset)] = MBEDTLS_BYTE_0(n); \
|
||||
{ \
|
||||
(data)[(offset)] = MBEDTLS_BYTE_0(n); \
|
||||
(data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
|
||||
(data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
|
||||
}
|
||||
@ -448,8 +454,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the eight bytes to build the 64 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT64_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT64_BE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? mbedtls_get_unaligned_uint64((data) + (offset)) \
|
||||
: MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
|
||||
)
|
||||
@ -463,11 +469,11 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the most significant
|
||||
* byte of the 64 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
|
||||
mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
@ -484,8 +490,8 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* byte of the eight bytes to build the 64 bits unsigned
|
||||
* integer from.
|
||||
*/
|
||||
#define MBEDTLS_GET_UINT64_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
#define MBEDTLS_GET_UINT64_LE(data, offset) \
|
||||
((MBEDTLS_IS_BIG_ENDIAN) \
|
||||
? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
|
||||
: mbedtls_get_unaligned_uint64((data) + (offset)) \
|
||||
)
|
||||
@ -499,15 +505,15 @@ static const uint16_t mbedtls_byte_order_detector = { 0x100 };
|
||||
* \param offset Offset from \p data where to put the least significant
|
||||
* byte of the 64 bits unsigned integer \p n.
|
||||
*/
|
||||
#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
|
||||
#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
|
||||
{ \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
if (MBEDTLS_IS_BIG_ENDIAN) \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
|
||||
mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
@ -663,7 +663,7 @@ static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
|
||||
*
|
||||
* \p X may be aliased to \p A, but may not otherwise overlap it.
|
||||
*
|
||||
* \p X may not alias \p N (it is in canonical form, so must be stricly less
|
||||
* \p X may not alias \p N (it is in canonical form, so must be strictly less
|
||||
* than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
|
||||
* required in practice.)
|
||||
*
|
||||
@ -702,7 +702,7 @@ void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
|
||||
*
|
||||
* \p X may be aliased to \p A, but may not otherwise overlap it.
|
||||
*
|
||||
* \p X may not alias \p N (it is in canonical form, so must be stricly less
|
||||
* \p X may not alias \p N (it is in canonical form, so must be strictly less
|
||||
* than \p N).
|
||||
*
|
||||
* This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
|
||||
|
@ -46,6 +46,11 @@
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
|
||||
@ -620,7 +625,7 @@ cleanup:
|
||||
|
||||
psa_hash_abort(&operation);
|
||||
psa_hash_abort(&aux_operation);
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
#undef MAX_HASH_BLOCK_LENGTH
|
||||
|
@ -239,13 +239,13 @@ cleanup:
|
||||
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
|
||||
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
|
||||
*/
|
||||
static int ecdsa_sign_restartable(mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
{
|
||||
int ret, key_tries, sign_tries;
|
||||
int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
|
||||
@ -394,8 +394,8 @@ int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
/* Use the same RNG for both blinding and ephemeral key generation */
|
||||
return ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
||||
f_rng, p_rng, f_rng, p_rng, NULL);
|
||||
return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
||||
f_rng, p_rng, f_rng, p_rng, NULL);
|
||||
}
|
||||
#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
|
||||
|
||||
@ -406,13 +406,13 @@ int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
* note: The f_rng_blind parameter must not be NULL.
|
||||
*
|
||||
*/
|
||||
static int ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
mbedtls_md_type_t md_alg,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
mbedtls_md_type_t md_alg,
|
||||
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
||||
void *p_rng_blind,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_hmac_drbg_context rng_ctx;
|
||||
@ -462,9 +462,9 @@ sign:
|
||||
ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen,
|
||||
mbedtls_hmac_drbg_random, p_rng);
|
||||
#else
|
||||
ret = ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
||||
mbedtls_hmac_drbg_random, p_rng,
|
||||
f_rng_blind, p_rng_blind, rs_ctx);
|
||||
ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
||||
mbedtls_hmac_drbg_random, p_rng,
|
||||
f_rng_blind, p_rng_blind, rs_ctx);
|
||||
#endif /* MBEDTLS_ECDSA_SIGN_ALT */
|
||||
|
||||
cleanup:
|
||||
@ -487,8 +487,8 @@ int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
|
||||
size_t),
|
||||
void *p_rng_blind)
|
||||
{
|
||||
return ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
|
||||
f_rng_blind, p_rng_blind, NULL);
|
||||
return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
|
||||
f_rng_blind, p_rng_blind, NULL);
|
||||
}
|
||||
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
|
||||
@ -497,11 +497,12 @@ int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
|
||||
* Verify ECDSA signature of hashed message (SEC1 4.1.4)
|
||||
* Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
|
||||
*/
|
||||
static int ecdsa_verify_restartable(mbedtls_ecp_group *grp,
|
||||
const unsigned char *buf, size_t blen,
|
||||
const mbedtls_ecp_point *Q,
|
||||
const mbedtls_mpi *r, const mbedtls_mpi *s,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
|
||||
const unsigned char *buf, size_t blen,
|
||||
const mbedtls_ecp_point *Q,
|
||||
const mbedtls_mpi *r,
|
||||
const mbedtls_mpi *s,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_mpi e, s_inv, u1, u2;
|
||||
@ -610,7 +611,7 @@ int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
|
||||
const mbedtls_mpi *r,
|
||||
const mbedtls_mpi *s)
|
||||
{
|
||||
return ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
|
||||
return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
|
||||
}
|
||||
#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
|
||||
|
||||
@ -665,9 +666,9 @@ int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
|
||||
mbedtls_mpi_init(&s);
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||
MBEDTLS_MPI_CHK(ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
|
||||
hash, hlen, md_alg, f_rng,
|
||||
p_rng, rs_ctx));
|
||||
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
|
||||
hash, hlen, md_alg, f_rng,
|
||||
p_rng, rs_ctx));
|
||||
#else
|
||||
(void) md_alg;
|
||||
|
||||
@ -678,9 +679,9 @@ int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
|
||||
hash, hlen, f_rng, p_rng));
|
||||
#else
|
||||
/* Use the same RNG for both blinding and ephemeral key generation */
|
||||
MBEDTLS_MPI_CHK(ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
|
||||
hash, hlen, f_rng, p_rng, f_rng,
|
||||
p_rng, rs_ctx));
|
||||
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
|
||||
hash, hlen, f_rng, p_rng, f_rng,
|
||||
p_rng, rs_ctx));
|
||||
#endif /* MBEDTLS_ECDSA_SIGN_ALT */
|
||||
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
||||
|
||||
@ -760,8 +761,8 @@ int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
if ((ret = ecdsa_verify_restartable(&ctx->grp, hash, hlen,
|
||||
&ctx->Q, &r, &s, rs_ctx)) != 0) {
|
||||
if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
|
||||
&ctx->Q, &r, &s, rs_ctx)) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
|
||||
|
@ -35,6 +35,11 @@
|
||||
#if !defined(MBEDTLS_MD_C)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#if !defined(MBEDTLS_ECJPAKE_ALT)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif /* !MBEDTLS_ECJPAKE_ALT */
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
|
||||
#include "hash_info.h"
|
||||
@ -72,7 +77,7 @@ static int mbedtls_ecjpake_compute_hash(mbedtls_md_type_t md_type,
|
||||
|
||||
status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
}
|
||||
|
||||
|
@ -4575,6 +4575,8 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
||||
static int ecp_mod_p224(mbedtls_mpi *);
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
||||
static int ecp_mod_p256(mbedtls_mpi *);
|
||||
@ -4584,6 +4586,8 @@ static int ecp_mod_p384(mbedtls_mpi *);
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
||||
static int ecp_mod_p521(mbedtls_mpi *);
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *N_p, size_t N_n);
|
||||
#endif
|
||||
|
||||
#define NIST_MODP(P) grp->modp = ecp_mod_ ## P;
|
||||
@ -4949,6 +4953,173 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn)
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
|
||||
defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
||||
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
||||
|
||||
/*
|
||||
* The reader is advised to first understand ecp_mod_p192() since the same
|
||||
* general structure is used here, but with additional complications:
|
||||
* (1) chunks of 32 bits, and (2) subtractions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* For these primes, we need to handle data in chunks of 32 bits.
|
||||
* This makes it more complicated if we use 64 bits limbs in MPI,
|
||||
* which prevents us from using a uniform access method as for p192.
|
||||
*
|
||||
* So, we define a mini abstraction layer to access 32 bit chunks,
|
||||
* load them in 'cur' for work, and store them back from 'cur' when done.
|
||||
*
|
||||
* While at it, also define the size of N in terms of 32-bit chunks.
|
||||
*/
|
||||
#define LOAD32 cur = A(i);
|
||||
|
||||
#if defined(MBEDTLS_HAVE_INT32) /* 32 bit */
|
||||
|
||||
#define MAX32 X_limbs
|
||||
#define A(j) X[j]
|
||||
#define STORE32 X[i] = (mbedtls_mpi_uint) cur;
|
||||
#define STORE0 X[i] = 0;
|
||||
|
||||
#else /* 64 bit */
|
||||
|
||||
#define MAX32 X_limbs * 2
|
||||
#define A(j) \
|
||||
(j) % 2 ? \
|
||||
(uint32_t) (X[(j) / 2] >> 32) : \
|
||||
(uint32_t) (X[(j) / 2])
|
||||
#define STORE32 \
|
||||
if (i % 2) { \
|
||||
X[i/2] &= 0x00000000FFFFFFFF; \
|
||||
X[i/2] |= (uint64_t) (cur) << 32; \
|
||||
} else { \
|
||||
X[i/2] &= 0xFFFFFFFF00000000; \
|
||||
X[i/2] |= (uint32_t) cur; \
|
||||
}
|
||||
|
||||
#define STORE0 \
|
||||
if (i % 2) { \
|
||||
X[i/2] &= 0x00000000FFFFFFFF; \
|
||||
} else { \
|
||||
X[i/2] &= 0xFFFFFFFF00000000; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static inline int8_t extract_carry(int64_t cur)
|
||||
{
|
||||
return (int8_t) (cur >> 32);
|
||||
}
|
||||
|
||||
#define ADD(j) cur += A(j)
|
||||
#define SUB(j) cur -= A(j)
|
||||
|
||||
#define ADD_CARRY(cc) cur += (cc)
|
||||
#define SUB_CARRY(cc) cur -= (cc)
|
||||
|
||||
#define ADD_LAST ADD_CARRY(last_c)
|
||||
#define SUB_LAST SUB_CARRY(last_c)
|
||||
|
||||
/*
|
||||
* Helpers for the main 'loop'
|
||||
*/
|
||||
#define INIT(b) \
|
||||
int8_t c = 0, last_c; \
|
||||
int64_t cur; \
|
||||
size_t i = 0; \
|
||||
LOAD32;
|
||||
|
||||
#define NEXT \
|
||||
c = extract_carry(cur); \
|
||||
STORE32; i++; LOAD32; \
|
||||
ADD_CARRY(c);
|
||||
|
||||
#define RESET \
|
||||
c = extract_carry(cur); \
|
||||
last_c = c; \
|
||||
STORE32; i = 0; LOAD32; \
|
||||
c = 0; \
|
||||
|
||||
#define LAST \
|
||||
c = extract_carry(cur); \
|
||||
STORE32; i++; \
|
||||
if (c != 0) \
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; \
|
||||
while (i < MAX32) { STORE0; i++; }
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
||||
|
||||
/*
|
||||
* Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
|
||||
*/
|
||||
static int ecp_mod_p224(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * 224 / biL;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p224_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
if (X_limbs != 2 * 224 / biL) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
INIT(224);
|
||||
|
||||
SUB(7); SUB(11); NEXT; // A0 += -A7 - A11
|
||||
SUB(8); SUB(12); NEXT; // A1 += -A8 - A12
|
||||
SUB(9); SUB(13); NEXT; // A2 += -A9 - A13
|
||||
SUB(10); ADD(7); ADD(11); NEXT; // A3 += -A10 + A7 + A11
|
||||
SUB(11); ADD(8); ADD(12); NEXT; // A4 += -A11 + A8 + A12
|
||||
SUB(12); ADD(9); ADD(13); NEXT; // A5 += -A12 + A9 + A13
|
||||
SUB(13); ADD(10); // A6 += -A13 + A10
|
||||
|
||||
RESET;
|
||||
|
||||
/* Use 2^224 = P + 2^96 - 1 to modulo reduce the final carry */
|
||||
SUB_LAST; NEXT; // A0 -= last_c
|
||||
; NEXT; // A1
|
||||
; NEXT; // A2
|
||||
ADD_LAST; NEXT; // A3 += last_c
|
||||
; NEXT; // A4
|
||||
; NEXT; // A5
|
||||
// A6
|
||||
|
||||
/* The carry reduction cannot generate a carry
|
||||
* (see commit 73e8553 for details)*/
|
||||
|
||||
LAST;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
|
||||
|
||||
#undef LOAD32
|
||||
#undef MAX32
|
||||
#undef A
|
||||
#undef STORE32
|
||||
#undef STORE0
|
||||
#undef ADD
|
||||
#undef SUB
|
||||
#undef ADD_CARRY
|
||||
#undef SUB_CARRY
|
||||
#undef ADD_LAST
|
||||
#undef SUB_LAST
|
||||
#undef INIT
|
||||
#undef NEXT
|
||||
#undef RESET
|
||||
#undef LAST
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED ||
|
||||
MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
|
||||
MBEDTLS_ECP_DP_SECP384R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
||||
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
||||
/*
|
||||
* The reader is advised to first understand ecp_mod_p192() since the same
|
||||
* general structure is used here, but with additional complications:
|
||||
@ -5069,27 +5240,6 @@ void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits)
|
||||
N->p[bits / 8 / sizeof(mbedtls_mpi_uint)] += msw;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
||||
/*
|
||||
* Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
|
||||
*/
|
||||
static int ecp_mod_p224(mbedtls_mpi *N)
|
||||
{
|
||||
INIT(224);
|
||||
|
||||
SUB(7); SUB(11); NEXT; // A0 += -A7 - A11
|
||||
SUB(8); SUB(12); NEXT; // A1 += -A8 - A12
|
||||
SUB(9); SUB(13); NEXT; // A2 += -A9 - A13
|
||||
SUB(10); ADD(7); ADD(11); NEXT; // A3 += -A10 + A7 + A11
|
||||
SUB(11); ADD(8); ADD(12); NEXT; // A4 += -A11 + A8 + A12
|
||||
SUB(12); ADD(9); ADD(13); NEXT; // A5 += -A12 + A9 + A13
|
||||
SUB(13); ADD(10); LAST; // A6 += -A13 + A10
|
||||
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
||||
/*
|
||||
* Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
|
||||
@ -5184,16 +5334,10 @@ cleanup:
|
||||
#undef NEXT
|
||||
#undef LAST
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED ||
|
||||
MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
|
||||
MBEDTLS_ECP_DP_SECP384R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
||||
/*
|
||||
* Here we have an actual Mersenne prime, so things are more straightforward.
|
||||
* However, chunks are aligned on a 'weird' boundary (521 bits).
|
||||
*/
|
||||
|
||||
/* Size of p521 in terms of mbedtls_mpi_uint */
|
||||
#define P521_WIDTH (521 / 8 / sizeof(mbedtls_mpi_uint) + 1)
|
||||
|
||||
@ -5201,48 +5345,81 @@ cleanup:
|
||||
#define P521_MASK 0x01FF
|
||||
|
||||
/*
|
||||
* Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
|
||||
* Write N as A1 + 2^521 A0, return A0 + A1
|
||||
* Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
|
||||
*/
|
||||
static int ecp_mod_p521(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t i;
|
||||
mbedtls_mpi M;
|
||||
mbedtls_mpi_uint Mp[P521_WIDTH + 1];
|
||||
/* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits:
|
||||
* we need to hold bits 513 to 1056, which is 34 limbs, that is
|
||||
* P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
|
||||
|
||||
if (N->n < P521_WIDTH) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* M = A1 */
|
||||
M.s = 1;
|
||||
M.n = N->n - (P521_WIDTH - 1);
|
||||
if (M.n > P521_WIDTH + 1) {
|
||||
M.n = P521_WIDTH + 1;
|
||||
}
|
||||
M.p = Mp;
|
||||
memcpy(Mp, N->p + P521_WIDTH - 1, M.n * sizeof(mbedtls_mpi_uint));
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&M, 521 % (8 * sizeof(mbedtls_mpi_uint))));
|
||||
|
||||
/* N = A0 */
|
||||
N->p[P521_WIDTH - 1] &= P521_MASK;
|
||||
for (i = P521_WIDTH; i < N->n; i++) {
|
||||
N->p[i] = 0;
|
||||
}
|
||||
|
||||
/* N = A0 + A1 */
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(N, N, &M));
|
||||
|
||||
size_t expected_width = 2 * P521_WIDTH;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p521_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
mbedtls_mpi_uint carry = 0;
|
||||
|
||||
if (X_limbs != 2 * P521_WIDTH || X[2 * P521_WIDTH - 1] != 0) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
/* Step 1: Reduction to P521_WIDTH limbs */
|
||||
/* Helper references for bottom part of X */
|
||||
mbedtls_mpi_uint *X0 = X;
|
||||
size_t X0_limbs = P521_WIDTH;
|
||||
/* Helper references for top part of X */
|
||||
mbedtls_mpi_uint *X1 = X + X0_limbs;
|
||||
size_t X1_limbs = X_limbs - X0_limbs;
|
||||
/* Split X as X0 + 2^P521_WIDTH X1 and compute X0 + 2^(biL - 9) X1.
|
||||
* (We are using that 2^P521_WIDTH = 2^(512 + biL) and that
|
||||
* 2^(512 + biL) X1 = 2^(biL - 9) X1 mod P521.)
|
||||
* The high order limb of the result will be held in carry and the rest
|
||||
* in X0 (that is the result will be represented as
|
||||
* 2^P521_WIDTH carry + X0).
|
||||
*
|
||||
* Also, note that the resulting carry is either 0 or 1:
|
||||
* X0 < 2^P521_WIDTH = 2^(512 + biL) and X1 < 2^(P521_WIDTH-biL) = 2^512
|
||||
* therefore
|
||||
* X0 + 2^(biL - 9) X1 < 2^(512 + biL) + 2^(512 + biL - 9)
|
||||
* which in turn is less than 2 * 2^(512 + biL).
|
||||
*/
|
||||
mbedtls_mpi_uint shift = ((mbedtls_mpi_uint) 1u) << (biL - 9);
|
||||
carry = mbedtls_mpi_core_mla(X0, X0_limbs, X1, X1_limbs, shift);
|
||||
/* Set X to X0 (by clearing the top part). */
|
||||
memset(X1, 0, X1_limbs * sizeof(mbedtls_mpi_uint));
|
||||
|
||||
/* Step 2: Reduction modulo P521
|
||||
*
|
||||
* At this point X is reduced to P521_WIDTH limbs. What remains is to add
|
||||
* the carry (that is 2^P521_WIDTH carry) and to reduce mod P521. */
|
||||
|
||||
/* 2^P521_WIDTH carry = 2^(512 + biL) carry = 2^(biL - 9) carry mod P521.
|
||||
* Also, recall that carry is either 0 or 1. */
|
||||
mbedtls_mpi_uint addend = carry << (biL - 9);
|
||||
/* Keep the top 9 bits and reduce the rest, using 2^521 = 1 mod P521. */
|
||||
addend += (X[P521_WIDTH - 1] >> 9);
|
||||
X[P521_WIDTH - 1] &= P521_MASK;
|
||||
|
||||
/* Reuse the top part of X (already zeroed) as a helper array for
|
||||
* carrying out the addition. */
|
||||
mbedtls_mpi_uint *addend_arr = X + P521_WIDTH;
|
||||
addend_arr[0] = addend;
|
||||
(void) mbedtls_mpi_core_add(X, X, addend_arr, P521_WIDTH);
|
||||
/* Both addends were less than P521 therefore X < 2 * P521. (This also means
|
||||
* that the result fit in P521_WIDTH limbs and there won't be any carry.) */
|
||||
|
||||
/* Clear the reused part of X. */
|
||||
addend_arr[0] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#undef P521_WIDTH
|
||||
#undef P521_MASK
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
|
||||
|
||||
#endif /* MBEDTLS_ECP_NIST_OPTIM */
|
||||
@ -5504,6 +5681,188 @@ static int ecp_mod_p256k1(mbedtls_mpi *N)
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
|
||||
|
||||
#endif /* !MBEDTLS_ECP_ALT */
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||
const mbedtls_ecp_group_id id,
|
||||
const mbedtls_ecp_curve_type ctype)
|
||||
{
|
||||
mbedtls_mpi_uint *p = NULL;
|
||||
size_t p_limbs;
|
||||
|
||||
if (!(ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE || \
|
||||
ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_SCALAR)) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
switch (id) {
|
||||
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP192R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp192r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp192r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp192r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp192r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP224R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp224r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp224r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp224r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp224r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP256R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp256r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp256r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp256r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp256r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP384R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp384r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp384r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp384r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp384r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP521R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp521r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp521r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp521r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp521r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_BP256R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP256r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP256r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP256r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP256r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_BP384R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP384r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP384r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP384r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP384r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_BP512R1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP512r1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP512r1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) brainpoolP512r1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(brainpoolP512r1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
||||
case MBEDTLS_ECP_DP_CURVE25519:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) curve25519_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(curve25519_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) curve25519_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(curve25519_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP192K1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp192k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp192k1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp192k1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp192k1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP224K1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp224k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp224k1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp224k1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp224k1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP256K1:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) secp256k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp256k1_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) secp256k1_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp256k1_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
|
||||
case MBEDTLS_ECP_DP_CURVE448:
|
||||
if (ctype == (mbedtls_ecp_curve_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
p = (mbedtls_mpi_uint *) curve448_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(curve448_p));
|
||||
} else {
|
||||
p = (mbedtls_mpi_uint *) curve448_n;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(curve448_n));
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
case MBEDTLS_ECP_DP_NONE:
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (mbedtls_mpi_mod_modulus_setup(N, p, p_limbs,
|
||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY)) {
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_TEST_HOOKS */
|
||||
#endif /* !MBEDTLS_ECP_ALT */
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
|
@ -28,12 +28,12 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "bignum_mod.h"
|
||||
#include "mbedtls/ecp.h"
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
|
||||
defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
||||
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
||||
/* Preconditions:
|
||||
* - bits is a multiple of 64 or is 224
|
||||
@ -95,6 +95,72 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
||||
|
||||
/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
|
||||
*
|
||||
* \param[in,out] X The address of the MPI to be converted.
|
||||
* Must have exact limb size that stores a 448-bit MPI
|
||||
* (double the bitlength of the modulus).
|
||||
* Upon return holds the reduced value which is
|
||||
* in range `0 <= X < 2 * N` (where N is the modulus).
|
||||
* The bitlength of the reduced value is the same as
|
||||
* that of the modulus (224 bits).
|
||||
* \param[in] X_limbs The length of \p X in limbs.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
|
||||
* limb size that sores a 448-bit MPI.
|
||||
*/
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
||||
|
||||
/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
|
||||
*
|
||||
* \param[in,out] X The address of the MPI to be converted.
|
||||
* Must have twice as many limbs as the modulus
|
||||
* (the modulus is 521 bits long). Upon return this
|
||||
* holds the reduced value. The reduced value is
|
||||
* in range `0 <= X < 2 * N` (where N is the modulus).
|
||||
* and its the bitlength is one plus the bitlength
|
||||
* of the modulus.
|
||||
* \param[in] X_limbs The length of \p X in limbs.
|
||||
*
|
||||
* \return \c 0 on success.
|
||||
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
|
||||
* twice as many limbs as the modulus.
|
||||
*/
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
|
||||
|
||||
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
|
||||
|
||||
/** Initialise a modulus with hard-coded const curve data.
|
||||
*
|
||||
* \note The caller is responsible for the \p N modulus' memory.
|
||||
* mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
|
||||
* end of its lifecycle.
|
||||
*
|
||||
* \param[in,out] N The address of the modulus structure to populate.
|
||||
* Must be initialized.
|
||||
* \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
|
||||
* \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P)
|
||||
* or a scalar modulus (N).
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
|
||||
* have the correct number of limbs.
|
||||
*
|
||||
*/
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||
const mbedtls_ecp_group_id id,
|
||||
const mbedtls_ecp_curve_type ctype);
|
||||
|
||||
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
|
||||
|
||||
#endif /* MBEDTLS_ECP_INVASIVE_H */
|
||||
|
@ -104,6 +104,7 @@ mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg)
|
||||
return entry->md_type;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
int mbedtls_md_error_from_psa(psa_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
@ -119,3 +120,4 @@ int mbedtls_md_error_from_psa(psa_status_t status)
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
@ -34,6 +34,7 @@
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
/** \def MBEDTLS_HASH_MAX_SIZE
|
||||
*
|
||||
@ -88,12 +89,13 @@ psa_algorithm_t mbedtls_hash_info_psa_from_md(mbedtls_md_type_t md_type);
|
||||
*/
|
||||
mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg);
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
/** Convert PSA status to MD error code.
|
||||
*
|
||||
* \param status PSA status.
|
||||
*
|
||||
* \return The corresponding MD error code,
|
||||
*/
|
||||
int mbedtls_md_error_from_psa(psa_status_t status);
|
||||
|
||||
int MBEDTLS_DEPRECATED mbedtls_md_error_from_psa(psa_status_t status);
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
#endif /* MBEDTLS_HASH_INFO_H */
|
||||
|
@ -41,9 +41,14 @@
|
||||
#include "mbedtls/lms.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_lms_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
|
||||
#define PUBLIC_KEY_TYPE_OFFSET (0)
|
||||
#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
|
||||
MBEDTLS_LMOTS_TYPE_LEN)
|
||||
@ -198,7 +203,7 @@ static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *pa
|
||||
exit:
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* Hash each element of the string of digits (+ checksum), producing a hash
|
||||
@ -321,7 +326,7 @@ exit:
|
||||
psa_hash_abort(&op);
|
||||
mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash));
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* Combine the hashes of the digit array into a public key. This is used in
|
||||
@ -386,9 +391,10 @@ exit:
|
||||
psa_hash_abort(&op);
|
||||
}
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
int mbedtls_lms_error_from_psa(psa_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
@ -406,6 +412,7 @@ int mbedtls_lms_error_from_psa(psa_status_t status)
|
||||
return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
||||
void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx)
|
||||
{
|
||||
@ -682,7 +689,7 @@ int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
|
||||
exit:
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
|
||||
|
@ -79,6 +79,7 @@ void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len,
|
||||
unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len,
|
||||
const unsigned char *bytes);
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
/**
|
||||
* \brief This function converts a \ref psa_status_t to a
|
||||
* low-level LMS error code.
|
||||
@ -87,8 +88,8 @@ unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len,
|
||||
*
|
||||
* \return The corresponding LMS error code.
|
||||
*/
|
||||
int mbedtls_lms_error_from_psa(psa_status_t status);
|
||||
|
||||
int MBEDTLS_DEPRECATED mbedtls_lms_error_from_psa(psa_status_t status);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief This function initializes a public LMOTS context
|
||||
|
@ -39,13 +39,17 @@
|
||||
#include "lmots.h"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "mbedtls/psa_util.h"
|
||||
#include "mbedtls/lms.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_lms_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
|
||||
#define SIG_Q_LEAF_ID_OFFSET (0)
|
||||
#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
|
||||
MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
|
||||
@ -140,7 +144,7 @@ static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
|
||||
exit:
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* Calculate the value of an internal node of the Merkle tree (which is a hash
|
||||
@ -220,7 +224,7 @@ static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
|
||||
exit:
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_lms_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
|
||||
|
292
library/md.c
292
library/md.c
@ -23,7 +23,23 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
/*
|
||||
* Availability of functions in this module is controlled by two
|
||||
* feature macros:
|
||||
* - MBEDTLS_MD_C enables the whole module;
|
||||
* - MBEDTLS_MD_LIGHT enables only functions for hashing and accessing
|
||||
* most hash metadata (everything except string names); is it
|
||||
* automatically set whenever MBEDTLS_MD_C is defined.
|
||||
*
|
||||
* In this file, functions from MD_LIGHT are at the top, MD_C at the end.
|
||||
*
|
||||
* In the future we may want to change the contract of some functions
|
||||
* (behaviour with NULL arguments) depending on whether MD_C is defined or
|
||||
* only MD_LIGHT. Also, the exact scope of MD_LIGHT might vary.
|
||||
*
|
||||
* For these reasons, we're keeping MD_LIGHT internal for now.
|
||||
*/
|
||||
#if defined(MBEDTLS_MD_LIGHT)
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "md_wrap.h"
|
||||
@ -107,91 +123,6 @@ const mbedtls_md_info_t mbedtls_sha512_info = {
|
||||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Reminder: update profiles in x509_crt.c when adding a new hash!
|
||||
*/
|
||||
static const int supported_digests[] = {
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
MBEDTLS_MD_SHA512,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
MBEDTLS_MD_SHA384,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
MBEDTLS_MD_SHA256,
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
MBEDTLS_MD_SHA224,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
MBEDTLS_MD_SHA1,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
MBEDTLS_MD_RIPEMD160,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
MBEDTLS_MD_MD5,
|
||||
#endif
|
||||
|
||||
MBEDTLS_MD_NONE
|
||||
};
|
||||
|
||||
const int *mbedtls_md_list(void)
|
||||
{
|
||||
return supported_digests;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
||||
{
|
||||
if (NULL == md_name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get the appropriate digest information */
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
if (!strcmp("MD5", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
if (!strcmp("RIPEMD160", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
if (!strcmp("SHA224", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
if (!strcmp("SHA256", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
if (!strcmp("SHA384", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if (!strcmp("SHA512", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
||||
{
|
||||
switch (md_type) {
|
||||
@ -228,16 +159,6 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
||||
}
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ctx->MBEDTLS_PRIVATE(md_info);
|
||||
}
|
||||
|
||||
void mbedtls_md_init(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(mbedtls_md_context_t));
|
||||
@ -586,6 +507,125 @@ int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, siz
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return md_info->size;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return MBEDTLS_MD_NONE;
|
||||
}
|
||||
|
||||
return md_info->type;
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* Functions above this separator are part of MBEDTLS_MD_LIGHT, *
|
||||
* functions below are only available when MBEDTLS_MD_C is set. *
|
||||
************************************************************************/
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
|
||||
/*
|
||||
* Reminder: update profiles in x509_crt.c when adding a new hash!
|
||||
*/
|
||||
static const int supported_digests[] = {
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
MBEDTLS_MD_SHA512,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
MBEDTLS_MD_SHA384,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
MBEDTLS_MD_SHA256,
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
MBEDTLS_MD_SHA224,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
MBEDTLS_MD_SHA1,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
MBEDTLS_MD_RIPEMD160,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
MBEDTLS_MD_MD5,
|
||||
#endif
|
||||
|
||||
MBEDTLS_MD_NONE
|
||||
};
|
||||
|
||||
const int *mbedtls_md_list(void)
|
||||
{
|
||||
return supported_digests;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
||||
{
|
||||
if (NULL == md_name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get the appropriate digest information */
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
if (!strcmp("MD5", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
if (!strcmp("RIPEMD160", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
if (!strcmp("SHA224", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
if (!strcmp("SHA256", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
if (!strcmp("SHA384", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if (!strcmp("SHA512", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ctx->MBEDTLS_PRIVATE(md_info);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
|
||||
{
|
||||
@ -774,64 +814,6 @@ cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
|
||||
{
|
||||
if (ctx == NULL || ctx->md_info == NULL) {
|
||||
return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
switch (ctx->md_info->type) {
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
case MBEDTLS_MD_MD5:
|
||||
return mbedtls_internal_md5_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
case MBEDTLS_MD_RIPEMD160:
|
||||
return mbedtls_internal_ripemd160_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
case MBEDTLS_MD_SHA1:
|
||||
return mbedtls_internal_sha1_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
case MBEDTLS_MD_SHA224:
|
||||
return mbedtls_internal_sha256_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
case MBEDTLS_MD_SHA256:
|
||||
return mbedtls_internal_sha256_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
case MBEDTLS_MD_SHA384:
|
||||
return mbedtls_internal_sha512_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
case MBEDTLS_MD_SHA512:
|
||||
return mbedtls_internal_sha512_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
default:
|
||||
return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return md_info->size;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return MBEDTLS_MD_NONE;
|
||||
}
|
||||
|
||||
return md_info->type;
|
||||
}
|
||||
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
@ -842,3 +824,5 @@ const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_MD_C */
|
||||
|
||||
#endif /* MBEDTLS_MD_LIGHT */
|
||||
|
@ -834,21 +834,55 @@ int mbedtls_oid_get_numeric_string(char *buf, size_t size,
|
||||
p = buf;
|
||||
n = size;
|
||||
|
||||
/* First byte contains first two dots */
|
||||
if (oid->len > 0) {
|
||||
ret = mbedtls_snprintf(p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40);
|
||||
OID_SAFE_SNPRINTF;
|
||||
/* First subidentifier contains first two OID components */
|
||||
i = 0;
|
||||
value = 0;
|
||||
if ((oid->p[0]) == 0x80) {
|
||||
/* Overlong encoding is not allowed */
|
||||
return MBEDTLS_ERR_ASN1_INVALID_DATA;
|
||||
}
|
||||
|
||||
value = 0;
|
||||
for (i = 1; i < oid->len; i++) {
|
||||
while (i < oid->len && ((oid->p[i] & 0x80) != 0)) {
|
||||
/* Prevent overflow in value. */
|
||||
if (((value << 7) >> 7) != value) {
|
||||
return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
|
||||
if (value > (UINT_MAX >> 7)) {
|
||||
return MBEDTLS_ERR_ASN1_INVALID_DATA;
|
||||
}
|
||||
|
||||
value |= oid->p[i] & 0x7F;
|
||||
value <<= 7;
|
||||
i++;
|
||||
}
|
||||
if (i >= oid->len) {
|
||||
return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
|
||||
}
|
||||
/* Last byte of first subidentifier */
|
||||
value |= oid->p[i] & 0x7F;
|
||||
i++;
|
||||
|
||||
unsigned int component1 = value / 40;
|
||||
if (component1 > 2) {
|
||||
/* The first component can only be 0, 1 or 2.
|
||||
* If oid->p[0] / 40 is greater than 2, the leftover belongs to
|
||||
* the second component. */
|
||||
component1 = 2;
|
||||
}
|
||||
unsigned int component2 = value - (40 * component1);
|
||||
ret = mbedtls_snprintf(p, n, "%u.%u", component1, component2);
|
||||
OID_SAFE_SNPRINTF;
|
||||
|
||||
value = 0;
|
||||
for (; i < oid->len; i++) {
|
||||
/* Prevent overflow in value. */
|
||||
if (value > (UINT_MAX >> 7)) {
|
||||
return MBEDTLS_ERR_ASN1_INVALID_DATA;
|
||||
}
|
||||
if ((value == 0) && ((oid->p[i]) == 0x80)) {
|
||||
/* Overlong encoding is not allowed */
|
||||
return MBEDTLS_ERR_ASN1_INVALID_DATA;
|
||||
}
|
||||
|
||||
value <<= 7;
|
||||
value += oid->p[i] & 0x7F;
|
||||
value |= oid->p[i] & 0x7F;
|
||||
|
||||
if (!(oid->p[i] & 0x80)) {
|
||||
/* Last byte */
|
||||
|
@ -39,6 +39,13 @@
|
||||
#include "psa/crypto.h"
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_MD5_C)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#include "mbedtls/legacy_or_psa.h"
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
@ -236,7 +243,7 @@ static int pem_pbkdf1(unsigned char *key, size_t keylen,
|
||||
exit:
|
||||
mbedtls_platform_zeroize(md5sum, 16);
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_MD5_C */
|
||||
|
||||
|
17
library/pk.c
17
library/pk.c
@ -41,6 +41,13 @@
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
|
||||
#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_pk_rsa_errors, \
|
||||
psa_pk_status_to_mbedtls)
|
||||
#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_pk_ecdsa_errors, \
|
||||
psa_pk_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
@ -540,7 +547,7 @@ int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
psa_destroy_key(key_id);
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
|
||||
@ -562,7 +569,7 @@ int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
|
||||
status = destruction_status;
|
||||
}
|
||||
|
||||
return mbedtls_pk_error_from_psa_rsa(status);
|
||||
return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
@ -700,7 +707,7 @@ int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
|
||||
status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg),
|
||||
hash, hash_len,
|
||||
sig, sig_size, sig_len);
|
||||
return mbedtls_pk_error_from_psa_rsa(status);
|
||||
return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
|
||||
@ -896,7 +903,7 @@ int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
|
||||
/* import private key into PSA */
|
||||
status = psa_import_key(&attributes, d, d_len, key);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* make PK context wrap the key slot */
|
||||
@ -936,7 +943,7 @@ int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* make PK context wrap the key slot */
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#if defined(MBEDTLS_PK_C)
|
||||
#include "pk_wrap.h"
|
||||
#include "mbedtls/error.h"
|
||||
@ -26,40 +28,46 @@
|
||||
/* Even if RSA not activated, for the sake of RSA-alt */
|
||||
#include "mbedtls/rsa.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
#include "mbedtls/ecp.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
|
||||
#include "pkwrite.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/asn1write.h"
|
||||
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#include "pkwrite.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
||||
#include "mbedtls/platform_util.h"
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
|
||||
#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_pk_rsa_errors, \
|
||||
psa_pk_status_to_mbedtls)
|
||||
#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_pk_ecdsa_errors, \
|
||||
psa_pk_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "hash_info.h"
|
||||
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
#endif
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
int mbedtls_pk_error_from_psa(psa_status_t status)
|
||||
{
|
||||
@ -111,16 +119,31 @@ int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
|
||||
return MBEDTLS_ERR_RSA_VERIFY_FAILED;
|
||||
case PSA_ERROR_INVALID_PADDING:
|
||||
return MBEDTLS_ERR_RSA_INVALID_PADDING;
|
||||
case PSA_SUCCESS:
|
||||
return 0;
|
||||
case PSA_ERROR_NOT_SUPPORTED:
|
||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
||||
case PSA_ERROR_BAD_STATE:
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
case PSA_ERROR_COMMUNICATION_FAILURE:
|
||||
case PSA_ERROR_HARDWARE_FAILURE:
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
case PSA_ERROR_DATA_CORRUPT:
|
||||
case PSA_ERROR_DATA_INVALID:
|
||||
case PSA_ERROR_STORAGE_FAILURE:
|
||||
return MBEDTLS_ERR_PK_FILE_IO_ERROR;
|
||||
case PSA_ERROR_CORRUPTION_DETECTED:
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
default:
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
|
||||
#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||
int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
|
||||
{
|
||||
@ -136,13 +159,30 @@ int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
|
||||
return MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
||||
case PSA_ERROR_INVALID_SIGNATURE:
|
||||
return MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
||||
case PSA_SUCCESS:
|
||||
return 0;
|
||||
case PSA_ERROR_NOT_SUPPORTED:
|
||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
||||
case PSA_ERROR_BAD_STATE:
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
case PSA_ERROR_COMMUNICATION_FAILURE:
|
||||
case PSA_ERROR_HARDWARE_FAILURE:
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
case PSA_ERROR_DATA_CORRUPT:
|
||||
case PSA_ERROR_DATA_INVALID:
|
||||
case PSA_ERROR_STORAGE_FAILURE:
|
||||
return MBEDTLS_ERR_PK_FILE_IO_ERROR;
|
||||
case PSA_ERROR_CORRUPTION_DETECTED:
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
default:
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
|
||||
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
static int rsa_can_do(mbedtls_pk_type_t type)
|
||||
@ -199,14 +239,14 @@ static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
buf + sizeof(buf) - key_len, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
|
||||
sig, sig_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_rsa(status);
|
||||
ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
ret = 0;
|
||||
@ -214,7 +254,7 @@ static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
cleanup:
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -292,13 +332,13 @@ int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
|
||||
buf + sizeof(buf) - key_len, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
status = psa_sign_hash(key_id, alg, hash, hash_len,
|
||||
sig, sig_size, sig_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_rsa(status);
|
||||
ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -307,7 +347,7 @@ int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
|
||||
cleanup:
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -401,7 +441,7 @@ static int rsa_decrypt_wrap(void *ctx,
|
||||
buf + sizeof(buf) - key_len, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -410,7 +450,7 @@ static int rsa_decrypt_wrap(void *ctx,
|
||||
NULL, 0,
|
||||
output, osize, olen);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_rsa(status);
|
||||
ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -420,7 +460,7 @@ cleanup:
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -487,7 +527,7 @@ static int rsa_encrypt_wrap(void *ctx,
|
||||
buf + sizeof(buf) - key_len, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -496,7 +536,7 @@ static int rsa_encrypt_wrap(void *ctx,
|
||||
NULL, 0,
|
||||
output, osize, olen);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_rsa(status);
|
||||
ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -505,7 +545,7 @@ static int rsa_encrypt_wrap(void *ctx,
|
||||
cleanup:
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -685,11 +725,14 @@ static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
psa_status_t status;
|
||||
mbedtls_pk_context key;
|
||||
int key_len;
|
||||
unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
|
||||
size_t key_len;
|
||||
/* This buffer will initially contain the public key and then the signature
|
||||
* but at different points in time. For all curves except secp224k1, which
|
||||
* is not currently supported in PSA, the public key is one byte longer
|
||||
* (header byte + 2 numbers, while the signature is only 2 numbers),
|
||||
* so use that as the buffer size. */
|
||||
unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
|
||||
unsigned char *p;
|
||||
mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
|
||||
psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
|
||||
size_t curve_bits;
|
||||
psa_ecc_family_t curve =
|
||||
@ -701,25 +744,22 @@ static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
/* mbedtls_pk_write_pubkey() expects a full PK context;
|
||||
* re-construct one to make it happy */
|
||||
key.pk_info = &pk_info;
|
||||
key.pk_ctx = ctx;
|
||||
p = buf + sizeof(buf);
|
||||
key_len = mbedtls_pk_write_pubkey(&p, buf, &key);
|
||||
if (key_len <= 0) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
||||
psa_set_key_algorithm(&attributes, psa_sig_md);
|
||||
|
||||
ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
|
||||
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||
&key_len, buf, sizeof(buf));
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = psa_import_key(&attributes,
|
||||
buf + sizeof(buf) - key_len, key_len,
|
||||
buf, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -740,7 +780,7 @@ static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
hash, hash_len,
|
||||
buf, 2 * signature_part_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_ecdsa(status);
|
||||
ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -753,7 +793,7 @@ static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
cleanup:
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -864,54 +904,6 @@ static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
|
||||
*
|
||||
* [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
|
||||
* [in] end: ASN.1 buffer end
|
||||
* [out] key_len: the ECDSA privateKey length in bytes
|
||||
*/
|
||||
static int find_ecdsa_private_key(unsigned char **buf, unsigned char *end,
|
||||
size_t *key_len)
|
||||
{
|
||||
size_t len;
|
||||
int ret;
|
||||
|
||||
/*
|
||||
* RFC 5915, or SEC1 Appendix C.4
|
||||
*
|
||||
* ECPrivateKey ::= SEQUENCE {
|
||||
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
|
||||
* privateKey OCTET STRING,
|
||||
* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
|
||||
* publicKey [1] BIT STRING OPTIONAL
|
||||
* }
|
||||
*/
|
||||
|
||||
if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
|
||||
MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* version */
|
||||
if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
*buf += len;
|
||||
|
||||
/* privateKey */
|
||||
if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
|
||||
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
*key_len = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t sig_size, size_t *sig_len,
|
||||
@ -922,19 +914,18 @@ static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
psa_status_t status;
|
||||
mbedtls_pk_context key;
|
||||
size_t key_len;
|
||||
unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
|
||||
unsigned char *p;
|
||||
psa_algorithm_t psa_hash = mbedtls_hash_info_psa_from_md(md_alg);
|
||||
unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
|
||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
||||
psa_algorithm_t psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(psa_hash);
|
||||
psa_algorithm_t psa_sig_md =
|
||||
PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
|
||||
#else
|
||||
psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA(psa_hash);
|
||||
psa_algorithm_t psa_sig_md =
|
||||
PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
|
||||
#endif
|
||||
size_t curve_bits;
|
||||
psa_ecc_family_t curve =
|
||||
mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
|
||||
size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
|
||||
|
||||
/* PSA has its own RNG */
|
||||
((void) f_rng);
|
||||
@ -944,17 +935,10 @@ static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
/* mbedtls_pk_write_key_der() expects a full PK context;
|
||||
* re-construct one to make it happy */
|
||||
key.pk_info = &mbedtls_eckey_info;
|
||||
key.pk_ctx = ctx;
|
||||
key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
|
||||
if (key_len <= 0) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
if (key_len > sizeof(buf)) {
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
|
||||
p = buf + sizeof(buf) - key_len;
|
||||
ret = find_ecdsa_private_key(&p, buf + sizeof(buf), &key_len);
|
||||
ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
@ -964,17 +948,17 @@ static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
|
||||
psa_set_key_algorithm(&attributes, psa_sig_md);
|
||||
|
||||
status = psa_import_key(&attributes,
|
||||
p, key_len,
|
||||
buf, key_len,
|
||||
&key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
|
||||
sig, sig_size, sig_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa_ecdsa(status);
|
||||
ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -984,7 +968,7 @@ cleanup:
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
status = psa_destroy_key(key_id);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = mbedtls_pk_error_from_psa(status);
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -1003,8 +987,7 @@ static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
||||
/* Forward declarations */
|
||||
static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
@ -1110,8 +1093,7 @@ static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
||||
#endif /* MBEDTLS_ECDSA_C */
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
|
||||
|
||||
static int eckey_check_pair(const void *pub, const void *prv,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
@ -1509,7 +1491,7 @@ static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
|
||||
status = psa_get_key_attributes(*key, &attributes);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
type = psa_get_key_type(&attributes);
|
||||
@ -1533,15 +1515,15 @@ static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
if (status != PSA_SUCCESS) {
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
|
||||
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
|
||||
return mbedtls_pk_error_from_psa_ecdsa(status);
|
||||
return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
|
||||
} else
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if (PSA_KEY_TYPE_IS_RSA(type)) {
|
||||
return mbedtls_pk_error_from_psa_rsa(status);
|
||||
return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
} else
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
return mbedtls_pk_error_from_psa(status);
|
||||
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
|
||||
@ -1596,7 +1578,7 @@ static int pk_opaque_rsa_decrypt(void *ctx,
|
||||
NULL, 0,
|
||||
output, osize, olen);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return mbedtls_pk_error_from_psa_rsa(status);
|
||||
return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -137,26 +137,30 @@ extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
|
||||
extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info;
|
||||
extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info;
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||
int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status);
|
||||
int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_ecdsa(psa_status_t status);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
int mbedtls_pk_error_from_psa(psa_status_t status);
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa(psa_status_t status);
|
||||
|
||||
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
|
||||
defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
|
||||
int mbedtls_pk_error_from_psa_rsa(psa_status_t status);
|
||||
int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_rsa(psa_status_t status);
|
||||
#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t psa_alg_md,
|
||||
mbedtls_rsa_context *rsa_ctx,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t sig_size,
|
||||
size_t *sig_len);
|
||||
int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t psa_alg_md,
|
||||
mbedtls_rsa_context *rsa_ctx,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t sig_size,
|
||||
size_t *sig_len);
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
@ -35,6 +35,13 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(MBEDTLS_MD_C)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_DES_C)
|
||||
#include "mbedtls/des.h"
|
||||
#endif
|
||||
@ -328,7 +335,7 @@ exit:
|
||||
if (status == PSA_SUCCESS) {
|
||||
status = status_abort;
|
||||
}
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,12 @@
|
||||
#include "hash_info.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
|
||||
#if !defined(MBEDTLS_MD_C)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ASN1_PARSE_C)
|
||||
static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations,
|
||||
@ -452,7 +458,7 @@ cleanup:
|
||||
status = status_destruction;
|
||||
}
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
}
|
||||
|
||||
|
213
library/pkcs7.c
213
library/pkcs7.c
@ -57,7 +57,10 @@ static int pkcs7_get_next_content_len(unsigned char **p, unsigned char *end,
|
||||
ret = mbedtls_asn1_get_tag(p, end, len, MBEDTLS_ASN1_CONSTRUCTED
|
||||
| MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
||||
if (ret != 0) {
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
||||
} else if ((size_t) (end - *p) != *len) {
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO,
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -184,13 +187,13 @@ static int pkcs7_get_certificates(unsigned char **p, unsigned char *end,
|
||||
size_t len2 = 0;
|
||||
unsigned char *end_set, *end_cert, *start;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_tag(p, end, &len1, MBEDTLS_ASN1_CONSTRUCTED
|
||||
| MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
|
||||
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
||||
return 0;
|
||||
} else {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
||||
}
|
||||
ret = mbedtls_asn1_get_tag(p, end, &len1, MBEDTLS_ASN1_CONSTRUCTED
|
||||
| MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
||||
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
||||
return 0;
|
||||
}
|
||||
if (ret != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
||||
}
|
||||
start = *p;
|
||||
end_set = *p + len1;
|
||||
@ -213,12 +216,11 @@ static int pkcs7_get_certificates(unsigned char **p, unsigned char *end,
|
||||
return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
*p = start;
|
||||
if ((ret = mbedtls_x509_crt_parse_der(certs, *p, len1)) < 0) {
|
||||
if ((ret = mbedtls_x509_crt_parse_der(certs, start, len1)) < 0) {
|
||||
return MBEDTLS_ERR_PKCS7_INVALID_CERT;
|
||||
}
|
||||
|
||||
*p = *p + len1;
|
||||
*p = end_cert;
|
||||
|
||||
/*
|
||||
* Since in this version we strictly support single certificate, and reaching
|
||||
@ -285,7 +287,8 @@ static void pkcs7_free_signer_info(mbedtls_pkcs7_signer_info *signer)
|
||||
* and unauthenticatedAttributes.
|
||||
**/
|
||||
static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
||||
mbedtls_pkcs7_signer_info *signer)
|
||||
mbedtls_pkcs7_signer_info *signer,
|
||||
mbedtls_x509_buf *alg)
|
||||
{
|
||||
unsigned char *end_signer, *end_issuer_and_sn;
|
||||
int asn1_ret = 0, ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
@ -343,8 +346,15 @@ static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Assume authenticatedAttributes is nonexistent */
|
||||
/* Check that the digest algorithm used matches the one provided earlier */
|
||||
if (signer->alg_identifier.tag != alg->tag ||
|
||||
signer->alg_identifier.len != alg->len ||
|
||||
memcmp(signer->alg_identifier.p, alg->p, alg->len) != 0) {
|
||||
ret = MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Assume authenticatedAttributes is nonexistent */
|
||||
ret = pkcs7_get_digest_algorithm(p, end_signer, &signer->sig_alg_identifier);
|
||||
if (ret != 0) {
|
||||
goto out;
|
||||
@ -377,7 +387,8 @@ out:
|
||||
* Return negative error code for failure.
|
||||
**/
|
||||
static int pkcs7_get_signers_info_set(unsigned char **p, unsigned char *end,
|
||||
mbedtls_pkcs7_signer_info *signers_set)
|
||||
mbedtls_pkcs7_signer_info *signers_set,
|
||||
mbedtls_x509_buf *digest_alg)
|
||||
{
|
||||
unsigned char *end_set;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
@ -397,7 +408,7 @@ static int pkcs7_get_signers_info_set(unsigned char **p, unsigned char *end,
|
||||
|
||||
end_set = *p + len;
|
||||
|
||||
ret = pkcs7_get_signer_info(p, end_set, signers_set);
|
||||
ret = pkcs7_get_signer_info(p, end_set, signers_set, digest_alg);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
@ -412,7 +423,7 @@ static int pkcs7_get_signers_info_set(unsigned char **p, unsigned char *end,
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = pkcs7_get_signer_info(p, end_set, signer);
|
||||
ret = pkcs7_get_signer_info(p, end_set, signer, digest_alg);
|
||||
if (ret != 0) {
|
||||
mbedtls_free(signer);
|
||||
goto cleanup;
|
||||
@ -454,7 +465,7 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
||||
{
|
||||
unsigned char *p = buf;
|
||||
unsigned char *end = buf + buflen;
|
||||
unsigned char *end_set, *end_content_info;
|
||||
unsigned char *end_content_info = NULL;
|
||||
size_t len = 0;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_type_t md_alg;
|
||||
@ -465,16 +476,19 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
||||
}
|
||||
|
||||
end_set = p + len;
|
||||
if (p + len != end) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
||||
}
|
||||
|
||||
/* Get version of signed data */
|
||||
ret = pkcs7_get_version(&p, end_set, &signed_data->version);
|
||||
ret = pkcs7_get_version(&p, end, &signed_data->version);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Get digest algorithm */
|
||||
ret = pkcs7_get_digest_algorithm_set(&p, end_set,
|
||||
ret = pkcs7_get_digest_algorithm_set(&p, end,
|
||||
&signed_data->digest_alg_identifiers);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
@ -485,12 +499,15 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
||||
return MBEDTLS_ERR_PKCS7_INVALID_ALG;
|
||||
}
|
||||
|
||||
/* Do not expect any content */
|
||||
ret = pkcs7_get_content_info_type(&p, end_set, &end_content_info,
|
||||
&signed_data->content.oid);
|
||||
mbedtls_pkcs7_buf content_type;
|
||||
memset(&content_type, 0, sizeof(content_type));
|
||||
ret = pkcs7_get_content_info_type(&p, end, &end_content_info, &content_type);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &content_type)) {
|
||||
return MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO;
|
||||
}
|
||||
|
||||
if (p != end_content_info) {
|
||||
/* Determine if valid content is present */
|
||||
@ -509,13 +526,9 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
||||
return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &signed_data->content.oid)) {
|
||||
return MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO;
|
||||
}
|
||||
|
||||
/* Look for certificates, there may or may not be any */
|
||||
mbedtls_x509_crt_init(&signed_data->certs);
|
||||
ret = pkcs7_get_certificates(&p, end_set, &signed_data->certs);
|
||||
ret = pkcs7_get_certificates(&p, end, &signed_data->certs);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
@ -531,7 +544,10 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
||||
signed_data->no_of_crls = 0;
|
||||
|
||||
/* Get signers info */
|
||||
ret = pkcs7_get_signers_info_set(&p, end_set, &signed_data->signers);
|
||||
ret = pkcs7_get_signers_info_set(&p,
|
||||
end,
|
||||
&signed_data->signers,
|
||||
&signed_data->digest_alg_identifiers);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
@ -550,10 +566,9 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
||||
const size_t buflen)
|
||||
{
|
||||
unsigned char *p;
|
||||
unsigned char *end, *end_content_info;
|
||||
unsigned char *end;
|
||||
size_t len = 0;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
int isoidset = 0;
|
||||
|
||||
if (pkcs7 == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
@ -569,34 +584,45 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
||||
pkcs7->raw.len = buflen;
|
||||
end = p + buflen;
|
||||
|
||||
ret = pkcs7_get_content_info_type(&p, end, &end_content_info,
|
||||
&pkcs7->content_type_oid);
|
||||
ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
||||
| MBEDTLS_ASN1_SEQUENCE);
|
||||
if (ret != 0) {
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ((size_t) (end - p) != len) {
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
|
||||
if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
||||
goto out;
|
||||
}
|
||||
p = pkcs7->raw.p;
|
||||
len = buflen;
|
||||
goto try_data;
|
||||
}
|
||||
|
||||
/* Ensure PKCS7 data uses the exact number of bytes specified in buflen */
|
||||
if (end_content_info != end) {
|
||||
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
if (MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_SIGNED_DATA, p, len)) {
|
||||
/* OID is not MBEDTLS_OID_PKCS7_SIGNED_DATA, which is the only supported feature */
|
||||
if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_DATA, p, len)
|
||||
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, p, len)
|
||||
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_ENVELOPED_DATA, p, len)
|
||||
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_SIGNED_AND_ENVELOPED_DATA, p, len)
|
||||
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_DIGESTED_DATA, p, len)) {
|
||||
/* OID is valid according to the spec, but unsupported */
|
||||
ret = MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
||||
} else {
|
||||
/* OID is invalid according to the spec */
|
||||
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_ENVELOPED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_SIGNED_AND_ENVELOPED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DIGESTED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, &pkcs7->content_type_oid)) {
|
||||
ret = MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_SIGNED_DATA, &pkcs7->content_type_oid)) {
|
||||
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
goto out;
|
||||
}
|
||||
|
||||
isoidset = 1;
|
||||
p += len;
|
||||
|
||||
ret = pkcs7_get_next_content_len(&p, end, &len);
|
||||
if (ret != 0) {
|
||||
@ -615,12 +641,6 @@ try_data:
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!isoidset) {
|
||||
pkcs7->content_type_oid.tag = MBEDTLS_ASN1_OID;
|
||||
pkcs7->content_type_oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS7_SIGNED_DATA);
|
||||
pkcs7->content_type_oid.p = (unsigned char *) MBEDTLS_OID_PKCS7_SIGNED_DATA;
|
||||
}
|
||||
|
||||
ret = MBEDTLS_PKCS7_SIGNED_DATA;
|
||||
|
||||
out:
|
||||
@ -653,6 +673,39 @@ static int mbedtls_pkcs7_data_or_hash_verify(mbedtls_pkcs7 *pkcs7,
|
||||
return MBEDTLS_ERR_PKCS7_CERT_DATE_INVALID;
|
||||
}
|
||||
|
||||
ret = mbedtls_oid_get_md_alg(&pkcs7->signed_data.digest_alg_identifiers, &md_alg);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
md_info = mbedtls_md_info_from_type(md_alg);
|
||||
if (md_info == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
}
|
||||
|
||||
hash = mbedtls_calloc(mbedtls_md_get_size(md_info), 1);
|
||||
if (hash == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
/* BEGIN must free hash before jumping out */
|
||||
if (is_data_hash) {
|
||||
if (datalen != mbedtls_md_get_size(md_info)) {
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
} else {
|
||||
memcpy(hash, data, datalen);
|
||||
}
|
||||
} else {
|
||||
ret = mbedtls_md(md_info, data, datalen, hash);
|
||||
}
|
||||
if (ret != 0) {
|
||||
mbedtls_free(hash);
|
||||
return MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
}
|
||||
|
||||
/* assume failure */
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
|
||||
/*
|
||||
* Potential TODOs
|
||||
* Currently we iterate over all signers and return success if any of them
|
||||
@ -662,61 +715,30 @@ static int mbedtls_pkcs7_data_or_hash_verify(mbedtls_pkcs7 *pkcs7,
|
||||
* identification and SignerIdentifier fields first. That would also allow
|
||||
* us to distinguish between 'no signature for key' and 'signature for key
|
||||
* failed to validate'.
|
||||
*
|
||||
* We could also cache hashes by md, so if there are several sigs all using
|
||||
* the same algo we don't recalculate the hash each time.
|
||||
*/
|
||||
for (signer = &pkcs7->signed_data.signers; signer; signer = signer->next) {
|
||||
ret = mbedtls_oid_get_md_alg(&signer->alg_identifier, &md_alg);
|
||||
if (ret != 0) {
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
continue;
|
||||
}
|
||||
|
||||
md_info = mbedtls_md_info_from_type(md_alg);
|
||||
if (md_info == NULL) {
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
continue;
|
||||
}
|
||||
|
||||
hash = mbedtls_calloc(mbedtls_md_get_size(md_info), 1);
|
||||
if (hash == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_ALLOC_FAILED;
|
||||
}
|
||||
/* BEGIN must free hash before jumping out */
|
||||
if (is_data_hash) {
|
||||
if (datalen != mbedtls_md_get_size(md_info)) {
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
} else {
|
||||
memcpy(hash, data, datalen);
|
||||
}
|
||||
} else {
|
||||
ret = mbedtls_md(md_info, data, datalen, hash);
|
||||
}
|
||||
if (ret != 0) {
|
||||
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
||||
mbedtls_free(hash);
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = mbedtls_pk_verify(&pk_cxt, md_alg, hash,
|
||||
mbedtls_md_get_size(md_info),
|
||||
signer->sig.p, signer->sig.len);
|
||||
mbedtls_free(hash);
|
||||
/* END must free hash before jumping out */
|
||||
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mbedtls_free(hash);
|
||||
/* END must free hash before jumping out */
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7,
|
||||
const mbedtls_x509_crt *cert,
|
||||
const unsigned char *data,
|
||||
size_t datalen)
|
||||
{
|
||||
if (data == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
}
|
||||
return mbedtls_pkcs7_data_or_hash_verify(pkcs7, cert, data, datalen, 0);
|
||||
}
|
||||
|
||||
@ -725,6 +747,9 @@ int mbedtls_pkcs7_signed_hash_verify(mbedtls_pkcs7 *pkcs7,
|
||||
const unsigned char *hash,
|
||||
size_t hashlen)
|
||||
{
|
||||
if (hash == NULL) {
|
||||
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
}
|
||||
return mbedtls_pkcs7_data_or_hash_verify(pkcs7, cert, hash, hashlen, 1);
|
||||
}
|
||||
|
||||
|
@ -20,12 +20,18 @@
|
||||
|
||||
/*
|
||||
* Ensure gmtime_r is available even with -std=c99; must be defined before
|
||||
* mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms.
|
||||
* mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms
|
||||
* except OpenBSD, where it stops us accessing explicit_bzero.
|
||||
*/
|
||||
#if !defined(_POSIX_C_SOURCE)
|
||||
#if !defined(_POSIX_C_SOURCE) && !defined(__OpenBSD__)
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
#endif
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
/* Clang requires this to get support for explicit_bzero */
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "mbedtls/platform_util.h"
|
||||
@ -33,11 +39,31 @@
|
||||
#include "mbedtls/threading.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef __STDC_WANT_LIB_EXT1__
|
||||
#define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() and memset_s() if available */
|
||||
#endif
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
// Detect platforms known to support explicit_bzero()
|
||||
#if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
|
||||
#define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
|
||||
#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 1100037)) || defined(__OpenBSD__)
|
||||
#define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
|
||||
/*
|
||||
* This implementation should never be optimized out by the compiler
|
||||
* Where possible, we try to detect the presence of a platform-provided
|
||||
* secure memset, such as explicit_bzero(), that is safe against being optimized
|
||||
* out, and use that.
|
||||
*
|
||||
* For other platforms, we provide an implementation that aims not to be
|
||||
* optimized out by the compiler.
|
||||
*
|
||||
* This implementation for mbedtls_platform_zeroize() was inspired from Colin
|
||||
* Percival's blog article at:
|
||||
@ -52,30 +78,40 @@
|
||||
* (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
|
||||
* details), optimizations of the following form are still possible:
|
||||
*
|
||||
* if( memset_func != memset )
|
||||
* memset_func( buf, 0, len );
|
||||
* if (memset_func != memset)
|
||||
* memset_func(buf, 0, len);
|
||||
*
|
||||
* Note that it is extremely difficult to guarantee that
|
||||
* mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
|
||||
* the memset() call will not be optimized out by aggressive compilers
|
||||
* in a portable way. For this reason, Mbed TLS also provides the configuration
|
||||
* option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
|
||||
* mbedtls_platform_zeroize() to use a suitable implementation for their
|
||||
* platform and needs.
|
||||
*/
|
||||
#if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !defined(__STDC_LIB_EXT1__) \
|
||||
&& !defined(_WIN32)
|
||||
static void *(*const volatile memset_func)(void *, int, size_t) = memset;
|
||||
#endif
|
||||
|
||||
void mbedtls_platform_zeroize(void *buf, size_t len)
|
||||
{
|
||||
MBEDTLS_INTERNAL_VALIDATE(len == 0 || buf != NULL);
|
||||
|
||||
if (len > 0) {
|
||||
#if defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO)
|
||||
explicit_bzero(buf, len);
|
||||
#elif defined(__STDC_LIB_EXT1__)
|
||||
memset_s(buf, len, 0, len);
|
||||
#elif defined(_WIN32)
|
||||
SecureZeroMemory(buf, len);
|
||||
#else
|
||||
memset_func(buf, 0, len);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
|
||||
#define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() if it's available */
|
||||
#include <time.h>
|
||||
#if !defined(_WIN32) && (defined(unix) || \
|
||||
defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
|
||||
|
1590
library/psa_crypto.c
1590
library/psa_crypto.c
File diff suppressed because it is too large
Load Diff
@ -71,10 +71,10 @@
|
||||
* \retval #PSA_SUCCESS Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not supported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* ciphertext_size is too small.
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_aead_encrypt(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -134,10 +134,10 @@ psa_status_t mbedtls_psa_aead_encrypt(
|
||||
* The cipher is not authentic.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not supported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* plaintext_size is too small.
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_aead_decrypt(
|
||||
const psa_key_attributes_t *attributes,
|
||||
|
@ -59,10 +59,10 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
||||
* (\c PSA_ALG_XXX value such that
|
||||
* #PSA_ALG_IS_CIPHER(\p alg) is true).
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_encrypt_setup(
|
||||
mbedtls_psa_cipher_operation_t *operation,
|
||||
@ -89,10 +89,10 @@ psa_status_t mbedtls_psa_cipher_encrypt_setup(
|
||||
* (\c PSA_ALG_XXX value such that
|
||||
* #PSA_ALG_IS_CIPHER(\p alg) is true).
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_decrypt_setup(
|
||||
mbedtls_psa_cipher_operation_t *operation,
|
||||
@ -116,11 +116,11 @@ psa_status_t mbedtls_psa_cipher_decrypt_setup(
|
||||
* the core to be less or equal to
|
||||
* PSA_CIPHER_IV_MAX_SIZE.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* The size of \p iv is not acceptable for the chosen algorithm,
|
||||
* or the chosen algorithm does not use an IV.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_set_iv(
|
||||
mbedtls_psa_cipher_operation_t *operation,
|
||||
@ -142,10 +142,10 @@ psa_status_t mbedtls_psa_cipher_set_iv(
|
||||
* \param[out] output_length On success, the number of bytes
|
||||
* that make up the returned output.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_update(
|
||||
mbedtls_psa_cipher_operation_t *operation,
|
||||
@ -165,7 +165,7 @@ psa_status_t mbedtls_psa_cipher_update(
|
||||
* \param[out] output_length On success, the number of bytes
|
||||
* that make up the returned output.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* The total input size passed to this operation is not valid for
|
||||
* this particular algorithm. For example, the algorithm is a based
|
||||
@ -176,7 +176,7 @@ psa_status_t mbedtls_psa_cipher_update(
|
||||
* padding, and the ciphertext does not contain valid padding.
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_finish(
|
||||
mbedtls_psa_cipher_operation_t *operation,
|
||||
@ -195,7 +195,7 @@ psa_status_t mbedtls_psa_cipher_finish(
|
||||
*
|
||||
* \param[in,out] operation Initialized cipher operation.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation);
|
||||
|
||||
@ -224,10 +224,10 @@ psa_status_t mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation)
|
||||
* the returned output. Initialized to zero
|
||||
* by the core.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
@ -275,10 +275,10 @@ psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes,
|
||||
* the returned output. Initialized to zero
|
||||
* by the core.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
|
@ -182,24 +182,6 @@ static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
|
||||
}
|
||||
#endif
|
||||
|
||||
/** Get the description of a key given its identifier and policy constraints
|
||||
* and lock it.
|
||||
*
|
||||
* The key must have allow all the usage flags set in \p usage. If \p alg is
|
||||
* nonzero, the key must allow operations with this algorithm. If \p alg is
|
||||
* zero, the algorithm is not checked.
|
||||
*
|
||||
* In case of a persistent key, the function loads the description of the key
|
||||
* into a key slot if not already done.
|
||||
*
|
||||
* On success, the returned key slot is locked. It is the responsibility of
|
||||
* the caller to unlock the key slot when it does not access it anymore.
|
||||
*/
|
||||
psa_status_t psa_get_and_lock_key_slot_with_policy(mbedtls_svc_key_id_t key,
|
||||
psa_key_slot_t **p_slot,
|
||||
psa_key_usage_t usage,
|
||||
psa_algorithm_t alg);
|
||||
|
||||
/** Completely wipe a slot in memory, including its policy.
|
||||
*
|
||||
* Persistent storage is not affected.
|
||||
@ -209,7 +191,7 @@ psa_status_t psa_get_and_lock_key_slot_with_policy(mbedtls_svc_key_id_t key,
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success. This includes the case of a key slot that was
|
||||
* already fully wiped.
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot);
|
||||
|
||||
@ -285,9 +267,9 @@ psa_status_t mbedtls_to_psa_error(int ret);
|
||||
* \retval #PSA_SUCCESS The key was imported successfully.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* The key data is not correctly formatted.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t psa_import_key_into_slot(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -310,12 +292,12 @@ psa_status_t psa_import_key_into_slot(
|
||||
* \p data
|
||||
*
|
||||
* \retval #PSA_SUCCESS The key was exported successfully.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_export_key_internal(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -338,12 +320,12 @@ psa_status_t psa_export_key_internal(
|
||||
* \p data
|
||||
*
|
||||
* \retval #PSA_SUCCESS The public key was exported successfully.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_export_public_key_internal(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -364,7 +346,7 @@ psa_status_t psa_export_public_key_internal(
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* The key was generated successfully.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* Key size in bits or type not supported.
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
@ -399,18 +381,18 @@ psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes,
|
||||
* \param[out] signature_length On success, the number of bytes
|
||||
* that make up the returned signature value.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p signature buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
|
||||
* where \c key_type and \c key_bits are the type and bit-size
|
||||
* respectively of the key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_sign_message_builtin(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -445,9 +427,9 @@ psa_status_t psa_sign_message_builtin(
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The calculation was performed successfully, but the passed
|
||||
* signature is not a valid signature.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_verify_message_builtin(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -475,18 +457,18 @@ psa_status_t psa_verify_message_builtin(
|
||||
* \param[out] signature_length On success, the number of bytes
|
||||
* that make up the returned signature value.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p signature buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
|
||||
* where \c key_type and \c key_bits are the type and bit-size
|
||||
* respectively of the key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_sign_hash_builtin(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -519,9 +501,9 @@ psa_status_t psa_sign_hash_builtin(
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The calculation was performed successfully, but the passed
|
||||
* signature is not a valid signature.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t psa_verify_hash_builtin(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -577,8 +559,8 @@ psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
|
||||
* up the returned shared secret.
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success. Shared secret successfully calculated.
|
||||
* \retval #PSA_ERROR_INVALID_HANDLE
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED
|
||||
* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \p alg is not a key agreement algorithm, or
|
||||
* \p private_key is not compatible with \p alg,
|
||||
@ -588,12 +570,12 @@ psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
|
||||
* \p shared_secret_size is too small
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not a supported key agreement algorithm.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE \emptydescription
|
||||
*/
|
||||
psa_status_t psa_key_agreement_raw_builtin(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -606,4 +588,272 @@ psa_status_t psa_key_agreement_raw_builtin(
|
||||
size_t shared_secret_size,
|
||||
size_t *shared_secret_length);
|
||||
|
||||
/**
|
||||
* \brief Set the maximum number of ops allowed to be executed by an
|
||||
* interruptible function in a single call.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* interruptible_set_max_ops entry point. This function behaves as an
|
||||
* interruptible_set_max_ops entry point as defined in the PSA driver
|
||||
* interface specification for transparent drivers.
|
||||
*
|
||||
* \param[in] max_ops The maximum number of ops to be executed in a
|
||||
* single call, this can be a number from 0 to
|
||||
* #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
|
||||
* is obviously the least amount of work done per
|
||||
* call.
|
||||
*/
|
||||
void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops);
|
||||
|
||||
/**
|
||||
* \brief Get the maximum number of ops allowed to be executed by an
|
||||
* interruptible function in a single call.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* interruptible_get_max_ops entry point. This function behaves as an
|
||||
* interruptible_get_max_ops entry point as defined in the PSA driver
|
||||
* interface specification for transparent drivers.
|
||||
*
|
||||
* \return Maximum number of ops allowed to be executed
|
||||
* by an interruptible function in a single call.
|
||||
*/
|
||||
uint32_t mbedtls_psa_interruptible_get_max_ops(void);
|
||||
|
||||
/**
|
||||
* \brief Get the number of ops that a hash signing operation has taken for the
|
||||
* previous call. If no call or work has taken place, this will return
|
||||
* zero.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* sign_hash_get_num_ops entry point. This function behaves as an
|
||||
* sign_hash_get_num_ops entry point as defined in the PSA driver
|
||||
* interface specification for transparent drivers.
|
||||
*
|
||||
* \param operation The \c
|
||||
* mbedtls_psa_sign_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
*
|
||||
* \return Number of ops that were completed
|
||||
* in the last call to \c
|
||||
* mbedtls_psa_sign_hash_complete().
|
||||
*/
|
||||
uint32_t mbedtls_psa_sign_hash_get_num_ops(
|
||||
const mbedtls_psa_sign_hash_interruptible_operation_t *operation);
|
||||
|
||||
/**
|
||||
* \brief Get the number of ops that a hash verification operation has taken for
|
||||
* the previous call. If no call or work has taken place, this will
|
||||
* return zero.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* verify_hash_get_num_ops entry point. This function behaves as an
|
||||
* verify_hash_get_num_ops entry point as defined in the PSA driver
|
||||
* interface specification for transparent drivers.
|
||||
*
|
||||
* \param operation The \c
|
||||
* mbedtls_psa_verify_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
*
|
||||
* \return Number of ops that were completed
|
||||
* in the last call to \c
|
||||
* mbedtls_psa_verify_hash_complete().
|
||||
*/
|
||||
uint32_t mbedtls_psa_verify_hash_get_num_ops(
|
||||
const mbedtls_psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
/**
|
||||
* \brief Start signing a hash or short message with a private key, in an
|
||||
* interruptible manner.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* sign_hash_start entry point. This function behaves as a
|
||||
* sign_hash_start entry point as defined in the PSA driver interface
|
||||
* specification for transparent drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_sign_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
* \param[in] attributes The attributes of the key to use for the
|
||||
* operation.
|
||||
* \param[in] key_buffer The buffer containing the key context.
|
||||
* \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
|
||||
* \param[in] alg A signature algorithm that is compatible with
|
||||
* the type of the key.
|
||||
* \param[in] hash The hash or message to sign.
|
||||
* \param hash_length Size of the \p hash buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* The operation started successfully - call \c psa_sign_hash_complete()
|
||||
* with the same context to complete the operation
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* An unsupported, incorrectly formatted or incorrect type of key was
|
||||
* used.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED Either no internal interruptible operations
|
||||
* are currently supported, or the key type is currently unsupported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* There was insufficient memory to load the key representation.
|
||||
*/
|
||||
psa_status_t mbedtls_psa_sign_hash_start(
|
||||
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
|
||||
size_t key_buffer_size, psa_algorithm_t alg,
|
||||
const uint8_t *hash, size_t hash_length);
|
||||
|
||||
/**
|
||||
* \brief Continue and eventually complete the action of signing a hash or
|
||||
* short message with a private key, in an interruptible manner.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* sign_hash_complete entry point. This function behaves as a
|
||||
* sign_hash_complete entry point as defined in the PSA driver interface
|
||||
* specification for transparent drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_sign_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
*
|
||||
* \param[out] signature Buffer where the signature is to be written.
|
||||
* \param signature_size Size of the \p signature buffer in bytes. This
|
||||
* must be appropriate for the selected
|
||||
* algorithm and key.
|
||||
* \param[out] signature_length On success, the number of bytes that make up
|
||||
* the returned signature value.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Operation completed successfully
|
||||
*
|
||||
* \retval #PSA_OPERATION_INCOMPLETE
|
||||
* Operation was interrupted due to the setting of \c
|
||||
* psa_interruptible_set_max_ops(), there is still work to be done,
|
||||
* please call this function again with the same operation object.
|
||||
*
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p signature buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
|
||||
* where \c key_type and \c key_bits are the type and bit-size
|
||||
* respectively of \p key.
|
||||
*
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_sign_hash_complete(
|
||||
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
|
||||
uint8_t *signature, size_t signature_size,
|
||||
size_t *signature_length);
|
||||
|
||||
/**
|
||||
* \brief Abort a sign hash operation.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver sign_hash_abort
|
||||
* entry point. This function behaves as a sign_hash_abort entry point as
|
||||
* defined in the PSA driver interface specification for transparent
|
||||
* drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_sign_hash_interruptible_operation_t
|
||||
* to abort.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* The operation was aborted successfully.
|
||||
*/
|
||||
psa_status_t mbedtls_psa_sign_hash_abort(
|
||||
mbedtls_psa_sign_hash_interruptible_operation_t *operation);
|
||||
|
||||
/**
|
||||
* \brief Start reading and verifying a hash or short message, in an
|
||||
* interruptible manner.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* verify_hash_start entry point. This function behaves as a
|
||||
* verify_hash_start entry point as defined in the PSA driver interface
|
||||
* specification for transparent drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_verify_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
* \param[in] attributes The attributes of the key to use for the
|
||||
* operation.
|
||||
* \param[in] key_buffer The buffer containing the key context.
|
||||
* \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
|
||||
* \param[in] alg A signature algorithm that is compatible with
|
||||
* the type of the key.
|
||||
* \param[in] hash The hash whose signature is to be verified.
|
||||
* \param hash_length Size of the \p hash buffer in bytes.
|
||||
* \param[in] signature Buffer containing the signature to verify.
|
||||
* \param signature_length Size of the \p signature buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* The operation started successfully - call \c psa_sign_hash_complete()
|
||||
* with the same context to complete the operation
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* An unsupported or incorrect type of key was used.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* Either no internal interruptible operations are currently supported,
|
||||
* or the key type is currently unsupported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* There was insufficient memory either to load the key representation,
|
||||
* or to prepare the operation.
|
||||
*/
|
||||
psa_status_t mbedtls_psa_verify_hash_start(
|
||||
mbedtls_psa_verify_hash_interruptible_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key_buffer, size_t key_buffer_size,
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t *hash, size_t hash_length,
|
||||
const uint8_t *signature, size_t signature_length);
|
||||
|
||||
/**
|
||||
* \brief Continue and eventually complete the action of signing a hash or
|
||||
* short message with a private key, in an interruptible manner.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* sign_hash_complete entry point. This function behaves as a
|
||||
* sign_hash_complete entry point as defined in the PSA driver interface
|
||||
* specification for transparent drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_sign_hash_interruptible_operation_t
|
||||
* to use. This must be initialized first.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Operation completed successfully, and the passed signature is valid.
|
||||
*
|
||||
* \retval #PSA_OPERATION_INCOMPLETE
|
||||
* Operation was interrupted due to the setting of \c
|
||||
* psa_interruptible_set_max_ops(), there is still work to be done,
|
||||
* please call this function again with the same operation object.
|
||||
*
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The calculation was performed successfully, but the passed
|
||||
* signature is not a valid signature.
|
||||
*
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_verify_hash_complete(
|
||||
mbedtls_psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
/**
|
||||
* \brief Abort a verify signed hash operation.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* verify_hash_abort entry point. This function behaves as a
|
||||
* verify_hash_abort entry point as defined in the PSA driver interface
|
||||
* specification for transparent drivers.
|
||||
*
|
||||
* \param[in] operation The \c
|
||||
* mbedtls_psa_verify_hash_interruptible_operation_t
|
||||
* to abort.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* The operation was aborted successfully.
|
||||
*/
|
||||
psa_status_t mbedtls_psa_verify_hash_abort(
|
||||
mbedtls_psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
#endif /* PSA_CRYPTO_CORE_H */
|
||||
|
@ -66,6 +66,43 @@ psa_status_t psa_driver_wrapper_verify_hash(
|
||||
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
||||
const uint8_t *signature, size_t signature_length);
|
||||
|
||||
/*
|
||||
* Interruptible Signature functions
|
||||
*/
|
||||
|
||||
uint32_t psa_driver_wrapper_sign_hash_get_num_ops(
|
||||
psa_sign_hash_interruptible_operation_t *operation);
|
||||
|
||||
uint32_t psa_driver_wrapper_verify_hash_get_num_ops(
|
||||
psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
psa_status_t psa_driver_wrapper_sign_hash_start(
|
||||
psa_sign_hash_interruptible_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
|
||||
size_t key_buffer_size, psa_algorithm_t alg,
|
||||
const uint8_t *hash, size_t hash_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_sign_hash_complete(
|
||||
psa_sign_hash_interruptible_operation_t *operation,
|
||||
uint8_t *signature, size_t signature_size,
|
||||
size_t *signature_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_sign_hash_abort(
|
||||
psa_sign_hash_interruptible_operation_t *operation);
|
||||
|
||||
psa_status_t psa_driver_wrapper_verify_hash_start(
|
||||
psa_verify_hash_interruptible_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
|
||||
size_t key_buffer_size, psa_algorithm_t alg,
|
||||
const uint8_t *hash, size_t hash_length,
|
||||
const uint8_t *signature, size_t signature_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_verify_hash_complete(
|
||||
psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
psa_status_t psa_driver_wrapper_verify_hash_abort(
|
||||
psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
/*
|
||||
* Key handling functions
|
||||
*/
|
||||
@ -371,6 +408,34 @@ psa_status_t psa_driver_wrapper_key_agreement(
|
||||
size_t shared_secret_size,
|
||||
size_t *shared_secret_length);
|
||||
|
||||
/*
|
||||
* PAKE functions.
|
||||
*/
|
||||
psa_status_t psa_driver_wrapper_pake_setup(
|
||||
psa_pake_operation_t *operation,
|
||||
const psa_crypto_driver_pake_inputs_t *inputs);
|
||||
|
||||
psa_status_t psa_driver_wrapper_pake_output(
|
||||
psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_pake_input(
|
||||
psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
const uint8_t *input,
|
||||
size_t input_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_pake_get_implicit_key(
|
||||
psa_pake_operation_t *operation,
|
||||
uint8_t *output, size_t output_size,
|
||||
size_t *output_length);
|
||||
|
||||
psa_status_t psa_driver_wrapper_pake_abort(
|
||||
psa_pake_operation_t *operation);
|
||||
|
||||
#endif /* PSA_CRYPTO_DRIVER_WRAPPERS_H */
|
||||
|
||||
/* End of automatically generated file. */
|
||||
|
@ -404,6 +404,21 @@ cleanup:
|
||||
return mbedtls_to_psa_error(ret);
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
/* Check whether the public part is loaded. If not, load it. */
|
||||
if (mbedtls_ecp_is_zero(&ecp->Q)) {
|
||||
ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q,
|
||||
&ecp->d, &ecp->grp.G,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
}
|
||||
|
||||
return mbedtls_to_psa_error(ret);
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key_buffer, size_t key_buffer_size,
|
||||
@ -412,7 +427,6 @@ psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_ecp_keypair *ecp = NULL;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t curve_bytes;
|
||||
mbedtls_mpi r, s;
|
||||
|
||||
@ -432,34 +446,39 @@ psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
mbedtls_mpi_init(&s);
|
||||
|
||||
if (signature_length != 2 * curve_bytes) {
|
||||
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
||||
status = PSA_ERROR_INVALID_SIGNATURE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&r,
|
||||
signature,
|
||||
curve_bytes));
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&s,
|
||||
signature + curve_bytes,
|
||||
curve_bytes));
|
||||
|
||||
/* Check whether the public part is loaded. If not, load it. */
|
||||
if (mbedtls_ecp_is_zero(&ecp->Q)) {
|
||||
MBEDTLS_MPI_CHK(
|
||||
mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
|
||||
mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE));
|
||||
status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r,
|
||||
signature,
|
||||
curve_bytes));
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdsa_verify(&ecp->grp, hash, hash_length,
|
||||
&ecp->Q, &r, &s);
|
||||
status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s,
|
||||
signature + curve_bytes,
|
||||
curve_bytes));
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = mbedtls_psa_ecp_load_public_part(ecp);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash,
|
||||
hash_length, &ecp->Q,
|
||||
&r, &s));
|
||||
cleanup:
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecp_keypair_free(ecp);
|
||||
mbedtls_free(ecp);
|
||||
|
||||
return mbedtls_to_psa_error(ret);
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
|
||||
|
@ -48,6 +48,15 @@ psa_status_t mbedtls_psa_ecp_load_representation(psa_key_type_t type,
|
||||
size_t data_length,
|
||||
mbedtls_ecp_keypair **p_ecp);
|
||||
|
||||
/** Load the public part of an internal ECP, if required.
|
||||
*
|
||||
* \param ecp The ECP context to load the public part for.
|
||||
*
|
||||
* \return PSA_SUCCESS on success, otherwise an MPI error.
|
||||
*/
|
||||
|
||||
psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp);
|
||||
|
||||
/** Import an ECP key in binary format.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
@ -70,9 +79,9 @@ psa_status_t mbedtls_psa_ecp_load_representation(psa_key_type_t type,
|
||||
* \retval #PSA_SUCCESS The ECP key was imported successfully.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* The key data is not correctly formatted.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_ecp_import_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -111,12 +120,12 @@ psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
|
||||
* \p data
|
||||
*
|
||||
* \retval #PSA_SUCCESS The ECP public key was exported successfully.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_ecp_export_public_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -166,17 +175,17 @@ psa_status_t mbedtls_psa_ecp_generate_key(
|
||||
* \param[out] signature_length On success, the number of bytes
|
||||
* that make up the returned signature value.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p signature buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_ECC_KEY_PAIR, \c key_bits,
|
||||
* \p alg) where \c key_bits is the bit-size of the ECC key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_ecdsa_sign_hash(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -209,9 +218,9 @@ psa_status_t mbedtls_psa_ecdsa_sign_hash(
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The calculation was performed successfully, but the passed
|
||||
* signature is not a valid signature.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -247,8 +256,8 @@ psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
* up the returned shared secret.
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success. Shared secret successfully calculated.
|
||||
* \retval #PSA_ERROR_INVALID_HANDLE
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED
|
||||
* \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \p alg is not a key agreement algorithm, or
|
||||
* \p private_key is not compatible with \p alg,
|
||||
@ -258,8 +267,8 @@ psa_status_t mbedtls_psa_ecdsa_verify_hash(
|
||||
* \p shared_secret_size is too small
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not a supported key agreement algorithm.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_key_agreement_ecdh(
|
||||
const psa_key_attributes_t *attributes,
|
||||
|
@ -48,8 +48,8 @@
|
||||
* \p alg is not supported
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* \p hash_size is too small
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_compute(
|
||||
psa_algorithm_t alg,
|
||||
@ -88,8 +88,8 @@ psa_status_t mbedtls_psa_hash_compute(
|
||||
* \p alg is not supported
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be inactive).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_setup(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
@ -115,13 +115,13 @@ psa_status_t mbedtls_psa_hash_setup(
|
||||
* \param[in,out] target_operation The operation object to set up.
|
||||
* It must be initialized but not active.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The \p source_operation state is not valid (it must be active).
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The \p target_operation state is not valid (it must be inactive).
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_clone(
|
||||
const mbedtls_psa_hash_operation_t *source_operation,
|
||||
@ -147,8 +147,8 @@ psa_status_t mbedtls_psa_hash_clone(
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be active).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_update(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
@ -186,8 +186,8 @@ psa_status_t mbedtls_psa_hash_update(
|
||||
* The size of the \p hash buffer is too small. You can determine a
|
||||
* sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
|
||||
* where \c alg is the hash algorithm that is calculated.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_finish(
|
||||
mbedtls_psa_hash_operation_t *operation,
|
||||
@ -216,8 +216,8 @@ psa_status_t mbedtls_psa_hash_finish(
|
||||
*
|
||||
* \param[in,out] operation Initialized hash operation.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_hash_abort(
|
||||
mbedtls_psa_hash_operation_t *operation);
|
||||
|
@ -52,8 +52,8 @@
|
||||
* \p alg is not supported.
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* \p mac_size is too small
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_mac_compute(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -89,8 +89,8 @@ psa_status_t mbedtls_psa_mac_compute(
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not supported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be inactive).
|
||||
*/
|
||||
@ -124,8 +124,8 @@ psa_status_t mbedtls_psa_mac_sign_setup(
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \p alg is not supported.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be inactive).
|
||||
*/
|
||||
@ -158,8 +158,8 @@ psa_status_t mbedtls_psa_mac_verify_setup(
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be active).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_mac_update(
|
||||
mbedtls_psa_mac_operation_t *operation,
|
||||
@ -200,8 +200,8 @@ psa_status_t mbedtls_psa_mac_update(
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p mac buffer is too small. A sufficient buffer size
|
||||
* can be determined by calling PSA_MAC_LENGTH().
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_mac_sign_finish(
|
||||
mbedtls_psa_mac_operation_t *operation,
|
||||
@ -241,8 +241,8 @@ psa_status_t mbedtls_psa_mac_sign_finish(
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The operation state is not valid (it must be an active mac verify
|
||||
* operation).
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_mac_verify_finish(
|
||||
mbedtls_psa_mac_operation_t *operation,
|
||||
@ -267,8 +267,8 @@ psa_status_t mbedtls_psa_mac_verify_finish(
|
||||
*
|
||||
* \param[in,out] operation Initialized MAC operation.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_mac_abort(
|
||||
mbedtls_psa_mac_operation_t *operation);
|
||||
|
@ -24,6 +24,7 @@
|
||||
|
||||
#include <psa/crypto.h>
|
||||
#include "psa_crypto_core.h"
|
||||
#include "psa_crypto_pake.h"
|
||||
#include "psa_crypto_slot_management.h"
|
||||
|
||||
#include <mbedtls/ecjpake.h>
|
||||
@ -78,23 +79,6 @@
|
||||
* psa_pake_abort()
|
||||
*/
|
||||
|
||||
enum psa_pake_step {
|
||||
PSA_PAKE_STEP_INVALID = 0,
|
||||
PSA_PAKE_STEP_X1_X2 = 1,
|
||||
PSA_PAKE_STEP_X2S = 2,
|
||||
PSA_PAKE_STEP_DERIVE = 3,
|
||||
};
|
||||
|
||||
enum psa_pake_state {
|
||||
PSA_PAKE_STATE_INVALID = 0,
|
||||
PSA_PAKE_STATE_SETUP = 1,
|
||||
PSA_PAKE_STATE_READY = 2,
|
||||
PSA_PAKE_OUTPUT_X1_X2 = 3,
|
||||
PSA_PAKE_OUTPUT_X2S = 4,
|
||||
PSA_PAKE_INPUT_X1_X2 = 5,
|
||||
PSA_PAKE_INPUT_X4S = 6,
|
||||
};
|
||||
|
||||
/*
|
||||
* The first PAKE step shares the same sequences of the second PAKE step
|
||||
* but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
|
||||
@ -156,16 +140,6 @@ enum psa_pake_state {
|
||||
* psa_pake_get_implicit_key()
|
||||
* => Input & Output Step = PSA_PAKE_STEP_INVALID
|
||||
*/
|
||||
enum psa_pake_sequence {
|
||||
PSA_PAKE_SEQ_INVALID = 0,
|
||||
PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
|
||||
PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
|
||||
PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
|
||||
PSA_PAKE_X2_STEP_KEY_SHARE = 4,
|
||||
PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
|
||||
PSA_PAKE_X2_STEP_ZK_PROOF = 6,
|
||||
PSA_PAKE_SEQ_END = 7,
|
||||
};
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
|
||||
@ -190,232 +164,16 @@ static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
|
||||
psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
|
||||
const psa_pake_cipher_suite_t *cipher_suite)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* A context must be freshly initialized before it can be set up. */
|
||||
if (operation->alg != PSA_ALG_NONE) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (cipher_suite == NULL ||
|
||||
PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
|
||||
(cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
|
||||
cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH) ||
|
||||
PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
if (cipher_suite->algorithm == PSA_ALG_JPAKE) {
|
||||
if (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
|
||||
cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
|
||||
cipher_suite->bits != 256 ||
|
||||
cipher_suite->hash != PSA_ALG_SHA_256) {
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
goto error;
|
||||
}
|
||||
|
||||
operation->alg = cipher_suite->algorithm;
|
||||
|
||||
mbedtls_ecjpake_init(&operation->ctx.ecjpake);
|
||||
|
||||
operation->state = PSA_PAKE_STATE_SETUP;
|
||||
operation->sequence = PSA_PAKE_SEQ_INVALID;
|
||||
operation->input_step = PSA_PAKE_STEP_X1_X2;
|
||||
operation->output_step = PSA_PAKE_STEP_X1_X2;
|
||||
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
operation->buffer_length = 0;
|
||||
operation->buffer_offset = 0;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#endif
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
|
||||
error:
|
||||
psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
|
||||
mbedtls_svc_key_id_t password)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_attributes_t attributes = psa_key_attributes_init();
|
||||
psa_key_type_t type;
|
||||
psa_key_usage_t usage;
|
||||
psa_key_slot_t *slot = NULL;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state != PSA_PAKE_STATE_SETUP) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = psa_get_key_attributes(password, &attributes);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
type = psa_get_key_type(&attributes);
|
||||
usage = psa_get_key_usage_flags(&attributes);
|
||||
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
if (type != PSA_KEY_TYPE_PASSWORD &&
|
||||
type != PSA_KEY_TYPE_PASSWORD_HASH) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((usage & PSA_KEY_USAGE_DERIVE) == 0) {
|
||||
status = PSA_ERROR_NOT_PERMITTED;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (operation->password != NULL) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
status = psa_get_and_lock_key_slot_with_policy(password, &slot,
|
||||
PSA_KEY_USAGE_DERIVE,
|
||||
PSA_ALG_JPAKE);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
operation->password = mbedtls_calloc(1, slot->key.bytes);
|
||||
if (operation->password == NULL) {
|
||||
psa_unlock_key_slot(slot);
|
||||
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
}
|
||||
memcpy(operation->password, slot->key.data, slot->key.bytes);
|
||||
operation->password_len = slot->key.bytes;
|
||||
|
||||
status = psa_unlock_key_slot(slot);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
|
||||
error:
|
||||
psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
|
||||
const uint8_t *user_id,
|
||||
size_t user_id_len)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state != PSA_PAKE_STATE_SETUP) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (user_id_len == 0 || user_id == NULL) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
|
||||
error:
|
||||
psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
|
||||
const uint8_t *peer_id,
|
||||
size_t peer_id_len)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state != PSA_PAKE_STATE_SETUP) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (peer_id_len == 0 || peer_id == NULL) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
|
||||
error:
|
||||
psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
|
||||
psa_pake_role_t role)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state != PSA_PAKE_STATE_SETUP) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (role != PSA_PAKE_ROLE_NONE &&
|
||||
role != PSA_PAKE_ROLE_FIRST &&
|
||||
role != PSA_PAKE_ROLE_SECOND &&
|
||||
role != PSA_PAKE_ROLE_CLIENT &&
|
||||
role != PSA_PAKE_ROLE_SERVER) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
if (operation->alg == PSA_ALG_JPAKE) {
|
||||
if (role != PSA_PAKE_ROLE_CLIENT &&
|
||||
role != PSA_PAKE_ROLE_SERVER) {
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
operation->role = role;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#endif
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
|
||||
error:
|
||||
psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
static psa_status_t psa_pake_ecjpake_setup(psa_pake_operation_t *operation)
|
||||
static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_ecjpake_role role;
|
||||
mbedtls_ecjpake_role role = (operation->role == PSA_PAKE_ROLE_CLIENT) ?
|
||||
MBEDTLS_ECJPAKE_CLIENT : MBEDTLS_ECJPAKE_SERVER;
|
||||
|
||||
if (operation->role == PSA_PAKE_ROLE_CLIENT) {
|
||||
role = MBEDTLS_ECJPAKE_CLIENT;
|
||||
} else if (operation->role == PSA_PAKE_ROLE_SERVER) {
|
||||
role = MBEDTLS_ECJPAKE_SERVER;
|
||||
} else {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
mbedtls_ecjpake_init(&operation->ctx.jpake);
|
||||
|
||||
if (operation->password_len == 0) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecjpake_setup(&operation->ctx.ecjpake,
|
||||
ret = mbedtls_ecjpake_setup(&operation->ctx.jpake,
|
||||
role,
|
||||
MBEDTLS_MD_SHA256,
|
||||
MBEDTLS_ECP_DP_SECP256R1,
|
||||
@ -423,39 +181,101 @@ static psa_status_t psa_pake_ecjpake_setup(psa_pake_operation_t *operation)
|
||||
operation->password_len);
|
||||
|
||||
mbedtls_platform_zeroize(operation->password, operation->password_len);
|
||||
mbedtls_free(operation->password);
|
||||
operation->password = NULL;
|
||||
operation->password_len = 0;
|
||||
|
||||
if (ret != 0) {
|
||||
return mbedtls_ecjpake_to_psa_error(ret);
|
||||
}
|
||||
|
||||
operation->state = PSA_PAKE_STATE_READY;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
static psa_status_t psa_pake_output_internal(
|
||||
psa_pake_operation_t *operation,
|
||||
psa_pake_step_t step,
|
||||
psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
|
||||
const psa_crypto_driver_pake_inputs_t *inputs)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t password_len = 0;
|
||||
psa_pake_role_t role = PSA_PAKE_ROLE_NONE;
|
||||
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
||||
size_t actual_password_len = 0;
|
||||
|
||||
status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = psa_crypto_driver_pake_get_role(inputs, &role);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
operation->password = mbedtls_calloc(1, password_len);
|
||||
if (operation->password == NULL) {
|
||||
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
}
|
||||
|
||||
status = psa_crypto_driver_pake_get_password(inputs, operation->password,
|
||||
password_len, &actual_password_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
operation->password_len = actual_password_len;
|
||||
operation->alg = cipher_suite.algorithm;
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
|
||||
if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
|
||||
cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
|
||||
cipher_suite.bits != 256 ||
|
||||
cipher_suite.hash != PSA_ALG_SHA_256) {
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
goto error;
|
||||
}
|
||||
|
||||
operation->role = role;
|
||||
|
||||
operation->buffer_length = 0;
|
||||
operation->buffer_offset = 0;
|
||||
|
||||
status = psa_pake_ecjpake_setup(operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#else
|
||||
(void) operation;
|
||||
(void) inputs;
|
||||
#endif
|
||||
{ status = PSA_ERROR_NOT_SUPPORTED; }
|
||||
|
||||
error:
|
||||
/* In case of failure of the setup of a multipart operation, the PSA driver interface
|
||||
* specifies that the core does not call any other driver entry point thus does not
|
||||
* call mbedtls_psa_pake_abort(). Therefore call it here to do the needed clean
|
||||
* up like freeing the memory that may have been allocated to store the password.
|
||||
*/
|
||||
mbedtls_psa_pake_abort(operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
static psa_status_t mbedtls_psa_pake_output_internal(
|
||||
mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t length;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state == PSA_PAKE_STATE_INVALID) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (output == NULL || output_size == 0 || output_length == NULL) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
(void) step; // Unused parameter
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
/*
|
||||
@ -473,77 +293,11 @@ static psa_status_t psa_pake_output_internal(
|
||||
* to return the right parts on each step.
|
||||
*/
|
||||
if (operation->alg == PSA_ALG_JPAKE) {
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE &&
|
||||
step != PSA_PAKE_STEP_ZK_PUBLIC &&
|
||||
step != PSA_PAKE_STEP_ZK_PROOF) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (operation->state == PSA_PAKE_STATE_SETUP) {
|
||||
status = psa_pake_ecjpake_setup(operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if (operation->state != PSA_PAKE_STATE_READY &&
|
||||
operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
|
||||
operation->state != PSA_PAKE_OUTPUT_X2S) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (operation->state == PSA_PAKE_STATE_READY) {
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
switch (operation->output_step) {
|
||||
case PSA_PAKE_STEP_X1_X2:
|
||||
operation->state = PSA_PAKE_OUTPUT_X1_X2;
|
||||
break;
|
||||
case PSA_PAKE_STEP_X2S:
|
||||
operation->state = PSA_PAKE_OUTPUT_X2S;
|
||||
break;
|
||||
default:
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
|
||||
}
|
||||
|
||||
/* Check if step matches current sequence */
|
||||
switch (operation->sequence) {
|
||||
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
||||
case PSA_PAKE_X2_STEP_KEY_SHARE:
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
||||
case PSA_PAKE_X2_STEP_ZK_PUBLIC:
|
||||
if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
||||
case PSA_PAKE_X2_STEP_ZK_PROOF:
|
||||
if (step != PSA_PAKE_STEP_ZK_PROOF) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
/* Initialize & write round on KEY_SHARE sequences */
|
||||
if (operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
|
||||
ret = mbedtls_ecjpake_write_round_one(&operation->ctx.ecjpake,
|
||||
if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
|
||||
ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake,
|
||||
operation->buffer,
|
||||
MBEDTLS_PSA_PAKE_BUFFER_SIZE,
|
||||
sizeof(operation->buffer),
|
||||
&operation->buffer_length,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
@ -552,11 +306,10 @@ static psa_status_t psa_pake_output_internal(
|
||||
}
|
||||
|
||||
operation->buffer_offset = 0;
|
||||
} else if (operation->state == PSA_PAKE_OUTPUT_X2S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
|
||||
ret = mbedtls_ecjpake_write_round_two(&operation->ctx.ecjpake,
|
||||
} else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
|
||||
ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake,
|
||||
operation->buffer,
|
||||
MBEDTLS_PSA_PAKE_BUFFER_SIZE,
|
||||
sizeof(operation->buffer),
|
||||
&operation->buffer_length,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
@ -578,8 +331,7 @@ static psa_status_t psa_pake_output_internal(
|
||||
* output with a length byte, even less a curve identifier, as that
|
||||
* information is already available.
|
||||
*/
|
||||
if (operation->state == PSA_PAKE_OUTPUT_X2S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
|
||||
if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
|
||||
operation->role == PSA_PAKE_ROLE_SERVER) {
|
||||
/* Skip ECParameters, with is 3 bytes (RFC 8422) */
|
||||
operation->buffer_offset += 3;
|
||||
@ -605,60 +357,44 @@ static psa_status_t psa_pake_output_internal(
|
||||
operation->buffer_offset += length;
|
||||
|
||||
/* Reset buffer after ZK_PROOF sequence */
|
||||
if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
|
||||
operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
|
||||
(operation->state == PSA_PAKE_OUTPUT_X2S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
|
||||
(step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
|
||||
mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
|
||||
operation->buffer_length = 0;
|
||||
operation->buffer_offset = 0;
|
||||
|
||||
operation->state = PSA_PAKE_STATE_READY;
|
||||
operation->output_step++;
|
||||
operation->sequence = PSA_PAKE_SEQ_INVALID;
|
||||
} else {
|
||||
operation->sequence++;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#else
|
||||
(void) step;
|
||||
(void) output;
|
||||
(void) output_size;
|
||||
(void) output_length;
|
||||
#endif
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
{ return PSA_ERROR_NOT_SUPPORTED; }
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_output(psa_pake_operation_t *operation,
|
||||
psa_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length)
|
||||
psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length)
|
||||
{
|
||||
psa_status_t status = psa_pake_output_internal(
|
||||
psa_status_t status = mbedtls_psa_pake_output_internal(
|
||||
operation, step, output, output_size, output_length);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
psa_pake_abort(operation);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static psa_status_t psa_pake_input_internal(
|
||||
psa_pake_operation_t *operation,
|
||||
psa_pake_step_t step,
|
||||
static psa_status_t mbedtls_psa_pake_input_internal(
|
||||
mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
const uint8_t *input,
|
||||
size_t input_length)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state == PSA_PAKE_STATE_INVALID) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (input == NULL || input_length == 0) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
(void) step; // Unused parameter
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
/*
|
||||
@ -677,77 +413,6 @@ static psa_status_t psa_pake_input_internal(
|
||||
* This causes any input error to be only detected on the last step.
|
||||
*/
|
||||
if (operation->alg == PSA_ALG_JPAKE) {
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE &&
|
||||
step != PSA_PAKE_STEP_ZK_PUBLIC &&
|
||||
step != PSA_PAKE_STEP_ZK_PROOF) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
|
||||
PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256);
|
||||
if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) {
|
||||
return PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (operation->state == PSA_PAKE_STATE_SETUP) {
|
||||
status = psa_pake_ecjpake_setup(operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if (operation->state != PSA_PAKE_STATE_READY &&
|
||||
operation->state != PSA_PAKE_INPUT_X1_X2 &&
|
||||
operation->state != PSA_PAKE_INPUT_X4S) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (operation->state == PSA_PAKE_STATE_READY) {
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
switch (operation->input_step) {
|
||||
case PSA_PAKE_STEP_X1_X2:
|
||||
operation->state = PSA_PAKE_INPUT_X1_X2;
|
||||
break;
|
||||
case PSA_PAKE_STEP_X2S:
|
||||
operation->state = PSA_PAKE_INPUT_X4S;
|
||||
break;
|
||||
default:
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
|
||||
}
|
||||
|
||||
/* Check if step matches current sequence */
|
||||
switch (operation->sequence) {
|
||||
case PSA_PAKE_X1_STEP_KEY_SHARE:
|
||||
case PSA_PAKE_X2_STEP_KEY_SHARE:
|
||||
if (step != PSA_PAKE_STEP_KEY_SHARE) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_PAKE_X1_STEP_ZK_PUBLIC:
|
||||
case PSA_PAKE_X2_STEP_ZK_PUBLIC:
|
||||
if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
case PSA_PAKE_X1_STEP_ZK_PROOF:
|
||||
case PSA_PAKE_X2_STEP_ZK_PROOF:
|
||||
if (step != PSA_PAKE_STEP_ZK_PROOF) {
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy input to local buffer and format it as the Mbed TLS API
|
||||
* expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
|
||||
@ -757,8 +422,7 @@ static psa_status_t psa_pake_input_internal(
|
||||
* ECParameters structure - which means we have to prepend that when
|
||||
* we're a client.
|
||||
*/
|
||||
if (operation->state == PSA_PAKE_INPUT_X4S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
|
||||
if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
|
||||
operation->role == PSA_PAKE_ROLE_CLIENT) {
|
||||
/* We only support secp256r1. */
|
||||
/* This is the ECParameters structure defined by RFC 8422. */
|
||||
@ -766,11 +430,26 @@ static psa_status_t psa_pake_input_internal(
|
||||
3, /* named_curve */
|
||||
0, 23 /* secp256r1 */
|
||||
};
|
||||
|
||||
if (operation->buffer_length + sizeof(ecparameters) >
|
||||
sizeof(operation->buffer)) {
|
||||
return PSA_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
memcpy(operation->buffer + operation->buffer_length,
|
||||
ecparameters, sizeof(ecparameters));
|
||||
operation->buffer_length += sizeof(ecparameters);
|
||||
}
|
||||
|
||||
/*
|
||||
* The core checks that input_length is smaller than
|
||||
* PSA_PAKE_INPUT_MAX_SIZE.
|
||||
* Thus no risk of integer overflow here.
|
||||
*/
|
||||
if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) {
|
||||
return PSA_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
/* Write the length byte */
|
||||
operation->buffer[operation->buffer_length] = (uint8_t) input_length;
|
||||
operation->buffer_length += 1;
|
||||
@ -781,25 +460,23 @@ static psa_status_t psa_pake_input_internal(
|
||||
operation->buffer_length += input_length;
|
||||
|
||||
/* Load buffer at each last round ZK_PROOF */
|
||||
if (operation->state == PSA_PAKE_INPUT_X1_X2 &&
|
||||
operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
|
||||
ret = mbedtls_ecjpake_read_round_one(&operation->ctx.ecjpake,
|
||||
if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
|
||||
ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake,
|
||||
operation->buffer,
|
||||
operation->buffer_length);
|
||||
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
|
||||
operation->buffer_length = 0;
|
||||
|
||||
if (ret != 0) {
|
||||
return mbedtls_ecjpake_to_psa_error(ret);
|
||||
}
|
||||
} else if (operation->state == PSA_PAKE_INPUT_X4S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
|
||||
ret = mbedtls_ecjpake_read_round_two(&operation->ctx.ecjpake,
|
||||
} else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
|
||||
ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake,
|
||||
operation->buffer,
|
||||
operation->buffer_length);
|
||||
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
|
||||
operation->buffer_length = 0;
|
||||
|
||||
if (ret != 0) {
|
||||
@ -807,113 +484,72 @@ static psa_status_t psa_pake_input_internal(
|
||||
}
|
||||
}
|
||||
|
||||
if ((operation->state == PSA_PAKE_INPUT_X1_X2 &&
|
||||
operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
|
||||
(operation->state == PSA_PAKE_INPUT_X4S &&
|
||||
operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
|
||||
operation->state = PSA_PAKE_STATE_READY;
|
||||
operation->input_step++;
|
||||
operation->sequence = PSA_PAKE_SEQ_INVALID;
|
||||
} else {
|
||||
operation->sequence++;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#else
|
||||
(void) step;
|
||||
(void) input;
|
||||
(void) input_length;
|
||||
#endif
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
{ return PSA_ERROR_NOT_SUPPORTED; }
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_input(psa_pake_operation_t *operation,
|
||||
psa_pake_step_t step,
|
||||
const uint8_t *input,
|
||||
size_t input_length)
|
||||
psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
const uint8_t *input,
|
||||
size_t input_length)
|
||||
{
|
||||
psa_status_t status = psa_pake_input_internal(
|
||||
psa_status_t status = mbedtls_psa_pake_input_internal(
|
||||
operation, step, input, input_length);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
psa_pake_abort(operation);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
|
||||
psa_key_derivation_operation_t *output)
|
||||
psa_status_t mbedtls_psa_pake_get_implicit_key(
|
||||
mbedtls_psa_pake_operation_t *operation,
|
||||
uint8_t *output, size_t output_size,
|
||||
size_t *output_length)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (operation->alg == PSA_ALG_NONE ||
|
||||
operation->state != PSA_PAKE_STATE_READY ||
|
||||
operation->input_step != PSA_PAKE_STEP_DERIVE ||
|
||||
operation->output_step != PSA_PAKE_STEP_DERIVE) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
if (operation->alg == PSA_ALG_JPAKE) {
|
||||
ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.ecjpake,
|
||||
operation->buffer,
|
||||
MBEDTLS_PSA_PAKE_BUFFER_SIZE,
|
||||
&operation->buffer_length,
|
||||
ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake,
|
||||
output,
|
||||
output_size,
|
||||
output_length,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
if (ret != 0) {
|
||||
psa_pake_abort(operation);
|
||||
return mbedtls_ecjpake_to_psa_error(ret);
|
||||
}
|
||||
|
||||
status = psa_key_derivation_input_bytes(output,
|
||||
PSA_KEY_DERIVATION_INPUT_SECRET,
|
||||
operation->buffer,
|
||||
operation->buffer_length);
|
||||
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
|
||||
psa_pake_abort(operation);
|
||||
|
||||
return status;
|
||||
return PSA_SUCCESS;
|
||||
} else
|
||||
#else
|
||||
(void) output;
|
||||
#endif
|
||||
status = PSA_ERROR_NOT_SUPPORTED;
|
||||
|
||||
error:
|
||||
psa_key_derivation_abort(output);
|
||||
psa_pake_abort(operation);
|
||||
|
||||
return status;
|
||||
{ return PSA_ERROR_NOT_SUPPORTED; }
|
||||
}
|
||||
|
||||
psa_status_t psa_pake_abort(psa_pake_operation_t *operation)
|
||||
psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
|
||||
{
|
||||
if (operation->alg == PSA_ALG_NONE) {
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
mbedtls_platform_zeroize(operation->password, operation->password_len);
|
||||
mbedtls_free(operation->password);
|
||||
operation->password = NULL;
|
||||
operation->password_len = 0;
|
||||
|
||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
|
||||
if (operation->alg == PSA_ALG_JPAKE) {
|
||||
operation->input_step = PSA_PAKE_STEP_INVALID;
|
||||
operation->output_step = PSA_PAKE_STEP_INVALID;
|
||||
if (operation->password_len > 0) {
|
||||
mbedtls_platform_zeroize(operation->password, operation->password_len);
|
||||
}
|
||||
mbedtls_free(operation->password);
|
||||
operation->password = NULL;
|
||||
operation->password_len = 0;
|
||||
operation->role = PSA_PAKE_ROLE_NONE;
|
||||
mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
|
||||
mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
|
||||
operation->buffer_length = 0;
|
||||
operation->buffer_offset = 0;
|
||||
mbedtls_ecjpake_free(&operation->ctx.ecjpake);
|
||||
mbedtls_ecjpake_free(&operation->ctx.jpake);
|
||||
}
|
||||
#endif
|
||||
|
||||
operation->alg = PSA_ALG_NONE;
|
||||
operation->state = PSA_PAKE_STATE_INVALID;
|
||||
operation->sequence = PSA_PAKE_SEQ_INVALID;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
171
library/psa_crypto_pake.h
Normal file
171
library/psa_crypto_pake.h
Normal file
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* PSA PAKE layer on top of Mbed TLS software crypto
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef PSA_CRYPTO_PAKE_H
|
||||
#define PSA_CRYPTO_PAKE_H
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/** Set the session information for a password-authenticated key exchange.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* pake_setup entry point. This function behaves as a pake_setup
|
||||
* entry point as defined in the PSA driver interface specification for
|
||||
* transparent drivers.
|
||||
*
|
||||
* \param[in,out] operation The operation object to set up. It must have
|
||||
* been initialized but not set up yet.
|
||||
* \param[in] inputs Inputs required for PAKE operation (role, password,
|
||||
* key lifetime, cipher suite)
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* The algorithm in \p cipher_suite is not a supported PAKE algorithm,
|
||||
* or the PAKE primitive in \p cipher_suite is not supported or not
|
||||
* compatible with the PAKE algorithm, or the hash algorithm in
|
||||
* \p cipher_suite is not supported or not compatible with the PAKE
|
||||
* algorithm and primitive.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
|
||||
const psa_crypto_driver_pake_inputs_t *inputs);
|
||||
|
||||
|
||||
/** Get output for a step of a password-authenticated key exchange.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* pake_output entry point. This function behaves as a pake_output
|
||||
* entry point as defined in the PSA driver interface specification for
|
||||
* transparent drivers.
|
||||
*
|
||||
* \param[in,out] operation Active PAKE operation.
|
||||
* \param step The step of the algorithm for which the output is
|
||||
* requested.
|
||||
* \param[out] output Buffer where the output is to be written in the
|
||||
* format appropriate for this driver \p step. Refer to
|
||||
* the documentation of psa_crypto_driver_pake_step_t for
|
||||
* more information.
|
||||
* \param output_size Size of the \p output buffer in bytes. This must
|
||||
* be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
|
||||
* primitive, \p step) where \p alg and
|
||||
* \p primitive are the PAKE algorithm and primitive
|
||||
* in the operation's cipher suite, and \p step is
|
||||
* the output step.
|
||||
*
|
||||
* \param[out] output_length On success, the number of bytes of the returned
|
||||
* output.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
*/
|
||||
psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length);
|
||||
|
||||
/** Provide input for a step of a password-authenticated key exchange.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* pake_input entry point. This function behaves as a pake_input
|
||||
* entry point as defined in the PSA driver interface specification for
|
||||
* transparent drivers.
|
||||
*
|
||||
* \note The core checks that input_length is smaller than PSA_PAKE_INPUT_MAX_SIZE.
|
||||
*
|
||||
* \param[in,out] operation Active PAKE operation.
|
||||
* \param step The driver step for which the input is provided.
|
||||
* \param[in] input Buffer containing the input in the format
|
||||
* appropriate for this \p step. Refer to the
|
||||
* documentation of psa_crypto_driver_pake_step_t
|
||||
* for more information.
|
||||
* \param input_length Size of the \p input buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The verification fails for a zero-knowledge input step.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* the \p input is not valid for the \p operation's algorithm, cipher suite
|
||||
* or \p step.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* the \p input is not supported for the \p operation's algorithm, cipher
|
||||
* suite or \p step.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
*/
|
||||
psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
|
||||
psa_crypto_driver_pake_step_t step,
|
||||
const uint8_t *input,
|
||||
size_t input_length);
|
||||
|
||||
/** Get implicitly confirmed shared secret from a PAKE.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* pake_get_implicit_key entry point. This function behaves as a
|
||||
* pake_get_implicit_key entry point as defined in the PSA driver
|
||||
* interface specification for transparent drivers.
|
||||
*
|
||||
* \param[in,out] operation Active PAKE operation.
|
||||
* \param[out] output Output buffer for implicit key.
|
||||
* \param output_size Size of the output buffer in bytes.
|
||||
* \param[out] output_length On success, the number of bytes of the implicit key.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* Input from a PAKE is not supported by the algorithm in the \p output
|
||||
* key derivation operation.
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
*/
|
||||
psa_status_t mbedtls_psa_pake_get_implicit_key(
|
||||
mbedtls_psa_pake_operation_t *operation,
|
||||
uint8_t *output, size_t output_size,
|
||||
size_t *output_length);
|
||||
|
||||
/** Abort a PAKE operation.
|
||||
*
|
||||
* \note The signature of this function is that of a PSA driver
|
||||
* pake_abort entry point. This function behaves as a pake_abort
|
||||
* entry point as defined in the PSA driver interface specification for
|
||||
* transparent drivers.
|
||||
*
|
||||
* \param[in,out] operation The operation to abort.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success.
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
*/
|
||||
psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation);
|
||||
|
||||
#endif /* PSA_CRYPTO_PAKE_H */
|
@ -172,7 +172,6 @@ psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
|
||||
size_t data_size,
|
||||
size_t *data_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PK_WRITE_C)
|
||||
int ret;
|
||||
mbedtls_pk_context pk;
|
||||
uint8_t *pos = data + data_size;
|
||||
@ -209,14 +208,6 @@ psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
|
||||
|
||||
*data_length = ret;
|
||||
return PSA_SUCCESS;
|
||||
#else
|
||||
(void) type;
|
||||
(void) rsa;
|
||||
(void) data;
|
||||
(void) data_size;
|
||||
(void) data_length;
|
||||
return PSA_ERROR_NOT_SUPPORTED;
|
||||
#endif /* MBEDTLS_PK_WRITE_C */
|
||||
}
|
||||
|
||||
psa_status_t mbedtls_psa_rsa_export_public_key(
|
||||
|
@ -61,9 +61,9 @@ psa_status_t mbedtls_psa_rsa_load_representation(psa_key_type_t type,
|
||||
* \retval #PSA_SUCCESS The RSA key was imported successfully.
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* The key data is not correctly formatted.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_rsa_import_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -102,12 +102,12 @@ psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
|
||||
* \p data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS The RSA public key was exported successfully.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_rsa_export_public_key(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -158,17 +158,17 @@ psa_status_t mbedtls_psa_rsa_generate_key(
|
||||
* \param[out] signature_length On success, the number of bytes
|
||||
* that make up the returned signature value.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p signature buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_RSA_KEY_PAIR, \c key_bits,
|
||||
* \p alg) where \c key_bits is the bit-size of the RSA key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_rsa_sign_hash(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -202,9 +202,9 @@ psa_status_t mbedtls_psa_rsa_sign_hash(
|
||||
* \retval #PSA_ERROR_INVALID_SIGNATURE
|
||||
* The calculation was performed successfully, but the passed
|
||||
* signature is not a valid signature.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
*/
|
||||
psa_status_t mbedtls_psa_rsa_verify_hash(
|
||||
const psa_key_attributes_t *attributes,
|
||||
@ -237,20 +237,20 @@ psa_status_t mbedtls_psa_rsa_verify_hash(
|
||||
* \param[out] output_length On success, the number of bytes
|
||||
* that make up the returned output.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
|
||||
* where \c key_type and \c key_bits are the type and bit-size
|
||||
* respectively of \p key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
@ -294,21 +294,21 @@ psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attribut
|
||||
* \param[out] output_length On success, the number of bytes
|
||||
* that make up the returned output.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_BUFFER_TOO_SMALL
|
||||
* The size of the \p output buffer is too small. You can
|
||||
* determine a sufficient buffer size by calling
|
||||
* #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
|
||||
* where \c key_type and \c key_bits are the type and bit-size
|
||||
* respectively of \p key.
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
|
||||
* \retval #PSA_ERROR_INVALID_PADDING
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_PADDING \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* The library has not been previously initialized by psa_crypto_init().
|
||||
* It is implementation-dependent whether a failure to initialize
|
||||
|
@ -88,9 +88,9 @@ static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
|
||||
* due to a lack of empty key slot, or available memory.
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* There is no key with key identifier \p key.
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
*/
|
||||
psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
|
||||
psa_key_slot_t **p_slot);
|
||||
@ -118,9 +118,9 @@ void psa_wipe_all_key_slots(void);
|
||||
* associated to the returned slot.
|
||||
* \param[out] p_slot On success, a pointer to the slot.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_BAD_STATE
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_BAD_STATE \emptydescription
|
||||
*/
|
||||
psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
|
||||
psa_key_slot_t **p_slot);
|
||||
@ -195,8 +195,8 @@ static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
|
||||
* storage, returns a pointer to the driver table
|
||||
* associated with the key's storage location.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
*/
|
||||
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
|
||||
psa_se_drv_table_entry_t **p_drv);
|
||||
@ -205,7 +205,7 @@ psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
|
||||
*
|
||||
* \param[in] lifetime The key lifetime attribute.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
|
||||
* are not supported.
|
||||
*/
|
||||
|
@ -79,11 +79,11 @@ static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
|
||||
* \param[out] data Buffer where the data is to be written.
|
||||
* \param data_size Size of the \c data buffer in bytes.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
|
||||
*/
|
||||
static psa_status_t psa_crypto_storage_load(
|
||||
const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
|
||||
@ -131,11 +131,11 @@ int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
|
||||
* \param data_length The number of bytes
|
||||
* that make up the data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_ALREADY_EXISTS
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
* \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
*/
|
||||
static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
|
||||
const uint8_t *data,
|
||||
@ -205,10 +205,10 @@ psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
|
||||
* is to be obtained.
|
||||
* \param[out] data_length The number of bytes that make up the data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
*/
|
||||
static psa_status_t psa_crypto_storage_get_data_length(
|
||||
const mbedtls_svc_key_id_t key,
|
||||
|
@ -96,14 +96,14 @@ int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key);
|
||||
* \param[in] data Buffer containing the key data.
|
||||
* \param data_length The number of bytes that make up the key data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_ALREADY_EXISTS
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
*/
|
||||
psa_status_t psa_save_persistent_key(const psa_core_key_attributes_t *attr,
|
||||
const uint8_t *data,
|
||||
@ -129,11 +129,11 @@ psa_status_t psa_save_persistent_key(const psa_core_key_attributes_t *attr,
|
||||
* \param[out] data Pointer to an allocated key data buffer on return.
|
||||
* \param[out] data_length The number of bytes that make up the key data.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
|
||||
*/
|
||||
psa_status_t psa_load_persistent_key(psa_core_key_attributes_t *attr,
|
||||
uint8_t **data,
|
||||
@ -148,7 +148,7 @@ psa_status_t psa_load_persistent_key(psa_core_key_attributes_t *attr,
|
||||
* \retval #PSA_SUCCESS
|
||||
* The key was successfully removed,
|
||||
* or the key did not exist.
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
*/
|
||||
psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key);
|
||||
|
||||
@ -190,9 +190,9 @@ void psa_format_key_data_for_storage(const uint8_t *data,
|
||||
* \param[out] attr On success, the attribute structure is filled
|
||||
* with the loaded key metadata.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
*/
|
||||
psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
|
||||
size_t storage_data_length,
|
||||
@ -322,10 +322,10 @@ static inline void psa_crypto_prepare_transaction(
|
||||
* You may call this function multiple times during a transaction to
|
||||
* atomically update the transaction state.
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_SUCCESS \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
*/
|
||||
psa_status_t psa_crypto_save_transaction(void);
|
||||
|
||||
@ -339,9 +339,9 @@ psa_status_t psa_crypto_save_transaction(void);
|
||||
* #psa_crypto_transaction.
|
||||
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
||||
* There is no ongoing transaction.
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_DATA_INVALID
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_INVALID \emptydescription
|
||||
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
||||
*/
|
||||
psa_status_t psa_crypto_load_transaction(void);
|
||||
|
||||
@ -380,8 +380,8 @@ psa_status_t psa_crypto_stop_transaction(void);
|
||||
*
|
||||
* \retval #PSA_SUCCESS
|
||||
* Success
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
||||
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
||||
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
|
||||
* \retval #PSA_ERROR_NOT_PERMITTED
|
||||
* The entropy seed file already exists.
|
||||
*/
|
||||
|
150
library/psa_util.c
Normal file
150
library/psa_util.c
Normal file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* PSA hashing layer on top of Mbed TLS software crypto
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
#include "psa_crypto_core.h"
|
||||
#include <mbedtls/psa_util.h>
|
||||
#include <mbedtls/error.h>
|
||||
#include <mbedtls/lms.h>
|
||||
#include <mbedtls/ssl.h>
|
||||
#include <mbedtls/rsa.h>
|
||||
|
||||
/* PSA_SUCCESS is kept at the top of each error table since
|
||||
* it's the most common status when everything functions properly. */
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_MD5_C) || defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
const mbedtls_error_pair_t psa_to_md_errors[] =
|
||||
{
|
||||
{ PSA_SUCCESS, 0 },
|
||||
{ PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
|
||||
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
|
||||
};
|
||||
#endif
|
||||
#if defined(MBEDTLS_LMS_C)
|
||||
const mbedtls_error_pair_t psa_to_lms_errors[] =
|
||||
{
|
||||
{ PSA_SUCCESS, 0 },
|
||||
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
|
||||
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
|
||||
};
|
||||
#endif
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
const mbedtls_error_pair_t psa_to_ssl_errors[] =
|
||||
{
|
||||
{ PSA_SUCCESS, 0 },
|
||||
{ PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
|
||||
{ PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
|
||||
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
|
||||
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
|
||||
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
|
||||
};
|
||||
#endif
|
||||
|
||||
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
|
||||
defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
|
||||
const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
|
||||
{
|
||||
{ PSA_SUCCESS, 0 },
|
||||
{ PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
|
||||
{ PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
|
||||
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
|
||||
{ PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
|
||||
};
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
||||
const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
|
||||
{
|
||||
{ PSA_SUCCESS, 0 },
|
||||
{ PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
|
||||
{ PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
|
||||
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
|
||||
{ PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
|
||||
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
|
||||
};
|
||||
#endif
|
||||
|
||||
int psa_generic_status_to_mbedtls(psa_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case PSA_SUCCESS:
|
||||
return 0;
|
||||
case PSA_ERROR_NOT_SUPPORTED:
|
||||
return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
|
||||
case PSA_ERROR_CORRUPTION_DETECTED:
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
case PSA_ERROR_COMMUNICATION_FAILURE:
|
||||
case PSA_ERROR_HARDWARE_FAILURE:
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
case PSA_ERROR_NOT_PERMITTED:
|
||||
default:
|
||||
return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
int psa_status_to_mbedtls(psa_status_t status,
|
||||
const mbedtls_error_pair_t *local_translations,
|
||||
size_t local_errors_size,
|
||||
int (*fallback_f)(psa_status_t))
|
||||
{
|
||||
size_t local_errors_num = (size_t) local_errors_size / 2;
|
||||
for (size_t i = 0; i < local_errors_num; i++) {
|
||||
if (status == local_translations[i].psa_status) {
|
||||
return local_translations[i].mbedtls_error;
|
||||
}
|
||||
}
|
||||
return fallback_f(status);
|
||||
}
|
||||
|
||||
int psa_pk_status_to_mbedtls(psa_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case PSA_ERROR_INVALID_HANDLE:
|
||||
return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
|
||||
case PSA_ERROR_BUFFER_TOO_SMALL:
|
||||
return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
|
||||
case PSA_ERROR_NOT_SUPPORTED:
|
||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||
case PSA_ERROR_INVALID_ARGUMENT:
|
||||
return MBEDTLS_ERR_PK_INVALID_ALG;
|
||||
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
||||
case PSA_ERROR_BAD_STATE:
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
case PSA_ERROR_DATA_CORRUPT:
|
||||
case PSA_ERROR_DATA_INVALID:
|
||||
case PSA_ERROR_STORAGE_FAILURE:
|
||||
return MBEDTLS_ERR_PK_FILE_IO_ERROR;
|
||||
default:
|
||||
return psa_generic_status_to_mbedtls(status);
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
@ -60,7 +60,10 @@
|
||||
#if !defined(MBEDTLS_MD_C)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif /* MBEDTLS_MD_C */
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
#endif /* MBEDTLS_PKCS1_V21 */
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
@ -1156,7 +1159,7 @@ exit:
|
||||
#else
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -1236,7 +1239,7 @@ exit:
|
||||
exit:
|
||||
psa_hash_abort(&op);
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
}
|
||||
|
||||
@ -1269,7 +1272,7 @@ static int compute_hash(mbedtls_md_type_t md_alg,
|
||||
|
||||
status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
|
||||
|
||||
return mbedtls_md_error_from_psa(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
#endif /* !MBEDTLS_MD_C */
|
||||
}
|
||||
#endif /* MBEDTLS_PKCS1_V21 */
|
||||
|
@ -22,6 +22,23 @@
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
*/
|
||||
|
||||
#if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \
|
||||
defined(__clang__) && __clang_major__ < 18 && __clang_major__ > 3
|
||||
/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
|
||||
*
|
||||
* The intrinsic declaration are guarded by predefined ACLE macros in clang:
|
||||
* these are normally only enabled by the -march option on the command line.
|
||||
* By defining the macros ourselves we gain access to those declarations without
|
||||
* requiring -march on the command line.
|
||||
*
|
||||
* `arm_neon.h` could be included by any header file, so we put these defines
|
||||
* at the top of this file, before any includes.
|
||||
*/
|
||||
#define __ARM_FEATURE_CRYPTO 1
|
||||
#define NEED_TARGET_OPTIONS
|
||||
#endif /* __aarch64__ && __clang__ &&
|
||||
!__ARM_FEATURE_CRYPTO && __clang_major__ < 18 && __clang_major__ > 3 */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
|
||||
@ -37,6 +54,30 @@
|
||||
#if defined(__aarch64__)
|
||||
# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
|
||||
defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
|
||||
/* *INDENT-OFF* */
|
||||
# if !defined(__ARM_FEATURE_CRYPTO) || defined(NEED_TARGET_OPTIONS)
|
||||
# if defined(__clang__)
|
||||
# if __clang_major__ < 4
|
||||
# error "A more recent Clang is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
|
||||
# define MBEDTLS_POP_TARGET_PRAGMA
|
||||
# elif defined(__GNUC__)
|
||||
/* FIXME: GCC 5 claims to support Armv8 Crypto Extensions, but some
|
||||
* intrinsics are missing. Missing intrinsics could be worked around.
|
||||
*/
|
||||
# if __GNUC__ < 6
|
||||
# error "A more recent GCC is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
|
||||
# else
|
||||
# pragma GCC push_options
|
||||
# pragma GCC target ("arch=armv8-a+crypto")
|
||||
# define MBEDTLS_POP_TARGET_PRAGMA
|
||||
# endif
|
||||
# else
|
||||
# error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
# endif
|
||||
/* *INDENT-ON* */
|
||||
# include <arm_neon.h>
|
||||
# endif
|
||||
# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
|
||||
@ -353,8 +394,16 @@ int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
|
||||
SHA256_BLOCK_SIZE) ? 0 : -1;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
|
||||
#if defined(MBEDTLS_POP_TARGET_PRAGMA)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
#undef MBEDTLS_POP_TARGET_PRAGMA
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
|
||||
|
||||
#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
|
||||
#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
|
||||
|
@ -22,6 +22,26 @@
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
*/
|
||||
|
||||
#if defined(__aarch64__) && !defined(__ARM_FEATURE_SHA512) && \
|
||||
defined(__clang__) && __clang_major__ < 18 && \
|
||||
__clang_major__ >= 13 && __clang_minor__ > 0 && __clang_patchlevel__ > 0
|
||||
/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
|
||||
*
|
||||
* The intrinsic declaration are guarded by predefined ACLE macros in clang:
|
||||
* these are normally only enabled by the -march option on the command line.
|
||||
* By defining the macros ourselves we gain access to those declarations without
|
||||
* requiring -march on the command line.
|
||||
*
|
||||
* `arm_neon.h` could be included by any header file, so we put these defines
|
||||
* at the top of this file, before any includes.
|
||||
*/
|
||||
#define __ARM_FEATURE_SHA512 1
|
||||
#define NEED_TARGET_OPTIONS
|
||||
#endif /* __aarch64__ && __clang__ &&
|
||||
!__ARM_FEATURE_SHA512 && __clang_major__ < 18 &&
|
||||
__clang_major__ >= 13 && __clang_minor__ > 0 &&
|
||||
__clang_patchlevel__ > 0 */
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA384_C)
|
||||
@ -43,6 +63,47 @@
|
||||
#if defined(__aarch64__)
|
||||
# if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT) || \
|
||||
defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
|
||||
/* *INDENT-OFF* */
|
||||
/*
|
||||
* Best performance comes from most recent compilers, with intrinsics and -O3.
|
||||
* Must compile with -march=armv8.2-a+sha3, but we can't detect armv8.2-a, and
|
||||
* can't always detect __ARM_FEATURE_SHA512 (notably clang 7-12).
|
||||
*
|
||||
* GCC < 8 won't work at all (lacks the sha512 instructions)
|
||||
* GCC >= 8 uses intrinsics, sets __ARM_FEATURE_SHA512
|
||||
*
|
||||
* Clang < 7 won't work at all (lacks the sha512 instructions)
|
||||
* Clang 7-12 don't have intrinsics (but we work around that with inline
|
||||
* assembler) or __ARM_FEATURE_SHA512
|
||||
* Clang == 13.0.0 same as clang 12 (only seen on macOS)
|
||||
* Clang >= 13.0.1 has __ARM_FEATURE_SHA512 and intrinsics
|
||||
*/
|
||||
# if !defined(__ARM_FEATURE_SHA512) || defined(NEED_TARGET_OPTIONS)
|
||||
/* Test Clang first, as it defines __GNUC__ */
|
||||
# if defined(__clang__)
|
||||
# if __clang_major__ < 7
|
||||
# error "A more recent Clang is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# elif __clang_major__ < 13 || \
|
||||
(__clang_major__ == 13 && __clang_minor__ == 0 && \
|
||||
__clang_patchlevel__ == 0)
|
||||
/* We implement the intrinsics with inline assembler, so don't error */
|
||||
# else
|
||||
# pragma clang attribute push (__attribute__((target("sha3"))), apply_to=function)
|
||||
# define MBEDTLS_POP_TARGET_PRAGMA
|
||||
# endif
|
||||
# elif defined(__GNUC__)
|
||||
# if __GNUC__ < 8
|
||||
# error "A more recent GCC is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# else
|
||||
# pragma GCC push_options
|
||||
# pragma GCC target ("arch=armv8.2-a+sha3")
|
||||
# define MBEDTLS_POP_TARGET_PRAGMA
|
||||
# endif
|
||||
# else
|
||||
# error "Only GCC and Clang supported for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
|
||||
# endif
|
||||
# endif
|
||||
/* *INDENT-ON* */
|
||||
# include <arm_neon.h>
|
||||
# endif
|
||||
# if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
|
||||
@ -516,6 +577,15 @@ int mbedtls_internal_sha512_process_a64_crypto(mbedtls_sha512_context *ctx,
|
||||
SHA512_BLOCK_SIZE) ? 0 : -1;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_POP_TARGET_PRAGMA)
|
||||
#if defined(__clang__)
|
||||
#pragma clang attribute pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
#undef MBEDTLS_POP_TARGET_PRAGMA
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY */
|
||||
|
||||
|
||||
|
@ -945,16 +945,29 @@ int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
|
||||
{
|
||||
|
||||
mbedtls_ssl_add_hs_hdr_to_checksum(ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
|
||||
msg_len);
|
||||
ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
|
||||
ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CLIENT_HELLO,
|
||||
msg_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
||||
if (binders_len > 0) {
|
||||
MBEDTLS_SSL_PROC_CHK(
|
||||
mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
|
||||
ssl, buf + msg_len - binders_len, buf + msg_len));
|
||||
ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
|
||||
binders_len);
|
||||
ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
|
||||
binders_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
||||
|
||||
|
@ -37,6 +37,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If DTLS is in use, then at least one of SHA-256 or SHA-384 is
|
||||
* available. Try SHA-256 first as 384 wastes resources
|
||||
@ -126,7 +132,7 @@ int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
|
||||
|
||||
if ((status = psa_generate_key(&attributes,
|
||||
&ctx->psa_hmac_key)) != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
@ -215,26 +221,26 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
|
||||
status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
|
||||
ctx->psa_hmac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_update(&operation, *p - 4, 4);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_update(&operation, cli_id, cli_id_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
|
||||
&sign_mac_length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -263,7 +269,7 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
|
||||
exit:
|
||||
status = psa_mac_abort(&operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
return ret;
|
||||
@ -299,27 +305,27 @@ int mbedtls_ssl_cookie_check(void *p_ctx,
|
||||
status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
|
||||
ctx->psa_hmac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_update(&operation, cookie, 4);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_update(&operation, cli_id,
|
||||
cli_id_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_verify_finish(&operation, cookie + 4,
|
||||
COOKIE_HMAC_LEN);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -374,7 +380,7 @@ exit:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_abort(&operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
|
||||
|
@ -55,6 +55,7 @@
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#endif
|
||||
|
||||
#include "mbedtls/pk.h"
|
||||
#include "common.h"
|
||||
|
||||
/* Shorthand for restartable ECC */
|
||||
@ -705,9 +706,12 @@ struct mbedtls_ssl_handshake_params {
|
||||
|
||||
mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
|
||||
|
||||
void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
void (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
|
||||
mbedtls_ssl_tls_prf_cb *tls_prf;
|
||||
|
||||
/*
|
||||
@ -902,14 +906,14 @@ struct mbedtls_ssl_handshake_params {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_operation_t fin_sha256_psa;
|
||||
#else
|
||||
mbedtls_sha256_context fin_sha256;
|
||||
mbedtls_md_context_t fin_sha256;
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_operation_t fin_sha384_psa;
|
||||
#else
|
||||
mbedtls_sha512_context fin_sha384;
|
||||
mbedtls_md_context_t fin_sha384;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -1317,7 +1321,8 @@ static inline void mbedtls_ssl_handshake_set_state(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl);
|
||||
|
||||
void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
@ -1328,7 +1333,8 @@ MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl);
|
||||
void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
|
||||
|
||||
/**
|
||||
* \brief Update record layer
|
||||
@ -1461,14 +1467,16 @@ void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
|
||||
/*
|
||||
* Update checksum of handshake messages.
|
||||
*/
|
||||
void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
unsigned char const *msg,
|
||||
size_t msg_len);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
unsigned char const *msg,
|
||||
size_t msg_len);
|
||||
|
||||
void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
size_t total_hs_len);
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
size_t total_hs_len);
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
@ -2272,7 +2280,7 @@ static inline int mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
|
||||
const uint16_t sig_alg)
|
||||
{
|
||||
switch (sig_alg) {
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
#if defined(PSA_WANT_ALG_SHA_256) && defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
||||
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
|
||||
break;
|
||||
@ -2285,7 +2293,7 @@ static inline int mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
|
||||
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
|
||||
break;
|
||||
#endif /* PSA_WANT_ALG_SHA_512 && MBEDTLS_ECP_DP_SECP521R1_ENABLED */
|
||||
#endif /* MBEDTLS_ECDSA_C */
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
|
||||
|
||||
#if defined(MBEDTLS_PKCS1_V21)
|
||||
#if defined(PSA_WANT_ALG_SHA_256)
|
||||
@ -2441,7 +2449,7 @@ static inline int mbedtls_ssl_tls12_sig_alg_is_supported(
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
case MBEDTLS_SSL_SIG_ECDSA:
|
||||
break;
|
||||
#endif
|
||||
@ -2507,6 +2515,7 @@ psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type
|
||||
psa_key_type_t *key_type,
|
||||
size_t *key_size);
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
/**
|
||||
* \brief Convert given PSA status to mbedtls error code.
|
||||
*
|
||||
@ -2514,7 +2523,7 @@ psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type
|
||||
*
|
||||
* \return corresponding mbedtls error code
|
||||
*/
|
||||
static inline int psa_ssl_status_to_mbedtls(psa_status_t status)
|
||||
static inline MBEDTLS_DEPRECATED int psa_ssl_status_to_mbedtls(psa_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case PSA_SUCCESS:
|
||||
@ -2535,6 +2544,7 @@ static inline int psa_ssl_status_to_mbedtls(psa_status_t status)
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
|
@ -48,6 +48,12 @@
|
||||
#include "mbedtls/oid.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
|
||||
|
||||
/*
|
||||
@ -879,10 +885,10 @@ int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
|
||||
hmac_failed_etm_disabled:
|
||||
mbedtls_platform_zeroize(mac, transform->maclen);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
@ -979,7 +985,7 @@ hmac_failed_etm_disabled:
|
||||
&rec->data_len);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1089,7 +1095,7 @@ hmac_failed_etm_disabled:
|
||||
transform->psa_key_enc, transform->psa_alg);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1097,7 +1103,7 @@ hmac_failed_etm_disabled:
|
||||
status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
|
||||
return ret;
|
||||
|
||||
@ -1108,7 +1114,7 @@ hmac_failed_etm_disabled:
|
||||
data, rec->data_len, &olen);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
|
||||
return ret;
|
||||
|
||||
@ -1119,7 +1125,7 @@ hmac_failed_etm_disabled:
|
||||
&part_len);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
|
||||
return ret;
|
||||
|
||||
@ -1222,10 +1228,10 @@ hmac_failed_etm_disabled:
|
||||
hmac_failed_etm_enabled:
|
||||
mbedtls_platform_zeroize(mac, transform->maclen);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
@ -1399,7 +1405,7 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
&olen);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1571,10 +1577,10 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
|
||||
hmac_failed_etm_enabled:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_platform_zeroize(mac_expect, transform->maclen);
|
||||
@ -1621,7 +1627,7 @@ hmac_failed_etm_enabled:
|
||||
transform->psa_key_dec, transform->psa_alg);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1629,7 +1635,7 @@ hmac_failed_etm_enabled:
|
||||
status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1639,7 +1645,7 @@ hmac_failed_etm_enabled:
|
||||
data, rec->data_len, &olen);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1649,7 +1655,7 @@ hmac_failed_etm_enabled:
|
||||
&part_len);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -2639,7 +2645,12 @@ int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
|
||||
|
||||
/* Update running hashes of handshake messages seen */
|
||||
if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) {
|
||||
ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen);
|
||||
ret = ssl->handshake->update_checksum(ssl, ssl->out_msg,
|
||||
ssl->out_msglen);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3067,12 +3078,17 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
|
||||
int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
||||
|
||||
if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) {
|
||||
ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
|
||||
ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handshake message is complete, increment counter */
|
||||
@ -3103,6 +3119,7 @@ void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
|
||||
memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -3928,7 +3945,11 @@ int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
|
||||
|
||||
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
||||
update_hs_digest == 1) {
|
||||
mbedtls_ssl_update_handshake_status(ssl);
|
||||
ret = mbedtls_ssl_update_handshake_status(ssl);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
|
||||
@ -5578,8 +5599,10 @@ int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
|
||||
n = (len < ssl->in_msglen)
|
||||
? len : ssl->in_msglen;
|
||||
|
||||
memcpy(buf, ssl->in_offt, n);
|
||||
ssl->in_msglen -= n;
|
||||
if (len != 0) {
|
||||
memcpy(buf, ssl->in_offt, n);
|
||||
ssl->in_msglen -= n;
|
||||
}
|
||||
|
||||
/* Zeroising the plaintext buffer to erase unused application data
|
||||
from the memory. */
|
||||
@ -5655,7 +5678,9 @@ static int ssl_write_real(mbedtls_ssl_context *ssl,
|
||||
*/
|
||||
ssl->out_msglen = len;
|
||||
ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
|
||||
memcpy(ssl->out_msg, buf, len);
|
||||
if (len > 0) {
|
||||
memcpy(ssl->out_msg, buf, len);
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
|
||||
|
@ -30,6 +30,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Initialize context
|
||||
*/
|
||||
@ -91,7 +97,7 @@ static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
|
||||
psa_set_key_type(&attributes, key->key_type);
|
||||
psa_set_key_bits(&attributes, key->key_bits);
|
||||
|
||||
ret = psa_ssl_status_to_mbedtls(
|
||||
ret = PSA_TO_MBEDTLS_ERR(
|
||||
psa_import_key(&attributes, buf,
|
||||
PSA_BITS_TO_BYTES(key->key_bits),
|
||||
&key->key));
|
||||
@ -133,7 +139,7 @@ static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx)
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_destroy_key(ctx->keys[ctx->active].key)) != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
@ -169,7 +175,7 @@ int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_destroy_key(key->key)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -182,7 +188,7 @@ int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
|
||||
if ((status = psa_import_key(&attributes, k,
|
||||
PSA_BITS_TO_BYTES(key->key_bits),
|
||||
&key->key)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
@ -355,7 +361,7 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
|
||||
state, clear_len,
|
||||
state, end - state,
|
||||
&ciph_len)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
@ -465,7 +471,7 @@ int mbedtls_ssl_ticket_parse(void *p_ticket,
|
||||
key_name, TICKET_ADD_DATA_LEN,
|
||||
ticket, enc_len + TICKET_AUTH_TAG_BYTES,
|
||||
ticket, enc_len, &clear_len)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
|
@ -52,6 +52,15 @@
|
||||
#include "mbedtls/oid.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#define PSA_TO_MD_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_md_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
|
||||
|
||||
@ -418,8 +427,8 @@ static int tls_prf_sha256(const unsigned char *secret, size_t slen,
|
||||
const char *label,
|
||||
const unsigned char *random, size_t rlen,
|
||||
unsigned char *dstbuf, size_t dlen);
|
||||
static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
|
||||
static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
|
||||
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
@ -430,8 +439,8 @@ static int tls_prf_sha384(const unsigned char *secret, size_t slen,
|
||||
const unsigned char *random, size_t rlen,
|
||||
unsigned char *dstbuf, size_t dlen);
|
||||
|
||||
static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
|
||||
static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
||||
static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
|
||||
@ -444,14 +453,14 @@ static int ssl_tls12_session_load(mbedtls_ssl_session *session,
|
||||
size_t len);
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
||||
|
||||
static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
|
||||
@ -788,9 +797,9 @@ void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
}
|
||||
|
||||
void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
size_t total_hs_len)
|
||||
int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
size_t total_hs_len)
|
||||
{
|
||||
unsigned char hs_hdr[4];
|
||||
|
||||
@ -800,84 +809,152 @@ void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
|
||||
hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
|
||||
|
||||
ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
|
||||
return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
|
||||
}
|
||||
|
||||
void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
unsigned char const *msg,
|
||||
size_t msg_len)
|
||||
int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
unsigned char const *msg,
|
||||
size_t msg_len)
|
||||
{
|
||||
mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
|
||||
ssl->handshake->update_checksum(ssl, msg, msg_len);
|
||||
int ret;
|
||||
ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
return ssl->handshake->update_checksum(ssl, msg, msg_len);
|
||||
}
|
||||
|
||||
void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
|
||||
int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status;
|
||||
#else
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
#endif
|
||||
#else /* SHA-256 or SHA-384 */
|
||||
((void) ssl);
|
||||
#endif /* SHA-256 or SHA-384 */
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&ssl->handshake->fin_sha256_psa);
|
||||
psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
|
||||
status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
|
||||
mbedtls_md_free(&ssl->handshake->fin_sha256);
|
||||
mbedtls_md_init(&ssl->handshake->fin_sha256);
|
||||
ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
|
||||
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
|
||||
0);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&ssl->handshake->fin_sha384_psa);
|
||||
psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
|
||||
status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
|
||||
mbedtls_md_free(&ssl->handshake->fin_sha384);
|
||||
mbedtls_md_init(&ssl->handshake->fin_sha384);
|
||||
ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
|
||||
mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
{
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
|
||||
psa_status_t status;
|
||||
#else
|
||||
mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
|
||||
#else
|
||||
mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
|
||||
#endif
|
||||
#endif
|
||||
#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
!defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
(void) ssl;
|
||||
#else /* SHA-256 or SHA-384 */
|
||||
((void) ssl);
|
||||
(void) buf;
|
||||
(void) len;
|
||||
#endif /* SHA-256 or SHA-384 */
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return PSA_TO_MD_ERR(status);
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
|
||||
return PSA_TO_MD_ERR(psa_hash_update(
|
||||
&ssl->handshake->fin_sha256_psa, buf, len));
|
||||
#else
|
||||
mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
|
||||
return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *buf, size_t len)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
|
||||
return PSA_TO_MD_ERR(psa_hash_update(
|
||||
&ssl->handshake->fin_sha384_psa, buf, len));
|
||||
#else
|
||||
mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
|
||||
return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@ -889,19 +966,15 @@ static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
handshake->fin_sha256_psa = psa_hash_operation_init();
|
||||
psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
|
||||
#else
|
||||
mbedtls_sha256_init(&handshake->fin_sha256);
|
||||
mbedtls_sha256_starts(&handshake->fin_sha256, 0);
|
||||
mbedtls_md_init(&handshake->fin_sha256);
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
handshake->fin_sha384_psa = psa_hash_operation_init();
|
||||
psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
|
||||
#else
|
||||
mbedtls_sha512_init(&handshake->fin_sha384);
|
||||
mbedtls_sha512_starts(&handshake->fin_sha384, 1);
|
||||
mbedtls_md_init(&handshake->fin_sha384);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -971,6 +1044,8 @@ void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* Clear old handshake information if present */
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
if (ssl->transform_negotiate) {
|
||||
@ -1038,6 +1113,13 @@ static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
||||
mbedtls_ssl_transform_init(ssl->transform_negotiate);
|
||||
#endif
|
||||
|
||||
/* Setup handshake checksums */
|
||||
ret = mbedtls_ssl_reset_checksum(ssl);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
||||
defined(MBEDTLS_SSL_SRV_C) && \
|
||||
defined(MBEDTLS_SSL_SESSION_TICKETS)
|
||||
@ -1124,7 +1206,7 @@ static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
||||
if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
|
||||
continue;
|
||||
}
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
sig_algs_len += sizeof(uint16_t);
|
||||
#endif
|
||||
|
||||
@ -1152,7 +1234,7 @@ static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
||||
if (hash == MBEDTLS_SSL_HASH_NONE) {
|
||||
continue;
|
||||
}
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
*p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
|
||||
p++;
|
||||
#endif
|
||||
@ -4023,14 +4105,14 @@ void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&handshake->fin_sha256_psa);
|
||||
#else
|
||||
mbedtls_sha256_free(&handshake->fin_sha256);
|
||||
mbedtls_md_free(&handshake->fin_sha256);
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&handshake->fin_sha384_psa);
|
||||
#else
|
||||
mbedtls_sha512_free(&handshake->fin_sha384);
|
||||
mbedtls_md_free(&handshake->fin_sha384);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -4897,22 +4979,25 @@ static int ssl_preset_suiteb_ciphersuites[] = {
|
||||
*/
|
||||
static uint16_t ssl_preset_default_sig_algs[] = {
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
||||
MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
|
||||
MBEDTLS_ECP_DP_SECP256R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
||||
MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
|
||||
MBEDTLS_ECP_DP_SECP384R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
|
||||
defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
||||
MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
|
||||
#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
|
||||
MBEDTLS_ECP_DP_SECP521R1_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
|
||||
@ -4952,7 +5037,7 @@ static uint16_t ssl_preset_default_sig_algs[] = {
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
static uint16_t ssl_tls12_preset_default_sig_algs[] = {
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
|
||||
#endif
|
||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
||||
@ -4963,7 +5048,7 @@ static uint16_t ssl_tls12_preset_default_sig_algs[] = {
|
||||
#endif
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
|
||||
#endif
|
||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
||||
@ -4974,7 +5059,7 @@ static uint16_t ssl_tls12_preset_default_sig_algs[] = {
|
||||
#endif
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
|
||||
#endif
|
||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
||||
@ -5324,7 +5409,7 @@ void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PK_C) && \
|
||||
(defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
|
||||
(defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
|
||||
/*
|
||||
* Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
|
||||
*/
|
||||
@ -5335,7 +5420,7 @@ unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
|
||||
return MBEDTLS_SSL_SIG_RSA;
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
|
||||
return MBEDTLS_SSL_SIG_ECDSA;
|
||||
}
|
||||
@ -5363,7 +5448,7 @@ mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
|
||||
case MBEDTLS_SSL_SIG_RSA:
|
||||
return MBEDTLS_PK_RSA;
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
|
||||
case MBEDTLS_SSL_SIG_ECDSA:
|
||||
return MBEDTLS_PK_ECDSA;
|
||||
#endif
|
||||
@ -5371,7 +5456,7 @@ mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
|
||||
return MBEDTLS_PK_NONE;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
|
||||
#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
|
||||
|
||||
/*
|
||||
* Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
|
||||
@ -5701,7 +5786,7 @@ exit:
|
||||
!defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
(void) ssl;
|
||||
#endif
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
@ -5713,17 +5798,24 @@ static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
|
||||
size_t *olen)
|
||||
{
|
||||
int ret;
|
||||
mbedtls_sha512_context sha512;
|
||||
mbedtls_md_context_t sha384;
|
||||
|
||||
if (dst_len < 48) {
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
mbedtls_sha512_init(&sha512);
|
||||
mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
|
||||
mbedtls_md_init(&sha384);
|
||||
ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
|
||||
if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -5731,7 +5823,7 @@ static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_sha512_free(&sha512);
|
||||
mbedtls_md_free(&sha384);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
@ -5744,17 +5836,24 @@ static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
|
||||
size_t *olen)
|
||||
{
|
||||
int ret;
|
||||
mbedtls_sha256_context sha256;
|
||||
mbedtls_md_context_t sha256;
|
||||
|
||||
if (dst_len < 32) {
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
mbedtls_sha256_init(&sha256);
|
||||
mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
mbedtls_md_init(&sha256);
|
||||
ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
|
||||
if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -5762,7 +5861,7 @@ static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_sha256_free(&sha256);
|
||||
mbedtls_md_free(&sha256);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
@ -6285,7 +6384,10 @@ static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
|
||||
if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
|
||||
lbl = "extended master secret";
|
||||
seed = session_hash;
|
||||
handshake->calc_verify(ssl, session_hash, &seed_len);
|
||||
ret = handshake->calc_verify(ssl, session_hash, &seed_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
|
||||
session_hash, seed_len);
|
||||
@ -6513,9 +6615,9 @@ int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
|
||||
unsigned char *hash,
|
||||
size_t *hlen)
|
||||
int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
|
||||
unsigned char *hash,
|
||||
size_t *hlen)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
size_t hash_size;
|
||||
@ -6525,44 +6627,59 @@ void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
|
||||
status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
*hlen = 32;
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
|
||||
#else
|
||||
mbedtls_sha256_context sha256;
|
||||
|
||||
mbedtls_sha256_init(&sha256);
|
||||
exit:
|
||||
psa_hash_abort(&sha256_psa);
|
||||
return PSA_TO_MD_ERR(status);
|
||||
#else
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_context_t sha256;
|
||||
|
||||
mbedtls_md_init(&sha256);
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
|
||||
|
||||
mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
mbedtls_sha256_finish(&sha256, hash);
|
||||
ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_md_finish(&sha256, hash);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
*hlen = 32;
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
|
||||
|
||||
mbedtls_sha256_free(&sha256);
|
||||
exit:
|
||||
mbedtls_md_free(&sha256);
|
||||
return ret;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
return;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
|
||||
unsigned char *hash,
|
||||
size_t *hlen)
|
||||
int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
|
||||
unsigned char *hash,
|
||||
size_t *hlen)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
size_t hash_size;
|
||||
@ -6572,37 +6689,52 @@ void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
|
||||
status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
*hlen = 48;
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
|
||||
#else
|
||||
mbedtls_sha512_context sha512;
|
||||
|
||||
mbedtls_sha512_init(&sha512);
|
||||
exit:
|
||||
psa_hash_abort(&sha384_psa);
|
||||
return PSA_TO_MD_ERR(status);
|
||||
#else
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_context_t sha384;
|
||||
|
||||
mbedtls_md_init(&sha384);
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
|
||||
|
||||
mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
|
||||
mbedtls_sha512_finish(&sha512, hash);
|
||||
ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_md_finish(&sha384, hash);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
*hlen = 48;
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
|
||||
|
||||
mbedtls_sha512_free(&sha512);
|
||||
exit:
|
||||
mbedtls_md_free(&sha384);
|
||||
return ret;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
return;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
|
||||
@ -7545,7 +7677,7 @@ exit:
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_calc_finished_tls_sha256(
|
||||
static int ssl_calc_finished_tls_sha256(
|
||||
mbedtls_ssl_context *ssl, unsigned char *buf, int from)
|
||||
{
|
||||
int len = 12;
|
||||
@ -7556,7 +7688,8 @@ static void ssl_calc_finished_tls_sha256(
|
||||
psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
|
||||
psa_status_t status;
|
||||
#else
|
||||
mbedtls_sha256_context sha256;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_context_t sha256;
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_session *session = ssl->session_negotiate;
|
||||
@ -7575,23 +7708,28 @@ static void ssl_calc_finished_tls_sha256(
|
||||
|
||||
status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
|
||||
#else
|
||||
|
||||
mbedtls_sha256_init(&sha256);
|
||||
mbedtls_md_init(&sha256);
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
|
||||
|
||||
mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* TLSv1.2:
|
||||
@ -7599,15 +7737,14 @@ static void ssl_calc_finished_tls_sha256(
|
||||
* Hash( handshake ) )[0.11]
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_SHA256_ALT)
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
|
||||
sha256.state, sizeof(sha256.state));
|
||||
#endif
|
||||
|
||||
mbedtls_sha256_finish(&sha256, padbuf);
|
||||
mbedtls_sha256_free(&sha256);
|
||||
ret = mbedtls_md_finish(&sha256, padbuf);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
|
||||
|
||||
ssl->handshake->tls_prf(session->master, 48, sender,
|
||||
padbuf, 32, buf, len);
|
||||
|
||||
@ -7616,12 +7753,21 @@ static void ssl_calc_finished_tls_sha256(
|
||||
mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
|
||||
|
||||
exit:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&sha256_psa);
|
||||
return PSA_TO_MD_ERR(status);
|
||||
#else
|
||||
mbedtls_md_free(&sha256);
|
||||
return ret;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
static void ssl_calc_finished_tls_sha384(
|
||||
static int ssl_calc_finished_tls_sha384(
|
||||
mbedtls_ssl_context *ssl, unsigned char *buf, int from)
|
||||
{
|
||||
int len = 12;
|
||||
@ -7632,7 +7778,8 @@ static void ssl_calc_finished_tls_sha384(
|
||||
psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
|
||||
psa_status_t status;
|
||||
#else
|
||||
mbedtls_sha512_context sha512;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_context_t sha384;
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_session *session = ssl->session_negotiate;
|
||||
@ -7651,22 +7798,27 @@ static void ssl_calc_finished_tls_sha384(
|
||||
|
||||
status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
|
||||
return;
|
||||
goto exit;
|
||||
}
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
|
||||
#else
|
||||
mbedtls_sha512_init(&sha512);
|
||||
mbedtls_md_init(&sha384);
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
|
||||
|
||||
mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
|
||||
ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* TLSv1.2:
|
||||
@ -7674,14 +7826,13 @@ static void ssl_calc_finished_tls_sha384(
|
||||
* Hash( handshake ) )[0.11]
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_SHA512_ALT)
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
|
||||
sha512.state, sizeof(sha512.state));
|
||||
ret = mbedtls_md_finish(&sha384, padbuf);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
mbedtls_sha512_finish(&sha512, padbuf);
|
||||
|
||||
mbedtls_sha512_free(&sha512);
|
||||
#endif
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
|
||||
|
||||
ssl->handshake->tls_prf(session->master, 48, sender,
|
||||
padbuf, 48, buf, len);
|
||||
@ -7691,6 +7842,15 @@ static void ssl_calc_finished_tls_sha384(
|
||||
mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
|
||||
|
||||
exit:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&sha384_psa);
|
||||
return PSA_TO_MD_ERR(status);
|
||||
#else
|
||||
mbedtls_md_free(&sha384);
|
||||
return ret;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
|
||||
|
||||
@ -7787,7 +7947,10 @@ int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
|
||||
|
||||
mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
|
||||
|
||||
ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
|
||||
ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
|
||||
@ -7897,7 +8060,10 @@ int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
|
||||
|
||||
ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
|
||||
ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
||||
@ -8128,7 +8294,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
||||
&alg,
|
||||
&key_type,
|
||||
&key_bits)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
|
||||
goto end;
|
||||
}
|
||||
@ -8376,7 +8542,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
||||
PSA_BITS_TO_BYTES(key_bits),
|
||||
&transform->psa_key_enc)) != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
|
||||
goto end;
|
||||
}
|
||||
@ -8387,7 +8553,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
||||
key2,
|
||||
PSA_BITS_TO_BYTES(key_bits),
|
||||
&transform->psa_key_dec)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
|
||||
goto end;
|
||||
}
|
||||
@ -8450,7 +8616,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
||||
if ((status = psa_import_key(&attributes,
|
||||
mac_enc, mac_key_len,
|
||||
&transform->psa_mac_enc)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
|
||||
goto end;
|
||||
}
|
||||
@ -8471,7 +8637,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
||||
if ((status = psa_import_key(&attributes,
|
||||
mac_dec, mac_key_len,
|
||||
&transform->psa_mac_dec)) != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
|
||||
goto end;
|
||||
}
|
||||
@ -8526,7 +8692,7 @@ int mbedtls_psa_ecjpake_read_round(
|
||||
status = psa_pake_input(pake_ctx, step,
|
||||
buf + input_offset, length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
input_offset += length;
|
||||
@ -8568,7 +8734,7 @@ int mbedtls_psa_ecjpake_write_round(
|
||||
len - output_offset - 1,
|
||||
&output_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
*(buf + output_offset) = (uint8_t) output_len;
|
||||
|
@ -33,6 +33,9 @@
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#include "psa/crypto.h"
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#include <string.h>
|
||||
@ -1090,6 +1093,7 @@ static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
||||
const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
|
||||
uint16_t dtls_legacy_version;
|
||||
|
||||
@ -1160,7 +1164,11 @@ static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
|
||||
|
||||
/* Start over at ClientHello */
|
||||
ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
|
||||
mbedtls_ssl_reset_checksum(ssl);
|
||||
ret = mbedtls_ssl_reset_checksum(ssl);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
mbedtls_ssl_recv_flight_completed(ssl);
|
||||
|
||||
@ -2946,7 +2954,7 @@ ecdh_calc_secret:
|
||||
status = psa_generate_key(&key_attributes,
|
||||
&handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
/* Export the public part of the ECDH private key from PSA.
|
||||
@ -2963,7 +2971,7 @@ ecdh_calc_secret:
|
||||
if (status != PSA_SUCCESS) {
|
||||
psa_destroy_key(handshake->ecdh_psa_privkey);
|
||||
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
*p = (unsigned char) own_pubkey_len;
|
||||
@ -2995,9 +3003,9 @@ ecdh_calc_secret:
|
||||
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
} else if (destruction_status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(destruction_status);
|
||||
return PSA_TO_MBEDTLS_ERR(destruction_status);
|
||||
}
|
||||
|
||||
/* Write the ECDH computation length before the ECDH computation */
|
||||
@ -3283,7 +3291,11 @@ static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
|
||||
sign:
|
||||
#endif
|
||||
|
||||
ssl->handshake->calc_verify(ssl, hash, &hashlen);
|
||||
ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* digitally-signed struct {
|
||||
|
@ -34,6 +34,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
#include "mbedtls/ecp.h"
|
||||
#endif
|
||||
@ -1020,7 +1026,11 @@ read_record_header:
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, msg_len);
|
||||
|
||||
ssl->handshake->update_checksum(ssl, buf, msg_len);
|
||||
ret = ssl->handshake->update_checksum(ssl, buf, msg_len);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handshake layer:
|
||||
@ -2584,7 +2594,7 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
&key_attributes);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
ssl->handshake->ecdh_psa_type = psa_get_key_type(&key_attributes);
|
||||
@ -2631,7 +2641,7 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
status = psa_import_key(&key_attributes, buf, key_len,
|
||||
&ssl->handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@ -2952,7 +2962,7 @@ curve_matching_done:
|
||||
status = psa_generate_key(&key_attributes,
|
||||
&handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -2976,7 +2986,7 @@ curve_matching_done:
|
||||
own_pubkey, own_pubkey_max_len,
|
||||
&len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
|
||||
(void) psa_destroy_key(handshake->ecdh_psa_privkey);
|
||||
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
@ -3684,7 +3694,7 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
handshake->premaster, sizeof(handshake->premaster),
|
||||
&handshake->pmslen);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
|
||||
if (handshake->ecdh_psa_privkey_is_external == 0) {
|
||||
(void) psa_destroy_key(handshake->ecdh_psa_privkey);
|
||||
@ -3697,7 +3707,7 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
status = psa_destroy_key(handshake->ecdh_psa_privkey);
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -3890,9 +3900,9 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
} else if (destruction_status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(destruction_status);
|
||||
return PSA_TO_MBEDTLS_ERR(destruction_status);
|
||||
}
|
||||
|
||||
/* Write the ECDH computation length before the ECDH computation */
|
||||
@ -4129,7 +4139,11 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
|
||||
/* Calculate hash and verify signature */
|
||||
{
|
||||
size_t dummy_hlen;
|
||||
ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
|
||||
ret = ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_pk_verify(peer_pk,
|
||||
@ -4139,7 +4153,11 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
|
||||
return ret;
|
||||
}
|
||||
|
||||
mbedtls_ssl_update_handshake_status(ssl);
|
||||
ret = mbedtls_ssl_update_handshake_status(ssl);
|
||||
if (0 != ret) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
|
||||
|
||||
|
@ -34,6 +34,10 @@
|
||||
#include "ssl_tls13_keys.h"
|
||||
#include "ssl_debug_helpers.h"
|
||||
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
|
||||
/* Write extensions */
|
||||
|
||||
/*
|
||||
@ -188,7 +192,7 @@ static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
|
||||
/* Destroy generated private key. */
|
||||
status = psa_destroy_key(ssl->handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
|
||||
return ret;
|
||||
}
|
||||
@ -1270,7 +1274,7 @@ int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
|
||||
ssl->session_negotiate->ciphersuite);
|
||||
ssl->handshake->ciphersuite_info = ciphersuite_info;
|
||||
|
||||
/* Enable psk and psk_ephermal to make stage early happy */
|
||||
/* Enable psk and psk_ephemeral to make stage early happy */
|
||||
ssl->handshake->key_exchange_mode =
|
||||
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
|
||||
|
||||
@ -1489,8 +1493,9 @@ static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
|
||||
|
||||
ssl->keep_current_message = 1;
|
||||
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
||||
buf, (size_t) (end - buf));
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_SERVER_HELLO,
|
||||
buf, (size_t) (end - buf)));
|
||||
|
||||
if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
|
||||
ret = ssl_tls13_reset_key_share(ssl);
|
||||
@ -2056,8 +2061,9 @@ static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
|
||||
}
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_SERVER_HELLO, buf,
|
||||
buf_len));
|
||||
|
||||
if (is_hrr) {
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
|
||||
@ -2214,8 +2220,9 @@ static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
||||
buf, buf_len));
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
||||
if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
|
||||
@ -2259,8 +2266,8 @@ static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
|
||||
ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
|
||||
&buf, &buf_len));
|
||||
|
||||
mbedtls_ssl_add_hs_hdr_to_checksum(
|
||||
ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_hdr_to_checksum(
|
||||
ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0));
|
||||
|
||||
MBEDTLS_SSL_PROC_CHK(
|
||||
mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
|
||||
@ -2458,8 +2465,9 @@ static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(ssl,
|
||||
buf, buf + buf_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
||||
buf, buf_len));
|
||||
} else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
|
||||
ret = 0;
|
||||
} else {
|
||||
|
@ -36,6 +36,13 @@
|
||||
#include "ssl_tls13_keys.h"
|
||||
#include "ssl_debug_helpers.h"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
|
||||
const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
|
||||
MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
|
||||
{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
|
||||
@ -322,8 +329,9 @@ int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
|
||||
buf + buf_len, verify_buffer,
|
||||
verify_buffer_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
||||
buf, buf_len));
|
||||
|
||||
cleanup:
|
||||
|
||||
@ -752,8 +760,9 @@ int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
|
||||
/* Validate the certificate chain and set the verification results. */
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CERTIFICATE, buf,
|
||||
buf_len));
|
||||
|
||||
cleanup:
|
||||
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
||||
@ -868,8 +877,9 @@ int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
|
||||
buf + buf_len,
|
||||
&msg_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE,
|
||||
buf, msg_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CERTIFICATE, buf,
|
||||
msg_len));
|
||||
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
||||
ssl, buf_len, msg_len));
|
||||
@ -1013,7 +1023,7 @@ static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
|
||||
verify_hash, sizeof(verify_hash),
|
||||
&verify_hash_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
|
||||
@ -1070,8 +1080,9 @@ int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
|
||||
ssl, buf, buf + buf_len, &msg_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
||||
buf, msg_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf,
|
||||
msg_len));
|
||||
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
||||
ssl, buf_len, msg_len));
|
||||
@ -1171,8 +1182,8 @@ int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
|
||||
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(ssl, buf, buf + buf_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_FINISHED,
|
||||
buf, buf_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
|
||||
|
||||
cleanup:
|
||||
|
||||
@ -1248,8 +1259,8 @@ int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
|
||||
ssl, buf, buf + buf_len, &msg_len));
|
||||
|
||||
mbedtls_ssl_add_hs_msg_to_checksum(ssl, MBEDTLS_SSL_HS_FINISHED,
|
||||
buf, msg_len);
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
||||
MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
|
||||
|
||||
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
||||
ssl, buf_len, msg_len));
|
||||
@ -1388,7 +1399,7 @@ int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
|
||||
PSA_HASH_MAX_SIZE,
|
||||
&hash_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(4, "mbedtls_ssl_get_handshake_transcript", ret);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1399,37 +1410,20 @@ int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
|
||||
|
||||
hash_len += 4;
|
||||
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "Truncated SHA-256 handshake transcript",
|
||||
hash_transcript, hash_len);
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
|
||||
hash_transcript, hash_len);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&ssl->handshake->fin_sha256_psa);
|
||||
psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
|
||||
#else
|
||||
mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
|
||||
#endif
|
||||
/* Reset running hash and replace it with a hash of the transcript */
|
||||
ret = mbedtls_ssl_reset_checksum(ssl);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "Truncated SHA-384 handshake transcript",
|
||||
hash_transcript, hash_len);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_abort(&ssl->handshake->fin_sha384_psa);
|
||||
psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
|
||||
#else
|
||||
mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
|
||||
#endif
|
||||
ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
|
||||
defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
|
||||
ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
|
||||
#endif \
|
||||
/* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA || MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -1495,7 +1489,7 @@ int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
|
||||
status = psa_generate_key(&key_attributes,
|
||||
&handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
|
||||
return ret;
|
||||
|
||||
@ -1506,7 +1500,7 @@ int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
|
||||
buf, (size_t) (end - buf),
|
||||
&own_pubkey_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
|
||||
return ret;
|
||||
|
||||
|
@ -35,6 +35,10 @@
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
||||
psa_to_ssl_errors, \
|
||||
psa_generic_status_to_mbedtls)
|
||||
|
||||
#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
|
||||
.name = string,
|
||||
|
||||
@ -215,7 +219,7 @@ cleanup:
|
||||
abort_status = psa_key_derivation_abort(&operation);
|
||||
status = (status == PSA_SUCCESS ? abort_status : status);
|
||||
mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
@ -309,7 +313,7 @@ int mbedtls_ssl_tls13_derive_secret(
|
||||
status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
|
||||
PSA_HASH_LENGTH(hash_alg), &ctx_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
@ -416,7 +420,7 @@ int mbedtls_ssl_tls13_evolve_secret(
|
||||
cleanup:
|
||||
abort_status = psa_key_derivation_abort(&operation);
|
||||
status = (status == PSA_SUCCESS ? abort_status : status);
|
||||
ret = (ret == 0 ? psa_ssl_status_to_mbedtls(status) : ret);
|
||||
ret = (ret == 0 ? PSA_TO_MBEDTLS_ERR(status) : ret);
|
||||
mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
|
||||
return ret;
|
||||
}
|
||||
@ -740,19 +744,19 @@ static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
|
||||
|
||||
status = psa_import_key(&attributes, finished_key, hash_len, &key);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_mac_compute(key, alg, transcript, hash_len,
|
||||
dst, hash_len, dst_len);
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
|
||||
exit:
|
||||
|
||||
status = psa_destroy_key(key);
|
||||
if (ret == 0) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
|
||||
@ -1040,8 +1044,8 @@ int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
|
||||
&alg,
|
||||
&key_type,
|
||||
&key_bits)) != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls(status));
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", PSA_TO_MBEDTLS_ERR(status));
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
transform->psa_alg = alg;
|
||||
@ -1055,8 +1059,8 @@ int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
|
||||
key_enc,
|
||||
PSA_BITS_TO_BYTES(key_bits),
|
||||
&transform->psa_key_enc)) != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
||||
@ -1065,8 +1069,8 @@ int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
|
||||
key_dec,
|
||||
PSA_BITS_TO_BYTES(key_bits),
|
||||
&transform->psa_key_dec)) != PSA_SUCCESS) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
@ -1094,7 +1098,7 @@ static int ssl_tls13_get_cipher_key_info(
|
||||
status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
|
||||
&alg, &key_type, &key_bits);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
*key_len = PSA_BITS_TO_BYTES(key_bits);
|
||||
@ -1467,7 +1471,7 @@ static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
|
||||
status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
|
||||
&key_attributes);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
shared_secret_len = PSA_BITS_TO_BYTES(
|
||||
@ -1482,14 +1486,14 @@ static int ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
|
||||
handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
|
||||
shared_secret, shared_secret_len, &shared_secret_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
status = psa_destroy_key(handshake->ecdh_psa_privkey);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = psa_ssl_status_to_mbedtls(status);
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
|
||||
goto cleanup;
|
||||
}
|
||||
@ -1826,7 +1830,7 @@ int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
|
||||
|
||||
status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
|
||||
*psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
|
||||
@ -1840,7 +1844,7 @@ int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
|
||||
if (status != PSA_SUCCESS) {
|
||||
mbedtls_free((void *) *psk);
|
||||
*psk = NULL;
|
||||
return psa_ssl_status_to_mbedtls(status);
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user