OSSL_SAFE_MATH_SIGNED.pod 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. =pod
  2. =head1 NAME
  3. OSSL_SAFE_MATH_SIGNED, OSSL_SAFE_MATH_UNSIGNED,
  4. safe_add_TYPE, safe_sub_TYPE, safe_mul_TYPE, safe_div_TYPE, safe_mod_TYPE,
  5. safe_div_round_up_TYPE, safe_neg_TYPE
  6. - create helper functions to safely perform non-overflowing integer operations
  7. =head1 SYNOPSIS
  8. =for openssl generic
  9. #include "internal/safe_math.h"
  10. OSSL_SAFE_MATH_SIGNED(NAME, TYPE)
  11. OSSL_SAFE_MATH_UNSIGNED(NAME, TYPE)
  12. TYPE safe_add_TYPE(TYPE a, TYPE b, int *err);
  13. TYPE safe_sub_TYPE(TYPE a, TYPE b, int *err);
  14. TYPE safe_mul_TYPE(TYPE a, TYPE b, int *err);
  15. TYPE safe_div_TYPE(TYPE a, TYPE b, int *err);
  16. TYPE safe_mod_TYPE(TYPE a, TYPE b, int *err);
  17. TYPE safe_div_round_up_TYPE(TYPE a, TYPE b, int *err);
  18. TYPE safe_muldiv_TYPE(TYPE a, TYPE b, TYPE c, int *err);
  19. TYPE safe_neg_TYPE(TYPE a, int *err);
  20. TYPE safe_abs_TYPE(TYPE a, int *err);
  21. =head1 DESCRIPTION
  22. Define helper functions to assist with handling integer overflow detection.
  23. All of these functions perform an arithmetic operation on its arguments and
  24. return the result of the operation. If the operation cannot be
  25. correctly represented, the error I<err> flag is set. No behaviour that is
  26. undefined as per the C standard will take place.
  27. OSSL_SAFE_MATH_SIGNED() creates helper functions for the B<I<TYPE>> with the
  28. suffix B<I<NAME>>.
  29. OSSL_SAFE_MATH_UNSIGNED() creates helper functions for the B<I<TYPE>> with the
  30. suffix B<I<NAME>>.
  31. safe_add_TYPE() adds the two arguments I<a> and I<b> together.
  32. safe_sub_TYPE() subtracts I<b> from I<a>.
  33. safe_mul_TYPE() multiplies the two arguments I<a> and I<b> together.
  34. safe_div_TYPE() divides I<a> by I<b>.
  35. safe_mod_TYPE() calculates the remainder when I<a> is divided by I<b>.
  36. safe_div_round_up_TYPE() calculates I<a> / I<b> + (I<a> % I<b> != 0).
  37. I.e. it computes the quotient of I<a> and I<b> rounding any remainder towards
  38. positive infinity.
  39. safe_muldiv_TYPE() multiplies I<a> and I<b> together and divides the
  40. result by I<c>.
  41. safe_neg_TYPE() calculates the negation of I<a>.
  42. safe_abs_TYPE() calculates the absolute value of I<a>.
  43. =head1 NOTES
  44. The safe_muldiv_TYPE() function is not perfect. There exist inputs where
  45. a valid result could be computed with infinite length integers but this
  46. function returns an error condition. Such instances should, however,
  47. be rare in practice. The converse is not true. An invalid result will
  48. always be flagged as an error.
  49. =head1 RETURN VALUES
  50. All these functions return the result of the operation, if the operation
  51. is well defined. They return an arbitrary value if not.
  52. =head1 EXAMPLES
  53. This example is of a function that computes the size of a record that
  54. has a four byte element count which is followed by that many elements.
  55. It returns zero on overflow.
  56. OSSL_SAFE_MATH_UNSIGNED(sizet, size_t, SIZE_MAX)
  57. size_t compute_record_size(uint32_t n)
  58. {
  59. int err = 0;
  60. size_t result, product;
  61. product = safe_mul_sizet(n, sizeof(struct widget), &err);
  62. result = safe_add_sizet(product, sizeof(n), &err);
  63. return err ? 0 : result;
  64. }
  65. =head1 HISTORY
  66. The functions described here were all added in OpenSSL 3.2.
  67. =head1 COPYRIGHT
  68. Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  69. Licensed under the Apache License 2.0 (the "License"). You may not use
  70. this file except in compliance with the License. You can obtain a copy
  71. in the file LICENSE in the source distribution or at
  72. L<https://www.openssl.org/source/license.html>.
  73. =cut