psa_crypto_core_common.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #ifndef PSA_CRYPTO_CORE_COMMON_H
  11. #define PSA_CRYPTO_CORE_COMMON_H
  12. /** Return an offset into a buffer.
  13. *
  14. * This is just the addition of an offset to a pointer, except that this
  15. * function also accepts an offset of 0 into a buffer whose pointer is null.
  16. * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
  17. * A null pointer is a valid buffer pointer when the size is 0, for example
  18. * as the result of `malloc(0)` on some platforms.)
  19. *
  20. * \param p Pointer to a buffer of at least n bytes.
  21. * This may be \p NULL if \p n is zero.
  22. * \param n An offset in bytes.
  23. * \return Pointer to offset \p n in the buffer \p p.
  24. * Note that this is only a valid pointer if the size of the
  25. * buffer is at least \p n + 1.
  26. */
  27. static inline unsigned char *psa_crypto_buffer_offset(
  28. unsigned char *p, size_t n)
  29. {
  30. return p == NULL ? NULL : p + n;
  31. }
  32. /** Return an offset into a read-only buffer.
  33. *
  34. * Similar to mbedtls_buffer_offset(), but for const pointers.
  35. *
  36. * \param p Pointer to a buffer of at least n bytes.
  37. * This may be \p NULL if \p n is zero.
  38. * \param n An offset in bytes.
  39. * \return Pointer to offset \p n in the buffer \p p.
  40. * Note that this is only a valid pointer if the size of the
  41. * buffer is at least \p n + 1.
  42. */
  43. static inline const unsigned char *psa_crypto_buffer_offset_const(
  44. const unsigned char *p, size_t n)
  45. {
  46. return p == NULL ? NULL : p + n;
  47. }
  48. #endif /* PSA_CRYPTO_CORE_COMMON_H */