[openssl/openssl] 9a4ddf: design: Fixed size large numbers

"'Richard Levitte' via openssl-commits" <[email protected]>
Newsgroups gmane.comp.encryption.openssl.cvs
Message-ID <openssl/openssl/push/refs/heads/feature/ossl_fn/[email protected]>
  Branch: refs/heads/feature/ossl_fn
  Home:   https://github.com/openssl/openssl
  Commit: 9a4ddf83bcd887276733931a517383996fba0e7a
      https://github.com/openssl/openssl/commit/9a4ddf83bcd887276733931a517383996fba0e7a
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    A doc/designs/fixed-size-large-numbers.md

  Log Message:
  -----------
  design: Fixed size large numbers

For the longest time, we have mitigated security issues related to large
numbers (BIGNUM) and constant time in a piece-meal fashion, without really
looking at the problem from a zoomed out, holistic perspective.

An interesting aspect in this problem is that large numbers can vary in
size, and that depending on their combined sizes, the time to perform
mathematical calculations with them vary equally much, and may thereby
unintentionally leak information on those numbers.

To mitigate that sort of timing issue, we introduce fixed size numbers,
which are designed to have payload sizes that are pre-determined, usually by
the crypto system that uses them.  This means that even a very small number
(let's take 1 as a ridiculous example) would have the same size payload as a
much larger number, and calculations using them would perform across all
payload bits of all input numbers combined.

These fixed size numbers primarly differ from BIGNUMs in that once they have
been allocated to a certain size, that size will not change throughout its
lifetime.

Reviewed-by: Dmitry Belyavskiy <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/28522)


  Commit: cfee88d925895b08c21902259af9641d2a27eee4
      https://github.com/openssl/openssl/commit/cfee88d925895b08c21902259af9641d2a27eee4
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M doc/designs/fixed-size-large-numbers.md

  Log Message:
  -----------
  Fix spelling in doc/designs/fixed-size-large-numbers.md

Found by codespell.

payed → paid

Reviewed-by: Frederik Wedel-Heinen <[email protected]>
Reviewed-by: Shane Lontis <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/28787)


  Commit: ecb93247e1771cbc81b64190eca64238e3e1082c
      https://github.com/openssl/openssl/commit/ecb93247e1771cbc81b64190eca64238e3e1082c
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/build.info
    A crypto/fn/build.info
    A crypto/fn/fn_lib.c
    A crypto/fn/fn_local.h
    A include/crypto/fn.h
    M include/crypto/types.h
    M include/openssl/bn.h
    A include/openssl/bn_limbs.h
    M test/build.info
    A test/fn_internal_test.c
    A test/recipes/03-test_internal_fn.t

  Log Message:
  -----------
  Add the fixed number type (OSSL_FN) and its allocators and deallocator

This includes a small test program that performs introspection of the
OSSL_FN, to check that diverse functions do what's expected of them.

For future compatibility reasons, the limb type OSSL_FN_ULONG is based
on BN_ULONG.  This caused a slight rearrangement of public BIGNUM related
headers.

Note: experiments with changing the current BIGNUM's 'dmax' and 'top' to be
"size_t" has shown disastrous effects, due to some lower level functions
assuming that they'll receive the size in "int" form rather than "size_t"
form (on some major platforms, these two types have different sizes).
Therefore, this change deviates slightly from the design for fixed numbers
(doc/designs/fixed-size-large-numbers.md) by making OSSL_FN's 'dsize' an
"int" rather than a "size_t".

Related-to: doc/designs/fixed-size-large-numbers.md
Resolves: https://github.com/openssl/project/issues/1649

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/28784)


  Commit: 2379ba80c182029b5d2cb8cf6f9932cb55a72540
      https://github.com/openssl/openssl/commit/2379ba80c182029b5d2cb8cf6f9932cb55a72540
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_dh.c
    M crypto/bn/bn_exp.c
    M crypto/bn/bn_intern.c
    M crypto/bn/bn_lib.c
    M crypto/bn/bn_local.h
    M crypto/bn/bn_nist.c
    M crypto/bn/bn_prime.c
    M crypto/bn/bn_rsa_fips186_5.c
    M crypto/bn/bn_srp.c
    M crypto/fn/fn_lib.c
    M crypto/fn/fn_local.h
    M include/crypto/bn.h
    M include/crypto/fn.h

  Log Message:
  -----------
  First integration of OSSL_FN into BIGNUM

