mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-01-30 06:33:06 +00:00
Merge remote-tracking branch 'restricted/development-restricted' into update-development-r
Conflicts: programs/Makefile tests/scripts/check-generated-files.sh
This commit is contained in:
commit
047c724c22
@ -301,6 +301,8 @@ if(ENABLE_TESTING OR ENABLE_PROGRAMS)
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library)
|
||||
# Request C11, needed for memory poisoning tests
|
||||
set_target_properties(mbedtls_test PROPERTIES C_STANDARD 11)
|
||||
|
||||
file(GLOB MBEDTLS_TEST_HELPER_FILES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/src/test_helpers/*.c)
|
||||
|
8
ChangeLog.d/fix-Marvin-attack.txt
Normal file
8
ChangeLog.d/fix-Marvin-attack.txt
Normal file
@ -0,0 +1,8 @@
|
||||
Security
|
||||
* Fix a timing side channel in private key RSA operations. This side channel
|
||||
could be sufficient for an attacker to recover the plaintext. A local
|
||||
attacker or a remote attacker who is close to the victim on the network
|
||||
might have precise enough timing measurements to exploit this. It requires
|
||||
the attacker to send a large number of messages for decryption. For
|
||||
details, see "Everlasting ROBOT: the Marvin Attack", Hubert Kario. Reported
|
||||
by Hubert Kario, Red Hat.
|
@ -2,20 +2,7 @@ PANDOC = pandoc
|
||||
|
||||
default: all
|
||||
|
||||
all_markdown = \
|
||||
alternative-implementations.md \
|
||||
mbed-crypto-storage-specification.md \
|
||||
psa-crypto-implementation-structure.md \
|
||||
psa-migration/psa-limitations.md \
|
||||
psa-migration/strategy.md \
|
||||
psa-migration/tasks-g2.md \
|
||||
psa-migration/testing.md \
|
||||
testing/driver-interface-test-strategy.md \
|
||||
testing/invasive-testing.md \
|
||||
testing/psa-storage-format-testing.md \
|
||||
testing/test-framework.md \
|
||||
tls13-support.md \
|
||||
# This line is intentionally left blank
|
||||
all_markdown = $(wildcard *.md */*.md)
|
||||
|
||||
html: $(all_markdown:.md=.html)
|
||||
pdf: $(all_markdown:.md=.pdf)
|
||||
|
597
docs/architecture/psa-shared-memory.md
Normal file
597
docs/architecture/psa-shared-memory.md
Normal file
@ -0,0 +1,597 @@
|
||||
PSA API functions and shared memory
|
||||
===================================
|
||||
|
||||
## Introduction
|
||||
|
||||
This document discusses the security architecture of systems where PSA API functions might receive arguments that are in memory that is shared with an untrusted process. On such systems, the untrusted process might access a shared memory buffer while the cryptography library is using it, and thus cause unexpected behavior in the cryptography code.
|
||||
|
||||
### Core assumptions
|
||||
|
||||
We assume the following scope limitations:
|
||||
|
||||
* Only PSA Crypto API functions are in scope (including Mbed TLS extensions to the official API specification). Legacy crypto, X.509, TLS, or any other function which is not called `psa_xxx` is out of scope.
|
||||
* We only consider [input buffers](https://arm-software.github.io/psa-api/crypto/1.1/overview/conventions.html#input-buffer-sizes) and [output buffers](https://arm-software.github.io/psa-api/crypto/1.1/overview/conventions.html#output-buffer-sizes). Any other data is assumed to be in non-shared memory.
|
||||
|
||||
## System architecture discussion
|
||||
|
||||
### Architecture overview
|
||||
|
||||
We consider a system that has memory separation between partitions: a partition can't access another partition's memory directly. Partitions are meant to be isolated from each other: a partition may only affect the integrity of another partition via well-defined system interfaces. For example, this can be a Unix/POSIX-like system that isolates processes, or isolation between the secure world and the non-secure world relying on a mechanism such as TrustZone, or isolation between secure-world applications on such a system.
|
||||
|
||||
More precisely, we consider such a system where our PSA Crypto implementation is running inside one partition, called the **crypto service**. The crypto service receives remote procedure calls (RPC) from other partitions, validates their arguments (e.g. validation of key identifier ownership), and calls a PSA Crypto API function. This document is concerned with environments where the arguments passed to a PSA Crypto API function may be in shared memory (as opposed to environments where the inputs are always copied into memory that is solely accessible by the crypto service before calling the API function, and likewise with output buffers after the function returns).
|
||||
|
||||
When the data is accessible to another partition, there is a risk that this other partition will access it while the crypto implementation is working. Although this could be prevented by suspending the whole system while crypto is working, such a limitation is rarely desirable and most systems don't offer a way to do it. (Even systems that have absolute thread priorities, and where crypto has a higher priority than any untrusted partition, may be vulnerable due to having multiple cores or asynchronous data transfers with peripherals.)
|
||||
|
||||
The crypto service must guarantee that it behaves as if the rest of the world was suspended while it is executed. A behavior that is only possible if an untrusted entity accesses a buffer while the crypto service is processing the data is a security violation.
|
||||
|
||||
### Risks and vulnerabilities
|
||||
|
||||
We consider a security architecture with two or three entities:
|
||||
|
||||
* a crypto service, which offers PSA crypto API calls over RPC (remote procedure call) using shared memory for some input or output arguments;
|
||||
* a client of the crypto service, which makes a RPC to the crypto service;
|
||||
* in some scenarios, a client of the client, which makes a RPC to the crypto client which re-shares the memory with the crypto service.
|
||||
|
||||
The behavior of RPC is defined for in terms of values of inputs and outputs. This models an ideal world where the content of input and output buffers is not accessible outside the crypto service while it is processing an RPC. It is a security violation if the crypto service behaves in a way that cannot be achieved by setting the inputs before the RPC call, and reading the outputs after the RPC call is finished.
|
||||
|
||||
#### Read-read inconsistency
|
||||
|
||||
If an input argument is in shared memory, there is a risk of a **read-read inconsistency**:
|
||||
|
||||
1. The crypto code reads part of the input and validates it, or injects it into a calculation.
|
||||
2. The client (or client's client) modifies the input.
|
||||
3. The crypto code reads the same part again, and performs an action which would be impossible if the input had had the same value all along.
|
||||
|
||||
Vulnerability example (parsing): suppose the input contains data with a type-length-value or length-value encoding (for example, importing an RSA key). The crypto code reads the length field and checks that it fits within the buffer. (This could be the length of the overall data, or the length of an embedded field) Later, the crypto code reads the length again and uses it without validation. A malicious client can modify the length field in the shared memory between the two reads and thus cause a buffer overread on the second read.
|
||||
|
||||
Vulnerability example (dual processing): consider an RPC to perform authenticated encryption, using a mechanism with an encrypt-and-MAC structure. The authenticated encryption implementation separately calculates the ciphertext and the MAC from the plaintext. A client sets the plaintext input to `"PPPP"`, then starts the RPC call, then changes the input buffer to `"QQQQ"` while the crypto service is working.
|
||||
|
||||
* Any of `enc("PPPP")+mac("PPPP")`, `enc("PPQQ")+mac("PPQQ")` or `enc("QQQQ")+mac("QQQQ")` are valid outputs: they are outputs that can be produced by this authenticated encryption RPC.
|
||||
* If the authenticated encryption calculates the ciphertext before the client changes the output buffer and calculates the MAC after that change, reading the input buffer again each time, the output will be `enc("PPPP")+mac("QQQQ")`. There is no input that can lead to this output, hence this behavior violates the security guarantees of the crypto service.
|
||||
|
||||
#### Write-read inconsistency
|
||||
|
||||
If an output argument is in shared memory, there is a risk of a **write-read inconsistency**:
|
||||
|
||||
1. The crypto code writes some intermediate data into the output buffer.
|
||||
2. The client (or client's client) modifies the intermediate data.
|
||||
3. The crypto code reads the intermediate data back and continues the calculation, leading to an outcome that would not be possible if the intermediate data had not been modified.
|
||||
|
||||
Vulnerability example: suppose that an RSA signature function works by formatting the data in place in the output buffer, then applying the RSA private-key operation in place. (This is how `mbedtls_rsa_pkcs1_sign` works.) A malicious client may write badly formatted data into the buffer, so that the private-key operation is not a valid signature (e.g. it could be a decryption), violating the RSA key's usage policy.
|
||||
|
||||
Vulnerability example with chained calls: we consider the same RSA signature operation as before. In this example, we additionally assume that the data to sign comes from an attestation application which signs some data on behalf of a final client: the key and the data to sign are under the attestation application's control, and the final client must not be able to obtain arbitrary signatures. The final client shares an output buffer for the signature with the attestation application, and the attestation application re-shares this buffer with the crypto service. A malicious final client can modify the intermediate data and thus sign arbitrary data.
|
||||
|
||||
#### Write-write disclosure
|
||||
|
||||
If an output argument is in shared memory, there is a risk of a **write-write disclosure**:
|
||||
|
||||
1. The crypto code writes some intermediate data into the output buffer. This intermediate data must remain confidential.
|
||||
2. The client (or client's client) reads the intermediate data.
|
||||
3. The crypto code overwrites the intermediate data.
|
||||
|
||||
Vulnerability example with chained calls (temporary exposure): an application encrypts some data, and lets its clients store the ciphertext. Clients may not have access to the plaintext. To save memory, when it calls the crypto service, it passes an output buffer that is in the final client's memory. Suppose the encryption mechanism works by copying its input to the output buffer then encrypting in place (for example, to simplify considerations related to overlap, or because the implementation relies on a low-level API that works in place). In this scenario, the plaintext is exposed to the final client while the encryption in progress, which violates the confidentiality of the plaintext.
|
||||
|
||||
Vulnerability example with chained calls (backtrack): we consider a provisioning application that provides a data encryption service on behalf of multiple clients, using a single shared key. Clients are not allowed to access each other's data. The provisioning application isolates clients by including the client identity in the associated data. Suppose that an AEAD decryption function processes the ciphertext incrementally by simultaneously writing the plaintext to the output buffer and calculating the tag. (This is how AEAD decryption usually works.) At the end, if the tag is wrong, the decryption function wipes the output buffer. Assume that the output buffer for the plaintext is shared from the client to the provisioning application, which re-shares it with the crypto service. A malicious client can read another client (the victim)'s encrypted data by passing the ciphertext to the provisioning application, which will attempt to decrypt it with associated data identifying the requesting client. Although the operation will fail beacuse the tag is wrong, the malicious client still reads the victim plaintext.
|
||||
|
||||
#### Write-read feedback
|
||||
|
||||
If a function both has an input argument and an output argument in shared memory, and processes its input incrementally to emit output incrementally, the following sequence of events is possible:
|
||||
|
||||
1. The crypto code processes part of the input and writes the corresponding part of the output.
|
||||
2. The client reads the early output and uses that to calculate the next part of the input.
|
||||
3. The crypto code processes the rest of the input.
|
||||
|
||||
There are cryptographic mechanisms for which this breaks security properties. An example is [CBC encryption](https://link.springer.com/content/pdf/10.1007/3-540-45708-9_2.pdf): if the client can choose the content of a plaintext block after seeing the immediately preceding ciphertext block, this gives the client a decryption oracle. This is a security violation if the key policy only allowed the client to encrypt, not to decrypt.
|
||||
|
||||
TODO: is this a risk we want to take into account? Although this extends the possible behaviors of the one-shot interface, the client can do the same thing legitimately with the multipart interface.
|
||||
|
||||
### Possible countermeasures
|
||||
|
||||
In this section, we briefly discuss generic countermeasures.
|
||||
|
||||
#### Copying
|
||||
|
||||
Copying is a valid countermeasure. It is conceptually simple. However, it is often unattractive because it requires additional memory and time.
|
||||
|
||||
Note that although copying is very easy to write into a program, there is a risk that a compiler (especially with whole-program optimization) may optimize the copy away, if it does not understand that copies between shared memory and non-shared memory are semantically meaningful.
|
||||
|
||||
Example: the PSA Firmware Framework 1.0 forbids shared memory between partitions. This restriction is lifted in version 1.1 due to concerns over RAM usage.
|
||||
|
||||
#### Careful accesses
|
||||
|
||||
The following rules guarantee that shared memory cannot result in a security violation other than [write-read feedback](#write-read-feedback):
|
||||
|
||||
* Never read the same input twice at the same index.
|
||||
* Never read back from an output.
|
||||
* Never write to the output twice at the same index.
|
||||
* This rule can usefully be relaxed in many circumstances. It is ok to write data that is independent of the inputs (and not otherwise confidential), then overwrite it. For example, it is ok to zero the output buffer before starting to process the input.
|
||||
|
||||
These rules are very difficult to enforce.
|
||||
|
||||
Example: these are the rules that a GlobalPlatform TEE Trusted Application (application running on the secure side of TrustZone on Cortex-A) must follow.
|
||||
|
||||
## Protection requirements
|
||||
|
||||
### Responsibility for protection
|
||||
|
||||
A call to a crypto service to perform a crypto operation involves the following components:
|
||||
|
||||
1. The remote procedure call framework provided by the operating system.
|
||||
2. The code of the crypto service.
|
||||
3. The code of the PSA Crypto dispatch layer (also known as the core), which is provided by Mbed TLS.
|
||||
4. The driver implementing the cryptographic mechanism, which may be provided by Mbed TLS (built-in driver) or by a third-party driver.
|
||||
|
||||
The [PSA Crypto API specification](https://arm-software.github.io/psa-api/crypto/1.1/overview/conventions.html#stability-of-parameters) puts the responsibility for protection on the implementation of the PSA Crypto API, i.e. (3) or (4).
|
||||
|
||||
> In an environment with multiple threads or with shared memory, the implementation carefully accesses non-overlapping buffer parameters in order to prevent any security risk resulting from the content of the buffer being modified or observed during the execution of the function. (...)
|
||||
|
||||
In Mbed TLS 2.x and 3.x up to and including 3.5.0, there is no defense against buffers in shared memory. The responsibility shifts to (1) or (2), but this is not documented.
|
||||
|
||||
In the remainder of this chapter, we will discuss how to implement this high-level requirement where it belongs: inside the implementation of the PSA Crypto API. Note that this allows two possible levels: in the dispatch layer (independently of the implementation of each mechanism) or in the driver (specific to each implementation).
|
||||
|
||||
#### Protection in the dispatch layer
|
||||
|
||||
The dispatch layer has no control over how the driver layer will access buffers. Therefore the only possible protection at this layer method is to ensure that drivers have no access to shared memory. This means that any buffer located in shared memory must be copied into or out of a buffer in memory owned by the crypto service (heap or stack). This adds inefficiency, mostly in terms of RAM usage.
|
||||
|
||||
For buffers with a small static size limit, this is something we often do for convenience, especially with output buffers. However, as of Mbed TLS 3.5.0, it is not done systematically.
|
||||
|
||||
It is ok to skip the copy if it is known for sure that a buffer is not in shared memory. However, the location of the buffer is not under the control of Mbed TLS. This means skipping the copy would have to be a compile-time or run-time option which has to be set by the application using Mbed TLS. This is both an additional maintenance cost (more code to analyze, more testing burden), and a residual security risk in case the party who is responsible for setting this option does not set it correctly. As a consequence, Mbed TLS will not offer this configurability unless there is a compelling argument.
|
||||
|
||||
#### Protection in the driver layer
|
||||
|
||||
Putting the responsibility for protection in the driver layer increases the overall amount of work since there are more driver implementations than dispatch implementations. (This is true even inside Mbed TLS: almost all API functions have multiple underlying implementations, one for each algorithm.) It also increases the risk to the ecosystem since some drivers might not protect correctly. Therefore having drivers be responsible for protection is only a good choice if there is a definite benefit to it, compared to allocating an internal buffer and copying. An expected benefit in some cases is that there are practical protection methods other than copying.
|
||||
|
||||
Some cryptographic mechanisms are naturally implemented by processing the input in a single pass, with a low risk of ever reading the same byte twice, and by writing the final output directly into the output buffer. For such mechanism, it is sensible to mandate that drivers respect these rules.
|
||||
|
||||
In the next section, we will analyze how susceptible various cryptographic mechanisms are to shared memory vulnerabilities.
|
||||
|
||||
### Susceptibility of different mechanisms
|
||||
|
||||
#### Operations involving small buffers
|
||||
|
||||
For operations involving **small buffers**, the cost of copying is low. For many of those, the risk of not copying is high:
|
||||
|
||||
* Any parsing of formatted data has a high risk of [read-read inconsistency](#read-read-inconsistency).
|
||||
* An internal review shows that for RSA operations, it is natural for an implementation to have a [write-read inconsistency](#write-read-inconsistency) or a [write-write disclosure](#write-write-disclosure).
|
||||
|
||||
Note that in this context, a “small buffer” is one with a size limit that is known at compile time, and small enough that copying the data is not prohibitive. For example, an RSA key fits in a small buffer. A hash input is not a small buffer, even if it happens to be only a few bytes long in one particular call.
|
||||
|
||||
The following buffers are considered small buffers:
|
||||
|
||||
* Any input or output directly related to asymmetric cryptography (signature, encryption/decryption, key exchange, PAKE), including key import and export.
|
||||
* Note that this does not include inputs or outputs that are not processed by an asymmetric primitives, for example the message input to `psa_sign_message` or `psa_verify_message`.
|
||||
* Cooked key derivation output.
|
||||
* The output of a hash or MAC operation.
|
||||
|
||||
**Design decision: the dispatch layer shall copy all small buffers**.
|
||||
|
||||
#### Symmetric cryptography inputs with small output
|
||||
|
||||
Message inputs to hash, MAC and key derivation operations are at a low risk of [read-read inconsistency](#read-read-inconsistency) because they are unformatted data, and for all specified algorithms, it is natural to process the input one byte at a time.
|
||||
|
||||
**Design decision: require symmetric cryptography drivers to read their input without a risk of read-read inconsistency**.
|
||||
|
||||
TODO: what about IV/nonce inputs? They are typically small, but don't necessarily have a static size limit (e.g. GCM recommends a 12-byte nonce, but also allows large nonces).
|
||||
|
||||
#### Key derivation outputs
|
||||
|
||||
Key derivation typically emits its output as a stream, with no error condition detected after setup other than operational failures (e.g. communication failure with an accelerator) or running out of data to emit (which can easily be checked before emitting any data, since the data size is known in advance).
|
||||
|
||||
(Note that this is about raw byte output, not about cooked key derivation, i.e. deriving a structured key, which is considered a [small buffer](#operations-involving-small-buffers).)
|
||||
|
||||
**Design decision: require key derivation drivers to emit their output without reading back from the output buffer**.
|
||||
|
||||
#### Cipher and AEAD
|
||||
|
||||
AEAD decryption is at risk of [write-write disclosure](#write-write-disclosure) when the tag does not match.
|
||||
|
||||
AEAD encryption and decryption are at risk of [read-read inconsistency](#read-read-inconsistency) if they process the input multiple times, which is natural in a number of cases:
|
||||
|
||||
* when encrypting with an encrypt-and-authenticate or authenticate-then-encrypt structure (one read to calculate the authentication tag and another read to encrypt);
|
||||
* when decrypting with an encrypt-then-authenticate structure (one read to decrypt and one read to calculate the authentication tag);
|
||||
* with SIV modes (not yet present in the PSA API, but likely to come one day) (one full pass to calculate the IV, then another full pass for the core authenticated encryption);
|
||||
|
||||
Cipher and AEAD outputs are at risk of [write-read inconsistency](#write-read-inconsistency) and [write-write disclosure](#write-write-disclosure) if they are implemented by copying the input into the output buffer with `memmove`, then processing the data in place. In particular, this approach makes it easy to fully support overlapping, since `memmove` will take care of overlapping cases correctly, which is otherwise hard to do portably (C99 does not offer an efficient, portable way to check whether two buffers overlap).
|
||||
|
||||
**Design decision: the dispatch layer shall allocate an intermediate buffer for cipher and AEAD plaintext/ciphertext inputs and outputs**.
|
||||
|
||||
Note that this can be a single buffer for the input and the output if the driver supports in-place operation (which it is supposed to, since it is supposed to support arbitrary overlap, although this is not always the case in Mbed TLS, a [known issue](https://github.com/Mbed-TLS/mbedtls/issues/3266)). A side benefit of doing this intermediate copy is that overlap will be supported.
|
||||
|
||||
For all currently implemented AEAD modes, the associated data is only processed once to calculate an intermediate value of the authentication tag.
|
||||
|
||||
**Design decision: for now, require AEAD drivers to read the additional data without a risk of read-read inconsistency**. Make a note to revisit this when we start supporting an SIV mode, at which point the dispatch layer shall copy the input for modes that are not known to be low-risk.
|
||||
|
||||
#### Message signature
|
||||
|
||||
For signature algorithms with a hash-and-sign framework, the input to sign/verify-message is passed to a hash, and thus can follow the same rules as [symmetric cryptography inputs with small output](#symmetric-cryptography-inputs-with-small-output). This is also true for `PSA_ALG_RSA_PKCS1V15_SIGN_RAW`, which is the only non-hash-and-sign signature mechanism implemented in Mbed TLS 3.5. This is not true for PureEdDSA (`#PSA_ALG_PURE_EDDSA`), which is not yet implemented: [PureEdDSA signature](https://www.rfc-editor.org/rfc/rfc8032#section-5.1.6) processes the message twice. (However, PureEdDSA verification only processes the message once.)
|
||||
|
||||
**Design decision: for now, require sign/verify-message drivers to read their input without a risk of read-read inconsistency**. Make a note to revisit this when we start supporting PureEdDSA, at which point the dispatch layer shall copy the input for algorithms such as PureEdDSA that are not known to be low-risk.
|
||||
|
||||
## Design of shared memory protection
|
||||
|
||||
This section explains how Mbed TLS implements the shared memory protection strategy summarized below.
|
||||
|
||||
### Shared memory protection strategy
|
||||
|
||||
* The core (dispatch layer) shall make a copy of the following buffers, so that drivers do not receive arguments that are in shared memory:
|
||||
* Any input or output from asymmetric cryptography (signature, encryption/decryption, key exchange, PAKE), including key import and export.
|
||||
* Plaintext/ciphertext inputs and outputs for cipher and AEAD.
|
||||
* The output of a hash or MAC operation.
|
||||
* Cooked key derivation output.
|
||||
|
||||
* A document shall explain the requirements on drivers for arguments whose access needs to be protected:
|
||||
* Hash and MAC input.
|
||||
* Cipher/AEAD IV/nonce (to be confirmed).
|
||||
* AEAD associated data (to be confirmed).
|
||||
* Key derivation input (excluding key agreement).
|
||||
* Raw key derivation output (excluding cooked key derivation output).
|
||||
|
||||
* The built-in implementations of cryptographic mechanisms with arguments whose access needs to be protected shall protect those arguments.
|
||||
|
||||
Justification: see “[Susceptibility of different mechanisms](#susceptibility-of-different-mechanisms)”.
|
||||
|
||||
### Implementation of copying
|
||||
|
||||
Copy what needs copying. This is broadly straightforward, however there are a few things to consider.
|
||||
|
||||
#### Compiler optimization of copies
|
||||
|
||||
It is unclear whether the compiler will attempt to optimize away copying operations.
|
||||
|
||||
Once the copying code is implemented, it should be evaluated to see whether compiler optimization is a problem. Specifically, for the major compilers supported by Mbed TLS:
|
||||
* Write a small program that uses a PSA function which copies inputs or outputs.
|
||||
* Build the program with link-time optimization / full-program optimization enabled (e.g. `-flto` with `gcc`). Try also enabling the most extreme optimization options such as `-Ofast` (`gcc`) and `-Oz` (`clang`).
|
||||
* Inspect the generated code with `objdump` or a similar tool to see if copying operations are preserved.
|
||||
|
||||
If copying behaviour is preserved by all major compilers then assume that compiler optimization is not a problem.
|
||||
|
||||
If copying behaviour is optimized away by the compiler, further investigation is needed. Experiment with using the `volatile` keyword to force the compiler not to optimize accesses to the copied buffers. If the `volatile` keyword is not sufficient, we may be able to use compiler or target-specific techniques to prevent optimization, for example memory barriers or empty `asm` blocks. These may be implemented and verified for important platforms while retaining a C implementation that is likely to be correct on most platforms as a fallback - the same approach taken by the constant-time module.
|
||||
|
||||
**Open questions: Will the compiler optimize away copies? If so, can it be prevented from doing so in a portable way?**
|
||||
|
||||
#### Copying code
|
||||
|
||||
We may either copy buffers on an ad-hoc basis using `memcpy()` in each PSA function, or use a unified set of functions for copying input and output data. The advantages of the latter are obvious:
|
||||
|
||||
* Any test hooks need only be added in one place.
|
||||
* Copying code must only be reviewed for correctness in one place, rather than in all functions where it occurs.
|
||||
* Copy bypass is simpler as we can just replace these functions with no-ops in a single place.
|
||||
* Any complexity needed to prevent the compiler optimizing copies away does not have to be duplicated.
|
||||
|
||||
On the other hand, the only advantage of ad-hoc copying is slightly greater flexibility.
|
||||
|
||||
**Design decision: Create a unified set of functions for copying input and output data.**
|
||||
|
||||
#### Copying in multipart APIs
|
||||
|
||||
Multipart APIs may follow one of 2 possible approaches for copying of input:
|
||||
|
||||
##### 1. Allocate a buffer and copy input on each call to `update()`
|
||||
|
||||
This is simple and mirrors the approach for one-shot APIs nicely. However, allocating memory in the middle of a multi-part operation is likely to be bad for performance. Multipart APIs are designed in part for systems that do not have time to perform an operation at once, so introducing poor performance may be a problem here.
|
||||
|
||||
**Open question: Does memory allocation in `update()` cause a performance problem? If so, to what extent?**
|
||||
|
||||
##### 2. Allocate a buffer at the start of the operation and subdivide calls to `update()`
|
||||
|
||||
In this approach, input and output buffers are allocated at the start of the operation that are large enough to hold the expected average call to `update()`. When `update()` is called with larger buffers than these, the PSA API layer makes multiple calls to the driver, chopping the input into chunks of the temporary buffer size and filling the output from the results until the operation is finished.
|
||||
|
||||
This would be more complicated than approach (1) and introduces some extra issues. For example, if one of the intermediate calls to the driver's `update()` returns an error, it is not possible for the driver's state to be rolled back to before the first call to `update()`. It is unclear how this could be solved.
|
||||
|
||||
However, this approach would reduce memory usage in some cases and prevent memory allocation during an operation. Additionally, since the input and output buffers would be fixed-size it would be possible to allocate them statically, avoiding the need for any dynamic memory allocation at all.
|
||||
|
||||
**Design decision: Initially use approach (1) and treat approach (2) as an optimization to be done if necessary.**
|
||||
|
||||
### Validation of copying
|
||||
|
||||
#### Validation of copying by review
|
||||
|
||||
This is fairly self-explanatory. Review all functions that use shared memory and ensure that they each copy memory. This is the simplest strategy to implement but is less reliable than automated validation.
|
||||
|
||||
#### Validation of copying with memory pools
|
||||
|
||||
Proposed general idea: have tests where the test code calling API functions allocates memory in a certain pool, and code in the library allocates memory in a different pool. Test drivers check that needs-copying arguments are within the library pool, not within the test pool.
|
||||
|
||||
#### Validation of copying by memory poisoning
|
||||
|
||||
Proposed general idea: in test code, “poison” the memory area used by input and output parameters that must be copied. Poisoning means something that prevents accessing memory while it is poisoned. This could be via memory protection (allocate with `mmap` then disable access with `mprotect`), or some kind of poisoning for an analyzer such as MSan or Valgrind.
|
||||
|
||||
In the library, the code that does the copying temporarily unpoisons the memory by calling a test hook.
|
||||
|
||||
```c
|
||||
static void copy_to_user(void *copy_buffer, void *const input_buffer, size_t length) {
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (mbedtls_psa_core_poison_memory != NULL) {
|
||||
mbedtls_psa_core_poison_memory(copy_buffer, length, 0);
|
||||
}
|
||||
#endif
|
||||
memcpy(copy_buffer, input_buffer, length);
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (mbedtls_psa_core_poison_memory != NULL) {
|
||||
mbedtls_psa_core_poison_memory(copy_buffer, length, 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
```
|
||||
The reason to poison the memory before calling the library, rather than after the copy-in (and symmetrically for output buffers) is so that the test will fail if we forget to copy, or we copy the wrong thing. This would not be the case if we relied on the library's copy function to do the poisoning: that would only validate that the driver code does not access the memory on the condition that the copy is done as expected.
|
||||
|
||||
##### Options for implementing poisoning
|
||||
|
||||
There are several different ways that poisoning could be implemented:
|
||||
|
||||
1. Using Valgrind's memcheck tool. Valgrind provides a macro `VALGRIND_MAKE_MEM_NO_ACCESS` that allows manual memory poisoning. Valgrind memory poisoning is already used for constant-flow testing in Mbed TLS.
|
||||
2. Using Memory Sanitizer (MSan), which allows us to mark memory as uninitialized. This is also used for constant-flow testing. It is suitable for input buffers only, since it allows us to detect when a poisoned buffer is read but not when it is written.
|
||||
3. Using Address Sanitizer (ASan). This provides `ASAN_POISON_MEMORY_REGION` which marks memory as inaccessible.
|
||||
4. Allocating buffers separate pages and calling `mprotect()` to set pages as inaccessible. This has the disadvantage that we will have to manually ensure that buffers sit in their own pages, which likely means making a copy.
|
||||
5. Filling buffers with random data, keeping a copy of the original. For input buffers, keep a copy of the original and copy it back once the PSA function returns. For output buffers, fill them with random data and keep a separate copy of it. In the memory poisoning hooks, compare the copy of random data with the original to ensure that the output buffer has not been written directly.
|
||||
|
||||
Approach (2) is insufficient for the full testing we require as we need to be able to check both input and output buffers.
|
||||
|
||||
Approach (5) is simple and requires no extra tooling. It is likely to have good performance as it does not use any sanitizers. However, it requires the memory poisoning test hooks to maintain extra copies of the buffers, which seems difficult to implement in practice. Additionally, it does not precisely test the property we want to validate, so we are relying on the tests to fail if given random data as input. It is possible (if unlikely) that the PSA function will access the poisoned buffer without causing the test to fail. This becomes more likely when we consider test cases that call PSA functions on incorrect inputs to check that the correct error is returned. For these reasons, this memory poisoning approach seems unsuitable.
|
||||
|
||||
All three remaining approaches are suitable for our purposes. However, approach (4) is more complex than the other two. To implement it, we would need to allocate poisoned buffers in separate memory pages. They would require special handling and test code would likely have to be designed around this special handling.
|
||||
|
||||
Meanwhile, approaches (1) and (3) are much more convenient. We are simply required to call a special macro on some buffer that was allocated by us and the sanitizer takes care of everything else. Of these two, ASan appears to have a limitation related to buffer alignment. From code comments quoted in [the documentation](https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning):
|
||||
|
||||
> This function is not guaranteed to poison the whole region - it may poison only subregion of [addr, addr+size) due to ASan alignment restrictions.
|
||||
|
||||
Specifically, ASan will round the buffer size down to 8 bytes before poisoning due to details of its implementation. For more information on this, see [Microsoft documentation of this feature](https://learn.microsoft.com/en-us/cpp/sanitizers/asan-runtime?view=msvc-170#alignment-requirements-for-addresssanitizer-poisoning).
|
||||
|
||||
It should be possible to work around this by manually rounding buffer lengths up to the nearest multiple of 8 in the poisoning function, although it's remotely possible that this will cause other problems. Valgrind does not appear to have this limitation (unless Valgrind is simply more poorly documented). However, running tests under Valgrind causes a much greater slowdown compared with ASan. As a result, it would be beneficial to implement support for both Valgrind and ASan, to give the extra flexibility to choose either performance or accuracy as required. This should be simple as both have very similar memory poisoning interfaces.
|
||||
|
||||
**Design decision: Implement memory poisoning tests with both Valgrind's memcheck and ASan manual poisoning.**
|
||||
|
||||
**Question: Should we try to build memory poisoning validation on existing Mbed TLS tests, or write new tests for this?**
|
||||
|
||||
##### Validation with new tests
|
||||
|
||||
Validation with newly created tests would be simpler to implement than using existing tests, since the tests can be written to take into account memory poisoning. It is also possible to build such a testsuite using existing tests as a starting point - `mbedtls_test_psa_exercise_key` is a test helper that already exercises many PSA operations on a key. This would need to be extended to cover operations without keys (e.g. hashes) and multipart operations, but it provides a good base from which to build all of the required testing.
|
||||
|
||||
Additionally, we can ensure that all functions are exercised by automatically generating test data files.
|
||||
|
||||
##### Validation with existing tests
|
||||
|
||||
An alternative approach would be to integrate memory poisoning validation with existing tests. This has two main advantages:
|
||||
|
||||
* All of the tests are written already, potentially saving development time.
|
||||
* The code coverage of these tests is greater than would be achievable writing new tests from scratch. In practice this advantage is small as buffer copying will take place in the dispatch layer. The tests are therefore independent of the values of parameters passed to the driver, so extra coverage in these parameters does not gain anything.
|
||||
|
||||
It may be possible to transparently implement memory poisoning so that existing tests can work without modification. This would be achieved by replacing the implementation of `malloc()` with one that allocates poisoned buffers. However, there are some difficulties with this:
|
||||
|
||||
* Not all buffers allocated by tests are used as inputs and outputs to PSA functions being tested.
|
||||
* Those buffers that are inputs to a PSA function need to be unpoisoned right up until the function is called, so that they can be filled with input data.
|
||||
* Those buffers that are outputs from a PSA function need to be unpoisoned straight after the function returns, so that they can be read to check the output is correct.
|
||||
|
||||
These issues may be solved by creating some kind of test wrapper around every PSA function call that poisons the memory. However, it is unclear how straightforward this will be in practice. If this is simple to achieve, the extra coverage and time saved on new tests will be a benefit. If not, writing new tests is the best strategy.
|
||||
|
||||
**Design decision: Attempt to add memory poisoning transparently to existing tests. If this proves difficult, write new tests instead.**
|
||||
|
||||
#### Discussion of copying validation
|
||||
|
||||
Of all discussed approaches, validation by memory poisoning appears as the best. This is because it:
|
||||
|
||||
* Does not require complex linking against different versions of `malloc()` (as is the case with the memory pool approach).
|
||||
* Allows automated testing (unlike the review approach).
|
||||
|
||||
**Design decision: Use a memory poisoning approach to validate copying.**
|
||||
|
||||
### Shared memory protection requirements
|
||||
|
||||
TODO: write document and reference it here.
|
||||
|
||||
### Validation of careful access for built-in drivers
|
||||
|
||||
For PSA functions whose inputs and outputs are not copied, it is important that we validate that the builtin drivers are correctly accessing their inputs and outputs so as not to cause a security issue. Specifically, we must check that each memory location in a shared buffer is not accessed more than once by a driver function. In this section we examine various possible methods for performing this validation.
|
||||
|
||||
Note: We are focusing on read-read inconsistencies for now, as most of the cases where we aren't copying are inputs.
|
||||
|
||||
#### Review
|
||||
|
||||
As with validation of copying, the simplest method of validation we can implement is careful code review. This is the least desirable method of validation for several reasons:
|
||||
|
||||
1. It is tedious for the reviewers.
|
||||
2. Reviewers are prone to make mistakes (especially when performing tedious tasks).
|
||||
3. It requires engineering time linear in the number of PSA functions to be tested.
|
||||
4. It cannot assure the quality of third-party drivers, whereas automated tests can be ported to any driver implementation in principle.
|
||||
|
||||
If all other approaches turn out to be prohibitively difficult, code review exists as a fallback option. However, it should be understood that this is far from ideal.
|
||||
|
||||
#### Tests using `mprotect()`
|
||||
|
||||
Checking that a memory location is not accessed more than once may be achieved by using `mprotect()` on a Linux system to cause a segmentation fault whenever a memory access happens. Tests based on this approach are sketched below.
|
||||
|
||||
##### Linux mprotect+ptrace
|
||||
|
||||
Idea: call `mmap` to allocate memory for arguments and `mprotect` to deny or reenable access. Use `ptrace` from a parent process to react to SIGSEGV from a denied access. On SIGSEGV happening in the faulting region:
|
||||
|
||||
1. Use `ptrace` to execute a `mprotect` system call in the child to enable access. TODO: How? `ptrace` can modify registers and memory in the child, which includes changing parameters of a syscall that's about to be executed, but not directly cause the child process to execute a syscall that it wasn't about to execute.
|
||||
2. Use `ptrace` with `PTRACE_SINGLESTEP` to re-execute the failed load/store instrution.
|
||||
3. Use `ptrace` to execute a `mprotect` system call in the child to disable access.
|
||||
4. Use `PTRACE_CONT` to resume the child execution.
|
||||
|
||||
Record the addresses that are accessed. Mark the test as failed if the same address is read twice.
|
||||
|
||||
##### Debugger + mprotect
|
||||
|
||||
Idea: call `mmap` to allocate memory for arguments and `mprotect` to deny or reenable access. Use a debugger to handle SIGSEGV (Gdb: set signal catchpoint). If the segfault was due to accessing the protected region:
|
||||
|
||||
1. Execute `mprotect` to allow access.
|
||||
2. Single-step the load/store instruction.
|
||||
3. Execute `mprotect` to disable access.
|
||||
4. Continue execution.
|
||||
|
||||
Record the addresses that are accessed. Mark the test as failed if the same address is read twice. This part might be hard to do in the gdb language, so we may want to just log the addresses and then use a separate program to analyze the logs, or do the gdb tasks from Python.
|
||||
|
||||
#### Instrumentation (Valgrind)
|
||||
|
||||
An alternative approach is to use a dynamic instrumentation tool (the most obvious being Valgrind) to trace memory accesses and check that each of the important memory addresses is accessed no more than once.
|
||||
|
||||
Valgrind has no tool specifically that checks the property that we are looking for. However, it is possible to generate a memory trace with Valgrind using the following:
|
||||
|
||||
```
|
||||
valgrind --tool=lackey --trace-mem=yes --log-file=logfile ./myprogram
|
||||
```
|
||||
This will execute `myprogram` and dump a record of every memory access to `logfile`, with its address and data width. If `myprogram` is a test that does the following:
|
||||
|
||||
1. Set up input and output buffers for a PSA function call.
|
||||
2. Leak the start and end address of each buffer via `print()`.
|
||||
3. Write data into the input buffer exactly once.
|
||||
4. Call the PSA function.
|
||||
5. Read data from the output buffer exactly once.
|
||||
|
||||
Then it should be possible to parse the output from the program and from Valgrind and check that each location was accessed exactly twice: once by the program's setup and once by the PSA function.
|
||||
|
||||
#### Fixed Virtual Platform testing
|
||||
|
||||
It may be possible to measure double accesses by running tests on a Fixed Virtual Platform such as Corstone 310 ecosystem FVP, available [here](https://developer.arm.com/downloads/-/arm-ecosystem-fvps). There exists a pre-packaged example program for the Corstone 310 FVP available as part of the Open IoT SDK [here](https://git.gitlab.arm.com/iot/open-iot-sdk/examples/sdk-examples/-/tree/main/examples/mbedtls/cmsis-rtx/corstone-310) that could provide a starting point for a set of tests.
|
||||
|
||||
Running on an FVP allows two approaches to careful-access testing:
|
||||
|
||||
* Convenient scripted use of a debugger with [Iris](https://developer.arm.com/documentation/101196/latest/). This allows memory watchpoints to be set, perhaps more flexibly than with GDB.
|
||||
* Tracing of all memory accesses with [Tarmac Trace](https://developer.arm.com/documentation/100964/1123/Plug-ins-for-Fast-Models/TarmacTrace). To validate the single-access properties, the [processor memory access trace source](https://developer.arm.com/documentation/100964/1123/Plug-ins-for-Fast-Models/TarmacTrace/Processor-memory-access-trace) can be used to output all memory accesses happening on the FVP. This output can then be easily parsed and processed to ensure that the input and output buffers are accessed only once. The addresses of buffers can either be leaked by the program through printing to the serial port or set to fixed values in the FVP's linker script.
|
||||
|
||||
#### Discussion of careful-access validation
|
||||
|
||||
The best approach for validating the correctness of memory accesses is an open question that requires further investigation. To answer this question, each of the test strategies discussed above must be prototyped as follows:
|
||||
|
||||
1. Take 1-2 days to create a basic prototype of a test that uses the approach.
|
||||
2. Document the prototype - write a short guide that can be followed to arrive at the same prototype.
|
||||
3. Evaluate the prototype according to its usefulness. The criteria of evaluation should include:
|
||||
* Ease of implementation - Was the prototype simple to implement? Having implemented it, is it simple to extend it to do all of the required testing?
|
||||
* Flexibility - Could the prototype be extended to cover other careful-access testing that may be needed in future?
|
||||
* Performance - Does the test method perform well? Will it cause significant slowdown to CI jobs?
|
||||
* Ease of reproduction - Does the prototype require a particular platform or tool to be set up? How easy would it be for an external user to run the prototype?
|
||||
* Comprehensibility - Accounting for the lower code quality of a prototype, would developers unfamiliar with the tests based on the prototype be able to understand them easily?
|
||||
* Portability - How well can this approach be ported to multiple platforms? This would allow us to ensure that there are no double-accesses due to a bug that only affects a specific target.
|
||||
|
||||
Once each prototype is complete, choose the best approach to implement the careful-access testing. Implement tests using this approach for each of the PSA interfaces that require careful-access testing:
|
||||
|
||||
* Hash
|
||||
* MAC
|
||||
* AEAD (additional data only)
|
||||
* Key derivation
|
||||
* Asymmetric signature (input only)
|
||||
|
||||
##### New vs existing tests
|
||||
|
||||
Most of the test methods discussed above need extra setup. Some require leaking of buffer bounds, predictable memory access patterns or allocation of special buffers. FVP testing even requires the tests to be run on a non-host target.
|
||||
|
||||
With this complexity in mind it does not seem feasible to run careful-access tests using existing testsuites. Instead, new tests should be written that exercise the drivers in the required way. Fortunately, the only interfaces that need testing are hash, MAC, AEAD (testing over AD only), Key derivation and Asymmetric signature, which limits the number of new tests that must be written.
|
||||
|
||||
#### Validation of validation for careful-access
|
||||
|
||||
In order to ensure that the careful-access validation works, it is necessary to write tests to check that we can correctly detect careful-access violations when they occur. To do this, write a test function that:
|
||||
|
||||
* Reads its input multiple times at the same location.
|
||||
* Writes to its output multiple times at the same location.
|
||||
|
||||
Then, write a careful-access test for this function and ensure that it fails.
|
||||
|
||||
## Analysis of argument protection in built-in drivers
|
||||
|
||||
TODO: analyze the built-in implementations of mechanisms for which there is a requirement on drivers. By code inspection, how satisfied are we that they meet the requirement?
|
||||
|
||||
## Copy bypass
|
||||
|
||||
For efficiency, we are likely to want mechanisms to bypass the copy and process buffers directly in builds that are not affected by shared memory considerations.
|
||||
|
||||
Expand this section to document any mechanisms that bypass the copy.
|
||||
|
||||
Make sure that such mechanisms preserve the guarantees when buffers overlap.
|
||||
|
||||
## Detailed design
|
||||
|
||||
### Implementation by module
|
||||
|
||||
Module | Input protection strategy | Output protection strategy | Notes
|
||||
---|---|---|---
|
||||
Hash and MAC | Careful access | Careful access | Low risk of multiple-access as the input and output are raw unformatted data.
|
||||
Cipher | Copying | Copying |
|
||||
AEAD | Copying (careful access for additional data) | Copying |
|
||||
Key derivation | Careful access | Careful access |
|
||||
Asymmetric signature | Careful access | Copying | Inputs to signatures are passed to a hash. This will no longer hold once PureEdDSA support is implemented.
|
||||
Asymmetric encryption | Copying | Copying |
|
||||
Key agreement | Copying | Copying |
|
||||
PAKE | Copying | Copying |
|
||||
Key import / export | Copying | Copying | Keys may be imported and exported in DER format, which is a structured format and therefore susceptible to read-read inconsistencies and potentially write-read inconsistencies.
|
||||
|
||||
### Copying functions
|
||||
|
||||
As discussed in [Copying code](#copying-code), it is simpler to use a single unified API for copying. Therefore, we create the following functions:
|
||||
|
||||
* `psa_crypto_copy_input(const uint8_t *input, size_t input_length, uint8_t *input_copy, size_t input_copy_length)`
|
||||
* `psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_length, uint8_t *output, size_t output_length)`
|
||||
|
||||
These seem to be a repeat of the same function, however it is useful to retain two separate functions for input and output parameters so that we can use different test hooks in each when using memory poisoning for tests.
|
||||
|
||||
Given that the majority of functions will be allocating memory on the heap to copy, it may help to build convenience functions that allocate the memory as well. One function allocates and copies the buffers:
|
||||
|
||||
* `psa_crypto_alloc_and_copy(const uint8_t *input, size_t input_length, uint8_t *output, size_t output_length, struct {uint8_t *inp, size_t inp_len, uint8_t *out, size_t out_len} *buffers)`
|
||||
|
||||
This function allocates an input and output buffer in `buffers` and copy the input from the user-supplied input buffer to `buffers->inp`.
|
||||
|
||||
An analogous function is needed to copy and free the buffers:
|
||||
|
||||
* `psa_crypto_copy_and_free(struct {uint8_t *inp, size_t inp_len, uint8_t *out, size_t out_len} buffers, const uint8_t *input, size_t input_length, const uint8_t *output, size_t output_length)`
|
||||
|
||||
This function would first copy the `buffers->out` buffer to the user-supplied output buffer and then free `buffers->inp` and `buffers->out`.
|
||||
|
||||
Some PSA functions may not use these convenience functions as they may have local optimizations that reduce memory usage. For example, ciphers may be able to use a single intermediate buffer for both input and output.
|
||||
|
||||
### Implementation of copying validation
|
||||
|
||||
As discussed in the [design exploration of copying validation](#validation-of-copying), the best strategy for validation of copies appears to be validation by memory poisoning, implemented using Valgrind and ASan.
|
||||
|
||||
To perform memory poisoning, we must implement the function alluded to in [Validation of copying by memory poisoning](#validation-of-copying-by-memory-poisoning):
|
||||
```c
|
||||
mbedtls_psa_core_poison_memory(uint8_t *buffer, size_t length, int should_poison);
|
||||
```
|
||||
This should either poison or unpoison the given buffer based on the value of `should_poison`:
|
||||
|
||||
* When `should_poison == 1`, this is equivalent to calling `VALGRIND_MAKE_MEM_NOACCESS(buffer, length)` or `ASAN_POISON_MEMORY_REGION(buffer, length)`.
|
||||
* When `should_poison == 0`, this is equivalent to calling `VALGRIND_MAKE_MEM_DEFINED(buffer, length)` or `ASAN_UNPOISON_MEMORY_REGION(buffer, length)`.
|
||||
|
||||
The PSA copying function must then have test hooks implemented as outlined in [Validation of copying by memory poisoning](#validation-of-copying-by-memory-poisoning).
|
||||
|
||||
As discussed in [the design exploration](#validation-with-existing-tests), the preferred approach for implementing copy-testing is to implement it transparently using existing tests. This is specified in more detail below.
|
||||
|
||||
#### Transparent allocation-based memory poisoning
|
||||
|
||||
In order to implement transparent memory poisoning we require a wrapper around all PSA function calls that poisons any input and output buffers.
|
||||
|
||||
The easiest way to do this is to create wrapper functions that poison the memory and then `#define` PSA function names to be wrapped versions of themselves. For example, to replace `psa_aead_update()`:
|
||||
```c
|
||||
psa_status_t mem_poison_psa_aead_update(psa_aead_operation_t *operation,
|
||||
const uint8_t *input,
|
||||
size_t input_length,
|
||||
uint8_t *output,
|
||||
size_t output_size,
|
||||
size_t *output_length)
|
||||
{
|
||||
mbedtls_psa_core_poison_memory(input, input_length, 1);
|
||||
mbedtls_psa_core_poison_memory(output, output_size, 1);
|
||||
psa_status_t status = psa_aead_update(operation, input, input_length,
|
||||
output, output_size, output_length);
|
||||
mbedtls_psa_core_poison_memory(input, input_length, 0);
|
||||
mbedtls_psa_core_poison_memory(output, output_size, 0);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
#define psa_aead_update(...) mem_poison_psa_aead_update(__VA_ARGS__)
|
||||
```
|
||||
|
||||
#### Configuration of poisoning tests
|
||||
|
||||
Since the memory poisoning tests will require the use of interfaces specific to the sanitizers used to poison memory, they must be guarded by new config options, for example `MBEDTLS_TEST_PSA_COPYING_ASAN` and `MBEDTLS_TEST_PSA_COPYING_VALGRIND`, as well as `MBEDTLS_TEST_HOOKS`. These would be analogous to the existing `MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN` and `MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND`. Since they require special tooling and are for testing only, these options should not be present in `mbedtls_config.h`. Instead, they should be set only in a new component in `all.sh` that performs the copy testing with Valgrind or ASan.
|
||||
|
||||
#### Validation of validation for copying
|
||||
|
||||
To make sure that we can correctly detect functions that access their input/output buffers rather than the copies, it would be best to write a test function that misbehaves and test it with memory poisoning. Specifically, the function should:
|
||||
|
||||
* Read its input buffer and after calling the input-buffer-copying function to create a local copy of its input.
|
||||
* Write to its output buffer before and after calling the output-buffer-copying function to copy-back its output.
|
||||
|
||||
Then, we could write a test that uses this function with memory poisoning and ensure that it fails. Since we are expecting a failure due to memory-poisoning, we would run this test separately from the rest of the memory-poisoning testing.
|
||||
|
||||
However, performing this testing automatically is not urgent. It will suffice to manually verify that the test framework works, automatic tests are a 'nice to have' feature that may be left to future work.
|
@ -1468,6 +1468,22 @@
|
||||
*/
|
||||
//#define MBEDTLS_PSA_INJECT_ENTROPY
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_PSA_COPY_CALLER_BUFFERS
|
||||
*
|
||||
* Make local copies of buffers supplied by the callers of PSA functions.
|
||||
*
|
||||
* This should be enabled whenever caller-supplied buffers are owned by
|
||||
* an untrusted party, for example where arguments to PSA calls are passed
|
||||
* across a trust boundary.
|
||||
*
|
||||
* \note Enabling this option increases memory usage and code size.
|
||||
*
|
||||
* \note Disabling this option causes overlap of input and output buffers
|
||||
* not to be supported by PSA functions.
|
||||
*/
|
||||
#define MBEDTLS_PSA_COPY_CALLER_BUFFERS
|
||||
|
||||
/**
|
||||
* \def MBEDTLS_RSA_NO_CRT
|
||||
*
|
||||
|
@ -110,6 +110,118 @@ mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
|
||||
if (global_data.initialized == 0) \
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
|
||||
/* Declare a local copy of an input buffer and a variable that will be used
|
||||
* to store a pointer to the start of the buffer.
|
||||
*
|
||||
* Note: This macro must be called before any operations which may jump to
|
||||
* the exit label, so that the local input copy object is safe to be freed.
|
||||
*
|
||||
* Assumptions:
|
||||
* - input is the name of a pointer to the buffer to be copied
|
||||
* - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
|
||||
* - input_copy_name is a name that is unused in the current scope
|
||||
*/
|
||||
#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
|
||||
psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
|
||||
const uint8_t *input_copy_name = NULL;
|
||||
|
||||
/* Allocate a copy of the buffer input and set the pointer input_copy to
|
||||
* point to the start of the copy.
|
||||
*
|
||||
* Assumptions:
|
||||
* - psa_status_t status exists
|
||||
* - An exit label is declared
|
||||
* - input is the name of a pointer to the buffer to be copied
|
||||
* - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
|
||||
*/
|
||||
#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
|
||||
status = psa_crypto_local_input_alloc(input, length, \
|
||||
&LOCAL_INPUT_COPY_OF_##input); \
|
||||
if (status != PSA_SUCCESS) { \
|
||||
goto exit; \
|
||||
} \
|
||||
input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
|
||||
|
||||
/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
|
||||
*
|
||||
* Assumptions:
|
||||
* - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
|
||||
* - input is the name of the original buffer that was copied
|
||||
*/
|
||||
#define LOCAL_INPUT_FREE(input, input_copy) \
|
||||
input_copy = NULL; \
|
||||
psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
|
||||
|
||||
/* Declare a local copy of an output buffer and a variable that will be used
|
||||
* to store a pointer to the start of the buffer.
|
||||
*
|
||||
* Note: This macro must be called before any operations which may jump to
|
||||
* the exit label, so that the local output copy object is safe to be freed.
|
||||
*
|
||||
* Assumptions:
|
||||
* - output is the name of a pointer to the buffer to be copied
|
||||
* - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
|
||||
* - output_copy_name is a name that is unused in the current scope
|
||||
*/
|
||||
#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
|
||||
psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
|
||||
uint8_t *output_copy_name = NULL;
|
||||
|
||||
/* Allocate a copy of the buffer output and set the pointer output_copy to
|
||||
* point to the start of the copy.
|
||||
*
|
||||
* Assumptions:
|
||||
* - psa_status_t status exists
|
||||
* - An exit label is declared
|
||||
* - output is the name of a pointer to the buffer to be copied
|
||||
* - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
|
||||
*/
|
||||
#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
|
||||
status = psa_crypto_local_output_alloc(output, length, \
|
||||
&LOCAL_OUTPUT_COPY_OF_##output); \
|
||||
if (status != PSA_SUCCESS) { \
|
||||
goto exit; \
|
||||
} \
|
||||
output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
|
||||
|
||||
/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
|
||||
* after first copying back its contents to the original buffer.
|
||||
*
|
||||
* Assumptions:
|
||||
* - psa_status_t status exists
|
||||
* - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
|
||||
* - output is the name of the original buffer that was copied
|
||||
*/
|
||||
#define LOCAL_OUTPUT_FREE(output, output_copy) \
|
||||
output_copy = NULL; \
|
||||
do { \
|
||||
psa_status_t local_output_status; \
|
||||
local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
|
||||
if (local_output_status != PSA_SUCCESS) { \
|
||||
/* Since this error case is an internal error, it's more serious than \
|
||||
* any existing error code and so it's fine to overwrite the existing \
|
||||
* status. */ \
|
||||
status = local_output_status; \
|
||||
} \
|
||||
} while (0)
|
||||
#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
|
||||
#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
|
||||
const uint8_t *input_copy_name = NULL;
|
||||
#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
|
||||
input_copy = input;
|
||||
#define LOCAL_INPUT_FREE(input, input_copy) \
|
||||
input_copy = NULL;
|
||||
#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
|
||||
uint8_t *output_copy_name = NULL;
|
||||
#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
|
||||
output_copy = output;
|
||||
#define LOCAL_OUTPUT_FREE(output, output_copy) \
|
||||
output_copy = NULL;
|
||||
#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
|
||||
|
||||
|
||||
int psa_can_do_hash(psa_algorithm_t hash_alg)
|
||||
{
|
||||
(void) hash_alg;
|
||||
@ -4202,9 +4314,9 @@ psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
|
||||
|
||||
psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t *input,
|
||||
const uint8_t *input_external,
|
||||
size_t input_length,
|
||||
uint8_t *output,
|
||||
uint8_t *output_external,
|
||||
size_t output_size,
|
||||
size_t *output_length)
|
||||
{
|
||||
@ -4215,6 +4327,12 @@ psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
|
||||
size_t default_iv_length = 0;
|
||||
psa_key_attributes_t attributes;
|
||||
|
||||
LOCAL_INPUT_DECLARE(input_external, input);
|
||||
LOCAL_OUTPUT_DECLARE(output_external, output);
|
||||
|
||||
LOCAL_INPUT_ALLOC(input_external, input_length, input);
|
||||
LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
|
||||
|
||||
if (!PSA_ALG_IS_CIPHER(alg)) {
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
goto exit;
|
||||
@ -4270,6 +4388,9 @@ exit:
|
||||
*output_length = 0;
|
||||
}
|
||||
|
||||
LOCAL_INPUT_FREE(input_external, input);
|
||||
LOCAL_OUTPUT_FREE(output_external, output);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -8303,4 +8424,182 @@ psa_status_t psa_pake_abort(
|
||||
}
|
||||
#endif /* PSA_WANT_ALG_SOME_PAKE */
|
||||
|
||||
/* Memory copying test hooks. These are called before input copy, after input
|
||||
* copy, before output copy and after output copy, respectively.
|
||||
* They are used by memory-poisoning tests to temporarily unpoison buffers
|
||||
* while they are copied. */
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
|
||||
void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
|
||||
void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
|
||||
void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
|
||||
#endif
|
||||
|
||||
/** Copy from an input buffer to a local copy.
|
||||
*
|
||||
* \param[in] input Pointer to input buffer.
|
||||
* \param[in] input_len Length of the input buffer.
|
||||
* \param[out] input_copy Pointer to a local copy in which to store the input data.
|
||||
* \param[out] input_copy_len Length of the local copy buffer.
|
||||
* \return #PSA_SUCCESS, if the buffer was successfully
|
||||
* copied.
|
||||
* \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
|
||||
* copy is too small to hold contents of the
|
||||
* input buffer.
|
||||
*/
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
|
||||
uint8_t *input_copy, size_t input_copy_len)
|
||||
{
|
||||
if (input_len > input_copy_len) {
|
||||
return PSA_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (psa_input_pre_copy_hook != NULL) {
|
||||
psa_input_pre_copy_hook(input, input_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (input_len > 0) {
|
||||
memcpy(input_copy, input, input_len);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (psa_input_post_copy_hook != NULL) {
|
||||
psa_input_post_copy_hook(input, input_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
/** Copy from a local output buffer into a user-supplied one.
|
||||
*
|
||||
* \param[in] output_copy Pointer to a local buffer containing the output.
|
||||
* \param[in] output_copy_len Length of the local buffer.
|
||||
* \param[out] output Pointer to user-supplied output buffer.
|
||||
* \param[out] output_len Length of the user-supplied output buffer.
|
||||
* \return #PSA_SUCCESS, if the buffer was successfully
|
||||
* copied.
|
||||
* \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
|
||||
* user-supplied output buffer is too small to
|
||||
* hold the contents of the local buffer.
|
||||
*/
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
|
||||
uint8_t *output, size_t output_len)
|
||||
{
|
||||
if (output_len < output_copy_len) {
|
||||
return PSA_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (psa_output_pre_copy_hook != NULL) {
|
||||
psa_output_pre_copy_hook(output, output_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (output_copy_len > 0) {
|
||||
memcpy(output, output_copy, output_copy_len);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
if (psa_output_post_copy_hook != NULL) {
|
||||
psa_output_post_copy_hook(output, output_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
|
||||
psa_crypto_local_input_t *local_input)
|
||||
{
|
||||
psa_status_t status;
|
||||
|
||||
*local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
|
||||
|
||||
if (input_len == 0) {
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
local_input->buffer = mbedtls_calloc(input_len, 1);
|
||||
if (local_input->buffer == NULL) {
|
||||
/* Since we dealt with the zero-length case above, we know that
|
||||
* a NULL return value means a failure of allocation. */
|
||||
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
}
|
||||
/* From now on, we must free local_input->buffer on error. */
|
||||
|
||||
local_input->length = input_len;
|
||||
|
||||
status = psa_crypto_copy_input(input, input_len,
|
||||
local_input->buffer, local_input->length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return PSA_SUCCESS;
|
||||
|
||||
error:
|
||||
mbedtls_free(local_input->buffer);
|
||||
local_input->buffer = NULL;
|
||||
local_input->length = 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
|
||||
{
|
||||
mbedtls_free(local_input->buffer);
|
||||
local_input->buffer = NULL;
|
||||
local_input->length = 0;
|
||||
}
|
||||
|
||||
psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
|
||||
psa_crypto_local_output_t *local_output)
|
||||
{
|
||||
*local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
|
||||
|
||||
if (output_len == 0) {
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
local_output->buffer = mbedtls_calloc(output_len, 1);
|
||||
if (local_output->buffer == NULL) {
|
||||
/* Since we dealt with the zero-length case above, we know that
|
||||
* a NULL return value means a failure of allocation. */
|
||||
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
||||
}
|
||||
local_output->length = output_len;
|
||||
local_output->original = output;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
|
||||
{
|
||||
psa_status_t status;
|
||||
|
||||
if (local_output->buffer == NULL) {
|
||||
local_output->length = 0;
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
if (local_output->original == NULL) {
|
||||
/* We have an internal copy but nothing to copy back to. */
|
||||
return PSA_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
|
||||
status = psa_crypto_copy_output(local_output->buffer, local_output->length,
|
||||
local_output->original, local_output->length);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
mbedtls_free(local_output->buffer);
|
||||
local_output->buffer = NULL;
|
||||
local_output->length = 0;
|
||||
|
||||
return PSA_SUCCESS;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
@ -874,4 +874,74 @@ psa_status_t mbedtls_psa_verify_hash_complete(
|
||||
psa_status_t mbedtls_psa_verify_hash_abort(
|
||||
mbedtls_psa_verify_hash_interruptible_operation_t *operation);
|
||||
|
||||
typedef struct psa_crypto_local_input_s {
|
||||
uint8_t *buffer;
|
||||
size_t length;
|
||||
} psa_crypto_local_input_t;
|
||||
|
||||
#define PSA_CRYPTO_LOCAL_INPUT_INIT ((psa_crypto_local_input_t) { NULL, 0 })
|
||||
|
||||
/** Allocate a local copy of an input buffer and copy the contents into it.
|
||||
*
|
||||
* \param[in] input Pointer to input buffer.
|
||||
* \param[in] input_len Length of the input buffer.
|
||||
* \param[out] local_input Pointer to a psa_crypto_local_input_t struct
|
||||
* containing a local input copy.
|
||||
* \return #PSA_SUCCESS, if the buffer was successfully
|
||||
* copied.
|
||||
* \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
|
||||
* the buffer cannot be allocated.
|
||||
*/
|
||||
psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
|
||||
psa_crypto_local_input_t *local_input);
|
||||
|
||||
/** Free a local copy of an input buffer.
|
||||
*
|
||||
* \param[in] local_input Pointer to a psa_crypto_local_input_t struct
|
||||
* populated by a previous call to
|
||||
* psa_crypto_local_input_alloc().
|
||||
*/
|
||||
void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input);
|
||||
|
||||
typedef struct psa_crypto_local_output_s {
|
||||
uint8_t *original;
|
||||
uint8_t *buffer;
|
||||
size_t length;
|
||||
} psa_crypto_local_output_t;
|
||||
|
||||
#define PSA_CRYPTO_LOCAL_OUTPUT_INIT ((psa_crypto_local_output_t) { NULL, NULL, 0 })
|
||||
|
||||
/** Allocate a local copy of an output buffer.
|
||||
*
|
||||
* \note This does not copy any data from the original
|
||||
* output buffer but only allocates a buffer
|
||||
* whose contents will be copied back to the
|
||||
* original in a future call to
|
||||
* psa_crypto_local_output_free().
|
||||
*
|
||||
* \param[in] output Pointer to output buffer.
|
||||
* \param[in] output_len Length of the output buffer.
|
||||
* \param[out] local_output Pointer to a psa_crypto_local_output_t struct to
|
||||
* populate with the local output copy.
|
||||
* \return #PSA_SUCCESS, if the buffer was successfully
|
||||
* copied.
|
||||
* \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
|
||||
* the buffer cannot be allocated.
|
||||
*/
|
||||
psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
|
||||
psa_crypto_local_output_t *local_output);
|
||||
|
||||
/** Copy from a local copy of an output buffer back to the original, then
|
||||
* free the local copy.
|
||||
*
|
||||
* \param[in] local_output Pointer to a psa_crypto_local_output_t struct
|
||||
* populated by a previous call to
|
||||
* psa_crypto_local_output_alloc().
|
||||
* \return #PSA_SUCCESS, if the local output was
|
||||
* successfully copied back to the original.
|
||||
* \return #PSA_ERROR_CORRUPTION_DETECTED, if the output
|
||||
* could not be copied back to the original.
|
||||
*/
|
||||
psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output);
|
||||
|
||||
#endif /* PSA_CRYPTO_CORE_H */
|
||||
|
@ -72,6 +72,21 @@ psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
|
||||
psa_status_t psa_mac_key_can_do(
|
||||
psa_algorithm_t algorithm,
|
||||
psa_key_type_t key_type);
|
||||
|
||||
psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
|
||||
uint8_t *input_copy, size_t input_copy_len);
|
||||
|
||||
psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
|
||||
uint8_t *output, size_t output_len);
|
||||
|
||||
/*
|
||||
* Test hooks to use for memory unpoisoning/poisoning in copy functions.
|
||||
*/
|
||||
extern void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len);
|
||||
extern void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len);
|
||||
extern void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len);
|
||||
extern void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len);
|
||||
|
||||
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#endif /* PSA_CRYPTO_INVASIVE_H */
|
||||
|
@ -7,6 +7,9 @@ else
|
||||
DLOPEN_LDFLAGS ?=
|
||||
endif
|
||||
|
||||
ifdef RECORD_PSA_STATUS_COVERAGE_LOG
|
||||
LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
|
||||
endif
|
||||
DEP=${MBEDLIBS} ${MBEDTLS_TEST_OBJS}
|
||||
|
||||
# Only build the dlopen test in shared library builds, and not when building
|
||||
|
@ -78,6 +78,9 @@ foreach(exe IN LISTS executables_libs executables_mbedcrypto)
|
||||
target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
# Request C11, required for memory poisoning
|
||||
set_target_properties(${exe} PROPERTIES C_STANDARD 11)
|
||||
|
||||
# This emulates "if ( ... IN_LIST ... )" which becomes available in CMake 3.3
|
||||
list(FIND executables_libs ${exe} exe_index)
|
||||
if (${exe_index} GREATER -1)
|
||||
|
@ -28,10 +28,13 @@
|
||||
|
||||
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
||||
|
||||
#include <mbedtls/debug.h>
|
||||
#include <mbedtls/platform.h>
|
||||
#include <mbedtls/platform_util.h>
|
||||
#include "test/helpers.h"
|
||||
#include "test/macros.h"
|
||||
#include "test/memory.h"
|
||||
#include "common.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@ -59,6 +62,15 @@ static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t
|
||||
memset((void *) p, false_but_the_compiler_does_not_know, n);
|
||||
}
|
||||
|
||||
/* Simulate an access to the given object, to avoid compiler optimizations
|
||||
* in code that prepares or consumes the object. */
|
||||
static void do_nothing_with_object(void *p)
|
||||
{
|
||||
(void) p;
|
||||
}
|
||||
void(*volatile do_nothing_with_object_but_the_compiler_does_not_know)(void *) =
|
||||
do_nothing_with_object;
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
/* Test framework features */
|
||||
@ -143,6 +155,65 @@ void memory_leak(const char *name)
|
||||
/* Leak of a heap object */
|
||||
}
|
||||
|
||||
/* name = "test_memory_poison_%(start)_%(offset)_%(count)_%(direction)"
|
||||
* Poison a region starting at start from an 8-byte aligned origin,
|
||||
* encompassing count bytes. Access the region at offset from the start.
|
||||
* %(start), %(offset) and %(count) are decimal integers.
|
||||
* %(direction) is either the character 'r' for read or 'w' for write.
|
||||
*/
|
||||
void test_memory_poison(const char *name)
|
||||
{
|
||||
size_t start = 0, offset = 0, count = 0;
|
||||
char direction = 'r';
|
||||
if (sscanf(name,
|
||||
"%*[^0-9]%" MBEDTLS_PRINTF_SIZET
|
||||
"%*[^0-9]%" MBEDTLS_PRINTF_SIZET
|
||||
"%*[^0-9]%" MBEDTLS_PRINTF_SIZET
|
||||
"_%c",
|
||||
&start, &offset, &count, &direction) != 4) {
|
||||
mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
|
||||
return;
|
||||
}
|
||||
|
||||
union {
|
||||
long long ll;
|
||||
unsigned char buf[32];
|
||||
} aligned;
|
||||
memset(aligned.buf, 'a', sizeof(aligned.buf));
|
||||
|
||||
if (start > sizeof(aligned.buf)) {
|
||||
mbedtls_fprintf(stderr,
|
||||
"%s: start=%" MBEDTLS_PRINTF_SIZET
|
||||
" > size=%" MBEDTLS_PRINTF_SIZET,
|
||||
__func__, start, sizeof(aligned.buf));
|
||||
return;
|
||||
}
|
||||
if (start + count > sizeof(aligned.buf)) {
|
||||
mbedtls_fprintf(stderr,
|
||||
"%s: start+count=%" MBEDTLS_PRINTF_SIZET
|
||||
" > size=%" MBEDTLS_PRINTF_SIZET,
|
||||
__func__, start + count, sizeof(aligned.buf));
|
||||
return;
|
||||
}
|
||||
if (offset >= count) {
|
||||
mbedtls_fprintf(stderr,
|
||||
"%s: offset=%" MBEDTLS_PRINTF_SIZET
|
||||
" >= count=%" MBEDTLS_PRINTF_SIZET,
|
||||
__func__, offset, count);
|
||||
return;
|
||||
}
|
||||
|
||||
MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count);
|
||||
|
||||
if (direction == 'w') {
|
||||
aligned.buf[start + offset] = 'b';
|
||||
do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
|
||||
} else {
|
||||
do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
|
||||
mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
/* Threading */
|
||||
@ -291,6 +362,22 @@ metatest_t metatests[] = {
|
||||
{ "double_free", "asan", double_free },
|
||||
{ "read_uninitialized_stack", "msan", read_uninitialized_stack },
|
||||
{ "memory_leak", "asan", memory_leak },
|
||||
{ "test_memory_poison_0_0_8_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_0_8_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_7_8_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_7_8_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_0_1_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_0_1_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_1_2_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_0_1_2_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_0_8_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_0_8_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_7_8_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_7_8_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_0_1_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_0_1_w", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_1_2_r", "poison", test_memory_poison },
|
||||
{ "test_memory_poison_7_1_2_w", "poison", test_memory_poison },
|
||||
{ "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
|
||||
{ "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
|
||||
{ "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
|
||||
|
131
scripts/mbedtls_dev/c_parsing_helper.py
Normal file
131
scripts/mbedtls_dev/c_parsing_helper.py
Normal file
@ -0,0 +1,131 @@
|
||||
"""Helper functions to parse C code in heavily constrained scenarios.
|
||||
|
||||
Currently supported functionality:
|
||||
|
||||
* read_function_declarations: read function declarations from a header file.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
|
||||
### WARNING: the code in this file has not been extensively reviewed yet.
|
||||
### We do not think it is harmful, but it may be below our normal standards
|
||||
### for robustness and maintainability.
|
||||
|
||||
import re
|
||||
from typing import Dict, Iterable, Iterator, List, Optional, Tuple
|
||||
|
||||
|
||||
class ArgumentInfo:
|
||||
"""Information about an argument to an API function."""
|
||||
#pylint: disable=too-few-public-methods
|
||||
|
||||
_KEYWORDS = [
|
||||
'const', 'register', 'restrict',
|
||||
'int', 'long', 'short', 'signed', 'unsigned',
|
||||
]
|
||||
_DECLARATION_RE = re.compile(
|
||||
r'(?P<type>\w[\w\s*]*?)\s*' +
|
||||
r'(?!(?:' + r'|'.join(_KEYWORDS) + r'))(?P<name>\b\w+\b)?' +
|
||||
r'\s*(?P<suffix>\[[^][]*\])?\Z',
|
||||
re.A | re.S)
|
||||
|
||||
@classmethod
|
||||
def normalize_type(cls, typ: str) -> str:
|
||||
"""Normalize whitespace in a type."""
|
||||
typ = re.sub(r'\s+', r' ', typ)
|
||||
typ = re.sub(r'\s*\*', r' *', typ)
|
||||
return typ
|
||||
|
||||
def __init__(self, decl: str) -> None:
|
||||
self.decl = decl.strip()
|
||||
m = self._DECLARATION_RE.match(self.decl)
|
||||
if not m:
|
||||
raise ValueError(self.decl)
|
||||
self.type = self.normalize_type(m.group('type')) #type: str
|
||||
self.name = m.group('name') #type: Optional[str]
|
||||
self.suffix = m.group('suffix') if m.group('suffix') else '' #type: str
|
||||
|
||||
|
||||
class FunctionInfo:
|
||||
"""Information about an API function."""
|
||||
#pylint: disable=too-few-public-methods
|
||||
|
||||
# Regex matching the declaration of a function that returns void.
|
||||
VOID_RE = re.compile(r'\s*\bvoid\s*\Z', re.A)
|
||||
|
||||
def __init__(self, #pylint: disable=too-many-arguments
|
||||
filename: str,
|
||||
line_number: int,
|
||||
qualifiers: Iterable[str],
|
||||
return_type: str,
|
||||
name: str,
|
||||
arguments: List[str]) -> None:
|
||||
self.filename = filename
|
||||
self.line_number = line_number
|
||||
self.qualifiers = frozenset(qualifiers)
|
||||
self.return_type = return_type
|
||||
self.name = name
|
||||
self.arguments = [ArgumentInfo(arg) for arg in arguments]
|
||||
|
||||
def returns_void(self) -> bool:
|
||||
"""Whether the function returns void."""
|
||||
return bool(self.VOID_RE.search(self.return_type))
|
||||
|
||||
|
||||
# Match one C comment.
|
||||
# Note that we match both comment types, so things like // in a /*...*/
|
||||
# comment are handled correctly.
|
||||
_C_COMMENT_RE = re.compile(r'//(?:[^\n]|\\\n)*|/\*.*?\*/', re.S)
|
||||
_NOT_NEWLINES_RE = re.compile(r'[^\n]+')
|
||||
|
||||
def read_logical_lines(filename: str) -> Iterator[Tuple[int, str]]:
|
||||
"""Read logical lines from a file.
|
||||
|
||||
Logical lines are one or more physical line, with balanced parentheses.
|
||||
"""
|
||||
with open(filename, encoding='utf-8') as inp:
|
||||
content = inp.read()
|
||||
# Strip comments, but keep newlines for line numbering
|
||||
content = re.sub(_C_COMMENT_RE,
|
||||
lambda m: re.sub(_NOT_NEWLINES_RE, "", m.group(0)),
|
||||
content)
|
||||
lines = enumerate(content.splitlines(), 1)
|
||||
for line_number, line in lines:
|
||||
# Read a logical line, containing balanced parentheses.
|
||||
# We assume that parentheses are balanced (this should be ok
|
||||
# since comments have been stripped), otherwise there will be
|
||||
# a gigantic logical line at the end.
|
||||
paren_level = line.count('(') - line.count(')')
|
||||
while paren_level > 0:
|
||||
_, more = next(lines) #pylint: disable=stop-iteration-return
|
||||
paren_level += more.count('(') - more.count(')')
|
||||
line += '\n' + more
|
||||
yield line_number, line
|
||||
|
||||
_C_FUNCTION_DECLARATION_RE = re.compile(
|
||||
r'(?P<qualifiers>(?:(?:extern|inline|static)\b\s*)*)'
|
||||
r'(?P<return_type>\w[\w\s*]*?)\s*' +
|
||||
r'\b(?P<name>\w+)' +
|
||||
r'\s*\((?P<arguments>.*)\)\s*;',
|
||||
re.A | re.S)
|
||||
|
||||
def read_function_declarations(functions: Dict[str, FunctionInfo],
|
||||
filename: str) -> None:
|
||||
"""Collect function declarations from a C header file."""
|
||||
for line_number, line in read_logical_lines(filename):
|
||||
m = _C_FUNCTION_DECLARATION_RE.match(line)
|
||||
if not m:
|
||||
continue
|
||||
qualifiers = m.group('qualifiers').split()
|
||||
return_type = m.group('return_type')
|
||||
name = m.group('name')
|
||||
arguments = m.group('arguments').split(',')
|
||||
if len(arguments) == 1 and re.match(FunctionInfo.VOID_RE, arguments[0]):
|
||||
arguments = []
|
||||
# Note: we replace any existing declaration for the same name.
|
||||
functions[name] = FunctionInfo(filename, line_number,
|
||||
qualifiers,
|
||||
return_type,
|
||||
name,
|
||||
arguments)
|
473
scripts/mbedtls_dev/c_wrapper_generator.py
Normal file
473
scripts/mbedtls_dev/c_wrapper_generator.py
Normal file
@ -0,0 +1,473 @@
|
||||
"""Generate C wrapper functions.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
|
||||
### WARNING: the code in this file has not been extensively reviewed yet.
|
||||
### We do not think it is harmful, but it may be below our normal standards
|
||||
### for robustness and maintainability.
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import typing
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from .c_parsing_helper import ArgumentInfo, FunctionInfo
|
||||
from . import typing_util
|
||||
|
||||
|
||||
def c_declare(prefix: str, name: str, suffix: str) -> str:
|
||||
"""Format a declaration of name with the given type prefix and suffix."""
|
||||
if not prefix.endswith('*'):
|
||||
prefix += ' '
|
||||
return prefix + name + suffix
|
||||
|
||||
|
||||
WrapperInfo = typing.NamedTuple('WrapperInfo', [
|
||||
('argument_names', List[str]),
|
||||
('guard', Optional[str]),
|
||||
('wrapper_name', str),
|
||||
])
|
||||
|
||||
|
||||
class Base:
|
||||
"""Generate a C source file containing wrapper functions."""
|
||||
|
||||
# This class is designed to have many methods potentially overloaded.
|
||||
# Tell pylint not to complain about methods that have unused arguments:
|
||||
# child classes are likely to override those methods and need the
|
||||
# arguments in question.
|
||||
#pylint: disable=no-self-use,unused-argument
|
||||
|
||||
# Prefix prepended to the function's name to form the wrapper name.
|
||||
_WRAPPER_NAME_PREFIX = ''
|
||||
# Suffix appended to the function's name to form the wrapper name.
|
||||
_WRAPPER_NAME_SUFFIX = '_wrap'
|
||||
|
||||
# Functions with one of these qualifiers are skipped.
|
||||
_SKIP_FUNCTION_WITH_QUALIFIERS = frozenset(['inline', 'static'])
|
||||
|
||||
def __init__(self):
|
||||
"""Construct a wrapper generator object.
|
||||
"""
|
||||
self.program_name = os.path.basename(sys.argv[0])
|
||||
# To be populated in a derived class
|
||||
self.functions = {} #type: Dict[str, FunctionInfo]
|
||||
# Preprocessor symbol used as a guard against multiple inclusion in the
|
||||
# header. Must be set before writing output to a header.
|
||||
# Not used when writing .c output.
|
||||
self.header_guard = None #type: Optional[str]
|
||||
|
||||
def _write_prologue(self, out: typing_util.Writable, header: bool) -> None:
|
||||
"""Write the prologue of a C file.
|
||||
|
||||
This includes a description comment and some include directives.
|
||||
"""
|
||||
out.write("""/* Automatically generated by {}, do not edit! */
|
||||
|
||||
/* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
"""
|
||||
.format(self.program_name))
|
||||
if header:
|
||||
out.write("""
|
||||
#ifndef {guard}
|
||||
#define {guard}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {{
|
||||
#endif
|
||||
"""
|
||||
.format(guard=self.header_guard))
|
||||
out.write("""
|
||||
#include <mbedtls/build_info.h>
|
||||
""")
|
||||
|
||||
def _write_epilogue(self, out: typing_util.Writable, header: bool) -> None:
|
||||
"""Write the epilogue of a C file.
|
||||
"""
|
||||
if header:
|
||||
out.write("""
|
||||
#ifdef __cplusplus
|
||||
}}
|
||||
#endif
|
||||
|
||||
#endif /* {guard} */
|
||||
"""
|
||||
.format(guard=self.header_guard))
|
||||
out.write("""
|
||||
/* End of automatically generated file. */
|
||||
""")
|
||||
|
||||
def _wrapper_function_name(self, original_name: str) -> str:
|
||||
"""The name of the wrapper function.
|
||||
|
||||
By default, this adds a suffix.
|
||||
"""
|
||||
return (self._WRAPPER_NAME_PREFIX +
|
||||
original_name +
|
||||
self._WRAPPER_NAME_SUFFIX)
|
||||
|
||||
def _wrapper_declaration_start(self,
|
||||
function: FunctionInfo,
|
||||
wrapper_name: str) -> str:
|
||||
"""The beginning of the wrapper function declaration.
|
||||
|
||||
This ends just before the opening parenthesis of the argument list.
|
||||
|
||||
This is a string containing at least the return type and the
|
||||
function name. It may start with additional qualifiers or attributes
|
||||
such as `static`, `__attribute__((...))`, etc.
|
||||
"""
|
||||
return c_declare(function.return_type, wrapper_name, '')
|
||||
|
||||
def _argument_name(self,
|
||||
function_name: str,
|
||||
num: int,
|
||||
arg: ArgumentInfo) -> str:
|
||||
"""Name to use for the given argument in the wrapper function.
|
||||
|
||||
Argument numbers count from 0.
|
||||
"""
|
||||
name = 'arg' + str(num)
|
||||
if arg.name:
|
||||
name += '_' + arg.name
|
||||
return name
|
||||
|
||||
def _wrapper_declaration_argument(self,
|
||||
function_name: str,
|
||||
num: int, name: str,
|
||||
arg: ArgumentInfo) -> str:
|
||||
"""One argument definition in the wrapper function declaration.
|
||||
|
||||
Argument numbers count from 0.
|
||||
"""
|
||||
return c_declare(arg.type, name, arg.suffix)
|
||||
|
||||
def _underlying_function_name(self, function: FunctionInfo) -> str:
|
||||
"""The name of the underlying function.
|
||||
|
||||
By default, this is the name of the wrapped function.
|
||||
"""
|
||||
return function.name
|
||||
|
||||
def _return_variable_name(self, function: FunctionInfo) -> str:
|
||||
"""The name of the variable that will contain the return value."""
|
||||
return 'retval'
|
||||
|
||||
def _write_function_call(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
argument_names: List[str]) -> None:
|
||||
"""Write the call to the underlying function.
|
||||
"""
|
||||
# Note that the function name is in parentheses, to avoid calling
|
||||
# a function-like macro with the same name, since in typical usage
|
||||
# there is a function-like macro with the same name which is the
|
||||
# wrapper.
|
||||
call = '({})({})'.format(self._underlying_function_name(function),
|
||||
', '.join(argument_names))
|
||||
if function.returns_void():
|
||||
out.write(' {};\n'.format(call))
|
||||
else:
|
||||
ret_name = self._return_variable_name(function)
|
||||
ret_decl = c_declare(function.return_type, ret_name, '')
|
||||
out.write(' {} = {};\n'.format(ret_decl, call))
|
||||
|
||||
def _write_function_return(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
if_void: bool = False) -> None:
|
||||
"""Write a return statement.
|
||||
|
||||
If the function returns void, only write a statement if if_void is true.
|
||||
"""
|
||||
if function.returns_void():
|
||||
if if_void:
|
||||
out.write(' return;\n')
|
||||
else:
|
||||
ret_name = self._return_variable_name(function)
|
||||
out.write(' return {};\n'.format(ret_name))
|
||||
|
||||
def _write_function_body(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
argument_names: List[str]) -> None:
|
||||
"""Write the body of the wrapper code for the specified function.
|
||||
"""
|
||||
self._write_function_call(out, function, argument_names)
|
||||
self._write_function_return(out, function)
|
||||
|
||||
def _skip_function(self, function: FunctionInfo) -> bool:
|
||||
"""Whether to skip this function.
|
||||
|
||||
By default, static or inline functions are skipped.
|
||||
"""
|
||||
if not self._SKIP_FUNCTION_WITH_QUALIFIERS.isdisjoint(function.qualifiers):
|
||||
return True
|
||||
return False
|
||||
|
||||
_FUNCTION_GUARDS = {
|
||||
} #type: Dict[str, str]
|
||||
|
||||
def _function_guard(self, function: FunctionInfo) -> Optional[str]:
|
||||
"""A preprocessor condition for this function.
|
||||
|
||||
The wrapper will be guarded with `#if` on this condition, if not None.
|
||||
"""
|
||||
return self._FUNCTION_GUARDS.get(function.name)
|
||||
|
||||
def _wrapper_info(self, function: FunctionInfo) -> Optional[WrapperInfo]:
|
||||
"""Information about the wrapper for one function.
|
||||
|
||||
Return None if the function should be skipped.
|
||||
"""
|
||||
if self._skip_function(function):
|
||||
return None
|
||||
argument_names = [self._argument_name(function.name, num, arg)
|
||||
for num, arg in enumerate(function.arguments)]
|
||||
return WrapperInfo(
|
||||
argument_names=argument_names,
|
||||
guard=self._function_guard(function),
|
||||
wrapper_name=self._wrapper_function_name(function.name),
|
||||
)
|
||||
|
||||
def _write_function_prototype(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
wrapper: WrapperInfo,
|
||||
header: bool) -> None:
|
||||
"""Write the prototype of a wrapper function.
|
||||
|
||||
If header is true, write a function declaration, with a semicolon at
|
||||
the end. Otherwise just write the prototype, intended to be followed
|
||||
by the function's body.
|
||||
"""
|
||||
declaration_start = self._wrapper_declaration_start(function,
|
||||
wrapper.wrapper_name)
|
||||
arg_indent = ' '
|
||||
terminator = ';\n' if header else '\n'
|
||||
if function.arguments:
|
||||
out.write(declaration_start + '(\n')
|
||||
for num in range(len(function.arguments)):
|
||||
arg_def = self._wrapper_declaration_argument(
|
||||
function.name,
|
||||
num, wrapper.argument_names[num], function.arguments[num])
|
||||
arg_terminator = \
|
||||
(')' + terminator if num == len(function.arguments) - 1 else
|
||||
',\n')
|
||||
out.write(arg_indent + arg_def + arg_terminator)
|
||||
else:
|
||||
out.write(declaration_start + '(void)' + terminator)
|
||||
|
||||
def _write_c_function(self, out: typing_util.Writable,
|
||||
function: FunctionInfo) -> None:
|
||||
"""Write wrapper code for one function.
|
||||
|
||||
Do nothing if the function is skipped.
|
||||
"""
|
||||
wrapper = self._wrapper_info(function)
|
||||
if wrapper is None:
|
||||
return
|
||||
out.write("""
|
||||
/* Wrapper for {} */
|
||||
"""
|
||||
.format(function.name))
|
||||
if wrapper.guard is not None:
|
||||
out.write('#if {}\n'.format(wrapper.guard))
|
||||
self._write_function_prototype(out, function, wrapper, False)
|
||||
out.write('{\n')
|
||||
self._write_function_body(out, function, wrapper.argument_names)
|
||||
out.write('}\n')
|
||||
if wrapper.guard is not None:
|
||||
out.write('#endif /* {} */\n'.format(wrapper.guard))
|
||||
|
||||
def _write_h_function_declaration(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
wrapper: WrapperInfo) -> None:
|
||||
"""Write the declaration of one wrapper function.
|
||||
"""
|
||||
self._write_function_prototype(out, function, wrapper, True)
|
||||
|
||||
def _write_h_macro_definition(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
wrapper: WrapperInfo) -> None:
|
||||
"""Write the macro definition for one wrapper.
|
||||
"""
|
||||
arg_list = ', '.join(wrapper.argument_names)
|
||||
out.write('#define {function_name}({args}) \\\n {wrapper_name}({args})\n'
|
||||
.format(function_name=function.name,
|
||||
wrapper_name=wrapper.wrapper_name,
|
||||
args=arg_list))
|
||||
|
||||
def _write_h_function(self, out: typing_util.Writable,
|
||||
function: FunctionInfo) -> None:
|
||||
"""Write the complete header content for one wrapper.
|
||||
|
||||
This is the declaration of the wrapper function, and the
|
||||
definition of a function-like macro that calls the wrapper function.
|
||||
|
||||
Do nothing if the function is skipped.
|
||||
"""
|
||||
wrapper = self._wrapper_info(function)
|
||||
if wrapper is None:
|
||||
return
|
||||
out.write('\n')
|
||||
if wrapper.guard is not None:
|
||||
out.write('#if {}\n'.format(wrapper.guard))
|
||||
self._write_h_function_declaration(out, function, wrapper)
|
||||
self._write_h_macro_definition(out, function, wrapper)
|
||||
if wrapper.guard is not None:
|
||||
out.write('#endif /* {} */\n'.format(wrapper.guard))
|
||||
|
||||
def write_c_file(self, filename: str) -> None:
|
||||
"""Output a whole C file containing function wrapper definitions."""
|
||||
with open(filename, 'w', encoding='utf-8') as out:
|
||||
self._write_prologue(out, False)
|
||||
for name in sorted(self.functions):
|
||||
self._write_c_function(out, self.functions[name])
|
||||
self._write_epilogue(out, False)
|
||||
|
||||
def _header_guard_from_file_name(self, filename: str) -> str:
|
||||
"""Preprocessor symbol used as a guard against multiple inclusion."""
|
||||
# Heuristic to strip irrelevant leading directories
|
||||
filename = re.sub(r'.*include[\\/]', r'', filename)
|
||||
return re.sub(r'[^0-9A-Za-z]', r'_', filename, re.A).upper()
|
||||
|
||||
def write_h_file(self, filename: str) -> None:
|
||||
"""Output a header file with function wrapper declarations and macro definitions."""
|
||||
self.header_guard = self._header_guard_from_file_name(filename)
|
||||
with open(filename, 'w', encoding='utf-8') as out:
|
||||
self._write_prologue(out, True)
|
||||
for name in sorted(self.functions):
|
||||
self._write_h_function(out, self.functions[name])
|
||||
self._write_epilogue(out, True)
|
||||
|
||||
|
||||
class UnknownTypeForPrintf(Exception):
|
||||
"""Exception raised when attempting to generate code that logs a value of an unknown type."""
|
||||
|
||||
def __init__(self, typ: str) -> None:
|
||||
super().__init__("Unknown type for printf format generation: " + typ)
|
||||
|
||||
|
||||
class Logging(Base):
|
||||
"""Generate wrapper functions that log the inputs and outputs."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Construct a wrapper generator including logging of inputs and outputs.
|
||||
|
||||
Log to stdout by default. Call `set_stream` to change this.
|
||||
"""
|
||||
super().__init__()
|
||||
self.stream = 'stdout'
|
||||
|
||||
def set_stream(self, stream: str) -> None:
|
||||
"""Set the stdio stream to log to.
|
||||
|
||||
Call this method before calling `write_c_output` or `write_h_output`.
|
||||
"""
|
||||
self.stream = stream
|
||||
|
||||
def _write_prologue(self, out: typing_util.Writable, header: bool) -> None:
|
||||
super()._write_prologue(out, header)
|
||||
if not header:
|
||||
out.write("""
|
||||
#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS)
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <mbedtls/debug.h> // for MBEDTLS_PRINTF_SIZET
|
||||
#include <mbedtls/platform.h> // for mbedtls_fprintf
|
||||
#endif /* defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) */
|
||||
""")
|
||||
|
||||
_PRINTF_SIMPLE_FORMAT = {
|
||||
'int': '%d',
|
||||
'long': '%ld',
|
||||
'long long': '%lld',
|
||||
'size_t': '%"MBEDTLS_PRINTF_SIZET"',
|
||||
'unsigned': '0x%08x',
|
||||
'unsigned int': '0x%08x',
|
||||
'unsigned long': '0x%08lx',
|
||||
'unsigned long long': '0x%016llx',
|
||||
}
|
||||
|
||||
def _printf_simple_format(self, typ: str) -> Optional[str]:
|
||||
"""Use this printf format for a value of typ.
|
||||
|
||||
Return None if values of typ need more complex handling.
|
||||
"""
|
||||
return self._PRINTF_SIMPLE_FORMAT.get(typ)
|
||||
|
||||
_PRINTF_TYPE_CAST = {
|
||||
'int32_t': 'int',
|
||||
'uint32_t': 'unsigned',
|
||||
'uint64_t': 'unsigned long long',
|
||||
} #type: Dict[str, str]
|
||||
|
||||
def _printf_type_cast(self, typ: str) -> Optional[str]:
|
||||
"""Cast values of typ to this type before passing them to printf.
|
||||
|
||||
Return None if values of the given type do not need a cast.
|
||||
"""
|
||||
return self._PRINTF_TYPE_CAST.get(typ)
|
||||
|
||||
_POINTER_TYPE_RE = re.compile(r'\s*\*\Z')
|
||||
|
||||
def _printf_parameters(self, typ: str, var: str) -> Tuple[str, List[str]]:
|
||||
"""The printf format and arguments for a value of type typ stored in var.
|
||||
"""
|
||||
expr = var
|
||||
base_type = typ
|
||||
# For outputs via a pointer, get the value that has been written.
|
||||
# Note: we don't support pointers to pointers here.
|
||||
pointer_match = self._POINTER_TYPE_RE.search(base_type)
|
||||
if pointer_match:
|
||||
base_type = base_type[:pointer_match.start(0)]
|
||||
expr = '*({})'.format(expr)
|
||||
# Maybe cast the value to a standard type.
|
||||
cast_to = self._printf_type_cast(base_type)
|
||||
if cast_to is not None:
|
||||
expr = '({}) {}'.format(cast_to, expr)
|
||||
base_type = cast_to
|
||||
# Try standard types.
|
||||
fmt = self._printf_simple_format(base_type)
|
||||
if fmt is not None:
|
||||
return '{}={}'.format(var, fmt), [expr]
|
||||
raise UnknownTypeForPrintf(typ)
|
||||
|
||||
def _write_function_logging(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
argument_names: List[str]) -> None:
|
||||
"""Write code to log the function's inputs and outputs."""
|
||||
formats, values = '%s', ['"' + function.name + '"']
|
||||
for arg_info, arg_name in zip(function.arguments, argument_names):
|
||||
fmt, vals = self._printf_parameters(arg_info.type, arg_name)
|
||||
if fmt:
|
||||
formats += ' ' + fmt
|
||||
values += vals
|
||||
if not function.returns_void():
|
||||
ret_name = self._return_variable_name(function)
|
||||
fmt, vals = self._printf_parameters(function.return_type, ret_name)
|
||||
if fmt:
|
||||
formats += ' ' + fmt
|
||||
values += vals
|
||||
out.write("""\
|
||||
#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS)
|
||||
if ({stream}) {{
|
||||
mbedtls_fprintf({stream}, "{formats}\\n",
|
||||
{values});
|
||||
}}
|
||||
#endif /* defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) */
|
||||
"""
|
||||
.format(stream=self.stream,
|
||||
formats=formats,
|
||||
values=', '.join(values)))
|
||||
|
||||
def _write_function_body(self, out: typing_util.Writable,
|
||||
function: FunctionInfo,
|
||||
argument_names: List[str]) -> None:
|
||||
"""Write the body of the wrapper code for the specified function.
|
||||
"""
|
||||
self._write_function_call(out, function, argument_names)
|
||||
self._write_function_logging(out, function, argument_names)
|
||||
self._write_function_return(out, function)
|
@ -254,6 +254,8 @@ function(add_test_suite suite_name)
|
||||
target_include_directories(test_suite_${data_name}
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
|
||||
# Request C11, which is needed for memory poisoning tests
|
||||
set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11)
|
||||
|
||||
if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
|
||||
message(STATUS "The test suite ${data_name} will not be executed.")
|
||||
|
107
tests/include/test/memory.h
Normal file
107
tests/include/test/memory.h
Normal file
@ -0,0 +1,107 @@
|
||||
/**
|
||||
* \file memory.h
|
||||
*
|
||||
* \brief Helper macros and functions related to testing memory management.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef TEST_MEMORY_H
|
||||
#define TEST_MEMORY_H
|
||||
|
||||
#include "mbedtls/build_info.h"
|
||||
#include "mbedtls/platform.h"
|
||||
#include "test/helpers.h"
|
||||
|
||||
/** \def MBEDTLS_TEST_MEMORY_CAN_POISON
|
||||
*
|
||||
* This macro is defined if the tests are compiled with a method to mark
|
||||
* memory as poisoned, which can be used to enforce some memory access
|
||||
* policies.
|
||||
*
|
||||
* Support for the C11 thread_local keyword is also required.
|
||||
*
|
||||
* Currently, only Asan (Address Sanitizer) is supported.
|
||||
*/
|
||||
#if defined(MBEDTLS_TEST_HAVE_ASAN) && \
|
||||
(__STDC_VERSION__ >= 201112L)
|
||||
# define MBEDTLS_TEST_MEMORY_CAN_POISON
|
||||
#endif
|
||||
|
||||
/** \def MBEDTLS_TEST_MEMORY_POISON(buf, size)
|
||||
*
|
||||
* Poison a memory area so that any attempt to read or write from it will
|
||||
* cause a runtime failure.
|
||||
*
|
||||
* Depending on the implementation, this may poison a few bytes beyond the
|
||||
* indicated region, but will never poison a separate object on the heap
|
||||
* or a separate object with more than the alignment of a long long.
|
||||
*
|
||||
* The behavior is undefined if any part of the memory area is invalid.
|
||||
*
|
||||
* This is a no-op in builds without a poisoning method.
|
||||
* See #MBEDTLS_TEST_MEMORY_CAN_POISON.
|
||||
*
|
||||
* \param buf Pointer to the beginning of the memory area to poison.
|
||||
* \param size Size of the memory area in bytes.
|
||||
*/
|
||||
|
||||
/** \def MBEDTLS_TEST_MEMORY_UNPOISON(buf, size)
|
||||
*
|
||||
* Undo the effect of #MBEDTLS_TEST_MEMORY_POISON.
|
||||
*
|
||||
* The behavior is undefined if any part of the memory area is invalid,
|
||||
* or if the memory area contains a mixture of poisoned and unpoisoned parts.
|
||||
*
|
||||
* This is a no-op in builds without a poisoning method.
|
||||
* See #MBEDTLS_TEST_MEMORY_CAN_POISON.
|
||||
*
|
||||
* \param buf Pointer to the beginning of the memory area to unpoison.
|
||||
* \param size Size of the memory area in bytes.
|
||||
*/
|
||||
|
||||
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
|
||||
/** Thread-local variable used to enable memory poisoning. This is set and
|
||||
* unset in the test wrappers so that calls to PSA functions from the library
|
||||
* do not poison memory.
|
||||
*/
|
||||
extern _Thread_local unsigned int mbedtls_test_memory_poisoning_count;
|
||||
|
||||
/** Poison a memory area so that any attempt to read or write from it will
|
||||
* cause a runtime failure.
|
||||
*
|
||||
* The behavior is undefined if any part of the memory area is invalid.
|
||||
*/
|
||||
void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
|
||||
#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
|
||||
do { \
|
||||
mbedtls_test_memory_poisoning_count++; \
|
||||
mbedtls_test_memory_poison(ptr, size); \
|
||||
} while (0)
|
||||
|
||||
/** Undo the effect of mbedtls_test_memory_poison().
|
||||
*
|
||||
* This is a no-op if the given area is entirely valid, unpoisoned memory.
|
||||
*
|
||||
* The behavior is undefined if any part of the memory area is invalid,
|
||||
* or if the memory area contains a mixture of poisoned and unpoisoned parts.
|
||||
*/
|
||||
void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
|
||||
#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
|
||||
do { \
|
||||
mbedtls_test_memory_unpoison(ptr, size); \
|
||||
if (mbedtls_test_memory_poisoning_count != 0) { \
|
||||
mbedtls_test_memory_poisoning_count--; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
|
||||
#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) (ptr), (void) (size))
|
||||
#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) (ptr), (void) (size))
|
||||
#endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */
|
||||
|
||||
#endif /* TEST_MEMORY_H */
|
@ -16,6 +16,12 @@
|
||||
#include <psa/crypto.h>
|
||||
#endif
|
||||
|
||||
#include "test/psa_test_wrappers.h"
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
|
||||
&& defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
#include "test/psa_memory_poisoning_wrappers.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
/** Initialize the PSA Crypto subsystem. */
|
||||
|
40
tests/include/test/psa_memory_poisoning_wrappers.h
Normal file
40
tests/include/test/psa_memory_poisoning_wrappers.h
Normal file
@ -0,0 +1,40 @@
|
||||
/** Support for memory poisoning wrappers for PSA functions.
|
||||
*
|
||||
* The wrappers poison the input and output buffers of each function
|
||||
* before calling it, to ensure that it does not access the buffers
|
||||
* except by calling the approved buffer-copying functions.
|
||||
*
|
||||
* This header declares support functions. The wrappers themselves are
|
||||
* decalred in the automatically generated file `test/psa_test_wrappers.h`.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef PSA_MEMORY_POISONING_WRAPPERS_H
|
||||
#define PSA_MEMORY_POISONING_WRAPPERS_H
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "test/memory.h"
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
|
||||
/**
|
||||
* \brief Setup the memory poisoning test hooks used by
|
||||
* psa_crypto_copy_input() and psa_crypto_copy_output() for
|
||||
* memory poisoning.
|
||||
*/
|
||||
void mbedtls_poison_test_hooks_setup(void);
|
||||
|
||||
/**
|
||||
* \brief Teardown the memory poisoning test hooks used by
|
||||
* psa_crypto_copy_input() and psa_crypto_copy_output() for
|
||||
* memory poisoning.
|
||||
*/
|
||||
void mbedtls_poison_test_hooks_teardown(void);
|
||||
|
||||
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_TEST_MEMORY_CAN_POISON */
|
||||
|
||||
#endif /* PSA_MEMORY_POISONING_WRAPPERS_H */
|
722
tests/include/test/psa_test_wrappers.h
Normal file
722
tests/include/test/psa_test_wrappers.h
Normal file
@ -0,0 +1,722 @@
|
||||
/* Automatically generated by generate_psa_wrappers.py, do not edit! */
|
||||
|
||||
/* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef TEST_PSA_TEST_WRAPPERS_H
|
||||
#define TEST_PSA_TEST_WRAPPERS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <mbedtls/build_info.h>
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_TEST_HOOKS) && \
|
||||
!defined(RECORD_PSA_STATUS_COVERAGE_LOG)
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
#include <test/memory.h>
|
||||
#include <test/psa_crypto_helpers.h>
|
||||
#include <test/psa_test_wrappers.h>
|
||||
|
||||
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_inject_entropy(
|
||||
const uint8_t *arg0_seed,
|
||||
size_t arg1_seed_size);
|
||||
#define mbedtls_psa_inject_entropy(arg0_seed, arg1_seed_size) \
|
||||
mbedtls_test_wrap_mbedtls_psa_inject_entropy(arg0_seed, arg1_seed_size)
|
||||
#endif /* defined(MBEDTLS_PSA_INJECT_ENTROPY) */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_platform_get_builtin_key(
|
||||
mbedtls_svc_key_id_t arg0_key_id,
|
||||
psa_key_lifetime_t *arg1_lifetime,
|
||||
psa_drv_slot_number_t *arg2_slot_number);
|
||||
#define mbedtls_psa_platform_get_builtin_key(arg0_key_id, arg1_lifetime, arg2_slot_number) \
|
||||
mbedtls_test_wrap_mbedtls_psa_platform_get_builtin_key(arg0_key_id, arg1_lifetime, arg2_slot_number)
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_register_se_key(
|
||||
const psa_key_attributes_t *arg0_attributes);
|
||||
#define mbedtls_psa_register_se_key(arg0_attributes) \
|
||||
mbedtls_test_wrap_mbedtls_psa_register_se_key(arg0_attributes)
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_abort(
|
||||
psa_aead_operation_t *arg0_operation);
|
||||
#define psa_aead_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_aead_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_nonce,
|
||||
size_t arg3_nonce_length,
|
||||
const uint8_t *arg4_additional_data,
|
||||
size_t arg5_additional_data_length,
|
||||
const uint8_t *arg6_ciphertext,
|
||||
size_t arg7_ciphertext_length,
|
||||
uint8_t *arg8_plaintext,
|
||||
size_t arg9_plaintext_size,
|
||||
size_t *arg10_plaintext_length);
|
||||
#define psa_aead_decrypt(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_ciphertext, arg7_ciphertext_length, arg8_plaintext, arg9_plaintext_size, arg10_plaintext_length) \
|
||||
mbedtls_test_wrap_psa_aead_decrypt(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_ciphertext, arg7_ciphertext_length, arg8_plaintext, arg9_plaintext_size, arg10_plaintext_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_decrypt_setup(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_aead_decrypt_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_aead_decrypt_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_nonce,
|
||||
size_t arg3_nonce_length,
|
||||
const uint8_t *arg4_additional_data,
|
||||
size_t arg5_additional_data_length,
|
||||
const uint8_t *arg6_plaintext,
|
||||
size_t arg7_plaintext_length,
|
||||
uint8_t *arg8_ciphertext,
|
||||
size_t arg9_ciphertext_size,
|
||||
size_t *arg10_ciphertext_length);
|
||||
#define psa_aead_encrypt(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_plaintext, arg7_plaintext_length, arg8_ciphertext, arg9_ciphertext_size, arg10_ciphertext_length) \
|
||||
mbedtls_test_wrap_psa_aead_encrypt(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_plaintext, arg7_plaintext_length, arg8_ciphertext, arg9_ciphertext_size, arg10_ciphertext_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_encrypt_setup(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_aead_encrypt_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_aead_encrypt_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_finish(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_ciphertext,
|
||||
size_t arg2_ciphertext_size,
|
||||
size_t *arg3_ciphertext_length,
|
||||
uint8_t *arg4_tag,
|
||||
size_t arg5_tag_size,
|
||||
size_t *arg6_tag_length);
|
||||
#define psa_aead_finish(arg0_operation, arg1_ciphertext, arg2_ciphertext_size, arg3_ciphertext_length, arg4_tag, arg5_tag_size, arg6_tag_length) \
|
||||
mbedtls_test_wrap_psa_aead_finish(arg0_operation, arg1_ciphertext, arg2_ciphertext_size, arg3_ciphertext_length, arg4_tag, arg5_tag_size, arg6_tag_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_generate_nonce(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_nonce,
|
||||
size_t arg2_nonce_size,
|
||||
size_t *arg3_nonce_length);
|
||||
#define psa_aead_generate_nonce(arg0_operation, arg1_nonce, arg2_nonce_size, arg3_nonce_length) \
|
||||
mbedtls_test_wrap_psa_aead_generate_nonce(arg0_operation, arg1_nonce, arg2_nonce_size, arg3_nonce_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_set_lengths(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
size_t arg1_ad_length,
|
||||
size_t arg2_plaintext_length);
|
||||
#define psa_aead_set_lengths(arg0_operation, arg1_ad_length, arg2_plaintext_length) \
|
||||
mbedtls_test_wrap_psa_aead_set_lengths(arg0_operation, arg1_ad_length, arg2_plaintext_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_set_nonce(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_nonce,
|
||||
size_t arg2_nonce_length);
|
||||
#define psa_aead_set_nonce(arg0_operation, arg1_nonce, arg2_nonce_length) \
|
||||
mbedtls_test_wrap_psa_aead_set_nonce(arg0_operation, arg1_nonce, arg2_nonce_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_update(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_output,
|
||||
size_t arg4_output_size,
|
||||
size_t *arg5_output_length);
|
||||
#define psa_aead_update(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length) \
|
||||
mbedtls_test_wrap_psa_aead_update(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_update_ad(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length);
|
||||
#define psa_aead_update_ad(arg0_operation, arg1_input, arg2_input_length) \
|
||||
mbedtls_test_wrap_psa_aead_update_ad(arg0_operation, arg1_input, arg2_input_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_verify(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_plaintext,
|
||||
size_t arg2_plaintext_size,
|
||||
size_t *arg3_plaintext_length,
|
||||
const uint8_t *arg4_tag,
|
||||
size_t arg5_tag_length);
|
||||
#define psa_aead_verify(arg0_operation, arg1_plaintext, arg2_plaintext_size, arg3_plaintext_length, arg4_tag, arg5_tag_length) \
|
||||
mbedtls_test_wrap_psa_aead_verify(arg0_operation, arg1_plaintext, arg2_plaintext_size, arg3_plaintext_length, arg4_tag, arg5_tag_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_asymmetric_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_salt,
|
||||
size_t arg5_salt_length,
|
||||
uint8_t *arg6_output,
|
||||
size_t arg7_output_size,
|
||||
size_t *arg8_output_length);
|
||||
#define psa_asymmetric_decrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length) \
|
||||
mbedtls_test_wrap_psa_asymmetric_decrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_asymmetric_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_salt,
|
||||
size_t arg5_salt_length,
|
||||
uint8_t *arg6_output,
|
||||
size_t arg7_output_size,
|
||||
size_t *arg8_output_length);
|
||||
#define psa_asymmetric_encrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length) \
|
||||
mbedtls_test_wrap_psa_asymmetric_encrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_abort(
|
||||
psa_cipher_operation_t *arg0_operation);
|
||||
#define psa_cipher_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_cipher_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length);
|
||||
#define psa_cipher_decrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length) \
|
||||
mbedtls_test_wrap_psa_cipher_decrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_decrypt_setup(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_cipher_decrypt_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_cipher_decrypt_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length);
|
||||
#define psa_cipher_encrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length) \
|
||||
mbedtls_test_wrap_psa_cipher_encrypt(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_encrypt_setup(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_cipher_encrypt_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_cipher_encrypt_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_finish(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
uint8_t *arg1_output,
|
||||
size_t arg2_output_size,
|
||||
size_t *arg3_output_length);
|
||||
#define psa_cipher_finish(arg0_operation, arg1_output, arg2_output_size, arg3_output_length) \
|
||||
mbedtls_test_wrap_psa_cipher_finish(arg0_operation, arg1_output, arg2_output_size, arg3_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_generate_iv(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
uint8_t *arg1_iv,
|
||||
size_t arg2_iv_size,
|
||||
size_t *arg3_iv_length);
|
||||
#define psa_cipher_generate_iv(arg0_operation, arg1_iv, arg2_iv_size, arg3_iv_length) \
|
||||
mbedtls_test_wrap_psa_cipher_generate_iv(arg0_operation, arg1_iv, arg2_iv_size, arg3_iv_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_set_iv(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_iv,
|
||||
size_t arg2_iv_length);
|
||||
#define psa_cipher_set_iv(arg0_operation, arg1_iv, arg2_iv_length) \
|
||||
mbedtls_test_wrap_psa_cipher_set_iv(arg0_operation, arg1_iv, arg2_iv_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_update(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_output,
|
||||
size_t arg4_output_size,
|
||||
size_t *arg5_output_length);
|
||||
#define psa_cipher_update(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length) \
|
||||
mbedtls_test_wrap_psa_cipher_update(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_copy_key(
|
||||
mbedtls_svc_key_id_t arg0_source_key,
|
||||
const psa_key_attributes_t *arg1_attributes,
|
||||
mbedtls_svc_key_id_t *arg2_target_key);
|
||||
#define psa_copy_key(arg0_source_key, arg1_attributes, arg2_target_key) \
|
||||
mbedtls_test_wrap_psa_copy_key(arg0_source_key, arg1_attributes, arg2_target_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_cipher_suite(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
psa_pake_cipher_suite_t *arg1_cipher_suite);
|
||||
#define psa_crypto_driver_pake_get_cipher_suite(arg0_inputs, arg1_cipher_suite) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_cipher_suite(arg0_inputs, arg1_cipher_suite)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_password(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_buffer,
|
||||
size_t arg2_buffer_size,
|
||||
size_t *arg3_buffer_length);
|
||||
#define psa_crypto_driver_pake_get_password(arg0_inputs, arg1_buffer, arg2_buffer_size, arg3_buffer_length) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_password(arg0_inputs, arg1_buffer, arg2_buffer_size, arg3_buffer_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_password_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_password_len);
|
||||
#define psa_crypto_driver_pake_get_password_len(arg0_inputs, arg1_password_len) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_password_len(arg0_inputs, arg1_password_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_peer(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_peer_id,
|
||||
size_t arg2_peer_id_size,
|
||||
size_t *arg3_peer_id_length);
|
||||
#define psa_crypto_driver_pake_get_peer(arg0_inputs, arg1_peer_id, arg2_peer_id_size, arg3_peer_id_length) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_peer(arg0_inputs, arg1_peer_id, arg2_peer_id_size, arg3_peer_id_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_peer_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_peer_len);
|
||||
#define psa_crypto_driver_pake_get_peer_len(arg0_inputs, arg1_peer_len) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_peer_len(arg0_inputs, arg1_peer_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_user(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_user_id,
|
||||
size_t arg2_user_id_size,
|
||||
size_t *arg3_user_id_len);
|
||||
#define psa_crypto_driver_pake_get_user(arg0_inputs, arg1_user_id, arg2_user_id_size, arg3_user_id_len) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_user(arg0_inputs, arg1_user_id, arg2_user_id_size, arg3_user_id_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_user_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_user_len);
|
||||
#define psa_crypto_driver_pake_get_user_len(arg0_inputs, arg1_user_len) \
|
||||
mbedtls_test_wrap_psa_crypto_driver_pake_get_user_len(arg0_inputs, arg1_user_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_init(void);
|
||||
#define psa_crypto_init() \
|
||||
mbedtls_test_wrap_psa_crypto_init()
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_destroy_key(
|
||||
mbedtls_svc_key_id_t arg0_key);
|
||||
#define psa_destroy_key(arg0_key) \
|
||||
mbedtls_test_wrap_psa_destroy_key(arg0_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_export_key(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
uint8_t *arg1_data,
|
||||
size_t arg2_data_size,
|
||||
size_t *arg3_data_length);
|
||||
#define psa_export_key(arg0_key, arg1_data, arg2_data_size, arg3_data_length) \
|
||||
mbedtls_test_wrap_psa_export_key(arg0_key, arg1_data, arg2_data_size, arg3_data_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_export_public_key(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
uint8_t *arg1_data,
|
||||
size_t arg2_data_size,
|
||||
size_t *arg3_data_length);
|
||||
#define psa_export_public_key(arg0_key, arg1_data, arg2_data_size, arg3_data_length) \
|
||||
mbedtls_test_wrap_psa_export_public_key(arg0_key, arg1_data, arg2_data_size, arg3_data_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_generate_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
mbedtls_svc_key_id_t *arg1_key);
|
||||
#define psa_generate_key(arg0_attributes, arg1_key) \
|
||||
mbedtls_test_wrap_psa_generate_key(arg0_attributes, arg1_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_generate_random(
|
||||
uint8_t *arg0_output,
|
||||
size_t arg1_output_size);
|
||||
#define psa_generate_random(arg0_output, arg1_output_size) \
|
||||
mbedtls_test_wrap_psa_generate_random(arg0_output, arg1_output_size)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_get_key_attributes(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_key_attributes_t *arg1_attributes);
|
||||
#define psa_get_key_attributes(arg0_key, arg1_attributes) \
|
||||
mbedtls_test_wrap_psa_get_key_attributes(arg0_key, arg1_attributes)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_abort(
|
||||
psa_hash_operation_t *arg0_operation);
|
||||
#define psa_hash_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_hash_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_clone(
|
||||
const psa_hash_operation_t *arg0_source_operation,
|
||||
psa_hash_operation_t *arg1_target_operation);
|
||||
#define psa_hash_clone(arg0_source_operation, arg1_target_operation) \
|
||||
mbedtls_test_wrap_psa_hash_clone(arg0_source_operation, arg1_target_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_compare(
|
||||
psa_algorithm_t arg0_alg,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length);
|
||||
#define psa_hash_compare(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_length) \
|
||||
mbedtls_test_wrap_psa_hash_compare(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_compute(
|
||||
psa_algorithm_t arg0_alg,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_hash,
|
||||
size_t arg4_hash_size,
|
||||
size_t *arg5_hash_length);
|
||||
#define psa_hash_compute(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_size, arg5_hash_length) \
|
||||
mbedtls_test_wrap_psa_hash_compute(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_size, arg5_hash_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_finish(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
uint8_t *arg1_hash,
|
||||
size_t arg2_hash_size,
|
||||
size_t *arg3_hash_length);
|
||||
#define psa_hash_finish(arg0_operation, arg1_hash, arg2_hash_size, arg3_hash_length) \
|
||||
mbedtls_test_wrap_psa_hash_finish(arg0_operation, arg1_hash, arg2_hash_size, arg3_hash_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_setup(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
psa_algorithm_t arg1_alg);
|
||||
#define psa_hash_setup(arg0_operation, arg1_alg) \
|
||||
mbedtls_test_wrap_psa_hash_setup(arg0_operation, arg1_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_update(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length);
|
||||
#define psa_hash_update(arg0_operation, arg1_input, arg2_input_length) \
|
||||
mbedtls_test_wrap_psa_hash_update(arg0_operation, arg1_input, arg2_input_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_verify(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_hash,
|
||||
size_t arg2_hash_length);
|
||||
#define psa_hash_verify(arg0_operation, arg1_hash, arg2_hash_length) \
|
||||
mbedtls_test_wrap_psa_hash_verify(arg0_operation, arg1_hash, arg2_hash_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_import_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
const uint8_t *arg1_data,
|
||||
size_t arg2_data_length,
|
||||
mbedtls_svc_key_id_t *arg3_key);
|
||||
#define psa_import_key(arg0_attributes, arg1_data, arg2_data_length, arg3_key) \
|
||||
mbedtls_test_wrap_psa_import_key(arg0_attributes, arg1_data, arg2_data_length, arg3_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_abort(
|
||||
psa_key_derivation_operation_t *arg0_operation);
|
||||
#define psa_key_derivation_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_key_derivation_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_get_capacity(
|
||||
const psa_key_derivation_operation_t *arg0_operation,
|
||||
size_t *arg1_capacity);
|
||||
#define psa_key_derivation_get_capacity(arg0_operation, arg1_capacity) \
|
||||
mbedtls_test_wrap_psa_key_derivation_get_capacity(arg0_operation, arg1_capacity)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_bytes(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
const uint8_t *arg2_data,
|
||||
size_t arg3_data_length);
|
||||
#define psa_key_derivation_input_bytes(arg0_operation, arg1_step, arg2_data, arg3_data_length) \
|
||||
mbedtls_test_wrap_psa_key_derivation_input_bytes(arg0_operation, arg1_step, arg2_data, arg3_data_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_integer(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
uint64_t arg2_value);
|
||||
#define psa_key_derivation_input_integer(arg0_operation, arg1_step, arg2_value) \
|
||||
mbedtls_test_wrap_psa_key_derivation_input_integer(arg0_operation, arg1_step, arg2_value)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_key(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
mbedtls_svc_key_id_t arg2_key);
|
||||
#define psa_key_derivation_input_key(arg0_operation, arg1_step, arg2_key) \
|
||||
mbedtls_test_wrap_psa_key_derivation_input_key(arg0_operation, arg1_step, arg2_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_key_agreement(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
mbedtls_svc_key_id_t arg2_private_key,
|
||||
const uint8_t *arg3_peer_key,
|
||||
size_t arg4_peer_key_length);
|
||||
#define psa_key_derivation_key_agreement(arg0_operation, arg1_step, arg2_private_key, arg3_peer_key, arg4_peer_key_length) \
|
||||
mbedtls_test_wrap_psa_key_derivation_key_agreement(arg0_operation, arg1_step, arg2_private_key, arg3_peer_key, arg4_peer_key_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_output_bytes(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
uint8_t *arg1_output,
|
||||
size_t arg2_output_length);
|
||||
#define psa_key_derivation_output_bytes(arg0_operation, arg1_output, arg2_output_length) \
|
||||
mbedtls_test_wrap_psa_key_derivation_output_bytes(arg0_operation, arg1_output, arg2_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_output_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
psa_key_derivation_operation_t *arg1_operation,
|
||||
mbedtls_svc_key_id_t *arg2_key);
|
||||
#define psa_key_derivation_output_key(arg0_attributes, arg1_operation, arg2_key) \
|
||||
mbedtls_test_wrap_psa_key_derivation_output_key(arg0_attributes, arg1_operation, arg2_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_set_capacity(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
size_t arg1_capacity);
|
||||
#define psa_key_derivation_set_capacity(arg0_operation, arg1_capacity) \
|
||||
mbedtls_test_wrap_psa_key_derivation_set_capacity(arg0_operation, arg1_capacity)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_setup(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_algorithm_t arg1_alg);
|
||||
#define psa_key_derivation_setup(arg0_operation, arg1_alg) \
|
||||
mbedtls_test_wrap_psa_key_derivation_setup(arg0_operation, arg1_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_abort(
|
||||
psa_mac_operation_t *arg0_operation);
|
||||
#define psa_mac_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_mac_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_compute(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_mac,
|
||||
size_t arg5_mac_size,
|
||||
size_t *arg6_mac_length);
|
||||
#define psa_mac_compute(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_size, arg6_mac_length) \
|
||||
mbedtls_test_wrap_psa_mac_compute(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_size, arg6_mac_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_sign_finish(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
uint8_t *arg1_mac,
|
||||
size_t arg2_mac_size,
|
||||
size_t *arg3_mac_length);
|
||||
#define psa_mac_sign_finish(arg0_operation, arg1_mac, arg2_mac_size, arg3_mac_length) \
|
||||
mbedtls_test_wrap_psa_mac_sign_finish(arg0_operation, arg1_mac, arg2_mac_size, arg3_mac_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_sign_setup(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_mac_sign_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_mac_sign_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_update(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length);
|
||||
#define psa_mac_update(arg0_operation, arg1_input, arg2_input_length) \
|
||||
mbedtls_test_wrap_psa_mac_update(arg0_operation, arg1_input, arg2_input_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_mac,
|
||||
size_t arg5_mac_length);
|
||||
#define psa_mac_verify(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_length) \
|
||||
mbedtls_test_wrap_psa_mac_verify(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify_finish(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_mac,
|
||||
size_t arg2_mac_length);
|
||||
#define psa_mac_verify_finish(arg0_operation, arg1_mac, arg2_mac_length) \
|
||||
mbedtls_test_wrap_psa_mac_verify_finish(arg0_operation, arg1_mac, arg2_mac_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify_setup(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg);
|
||||
#define psa_mac_verify_setup(arg0_operation, arg1_key, arg2_alg) \
|
||||
mbedtls_test_wrap_psa_mac_verify_setup(arg0_operation, arg1_key, arg2_alg)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_abort(
|
||||
psa_pake_operation_t *arg0_operation);
|
||||
#define psa_pake_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_pake_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_get_implicit_key(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_key_derivation_operation_t *arg1_output);
|
||||
#define psa_pake_get_implicit_key(arg0_operation, arg1_output) \
|
||||
mbedtls_test_wrap_psa_pake_get_implicit_key(arg0_operation, arg1_output)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_input(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_step_t arg1_step,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length);
|
||||
#define psa_pake_input(arg0_operation, arg1_step, arg2_input, arg3_input_length) \
|
||||
mbedtls_test_wrap_psa_pake_input(arg0_operation, arg1_step, arg2_input, arg3_input_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_output(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_step_t arg1_step,
|
||||
uint8_t *arg2_output,
|
||||
size_t arg3_output_size,
|
||||
size_t *arg4_output_length);
|
||||
#define psa_pake_output(arg0_operation, arg1_step, arg2_output, arg3_output_size, arg4_output_length) \
|
||||
mbedtls_test_wrap_psa_pake_output(arg0_operation, arg1_step, arg2_output, arg3_output_size, arg4_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_password_key(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_password);
|
||||
#define psa_pake_set_password_key(arg0_operation, arg1_password) \
|
||||
mbedtls_test_wrap_psa_pake_set_password_key(arg0_operation, arg1_password)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_peer(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_peer_id,
|
||||
size_t arg2_peer_id_len);
|
||||
#define psa_pake_set_peer(arg0_operation, arg1_peer_id, arg2_peer_id_len) \
|
||||
mbedtls_test_wrap_psa_pake_set_peer(arg0_operation, arg1_peer_id, arg2_peer_id_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_role(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_role_t arg1_role);
|
||||
#define psa_pake_set_role(arg0_operation, arg1_role) \
|
||||
mbedtls_test_wrap_psa_pake_set_role(arg0_operation, arg1_role)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_user(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_user_id,
|
||||
size_t arg2_user_id_len);
|
||||
#define psa_pake_set_user(arg0_operation, arg1_user_id, arg2_user_id_len) \
|
||||
mbedtls_test_wrap_psa_pake_set_user(arg0_operation, arg1_user_id, arg2_user_id_len)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_setup(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const psa_pake_cipher_suite_t *arg1_cipher_suite);
|
||||
#define psa_pake_setup(arg0_operation, arg1_cipher_suite) \
|
||||
mbedtls_test_wrap_psa_pake_setup(arg0_operation, arg1_cipher_suite)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_purge_key(
|
||||
mbedtls_svc_key_id_t arg0_key);
|
||||
#define psa_purge_key(arg0_key) \
|
||||
mbedtls_test_wrap_psa_purge_key(arg0_key)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_raw_key_agreement(
|
||||
psa_algorithm_t arg0_alg,
|
||||
mbedtls_svc_key_id_t arg1_private_key,
|
||||
const uint8_t *arg2_peer_key,
|
||||
size_t arg3_peer_key_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length);
|
||||
#define psa_raw_key_agreement(arg0_alg, arg1_private_key, arg2_peer_key, arg3_peer_key_length, arg4_output, arg5_output_size, arg6_output_length) \
|
||||
mbedtls_test_wrap_psa_raw_key_agreement(arg0_alg, arg1_private_key, arg2_peer_key, arg3_peer_key_length, arg4_output, arg5_output_size, arg6_output_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_hash,
|
||||
size_t arg3_hash_length,
|
||||
uint8_t *arg4_signature,
|
||||
size_t arg5_signature_size,
|
||||
size_t *arg6_signature_length);
|
||||
#define psa_sign_hash(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_size, arg6_signature_length) \
|
||||
mbedtls_test_wrap_psa_sign_hash(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_size, arg6_signature_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_abort(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation);
|
||||
#define psa_sign_hash_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_sign_hash_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_complete(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation,
|
||||
uint8_t *arg1_signature,
|
||||
size_t arg2_signature_size,
|
||||
size_t *arg3_signature_length);
|
||||
#define psa_sign_hash_complete(arg0_operation, arg1_signature, arg2_signature_size, arg3_signature_length) \
|
||||
mbedtls_test_wrap_psa_sign_hash_complete(arg0_operation, arg1_signature, arg2_signature_size, arg3_signature_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_start(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length);
|
||||
#define psa_sign_hash_start(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length) \
|
||||
mbedtls_test_wrap_psa_sign_hash_start(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_message(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_signature,
|
||||
size_t arg5_signature_size,
|
||||
size_t *arg6_signature_length);
|
||||
#define psa_sign_message(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_size, arg6_signature_length) \
|
||||
mbedtls_test_wrap_psa_sign_message(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_size, arg6_signature_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_hash,
|
||||
size_t arg3_hash_length,
|
||||
const uint8_t *arg4_signature,
|
||||
size_t arg5_signature_length);
|
||||
#define psa_verify_hash(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_length) \
|
||||
mbedtls_test_wrap_psa_verify_hash(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_abort(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation);
|
||||
#define psa_verify_hash_abort(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_verify_hash_abort(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_complete(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation);
|
||||
#define psa_verify_hash_complete(arg0_operation) \
|
||||
mbedtls_test_wrap_psa_verify_hash_complete(arg0_operation)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_start(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length,
|
||||
const uint8_t *arg5_signature,
|
||||
size_t arg6_signature_length);
|
||||
#define psa_verify_hash_start(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length, arg5_signature, arg6_signature_length) \
|
||||
mbedtls_test_wrap_psa_verify_hash_start(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length, arg5_signature, arg6_signature_length)
|
||||
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_message(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_signature,
|
||||
size_t arg5_signature_length);
|
||||
#define psa_verify_message(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_length) \
|
||||
mbedtls_test_wrap_psa_verify_message(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_length)
|
||||
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_TEST_HOOKS) && \
|
||||
!defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TEST_PSA_TEST_WRAPPERS_H */
|
||||
|
||||
/* End of automatically generated file. */
|
@ -1050,7 +1050,7 @@ component_check_test_dependencies () {
|
||||
grep 'depends_on' \
|
||||
tests/suites/test_suite_psa*.data tests/suites/test_suite_psa*.function |
|
||||
grep -Eo '!?MBEDTLS_[^: ]*' |
|
||||
grep -v MBEDTLS_PSA_ |
|
||||
grep -v -e MBEDTLS_PSA_ -e MBEDTLS_TEST_ |
|
||||
sort -u > $found
|
||||
|
||||
# Expected ones with justification - keep in sorted order by ASCII table!
|
||||
@ -1129,7 +1129,7 @@ component_test_default_cmake_gcc_asan () {
|
||||
programs/test/selftest
|
||||
|
||||
msg "test: metatests (GCC, ASan build)"
|
||||
tests/scripts/run-metatests.sh any asan
|
||||
tests/scripts/run-metatests.sh any asan poison
|
||||
|
||||
msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
|
||||
tests/ssl-opt.sh
|
||||
@ -1220,6 +1220,17 @@ component_test_psa_crypto_key_id_encodes_owner () {
|
||||
make test
|
||||
}
|
||||
|
||||
component_test_no_psa_copy_caller_buffers () {
|
||||
msg "build: full config - MBEDTLS_PSA_COPY_CALLER_BUFFERS, cmake, gcc, ASan"
|
||||
scripts/config.py full
|
||||
scripts/config.py unset MBEDTLS_PSA_COPY_CALLER_BUFFERS
|
||||
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
|
||||
make
|
||||
|
||||
msg "test: full config - MBEDTLS_PSA_COPY_CALLER_BUFFERS, cmake, gcc, ASan"
|
||||
make test
|
||||
}
|
||||
|
||||
# check_renamed_symbols HEADER LIB
|
||||
# Check that if HEADER contains '#define MACRO ...' then MACRO is not a symbol
|
||||
# name is LIB.
|
||||
@ -1930,7 +1941,7 @@ component_test_everest () {
|
||||
make test
|
||||
|
||||
msg "test: metatests (clang, ASan)"
|
||||
tests/scripts/run-metatests.sh any asan
|
||||
tests/scripts/run-metatests.sh any asan poison
|
||||
|
||||
msg "test: Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s
|
||||
tests/ssl-opt.sh -f ECDH
|
||||
|
@ -144,3 +144,8 @@ if in_mbedtls_repo; then
|
||||
# the step that creates or updates these files.
|
||||
check scripts/generate_visualc_files.pl visualc/VS2013
|
||||
fi
|
||||
|
||||
# Generated files that are present in the repository even in the development
|
||||
# branch. (This is intended to be temporary, until the generator scripts are
|
||||
# fully reviewed and the build scripts support a generated header file.)
|
||||
check tests/scripts/generate_psa_wrappers.py tests/include/test/psa_test_wrappers.h tests/src/psa_test_wrappers.c
|
||||
|
253
tests/scripts/generate_psa_wrappers.py
Executable file
253
tests/scripts/generate_psa_wrappers.py
Executable file
@ -0,0 +1,253 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate wrapper functions for PSA function calls.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
|
||||
### WARNING: the code in this file has not been extensively reviewed yet.
|
||||
### We do not think it is harmful, but it may be below our normal standards
|
||||
### for robustness and maintainability.
|
||||
|
||||
import argparse
|
||||
import itertools
|
||||
import os
|
||||
from typing import Iterator, List, Optional, Tuple
|
||||
|
||||
import scripts_path #pylint: disable=unused-import
|
||||
from mbedtls_dev import build_tree
|
||||
from mbedtls_dev import c_parsing_helper
|
||||
from mbedtls_dev import c_wrapper_generator
|
||||
from mbedtls_dev import typing_util
|
||||
|
||||
|
||||
class BufferParameter:
|
||||
"""Description of an input or output buffer parameter sequence to a PSA function."""
|
||||
#pylint: disable=too-few-public-methods
|
||||
|
||||
def __init__(self, i: int, is_output: bool,
|
||||
buffer_name: str, size_name: str) -> None:
|
||||
"""Initialize the parameter information.
|
||||
|
||||
i is the index of the function argument that is the pointer to the buffer.
|
||||
The size is argument i+1. For a variable-size output, the actual length
|
||||
goes in argument i+2.
|
||||
|
||||
buffer_name and size_names are the names of arguments i and i+1.
|
||||
This class does not yet help with the output length.
|
||||
"""
|
||||
self.index = i
|
||||
self.buffer_name = buffer_name
|
||||
self.size_name = size_name
|
||||
self.is_output = is_output
|
||||
|
||||
|
||||
class PSAWrapperGenerator(c_wrapper_generator.Base):
|
||||
"""Generate a C source file containing wrapper functions for PSA Crypto API calls."""
|
||||
|
||||
_CPP_GUARDS = ('defined(MBEDTLS_PSA_CRYPTO_C) && ' +
|
||||
'defined(MBEDTLS_TEST_HOOKS) && \\\n ' +
|
||||
'!defined(RECORD_PSA_STATUS_COVERAGE_LOG)')
|
||||
_WRAPPER_NAME_PREFIX = 'mbedtls_test_wrap_'
|
||||
_WRAPPER_NAME_SUFFIX = ''
|
||||
|
||||
def gather_data(self) -> None:
|
||||
root_dir = build_tree.guess_mbedtls_root()
|
||||
for header_name in ['crypto.h', 'crypto_extra.h']:
|
||||
header_path = os.path.join(root_dir, 'include', 'psa', header_name)
|
||||
c_parsing_helper.read_function_declarations(self.functions, header_path)
|
||||
|
||||
_SKIP_FUNCTIONS = frozenset([
|
||||
'mbedtls_psa_external_get_random', # not a library function
|
||||
'psa_get_key_domain_parameters', # client-side function
|
||||
'psa_get_key_slot_number', # client-side function
|
||||
'psa_key_derivation_verify_bytes', # not implemented yet
|
||||
'psa_key_derivation_verify_key', # not implemented yet
|
||||
'psa_set_key_domain_parameters', # client-side function
|
||||
])
|
||||
|
||||
def _skip_function(self, function: c_wrapper_generator.FunctionInfo) -> bool:
|
||||
if function.return_type != 'psa_status_t':
|
||||
return True
|
||||
if function.name in self._SKIP_FUNCTIONS:
|
||||
return True
|
||||
return False
|
||||
|
||||
# PAKE stuff: not implemented yet
|
||||
_PAKE_STUFF = frozenset([
|
||||
'psa_crypto_driver_pake_inputs_t *',
|
||||
'psa_pake_cipher_suite_t *',
|
||||
])
|
||||
|
||||
def _return_variable_name(self,
|
||||
function: c_wrapper_generator.FunctionInfo) -> str:
|
||||
"""The name of the variable that will contain the return value."""
|
||||
if function.return_type == 'psa_status_t':
|
||||
return 'status'
|
||||
return super()._return_variable_name(function)
|
||||
|
||||
_FUNCTION_GUARDS = c_wrapper_generator.Base._FUNCTION_GUARDS.copy() \
|
||||
#pylint: disable=protected-access
|
||||
_FUNCTION_GUARDS.update({
|
||||
'mbedtls_psa_register_se_key': 'defined(MBEDTLS_PSA_CRYPTO_SE_C)',
|
||||
'mbedtls_psa_inject_entropy': 'defined(MBEDTLS_PSA_INJECT_ENTROPY)',
|
||||
'mbedtls_psa_external_get_random': 'defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)',
|
||||
'mbedtls_psa_platform_get_builtin_key': 'defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)',
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _detect_buffer_parameters(arguments: List[c_parsing_helper.ArgumentInfo],
|
||||
argument_names: List[str]) -> Iterator[BufferParameter]:
|
||||
"""Detect function arguments that are buffers (pointer, size [,length])."""
|
||||
types = ['' if arg.suffix else arg.type for arg in arguments]
|
||||
# pairs = list of (type_of_arg_N, type_of_arg_N+1)
|
||||
# where each type_of_arg_X is the empty string if the type is an array
|
||||
# or there is no argument X.
|
||||
pairs = enumerate(itertools.zip_longest(types, types[1:], fillvalue=''))
|
||||
for i, t01 in pairs:
|
||||
if (t01[0] == 'const uint8_t *' or t01[0] == 'uint8_t *') and \
|
||||
t01[1] == 'size_t':
|
||||
yield BufferParameter(i, not t01[0].startswith('const '),
|
||||
argument_names[i], argument_names[i+1])
|
||||
|
||||
@staticmethod
|
||||
def _write_poison_buffer_parameter(out: typing_util.Writable,
|
||||
param: BufferParameter,
|
||||
poison: bool) -> None:
|
||||
"""Write poisoning or unpoisoning code for a buffer parameter.
|
||||
|
||||
Write poisoning code if poison is true, unpoisoning code otherwise.
|
||||
"""
|
||||
out.write(' MBEDTLS_TEST_MEMORY_{}({}, {});\n'.format(
|
||||
'POISON' if poison else 'UNPOISON',
|
||||
param.buffer_name, param.size_name
|
||||
))
|
||||
|
||||
def _write_poison_buffer_parameters(self, out: typing_util.Writable,
|
||||
buffer_parameters: List[BufferParameter],
|
||||
poison: bool) -> None:
|
||||
"""Write poisoning or unpoisoning code for the buffer parameters.
|
||||
|
||||
Write poisoning code if poison is true, unpoisoning code otherwise.
|
||||
"""
|
||||
if not buffer_parameters:
|
||||
return
|
||||
out.write('#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)\n')
|
||||
for param in buffer_parameters:
|
||||
self._write_poison_buffer_parameter(out, param, poison)
|
||||
out.write('#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */\n')
|
||||
|
||||
@staticmethod
|
||||
def _parameter_should_be_copied(function_name: str,
|
||||
_buffer_name: Optional[str]) -> bool:
|
||||
"""Whether the specified buffer argument to a PSA function should be copied.
|
||||
"""
|
||||
# Proof-of-concept: just instrument one function for now
|
||||
if function_name == 'psa_cipher_encrypt':
|
||||
return True
|
||||
return False
|
||||
|
||||
def _write_function_call(self, out: typing_util.Writable,
|
||||
function: c_wrapper_generator.FunctionInfo,
|
||||
argument_names: List[str]) -> None:
|
||||
buffer_parameters = list(
|
||||
param
|
||||
for param in self._detect_buffer_parameters(function.arguments,
|
||||
argument_names)
|
||||
if self._parameter_should_be_copied(function.name,
|
||||
function.arguments[param.index].name))
|
||||
self._write_poison_buffer_parameters(out, buffer_parameters, True)
|
||||
super()._write_function_call(out, function, argument_names)
|
||||
self._write_poison_buffer_parameters(out, buffer_parameters, False)
|
||||
|
||||
def _write_prologue(self, out: typing_util.Writable, header: bool) -> None:
|
||||
super()._write_prologue(out, header)
|
||||
out.write("""
|
||||
#if {}
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
#include <test/memory.h>
|
||||
#include <test/psa_crypto_helpers.h>
|
||||
#include <test/psa_test_wrappers.h>
|
||||
"""
|
||||
.format(self._CPP_GUARDS))
|
||||
|
||||
def _write_epilogue(self, out: typing_util.Writable, header: bool) -> None:
|
||||
out.write("""
|
||||
#endif /* {} */
|
||||
"""
|
||||
.format(self._CPP_GUARDS))
|
||||
super()._write_epilogue(out, header)
|
||||
|
||||
|
||||
class PSALoggingWrapperGenerator(PSAWrapperGenerator, c_wrapper_generator.Logging):
|
||||
"""Generate a C source file containing wrapper functions that log PSA Crypto API calls."""
|
||||
|
||||
def __init__(self, stream: str) -> None:
|
||||
super().__init__()
|
||||
self.set_stream(stream)
|
||||
|
||||
_PRINTF_TYPE_CAST = c_wrapper_generator.Logging._PRINTF_TYPE_CAST.copy()
|
||||
_PRINTF_TYPE_CAST.update({
|
||||
'mbedtls_svc_key_id_t': 'unsigned',
|
||||
'psa_algorithm_t': 'unsigned',
|
||||
'psa_drv_slot_number_t': 'unsigned long long',
|
||||
'psa_key_derivation_step_t': 'int',
|
||||
'psa_key_id_t': 'unsigned',
|
||||
'psa_key_slot_number_t': 'unsigned long long',
|
||||
'psa_key_lifetime_t': 'unsigned',
|
||||
'psa_key_type_t': 'unsigned',
|
||||
'psa_key_usage_flags_t': 'unsigned',
|
||||
'psa_pake_role_t': 'int',
|
||||
'psa_pake_step_t': 'int',
|
||||
'psa_status_t': 'int',
|
||||
})
|
||||
|
||||
def _printf_parameters(self, typ: str, var: str) -> Tuple[str, List[str]]:
|
||||
if typ.startswith('const '):
|
||||
typ = typ[6:]
|
||||
if typ == 'uint8_t *':
|
||||
# Skip buffers
|
||||
return '', []
|
||||
if typ.endswith('operation_t *'):
|
||||
return '', []
|
||||
if typ in self._PAKE_STUFF:
|
||||
return '', []
|
||||
if typ == 'psa_key_attributes_t *':
|
||||
return (var + '={id=%u, lifetime=0x%08x, type=0x%08x, bits=%u, alg=%08x, usage=%08x}',
|
||||
['(unsigned) psa_get_key_{}({})'.format(field, var)
|
||||
for field in ['id', 'lifetime', 'type', 'bits', 'algorithm', 'usage_flags']])
|
||||
return super()._printf_parameters(typ, var)
|
||||
|
||||
|
||||
DEFAULT_C_OUTPUT_FILE_NAME = 'tests/src/psa_test_wrappers.c'
|
||||
DEFAULT_H_OUTPUT_FILE_NAME = 'tests/include/test/psa_test_wrappers.h'
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=globals()['__doc__'])
|
||||
parser.add_argument('--log',
|
||||
help='Stream to log to (default: no logging code)')
|
||||
parser.add_argument('--output-c',
|
||||
metavar='FILENAME',
|
||||
default=DEFAULT_C_OUTPUT_FILE_NAME,
|
||||
help=('Output .c file path (default: {}; skip .c output if empty)'
|
||||
.format(DEFAULT_C_OUTPUT_FILE_NAME)))
|
||||
parser.add_argument('--output-h',
|
||||
metavar='FILENAME',
|
||||
default=DEFAULT_H_OUTPUT_FILE_NAME,
|
||||
help=('Output .h file path (default: {}; skip .h output if empty)'
|
||||
.format(DEFAULT_H_OUTPUT_FILE_NAME)))
|
||||
options = parser.parse_args()
|
||||
if options.log:
|
||||
generator = PSALoggingWrapperGenerator(options.log) #type: PSAWrapperGenerator
|
||||
else:
|
||||
generator = PSAWrapperGenerator()
|
||||
generator.gather_data()
|
||||
if options.output_h:
|
||||
generator.write_h_file(options.output_h)
|
||||
if options.output_c:
|
||||
generator.write_c_file(options.output_c)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -13,6 +13,10 @@
|
||||
#include <test/psa_crypto_helpers.h>
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#include <test/psa_memory_poisoning_wrappers.h>
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Static global variables */
|
||||
|
||||
@ -29,6 +33,12 @@ int mbedtls_test_platform_setup(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
|
||||
&& defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) \
|
||||
&& defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
mbedtls_poison_test_hooks_setup();
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
||||
/* Make sure that injected entropy is present. Otherwise
|
||||
* psa_crypto_init() will fail. This is not necessary for test suites
|
||||
@ -49,6 +59,12 @@ int mbedtls_test_platform_setup(void)
|
||||
|
||||
void mbedtls_test_platform_teardown(void)
|
||||
{
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
|
||||
&& defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) \
|
||||
&& defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
mbedtls_poison_test_hooks_teardown();
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
mbedtls_platform_teardown(&platform_ctx);
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
31
tests/src/psa_memory_poisoning_wrappers.c
Normal file
31
tests/src/psa_memory_poisoning_wrappers.c
Normal file
@ -0,0 +1,31 @@
|
||||
/** Helper functions for memory poisoning in tests.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
#include "test/memory.h"
|
||||
|
||||
#include "psa_crypto_invasive.h"
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
|
||||
&& defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
|
||||
void mbedtls_poison_test_hooks_setup(void)
|
||||
{
|
||||
psa_input_pre_copy_hook = mbedtls_test_memory_unpoison;
|
||||
psa_input_post_copy_hook = mbedtls_test_memory_poison;
|
||||
psa_output_pre_copy_hook = mbedtls_test_memory_unpoison;
|
||||
psa_output_post_copy_hook = mbedtls_test_memory_poison;
|
||||
}
|
||||
|
||||
void mbedtls_poison_test_hooks_teardown(void)
|
||||
{
|
||||
psa_input_pre_copy_hook = NULL;
|
||||
psa_input_post_copy_hook = NULL;
|
||||
psa_output_pre_copy_hook = NULL;
|
||||
psa_output_post_copy_hook = NULL;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_PSA_CRYPTO_C &&
|
||||
MBEDTLS_TEST_MEMORY_CAN_POISON */
|
984
tests/src/psa_test_wrappers.c
Normal file
984
tests/src/psa_test_wrappers.c
Normal file
@ -0,0 +1,984 @@
|
||||
/* Automatically generated by generate_psa_wrappers.py, do not edit! */
|
||||
|
||||
/* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include <mbedtls/build_info.h>
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_TEST_HOOKS) && \
|
||||
!defined(RECORD_PSA_STATUS_COVERAGE_LOG)
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
#include <test/memory.h>
|
||||
#include <test/psa_crypto_helpers.h>
|
||||
#include <test/psa_test_wrappers.h>
|
||||
|
||||
/* Wrapper for mbedtls_psa_inject_entropy */
|
||||
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_inject_entropy(
|
||||
const uint8_t *arg0_seed,
|
||||
size_t arg1_seed_size)
|
||||
{
|
||||
psa_status_t status = (mbedtls_psa_inject_entropy)(arg0_seed, arg1_seed_size);
|
||||
return status;
|
||||
}
|
||||
#endif /* defined(MBEDTLS_PSA_INJECT_ENTROPY) */
|
||||
|
||||
/* Wrapper for mbedtls_psa_platform_get_builtin_key */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_platform_get_builtin_key(
|
||||
mbedtls_svc_key_id_t arg0_key_id,
|
||||
psa_key_lifetime_t *arg1_lifetime,
|
||||
psa_drv_slot_number_t *arg2_slot_number)
|
||||
{
|
||||
psa_status_t status = (mbedtls_psa_platform_get_builtin_key)(arg0_key_id, arg1_lifetime, arg2_slot_number);
|
||||
return status;
|
||||
}
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) */
|
||||
|
||||
/* Wrapper for mbedtls_psa_register_se_key */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||
psa_status_t mbedtls_test_wrap_mbedtls_psa_register_se_key(
|
||||
const psa_key_attributes_t *arg0_attributes)
|
||||
{
|
||||
psa_status_t status = (mbedtls_psa_register_se_key)(arg0_attributes);
|
||||
return status;
|
||||
}
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
|
||||
|
||||
/* Wrapper for psa_aead_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_abort(
|
||||
psa_aead_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_aead_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_decrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_nonce,
|
||||
size_t arg3_nonce_length,
|
||||
const uint8_t *arg4_additional_data,
|
||||
size_t arg5_additional_data_length,
|
||||
const uint8_t *arg6_ciphertext,
|
||||
size_t arg7_ciphertext_length,
|
||||
uint8_t *arg8_plaintext,
|
||||
size_t arg9_plaintext_size,
|
||||
size_t *arg10_plaintext_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_decrypt)(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_ciphertext, arg7_ciphertext_length, arg8_plaintext, arg9_plaintext_size, arg10_plaintext_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_decrypt_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_decrypt_setup(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_aead_decrypt_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_encrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_nonce,
|
||||
size_t arg3_nonce_length,
|
||||
const uint8_t *arg4_additional_data,
|
||||
size_t arg5_additional_data_length,
|
||||
const uint8_t *arg6_plaintext,
|
||||
size_t arg7_plaintext_length,
|
||||
uint8_t *arg8_ciphertext,
|
||||
size_t arg9_ciphertext_size,
|
||||
size_t *arg10_ciphertext_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_encrypt)(arg0_key, arg1_alg, arg2_nonce, arg3_nonce_length, arg4_additional_data, arg5_additional_data_length, arg6_plaintext, arg7_plaintext_length, arg8_ciphertext, arg9_ciphertext_size, arg10_ciphertext_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_encrypt_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_encrypt_setup(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_aead_encrypt_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_finish */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_finish(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_ciphertext,
|
||||
size_t arg2_ciphertext_size,
|
||||
size_t *arg3_ciphertext_length,
|
||||
uint8_t *arg4_tag,
|
||||
size_t arg5_tag_size,
|
||||
size_t *arg6_tag_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_finish)(arg0_operation, arg1_ciphertext, arg2_ciphertext_size, arg3_ciphertext_length, arg4_tag, arg5_tag_size, arg6_tag_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_generate_nonce */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_generate_nonce(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_nonce,
|
||||
size_t arg2_nonce_size,
|
||||
size_t *arg3_nonce_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_generate_nonce)(arg0_operation, arg1_nonce, arg2_nonce_size, arg3_nonce_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_set_lengths */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_set_lengths(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
size_t arg1_ad_length,
|
||||
size_t arg2_plaintext_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_set_lengths)(arg0_operation, arg1_ad_length, arg2_plaintext_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_set_nonce */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_set_nonce(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_nonce,
|
||||
size_t arg2_nonce_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_set_nonce)(arg0_operation, arg1_nonce, arg2_nonce_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_update */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_update(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_output,
|
||||
size_t arg4_output_size,
|
||||
size_t *arg5_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_update)(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_update_ad */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_update_ad(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_update_ad)(arg0_operation, arg1_input, arg2_input_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_aead_verify */
|
||||
psa_status_t mbedtls_test_wrap_psa_aead_verify(
|
||||
psa_aead_operation_t *arg0_operation,
|
||||
uint8_t *arg1_plaintext,
|
||||
size_t arg2_plaintext_size,
|
||||
size_t *arg3_plaintext_length,
|
||||
const uint8_t *arg4_tag,
|
||||
size_t arg5_tag_length)
|
||||
{
|
||||
psa_status_t status = (psa_aead_verify)(arg0_operation, arg1_plaintext, arg2_plaintext_size, arg3_plaintext_length, arg4_tag, arg5_tag_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_asymmetric_decrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_asymmetric_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_salt,
|
||||
size_t arg5_salt_length,
|
||||
uint8_t *arg6_output,
|
||||
size_t arg7_output_size,
|
||||
size_t *arg8_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_asymmetric_decrypt)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_asymmetric_encrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_asymmetric_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_salt,
|
||||
size_t arg5_salt_length,
|
||||
uint8_t *arg6_output,
|
||||
size_t arg7_output_size,
|
||||
size_t *arg8_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_asymmetric_encrypt)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_salt, arg5_salt_length, arg6_output, arg7_output_size, arg8_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_abort(
|
||||
psa_cipher_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_decrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_decrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_decrypt)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_decrypt_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_decrypt_setup(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_decrypt_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_encrypt */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_encrypt(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg2_input, arg3_input_length);
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg4_output, arg5_output_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_cipher_encrypt)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_output, arg5_output_size, arg6_output_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg2_input, arg3_input_length);
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg4_output, arg5_output_size);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_encrypt_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_encrypt_setup(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_encrypt_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_finish */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_finish(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
uint8_t *arg1_output,
|
||||
size_t arg2_output_size,
|
||||
size_t *arg3_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_finish)(arg0_operation, arg1_output, arg2_output_size, arg3_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_generate_iv */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_generate_iv(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
uint8_t *arg1_iv,
|
||||
size_t arg2_iv_size,
|
||||
size_t *arg3_iv_length)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_generate_iv)(arg0_operation, arg1_iv, arg2_iv_size, arg3_iv_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_set_iv */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_set_iv(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_iv,
|
||||
size_t arg2_iv_length)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_set_iv)(arg0_operation, arg1_iv, arg2_iv_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_cipher_update */
|
||||
psa_status_t mbedtls_test_wrap_psa_cipher_update(
|
||||
psa_cipher_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_output,
|
||||
size_t arg4_output_size,
|
||||
size_t *arg5_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_cipher_update)(arg0_operation, arg1_input, arg2_input_length, arg3_output, arg4_output_size, arg5_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_copy_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_copy_key(
|
||||
mbedtls_svc_key_id_t arg0_source_key,
|
||||
const psa_key_attributes_t *arg1_attributes,
|
||||
mbedtls_svc_key_id_t *arg2_target_key)
|
||||
{
|
||||
psa_status_t status = (psa_copy_key)(arg0_source_key, arg1_attributes, arg2_target_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_cipher_suite */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_cipher_suite(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
psa_pake_cipher_suite_t *arg1_cipher_suite)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_cipher_suite)(arg0_inputs, arg1_cipher_suite);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_password */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_password(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_buffer,
|
||||
size_t arg2_buffer_size,
|
||||
size_t *arg3_buffer_length)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_password)(arg0_inputs, arg1_buffer, arg2_buffer_size, arg3_buffer_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_password_len */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_password_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_password_len)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_password_len)(arg0_inputs, arg1_password_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_peer */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_peer(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_peer_id,
|
||||
size_t arg2_peer_id_size,
|
||||
size_t *arg3_peer_id_length)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_peer)(arg0_inputs, arg1_peer_id, arg2_peer_id_size, arg3_peer_id_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_peer_len */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_peer_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_peer_len)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_peer_len)(arg0_inputs, arg1_peer_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_user */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_user(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
uint8_t *arg1_user_id,
|
||||
size_t arg2_user_id_size,
|
||||
size_t *arg3_user_id_len)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_user)(arg0_inputs, arg1_user_id, arg2_user_id_size, arg3_user_id_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_driver_pake_get_user_len */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_driver_pake_get_user_len(
|
||||
const psa_crypto_driver_pake_inputs_t *arg0_inputs,
|
||||
size_t *arg1_user_len)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_driver_pake_get_user_len)(arg0_inputs, arg1_user_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_crypto_init */
|
||||
psa_status_t mbedtls_test_wrap_psa_crypto_init(void)
|
||||
{
|
||||
psa_status_t status = (psa_crypto_init)();
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_destroy_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_destroy_key(
|
||||
mbedtls_svc_key_id_t arg0_key)
|
||||
{
|
||||
psa_status_t status = (psa_destroy_key)(arg0_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_export_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_export_key(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
uint8_t *arg1_data,
|
||||
size_t arg2_data_size,
|
||||
size_t *arg3_data_length)
|
||||
{
|
||||
psa_status_t status = (psa_export_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_export_public_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_export_public_key(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
uint8_t *arg1_data,
|
||||
size_t arg2_data_size,
|
||||
size_t *arg3_data_length)
|
||||
{
|
||||
psa_status_t status = (psa_export_public_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_generate_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_generate_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
mbedtls_svc_key_id_t *arg1_key)
|
||||
{
|
||||
psa_status_t status = (psa_generate_key)(arg0_attributes, arg1_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_generate_random */
|
||||
psa_status_t mbedtls_test_wrap_psa_generate_random(
|
||||
uint8_t *arg0_output,
|
||||
size_t arg1_output_size)
|
||||
{
|
||||
psa_status_t status = (psa_generate_random)(arg0_output, arg1_output_size);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_get_key_attributes */
|
||||
psa_status_t mbedtls_test_wrap_psa_get_key_attributes(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_key_attributes_t *arg1_attributes)
|
||||
{
|
||||
psa_status_t status = (psa_get_key_attributes)(arg0_key, arg1_attributes);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_abort(
|
||||
psa_hash_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_hash_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_clone */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_clone(
|
||||
const psa_hash_operation_t *arg0_source_operation,
|
||||
psa_hash_operation_t *arg1_target_operation)
|
||||
{
|
||||
psa_status_t status = (psa_hash_clone)(arg0_source_operation, arg1_target_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_compare */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_compare(
|
||||
psa_algorithm_t arg0_alg,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length)
|
||||
{
|
||||
psa_status_t status = (psa_hash_compare)(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_compute */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_compute(
|
||||
psa_algorithm_t arg0_alg,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length,
|
||||
uint8_t *arg3_hash,
|
||||
size_t arg4_hash_size,
|
||||
size_t *arg5_hash_length)
|
||||
{
|
||||
psa_status_t status = (psa_hash_compute)(arg0_alg, arg1_input, arg2_input_length, arg3_hash, arg4_hash_size, arg5_hash_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_finish */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_finish(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
uint8_t *arg1_hash,
|
||||
size_t arg2_hash_size,
|
||||
size_t *arg3_hash_length)
|
||||
{
|
||||
psa_status_t status = (psa_hash_finish)(arg0_operation, arg1_hash, arg2_hash_size, arg3_hash_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_setup(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
psa_algorithm_t arg1_alg)
|
||||
{
|
||||
psa_status_t status = (psa_hash_setup)(arg0_operation, arg1_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_update */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_update(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length)
|
||||
{
|
||||
psa_status_t status = (psa_hash_update)(arg0_operation, arg1_input, arg2_input_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_hash_verify */
|
||||
psa_status_t mbedtls_test_wrap_psa_hash_verify(
|
||||
psa_hash_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_hash,
|
||||
size_t arg2_hash_length)
|
||||
{
|
||||
psa_status_t status = (psa_hash_verify)(arg0_operation, arg1_hash, arg2_hash_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_import_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_import_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
const uint8_t *arg1_data,
|
||||
size_t arg2_data_length,
|
||||
mbedtls_svc_key_id_t *arg3_key)
|
||||
{
|
||||
psa_status_t status = (psa_import_key)(arg0_attributes, arg1_data, arg2_data_length, arg3_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_abort(
|
||||
psa_key_derivation_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_get_capacity */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_get_capacity(
|
||||
const psa_key_derivation_operation_t *arg0_operation,
|
||||
size_t *arg1_capacity)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_get_capacity)(arg0_operation, arg1_capacity);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_input_bytes */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_bytes(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
const uint8_t *arg2_data,
|
||||
size_t arg3_data_length)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_input_bytes)(arg0_operation, arg1_step, arg2_data, arg3_data_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_input_integer */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_integer(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
uint64_t arg2_value)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_input_integer)(arg0_operation, arg1_step, arg2_value);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_input_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_input_key(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
mbedtls_svc_key_id_t arg2_key)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_input_key)(arg0_operation, arg1_step, arg2_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_key_agreement */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_key_agreement(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_key_derivation_step_t arg1_step,
|
||||
mbedtls_svc_key_id_t arg2_private_key,
|
||||
const uint8_t *arg3_peer_key,
|
||||
size_t arg4_peer_key_length)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_key_agreement)(arg0_operation, arg1_step, arg2_private_key, arg3_peer_key, arg4_peer_key_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_output_bytes */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_output_bytes(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
uint8_t *arg1_output,
|
||||
size_t arg2_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_output_bytes)(arg0_operation, arg1_output, arg2_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_output_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_output_key(
|
||||
const psa_key_attributes_t *arg0_attributes,
|
||||
psa_key_derivation_operation_t *arg1_operation,
|
||||
mbedtls_svc_key_id_t *arg2_key)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_output_key)(arg0_attributes, arg1_operation, arg2_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_set_capacity */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_set_capacity(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
size_t arg1_capacity)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_set_capacity)(arg0_operation, arg1_capacity);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_key_derivation_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_key_derivation_setup(
|
||||
psa_key_derivation_operation_t *arg0_operation,
|
||||
psa_algorithm_t arg1_alg)
|
||||
{
|
||||
psa_status_t status = (psa_key_derivation_setup)(arg0_operation, arg1_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_abort(
|
||||
psa_mac_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_mac_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_compute */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_compute(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_mac,
|
||||
size_t arg5_mac_size,
|
||||
size_t *arg6_mac_length)
|
||||
{
|
||||
psa_status_t status = (psa_mac_compute)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_size, arg6_mac_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_sign_finish */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_sign_finish(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
uint8_t *arg1_mac,
|
||||
size_t arg2_mac_size,
|
||||
size_t *arg3_mac_length)
|
||||
{
|
||||
psa_status_t status = (psa_mac_sign_finish)(arg0_operation, arg1_mac, arg2_mac_size, arg3_mac_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_sign_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_sign_setup(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_mac_sign_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_update */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_update(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_input,
|
||||
size_t arg2_input_length)
|
||||
{
|
||||
psa_status_t status = (psa_mac_update)(arg0_operation, arg1_input, arg2_input_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_verify */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_mac,
|
||||
size_t arg5_mac_length)
|
||||
{
|
||||
psa_status_t status = (psa_mac_verify)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_mac, arg5_mac_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_verify_finish */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify_finish(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_mac,
|
||||
size_t arg2_mac_length)
|
||||
{
|
||||
psa_status_t status = (psa_mac_verify_finish)(arg0_operation, arg1_mac, arg2_mac_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_mac_verify_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_mac_verify_setup(
|
||||
psa_mac_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg)
|
||||
{
|
||||
psa_status_t status = (psa_mac_verify_setup)(arg0_operation, arg1_key, arg2_alg);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_abort(
|
||||
psa_pake_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_pake_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_get_implicit_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_get_implicit_key(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_key_derivation_operation_t *arg1_output)
|
||||
{
|
||||
psa_status_t status = (psa_pake_get_implicit_key)(arg0_operation, arg1_output);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_input */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_input(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_step_t arg1_step,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length)
|
||||
{
|
||||
psa_status_t status = (psa_pake_input)(arg0_operation, arg1_step, arg2_input, arg3_input_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_output */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_output(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_step_t arg1_step,
|
||||
uint8_t *arg2_output,
|
||||
size_t arg3_output_size,
|
||||
size_t *arg4_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_pake_output)(arg0_operation, arg1_step, arg2_output, arg3_output_size, arg4_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_set_password_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_password_key(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_password)
|
||||
{
|
||||
psa_status_t status = (psa_pake_set_password_key)(arg0_operation, arg1_password);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_set_peer */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_peer(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_peer_id,
|
||||
size_t arg2_peer_id_len)
|
||||
{
|
||||
psa_status_t status = (psa_pake_set_peer)(arg0_operation, arg1_peer_id, arg2_peer_id_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_set_role */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_role(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
psa_pake_role_t arg1_role)
|
||||
{
|
||||
psa_status_t status = (psa_pake_set_role)(arg0_operation, arg1_role);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_set_user */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_set_user(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const uint8_t *arg1_user_id,
|
||||
size_t arg2_user_id_len)
|
||||
{
|
||||
psa_status_t status = (psa_pake_set_user)(arg0_operation, arg1_user_id, arg2_user_id_len);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_pake_setup */
|
||||
psa_status_t mbedtls_test_wrap_psa_pake_setup(
|
||||
psa_pake_operation_t *arg0_operation,
|
||||
const psa_pake_cipher_suite_t *arg1_cipher_suite)
|
||||
{
|
||||
psa_status_t status = (psa_pake_setup)(arg0_operation, arg1_cipher_suite);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_purge_key */
|
||||
psa_status_t mbedtls_test_wrap_psa_purge_key(
|
||||
mbedtls_svc_key_id_t arg0_key)
|
||||
{
|
||||
psa_status_t status = (psa_purge_key)(arg0_key);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_raw_key_agreement */
|
||||
psa_status_t mbedtls_test_wrap_psa_raw_key_agreement(
|
||||
psa_algorithm_t arg0_alg,
|
||||
mbedtls_svc_key_id_t arg1_private_key,
|
||||
const uint8_t *arg2_peer_key,
|
||||
size_t arg3_peer_key_length,
|
||||
uint8_t *arg4_output,
|
||||
size_t arg5_output_size,
|
||||
size_t *arg6_output_length)
|
||||
{
|
||||
psa_status_t status = (psa_raw_key_agreement)(arg0_alg, arg1_private_key, arg2_peer_key, arg3_peer_key_length, arg4_output, arg5_output_size, arg6_output_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_sign_hash */
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_hash,
|
||||
size_t arg3_hash_length,
|
||||
uint8_t *arg4_signature,
|
||||
size_t arg5_signature_size,
|
||||
size_t *arg6_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_sign_hash)(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_size, arg6_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_sign_hash_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_abort(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_sign_hash_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_sign_hash_complete */
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_complete(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation,
|
||||
uint8_t *arg1_signature,
|
||||
size_t arg2_signature_size,
|
||||
size_t *arg3_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_sign_hash_complete)(arg0_operation, arg1_signature, arg2_signature_size, arg3_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_sign_hash_start */
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_hash_start(
|
||||
psa_sign_hash_interruptible_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length)
|
||||
{
|
||||
psa_status_t status = (psa_sign_hash_start)(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_sign_message */
|
||||
psa_status_t mbedtls_test_wrap_psa_sign_message(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
uint8_t *arg4_signature,
|
||||
size_t arg5_signature_size,
|
||||
size_t *arg6_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_sign_message)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_size, arg6_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_verify_hash */
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_hash,
|
||||
size_t arg3_hash_length,
|
||||
const uint8_t *arg4_signature,
|
||||
size_t arg5_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_verify_hash)(arg0_key, arg1_alg, arg2_hash, arg3_hash_length, arg4_signature, arg5_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_verify_hash_abort */
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_abort(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_verify_hash_abort)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_verify_hash_complete */
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_complete(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation)
|
||||
{
|
||||
psa_status_t status = (psa_verify_hash_complete)(arg0_operation);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_verify_hash_start */
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_hash_start(
|
||||
psa_verify_hash_interruptible_operation_t *arg0_operation,
|
||||
mbedtls_svc_key_id_t arg1_key,
|
||||
psa_algorithm_t arg2_alg,
|
||||
const uint8_t *arg3_hash,
|
||||
size_t arg4_hash_length,
|
||||
const uint8_t *arg5_signature,
|
||||
size_t arg6_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_verify_hash_start)(arg0_operation, arg1_key, arg2_alg, arg3_hash, arg4_hash_length, arg5_signature, arg6_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Wrapper for psa_verify_message */
|
||||
psa_status_t mbedtls_test_wrap_psa_verify_message(
|
||||
mbedtls_svc_key_id_t arg0_key,
|
||||
psa_algorithm_t arg1_alg,
|
||||
const uint8_t *arg2_input,
|
||||
size_t arg3_input_length,
|
||||
const uint8_t *arg4_signature,
|
||||
size_t arg5_signature_length)
|
||||
{
|
||||
psa_status_t status = (psa_verify_message)(arg0_key, arg1_alg, arg2_input, arg3_input_length, arg4_signature, arg5_signature_length);
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif /* defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_TEST_HOOKS) && \
|
||||
!defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
|
||||
|
||||
/* End of automatically generated file. */
|
60
tests/src/test_memory.c
Normal file
60
tests/src/test_memory.c
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* \file memory.c
|
||||
*
|
||||
* \brief Helper functions related to testing memory management.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include <test/helpers.h>
|
||||
#include <test/macros.h>
|
||||
#include <test/memory.h>
|
||||
|
||||
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
#include <sanitizer/asan_interface.h>
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||
|
||||
_Thread_local unsigned int mbedtls_test_memory_poisoning_count = 0;
|
||||
|
||||
static void align_for_asan(const unsigned char **p_ptr, size_t *p_size)
|
||||
{
|
||||
uintptr_t start = (uintptr_t) *p_ptr;
|
||||
uintptr_t end = start + (uintptr_t) *p_size;
|
||||
/* ASan can only poison regions with 8-byte alignment, and only poisons a
|
||||
* region if it's fully within the requested range. We want to poison the
|
||||
* whole requested region and don't mind a few extra bytes. Therefore,
|
||||
* align start down to an 8-byte boundary, and end up to an 8-byte
|
||||
* boundary. */
|
||||
start = start & ~(uintptr_t) 7;
|
||||
end = (end + 7) & ~(uintptr_t) 7;
|
||||
*p_ptr = (const unsigned char *) start;
|
||||
*p_size = end - start;
|
||||
}
|
||||
|
||||
void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size)
|
||||
{
|
||||
if (mbedtls_test_memory_poisoning_count == 0) {
|
||||
return;
|
||||
}
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
align_for_asan(&ptr, &size);
|
||||
__asan_poison_memory_region(ptr, size);
|
||||
}
|
||||
|
||||
void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size)
|
||||
{
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
align_for_asan(&ptr, &size);
|
||||
__asan_unpoison_memory_region(ptr, size);
|
||||
}
|
||||
#endif /* Memory poisoning */
|
@ -15,7 +15,6 @@
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_crypto_slot_management.h"
|
||||
|
||||
/* For psa_can_do_hash() */
|
||||
#include "psa_crypto_core.h"
|
||||
|
||||
#include "test/asn1_helpers.h"
|
||||
|
@ -1495,14 +1495,7 @@ void cipher_entry_points(int alg_arg, int key_type_arg,
|
||||
output, output_buffer_size, &function_output_length);
|
||||
TEST_EQUAL(mbedtls_test_driver_cipher_hooks.hits_encrypt, 1);
|
||||
TEST_EQUAL(status, PSA_ERROR_GENERIC_ERROR);
|
||||
/*
|
||||
* Check that the output buffer is still in the same state.
|
||||
* This will fail if the output buffer is used by the core to pass the IV
|
||||
* it generated to the driver (and is not restored).
|
||||
*/
|
||||
for (size_t i = 0; i < output_buffer_size; i++) {
|
||||
TEST_EQUAL(output[i], 0xa5);
|
||||
}
|
||||
|
||||
mbedtls_test_driver_cipher_hooks.hits = 0;
|
||||
|
||||
/* Test setup call, encrypt */
|
||||
|
62
tests/suites/test_suite_psa_crypto_memory.data
Normal file
62
tests/suites/test_suite_psa_crypto_memory.data
Normal file
@ -0,0 +1,62 @@
|
||||
PSA input buffer copy: straightforward copy
|
||||
copy_input:20:20:PSA_SUCCESS
|
||||
|
||||
PSA input buffer copy: copy buffer larger than required
|
||||
copy_input:10:20:PSA_SUCCESS
|
||||
|
||||
PSA input buffer copy: copy buffer too small
|
||||
copy_input:20:10:PSA_ERROR_CORRUPTION_DETECTED
|
||||
|
||||
PSA input buffer copy: zero-length source buffer
|
||||
copy_input:0:10:PSA_SUCCESS
|
||||
|
||||
PSA input buffer copy: zero-length both buffers
|
||||
copy_input:0:0:PSA_SUCCESS
|
||||
|
||||
PSA output buffer copy: straightforward copy
|
||||
copy_output:20:20:PSA_SUCCESS
|
||||
|
||||
PSA output buffer copy: output buffer larger than required
|
||||
copy_output:10:20:PSA_SUCCESS
|
||||
|
||||
PSA output buffer copy: output buffer too small
|
||||
copy_output:20:10:PSA_ERROR_BUFFER_TOO_SMALL
|
||||
|
||||
PSA output buffer copy: zero-length source buffer
|
||||
copy_output:0:10:PSA_SUCCESS
|
||||
|
||||
PSA output buffer copy: zero-length both buffers
|
||||
copy_output:0:0:PSA_SUCCESS
|
||||
|
||||
PSA crypto local input alloc
|
||||
local_input_alloc:200:PSA_SUCCESS
|
||||
|
||||
PSA crypto local input alloc, NULL buffer
|
||||
local_input_alloc:0:PSA_SUCCESS
|
||||
|
||||
PSA crypto local input free
|
||||
local_input_free:200
|
||||
|
||||
PSA crypto local input free, NULL buffer
|
||||
local_input_free:0
|
||||
|
||||
PSA crypto local input round-trip
|
||||
local_input_round_trip
|
||||
|
||||
PSA crypto local output alloc
|
||||
local_output_alloc:200:PSA_SUCCESS
|
||||
|
||||
PSA crypto local output alloc, NULL buffer
|
||||
local_output_alloc:0:PSA_SUCCESS
|
||||
|
||||
PSA crypto local output free
|
||||
local_output_free:200:0:PSA_SUCCESS
|
||||
|
||||
PSA crypto local output free, NULL buffer
|
||||
local_output_free:0:0:PSA_SUCCESS
|
||||
|
||||
PSA crypto local output free, NULL original buffer
|
||||
local_output_free:200:1:PSA_ERROR_CORRUPTION_DETECTED
|
||||
|
||||
PSA crypto local output round-trip
|
||||
local_output_round_trip
|
258
tests/suites/test_suite_psa_crypto_memory.function
Normal file
258
tests/suites/test_suite_psa_crypto_memory.function
Normal file
@ -0,0 +1,258 @@
|
||||
/* BEGIN_HEADER */
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "psa_crypto_core.h"
|
||||
#include "psa_crypto_invasive.h"
|
||||
|
||||
#include "test/psa_crypto_helpers.h"
|
||||
#include "test/memory.h"
|
||||
|
||||
/* Helper to fill a buffer with a data pattern. The pattern is not
|
||||
* important, it just allows a basic check that the correct thing has
|
||||
* been written, in a way that will detect an error in offset. */
|
||||
static void fill_buffer_pattern(uint8_t *buffer, size_t len)
|
||||
{
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
buffer[i] = (uint8_t) (i % 256);
|
||||
}
|
||||
}
|
||||
/* END_HEADER */
|
||||
|
||||
/* BEGIN_DEPENDENCIES
|
||||
* depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_TEST_HOOKS
|
||||
* END_DEPENDENCIES
|
||||
*/
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void copy_input(int src_len, int dst_len, psa_status_t exp_status)
|
||||
{
|
||||
uint8_t *src_buffer = NULL;
|
||||
uint8_t *dst_buffer = NULL;
|
||||
psa_status_t status;
|
||||
|
||||
TEST_CALLOC(src_buffer, src_len);
|
||||
TEST_CALLOC(dst_buffer, dst_len);
|
||||
|
||||
fill_buffer_pattern(src_buffer, src_len);
|
||||
|
||||
status = psa_crypto_copy_input(src_buffer, src_len, dst_buffer, dst_len);
|
||||
TEST_EQUAL(status, exp_status);
|
||||
|
||||
if (exp_status == PSA_SUCCESS) {
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(src_buffer, src_len);
|
||||
/* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */
|
||||
TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len);
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(src_buffer);
|
||||
mbedtls_free(dst_buffer);
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void copy_output(int src_len, int dst_len, psa_status_t exp_status)
|
||||
{
|
||||
uint8_t *src_buffer = NULL;
|
||||
uint8_t *dst_buffer = NULL;
|
||||
psa_status_t status;
|
||||
|
||||
TEST_CALLOC(src_buffer, src_len);
|
||||
TEST_CALLOC(dst_buffer, dst_len);
|
||||
|
||||
fill_buffer_pattern(src_buffer, src_len);
|
||||
|
||||
status = psa_crypto_copy_output(src_buffer, src_len, dst_buffer, dst_len);
|
||||
TEST_EQUAL(status, exp_status);
|
||||
|
||||
if (exp_status == PSA_SUCCESS) {
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(dst_buffer, dst_len);
|
||||
/* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */
|
||||
TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len);
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(src_buffer);
|
||||
mbedtls_free(dst_buffer);
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_input_alloc(int input_len, psa_status_t exp_status)
|
||||
{
|
||||
uint8_t *input = NULL;
|
||||
psa_crypto_local_input_t local_input;
|
||||
psa_status_t status;
|
||||
|
||||
local_input.buffer = NULL;
|
||||
|
||||
TEST_CALLOC(input, input_len);
|
||||
fill_buffer_pattern(input, input_len);
|
||||
|
||||
status = psa_crypto_local_input_alloc(input, input_len, &local_input);
|
||||
TEST_EQUAL(status, exp_status);
|
||||
|
||||
if (exp_status == PSA_SUCCESS) {
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len);
|
||||
if (input_len != 0) {
|
||||
TEST_ASSERT(local_input.buffer != input);
|
||||
}
|
||||
TEST_MEMORY_COMPARE(input, input_len,
|
||||
local_input.buffer, local_input.length);
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(local_input.buffer);
|
||||
mbedtls_free(input);
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_input_free(int input_len)
|
||||
{
|
||||
psa_crypto_local_input_t local_input;
|
||||
|
||||
local_input.buffer = NULL;
|
||||
local_input.length = input_len;
|
||||
TEST_CALLOC(local_input.buffer, local_input.length);
|
||||
|
||||
psa_crypto_local_input_free(&local_input);
|
||||
|
||||
TEST_ASSERT(local_input.buffer == NULL);
|
||||
TEST_EQUAL(local_input.length, 0);
|
||||
|
||||
exit:
|
||||
mbedtls_free(local_input.buffer);
|
||||
local_input.buffer = NULL;
|
||||
local_input.length = 0;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_input_round_trip()
|
||||
{
|
||||
psa_crypto_local_input_t local_input;
|
||||
uint8_t input[200];
|
||||
psa_status_t status;
|
||||
|
||||
fill_buffer_pattern(input, sizeof(input));
|
||||
|
||||
status = psa_crypto_local_input_alloc(input, sizeof(input), &local_input);
|
||||
TEST_EQUAL(status, PSA_SUCCESS);
|
||||
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(input, sizeof(input));
|
||||
TEST_MEMORY_COMPARE(local_input.buffer, local_input.length,
|
||||
input, sizeof(input));
|
||||
TEST_ASSERT(local_input.buffer != input);
|
||||
|
||||
psa_crypto_local_input_free(&local_input);
|
||||
TEST_ASSERT(local_input.buffer == NULL);
|
||||
TEST_EQUAL(local_input.length, 0);
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_output_alloc(int output_len, psa_status_t exp_status)
|
||||
{
|
||||
uint8_t *output = NULL;
|
||||
psa_crypto_local_output_t local_output;
|
||||
psa_status_t status;
|
||||
|
||||
local_output.buffer = NULL;
|
||||
|
||||
TEST_CALLOC(output, output_len);
|
||||
|
||||
status = psa_crypto_local_output_alloc(output, output_len, &local_output);
|
||||
TEST_EQUAL(status, exp_status);
|
||||
|
||||
if (exp_status == PSA_SUCCESS) {
|
||||
TEST_ASSERT(local_output.original == output);
|
||||
TEST_EQUAL(local_output.length, output_len);
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(local_output.buffer);
|
||||
local_output.original = NULL;
|
||||
local_output.buffer = NULL;
|
||||
local_output.length = 0;
|
||||
mbedtls_free(output);
|
||||
output = NULL;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_output_free(int output_len, int original_is_null,
|
||||
psa_status_t exp_status)
|
||||
{
|
||||
uint8_t *output = NULL;
|
||||
uint8_t *buffer_copy_for_comparison = NULL;
|
||||
psa_crypto_local_output_t local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
|
||||
psa_status_t status;
|
||||
|
||||
if (!original_is_null) {
|
||||
TEST_CALLOC(output, output_len);
|
||||
}
|
||||
TEST_CALLOC(buffer_copy_for_comparison, output_len);
|
||||
TEST_CALLOC(local_output.buffer, output_len);
|
||||
local_output.length = output_len;
|
||||
local_output.original = output;
|
||||
|
||||
if (local_output.length != 0) {
|
||||
fill_buffer_pattern(local_output.buffer, local_output.length);
|
||||
memcpy(buffer_copy_for_comparison, local_output.buffer, local_output.length);
|
||||
}
|
||||
|
||||
status = psa_crypto_local_output_free(&local_output);
|
||||
TEST_EQUAL(status, exp_status);
|
||||
|
||||
if (exp_status == PSA_SUCCESS) {
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len);
|
||||
TEST_ASSERT(local_output.buffer == NULL);
|
||||
TEST_EQUAL(local_output.length, 0);
|
||||
TEST_MEMORY_COMPARE(buffer_copy_for_comparison, output_len,
|
||||
output, output_len);
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(output);
|
||||
mbedtls_free(buffer_copy_for_comparison);
|
||||
mbedtls_free(local_output.buffer);
|
||||
local_output.length = 0;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void local_output_round_trip()
|
||||
{
|
||||
psa_crypto_local_output_t local_output;
|
||||
uint8_t output[200];
|
||||
uint8_t *buffer_copy_for_comparison = NULL;
|
||||
psa_status_t status;
|
||||
|
||||
status = psa_crypto_local_output_alloc(output, sizeof(output), &local_output);
|
||||
TEST_EQUAL(status, PSA_SUCCESS);
|
||||
TEST_ASSERT(local_output.buffer != output);
|
||||
|
||||
/* Simulate the function generating output */
|
||||
fill_buffer_pattern(local_output.buffer, local_output.length);
|
||||
|
||||
TEST_CALLOC(buffer_copy_for_comparison, local_output.length);
|
||||
memcpy(buffer_copy_for_comparison, local_output.buffer, local_output.length);
|
||||
|
||||
psa_crypto_local_output_free(&local_output);
|
||||
TEST_ASSERT(local_output.buffer == NULL);
|
||||
TEST_EQUAL(local_output.length, 0);
|
||||
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(output, sizeof(output));
|
||||
/* Check that the buffer was correctly copied back */
|
||||
TEST_MEMORY_COMPARE(output, sizeof(output),
|
||||
buffer_copy_for_comparison, sizeof(output));
|
||||
|
||||
exit:
|
||||
mbedtls_free(buffer_copy_for_comparison);
|
||||
}
|
||||
/* END_CASE */
|
23
tests/suites/test_suite_test_helpers.data
Normal file
23
tests/suites/test_suite_test_helpers.data
Normal file
@ -0,0 +1,23 @@
|
||||
Memory poison+unpoison: offset=0 len=42
|
||||
memory_poison_unpoison:0:42
|
||||
|
||||
Memory poison+unpoison: offset=0 len=1
|
||||
memory_poison_unpoison:0:1
|
||||
|
||||
Memory poison+unpoison: offset=0 len=2
|
||||
memory_poison_unpoison:0:2
|
||||
|
||||
Memory poison+unpoison: offset=1 len=1
|
||||
memory_poison_unpoison:1:1
|
||||
|
||||
Memory poison+unpoison: offset=1 len=2
|
||||
memory_poison_unpoison:1:2
|
||||
|
||||
Memory poison+unpoison: offset=7 len=1
|
||||
memory_poison_unpoison:7:1
|
||||
|
||||
Memory poison+unpoison: offset=7 len=2
|
||||
memory_poison_unpoison:7:2
|
||||
|
||||
Memory poison+unpoison: offset=0 len=0
|
||||
memory_poison_unpoison:0:0
|
40
tests/suites/test_suite_test_helpers.function
Normal file
40
tests/suites/test_suite_test_helpers.function
Normal file
@ -0,0 +1,40 @@
|
||||
/* BEGIN_HEADER */
|
||||
|
||||
/* Test some parts of the test framework. */
|
||||
|
||||
#include <test/helpers.h>
|
||||
#include <test/memory.h>
|
||||
|
||||
/* END_HEADER */
|
||||
|
||||
/* BEGIN_DEPENDENCIES */
|
||||
|
||||
/* END_DEPENDENCIES */
|
||||
|
||||
/* BEGIN_CASE depends_on:MBEDTLS_TEST_MEMORY_CAN_POISON */
|
||||
/* Test that poison+unpoison leaves the memory accessible. */
|
||||
/* We can't test that poisoning makes the memory inaccessible:
|
||||
* there's no sane way to catch an Asan/Valgrind complaint.
|
||||
* That negative testing is done in programs/test/metatest.c. */
|
||||
void memory_poison_unpoison(int align, int size)
|
||||
{
|
||||
unsigned char *buf = NULL;
|
||||
const size_t buffer_size = align + size;
|
||||
TEST_CALLOC(buf, buffer_size);
|
||||
|
||||
for (size_t i = 0; i < buffer_size; i++) {
|
||||
buf[i] = (unsigned char) (i & 0xff);
|
||||
}
|
||||
|
||||
const unsigned char *start = buf == NULL ? NULL : buf + align;
|
||||
mbedtls_test_memory_poison(start, (size_t) size);
|
||||
mbedtls_test_memory_unpoison(start, (size_t) size);
|
||||
|
||||
for (size_t i = 0; i < buffer_size; i++) {
|
||||
TEST_EQUAL(buf[i], (unsigned char) (i & 0xff));
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free(buf);
|
||||
}
|
||||
/* END_CASE */
|
Loading…
x
Reference in New Issue
Block a user