error.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * \file error.h
  3. *
  4. * \brief Error to string translation
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_ERROR_H
  11. #define MBEDTLS_ERROR_H
  12. #include "mbedtls/build_info.h"
  13. #include <stddef.h>
  14. /**
  15. * Error code layout.
  16. *
  17. * Currently we try to keep all error codes within the negative space of 16
  18. * bits signed integers to support all platforms (-0x0001 - -0x7FFF). In
  19. * addition we'd like to give two layers of information on the error if
  20. * possible.
  21. *
  22. * For that purpose the error codes are segmented in the following manner:
  23. *
  24. * 16 bit error code bit-segmentation
  25. *
  26. * 1 bit - Unused (sign bit)
  27. * 3 bits - High level module ID
  28. * 5 bits - Module-dependent error code
  29. * 7 bits - Low level module errors
  30. *
  31. * For historical reasons, low-level error codes are divided in even and odd,
  32. * even codes were assigned first, and -1 is reserved for other errors.
  33. *
  34. * Low-level module errors (0x0002-0x007E, 0x0001-0x007F)
  35. *
  36. * Module Nr Codes assigned
  37. * ERROR 2 0x006E 0x0001
  38. * MPI 7 0x0002-0x0010
  39. * GCM 3 0x0012-0x0016 0x0013-0x0013
  40. * THREADING 3 0x001A-0x001E
  41. * AES 5 0x0020-0x0022 0x0021-0x0025
  42. * CAMELLIA 3 0x0024-0x0026 0x0027-0x0027
  43. * BASE64 2 0x002A-0x002C
  44. * OID 1 0x002E-0x002E 0x000B-0x000B
  45. * PADLOCK 1 0x0030-0x0030
  46. * DES 2 0x0032-0x0032 0x0033-0x0033
  47. * CTR_DBRG 4 0x0034-0x003A
  48. * ENTROPY 3 0x003C-0x0040 0x003D-0x003F
  49. * NET 13 0x0042-0x0052 0x0043-0x0049
  50. * ARIA 4 0x0058-0x005E
  51. * ASN1 7 0x0060-0x006C
  52. * CMAC 1 0x007A-0x007A
  53. * PBKDF2 1 0x007C-0x007C
  54. * HMAC_DRBG 4 0x0003-0x0009
  55. * CCM 3 0x000D-0x0011
  56. * MD5 1 0x002F-0x002F
  57. * RIPEMD160 1 0x0031-0x0031
  58. * SHA1 1 0x0035-0x0035 0x0073-0x0073
  59. * SHA256 1 0x0037-0x0037 0x0074-0x0074
  60. * SHA512 1 0x0039-0x0039 0x0075-0x0075
  61. * SHA-3 1 0x0076-0x0076
  62. * CHACHA20 3 0x0051-0x0055
  63. * POLY1305 3 0x0057-0x005B
  64. * CHACHAPOLY 2 0x0054-0x0056
  65. * PLATFORM 2 0x0070-0x0072
  66. * LMS 5 0x0011-0x0019
  67. *
  68. * High-level module nr (3 bits - 0x0...-0x7...)
  69. * Name ID Nr of Errors
  70. * PEM 1 9
  71. * PKCS#12 1 4 (Started from top)
  72. * X509 2 20
  73. * PKCS5 2 4 (Started from top)
  74. * DHM 3 11
  75. * PK 3 15 (Started from top)
  76. * RSA 4 11
  77. * ECP 4 10 (Started from top)
  78. * MD 5 5
  79. * HKDF 5 1 (Started from top)
  80. * PKCS7 5 12 (Started from 0x5300)
  81. * SSL 5 3 (Started from 0x5F00)
  82. * CIPHER 6 8 (Started from 0x6080)
  83. * SSL 6 22 (Started from top, plus 0x6000)
  84. * SSL 7 20 (Started from 0x7000, gaps at
  85. * 0x7380, 0x7900-0x7980, 0x7A80-0x7E80)
  86. *
  87. * Module dependent error code (5 bits 0x.00.-0x.F8.)
  88. */
  89. #ifdef __cplusplus
  90. extern "C" {
  91. #endif
  92. /** Generic error */
  93. #define MBEDTLS_ERR_ERROR_GENERIC_ERROR -0x0001
  94. /** This is a bug in the library */
  95. #define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E
  96. /** Hardware accelerator failed */
  97. #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
  98. /** The requested feature is not supported by the platform */
  99. #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
  100. /**
  101. * \brief Combines a high-level and low-level error code together.
  102. *
  103. * Wrapper macro for mbedtls_error_add(). See that function for
  104. * more details.
  105. */
  106. #define MBEDTLS_ERROR_ADD(high, low) \
  107. mbedtls_error_add(high, low, __FILE__, __LINE__)
  108. #if defined(MBEDTLS_TEST_HOOKS)
  109. /**
  110. * \brief Testing hook called before adding/combining two error codes together.
  111. * Only used when invasive testing is enabled via MBEDTLS_TEST_HOOKS.
  112. */
  113. extern void (*mbedtls_test_hook_error_add)(int, int, const char *, int);
  114. #endif
  115. /**
  116. * \brief Combines a high-level and low-level error code together.
  117. *
  118. * This function can be called directly however it is usually
  119. * called via the #MBEDTLS_ERROR_ADD macro.
  120. *
  121. * While a value of zero is not a negative error code, it is still an
  122. * error code (that denotes success) and can be combined with both a
  123. * negative error code or another value of zero.
  124. *
  125. * \note When invasive testing is enabled via #MBEDTLS_TEST_HOOKS, also try to
  126. * call \link mbedtls_test_hook_error_add \endlink.
  127. *
  128. * \param high high-level error code. See error.h for more details.
  129. * \param low low-level error code. See error.h for more details.
  130. * \param file file where this error code addition occurred.
  131. * \param line line where this error code addition occurred.
  132. */
  133. static inline int mbedtls_error_add(int high, int low,
  134. const char *file, int line)
  135. {
  136. #if defined(MBEDTLS_TEST_HOOKS)
  137. if (*mbedtls_test_hook_error_add != NULL) {
  138. (*mbedtls_test_hook_error_add)(high, low, file, line);
  139. }
  140. #endif
  141. (void) file;
  142. (void) line;
  143. return high + low;
  144. }
  145. /**
  146. * \brief Translate an Mbed TLS error code into a string representation.
  147. * The result is truncated if necessary and always includes a
  148. * terminating null byte.
  149. *
  150. * \param errnum error code
  151. * \param buffer buffer to place representation in
  152. * \param buflen length of the buffer
  153. */
  154. void mbedtls_strerror(int errnum, char *buffer, size_t buflen);
  155. /**
  156. * \brief Translate the high-level part of an Mbed TLS error code into a string
  157. * representation.
  158. *
  159. * This function returns a const pointer to an un-modifiable string. The caller
  160. * must not try to modify the string. It is intended to be used mostly for
  161. * logging purposes.
  162. *
  163. * \param error_code error code
  164. *
  165. * \return The string representation of the error code, or \c NULL if the error
  166. * code is unknown.
  167. */
  168. const char *mbedtls_high_level_strerr(int error_code);
  169. /**
  170. * \brief Translate the low-level part of an Mbed TLS error code into a string
  171. * representation.
  172. *
  173. * This function returns a const pointer to an un-modifiable string. The caller
  174. * must not try to modify the string. It is intended to be used mostly for
  175. * logging purposes.
  176. *
  177. * \param error_code error code
  178. *
  179. * \return The string representation of the error code, or \c NULL if the error
  180. * code is unknown.
  181. */
  182. const char *mbedtls_low_level_strerr(int error_code);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* error.h */