This integration is made in such a way that OSSL_FN is an optional
'data' field in BIGNUM, i.e. it's allowed to be NULL even though
the BIGNUM's 'd' field is non-NULL.

The public BIGNUM API will do what it can to ensure that the 'data'
field becomes non-NULL, but remains lax on input BIGNUMs, for now.
This allows diverse internal bn constants and hacks to continue to
function with minimal friction.  These constants and hacks will
incrementally be modified to use OSSL_FN where they currently use
BN_ULONG.

Related-to: doc/designs/fixed-size-large-numbers.md
Resolves: https://github.com/openssl/openssl/issues/28931

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/28930)


  Commit: dd286f40abeba86e01be69bc9c3d8cb6dd7dd44b
      https://github.com/openssl/openssl/commit/dd286f40abeba86e01be69bc9c3d8cb6dd7dd44b
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_add.c
    M crypto/bn/bn_blind.c
    M crypto/bn/bn_conv.c
    M crypto/bn/bn_div.c
    M crypto/bn/bn_exp.c
    M crypto/bn/bn_gf2m.c
    M crypto/bn/bn_intern.c
    M crypto/bn/bn_lib.c
    M crypto/bn/bn_local.h
    M crypto/bn/bn_mod.c
    M crypto/bn/bn_mont.c
    M crypto/bn/bn_mpi.c
    M crypto/bn/bn_mul.c
    M crypto/bn/bn_nist.c
    M crypto/bn/bn_shift.c
    M crypto/bn/bn_sqr.c

  Log Message:
  -----------
  BIGNUM: Adjust the requirements on 'top' and the 'd' array for OSSL_FN compat

BIGNUM is quite sloppy with its contents of the 'd' array above 'top'.  This
has been further exasperated by the 'bn_pollute' macro, which makes that slop
quite explicit.

That's fine within a purely BIGNUM context.

Enter OSSL_FN, which requires that the whole 'd' array is numerically
consistent, not just the BN_ULONGs up to 'top'.

This will, of course, cause trouble as soon as an OSSL_FN that's integrated
in a BIGNUM gets passed to OSSL_FN functions.

To ensure consistency, the following updates are made:

- [only for BIGNUMs in which 'data' is non-NULL] when decreasing 'top', all
  BN_ULONGs between the preceding 'top' and the new 'top' must be made zero.
- Drop bn_pollute() entirely, as it's now more harmful than useful.
- Modify bn_check_top() to better check the consistency of BIGNUM with
  integrated OSSL_FN, by checking that the part of the 'd' array between
  'top' and 'dmax' is all zeroes.
- Add the function 'bn_set_top()', which is recommended to use instead of
  assigning 'top' directly, as it will zeroise the intermediary limbs in
  the 'd' array when 'top' decreases.

On using 'bn_set_top()', it's highly recommended to use it everywhere,
unless you can be absolutely sure that the BIGNUM that's modified will never
be checked with 'bn_check_top()' or passed to any OSSL_FN function.

Related-to: doc/designs/fixed-size-large-numbers.md

Reviewed-by: Dmitry Belyavskiy <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29015)


  Commit: 8f88f337c5b086adb69be776602a4ff52772cd47
      https://github.com/openssl/openssl/commit/8f88f337c5b086adb69be776602a4ff52772cd47
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_local.h

  Log Message:
  -----------
  BN_DEBUG: Refactor 'bn_check_top', 'bn_wcheck_size' into static inline functions

All the BN_DEBUG code is also moved below the definition of 'struct bignum_st',
to keep the compilers happy.

