1 /**
2  * \file psa_crypto_core_common.h
3  *
4  * \brief Utility macros for internal use in the PSA cryptography core.
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #ifndef PSA_CRYPTO_CORE_COMMON_H
12 #define PSA_CRYPTO_CORE_COMMON_H
13 
14 /** Return an offset into a buffer.
15  *
16  * This is just the addition of an offset to a pointer, except that this
17  * function also accepts an offset of 0 into a buffer whose pointer is null.
18  * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
19  * A null pointer is a valid buffer pointer when the size is 0, for example
20  * as the result of `malloc(0)` on some platforms.)
21  *
22  * \param p     Pointer to a buffer of at least n bytes.
23  *              This may be \p NULL if \p n is zero.
24  * \param n     An offset in bytes.
25  * \return      Pointer to offset \p n in the buffer \p p.
26  *              Note that this is only a valid pointer if the size of the
27  *              buffer is at least \p n + 1.
28  */
psa_crypto_buffer_offset(unsigned char * p,size_t n)29 static inline unsigned char *psa_crypto_buffer_offset(
30     unsigned char *p, size_t n)
31 {
32     return p == NULL ? NULL : p + n;
33 }
34 
35 /** Return an offset into a read-only buffer.
36  *
37  * Similar to mbedtls_buffer_offset(), but for const pointers.
38  *
39  * \param p     Pointer to a buffer of at least n bytes.
40  *              This may be \p NULL if \p n is zero.
41  * \param n     An offset in bytes.
42  * \return      Pointer to offset \p n in the buffer \p p.
43  *              Note that this is only a valid pointer if the size of the
44  *              buffer is at least \p n + 1.
45  */
psa_crypto_buffer_offset_const(const unsigned char * p,size_t n)46 static inline const unsigned char *psa_crypto_buffer_offset_const(
47     const unsigned char *p, size_t n)
48 {
49     return p == NULL ? NULL : p + n;
50 }
51 
52 #endif /* PSA_CRYPTO_CORE_COMMON_H */
53