1.\" $OpenBSD: BN_swap.3,v 1.6 2021/12/19 22:06:35 schwarze Exp $ 2.\" full merge up to: OpenSSL 61f805c1 Jan 16 01:01:46 2018 +0800 3.\" 4.\" This file is a derived work. 5.\" The changes are covered by the following Copyright and license: 6.\" 7.\" Copyright (c) 2021 Ingo Schwarze <schwarze@openbsd.org> 8.\" 9.\" Permission to use, copy, modify, and distribute this software for any 10.\" purpose with or without fee is hereby granted, provided that the above 11.\" copyright notice and this permission notice appear in all copies. 12.\" 13.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20.\" 21.\" The original file was written by Bodo Moeller <bodo@openssl.org>. 22.\" Copyright (c) 2000 The OpenSSL Project. All rights reserved. 23.\" 24.\" Redistribution and use in source and binary forms, with or without 25.\" modification, are permitted provided that the following conditions 26.\" are met: 27.\" 28.\" 1. Redistributions of source code must retain the above copyright 29.\" notice, this list of conditions and the following disclaimer. 30.\" 31.\" 2. Redistributions in binary form must reproduce the above copyright 32.\" notice, this list of conditions and the following disclaimer in 33.\" the documentation and/or other materials provided with the 34.\" distribution. 35.\" 36.\" 3. All advertising materials mentioning features or use of this 37.\" software must display the following acknowledgment: 38.\" "This product includes software developed by the OpenSSL Project 39.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 40.\" 41.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 42.\" endorse or promote products derived from this software without 43.\" prior written permission. For written permission, please contact 44.\" openssl-core@openssl.org. 45.\" 46.\" 5. Products derived from this software may not be called "OpenSSL" 47.\" nor may "OpenSSL" appear in their names without prior written 48.\" permission of the OpenSSL Project. 49.\" 50.\" 6. Redistributions of any form whatsoever must retain the following 51.\" acknowledgment: 52.\" "This product includes software developed by the OpenSSL Project 53.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 54.\" 55.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 56.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 58.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 59.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 60.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 61.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 62.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 66.\" OF THE POSSIBILITY OF SUCH DAMAGE. 67.\" 68.Dd $Mdocdate: December 19 2021 $ 69.Dt BN_SWAP 3 70.Os 71.Sh NAME 72.Nm BN_swap , 73.Nm BN_consttime_swap 74.Nd exchange BIGNUMs 75.Sh SYNOPSIS 76.In openssl/bn.h 77.Ft void 78.Fo BN_swap 79.Fa "BIGNUM *a" 80.Fa "BIGNUM *b" 81.Fc 82.Ft void 83.Fo BN_consttime_swap 84.Fa "BN_ULONG condition" 85.Fa "BIGNUM *a" 86.Fa "BIGNUM *b" 87.Fa "int nwords" 88.Fc 89.Sh DESCRIPTION 90.Fn BN_swap 91and 92.Fn BN_consttime_swap 93exchange the values of 94.Fa a 95and 96.Fa b . 97.Pp 98.Fn BN_swap 99implements this by exchanging the pointers to the data buffers of 100.Fa a 101and 102.Fa b 103and also exchanging the values of the 104.Dv BN_FLG_STATIC_DATA 105bits. 106Consequently, the operation is fast and execution time does not depend 107on any properties of the two numbers. 108However, execution time obviously differs between swapping (by calling 109this function) and not swapping (by not calling this function). 110.Pp 111.Fn BN_consttime_swap 112only performs the exchange if the 113.Fa condition 114is non-zero; otherwise, it has no effect. 115It implements the exchange by exchanging the contents of the data 116buffers rather than the pointers to the data buffers. 117This is slower, but implemented in such a way that the execution time 118is not only independent of the properties of the two numbers, but also 119independent of the 120.Fa condition 121argument, i.e. the same for swapping or not swapping. 122Execution time does however grow in an approximately linear manner with the 123.Fa nwords 124argument. 125.Pp 126.Fn BN_consttime_swap 127calls 128.Xr abort 3 129if at least one of 130.Fa a 131or 132.Fa b 133has fewer than 134.Fa nwords 135data words allocated or more than 136.Fa nwords 137data words are currently in use in at least one of them. 138.Sh SEE ALSO 139.Xr BN_new 3 , 140.Xr BN_set_flags 3 141.Sh HISTORY 142.Fn BN_swap 143first appeared in OpenSSL 0.9.7 and has been available since 144.Ox 3.2 . 145.Pp 146.Fn BN_consttime_swap 147first appeared in OpenSSL 1.0.1g and has been available since 148.Ox 5.6 . 149