Reviewed-by: Dmitry Belyavskiy <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29015)


  Commit: cd0a17c522936d24baff7d902ad36476a3a212ef
      https://github.com/openssl/openssl/commit/cd0a17c522936d24baff7d902ad36476a3a212ef
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/err/err_all.c
    M crypto/err/openssl.ec
    M crypto/err/openssl.txt
    M crypto/fn/build.info
    A crypto/fn/fn_err.c
    A crypto/fn/fn_intern.c
    M crypto/fn/fn_local.h
    A include/crypto/fn_intern.h
    A include/crypto/fnerr.h
    M include/openssl/err.h.in
    M test/fn_internal_test.c

  Log Message:
  -----------
  OSSL_FN: Add internal construction and introspection functions

These functions will be useful with other test programs without having
to include crypto/fn/fn_local.h, making them closer to real world use.

This also introduces OSSL_FN errors

Related-to: doc/designs/fixed-size-large-numbers.md

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29028)


  Commit: 22a9b22d84abaec894da22c9e349c60dd40565bd
      https://github.com/openssl/openssl/commit/22a9b22d84abaec894da22c9e349c60dd40565bd
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/fn/build.info
    A crypto/fn/fn_addsub.c
    M crypto/fn/fn_local.h
    M include/crypto/fn.h
    M test/README-dev.md
    M test/build.info
    A test/fn_api_test.c
    A test/recipes/11-test_fn_api.t

  Log Message:
  -----------
  OSSL_FN: Add 'add' and 'sub' functions

This also introduces 'cmp' and 'ucmp' functions, as well as an OSSL_FN
API test program.

OSSL_FNs must not be polluted, so if a BIGNUM has a non-NULL 'data'
field, bn_pollute() will not pollute it.  It may be a good idea, though,
to pollute an OSSL_FN before an operation result is written to it, for
testing purposes.

Related-to: doc/designs/fixed-size-large-numbers.md
Resolves: https://github.com/openssl/openssl/issues/28932

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29028)


  Commit: e9f71e27e4241b9e4caef22f40483075daf64671
      https://github.com/openssl/openssl/commit/e9f71e27e4241b9e4caef22f40483075daf64671
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_local.h

  Log Message:
  -----------
  OSSL_FN: Add internal functions to acquire the OSSL_FN from a BIGNUM

bn_acquire_ossl_fn() returns the OSSL_FN of a BIGNUM if there is one,
expanded to a given number of limbs.
bn_release() makes necessary adjustments to the BIGNUM after a run of
OSSL_FN operations on the OSSL instance it's backed by.

These two functions are most useful with BIGNUMs used to store operation
results, and are meant to help refactoring BN operators to be wrappers
around corresponding OSSL_FN operators.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29028)


  Commit: 905661e05cc03bf93756ba49c0b688a3ea1cd0d7
      https://github.com/openssl/openssl/commit/905661e05cc03bf93756ba49c0b688a3ea1cd0d7
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_add.c
    M crypto/bn/bn_lib.c
    M crypto/bn/bn_local.h

  Log Message:
  -----------
  OSSL_FN: Wrap BIGNUM 'add' and 'sub' functions around OSSL_FN functions

This involves the following functions:

- 'BN_add'
- 'BN_sub'
- 'BN_uadd'
- 'BN_usub'

Care is taken to use the previous (now legacy) code if any of the argument
BIGNUMs isn't backed by an OSSL_FN (i.e. if its 'data' field is NULL).

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29028)


  Commit: aea82b4836163626bd1d3e5ec05670a625d70526
      https://github.com/openssl/openssl/commit/aea82b4836163626bd1d3e5ec05670a625d70526
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_mul.c

  Log Message:
  -----------
  BIGNUM: fix setting of 'top' in 'bn_mul_fixed_top'

When 'bn_set_top()' was applied, a couple of spots were missed.  This
cause some test failures.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29028)


  Commit: 97efcb74d8e0b8a66515297695950681913ca76e
      https://github.com/openssl/openssl/commit/97efcb74d8e0b8a66515297695950681913ca76e
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/fn/build.info
    A crypto/fn/fn_ctx.c
    M crypto/fn/fn_lib.c
    M crypto/fn/fn_local.h
    M include/crypto/fn.h
    M include/crypto/types.h
    M test/fn_internal_test.c

  Log Message:
  -----------
  Add the fixed number context (OSSL_FN_CTX) with (de)allocators and tests

OSSL_FN_CTX is a reimplementation of BN_CTX, with the intent to have
good enough API parity.

