-.\" $OpenBSD: BN_mod_mul_montgomery.3,v 1.13 2021/12/06 19:45:27 schwarze Exp $
-.\" OpenSSL 6859cf74 Sep 25 13:33:28 2002 +0000
+.\" $OpenBSD: BN_mod_mul_montgomery.3,v 1.14 2021/12/21 11:14:07 schwarze Exp $
+.\" full merge up to: OpenSSL 6859cf74 Sep 25 13:33:28 2002 +0000
+.\" selective merge up to: OpenSSL 24a535ea Sep 22 13:14:20 2020 +0100
.\"
-.\" This file was written by Ulf Moeller <ulf@openssl.org>.
+.\" This file is a derived work.
+.\" The changes are covered by the following Copyright and license:
+.\"
+.\" Copyright (c) 2021 Ingo Schwarze <schwarze@openbsd.org>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.\" The original file was written by Ulf Moeller <ulf@openssl.org>.
.\" Copyright (c) 2000 The OpenSSL Project. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd $Mdocdate: December 6 2021 $
+.Dd $Mdocdate: December 21 2021 $
.Dt BN_MOD_MUL_MONTGOMERY 3
.Os
.Sh NAME
.Nm BN_MONT_CTX_init ,
.Nm BN_MONT_CTX_free ,
.Nm BN_MONT_CTX_set ,
+.Nm BN_MONT_CTX_set_locked ,
.Nm BN_MONT_CTX_copy ,
.Nm BN_mod_mul_montgomery ,
.Nm BN_from_montgomery ,
.Fa "BN_CTX *ctx"
.Fc
.Ft BN_MONT_CTX *
+.Fo BN_MONT_CTX_set_locked
+.Fa "BN_MONT_CTX **pmont"
+.Fa "int lock"
+.Fa "const BIGNUM *m"
+.Fa "BN_CTX *ctx"
+.Fc
+.Ft BN_MONT_CTX *
.Fo BN_MONT_CTX_copy
.Fa "BN_MONT_CTX *to"
.Fa "BN_MONT_CTX *from"
.Fa m
by precomputing its inverse and a value R.
.Pp
+.Fn BN_MONT_CTX_set_locked
+is a wrapper around
+.Fn BN_MONT_CTX_new
+and
+.Fn BN_MONT_CTX_set
+that is useful if more than one thread intends to use the same
+.Vt BN_MONT_CTX
+and none of these threads is exclusively responsible for creating
+and initializing the context.
+.Fn BN_MONT_CTX_set_locked
+first acquires the specified
+.Fa lock
+using
+.Xr CRYPTO_lock 3 .
+If
+.Pf * Fa pmont
+already differs from
+.Dv NULL ,
+no action occurs.
+Otherwise, a new
+.Vt BN_MONT_CTX
+is allocated with
+.Fn BN_MONT_CTX_new ,
+set up with
+.Fn BN_MONT_CTX_set ,
+and a pointer to it is stored in
+.Pf * Fa pmont .
+Finally, the
+.Fa lock
+is released.
+.Pp
.Fn BN_MONT_CTX_copy
copies the
.Vt BN_MONT_CTX
.Dv NULL
on error.
.Pp
+.Fn BN_MONT_CTX_set_locked
+returns a pointer to the existing or newly created context or
+.Dv NULL
+on error.
+.Pp
For the other functions, 1 is returned for success or 0 on error.
The error codes can be obtained by
.Xr ERR_get_error 3 .
.Sh SEE ALSO
.Xr BN_add 3 ,
.Xr BN_CTX_new 3 ,
-.Xr BN_new 3
+.Xr BN_new 3 ,
+.Xr CRYPTO_lock 3
.Sh HISTORY
.Fn BN_MONT_CTX_new ,
.Fn BN_MONT_CTX_free ,
.Fn BN_MONT_CTX_copy
first appeared in SSLeay 0.9.1 and have been available since
.Ox 2.6 .
+.Pp
+.Fn BN_MONT_CTX_set_locked
+first appeared in OpenSSL 0.9.8 and has been available since
+.Ox 4.0 .
.Sh CAVEATS
.Fn BN_MONT_CTX_init
must not be called on a context that was used previously, or