s2n_safety.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "error/s2n_errno.h"
  21. #include "utils/s2n_ensure.h"
  22. #include "utils/s2n_result.h"
  23. #include "utils/s2n_safety_macros.h"
  24. /**
  25. * The goal of s2n_safety is to provide helpers to perform common
  26. * checks, which help with code readability.
  27. */
  28. /**
  29. * Marks a case of a switch statement as able to fall through to the next case
  30. */
  31. #if defined(S2N_FALL_THROUGH_SUPPORTED)
  32. #define FALL_THROUGH __attribute__((fallthrough))
  33. #else
  34. #define FALL_THROUGH ((void) 0)
  35. #endif
  36. int s2n_in_unit_test_set(bool is_unit);
  37. int s2n_in_integ_test_set(bool is_integ);
  38. bool s2n_in_unit_test();
  39. bool s2n_in_test();
  40. /* Returns 1 if a and b are equal, in constant time */
  41. bool s2n_constant_time_equals(const uint8_t* a, const uint8_t* b, const uint32_t len);
  42. /* Copy src to dst, or don't copy it, in constant time */
  43. int s2n_constant_time_copy_or_dont(uint8_t* dst, const uint8_t* src, uint32_t len, uint8_t dont);
  44. /* If src contains valid PKCS#1 v1.5 padding of exactly expectlen bytes, decode
  45. * it into dst, otherwise leave dst alone, in constant time.
  46. * Always returns zero. */
  47. int s2n_constant_time_pkcs1_unpad_or_dont(uint8_t* dst, const uint8_t* src, uint32_t srclen, uint32_t expectlen);
  48. /**
  49. * Runs _thecleanup function on _thealloc once _thealloc went out of scope
  50. */
  51. #define DEFER_CLEANUP(_thealloc, _thecleanup) \
  52. __attribute__((cleanup(_thecleanup))) _thealloc
  53. /**
  54. * Often we want to free memory on an error, but not on a success.
  55. * We do this by declaring a variable with DEFER_CLEANUP, then zeroing
  56. * that variable after success to prevent DEFER_CLEANUP from accessing
  57. * and freeing any memory it allocated.
  58. *
  59. * This pattern is not intuitive, so a named macro makes it more readable.
  60. */
  61. #define ZERO_TO_DISABLE_DEFER_CLEANUP(_thealloc) memset(&_thealloc, 0, sizeof(_thealloc))
  62. /* We want to apply blinding whenever `action` fails.
  63. * Unfortunately, because functions in S2N do not have a consistent return type, determining failure is difficult.
  64. * Instead, let's rely on the consistent error handling behavior of returning from a method early on error
  65. * and apply blinding if our tracking variable goes out of scope early.
  66. */
  67. S2N_CLEANUP_RESULT s2n_connection_apply_error_blinding(struct s2n_connection** conn);
  68. #define WITH_ERROR_BLINDING(conn, action) \
  69. do { \
  70. DEFER_CLEANUP(struct s2n_connection* _conn_to_blind = conn, s2n_connection_apply_error_blinding); \
  71. action; \
  72. /* The `if` here is to avoid a redundantInitialization warning from cppcheck */ \
  73. if (_conn_to_blind) { \
  74. _conn_to_blind = NULL; \
  75. } \
  76. } while (0)
  77. /* Creates cleanup function for pointers from function func which accepts a pointer.
  78. * This is useful for DEFER_CLEANUP as it passes &_thealloc into _thecleanup function,
  79. * so if _thealloc is a pointer _thecleanup will receive a pointer to a pointer.*/
  80. #define DEFINE_POINTER_CLEANUP_FUNC(type, func) \
  81. static inline void func##_pointer(type* p) \
  82. { \
  83. if (p && *p) \
  84. func(*p); \
  85. } \
  86. struct __useless_struct_to_allow_trailing_semicolon__
  87. #define s2n_array_len(array) ((array != NULL) ? (sizeof(array) / sizeof(array[0])) : 0)
  88. int s2n_mul_overflow(uint32_t a, uint32_t b, uint32_t* out);
  89. /**
  90. * Rounds "initial" up to a multiple of "alignment", and stores the result in "out".
  91. * Raises an error if overflow would occur.
  92. * NOT CONSTANT TIME.
  93. */
  94. int s2n_align_to(uint32_t initial, uint32_t alignment, uint32_t* out);
  95. int s2n_add_overflow(uint32_t a, uint32_t b, uint32_t* out);
  96. int s2n_sub_overflow(uint32_t a, uint32_t b, uint32_t* out);