The distinguishing feature with OSSL_FN_CTX is that it works as an arena
allocator, so it must be allocated with an estimate of how much memory
space will be needed for all OSSL_FN instances that will be retrieved
from that arena, and a bit of overhead.

Related-to: doc/designs/fixed-size-large-numbers.md
Resolves: https://github.com/openssl/openssl/issues/28933

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29161)


  Commit: 4eb452f181c22033e5a797ae50e2d90f9fa793f2
      https://github.com/openssl/openssl/commit/4eb452f181c22033e5a797ae50e2d90f9fa793f2
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/asm/x86_64-gcc.c
    M crypto/bn/bn_asm.c
    M crypto/bn/bn_local.h
    M crypto/bn/bn_mul.c
    A crypto/bn/bnw_mul.c
    A crypto/bn/bnw_sub.c
    M crypto/bn/build.info

  Log Message:
  -----------
  BIGNUM: separate out word-only helper functions from bn_mul.c

This separation will allow us to use the word-only helper functions
from OSSL_FN functions without pulling in functions that operate on
BIGNUMs.

This also starts the collection of source files with word-only BN
functions that haven't found their way into bn_asm.c for various
reasons.
To recognise them, they are prefixed 'bnw_' instead of 'bn_'.

While at it, consitification is done where appropriate among words
helpers.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: bd53a3d9bb76a114c30408e01028171c87034350
      https://github.com/openssl/openssl/commit/bd53a3d9bb76a114c30408e01028171c87034350
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_local.h
    M crypto/bn/bnw_mul.c
    M doc/internal/man3/bn_mul_words.pod

  Log Message:
  -----------
  BIGNUM: add internal function 'bn_mul_truncated'

This is a variant of 'bn_mul_normal' that allows the result array to
have a smaller size than otherwise expected.

'bn_mul_normal' is refactored to be a wrapper around 'bn_mul_truncated',
with an assumed result size (which is the sum of the operands' sizes).

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: 235116c33eb6c9028dc9feeecea71b2f94e4e3a8
      https://github.com/openssl/openssl/commit/235116c33eb6c9028dc9feeecea71b2f94e4e3a8
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/fn/fn_intern.c
    M include/crypto/fn_intern.h

  Log Message:
  -----------
  OSSL_FN: constify some of the internal construction and introspection functions

This should have been done when they were added.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: 17c679fd808da87e82863466cbd2bb40b3d7ad48
      https://github.com/openssl/openssl/commit/17c679fd808da87e82863466cbd2bb40b3d7ad48
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/asn1/x_long.c
    M crypto/build.info
    A crypto/int.c
    M include/crypto/cryptlib.h

  Log Message:
  -----------
  Add ossl_num_bits(), which returns the significant number of bits in a size_t

This existed as an isolated static function in crypto/asn1/x_long.c, but
is really a pretty generic integer function, so it will serve better by
being exactly that.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: 2f93ffdd8d1bc7d27b4349c510fce8c78fd95d13
      https://github.com/openssl/openssl/commit/2f93ffdd8d1bc7d27b4349c510fce8c78fd95d13
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/fn/build.info
    M crypto/fn/fn_lib.c
    A crypto/fn/fn_mul.c
    M include/crypto/fn.h
    M test/fn_api_test.c

  Log Message:
  -----------
  OSSL_FN: Add the 'mul' function

OSSL_FN_mul() multiplies two operands.  The result OSSL_FN may be of any
size, and if it's smaller than the result of multiplying the two operands,
the result is truncated to that size.

This also adds the function OSSL_FN_copy(), a counterpart for BN_copy(),
as well as an OSSL_FN API test program.

Finally, test/fn_api_test.c is updated with a 'struct test_case_st', used
to instruct test functions what numbers to operate on, and some conditions,
and the 'add' and 'sub' test functions are upgraded to use that structure
for their input.

Related-to: doc/designs/fixed-size-large-numbers.md
Resolves: https://github.com/openssl/openssl/issues/28934

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: 2b9bd6dcf336e60998fdb0523d372591972a5555
      https://github.com/openssl/openssl/commit/2b9bd6dcf336e60998fdb0523d372591972a5555
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-11 (Thu, 11 Dec 2025)

  Changed paths:
    M crypto/bn/bn_mul.c

  Log Message:
  -----------
  OSSL_FN: Wrap BIGNUM the 'mul' function around the OSSL_FN 'mul' function

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29203)


  Commit: 2f6e92db2e9c71f9943f823b3d038f0956b67fb5
      https://github.com/openssl/openssl/commit/2f6e92db2e9c71f9943f823b3d038f0956b67fb5
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-16 (Tue, 16 Dec 2025)

  Changed paths:
    M .clang-format
    M test/fn_api_test.c

  Log Message:
  -----------
  OSSL_FN: Add the OSSL_FN_ typedefs to .clang-format's TypeNames

Furthermore, reformatting left some weird commenting in fn_api_test.c
because of (legitimate) line breaks, so they get adjust for purpose.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29412)


  Commit: 9043e50a396fab5b58435a1fb77f84ce9a8432b1
      https://github.com/openssl/openssl/commit/9043e50a396fab5b58435a1fb77f84ce9a8432b1
  Author: Richard Levitte <[email protected]>
  Date:   2025-12-17 (Wed, 17 Dec 2025)

  Changed paths:
    M crypto/fn/fn_addsub.c
    M test/fn_api_test.c

  Log Message:
  -----------
  OSSL_FN: Refactor OSSL_FN_add() and OSSL_FN_sub() for truncation

OSSL_FN_mul() set a path that wasn't considered for OSSL_FN_add() and
OSSL_FN_sub(); a truncated result if the result OSSL_FN isn't large
enough to contain the full result.

This is done to keep the OSSL_FN API consistent, with a (tentative)
bonus, that the function calls become more constant time accross
repeated calls with the same size for operands and result.

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/29309)


  Commit: dd7794c80fc6e7746e31ee70b4fa19791059e18f
      https://github.com/openssl/openssl/commit/dd7794c80fc6e7746e31ee70b4fa19791059e18f
  Author: Richard Levitte <[email protected]>
  Date:   2026-01-28 (Wed, 28 Jan 2026)

  Changed paths:
    M crypto/fn/fn_local.h
    M crypto/fn/fn_mul.c
    M test/fn_api_test.c

  Log Message:
  -----------
  Small fixups of things that somehow passed through our tests

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Fri Apr  3 12:32:17 2026
(Merged from https://github.com/openssl/openssl/pull/29819)


  Commit: 7a6da03623ecbbb0b2f4f603b98b1df590c9588d
      https://github.com/openssl/openssl/commit/7a6da03623ecbbb0b2f4f603b98b1df590c9588d
  Author: Richard Levitte <[email protected]>
  Date:   2026-03-19 (Thu, 19 Mar 2026)

  Changed paths:
    M crypto/fn/build.info
    M crypto/fn/fn_local.h
    A crypto/fn/fn_sqr.c
    M include/crypto/fn.h
    M test/fn_api_test.c

  Log Message:
  -----------
  OSSL_FN: Add the 'sqr' function

OSSL_FN_sqr() takes the square of on operand.  The result OSSL_FN may be of
any size, and if it's smaller than the result of squaring the operand, the
result is truncated to that size.

This also adds OSSL_FN_sqr() testcases in test/fn_api_test.c

Related-to: doc/designs/fixed-size-large-numbers.md
Issue: https://github.com/openssl/project/issues/1654

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Fri Apr  3 12:32:20 2026
(Merged from https://github.com/openssl/openssl/pull/29819)


  Commit: 15ec5d8b14cef7efca11cc3743c4a51bf97f169d
      https://github.com/openssl/openssl/commit/15ec5d8b14cef7efca11cc3743c4a51bf97f169d
  Author: Richard Levitte <[email protected]>
  Date:   2026-03-19 (Thu, 19 Mar 2026)

  Changed paths:
    M crypto/bn/bn_sqr.c

  Log Message:
  -----------
  OSSL_FN: Wrap the BIGNUM 'sqr' function around the OSSL_FN 'sqr' function

Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Fri Apr  3 12:32:22 2026
(Merged from https://github.com/openssl/openssl/pull/29819)


  Commit: 4f17e9e972407ed88187bb91420c5fbc82d5f917
      https://github.com/openssl/openssl/commit/4f17e9e972407ed88187bb91420c5fbc82d5f917
  Author: Igor Ustinov <[email protected]>
  Date:   2026-04-16 (Thu, 16 Apr 2026)

  Changed paths:
    M crypto/fn/fn_lib.c
    M crypto/fn/fn_sqr.c
    M include/crypto/fn.h

  Log Message:
  -----------
  Added OSSL_FN_truncate() function, which allows to copy a longer number
to a shorter one, truncating the high bytes.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
MergeDate: Fri Apr 17 14:09:28 2026
(Merged from https://github.com/openssl/openssl/pull/30846)


  Commit: 22c18b12a9baf2eea6c95ce0535bf8de4adf26a6
      https://github.com/openssl/openssl/commit/22c18b12a9baf2eea6c95ce0535bf8de4adf26a6
  Author: Igor Ustinov <[email protected]>
  Date:   2026-04-17 (Fri, 17 Apr 2026)

  Changed paths:
    M crypto/bn/bnw_mul.c
    M crypto/fn/fn_addsub.c
    M crypto/fn/fn_ctx.c
    M crypto/fn/fn_lib.c
    M include/crypto/fn.h
    M test/fn_api_test.c
    M test/fn_internal_test.c

  Log Message:
  -----------
  Some bugfixes for OSSL_FN:
- Fixed the stage 2 exit criteria in OSSL_FN_add() and OSSL_FN_sub().
- Fixed the calculation of a-b when a is shorter than b and borrow==1
  after stage 1.
- Added sanity check in OSSL_FN_CTX_end().
- Do not set the is_dynamically_allocated flag when getting a
  new OSSL_FN from OSSL_FN_CTX.
- Added units of measurement to the error message in the OSSL_FN_copy()
  function.
- is_dynamically_allocated and is_securely_allocated shouldn't be
  copied in OSSL_BN_copy().
- Fixed the comment in OSSL_FN_add().
- Added a comment to bn_mul_truncated() that it is a duty of the calling
  function to zero out the rest of r in case of rn>an+bn.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
MergeDate: Fri Apr 17 16:02:44 2026
(Merged from https://github.com/openssl/openssl/pull/30833)


  Commit: f811a2894a94d3a9b4531bcda3a176c4cf3ca1fd
      https://github.com/openssl/openssl/commit/f811a2894a94d3a9b4531bcda3a176c4cf3ca1fd
  Author: Igor Ustinov <[email protected]>
  Date:   2026-04-30 (Thu, 30 Apr 2026)

  Changed paths:
    M crypto/fn/fn_ctx.c
    M crypto/fn/fn_mul.c
    M crypto/fn/fn_sqr.c
    M include/crypto/fn.h
    M test/fn_internal_test.c

  Log Message:
  -----------
  Add an ownership token for OSSL_FN_CTX frames

This prevents ending a frame that wasn't started by the caller.

Reviewed-by: Richard Levitte <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Thu Apr 30 09:18:52 2026
(Merged from https://github.com/openssl/openssl/pull/30886)


  Commit: fdd0934158fc33cdfdf18b4971d590779dc6aad9
      https://github.com/openssl/openssl/commit/fdd0934158fc33cdfdf18b4971d590779dc6aad9
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-11 (Thu, 11 Jun 2026)

  Changed paths:
    M crypto/bn/bn_add.c

  Log Message:
  -----------
  Use top to size BN add/sub results

Size the acquired result OSSL_FN from the significant operand limbs instead of
the allocated operand width. This keeps BN_uadd() and BN_usub() consistent
with the other wrappers that already use top for result sizing.

This follows a realization that applying OSSL_FN semantics onto BIGNUM too much
is pointless, and as demonstrated with BN_mul(), may be harmful, especially when
the result BIGNUM as the same as one of the operands.

Assisted-by: Pi:openai/gpt-5.5
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Igor Ustinov <[email protected]>
MergeDate: Fri Jun 12 15:16:33 2026
(Merged from https://github.com/openssl/openssl/pull/31448)


  Commit: c8edda8b2863fed9ebd71de1bee35700638553ff
      https://github.com/openssl/openssl/commit/c8edda8b2863fed9ebd71de1bee35700638553ff
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-11 (Thu, 11 Jun 2026)

  Changed paths:
    M crypto/bn/bn_mul.c

  Log Message:
  -----------
  Acquire BN_mul result before ctx sizing

Acquire the writable result before reading operand dmax values for the
OSSL_FN_CTX arena size. This avoids stale context sizing when the result
aliases an operand and acquisition expands that BIGNUM.

Assisted-by: Pi:openai/gpt-5.5
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Igor Ustinov <[email protected]>
MergeDate: Fri Jun 12 15:16:34 2026
(Merged from https://github.com/openssl/openssl/pull/31448)


  Commit: 7190474bc7edf56c1d6f3d94059015991c9d18f1
      https://github.com/openssl/openssl/commit/7190474bc7edf56c1d6f3d94059015991c9d18f1
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-11 (Thu, 11 Jun 2026)

  Changed paths:
    M crypto/bn/bn_sqr.c

  Log Message:
  -----------
  Acquire BN_sqr result before ctx sizing

Acquire the writable result before reading the operand dmax value for the
OSSL_FN_CTX arena size. This avoids stale context sizing when the result
aliases the operand and acquisition expands that BIGNUM.

Assisted-by: Pi:openai/gpt-5.5
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Igor Ustinov <[email protected]>
MergeDate: Fri Jun 12 15:16:36 2026
(Merged from https://github.com/openssl/openssl/pull/31448)


  Commit: cc104b651a176a117bb2c7dcb4ac72db76f8a12d
      https://github.com/openssl/openssl/commit/cc104b651a176a117bb2c7dcb4ac72db76f8a12d
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M crypto/fn/fn_local.h
    M test/fn_internal_test.c

  Log Message:
  -----------
  Add OSSL_FN_BITS, use it instead of 'OSSL_FN_BYTES * 8'

Reviewed-by: Dmitry Belyavskiy <[email protected]>
Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
MergeDate: Sun Jun 14 08:35:04 2026
(Merged from https://github.com/openssl/openssl/pull/31452)


  Commit: b4847e73de6ec9adc54c18b477da7b027b1e54e4
      https://github.com/openssl/openssl/commit/b4847e73de6ec9adc54c18b477da7b027b1e54e4
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M include/crypto/fn.h

  Log Message:
  -----------
  Document OSSL_FN_copy zero-padding of oversized destination limbs

The OSSL_FN_copy() function already zeroes any high destination limbs
beyond the source size, but this was not stated in the public header.
Add an explicit note so callers know unused high bytes are cleared.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Dmitry Belyavskiy <[email protected]>
Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
MergeDate: Sun Jun 14 08:35:06 2026
(Merged from https://github.com/openssl/openssl/pull/31452)


  Commit: 2108bf6891cdc04549fab54e99cded7da072a609
      https://github.com/openssl/openssl/commit/2108bf6891cdc04549fab54e99cded7da072a609
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M crypto/fn/fn_ctx.c
    M include/crypto/fn.h

  Log Message:
  -----------
  Add OSSL_FN_CTX_peak_usage() for arena usage instrumentation

Track the maximum number of frames, numbers and limbs ever consumed
from an OSSL_FN_CTX arena during its lifetime.  This allows callers
to empirically size their contexts instead of predicting precise
max_n_frames / max_n_numbers / max_n_limbs ahead of time.

These peak usages are updated whenever a frame is started or an OSSL_FN
is obtained.  They never decrease, reflecting the worst-case simultaneous
allocation across all nested frames.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Sun Jun 14 08:40:38 2026
(Merged from https://github.com/openssl/openssl/pull/31431)


  Commit: 0228b1303f0667f8e8b7a612cb7134d31b6e44d7
      https://github.com/openssl/openssl/commit/0228b1303f0667f8e8b7a612cb7134d31b6e44d7
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M test/fn_internal_test.c

  Log Message:
  -----------
  Add test for OSSL_FN_CTX_peak_usage

Verify that peak_usage starts at zero, rises after frame start and
allocation, and never decreases after a frame is ended.

Also correct misleading comments that claimed OSSL_FN_CTX_new()
takes a size in bytes -- the last parameter is actually max_n_limbs.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Sun Jun 14 08:40:40 2026
(Merged from https://github.com/openssl/openssl/pull/31431)


  Commit: 1bd960ea71fd55606ee51422f70327aa5c49eb9f
      https://github.com/openssl/openssl/commit/1bd960ea71fd55606ee51422f70327aa5c49eb9f
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M crypto/fn/fn_ctx.c
    M crypto/fn/fn_local.h

  Log Message:
  -----------
  Expose OSSL_FN_CTX internals via fn_local.h

Move struct ossl_fn_ctx_st and struct ossl_fn_ctx_frame_st from
fn_ctx.c to fn_local.h, making them accessible to internal code in
the bn/ directory and elsewhere.  Also expose
ossl_fn_ctx_calculate_arena_size() as an inline helper so callers
can determine the required arena size without duplicating the formula.

This is groundwork for BN_CTX integration.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Sun Jun 14 08:40:42 2026
(Merged from https://github.com/openssl/openssl/pull/31431)


  Commit: 4fec2aafd89c73889f320b054fbf76818bb38b25
      https://github.com/openssl/openssl/commit/4fec2aafd89c73889f320b054fbf76818bb38b25
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M crypto/bn/bn_ctx.c
    M include/crypto/bn.h
    M test/bn_internal_test.c

  Log Message:
  -----------
  Add OSSL_FN_CTX integration with BN_CTX

Add an OSSL_FN_CTX * pointer to struct bignum_ctx, allowing a BN_CTX
to cache an OSSL_FN_CTX for use by BIGNUM wrapper functions.  The
pointer is freed automatically when BN_CTX_free() is called.

Also add bn_ctx_acquire_ossl_fn_ctx() and bn_ctx_release_ossl_fn_ctx():
- acquire creates (or reuses if large enough) an OSSL_FN_CTX inside
  the BN_CTX, sizing it according to the caller's needs.
- release checks that no frames remain in the cached OSSL_FN_CTX.

The acquire function respects BN_FLG_SECURE: if the BN_CTX was created
with BN_CTX_secure_new(), the OSSL_FN_CTX is also allocated in secure
memory.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Sun Jun 14 08:40:44 2026
(Merged from https://github.com/openssl/openssl/pull/31431)


  Commit: e183d91627bf433a15df2ad249dfd8a7ed248649
      https://github.com/openssl/openssl/commit/e183d91627bf433a15df2ad249dfd8a7ed248649
  Author: Richard Levitte <[email protected]>
  Date:   2026-06-14 (Sun, 14 Jun 2026)

  Changed paths:
    M crypto/bn/bn_mul.c
    M crypto/bn/bn_sqr.c

  Log Message:
  -----------
  Use BN_CTX cached OSSL_FN_CTX in BN_mul() and BN_sqr()

The previous commit added bn_ctx_acquire_ossl_fn_ctx() and
bn_ctx_release_ossl_fn_ctx() to cache an OSSL_FN_CTX inside a BN_CTX.
However, BN_mul() and BN_sqr() were still creating and freeing their own
temporary OSSL_FN_CTX on every call, ignoring the passed-in BN_CTX entirely.

Update both functions to acquire the OSSL_FN_CTX from the BN_CTX and release
it afterwards, allowing the cached context to be reused across calls.

Assisted-by: Pi:moonshotai/kimi-k2.6
Signed-off-by: Richard Levitte <[email protected]>

Reviewed-by: Igor Ustinov <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
MergeDate: Sun Jun 14 08:40:47 2026
(Merged from https://github.com/openssl/openssl/pull/31431)


Compare: https://github.com/openssl/openssl/compare/dbf9025bc283...e183d91627bf

To unsubscribe from these emails, change your notification settings at https://github.com/openssl/openssl/settings/notifications

-- 
You received this message because you are subscribed to the Google Groups "openssl-commits" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/a/openssl.org/d/msgid/openssl-commits/openssl/openssl/push/refs/heads/feature/ossl_fn/dbf902-e183d9%40github.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.